Added include stdlib.h a few places to get prototype for atoi/exit/..
[idzebra-moved-to-github.git] / util / charmap.c
1 /* $Id: charmap.c,v 1.35 2005-01-16 23:14:58 adam Exp $
2    Copyright (C) 1995-2005
3    Index Data ApS
4
5 This file is part of the Zebra server.
6
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
10 version.
11
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
15 for more details.
16
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
20 02111-1307, USA.
21 */
22
23
24
25 /*
26  * Support module to handle character-conversions into and out of the
27  * Zebra dictionary.
28  */
29
30 #include <ctype.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <assert.h>
34
35 typedef unsigned ucs4_t;
36
37 #include <charmap.h>
38
39 #include <yaz/yaz-util.h>
40
41 #define CHR_MAXSTR 1024
42 #define CHR_MAXEQUIV 32
43
44 const unsigned char CHR_FIELD_BEGIN = '^';
45
46 const char *CHR_UNKNOWN = "\001";
47 const char *CHR_SPACE   = "\002";
48 const char *CHR_CUT     = "\003";
49 const char *CHR_BASE    = "\005";
50
51 struct chrmaptab_info
52 {
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 */
57     NMEM nmem;
58 };
59
60 /*
61  * Character map trie node.
62  */
63 struct chr_t_entry
64 {
65     chr_t_entry **children;  /* array of children */
66     unsigned char **target;  /* target for this node, if any */
67 };
68
69 /*
70  * General argument structure for callback functions (internal use only)
71  */
72 typedef struct chrwork 
73 {
74     chrmaptab map;
75     char string[CHR_MAXSTR+1];
76 } chrwork;
77
78 /*
79  * Add an entry to the character map.
80  */
81 static chr_t_entry *set_map_string(chr_t_entry *root, NMEM nmem,
82                                    const char *from, int len, char *to,
83                                    const char *from_0)
84 {
85     if (!from_0)
86         from_0 = from;
87     if (!root)
88     {
89         root = (chr_t_entry *) nmem_malloc(nmem, sizeof(*root));
90         root->children = 0;
91         root->target = 0;
92     }
93     if (!len)
94     {
95         if (!root->target || !root->target[0] || strcmp(root->target[0], to))
96         {
97             if (from_0 && 
98                 root->target && root->target[0] && root->target[0][0] &&
99                 strcmp (root->target[0], CHR_UNKNOWN))
100             {
101                 yaz_log (YLOG_WARN, "duplicate entry for charmap from '%s'",
102                          from_0);
103             }
104             root->target = (unsigned char **)
105                 nmem_malloc(nmem, sizeof(*root->target)*2);
106             root->target[0] = (unsigned char *) nmem_strdup(nmem, to);
107             root->target[1] = 0;
108         }
109     }
110     else
111     {
112         if (!root->children)
113         {
114             int i;
115
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;
120         }
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)))
124             return 0;
125     }
126     return root;
127 }
128
129 static chr_t_entry *find_entry(chr_t_entry *t, const char **from, int len)
130 {
131     chr_t_entry *res;
132
133     if (len && t->children && t->children[(unsigned char) **from])
134     {
135         const char *pos = *from;
136
137         (*from)++;
138         if ((res = find_entry(t->children[(unsigned char) *pos],
139             from, len - 1)))
140             return res;
141         /* no match */
142         *from = pos;
143     }
144     /* no children match. use ourselves, if we have a target */
145     return t->target ? t : 0;
146 }
147
148 static chr_t_entry *find_entry_x(chr_t_entry *t, const char **from, int *len, int first)
149 {
150     chr_t_entry *res;
151
152     while (*len <= 0)
153     {   /* switch to next buffer */
154         if (*len < 0)
155             break;
156         from++;
157         len++;
158     }
159     if (*len > 0 && t->children)
160     {
161         const char *old_from = *from;
162         int old_len = *len;
163
164         res = 0;
165
166         if (first && t->children[CHR_FIELD_BEGIN])
167         {
168             if ((res = find_entry_x(t->children[CHR_FIELD_BEGIN], from, len, 0)) && res != t->children[CHR_FIELD_BEGIN])
169                 return res;
170             else
171                 res = 0;
172             /* otherwhise there was no match on beginning of field, move on */
173         } 
174         
175         if (!res && t->children[(unsigned char) **from])
176         {
177             (*len)--;
178             (*from)++;
179             if ((res = find_entry_x(t->children[(unsigned char) *old_from],
180                                     from, len, 0)))
181                 return res;
182             /* no match */
183             *len = old_len;
184             *from = old_from;
185         }
186     }
187     /* no children match. use ourselves, if we have a target */
188     return t->target ? t : 0;
189 }
190
191 const char **chr_map_input_x(chrmaptab maptab, const char **from, int *len, int first)
192 {
193     chr_t_entry *t = maptab->input;
194     chr_t_entry *res;
195
196     if (!(res = find_entry_x(t, from, len, first)))
197         abort();
198     return (const char **) (res->target);
199 }
200
201 const char **chr_map_input(chrmaptab maptab, const char **from, int len, int first)
202 {
203     chr_t_entry *t = maptab->input;
204     chr_t_entry *res;
205     int len_tmp[2];
206
207     len_tmp[0] = len;
208     len_tmp[1] = -1;
209     if (!(res = find_entry_x(t, from, len_tmp, first)))
210         abort();
211     return (const char **) (res->target);
212 }
213
214 const char *chr_map_output(chrmaptab maptab, const char **from, int len)
215 {
216     unsigned char c = ** (unsigned char **) from;
217     (*from)++;
218     return (const char*) maptab->output[c];
219 }
220
221 unsigned char zebra_prim(char **s)
222 {
223     unsigned char c;
224     unsigned int i = 0;
225
226     yaz_log (YLOG_DEBUG, "prim %.3s", *s);
227     if (**s == '\\')
228     {
229         (*s)++;
230         c = **s;
231         switch (c)
232         {
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;
239         case '0':
240         case '1':
241         case '2':
242         case '3':
243         case '4':
244         case '5':
245         case '6':
246         case '7':
247         case '8':
248         case '9':
249             sscanf(*s, "%3o", &i);
250             c = i;
251             *s += 3;
252             break;
253         default:
254             (*s)++;
255         }
256     }
257     else
258     {
259         c = **s;
260         ++(*s);
261     }
262     return c;
263 }
264
265 static int zebra_ucs4_strlen(ucs4_t *s)
266 {
267     int i = 0;
268     while (*s++)
269         i++;
270     return i;
271 }
272
273 ucs4_t zebra_prim_w(ucs4_t **s)
274 {
275     ucs4_t c;
276     ucs4_t i = 0;
277     char fmtstr[8];
278
279     yaz_log (YLOG_DEBUG, "prim_w %.3s", (char *) *s);
280     if (**s == '\\')
281     {
282         (*s)++;
283         c = **s;
284         switch (c)
285         {
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;
291         case 'x': 
292             if (zebra_ucs4_strlen(*s) >= 3)
293             {
294                 fmtstr[0] = (*s)[1];
295                 fmtstr[1] = (*s)[2];
296                 fmtstr[2] = 0;
297                 sscanf(fmtstr, "%x", &i);
298                 c = i;
299                 *s += 3;
300             }
301             break;
302         case '0':
303         case '1':
304         case '2':
305         case '3':
306         case '4':
307         case '5':
308         case '6':
309         case '7':
310         case '8':
311         case '9':
312             if (zebra_ucs4_strlen(*s) >= 3)
313             {
314                 fmtstr[0] = (*s)[0];
315                 fmtstr[1] = (*s)[1];
316                 fmtstr[2] = (*s)[2];
317                 fmtstr[3] = 0;
318                 sscanf(fmtstr, "%o", &i);
319                 c = i;
320                 *s += 3;
321             }
322             break;
323         case 'L':
324             if (zebra_ucs4_strlen(*s) >= 5)
325             {
326                 fmtstr[0] = (*s)[1];
327                 fmtstr[1] = (*s)[2];
328                 fmtstr[2] = (*s)[3];
329                 fmtstr[3] = (*s)[4];
330                 fmtstr[4] = 0;
331                 sscanf(fmtstr, "%x", &i);
332                 c = i;
333                 *s += 5;
334             }
335             break;
336         default:
337             (*s)++;
338         }
339     }
340     else
341     {
342         c = **s;
343         ++(*s);
344     }
345     yaz_log (YLOG_DEBUG, "out %d", c);
346     return c;
347 }
348
349 /*
350  * Callback function.
351  * Add an entry to the value space.
352  */
353 static void fun_addentry(const char *s, void *data, int num)
354 {
355     chrmaptab tab = (chrmaptab) data;
356     char tmp[2];
357     
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);
362 }
363
364 /* 
365  * Callback function.
366  * Add a space-entry to the value space.
367  */
368 static void fun_addspace(const char *s, void *data, int num)
369 {
370     chrmaptab tab = (chrmaptab) data;
371     tab->input = set_map_string(tab->input, tab->nmem, s, strlen(s),
372                                 (char*) CHR_SPACE, 0);
373 }
374
375 /* 
376  * Callback function.
377  * Add a space-entry to the value space.
378  */
379 static void fun_addcut(const char *s, void *data, int num)
380 {
381     chrmaptab tab = (chrmaptab) data;
382     tab->input = set_map_string(tab->input, tab->nmem, s, strlen(s),
383                                 (char*) CHR_CUT, 0);
384 }
385
386 /*
387  * Create a string containing the mapped characters provided.
388  */
389 static void fun_mkstring(const char *s, void *data, int num)
390 {
391     chrwork *arg = (chrwork *) data;
392     const char **res, *p = s;
393
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';
399 }
400
401 /*
402  * Add a map to the string contained in the argument.
403  */
404 static void fun_add_map(const char *s, void *data, int num)
405 {
406     chrwork *arg = (chrwork *) data;
407
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,
411                    0);
412     for (s = arg->string; *s; s++)
413         yaz_log (YLOG_DEBUG, " %3d", (unsigned char) *s);
414 }
415
416 /*
417  * Add a query map to the string contained in the argument.
418  */
419 static void fun_add_qmap(const char *s, void *data, int num)
420 {
421     chrwork *arg = (chrwork *) data;
422
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);
429 }
430
431 static int scan_to_utf8 (yaz_iconv_t t, ucs4_t *from, size_t inlen,
432                         char *outbuf, size_t outbytesleft)
433 {
434     size_t inbytesleft = inlen * sizeof(ucs4_t);
435     char *inbuf = (char*) from;
436     size_t ret;
437    
438     if (t == 0)
439         *outbuf++ = *from;  /* ISO-8859-1 is OK here */
440     else
441     {
442         ret = yaz_iconv (t, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
443         if (ret == (size_t) (-1))
444         {
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");
448             return -1;
449         }
450     }
451     *outbuf = '\0';
452     return 0;
453 }
454
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)
459 {
460     char str[1024];
461
462     ucs4_t arg[512];
463     ucs4_t arg_prim[512];
464     ucs4_t *s0, *s = arg;
465     ucs4_t c, begin, end;
466     size_t i;
467
468     if (t_unicode != 0)
469     {
470         char *outbuf = (char *) arg;
471         char *inbuf = s_native;
472         size_t outbytesleft = sizeof(arg)-4;
473         size_t inbytesleft = strlen(s_native);
474         size_t ret;             
475         ret = yaz_iconv(t_unicode, &inbuf, &inbytesleft,
476                         &outbuf, &outbytesleft);
477         if (ret == (size_t)(-1))
478             return -1;
479         i = (outbuf - (char*) arg)/sizeof(ucs4_t);
480     }
481     else
482     { 
483         for (i = 0; s_native[i]; i++)
484             arg[i] = s_native[i] & 255; /* ISO-8859-1 conversion */
485     }
486     arg[i] = 0;      /* terminate */
487     if (s[0] == 0xfeff || s[0] == 0xfeff)  /* skip byte Order Mark */
488         s++;
489     while (*s)
490     {
491         switch (*s)
492         {
493         case '{':
494             s++;
495             begin = zebra_prim_w(&s);
496             if (*s != '-')
497             {
498                 yaz_log(YLOG_FATAL, "Bad range in char-map");
499                 return -1;
500             }
501             s++;
502             end = zebra_prim_w(&s);
503             if (end <= begin)
504             {
505                 yaz_log(YLOG_FATAL, "Bad range in char-map");
506                 return -1;
507             }
508             s++;
509             for (c = begin; c <= end; c++)
510             {
511                 if (scan_to_utf8 (t_utf8, &c, 1, str, sizeof(str)-1))
512                     return -1;
513                 (*fun)(str, data, num ? (*num)++ : 0);
514             }
515             break;
516         case '[': s++; abort(); break;
517         case '(':
518             ++s;
519             s0 = s; i = 0;
520             while (*s != ')' || s[-1] == '\\')
521                 arg_prim[i++] = zebra_prim_w(&s);
522             arg_prim[i] = 0;
523             if (scan_to_utf8 (t_utf8, arg_prim, zebra_ucs4_strlen(arg_prim), str, sizeof(str)-1))
524                 return -1;
525             (*fun)(str, data, num ? (*num)++ : 0);
526             s++;
527             break;
528         default:
529             c = zebra_prim_w(&s);
530             if (scan_to_utf8 (t_utf8, &c, 1, str, sizeof(str)-1))
531                 return -1;
532             (*fun)(str, data, num ? (*num)++ : 0);
533         }
534     }
535     return 0;
536 }
537
538 chrmaptab chrmaptab_create(const char *tabpath, const char *name, int map_only,
539                            const char *tabroot)
540 {
541     FILE *f;
542     char line[512], *argv[50];
543     chrmaptab res;
544     int lineno = 0;
545     int errors = 0;
546     int argc, num = (int) *CHR_BASE, i;
547     NMEM nmem;
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";
552
553     if (*(char*) &endian == 31)      /* little endian? */
554         ucs4_native = "UCS-4LE";
555
556     t_utf8 = yaz_iconv_open ("UTF-8", ucs4_native);
557
558     yaz_log (YLOG_DEBUG, "maptab %s open", name);
559     if (!(f = yaz_fopen(tabpath, name, "r", tabroot)))
560     {
561         yaz_log(YLOG_WARN|YLOG_ERRNO, "%s", name);
562         return 0;
563     }
564     nmem = nmem_create ();
565     res = (chrmaptab) nmem_malloc(nmem, sizeof(*res));
566     res->nmem = nmem;
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++)
575     {
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;
582         if (map_only)
583         {
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;
588         }
589         else
590             res->input->children[i]->target[0] = (unsigned char*) CHR_UNKNOWN;
591     }
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;
596
597     for (i = *CHR_BASE; i < 256; i++)
598         res->output[i] = 0;
599     res->output[(int) *CHR_SPACE] = (unsigned char *) " ";
600     res->output[(int) *CHR_UNKNOWN] = (unsigned char*) "@";
601     res->base_uppercase = 0;
602
603     while (!errors && (argc = readconf_line(f, &lineno, line, 512, argv, 50)))
604         if (!map_only && !yaz_matchstr(argv[0], "lowercase"))
605         {
606             if (argc != 2)
607             {
608                 yaz_log(YLOG_FATAL, "Syntax error in charmap");
609                 ++errors;
610             }
611             if (scan_string(argv[1], t_unicode, t_utf8, fun_addentry,
612                             res, &num) < 0)
613             {
614                 yaz_log(YLOG_FATAL, "Bad value-set specification");
615                 ++errors;
616             }
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;
621         }
622         else if (!map_only && !yaz_matchstr(argv[0], "uppercase"))
623         {
624             if (!res->base_uppercase)
625             {
626                 yaz_log(YLOG_FATAL, "Uppercase directive with no lowercase set");
627                 ++errors;
628             }
629             if (argc != 2)
630             {
631                 yaz_log(YLOG_FATAL, "Missing arg for uppercase directive");
632                 ++errors;
633             }
634             if (scan_string(argv[1], t_unicode, t_utf8, fun_addentry,
635                             res, &num) < 0)
636             {
637                 yaz_log(YLOG_FATAL, "Bad value-set specification");
638                 ++errors;
639             }
640         }
641         else if (!map_only && !yaz_matchstr(argv[0], "space"))
642         {
643             if (argc != 2)
644             {
645                 yaz_log(YLOG_FATAL, "Syntax error in charmap for space");
646                 ++errors;
647             }
648             if (scan_string(argv[1], t_unicode, t_utf8,
649                             fun_addspace, res, 0) < 0)
650             {
651                 yaz_log(YLOG_FATAL, "Bad space specification");
652                 ++errors;
653             }
654         }
655         else if (!map_only && !yaz_matchstr(argv[0], "cut"))
656         {
657             if (argc != 2)
658             {
659                 yaz_log(YLOG_FATAL, "Syntax error in charmap for cut");
660                 ++errors;
661             }
662             if (scan_string(argv[1], t_unicode, t_utf8,
663                             fun_addcut, res, 0) < 0)
664             {
665                 yaz_log(YLOG_FATAL, "Bad cut specification");
666                 ++errors;
667             }
668         }
669         else if (!yaz_matchstr(argv[0], "map"))
670         {
671             chrwork buf;
672
673             if (argc != 3)
674             {
675                 yaz_log(YLOG_FATAL, "charmap directive map requires 2 args");
676                 ++errors;
677             }
678             buf.map = res;
679             buf.string[0] = '\0';
680             if (scan_string(argv[2], t_unicode, t_utf8,
681                             fun_mkstring, &buf, 0) < 0)
682             {
683                 yaz_log(YLOG_FATAL, "Bad map target");
684                 ++errors;
685             }
686             if (scan_string(argv[1], t_unicode, t_utf8,
687                             fun_add_map, &buf, 0) < 0)
688             {
689                 yaz_log(YLOG_FATAL, "Bad map source");
690                 ++errors;
691             }
692         }
693         else if (!yaz_matchstr(argv[0], "qmap"))
694         {
695             chrwork buf;
696
697             if (argc != 3)
698             {
699                 yaz_log(YLOG_FATAL, "charmap directive qmap requires 2 args");
700                 ++errors;
701             }
702             buf.map = res;
703             buf.string[0] = '\0';
704             if (scan_string(argv[2], t_unicode, t_utf8, 
705                             fun_mkstring, &buf, 0) < 0)
706             {
707                 yaz_log(YLOG_FATAL, "Bad qmap target");
708                 ++errors;
709             }
710             if (scan_string(argv[1], t_unicode, t_utf8, 
711                             fun_add_qmap, &buf, 0) < 0)
712             {
713                 yaz_log(YLOG_FATAL, "Bad qmap source");
714                 ++errors;
715             }
716         }
717         else if (!yaz_matchstr(argv[0], "encoding"))
718         {
719             /*
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.
722              */
723
724             /* Original code */
725 #if 1
726             if (t_unicode != 0)
727                 yaz_iconv_close (t_unicode);
728             t_unicode = yaz_iconv_open (ucs4_native, argv[1]);
729 #endif
730             /*
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.
734              */
735             /* For whatever reason Oleg enabled this.. */
736 #if 0
737             if (t_utf8 != 0)
738                 yaz_iconv_close(t_utf8);
739             t_utf8 = yaz_iconv_open ("UTF-8", argv[1]);
740 #endif
741         }
742         else
743         {
744             yaz_log(YLOG_WARN, "Syntax error at '%s' in %s", line, name);
745         }
746     
747     yaz_fclose(f);
748     if (errors)
749     {
750         chrmaptab_destroy(res);
751         res = 0;
752     }
753     yaz_log (YLOG_DEBUG, "maptab %s close %d errors", name, errors);
754     if (t_utf8 != 0)
755         yaz_iconv_close(t_utf8);
756     if (t_unicode != 0)
757         yaz_iconv_close(t_unicode);
758     return res;
759 }
760
761 void chrmaptab_destroy(chrmaptab tab)
762 {
763     if (tab)
764         nmem_destroy (tab->nmem);
765 }
766
767