1 /* This file is part of the Zebra server.
2 Copyright (C) 1994-2010 Index Data
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 * \brief character conversions (.chr)
25 * Support module to handle character-conversions into and out of the
34 typedef unsigned ucs4_t;
38 #include <yaz/yaz-util.h>
40 #define CHR_MAXSTR 1024
41 #define CHR_MAXEQUIV 32
43 const unsigned char CHR_FIELD_BEGIN = '^';
45 const char *CHR_UNKNOWN = "\001";
46 const char *CHR_SPACE = "\002";
47 const char *CHR_CUT = "\003";
48 const char *CHR_BASE = "\005"; /* CHECK CHR_BASE_CHAR as well */
52 chr_t_entry *input; /* mapping table for input data */
53 chr_t_entry *q_input; /* mapping table for queries */
54 unsigned char *output[256]; /* return mapping - for display of registers */
55 int base_uppercase; /* Start of upper-case ordinals */
60 * Character map trie node.
64 chr_t_entry **children; /* array of children */
65 unsigned char **target; /* target for this node, if any */
69 * General argument structure for callback functions (internal use only)
71 typedef struct chrwork
74 char string[CHR_MAXSTR+1];
78 * Callback for equivalent stuff
84 char *eq[CHR_MAXEQUIV];
87 * Add an entry to the character map.
89 static chr_t_entry *set_map_string(chr_t_entry *root, NMEM nmem,
90 const char *from, int len, char *to,
97 root = (chr_t_entry *) nmem_malloc(nmem, sizeof(*root));
103 if (!root->target || !root->target[0] ||
104 strcmp((const char *) root->target[0], to))
107 root->target && root->target[0] && root->target[0][0] &&
108 strcmp((const char *) root->target[0], CHR_UNKNOWN))
110 yaz_log(YLOG_WARN, "duplicate entry for charmap from '%s'",
113 root->target = (unsigned char **)
114 nmem_malloc(nmem, sizeof(*root->target)*2);
115 root->target[0] = (unsigned char *) nmem_strdup(nmem, to);
125 root->children = (chr_t_entry **)
126 nmem_malloc(nmem, sizeof(chr_t_entry*) * 256);
127 for (i = 0; i < 256; i++)
128 root->children[i] = 0;
130 if (!(root->children[(unsigned char) *from] =
131 set_map_string(root->children[(unsigned char) *from], nmem,
132 from + 1, len - 1, to, from_0)))
138 static chr_t_entry *find_entry_x(chr_t_entry *t, const char **from, int *len, int first)
143 { /* switch to next buffer */
149 if (*len > 0 && t->children)
151 const char *old_from = *from;
156 if (first && t->children[CHR_FIELD_BEGIN])
158 if ((res = find_entry_x(t->children[CHR_FIELD_BEGIN], from, len, 0)) && res != t->children[CHR_FIELD_BEGIN])
162 /* otherwhise there was no match on beginning of field, move on */
165 if (!res && t->children[(unsigned char) **from])
169 if ((res = find_entry_x(t->children[(unsigned char) *old_from],
177 /* no children match. use ourselves, if we have a target */
178 return t->target ? t : 0;
181 const char **chr_map_input_x(chrmaptab maptab, const char **from, int *len, int first)
183 chr_t_entry *t = maptab->input;
186 if (!(res = find_entry_x(t, from, len, first)))
188 return (const char **) (res->target);
191 const char **chr_map_input(chrmaptab maptab, const char **from, int len, int first)
193 chr_t_entry *t = maptab->input;
199 if (!(res = find_entry_x(t, from, len_tmp, first)))
201 return (const char **) (res->target);
204 const char **chr_map_q_input(chrmaptab maptab,
205 const char **from, int len, int first)
207 chr_t_entry *t = maptab->q_input;
213 if (!(res = find_entry_x(t, from, len_tmp, first)))
215 return (const char **) (res->target);
218 const char *chr_map_output(chrmaptab maptab, const char **from, int len)
220 unsigned char c = ** (unsigned char **) from;
221 const char *out = (const char*) maptab->output[c];
228 static int zebra_ucs4_strlen(ucs4_t *s)
236 ucs4_t zebra_prim_w(ucs4_t **s)
242 yaz_log(YLOG_DEBUG, "prim_w %.3s", (char *) *s);
243 if (**s == '\\' && 1[*s])
249 case '\\': c = '\\'; (*s)++; break;
250 case 'r': c = '\r'; (*s)++; break;
251 case 'n': c = '\n'; (*s)++; break;
252 case 't': c = '\t'; (*s)++; break;
253 case 's': c = ' '; (*s)++; break;
255 if (zebra_ucs4_strlen(*s) >= 3)
260 sscanf(fmtstr, "%x", &i);
275 if (zebra_ucs4_strlen(*s) >= 3)
281 sscanf(fmtstr, "%o", &i);
287 if (zebra_ucs4_strlen(*s) >= 5)
294 sscanf(fmtstr, "%x", &i);
308 yaz_log(YLOG_DEBUG, "out %d", c);
314 * Add an entry to the value space.
316 static void fun_addentry(const char *s, void *data, int num)
318 chrmaptab tab = (chrmaptab) data;
321 tmp[0] = num; tmp[1] = '\0';
322 tab->input = set_map_string(tab->input, tab->nmem, s, strlen(s), tmp, 0);
323 tab->output[num + tab->base_uppercase] =
324 (unsigned char *) nmem_strdup(tab->nmem, s);
329 * Add a space-entry to the value space.
331 static void fun_addspace(const char *s, void *data, int num)
333 chrmaptab tab = (chrmaptab) data;
334 tab->input = set_map_string(tab->input, tab->nmem, s, strlen(s),
335 (char*) CHR_SPACE, 0);
340 * Add a space-entry to the value space.
342 static void fun_addcut(const char *s, void *data, int num)
344 chrmaptab tab = (chrmaptab) data;
345 tab->input = set_map_string(tab->input, tab->nmem, s, strlen(s),
350 * Create a string containing the mapped characters provided.
352 static void fun_mkstring(const char *s, void *data, int num)
354 chrwork *arg = (chrwork *) data;
355 const char **res, *p = s;
357 res = chr_map_input(arg->map, &s, strlen(s), 0);
358 if (*res == (char*) CHR_UNKNOWN)
359 yaz_log(YLOG_WARN, "Map: '%s' has no mapping", p);
360 strncat(arg->string, *res, CHR_MAXSTR - strlen(arg->string));
361 arg->string[CHR_MAXSTR] = '\0';
365 * Create an unmodified string (scan_string handler).
367 static void fun_add_equivalent_string(const char *s, void *data, int num)
369 chr_equiv_work *arg = (chr_equiv_work *) data;
371 if (arg->no_eq == CHR_MAXEQUIV)
373 arg->eq[arg->no_eq++] = nmem_strdup(arg->nmem, s);
377 * Add a map to the string contained in the argument.
379 static void fun_add_map(const char *s, void *data, int num)
381 chrwork *arg = (chrwork *) data;
383 assert(arg->map->input);
384 yaz_log(YLOG_DEBUG, "set map %.*s", (int) strlen(s), s);
385 set_map_string(arg->map->input, arg->map->nmem, s, strlen(s), arg->string,
387 for (s = arg->string; *s; s++)
388 yaz_log(YLOG_DEBUG, " %3d", (unsigned char) *s);
391 static int scan_to_utf8(yaz_iconv_t t, ucs4_t *from, size_t inlen,
392 char *outbuf, size_t outbytesleft)
394 size_t inbytesleft = inlen * sizeof(ucs4_t);
395 char *inbuf = (char*) from;
399 *outbuf++ = *from; /* ISO-8859-1 is OK here */
402 ret = yaz_iconv(t, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
403 if (ret != (size_t) (-1))
404 ret = yaz_iconv(t, 0, 0, &outbuf, &outbytesleft);
407 if (ret == (size_t) (-1))
409 yaz_log(YLOG_LOG, "from: %2X %2X %2X %2X",
410 from[0], from[1], from[2], from[3]);
411 yaz_log(YLOG_WARN|YLOG_ERRNO, "bad unicode sequence");
419 static int scan_string(char *s_native,
420 yaz_iconv_t t_unicode, yaz_iconv_t t_utf8,
421 void (*fun)(const char *c, void *data, int num),
422 void *data, int *num)
427 ucs4_t arg_prim[512];
428 ucs4_t *s0, *s = arg;
429 ucs4_t c, begin, end;
434 char *outbuf = (char *) arg;
435 char *inbuf = s_native;
436 size_t outbytesleft = sizeof(arg)-4;
437 size_t inbytesleft = strlen(s_native);
439 ret = yaz_iconv(t_unicode, &inbuf, &inbytesleft,
440 &outbuf, &outbytesleft);
441 if (ret != (size_t)(-1))
442 ret = yaz_iconv(t_unicode, 0, 0, &outbuf, &outbytesleft);
444 if (ret == (size_t)(-1))
446 i = (outbuf - (char*) arg)/sizeof(ucs4_t);
450 for (i = 0; s_native[i]; i++)
451 arg[i] = s_native[i] & 255; /* ISO-8859-1 conversion */
453 arg[i] = 0; /* terminate */
454 if (s[0] == 0xfeff || s[0] == 0xfeff) /* skip byte Order Mark */
462 begin = zebra_prim_w(&s);
465 yaz_log(YLOG_FATAL, "Bad range in char-map");
469 end = zebra_prim_w(&s);
472 yaz_log(YLOG_FATAL, "Bad range in char-map");
476 for (c = begin; c <= end; c++)
478 if (scan_to_utf8(t_utf8, &c, 1, str, sizeof(str)-1))
480 (*fun)(str, data, num ? (*num)++ : 0);
486 while (*s != ')' || s[-1] == '\\')
487 arg_prim[i++] = zebra_prim_w(&s);
489 if (scan_to_utf8(t_utf8, arg_prim, zebra_ucs4_strlen(arg_prim), str, sizeof(str)-1))
491 (*fun)(str, data, num ? (*num)++ : 0);
495 c = zebra_prim_w(&s);
496 if (scan_to_utf8(t_utf8, &c, 1, str, sizeof(str)-1))
498 (*fun)(str, data, num ? (*num)++ : 0);
504 chrmaptab chrmaptab_create(const char *tabpath, const char *name,
508 char line[512], *argv[50];
511 int no_directives = 0;
513 int argc, num = (int) *CHR_BASE, i;
515 yaz_iconv_t t_unicode = 0;
516 yaz_iconv_t t_utf8 = 0;
517 unsigned endian = 31;
518 const char *ucs4_native = "UCS-4";
520 yaz_log(YLOG_DEBUG, "maptab %s open", name);
521 if (!(f = yaz_fopen(tabpath, name, "r", tabroot)))
523 yaz_log(YLOG_WARN|YLOG_ERRNO, "%s", name);
527 if (*(char*) &endian == 31) /* little endian? */
528 ucs4_native = "UCS-4LE";
530 t_utf8 = yaz_iconv_open("UTF-8", ucs4_native);
532 nmem = nmem_create();
533 res = (chrmaptab) nmem_malloc(nmem, sizeof(*res));
535 res->input = (chr_t_entry *) nmem_malloc(res->nmem, sizeof(*res->input));
536 res->input->target = (unsigned char **)
537 nmem_malloc(res->nmem, sizeof(*res->input->target) * 2);
538 res->input->target[0] = (unsigned char*) CHR_UNKNOWN;
539 res->input->target[1] = 0;
540 res->input->children = (chr_t_entry **)
541 nmem_malloc(res->nmem, sizeof(res->input) * 256);
542 for (i = 0; i < 256; i++)
544 res->input->children[i] = (chr_t_entry *)
545 nmem_malloc(res->nmem, sizeof(*res->input));
546 res->input->children[i]->children = 0;
547 res->input->children[i]->target = (unsigned char **)
548 nmem_malloc(res->nmem, 2 * sizeof(unsigned char *));
549 res->input->children[i]->target[1] = 0;
550 res->input->children[i]->target[0] = (unsigned char*) CHR_UNKNOWN;
552 res->q_input = (chr_t_entry *)
553 nmem_malloc(res->nmem, sizeof(*res->q_input));
554 res->q_input->target = 0;
555 res->q_input->children = 0;
557 for (i = *CHR_BASE; i < 256; i++)
559 res->output[(int) *CHR_SPACE] = (unsigned char *) " ";
560 res->output[(int) *CHR_UNKNOWN] = (unsigned char*) "@";
561 res->base_uppercase = 0;
563 while (!errors && (argc = readconf_line(f, &lineno, line, 512, argv, 50)))
566 if (!yaz_matchstr(argv[0], "lowercase"))
570 yaz_log(YLOG_FATAL, "Syntax error in charmap");
573 if (scan_string(argv[1], t_unicode, t_utf8, fun_addentry,
576 yaz_log(YLOG_FATAL, "Bad value-set specification");
579 res->base_uppercase = num;
580 res->output[(int) *CHR_SPACE + num] = (unsigned char *) " ";
581 res->output[(int) *CHR_UNKNOWN + num] = (unsigned char*) "@";
582 num = (int) *CHR_BASE;
584 else if (!yaz_matchstr(argv[0], "uppercase"))
586 if (!res->base_uppercase)
588 yaz_log(YLOG_FATAL, "Uppercase directive with no lowercase set");
593 yaz_log(YLOG_FATAL, "Missing arg for uppercase directive");
596 if (scan_string(argv[1], t_unicode, t_utf8, fun_addentry,
599 yaz_log(YLOG_FATAL, "Bad value-set specification");
603 else if (!yaz_matchstr(argv[0], "space"))
607 yaz_log(YLOG_FATAL, "Syntax error in charmap for space");
610 if (scan_string(argv[1], t_unicode, t_utf8,
611 fun_addspace, res, 0) < 0)
613 yaz_log(YLOG_FATAL, "Bad space specification");
617 else if (!yaz_matchstr(argv[0], "cut"))
621 yaz_log(YLOG_FATAL, "Syntax error in charmap for cut");
624 if (scan_string(argv[1], t_unicode, t_utf8,
625 fun_addcut, res, 0) < 0)
627 yaz_log(YLOG_FATAL, "Bad cut specification");
631 else if (!yaz_matchstr(argv[0], "map"))
637 yaz_log(YLOG_FATAL, "charmap directive map requires 2 args");
641 buf.string[0] = '\0';
642 if (scan_string(argv[2], t_unicode, t_utf8,
643 fun_mkstring, &buf, 0) < 0)
645 yaz_log(YLOG_FATAL, "Bad map target");
648 if (scan_string(argv[1], t_unicode, t_utf8,
649 fun_add_map, &buf, 0) < 0)
651 yaz_log(YLOG_FATAL, "Bad map source");
655 else if (!yaz_matchstr(argv[0], "equivalent"))
661 yaz_log(YLOG_FATAL, "equivalent requires 1 argument");
666 if (scan_string(argv[1], t_unicode, t_utf8,
667 fun_add_equivalent_string, &w, 0) < 0)
669 yaz_log(YLOG_FATAL, "equivalent: invalid string");
672 else if (w.no_eq == 0)
674 yaz_log(YLOG_FATAL, "equivalent: no strings");
682 /* determine length of regular expression */
683 for (i = 0; i<w.no_eq; i++)
684 slen += strlen(w.eq[i]) + 1;
685 result_str = nmem_malloc(res->nmem, slen + 5);
687 /* build the regular expression */
690 for (i = 0; i<w.no_eq; i++)
692 result_str[slen++] = i ? '|' : '(';
693 strcpy(result_str + slen, w.eq[i]);
694 slen += strlen(w.eq[i]);
696 result_str[slen++] = ')';
697 result_str[slen] = '\0';
699 /* each eq will map to this regular expression */
700 for (i = 0; i<w.no_eq; i++)
702 set_map_string(res->q_input, res->nmem,
703 w.eq[i], strlen(w.eq[i]),
708 else if (!yaz_matchstr(argv[0], "encoding"))
711 yaz_iconv_close(t_unicode);
712 t_unicode = yaz_iconv_open(ucs4_native, argv[1]);
716 yaz_log(YLOG_WARN, "Syntax error at '%s' in %s", line, name);
721 if (no_directives == 0)
723 yaz_log(YLOG_WARN, "No directives in '%s'", name);
728 chrmaptab_destroy(res);
731 yaz_log(YLOG_DEBUG, "maptab %s close %d errors", name, errors);
733 yaz_iconv_close(t_utf8);
735 yaz_iconv_close(t_unicode);
739 void chrmaptab_destroy(chrmaptab tab)
742 nmem_destroy(tab->nmem);
749 * c-file-style: "Stroustrup"
750 * indent-tabs-mode: nil
752 * vim: shiftwidth=4 tabstop=8 expandtab