1 /* $Id: charmap.c,v 1.35 2005-01-16 23:14:58 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 * Add an entry to the character map.
81 static chr_t_entry *set_map_string(chr_t_entry *root, NMEM nmem,
82 const char *from, int len, char *to,
89 root = (chr_t_entry *) nmem_malloc(nmem, sizeof(*root));
95 if (!root->target || !root->target[0] || strcmp(root->target[0], to))
98 root->target && root->target[0] && root->target[0][0] &&
99 strcmp (root->target[0], CHR_UNKNOWN))
101 yaz_log (YLOG_WARN, "duplicate entry for charmap from '%s'",
104 root->target = (unsigned char **)
105 nmem_malloc(nmem, sizeof(*root->target)*2);
106 root->target[0] = (unsigned char *) nmem_strdup(nmem, to);
116 root->children = (chr_t_entry **)
117 nmem_malloc(nmem, sizeof(chr_t_entry*) * 256);
118 for (i = 0; i < 256; i++)
119 root->children[i] = 0;
121 if (!(root->children[(unsigned char) *from] =
122 set_map_string(root->children[(unsigned char) *from], nmem,
123 from + 1, len - 1, to, from_0)))
129 static chr_t_entry *find_entry(chr_t_entry *t, const char **from, int len)
133 if (len && t->children && t->children[(unsigned char) **from])
135 const char *pos = *from;
138 if ((res = find_entry(t->children[(unsigned char) *pos],
144 /* no children match. use ourselves, if we have a target */
145 return t->target ? t : 0;
148 static chr_t_entry *find_entry_x(chr_t_entry *t, const char **from, int *len, int first)
153 { /* switch to next buffer */
159 if (*len > 0 && t->children)
161 const char *old_from = *from;
166 if (first && t->children[CHR_FIELD_BEGIN])
168 if ((res = find_entry_x(t->children[CHR_FIELD_BEGIN], from, len, 0)) && res != t->children[CHR_FIELD_BEGIN])
172 /* otherwhise there was no match on beginning of field, move on */
175 if (!res && t->children[(unsigned char) **from])
179 if ((res = find_entry_x(t->children[(unsigned char) *old_from],
187 /* no children match. use ourselves, if we have a target */
188 return t->target ? t : 0;
191 const char **chr_map_input_x(chrmaptab maptab, const char **from, int *len, int first)
193 chr_t_entry *t = maptab->input;
196 if (!(res = find_entry_x(t, from, len, first)))
198 return (const char **) (res->target);
201 const char **chr_map_input(chrmaptab maptab, const char **from, int len, int first)
203 chr_t_entry *t = maptab->input;
209 if (!(res = find_entry_x(t, from, len_tmp, first)))
211 return (const char **) (res->target);
214 const char *chr_map_output(chrmaptab maptab, const char **from, int len)
216 unsigned char c = ** (unsigned char **) from;
218 return (const char*) maptab->output[c];
221 unsigned char zebra_prim(char **s)
226 yaz_log (YLOG_DEBUG, "prim %.3s", *s);
233 case '\\': c = '\\'; (*s)++; break;
234 case 'r': c = '\r'; (*s)++; break;
235 case 'n': c = '\n'; (*s)++; break;
236 case 't': c = '\t'; (*s)++; break;
237 case 's': c = ' '; (*s)++; break;
238 case 'x': sscanf(*s, "x%2x", &i); c = i; *s += 3; break;
249 sscanf(*s, "%3o", &i);
265 static int zebra_ucs4_strlen(ucs4_t *s)
273 ucs4_t zebra_prim_w(ucs4_t **s)
279 yaz_log (YLOG_DEBUG, "prim_w %.3s", (char *) *s);
286 case '\\': c = '\\'; (*s)++; break;
287 case 'r': c = '\r'; (*s)++; break;
288 case 'n': c = '\n'; (*s)++; break;
289 case 't': c = '\t'; (*s)++; break;
290 case 's': c = ' '; (*s)++; break;
292 if (zebra_ucs4_strlen(*s) >= 3)
297 sscanf(fmtstr, "%x", &i);
312 if (zebra_ucs4_strlen(*s) >= 3)
318 sscanf(fmtstr, "%o", &i);
324 if (zebra_ucs4_strlen(*s) >= 5)
331 sscanf(fmtstr, "%x", &i);
345 yaz_log (YLOG_DEBUG, "out %d", c);
351 * Add an entry to the value space.
353 static void fun_addentry(const char *s, void *data, int num)
355 chrmaptab tab = (chrmaptab) data;
358 tmp[0] = num; tmp[1] = '\0';
359 tab->input = set_map_string(tab->input, tab->nmem, s, strlen(s), tmp, 0);
360 tab->output[num + tab->base_uppercase] =
361 (unsigned char *) nmem_strdup(tab->nmem, s);
366 * Add a space-entry to the value space.
368 static void fun_addspace(const char *s, void *data, int num)
370 chrmaptab tab = (chrmaptab) data;
371 tab->input = set_map_string(tab->input, tab->nmem, s, strlen(s),
372 (char*) CHR_SPACE, 0);
377 * Add a space-entry to the value space.
379 static void fun_addcut(const char *s, void *data, int num)
381 chrmaptab tab = (chrmaptab) data;
382 tab->input = set_map_string(tab->input, tab->nmem, s, strlen(s),
387 * Create a string containing the mapped characters provided.
389 static void fun_mkstring(const char *s, void *data, int num)
391 chrwork *arg = (chrwork *) data;
392 const char **res, *p = s;
394 res = chr_map_input(arg->map, &s, strlen(s), 0);
395 if (*res == (char*) CHR_UNKNOWN)
396 yaz_log(YLOG_WARN, "Map: '%s' has no mapping", p);
397 strncat(arg->string, *res, CHR_MAXSTR - strlen(arg->string));
398 arg->string[CHR_MAXSTR] = '\0';
402 * Add a map to the string contained in the argument.
404 static void fun_add_map(const char *s, void *data, int num)
406 chrwork *arg = (chrwork *) data;
408 assert(arg->map->input);
409 yaz_log (YLOG_DEBUG, "set map %.*s", (int) strlen(s), s);
410 set_map_string(arg->map->input, arg->map->nmem, s, strlen(s), arg->string,
412 for (s = arg->string; *s; s++)
413 yaz_log (YLOG_DEBUG, " %3d", (unsigned char) *s);
417 * Add a query map to the string contained in the argument.
419 static void fun_add_qmap(const char *s, void *data, int num)
421 chrwork *arg = (chrwork *) data;
423 assert(arg->map->q_input);
424 yaz_log (YLOG_DEBUG, "set qmap %.*s", (int) strlen(s), s);
425 set_map_string(arg->map->q_input, arg->map->nmem, s,
426 strlen(s), arg->string, 0);
427 for (s = arg->string; *s; s++)
428 yaz_log (YLOG_DEBUG, " %3d", (unsigned char) *s);
431 static int scan_to_utf8 (yaz_iconv_t t, ucs4_t *from, size_t inlen,
432 char *outbuf, size_t outbytesleft)
434 size_t inbytesleft = inlen * sizeof(ucs4_t);
435 char *inbuf = (char*) from;
439 *outbuf++ = *from; /* ISO-8859-1 is OK here */
442 ret = yaz_iconv (t, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
443 if (ret == (size_t) (-1))
445 yaz_log(YLOG_LOG, "from: %2X %2X %2X %2X",
446 from[0], from[1], from[2], from[3]);
447 yaz_log (YLOG_WARN|YLOG_ERRNO, "bad unicode sequence");
455 static int scan_string(char *s_native,
456 yaz_iconv_t t_unicode, yaz_iconv_t t_utf8,
457 void (*fun)(const char *c, void *data, int num),
458 void *data, int *num)
463 ucs4_t arg_prim[512];
464 ucs4_t *s0, *s = arg;
465 ucs4_t c, begin, end;
470 char *outbuf = (char *) arg;
471 char *inbuf = s_native;
472 size_t outbytesleft = sizeof(arg)-4;
473 size_t inbytesleft = strlen(s_native);
475 ret = yaz_iconv(t_unicode, &inbuf, &inbytesleft,
476 &outbuf, &outbytesleft);
477 if (ret == (size_t)(-1))
479 i = (outbuf - (char*) arg)/sizeof(ucs4_t);
483 for (i = 0; s_native[i]; i++)
484 arg[i] = s_native[i] & 255; /* ISO-8859-1 conversion */
486 arg[i] = 0; /* terminate */
487 if (s[0] == 0xfeff || s[0] == 0xfeff) /* skip byte Order Mark */
495 begin = zebra_prim_w(&s);
498 yaz_log(YLOG_FATAL, "Bad range in char-map");
502 end = zebra_prim_w(&s);
505 yaz_log(YLOG_FATAL, "Bad range in char-map");
509 for (c = begin; c <= end; c++)
511 if (scan_to_utf8 (t_utf8, &c, 1, str, sizeof(str)-1))
513 (*fun)(str, data, num ? (*num)++ : 0);
516 case '[': s++; abort(); break;
520 while (*s != ')' || s[-1] == '\\')
521 arg_prim[i++] = zebra_prim_w(&s);
523 if (scan_to_utf8 (t_utf8, arg_prim, zebra_ucs4_strlen(arg_prim), str, sizeof(str)-1))
525 (*fun)(str, data, num ? (*num)++ : 0);
529 c = zebra_prim_w(&s);
530 if (scan_to_utf8 (t_utf8, &c, 1, str, sizeof(str)-1))
532 (*fun)(str, data, num ? (*num)++ : 0);
538 chrmaptab chrmaptab_create(const char *tabpath, const char *name, int map_only,
542 char line[512], *argv[50];
546 int argc, num = (int) *CHR_BASE, i;
548 yaz_iconv_t t_unicode = 0;
549 yaz_iconv_t t_utf8 = 0;
550 unsigned endian = 31;
551 const char *ucs4_native = "UCS-4";
553 if (*(char*) &endian == 31) /* little endian? */
554 ucs4_native = "UCS-4LE";
556 t_utf8 = yaz_iconv_open ("UTF-8", ucs4_native);
558 yaz_log (YLOG_DEBUG, "maptab %s open", name);
559 if (!(f = yaz_fopen(tabpath, name, "r", tabroot)))
561 yaz_log(YLOG_WARN|YLOG_ERRNO, "%s", name);
564 nmem = nmem_create ();
565 res = (chrmaptab) nmem_malloc(nmem, sizeof(*res));
567 res->input = (chr_t_entry *) nmem_malloc(res->nmem, sizeof(*res->input));
568 res->input->target = (unsigned char **)
569 nmem_malloc(res->nmem, sizeof(*res->input->target) * 2);
570 res->input->target[0] = (unsigned char*) CHR_UNKNOWN;
571 res->input->target[1] = 0;
572 res->input->children = (chr_t_entry **)
573 nmem_malloc(res->nmem, sizeof(res->input) * 256);
574 for (i = 0; i < 256; i++)
576 res->input->children[i] = (chr_t_entry *)
577 nmem_malloc(res->nmem, sizeof(*res->input));
578 res->input->children[i]->children = 0;
579 res->input->children[i]->target = (unsigned char **)
580 nmem_malloc (res->nmem, 2 * sizeof(unsigned char *));
581 res->input->children[i]->target[1] = 0;
584 res->input->children[i]->target[0] = (unsigned char *)
585 nmem_malloc (res->nmem, 2 * sizeof(unsigned char));
586 res->input->children[i]->target[0][0] = i;
587 res->input->children[i]->target[0][1] = 0;
590 res->input->children[i]->target[0] = (unsigned char*) CHR_UNKNOWN;
592 res->q_input = (chr_t_entry *)
593 nmem_malloc(res->nmem, sizeof(*res->q_input));
594 res->q_input->target = 0;
595 res->q_input->children = 0;
597 for (i = *CHR_BASE; i < 256; i++)
599 res->output[(int) *CHR_SPACE] = (unsigned char *) " ";
600 res->output[(int) *CHR_UNKNOWN] = (unsigned char*) "@";
601 res->base_uppercase = 0;
603 while (!errors && (argc = readconf_line(f, &lineno, line, 512, argv, 50)))
604 if (!map_only && !yaz_matchstr(argv[0], "lowercase"))
608 yaz_log(YLOG_FATAL, "Syntax error in charmap");
611 if (scan_string(argv[1], t_unicode, t_utf8, fun_addentry,
614 yaz_log(YLOG_FATAL, "Bad value-set specification");
617 res->base_uppercase = num;
618 res->output[(int) *CHR_SPACE + num] = (unsigned char *) " ";
619 res->output[(int) *CHR_UNKNOWN + num] = (unsigned char*) "@";
620 num = (int) *CHR_BASE;
622 else if (!map_only && !yaz_matchstr(argv[0], "uppercase"))
624 if (!res->base_uppercase)
626 yaz_log(YLOG_FATAL, "Uppercase directive with no lowercase set");
631 yaz_log(YLOG_FATAL, "Missing arg for uppercase directive");
634 if (scan_string(argv[1], t_unicode, t_utf8, fun_addentry,
637 yaz_log(YLOG_FATAL, "Bad value-set specification");
641 else if (!map_only && !yaz_matchstr(argv[0], "space"))
645 yaz_log(YLOG_FATAL, "Syntax error in charmap for space");
648 if (scan_string(argv[1], t_unicode, t_utf8,
649 fun_addspace, res, 0) < 0)
651 yaz_log(YLOG_FATAL, "Bad space specification");
655 else if (!map_only && !yaz_matchstr(argv[0], "cut"))
659 yaz_log(YLOG_FATAL, "Syntax error in charmap for cut");
662 if (scan_string(argv[1], t_unicode, t_utf8,
663 fun_addcut, res, 0) < 0)
665 yaz_log(YLOG_FATAL, "Bad cut specification");
669 else if (!yaz_matchstr(argv[0], "map"))
675 yaz_log(YLOG_FATAL, "charmap directive map requires 2 args");
679 buf.string[0] = '\0';
680 if (scan_string(argv[2], t_unicode, t_utf8,
681 fun_mkstring, &buf, 0) < 0)
683 yaz_log(YLOG_FATAL, "Bad map target");
686 if (scan_string(argv[1], t_unicode, t_utf8,
687 fun_add_map, &buf, 0) < 0)
689 yaz_log(YLOG_FATAL, "Bad map source");
693 else if (!yaz_matchstr(argv[0], "qmap"))
699 yaz_log(YLOG_FATAL, "charmap directive qmap requires 2 args");
703 buf.string[0] = '\0';
704 if (scan_string(argv[2], t_unicode, t_utf8,
705 fun_mkstring, &buf, 0) < 0)
707 yaz_log(YLOG_FATAL, "Bad qmap target");
710 if (scan_string(argv[1], t_unicode, t_utf8,
711 fun_add_qmap, &buf, 0) < 0)
713 yaz_log(YLOG_FATAL, "Bad qmap source");
717 else if (!yaz_matchstr(argv[0], "encoding"))
720 * Fix me. When t_unicode==0 and use encoding directive in *.chr file the beheviour of the
721 * zebra need to comment next part of code.
727 yaz_iconv_close (t_unicode);
728 t_unicode = yaz_iconv_open (ucs4_native, argv[1]);
731 * Fix me. It is additional staff for conversion of characters from local encoding
732 * of *.chr file to UTF-8 (internal encoding).
733 * NOTE: The derective encoding must be first directive in *.chr file.
735 /* For whatever reason Oleg enabled this.. */
738 yaz_iconv_close(t_utf8);
739 t_utf8 = yaz_iconv_open ("UTF-8", argv[1]);
744 yaz_log(YLOG_WARN, "Syntax error at '%s' in %s", line, name);
750 chrmaptab_destroy(res);
753 yaz_log (YLOG_DEBUG, "maptab %s close %d errors", name, errors);
755 yaz_iconv_close(t_utf8);
757 yaz_iconv_close(t_unicode);
761 void chrmaptab_destroy(chrmaptab tab)
764 nmem_destroy (tab->nmem);