php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
mbfilter_ucs2.c
Go to the documentation of this file.
1/*
2 * "streamable kanji code filter and converter"
3 * Copyright (c) 1998-2002 HappySize, Inc. All rights reserved.
4 *
5 * LICENSE NOTICES
6 *
7 * This file is part of "streamable kanji code filter and converter",
8 * which is distributed under the terms of GNU Lesser General Public
9 * License (version 2) as published by the Free Software Foundation.
10 *
11 * This software is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with "streamable kanji code filter and converter";
18 * if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 * Suite 330, Boston, MA 02111-1307 USA
20 *
21 * The author of this file:
22 *
23 */
24/*
25 * The source code included in this files was separated from mbfilter.c
26 * by moriyoshi koizumi <moriyoshi@php.net> on 4 dec 2002.
27 *
28 */
29
30#include "mbfilter.h"
31#include "mbfilter_ucs2.h"
32
33static int mbfl_filt_conv_ucs2_wchar_flush(mbfl_convert_filter *filter);
34static size_t mb_ucs2_to_wchar(unsigned char **in, size_t *in_len, uint32_t *buf, size_t bufsize, unsigned int *state);
35static size_t mb_ucs2be_to_wchar(unsigned char **in, size_t *in_len, uint32_t *buf, size_t bufsize, unsigned int *state);
36static void mb_wchar_to_ucs2be(uint32_t *in, size_t len, mb_convert_buf *buf, bool end);
37static size_t mb_ucs2le_to_wchar(unsigned char **in, size_t *in_len, uint32_t *buf, size_t bufsize, unsigned int *state);
38static void mb_wchar_to_ucs2le(uint32_t *in, size_t len, mb_convert_buf *buf, bool end);
39
40static const char *mbfl_encoding_ucs2_aliases[] = {"ISO-10646-UCS-2", "UCS2" , "UNICODE", NULL};
41
42/* This library historically had encodings called 'byte2be' and 'byte2le'
43 * which were almost identical to UCS-2, except that they would truncate
44 * Unicode codepoints higher than 0xFFFF quietly
45 * Maintain minimal support by aliasing to UCS-2 */
46static const char *mbfl_encoding_ucs2be_aliases[] = {"byte2be", NULL};
47static const char *mbfl_encoding_ucs2le_aliases[] = {"byte2le", NULL};
48
51 "UCS-2",
52 "UCS-2",
53 mbfl_encoding_ucs2_aliases,
54 NULL,
58 mb_ucs2_to_wchar,
59 mb_wchar_to_ucs2be,
60 NULL,
61 NULL,
62};
63
66 "UCS-2BE",
67 "UCS-2BE",
68 mbfl_encoding_ucs2be_aliases,
69 NULL,
73 mb_ucs2be_to_wchar,
74 mb_wchar_to_ucs2be,
75 NULL,
76 NULL,
77};
78
81 "UCS-2LE",
82 "UCS-2LE",
83 mbfl_encoding_ucs2le_aliases,
84 NULL,
88 mb_ucs2le_to_wchar,
89 mb_wchar_to_ucs2le,
90 NULL,
91 NULL,
92};
93
98 NULL,
100 mbfl_filt_conv_ucs2_wchar_flush,
101 NULL,
102};
103
113
118 NULL,
120 mbfl_filt_conv_ucs2_wchar_flush,
121 NULL,
122};
123
133
138 NULL,
140 mbfl_filt_conv_ucs2_wchar_flush,
141 NULL,
142};
143
153
154#define CK(statement) do { if ((statement) < 0) return (-1); } while (0)
155
157{
158 if (filter->status == 0) {
159 filter->status = 1;
160 filter->cache = c & 0xFF;
161 } else {
162 filter->status = 0;
163 int n = (filter->cache << 8) | (c & 0xFF);
164 if (n == 0xFFFE) {
165 /* Found little-endian byte order mark */
167 } else {
169 if (n != 0xFEFF) {
170 CK((*filter->output_function)(n, filter->data));
171 }
172 }
173 }
174 return 0;
175}
176
178{
179 if (filter->status == 0) {
180 filter->status = 1;
181 filter->cache = (c & 0xFF) << 8;
182 } else {
183 filter->status = 0;
184 CK((*filter->output_function)((c & 0xFF) | filter->cache, filter->data));
185 }
186 return 0;
187}
188
190{
191 if (c >= 0 && c < MBFL_WCSPLANE_UCS2MAX) {
192 CK((*filter->output_function)((c >> 8) & 0xFF, filter->data));
193 CK((*filter->output_function)(c & 0xFF, filter->data));
194 } else {
196 }
197 return 0;
198}
199
201{
202 if (filter->status == 0) {
203 filter->status = 1;
204 filter->cache = c & 0xFF;
205 } else {
206 filter->status = 0;
207 CK((*filter->output_function)(((c & 0xFF) << 8) | filter->cache, filter->data));
208 }
209 return 0;
210}
211
213{
214 if (c >= 0 && c < MBFL_WCSPLANE_UCS2MAX) {
215 CK((*filter->output_function)(c & 0xFF, filter->data));
216 CK((*filter->output_function)((c >> 8) & 0xFF, filter->data));
217 } else {
219 }
220 return 0;
221}
222
223static int mbfl_filt_conv_ucs2_wchar_flush(mbfl_convert_filter *filter)
224{
225 if (filter->status) {
226 /* Input string was truncated */
227 filter->status = 0;
228 CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
229 }
230
231 if (filter->flush_function) {
232 (*filter->flush_function)(filter->data);
233 }
234
235 return 0;
236}
237
238#define DETECTED_BE 1
239#define DETECTED_LE 2
240
241static size_t mb_ucs2_to_wchar(unsigned char **in, size_t *in_len, uint32_t *buf, size_t bufsize, unsigned int *state)
242{
243 if (*state == DETECTED_BE) {
244 return mb_ucs2be_to_wchar(in, in_len, buf, bufsize, NULL);
245 } else if (*state == DETECTED_LE) {
246 return mb_ucs2le_to_wchar(in, in_len, buf, bufsize, NULL);
247 } else if (*in_len >= 2) {
248 unsigned char *p = *in;
249 unsigned char c1 = *p++;
250 unsigned char c2 = *p++;
251 uint32_t w = (c1 << 8) | c2;
252
253 if (w == 0xFFFE) {
254 /* Little-endian BOM */
255 *in = p;
256 *in_len -= 2;
258 return mb_ucs2le_to_wchar(in, in_len, buf, bufsize, NULL);
259 } else if (w == 0xFEFF) {
260 /* Big-endian BOM; don't send it to output */
261 *in = p;
262 *in_len -= 2;
263 }
264 }
265
267 return mb_ucs2be_to_wchar(in, in_len, buf, bufsize, NULL);
268}
269
270static size_t mb_ucs2be_to_wchar(unsigned char **in, size_t *in_len, uint32_t *buf, size_t bufsize, unsigned int *state)
271{
272 unsigned char *p = *in, *e = p + (*in_len & ~1);
273 uint32_t *out = buf, *limit = buf + bufsize;
274
275 while (p < e && out < limit) {
276 unsigned char c1 = *p++;
277 unsigned char c2 = *p++;
278 uint32_t w = (c1 << 8) | c2;
279 *out++ = w;
280 }
281
282 if (p == e && (*in_len & 0x1) && out < limit) {
283 /* There is 1 trailing byte, which shouldn't be there */
284 *out++ = MBFL_BAD_INPUT;
285 p++;
286 }
287
288 *in_len -= (p - *in);
289 *in = p;
290 return out - buf;
291}
292
293static void mb_wchar_to_ucs2be(uint32_t *in, size_t len, mb_convert_buf *buf, bool end)
294{
295 unsigned char *out, *limit;
296 MB_CONVERT_BUF_LOAD(buf, out, limit);
297 MB_CONVERT_BUF_ENSURE(buf, out, limit, len * 2);
298
299 while (len--) {
300 uint32_t w = *in++;
301 if (w < MBFL_WCSPLANE_UCS2MAX) {
302 out = mb_convert_buf_add2(out, (w >> 8) & 0xFF, w & 0xFF);
303 } else {
304 MB_CONVERT_ERROR(buf, out, limit, w, mb_wchar_to_ucs2be);
305 MB_CONVERT_BUF_ENSURE(buf, out, limit, len * 2);
306 }
307 }
308
310}
311
312static size_t mb_ucs2le_to_wchar(unsigned char **in, size_t *in_len, uint32_t *buf, size_t bufsize, unsigned int *state)
313{
314 unsigned char *p = *in, *e = p + (*in_len & ~1);
315 uint32_t *out = buf, *limit = buf + bufsize;
316
317 while (p < e && out < limit) {
318 unsigned char c1 = *p++;
319 unsigned char c2 = *p++;
320 uint32_t w = (c2 << 8) | c1;
321 *out++ = w;
322 }
323
324 if (p == e && (*in_len & 0x1) && out < limit) {
325 /* There is 1 trailing byte, which shouldn't be there */
326 *out++ = MBFL_BAD_INPUT;
327 p++;
328 }
329
330 *in_len -= (p - *in);
331 *in = p;
332 return out - buf;
333}
334
335static void mb_wchar_to_ucs2le(uint32_t *in, size_t len, mb_convert_buf *buf, bool end)
336{
337 unsigned char *out, *limit;
338 MB_CONVERT_BUF_LOAD(buf, out, limit);
339 MB_CONVERT_BUF_ENSURE(buf, out, limit, len * 2);
340
341 while (len--) {
342 uint32_t w = *in++;
343 if (w < MBFL_WCSPLANE_UCS2MAX) {
344 out = mb_convert_buf_add2(out, w & 0xFF, (w >> 8) & 0xFF);
345 } else {
346 MB_CONVERT_ERROR(buf, out, limit, w, mb_wchar_to_ucs2le);
347 MB_CONVERT_BUF_ENSURE(buf, out, limit, len * 2);
348 }
349 }
350
352}
size_t len
Definition apprentice.c:174
zend_long n
Definition ffi.c:4979
zend_ffi_ctype_name_buf buf
Definition ffi.c:4685
#define NULL
Definition gdcache.h:45
#define DETECTED_BE
const mbfl_encoding mbfl_encoding_ucs2
const mbfl_encoding mbfl_encoding_ucs2be
const struct mbfl_convert_vtbl vtbl_ucs2_wchar
#define CK(statement)
int mbfl_filt_conv_wchar_ucs2be(int c, mbfl_convert_filter *filter)
const struct mbfl_convert_vtbl vtbl_wchar_ucs2le
int mbfl_filt_conv_wchar_ucs2le(int c, mbfl_convert_filter *filter)
const mbfl_encoding mbfl_encoding_ucs2le
int mbfl_filt_conv_ucs2be_wchar(int c, mbfl_convert_filter *filter)
int mbfl_filt_conv_ucs2le_wchar(int c, mbfl_convert_filter *filter)
const struct mbfl_convert_vtbl vtbl_ucs2be_wchar
#define DETECTED_LE
int mbfl_filt_conv_ucs2_wchar(int c, mbfl_convert_filter *filter)
const struct mbfl_convert_vtbl vtbl_ucs2le_wchar
const struct mbfl_convert_vtbl vtbl_wchar_ucs2
const struct mbfl_convert_vtbl vtbl_wchar_ucs2be
#define MBFL_WCSPLANE_UCS2MAX
Definition mbfl_consts.h:39
#define MBFL_ENCTYPE_WCS2
Definition mbfl_consts.h:35
#define MBFL_BAD_INPUT
Definition mbfl_consts.h:45
int mbfl_filt_conv_common_flush(mbfl_convert_filter *filter)
int mbfl_filt_conv_illegal_output(int c, mbfl_convert_filter *filter)
void mbfl_filt_conv_common_ctor(mbfl_convert_filter *filter)
struct _mbfl_convert_filter mbfl_convert_filter
@ mbfl_no_encoding_ucs2le
@ mbfl_no_encoding_ucs2
@ mbfl_no_encoding_wchar
@ mbfl_no_encoding_ucs2be
#define MB_CONVERT_BUF_STORE(buf, _out, _limit)
#define MB_CONVERT_BUF_ENSURE(buf, out, limit, needed)
#define MB_CONVERT_ERROR(buf, out, limit, bad_cp, conv_fn)
#define MB_CONVERT_BUF_LOAD(buf, _out, _limit)
unsigned const char * end
Definition php_ffi.h:51
p
Definition session.c:1105
output_function_t output_function
int(* filter_function)(int c, mbfl_convert_filter *filter)
flush_function_t flush_function
out($f, $s)