php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
mbfilter_ucs4.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 file 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_ucs4.h"
32
33static size_t mb_ucs4_to_wchar(unsigned char **in, size_t *in_len, uint32_t *buf, size_t bufsize, unsigned int *state);
34static size_t mb_ucs4be_to_wchar(unsigned char **in, size_t *in_len, uint32_t *buf, size_t bufsize, unsigned int *state);
35static void mb_wchar_to_ucs4be(uint32_t *in, size_t len, mb_convert_buf *buf, bool end);
36static size_t mb_ucs4le_to_wchar(unsigned char **in, size_t *in_len, uint32_t *buf, size_t bufsize, unsigned int *state);
37static void mb_wchar_to_ucs4le(uint32_t *in, size_t len, mb_convert_buf *buf, bool end);
38
39static const char *mbfl_encoding_ucs4_aliases[] = {"ISO-10646-UCS-4", "UCS4", NULL};
40
41/* This library historically had encodings called 'byte4be' and 'byte4le'
42 * which were almost identical to UCS-4
43 * Maintain minimal support by aliasing to UCS-4 */
44static const char *mbfl_encoding_ucs4be_aliases[] = {"byte4be", NULL};
45static const char *mbfl_encoding_ucs4le_aliases[] = {"byte4le", NULL};
46
47static int mbfl_filt_conv_ucs4_wchar_flush(mbfl_convert_filter *filter);
48
51 "UCS-4",
52 "UCS-4",
53 mbfl_encoding_ucs4_aliases,
54 NULL,
58 mb_ucs4_to_wchar,
59 mb_wchar_to_ucs4be,
60 NULL,
61 NULL,
62};
63
66 "UCS-4BE",
67 "UCS-4BE",
68 mbfl_encoding_ucs4be_aliases,
69 NULL,
73 mb_ucs4be_to_wchar,
74 mb_wchar_to_ucs4be,
75 NULL,
76 NULL,
77};
78
81 "UCS-4LE",
82 "UCS-4LE",
83 mbfl_encoding_ucs4le_aliases,
84 NULL,
88 mb_ucs4le_to_wchar,
89 mb_wchar_to_ucs4le,
90 NULL,
91 NULL,
92};
93
98 NULL,
100 mbfl_filt_conv_ucs4_wchar_flush,
101 NULL,
102};
103
113
118 NULL,
120 mbfl_filt_conv_ucs4_wchar_flush,
121 NULL,
122};
123
133
138 NULL,
140 mbfl_filt_conv_ucs4_wchar_flush,
141 NULL,
142};
143
153
154
155#define CK(statement) do { if ((statement) < 0) return (-1); } while (0)
156
157/*
158 * UCS-4 => wchar
159 */
161{
162 int n, endian;
163
164 endian = filter->status & 0xff00;
165 switch (filter->status & 0xff) {
166 case 0:
167 if (endian) {
168 n = c & 0xff;
169 } else {
170 n = (c & 0xffu) << 24;
171 }
172 filter->cache = n;
173 filter->status++;
174 break;
175 case 1:
176 if (endian) {
177 n = (c & 0xff) << 8;
178 } else {
179 n = (c & 0xff) << 16;
180 }
181 filter->cache |= n;
182 filter->status++;
183 break;
184 case 2:
185 if (endian) {
186 n = (c & 0xff) << 16;
187 } else {
188 n = (c & 0xff) << 8;
189 }
190 filter->cache |= n;
191 filter->status++;
192 break;
193 default:
194 if (endian) {
195 n = (c & 0xffu) << 24;
196 } else {
197 n = c & 0xff;
198 }
199 n |= filter->cache;
200 filter->status &= ~0xff;
201 if ((n & 0xffff) == 0 && ((n >> 16) & 0xffff) == 0xfffe) {
202 if (endian) {
203 filter->status = 0; /* big-endian */
204 } else {
205 filter->status = 0x100; /* little-endian */
206 }
207 } else if (n != 0xfeff) {
208 CK((*filter->output_function)(n, filter->data));
209 }
210 break;
211 }
212
213 return 0;
214}
215
216/*
217 * UCS-4BE => wchar
218 */
220{
221 int n;
222
223 if (filter->status == 0) {
224 filter->status = 1;
225 n = (c & 0xffu) << 24;
226 filter->cache = n;
227 } else if (filter->status == 1) {
228 filter->status = 2;
229 n = (c & 0xff) << 16;
230 filter->cache |= n;
231 } else if (filter->status == 2) {
232 filter->status = 3;
233 n = (c & 0xff) << 8;
234 filter->cache |= n;
235 } else {
236 filter->status = 0;
237 n = (c & 0xff) | filter->cache;
238 CK((*filter->output_function)(n, filter->data));
239 }
240 return 0;
241}
242
243/*
244 * wchar => UCS-4BE
245 */
247{
248 if (c != MBFL_BAD_INPUT) {
249 CK((*filter->output_function)((c >> 24) & 0xff, filter->data));
250 CK((*filter->output_function)((c >> 16) & 0xff, filter->data));
251 CK((*filter->output_function)((c >> 8) & 0xff, filter->data));
252 CK((*filter->output_function)(c & 0xff, filter->data));
253 } else {
255 }
256
257 return 0;
258}
259
260/*
261 * UCS-4LE => wchar
262 */
264{
265 int n;
266
267 if (filter->status == 0) {
268 filter->status = 1;
269 n = (c & 0xff);
270 filter->cache = n;
271 } else if (filter->status == 1) {
272 filter->status = 2;
273 n = (c & 0xff) << 8;
274 filter->cache |= n;
275 } else if (filter->status == 2) {
276 filter->status = 3;
277 n = (c & 0xff) << 16;
278 filter->cache |= n;
279 } else {
280 filter->status = 0;
281 n = ((c & 0xffu) << 24) | filter->cache;
282 CK((*filter->output_function)(n, filter->data));
283 }
284 return 0;
285}
286
287/*
288 * wchar => UCS-4LE
289 */
291{
292 if (c != MBFL_BAD_INPUT) {
293 CK((*filter->output_function)(c & 0xff, filter->data));
294 CK((*filter->output_function)((c >> 8) & 0xff, filter->data));
295 CK((*filter->output_function)((c >> 16) & 0xff, filter->data));
296 CK((*filter->output_function)((c >> 24) & 0xff, filter->data));
297 } else {
299 }
300
301 return 0;
302}
303
304static int mbfl_filt_conv_ucs4_wchar_flush(mbfl_convert_filter *filter)
305{
306 if (filter->status & 0xF) {
307 /* Input string was truncated */
308 CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
309 }
310 filter->status = 0;
311
312 if (filter->flush_function) {
313 (*filter->flush_function)(filter->data);
314 }
315
316 return 0;
317}
318
319#define DETECTED_BE 1
320#define DETECTED_LE 2
321
322static size_t mb_ucs4_to_wchar(unsigned char **in, size_t *in_len, uint32_t *buf, size_t bufsize, unsigned int *state)
323{
324 if (*state == DETECTED_BE) {
325 return mb_ucs4be_to_wchar(in, in_len, buf, bufsize, NULL);
326 } else if (*state == DETECTED_LE) {
327 return mb_ucs4le_to_wchar(in, in_len, buf, bufsize, NULL);
328 } else if (*in_len >= 4) {
329 unsigned char *p = *in;
330 uint32_t c1 = *p++;
331 uint32_t c2 = *p++;
332 uint32_t c3 = *p++;
333 uint32_t c4 = *p++;
334 uint32_t w = (c1 << 24) | (c2 << 16) | (c3 << 8) | c4;
335
336 if (w == 0xFFFE0000) {
337 /* Little-endian BOM */
338 *in = p;
339 *in_len -= 4;
341 return mb_ucs4le_to_wchar(in, in_len, buf, bufsize, NULL);
342 } else if (w == 0xFEFF) {
343 /* Big-endian BOM; don't send it to output */
344 *in = p;
345 *in_len -= 4;
346 }
347 }
348
350 return mb_ucs4be_to_wchar(in, in_len, buf, bufsize, NULL);
351}
352
353static size_t mb_ucs4be_to_wchar(unsigned char **in, size_t *in_len, uint32_t *buf, size_t bufsize, unsigned int *state)
354{
355 unsigned char *p = *in, *e = p + (*in_len & ~3);
356 uint32_t *out = buf, *limit = buf + bufsize;
357
358 while (p < e && out < limit) {
359 uint32_t c1 = *p++;
360 uint32_t c2 = *p++;
361 uint32_t c3 = *p++;
362 uint32_t c4 = *p++;
363 uint32_t w = (c1 << 24) | (c2 << 16) | (c3 << 8) | c4;
364 *out++ = w;
365 }
366
367 if (p == e && (*in_len & 0x3) && out < limit) {
368 /* There are 1-3 trailing bytes, which shouldn't be there */
369 *out++ = MBFL_BAD_INPUT;
370 p = *in + *in_len;
371 }
372
373 *in_len -= (p - *in);
374 *in = p;
375 return out - buf;
376}
377
378static void mb_wchar_to_ucs4be(uint32_t *in, size_t len, mb_convert_buf *buf, bool end)
379{
380 unsigned char *out, *limit;
381 MB_CONVERT_BUF_LOAD(buf, out, limit);
382 MB_CONVERT_BUF_ENSURE(buf, out, limit, len * 4);
383
384 while (len--) {
385 uint32_t w = *in++;
386 if (w != MBFL_BAD_INPUT) {
387 out = mb_convert_buf_add4(out, (w >> 24) & 0xFF, (w >> 16) & 0xFF, (w >> 8) & 0xFF, w & 0xFF);
388 } else {
389 MB_CONVERT_ERROR(buf, out, limit, w, mb_wchar_to_ucs4be);
390 MB_CONVERT_BUF_ENSURE(buf, out, limit, len * 4);
391 }
392 }
393
395}
396
397static size_t mb_ucs4le_to_wchar(unsigned char **in, size_t *in_len, uint32_t *buf, size_t bufsize, unsigned int *state)
398{
399 unsigned char *p = *in, *e = p + (*in_len & ~3);
400 uint32_t *out = buf, *limit = buf + bufsize;
401
402 while (p < e && out < limit) {
403 uint32_t c1 = *p++;
404 uint32_t c2 = *p++;
405 uint32_t c3 = *p++;
406 uint32_t c4 = *p++;
407 uint32_t w = (c4 << 24) | (c3 << 16) | (c2 << 8) | c1;
408 *out++ = w;
409 }
410
411 if (p == e && (*in_len & 0x3) && out < limit) {
412 /* There are 1-3 trailing bytes, which shouldn't be there */
413 *out++ = MBFL_BAD_INPUT;
414 p = *in + *in_len;
415 }
416
417 *in_len -= (p - *in);
418 *in = p;
419 return out - buf;
420}
421
422static void mb_wchar_to_ucs4le(uint32_t *in, size_t len, mb_convert_buf *buf, bool end)
423{
424 unsigned char *out, *limit;
425 MB_CONVERT_BUF_LOAD(buf, out, limit);
426 MB_CONVERT_BUF_ENSURE(buf, out, limit, len * 4);
427
428 while (len--) {
429 uint32_t w = *in++;
430 if (w != MBFL_BAD_INPUT) {
431 out = mb_convert_buf_add4(out, w & 0xFF, (w >> 8) & 0xFF, (w >> 16) & 0xFF, (w >> 24) & 0xFF);
432 } else {
433 MB_CONVERT_ERROR(buf, out, limit, w, mb_wchar_to_ucs4le);
434 MB_CONVERT_BUF_ENSURE(buf, out, limit, len * 4);
435 }
436 }
437
439}
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
#define DETECTED_LE
int mbfl_filt_conv_ucs4le_wchar(int c, mbfl_convert_filter *filter)
const struct mbfl_convert_vtbl vtbl_wchar_ucs4be
#define CK(statement)
int mbfl_filt_conv_wchar_ucs4le(int c, mbfl_convert_filter *filter)
const struct mbfl_convert_vtbl vtbl_ucs4_wchar
const struct mbfl_convert_vtbl vtbl_wchar_ucs4le
const struct mbfl_convert_vtbl vtbl_ucs4le_wchar
int mbfl_filt_conv_ucs4_wchar(int c, mbfl_convert_filter *filter)
const mbfl_encoding mbfl_encoding_ucs4be
int mbfl_filt_conv_wchar_ucs4be(int c, mbfl_convert_filter *filter)
const struct mbfl_convert_vtbl vtbl_wchar_ucs4
int mbfl_filt_conv_ucs4be_wchar(int c, mbfl_convert_filter *filter)
const mbfl_encoding mbfl_encoding_ucs4
const mbfl_encoding mbfl_encoding_ucs4le
const struct mbfl_convert_vtbl vtbl_ucs4be_wchar
#define MBFL_ENCTYPE_WCS4
Definition mbfl_consts.h:36
#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_ucs4le
@ mbfl_no_encoding_wchar
@ mbfl_no_encoding_ucs4
@ mbfl_no_encoding_ucs4be
#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
flush_function_t flush_function
out($f, $s)