locales, libconv: Sync with FreeBSD (extensive reach)
[dragonfly.git] / lib / i18n_module / UTF7 / citrus_utf7.c
1 /* $FreeBSD: head/lib/libiconv_modules/UTF7/citrus_utf7.c 252583 2013-07-03 18:27:45Z peter $ */
2 /*      $NetBSD: citrus_utf7.c,v 1.5 2006/08/23 12:57:24 tnozaki Exp $  */
3
4 /*-
5  * Copyright (c)2004, 2005 Citrus Project,
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  */
30
31 #include <sys/cdefs.h>
32
33 #include <assert.h>
34 #include <errno.h>
35 #include <limits.h>
36 #include <stdio.h>
37 #include <stdint.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <wchar.h>
41
42 #include "citrus_namespace.h"
43 #include "citrus_types.h"
44 #include "citrus_module.h"
45 #include "citrus_stdenc.h"
46 #include "citrus_utf7.h"
47
48 /* ----------------------------------------------------------------------
49  * private stuffs used by templates
50  */
51
52 #define EI_MASK         UINT16_C(0xff)
53 #define EI_DIRECT       UINT16_C(0x100)
54 #define EI_OPTION       UINT16_C(0x200)
55 #define EI_SPACE        UINT16_C(0x400)
56
57 typedef struct {
58         uint16_t         cell[0x80];
59 } _UTF7EncodingInfo;
60
61 typedef struct {
62         unsigned int
63                 mode: 1,        /* whether base64 mode */
64                 bits: 4,        /* need to hold 0 - 15 */
65                 cache: 22,      /* 22 = BASE64_BIT + UTF16_BIT */
66                 surrogate: 1;   /* whether surrogate pair or not */
67         int chlen;
68         char ch[4]; /* BASE64_IN, 3 * 6 = 18, most closed to UTF16_BIT */
69 } _UTF7State;
70
71 #define _CEI_TO_EI(_cei_)               (&(_cei_)->ei)
72 #define _CEI_TO_STATE(_cei_, _func_)    (_cei_)->states.s_##_func_
73
74 #define _FUNCNAME(m)                    _citrus_UTF7_##m
75 #define _ENCODING_INFO                  _UTF7EncodingInfo
76 #define _ENCODING_STATE                 _UTF7State
77 #define _ENCODING_MB_CUR_MAX(_ei_)              4
78 #define _ENCODING_IS_STATE_DEPENDENT            1
79 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_)        0
80
81 static __inline void
82 /*ARGSUSED*/
83 _citrus_UTF7_init_state(_UTF7EncodingInfo * __restrict ei __unused,
84     _UTF7State * __restrict s)
85 {
86
87         memset((void *)s, 0, sizeof(*s));
88 }
89
90 static __inline void
91 /*ARGSUSED*/
92 _citrus_UTF7_pack_state(_UTF7EncodingInfo * __restrict ei __unused,
93     void *__restrict pspriv, const _UTF7State * __restrict s)
94 {
95
96         memcpy(pspriv, (const void *)s, sizeof(*s));
97 }
98
99 static __inline void
100 /*ARGSUSED*/
101 _citrus_UTF7_unpack_state(_UTF7EncodingInfo * __restrict ei __unused,
102     _UTF7State * __restrict s, const void * __restrict pspriv)
103 {
104
105         memcpy((void *)s, pspriv, sizeof(*s));
106 }
107
108 static const char base64[] =
109         "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
110         "abcdefghijklmnopqrstuvwxyz"
111         "0123456789+/";
112
113 static const char direct[] =
114         "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
115         "abcdefghijklmnopqrstuvwxyz"
116         "0123456789(),-./:?";
117
118 static const char option[] = "!\"#$%&';<=>@[]^_`{|}";
119 static const char spaces[] = " \t\r\n";
120
121 #define BASE64_BIT      6
122 #define UTF16_BIT       16
123
124 #define BASE64_MAX      0x3f
125 #define UTF16_MAX       UINT16_C(0xffff)
126 #define UTF32_MAX       UINT32_C(0x10ffff)
127
128 #define BASE64_IN       '+'
129 #define BASE64_OUT      '-'
130
131 #define SHIFT7BIT(c)    ((c) >> 7)
132 #define ISSPECIAL(c)    ((c) == '\0' || (c) == BASE64_IN)
133
134 #define FINDLEN(ei, c) \
135         (SHIFT7BIT((c)) ? -1 : (((ei)->cell[(c)] & EI_MASK) - 1))
136
137 #define ISDIRECT(ei, c) (!SHIFT7BIT((c)) && (ISSPECIAL((c)) || \
138         ei->cell[(c)] & (EI_DIRECT | EI_OPTION | EI_SPACE)))
139
140 #define ISSAFE(ei, c)   (!SHIFT7BIT((c)) && (ISSPECIAL((c)) || \
141         (c < 0x80 && ei->cell[(c)] & (EI_DIRECT | EI_SPACE))))
142
143 /* surrogate pair */
144 #define SRG_BASE        UINT32_C(0x10000)
145 #define HISRG_MIN       UINT16_C(0xd800)
146 #define HISRG_MAX       UINT16_C(0xdbff)
147 #define LOSRG_MIN       UINT16_C(0xdc00)
148 #define LOSRG_MAX       UINT16_C(0xdfff)
149
150 static int
151 _citrus_UTF7_mbtoutf16(_UTF7EncodingInfo * __restrict ei,
152     uint16_t * __restrict u16, const char ** __restrict s, size_t n,
153     _UTF7State * __restrict psenc, size_t * __restrict nresult)
154 {
155         _UTF7State sv;
156         const char *s0;
157         int done, i, len;
158
159         s0 = *s;
160         sv = *psenc;
161
162         for (i = 0, done = 0; done == 0; i++) {
163                 if (i == psenc->chlen) {
164                         if (n-- < 1) {
165                                 *nresult = (size_t)-2;
166                                 *s = s0;
167                                 sv.chlen = psenc->chlen;
168                                 *psenc = sv;
169                                 return (0);
170                         }
171                         psenc->ch[psenc->chlen++] = *s0++;
172                 }
173                 if (SHIFT7BIT((int)psenc->ch[i]))
174                         goto ilseq;
175                 if (!psenc->mode) {
176                         if (psenc->bits > 0 || psenc->cache > 0)
177                                 return (EINVAL);
178                         if (psenc->ch[i] == BASE64_IN)
179                                 psenc->mode = 1;
180                         else {
181                                 if (!ISDIRECT(ei, (int)psenc->ch[i]))
182                                         goto ilseq;
183                                 *u16 = (uint16_t)psenc->ch[i];
184                                 done = 1;
185                                 continue;
186                         }
187                 } else {
188                         if (psenc->ch[i] == BASE64_OUT && psenc->cache == 0) {
189                                 psenc->mode = 0;
190                                 *u16 = (uint16_t)BASE64_IN;
191                                 done = 1;
192                                 continue;
193                         }
194                         len = FINDLEN(ei, (int)psenc->ch[i]);
195                         if (len < 0) {
196                                 if (psenc->bits >= BASE64_BIT)
197                                         return (EINVAL);
198                                 psenc->mode = 0;
199                                 psenc->bits = psenc->cache = 0;
200                                 if (psenc->ch[i] != BASE64_OUT) {
201                                         if (!ISDIRECT(ei, (int)psenc->ch[i]))
202                                                 goto ilseq;
203                                         *u16 = (uint16_t)psenc->ch[i];
204                                         done = 1;
205                                 }
206                         } else {
207                                 psenc->cache =
208                                     (psenc->cache << BASE64_BIT) | len;
209                                 switch (psenc->bits) {
210                                 case 0: case 2: case 4: case 6: case 8:
211                                         psenc->bits += BASE64_BIT;
212                                         break;
213                                 case 10: case 12: case 14:
214                                         psenc->bits -= (UTF16_BIT - BASE64_BIT);
215                                         *u16 = (psenc->cache >> psenc->bits) &
216                                             UTF16_MAX;
217                                         done = 1;
218                                         break;
219                                 default:
220                                         return (EINVAL);
221                                 }
222                         }
223                 }
224         }
225
226         if (psenc->chlen > i)
227                 return (EINVAL);
228         psenc->chlen = 0;
229         *nresult = (size_t)((*u16 == 0) ? 0 : s0 - *s);
230         *s = s0;
231
232         return (0);
233
234 ilseq:
235         *nresult = (size_t)-1;
236         return (EILSEQ);
237 }
238
239 static int
240 _citrus_UTF7_mbrtowc_priv(_UTF7EncodingInfo * __restrict ei,
241     wchar_t * __restrict pwc, const char ** __restrict s, size_t n,
242     _UTF7State * __restrict psenc, size_t * __restrict nresult)
243 {
244         const char *s0;
245         uint32_t u32;
246         uint16_t hi, lo;
247         size_t nr, siz;
248         int err;
249
250         if (*s == NULL) {
251                 _citrus_UTF7_init_state(ei, psenc);
252                 *nresult = (size_t)_ENCODING_IS_STATE_DEPENDENT;
253                 return (0);
254         }
255         s0 = *s;
256         if (psenc->surrogate) {
257                 hi = (psenc->cache >> 2) & UTF16_MAX;
258                 if (hi < HISRG_MIN || hi > HISRG_MAX)
259                         return (EINVAL);
260                 siz = 0;
261         } else {
262                 err = _citrus_UTF7_mbtoutf16(ei, &hi, &s0, n, psenc, &nr);
263                 if (nr == (size_t)-1 || nr == (size_t)-2) {
264                         *nresult = nr;
265                         return (err);
266                 }
267                 if (err != 0)
268                         return (err);
269                 n -= nr;
270                 siz = nr;
271                 if (hi < HISRG_MIN || hi > HISRG_MAX) {
272                         u32 = (uint32_t)hi;
273                         goto done;
274                 }
275                 psenc->surrogate = 1;
276         }
277         err = _citrus_UTF7_mbtoutf16(ei, &lo, &s0, n, psenc, &nr);
278         if (nr == (size_t)-1 || nr == (size_t)-2) {
279                 *nresult = nr;
280                 return (err);
281         }
282         if (err != 0)
283                 return (err);
284         hi -= HISRG_MIN;
285         lo -= LOSRG_MIN;
286         u32 = (hi << 10 | lo) + SRG_BASE;
287         siz += nr;
288 done:
289         *s = s0;
290         if (pwc != NULL)
291                 *pwc = (wchar_t)u32;
292         if (u32 == (uint32_t)0) {
293                 *nresult = (size_t)0;
294                 _citrus_UTF7_init_state(ei, psenc);
295         } else {
296                 *nresult = siz;
297                 psenc->surrogate = 0;
298         }
299         return (err);
300 }
301
302 static int
303 _citrus_UTF7_utf16tomb(_UTF7EncodingInfo * __restrict ei,
304     char * __restrict s, size_t n __unused, uint16_t u16,
305     _UTF7State * __restrict psenc, size_t * __restrict nresult)
306 {
307         int bits, i;
308
309         if (psenc->chlen != 0 || psenc->bits > BASE64_BIT)
310                 return (EINVAL);
311
312         if (ISSAFE(ei, u16)) {
313                 if (psenc->mode) {
314                         if (psenc->bits > 0) {
315                                 bits = BASE64_BIT - psenc->bits;
316                                 i = (psenc->cache << bits) & BASE64_MAX;
317                                 psenc->ch[psenc->chlen++] = base64[i];
318                                 psenc->bits = psenc->cache = 0;
319                         }
320                         if (u16 == BASE64_OUT || FINDLEN(ei, u16) >= 0)
321                                 psenc->ch[psenc->chlen++] = BASE64_OUT;
322                         psenc->mode = 0;
323                 }
324                 if (psenc->bits != 0)
325                         return (EINVAL);
326                 psenc->ch[psenc->chlen++] = (char)u16;
327                 if (u16 == BASE64_IN)
328                         psenc->ch[psenc->chlen++] = BASE64_OUT;
329         } else {
330                 if (!psenc->mode) {
331                         if (psenc->bits > 0)
332                                 return (EINVAL);
333                         psenc->ch[psenc->chlen++] = BASE64_IN;
334                         psenc->mode = 1;
335                 }
336                 psenc->cache = (psenc->cache << UTF16_BIT) | u16;
337                 bits = UTF16_BIT + psenc->bits;
338                 psenc->bits = bits % BASE64_BIT;
339                 while ((bits -= BASE64_BIT) >= 0) {
340                         i = (psenc->cache >> bits) & BASE64_MAX;
341                         psenc->ch[psenc->chlen++] = base64[i];
342                 }
343         }
344         memcpy(s, psenc->ch, psenc->chlen);
345         *nresult = psenc->chlen;
346         psenc->chlen = 0;
347
348         return (0);
349 }
350
351 static int
352 _citrus_UTF7_wcrtomb_priv(_UTF7EncodingInfo * __restrict ei,
353     char * __restrict s, size_t n, wchar_t wchar,
354     _UTF7State * __restrict psenc, size_t * __restrict nresult)
355 {
356         uint32_t u32;
357         uint16_t u16[2];
358         int err, i, len;
359         size_t nr, siz;
360
361         u32 = (uint32_t)wchar;
362         if (u32 <= UTF16_MAX) {
363                 u16[0] = (uint16_t)u32;
364                 len = 1;
365         } else if (u32 <= UTF32_MAX) {
366                 u32 -= SRG_BASE;
367                 u16[0] = (u32 >> 10) + HISRG_MIN;
368                 u16[1] = ((uint16_t)(u32 & UINT32_C(0x3ff))) + LOSRG_MIN;
369                 len = 2;
370         } else {
371                 *nresult = (size_t)-1;
372                 return (EILSEQ);
373         }
374         siz = 0;
375         for (i = 0; i < len; ++i) {
376                 err = _citrus_UTF7_utf16tomb(ei, s, n, u16[i], psenc, &nr);
377                 if (err != 0)
378                         return (err); /* XXX: state has been modified */
379                 s += nr;
380                 n -= nr;
381                 siz += nr;
382         }
383         *nresult = siz;
384
385         return (0);
386 }
387
388 static int
389 /* ARGSUSED */
390 _citrus_UTF7_put_state_reset(_UTF7EncodingInfo * __restrict ei __unused,
391     char * __restrict s, size_t n, _UTF7State * __restrict psenc,
392     size_t * __restrict nresult)
393 {
394         int bits, pos;
395
396         if (psenc->chlen != 0 || psenc->bits > BASE64_BIT || psenc->surrogate)
397                 return (EINVAL);
398
399         if (psenc->mode) {
400                 if (psenc->bits > 0) {
401                         if (n-- < 1)
402                                 return (E2BIG);
403                         bits = BASE64_BIT - psenc->bits;
404                         pos = (psenc->cache << bits) & BASE64_MAX;
405                         psenc->ch[psenc->chlen++] = base64[pos];
406                         psenc->ch[psenc->chlen++] = BASE64_OUT;
407                         psenc->bits = psenc->cache = 0;
408                 }
409                 psenc->mode = 0;
410         }
411         if (psenc->bits != 0)
412                 return (EINVAL);
413         if (n-- < 1)
414                 return (E2BIG);
415
416         *nresult = (size_t)psenc->chlen;
417         if (psenc->chlen > 0) {
418                 memcpy(s, psenc->ch, psenc->chlen);
419                 psenc->chlen = 0;
420         }
421
422         return (0);
423 }
424
425 static __inline int
426 /*ARGSUSED*/
427 _citrus_UTF7_stdenc_wctocs(_UTF7EncodingInfo * __restrict ei __unused,
428     _csid_t * __restrict csid, _index_t * __restrict idx, wchar_t wc)
429 {
430
431         *csid = 0;
432         *idx = (_index_t)wc;
433
434         return (0);
435 }
436
437 static __inline int
438 /*ARGSUSED*/
439 _citrus_UTF7_stdenc_cstowc(_UTF7EncodingInfo * __restrict ei __unused,
440     wchar_t * __restrict wc, _csid_t csid, _index_t idx)
441 {
442
443         if (csid != 0)
444                 return (EILSEQ);
445         *wc = (wchar_t)idx;
446
447         return (0);
448 }
449
450 static __inline int
451 /*ARGSUSED*/
452 _citrus_UTF7_stdenc_get_state_desc_generic(_UTF7EncodingInfo * __restrict ei __unused,
453     _UTF7State * __restrict psenc, int * __restrict rstate)
454 {
455
456         *rstate = (psenc->chlen == 0) ? _STDENC_SDGEN_INITIAL :
457             _STDENC_SDGEN_INCOMPLETE_CHAR;
458         return (0);
459 }
460
461 static void
462 /*ARGSUSED*/
463 _citrus_UTF7_encoding_module_uninit(_UTF7EncodingInfo *ei __unused)
464 {
465
466         /* ei seems to be unused */
467 }
468
469 static int
470 /*ARGSUSED*/
471 _citrus_UTF7_encoding_module_init(_UTF7EncodingInfo * __restrict ei,
472     const void * __restrict var __unused, size_t lenvar __unused)
473 {
474         const char *s;
475
476         memset(ei, 0, sizeof(*ei));
477
478 #define FILL(str, flag)                         \
479 do {                                            \
480         for (s = str; *s != '\0'; s++)          \
481                 ei->cell[*s & 0x7f] |= flag;    \
482 } while (/*CONSTCOND*/0)
483
484         FILL(base64, (s - base64) + 1);
485         FILL(direct, EI_DIRECT);
486         FILL(option, EI_OPTION);
487         FILL(spaces, EI_SPACE);
488
489         return (0);
490 }
491
492 /* ----------------------------------------------------------------------
493  * public interface for stdenc
494  */
495
496 _CITRUS_STDENC_DECLS(UTF7);
497 _CITRUS_STDENC_DEF_OPS(UTF7);
498
499 #include "citrus_stdenc_template.h"