1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2008 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>
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))
98 yaz_iconv_t yaz_iconv_open(const char *tocode, const char *fromcode)
100 yaz_iconv_t cd = (yaz_iconv_t) xmalloc (sizeof(*cd));
102 cd->encoder.data = 0;
103 cd->encoder.write_handle = 0;
104 cd->encoder.flush_handle = 0;
105 cd->encoder.init_handle = 0;
106 cd->encoder.destroy_handle = 0;
108 cd->decoder.data = 0;
109 cd->decoder.read_handle = 0;
110 cd->decoder.init_handle = 0;
111 cd->decoder.destroy_handle = 0;
113 cd->my_errno = YAZ_ICONV_UNKNOWN;
115 /* a useful hack: if fromcode has leading @,
116 the library not use YAZ's own conversions .. */
117 if (fromcode[0] == '@')
121 prepare_encoders(cd, tocode);
122 prepare_decoders(cd, fromcode);
124 if (cd->decoder.read_handle && cd->encoder.write_handle)
127 cd->iconv_cd = (iconv_t) (-1);
134 cd->iconv_cd = iconv_open(tocode, fromcode);
135 if (cd->iconv_cd == (iconv_t) (-1))
149 size_t yaz_iconv(yaz_iconv_t cd, char **inbuf, size_t *inbytesleft,
150 char **outbuf, size_t *outbytesleft)
156 if (cd->iconv_cd != (iconv_t) (-1))
159 iconv(cd->iconv_cd, inbuf, inbytesleft, outbuf, outbytesleft);
160 if (r == (size_t)(-1))
165 cd->my_errno = YAZ_ICONV_E2BIG;
168 cd->my_errno = YAZ_ICONV_EINVAL;
171 cd->my_errno = YAZ_ICONV_EILSEQ;
174 cd->my_errno = YAZ_ICONV_UNKNOWN;
186 cd->my_errno = YAZ_ICONV_UNKNOWN;
188 if (cd->encoder.init_handle)
189 (*cd->encoder.init_handle)(&cd->encoder);
194 if (cd->decoder.init_handle)
197 size_t r = (cd->decoder.init_handle)(
199 inbuf ? (unsigned char *) *inbuf : 0,
200 inbytesleft ? *inbytesleft : 0,
204 if (cd->my_errno == YAZ_ICONV_EINVAL)
210 *inbytesleft -= no_read;
217 if (!inbuf || !*inbuf)
219 if (outbuf && *outbuf)
222 r = (*cd->encoder.write_handle)(cd, &cd->encoder,
223 cd->unget_x, outbuf, outbytesleft);
224 if (cd->encoder.flush_handle)
225 r = (*cd->encoder.flush_handle)(cd, &cd->encoder,
226 outbuf, outbytesleft);
241 no_read = cd->no_read_x;
245 if (*inbytesleft == 0)
250 x = (*cd->decoder.read_handle)(
252 (unsigned char *) *inbuf, *inbytesleft, &no_read);
261 r = (*cd->encoder.write_handle)(cd, &cd->encoder,
262 x, outbuf, outbytesleft);
265 /* unable to write it. save it because read_handle cannot
267 if (cd->my_errno == YAZ_ICONV_E2BIG)
270 cd->no_read_x = no_read;
276 *inbytesleft -= no_read;
282 int yaz_iconv_error(yaz_iconv_t cd)
287 int yaz_iconv_close(yaz_iconv_t cd)
290 if (cd->iconv_cd != (iconv_t) (-1))
291 iconv_close(cd->iconv_cd);
293 if (cd->encoder.destroy_handle)
294 (*cd->encoder.destroy_handle)(&cd->encoder);
295 if (cd->decoder.destroy_handle)
296 (*cd->decoder.destroy_handle)(&cd->decoder);
301 void yaz_iconv_set_errno(yaz_iconv_t cd, int no)
309 * indent-tabs-mode: nil
311 * vim: shiftwidth=4 tabstop=8 expandtab