1 /* $Id: charmap.c,v 1.29.2.3 2004-11-26 11:06:13 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;
39 #define CHR_MAXSTR 1024
40 #define CHR_MAXEQUIV 32
42 const unsigned char CHR_FIELD_BEGIN = '^';
44 const char *CHR_UNKNOWN = "\001";
45 const char *CHR_SPACE = "\002";
46 const char *CHR_BASE = "\003";
47 const char *CHR_CUT = "\376"; /* high number to keep BASE stable */
51 chr_t_entry *input; /* mapping table for input data */
52 chr_t_entry *q_input; /* mapping table for queries */
53 unsigned char *output[256]; /* return mapping - for display of registers */
54 int base_uppercase; /* Start of upper-case ordinals */
59 * Character map trie node.
63 chr_t_entry **children; /* array of children */
64 unsigned char **target; /* target for this node, if any */
68 * General argument structure for callback functions (internal use only)
70 typedef struct chrwork
73 char string[CHR_MAXSTR+1];
77 * Add an entry to the character map.
79 static chr_t_entry *set_map_string(chr_t_entry *root, NMEM nmem,
80 const char *from, int len, char *to,
87 root = (chr_t_entry *) nmem_malloc(nmem, sizeof(*root));
93 if (!root->target || !root->target[0] || strcmp(root->target[0], to))
96 root->target && root->target[0] && root->target[0][0] &&
97 strcmp (root->target[0], CHR_UNKNOWN))
99 yaz_log (LOG_WARN, "duplicate entry for charmap from '%s'",
102 root->target = (unsigned char **)
103 nmem_malloc(nmem, sizeof(*root->target)*2);
104 root->target[0] = (unsigned char *) nmem_strdup(nmem, to);
114 root->children = (chr_t_entry **)
115 nmem_malloc(nmem, sizeof(chr_t_entry*) * 256);
116 for (i = 0; i < 256; i++)
117 root->children[i] = 0;
119 if (!(root->children[(unsigned char) *from] =
120 set_map_string(root->children[(unsigned char) *from], nmem,
121 from + 1, len - 1, to, from_0)))
127 static chr_t_entry *find_entry(chr_t_entry *t, const char **from, int len)
131 if (len && t->children && t->children[(unsigned char) **from])
133 const char *pos = *from;
136 if ((res = find_entry(t->children[(unsigned char) *pos],
142 /* no children match. use ourselves, if we have a target */
143 return t->target ? t : 0;
146 static chr_t_entry *find_entry_x(chr_t_entry *t, const char **from, int *len, int first)
151 { /* switch to next buffer */
157 if (*len > 0 && t->children)
159 const char *old_from = *from;
164 if (first && t->children[CHR_FIELD_BEGIN])
166 if ((res = find_entry_x(t->children[CHR_FIELD_BEGIN], from, len, 0)) && res != t->children[CHR_FIELD_BEGIN])
170 /* otherwhise there was no match on beginning of field, move on */
173 if (!res && t->children[(unsigned char) **from])
177 if ((res = find_entry_x(t->children[(unsigned char) *old_from],
185 /* no children match. use ourselves, if we have a target */
186 return t->target ? t : 0;
189 const char **chr_map_input_x(chrmaptab maptab, const char **from, int *len, int first)
191 chr_t_entry *t = maptab->input;
194 if (!(res = find_entry_x(t, from, len, first)))
196 return (const char **) (res->target);
199 const char **chr_map_input(chrmaptab maptab, const char **from, int len, int first)
201 chr_t_entry *t = maptab->input;
207 if (!(res = find_entry_x(t, from, len_tmp, first)))
209 return (const char **) (res->target);
212 const char *chr_map_output(chrmaptab maptab, const char **from, int len)
214 unsigned char c = ** (unsigned char **) from;
216 return (const char*) maptab->output[c];
219 unsigned char zebra_prim(char **s)
224 yaz_log (LOG_DEBUG, "prim %.3s", *s);
231 case '\\': c = '\\'; (*s)++; break;
232 case 'r': c = '\r'; (*s)++; break;
233 case 'n': c = '\n'; (*s)++; break;
234 case 't': c = '\t'; (*s)++; break;
235 case 's': c = ' '; (*s)++; break;
236 case 'x': sscanf(*s, "x%2x", &i); c = i; *s += 3; break;
247 sscanf(*s, "%3o", &i);
263 static int zebra_ucs4_strlen(ucs4_t *s)
271 ucs4_t zebra_prim_w(ucs4_t **s)
277 yaz_log (LOG_DEBUG, "prim_w %.3s", (char *) *s);
284 case '\\': c = '\\'; (*s)++; break;
285 case 'r': c = '\r'; (*s)++; break;
286 case 'n': c = '\n'; (*s)++; break;
287 case 't': c = '\t'; (*s)++; break;
288 case 's': c = ' '; (*s)++; break;
290 if (zebra_ucs4_strlen(*s) >= 3)
295 sscanf(fmtstr, "%x", &i);
310 if (zebra_ucs4_strlen(*s) >= 3)
316 sscanf(fmtstr, "%o", &i);
322 if (zebra_ucs4_strlen(*s) >= 5)
329 sscanf(fmtstr, "%x", &i);
343 yaz_log (LOG_DEBUG, "out %d", c);
349 * Add an entry to the value space.
351 static void fun_addentry(const char *s, void *data, int num)
353 chrmaptab tab = (chrmaptab) data;
356 tmp[0] = num; tmp[1] = '\0';
357 tab->input = set_map_string(tab->input, tab->nmem, s, strlen(s), tmp, 0);
358 tab->output[num + tab->base_uppercase] =
359 (unsigned char *) nmem_strdup(tab->nmem, s);
364 * Add a space-entry to the value space.
366 static void fun_addspace(const char *s, void *data, int num)
368 chrmaptab tab = (chrmaptab) data;
369 tab->input = set_map_string(tab->input, tab->nmem, s, strlen(s),
370 (char*) CHR_SPACE, 0);
375 * Add a space-entry to the value space.
377 static void fun_addcut(const char *s, void *data, int num)
379 chrmaptab tab = (chrmaptab) data;
380 tab->input = set_map_string(tab->input, tab->nmem, s, strlen(s),
385 * Create a string containing the mapped characters provided.
387 static void fun_mkstring(const char *s, void *data, int num)
389 chrwork *arg = (chrwork *) data;
390 const char **res, *p = s;
392 res = chr_map_input(arg->map, &s, strlen(s), 0);
393 if (*res == (char*) CHR_UNKNOWN)
394 logf(LOG_WARN, "Map: '%s' has no mapping", p);
395 strncat(arg->string, *res, CHR_MAXSTR - strlen(arg->string));
396 arg->string[CHR_MAXSTR] = '\0';
400 * Add a map to the string contained in the argument.
402 static void fun_add_map(const char *s, void *data, int num)
404 chrwork *arg = (chrwork *) data;
406 assert(arg->map->input);
407 logf (LOG_DEBUG, "set map %.*s", (int) strlen(s), s);
408 set_map_string(arg->map->input, arg->map->nmem, s, strlen(s), arg->string,
410 for (s = arg->string; *s; s++)
411 logf (LOG_DEBUG, " %3d", (unsigned char) *s);
415 * Add a query map to the string contained in the argument.
417 static void fun_add_qmap(const char *s, void *data, int num)
419 chrwork *arg = (chrwork *) data;
421 assert(arg->map->q_input);
422 logf (LOG_DEBUG, "set qmap %.*s", (int) strlen(s), s);
423 set_map_string(arg->map->q_input, arg->map->nmem, s,
424 strlen(s), arg->string, 0);
425 for (s = arg->string; *s; s++)
426 logf (LOG_DEBUG, " %3d", (unsigned char) *s);
429 static int scan_to_utf8 (yaz_iconv_t t, ucs4_t *from, size_t inlen,
430 char *outbuf, size_t outbytesleft)
432 size_t inbytesleft = inlen * sizeof(ucs4_t);
433 char *inbuf = (char*) from;
437 *outbuf++ = *from; /* ISO-8859-1 is OK here */
440 ret = yaz_iconv (t, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
441 if (ret == (size_t) (-1))
443 yaz_log(LOG_LOG, "from: %2X %2X %2X %2X",
444 from[0], from[1], from[2], from[3]);
445 yaz_log (LOG_WARN|LOG_ERRNO, "bad unicode sequence");
453 static int scan_string(char *s_native,
454 yaz_iconv_t t_unicode, yaz_iconv_t t_utf8,
455 void (*fun)(const char *c, void *data, int num),
456 void *data, int *num)
461 ucs4_t arg_prim[512];
462 ucs4_t *s0, *s = arg;
463 ucs4_t c, begin, end;
468 char *outbuf = (char *) arg;
469 char *inbuf = s_native;
470 size_t outbytesleft = sizeof(arg)-4;
471 size_t inbytesleft = strlen(s_native);
473 ret = yaz_iconv(t_unicode, &inbuf, &inbytesleft,
474 &outbuf, &outbytesleft);
475 if (ret == (size_t)(-1))
477 i = (outbuf - (char*) arg)/sizeof(ucs4_t);
481 for (i = 0; s_native[i]; i++)
482 arg[i] = s_native[i] & 255; /* ISO-8859-1 conversion */
484 arg[i] = 0; /* terminate */
485 if (s[0] == 0xfeff || s[0] == 0xfeff) /* skip byte Order Mark */
493 begin = zebra_prim_w(&s);
496 logf(LOG_FATAL, "Bad range in char-map");
500 end = zebra_prim_w(&s);
503 logf(LOG_FATAL, "Bad range in char-map");
507 for (c = begin; c <= end; c++)
509 if (scan_to_utf8 (t_utf8, &c, 1, str, sizeof(str)-1))
511 (*fun)(str, data, num ? (*num)++ : 0);
514 case '[': s++; abort(); break;
518 while (*s != ')' || s[-1] == '\\')
519 arg_prim[i++] = zebra_prim_w(&s);
521 if (scan_to_utf8 (t_utf8, arg_prim, zebra_ucs4_strlen(arg_prim), str, sizeof(str)-1))
523 (*fun)(str, data, num ? (*num)++ : 0);
527 c = zebra_prim_w(&s);
528 if (scan_to_utf8 (t_utf8, &c, 1, str, sizeof(str)-1))
530 (*fun)(str, data, num ? (*num)++ : 0);
536 chrmaptab chrmaptab_create(const char *tabpath, const char *name, int map_only,
540 char line[512], *argv[50];
544 int argc, num = (int) *CHR_BASE, i;
546 yaz_iconv_t t_unicode = 0;
547 yaz_iconv_t t_utf8 = 0;
548 unsigned endian = 31;
549 const char *ucs4_native = "UCS-4";
551 if (*(char*) &endian == 31) /* little endian? */
552 ucs4_native = "UCS-4LE";
554 t_utf8 = yaz_iconv_open ("UTF-8", ucs4_native);
556 logf (LOG_DEBUG, "maptab %s open", name);
557 if (!(f = yaz_fopen(tabpath, name, "r", tabroot)))
559 logf(LOG_WARN|LOG_ERRNO, "%s", name);
562 nmem = nmem_create ();
563 res = (chrmaptab) nmem_malloc(nmem, sizeof(*res));
565 res->input = (chr_t_entry *) nmem_malloc(res->nmem, sizeof(*res->input));
566 res->input->target = (unsigned char **)
567 nmem_malloc(res->nmem, sizeof(*res->input->target) * 2);
568 res->input->target[0] = (unsigned char*) CHR_UNKNOWN;
569 res->input->target[1] = 0;
570 res->input->children = (chr_t_entry **)
571 nmem_malloc(res->nmem, sizeof(res->input) * 256);
572 for (i = 0; i < 256; i++)
574 res->input->children[i] = (chr_t_entry *)
575 nmem_malloc(res->nmem, sizeof(*res->input));
576 res->input->children[i]->children = 0;
577 res->input->children[i]->target = (unsigned char **)
578 nmem_malloc (res->nmem, 2 * sizeof(unsigned char *));
579 res->input->children[i]->target[1] = 0;
582 res->input->children[i]->target[0] = (unsigned char *)
583 nmem_malloc (res->nmem, 2 * sizeof(unsigned char));
584 res->input->children[i]->target[0][0] = i;
585 res->input->children[i]->target[0][1] = 0;
588 res->input->children[i]->target[0] = (unsigned char*) CHR_UNKNOWN;
590 res->q_input = (chr_t_entry *)
591 nmem_malloc(res->nmem, sizeof(*res->q_input));
592 res->q_input->target = 0;
593 res->q_input->children = 0;
595 for (i = *CHR_BASE; i < 256; i++)
597 res->output[(int) *CHR_SPACE] = (unsigned char *) " ";
598 res->output[(int) *CHR_UNKNOWN] = (unsigned char*) "@";
599 res->base_uppercase = 0;
601 while (!errors && (argc = readconf_line(f, &lineno, line, 512, argv, 50)))
602 if (!map_only && !yaz_matchstr(argv[0], "lowercase"))
606 logf(LOG_FATAL, "Syntax error in charmap");
609 if (scan_string(argv[1], t_unicode, t_utf8, fun_addentry,
612 logf(LOG_FATAL, "Bad value-set specification");
615 res->base_uppercase = num;
616 res->output[(int) *CHR_SPACE + num] = (unsigned char *) " ";
617 res->output[(int) *CHR_UNKNOWN + num] = (unsigned char*) "@";
618 num = (int) *CHR_BASE;
620 else if (!map_only && !yaz_matchstr(argv[0], "uppercase"))
622 if (!res->base_uppercase)
624 logf(LOG_FATAL, "Uppercase directive with no lowercase set");
629 logf(LOG_FATAL, "Missing arg for uppercase directive");
632 if (scan_string(argv[1], t_unicode, t_utf8, fun_addentry,
635 logf(LOG_FATAL, "Bad value-set specification");
639 else if (!map_only && !yaz_matchstr(argv[0], "space"))
643 logf(LOG_FATAL, "Syntax error in charmap for space");
646 if (scan_string(argv[1], t_unicode, t_utf8,
647 fun_addspace, res, 0) < 0)
649 logf(LOG_FATAL, "Bad space specification");
653 else if (!map_only && !yaz_matchstr(argv[0], "cut"))
657 logf(LOG_FATAL, "Syntax error in charmap for cut");
660 if (scan_string(argv[1], t_unicode, t_utf8,
661 fun_addcut, res, 0) < 0)
663 logf(LOG_FATAL, "Bad cut specification");
667 else if (!yaz_matchstr(argv[0], "map"))
673 logf(LOG_FATAL, "charmap directive map requires 2 args");
677 buf.string[0] = '\0';
678 if (scan_string(argv[2], t_unicode, t_utf8,
679 fun_mkstring, &buf, 0) < 0)
681 logf(LOG_FATAL, "Bad map target");
684 if (scan_string(argv[1], t_unicode, t_utf8,
685 fun_add_map, &buf, 0) < 0)
687 logf(LOG_FATAL, "Bad map source");
691 else if (!yaz_matchstr(argv[0], "qmap"))
697 logf(LOG_FATAL, "charmap directive qmap requires 2 args");
701 buf.string[0] = '\0';
702 if (scan_string(argv[2], t_unicode, t_utf8,
703 fun_mkstring, &buf, 0) < 0)
705 logf(LOG_FATAL, "Bad qmap target");
708 if (scan_string(argv[1], t_unicode, t_utf8,
709 fun_add_qmap, &buf, 0) < 0)
711 logf(LOG_FATAL, "Bad qmap source");
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 logf(LOG_WARN, "Syntax error at '%s' in %s", line, name);
748 chrmaptab_destroy(res);
751 logf (LOG_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);