Add citrus backend code and iconv front end. This is intentionally
[dragonfly.git] / lib / libc / citrus / modules / citrus_utf1632.c
1 /*      $NetBSD: src/lib/libc/citrus/modules/citrus_utf1632.c,v 1.3 2003/06/27 12:55:13 yamt Exp $      */
2 /*      $DragonFly: src/lib/libc/citrus/modules/citrus_utf1632.c,v 1.1 2005/03/11 23:33:53 joerg Exp $ */
3
4 /*-
5  * Copyright (c)2003 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 #include <sys/types.h>
31 #include <sys/endian.h>
32 #include <assert.h>
33 #include <errno.h>
34 #include <limits.h>
35 #include <locale.h>
36 #include <stddef.h>
37 #include <stdio.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_bcs.h"
47
48 #include "citrus_utf1632.h"
49
50
51 /* ----------------------------------------------------------------------
52  * private stuffs used by templates
53  */
54
55 typedef struct {
56         u_int8_t                ch[4];
57         int                     chlen;
58         int                     current_endian;
59 } _UTF1632State;
60
61 typedef struct {
62         int             preffered_endian;
63         unsigned int    cur_max;
64 #define _ENDIAN_UNKNOWN 0
65 #define _ENDIAN_BIG     1
66 #define _ENDIAN_LITTLE  2
67         u_int32_t       mode;
68 #define _MODE_UTF32             0x00000001U
69 #define _MODE_FORCE_ENDIAN      0x00000002U
70 } _UTF1632EncodingInfo;
71
72 #define _FUNCNAME(m)                    _citrus_UTF1632_##m
73 #define _ENCODING_INFO                  _UTF1632EncodingInfo
74 #define _ENCODING_STATE                 _UTF1632State
75 #define _ENCODING_MB_CUR_MAX(_ei_)      ((_ei_)->cur_max)
76 #define _ENCODING_IS_STATE_DEPENDENT    0
77 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_)        0
78
79
80 static __inline void
81 /*ARGSUSED*/
82 _citrus_UTF1632_init_state(_UTF1632EncodingInfo *ei, _UTF1632State *s)
83 {
84         memset(s, 0, sizeof(*s));
85 }
86
87 static int
88 _citrus_UTF1632_mbrtowc_priv(_UTF1632EncodingInfo *ei, wchar_t *pwc,
89                              const char **s, size_t n, _UTF1632State *psenc,
90                              size_t *nresult)
91 {
92         int chlenbak, endian, needlen;
93         wchar_t wc;
94         size_t result;
95         const char *s0;
96
97         _DIAGASSERT(nresult != 0);
98         _DIAGASSERT(ei != NULL);
99         _DIAGASSERT(s != NULL);
100         _DIAGASSERT(psenc != NULL);
101
102         s0 = *s;
103
104         if (s0 == NULL) {
105                 _citrus_UTF1632_init_state(ei, psenc);
106                 *nresult = 0; /* state independent */
107                 return (0);
108         }
109
110         result = 0;
111         chlenbak = psenc->chlen;
112
113 refetch:
114         if ((ei->mode & _MODE_UTF32) != 0 || chlenbak>=2)
115                 needlen = 4;
116         else
117                 needlen = 2;
118
119         while (chlenbak < needlen) {
120                 if (n==0)
121                         goto restart;
122                 psenc->ch[chlenbak++] = *s0++;
123                 n--;
124                 result++;
125         }
126
127         /* judge endian marker */
128         if ((ei->mode & _MODE_UTF32) == 0) {
129                 /* UTF16 */
130                 if (psenc->ch[0]==0xFE && psenc->ch[1]==0xFF) {
131                         psenc->current_endian = _ENDIAN_BIG;
132                         chlenbak = 0;
133                         goto refetch;
134                 } else if (psenc->ch[0]==0xFF && psenc->ch[1]==0xFE) {
135                         psenc->current_endian = _ENDIAN_LITTLE;
136                         chlenbak = 0;
137                         goto refetch;
138                 }
139         } else {
140                 /* UTF32 */
141                 if (psenc->ch[0]==0x00 && psenc->ch[1]==0x00 &&
142                     psenc->ch[2]==0xFE && psenc->ch[3]==0xFF) {
143                         psenc->current_endian = _ENDIAN_BIG;
144                         chlenbak = 0;
145                         goto refetch;
146                 } else if (psenc->ch[0]==0xFF && psenc->ch[1]==0xFE &&
147                            psenc->ch[2]==0x00 && psenc->ch[3]==0x00) {
148                         psenc->current_endian = _ENDIAN_LITTLE;
149                         chlenbak = 0;
150                         goto refetch;
151                 }
152         }
153         if ((ei->mode & _MODE_FORCE_ENDIAN) != 0 ||
154             psenc->current_endian == _ENDIAN_UNKNOWN)
155                 endian = ei->preffered_endian;
156         else
157                 endian = psenc->current_endian;
158
159         /* get wc */
160         if ((ei->mode & _MODE_UTF32) == 0) {
161                 /* UTF16 */
162                 if (needlen==2) {
163                         switch (endian) {
164                         case _ENDIAN_LITTLE:
165                                 wc = (psenc->ch[0] |
166                                       ((wchar_t)psenc->ch[1] << 8));
167                                 break;
168                         case _ENDIAN_BIG:
169                                 wc = (psenc->ch[1] |
170                                       ((wchar_t)psenc->ch[0] << 8));
171                                 break;
172                         }
173                         if (wc >= 0xD800 && wc <= 0xDBFF) {
174                                 /* surrogate high */
175                                 needlen=4;
176                                 goto refetch;
177                         }
178                 } else {
179                         /* surrogate low */
180                         wc -= 0xD800; /* wc : surrogate high (see above) */
181                         wc <<= 10;
182                         switch (endian) {
183                         case _ENDIAN_LITTLE:
184                                 if (psenc->ch[2]<0xDC || psenc->ch[2]>0xDF)
185                                         goto ilseq;
186                                 wc |= psenc->ch[2];
187                                 wc |= (wchar_t)(psenc->ch[3] & 3) << 8;
188                                 break;
189                         case _ENDIAN_BIG:
190                                 if (psenc->ch[3]<0xDC || psenc->ch[3]>0xDF)
191                                         goto ilseq;
192                                 wc |= psenc->ch[3];
193                                 wc |= (wchar_t)(psenc->ch[2] & 3) << 8;
194                                 break;
195                         }
196                         wc += 0x10000;
197                 }
198         } else {
199                 /* UTF32 */
200                 switch (endian) {
201                 case _ENDIAN_LITTLE:
202                         wc = (psenc->ch[0] |
203                               ((wchar_t)psenc->ch[1] << 8) |
204                               ((wchar_t)psenc->ch[2] << 16) |
205                               ((wchar_t)psenc->ch[3] << 24));
206                         break;
207                 case _ENDIAN_BIG:
208                         wc = (psenc->ch[3] |
209                               ((wchar_t)psenc->ch[2] << 8) |
210                               ((wchar_t)psenc->ch[1] << 16) |
211                               ((wchar_t)psenc->ch[0] << 24));
212                         break;
213                 }
214         }
215
216
217         *pwc = wc;
218         psenc->chlen = 0;
219         *nresult = result;
220         *s = s0;
221
222         return (0);
223
224 ilseq:
225         *nresult = (size_t)-1;
226         psenc->chlen = 0;
227         return (EILSEQ);
228
229 restart:
230         *nresult = (size_t)-2;
231         psenc->chlen = chlenbak;
232         *s = s0;
233         return (0);
234 }
235
236 static int
237 _citrus_UTF1632_wcrtomb_priv(_UTF1632EncodingInfo *ei, char *s, size_t n,
238                              wchar_t wc, _UTF1632State *psenc,
239                              size_t *nresult)
240 {
241         int ret;
242         wchar_t wc2;
243
244         _DIAGASSERT(ei != NULL);
245         _DIAGASSERT(nresult != 0);
246         _DIAGASSERT(s != NULL);
247
248         wc2 = 0;
249         if ((ei->mode & _MODE_UTF32)==0) {
250                 /* UTF16 */
251                 if (wc>0xFFFF) {
252                         /* surrogate */
253                         if (wc>0x10FFFF) {
254                                 ret = EILSEQ;
255                                 goto err;
256                         }
257                         if (n < 4) {
258                                 ret = E2BIG;
259                                 goto err;
260                         }
261                         wc -= 0x10000;
262                         wc2 = (wc & 0x3FF) | 0xDC00;
263                         wc = (wc>>10) | 0xD800;
264                         *nresult = (size_t)4;
265                 } else {
266                         if (n < 2) {
267                                 ret = E2BIG;
268                                 goto err;
269                         }
270                         *nresult = (size_t)2;
271                 }
272
273 surrogate:
274                 switch (ei->preffered_endian) {
275                 case _ENDIAN_BIG:
276                         s[1] = wc;
277                         s[0] = (wc >>= 8);
278                         break;
279                 case _ENDIAN_LITTLE:
280                         s[0] = wc;
281                         s[1] = (wc >>= 8);
282                         break;
283                 }
284                 if (wc2!=0) {
285                         wc = wc2;
286                         wc2 = 0;
287                         s += 2;
288                         goto surrogate;
289                 }
290         } else {
291                 /* UTF32 */
292                 if (n < 4) {
293                         ret = E2BIG;
294                         goto err;
295                 }
296                 switch (ei->preffered_endian) {
297                 case _ENDIAN_BIG:
298                         s[3] = wc;
299                         s[2] = (wc >>= 8);
300                         s[1] = (wc >>= 8);
301                         s[0] = (wc >>= 8);
302                         break;
303                 case _ENDIAN_LITTLE:
304                         s[0] = wc;
305                         s[1] = (wc >>= 8);
306                         s[2] = (wc >>= 8);
307                         s[3] = (wc >>= 8);
308                         break;
309                 }
310                 *nresult = (size_t)4;
311         }
312
313         return 0;
314
315 err:
316         *nresult = (size_t)-1;
317         return ret;
318 }
319
320 static void
321 parse_variable(_UTF1632EncodingInfo * __restrict ei,
322                const void * __restrict var, size_t lenvar)
323 {
324 #define MATCH(x, act)                                           \
325 do {                                                            \
326         if (lenvar >= (sizeof(#x)-1) &&                         \
327             _bcs_strncasecmp(p, #x, sizeof(#x)-1) == 0) {       \
328                 act;                                            \
329                 lenvar -= sizeof(#x)-1;                         \
330                 p += sizeof(#x)-1;                              \
331         }                                                       \
332 } while (/*CONSTCOND*/0)
333         const char *p;
334         p = var;
335         while (lenvar>0) {
336                 switch (*p) {
337                 case 'B':
338                 case 'b':
339                         MATCH(big, ei->preffered_endian = _ENDIAN_BIG);
340                         break;
341                 case 'L':
342                 case 'l':
343                         MATCH(little, ei->preffered_endian = _ENDIAN_LITTLE);
344                         break;
345                 case 'F':
346                 case 'f':
347                         MATCH(force, ei->mode |= _MODE_FORCE_ENDIAN);
348                         break;
349                 case 'U':
350                 case 'u':
351                         MATCH(utf32, ei->mode |= _MODE_UTF32);
352                         break;
353                 }
354                 p++;
355                 lenvar--;
356         }
357 }
358
359 static int
360 /*ARGSUSED*/
361 _citrus_UTF1632_encoding_module_init(_UTF1632EncodingInfo * __restrict ei,
362                                      const void * __restrict var,
363                                      size_t lenvar)
364 {
365         _DIAGASSERT(ei != NULL);
366
367         memset((void *)ei, 0, sizeof(*ei));
368
369         parse_variable(ei, var, lenvar);
370
371         if ((ei->mode&_MODE_UTF32)==0)
372                 ei->cur_max = 6; /* endian + surrogate */
373         else
374                 ei->cur_max = 8; /* endian + normal */
375
376         if (ei->preffered_endian == _ENDIAN_UNKNOWN) {
377 #if BYTE_ORDER == BIG_ENDIAN
378                 ei->preffered_endian = _ENDIAN_BIG;
379 #else
380                 ei->preffered_endian = _ENDIAN_LITTLE;
381 #endif
382         }
383
384         return (0);
385 }
386
387 static void
388 /*ARGSUSED*/
389 _citrus_UTF1632_encoding_module_uninit(_UTF1632EncodingInfo *ei)
390 {
391 }
392
393 static __inline int
394 /*ARGSUSED*/
395 _citrus_UTF1632_stdenc_wctocs(_UTF1632EncodingInfo * __restrict ei,
396                               _csid_t * __restrict csid,
397                               _index_t * __restrict idx,
398                               _wc_t wc)
399 {
400
401         _DIAGASSERT(csid != NULL && idx != NULL);
402
403         *csid = 0;
404         *idx = (_index_t)wc;
405
406         return (0);
407 }
408
409 static __inline int
410 /*ARGSUSED*/
411 _citrus_UTF1632_stdenc_cstowc(_UTF1632EncodingInfo * __restrict ei,
412                               _wc_t * __restrict wc,
413                               _csid_t csid, _index_t idx)
414 {
415
416         _DIAGASSERT(wc != NULL);
417
418         if (csid != 0)
419                 return (EILSEQ);
420
421         *wc = (_wc_t)idx;
422
423         return (0);
424 }
425
426
427 /* ----------------------------------------------------------------------
428  * public interface for stdenc
429  */
430
431 _CITRUS_STDENC_DECLS(UTF1632);
432 _CITRUS_STDENC_DEF_OPS(UTF1632);
433
434 #include "citrus_stdenc_template.h"