- int hash_idx, *hashp;
- unsigned char *buf;
- int embedded_rule; /* 1 if composition rule is embedded. */
- int chars; /* number of components. */
- int i;
- struct cmpchar_info *cmpcharp;
-
- /* The second byte 0xFF means compostion rule is embedded. */
- embedded_rule = (str[1] == 0xFF);
-
- /* At first, get the actual length of the composite character. */
- {
- const unsigned char *p, *endp = str + 1, *lastp = str + len;
- int bytes;
-
- while (endp < lastp && ! CHAR_HEAD_P (*endp)) endp++;
- if (endp - str < 5)
- /* Any composite char have at least 5-byte length. */
- return -1;
-
- chars = 0;
- p = str + 1;
- while (p < endp)
- {
- if (embedded_rule) p++;
- /* No need of checking if *P is 0xA0 because
- BYTES_BY_CHAR_HEAD (0x80) surely returns 2. */
- p += BYTES_BY_CHAR_HEAD (*p - 0x20);
- chars++;
- }
- if (p > endp || chars < 2 || chars > MAX_COMPONENT_COUNT)
- /* Invalid components. */
- return -1;
- len = p - str;
- }
- hash_idx = hash_string (str, len) % CMPCHAR_HASH_TABLE_SIZE;
- hashp = cmpchar_hash_table[hash_idx];
-
- /* Then, look into the hash table. */
- if (hashp != NULL)
- /* Find the correct one among composite characters of the same
- hash value. */
- for (i = 2; i < CMPCHAR_HASH_USED (hashp); i++)
- {
- cmpcharp = cmpchar_table[CMPCHAR_HASH_CMPCHAR_ID (hashp, i)];
- if (len == cmpcharp->len
- && ! bcmp (str, cmpcharp->data, len))
- return CMPCHAR_HASH_CMPCHAR_ID (hashp, i);
- }
-
- /* We have to register the composite character in cmpchar_table. */
- if (n_cmpchars > (CHAR_FIELD2_MASK | CHAR_FIELD3_MASK))
- /* No, we have no more room for a new composite character. */
- return -1;
-
- /* Make the entry in hash table. */
- if (hashp == NULL)
- {
- /* Make a table for 8 composite characters initially. */
- hashp = (cmpchar_hash_table[hash_idx]
- = (int *) xmalloc (sizeof (int) * (2 + 8)));
- CMPCHAR_HASH_SIZE (hashp) = 10;
- CMPCHAR_HASH_USED (hashp) = 2;
- }
- else if (CMPCHAR_HASH_USED (hashp) >= CMPCHAR_HASH_SIZE (hashp))
- {
- CMPCHAR_HASH_SIZE (hashp) += 8;
- hashp = (cmpchar_hash_table[hash_idx]
- = (int *) xrealloc (hashp,
- sizeof (int) * CMPCHAR_HASH_SIZE (hashp)));
- }
- CMPCHAR_HASH_CMPCHAR_ID (hashp, CMPCHAR_HASH_USED (hashp)) = n_cmpchars;
- CMPCHAR_HASH_USED (hashp)++;