]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - lib/libiconv_modules/UTF7/citrus_utf7.c
MFC r363988:
[FreeBSD/stable/9.git] / lib / libiconv_modules / UTF7 / citrus_utf7.c
1 /* $FreeBSD$ */
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 #if 0
91 static __inline void
92 /*ARGSUSED*/
93 _citrus_UTF7_pack_state(_UTF7EncodingInfo * __restrict ei __unused,
94     void *__restrict pspriv, const _UTF7State * __restrict s)
95 {
96
97         memcpy(pspriv, (const void *)s, sizeof(*s));
98 }
99
100 static __inline void
101 /*ARGSUSED*/
102 _citrus_UTF7_unpack_state(_UTF7EncodingInfo * __restrict ei __unused,
103     _UTF7State * __restrict s, const void * __restrict pspriv)
104 {
105
106         memcpy((void *)s, pspriv, sizeof(*s));
107 }
108 #endif
109
110 static const char base64[] =
111         "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
112         "abcdefghijklmnopqrstuvwxyz"
113         "0123456789+/";
114
115 static const char direct[] =
116         "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
117         "abcdefghijklmnopqrstuvwxyz"
118         "0123456789(),-./:?";
119
120 static const char option[] = "!\"#$%&';<=>@[]^_`{|}";
121 static const char spaces[] = " \t\r\n";
122
123 #define BASE64_BIT      6
124 #define UTF16_BIT       16
125
126 #define BASE64_MAX      0x3f
127 #define UTF16_MAX       UINT16_C(0xffff)
128 #define UTF32_MAX       UINT32_C(0x10ffff)
129
130 #define BASE64_IN       '+'
131 #define BASE64_OUT      '-'
132
133 #define SHIFT7BIT(c)    ((c) >> 7)
134 #define ISSPECIAL(c)    ((c) == '\0' || (c) == BASE64_IN)
135
136 #define FINDLEN(ei, c) \
137         (SHIFT7BIT((c)) ? -1 : (((ei)->cell[(c)] & EI_MASK) - 1))
138
139 #define ISDIRECT(ei, c) (!SHIFT7BIT((c)) && (ISSPECIAL((c)) || \
140         ei->cell[(c)] & (EI_DIRECT | EI_OPTION | EI_SPACE)))
141
142 #define ISSAFE(ei, c)   (!SHIFT7BIT((c)) && (ISSPECIAL((c)) || \
143         (c < 0x80 && ei->cell[(c)] & (EI_DIRECT | EI_SPACE))))
144
145 /* surrogate pair */
146 #define SRG_BASE        UINT32_C(0x10000)
147 #define HISRG_MIN       UINT16_C(0xd800)
148 #define HISRG_MAX       UINT16_C(0xdbff)
149 #define LOSRG_MIN       UINT16_C(0xdc00)
150 #define LOSRG_MAX       UINT16_C(0xdfff)
151
152 static int
153 _citrus_UTF7_mbtoutf16(_UTF7EncodingInfo * __restrict ei,
154     uint16_t * __restrict u16, char ** __restrict s, size_t n,
155     _UTF7State * __restrict psenc, size_t * __restrict nresult)
156 {
157         _UTF7State sv;
158         char *s0;
159         int done, i, len;
160
161         s0 = *s;
162         sv = *psenc;
163
164         for (i = 0, done = 0; done == 0; i++) {
165                 if (i == psenc->chlen) {
166                         if (n-- < 1) {
167                                 *nresult = (size_t)-2;
168                                 *s = s0;
169                                 sv.chlen = psenc->chlen;
170                                 *psenc = sv;
171                                 return (0);
172                         }
173                         psenc->ch[psenc->chlen++] = *s0++;
174                 }
175                 if (SHIFT7BIT((int)psenc->ch[i]))
176                         goto ilseq;
177                 if (!psenc->mode) {
178                         if (psenc->bits > 0 || psenc->cache > 0)
179                                 return (EINVAL);
180                         if (psenc->ch[i] == BASE64_IN)
181                                 psenc->mode = 1;
182                         else {
183                                 if (!ISDIRECT(ei, (int)psenc->ch[i]))
184                                         goto ilseq;
185                                 *u16 = (uint16_t)psenc->ch[i];
186                                 done = 1;
187                                 continue;
188                         }
189                 } else {
190                         if (psenc->ch[i] == BASE64_OUT && psenc->cache == 0) {
191                                 psenc->mode = 0;
192                                 *u16 = (uint16_t)BASE64_IN;
193                                 done = 1;
194                                 continue;
195                         }
196                         len = FINDLEN(ei, (int)psenc->ch[i]);
197                         if (len < 0) {
198                                 if (psenc->bits >= BASE64_BIT)
199                                         return (EINVAL);
200                                 psenc->mode = 0;
201                                 psenc->bits = psenc->cache = 0;
202                                 if (psenc->ch[i] != BASE64_OUT) {
203                                         if (!ISDIRECT(ei, (int)psenc->ch[i]))
204                                                 goto ilseq;
205                                         *u16 = (uint16_t)psenc->ch[i];
206                                         done = 1;
207                                 }
208                         } else {
209                                 psenc->cache =
210                                     (psenc->cache << BASE64_BIT) | len;
211                                 switch (psenc->bits) {
212                                 case 0: case 2: case 4: case 6: case 8:
213                                         psenc->bits += BASE64_BIT;
214                                         break;
215                                 case 10: case 12: case 14:
216                                         psenc->bits -= (UTF16_BIT - BASE64_BIT);
217                                         *u16 = (psenc->cache >> psenc->bits) &
218                                             UTF16_MAX;
219                                         done = 1;
220                                         break;
221                                 default:
222                                         return (EINVAL);
223                                 }
224                         }
225                 }
226         }
227
228         if (psenc->chlen > i)
229                 return (EINVAL);
230         psenc->chlen = 0;
231         *nresult = (size_t)((*u16 == 0) ? 0 : s0 - *s);
232         *s = s0;
233
234         return (0);
235
236 ilseq:
237         *nresult = (size_t)-1;
238         return (EILSEQ);
239 }
240
241 static int
242 _citrus_UTF7_mbrtowc_priv(_UTF7EncodingInfo * __restrict ei,
243     wchar_t * __restrict pwc, char ** __restrict s, size_t n,
244     _UTF7State * __restrict psenc, size_t * __restrict nresult)
245 {
246         char *s0;
247         uint32_t u32;
248         uint16_t hi, lo;
249         size_t nr, siz;
250         int err;
251
252         if (*s == NULL) {
253                 _citrus_UTF7_init_state(ei, psenc);
254                 *nresult = (size_t)_ENCODING_IS_STATE_DEPENDENT;
255                 return (0);
256         }
257         s0 = *s;
258         if (psenc->surrogate) {
259                 hi = (psenc->cache >> 2) & UTF16_MAX;
260                 if (hi < HISRG_MIN || hi > HISRG_MAX)
261                         return (EINVAL);
262                 siz = 0;
263         } else {
264                 err = _citrus_UTF7_mbtoutf16(ei, &hi, &s0, n, psenc, &nr);
265                 if (nr == (size_t)-1 || nr == (size_t)-2) {
266                         *nresult = nr;
267                         return (err);
268                 }
269                 if (err != 0)
270                         return (err);
271                 n -= nr;
272                 siz = nr;
273                 if (hi < HISRG_MIN || hi > HISRG_MAX) {
274                         u32 = (uint32_t)hi;
275                         goto done;
276                 }
277                 psenc->surrogate = 1;
278         }
279         err = _citrus_UTF7_mbtoutf16(ei, &lo, &s0, n, psenc, &nr);
280         if (nr == (size_t)-1 || nr == (size_t)-2) {
281                 *nresult = nr;
282                 return (err);
283         }
284         if (err != 0)
285                 return (err);
286         hi -= HISRG_MIN;
287         lo -= LOSRG_MIN;
288         u32 = (hi << 10 | lo) + SRG_BASE;
289         siz += nr;
290 done:
291         *s = s0;
292         if (pwc != NULL)
293                 *pwc = (wchar_t)u32;
294         if (u32 == (uint32_t)0) {
295                 *nresult = (size_t)0;
296                 _citrus_UTF7_init_state(ei, psenc);
297         } else {
298                 *nresult = siz;
299                 psenc->surrogate = 0;
300         }
301         return (err);
302 }
303
304 static int
305 _citrus_UTF7_utf16tomb(_UTF7EncodingInfo * __restrict ei,
306     char * __restrict s, size_t n __unused, uint16_t u16,
307     _UTF7State * __restrict psenc, size_t * __restrict nresult)
308 {
309         int bits, i;
310
311         if (psenc->chlen != 0 || psenc->bits > BASE64_BIT)
312                 return (EINVAL);
313
314         if (ISSAFE(ei, u16)) {
315                 if (psenc->mode) {
316                         if (psenc->bits > 0) {
317                                 bits = BASE64_BIT - psenc->bits;
318                                 i = (psenc->cache << bits) & BASE64_MAX;
319                                 psenc->ch[psenc->chlen++] = base64[i];
320                                 psenc->bits = psenc->cache = 0;
321                         }
322                         if (u16 == BASE64_OUT || FINDLEN(ei, u16) >= 0)
323                                 psenc->ch[psenc->chlen++] = BASE64_OUT;
324                         psenc->mode = 0;
325                 }
326                 if (psenc->bits != 0)
327                         return (EINVAL);
328                 psenc->ch[psenc->chlen++] = (char)u16;
329                 if (u16 == BASE64_IN)
330                         psenc->ch[psenc->chlen++] = BASE64_OUT;
331         } else {
332                 if (!psenc->mode) {
333                         if (psenc->bits > 0)
334                                 return (EINVAL);
335                         psenc->ch[psenc->chlen++] = BASE64_IN;
336                         psenc->mode = 1;
337                 }
338                 psenc->cache = (psenc->cache << UTF16_BIT) | u16;
339                 bits = UTF16_BIT + psenc->bits;
340                 psenc->bits = bits % BASE64_BIT;
341                 while ((bits -= BASE64_BIT) >= 0) {
342                         i = (psenc->cache >> bits) & BASE64_MAX;
343                         psenc->ch[psenc->chlen++] = base64[i];
344                 }
345         }
346         memcpy(s, psenc->ch, psenc->chlen);
347         *nresult = psenc->chlen;
348         psenc->chlen = 0;
349
350         return (0);
351 }
352
353 static int
354 _citrus_UTF7_wcrtomb_priv(_UTF7EncodingInfo * __restrict ei,
355     char * __restrict s, size_t n, wchar_t wchar,
356     _UTF7State * __restrict psenc, size_t * __restrict nresult)
357 {
358         uint32_t u32;
359         uint16_t u16[2];
360         int err, i, len;
361         size_t nr, siz;
362
363         u32 = (uint32_t)wchar;
364         if (u32 <= UTF16_MAX) {
365                 u16[0] = (uint16_t)u32;
366                 len = 1;
367         } else if (u32 <= UTF32_MAX) {
368                 u32 -= SRG_BASE;
369                 u16[0] = (u32 >> 10) + HISRG_MIN;
370                 u16[1] = ((uint16_t)(u32 & UINT32_C(0x3ff))) + LOSRG_MIN;
371                 len = 2;
372         } else {
373                 *nresult = (size_t)-1;
374                 return (EILSEQ);
375         }
376         siz = 0;
377         for (i = 0; i < len; ++i) {
378                 err = _citrus_UTF7_utf16tomb(ei, s, n, u16[i], psenc, &nr);
379                 if (err != 0)
380                         return (err); /* XXX: state has been modified */
381                 s += nr;
382                 n -= nr;
383                 siz += nr;
384         }
385         *nresult = siz;
386
387         return (0);
388 }
389
390 static int
391 /* ARGSUSED */
392 _citrus_UTF7_put_state_reset(_UTF7EncodingInfo * __restrict ei __unused,
393     char * __restrict s, size_t n, _UTF7State * __restrict psenc,
394     size_t * __restrict nresult)
395 {
396         int bits, pos;
397
398         if (psenc->chlen != 0 || psenc->bits > BASE64_BIT || psenc->surrogate)
399                 return (EINVAL);
400
401         if (psenc->mode) {
402                 if (psenc->bits > 0) {
403                         if (n-- < 1)
404                                 return (E2BIG);
405                         bits = BASE64_BIT - psenc->bits;
406                         pos = (psenc->cache << bits) & BASE64_MAX;
407                         psenc->ch[psenc->chlen++] = base64[pos];
408                         psenc->ch[psenc->chlen++] = BASE64_OUT;
409                         psenc->bits = psenc->cache = 0;
410                 }
411                 psenc->mode = 0;
412         }
413         if (psenc->bits != 0)
414                 return (EINVAL);
415         if (n-- < 1)
416                 return (E2BIG);
417
418         *nresult = (size_t)psenc->chlen;
419         if (psenc->chlen > 0) {
420                 memcpy(s, psenc->ch, psenc->chlen);
421                 psenc->chlen = 0;
422         }
423
424         return (0);
425 }
426
427 static __inline int
428 /*ARGSUSED*/
429 _citrus_UTF7_stdenc_wctocs(_UTF7EncodingInfo * __restrict ei __unused,
430     _csid_t * __restrict csid, _index_t * __restrict idx, wchar_t wc)
431 {
432
433         *csid = 0;
434         *idx = (_index_t)wc;
435
436         return (0);
437 }
438
439 static __inline int
440 /*ARGSUSED*/
441 _citrus_UTF7_stdenc_cstowc(_UTF7EncodingInfo * __restrict ei __unused,
442     wchar_t * __restrict wc, _csid_t csid, _index_t idx)
443 {
444
445         if (csid != 0)
446                 return (EILSEQ);
447         *wc = (wchar_t)idx;
448
449         return (0);
450 }
451
452 static __inline int
453 /*ARGSUSED*/
454 _citrus_UTF7_stdenc_get_state_desc_generic(_UTF7EncodingInfo * __restrict ei __unused,
455     _UTF7State * __restrict psenc, int * __restrict rstate)
456 {
457
458         *rstate = (psenc->chlen == 0) ? _STDENC_SDGEN_INITIAL :
459             _STDENC_SDGEN_INCOMPLETE_CHAR;
460         return (0);
461 }
462
463 static void
464 /*ARGSUSED*/
465 _citrus_UTF7_encoding_module_uninit(_UTF7EncodingInfo *ei __unused)
466 {
467
468         /* ei seems to be unused */
469 }
470
471 static int
472 /*ARGSUSED*/
473 _citrus_UTF7_encoding_module_init(_UTF7EncodingInfo * __restrict ei,
474     const void * __restrict var __unused, size_t lenvar __unused)
475 {
476         const char *s;
477
478         memset(ei, 0, sizeof(*ei));
479
480 #define FILL(str, flag)                         \
481 do {                                            \
482         for (s = str; *s != '\0'; s++)          \
483                 ei->cell[*s & 0x7f] |= flag;    \
484 } while (/*CONSTCOND*/0)
485
486         FILL(base64, (s - base64) + 1);
487         FILL(direct, EI_DIRECT);
488         FILL(option, EI_OPTION);
489         FILL(spaces, EI_SPACE);
490
491         return (0);
492 }
493
494 /* ----------------------------------------------------------------------
495  * public interface for stdenc
496  */
497
498 _CITRUS_STDENC_DECLS(UTF7);
499 _CITRUS_STDENC_DEF_OPS(UTF7);
500
501 #include "citrus_stdenc_template.h"