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 #define xzero(lvalue) ((void) memset (&(lvalue), '\0', sizeof (lvalue)))
26 chise_char_id_parse_c_string (unsigned char *str, int len)
30 if ( (len >= 2) && (str[i++] == '?') )
32 unsigned char c = str[i++];
49 return c & (0x80 | 0x1F);
83 if (counter + 2 <= len)
87 for (j = 0; j < counter; j++)
88 cid = (cid << 6) | (str[j + i] & 0x3F);
96 chise_format_char_id (CHISE_Char_ID cid, unsigned char *dest, int len)
108 else if (cid == '\n')
115 else if (cid == '\r')
122 else if (cid == 0x1C)
131 else if (cid <= 0x1F)
135 dest[i++] = '@' + cid;
139 else if ( (cid == ' ') || (cid == '"') ||
140 (cid == '#') || (cid == '\'') ||
141 (cid == '(') || (cid == ')') ||
142 (cid == ',') || (cid == '.') ||
143 (cid == ';') || (cid == '?') ||
144 (cid == '[') || (cid == '\\') ||
145 (cid == ']') || (cid == '`') )
152 else if (cid <= 0x7E)
158 else if (cid == 0x7F)
166 else if (cid <= 0x9F)
170 dest[i++] = ((cid + '@') >> 6) | 0xC0;
171 dest[i++] = ((cid + '@') & 0x3F) | 0x80;
175 else if (cid <= 0x7FF)
177 dest[i++] = (cid >> 6) | 0xC0;
178 dest[i++] = (cid & 0x3F) | 0x80;
182 else if (cid <= 0xFFFF)
184 dest[i++] = (cid >> 12) | 0xE0;
185 dest[i++]= ((cid >> 6) & 0x3F) | 0x80;
186 dest[i++]= (cid & 0x3F) | 0x80;
190 else if (cid <= 0x1FFFFF)
192 dest[i++]= (cid >> 18) | 0xF0;
193 dest[i++]= ((cid >> 12) & 0x3F) | 0x80;
194 dest[i++]= ((cid >> 6) & 0x3F) | 0x80;
195 dest[i++]= (cid & 0x3F) | 0x80;
199 else if (cid <= 0x3FFFFFF)
201 dest[i++]= (cid >> 24) | 0xF8;
202 dest[i++]= ((cid >> 18) & 0x3F) | 0x80;
203 dest[i++]= ((cid >> 12) & 0x3F) | 0x80;
204 dest[i++]= ((cid >> 6) & 0x3F) | 0x80;
205 dest[i++]= (cid & 0x3F) | 0x80;
211 dest[i++]= (cid >> 30) | 0xFC;
212 dest[i++]= ((cid >> 24) & 0x3F) | 0x80;
213 dest[i++]= ((cid >> 18) & 0x3F) | 0x80;
214 dest[i++]= ((cid >> 12) & 0x3F) | 0x80;
215 dest[i++]= ((cid >> 6) & 0x3F) | 0x80;
216 dest[i++]= (cid & 0x3F) | 0x80;
223 chise_open_data_source (CHISE_DS *ds, CHISE_DS_Type type, char *location)
226 ds->location = location;
231 chise_close_data_source (CHISE_DS *ds)
233 ds->type = CHISE_DS_NONE;
240 chise_open_decoding_table (CHISE_Decoding_Table **db,
241 CHISE_DS *ds, const char *ccs,
243 u_int32_t accessmask, int modemask)
246 chise_open_attribute_table (db, ds->location,
247 ccs, "system-char-id",
248 real_subtype, accessmask, modemask);
252 chise_close_decoding_table (CHISE_Decoding_Table *db)
255 return chise_close_attribute_table (db);
260 chise_dt_get_char (CHISE_Decoding_Table *db, int code_point)
262 CHISE_Value valdatum;
266 sprintf(key_buf, "%d", code_point);
267 status = chise_get_attribute_table (db, key_buf, &valdatum);
271 = (unsigned char *)chise_value_data (&valdatum);
272 int len = strnlen (str, chise_value_size (&valdatum));
274 return chise_char_id_parse_c_string (str, len);
280 chise_dt_put_char (CHISE_Decoding_Table *db,
281 int code_point, CHISE_Char_ID cid)
283 CHISE_Value valdatum;
284 char key_buf[16], val_buf[8];
286 sprintf(key_buf, "%d", code_point);
287 chise_format_char_id (cid, val_buf, 8);
288 return chise_put_attribute_table (db, key_buf, val_buf);
294 chise_open_feature_table (CHISE_Feature_Table **db,
295 CHISE_DS *ds, const char *feature,
297 u_int32_t accessmask, int modemask)
300 chise_open_attribute_table (db, ds->location,
301 "system-char-id", feature,
302 real_subtype, accessmask, modemask);
306 chise_close_feature_table (CHISE_Feature_Table *db)
309 return chise_close_attribute_table (db);
314 chise_ft_get_value (CHISE_Feature_Table *db,
315 CHISE_Char_ID cid, CHISE_Value *valdatum)
317 unsigned char key_buf[8];
319 chise_format_char_id (cid, key_buf, 8);
320 return chise_get_attribute_table (db, key_buf, valdatum);
324 chise_ft_iterate (CHISE_Feature_Table *dbp,
325 int (*func) (CHISE_Feature_Table *db,
326 CHISE_Char_ID cid, CHISE_Value *valdatum))
328 DBT keydatum, valdatum;
335 status = dbp->cursor (dbp, NULL, &dbcp, 0);
336 for (status = dbcp->c_get (dbcp, &keydatum, &valdatum, DB_FIRST);
338 status = dbcp->c_get (dbcp, &keydatum, &valdatum, DB_NEXT))
340 unsigned char *key_str = (unsigned char *)keydatum.data;
341 int key_len = strnlen (key_str, keydatum.size);
342 CHISE_Char_ID key = chise_char_id_parse_c_string (key_str, key_len);
345 if (ret = func (dbp, key, &valdatum))
348 dbcp->c_close (dbcp);
352 chise_open_attribute_table (CHISE_Attribute_Table **db,
354 const char *encoding, const char *feature,
356 u_int32_t accessmask, int modemask)
362 char *db_file_name, *sp;
364 status = db_create (&dbase, NULL, 0);
368 len = strlen (db_dir);
369 flen = strlen (feature);
370 size = len + strlen (encoding) + flen * 3 + 4;
371 db_file_name = alloca (size);
372 strcpy (db_file_name, db_dir);
373 if (db_file_name[len - 1] != '/')
375 db_file_name[len++] = '/';
376 db_file_name[len] = '\0';
378 strcat (db_file_name, encoding);
379 strcat (db_file_name, "/");
380 /* strcat (db_file_name, feature); */
381 sp = &db_file_name[strlen (db_file_name)];
382 for (i = 0; i < flen; i++)
386 if ( (c == '/') || (c == '%') )
388 sprintf (sp, "%%%02X", c);
395 status = dbase->open (dbase, db_file_name, NULL,
396 real_subtype, accessmask, modemask);
399 dbase->close (dbase, 0);
407 chise_close_attribute_table (CHISE_Attribute_Table *db)
418 chise_get_attribute_table (CHISE_Attribute_Table *db,
419 char *key, CHISE_Value *valdatum)
424 /* DB Version 2 requires DBT's to be zeroed before use. */
429 keydatum.size = strlen (key);
431 status = db->get (db, NULL, &keydatum, valdatum, 0);
436 chise_put_attribute_table (CHISE_Attribute_Table *db,
437 char *key, char *value)
439 DBT keydatum, valdatum;
442 /* DB Version 2 requires DBT's to be zeroed before use. */
447 keydatum.size = strlen (key);
449 valdatum.data = value;
450 valdatum.size = strlen (value);
452 status = db->put (db, NULL, &keydatum, &valdatum, 0);