1 /*-
2  * Copyright (c) 1999,2000
3  *    Konstantin Chuguev.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  *    iconv (Charset Conversion Library) v2.0
27  */
28 #include <sys/types.h>
29 #include <assert.h>
30 #include <errno.h>
31 #include <stdlib.h>
32 #include <reent.h>
33 #include "local.h"
34 
35 static int
36 _DEFUN(table_init, (rptr, data, name, desc_data),
37                    struct _reent *rptr _AND
38                    _VOID_PTR *data     _AND
39                    _CONST char *name   _AND
40                    _CONST _VOID_PTR desc_data)
41 {
42     int res;
43     struct iconv_ccs *ccs = _malloc_r(rptr, sizeof(struct iconv_ccs));
44     if (ccs == NULL)
45         return __errno_r(rptr);
46     res = _iconv_ccs_init(rptr, ccs, name);
47     if (res)
48         _free_r(rptr, ccs);
49     else
50         (struct iconv_ccs *)(*data) = ccs;
51     return res;
52 }
53 
54 static int
55 _DEFUN(table_close, (rptr, data),
56                     struct _reent *rptr _AND
57                     _VOID_PTR data)
58 {
59     int res = 0;
60 
61     if (data != NULL)
62         res = ((struct iconv_ccs *)data)->close(rptr, (struct iconv_ccs *)data);
63     _free_r(rptr, data);
64     return res;
65 }
66 
67 static ssize_t
68 _DEFUN(convert_from_ucs, (ces, in, outbuf, outbytesleft),
69                          struct iconv_ces *ces  _AND
70                          ucs_t in               _AND
71                          unsigned char **outbuf _AND
72                          size_t *outbytesleft)
73 {
74     ucs_t res;
75     size_t bytes;
76 
77     if (in == UCS_CHAR_NONE)
78         return 1;    /* No state reinitialization for table charsets */
79     if (iconv_char32bit(in))
80         return -1;
81     res = ICONV_CCS_CONVERT_FROM_UCS((struct iconv_ccs *)(ces->data), in);
82     if (res == UCS_CHAR_INVALID)
83         return -1;    /* No character in output charset */
84     bytes = res & 0xFF00 ? 2 : 1;
85     if (*outbytesleft < bytes)
86         return 0;    /* No space in output buffer */
87     if (bytes == 2)
88         *(*outbuf)++ = (res >> 8) & 0xFF;
89     *(*outbuf)++ = res & 0xFF;
90     *outbytesleft -= bytes;
91     return 1;
92 }
93 
94 static ucs_t
95 _DEFUN(convert_to_ucs, (ces, inbuf, inbytesleft),
96                        struct iconv_ces *ces        _AND
97                        _CONST unsigned char **inbuf _AND
98                        size_t *inbytesleft)
99 {
100     struct iconv_ccs *ccsd = (struct iconv_ccs *)(ces->data);
101     unsigned char byte = *(*inbuf);
102     ucs_t res = ICONV_CCS_CONVERT_TO_UCS(ccsd, byte);
103     size_t bytes = (res == UCS_CHAR_INVALID && ccsd->nbits > 8) ? 2 : 1;
104 
105     if (*inbytesleft < bytes)
106         return UCS_CHAR_NONE;    /* Not enough bytes in the input buffer */
107     if (bytes == 2)
108         res = ICONV_CCS_CONVERT_TO_UCS(ccsd, (byte << 8) | (* ++(*inbuf)));
109     (*inbuf) ++;
110     *inbytesleft -= bytes;
111     return res;
112 }
113 
114 _CONST struct iconv_ces_desc _iconv_ces_table_driven = {
115         table_init,
116         table_close,
117         _iconv_ces_reset_null,
118         convert_from_ucs,
119         convert_to_ucs,
120         NULL
121 };
122 
123