1 /* $NetBSD: citrus_utf7.c,v 1.4 2006/03/19 01:55:48 christos Exp $ */
4 * Copyright (c)2004, 2005 Citrus Project,
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 #if defined(LIB_SCCS) && !defined(lint)
32 __RCSID("$NetBSD: citrus_utf7.c,v 1.4 2006/03/19 01:55:48 christos Exp $");
33 #endif /* LIB_SCCS and not lint */
44 #include "citrus_namespace.h"
45 #include "citrus_types.h"
46 #include "citrus_module.h"
47 #include "citrus_ctype.h"
48 #include "citrus_stdenc.h"
49 #include "citrus_utf7.h"
51 /* ----------------------------------------------------------------------
52 * private stuffs used by templates
57 #define EI_MASK UINT16_C(0xff)
58 #define EI_DIRECT UINT16_C(0x100)
59 #define EI_OPTION UINT16_C(0x200)
60 #define EI_SPACE UINT16_C(0x400)
65 mode
: 1, /* whether base64 mode */
66 bits
: 4, /* need to hold 0 - 15 */
67 cache
: 22, /* 22 = BASE64_BIT + UTF16_BIT */
68 surrogate
: 1; /* whether surrogate pair or not */
70 char ch
[4]; /* BASE64_IN, 3 * 6 = 18, most closed to UTF16_BIT */
76 /* for future multi-locale facility */
81 _UTF7State s_mbsrtowcs
;
83 _UTF7State s_wcsrtombs
;
88 #define _CEI_TO_EI(_cei_) (&(_cei_)->ei)
89 #define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_
91 #define _FUNCNAME(m) _citrus_UTF7_##m
92 #define _ENCODING_INFO _UTF7EncodingInfo
93 #define _CTYPE_INFO _UTF7CTypeInfo
94 #define _ENCODING_STATE _UTF7State
95 #define _ENCODING_MB_CUR_MAX(_ei_) 4
96 #define _ENCODING_IS_STATE_DEPENDENT 1
97 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0
101 _citrus_UTF7_init_state(_UTF7EncodingInfo
* __restrict ei
,
102 _UTF7State
* __restrict s
)
104 /* ei appears to be unused */
105 _DIAGASSERT(s
!= NULL
);
107 memset((void *)s
, 0, sizeof(*s
));
112 _citrus_UTF7_pack_state(_UTF7EncodingInfo
* __restrict ei
,
113 void *__restrict pspriv
, const _UTF7State
* __restrict s
)
115 /* ei seem to be unused */
116 _DIAGASSERT(pspriv
!= NULL
);
117 _DIAGASSERT(s
!= NULL
);
119 memcpy(pspriv
, (const void *)s
, sizeof(*s
));
124 _citrus_UTF7_unpack_state(_UTF7EncodingInfo
* __restrict ei
,
125 _UTF7State
* __restrict s
, const void * __restrict pspriv
)
127 /* ei seem to be unused */
128 _DIAGASSERT(s
!= NULL
);
129 _DIAGASSERT(pspriv
!= NULL
);
131 memcpy((void *)s
, pspriv
, sizeof(*s
));
134 static const char base64
[] =
135 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
136 "abcdefghijklmnopqrstuvwxyz"
139 static const char direct
[] =
140 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
141 "abcdefghijklmnopqrstuvwxyz"
142 "0123456789(),-./:?";
144 static const char option
[] = "!\"#$%&';<=>@[]^_`{|}";
145 static const char spaces
[] = " \t\r\n";
150 #define BASE64_MAX 0x3f
151 #define UTF16_MAX UINT16_C(0xffff)
152 #define UTF32_MAX UINT32_C(0x10ffff)
154 #define BASE64_IN '+'
155 #define BASE64_OUT '-'
157 #define SHIFT7BIT(c) ((c) >> 7)
158 #define ISSPECIAL(c) ((c) == '\0' || (c) == BASE64_IN)
160 #define FINDLEN(ei, c) \
161 (SHIFT7BIT((c)) ? -1 : (((ei)->cell[(c)] & EI_MASK) - 1))
163 #define ISDIRECT(ei, c) (!SHIFT7BIT((c)) && (ISSPECIAL((c)) || \
164 ei->cell[(c)] & (EI_DIRECT | EI_OPTION | EI_SPACE)))
166 #define ISSAFE(ei, c) (!SHIFT7BIT((c)) && (ISSPECIAL((c)) || \
167 (c < 0x80 && ei->cell[(c)] & (EI_DIRECT | EI_SPACE))))
170 #define SRG_BASE UINT32_C(0x10000)
171 #define HISRG_MIN UINT16_C(0xd800)
172 #define HISRG_MAX UINT16_C(0xdbff)
173 #define LOSRG_MIN UINT16_C(0xdc00)
174 #define LOSRG_MAX UINT16_C(0xdfff)
177 _citrus_UTF7_mbtoutf16(_UTF7EncodingInfo
* __restrict ei
,
178 uint16_t * __restrict u16
, const char ** __restrict s
, size_t n
,
179 _UTF7State
* __restrict psenc
, size_t * __restrict nresult
)
185 _DIAGASSERT(ei
!= NULL
);
186 _DIAGASSERT(s
!= NULL
&& *s
!= NULL
);
187 _DIAGASSERT(psenc
!= NULL
);
192 for (i
= 0, done
= 0; done
== 0; i
++) {
193 _DIAGASSERT(i
<= psenc
->chlen
);
194 if (i
== psenc
->chlen
) {
196 *nresult
= (size_t)-2;
198 sv
.chlen
= psenc
->chlen
;
202 psenc
->ch
[psenc
->chlen
++] = *s0
++;
204 if (SHIFT7BIT((int)psenc
->ch
[i
]))
207 if (psenc
->bits
> 0 || psenc
->cache
> 0)
209 if (psenc
->ch
[i
] == BASE64_IN
) {
212 if (!ISDIRECT(ei
, (int)psenc
->ch
[i
]))
214 *u16
= (uint16_t)psenc
->ch
[i
];
219 if (psenc
->ch
[i
] == BASE64_OUT
&& psenc
->cache
== 0) {
221 *u16
= (uint16_t)BASE64_IN
;
225 len
= FINDLEN(ei
, (int)psenc
->ch
[i
]);
227 if (psenc
->bits
>= BASE64_BIT
)
230 psenc
->bits
= psenc
->cache
= 0;
231 if (psenc
->ch
[i
] != BASE64_OUT
) {
232 if (!ISDIRECT(ei
, (int)psenc
->ch
[i
]))
234 *u16
= (uint16_t)psenc
->ch
[i
];
239 (psenc
->cache
<< BASE64_BIT
) | len
;
240 switch (psenc
->bits
) {
241 case 0: case 2: case 4: case 6: case 8:
242 psenc
->bits
+= BASE64_BIT
;
244 case 10: case 12: case 14:
245 psenc
->bits
-= (UTF16_BIT
- BASE64_BIT
);
246 *u16
= (psenc
->cache
>> psenc
->bits
)
257 if (psenc
->chlen
> i
)
260 *nresult
= (size_t)((*u16
== 0) ? 0 : s0
- *s
);
266 *nresult
= (size_t)-1;
271 _citrus_UTF7_mbrtowc_priv(_UTF7EncodingInfo
* __restrict ei
,
272 wchar_t * __restrict pwc
, const char ** __restrict s
, size_t n
,
273 _UTF7State
* __restrict psenc
, size_t * __restrict nresult
)
281 _DIAGASSERT(ei
!= NULL
);
282 /* pwc may be null */
283 _DIAGASSERT(s
!= NULL
);
284 _DIAGASSERT(psenc
!= NULL
);
287 _citrus_UTF7_init_state(ei
, psenc
);
288 *nresult
= (size_t)_ENCODING_IS_STATE_DEPENDENT
;
292 if (psenc
->surrogate
) {
293 hi
= (psenc
->cache
>> 2) & UTF16_MAX
;
294 if (hi
< HISRG_MIN
|| hi
> HISRG_MAX
)
298 err
= _citrus_UTF7_mbtoutf16(ei
, &hi
, &s0
, n
, psenc
, &nr
);
299 if (nr
== (size_t)-1 || nr
== (size_t)-2) {
307 if (hi
< HISRG_MIN
|| hi
> HISRG_MAX
) {
311 psenc
->surrogate
= 1;
313 err
= _citrus_UTF7_mbtoutf16(ei
, &lo
, &s0
, n
, psenc
, &nr
);
314 if (nr
== (size_t)-1 || nr
== (size_t)-2) {
322 u32
= (hi
<< 10 | lo
) + SRG_BASE
;
328 if (u32
== (uint32_t)0) {
329 *nresult
= (size_t)0;
330 _citrus_UTF7_init_state(ei
, psenc
);
333 psenc
->surrogate
= 0;
339 _citrus_UTF7_utf16tomb(_UTF7EncodingInfo
* __restrict ei
,
340 char * __restrict s
, size_t n
, uint16_t u16
,
341 _UTF7State
* __restrict psenc
, size_t * __restrict nresult
)
345 _DIAGASSERT(ei
!= NULL
);
346 _DIAGASSERT(psenc
!= NULL
);
348 if (psenc
->chlen
!= 0 || psenc
->bits
> BASE64_BIT
)
351 if (ISSAFE(ei
, u16
)) {
353 if (psenc
->bits
> 0) {
354 bits
= BASE64_BIT
- psenc
->bits
;
355 i
= (psenc
->cache
<< bits
) & BASE64_MAX
;
356 psenc
->ch
[psenc
->chlen
++] = base64
[i
];
357 psenc
->bits
= psenc
->cache
= 0;
359 if (u16
== BASE64_OUT
|| FINDLEN(ei
, u16
) >= 0)
360 psenc
->ch
[psenc
->chlen
++] = BASE64_OUT
;
363 if (psenc
->bits
!= 0)
365 psenc
->ch
[psenc
->chlen
++] = (char)u16
;
366 if (u16
== BASE64_IN
)
367 psenc
->ch
[psenc
->chlen
++] = BASE64_OUT
;
372 psenc
->ch
[psenc
->chlen
++] = BASE64_IN
;
375 psenc
->cache
= (psenc
->cache
<< UTF16_BIT
) | u16
;
376 bits
= UTF16_BIT
+ psenc
->bits
;
377 psenc
->bits
= bits
% BASE64_BIT
;
378 while ((bits
-= BASE64_BIT
) >= 0) {
379 i
= (psenc
->cache
>> bits
) & BASE64_MAX
;
380 psenc
->ch
[psenc
->chlen
++] = base64
[i
];
383 memcpy(s
, psenc
->ch
, psenc
->chlen
);
384 *nresult
= psenc
->chlen
;
391 _citrus_UTF7_wcrtomb_priv(_UTF7EncodingInfo
* __restrict ei
,
392 char * __restrict s
, size_t n
, wchar_t wchar
,
393 _UTF7State
* __restrict psenc
, size_t * __restrict nresult
)
400 _DIAGASSERT(ei
!= NULL
);
401 _DIAGASSERT(s
!= NULL
);
402 _DIAGASSERT(psenc
!= NULL
);
403 _DIAGASSERT(nresult
!= NULL
);
405 u32
= (uint32_t)wchar
;
406 if (u32
<= UTF16_MAX
) {
407 u16
[0] = (uint16_t)u32
;
409 } else if (u32
<= UTF32_MAX
) {
411 u16
[0] = (u32
>> 10) + HISRG_MIN
;
412 u16
[1] = ((uint16_t)(u32
& UINT32_C(0x3ff))) + LOSRG_MIN
;
415 *nresult
= (size_t)-1;
419 for (i
= 0; i
< len
; ++i
) {
420 err
= _citrus_UTF7_utf16tomb(ei
, s
, n
, u16
[i
], psenc
, &nr
);
422 return err
; /* XXX: state has been modified */
434 _citrus_UTF7_put_state_reset(_UTF7EncodingInfo
* __restrict ei
,
435 char * __restrict s
, size_t n
, _UTF7State
* __restrict psenc
,
436 size_t * __restrict nresult
)
440 _DIAGASSERT(ei
!= NULL
);
441 _DIAGASSERT(s
!= NULL
);
442 _DIAGASSERT(psenc
!= NULL
);
443 _DIAGASSERT(nresult
!= NULL
);
445 if (psenc
->chlen
!= 0 || psenc
->bits
> BASE64_BIT
|| psenc
->surrogate
)
449 if (psenc
->bits
> 0) {
452 bits
= BASE64_BIT
- psenc
->bits
;
453 pos
= (psenc
->cache
<< bits
) & BASE64_MAX
;
454 psenc
->ch
[psenc
->chlen
++] = base64
[pos
];
455 psenc
->ch
[psenc
->chlen
++] = BASE64_OUT
;
456 psenc
->bits
= psenc
->cache
= 0;
460 if (psenc
->bits
!= 0)
465 _DIAGASSERT(n
>= psenc
->chlen
);
466 *nresult
= (size_t)psenc
->chlen
;
467 if (psenc
->chlen
> 0) {
468 memcpy(s
, psenc
->ch
, psenc
->chlen
);
477 _citrus_UTF7_stdenc_wctocs(_UTF7EncodingInfo
* __restrict ei
,
478 _csid_t
* __restrict csid
,
479 _index_t
* __restrict idx
, wchar_t wc
)
481 /* ei seem to be unused */
482 _DIAGASSERT(csid
!= NULL
);
483 _DIAGASSERT(idx
!= NULL
);
493 _citrus_UTF7_stdenc_cstowc(_UTF7EncodingInfo
* __restrict ei
,
494 wchar_t * __restrict wc
,
495 _csid_t csid
, _index_t idx
)
497 /* ei seem to be unused */
498 _DIAGASSERT(wc
!= NULL
);
509 _citrus_UTF7_stdenc_get_state_desc_generic(_UTF7EncodingInfo
* __restrict ei
,
510 _UTF7State
* __restrict psenc
,
511 int * __restrict rstate
)
514 if (psenc
->chlen
== 0)
515 *rstate
= _STDENC_SDGEN_INITIAL
;
517 *rstate
= _STDENC_SDGEN_INCOMPLETE_CHAR
;
524 _citrus_UTF7_encoding_module_uninit(_UTF7EncodingInfo
*ei
)
526 /* ei seems to be unused */
531 _citrus_UTF7_encoding_module_init(_UTF7EncodingInfo
* __restrict ei
,
532 const void * __restrict var
, size_t lenvar
)
536 _DIAGASSERT(ei
!= NULL
);
537 /* var may be null */
539 memset(ei
, 0, sizeof(*ei
));
541 #define FILL(str, flag) \
543 for (s = str; *s != '\0'; s++) \
544 ei->cell[*s & 0x7f] |= flag; \
545 } while (/*CONSTCOND*/0)
547 FILL(base64
, (s
- base64
) + 1);
548 FILL(direct
, EI_DIRECT
);
549 FILL(option
, EI_OPTION
);
550 FILL(spaces
, EI_SPACE
);
555 /* ----------------------------------------------------------------------
556 * public interface for ctype
559 _CITRUS_CTYPE_DECLS(UTF7
);
560 _CITRUS_CTYPE_DEF_OPS(UTF7
);
562 #include "citrus_ctype_template.h"
564 /* ----------------------------------------------------------------------
565 * public interface for stdenc
568 _CITRUS_STDENC_DECLS(UTF7
);
569 _CITRUS_STDENC_DEF_OPS(UTF7
);
571 #include "citrus_stdenc_template.h"