1 /* This file is part of the Zebra server.
2 Copyright (C) 1994-2009 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 * Support module to handle character-conversions into and out of the
32 typedef unsigned ucs4_t;
36 #include <yaz/yaz-util.h>
38 #define CHR_MAXSTR 1024
39 #define CHR_MAXEQUIV 32
41 const unsigned char CHR_FIELD_BEGIN = '^';
43 const char *CHR_UNKNOWN = "\001";
44 const char *CHR_SPACE = "\002";
45 const char *CHR_CUT = "\003";
46 const char *CHR_BASE = "\005"; /* CHECK CHR_BASE_CHAR as well */
50 chr_t_entry *input; /* mapping table for input data */
51 chr_t_entry *q_input; /* mapping table for queries */
52 unsigned char *output[256]; /* return mapping - for display of registers */
53 int base_uppercase; /* Start of upper-case ordinals */
58 * Character map trie node.
62 chr_t_entry **children; /* array of children */
63 unsigned char **target; /* target for this node, if any */
67 * General argument structure for callback functions (internal use only)
69 typedef struct chrwork
72 char string[CHR_MAXSTR+1];
76 * Callback for equivalent stuff
82 char *eq[CHR_MAXEQUIV];
85 * Add an entry to the character map.
87 static chr_t_entry *set_map_string(chr_t_entry *root, NMEM nmem,
88 const char *from, int len, char *to,
95 root = (chr_t_entry *) nmem_malloc(nmem, sizeof(*root));
101 if (!root->target || !root->target[0] ||
102 strcmp((const char *) root->target[0], to))
105 root->target && root->target[0] && root->target[0][0] &&
106 strcmp((const char *) root->target[0], CHR_UNKNOWN))
108 yaz_log(YLOG_WARN, "duplicate entry for charmap from '%s'",
111 root->target = (unsigned char **)
112 nmem_malloc(nmem, sizeof(*root->target)*2);
113 root->target[0] = (unsigned char *) nmem_strdup(nmem, to);
123 root->children = (chr_t_entry **)
124 nmem_malloc(nmem, sizeof(chr_t_entry*) * 256);
125 for (i = 0; i < 256; i++)
126 root->children[i] = 0;
128 if (!(root->children[(unsigned char) *from] =
129 set_map_string(root->children[(unsigned char) *from], nmem,
130 from + 1, len - 1, to, from_0)))
136 static chr_t_entry *find_entry(chr_t_entry *t, const char **from, int len)
140 if (len && t->children && t->children[(unsigned char) **from])
142 const char *pos = *from;
145 if ((res = find_entry(t->children[(unsigned char) *pos],
151 /* no children match. use ourselves, if we have a target */
152 return t->target ? t : 0;
155 static chr_t_entry *find_entry_x(chr_t_entry *t, const char **from, int *len, int first)
160 { /* switch to next buffer */
166 if (*len > 0 && t->children)
168 const char *old_from = *from;
173 if (first && t->children[CHR_FIELD_BEGIN])
175 if ((res = find_entry_x(t->children[CHR_FIELD_BEGIN], from, len, 0)) && res != t->children[CHR_FIELD_BEGIN])
179 /* otherwhise there was no match on beginning of field, move on */
182 if (!res && t->children[(unsigned char) **from])
186 if ((res = find_entry_x(t->children[(unsigned char) *old_from],
194 /* no children match. use ourselves, if we have a target */
195 return t->target ? t : 0;
198 const char **chr_map_input_x(chrmaptab maptab, const char **from, int *len, int first)
200 chr_t_entry *t = maptab->input;
203 if (!(res = find_entry_x(t, from, len, first)))
205 return (const char **) (res->target);
208 const char **chr_map_input(chrmaptab maptab, const char **from, int len, int first)
210 chr_t_entry *t = maptab->input;
216 if (!(res = find_entry_x(t, from, len_tmp, first)))
218 return (const char **) (res->target);
221 const char **chr_map_q_input(chrmaptab maptab,
222 const char **from, int len, int first)
224 chr_t_entry *t = maptab->q_input;
230 if (!(res = find_entry_x(t, from, len_tmp, first)))
232 return (const char **) (res->target);
235 const char *chr_map_output(chrmaptab maptab, const char **from, int len)
237 unsigned char c = ** (unsigned char **) from;
238 const char *out = (const char*) maptab->output[c];
245 static int zebra_ucs4_strlen(ucs4_t *s)
253 ucs4_t zebra_prim_w(ucs4_t **s)
259 yaz_log(YLOG_DEBUG, "prim_w %.3s", (char *) *s);
260 if (**s == '\\' && 1[*s])
266 case '\\': c = '\\'; (*s)++; break;
267 case 'r': c = '\r'; (*s)++; break;
268 case 'n': c = '\n'; (*s)++; break;
269 case 't': c = '\t'; (*s)++; break;
270 case 's': c = ' '; (*s)++; break;
272 if (zebra_ucs4_strlen(*s) >= 3)
277 sscanf(fmtstr, "%x", &i);
292 if (zebra_ucs4_strlen(*s) >= 3)
298 sscanf(fmtstr, "%o", &i);
304 if (zebra_ucs4_strlen(*s) >= 5)
311 sscanf(fmtstr, "%x", &i);
325 yaz_log(YLOG_DEBUG, "out %d", c);
331 * Add an entry to the value space.
333 static void fun_addentry(const char *s, void *data, int num)
335 chrmaptab tab = (chrmaptab) data;
338 tmp[0] = num; tmp[1] = '\0';
339 tab->input = set_map_string(tab->input, tab->nmem, s, strlen(s), tmp, 0);
340 tab->output[num + tab->base_uppercase] =
341 (unsigned char *) nmem_strdup(tab->nmem, s);
346 * Add a space-entry to the value space.
348 static void fun_addspace(const char *s, void *data, int num)
350 chrmaptab tab = (chrmaptab) data;
351 tab->input = set_map_string(tab->input, tab->nmem, s, strlen(s),
352 (char*) CHR_SPACE, 0);
357 * Add a space-entry to the value space.
359 static void fun_addcut(const char *s, void *data, int num)
361 chrmaptab tab = (chrmaptab) data;
362 tab->input = set_map_string(tab->input, tab->nmem, s, strlen(s),
367 * Create a string containing the mapped characters provided.
369 static void fun_mkstring(const char *s, void *data, int num)
371 chrwork *arg = (chrwork *) data;
372 const char **res, *p = s;
374 res = chr_map_input(arg->map, &s, strlen(s), 0);
375 if (*res == (char*) CHR_UNKNOWN)
376 yaz_log(YLOG_WARN, "Map: '%s' has no mapping", p);
377 strncat(arg->string, *res, CHR_MAXSTR - strlen(arg->string));
378 arg->string[CHR_MAXSTR] = '\0';
382 * Create an unmodified string (scan_string handler).
384 static void fun_add_equivalent_string(const char *s, void *data, int num)
386 chr_equiv_work *arg = (chr_equiv_work *) data;
388 if (arg->no_eq == CHR_MAXEQUIV)
390 arg->eq[arg->no_eq++] = nmem_strdup(arg->nmem, s);
394 * Add a map to the string contained in the argument.
396 static void fun_add_map(const char *s, void *data, int num)
398 chrwork *arg = (chrwork *) data;
400 assert(arg->map->input);
401 yaz_log(YLOG_DEBUG, "set map %.*s", (int) strlen(s), s);
402 set_map_string(arg->map->input, arg->map->nmem, s, strlen(s), arg->string,
404 for (s = arg->string; *s; s++)
405 yaz_log(YLOG_DEBUG, " %3d", (unsigned char) *s);
408 static int scan_to_utf8(yaz_iconv_t t, ucs4_t *from, size_t inlen,
409 char *outbuf, size_t outbytesleft)
411 size_t inbytesleft = inlen * sizeof(ucs4_t);
412 char *inbuf = (char*) from;
416 *outbuf++ = *from; /* ISO-8859-1 is OK here */
419 ret = yaz_iconv(t, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
420 if (ret != (size_t) (-1))
421 ret = yaz_iconv(t, 0, 0, &outbuf, &outbytesleft);
424 if (ret == (size_t) (-1))
426 yaz_log(YLOG_LOG, "from: %2X %2X %2X %2X",
427 from[0], from[1], from[2], from[3]);
428 yaz_log(YLOG_WARN|YLOG_ERRNO, "bad unicode sequence");
436 static int scan_string(char *s_native,
437 yaz_iconv_t t_unicode, yaz_iconv_t t_utf8,
438 void (*fun)(const char *c, void *data, int num),
439 void *data, int *num)
444 ucs4_t arg_prim[512];
445 ucs4_t *s0, *s = arg;
446 ucs4_t c, begin, end;
451 char *outbuf = (char *) arg;
452 char *inbuf = s_native;
453 size_t outbytesleft = sizeof(arg)-4;
454 size_t inbytesleft = strlen(s_native);
456 ret = yaz_iconv(t_unicode, &inbuf, &inbytesleft,
457 &outbuf, &outbytesleft);
458 if (ret != (size_t)(-1))
459 ret = yaz_iconv(t_unicode, 0, 0, &outbuf, &outbytesleft);
461 if (ret == (size_t)(-1))
463 i = (outbuf - (char*) arg)/sizeof(ucs4_t);
467 for (i = 0; s_native[i]; i++)
468 arg[i] = s_native[i] & 255; /* ISO-8859-1 conversion */
470 arg[i] = 0; /* terminate */
471 if (s[0] == 0xfeff || s[0] == 0xfeff) /* skip byte Order Mark */
479 begin = zebra_prim_w(&s);
482 yaz_log(YLOG_FATAL, "Bad range in char-map");
486 end = zebra_prim_w(&s);
489 yaz_log(YLOG_FATAL, "Bad range in char-map");
493 for (c = begin; c <= end; c++)
495 if (scan_to_utf8(t_utf8, &c, 1, str, sizeof(str)-1))
497 (*fun)(str, data, num ? (*num)++ : 0);
503 while (*s != ')' || s[-1] == '\\')
504 arg_prim[i++] = zebra_prim_w(&s);
506 if (scan_to_utf8(t_utf8, arg_prim, zebra_ucs4_strlen(arg_prim), str, sizeof(str)-1))
508 (*fun)(str, data, num ? (*num)++ : 0);
512 c = zebra_prim_w(&s);
513 if (scan_to_utf8(t_utf8, &c, 1, str, sizeof(str)-1))
515 (*fun)(str, data, num ? (*num)++ : 0);
521 chrmaptab chrmaptab_create(const char *tabpath, const char *name,
525 char line[512], *argv[50];
529 int argc, num = (int) *CHR_BASE, i;
531 yaz_iconv_t t_unicode = 0;
532 yaz_iconv_t t_utf8 = 0;
533 unsigned endian = 31;
534 const char *ucs4_native = "UCS-4";
536 yaz_log(YLOG_DEBUG, "maptab %s open", name);
537 if (!(f = yaz_fopen(tabpath, name, "r", tabroot)))
539 yaz_log(YLOG_WARN|YLOG_ERRNO, "%s", name);
543 if (*(char*) &endian == 31) /* little endian? */
544 ucs4_native = "UCS-4LE";
546 t_utf8 = yaz_iconv_open("UTF-8", ucs4_native);
548 nmem = nmem_create();
549 res = (chrmaptab) nmem_malloc(nmem, sizeof(*res));
551 res->input = (chr_t_entry *) nmem_malloc(res->nmem, sizeof(*res->input));
552 res->input->target = (unsigned char **)
553 nmem_malloc(res->nmem, sizeof(*res->input->target) * 2);
554 res->input->target[0] = (unsigned char*) CHR_UNKNOWN;
555 res->input->target[1] = 0;
556 res->input->children = (chr_t_entry **)
557 nmem_malloc(res->nmem, sizeof(res->input) * 256);
558 for (i = 0; i < 256; i++)
560 res->input->children[i] = (chr_t_entry *)
561 nmem_malloc(res->nmem, sizeof(*res->input));
562 res->input->children[i]->children = 0;
563 res->input->children[i]->target = (unsigned char **)
564 nmem_malloc(res->nmem, 2 * sizeof(unsigned char *));
565 res->input->children[i]->target[1] = 0;
566 res->input->children[i]->target[0] = (unsigned char*) CHR_UNKNOWN;
568 res->q_input = (chr_t_entry *)
569 nmem_malloc(res->nmem, sizeof(*res->q_input));
570 res->q_input->target = 0;
571 res->q_input->children = 0;
573 for (i = *CHR_BASE; i < 256; i++)
575 res->output[(int) *CHR_SPACE] = (unsigned char *) " ";
576 res->output[(int) *CHR_UNKNOWN] = (unsigned char*) "@";
577 res->base_uppercase = 0;
579 while (!errors && (argc = readconf_line(f, &lineno, line, 512, argv, 50)))
580 if (!yaz_matchstr(argv[0], "lowercase"))
584 yaz_log(YLOG_FATAL, "Syntax error in charmap");
587 if (scan_string(argv[1], t_unicode, t_utf8, fun_addentry,
590 yaz_log(YLOG_FATAL, "Bad value-set specification");
593 res->base_uppercase = num;
594 res->output[(int) *CHR_SPACE + num] = (unsigned char *) " ";
595 res->output[(int) *CHR_UNKNOWN + num] = (unsigned char*) "@";
596 num = (int) *CHR_BASE;
598 else if (!yaz_matchstr(argv[0], "uppercase"))
600 if (!res->base_uppercase)
602 yaz_log(YLOG_FATAL, "Uppercase directive with no lowercase set");
607 yaz_log(YLOG_FATAL, "Missing arg for uppercase directive");
610 if (scan_string(argv[1], t_unicode, t_utf8, fun_addentry,
613 yaz_log(YLOG_FATAL, "Bad value-set specification");
617 else if (!yaz_matchstr(argv[0], "space"))
621 yaz_log(YLOG_FATAL, "Syntax error in charmap for space");
624 if (scan_string(argv[1], t_unicode, t_utf8,
625 fun_addspace, res, 0) < 0)
627 yaz_log(YLOG_FATAL, "Bad space specification");
631 else if (!yaz_matchstr(argv[0], "cut"))
635 yaz_log(YLOG_FATAL, "Syntax error in charmap for cut");
638 if (scan_string(argv[1], t_unicode, t_utf8,
639 fun_addcut, res, 0) < 0)
641 yaz_log(YLOG_FATAL, "Bad cut specification");
645 else if (!yaz_matchstr(argv[0], "map"))
651 yaz_log(YLOG_FATAL, "charmap directive map requires 2 args");
655 buf.string[0] = '\0';
656 if (scan_string(argv[2], t_unicode, t_utf8,
657 fun_mkstring, &buf, 0) < 0)
659 yaz_log(YLOG_FATAL, "Bad map target");
662 if (scan_string(argv[1], t_unicode, t_utf8,
663 fun_add_map, &buf, 0) < 0)
665 yaz_log(YLOG_FATAL, "Bad map source");
669 else if (!yaz_matchstr(argv[0], "equivalent"))
675 yaz_log(YLOG_FATAL, "equivalent requires 1 argument");
680 if (scan_string(argv[1], t_unicode, t_utf8,
681 fun_add_equivalent_string, &w, 0) < 0)
683 yaz_log(YLOG_FATAL, "equivalent: invalid string");
686 else if (w.no_eq == 0)
688 yaz_log(YLOG_FATAL, "equivalent: no strings");
696 /* determine length of regular expression */
697 for (i = 0; i<w.no_eq; i++)
698 slen += strlen(w.eq[i]) + 1;
699 result_str = nmem_malloc(res->nmem, slen + 5);
701 /* build the regular expression */
704 for (i = 0; i<w.no_eq; i++)
706 result_str[slen++] = i ? '|' : '(';
707 strcpy(result_str + slen, w.eq[i]);
708 slen += strlen(w.eq[i]);
710 result_str[slen++] = ')';
711 result_str[slen] = '\0';
713 /* each eq will map to this regular expression */
714 for (i = 0; i<w.no_eq; i++)
716 set_map_string(res->q_input, res->nmem,
717 w.eq[i], strlen(w.eq[i]),
722 else if (!yaz_matchstr(argv[0], "encoding"))
725 * Fix me. When t_unicode==0 and use encoding directive in *.chr file the beheviour of the
726 * zebra need to comment next part of code.
732 yaz_iconv_close(t_unicode);
733 t_unicode = yaz_iconv_open(ucs4_native, argv[1]);
736 * Fix me. It is additional staff for conversion of characters from local encoding
737 * of *.chr file to UTF-8 (internal encoding).
738 * NOTE: The derective encoding must be first directive in *.chr file.
740 /* For whatever reason Oleg enabled this.. */
743 yaz_iconv_close(t_utf8);
744 t_utf8 = yaz_iconv_open("UTF-8", argv[1]);
749 yaz_log(YLOG_WARN, "Syntax error at '%s' in %s", line, name);
755 chrmaptab_destroy(res);
758 yaz_log(YLOG_DEBUG, "maptab %s close %d errors", name, errors);
760 yaz_iconv_close(t_utf8);
762 yaz_iconv_close(t_unicode);
766 void chrmaptab_destroy(chrmaptab tab)
769 nmem_destroy(tab->nmem);
776 * indent-tabs-mode: nil
778 * vim: shiftwidth=4 tabstop=8 expandtab