1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2011 Index Data
3 * See the file LICENSE for details.
7 * \brief Implements simple ICONV
9 * This implements an interface similar to that of iconv and
10 * is used by YAZ to interface with iconv (if present).
11 * For systems where iconv is not present, this layer
12 * provides a few important conversions: UTF-8, MARC-8, Latin-1.
29 #include <yaz/xmalloc.h>
30 #include <yaz/errno.h>
33 struct yaz_iconv_struct {
37 size_t (*init_handle)(yaz_iconv_t cd, unsigned char *inbuf,
38 size_t inbytesleft, size_t *no_read);
39 unsigned long (*read_handle)(yaz_iconv_t cd, unsigned char *inbuf,
40 size_t inbytesleft, size_t *no_read);
43 unsigned long unget_x;
47 struct yaz_iconv_encoder_s encoder;
48 struct yaz_iconv_decoder_s decoder;
52 int yaz_iconv_isbuiltin(yaz_iconv_t cd)
54 return cd->decoder.read_handle && cd->encoder.write_handle;
58 static int prepare_encoders(yaz_iconv_t cd, const char *tocode)
60 if (yaz_marc8_encoder(tocode, &cd->encoder))
62 if (yaz_utf8_encoder(tocode, &cd->encoder))
64 if (yaz_ucs4_encoder(tocode, &cd->encoder))
66 if (yaz_iso_8859_1_encoder(tocode, &cd->encoder))
68 if (yaz_iso_5428_encoder(tocode, &cd->encoder))
70 if (yaz_advancegreek_encoder(tocode, &cd->encoder))
72 if (yaz_wchar_encoder(tocode, &cd->encoder))
77 static int prepare_decoders(yaz_iconv_t cd, const char *tocode)
79 if (yaz_marc8_decoder(tocode, &cd->decoder))
81 if (yaz_iso5426_decoder(tocode, &cd->decoder))
83 if (yaz_utf8_decoder(tocode, &cd->decoder))
85 if (yaz_ucs4_decoder(tocode, &cd->decoder))
87 if (yaz_iso_8859_1_decoder(tocode, &cd->decoder))
89 if (yaz_iso_5428_decoder(tocode, &cd->decoder))
91 if (yaz_advancegreek_decoder(tocode, &cd->decoder))
93 if (yaz_wchar_decoder(tocode, &cd->decoder))
95 if (yaz_danmarc_decoder(tocode, &cd->decoder))
100 yaz_iconv_t yaz_iconv_open(const char *tocode, const char *fromcode)
102 yaz_iconv_t cd = (yaz_iconv_t) xmalloc (sizeof(*cd));
104 cd->encoder.data = 0;
105 cd->encoder.write_handle = 0;
106 cd->encoder.flush_handle = 0;
107 cd->encoder.init_handle = 0;
108 cd->encoder.destroy_handle = 0;
110 cd->decoder.data = 0;
111 cd->decoder.read_handle = 0;
112 cd->decoder.init_handle = 0;
113 cd->decoder.destroy_handle = 0;
115 cd->my_errno = YAZ_ICONV_UNKNOWN;
117 /* a useful hack: if fromcode has leading @,
118 the library not use YAZ's own conversions .. */
119 if (fromcode[0] == '@')
123 prepare_encoders(cd, tocode);
124 prepare_decoders(cd, fromcode);
126 if (cd->decoder.read_handle && cd->encoder.write_handle)
129 cd->iconv_cd = (iconv_t) (-1);
136 cd->iconv_cd = iconv_open(tocode, fromcode);
137 if (cd->iconv_cd == (iconv_t) (-1))
151 size_t yaz_iconv(yaz_iconv_t cd, char **inbuf, size_t *inbytesleft,
152 char **outbuf, size_t *outbytesleft)
158 if (cd->iconv_cd != (iconv_t) (-1))
161 iconv(cd->iconv_cd, inbuf, inbytesleft, outbuf, outbytesleft);
162 if (r == (size_t)(-1))
167 cd->my_errno = YAZ_ICONV_E2BIG;
170 cd->my_errno = YAZ_ICONV_EINVAL;
173 cd->my_errno = YAZ_ICONV_EILSEQ;
176 cd->my_errno = YAZ_ICONV_UNKNOWN;
188 cd->my_errno = YAZ_ICONV_UNKNOWN;
190 if (cd->encoder.init_handle)
191 (*cd->encoder.init_handle)(&cd->encoder);
196 if (cd->decoder.init_handle)
199 size_t r = (cd->decoder.init_handle)(
201 inbuf ? (unsigned char *) *inbuf : 0,
202 inbytesleft ? *inbytesleft : 0,
206 if (cd->my_errno == YAZ_ICONV_EINVAL)
212 *inbytesleft -= no_read;
219 if (!inbuf || !*inbuf)
221 if (outbuf && *outbuf)
224 r = (*cd->encoder.write_handle)(cd, &cd->encoder,
225 cd->unget_x, outbuf, outbytesleft);
226 if (cd->encoder.flush_handle)
227 r = (*cd->encoder.flush_handle)(cd, &cd->encoder,
228 outbuf, outbytesleft);
243 no_read = cd->no_read_x;
247 if (*inbytesleft == 0)
252 x = (*cd->decoder.read_handle)(
254 (unsigned char *) *inbuf, *inbytesleft, &no_read);
263 r = (*cd->encoder.write_handle)(cd, &cd->encoder,
264 x, outbuf, outbytesleft);
267 /* unable to write it. save it because read_handle cannot
269 if (cd->my_errno == YAZ_ICONV_E2BIG)
272 cd->no_read_x = no_read;
278 *inbytesleft -= no_read;
284 int yaz_iconv_error(yaz_iconv_t cd)
289 int yaz_iconv_close(yaz_iconv_t cd)
292 if (cd->iconv_cd != (iconv_t) (-1))
293 iconv_close(cd->iconv_cd);
295 if (cd->encoder.destroy_handle)
296 (*cd->encoder.destroy_handle)(&cd->encoder);
297 if (cd->decoder.destroy_handle)
298 (*cd->decoder.destroy_handle)(&cd->decoder);
303 void yaz_iconv_set_errno(yaz_iconv_t cd, int no)
311 * c-file-style: "Stroustrup"
312 * indent-tabs-mode: nil
314 * vim: shiftwidth=4 tabstop=8 expandtab