1 /* $NetBSD: citrus_utf7.c,v 1.5 2006/08/23 12:57:24 tnozaki Exp $ */
2 /* $DragonFly: src/lib/libc/citrus/modules/citrus_utf7.c,v 1.3 2008/04/10 10:21:02 hasso Exp $ */
5 * Copyright (c)2004, 2005 Citrus Project,
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
40 #include "citrus_namespace.h"
41 #include "citrus_types.h"
42 #include "citrus_module.h"
43 #include "citrus_ctype.h"
44 #include "citrus_stdenc.h"
45 #include "citrus_utf7.h"
47 /* ----------------------------------------------------------------------
48 * private stuffs used by templates
53 #define EI_MASK UINT16_C(0xff)
54 #define EI_DIRECT UINT16_C(0x100)
55 #define EI_OPTION UINT16_C(0x200)
56 #define EI_SPACE UINT16_C(0x400)
61 mode
: 1, /* whether base64 mode */
62 bits
: 4, /* need to hold 0 - 15 */
63 cache
: 22, /* 22 = BASE64_BIT + UTF16_BIT */
64 surrogate
: 1; /* whether surrogate pair or not */
66 char ch
[4]; /* BASE64_IN, 3 * 6 = 18, most closed to UTF16_BIT */
72 /* for future multi-locale facility */
77 _UTF7State s_mbsrtowcs
;
79 _UTF7State s_wcsrtombs
;
84 #define _CEI_TO_EI(_cei_) (&(_cei_)->ei)
85 #define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_
87 #define _FUNCNAME(m) _citrus_UTF7_##m
88 #define _ENCODING_INFO _UTF7EncodingInfo
89 #define _CTYPE_INFO _UTF7CTypeInfo
90 #define _ENCODING_STATE _UTF7State
91 #define _ENCODING_MB_CUR_MAX(_ei_) 4
92 #define _ENCODING_IS_STATE_DEPENDENT 1
93 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0
97 _citrus_UTF7_init_state(_UTF7EncodingInfo
* __restrict ei
,
98 _UTF7State
* __restrict s
)
100 /* ei appears to be unused */
101 _DIAGASSERT(s
!= NULL
);
103 memset((void *)s
, 0, sizeof(*s
));
108 _citrus_UTF7_pack_state(_UTF7EncodingInfo
* __restrict ei
,
109 void *__restrict pspriv
, const _UTF7State
* __restrict s
)
111 /* ei seem to be unused */
112 _DIAGASSERT(pspriv
!= NULL
);
113 _DIAGASSERT(s
!= NULL
);
115 memcpy(pspriv
, (const void *)s
, sizeof(*s
));
120 _citrus_UTF7_unpack_state(_UTF7EncodingInfo
* __restrict ei
,
121 _UTF7State
* __restrict s
, const void * __restrict pspriv
)
123 /* ei seem to be unused */
124 _DIAGASSERT(s
!= NULL
);
125 _DIAGASSERT(pspriv
!= NULL
);
127 memcpy((void *)s
, pspriv
, sizeof(*s
));
130 static const char base64
[] =
131 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
132 "abcdefghijklmnopqrstuvwxyz"
135 static const char direct
[] =
136 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
137 "abcdefghijklmnopqrstuvwxyz"
138 "0123456789(),-./:?";
140 static const char option
[] = "!\"#$%&';<=>@[]^_`{|}";
141 static const char spaces
[] = " \t\r\n";
146 #define BASE64_MAX 0x3f
147 #define UTF16_MAX UINT16_C(0xffff)
148 #define UTF32_MAX UINT32_C(0x10ffff)
150 #define BASE64_IN '+'
151 #define BASE64_OUT '-'
153 #define SHIFT7BIT(c) ((c) >> 7)
154 #define ISSPECIAL(c) ((c) == '\0' || (c) == BASE64_IN)
156 #define FINDLEN(ei, c) \
157 (SHIFT7BIT((c)) ? -1 : (((ei)->cell[(c)] & EI_MASK) - 1))
159 #define ISDIRECT(ei, c) (!SHIFT7BIT((c)) && (ISSPECIAL((c)) || \
160 ei->cell[(c)] & (EI_DIRECT | EI_OPTION | EI_SPACE)))
162 #define ISSAFE(ei, c) (!SHIFT7BIT((c)) && (ISSPECIAL((c)) || \
163 (c < 0x80 && ei->cell[(c)] & (EI_DIRECT | EI_SPACE))))
166 #define SRG_BASE UINT32_C(0x10000)
167 #define HISRG_MIN UINT16_C(0xd800)
168 #define HISRG_MAX UINT16_C(0xdbff)
169 #define LOSRG_MIN UINT16_C(0xdc00)
170 #define LOSRG_MAX UINT16_C(0xdfff)
173 _citrus_UTF7_mbtoutf16(_UTF7EncodingInfo
* __restrict ei
,
174 uint16_t * __restrict u16
, const char ** __restrict s
, size_t n
,
175 _UTF7State
* __restrict psenc
, size_t * __restrict nresult
)
181 _DIAGASSERT(ei
!= NULL
);
182 _DIAGASSERT(s
!= NULL
&& *s
!= NULL
);
183 _DIAGASSERT(psenc
!= NULL
);
188 for (i
= 0, done
= 0; done
== 0; i
++) {
189 _DIAGASSERT(i
<= psenc
->chlen
);
190 if (i
== psenc
->chlen
) {
192 *nresult
= (size_t)-2;
194 sv
.chlen
= psenc
->chlen
;
198 psenc
->ch
[psenc
->chlen
++] = *s0
++;
200 if (SHIFT7BIT((int)psenc
->ch
[i
]))
203 if (psenc
->bits
> 0 || psenc
->cache
> 0)
205 if (psenc
->ch
[i
] == BASE64_IN
) {
208 if (!ISDIRECT(ei
, (int)psenc
->ch
[i
]))
210 *u16
= (uint16_t)psenc
->ch
[i
];
215 if (psenc
->ch
[i
] == BASE64_OUT
&& psenc
->cache
== 0) {
217 *u16
= (uint16_t)BASE64_IN
;
221 len
= FINDLEN(ei
, (int)psenc
->ch
[i
]);
223 if (psenc
->bits
>= BASE64_BIT
)
226 psenc
->bits
= psenc
->cache
= 0;
227 if (psenc
->ch
[i
] != BASE64_OUT
) {
228 if (!ISDIRECT(ei
, (int)psenc
->ch
[i
]))
230 *u16
= (uint16_t)psenc
->ch
[i
];
235 (psenc
->cache
<< BASE64_BIT
) | len
;
236 switch (psenc
->bits
) {
237 case 0: case 2: case 4: case 6: case 8:
238 psenc
->bits
+= BASE64_BIT
;
240 case 10: case 12: case 14:
241 psenc
->bits
-= (UTF16_BIT
- BASE64_BIT
);
242 *u16
= (psenc
->cache
>> psenc
->bits
)
253 if (psenc
->chlen
> i
)
256 *nresult
= (size_t)((*u16
== 0) ? 0 : s0
- *s
);
262 *nresult
= (size_t)-1;
267 _citrus_UTF7_mbrtowc_priv(_UTF7EncodingInfo
* __restrict ei
,
268 wchar_t * __restrict pwc
, const char ** __restrict s
, size_t n
,
269 _UTF7State
* __restrict psenc
, size_t * __restrict nresult
)
277 _DIAGASSERT(ei
!= NULL
);
278 /* pwc may be null */
279 _DIAGASSERT(s
!= NULL
);
280 _DIAGASSERT(psenc
!= NULL
);
283 _citrus_UTF7_init_state(ei
, psenc
);
284 *nresult
= (size_t)_ENCODING_IS_STATE_DEPENDENT
;
288 if (psenc
->surrogate
) {
289 hi
= (psenc
->cache
>> 2) & UTF16_MAX
;
290 if (hi
< HISRG_MIN
|| hi
> HISRG_MAX
)
294 err
= _citrus_UTF7_mbtoutf16(ei
, &hi
, &s0
, n
, psenc
, &nr
);
295 if (nr
== (size_t)-1 || nr
== (size_t)-2) {
303 if (hi
< HISRG_MIN
|| hi
> HISRG_MAX
) {
307 psenc
->surrogate
= 1;
309 err
= _citrus_UTF7_mbtoutf16(ei
, &lo
, &s0
, n
, psenc
, &nr
);
310 if (nr
== (size_t)-1 || nr
== (size_t)-2) {
318 u32
= (hi
<< 10 | lo
) + SRG_BASE
;
324 if (u32
== (uint32_t)0) {
325 *nresult
= (size_t)0;
326 _citrus_UTF7_init_state(ei
, psenc
);
329 psenc
->surrogate
= 0;
335 _citrus_UTF7_utf16tomb(_UTF7EncodingInfo
* __restrict ei
,
336 char * __restrict s
, size_t n
, uint16_t u16
,
337 _UTF7State
* __restrict psenc
, size_t * __restrict nresult
)
341 _DIAGASSERT(ei
!= NULL
);
342 _DIAGASSERT(psenc
!= NULL
);
344 if (psenc
->chlen
!= 0 || psenc
->bits
> BASE64_BIT
)
347 if (ISSAFE(ei
, u16
)) {
349 if (psenc
->bits
> 0) {
350 bits
= BASE64_BIT
- psenc
->bits
;
351 i
= (psenc
->cache
<< bits
) & BASE64_MAX
;
352 psenc
->ch
[psenc
->chlen
++] = base64
[i
];
353 psenc
->bits
= psenc
->cache
= 0;
355 if (u16
== BASE64_OUT
|| FINDLEN(ei
, u16
) >= 0)
356 psenc
->ch
[psenc
->chlen
++] = BASE64_OUT
;
359 if (psenc
->bits
!= 0)
361 psenc
->ch
[psenc
->chlen
++] = (char)u16
;
362 if (u16
== BASE64_IN
)
363 psenc
->ch
[psenc
->chlen
++] = BASE64_OUT
;
368 psenc
->ch
[psenc
->chlen
++] = BASE64_IN
;
371 psenc
->cache
= (psenc
->cache
<< UTF16_BIT
) | u16
;
372 bits
= UTF16_BIT
+ psenc
->bits
;
373 psenc
->bits
= bits
% BASE64_BIT
;
374 while ((bits
-= BASE64_BIT
) >= 0) {
375 i
= (psenc
->cache
>> bits
) & BASE64_MAX
;
376 psenc
->ch
[psenc
->chlen
++] = base64
[i
];
379 memcpy(s
, psenc
->ch
, psenc
->chlen
);
380 *nresult
= psenc
->chlen
;
387 _citrus_UTF7_wcrtomb_priv(_UTF7EncodingInfo
* __restrict ei
,
388 char * __restrict s
, size_t n
, wchar_t wchar
,
389 _UTF7State
* __restrict psenc
, size_t * __restrict nresult
)
396 _DIAGASSERT(ei
!= NULL
);
397 _DIAGASSERT(s
!= NULL
);
398 _DIAGASSERT(psenc
!= NULL
);
399 _DIAGASSERT(nresult
!= NULL
);
401 u32
= (uint32_t)wchar
;
402 if (u32
<= UTF16_MAX
) {
403 u16
[0] = (uint16_t)u32
;
405 } else if (u32
<= UTF32_MAX
) {
407 u16
[0] = (u32
>> 10) + HISRG_MIN
;
408 u16
[1] = ((uint16_t)(u32
& UINT32_C(0x3ff))) + LOSRG_MIN
;
411 *nresult
= (size_t)-1;
415 for (i
= 0; i
< len
; ++i
) {
416 err
= _citrus_UTF7_utf16tomb(ei
, s
, n
, u16
[i
], psenc
, &nr
);
418 return err
; /* XXX: state has been modified */
430 _citrus_UTF7_put_state_reset(_UTF7EncodingInfo
* __restrict ei
,
431 char * __restrict s
, size_t n
, _UTF7State
* __restrict psenc
,
432 size_t * __restrict nresult
)
436 _DIAGASSERT(ei
!= NULL
);
437 _DIAGASSERT(s
!= NULL
);
438 _DIAGASSERT(psenc
!= NULL
);
439 _DIAGASSERT(nresult
!= NULL
);
441 if (psenc
->chlen
!= 0 || psenc
->bits
> BASE64_BIT
|| psenc
->surrogate
)
445 if (psenc
->bits
> 0) {
448 bits
= BASE64_BIT
- psenc
->bits
;
449 pos
= (psenc
->cache
<< bits
) & BASE64_MAX
;
450 psenc
->ch
[psenc
->chlen
++] = base64
[pos
];
451 psenc
->ch
[psenc
->chlen
++] = BASE64_OUT
;
452 psenc
->bits
= psenc
->cache
= 0;
456 if (psenc
->bits
!= 0)
461 _DIAGASSERT(n
>= psenc
->chlen
);
462 *nresult
= (size_t)psenc
->chlen
;
463 if (psenc
->chlen
> 0) {
464 memcpy(s
, psenc
->ch
, psenc
->chlen
);
473 _citrus_UTF7_stdenc_wctocs(_UTF7EncodingInfo
* __restrict ei
,
474 _csid_t
* __restrict csid
,
475 _index_t
* __restrict idx
, wchar_t wc
)
477 /* ei seem to be unused */
478 _DIAGASSERT(csid
!= NULL
);
479 _DIAGASSERT(idx
!= NULL
);
489 _citrus_UTF7_stdenc_cstowc(_UTF7EncodingInfo
* __restrict ei
,
490 wchar_t * __restrict wc
,
491 _csid_t csid
, _index_t idx
)
493 /* ei seem to be unused */
494 _DIAGASSERT(wc
!= NULL
);
505 _citrus_UTF7_stdenc_get_state_desc_generic(_UTF7EncodingInfo
* __restrict ei
,
506 _UTF7State
* __restrict psenc
,
507 int * __restrict rstate
)
510 if (psenc
->chlen
== 0)
511 *rstate
= _STDENC_SDGEN_INITIAL
;
513 *rstate
= _STDENC_SDGEN_INCOMPLETE_CHAR
;
520 _citrus_UTF7_encoding_module_uninit(_UTF7EncodingInfo
*ei
)
522 /* ei seems to be unused */
527 _citrus_UTF7_encoding_module_init(_UTF7EncodingInfo
* __restrict ei
,
528 const void * __restrict var
, size_t lenvar
)
532 _DIAGASSERT(ei
!= NULL
);
533 /* var may be null */
535 memset(ei
, 0, sizeof(*ei
));
537 #define FILL(str, flag) \
539 for (s = str; *s != '\0'; s++) \
540 ei->cell[*s & 0x7f] |= flag; \
541 } while (/*CONSTCOND*/0)
543 FILL(base64
, (s
- base64
) + 1);
544 FILL(direct
, EI_DIRECT
);
545 FILL(option
, EI_OPTION
);
546 FILL(spaces
, EI_SPACE
);
551 /* ----------------------------------------------------------------------
552 * public interface for ctype
555 _CITRUS_CTYPE_DECLS(UTF7
);
556 _CITRUS_CTYPE_DEF_OPS(UTF7
);
558 #include "citrus_ctype_template.h"
560 /* ----------------------------------------------------------------------
561 * public interface for stdenc
564 _CITRUS_STDENC_DECLS(UTF7
);
565 _CITRUS_STDENC_DEF_OPS(UTF7
);
567 #include "citrus_stdenc_template.h"