1 /* $FreeBSD: head/lib/libiconv_modules/UTF7/citrus_utf7.c 283418 2015-05-24 15:47:06Z tijl $ */
2 /* $NetBSD: citrus_utf7.c,v 1.5 2006/08/23 12:57:24 tnozaki 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
31 #include <sys/cdefs.h>
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"
48 /* ----------------------------------------------------------------------
49 * private stuffs used by templates
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)
63 mode
: 1, /* whether base64 mode */
64 bits
: 4, /* need to hold 0 - 15 */
65 cache
: 22; /* 22 = BASE64_BIT + UTF16_BIT */
67 char ch
[4]; /* BASE64_IN, 3 * 6 = 18, most closed to UTF16_BIT */
70 #define _CEI_TO_EI(_cei_) (&(_cei_)->ei)
71 #define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_
73 #define _FUNCNAME(m) _citrus_UTF7_##m
74 #define _ENCODING_INFO _UTF7EncodingInfo
75 #define _ENCODING_STATE _UTF7State
76 #define _ENCODING_MB_CUR_MAX(_ei_) 4
77 #define _ENCODING_IS_STATE_DEPENDENT 1
78 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0
82 _citrus_UTF7_init_state(_UTF7EncodingInfo
* __restrict ei __unused
,
83 _UTF7State
* __restrict s
)
86 memset((void *)s
, 0, sizeof(*s
));
92 _citrus_UTF7_pack_state(_UTF7EncodingInfo
* __restrict ei __unused
,
93 void *__restrict pspriv
, const _UTF7State
* __restrict s
)
96 memcpy(pspriv
, (const void *)s
, sizeof(*s
));
101 _citrus_UTF7_unpack_state(_UTF7EncodingInfo
* __restrict ei __unused
,
102 _UTF7State
* __restrict s
, const void * __restrict pspriv
)
105 memcpy((void *)s
, pspriv
, sizeof(*s
));
109 static const char base64
[] =
110 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
111 "abcdefghijklmnopqrstuvwxyz"
114 static const char direct
[] =
115 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
116 "abcdefghijklmnopqrstuvwxyz"
117 "0123456789'(),-./:?";
119 static const char option
[] = "!\"#$%&*;<=>@[]^_`{|}";
120 static const char spaces
[] = " \t\r\n";
125 #define BASE64_MAX 0x3f
126 #define UTF16_MAX UINT16_C(0xffff)
127 #define UTF32_MAX UINT32_C(0x10ffff)
129 #define BASE64_IN '+'
130 #define BASE64_OUT '-'
132 #define SHIFT7BIT(c) ((c) >> 7)
133 #define ISSPECIAL(c) ((c) == '\0' || (c) == BASE64_IN)
135 #define FINDLEN(ei, c) \
136 (SHIFT7BIT((c)) ? -1 : (((ei)->cell[(c)] & EI_MASK) - 1))
138 #define ISDIRECT(ei, c) (!SHIFT7BIT((c)) && (ISSPECIAL((c)) || \
139 ei->cell[(c)] & (EI_DIRECT | EI_OPTION | EI_SPACE)))
141 #define ISSAFE(ei, c) (!SHIFT7BIT((c)) && (ISSPECIAL((c)) || \
142 (c < 0x80 && ei->cell[(c)] & (EI_DIRECT | EI_SPACE))))
145 #define SRG_BASE UINT32_C(0x10000)
146 #define HISRG_MIN UINT16_C(0xd800)
147 #define HISRG_MAX UINT16_C(0xdbff)
148 #define LOSRG_MIN UINT16_C(0xdc00)
149 #define LOSRG_MAX UINT16_C(0xdfff)
152 _citrus_UTF7_mbtoutf16(_UTF7EncodingInfo
* __restrict ei
,
153 uint16_t * __restrict u16
, char ** __restrict s
, size_t n
,
154 _UTF7State
* __restrict psenc
, size_t * __restrict nresult
)
162 for (i
= 0, done
= 0; done
== 0; i
++) {
163 if (i
== psenc
->chlen
) {
165 *nresult
= (size_t)-2;
169 psenc
->ch
[psenc
->chlen
++] = *s0
++;
171 if (SHIFT7BIT((int)psenc
->ch
[i
]))
174 if (psenc
->bits
> 0 || psenc
->cache
> 0)
176 if (psenc
->ch
[i
] == BASE64_IN
)
179 if (!ISDIRECT(ei
, (int)psenc
->ch
[i
]))
181 *u16
= (uint16_t)psenc
->ch
[i
];
186 if (psenc
->ch
[i
] == BASE64_OUT
&& psenc
->cache
== 0) {
188 *u16
= (uint16_t)BASE64_IN
;
192 len
= FINDLEN(ei
, (int)psenc
->ch
[i
]);
194 if (psenc
->bits
>= BASE64_BIT
)
197 psenc
->bits
= psenc
->cache
= 0;
198 if (psenc
->ch
[i
] != BASE64_OUT
) {
199 if (!ISDIRECT(ei
, (int)psenc
->ch
[i
]))
201 *u16
= (uint16_t)psenc
->ch
[i
];
209 (psenc
->cache
<< BASE64_BIT
) | len
;
210 switch (psenc
->bits
) {
211 case 0: case 2: case 4: case 6: case 8:
212 psenc
->bits
+= BASE64_BIT
;
214 case 10: case 12: case 14:
215 psenc
->bits
-= (UTF16_BIT
- BASE64_BIT
);
216 *u16
= (psenc
->cache
>> psenc
->bits
) &
227 if (psenc
->chlen
> i
)
230 *nresult
= (size_t)((*u16
== 0) ? 0 : s0
- *s
);
236 *nresult
= (size_t)-1;
241 _citrus_UTF7_mbrtowc_priv(_UTF7EncodingInfo
* __restrict ei
,
242 wchar_t * __restrict pwc
, char ** __restrict s
, size_t n
,
243 _UTF7State
* __restrict psenc
, size_t * __restrict nresult
)
251 _citrus_UTF7_init_state(ei
, psenc
);
252 *nresult
= (size_t)_ENCODING_IS_STATE_DEPENDENT
;
255 err
= _citrus_UTF7_mbtoutf16(ei
, &hi
, s
, n
, psenc
, &nr
);
256 if (nr
== (size_t)-1 || nr
== (size_t)-2) {
264 if (hi
< HISRG_MIN
|| hi
> HISRG_MAX
) {
268 err
= _citrus_UTF7_mbtoutf16(ei
, &lo
, s
, n
, psenc
, &nr
);
269 if (nr
== (size_t)-1 || nr
== (size_t)-2) {
270 psenc
->chlen
= 1; /* make get_state_desc return incomplete */
276 if (lo
< LOSRG_MIN
|| lo
> LOSRG_MAX
) {
277 *nresult
= (size_t)-1;
282 u32
= (hi
<< 10 | lo
) + SRG_BASE
;
287 if (u32
== (uint32_t)0) {
288 *nresult
= (size_t)0;
289 _citrus_UTF7_init_state(ei
, psenc
);
297 _citrus_UTF7_utf16tomb(_UTF7EncodingInfo
* __restrict ei
,
298 char * __restrict s
, size_t n __unused
, uint16_t u16
,
299 _UTF7State
* __restrict psenc
, size_t * __restrict nresult
)
303 if (psenc
->chlen
!= 0 || psenc
->bits
> BASE64_BIT
)
306 if (ISSAFE(ei
, u16
)) {
308 if (psenc
->bits
> 0) {
309 bits
= BASE64_BIT
- psenc
->bits
;
310 i
= (psenc
->cache
<< bits
) & BASE64_MAX
;
311 psenc
->ch
[psenc
->chlen
++] = base64
[i
];
312 psenc
->bits
= psenc
->cache
= 0;
314 if (u16
== BASE64_OUT
|| FINDLEN(ei
, u16
) >= 0)
315 psenc
->ch
[psenc
->chlen
++] = BASE64_OUT
;
318 if (psenc
->bits
!= 0)
320 psenc
->ch
[psenc
->chlen
++] = (char)u16
;
321 if (u16
== BASE64_IN
)
322 psenc
->ch
[psenc
->chlen
++] = BASE64_OUT
;
327 psenc
->ch
[psenc
->chlen
++] = BASE64_IN
;
330 psenc
->cache
= (psenc
->cache
<< UTF16_BIT
) | u16
;
331 bits
= UTF16_BIT
+ psenc
->bits
;
332 psenc
->bits
= bits
% BASE64_BIT
;
333 while ((bits
-= BASE64_BIT
) >= 0) {
334 i
= (psenc
->cache
>> bits
) & BASE64_MAX
;
335 psenc
->ch
[psenc
->chlen
++] = base64
[i
];
338 memcpy(s
, psenc
->ch
, psenc
->chlen
);
339 *nresult
= psenc
->chlen
;
346 _citrus_UTF7_wcrtomb_priv(_UTF7EncodingInfo
* __restrict ei
,
347 char * __restrict s
, size_t n
, wchar_t wchar
,
348 _UTF7State
* __restrict psenc
, size_t * __restrict nresult
)
355 u32
= (uint32_t)wchar
;
356 if (u32
<= UTF16_MAX
) {
357 u16
[0] = (uint16_t)u32
;
359 } else if (u32
<= UTF32_MAX
) {
361 u16
[0] = (u32
>> 10) + HISRG_MIN
;
362 u16
[1] = ((uint16_t)(u32
& UINT32_C(0x3ff))) + LOSRG_MIN
;
365 *nresult
= (size_t)-1;
369 for (i
= 0; i
< len
; ++i
) {
370 err
= _citrus_UTF7_utf16tomb(ei
, s
, n
, u16
[i
], psenc
, &nr
);
372 return (err
); /* XXX: state has been modified */
384 _citrus_UTF7_put_state_reset(_UTF7EncodingInfo
* __restrict ei __unused
,
385 char * __restrict s
, size_t n
, _UTF7State
* __restrict psenc
,
386 size_t * __restrict nresult
)
390 if (psenc
->chlen
!= 0 || psenc
->bits
> BASE64_BIT
)
394 if (psenc
->bits
> 0) {
397 bits
= BASE64_BIT
- psenc
->bits
;
398 pos
= (psenc
->cache
<< bits
) & BASE64_MAX
;
399 psenc
->ch
[psenc
->chlen
++] = base64
[pos
];
400 psenc
->ch
[psenc
->chlen
++] = BASE64_OUT
;
401 psenc
->bits
= psenc
->cache
= 0;
405 if (psenc
->bits
!= 0)
410 *nresult
= (size_t)psenc
->chlen
;
411 if (psenc
->chlen
> 0) {
412 memcpy(s
, psenc
->ch
, psenc
->chlen
);
421 _citrus_UTF7_stdenc_wctocs(_UTF7EncodingInfo
* __restrict ei __unused
,
422 _csid_t
* __restrict csid
, _index_t
* __restrict idx
, wchar_t wc
)
433 _citrus_UTF7_stdenc_cstowc(_UTF7EncodingInfo
* __restrict ei __unused
,
434 wchar_t * __restrict wc
, _csid_t csid
, _index_t idx
)
446 _citrus_UTF7_stdenc_get_state_desc_generic(_UTF7EncodingInfo
* __restrict ei __unused
,
447 _UTF7State
* __restrict psenc
, int * __restrict rstate
)
450 *rstate
= (psenc
->chlen
== 0) ? _STDENC_SDGEN_INITIAL
:
451 _STDENC_SDGEN_INCOMPLETE_CHAR
;
457 _citrus_UTF7_encoding_module_uninit(_UTF7EncodingInfo
*ei __unused
)
460 /* ei seems to be unused */
465 _citrus_UTF7_encoding_module_init(_UTF7EncodingInfo
* __restrict ei
,
466 const void * __restrict var __unused
, size_t lenvar __unused
)
470 memset(ei
, 0, sizeof(*ei
));
472 #define FILL(str, flag) \
474 for (s = str; *s != '\0'; s++) \
475 ei->cell[*s & 0x7f] |= flag; \
476 } while (/*CONSTCOND*/0)
478 FILL(base64
, (s
- base64
) + 1);
479 FILL(direct
, EI_DIRECT
);
480 FILL(option
, EI_OPTION
);
481 FILL(spaces
, EI_SPACE
);
486 /* ----------------------------------------------------------------------
487 * public interface for stdenc
490 _CITRUS_STDENC_DECLS(UTF7
);
491 _CITRUS_STDENC_DEF_OPS(UTF7
);
493 #include "citrus_stdenc_template.h"