Deleted Added
full compact
gb18030.c (127834) gb18030.c (128004)
1/*-
2 * Copyright (c) 2002-2004 Tim J. Robbins
3 * 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

--- 15 unchanged lines hidden (view full) ---

24 * SUCH DAMAGE.
25 */
26/*
27 * PRC National Standard GB 18030-2000 encoding of Chinese text.
28 *
29 * See gb18030(5) for details.
30 */
31
1/*-
2 * Copyright (c) 2002-2004 Tim J. Robbins
3 * 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

--- 15 unchanged lines hidden (view full) ---

24 * SUCH DAMAGE.
25 */
26/*
27 * PRC National Standard GB 18030-2000 encoding of Chinese text.
28 *
29 * See gb18030(5) for details.
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD: head/lib/libc/locale/gb18030.c 127834 2004-04-04 11:00:42Z tjr $");
32#include <sys/param.h>
33__FBSDID("$FreeBSD: head/lib/libc/locale/gb18030.c 128004 2004-04-07 10:48:19Z tjr $");
34
35#include <errno.h>
36#include <runetype.h>
37#include <stdlib.h>
34
35#include <errno.h>
36#include <runetype.h>
37#include <stdlib.h>
38#include <string.h>
38#include <wchar.h>
39
40extern size_t (*__mbrtowc)(wchar_t * __restrict, const char * __restrict,
41 size_t, mbstate_t * __restrict);
39#include <wchar.h>
40
41extern size_t (*__mbrtowc)(wchar_t * __restrict, const char * __restrict,
42 size_t, mbstate_t * __restrict);
43extern int (*__mbsinit)(const mbstate_t *);
42extern size_t (*__wcrtomb)(char * __restrict, wchar_t, mbstate_t * __restrict);
43
44int _GB18030_init(_RuneLocale *);
44extern size_t (*__wcrtomb)(char * __restrict, wchar_t, mbstate_t * __restrict);
45
46int _GB18030_init(_RuneLocale *);
45size_t _GB18030_mbrtowc(wchar_t * __restrict, const char * __restrict, size_t,
47size_t _GB18030_mbrtowc(wchar_t * __restrict, const char * __restrict, size_t,
46 mbstate_t * __restrict);
48 mbstate_t * __restrict);
47size_t _GB18030_wcrtomb(char * __restrict, wchar_t, mbstate_t * __restrict);
49int _GB18030_mbsinit(const mbstate_t *);
50size_t _GB18030_wcrtomb(char * __restrict, wchar_t, mbstate_t * __restrict);
48
51
52typedef struct {
53 int count;
54 u_char bytes[4];
55} _GB18030State;
56
49int
50_GB18030_init(_RuneLocale *rl)
51{
52
53 __mbrtowc = _GB18030_mbrtowc;
54 __wcrtomb = _GB18030_wcrtomb;
57int
58_GB18030_init(_RuneLocale *rl)
59{
60
61 __mbrtowc = _GB18030_mbrtowc;
62 __wcrtomb = _GB18030_wcrtomb;
63 __mbsinit = _GB18030_mbsinit;
55 _CurrentRuneLocale = rl;
56 __mb_cur_max = 4;
57
58 return (0);
59}
60
64 _CurrentRuneLocale = rl;
65 __mb_cur_max = 4;
66
67 return (0);
68}
69
70int
71_GB18030_mbsinit(const mbstate_t *ps)
72{
73
74 return (ps == NULL || ((_GB18030State *)ps)->count == 0);
75}
76
61size_t
62_GB18030_mbrtowc(wchar_t * __restrict pwc, const char * __restrict s,
77size_t
78_GB18030_mbrtowc(wchar_t * __restrict pwc, const char * __restrict s,
63 size_t n, mbstate_t * __restrict ps __unused)
79 size_t n, mbstate_t * __restrict ps)
64{
80{
81 _GB18030State *gs;
65 wchar_t wch;
82 wchar_t wch;
66 int ch, len;
83 int ch, len, ocount;
84 size_t ncopy;
67
85
68 if (s == NULL)
69 /* Reset to initial shift state (no-op) */
70 return (0);
86 gs = (_GB18030State *)ps;
87
88 if (s == NULL) {
89 s = "";
90 n = 1;
91 pwc = NULL;
92 }
93
94 ncopy = MIN(MIN(n, MB_CUR_MAX), sizeof(gs->bytes) - gs->count);
95 memcpy(gs->bytes + gs->count, s, ncopy);
96 ocount = gs->count;
97 gs->count += ncopy;
98 s = (char *)gs->bytes;
99 n = gs->count;
100
71 if (n == 0)
72 /* Incomplete multibyte sequence */
73 return ((size_t)-2);
74
75 /*
76 * Single byte: [00-7f]
77 * Two byte: [81-fe][40-7e,80-fe]
78 * Four byte: [81-fe][30-39][81-fe][30-39]

--- 32 unchanged lines hidden (view full) ---

111 len = 4;
112 } else
113 goto ilseq;
114 } else
115 goto ilseq;
116
117 if (pwc != NULL)
118 *pwc = wch;
101 if (n == 0)
102 /* Incomplete multibyte sequence */
103 return ((size_t)-2);
104
105 /*
106 * Single byte: [00-7f]
107 * Two byte: [81-fe][40-7e,80-fe]
108 * Four byte: [81-fe][30-39][81-fe][30-39]

--- 32 unchanged lines hidden (view full) ---

141 len = 4;
142 } else
143 goto ilseq;
144 } else
145 goto ilseq;
146
147 if (pwc != NULL)
148 *pwc = wch;
119 return (wch == L'\0' ? 0 : len);
149 gs->count = 0;
150 return (wch == L'\0' ? 0 : len - ocount);
120ilseq:
121 errno = EILSEQ;
122 return ((size_t)-1);
123}
124
125size_t
126_GB18030_wcrtomb(char * __restrict s, wchar_t wc,
127 mbstate_t * __restrict ps __unused)
128{
129 size_t len;
130 int c;
131
132 if (s == NULL)
133 /* Reset to initial shift state (no-op) */
134 return (1);
151ilseq:
152 errno = EILSEQ;
153 return ((size_t)-1);
154}
155
156size_t
157_GB18030_wcrtomb(char * __restrict s, wchar_t wc,
158 mbstate_t * __restrict ps __unused)
159{
160 size_t len;
161 int c;
162
163 if (s == NULL)
164 /* Reset to initial shift state (no-op) */
165 return (1);
135
136 if ((wc & ~0x7fffffff) != 0)
137 goto ilseq;
138 if (wc & 0x7f000000) {
139 /* Replace high bit that mbrtowc() removed. */
140 wc |= 0x80000000;
141 c = (wc >> 24) & 0xff;
142 if (c < 0x81 || c > 0xfe)
143 goto ilseq;

--- 37 unchanged lines hidden ---
166 if ((wc & ~0x7fffffff) != 0)
167 goto ilseq;
168 if (wc & 0x7f000000) {
169 /* Replace high bit that mbrtowc() removed. */
170 wc |= 0x80000000;
171 c = (wc >> 24) & 0xff;
172 if (c < 0x81 || c > 0xfe)
173 goto ilseq;

--- 37 unchanged lines hidden ---