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 UTF-8 encoding / decoding
21 size_t yaz_init_UTF8(yaz_iconv_t cd, unsigned char *inp,
22 size_t inbytesleft, size_t *no_read)
31 yaz_iconv_set_errno(cd, YAZ_ICONV_EINVAL);
34 if (inp[1] != 0xbb && inp[2] == 0xbf)
41 unsigned long yaz_read_UTF8_char(unsigned char *inp,
42 size_t inbytesleft, size_t *no_read,
47 *no_read = 0; /* by default */
53 else if (inp[0] <= 0xbf || inp[0] >= 0xfe)
55 *error = YAZ_ICONV_EILSEQ;
57 else if (inp[0] <= 0xdf && inbytesleft >= 2)
59 if ((inp[1] & 0xc0) == 0x80)
61 x = ((inp[0] & 0x1f) << 6) | (inp[1] & 0x3f);
65 *error = YAZ_ICONV_EILSEQ;
68 *error = YAZ_ICONV_EILSEQ;
70 else if (inp[0] <= 0xef && inbytesleft >= 3)
72 if ((inp[1] & 0xc0) == 0x80 && (inp[2] & 0xc0) == 0x80)
74 x = ((inp[0] & 0x0f) << 12) | ((inp[1] & 0x3f) << 6) |
79 *error = YAZ_ICONV_EILSEQ;
82 *error = YAZ_ICONV_EILSEQ;
84 else if (inp[0] <= 0xf7 && inbytesleft >= 4)
86 if ((inp[1] & 0xc0) == 0x80 && (inp[2] & 0xc0) == 0x80
87 && (inp[3] & 0xc0) == 0x80)
89 x = ((inp[0] & 0x07) << 18) | ((inp[1] & 0x3f) << 12) |
90 ((inp[2] & 0x3f) << 6) | (inp[3] & 0x3f);
94 *error = YAZ_ICONV_EILSEQ;
97 *error = YAZ_ICONV_EILSEQ;
99 else if (inp[0] <= 0xfb && inbytesleft >= 5)
101 if ((inp[1] & 0xc0) == 0x80 && (inp[2] & 0xc0) == 0x80
102 && (inp[3] & 0xc0) == 0x80 && (inp[4] & 0xc0) == 0x80)
104 x = ((inp[0] & 0x03) << 24) | ((inp[1] & 0x3f) << 18) |
105 ((inp[2] & 0x3f) << 12) | ((inp[3] & 0x3f) << 6) |
110 *error = YAZ_ICONV_EILSEQ;
113 *error = YAZ_ICONV_EILSEQ;
115 else if (inp[0] <= 0xfd && inbytesleft >= 6)
117 if ((inp[1] & 0xc0) == 0x80 && (inp[2] & 0xc0) == 0x80
118 && (inp[3] & 0xc0) == 0x80 && (inp[4] & 0xc0) == 0x80
119 && (inp[5] & 0xc0) == 0x80)
121 x = ((inp[0] & 0x01) << 30) | ((inp[1] & 0x3f) << 24) |
122 ((inp[2] & 0x3f) << 18) | ((inp[3] & 0x3f) << 12) |
123 ((inp[4] & 0x3f) << 6) | (inp[5] & 0x3f);
127 *error = YAZ_ICONV_EILSEQ;
130 *error = YAZ_ICONV_EILSEQ;
133 *error = YAZ_ICONV_EINVAL; /* incomplete sentence */
138 unsigned long yaz_read_UTF8(yaz_iconv_t cd, unsigned char *inp,
139 size_t inbytesleft, size_t *no_read)
142 int r = yaz_read_UTF8_char(inp, inbytesleft, no_read, &err);
143 yaz_iconv_set_errno(cd, err);
148 static size_t write_UTF8(yaz_iconv_t cd, yaz_iconv_encoder_t en,
150 char **outbuf, size_t *outbytesleft)
153 int r = yaz_write_UTF8_char(x, outbuf, outbytesleft, &err);
154 yaz_iconv_set_errno(cd, err);
158 size_t yaz_write_UTF8_char(unsigned long x,
159 char **outbuf, size_t *outbytesleft,
162 unsigned char *outp = (unsigned char *) *outbuf;
164 if (x <= 0x7f && *outbytesleft >= 1)
166 *outp++ = (unsigned char) x;
169 else if (x <= 0x7ff && *outbytesleft >= 2)
171 *outp++ = (unsigned char) ((x >> 6) | 0xc0);
172 *outp++ = (unsigned char) ((x & 0x3f) | 0x80);
173 (*outbytesleft) -= 2;
175 else if (x <= 0xffff && *outbytesleft >= 3)
177 *outp++ = (unsigned char) ((x >> 12) | 0xe0);
178 *outp++ = (unsigned char) (((x >> 6) & 0x3f) | 0x80);
179 *outp++ = (unsigned char) ((x & 0x3f) | 0x80);
180 (*outbytesleft) -= 3;
182 else if (x <= 0x1fffff && *outbytesleft >= 4)
184 *outp++ = (unsigned char) ((x >> 18) | 0xf0);
185 *outp++ = (unsigned char) (((x >> 12) & 0x3f) | 0x80);
186 *outp++ = (unsigned char) (((x >> 6) & 0x3f) | 0x80);
187 *outp++ = (unsigned char) ((x & 0x3f) | 0x80);
188 (*outbytesleft) -= 4;
190 else if (x <= 0x3ffffff && *outbytesleft >= 5)
192 *outp++ = (unsigned char) ((x >> 24) | 0xf8);
193 *outp++ = (unsigned char) (((x >> 18) & 0x3f) | 0x80);
194 *outp++ = (unsigned char) (((x >> 12) & 0x3f) | 0x80);
195 *outp++ = (unsigned char) (((x >> 6) & 0x3f) | 0x80);
196 *outp++ = (unsigned char) ((x & 0x3f) | 0x80);
197 (*outbytesleft) -= 5;
199 else if (*outbytesleft >= 6)
201 *outp++ = (unsigned char) ((x >> 30) | 0xfc);
202 *outp++ = (unsigned char) (((x >> 24) & 0x3f) | 0x80);
203 *outp++ = (unsigned char) (((x >> 18) & 0x3f) | 0x80);
204 *outp++ = (unsigned char) (((x >> 12) & 0x3f) | 0x80);
205 *outp++ = (unsigned char) (((x >> 6) & 0x3f) | 0x80);
206 *outp++ = (unsigned char) ((x & 0x3f) | 0x80);
207 (*outbytesleft) -= 6;
211 *error = YAZ_ICONV_E2BIG; /* not room for output */
214 *outbuf = (char *) outp;
218 yaz_iconv_encoder_t yaz_utf8_encoder(const char *tocode,
219 yaz_iconv_encoder_t e)
222 if (!yaz_matchstr(tocode, "UTF8"))
224 e->write_handle = write_UTF8;
234 * indent-tabs-mode: nil
236 * vim: shiftwidth=4 tabstop=8 expandtab