1 /* $Id: charmap.c,v 1.33 2004-12-13 20:51:33 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
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
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";
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 * Add an entry to the character map.
80 static chr_t_entry *set_map_string(chr_t_entry *root, NMEM nmem,
81 const char *from, int len, char *to,
88 root = (chr_t_entry *) nmem_malloc(nmem, sizeof(*root));
94 if (!root->target || !root->target[0] || strcmp(root->target[0], to))
97 root->target && root->target[0] && root->target[0][0] &&
98 strcmp (root->target[0], CHR_UNKNOWN))
100 yaz_log (YLOG_WARN, "duplicate entry for charmap from '%s'",
103 root->target = (unsigned char **)
104 nmem_malloc(nmem, sizeof(*root->target)*2);
105 root->target[0] = (unsigned char *) nmem_strdup(nmem, to);
115 root->children = (chr_t_entry **)
116 nmem_malloc(nmem, sizeof(chr_t_entry*) * 256);
117 for (i = 0; i < 256; i++)
118 root->children[i] = 0;
120 if (!(root->children[(unsigned char) *from] =
121 set_map_string(root->children[(unsigned char) *from], nmem,
122 from + 1, len - 1, to, from_0)))
128 static chr_t_entry *find_entry(chr_t_entry *t, const char **from, int len)
132 if (len && t->children && t->children[(unsigned char) **from])
134 const char *pos = *from;
137 if ((res = find_entry(t->children[(unsigned char) *pos],
143 /* no children match. use ourselves, if we have a target */
144 return t->target ? t : 0;
147 static chr_t_entry *find_entry_x(chr_t_entry *t, const char **from, int *len, int first)
152 { /* switch to next buffer */
158 if (*len > 0 && t->children)
160 const char *old_from = *from;
165 if (first && t->children[CHR_FIELD_BEGIN])
167 if ((res = find_entry_x(t->children[CHR_FIELD_BEGIN], from, len, 0)) && res != t->children[CHR_FIELD_BEGIN])
171 /* otherwhise there was no match on beginning of field, move on */
174 if (!res && t->children[(unsigned char) **from])
178 if ((res = find_entry_x(t->children[(unsigned char) *old_from],
186 /* no children match. use ourselves, if we have a target */
187 return t->target ? t : 0;
190 const char **chr_map_input_x(chrmaptab maptab, const char **from, int *len, int first)
192 chr_t_entry *t = maptab->input;
195 if (!(res = find_entry_x(t, from, len, first)))
197 return (const char **) (res->target);
200 const char **chr_map_input(chrmaptab maptab, const char **from, int len, int first)
202 chr_t_entry *t = maptab->input;
208 if (!(res = find_entry_x(t, from, len_tmp, first)))
210 return (const char **) (res->target);
213 const char *chr_map_output(chrmaptab maptab, const char **from, int len)
215 unsigned char c = ** (unsigned char **) from;
217 return (const char*) maptab->output[c];
220 unsigned char zebra_prim(char **s)
225 yaz_log (YLOG_DEBUG, "prim %.3s", *s);
232 case '\\': c = '\\'; (*s)++; break;
233 case 'r': c = '\r'; (*s)++; break;
234 case 'n': c = '\n'; (*s)++; break;
235 case 't': c = '\t'; (*s)++; break;
236 case 's': c = ' '; (*s)++; break;
237 case 'x': sscanf(*s, "x%2x", &i); c = i; *s += 3; break;
248 sscanf(*s, "%3o", &i);
264 static int zebra_ucs4_strlen(ucs4_t *s)
272 ucs4_t zebra_prim_w(ucs4_t **s)
278 yaz_log (YLOG_DEBUG, "prim_w %.3s", (char *) *s);
285 case '\\': c = '\\'; (*s)++; break;
286 case 'r': c = '\r'; (*s)++; break;
287 case 'n': c = '\n'; (*s)++; break;
288 case 't': c = '\t'; (*s)++; break;
289 case 's': c = ' '; (*s)++; break;
291 if (zebra_ucs4_strlen(*s) >= 3)
296 sscanf(fmtstr, "%x", &i);
311 if (zebra_ucs4_strlen(*s) >= 3)
317 sscanf(fmtstr, "%o", &i);
323 if (zebra_ucs4_strlen(*s) >= 5)
330 sscanf(fmtstr, "%x", &i);
344 yaz_log (YLOG_DEBUG, "out %d", c);
350 * Add an entry to the value space.
352 static void fun_addentry(const char *s, void *data, int num)
354 chrmaptab tab = (chrmaptab) data;
357 tmp[0] = num; tmp[1] = '\0';
358 tab->input = set_map_string(tab->input, tab->nmem, s, strlen(s), tmp, 0);
359 tab->output[num + tab->base_uppercase] =
360 (unsigned char *) nmem_strdup(tab->nmem, s);
365 * Add a space-entry to the value space.
367 static void fun_addspace(const char *s, void *data, int num)
369 chrmaptab tab = (chrmaptab) data;
370 tab->input = set_map_string(tab->input, tab->nmem, s, strlen(s),
371 (char*) CHR_SPACE, 0);
376 * Add a space-entry to the value space.
378 static void fun_addcut(const char *s, void *data, int num)
380 chrmaptab tab = (chrmaptab) data;
381 tab->input = set_map_string(tab->input, tab->nmem, s, strlen(s),
386 * Create a string containing the mapped characters provided.
388 static void fun_mkstring(const char *s, void *data, int num)
390 chrwork *arg = (chrwork *) data;
391 const char **res, *p = s;
393 res = chr_map_input(arg->map, &s, strlen(s), 0);
394 if (*res == (char*) CHR_UNKNOWN)
395 yaz_log(YLOG_WARN, "Map: '%s' has no mapping", p);
396 strncat(arg->string, *res, CHR_MAXSTR - strlen(arg->string));
397 arg->string[CHR_MAXSTR] = '\0';
401 * Add a map to the string contained in the argument.
403 static void fun_add_map(const char *s, void *data, int num)
405 chrwork *arg = (chrwork *) data;
407 assert(arg->map->input);
408 yaz_log (YLOG_DEBUG, "set map %.*s", (int) strlen(s), s);
409 set_map_string(arg->map->input, arg->map->nmem, s, strlen(s), arg->string,
411 for (s = arg->string; *s; s++)
412 yaz_log (YLOG_DEBUG, " %3d", (unsigned char) *s);
416 * Add a query map to the string contained in the argument.
418 static void fun_add_qmap(const char *s, void *data, int num)
420 chrwork *arg = (chrwork *) data;
422 assert(arg->map->q_input);
423 yaz_log (YLOG_DEBUG, "set qmap %.*s", (int) strlen(s), s);
424 set_map_string(arg->map->q_input, arg->map->nmem, s,
425 strlen(s), arg->string, 0);
426 for (s = arg->string; *s; s++)
427 yaz_log (YLOG_DEBUG, " %3d", (unsigned char) *s);
430 static int scan_to_utf8 (yaz_iconv_t t, ucs4_t *from, size_t inlen,
431 char *outbuf, size_t outbytesleft)
433 size_t inbytesleft = inlen * sizeof(ucs4_t);
434 char *inbuf = (char*) from;
438 *outbuf++ = *from; /* ISO-8859-1 is OK here */
441 ret = yaz_iconv (t, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
442 if (ret == (size_t) (-1))
444 yaz_log(YLOG_LOG, "from: %2X %2X %2X %2X",
445 from[0], from[1], from[2], from[3]);
446 yaz_log (YLOG_WARN|YLOG_ERRNO, "bad unicode sequence");
454 static int scan_string(char *s_native,
455 yaz_iconv_t t_unicode, yaz_iconv_t t_utf8,
456 void (*fun)(const char *c, void *data, int num),
457 void *data, int *num)
462 ucs4_t arg_prim[512];
463 ucs4_t *s0, *s = arg;
464 ucs4_t c, begin, end;
469 char *outbuf = (char *) arg;
470 char *inbuf = s_native;
471 size_t outbytesleft = sizeof(arg)-4;
472 size_t inbytesleft = strlen(s_native);
474 ret = yaz_iconv(t_unicode, &inbuf, &inbytesleft,
475 &outbuf, &outbytesleft);
476 if (ret == (size_t)(-1))
478 i = (outbuf - (char*) arg)/sizeof(ucs4_t);
482 for (i = 0; s_native[i]; i++)
483 arg[i] = s_native[i] & 255; /* ISO-8859-1 conversion */
485 arg[i] = 0; /* terminate */
486 if (s[0] == 0xfeff || s[0] == 0xfeff) /* skip byte Order Mark */
494 begin = zebra_prim_w(&s);
497 yaz_log(YLOG_FATAL, "Bad range in char-map");
501 end = zebra_prim_w(&s);
504 yaz_log(YLOG_FATAL, "Bad range in char-map");
508 for (c = begin; c <= end; c++)
510 if (scan_to_utf8 (t_utf8, &c, 1, str, sizeof(str)-1))
512 (*fun)(str, data, num ? (*num)++ : 0);
515 case '[': s++; abort(); break;
519 while (*s != ')' || s[-1] == '\\')
520 arg_prim[i++] = zebra_prim_w(&s);
522 if (scan_to_utf8 (t_utf8, arg_prim, zebra_ucs4_strlen(arg_prim), str, sizeof(str)-1))
524 (*fun)(str, data, num ? (*num)++ : 0);
528 c = zebra_prim_w(&s);
529 if (scan_to_utf8 (t_utf8, &c, 1, str, sizeof(str)-1))
531 (*fun)(str, data, num ? (*num)++ : 0);
537 chrmaptab chrmaptab_create(const char *tabpath, const char *name, int map_only,
541 char line[512], *argv[50];
545 int argc, num = (int) *CHR_BASE, i;
547 yaz_iconv_t t_unicode = 0;
548 yaz_iconv_t t_utf8 = 0;
549 unsigned endian = 31;
550 const char *ucs4_native = "UCS-4";
552 if (*(char*) &endian == 31) /* little endian? */
553 ucs4_native = "UCS-4LE";
555 t_utf8 = yaz_iconv_open ("UTF-8", ucs4_native);
557 yaz_log (YLOG_DEBUG, "maptab %s open", name);
558 if (!(f = yaz_fopen(tabpath, name, "r", tabroot)))
560 yaz_log(YLOG_WARN|YLOG_ERRNO, "%s", name);
563 nmem = nmem_create ();
564 res = (chrmaptab) nmem_malloc(nmem, sizeof(*res));
566 res->input = (chr_t_entry *) nmem_malloc(res->nmem, sizeof(*res->input));
567 res->input->target = (unsigned char **)
568 nmem_malloc(res->nmem, sizeof(*res->input->target) * 2);
569 res->input->target[0] = (unsigned char*) CHR_UNKNOWN;
570 res->input->target[1] = 0;
571 res->input->children = (chr_t_entry **)
572 nmem_malloc(res->nmem, sizeof(res->input) * 256);
573 for (i = 0; i < 256; i++)
575 res->input->children[i] = (chr_t_entry *)
576 nmem_malloc(res->nmem, sizeof(*res->input));
577 res->input->children[i]->children = 0;
578 res->input->children[i]->target = (unsigned char **)
579 nmem_malloc (res->nmem, 2 * sizeof(unsigned char *));
580 res->input->children[i]->target[1] = 0;
583 res->input->children[i]->target[0] = (unsigned char *)
584 nmem_malloc (res->nmem, 2 * sizeof(unsigned char));
585 res->input->children[i]->target[0][0] = i;
586 res->input->children[i]->target[0][1] = 0;
589 res->input->children[i]->target[0] = (unsigned char*) CHR_UNKNOWN;
591 res->q_input = (chr_t_entry *)
592 nmem_malloc(res->nmem, sizeof(*res->q_input));
593 res->q_input->target = 0;
594 res->q_input->children = 0;
596 for (i = *CHR_BASE; i < 256; i++)
598 res->output[(int) *CHR_SPACE] = (unsigned char *) " ";
599 res->output[(int) *CHR_UNKNOWN] = (unsigned char*) "@";
600 res->base_uppercase = 0;
602 while (!errors && (argc = readconf_line(f, &lineno, line, 512, argv, 50)))
603 if (!map_only && !yaz_matchstr(argv[0], "lowercase"))
607 yaz_log(YLOG_FATAL, "Syntax error in charmap");
610 if (scan_string(argv[1], t_unicode, t_utf8, fun_addentry,
613 yaz_log(YLOG_FATAL, "Bad value-set specification");
616 res->base_uppercase = num;
617 res->output[(int) *CHR_SPACE + num] = (unsigned char *) " ";
618 res->output[(int) *CHR_UNKNOWN + num] = (unsigned char*) "@";
619 num = (int) *CHR_BASE;
621 else if (!map_only && !yaz_matchstr(argv[0], "uppercase"))
623 if (!res->base_uppercase)
625 yaz_log(YLOG_FATAL, "Uppercase directive with no lowercase set");
630 yaz_log(YLOG_FATAL, "Missing arg for uppercase directive");
633 if (scan_string(argv[1], t_unicode, t_utf8, fun_addentry,
636 yaz_log(YLOG_FATAL, "Bad value-set specification");
640 else if (!map_only && !yaz_matchstr(argv[0], "space"))
644 yaz_log(YLOG_FATAL, "Syntax error in charmap for space");
647 if (scan_string(argv[1], t_unicode, t_utf8,
648 fun_addspace, res, 0) < 0)
650 yaz_log(YLOG_FATAL, "Bad space specification");
654 else if (!map_only && !yaz_matchstr(argv[0], "cut"))
658 yaz_log(YLOG_FATAL, "Syntax error in charmap for cut");
661 if (scan_string(argv[1], t_unicode, t_utf8,
662 fun_addcut, res, 0) < 0)
664 yaz_log(YLOG_FATAL, "Bad cut specification");
668 else if (!yaz_matchstr(argv[0], "map"))
674 yaz_log(YLOG_FATAL, "charmap directive map requires 2 args");
678 buf.string[0] = '\0';
679 if (scan_string(argv[2], t_unicode, t_utf8,
680 fun_mkstring, &buf, 0) < 0)
682 yaz_log(YLOG_FATAL, "Bad map target");
685 if (scan_string(argv[1], t_unicode, t_utf8,
686 fun_add_map, &buf, 0) < 0)
688 yaz_log(YLOG_FATAL, "Bad map source");
692 else if (!yaz_matchstr(argv[0], "qmap"))
698 yaz_log(YLOG_FATAL, "charmap directive qmap requires 2 args");
702 buf.string[0] = '\0';
703 if (scan_string(argv[2], t_unicode, t_utf8,
704 fun_mkstring, &buf, 0) < 0)
706 yaz_log(YLOG_FATAL, "Bad qmap target");
709 if (scan_string(argv[1], t_unicode, t_utf8,
710 fun_add_qmap, &buf, 0) < 0)
712 yaz_log(YLOG_FATAL, "Bad qmap source");
716 else if (!yaz_matchstr(argv[0], "encoding"))
719 * Fix me. When t_unicode==0 and use encoding directive in *.chr file the beheviour of the
720 * zebra need to comment next part of code.
726 yaz_iconv_close (t_unicode);
727 t_unicode = yaz_iconv_open (ucs4_native, argv[1]);
730 * Fix me. It is additional staff for conversion of characters from local encoding
731 * of *.chr file to UTF-8 (internal encoding).
732 * NOTE: The derective encoding must be first directive in *.chr file.
734 /* For whatever reason Oleg enabled this.. */
737 yaz_iconv_close(t_utf8);
738 t_utf8 = yaz_iconv_open ("UTF-8", argv[1]);
743 yaz_log(YLOG_WARN, "Syntax error at '%s' in %s", line, name);
749 chrmaptab_destroy(res);
752 yaz_log (YLOG_DEBUG, "maptab %s close %d errors", name, errors);
754 yaz_iconv_close(t_utf8);
756 yaz_iconv_close(t_unicode);
760 void chrmaptab_destroy(chrmaptab tab)
763 nmem_destroy (tab->nmem);