+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#ifndef HAVE_STRNLEN
+/* original in mysql, strings/strnlen.c.
+uint strnlen(register const char *s, register uint maxlen)
+{
+ const char *end= (const char *)memchr(s, '\0', maxlen);
+ return end ? (uint) (end - s) : maxlen;
+}
+*/
+static inline int
+strnlen (register const char *s, register int maxlen)
+{
+ const char *end= (const char *)memchr(s, '\0', maxlen);
+ return end ? (int) (end - s) : maxlen;
+}
+#endif
+
#include "chise.h"
+typedef DB CHISE_Attribute_Table;
+
+CHISE_Attribute_Table*
+chise_open_attribute_table (const unsigned char *db_dir,
+ const char *encoding, const char *feature,
+ DBTYPE real_subtype,
+ u_int32_t accessmask, int modemask);
+
+int chise_close_attribute_table (CHISE_Attribute_Table *db);
+
+int chise_get_attribute_table (CHISE_Attribute_Table *db,
+ char *key, CHISE_Value *valdatum);
+
+int chise_put_attribute_table (CHISE_Attribute_Table *db,
+ char *key, unsigned char *value);
+
+
#define xzero(lvalue) ((void) memset (&(lvalue), '\0', sizeof (lvalue)))
+CHISE_Char_ID
+chise_char_id_parse_c_string (unsigned char *str, int len)
+{
+ int i = 0;
+
+ if ( (len >= 2) && (str[i++] == '?') )
+ {
+ unsigned char c = str[i++];
+ int counter;
+ CHISE_Char_ID cid;
+
+ if (c == '\\')
+ {
+ if (len < 3)
+ return -1;
+ c = str[i++];
+ if (c == '^')
+ {
+ if (len < 4)
+ return -1;
+ c = str[i++];
+ if (c == '?')
+ return 0x7F;
+ else
+ return c & (0x80 | 0x1F);
+ }
+ }
+ if ( c < 0xC0 )
+ {
+ cid = c;
+ counter = 0;
+ }
+ else if ( c < 0xE0 )
+ {
+ cid = c & 0x1f;
+ counter = 1;
+ }
+ else if ( c < 0xF0 )
+ {
+ cid = c & 0x0f;
+ counter = 2;
+ }
+ else if ( c < 0xF8 )
+ {
+ cid = c & 0x07;
+ counter = 3;
+ }
+ else if ( c < 0xFC )
+ {
+ cid = c & 0x03;
+ counter = 4;
+ }
+ else
+ {
+ cid = c & 0x01;
+ counter = 5;
+ }
+
+ if (counter + 2 <= len)
+ {
+ int j;
+
+ for (j = 0; j < counter; j++)
+ cid = (cid << 6) | (str[j + i] & 0x3F);
+ return cid;
+ }
+ }
+ return -1;
+}
int
-chise_open_data_source (CHISE_DS *ds, CHISE_DS_Type type, char *location)
+chise_format_char_id (CHISE_Char_ID cid, unsigned char *dest, int len)
{
+ int i = 0;
+
+ dest[i++] = '?';
+ if (cid == '\t')
+ {
+ dest[i++] = '\\';
+ dest[i++] = 't';
+ dest[i] = '\0';
+ return i;
+ }
+ else if (cid == '\n')
+ {
+ dest[i++] = '\\';
+ dest[i++] = 'n';
+ dest[i] = '\0';
+ return i;
+ }
+ else if (cid == '\r')
+ {
+ dest[i++] = '\\';
+ dest[i++] = 'r';
+ dest[i] = '\0';
+ return i;
+ }
+ else if (cid == 0x1C)
+ {
+ dest[i++] = '\\';
+ dest[i++] = '^';
+ dest[i++] = '\\';
+ dest[i++] = '\\';
+ dest[i] = '\0';
+ return i;
+ }
+ else if (cid <= 0x1F)
+ {
+ dest[i++] = '\\';
+ dest[i++] = '^';
+ dest[i++] = '@' + cid;
+ dest[i] = '\0';
+ return i;
+ }
+ else if ( (cid == ' ') || (cid == '"') ||
+ (cid == '#') || (cid == '\'') ||
+ (cid == '(') || (cid == ')') ||
+ (cid == ',') || (cid == '.') ||
+ (cid == ';') || (cid == '?') ||
+ (cid == '[') || (cid == '\\') ||
+ (cid == ']') || (cid == '`') )
+ {
+ dest[i++] = '\\';
+ dest[i++] = cid;
+ dest[i] = '\0';
+ return i;
+ }
+ else if (cid <= 0x7E)
+ {
+ dest[i++] = cid;
+ dest[i] = '\0';
+ return i;
+ }
+ else if (cid == 0x7F)
+ {
+ dest[i++] = '\\';
+ dest[i++] = '^';
+ dest[i++] = '?';
+ dest[i] = '\0';
+ return i;
+ }
+ else if (cid <= 0x9F)
+ {
+ dest[i++] = '\\';
+ dest[i++] = '^';
+ dest[i++] = ((cid + '@') >> 6) | 0xC0;
+ dest[i++] = ((cid + '@') & 0x3F) | 0x80;
+ dest[i] = '\0';
+ return i;
+ }
+ else if (cid <= 0x7FF)
+ {
+ dest[i++] = (cid >> 6) | 0xC0;
+ dest[i++] = (cid & 0x3F) | 0x80;
+ dest[i] = '\0';
+ return i;
+ }
+ else if (cid <= 0xFFFF)
+ {
+ dest[i++] = (cid >> 12) | 0xE0;
+ dest[i++]= ((cid >> 6) & 0x3F) | 0x80;
+ dest[i++]= (cid & 0x3F) | 0x80;
+ dest[i] = '\0';
+ return i;
+ }
+ else if (cid <= 0x1FFFFF)
+ {
+ dest[i++]= (cid >> 18) | 0xF0;
+ dest[i++]= ((cid >> 12) & 0x3F) | 0x80;
+ dest[i++]= ((cid >> 6) & 0x3F) | 0x80;
+ dest[i++]= (cid & 0x3F) | 0x80;
+ dest[i] = '\0';
+ return i;
+ }
+ else if (cid <= 0x3FFFFFF)
+ {
+ dest[i++]= (cid >> 24) | 0xF8;
+ dest[i++]= ((cid >> 18) & 0x3F) | 0x80;
+ dest[i++]= ((cid >> 12) & 0x3F) | 0x80;
+ dest[i++]= ((cid >> 6) & 0x3F) | 0x80;
+ dest[i++]= (cid & 0x3F) | 0x80;
+ dest[i] = '\0';
+ return i;
+ }
+ else
+ {
+ dest[i++]= (cid >> 30) | 0xFC;
+ dest[i++]= ((cid >> 24) & 0x3F) | 0x80;
+ dest[i++]= ((cid >> 18) & 0x3F) | 0x80;
+ dest[i++]= ((cid >> 12) & 0x3F) | 0x80;
+ dest[i++]= ((cid >> 6) & 0x3F) | 0x80;
+ dest[i++]= (cid & 0x3F) | 0x80;
+ dest[i] = '\0';
+ return i;
+ }
+}
+
+
+struct CHISE_DS
+{
+ CHISE_DS_Type type;
+ unsigned char *location;
+};
+
+CHISE_DS*
+chise_open_data_source (CHISE_DS_Type type, char *location)
+{
+ CHISE_DS *ds = (CHISE_DS*)malloc (sizeof (CHISE_DS));
+ size_t len = strlen (location);
+
+ if (ds == NULL)
+ return NULL;
+
ds->type = type;
- ds->location = location;
- return 0;
+ ds->location = (unsigned char*)malloc (len + 1);
+ if (ds->location == NULL)
+ {
+ free (ds);
+ return NULL;
+ }
+ strcpy (ds->location, location);
+ return ds;
}
int
-chise_close_data_source (CHISE_DS *ds)
+chise_ds_close (CHISE_DS *ds)
{
- ds->type = CHISE_DS_NONE;
- ds->location = NULL;
+ if (ds->location != NULL)
+ free (ds->location);
+ free (ds);
return 0;
}
-int
-chise_open_decoding_table (CHISE_Decoding_Table **db,
- CHISE_DS *ds, const char *ccs,
- DBTYPE real_subtype,
- u_int32_t accessmask, int modemask)
+struct CHISE_Decoding_Table
+{
+ CHISE_DS *ds;
+ CHISE_Attribute_Table *db;
+};
+
+CHISE_Decoding_Table*
+chise_ds_open_decoding_table (CHISE_DS *ds, const char *ccs,
+ DBTYPE real_subtype,
+ u_int32_t accessmask, int modemask)
{
- return
- chise_open_attribute_table (db, ds->location,
- ccs, "system-char-id",
- real_subtype, accessmask, modemask);
+ CHISE_Decoding_Table* table;
+
+ if (ds == NULL)
+ return NULL;
+
+ table = (CHISE_Decoding_Table*)malloc (sizeof (CHISE_Decoding_Table));
+ if (table == NULL)
+ return NULL;
+
+ table->ds = ds;
+ table->db = chise_open_attribute_table (ds->location,
+ ccs, "system-char-id",
+ real_subtype,
+ accessmask, modemask);
+ if (table->db == NULL)
+ {
+ free (table);
+ return NULL;
+ }
+ return table;
}
int
-chise_close_decoding_table (CHISE_Decoding_Table *db)
+chise_dt_close (CHISE_Decoding_Table *table)
{
- if (db)
- return chise_close_attribute_table (db);
- return -1;
+ int status;
+
+ if (table == NULL)
+ return -1;
+
+ if (table->db == NULL)
+ status = -1;
+ else
+ status = chise_close_attribute_table (table->db);
+ free (table);
+ return status;
}
CHISE_Char_ID
-chise_dt_get_char (CHISE_Decoding_Table *db, int code_point)
+chise_dt_get_char (CHISE_Decoding_Table *table, int code_point)
{
CHISE_Value valdatum;
int status = 0;
char key_buf[16];
sprintf(key_buf, "%d", code_point);
- status = chise_get_attribute_table (db, key_buf, &valdatum);
+ status = chise_get_attribute_table (table->db, key_buf, &valdatum);
if (!status)
{
unsigned char *str
- = (unsigned char *)chise_value_to_c_string (&valdatum);
- int len = strlen (str);
- int i = 0;
+ = (unsigned char *)chise_value_data (&valdatum);
+ int len = strnlen (str, chise_value_size (&valdatum));
- if ( (len >= 2) && (str[i++] == '?') )
- {
- unsigned char c = str[i++];
- int counter;
- CHISE_Char_ID cid;
-
- if (c == '\\')
- {
- if (len < 3)
- return -1;
- c = str[i++];
- }
- if ( c < 0xC0 )
- {
- cid = c;
- counter = 0;
- }
- else if ( c < 0xE0 )
- {
- cid = c & 0x1f;
- counter = 1;
- }
- else if ( c < 0xF0 )
- {
- cid = c & 0x0f;
- counter = 2;
- }
- else if ( c < 0xF8 )
- {
- cid = c & 0x07;
- counter = 3;
- }
- else if ( c < 0xFC )
- {
- cid = c & 0x03;
- counter = 4;
- }
- else
- {
- cid = c & 0x01;
- counter = 5;
- }
-
- if (counter + 2 <= len)
- {
- int j;
-
- for (j = 0; j < counter; j++)
- cid = (cid << 6) | (str[j + i] & 0x3F);
- return cid;
- }
- }
+ return chise_char_id_parse_c_string (str, len);
}
return -1;
}
+int
+chise_dt_put_char (CHISE_Decoding_Table *table,
+ int code_point, CHISE_Char_ID cid)
+{
+ CHISE_Value valdatum;
+ char key_buf[16], val_buf[8];
+
+ sprintf(key_buf, "%d", code_point);
+ chise_format_char_id (cid, val_buf, 8);
+ return chise_put_attribute_table (table->db, key_buf, val_buf);
+}
-int
-chise_open_feature_table (CHISE_Feature_Table **db,
- CHISE_DS *ds, const char *feature,
- DBTYPE real_subtype,
- u_int32_t accessmask, int modemask)
+struct CHISE_Feature_Table
{
- return
- chise_open_attribute_table (db, ds->location,
- "system-char-id", feature,
- real_subtype, accessmask, modemask);
+ CHISE_DS *ds;
+ CHISE_Attribute_Table *db;
+};
+
+CHISE_Feature_Table*
+chise_ds_open_feature_table (CHISE_DS *ds, const char *feature,
+ DBTYPE real_subtype,
+ u_int32_t accessmask, int modemask)
+{
+ CHISE_Feature_Table* table;
+
+ if (ds == NULL)
+ return NULL;
+
+ table = (CHISE_Feature_Table*)malloc (sizeof (CHISE_Feature_Table));
+ if (table == NULL)
+ return NULL;
+
+ table->ds = ds;
+ table->db = chise_open_attribute_table (ds->location,
+ "system-char-id", feature,
+ real_subtype,
+ accessmask, modemask);
+ if (table->db == NULL)
+ {
+ free (table);
+ return NULL;
+ }
+ return table;
}
int
-chise_close_feature_table (CHISE_Feature_Table *db)
+chise_ft_close (CHISE_Feature_Table *table)
{
- if (db)
- return chise_close_attribute_table (db);
- return -1;
+ int status;
+
+ if (table == NULL)
+ return -1;
+
+ if (table->db == NULL)
+ status = -1;
+ else
+ status = chise_close_attribute_table (table->db);
+ free (table);
+ return status;
}
-int chise_ft_get_value (CHISE_Feature_Table *db,
- CHISE_Char_ID cid, CHISE_Value *valdatum)
+int
+chise_ft_get_value (CHISE_Feature_Table *table,
+ CHISE_Char_ID cid, CHISE_Value *valdatum)
{
unsigned char key_buf[8];
- key_buf[0] = '?';
- if (cid <= 0x7f)
- {
- key_buf[1] = cid;
- key_buf[2] = '\0';
- }
- else if (cid <= 0x7ff)
- {
- key_buf[1] = (cid >> 6) | 0xc0;
- key_buf[2] = (cid & 0x3f) | 0x80;
- key_buf[3] = '\0';
- }
- else if (cid <= 0xffff)
- {
- key_buf[1] = (cid >> 12) | 0xe0;
- key_buf[2]= ((cid >> 6) & 0x3f) | 0x80;
- key_buf[3]= (cid & 0x3f) | 0x80;
- key_buf[4] = '\0';
- }
- else if (cid <= 0x1fffff)
- {
- key_buf[1]= (cid >> 18) | 0xf0;
- key_buf[2]= ((cid >> 12) & 0x3f) | 0x80;
- key_buf[3]= ((cid >> 6) & 0x3f) | 0x80;
- key_buf[4]= (cid & 0x3f) | 0x80;
- key_buf[5] = '\0';
- }
- else if (cid <= 0x3ffffff)
- {
- key_buf[1]= (cid >> 24) | 0xf8;
- key_buf[2]= ((cid >> 18) & 0x3f) | 0x80;
- key_buf[3]= ((cid >> 12) & 0x3f) | 0x80;
- key_buf[4]= ((cid >> 6) & 0x3f) | 0x80;
- key_buf[5]= (cid & 0x3f) | 0x80;
- key_buf[6] = '\0';
- }
- else
+ chise_format_char_id (cid, key_buf, 8);
+ return chise_get_attribute_table (table->db,
+ key_buf, valdatum);
+}
+
+void
+chise_ft_iterate (CHISE_Feature_Table *table,
+ int (*func) (CHISE_Feature_Table *table,
+ CHISE_Char_ID cid, CHISE_Value *valdatum))
+{
+ DBT keydatum, valdatum;
+ DBC *dbcp;
+ int status;
+
+ xzero (keydatum);
+ xzero (valdatum);
+
+ status = table->db->cursor (table->db, NULL, &dbcp, 0);
+ for (status = dbcp->c_get (dbcp, &keydatum, &valdatum, DB_FIRST);
+ status == 0;
+ status = dbcp->c_get (dbcp, &keydatum, &valdatum, DB_NEXT))
{
- key_buf[1]= (cid >> 30) | 0xfc;
- key_buf[2]= ((cid >> 24) & 0x3f) | 0x80;
- key_buf[3]= ((cid >> 18) & 0x3f) | 0x80;
- key_buf[4]= ((cid >> 12) & 0x3f) | 0x80;
- key_buf[5]= ((cid >> 6) & 0x3f) | 0x80;
- key_buf[6]= (cid & 0x3f) | 0x80;
- key_buf[7] = '\0';
+ unsigned char *key_str = (unsigned char *)keydatum.data;
+ int key_len = strnlen (key_str, keydatum.size);
+ CHISE_Char_ID key = chise_char_id_parse_c_string (key_str, key_len);
+ int ret;
+
+ if (ret = func (table, key, &valdatum))
+ break;
}
- return
- chise_get_attribute_table (db, key_buf, valdatum);
+ dbcp->c_close (dbcp);
}
-
-int
-chise_open_attribute_table (CHISE_Attribute_Table **db,
- const char *db_dir,
+CHISE_Attribute_Table*
+chise_open_attribute_table (const unsigned char *db_dir,
const char *encoding, const char *feature,
DBTYPE real_subtype,
u_int32_t accessmask, int modemask)
{
DB* dbase;
int status;
- int len;
+ int len, flen, i;
int size;
- char *db_file_name;
+ char *db_file_name, *sp;
status = db_create (&dbase, NULL, 0);
if (status)
- return -1;
+ return NULL;
len = strlen (db_dir);
- size = len + strlen (encoding) + strlen (feature) + 4;
+ flen = strlen (feature);
+ size = len + strlen (encoding) + flen * 3 + 4;
db_file_name = alloca (size);
strcpy (db_file_name, db_dir);
if (db_file_name[len - 1] != '/')
}
strcat (db_file_name, encoding);
strcat (db_file_name, "/");
- strcat (db_file_name, feature);
+ /* strcat (db_file_name, feature); */
+ sp = &db_file_name[strlen (db_file_name)];
+ for (i = 0; i < flen; i++)
+ {
+ int c = feature[i];
+
+ if ( (c == '/') || (c == '%') )
+ {
+ sprintf (sp, "%%%02X", c);
+ sp += 3;
+ }
+ else
+ *sp++ = c;
+ }
+ *sp = '\0';
status = dbase->open (dbase, db_file_name, NULL,
real_subtype, accessmask, modemask);
if (status)
{
dbase->close (dbase, 0);
- return -1;
+ return NULL;
}
- *db = dbase;
- return 0;
+ return dbase;
}
int
status = db->get (db, NULL, &keydatum, valdatum, 0);
return status;
}
+
+int
+chise_put_attribute_table (CHISE_Attribute_Table *db,
+ char *key, unsigned char *value)
+{
+ DBT keydatum, valdatum;
+ int status = 0;
+
+ /* DB Version 2 requires DBT's to be zeroed before use. */
+ xzero (keydatum);
+ xzero (valdatum);
+
+ keydatum.data = key;
+ keydatum.size = strlen (key);
+
+ valdatum.data = value;
+ valdatum.size = strlen (value);
+
+ status = db->put (db, NULL, &keydatum, &valdatum, 0);
+ return status;
+}