Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/* $NetBSD: citrus_gbk2k.c,v 1.9 2022/04/19 20:32:14 rillig Exp $ */

/*-
 * Copyright (c)2003 Citrus Project,
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
__RCSID("$NetBSD: citrus_gbk2k.c,v 1.9 2022/04/19 20:32:14 rillig Exp $");
#endif /* LIBC_SCCS and not lint */

#include <assert.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <wchar.h>
#include <sys/types.h>
#include <limits.h>

#include "citrus_namespace.h"
#include "citrus_types.h"
#include "citrus_bcs.h"
#include "citrus_module.h"
#include "citrus_ctype.h"
#include "citrus_stdenc.h"
#include "citrus_gbk2k.h"


/* ----------------------------------------------------------------------
 * private stuffs used by templates
 */

typedef struct _GBK2KState {
	char ch[4];
	int chlen;
} _GBK2KState;

typedef struct {
	int mb_cur_max;
} _GBK2KEncodingInfo;

typedef struct {
	_GBK2KEncodingInfo	ei;
	struct {
		/* for future multi-locale facility */
		_GBK2KState	s_mblen;
		_GBK2KState	s_mbrlen;
		_GBK2KState	s_mbrtowc;
		_GBK2KState	s_mbtowc;
		_GBK2KState	s_mbsrtowcs;
		_GBK2KState	s_mbsnrtowcs;
		_GBK2KState	s_wcrtomb;
		_GBK2KState	s_wcsrtombs;
		_GBK2KState	s_wcsnrtombs;
		_GBK2KState	s_wctomb;
	} states;
} _GBK2KCTypeInfo;

#define _CEI_TO_EI(_cei_)		(&(_cei_)->ei)
#define _CEI_TO_STATE(_cei_, _func_)	(_cei_)->states.s_##_func_

#define _FUNCNAME(m)			_citrus_GBK2K_##m
#define _ENCODING_INFO			_GBK2KEncodingInfo
#define _CTYPE_INFO			_GBK2KCTypeInfo
#define _ENCODING_STATE			_GBK2KState
#define _ENCODING_MB_CUR_MAX(_ei_)	(_ei_)->mb_cur_max
#define _ENCODING_IS_STATE_DEPENDENT	0
#define _STATE_NEEDS_EXPLICIT_INIT(_ps_)	0

static __inline void
/*ARGSUSED*/
_citrus_GBK2K_init_state(_GBK2KEncodingInfo * __restrict ei,
			 _GBK2KState * __restrict s)
{
	memset(s, 0, sizeof(*s));
}

static __inline void
/*ARGSUSED*/
_citrus_GBK2K_pack_state(_GBK2KEncodingInfo * __restrict ei,
			 void * __restrict pspriv,
			 const _GBK2KState * __restrict s)
{
	memcpy(pspriv, (const void *)s, sizeof(*s));
}

static __inline void
/*ARGSUSED*/
_citrus_GBK2K_unpack_state(_GBK2KEncodingInfo * __restrict ei,
			   _GBK2KState * __restrict s,
			   const void * __restrict pspriv)
{
	memcpy((void *)s, pspriv, sizeof(*s));
}

static  __inline int
_mb_singlebyte(int c)
{
	c &= 0xff;
	return (c <= 0x7f);
}

static __inline int
_mb_leadbyte(int c)
{
	c &= 0xff;
	return (0x81 <= c && c <= 0xfe);
}

static __inline int
_mb_trailbyte(int c)
{
	c &= 0xff;
	return ((0x40 <= c && c <= 0x7e) || (0x80 <= c && c <= 0xfe));
}

static __inline int
_mb_surrogate(int c)
{
	c &= 0xff;
	return (0x30 <= c && c <= 0x39);
}

static __inline int
_mb_count(wchar_t v)
{
	u_int32_t c;

	c = (u_int32_t)v; /* XXX */
	if (!(c & 0xffffff00))
		return (1);
	if (!(c & 0xffff0000))
		return (2);
	return (4);
}

#define	_PSENC		(psenc->ch[psenc->chlen - 1])
#define	_PUSH_PSENC(c)	(psenc->ch[psenc->chlen++] = (c))

static int
_citrus_GBK2K_mbrtowc_priv(_GBK2KEncodingInfo * __restrict ei,
			   wchar_t * __restrict pwc,
			   const char ** __restrict s, size_t n,
			   _GBK2KState * __restrict psenc,
			   size_t * __restrict nresult)
{
	int chlenbak, len;
	const char *s0, *s1;
	wchar_t wc;

	_DIAGASSERT(ei != NULL);
	/* pwc may be NULL */
	_DIAGASSERT(s != NULL);
	_DIAGASSERT(psenc != NULL);

	s0 = *s;

	if (s0 == NULL) {
		/* _citrus_GBK2K_init_state(ei, psenc); */
		psenc->chlen = 0;
		*nresult = 0;
		return (0);
	}

	chlenbak = psenc->chlen;

	switch (psenc->chlen) {
	case 3:
		if (!_mb_leadbyte (_PSENC))
			goto invalid;
	/* FALLTHROUGH */
	case 2:
		if (!_mb_surrogate(_PSENC) || _mb_trailbyte(_PSENC))
			goto invalid;
	/* FALLTHROUGH */
	case 1:
		if (!_mb_leadbyte (_PSENC))
			goto invalid;
	/* FALLTHOROUGH */
	case 0:
		break;
	default:
		goto invalid;
	}

	for (;;) {
		if (n-- < 1)
			goto restart;

		_PUSH_PSENC(*s0++);

		switch (psenc->chlen) {
		case 1:
			if (_mb_singlebyte(_PSENC))
				goto convert;
			if (_mb_leadbyte  (_PSENC))
				continue;
			goto ilseq;
		case 2:
			if (_mb_trailbyte (_PSENC))
				goto convert;
			if (ei->mb_cur_max == 4 &&
			    _mb_surrogate (_PSENC))
				continue;
			goto ilseq;
		case 3:
			if (_mb_leadbyte  (_PSENC))
				continue;
			goto ilseq;
		case 4:
			if (_mb_surrogate (_PSENC))
				goto convert;
			goto ilseq;
		}
	}

convert:
	len = psenc->chlen;
	s1  = &psenc->ch[0];
	wc  = 0;
	while (len-- > 0)
		wc = (wc << 8) | (*s1++ & 0xff);

	if (pwc != NULL)
		*pwc = wc;
	*s = s0;
	*nresult = (wc == 0) ? 0 : psenc->chlen - chlenbak; 
	/* _citrus_GBK2K_init_state(ei, psenc); */
	psenc->chlen = 0;

	return (0);

restart:
	*s = s0;
	*nresult = (size_t)-2;

	return (0);

invalid:
	return (EINVAL);

ilseq:
	*nresult = (size_t)-1;
	return (EILSEQ);
}

static int
_citrus_GBK2K_wcrtomb_priv(_GBK2KEncodingInfo * __restrict ei,
			   char * __restrict s, size_t n, wchar_t wc,
			   _GBK2KState * __restrict psenc,
			   size_t * __restrict nresult)
{
	int len, ret;

	_DIAGASSERT(ei != NULL);
	_DIAGASSERT(s != NULL);
	_DIAGASSERT(psenc != NULL);

	if (psenc->chlen != 0) {
		ret = EINVAL;
		goto err;
	}

	len = _mb_count(wc);
	if (n < len) {
		ret = E2BIG;
		goto err;
	}

	switch (len) {
	case 1:
		if (!_mb_singlebyte(_PUSH_PSENC(wc     ))) {
			ret = EILSEQ;
			goto err;
		}
		break;
	case 2:
		if (!_mb_leadbyte  (_PUSH_PSENC(wc >> 8)) ||
		    !_mb_trailbyte (_PUSH_PSENC(wc     ))) {
			ret = EILSEQ;
			goto err;
		}
		break;
	case 4:
		if (ei->mb_cur_max != 4 ||
		    !_mb_leadbyte  (_PUSH_PSENC(wc >> 24)) ||
		    !_mb_surrogate (_PUSH_PSENC(wc >> 16)) ||
		    !_mb_leadbyte  (_PUSH_PSENC(wc >>  8)) ||
		    !_mb_surrogate (_PUSH_PSENC(wc      ))) {
			ret = EILSEQ;
			goto err;
		}
		break;
	}

	_DIAGASSERT(len == psenc->chlen);

	memcpy(s, psenc->ch, psenc->chlen);
	*nresult = psenc->chlen;
	/* _citrus_GBK2K_init_state(ei, psenc); */
	psenc->chlen = 0;

	return (0);

err:
	*nresult = (size_t)-1;
	return ret;
}

static __inline int
/*ARGSUSED*/
_citrus_GBK2K_stdenc_wctocs(_GBK2KEncodingInfo * __restrict ei,
			    _csid_t * __restrict csid,
			    _index_t * __restrict idx, wchar_t wc)
{
	u_int8_t ch, cl;

	_DIAGASSERT(csid != NULL && idx != NULL);

	if ((u_int32_t)wc<0x80) {
		/* ISO646 */
		*csid = 0;
		*idx = (_index_t)wc;
	} else if ((u_int32_t)wc>=0x10000) {
		/* GBKUCS : XXX */
		*csid = 3;
		*idx = (_index_t)wc;
	} else {
		ch = (u_int8_t)(wc >> 8);
		cl = (u_int8_t)wc;
		if (ch>=0xA1 && cl>=0xA1) {
			/* EUC G1 */
			*csid = 1;
			*idx = (_index_t)wc & 0x7F7FU;
		} else {
			/* extended area (0x8140-) */
			*csid = 2;
			*idx = (_index_t)wc;
		}
	}
		
	return 0;
}

static __inline int
/*ARGSUSED*/
_citrus_GBK2K_stdenc_cstowc(_GBK2KEncodingInfo * __restrict ei,
			    wchar_t * __restrict wc,
			    _csid_t csid, _index_t idx)
{

	_DIAGASSERT(wc != NULL);

	switch (csid) {
	case 0:
		/* ISO646 */
		*wc = (wchar_t)idx;
		break;
	case 1:
		/* EUC G1 */
		*wc = (wchar_t)idx | 0x8080U;
		break;
	case 2:
		/* extended area */
		*wc = (wchar_t)idx;
		break;
	case 3:
		/* GBKUCS : XXX */
		if (ei->mb_cur_max != 4)
			return EINVAL;
		*wc = (wchar_t)idx;
		break;
	default:
		return EILSEQ;
	}

	return 0;
}

static __inline int
/*ARGSUSED*/
_citrus_GBK2K_stdenc_get_state_desc_generic(_GBK2KEncodingInfo * __restrict ei,
					    _GBK2KState * __restrict psenc,
					    int * __restrict rstate)
{

	if (psenc->chlen == 0)
		*rstate = _STDENC_SDGEN_INITIAL;
	else
		*rstate = _STDENC_SDGEN_INCOMPLETE_CHAR;

	return 0;
}

static int
/*ARGSUSED*/
_citrus_GBK2K_encoding_module_init(_GBK2KEncodingInfo * __restrict ei,
				   const void * __restrict var, size_t lenvar)
{
	const char *p;

	_DIAGASSERT(ei != NULL);

	p = var;
#define MATCH(x, act)                                           \
do {                                                            \
        if (lenvar >= (sizeof(#x)-1) &&                         \
            _bcs_strncasecmp(p, #x, sizeof(#x)-1) == 0) {       \
                act;                                            \
                lenvar -= sizeof(#x)-1;                         \
                p += sizeof(#x)-1;                              \
        }                                                       \
} while (0)
	memset((void *)ei, 0, sizeof(*ei));
	ei->mb_cur_max = 4;
	while (lenvar>0) {
		switch (_bcs_tolower(*p)) {
		case '2':
			MATCH("2byte", ei->mb_cur_max = 2);
			break;
		}
		p++;
		lenvar--;
	}

	return (0);
}

static void
/*ARGSUSED*/
_citrus_GBK2K_encoding_module_uninit(_GBK2KEncodingInfo *ei)
{
}


/* ----------------------------------------------------------------------
 * public interface for ctype
 */

_CITRUS_CTYPE_DECLS(GBK2K);
_CITRUS_CTYPE_DEF_OPS(GBK2K);

#include "citrus_ctype_template.h"

/* ----------------------------------------------------------------------
 * public interface for stdenc
 */

_CITRUS_STDENC_DECLS(GBK2K);
_CITRUS_STDENC_DEF_OPS(GBK2K);

#include "citrus_stdenc_template.h"