6 /* original in mysql, strings/strnlen.c.
7 uint strnlen(register const char *s, register uint maxlen)
9 const char *end= (const char *)memchr(s, '\0', maxlen);
10 return end ? (uint) (end - s) : maxlen;
14 strnlen (register const char *s, register int maxlen)
16 const char *end= (const char *)memchr(s, '\0', maxlen);
17 return end ? (int) (end - s) : maxlen;
23 typedef DB CHISE_Attribute_Table;
25 CHISE_Attribute_Table*
26 chise_open_attribute_table (const unsigned char *db_dir,
27 const char *encoding, const char *feature,
29 u_int32_t accessmask, int modemask);
31 int chise_close_attribute_table (CHISE_Attribute_Table *db);
33 int chise_get_attribute_table (CHISE_Attribute_Table *db,
34 char *key, CHISE_Value *valdatum);
36 int chise_put_attribute_table (CHISE_Attribute_Table *db,
37 char *key, unsigned char *value);
40 #define xzero(lvalue) ((void) memset (&(lvalue), '\0', sizeof (lvalue)))
43 chise_char_id_parse_c_string (unsigned char *str, int len)
47 if ( (len >= 2) && (str[i++] == '?') )
49 unsigned char c = str[i++];
66 return c & (0x80 | 0x1F);
100 if (counter + 2 <= len)
104 for (j = 0; j < counter; j++)
105 cid = (cid << 6) | (str[j + i] & 0x3F);
113 chise_format_char_id (CHISE_Char_ID cid, unsigned char *dest, int len)
125 else if (cid == '\n')
132 else if (cid == '\r')
139 else if (cid == 0x1C)
148 else if (cid <= 0x1F)
152 dest[i++] = '@' + cid;
156 else if ( (cid == ' ') || (cid == '"') ||
157 (cid == '#') || (cid == '\'') ||
158 (cid == '(') || (cid == ')') ||
159 (cid == ',') || (cid == '.') ||
160 (cid == ';') || (cid == '?') ||
161 (cid == '[') || (cid == '\\') ||
162 (cid == ']') || (cid == '`') )
169 else if (cid <= 0x7E)
175 else if (cid == 0x7F)
183 else if (cid <= 0x9F)
187 dest[i++] = ((cid + '@') >> 6) | 0xC0;
188 dest[i++] = ((cid + '@') & 0x3F) | 0x80;
192 else if (cid <= 0x7FF)
194 dest[i++] = (cid >> 6) | 0xC0;
195 dest[i++] = (cid & 0x3F) | 0x80;
199 else if (cid <= 0xFFFF)
201 dest[i++] = (cid >> 12) | 0xE0;
202 dest[i++]= ((cid >> 6) & 0x3F) | 0x80;
203 dest[i++]= (cid & 0x3F) | 0x80;
207 else if (cid <= 0x1FFFFF)
209 dest[i++]= (cid >> 18) | 0xF0;
210 dest[i++]= ((cid >> 12) & 0x3F) | 0x80;
211 dest[i++]= ((cid >> 6) & 0x3F) | 0x80;
212 dest[i++]= (cid & 0x3F) | 0x80;
216 else if (cid <= 0x3FFFFFF)
218 dest[i++]= (cid >> 24) | 0xF8;
219 dest[i++]= ((cid >> 18) & 0x3F) | 0x80;
220 dest[i++]= ((cid >> 12) & 0x3F) | 0x80;
221 dest[i++]= ((cid >> 6) & 0x3F) | 0x80;
222 dest[i++]= (cid & 0x3F) | 0x80;
228 dest[i++]= (cid >> 30) | 0xFC;
229 dest[i++]= ((cid >> 24) & 0x3F) | 0x80;
230 dest[i++]= ((cid >> 18) & 0x3F) | 0x80;
231 dest[i++]= ((cid >> 12) & 0x3F) | 0x80;
232 dest[i++]= ((cid >> 6) & 0x3F) | 0x80;
233 dest[i++]= (cid & 0x3F) | 0x80;
243 unsigned char *location;
247 chise_open_data_source (CHISE_DS_Type type, char *location)
249 CHISE_DS *ds = (CHISE_DS*)malloc (sizeof (CHISE_DS));
250 size_t len = strlen (location);
256 ds->location = (unsigned char*)malloc (len + 1);
257 if (ds->location == NULL)
262 strcpy (ds->location, location);
267 chise_ds_close (CHISE_DS *ds)
269 if (ds->location != NULL)
276 struct CHISE_Decoding_Table
279 CHISE_Attribute_Table *db;
282 CHISE_Decoding_Table*
283 chise_ds_open_decoding_table (CHISE_DS *ds, const char *ccs,
285 u_int32_t accessmask, int modemask)
287 CHISE_Decoding_Table* table;
292 table = (CHISE_Decoding_Table*)malloc (sizeof (CHISE_Decoding_Table));
297 table->db = chise_open_attribute_table (ds->location,
298 ccs, "system-char-id",
300 accessmask, modemask);
301 if (table->db == NULL)
310 chise_dt_close (CHISE_Decoding_Table *table)
317 if (table->db == NULL)
320 status = chise_close_attribute_table (table->db);
326 chise_dt_get_char (CHISE_Decoding_Table *table, int code_point)
328 CHISE_Value valdatum;
332 sprintf(key_buf, "%d", code_point);
333 status = chise_get_attribute_table (table->db, key_buf, &valdatum);
337 = (unsigned char *)chise_value_data (&valdatum);
338 int len = strnlen (str, chise_value_size (&valdatum));
340 return chise_char_id_parse_c_string (str, len);
346 chise_dt_put_char (CHISE_Decoding_Table *table,
347 int code_point, CHISE_Char_ID cid)
349 CHISE_Value valdatum;
350 char key_buf[16], val_buf[8];
352 sprintf(key_buf, "%d", code_point);
353 chise_format_char_id (cid, val_buf, 8);
354 return chise_put_attribute_table (table->db, key_buf, val_buf);
358 struct CHISE_Feature_Table
361 CHISE_Attribute_Table *db;
365 chise_ds_open_feature_table (CHISE_DS *ds, const char *feature,
367 u_int32_t accessmask, int modemask)
369 CHISE_Feature_Table* table;
374 table = (CHISE_Feature_Table*)malloc (sizeof (CHISE_Feature_Table));
379 table->db = chise_open_attribute_table (ds->location,
380 "system-char-id", feature,
382 accessmask, modemask);
383 if (table->db == NULL)
392 chise_ft_close (CHISE_Feature_Table *table)
399 if (table->db == NULL)
402 status = chise_close_attribute_table (table->db);
408 chise_ft_get_value (CHISE_Feature_Table *table,
409 CHISE_Char_ID cid, CHISE_Value *valdatum)
411 unsigned char key_buf[8];
413 chise_format_char_id (cid, key_buf, 8);
414 return chise_get_attribute_table (table->db,
419 chise_ft_iterate (CHISE_Feature_Table *table,
420 int (*func) (CHISE_Feature_Table *table,
421 CHISE_Char_ID cid, CHISE_Value *valdatum))
423 DBT keydatum, valdatum;
430 status = table->db->cursor (table->db, NULL, &dbcp, 0);
431 for (status = dbcp->c_get (dbcp, &keydatum, &valdatum, DB_FIRST);
433 status = dbcp->c_get (dbcp, &keydatum, &valdatum, DB_NEXT))
435 unsigned char *key_str = (unsigned char *)keydatum.data;
436 int key_len = strnlen (key_str, keydatum.size);
437 CHISE_Char_ID key = chise_char_id_parse_c_string (key_str, key_len);
440 if (ret = func (table, key, &valdatum))
443 dbcp->c_close (dbcp);
446 CHISE_Attribute_Table*
447 chise_open_attribute_table (const unsigned char *db_dir,
448 const char *encoding, const char *feature,
450 u_int32_t accessmask, int modemask)
456 char *db_file_name, *sp;
458 status = db_create (&dbase, NULL, 0);
462 len = strlen (db_dir);
463 flen = strlen (feature);
464 size = len + strlen (encoding) + flen * 3 + 4;
465 db_file_name = alloca (size);
466 strcpy (db_file_name, db_dir);
467 if (db_file_name[len - 1] != '/')
469 db_file_name[len++] = '/';
470 db_file_name[len] = '\0';
472 strcat (db_file_name, encoding);
473 strcat (db_file_name, "/");
474 /* strcat (db_file_name, feature); */
475 sp = &db_file_name[strlen (db_file_name)];
476 for (i = 0; i < flen; i++)
480 if ( (c == '/') || (c == '%') )
482 sprintf (sp, "%%%02X", c);
489 status = dbase->open (dbase, db_file_name, NULL,
490 real_subtype, accessmask, modemask);
493 dbase->close (dbase, 0);
500 chise_close_attribute_table (CHISE_Attribute_Table *db)
511 chise_get_attribute_table (CHISE_Attribute_Table *db,
512 char *key, CHISE_Value *valdatum)
517 /* DB Version 2 requires DBT's to be zeroed before use. */
522 keydatum.size = strlen (key);
524 status = db->get (db, NULL, &keydatum, valdatum, 0);
529 chise_put_attribute_table (CHISE_Attribute_Table *db,
530 char *key, unsigned char *value)
532 DBT keydatum, valdatum;
535 /* DB Version 2 requires DBT's to be zeroed before use. */
540 keydatum.size = strlen (key);
542 valdatum.data = value;
543 valdatum.size = strlen (value);
545 status = db->put (db, NULL, &keydatum, &valdatum, 0);