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;
22 #include "chise-name.h"
26 chise_ds_open_feature_table (CHISE_DS *ds, const char *feature);
28 int chise_ft_close (CHISE_Feature_Table *table);
31 chise_ds_open_ccs_table (CHISE_DS *ds, const char *ccs);
33 int chise_ccst_close (CHISE_CCS_Table *table);
36 typedef DB CHISE_Attribute_Table;
38 CHISE_Attribute_Table*
39 chise_open_attribute_table (const unsigned char *db_dir,
40 const char *encoding, const char *feature,
42 u_int32_t accessmask, int modemask);
44 int chise_close_attribute_table (CHISE_Attribute_Table *db);
46 int chise_get_attribute_table (CHISE_Attribute_Table *db,
47 char *key, CHISE_Value *valdatum);
49 int chise_put_attribute_table (CHISE_Attribute_Table *db,
50 char *key, unsigned char *value);
53 #define xzero(lvalue) ((void) memset (&(lvalue), '\0', sizeof (lvalue)))
56 chise_char_id_parse_c_string (unsigned char *str, int len)
60 if ( (len >= 2) && (str[i++] == '?') )
62 unsigned char c = str[i++];
79 return c & (0x80 | 0x1F);
113 if (counter + 2 <= len)
117 for (j = 0; j < counter; j++)
118 cid = (cid << 6) | (str[j + i] & 0x3F);
126 chise_format_char_id (CHISE_Char_ID cid, unsigned char *dest, int len)
138 else if (cid == '\n')
145 else if (cid == '\r')
152 else if (cid == 0x1C)
161 else if (cid <= 0x1F)
165 dest[i++] = '@' + cid;
169 else if ( (cid == ' ') || (cid == '"') ||
170 (cid == '#') || (cid == '\'') ||
171 (cid == '(') || (cid == ')') ||
172 (cid == ',') || (cid == '.') ||
173 (cid == ';') || (cid == '?') ||
174 (cid == '[') || (cid == '\\') ||
175 (cid == ']') || (cid == '`') )
182 else if (cid <= 0x7E)
188 else if (cid == 0x7F)
196 else if (cid <= 0x9F)
200 dest[i++] = ((cid + '@') >> 6) | 0xC0;
201 dest[i++] = ((cid + '@') & 0x3F) | 0x80;
205 else if (cid <= 0x7FF)
207 dest[i++] = (cid >> 6) | 0xC0;
208 dest[i++] = (cid & 0x3F) | 0x80;
212 else if (cid <= 0xFFFF)
214 dest[i++] = (cid >> 12) | 0xE0;
215 dest[i++]= ((cid >> 6) & 0x3F) | 0x80;
216 dest[i++]= (cid & 0x3F) | 0x80;
220 else if (cid <= 0x1FFFFF)
222 dest[i++]= (cid >> 18) | 0xF0;
223 dest[i++]= ((cid >> 12) & 0x3F) | 0x80;
224 dest[i++]= ((cid >> 6) & 0x3F) | 0x80;
225 dest[i++]= (cid & 0x3F) | 0x80;
229 else if (cid <= 0x3FFFFFF)
231 dest[i++]= (cid >> 24) | 0xF8;
232 dest[i++]= ((cid >> 18) & 0x3F) | 0x80;
233 dest[i++]= ((cid >> 12) & 0x3F) | 0x80;
234 dest[i++]= ((cid >> 6) & 0x3F) | 0x80;
235 dest[i++]= (cid & 0x3F) | 0x80;
241 dest[i++]= (cid >> 30) | 0xFC;
242 dest[i++]= ((cid >> 24) & 0x3F) | 0x80;
243 dest[i++]= ((cid >> 18) & 0x3F) | 0x80;
244 dest[i++]= ((cid >> 12) & 0x3F) | 0x80;
245 dest[i++]= ((cid >> 6) & 0x3F) | 0x80;
246 dest[i++]= (cid & 0x3F) | 0x80;
256 unsigned char *location;
257 CHISE_NAME_TABLE* feature_names;
258 CHISE_NAME_TABLE* ccs_names;
264 chise_open_data_source (CHISE_DS_Type type, char *location,
265 DBTYPE subtype, int modemask)
267 CHISE_DS *ds = (CHISE_DS*)malloc (sizeof (CHISE_DS));
268 size_t len = strlen (location);
274 ds->subtype = subtype;
275 ds->modemask = modemask;
276 ds->location = (unsigned char*)malloc (len + 1);
277 if (ds->location == NULL)
282 strcpy (ds->location, location);
284 ds->feature_names = chise_make_name_table ();
285 if (ds->feature_names == NULL)
291 ds->ccs_names = chise_make_name_table ();
292 if (ds->ccs_names == NULL)
294 free (ds->feature_names);
302 chise_ds_close (CHISE_DS *ds)
304 if (ds->location != NULL)
306 if (ds->feature_names != NULL)
307 free (ds->feature_names);
308 if (ds->ccs_names != NULL)
309 free (ds->ccs_names);
315 chise_ds_get_feature (CHISE_DS *ds, const unsigned char *feature)
317 CHISE_Feature_Table* ft;
319 ft = chise_name_table_get (ds->feature_names, feature);
323 ft = chise_ds_open_feature_table (ds, feature);
327 if (chise_name_table_put (ds->feature_names, feature, ft))
336 chise_ds_get_ccs (CHISE_DS *ds, const unsigned char *ccs)
340 ct = chise_name_table_get (ds->ccs_names, ccs);
344 ct = chise_ds_open_ccs_table (ds, ccs);
348 if (chise_name_table_put (ds->ccs_names, ccs, ct))
350 chise_ccst_close (ct);
357 struct CHISE_Feature_Table
361 CHISE_Attribute_Table *db;
366 chise_ds_open_feature_table (CHISE_DS *ds, const char *feature)
368 CHISE_Feature_Table* table;
369 size_t len = strlen (feature);
374 table = (CHISE_Feature_Table*)malloc (sizeof (CHISE_Feature_Table));
381 table->name = (unsigned char*)malloc (len + 1);
382 if (table->name == NULL)
387 strcpy (table->name, feature);
392 chise_ft_close (CHISE_Feature_Table *table)
399 if (table->db == NULL)
402 status = chise_close_attribute_table (table->db);
404 if (table->name == NULL)
416 chise_char_load_feature_value (CHISE_Char_ID cid,
417 CHISE_Feature_Table *table,
418 CHISE_Value *valdatum)
420 unsigned char key_buf[8];
422 if (table->db == NULL)
424 CHISE_DS *ds = table->ds;
426 table->db = chise_open_attribute_table (ds->location,
427 "system-char-id", table->name,
429 DB_RDONLY, ds->modemask);
430 if (table->db == NULL)
432 table->access = DB_RDONLY;
434 chise_format_char_id (cid, key_buf, 8);
435 return chise_get_attribute_table (table->db,
440 chise_char_gets_feature_value (CHISE_Char_ID cid,
441 CHISE_Feature_Table *table,
442 unsigned char *buf, size_t size)
444 CHISE_Value valdatum;
445 unsigned char key_buf[8];
448 if (table->db == NULL)
450 CHISE_DS *ds = table->ds;
452 table->db = chise_open_attribute_table (ds->location,
453 "system-char-id", table->name,
455 DB_RDONLY, ds->modemask);
456 if (table->db == NULL)
458 table->access = DB_RDONLY;
460 chise_format_char_id (cid, key_buf, 8);
461 status = chise_get_attribute_table (table->db,
465 if (size < valdatum.size)
467 strncpy (buf, valdatum.data, valdatum.size);
468 buf[valdatum.size] = '\0';
473 chise_char_feature_value_iterate (CHISE_Feature feature,
474 int (*func) (CHISE_Char_ID cid,
475 CHISE_Feature feature,
476 CHISE_Value *valdatum))
478 DBT keydatum, valdatum;
482 if (feature->db == NULL)
484 CHISE_DS *ds = feature->ds;
487 = chise_open_attribute_table (ds->location,
488 "system-char-id", feature->name,
490 DB_RDONLY, ds->modemask);
491 if (feature->db == NULL)
493 feature->access = DB_RDONLY;
498 status = feature->db->cursor (feature->db, NULL, &dbcp, 0);
499 for (status = dbcp->c_get (dbcp, &keydatum, &valdatum, DB_FIRST);
501 status = dbcp->c_get (dbcp, &keydatum, &valdatum, DB_NEXT))
503 unsigned char *key_str = (unsigned char *)keydatum.data;
504 int key_len = strnlen (key_str, keydatum.size);
505 CHISE_Char_ID key = chise_char_id_parse_c_string (key_str, key_len);
508 if (ret = func (key, feature, &valdatum))
511 dbcp->c_close (dbcp);
516 struct CHISE_CCS_Table
520 CHISE_Attribute_Table *db;
525 chise_ds_open_ccs_table (CHISE_DS *ds, const char *ccs)
527 CHISE_CCS_Table* table;
528 size_t len = strlen (ccs);
533 table = (CHISE_CCS_Table*)malloc (sizeof (CHISE_CCS_Table));
540 table->name = (unsigned char*)malloc (len + 1);
541 if (table->name == NULL)
546 strcpy (table->name, ccs);
551 chise_ccst_close (CHISE_CCS_Table *table)
558 if (table->db == NULL)
561 status = chise_close_attribute_table (table->db);
563 if (table->name == NULL)
575 chise_ccs_sync (CHISE_CCS ccs)
582 status = chise_close_attribute_table (ccs->db);
589 chise_ccs_decode (CHISE_CCS ccs, int code_point)
591 CHISE_Value valdatum;
597 CHISE_DS *ds = ccs->ds;
599 ccs->db = chise_open_attribute_table (ds->location,
600 ccs->name, "system-char-id",
602 DB_RDONLY, ds->modemask);
605 ccs->access = DB_RDONLY;
608 sprintf(key_buf, "%d", code_point);
609 status = chise_get_attribute_table (ccs->db, key_buf, &valdatum);
613 = (unsigned char *)chise_value_data (&valdatum);
614 int len = strnlen (str, chise_value_size (&valdatum));
616 return chise_char_id_parse_c_string (str, len);
622 chise_ccs_set_decoded_char (CHISE_CCS ccs,
623 int code_point, CHISE_Char_ID cid)
625 CHISE_Value valdatum;
626 char key_buf[16], val_buf[8];
628 if ((ccs->access & DB_CREATE) == 0)
632 chise_close_attribute_table (ccs->db);
639 CHISE_DS *ds = ccs->ds;
641 ccs->db = chise_open_attribute_table (ds->location,
642 ccs->name, "system-char-id",
644 DB_CREATE, ds->modemask);
647 ccs->access = DB_CREATE;
650 sprintf(key_buf, "%d", code_point);
651 chise_format_char_id (cid, val_buf, 8);
652 return chise_put_attribute_table (ccs->db, key_buf, val_buf);
656 CHISE_Attribute_Table*
657 chise_open_attribute_table (const unsigned char *db_dir,
658 const char *encoding, const char *feature,
660 u_int32_t accessmask, int modemask)
666 char *db_file_name, *sp;
668 status = db_create (&dbase, NULL, 0);
672 len = strlen (db_dir);
673 flen = strlen (feature);
674 size = len + strlen (encoding) + flen * 3 + 4;
675 db_file_name = alloca (size);
676 strcpy (db_file_name, db_dir);
677 if (db_file_name[len - 1] != '/')
679 db_file_name[len++] = '/';
680 db_file_name[len] = '\0';
682 strcat (db_file_name, encoding);
683 strcat (db_file_name, "/");
684 /* strcat (db_file_name, feature); */
685 sp = &db_file_name[strlen (db_file_name)];
686 for (i = 0; i < flen; i++)
690 if ( (c == '/') || (c == '%') )
692 sprintf (sp, "%%%02X", c);
699 status = dbase->open (dbase, db_file_name, NULL,
700 real_subtype, accessmask, modemask);
703 dbase->close (dbase, 0);
710 chise_close_attribute_table (CHISE_Attribute_Table *db)
721 chise_get_attribute_table (CHISE_Attribute_Table *db,
722 char *key, CHISE_Value *valdatum)
727 /* DB Version 2 requires DBT's to be zeroed before use. */
732 keydatum.size = strlen (key);
734 status = db->get (db, NULL, &keydatum, valdatum, 0);
739 chise_put_attribute_table (CHISE_Attribute_Table *db,
740 char *key, unsigned char *value)
742 DBT keydatum, valdatum;
745 /* DB Version 2 requires DBT's to be zeroed before use. */
750 keydatum.size = strlen (key);
752 valdatum.data = value;
753 valdatum.size = strlen (value);
755 status = db->put (db, NULL, &keydatum, &valdatum, 0);