1 /* $Id: charmap.c,v 1.29 2004-07-28 09:47:42 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;
36 #include <yaz/yaz-util.h>
40 #define CHR_MAXSTR 1024
41 #define CHR_MAXEQUIV 32
43 const char *CHR_UNKNOWN = "\001";
44 const char *CHR_SPACE = "\002";
45 const char *CHR_CUT = "\003";
46 const char *CHR_BASE = "\005";
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 * Add an entry to the character map.
78 static chr_t_entry *set_map_string(chr_t_entry *root, NMEM nmem,
79 const char *from, int len, char *to,
86 root = (chr_t_entry *) nmem_malloc(nmem, sizeof(*root));
92 if (!root->target || !root->target[0] || strcmp(root->target[0], to))
95 root->target && root->target[0] && root->target[0][0] &&
96 strcmp (root->target[0], CHR_UNKNOWN))
98 yaz_log (LOG_WARN, "duplicate entry for charmap from '%s'",
101 root->target = (unsigned char **)
102 nmem_malloc(nmem, sizeof(*root->target)*2);
103 root->target[0] = (unsigned char *) nmem_strdup(nmem, to);
113 root->children = (chr_t_entry **)
114 nmem_malloc(nmem, sizeof(chr_t_entry*) * 256);
115 for (i = 0; i < 256; i++)
116 root->children[i] = 0;
118 if (!(root->children[(unsigned char) *from] =
119 set_map_string(root->children[(unsigned char) *from], nmem,
120 from + 1, len - 1, to, from_0)))
126 static chr_t_entry *find_entry(chr_t_entry *t, const char **from, int len)
130 if (len && t->children && t->children[(unsigned char) **from])
132 const char *pos = *from;
135 if ((res = find_entry(t->children[(unsigned char) *pos],
141 /* no children match. use ourselves, if we have a target */
142 return t->target ? t : 0;
145 static chr_t_entry *find_entry_x(chr_t_entry *t, const char **from, int *len)
150 { /* switch to next buffer */
156 if (*len > 0 && t->children && t->children[(unsigned char) **from])
158 const char *old_from = *from;
163 if ((res = find_entry_x(t->children[(unsigned char) *old_from],
170 /* no children match. use ourselves, if we have a target */
171 return t->target ? t : 0;
174 const char **chr_map_input_x(chrmaptab maptab, const char **from, int *len)
176 chr_t_entry *t = maptab->input;
179 if (!(res = find_entry_x(t, from, len)))
181 return (const char **) (res->target);
184 const char **chr_map_input(chrmaptab maptab, const char **from, int len)
186 chr_t_entry *t = maptab->input;
192 if (!(res = find_entry_x(t, from, len_tmp)))
194 return (const char **) (res->target);
197 const char *chr_map_output(chrmaptab maptab, const char **from, int len)
199 unsigned char c = ** (unsigned char **) from;
201 return (const char*) maptab->output[c];
204 unsigned char zebra_prim(char **s)
209 yaz_log (LOG_DEBUG, "prim %.3s", *s);
216 case '\\': c = '\\'; (*s)++; break;
217 case 'r': c = '\r'; (*s)++; break;
218 case 'n': c = '\n'; (*s)++; break;
219 case 't': c = '\t'; (*s)++; break;
220 case 's': c = ' '; (*s)++; break;
221 case 'x': sscanf(*s, "x%2x", &i); c = i; *s += 3; break;
232 sscanf(*s, "%3o", &i);
248 static int zebra_ucs4_strlen(ucs4_t *s)
256 ucs4_t zebra_prim_w(ucs4_t **s)
262 yaz_log (LOG_DEBUG, "prim %.3s", (char *) *s);
269 case '\\': c = '\\'; (*s)++; break;
270 case 'r': c = '\r'; (*s)++; break;
271 case 'n': c = '\n'; (*s)++; break;
272 case 't': c = '\t'; (*s)++; break;
273 case 's': c = ' '; (*s)++; break;
275 if (zebra_ucs4_strlen(*s) >= 3)
280 sscanf(fmtstr, "%x", &i);
295 if (zebra_ucs4_strlen(*s) >= 3)
301 sscanf(fmtstr, "%o", &i);
307 if (zebra_ucs4_strlen(*s) >= 5)
314 sscanf(fmtstr, "%x", &i);
328 yaz_log (LOG_DEBUG, "out %d", c);
334 * Add an entry to the value space.
336 static void fun_addentry(const char *s, void *data, int num)
338 chrmaptab tab = (chrmaptab) data;
341 tmp[0] = num; tmp[1] = '\0';
342 tab->input = set_map_string(tab->input, tab->nmem, s, strlen(s), tmp, 0);
343 tab->output[num + tab->base_uppercase] =
344 (unsigned char *) nmem_strdup(tab->nmem, s);
349 * Add a space-entry to the value space.
351 static void fun_addspace(const char *s, void *data, int num)
353 chrmaptab tab = (chrmaptab) data;
354 tab->input = set_map_string(tab->input, tab->nmem, s, strlen(s),
355 (char*) CHR_SPACE, 0);
360 * Add a space-entry to the value space.
362 static void fun_addcut(const char *s, void *data, int num)
364 chrmaptab tab = (chrmaptab) data;
365 tab->input = set_map_string(tab->input, tab->nmem, s, strlen(s),
370 * Create a string containing the mapped characters provided.
372 static void fun_mkstring(const char *s, void *data, int num)
374 chrwork *arg = (chrwork *) data;
375 const char **res, *p = s;
377 res = chr_map_input(arg->map, &s, strlen(s));
378 if (*res == (char*) CHR_UNKNOWN)
379 logf(LOG_WARN, "Map: '%s' has no mapping", p);
380 strncat(arg->string, *res, CHR_MAXSTR - strlen(arg->string));
381 arg->string[CHR_MAXSTR] = '\0';
385 * Add a map to the string contained in the argument.
387 static void fun_add_map(const char *s, void *data, int num)
389 chrwork *arg = (chrwork *) data;
391 assert(arg->map->input);
392 logf (LOG_DEBUG, "set map %.*s", (int) strlen(s), s);
393 set_map_string(arg->map->input, arg->map->nmem, s, strlen(s), arg->string,
395 for (s = arg->string; *s; s++)
396 logf (LOG_DEBUG, " %3d", (unsigned char) *s);
400 * Add a query map to the string contained in the argument.
402 static void fun_add_qmap(const char *s, void *data, int num)
404 chrwork *arg = (chrwork *) data;
406 assert(arg->map->q_input);
407 logf (LOG_DEBUG, "set qmap %.*s", (int) strlen(s), s);
408 set_map_string(arg->map->q_input, arg->map->nmem, s,
409 strlen(s), arg->string, 0);
410 for (s = arg->string; *s; s++)
411 logf (LOG_DEBUG, " %3d", (unsigned char) *s);
414 static int scan_to_utf8 (yaz_iconv_t t, ucs4_t *from, size_t inlen,
415 char *outbuf, size_t outbytesleft)
417 size_t inbytesleft = inlen * sizeof(ucs4_t);
418 char *inbuf = (char*) from;
422 *outbuf++ = *from; /* ISO-8859-1 is OK here */
425 ret = yaz_iconv (t, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
426 if (ret == (size_t) (-1))
428 yaz_log(LOG_LOG, "from: %2X %2X %2X %2X",
429 from[0], from[1], from[2], from[3]);
430 yaz_log (LOG_WARN|LOG_ERRNO, "bad unicode sequence");
438 static int scan_string(char *s_native,
439 yaz_iconv_t t_unicode, yaz_iconv_t t_utf8,
440 void (*fun)(const char *c, void *data, int num),
441 void *data, int *num)
446 ucs4_t *s0, *s = arg;
447 ucs4_t c, begin, end;
452 char *outbuf = (char *) arg;
453 char *inbuf = s_native;
454 size_t outbytesleft = sizeof(arg)-4;
455 size_t inbytesleft = strlen(s_native);
457 ret = yaz_iconv(t_unicode, &inbuf, &inbytesleft,
458 &outbuf, &outbytesleft);
459 if (ret == (size_t)(-1))
461 i = (outbuf - (char*) arg)/sizeof(ucs4_t);
465 for (i = 0; s_native[i]; i++)
466 arg[i] = s_native[i] & 255; /* ISO-8859-1 conversion */
468 arg[i] = 0; /* terminate */
469 if (s[0] == 0xfeff || s[0] == 0xfeff) /* skip byte Order Mark */
477 begin = zebra_prim_w(&s);
480 logf(LOG_FATAL, "Bad range in char-map");
484 end = zebra_prim_w(&s);
487 logf(LOG_FATAL, "Bad range in char-map");
491 for (c = begin; c <= end; c++)
493 if (scan_to_utf8 (t_utf8, &c, 1, str, sizeof(str)-1))
495 (*fun)(str, data, num ? (*num)++ : 0);
498 case '[': s++; abort(); break;
502 while (*s != ')' || s[-1] == '\\')
505 if (scan_to_utf8 (t_utf8, s0, s - s0, str, sizeof(str)-1))
507 (*fun)(str, data, num ? (*num)++ : 0);
511 c = zebra_prim_w(&s);
512 if (scan_to_utf8 (t_utf8, &c, 1, str, sizeof(str)-1))
514 (*fun)(str, data, num ? (*num)++ : 0);
520 chrmaptab chrmaptab_create(const char *tabpath, const char *name, int map_only,
524 char line[512], *argv[50];
528 int argc, num = (int) *CHR_BASE, i;
530 yaz_iconv_t t_unicode = 0;
531 yaz_iconv_t t_utf8 = 0;
532 unsigned endian = 31;
533 const char *ucs4_native = "UCS-4";
535 if (*(char*) &endian == 31) /* little endian? */
536 ucs4_native = "UCS-4LE";
538 t_utf8 = yaz_iconv_open ("UTF-8", ucs4_native);
540 logf (LOG_DEBUG, "maptab %s open", name);
541 if (!(f = yaz_fopen(tabpath, name, "r", tabroot)))
543 logf(LOG_WARN|LOG_ERRNO, "%s", name);
546 nmem = nmem_create ();
547 res = (chrmaptab) nmem_malloc(nmem, sizeof(*res));
549 res->input = (chr_t_entry *) nmem_malloc(res->nmem, sizeof(*res->input));
550 res->input->target = (unsigned char **)
551 nmem_malloc(res->nmem, sizeof(*res->input->target) * 2);
552 res->input->target[0] = (unsigned char*) CHR_UNKNOWN;
553 res->input->target[1] = 0;
554 res->input->children = (chr_t_entry **)
555 nmem_malloc(res->nmem, sizeof(res->input) * 256);
556 for (i = 0; i < 256; i++)
558 res->input->children[i] = (chr_t_entry *)
559 nmem_malloc(res->nmem, sizeof(*res->input));
560 res->input->children[i]->children = 0;
561 res->input->children[i]->target = (unsigned char **)
562 nmem_malloc (res->nmem, 2 * sizeof(unsigned char *));
563 res->input->children[i]->target[1] = 0;
566 res->input->children[i]->target[0] = (unsigned char *)
567 nmem_malloc (res->nmem, 2 * sizeof(unsigned char));
568 res->input->children[i]->target[0][0] = i;
569 res->input->children[i]->target[0][1] = 0;
572 res->input->children[i]->target[0] = (unsigned char*) CHR_UNKNOWN;
574 res->q_input = (chr_t_entry *)
575 nmem_malloc(res->nmem, sizeof(*res->q_input));
576 res->q_input->target = 0;
577 res->q_input->children = 0;
579 for (i = *CHR_BASE; i < 256; i++)
581 res->output[(int) *CHR_SPACE] = (unsigned char *) " ";
582 res->output[(int) *CHR_UNKNOWN] = (unsigned char*) "@";
583 res->base_uppercase = 0;
585 while (!errors && (argc = readconf_line(f, &lineno, line, 512, argv, 50)))
586 if (!map_only && !yaz_matchstr(argv[0], "lowercase"))
590 logf(LOG_FATAL, "Syntax error in charmap");
593 if (scan_string(argv[1], t_unicode, t_utf8, fun_addentry,
596 logf(LOG_FATAL, "Bad value-set specification");
599 res->base_uppercase = num;
600 res->output[(int) *CHR_SPACE + num] = (unsigned char *) " ";
601 res->output[(int) *CHR_UNKNOWN + num] = (unsigned char*) "@";
602 num = (int) *CHR_BASE;
604 else if (!map_only && !yaz_matchstr(argv[0], "uppercase"))
606 if (!res->base_uppercase)
608 logf(LOG_FATAL, "Uppercase directive with no lowercase set");
613 logf(LOG_FATAL, "Missing arg for uppercase directive");
616 if (scan_string(argv[1], t_unicode, t_utf8, fun_addentry,
619 logf(LOG_FATAL, "Bad value-set specification");
623 else if (!map_only && !yaz_matchstr(argv[0], "space"))
627 logf(LOG_FATAL, "Syntax error in charmap for space");
630 if (scan_string(argv[1], t_unicode, t_utf8,
631 fun_addspace, res, 0) < 0)
633 logf(LOG_FATAL, "Bad space specification");
637 else if (!map_only && !yaz_matchstr(argv[0], "cut"))
641 logf(LOG_FATAL, "Syntax error in charmap for cut");
644 if (scan_string(argv[1], t_unicode, t_utf8,
645 fun_addcut, res, 0) < 0)
647 logf(LOG_FATAL, "Bad cut specification");
651 else if (!yaz_matchstr(argv[0], "map"))
657 logf(LOG_FATAL, "charmap directive map requires 2 args");
661 buf.string[0] = '\0';
662 if (scan_string(argv[2], t_unicode, t_utf8,
663 fun_mkstring, &buf, 0) < 0)
665 logf(LOG_FATAL, "Bad map target");
668 if (scan_string(argv[1], t_unicode, t_utf8,
669 fun_add_map, &buf, 0) < 0)
671 logf(LOG_FATAL, "Bad map source");
675 else if (!yaz_matchstr(argv[0], "qmap"))
681 logf(LOG_FATAL, "charmap directive qmap requires 2 args");
685 buf.string[0] = '\0';
686 if (scan_string(argv[2], t_unicode, t_utf8,
687 fun_mkstring, &buf, 0) < 0)
689 logf(LOG_FATAL, "Bad qmap target");
692 if (scan_string(argv[1], t_unicode, t_utf8,
693 fun_add_qmap, &buf, 0) < 0)
695 logf(LOG_FATAL, "Bad qmap source");
699 else if (!yaz_matchstr(argv[0], "encoding"))
702 * Fix me. When t_unicode==0 and use encoding directive in *.chr file the beheviour of the
703 * zebra need to comment next part of code.
709 yaz_iconv_close (t_unicode);
710 t_unicode = yaz_iconv_open (ucs4_native, argv[1]);
713 * Fix me. It is additional staff for conversion of characters from local encoding
714 * of *.chr file to UTF-8 (internal encoding).
715 * NOTE: The derective encoding must be first directive in *.chr file.
717 /* For whatever reason Oleg enabled this.. */
720 yaz_iconv_close(t_utf8);
721 t_utf8 = yaz_iconv_open ("UTF-8", argv[1]);
726 logf(LOG_WARN, "Syntax error at '%s' in %s", line, name);
732 chrmaptab_destroy(res);
735 logf (LOG_DEBUG, "maptab %s close %d errors", name, errors);
737 yaz_iconv_close(t_utf8);
739 yaz_iconv_close(t_unicode);
743 void chrmaptab_destroy(chrmaptab tab)
746 nmem_destroy (tab->nmem);