1 /* $Id: charmap.c,v 1.39 2005-08-22 08:17:01 adam Exp $
2 Copyright (C) 1995-2005
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
26 * Support module to handle character-conversions into and out of the
35 typedef unsigned ucs4_t;
39 #include <yaz/yaz-util.h>
41 #define CHR_MAXSTR 1024
42 #define CHR_MAXEQUIV 32
44 const unsigned char CHR_FIELD_BEGIN = '^';
46 const char *CHR_UNKNOWN = "\001";
47 const char *CHR_SPACE = "\002";
48 const char *CHR_CUT = "\003";
49 const char *CHR_BASE = "\005";
53 chr_t_entry *input; /* mapping table for input data */
54 chr_t_entry *q_input; /* mapping table for queries */
55 unsigned char *output[256]; /* return mapping - for display of registers */
56 int base_uppercase; /* Start of upper-case ordinals */
61 * Character map trie node.
65 chr_t_entry **children; /* array of children */
66 unsigned char **target; /* target for this node, if any */
70 * General argument structure for callback functions (internal use only)
72 typedef struct chrwork
75 char string[CHR_MAXSTR+1];
79 * Callback for equivalent stuff
85 char *eq[CHR_MAXEQUIV];
88 * Add an entry to the character map.
90 static chr_t_entry *set_map_string(chr_t_entry *root, NMEM nmem,
91 const char *from, int len, char *to,
98 root = (chr_t_entry *) nmem_malloc(nmem, sizeof(*root));
104 if (!root->target || !root->target[0] ||
105 strcmp((const char *) root->target[0], to))
108 root->target && root->target[0] && root->target[0][0] &&
109 strcmp((const char *) root->target[0], CHR_UNKNOWN))
111 yaz_log (YLOG_WARN, "duplicate entry for charmap from '%s'",
114 root->target = (unsigned char **)
115 nmem_malloc(nmem, sizeof(*root->target)*2);
116 root->target[0] = (unsigned char *) nmem_strdup(nmem, to);
126 root->children = (chr_t_entry **)
127 nmem_malloc(nmem, sizeof(chr_t_entry*) * 256);
128 for (i = 0; i < 256; i++)
129 root->children[i] = 0;
131 if (!(root->children[(unsigned char) *from] =
132 set_map_string(root->children[(unsigned char) *from], nmem,
133 from + 1, len - 1, to, from_0)))
139 static chr_t_entry *find_entry(chr_t_entry *t, const char **from, int len)
143 if (len && t->children && t->children[(unsigned char) **from])
145 const char *pos = *from;
148 if ((res = find_entry(t->children[(unsigned char) *pos],
154 /* no children match. use ourselves, if we have a target */
155 return t->target ? t : 0;
158 static chr_t_entry *find_entry_x(chr_t_entry *t, const char **from, int *len, int first)
163 { /* switch to next buffer */
169 if (*len > 0 && t->children)
171 const char *old_from = *from;
176 if (first && t->children[CHR_FIELD_BEGIN])
178 if ((res = find_entry_x(t->children[CHR_FIELD_BEGIN], from, len, 0)) && res != t->children[CHR_FIELD_BEGIN])
182 /* otherwhise there was no match on beginning of field, move on */
185 if (!res && t->children[(unsigned char) **from])
189 if ((res = find_entry_x(t->children[(unsigned char) *old_from],
197 /* no children match. use ourselves, if we have a target */
198 return t->target ? t : 0;
201 const char **chr_map_input_x(chrmaptab maptab, const char **from, int *len, int first)
203 chr_t_entry *t = maptab->input;
206 if (!(res = find_entry_x(t, from, len, first)))
208 return (const char **) (res->target);
211 const char **chr_map_input(chrmaptab maptab, const char **from, int len, int first)
213 chr_t_entry *t = maptab->input;
219 if (!(res = find_entry_x(t, from, len_tmp, first)))
221 return (const char **) (res->target);
224 const char **chr_map_q_input(chrmaptab maptab,
225 const char **from, int len, int first)
227 chr_t_entry *t = maptab->q_input;
233 if (!(res = find_entry_x(t, from, len_tmp, first)))
235 return (const char **) (res->target);
238 const char *chr_map_output(chrmaptab maptab, const char **from, int len)
240 unsigned char c = ** (unsigned char **) from;
242 return (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))
422 yaz_log(YLOG_LOG, "from: %2X %2X %2X %2X",
423 from[0], from[1], from[2], from[3]);
424 yaz_log (YLOG_WARN|YLOG_ERRNO, "bad unicode sequence");
432 static int scan_string(char *s_native,
433 yaz_iconv_t t_unicode, yaz_iconv_t t_utf8,
434 void (*fun)(const char *c, void *data, int num),
435 void *data, int *num)
440 ucs4_t arg_prim[512];
441 ucs4_t *s0, *s = arg;
442 ucs4_t c, begin, end;
447 char *outbuf = (char *) arg;
448 char *inbuf = s_native;
449 size_t outbytesleft = sizeof(arg)-4;
450 size_t inbytesleft = strlen(s_native);
452 ret = yaz_iconv(t_unicode, &inbuf, &inbytesleft,
453 &outbuf, &outbytesleft);
454 if (ret == (size_t)(-1))
456 i = (outbuf - (char*) arg)/sizeof(ucs4_t);
460 for (i = 0; s_native[i]; i++)
461 arg[i] = s_native[i] & 255; /* ISO-8859-1 conversion */
463 arg[i] = 0; /* terminate */
464 if (s[0] == 0xfeff || s[0] == 0xfeff) /* skip byte Order Mark */
472 begin = zebra_prim_w(&s);
475 yaz_log(YLOG_FATAL, "Bad range in char-map");
479 end = zebra_prim_w(&s);
482 yaz_log(YLOG_FATAL, "Bad range in char-map");
486 for (c = begin; c <= end; c++)
488 if (scan_to_utf8 (t_utf8, &c, 1, str, sizeof(str)-1))
490 (*fun)(str, data, num ? (*num)++ : 0);
496 while (*s != ')' || s[-1] == '\\')
497 arg_prim[i++] = zebra_prim_w(&s);
499 if (scan_to_utf8 (t_utf8, arg_prim, zebra_ucs4_strlen(arg_prim), str, sizeof(str)-1))
501 (*fun)(str, data, num ? (*num)++ : 0);
505 c = zebra_prim_w(&s);
506 if (scan_to_utf8 (t_utf8, &c, 1, str, sizeof(str)-1))
508 (*fun)(str, data, num ? (*num)++ : 0);
514 chrmaptab chrmaptab_create(const char *tabpath, const char *name,
518 char line[512], *argv[50];
522 int argc, num = (int) *CHR_BASE, i;
524 yaz_iconv_t t_unicode = 0;
525 yaz_iconv_t t_utf8 = 0;
526 unsigned endian = 31;
527 const char *ucs4_native = "UCS-4";
529 yaz_log (YLOG_DEBUG, "maptab %s open", name);
530 if (!(f = yaz_fopen(tabpath, name, "r", tabroot)))
532 yaz_log(YLOG_WARN|YLOG_ERRNO, "%s", name);
536 if (*(char*) &endian == 31) /* little endian? */
537 ucs4_native = "UCS-4LE";
539 t_utf8 = yaz_iconv_open ("UTF-8", ucs4_native);
541 nmem = nmem_create ();
542 res = (chrmaptab) nmem_malloc(nmem, sizeof(*res));
544 res->input = (chr_t_entry *) nmem_malloc(res->nmem, sizeof(*res->input));
545 res->input->target = (unsigned char **)
546 nmem_malloc(res->nmem, sizeof(*res->input->target) * 2);
547 res->input->target[0] = (unsigned char*) CHR_UNKNOWN;
548 res->input->target[1] = 0;
549 res->input->children = (chr_t_entry **)
550 nmem_malloc(res->nmem, sizeof(res->input) * 256);
551 for (i = 0; i < 256; i++)
553 res->input->children[i] = (chr_t_entry *)
554 nmem_malloc(res->nmem, sizeof(*res->input));
555 res->input->children[i]->children = 0;
556 res->input->children[i]->target = (unsigned char **)
557 nmem_malloc (res->nmem, 2 * sizeof(unsigned char *));
558 res->input->children[i]->target[1] = 0;
559 res->input->children[i]->target[0] = (unsigned char*) CHR_UNKNOWN;
561 res->q_input = (chr_t_entry *)
562 nmem_malloc(res->nmem, sizeof(*res->q_input));
563 res->q_input->target = 0;
564 res->q_input->children = 0;
566 for (i = *CHR_BASE; i < 256; i++)
568 res->output[(int) *CHR_SPACE] = (unsigned char *) " ";
569 res->output[(int) *CHR_UNKNOWN] = (unsigned char*) "@";
570 res->base_uppercase = 0;
572 while (!errors && (argc = readconf_line(f, &lineno, line, 512, argv, 50)))
573 if (!yaz_matchstr(argv[0], "lowercase"))
577 yaz_log(YLOG_FATAL, "Syntax error in charmap");
580 if (scan_string(argv[1], t_unicode, t_utf8, fun_addentry,
583 yaz_log(YLOG_FATAL, "Bad value-set specification");
586 res->base_uppercase = num;
587 res->output[(int) *CHR_SPACE + num] = (unsigned char *) " ";
588 res->output[(int) *CHR_UNKNOWN + num] = (unsigned char*) "@";
589 num = (int) *CHR_BASE;
591 else if (!yaz_matchstr(argv[0], "uppercase"))
593 if (!res->base_uppercase)
595 yaz_log(YLOG_FATAL, "Uppercase directive with no lowercase set");
600 yaz_log(YLOG_FATAL, "Missing arg for uppercase directive");
603 if (scan_string(argv[1], t_unicode, t_utf8, fun_addentry,
606 yaz_log(YLOG_FATAL, "Bad value-set specification");
610 else if (!yaz_matchstr(argv[0], "space"))
614 yaz_log(YLOG_FATAL, "Syntax error in charmap for space");
617 if (scan_string(argv[1], t_unicode, t_utf8,
618 fun_addspace, res, 0) < 0)
620 yaz_log(YLOG_FATAL, "Bad space specification");
624 else if (!yaz_matchstr(argv[0], "cut"))
628 yaz_log(YLOG_FATAL, "Syntax error in charmap for cut");
631 if (scan_string(argv[1], t_unicode, t_utf8,
632 fun_addcut, res, 0) < 0)
634 yaz_log(YLOG_FATAL, "Bad cut specification");
638 else if (!yaz_matchstr(argv[0], "map"))
644 yaz_log(YLOG_FATAL, "charmap directive map requires 2 args");
648 buf.string[0] = '\0';
649 if (scan_string(argv[2], t_unicode, t_utf8,
650 fun_mkstring, &buf, 0) < 0)
652 yaz_log(YLOG_FATAL, "Bad map target");
655 if (scan_string(argv[1], t_unicode, t_utf8,
656 fun_add_map, &buf, 0) < 0)
658 yaz_log(YLOG_FATAL, "Bad map source");
662 else if (!yaz_matchstr(argv[0], "equivalent"))
668 yaz_log(YLOG_FATAL, "equivalent requires 1 argument");
673 if (scan_string(argv[1], t_unicode, t_utf8,
674 fun_add_equivalent_string, &w, 0) < 0)
676 yaz_log(YLOG_FATAL, "equivalent: invalid string");
679 else if (w.no_eq == 0)
681 yaz_log(YLOG_FATAL, "equivalent: no strings");
689 /* determine length of regular expression */
690 for (i = 0; i<w.no_eq; i++)
691 slen += strlen(w.eq[i]) + 1;
692 result_str = nmem_malloc(res->nmem, slen + 5);
694 /* build the regular expression */
697 for (i = 0; i<w.no_eq; i++)
699 result_str[slen++] = i ? '|' : '(';
700 strcpy(result_str + slen, w.eq[i]);
701 slen += strlen(w.eq[i]);
703 result_str[slen++] = ')';
704 result_str[slen] = '\0';
706 /* each eq will map to this regular expression */
707 for (i = 0; i<w.no_eq; i++)
709 set_map_string(res->q_input, res->nmem,
710 w.eq[i], strlen(w.eq[i]),
715 else if (!yaz_matchstr(argv[0], "encoding"))
718 * Fix me. When t_unicode==0 and use encoding directive in *.chr file the beheviour of the
719 * zebra need to comment next part of code.
725 yaz_iconv_close (t_unicode);
726 t_unicode = yaz_iconv_open (ucs4_native, argv[1]);
729 * Fix me. It is additional staff for conversion of characters from local encoding
730 * of *.chr file to UTF-8 (internal encoding).
731 * NOTE: The derective encoding must be first directive in *.chr file.
733 /* For whatever reason Oleg enabled this.. */
736 yaz_iconv_close(t_utf8);
737 t_utf8 = yaz_iconv_open ("UTF-8", argv[1]);
742 yaz_log(YLOG_WARN, "Syntax error at '%s' in %s", line, name);
748 chrmaptab_destroy(res);
751 yaz_log (YLOG_DEBUG, "maptab %s close %d errors", name, errors);
753 yaz_iconv_close(t_utf8);
755 yaz_iconv_close(t_unicode);
759 void chrmaptab_destroy(chrmaptab tab)
762 nmem_destroy (tab->nmem);