Debian Codegraph @

pypy/module/_multibytecodec/src/cjkcodecs/cjkcodecs.h

raw symbols SCIP
1 /*
2 * cjkcodecs.h is inspired by the file of the same name from CPython,
3 * but was heavily modified to suit PyPy.
4 *
5 * Original author: Hye-Shik Chang <[email protected]>
6 * Modified by: Armin Rigo <[email protected]>
7 */
8
9 #ifndef _CJKCODECS_H_
10 #define _CJKCODECS_H_
11
12 #include "src/cjkcodecs/multibytecodec.h"
13 #include "src/cjkcodecs/fixnames.h"
14
15
16 /* a unicode "undefined" codepoint */
17 #define UNIINV 0xFFFE
18
19 /* internal-use DBCS codepoints which aren't used by any charsets */
20 #define NOCHAR 0xFFFF
21 #define MULTIC 0xFFFE
22 #define DBCINV 0xFFFD
23
24 /* shorter macros to save source size of mapping tables */
25 #define U UNIINV
26 #define N NOCHAR
27 #define M MULTIC
28 #define D DBCINV
29
30 struct dbcs_index {
31 const ucs2_t *map;
32 unsigned char bottom, top;
33 };
34 typedef struct dbcs_index decode_map;
35
36 struct widedbcs_index {
37 const ucs4_t *map;
38 unsigned char bottom, top;
39 };
40 typedef struct widedbcs_index widedecode_map;
41
42 struct unim_index {
43 const DBCHAR *map;
44 unsigned char bottom, top;
45 };
46 typedef struct unim_index encode_map;
47
48 struct unim_index_bytebased {
49 const unsigned char *map;
50 unsigned char bottom, top;
51 };
52
53 struct dbcs_map {
54 const char *charset;
55 const struct unim_index *encmap;
56 const struct dbcs_index *decmap;
57 };
58
59 struct pair_encodemap {
60 ucs4_t uniseq;
61 DBCHAR code;
62 };
63
64 #define CODEC_INIT(encoding) \
65 static int encoding##_codec_init(const void *config)
66
67 #define ENCODER_INIT(encoding) \
68 static int encoding##_encode_init( \
69 MultibyteCodec_State *state, const void *config)
70 #define ENCODER(encoding) \
71 static Py_ssize_t encoding##_encode( \
72 MultibyteCodec_State *state, const void *config, \
73 const Py_UNICODE **inbuf, Py_ssize_t inleft, \
74 unsigned char **outbuf, Py_ssize_t outleft, int flags)
75 #define ENCODER_RESET(encoding) \
76 static Py_ssize_t encoding##_encode_reset( \
77 MultibyteCodec_State *state, const void *config, \
78 unsigned char **outbuf, Py_ssize_t outleft)
79
80 #define DECODER_INIT(encoding) \
81 static int encoding##_decode_init( \
82 MultibyteCodec_State *state, const void *config)
83 #define DECODER(encoding) \
84 static Py_ssize_t encoding##_decode( \
85 MultibyteCodec_State *state, const void *config, \
86 const unsigned char **inbuf, Py_ssize_t inleft, \
87 Py_UNICODE **outbuf, Py_ssize_t outleft)
88 #define DECODER_RESET(encoding) \
89 static Py_ssize_t encoding##_decode_reset( \
90 MultibyteCodec_State *state, const void *config)
91
92 #if Py_UNICODE_SIZE == 4
93 #define UCS4INVALID(code) \
94 if ((code) > 0xFFFF) \
95 return 1;
96 #else
97 #define UCS4INVALID(code) \
98 if (0) ;
99 #endif
100
101 #define NEXT_IN(i) \
102 (*inbuf) += (i); \
103 (inleft) -= (i);
104 #define NEXT_OUT(o) \
105 (*outbuf) += (o); \
106 (outleft) -= (o);
107 #define NEXT(i, o) \
108 NEXT_IN(i) NEXT_OUT(o)
109
110 #define REQUIRE_INBUF(n) \
111 if (inleft < (n)) \
112 return MBERR_TOOFEW;
113 #define REQUIRE_OUTBUF(n) \
114 if (outleft < (n)) \
115 return MBERR_TOOSMALL;
116
117 #define IN1 ((*inbuf)[0])
118 #define IN2 ((*inbuf)[1])
119 #define IN3 ((*inbuf)[2])
120 #define IN4 ((*inbuf)[3])
121
122 #define OUT1(c) ((*outbuf)[0]) = (c);
123 #define OUT2(c) ((*outbuf)[1]) = (c);
124 #define OUT3(c) ((*outbuf)[2]) = (c);
125 #define OUT4(c) ((*outbuf)[3]) = (c);
126
127 #define WRITE1(c1) \
128 REQUIRE_OUTBUF(1) \
129 (*outbuf)[0] = (c1);
130 #define WRITE2(c1, c2) \
131 REQUIRE_OUTBUF(2) \
132 (*outbuf)[0] = (c1); \
133 (*outbuf)[1] = (c2);
134 #define WRITE3(c1, c2, c3) \
135 REQUIRE_OUTBUF(3) \
136 (*outbuf)[0] = (c1); \
137 (*outbuf)[1] = (c2); \
138 (*outbuf)[2] = (c3);
139 #define WRITE4(c1, c2, c3, c4) \
140 REQUIRE_OUTBUF(4) \
141 (*outbuf)[0] = (c1); \
142 (*outbuf)[1] = (c2); \
143 (*outbuf)[2] = (c3); \
144 (*outbuf)[3] = (c4);
145
146 #if Py_UNICODE_SIZE == 2
147 # define WRITEUCS4(c) \
148 REQUIRE_OUTBUF(2) \
149 (*outbuf)[0] = 0xd800 + (((c) - 0x10000) >> 10); \
150 (*outbuf)[1] = 0xdc00 + (((c) - 0x10000) & 0x3ff); \
151 NEXT_OUT(2)
152 #else
153 # define WRITEUCS4(c) \
154 REQUIRE_OUTBUF(1) \
155 **outbuf = (Py_UNICODE)(c); \
156 NEXT_OUT(1)
157 #endif
158
159 #define _TRYMAP_ENC(m, assi, val) \
160 ((m)->map != NULL && (val) >= (m)->bottom && \
161 (val)<= (m)->top && ((assi) = (m)->map[(val) - \
162 (m)->bottom]) != NOCHAR)
163 #define TRYMAP_ENC_COND(charset, assi, uni) \
164 _TRYMAP_ENC(&charset##_encmap[(uni) >> 8], assi, (uni) & 0xff)
165 #define TRYMAP_ENC(charset, assi, uni) \
166 if TRYMAP_ENC_COND(charset, assi, uni)
167
168 #define _TRYMAP_DEC(m, assi, val) \
169 ((m)->map != NULL && (val) >= (m)->bottom && \
170 (val)<= (m)->top && ((assi) = (m)->map[(val) - \
171 (m)->bottom]) != UNIINV)
172 #define TRYMAP_DEC(charset, assi, c1, c2) \
173 if _TRYMAP_DEC(&charset##_decmap[c1], assi, c2)
174
175 #define _TRYMAP_ENC_MPLANE(m, assplane, asshi, asslo, val) \
176 ((m)->map != NULL && (val) >= (m)->bottom && \
177 (val)<= (m)->top && \
178 ((assplane) = (m)->map[((val) - (m)->bottom)*3]) != 0 && \
179 (((asshi) = (m)->map[((val) - (m)->bottom)*3 + 1]), 1) && \
180 (((asslo) = (m)->map[((val) - (m)->bottom)*3 + 2]), 1))
181 #define TRYMAP_ENC_MPLANE(charset, assplane, asshi, asslo, uni) \
182 if _TRYMAP_ENC_MPLANE(&charset##_encmap[(uni) >> 8], \
183 assplane, asshi, asslo, (uni) & 0xff)
184 #define TRYMAP_DEC_MPLANE(charset, assi, plane, c1, c2) \
185 if _TRYMAP_DEC(&charset##_decmap[plane][c1], assi, c2)
186
187 #if Py_UNICODE_SIZE == 2
188 #define DECODE_SURROGATE(c) \
189 if (c >> 10 == 0xd800 >> 10) { /* high surrogate */ \
190 REQUIRE_INBUF(2) \
191 if (IN2 >> 10 == 0xdc00 >> 10) { /* low surrogate */ \
192 c = 0x10000 + ((ucs4_t)(c - 0xd800) << 10) + \
193 ((ucs4_t)(IN2) - 0xdc00); \
194 } \
195 }
196 #define GET_INSIZE(c) ((c) > 0xffff ? 2 : 1)
197 #else
198 #define DECODE_SURROGATE(c) {;}
199 #define GET_INSIZE(c) 1
200 #endif
201
202 #define BEGIN_MAPPINGS_LIST /* empty */
203 #define MAPPING_ENCONLY(enc) \
204 RPY_EXTERN const struct dbcs_map pypy_cjkmap_##enc; \
205 const struct dbcs_map pypy_cjkmap_##enc = {#enc, (void*)enc##_encmap, NULL};
206 #define MAPPING_DECONLY(enc) \
207 RPY_EXTERN const struct dbcs_map pypy_cjkmap_##enc; \
208 const struct dbcs_map pypy_cjkmap_##enc = {#enc, NULL, (void*)enc##_decmap};
209 #define MAPPING_ENCDEC(enc) \
210 RPY_EXTERN const struct dbcs_map pypy_cjkmap_##enc; \
211 const struct dbcs_map pypy_cjkmap_##enc = {#enc, (void*)enc##_encmap, \
212 (void*)enc##_decmap};
213 #define END_MAPPINGS_LIST /* empty */
214
215 #define BEGIN_CODECS_LIST /* empty */
216 #define _CODEC(name) \
217 static MultibyteCodec _pypy_cjkcodec_##name; \
218 MultibyteCodec *pypy_cjkcodec_##name(void) { \
219 if (_pypy_cjkcodec_##name.codecinit != NULL) { \
220 int r = _pypy_cjkcodec_##name.codecinit(_pypy_cjkcodec_##name.config); \
221 assert(r == 0); \
222 } \
223 return &_pypy_cjkcodec_##name; \
224 } \
225 static MultibyteCodec _pypy_cjkcodec_##name
226 #define _STATEFUL_METHODS(enc) \
227 enc##_encode, \
228 enc##_encode_init, \
229 enc##_encode_reset, \
230 enc##_decode, \
231 enc##_decode_init, \
232 enc##_decode_reset,
233 #define _STATELESS_METHODS(enc) \
234 enc##_encode, NULL, NULL, \
235 enc##_decode, NULL, NULL,
236 #define CODEC_STATEFUL(enc) _CODEC(enc) = { \
237 #enc, NULL, NULL, \
238 _STATEFUL_METHODS(enc) \
239 };
240 #define CODEC_STATELESS(enc) _CODEC(enc) = { \
241 #enc, NULL, NULL, \
242 _STATELESS_METHODS(enc) \
243 };
244 #define CODEC_STATELESS_WINIT(enc) _CODEC(enc) = { \
245 #enc, NULL, \
246 enc##_codec_init, \
247 _STATELESS_METHODS(enc) \
248 };
249 #define CODEC_STATELESS_CONFIG(enc, config, baseenc) _CODEC(enc) = { \
250 #enc, config, NULL, \
251 _STATELESS_METHODS(baseenc) \
252 };
253 #define CODEC_STATEFUL_CONFIG(enc, variation, config) \
254 _CODEC(enc##_##variation) = { \
255 #enc "_" #variation, \
256 config, \
257 enc##_codec_init, \
258 _STATEFUL_METHODS(enc) \
259 };
260 #define END_CODECS_LIST /* empty */
261
262
263 #ifdef USING_BINARY_PAIR_SEARCH
264 static DBCHAR
265 find_pairencmap(ucs2_t body, ucs2_t modifier,
266 const struct pair_encodemap *haystack, int haystacksize)
267 {
268 int pos, min, max;
269 ucs4_t value = body << 16 | modifier;
270
271 min = 0;
272 max = haystacksize;
273
274 for (pos = haystacksize >> 1; min != max; pos = (min + max) >> 1) {
275 if (value < haystack[pos].uniseq) {
276 if (max != pos) {
277 max = pos;
278 continue;
279 }
280 }
281 else if (value > haystack[pos].uniseq) {
282 if (min != pos) {
283 min = pos;
284 continue;
285 }
286 }
287 break;
288 }
289
290 if (value == haystack[pos].uniseq) {
291 return haystack[pos].code;
292 }
293 return DBCINV;
294 }
295 #endif
296
297
298 #ifdef USING_IMPORTED_MAPS
299 #define USING_IMPORTED_MAP(charset) \
300 RPY_EXTERN const struct dbcs_map pypy_cjkmap_##charset;
301
302 #define IMPORT_MAP(locale, charset, encmap, decmap) \
303 importmap(&pypy_cjkmap_##charset, encmap, decmap)
304
305 static void importmap(const struct dbcs_map *src, void *encmp,
306 void *decmp)
307 {
308 if (encmp) *(const encode_map **)encmp = src->encmap;
309 if (decmp) *(const decode_map **)decmp = src->decmap;
310 }
311 #endif
312
313
314 #define I_AM_A_MODULE_FOR(loc) /* empty */
315
316
317 #endif