/src/dcmtk/oficonv/libsrc/citrus_none.c
Line | Count | Source |
1 | | /*- |
2 | | * Copyright (c) 2002 Citrus Project, |
3 | | * Copyright (c) 2010 Gabor Kovesdan <gabor@FreeBSD.org>, |
4 | | * All rights reserved. |
5 | | * |
6 | | * Redistribution and use in source and binary forms, with or without |
7 | | * modification, are permitted provided that the following conditions |
8 | | * are met: |
9 | | * 1. Redistributions of source code must retain the above copyright |
10 | | * notice, this list of conditions and the following disclaimer. |
11 | | * 2. Redistributions in binary form must reproduce the above copyright |
12 | | * notice, this list of conditions and the following disclaimer in the |
13 | | * documentation and/or other materials provided with the distribution. |
14 | | * |
15 | | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
16 | | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 | | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
19 | | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
20 | | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
21 | | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
22 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
23 | | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
24 | | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
25 | | * SUCH DAMAGE. |
26 | | */ |
27 | | |
28 | | #include "dcmtk/config/osconfig.h" |
29 | | #include "citrus_none.h" |
30 | | |
31 | | #include "dcmtk/oficonv/iconv.h" |
32 | | |
33 | | #include <sys/types.h> |
34 | | #include <errno.h> |
35 | | #include <stddef.h> |
36 | | #include <stdio.h> |
37 | | #include <stdlib.h> |
38 | | #include <string.h> |
39 | | #include <wchar.h> |
40 | | |
41 | | #include "citrus_bcs.h" |
42 | | #include "citrus_types.h" |
43 | | #include "citrus_module.h" |
44 | | #include "citrus_stdenc.h" |
45 | | |
46 | | _CITRUS_STDENC_DECLS(NONE); |
47 | | _CITRUS_STDENC_DEF_OPS(NONE); |
48 | | struct _citrus_stdenc_traits _citrus_NONE_stdenc_traits = { |
49 | | 0, /* et_state_size */ |
50 | | 1, /* mb_cur_max */ |
51 | | }; |
52 | | |
53 | | static int |
54 | | _citrus_NONE_stdenc_init(struct _citrus_stdenc * ce, |
55 | | const void *var , size_t lenvar , |
56 | | struct _citrus_stdenc_traits * et) |
57 | 0 | { |
58 | 0 | (void) var; |
59 | 0 | (void) lenvar; |
60 | |
|
61 | 0 | et->et_state_size = 0; |
62 | 0 | et->et_mb_cur_max = 1; |
63 | |
|
64 | 0 | ce->ce_closure = NULL; |
65 | |
|
66 | 0 | return (0); |
67 | 0 | } |
68 | | |
69 | | static void |
70 | | _citrus_NONE_stdenc_uninit(struct _citrus_stdenc *ce ) |
71 | 0 | { |
72 | 0 | (void) ce; |
73 | 0 | } |
74 | | |
75 | | static int |
76 | | _citrus_NONE_stdenc_init_state(struct _citrus_stdenc * ce , |
77 | | void * ps ) |
78 | 0 | { |
79 | 0 | (void) ce; |
80 | 0 | (void) ps; |
81 | |
|
82 | 0 | return (0); |
83 | 0 | } |
84 | | |
85 | | static int |
86 | | _citrus_NONE_stdenc_mbtocs(struct _citrus_stdenc * ce , |
87 | | _citrus_csid_t *csid, _citrus_index_t *idx, char **s, size_t n, |
88 | | void *ps , size_t *nresult, struct iconv_hooks *hooks) |
89 | 0 | { |
90 | 0 | (void) ce; |
91 | 0 | (void) ps; |
92 | |
|
93 | 0 | if (n < 1) { |
94 | 0 | *nresult = (size_t)-2; |
95 | 0 | return (0); |
96 | 0 | } |
97 | | |
98 | 0 | *csid = 0; |
99 | 0 | *idx = (_citrus_index_t)(unsigned char)*(*s)++; |
100 | 0 | *nresult = *idx == 0 ? 0 : 1; |
101 | |
|
102 | 0 | if ((hooks != NULL) && (hooks->uc_hook != NULL)) |
103 | 0 | hooks->uc_hook((unsigned int)*idx, hooks->data); |
104 | |
|
105 | 0 | return (0); |
106 | 0 | } |
107 | | |
108 | | static int |
109 | | _citrus_NONE_stdenc_cstomb(struct _citrus_stdenc * ce , |
110 | | char *s, size_t n, _citrus_csid_t csid, _citrus_index_t idx, void *ps , |
111 | | size_t *nresult, struct iconv_hooks *hooks ) |
112 | 0 | { |
113 | 0 | (void) ce; |
114 | 0 | (void) ps; |
115 | 0 | (void) hooks; |
116 | |
|
117 | 0 | if (csid == _CITRUS_CSID_INVALID) { |
118 | 0 | *nresult = 0; |
119 | 0 | return (0); |
120 | 0 | } |
121 | 0 | if (csid != 0) |
122 | 0 | return (EILSEQ); |
123 | | |
124 | 0 | if ((idx & 0x000000FF) == idx) { |
125 | 0 | if (n < 1) { |
126 | 0 | *nresult = (size_t)-1; |
127 | 0 | return (E2BIG); |
128 | 0 | } |
129 | 0 | *s = (char)idx; |
130 | 0 | *nresult = 1; |
131 | 0 | } else if ((idx & 0x0000FFFF) == idx) { |
132 | 0 | if (n < 2) { |
133 | 0 | *nresult = (size_t)-1; |
134 | 0 | return (E2BIG); |
135 | 0 | } |
136 | 0 | s[0] = (char)idx; |
137 | | /* XXX: might be endian dependent */ |
138 | 0 | s[1] = (char)(idx >> 8); |
139 | 0 | *nresult = 2; |
140 | 0 | } else if ((idx & 0x00FFFFFF) == idx) { |
141 | 0 | if (n < 3) { |
142 | 0 | *nresult = (size_t)-1; |
143 | 0 | return (E2BIG); |
144 | 0 | } |
145 | 0 | s[0] = (char)idx; |
146 | | /* XXX: might be endian dependent */ |
147 | 0 | s[1] = (char)(idx >> 8); |
148 | 0 | s[2] = (char)(idx >> 16); |
149 | 0 | *nresult = 3; |
150 | 0 | } else { |
151 | 0 | if (n < 4) { |
152 | 0 | *nresult = (size_t)-1; |
153 | 0 | return (E2BIG); |
154 | 0 | } |
155 | 0 | s[0] = (char)idx; |
156 | | /* XXX: might be endian dependent */ |
157 | 0 | s[1] = (char)(idx >> 8); |
158 | 0 | s[2] = (char)(idx >> 16); |
159 | 0 | s[3] = (char)(idx >> 24); |
160 | 0 | *nresult = 4; |
161 | 0 | } |
162 | | |
163 | 0 | return (0); |
164 | 0 | } |
165 | | |
166 | | static int |
167 | | _citrus_NONE_stdenc_mbtowc(struct _citrus_stdenc * ce , |
168 | | _citrus_wc_t * pwc, char ** s, size_t n, |
169 | | void * pspriv , size_t * nresult, |
170 | | struct iconv_hooks *hooks) |
171 | 0 | { |
172 | 0 | (void) ce; |
173 | 0 | (void) pspriv; |
174 | |
|
175 | 0 | if (*s == NULL) { |
176 | 0 | *nresult = 0; |
177 | 0 | return (0); |
178 | 0 | } |
179 | 0 | if (n == 0) { |
180 | 0 | *nresult = (size_t)-2; |
181 | 0 | return (0); |
182 | 0 | } |
183 | | |
184 | 0 | if (pwc != NULL) |
185 | 0 | *pwc = (_citrus_wc_t)(unsigned char) **s; |
186 | |
|
187 | 0 | *nresult = **s == '\0' ? 0 : 1; |
188 | |
|
189 | 0 | if ((hooks != NULL) && (hooks->wc_hook != NULL)) |
190 | 0 | hooks->wc_hook(*pwc, hooks->data); |
191 | |
|
192 | 0 | return (0); |
193 | 0 | } |
194 | | |
195 | | static int |
196 | | _citrus_NONE_stdenc_wctomb(struct _citrus_stdenc * ce , |
197 | | char * s, size_t n, _citrus_wc_t wc, |
198 | | void * pspriv , size_t * nresult, |
199 | | struct iconv_hooks *hooks ) |
200 | 0 | { |
201 | 0 | (void) ce; |
202 | 0 | (void) pspriv; |
203 | 0 | (void) hooks; |
204 | |
|
205 | 0 | if ((wc & ~0xFFU) != 0) { |
206 | 0 | *nresult = (size_t)-1; |
207 | 0 | return (EILSEQ); |
208 | 0 | } |
209 | 0 | if (n == 0) { |
210 | 0 | *nresult = (size_t)-1; |
211 | 0 | return (E2BIG); |
212 | 0 | } |
213 | | |
214 | 0 | *nresult = 1; |
215 | 0 | if (s != NULL && n > 0) |
216 | 0 | *s = (char)wc; |
217 | |
|
218 | 0 | return (0); |
219 | 0 | } |
220 | | |
221 | | static int |
222 | | _citrus_NONE_stdenc_put_state_reset(struct _citrus_stdenc * ce , |
223 | | char * s , size_t n , |
224 | | void * pspriv , size_t * nresult) |
225 | 0 | { |
226 | 0 | (void) ce; |
227 | 0 | (void) s; |
228 | 0 | (void) n; |
229 | 0 | (void) pspriv; |
230 | |
|
231 | 0 | *nresult = 0; |
232 | |
|
233 | 0 | return (0); |
234 | 0 | } |
235 | | |
236 | | static int |
237 | | _citrus_NONE_stdenc_get_state_desc(struct _citrus_stdenc * ce , |
238 | | void * ps , int id, |
239 | | struct _citrus_stdenc_state_desc * d) |
240 | 0 | { |
241 | 0 | int ret = 0; |
242 | 0 | (void) ce; |
243 | 0 | (void) ps; |
244 | |
|
245 | 0 | switch (id) { |
246 | 0 | case _CITRUS_STDENC_SDID_GENERIC: |
247 | 0 | d->u.generic.state = _CITRUS_STDENC_SDGEN_INITIAL; |
248 | 0 | break; |
249 | 0 | default: |
250 | 0 | ret = EOPNOTSUPP; |
251 | 0 | } |
252 | | |
253 | 0 | return (ret); |
254 | 0 | } |