771
|
1 /* Conversion functions for I18N encodings, but not Unicode (in separate file).
|
|
2 Copyright (C) 1991, 1995 Free Software Foundation, Inc.
|
|
3 Copyright (C) 1995 Sun Microsystems, Inc.
|
|
4 Copyright (C) 2000, 2001, 2002 Ben Wing.
|
|
5
|
|
6 This file is part of XEmacs.
|
|
7
|
|
8 XEmacs is free software; you can redistribute it and/or modify it
|
|
9 under the terms of the GNU General Public License as published by the
|
|
10 Free Software Foundation; either version 2, or (at your option) any
|
|
11 later version.
|
|
12
|
|
13 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
16 for more details.
|
|
17
|
|
18 You should have received a copy of the GNU General Public License
|
|
19 along with XEmacs; see the file COPYING. If not, write to
|
|
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
21 Boston, MA 02111-1307, USA. */
|
|
22
|
|
23 /* Synched up with: Mule 2.3. Not in FSF. */
|
|
24
|
|
25 /* For previous history, see file-coding.c.
|
|
26
|
|
27 September 10, 2001: Extracted from file-coding.c by Ben Wing.
|
|
28
|
|
29 Later in September: Finished abstraction of detection system, rewrote
|
|
30 all the detectors to include multiple levels of likelihood.
|
|
31 */
|
|
32
|
|
33 #include <config.h>
|
|
34 #include "lisp.h"
|
|
35
|
|
36 #include "charset.h"
|
|
37 #include "mule-ccl.h"
|
|
38 #include "file-coding.h"
|
|
39
|
|
40 Lisp_Object Qshift_jis, Qiso2022, Qbig5, Qccl;
|
|
41
|
|
42 Lisp_Object Qcharset_g0, Qcharset_g1, Qcharset_g2, Qcharset_g3;
|
|
43 Lisp_Object Qforce_g0_on_output, Qforce_g1_on_output;
|
|
44 Lisp_Object Qforce_g2_on_output, Qforce_g3_on_output;
|
|
45 Lisp_Object Qno_iso6429;
|
|
46 Lisp_Object Qinput_charset_conversion, Qoutput_charset_conversion;
|
|
47 Lisp_Object Qshort, Qno_ascii_eol, Qno_ascii_cntl, Qseven, Qlock_shift;
|
|
48
|
|
49 Lisp_Object Qiso_7, Qiso_8_designate, Qiso_8_1, Qiso_8_2, Qiso_lock_shift;
|
|
50
|
|
51
|
|
52 /************************************************************************/
|
|
53 /* Shift-JIS methods */
|
|
54 /************************************************************************/
|
|
55
|
|
56 /* Shift-JIS; Hankaku (half-width) KANA is also supported. */
|
|
57 DEFINE_CODING_SYSTEM_TYPE (shift_jis);
|
|
58
|
|
59 /* Shift-JIS is a coding system encoding three character sets: ASCII, right
|
|
60 half of JISX0201-Kana, and JISX0208. An ASCII character is encoded
|
|
61 as is. A character of JISX0201-Kana (DIMENSION1_CHARS94 character set) is
|
|
62 encoded by "position-code + 0x80". A character of JISX0208
|
|
63 (DIMENSION2_CHARS94 character set) is encoded in 2-byte but two
|
|
64 position-codes are divided and shifted so that it fit in the range
|
|
65 below.
|
|
66
|
|
67 --- CODE RANGE of Shift-JIS ---
|
|
68 (character set) (range)
|
|
69 ASCII 0x00 .. 0x7F
|
|
70 JISX0201-Kana 0xA0 .. 0xDF
|
|
71 JISX0208 (1st byte) 0x80 .. 0x9F and 0xE0 .. 0xEF
|
|
72 (2nd byte) 0x40 .. 0x7E and 0x80 .. 0xFC
|
|
73 -------------------------------
|
|
74
|
|
75 */
|
|
76
|
|
77 /* Is this the first byte of a Shift-JIS two-byte char? */
|
|
78
|
826
|
79 inline static int
|
|
80 byte_shift_jis_two_byte_1_p (int c)
|
|
81 {
|
|
82 return (c >= 0x81 && c <= 0x9F) || (c >= 0xE0 && c <= 0xEF);
|
|
83 }
|
771
|
84
|
|
85 /* Is this the second byte of a Shift-JIS two-byte char? */
|
|
86
|
826
|
87 inline static int
|
|
88 byte_shift_jis_two_byte_2_p (int c)
|
|
89 {
|
|
90 return (c >= 0x40 && c <= 0x7E) || (c >= 0x80 && c <= 0xFC);
|
|
91 }
|
|
92
|
|
93 inline static int
|
|
94 byte_shift_jis_katakana_p (int c)
|
|
95 {
|
|
96 return c >= 0xA1 && c <= 0xDF;
|
|
97 }
|
771
|
98
|
3439
|
99 inline static void
|
|
100 dynarr_add_2022_one_dimension (Lisp_Object charset, Ibyte c,
|
|
101 unsigned char charmask,
|
|
102 unsigned_char_dynarr *dst)
|
|
103 {
|
|
104 if (XCHARSET_ENCODE_AS_UTF_8 (charset))
|
|
105 {
|
|
106 encode_unicode_char (charset, c & charmask, 0,
|
|
107 dst, UNICODE_UTF_8, 0);
|
|
108 }
|
|
109 else
|
|
110 {
|
|
111 Dynarr_add (dst, c & charmask);
|
|
112 }
|
|
113 }
|
|
114
|
|
115 inline static void
|
|
116 dynarr_add_2022_two_dimensions (Lisp_Object charset, Ibyte c,
|
|
117 unsigned int ch,
|
|
118 unsigned char charmask,
|
|
119 unsigned_char_dynarr *dst)
|
|
120 {
|
|
121 if (XCHARSET_ENCODE_AS_UTF_8 (charset))
|
|
122 {
|
|
123 encode_unicode_char (charset,
|
|
124 ch & charmask,
|
|
125 c & charmask, dst,
|
|
126 UNICODE_UTF_8, 0);
|
|
127 }
|
|
128 else
|
|
129 {
|
|
130 Dynarr_add (dst, ch & charmask);
|
|
131 Dynarr_add (dst, c & charmask);
|
|
132 }
|
|
133 }
|
|
134
|
771
|
135 /* Convert Shift-JIS data to internal format. */
|
|
136
|
|
137 static Bytecount
|
|
138 shift_jis_convert (struct coding_stream *str, const UExtbyte *src,
|
|
139 unsigned_char_dynarr *dst, Bytecount n)
|
|
140 {
|
|
141 unsigned int ch = str->ch;
|
|
142 Bytecount orign = n;
|
|
143
|
|
144 if (str->direction == CODING_DECODE)
|
|
145 {
|
|
146 while (n--)
|
|
147 {
|
|
148 UExtbyte c = *src++;
|
|
149
|
|
150 if (ch)
|
|
151 {
|
|
152 /* Previous character was first byte of Shift-JIS Kanji char. */
|
826
|
153 if (byte_shift_jis_two_byte_2_p (c))
|
771
|
154 {
|
867
|
155 Ibyte e1, e2;
|
771
|
156
|
|
157 Dynarr_add (dst, LEADING_BYTE_JAPANESE_JISX0208);
|
|
158 DECODE_SHIFT_JIS (ch, c, e1, e2);
|
|
159 Dynarr_add (dst, e1);
|
|
160 Dynarr_add (dst, e2);
|
|
161 }
|
|
162 else
|
|
163 {
|
|
164 DECODE_ADD_BINARY_CHAR (ch, dst);
|
|
165 DECODE_ADD_BINARY_CHAR (c, dst);
|
|
166 }
|
|
167 ch = 0;
|
|
168 }
|
|
169 else
|
|
170 {
|
826
|
171 if (byte_shift_jis_two_byte_1_p (c))
|
771
|
172 ch = c;
|
826
|
173 else if (byte_shift_jis_katakana_p (c))
|
771
|
174 {
|
|
175 Dynarr_add (dst, LEADING_BYTE_KATAKANA_JISX0201);
|
|
176 Dynarr_add (dst, c);
|
|
177 }
|
|
178 else
|
|
179 DECODE_ADD_BINARY_CHAR (c, dst);
|
|
180 }
|
|
181 }
|
|
182
|
|
183 if (str->eof)
|
|
184 DECODE_OUTPUT_PARTIAL_CHAR (ch, dst);
|
|
185 }
|
|
186 else
|
|
187 {
|
|
188 while (n--)
|
|
189 {
|
867
|
190 Ibyte c = *src++;
|
826
|
191 if (byte_ascii_p (c))
|
771
|
192 {
|
|
193 Dynarr_add (dst, c);
|
|
194 ch = 0;
|
|
195 }
|
867
|
196 else if (ibyte_leading_byte_p (c))
|
771
|
197 ch = (c == LEADING_BYTE_KATAKANA_JISX0201 ||
|
|
198 c == LEADING_BYTE_JAPANESE_JISX0208_1978 ||
|
|
199 c == LEADING_BYTE_JAPANESE_JISX0208) ? c : 0;
|
|
200 else if (ch)
|
|
201 {
|
|
202 if (ch == LEADING_BYTE_KATAKANA_JISX0201)
|
|
203 {
|
|
204 Dynarr_add (dst, c);
|
|
205 ch = 0;
|
|
206 }
|
|
207 else if (ch == LEADING_BYTE_JAPANESE_JISX0208_1978 ||
|
|
208 ch == LEADING_BYTE_JAPANESE_JISX0208)
|
|
209 ch = c;
|
|
210 else
|
|
211 {
|
|
212 UExtbyte j1, j2;
|
|
213 ENCODE_SHIFT_JIS (ch, c, j1, j2);
|
|
214 Dynarr_add (dst, j1);
|
|
215 Dynarr_add (dst, j2);
|
|
216 ch = 0;
|
|
217 }
|
|
218 }
|
|
219 }
|
|
220 }
|
|
221
|
|
222 str->ch = ch;
|
|
223
|
|
224 return orign;
|
|
225 }
|
|
226
|
|
227 DEFUN ("decode-shift-jis-char", Fdecode_shift_jis_char, 1, 1, 0, /*
|
|
228 Decode a JISX0208 character of Shift-JIS coding-system.
|
|
229 CODE is the character code in Shift-JIS as a cons of type bytes.
|
|
230 Return the corresponding character.
|
|
231 */
|
|
232 (code))
|
|
233 {
|
|
234 int c1, c2, s1, s2;
|
|
235
|
|
236 CHECK_CONS (code);
|
|
237 CHECK_INT (XCAR (code));
|
|
238 CHECK_INT (XCDR (code));
|
|
239 s1 = XINT (XCAR (code));
|
|
240 s2 = XINT (XCDR (code));
|
826
|
241 if (byte_shift_jis_two_byte_1_p (s1) &&
|
|
242 byte_shift_jis_two_byte_2_p (s2))
|
771
|
243 {
|
|
244 DECODE_SHIFT_JIS (s1, s2, c1, c2);
|
867
|
245 return make_char (make_ichar (Vcharset_japanese_jisx0208,
|
831
|
246 c1 & 0x7F, c2 & 0x7F));
|
771
|
247 }
|
|
248 else
|
|
249 return Qnil;
|
|
250 }
|
|
251
|
|
252 DEFUN ("encode-shift-jis-char", Fencode_shift_jis_char, 1, 1, 0, /*
|
|
253 Encode a JISX0208 character CHARACTER to SHIFT-JIS coding-system.
|
|
254 Return the corresponding character code in SHIFT-JIS as a cons of two bytes.
|
|
255 */
|
|
256 (character))
|
|
257 {
|
|
258 Lisp_Object charset;
|
|
259 int c1, c2, s1, s2;
|
|
260
|
|
261 CHECK_CHAR_COERCE_INT (character);
|
867
|
262 BREAKUP_ICHAR (XCHAR (character), charset, c1, c2);
|
771
|
263 if (EQ (charset, Vcharset_japanese_jisx0208))
|
|
264 {
|
|
265 ENCODE_SHIFT_JIS (c1 | 0x80, c2 | 0x80, s1, s2);
|
|
266 return Fcons (make_int (s1), make_int (s2));
|
|
267 }
|
|
268 else
|
|
269 return Qnil;
|
|
270 }
|
|
271
|
|
272
|
|
273 /************************************************************************/
|
|
274 /* Shift-JIS detector */
|
|
275 /************************************************************************/
|
|
276
|
|
277 DEFINE_DETECTOR (shift_jis);
|
|
278 DEFINE_DETECTOR_CATEGORY (shift_jis, shift_jis);
|
|
279
|
|
280 struct shift_jis_detector
|
|
281 {
|
|
282 int seen_jisx0208_char_in_c1;
|
|
283 int seen_jisx0208_char_in_upper;
|
|
284 int seen_jisx0201_char;
|
|
285 unsigned int seen_iso2022_esc:1;
|
|
286 unsigned int seen_bad_first_byte:1;
|
|
287 unsigned int seen_bad_second_byte:1;
|
|
288 /* temporary */
|
|
289 unsigned int in_second_byte:1;
|
|
290 unsigned int first_byte_was_c1:1;
|
|
291 };
|
|
292
|
|
293 static void
|
|
294 shift_jis_detect (struct detection_state *st, const UExtbyte *src,
|
|
295 Bytecount n)
|
|
296 {
|
|
297 struct shift_jis_detector *data = DETECTION_STATE_DATA (st, shift_jis);
|
|
298
|
|
299 while (n--)
|
|
300 {
|
|
301 UExtbyte c = *src++;
|
|
302 if (!data->in_second_byte)
|
|
303 {
|
|
304 if (c >= 0x80 && c <= 0x9F)
|
|
305 data->first_byte_was_c1 = 1;
|
|
306 if (c >= 0xA0 && c <= 0xDF)
|
|
307 data->seen_jisx0201_char++;
|
|
308 else if ((c >= 0x80 && c <= 0x9F) || (c >= 0xE0 && c <= 0xEF))
|
|
309 data->in_second_byte = 1;
|
|
310 else if (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO)
|
|
311 data->seen_iso2022_esc = 1;
|
|
312 else if (c >= 0x80)
|
|
313 data->seen_bad_first_byte = 1;
|
|
314 }
|
|
315 else
|
|
316 {
|
|
317 if ((c >= 0x40 && c <= 0x7E) || (c >= 0x80 && c <= 0xFC))
|
|
318 {
|
|
319 if (data->first_byte_was_c1 || (c >= 0x80 && c <= 0x9F))
|
|
320 data->seen_jisx0208_char_in_c1++;
|
|
321 else
|
|
322 data->seen_jisx0208_char_in_upper++;
|
|
323 }
|
|
324 else
|
|
325 data->seen_bad_second_byte = 1;
|
|
326 data->in_second_byte = 0;
|
|
327 data->first_byte_was_c1 = 0;
|
|
328 }
|
|
329 }
|
|
330
|
|
331 if (data->seen_bad_second_byte)
|
|
332 DET_RESULT (st, shift_jis) = DET_NEARLY_IMPOSSIBLE;
|
|
333 else if (data->seen_bad_first_byte)
|
|
334 DET_RESULT (st, shift_jis) = DET_QUITE_IMPROBABLE;
|
|
335 else if (data->seen_iso2022_esc)
|
|
336 DET_RESULT (st, shift_jis) = DET_SOMEWHAT_UNLIKELY;
|
|
337 else if (data->seen_jisx0208_char_in_c1 >= 20 ||
|
|
338 (data->seen_jisx0208_char_in_c1 >= 10 &&
|
|
339 data->seen_jisx0208_char_in_upper >= 10))
|
|
340 DET_RESULT (st, shift_jis) = DET_QUITE_PROBABLE;
|
|
341 else if (data->seen_jisx0208_char_in_c1 > 3 ||
|
|
342 data->seen_jisx0208_char_in_upper >= 10 ||
|
|
343 /* Since the range is limited compared to what is often seen
|
|
344 is typical Latin-X charsets, the fact that we've seen a
|
|
345 bunch of them and none that are invalid is reasonably
|
|
346 strong statistical evidence of this encoding, or at least
|
|
347 not of the common Latin-X ones. */
|
|
348 data->seen_jisx0201_char >= 100)
|
|
349 DET_RESULT (st, shift_jis) = DET_SOMEWHAT_LIKELY;
|
|
350 else if (data->seen_jisx0208_char_in_c1 > 0 ||
|
|
351 data->seen_jisx0208_char_in_upper > 0 ||
|
|
352 data->seen_jisx0201_char > 0)
|
|
353 DET_RESULT (st, shift_jis) = DET_SLIGHTLY_LIKELY;
|
|
354 else
|
|
355 DET_RESULT (st, shift_jis) = DET_AS_LIKELY_AS_UNLIKELY;
|
|
356 }
|
|
357
|
|
358
|
|
359 /************************************************************************/
|
|
360 /* Big5 methods */
|
|
361 /************************************************************************/
|
|
362
|
2819
|
363 /* BIG5 (used for Mandarin in Taiwan). */
|
771
|
364 DEFINE_CODING_SYSTEM_TYPE (big5);
|
|
365
|
|
366 /* BIG5 is a coding system encoding two character sets: ASCII and
|
|
367 Big5. An ASCII character is encoded as is. Big5 is a two-byte
|
|
368 character set and is encoded in two-byte.
|
|
369
|
|
370 --- CODE RANGE of BIG5 ---
|
|
371 (character set) (range)
|
|
372 ASCII 0x00 .. 0x7F
|
|
373 Big5 (1st byte) 0xA1 .. 0xFE
|
|
374 (2nd byte) 0x40 .. 0x7E and 0xA1 .. 0xFE
|
|
375 --------------------------
|
|
376
|
|
377 Since the number of characters in Big5 is larger than maximum
|
|
378 characters in Emacs' charset (96x96), it can't be handled as one
|
|
379 charset. So, in XEmacs, Big5 is divided into two: `charset-big5-1'
|
|
380 and `charset-big5-2'. Both <type>s are DIMENSION2_CHARS94. The former
|
|
381 contains frequently used characters and the latter contains less
|
|
382 frequently used characters. */
|
|
383
|
826
|
384 inline static int
|
|
385 byte_big5_two_byte_1_p (int c)
|
|
386 {
|
|
387 return c >= 0xA1 && c <= 0xFE;
|
|
388 }
|
771
|
389
|
|
390 /* Is this the second byte of a Shift-JIS two-byte char? */
|
|
391
|
826
|
392 inline static int
|
|
393 byte_big5_two_byte_2_p (int c)
|
|
394 {
|
|
395 return (c >= 0x40 && c <= 0x7E) || (c >= 0xA1 && c <= 0xFE);
|
|
396 }
|
771
|
397
|
|
398 /* Number of Big5 characters which have the same code in 1st byte. */
|
|
399
|
|
400 #define BIG5_SAME_ROW (0xFF - 0xA1 + 0x7F - 0x40)
|
|
401
|
|
402 /* Code conversion macros. These are macros because they are used in
|
|
403 inner loops during code conversion.
|
|
404
|
|
405 Note that temporary variables in macros introduce the classic
|
|
406 dynamic-scoping problems with variable names. We use capital-
|
|
407 lettered variables in the assumption that XEmacs does not use
|
|
408 capital letters in variables except in a very formalized way
|
|
409 (e.g. Qstring). */
|
|
410
|
|
411 /* Convert Big5 code (b1, b2) into its internal string representation
|
|
412 (lb, c1, c2). */
|
|
413
|
|
414 /* There is a much simpler way to split the Big5 charset into two.
|
|
415 For the moment I'm going to leave the algorithm as-is because it
|
|
416 claims to separate out the most-used characters into a single
|
|
417 charset, which perhaps will lead to optimizations in various
|
|
418 places.
|
|
419
|
|
420 The way the algorithm works is something like this:
|
|
421
|
|
422 Big5 can be viewed as a 94x157 charset, where the row is
|
|
423 encoded into the bytes 0xA1 .. 0xFE and the column is encoded
|
|
424 into the bytes 0x40 .. 0x7E and 0xA1 .. 0xFE. As for frequency,
|
|
425 the split between low and high column numbers is apparently
|
|
426 meaningless; ascending rows produce less and less frequent chars.
|
|
427 Therefore, we assign the lower half of rows (0xA1 .. 0xC8) to
|
|
428 the first charset, and the upper half (0xC9 .. 0xFE) to the
|
|
429 second. To do the conversion, we convert the character into
|
|
430 a single number where 0 .. 156 is the first row, 157 .. 313
|
|
431 is the second, etc. That way, the characters are ordered by
|
|
432 decreasing frequency. Then we just chop the space in two
|
|
433 and coerce the result into a 94x94 space.
|
|
434 */
|
|
435
|
|
436 #define DECODE_BIG5(b1, b2, lb, c1, c2) do \
|
|
437 { \
|
|
438 int B1 = b1, B2 = b2; \
|
|
439 int I \
|
|
440 = (B1 - 0xA1) * BIG5_SAME_ROW + B2 - (B2 < 0x7F ? 0x40 : 0x62); \
|
|
441 \
|
|
442 if (B1 < 0xC9) \
|
|
443 { \
|
|
444 lb = LEADING_BYTE_CHINESE_BIG5_1; \
|
|
445 } \
|
|
446 else \
|
|
447 { \
|
|
448 lb = LEADING_BYTE_CHINESE_BIG5_2; \
|
|
449 I -= (BIG5_SAME_ROW) * (0xC9 - 0xA1); \
|
|
450 } \
|
|
451 c1 = I / (0xFF - 0xA1) + 0xA1; \
|
|
452 c2 = I % (0xFF - 0xA1) + 0xA1; \
|
|
453 } while (0)
|
|
454
|
|
455 /* Convert the internal string representation of a Big5 character
|
|
456 (lb, c1, c2) into Big5 code (b1, b2). */
|
|
457
|
|
458 #define ENCODE_BIG5(lb, c1, c2, b1, b2) do \
|
|
459 { \
|
|
460 int I = ((c1) - 0xA1) * (0xFF - 0xA1) + ((c2) - 0xA1); \
|
|
461 \
|
|
462 if (lb == LEADING_BYTE_CHINESE_BIG5_2) \
|
|
463 { \
|
|
464 I += BIG5_SAME_ROW * (0xC9 - 0xA1); \
|
|
465 } \
|
|
466 b1 = I / BIG5_SAME_ROW + 0xA1; \
|
|
467 b2 = I % BIG5_SAME_ROW; \
|
|
468 b2 += b2 < 0x3F ? 0x40 : 0x62; \
|
|
469 } while (0)
|
|
470
|
|
471 /* Convert Big5 data to internal format. */
|
|
472
|
|
473 static Bytecount
|
|
474 big5_convert (struct coding_stream *str, const UExtbyte *src,
|
|
475 unsigned_char_dynarr *dst, Bytecount n)
|
|
476 {
|
|
477 unsigned int ch = str->ch;
|
|
478 Bytecount orign = n;
|
|
479
|
|
480 if (str->direction == CODING_DECODE)
|
|
481 {
|
|
482 while (n--)
|
|
483 {
|
|
484 UExtbyte c = *src++;
|
|
485 if (ch)
|
|
486 {
|
|
487 /* Previous character was first byte of Big5 char. */
|
826
|
488 if (byte_big5_two_byte_2_p (c))
|
771
|
489 {
|
867
|
490 Ibyte b1, b2, b3;
|
771
|
491 DECODE_BIG5 (ch, c, b1, b2, b3);
|
|
492 Dynarr_add (dst, b1);
|
|
493 Dynarr_add (dst, b2);
|
|
494 Dynarr_add (dst, b3);
|
|
495 }
|
|
496 else
|
|
497 {
|
|
498 DECODE_ADD_BINARY_CHAR (ch, dst);
|
|
499 DECODE_ADD_BINARY_CHAR (c, dst);
|
|
500 }
|
|
501 ch = 0;
|
|
502 }
|
|
503 else
|
|
504 {
|
826
|
505 if (byte_big5_two_byte_1_p (c))
|
771
|
506 ch = c;
|
|
507 else
|
|
508 DECODE_ADD_BINARY_CHAR (c, dst);
|
|
509 }
|
|
510 }
|
|
511
|
|
512 if (str->eof)
|
|
513 DECODE_OUTPUT_PARTIAL_CHAR (ch, dst);
|
|
514 }
|
|
515 else
|
|
516 {
|
|
517 while (n--)
|
|
518 {
|
867
|
519 Ibyte c = *src++;
|
826
|
520 if (byte_ascii_p (c))
|
771
|
521 {
|
|
522 /* ASCII. */
|
|
523 Dynarr_add (dst, c);
|
|
524 }
|
867
|
525 else if (ibyte_leading_byte_p (c))
|
771
|
526 {
|
|
527 if (c == LEADING_BYTE_CHINESE_BIG5_1 ||
|
|
528 c == LEADING_BYTE_CHINESE_BIG5_2)
|
|
529 {
|
|
530 /* A recognized leading byte. */
|
|
531 ch = c;
|
|
532 continue; /* not done with this character. */
|
|
533 }
|
|
534 /* otherwise just ignore this character. */
|
|
535 }
|
|
536 else if (ch == LEADING_BYTE_CHINESE_BIG5_1 ||
|
|
537 ch == LEADING_BYTE_CHINESE_BIG5_2)
|
|
538 {
|
|
539 /* Previous char was a recognized leading byte. */
|
|
540 ch = (ch << 8) | c;
|
|
541 continue; /* not done with this character. */
|
|
542 }
|
|
543 else if (ch)
|
|
544 {
|
|
545 /* Encountering second byte of a Big5 character. */
|
|
546 UExtbyte b1, b2;
|
|
547
|
|
548 ENCODE_BIG5 (ch >> 8, ch & 0xFF, c, b1, b2);
|
|
549 Dynarr_add (dst, b1);
|
|
550 Dynarr_add (dst, b2);
|
|
551 }
|
|
552
|
|
553 ch = 0;
|
|
554 }
|
|
555 }
|
|
556
|
|
557 str->ch = ch;
|
|
558 return orign;
|
|
559 }
|
|
560
|
867
|
561 Ichar
|
771
|
562 decode_big5_char (int b1, int b2)
|
|
563 {
|
826
|
564 if (byte_big5_two_byte_1_p (b1) &&
|
|
565 byte_big5_two_byte_2_p (b2))
|
771
|
566 {
|
|
567 int leading_byte;
|
|
568 Lisp_Object charset;
|
|
569 int c1, c2;
|
|
570
|
|
571 DECODE_BIG5 (b1, b2, leading_byte, c1, c2);
|
826
|
572 charset = charset_by_leading_byte (leading_byte);
|
867
|
573 return make_ichar (charset, c1 & 0x7F, c2 & 0x7F);
|
771
|
574 }
|
|
575 else
|
|
576 return -1;
|
|
577 }
|
|
578
|
|
579 DEFUN ("decode-big5-char", Fdecode_big5_char, 1, 1, 0, /*
|
|
580 Convert Big Five character codes in CODE into a character.
|
|
581 CODE is a cons of two integers specifying the codepoints in Big Five.
|
|
582 Return the corresponding character, or nil if the codepoints are out of range.
|
|
583
|
|
584 The term `decode' is used because the codepoints can be viewed as the
|
|
585 representation of the character in the external Big Five encoding, and thus
|
|
586 converting them to a character is analogous to any other operation that
|
|
587 decodes an external representation.
|
|
588 */
|
|
589 (code))
|
|
590 {
|
867
|
591 Ichar ch;
|
771
|
592
|
|
593 CHECK_CONS (code);
|
|
594 CHECK_INT (XCAR (code));
|
|
595 CHECK_INT (XCDR (code));
|
|
596 ch = decode_big5_char (XINT (XCAR (code)), XINT (XCDR (code)));
|
|
597 if (ch == -1)
|
|
598 return Qnil;
|
|
599 else
|
|
600 return make_char (ch);
|
|
601 }
|
|
602
|
|
603 DEFUN ("encode-big5-char", Fencode_big5_char, 1, 1, 0, /*
|
|
604 Convert the specified Big Five character into its codepoints.
|
|
605 The codepoints are returned as a cons of two integers, specifying the
|
|
606 Big Five codepoints. See `decode-big5-char' for the reason why the
|
|
607 term `encode' is used for this operation.
|
|
608 */
|
|
609 (character))
|
|
610 {
|
|
611 Lisp_Object charset;
|
|
612 int c1, c2, b1, b2;
|
|
613
|
|
614 CHECK_CHAR_COERCE_INT (character);
|
867
|
615 BREAKUP_ICHAR (XCHAR (character), charset, c1, c2);
|
771
|
616 if (EQ (charset, Vcharset_chinese_big5_1) ||
|
|
617 EQ (charset, Vcharset_chinese_big5_2))
|
|
618 {
|
|
619 ENCODE_BIG5 (XCHARSET_LEADING_BYTE (charset), c1 | 0x80, c2 | 0x80,
|
|
620 b1, b2);
|
|
621 return Fcons (make_int (b1), make_int (b2));
|
|
622 }
|
|
623 else
|
|
624 return Qnil;
|
|
625 }
|
|
626
|
|
627
|
|
628 /************************************************************************/
|
|
629 /* Big5 detector */
|
|
630 /************************************************************************/
|
|
631
|
|
632 DEFINE_DETECTOR (big5);
|
|
633 DEFINE_DETECTOR_CATEGORY (big5, big5);
|
|
634
|
|
635 struct big5_detector
|
|
636 {
|
|
637 int seen_big5_char;
|
985
|
638 int seen_euc_char;
|
771
|
639 unsigned int seen_iso2022_esc:1;
|
|
640 unsigned int seen_bad_first_byte:1;
|
|
641 unsigned int seen_bad_second_byte:1;
|
|
642
|
|
643 /* temporary */
|
|
644 unsigned int in_second_byte:1;
|
|
645 };
|
|
646
|
|
647 static void
|
|
648 big5_detect (struct detection_state *st, const UExtbyte *src,
|
|
649 Bytecount n)
|
|
650 {
|
|
651 struct big5_detector *data = DETECTION_STATE_DATA (st, big5);
|
|
652
|
|
653 while (n--)
|
|
654 {
|
|
655 UExtbyte c = *src++;
|
|
656 if (!data->in_second_byte)
|
|
657 {
|
|
658 if (c >= 0xA1 && c <= 0xFE)
|
|
659 data->in_second_byte = 1;
|
|
660 else if (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO)
|
|
661 data->seen_iso2022_esc = 1;
|
|
662 else if (c >= 0x80)
|
|
663 data->seen_bad_first_byte = 1;
|
|
664 }
|
|
665 else
|
|
666 {
|
|
667 data->in_second_byte = 0;
|
985
|
668 if (c >= 0xA1 && c <= 0xFE)
|
|
669 data->seen_euc_char++;
|
|
670 else if (c >= 0x40 && c <= 0x7E)
|
771
|
671 data->seen_big5_char++;
|
|
672 else
|
|
673 data->seen_bad_second_byte = 1;
|
|
674 }
|
|
675 }
|
|
676
|
|
677 if (data->seen_bad_second_byte)
|
|
678 DET_RESULT (st, big5) = DET_NEARLY_IMPOSSIBLE;
|
|
679 else if (data->seen_bad_first_byte)
|
|
680 DET_RESULT (st, big5) = DET_QUITE_IMPROBABLE;
|
|
681 else if (data->seen_iso2022_esc)
|
|
682 DET_RESULT (st, big5) = DET_SOMEWHAT_UNLIKELY;
|
|
683 else if (data->seen_big5_char >= 4)
|
|
684 DET_RESULT (st, big5) = DET_SOMEWHAT_LIKELY;
|
985
|
685 else if (data->seen_euc_char)
|
|
686 DET_RESULT (st, big5) = DET_SLIGHTLY_LIKELY;
|
771
|
687 else
|
|
688 DET_RESULT (st, big5) = DET_AS_LIKELY_AS_UNLIKELY;
|
|
689 }
|
|
690
|
|
691
|
|
692 /************************************************************************/
|
|
693 /* ISO2022 methods */
|
|
694 /************************************************************************/
|
|
695
|
|
696 /* Any ISO-2022-compliant coding system. Includes JIS, EUC, CTEXT
|
|
697 (Compound Text, the encoding of selections in X Windows). See below for
|
|
698 a complete description of ISO-2022. */
|
|
699
|
|
700 /* Flags indicating what we've seen so far when parsing an
|
|
701 ISO2022 escape sequence. */
|
|
702 enum iso_esc_flag
|
|
703 {
|
|
704 /* Partial sequences */
|
|
705 ISO_ESC_NOTHING, /* Nothing has been seen. */
|
|
706 ISO_ESC, /* We've seen ESC. */
|
|
707 ISO_ESC_2_4, /* We've seen ESC $. This indicates
|
|
708 that we're designating a multi-byte, rather
|
|
709 than a single-byte, character set. */
|
3439
|
710 ISO_ESC_2_5, /* We've seen ESC %. This indicates an escape to a
|
|
711 Unicode coding system; the only one of these
|
|
712 we're prepared to deal with is UTF-8, which has
|
|
713 the next character as G. */
|
771
|
714 ISO_ESC_2_8, /* We've seen ESC 0x28, i.e. ESC (.
|
|
715 This means designate a 94-character
|
|
716 character set into G0. */
|
|
717 ISO_ESC_2_9, /* We've seen ESC 0x29 -- designate a
|
|
718 94-character character set into G1. */
|
|
719 ISO_ESC_2_10, /* We've seen ESC 0x2A. */
|
|
720 ISO_ESC_2_11, /* We've seen ESC 0x2B. */
|
|
721 ISO_ESC_2_12, /* We've seen ESC 0x2C -- designate a
|
|
722 96-character character set into G0.
|
|
723 (This is not ISO2022-standard.
|
|
724 The following 96-character
|
|
725 control sequences are standard,
|
|
726 though.) */
|
|
727 ISO_ESC_2_13, /* We've seen ESC 0x2D -- designate a
|
|
728 96-character character set into G1.
|
|
729 */
|
|
730 ISO_ESC_2_14, /* We've seen ESC 0x2E. */
|
|
731 ISO_ESC_2_15, /* We've seen ESC 0x2F. */
|
|
732 ISO_ESC_2_4_8, /* We've seen ESC $ 0x28 -- designate
|
|
733 a 94^N character set into G0. */
|
|
734 ISO_ESC_2_4_9, /* We've seen ESC $ 0x29. */
|
|
735 ISO_ESC_2_4_10, /* We've seen ESC $ 0x2A. */
|
|
736 ISO_ESC_2_4_11, /* We've seen ESC $ 0x2B. */
|
|
737 ISO_ESC_2_4_12, /* We've seen ESC $ 0x2C. */
|
|
738 ISO_ESC_2_4_13, /* We've seen ESC $ 0x2D. */
|
|
739 ISO_ESC_2_4_14, /* We've seen ESC $ 0x2E. */
|
|
740 ISO_ESC_2_4_15, /* We've seen ESC $ 0x2F. */
|
|
741 ISO_ESC_5_11, /* We've seen ESC [ or 0x9B. This
|
|
742 starts a directionality-control
|
|
743 sequence. The next character
|
|
744 must be 0, 1, 2, or ]. */
|
|
745 ISO_ESC_5_11_0, /* We've seen 0x9B 0. The next character must be ]. */
|
|
746 ISO_ESC_5_11_1, /* We've seen 0x9B 1. The next character must be ]. */
|
|
747 ISO_ESC_5_11_2, /* We've seen 0x9B 2. The next character must be ]. */
|
|
748
|
|
749 /* Full sequences. */
|
|
750 ISO_ESC_START_COMPOSITE, /* Private usage for START COMPOSING */
|
|
751 ISO_ESC_END_COMPOSITE, /* Private usage for END COMPOSING */
|
|
752 ISO_ESC_SINGLE_SHIFT, /* We've seen a complete single-shift sequence. */
|
|
753 ISO_ESC_LOCKING_SHIFT,/* We've seen a complete locking-shift sequence. */
|
|
754 ISO_ESC_DESIGNATE, /* We've seen a complete designation sequence. */
|
|
755 ISO_ESC_DIRECTIONALITY,/* We've seen a complete ISO6429 directionality
|
|
756 sequence. */
|
|
757 ISO_ESC_LITERAL /* We've seen a literal character ala
|
|
758 escape-quoting. */
|
|
759 };
|
|
760
|
|
761 enum iso_error
|
|
762 {
|
|
763 ISO_ERROR_BAD_FINAL,
|
|
764 ISO_ERROR_UNKWOWN_ESC_SEQUENCE,
|
|
765 ISO_ERROR_INVALID_CODE_POINT_CHARACTER,
|
|
766 };
|
|
767
|
|
768
|
|
769 /* Flags indicating current state while converting code. */
|
|
770
|
|
771 /************ Used during encoding and decoding: ************/
|
|
772 /* If set, the current directionality is right-to-left. Otherwise, it's
|
|
773 left-to-right. */
|
|
774 #define ISO_STATE_R2L (1 << 0)
|
|
775
|
|
776 /************ Used during encoding: ************/
|
|
777 /* If set, we just saw a CR. */
|
|
778 #define ISO_STATE_CR (1 << 1)
|
|
779
|
|
780 /************ Used during decoding: ************/
|
|
781 /* If set, we're currently parsing an escape sequence and the upper 16 bits
|
|
782 should be looked at to indicate what partial escape sequence we've seen
|
|
783 so far. Otherwise, we're running through actual text. */
|
|
784 #define ISO_STATE_ESCAPE (1 << 2)
|
|
785 /* If set, G2 is invoked into GL, but only for the next character. */
|
|
786 #define ISO_STATE_SS2 (1 << 3)
|
|
787 /* If set, G3 is invoked into GL, but only for the next character. If both
|
|
788 ISO_STATE_SS2 and ISO_STATE_SS3 are set, ISO_STATE_SS2 overrides; but
|
|
789 this probably indicates an error in the text encoding. */
|
|
790 #define ISO_STATE_SS3 (1 << 4)
|
|
791 /* If set, we're currently processing a composite character (i.e. a
|
|
792 character constructed by overstriking two or more characters). */
|
|
793 #define ISO_STATE_COMPOSITE (1 << 5)
|
|
794
|
3439
|
795 /* If set, we're processing UTF-8 encoded data within ISO-2022
|
|
796 processing. */
|
|
797 #define ISO_STATE_UTF_8 (1 << 6)
|
|
798
|
771
|
799 /* ISO_STATE_LOCK is the mask of flags that remain on until explicitly
|
|
800 turned off when in the ISO2022 encoder/decoder. Other flags are turned
|
|
801 off at the end of processing each character or escape sequence. */
|
|
802 # define ISO_STATE_LOCK \
|
3439
|
803 (ISO_STATE_COMPOSITE | ISO_STATE_R2L | ISO_STATE_UTF_8)
|
771
|
804
|
|
805 typedef struct charset_conversion_spec
|
|
806 {
|
|
807 Lisp_Object from_charset;
|
|
808 Lisp_Object to_charset;
|
|
809 } charset_conversion_spec;
|
|
810
|
|
811 typedef struct
|
|
812 {
|
|
813 Dynarr_declare (charset_conversion_spec);
|
|
814 } charset_conversion_spec_dynarr;
|
|
815
|
|
816 struct iso2022_coding_system
|
|
817 {
|
|
818 /* What are the charsets to be initially designated to G0, G1,
|
|
819 G2, G3? If t, no charset is initially designated. If nil,
|
|
820 no charset is initially designated and no charset is allowed
|
|
821 to be designated. */
|
|
822 Lisp_Object initial_charset[4];
|
|
823
|
|
824 /* If true, a designation escape sequence needs to be sent on output
|
|
825 for the charset in G[0-3] before that charset is used. */
|
|
826 unsigned char force_charset_on_output[4];
|
|
827
|
|
828 charset_conversion_spec_dynarr *input_conv;
|
|
829 charset_conversion_spec_dynarr *output_conv;
|
|
830
|
|
831 unsigned int shoort :1; /* C makes you speak Dutch */
|
|
832 unsigned int no_ascii_eol :1;
|
|
833 unsigned int no_ascii_cntl :1;
|
|
834 unsigned int seven :1;
|
|
835 unsigned int lock_shift :1;
|
|
836 unsigned int no_iso6429 :1;
|
|
837 unsigned int escape_quoted :1;
|
|
838 };
|
|
839
|
|
840 #define CODING_SYSTEM_ISO2022_INITIAL_CHARSET(codesys, g) \
|
|
841 (CODING_SYSTEM_TYPE_DATA (codesys, iso2022)->initial_charset[g])
|
|
842 #define CODING_SYSTEM_ISO2022_FORCE_CHARSET_ON_OUTPUT(codesys, g) \
|
|
843 (CODING_SYSTEM_TYPE_DATA (codesys, iso2022)->force_charset_on_output[g])
|
|
844 #define CODING_SYSTEM_ISO2022_SHORT(codesys) \
|
|
845 (CODING_SYSTEM_TYPE_DATA (codesys, iso2022)->shoort)
|
|
846 #define CODING_SYSTEM_ISO2022_NO_ASCII_EOL(codesys) \
|
|
847 (CODING_SYSTEM_TYPE_DATA (codesys, iso2022)->no_ascii_eol)
|
|
848 #define CODING_SYSTEM_ISO2022_NO_ASCII_CNTL(codesys) \
|
|
849 (CODING_SYSTEM_TYPE_DATA (codesys, iso2022)->no_ascii_cntl)
|
|
850 #define CODING_SYSTEM_ISO2022_SEVEN(codesys) \
|
|
851 (CODING_SYSTEM_TYPE_DATA (codesys, iso2022)->seven)
|
|
852 #define CODING_SYSTEM_ISO2022_LOCK_SHIFT(codesys) \
|
|
853 (CODING_SYSTEM_TYPE_DATA (codesys, iso2022)->lock_shift)
|
|
854 #define CODING_SYSTEM_ISO2022_NO_ISO6429(codesys) \
|
|
855 (CODING_SYSTEM_TYPE_DATA (codesys, iso2022)->no_iso6429)
|
|
856 #define CODING_SYSTEM_ISO2022_ESCAPE_QUOTED(codesys) \
|
|
857 (CODING_SYSTEM_TYPE_DATA (codesys, iso2022)->escape_quoted)
|
|
858 #define CODING_SYSTEM_ISO2022_INPUT_CONV(codesys) \
|
|
859 (CODING_SYSTEM_TYPE_DATA (codesys, iso2022)->input_conv)
|
|
860 #define CODING_SYSTEM_ISO2022_OUTPUT_CONV(codesys) \
|
|
861 (CODING_SYSTEM_TYPE_DATA (codesys, iso2022)->output_conv)
|
|
862
|
|
863 #define XCODING_SYSTEM_ISO2022_INITIAL_CHARSET(codesys, g) \
|
|
864 CODING_SYSTEM_ISO2022_INITIAL_CHARSET (XCODING_SYSTEM (codesys), g)
|
|
865 #define XCODING_SYSTEM_ISO2022_FORCE_CHARSET_ON_OUTPUT(codesys, g) \
|
|
866 CODING_SYSTEM_ISO2022_FORCE_CHARSET_ON_OUTPUT (XCODING_SYSTEM (codesys), g)
|
|
867 #define XCODING_SYSTEM_ISO2022_SHORT(codesys) \
|
|
868 CODING_SYSTEM_ISO2022_SHORT (XCODING_SYSTEM (codesys))
|
|
869 #define XCODING_SYSTEM_ISO2022_NO_ASCII_EOL(codesys) \
|
|
870 CODING_SYSTEM_ISO2022_NO_ASCII_EOL (XCODING_SYSTEM (codesys))
|
|
871 #define XCODING_SYSTEM_ISO2022_NO_ASCII_CNTL(codesys) \
|
|
872 CODING_SYSTEM_ISO2022_NO_ASCII_CNTL (XCODING_SYSTEM (codesys))
|
|
873 #define XCODING_SYSTEM_ISO2022_SEVEN(codesys) \
|
|
874 CODING_SYSTEM_ISO2022_SEVEN (XCODING_SYSTEM (codesys))
|
|
875 #define XCODING_SYSTEM_ISO2022_LOCK_SHIFT(codesys) \
|
|
876 CODING_SYSTEM_ISO2022_LOCK_SHIFT (XCODING_SYSTEM (codesys))
|
|
877 #define XCODING_SYSTEM_ISO2022_NO_ISO6429(codesys) \
|
|
878 CODING_SYSTEM_ISO2022_NO_ISO6429 (XCODING_SYSTEM (codesys))
|
|
879 #define XCODING_SYSTEM_ISO2022_ESCAPE_QUOTED(codesys) \
|
|
880 CODING_SYSTEM_ISO2022_ESCAPE_QUOTED (XCODING_SYSTEM (codesys))
|
|
881 #define XCODING_SYSTEM_ISO2022_INPUT_CONV(codesys) \
|
|
882 CODING_SYSTEM_ISO2022_INPUT_CONV (XCODING_SYSTEM (codesys))
|
|
883 #define XCODING_SYSTEM_ISO2022_OUTPUT_CONV(codesys) \
|
|
884 CODING_SYSTEM_ISO2022_OUTPUT_CONV (XCODING_SYSTEM (codesys))
|
|
885
|
|
886 /* Additional information used by the ISO2022 decoder and detector. */
|
|
887 struct iso2022_coding_stream
|
|
888 {
|
|
889 /* CHARSET holds the character sets currently assigned to the G0
|
|
890 through G3 variables. It is initialized from the array
|
|
891 INITIAL_CHARSET in CODESYS. */
|
|
892 Lisp_Object charset[4];
|
|
893
|
|
894 /* Which registers are currently invoked into the left (GL) and
|
|
895 right (GR) halves of the 8-bit encoding space? */
|
|
896 int register_left, register_right;
|
|
897
|
|
898 /* FLAGS holds flags indicating the current state of the encoding. Some of
|
|
899 these flags are actually part of the state-dependent data and should be
|
|
900 moved there. */
|
|
901 unsigned int flags;
|
|
902
|
|
903 /**************** for decoding ****************/
|
|
904
|
|
905 /* ISO_ESC holds a value indicating part of an escape sequence
|
|
906 that has already been seen. */
|
|
907 enum iso_esc_flag esc;
|
|
908
|
|
909 /* This records the bytes we've seen so far in an escape sequence,
|
|
910 in case the sequence is invalid (we spit out the bytes unchanged). */
|
|
911 unsigned char esc_bytes[8];
|
|
912
|
|
913 /* Index for next byte to store in ISO escape sequence. */
|
|
914 int esc_bytes_index;
|
|
915
|
|
916 #ifdef ENABLE_COMPOSITE_CHARS
|
|
917 /* Stuff seen so far when composing a string. */
|
|
918 unsigned_char_dynarr *composite_chars;
|
|
919 #endif
|
|
920
|
|
921 /* If we saw an invalid designation sequence for a particular
|
|
922 register, we flag it here and switch to ASCII. The next time we
|
|
923 see a valid designation for this register, we turn off the flag
|
|
924 and do the designation normally, but pretend the sequence was
|
|
925 invalid. The effect of all this is that (most of the time) the
|
|
926 escape sequences for both the switch to the unknown charset, and
|
|
927 the switch back to the known charset, get inserted literally into
|
|
928 the buffer and saved out as such. The hope is that we can
|
|
929 preserve the escape sequences so that the resulting written out
|
|
930 file makes sense. If we don't do any of this, the designation
|
|
931 to the invalid charset will be preserved but that switch back
|
|
932 to the known charset will probably get eaten because it was
|
|
933 the same charset that was already present in the register. */
|
|
934 unsigned char invalid_designated[4];
|
|
935
|
|
936 /* We try to do similar things as above for direction-switching
|
|
937 sequences. If we encountered a direction switch while an
|
|
938 invalid designation was present, or an invalid designation
|
|
939 just after a direction switch (i.e. no valid designation
|
|
940 encountered yet), we insert the direction-switch escape
|
|
941 sequence literally into the output stream, and later on
|
|
942 insert the corresponding direction-restoring escape sequence
|
|
943 literally also. */
|
|
944 unsigned int switched_dir_and_no_valid_charset_yet :1;
|
|
945 unsigned int invalid_switch_dir :1;
|
|
946
|
|
947 /* Tells the decoder to output the escape sequence literally
|
|
948 even though it was valid. Used in the games we play to
|
|
949 avoid lossage when we encounter invalid designations. */
|
|
950 unsigned int output_literally :1;
|
|
951 /* We encountered a direction switch followed by an invalid
|
|
952 designation. We didn't output the direction switch
|
|
953 literally because we didn't know about the invalid designation;
|
|
954 but we have to do so now. */
|
|
955 unsigned int output_direction_sequence :1;
|
|
956
|
|
957 /**************** for encoding ****************/
|
|
958
|
|
959 /* Whether we need to explicitly designate the charset in the
|
|
960 G? register before using it. It is initialized from the
|
|
961 array FORCE_CHARSET_ON_OUTPUT in CODESYS. */
|
|
962 unsigned char force_charset_on_output[4];
|
|
963
|
|
964 /* Other state variables that need to be preserved across
|
|
965 invocations. */
|
|
966 Lisp_Object current_charset;
|
|
967 int current_half;
|
|
968 int current_char_boundary;
|
3439
|
969
|
|
970 /* Used for handling UTF-8. */
|
|
971 unsigned char counter;
|
771
|
972 };
|
|
973
|
1204
|
974 static const struct memory_description ccs_description_1[] =
|
771
|
975 {
|
|
976 { XD_LISP_OBJECT, offsetof (charset_conversion_spec, from_charset) },
|
|
977 { XD_LISP_OBJECT, offsetof (charset_conversion_spec, to_charset) },
|
|
978 { XD_END }
|
|
979 };
|
|
980
|
1204
|
981 static const struct sized_memory_description ccs_description =
|
771
|
982 {
|
|
983 sizeof (charset_conversion_spec),
|
|
984 ccs_description_1
|
|
985 };
|
|
986
|
1204
|
987 static const struct memory_description ccsd_description_1[] =
|
771
|
988 {
|
|
989 XD_DYNARR_DESC (charset_conversion_spec_dynarr, &ccs_description),
|
|
990 { XD_END }
|
|
991 };
|
|
992
|
1204
|
993 static const struct sized_memory_description ccsd_description =
|
771
|
994 {
|
|
995 sizeof (charset_conversion_spec_dynarr),
|
|
996 ccsd_description_1
|
|
997 };
|
|
998
|
1204
|
999 static const struct memory_description iso2022_coding_system_description[] = {
|
|
1000 { XD_LISP_OBJECT_ARRAY, offsetof (struct iso2022_coding_system,
|
|
1001 initial_charset), 4 },
|
2367
|
1002 { XD_BLOCK_PTR, offsetof (struct iso2022_coding_system, input_conv),
|
2551
|
1003 1, { &ccsd_description } },
|
2367
|
1004 { XD_BLOCK_PTR, offsetof (struct iso2022_coding_system, output_conv),
|
2551
|
1005 1, { &ccsd_description } },
|
771
|
1006 { XD_END }
|
|
1007 };
|
|
1008
|
1204
|
1009 DEFINE_CODING_SYSTEM_TYPE_WITH_DATA (iso2022);
|
|
1010
|
771
|
1011 /* The following note taken directly from FSF 21.0.103. */
|
|
1012
|
|
1013 /* The following note describes the coding system ISO2022 briefly.
|
|
1014 Since the intention of this note is to help understand the
|
|
1015 functions in this file, some parts are NOT ACCURATE or are OVERLY
|
|
1016 SIMPLIFIED. For thorough understanding, please refer to the
|
|
1017 original document of ISO2022. This is equivalent to the standard
|
|
1018 ECMA-35, obtainable from <URL:http://www.ecma.ch/> (*).
|
|
1019
|
|
1020 ISO2022 provides many mechanisms to encode several character sets
|
|
1021 in 7-bit and 8-bit environments. For 7-bit environments, all text
|
|
1022 is encoded using bytes less than 128. This may make the encoded
|
|
1023 text a little bit longer, but the text passes more easily through
|
|
1024 several types of gateway, some of which strip off the MSB (Most
|
|
1025 Significant Bit).
|
|
1026
|
|
1027 There are two kinds of character sets: control character sets and
|
|
1028 graphic character sets. The former contain control characters such
|
|
1029 as `newline' and `escape' to provide control functions (control
|
|
1030 functions are also provided by escape sequences). The latter
|
|
1031 contain graphic characters such as 'A' and '-'. Emacs recognizes
|
|
1032 two control character sets and many graphic character sets.
|
|
1033
|
|
1034 Graphic character sets are classified into one of the following
|
|
1035 four classes, according to the number of bytes (DIMENSION) and
|
|
1036 number of characters in one dimension (CHARS) of the set:
|
|
1037 - DIMENSION1_CHARS94
|
|
1038 - DIMENSION1_CHARS96
|
|
1039 - DIMENSION2_CHARS94
|
|
1040 - DIMENSION2_CHARS96
|
|
1041
|
|
1042 In addition, each character set is assigned an identification tag,
|
|
1043 unique for each set, called the "final character" (denoted as <F>
|
|
1044 hereafter). The <F> of each character set is decided by ECMA(*)
|
|
1045 when it is registered in ISO. The code range of <F> is 0x30..0x7F
|
|
1046 (0x30..0x3F are for private use only).
|
|
1047
|
|
1048 Note (*): ECMA = European Computer Manufacturers Association
|
|
1049
|
|
1050 Here are examples of graphic character sets [NAME(<F>)]:
|
|
1051 o DIMENSION1_CHARS94 -- ASCII('B'), right-half-of-JISX0201('I'), ...
|
|
1052 o DIMENSION1_CHARS96 -- right-half-of-ISO8859-1('A'), ...
|
|
1053 o DIMENSION2_CHARS94 -- GB2312('A'), JISX0208('B'), ...
|
|
1054 o DIMENSION2_CHARS96 -- none for the moment
|
|
1055
|
|
1056 A code area (1 byte=8 bits) is divided into 4 areas, C0, GL, C1, and GR.
|
|
1057 C0 [0x00..0x1F] -- control character plane 0
|
|
1058 GL [0x20..0x7F] -- graphic character plane 0
|
|
1059 C1 [0x80..0x9F] -- control character plane 1
|
|
1060 GR [0xA0..0xFF] -- graphic character plane 1
|
|
1061
|
|
1062 A control character set is directly designated and invoked to C0 or
|
|
1063 C1 by an escape sequence. The most common case is that:
|
|
1064 - ISO646's control character set is designated/invoked to C0, and
|
|
1065 - ISO6429's control character set is designated/invoked to C1,
|
|
1066 and usually these designations/invocations are omitted in encoded
|
|
1067 text. In a 7-bit environment, only C0 can be used, and a control
|
|
1068 character for C1 is encoded by an appropriate escape sequence to
|
|
1069 fit into the environment. All control characters for C1 are
|
|
1070 defined to have corresponding escape sequences.
|
|
1071
|
|
1072 A graphic character set is at first designated to one of four
|
|
1073 graphic registers (G0 through G3), then these graphic registers are
|
|
1074 invoked to GL or GR. These designations and invocations can be
|
|
1075 done independently. The most common case is that G0 is invoked to
|
|
1076 GL, G1 is invoked to GR, and ASCII is designated to G0. Usually
|
|
1077 these invocations and designations are omitted in encoded text.
|
|
1078 In a 7-bit environment, only GL can be used.
|
|
1079
|
|
1080 When a graphic character set of CHARS94 is invoked to GL, codes
|
|
1081 0x20 and 0x7F of the GL area work as control characters SPACE and
|
|
1082 DEL respectively, and codes 0xA0 and 0xFF of the GR area should not
|
|
1083 be used.
|
|
1084
|
|
1085 There are two ways of invocation: locking-shift and single-shift.
|
|
1086 With locking-shift, the invocation lasts until the next different
|
|
1087 invocation, whereas with single-shift, the invocation affects the
|
|
1088 following character only and doesn't affect the locking-shift
|
|
1089 state. Invocations are done by the following control characters or
|
|
1090 escape sequences:
|
|
1091
|
|
1092 ----------------------------------------------------------------------
|
|
1093 abbrev function cntrl escape seq description
|
|
1094 ----------------------------------------------------------------------
|
|
1095 SI/LS0 (shift-in) 0x0F none invoke G0 into GL
|
|
1096 SO/LS1 (shift-out) 0x0E none invoke G1 into GL
|
|
1097 LS2 (locking-shift-2) none ESC 'n' invoke G2 into GL
|
|
1098 LS3 (locking-shift-3) none ESC 'o' invoke G3 into GL
|
|
1099 LS1R (locking-shift-1 right) none ESC '~' invoke G1 into GR (*)
|
|
1100 LS2R (locking-shift-2 right) none ESC '}' invoke G2 into GR (*)
|
|
1101 LS3R (locking-shift 3 right) none ESC '|' invoke G3 into GR (*)
|
|
1102 SS2 (single-shift-2) 0x8E ESC 'N' invoke G2 for one char
|
|
1103 SS3 (single-shift-3) 0x8F ESC 'O' invoke G3 for one char
|
|
1104 ----------------------------------------------------------------------
|
|
1105 (*) These are not used by any known coding system.
|
|
1106
|
|
1107 Control characters for these functions are defined by macros
|
|
1108 ISO_CODE_XXX in `coding.h'.
|
|
1109
|
|
1110 Designations are done by the following escape sequences:
|
|
1111 ----------------------------------------------------------------------
|
|
1112 escape sequence description
|
|
1113 ----------------------------------------------------------------------
|
|
1114 ESC '(' <F> designate DIMENSION1_CHARS94<F> to G0
|
|
1115 ESC ')' <F> designate DIMENSION1_CHARS94<F> to G1
|
|
1116 ESC '*' <F> designate DIMENSION1_CHARS94<F> to G2
|
|
1117 ESC '+' <F> designate DIMENSION1_CHARS94<F> to G3
|
|
1118 ESC ',' <F> designate DIMENSION1_CHARS96<F> to G0 (*)
|
|
1119 ESC '-' <F> designate DIMENSION1_CHARS96<F> to G1
|
|
1120 ESC '.' <F> designate DIMENSION1_CHARS96<F> to G2
|
|
1121 ESC '/' <F> designate DIMENSION1_CHARS96<F> to G3
|
|
1122 ESC '$' '(' <F> designate DIMENSION2_CHARS94<F> to G0 (**)
|
|
1123 ESC '$' ')' <F> designate DIMENSION2_CHARS94<F> to G1
|
|
1124 ESC '$' '*' <F> designate DIMENSION2_CHARS94<F> to G2
|
|
1125 ESC '$' '+' <F> designate DIMENSION2_CHARS94<F> to G3
|
|
1126 ESC '$' ',' <F> designate DIMENSION2_CHARS96<F> to G0 (*)
|
|
1127 ESC '$' '-' <F> designate DIMENSION2_CHARS96<F> to G1
|
|
1128 ESC '$' '.' <F> designate DIMENSION2_CHARS96<F> to G2
|
|
1129 ESC '$' '/' <F> designate DIMENSION2_CHARS96<F> to G3
|
|
1130 ----------------------------------------------------------------------
|
|
1131
|
|
1132 In this list, "DIMENSION1_CHARS94<F>" means a graphic character set
|
|
1133 of dimension 1, chars 94, and final character <F>, etc...
|
|
1134
|
|
1135 Note (*): Although these designations are not allowed in ISO2022,
|
|
1136 Emacs accepts them on decoding, and produces them on encoding
|
|
1137 CHARS96 character sets in a coding system which is characterized as
|
|
1138 7-bit environment, non-locking-shift, and non-single-shift.
|
|
1139
|
|
1140 Note (**): If <F> is '@', 'A', or 'B', the intermediate character
|
|
1141 '(' can be omitted. We refer to this as "short-form" hereafter.
|
|
1142
|
|
1143 Now you may notice that there are a lot of ways of encoding the
|
|
1144 same multilingual text in ISO2022. Actually, there exist many
|
|
1145 coding systems such as Compound Text (used in X11's inter client
|
|
1146 communication, ISO-2022-JP (used in Japanese Internet), ISO-2022-KR
|
|
1147 (used in Korean Internet), EUC (Extended UNIX Code, used in Asian
|
|
1148 localized platforms), and all of these are variants of ISO2022.
|
|
1149
|
|
1150 In addition to the above, Emacs handles two more kinds of escape
|
|
1151 sequences: ISO6429's direction specification and Emacs' private
|
|
1152 sequence for specifying character composition.
|
|
1153
|
|
1154 ISO6429's direction specification takes the following form:
|
|
1155 o CSI ']' -- end of the current direction
|
|
1156 o CSI '0' ']' -- end of the current direction
|
|
1157 o CSI '1' ']' -- start of left-to-right text
|
|
1158 o CSI '2' ']' -- start of right-to-left text
|
|
1159 The control character CSI (0x9B: control sequence introducer) is
|
|
1160 abbreviated to the escape sequence ESC '[' in a 7-bit environment.
|
|
1161
|
|
1162 Character composition specification takes the following form:
|
|
1163 o ESC '0' -- start relative composition
|
|
1164 o ESC '1' -- end composition
|
|
1165 o ESC '2' -- start rule-base composition (*)
|
|
1166 o ESC '3' -- start relative composition with alternate chars (**)
|
|
1167 o ESC '4' -- start rule-base composition with alternate chars (**)
|
|
1168 Since these are not standard escape sequences of any ISO standard,
|
|
1169 the use of them with these meanings is restricted to Emacs only.
|
|
1170
|
|
1171 (*) This form is used only in Emacs 20.5 and older versions,
|
|
1172 but the newer versions can safely decode it.
|
|
1173 (**) This form is used only in Emacs 21.1 and newer versions,
|
|
1174 and the older versions can't decode it.
|
|
1175
|
|
1176 Here's a list of example usages of these composition escape
|
|
1177 sequences (categorized by `enum composition_method').
|
|
1178
|
|
1179 COMPOSITION_RELATIVE:
|
|
1180 ESC 0 CHAR [ CHAR ] ESC 1
|
|
1181 COMPOSITION_WITH_RULE:
|
|
1182 ESC 2 CHAR [ RULE CHAR ] ESC 1
|
|
1183 COMPOSITION_WITH_ALTCHARS:
|
|
1184 ESC 3 ALTCHAR [ ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1
|
|
1185 COMPOSITION_WITH_RULE_ALTCHARS:
|
|
1186 ESC 4 ALTCHAR [ RULE ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1 */
|
|
1187
|
|
1188 static void
|
|
1189 reset_iso2022_decode (Lisp_Object coding_system,
|
|
1190 struct iso2022_coding_stream *data)
|
|
1191 {
|
|
1192 int i;
|
|
1193 #ifdef ENABLE_COMPOSITE_CHARS
|
|
1194 unsigned_char_dynarr *old_composite_chars = data->composite_chars;
|
|
1195 #endif
|
|
1196
|
|
1197 xzero (*data);
|
|
1198
|
|
1199 for (i = 0; i < 4; i++)
|
|
1200 {
|
|
1201 if (!NILP (coding_system))
|
|
1202 data->charset[i] =
|
|
1203 XCODING_SYSTEM_ISO2022_INITIAL_CHARSET (coding_system, i);
|
|
1204 else
|
|
1205 data->charset[i] = Qt;
|
|
1206 }
|
|
1207 data->esc = ISO_ESC_NOTHING;
|
|
1208 data->register_right = 1;
|
|
1209 #ifdef ENABLE_COMPOSITE_CHARS
|
|
1210 if (old_composite_chars)
|
|
1211 {
|
|
1212 data->composite_chars = old_composite_chars;
|
|
1213 Dynarr_reset (data->composite_chars);
|
|
1214 }
|
|
1215 #endif
|
|
1216 }
|
|
1217
|
|
1218 static void
|
|
1219 reset_iso2022_encode (Lisp_Object coding_system,
|
|
1220 struct iso2022_coding_stream *data)
|
|
1221 {
|
|
1222 int i;
|
|
1223
|
|
1224 xzero (*data);
|
|
1225
|
|
1226 for (i = 0; i < 4; i++)
|
|
1227 {
|
|
1228 data->charset[i] =
|
|
1229 XCODING_SYSTEM_ISO2022_INITIAL_CHARSET (coding_system, i);
|
|
1230 data->force_charset_on_output[i] =
|
|
1231 XCODING_SYSTEM_ISO2022_FORCE_CHARSET_ON_OUTPUT (coding_system, i);
|
|
1232 }
|
|
1233 data->register_right = 1;
|
|
1234 data->current_charset = Qnil;
|
|
1235 data->current_char_boundary = 1;
|
|
1236 }
|
|
1237
|
|
1238 static void
|
|
1239 iso2022_init_coding_stream (struct coding_stream *str)
|
|
1240 {
|
|
1241 if (str->direction == CODING_DECODE)
|
|
1242 reset_iso2022_decode (str->codesys,
|
|
1243 CODING_STREAM_TYPE_DATA (str, iso2022));
|
|
1244 else
|
|
1245 reset_iso2022_encode (str->codesys,
|
|
1246 CODING_STREAM_TYPE_DATA (str, iso2022));
|
|
1247 }
|
|
1248
|
|
1249 static void
|
|
1250 iso2022_rewind_coding_stream (struct coding_stream *str)
|
|
1251 {
|
|
1252 iso2022_init_coding_stream (str);
|
|
1253 }
|
|
1254
|
|
1255 static int
|
|
1256 fit_to_be_escape_quoted (unsigned char c)
|
|
1257 {
|
|
1258 switch (c)
|
|
1259 {
|
|
1260 case ISO_CODE_ESC:
|
|
1261 case ISO_CODE_CSI:
|
|
1262 case ISO_CODE_SS2:
|
|
1263 case ISO_CODE_SS3:
|
|
1264 case ISO_CODE_SO:
|
|
1265 case ISO_CODE_SI:
|
|
1266 return 1;
|
|
1267
|
|
1268 default:
|
|
1269 return 0;
|
|
1270 }
|
|
1271 }
|
|
1272
|
|
1273 static Lisp_Object
|
867
|
1274 charset_by_attributes_or_create_one (int type, Ibyte final, int dir)
|
771
|
1275 {
|
826
|
1276 Lisp_Object charset = charset_by_attributes (type, final, dir);
|
771
|
1277
|
|
1278 if (NILP (charset))
|
|
1279 {
|
|
1280 int chars, dim;
|
|
1281
|
|
1282 switch (type)
|
|
1283 {
|
|
1284 case CHARSET_TYPE_94:
|
|
1285 chars = 94; dim = 1;
|
|
1286 break;
|
|
1287 case CHARSET_TYPE_96:
|
|
1288 chars = 96; dim = 1;
|
|
1289 break;
|
|
1290 case CHARSET_TYPE_94X94:
|
|
1291 chars = 94; dim = 2;
|
|
1292 break;
|
|
1293 case CHARSET_TYPE_96X96:
|
|
1294 chars = 96; dim = 2;
|
|
1295 break;
|
|
1296 default:
|
2500
|
1297 ABORT (); chars = 0; dim = 0;
|
771
|
1298 }
|
|
1299
|
|
1300 charset = Fmake_charset (Qunbound, Qnil,
|
|
1301 nconc2 (list6 (Qfinal, make_char (final),
|
|
1302 Qchars, make_int (chars),
|
|
1303 Qdimension, make_int (dim)),
|
|
1304 list2 (Qdirection,
|
|
1305 dir == CHARSET_LEFT_TO_RIGHT ?
|
|
1306 Ql2r : Qr2l)));
|
|
1307 }
|
|
1308
|
|
1309 return charset;
|
|
1310 }
|
|
1311
|
|
1312 /* Parse one byte of an ISO2022 escape sequence.
|
|
1313 If the result is an invalid escape sequence, return 0 and
|
|
1314 do not change anything in STR. Otherwise, if the result is
|
|
1315 an incomplete escape sequence, update ISO2022.ESC and
|
|
1316 ISO2022.ESC_BYTES and return -1. Otherwise, update
|
|
1317 all the state variables (but not ISO2022.ESC_BYTES) and
|
|
1318 return 1.
|
|
1319
|
|
1320 If CHECK_INVALID_CHARSETS is non-zero, check for designation
|
|
1321 or invocation of an invalid character set and treat that as
|
|
1322 an unrecognized escape sequence.
|
|
1323
|
2367
|
1324 */
|
771
|
1325
|
|
1326 static int
|
|
1327 parse_iso2022_esc (Lisp_Object codesys, struct iso2022_coding_stream *iso,
|
|
1328 unsigned char c, unsigned int *flags,
|
|
1329 int check_invalid_charsets)
|
|
1330 {
|
|
1331 /* (1) If we're at the end of a designation sequence, CS is the
|
|
1332 charset being designated and REG is the register to designate
|
|
1333 it to.
|
|
1334
|
|
1335 (2) If we're at the end of a locking-shift sequence, REG is
|
|
1336 the register to invoke and HALF (0 == left, 1 == right) is
|
|
1337 the half to invoke it into.
|
|
1338
|
|
1339 (3) If we're at the end of a single-shift sequence, REG is
|
|
1340 the register to invoke. */
|
|
1341 Lisp_Object cs = Qnil;
|
|
1342 int reg, half;
|
|
1343
|
|
1344 /* NOTE: This code does goto's all over the fucking place.
|
|
1345 The reason for this is that we're basically implementing
|
|
1346 a state machine here, and hierarchical languages like C
|
|
1347 don't really provide a clean way of doing this. */
|
|
1348
|
|
1349 if (! (*flags & ISO_STATE_ESCAPE))
|
|
1350 /* At beginning of escape sequence; we need to reset our
|
|
1351 escape-state variables. */
|
|
1352 iso->esc = ISO_ESC_NOTHING;
|
|
1353
|
|
1354 iso->output_literally = 0;
|
|
1355 iso->output_direction_sequence = 0;
|
|
1356
|
|
1357 switch (iso->esc)
|
|
1358 {
|
|
1359 case ISO_ESC_NOTHING:
|
|
1360 iso->esc_bytes_index = 0;
|
|
1361 switch (c)
|
|
1362 {
|
|
1363 case ISO_CODE_ESC: /* Start escape sequence */
|
|
1364 *flags |= ISO_STATE_ESCAPE;
|
|
1365 iso->esc = ISO_ESC;
|
|
1366 goto not_done;
|
|
1367
|
|
1368 case ISO_CODE_CSI: /* ISO6429 (specifying directionality) */
|
|
1369 *flags |= ISO_STATE_ESCAPE;
|
|
1370 iso->esc = ISO_ESC_5_11;
|
|
1371 goto not_done;
|
|
1372
|
|
1373 case ISO_CODE_SO: /* locking shift 1 */
|
|
1374 reg = 1; half = 0;
|
|
1375 goto locking_shift;
|
|
1376 case ISO_CODE_SI: /* locking shift 0 */
|
|
1377 reg = 0; half = 0;
|
|
1378 goto locking_shift;
|
|
1379
|
|
1380 case ISO_CODE_SS2: /* single shift */
|
|
1381 reg = 2;
|
|
1382 goto single_shift;
|
|
1383 case ISO_CODE_SS3: /* single shift */
|
|
1384 reg = 3;
|
|
1385 goto single_shift;
|
|
1386
|
|
1387 default: /* Other control characters */
|
|
1388 error:
|
|
1389 *flags &= ISO_STATE_LOCK;
|
|
1390 return 0;
|
|
1391 }
|
|
1392
|
|
1393 case ISO_ESC:
|
3439
|
1394
|
|
1395 /* The only available ISO 2022 sequence in UTF-8 mode is ESC % @, to
|
|
1396 exit from it. If we see any other escape sequence, pass it through
|
|
1397 in the error handler. */
|
|
1398 if (*flags & ISO_STATE_UTF_8 && '%' != c)
|
|
1399 {
|
|
1400 return 0;
|
|
1401 }
|
|
1402
|
771
|
1403 switch (c)
|
|
1404 {
|
|
1405 /**** single shift ****/
|
|
1406
|
|
1407 case 'N': /* single shift 2 */
|
|
1408 reg = 2;
|
|
1409 goto single_shift;
|
|
1410 case 'O': /* single shift 3 */
|
|
1411 reg = 3;
|
|
1412 goto single_shift;
|
|
1413
|
|
1414 /**** locking shift ****/
|
|
1415
|
|
1416 case '~': /* locking shift 1 right */
|
|
1417 reg = 1; half = 1;
|
|
1418 goto locking_shift;
|
|
1419 case 'n': /* locking shift 2 */
|
|
1420 reg = 2; half = 0;
|
|
1421 goto locking_shift;
|
|
1422 case '}': /* locking shift 2 right */
|
|
1423 reg = 2; half = 1;
|
|
1424 goto locking_shift;
|
|
1425 case 'o': /* locking shift 3 */
|
|
1426 reg = 3; half = 0;
|
|
1427 goto locking_shift;
|
|
1428 case '|': /* locking shift 3 right */
|
|
1429 reg = 3; half = 1;
|
|
1430 goto locking_shift;
|
|
1431
|
|
1432 /**** composite ****/
|
|
1433
|
|
1434 #ifdef ENABLE_COMPOSITE_CHARS
|
|
1435 case '0':
|
|
1436 iso->esc = ISO_ESC_START_COMPOSITE;
|
|
1437 *flags = (*flags & ISO_STATE_LOCK) |
|
|
1438 ISO_STATE_COMPOSITE;
|
|
1439 return 1;
|
|
1440
|
|
1441 case '1':
|
|
1442 iso->esc = ISO_ESC_END_COMPOSITE;
|
|
1443 *flags = (*flags & ISO_STATE_LOCK) &
|
|
1444 ~ISO_STATE_COMPOSITE;
|
|
1445 return 1;
|
|
1446 #else
|
|
1447 case '0': case '1': case '2': case '3': case '4':
|
|
1448 /* We simply return a flag indicating that some composite
|
|
1449 escape was seen. The caller will use the particular
|
|
1450 character to encode the appropriate "composite hack"
|
|
1451 character out of Vcharset_composite, so that we will
|
|
1452 preserve these values on output. */
|
|
1453 iso->esc = ISO_ESC_START_COMPOSITE;
|
|
1454 *flags &= ISO_STATE_LOCK;
|
|
1455 return 1;
|
|
1456 #endif /* ENABLE_COMPOSITE_CHARS */
|
|
1457
|
|
1458 /**** directionality ****/
|
|
1459
|
|
1460 case '[':
|
|
1461 iso->esc = ISO_ESC_5_11;
|
|
1462 goto not_done;
|
|
1463
|
|
1464 /**** designation ****/
|
|
1465
|
|
1466 case '$': /* multibyte charset prefix */
|
|
1467 iso->esc = ISO_ESC_2_4;
|
|
1468 goto not_done;
|
|
1469
|
3439
|
1470 case '%': /* Prefix to an escape to or from Unicode. */
|
|
1471 iso->esc = ISO_ESC_2_5;
|
|
1472 goto not_done;
|
|
1473
|
771
|
1474 default:
|
|
1475 if (0x28 <= c && c <= 0x2F)
|
|
1476 {
|
|
1477 iso->esc = (enum iso_esc_flag) (c - 0x28 + ISO_ESC_2_8);
|
|
1478 goto not_done;
|
|
1479 }
|
|
1480
|
|
1481 /* This function is called with CODESYS equal to nil when
|
|
1482 doing coding-system detection. */
|
|
1483 if (!NILP (codesys)
|
|
1484 && XCODING_SYSTEM_ISO2022_ESCAPE_QUOTED (codesys)
|
|
1485 && fit_to_be_escape_quoted (c))
|
|
1486 {
|
|
1487 iso->esc = ISO_ESC_LITERAL;
|
|
1488 *flags &= ISO_STATE_LOCK;
|
|
1489 return 1;
|
|
1490 }
|
|
1491
|
|
1492 /* bzzzt! */
|
|
1493 goto error;
|
|
1494 }
|
|
1495
|
3439
|
1496 /* ISO-IR 196 UTF-8 support. */
|
|
1497 case ISO_ESC_2_5:
|
|
1498 if ('G' == c)
|
|
1499 {
|
|
1500 /* Activate UTF-8 mode. */
|
|
1501 *flags &= ISO_STATE_LOCK;
|
|
1502 *flags |= ISO_STATE_UTF_8;
|
|
1503 iso->esc = ISO_ESC_NOTHING;
|
|
1504 return 1;
|
|
1505 }
|
|
1506 else if ('@' == c)
|
|
1507 {
|
|
1508 /* Deactive UTF-8 mode. */
|
|
1509 *flags &= ISO_STATE_LOCK;
|
|
1510 *flags &= ~(ISO_STATE_UTF_8);
|
|
1511 iso->esc = ISO_ESC_NOTHING;
|
|
1512 return 1;
|
|
1513 }
|
|
1514 else
|
|
1515 {
|
|
1516 /* Oops, we don't support the other UTF-? coding systems within
|
|
1517 ISO 2022, only in their own context. */
|
|
1518 goto error;
|
|
1519 }
|
771
|
1520 /**** directionality ****/
|
|
1521
|
|
1522 case ISO_ESC_5_11: /* ISO6429 direction control */
|
|
1523 if (c == ']')
|
|
1524 {
|
|
1525 *flags &= (ISO_STATE_LOCK & ~ISO_STATE_R2L);
|
|
1526 goto directionality;
|
|
1527 }
|
|
1528 if (c == '0') iso->esc = ISO_ESC_5_11_0;
|
|
1529 else if (c == '1') iso->esc = ISO_ESC_5_11_1;
|
|
1530 else if (c == '2') iso->esc = ISO_ESC_5_11_2;
|
|
1531 else goto error;
|
|
1532 goto not_done;
|
|
1533
|
|
1534 case ISO_ESC_5_11_0:
|
|
1535 if (c == ']')
|
|
1536 {
|
|
1537 *flags &= (ISO_STATE_LOCK & ~ISO_STATE_R2L);
|
|
1538 goto directionality;
|
|
1539 }
|
|
1540 goto error;
|
|
1541
|
|
1542 case ISO_ESC_5_11_1:
|
|
1543 if (c == ']')
|
|
1544 {
|
|
1545 *flags = (ISO_STATE_LOCK & ~ISO_STATE_R2L);
|
|
1546 goto directionality;
|
|
1547 }
|
|
1548 goto error;
|
|
1549
|
|
1550 case ISO_ESC_5_11_2:
|
|
1551 if (c == ']')
|
|
1552 {
|
|
1553 *flags = (*flags & ISO_STATE_LOCK) | ISO_STATE_R2L;
|
|
1554 goto directionality;
|
|
1555 }
|
|
1556 goto error;
|
|
1557
|
|
1558 directionality:
|
|
1559 iso->esc = ISO_ESC_DIRECTIONALITY;
|
|
1560 /* Various junk here to attempt to preserve the direction sequences
|
|
1561 literally in the text if they would otherwise be swallowed due
|
|
1562 to invalid designations that don't show up as actual charset
|
|
1563 changes in the text. */
|
|
1564 if (iso->invalid_switch_dir)
|
|
1565 {
|
|
1566 /* We already inserted a direction switch literally into the
|
|
1567 text. We assume (#### this may not be right) that the
|
|
1568 next direction switch is the one going the other way,
|
|
1569 and we need to output that literally as well. */
|
|
1570 iso->output_literally = 1;
|
|
1571 iso->invalid_switch_dir = 0;
|
|
1572 }
|
|
1573 else
|
|
1574 {
|
|
1575 int jj;
|
|
1576
|
|
1577 /* If we are in the thrall of an invalid designation,
|
|
1578 then stick the directionality sequence literally into the
|
|
1579 output stream so it ends up in the original text again. */
|
|
1580 for (jj = 0; jj < 4; jj++)
|
|
1581 if (iso->invalid_designated[jj])
|
|
1582 break;
|
|
1583 if (jj < 4)
|
|
1584 {
|
|
1585 iso->output_literally = 1;
|
|
1586 iso->invalid_switch_dir = 1;
|
|
1587 }
|
|
1588 else
|
|
1589 /* Indicate that we haven't yet seen a valid designation,
|
|
1590 so that if a switch-dir is directly followed by an
|
|
1591 invalid designation, both get inserted literally. */
|
|
1592 iso->switched_dir_and_no_valid_charset_yet = 1;
|
|
1593 }
|
|
1594 return 1;
|
|
1595
|
|
1596
|
|
1597 /**** designation ****/
|
|
1598
|
|
1599 case ISO_ESC_2_4:
|
|
1600 if (0x28 <= c && c <= 0x2F)
|
|
1601 {
|
|
1602 iso->esc = (enum iso_esc_flag) (c - 0x28 + ISO_ESC_2_4_8);
|
|
1603 goto not_done;
|
|
1604 }
|
|
1605 if (0x40 <= c && c <= 0x42)
|
|
1606 {
|
|
1607 cs = charset_by_attributes_or_create_one (CHARSET_TYPE_94X94, c,
|
|
1608 *flags & ISO_STATE_R2L ?
|
|
1609 CHARSET_RIGHT_TO_LEFT :
|
|
1610 CHARSET_LEFT_TO_RIGHT);
|
|
1611 reg = 0;
|
|
1612 goto designated;
|
|
1613 }
|
|
1614 goto error;
|
|
1615
|
|
1616 default:
|
|
1617 {
|
|
1618 int type = -1;
|
|
1619
|
|
1620 if (iso->esc >= ISO_ESC_2_8 &&
|
|
1621 iso->esc <= ISO_ESC_2_15)
|
|
1622 {
|
|
1623 type = ((iso->esc >= ISO_ESC_2_12) ?
|
|
1624 CHARSET_TYPE_96 : CHARSET_TYPE_94);
|
|
1625 reg = (iso->esc - ISO_ESC_2_8) & 3;
|
|
1626 }
|
|
1627 else if (iso->esc >= ISO_ESC_2_4_8 &&
|
|
1628 iso->esc <= ISO_ESC_2_4_15)
|
|
1629 {
|
|
1630 type = ((iso->esc >= ISO_ESC_2_4_12) ?
|
|
1631 CHARSET_TYPE_96X96 : CHARSET_TYPE_94X94);
|
|
1632 reg = (iso->esc - ISO_ESC_2_4_8) & 3;
|
|
1633 }
|
|
1634 else
|
|
1635 {
|
|
1636 /* Can this ever be reached? -slb */
|
2500
|
1637 ABORT ();
|
771
|
1638 goto error;
|
|
1639 }
|
|
1640
|
|
1641 if (c < '0' || c > '~' ||
|
|
1642 (c > 0x5F && (type == CHARSET_TYPE_94X94 ||
|
|
1643 type == CHARSET_TYPE_96X96)))
|
|
1644 goto error; /* bad final byte */
|
|
1645
|
|
1646 cs = charset_by_attributes_or_create_one (type, c,
|
|
1647 *flags & ISO_STATE_R2L ?
|
|
1648 CHARSET_RIGHT_TO_LEFT :
|
|
1649 CHARSET_LEFT_TO_RIGHT);
|
|
1650 goto designated;
|
|
1651 }
|
|
1652 }
|
|
1653
|
|
1654 not_done:
|
|
1655 iso->esc_bytes[iso->esc_bytes_index++] = (unsigned char) c;
|
|
1656 return -1;
|
|
1657
|
|
1658 single_shift:
|
|
1659 if (check_invalid_charsets && !CHARSETP (iso->charset[reg]))
|
|
1660 /* can't invoke something that ain't there. */
|
|
1661 goto error;
|
|
1662 iso->esc = ISO_ESC_SINGLE_SHIFT;
|
|
1663 *flags &= ISO_STATE_LOCK;
|
|
1664 if (reg == 2)
|
|
1665 *flags |= ISO_STATE_SS2;
|
|
1666 else
|
|
1667 *flags |= ISO_STATE_SS3;
|
|
1668 return 1;
|
|
1669
|
|
1670 locking_shift:
|
|
1671 if (check_invalid_charsets &&
|
|
1672 !CHARSETP (iso->charset[reg]))
|
|
1673 /* can't invoke something that ain't there. */
|
|
1674 goto error;
|
|
1675 if (half)
|
|
1676 iso->register_right = reg;
|
|
1677 else
|
|
1678 iso->register_left = reg;
|
|
1679 *flags &= ISO_STATE_LOCK;
|
|
1680 iso->esc = ISO_ESC_LOCKING_SHIFT;
|
|
1681 return 1;
|
|
1682
|
|
1683 designated:
|
|
1684 if (NILP (cs) && check_invalid_charsets)
|
|
1685 {
|
2500
|
1686 ABORT ();
|
771
|
1687 /* #### This should never happen now that we automatically create
|
|
1688 temporary charsets as necessary. We should probably remove
|
|
1689 this code. --ben */
|
|
1690 iso->invalid_designated[reg] = 1;
|
|
1691 iso->charset[reg] = Vcharset_ascii;
|
|
1692 iso->esc = ISO_ESC_DESIGNATE;
|
|
1693 *flags &= ISO_STATE_LOCK;
|
|
1694 iso->output_literally = 1;
|
|
1695 if (iso->switched_dir_and_no_valid_charset_yet)
|
|
1696 {
|
|
1697 /* We encountered a switch-direction followed by an
|
|
1698 invalid designation. Ensure that the switch-direction
|
|
1699 gets outputted; otherwise it will probably get eaten
|
|
1700 when the text is written out again. */
|
|
1701 iso->switched_dir_and_no_valid_charset_yet = 0;
|
|
1702 iso->output_direction_sequence = 1;
|
|
1703 /* And make sure that the switch-dir going the other
|
|
1704 way gets outputted, as well. */
|
|
1705 iso->invalid_switch_dir = 1;
|
|
1706 }
|
|
1707 return 1;
|
|
1708 }
|
|
1709 /* This function is called with CODESYS equal to nil when
|
|
1710 doing coding-system detection. */
|
|
1711 if (!NILP (codesys))
|
|
1712 {
|
|
1713 charset_conversion_spec_dynarr *dyn =
|
|
1714 XCODING_SYSTEM_ISO2022_INPUT_CONV (codesys);
|
|
1715
|
|
1716 if (dyn)
|
|
1717 {
|
|
1718 int i;
|
|
1719
|
|
1720 for (i = 0; i < Dynarr_length (dyn); i++)
|
|
1721 {
|
|
1722 struct charset_conversion_spec *spec = Dynarr_atp (dyn, i);
|
|
1723 if (EQ (cs, spec->from_charset))
|
|
1724 cs = spec->to_charset;
|
|
1725 }
|
|
1726 }
|
|
1727 }
|
|
1728
|
|
1729 iso->charset[reg] = cs;
|
|
1730 iso->esc = ISO_ESC_DESIGNATE;
|
|
1731 *flags &= ISO_STATE_LOCK;
|
|
1732 if (iso->invalid_designated[reg])
|
|
1733 {
|
|
1734 iso->invalid_designated[reg] = 0;
|
|
1735 iso->output_literally = 1;
|
|
1736 }
|
|
1737 if (iso->switched_dir_and_no_valid_charset_yet)
|
|
1738 iso->switched_dir_and_no_valid_charset_yet = 0;
|
|
1739 return 1;
|
|
1740 }
|
|
1741
|
|
1742 /* If FLAGS is a null pointer or specifies right-to-left motion,
|
|
1743 output a switch-dir-to-left-to-right sequence to DST.
|
|
1744 Also update FLAGS if it is not a null pointer.
|
|
1745 If INTERNAL_P is set, we are outputting in internal format and
|
|
1746 need to handle the CSI differently. */
|
|
1747
|
|
1748 static void
|
|
1749 restore_left_to_right_direction (Lisp_Object codesys,
|
|
1750 unsigned_char_dynarr *dst,
|
|
1751 unsigned int *flags,
|
|
1752 int internal_p)
|
|
1753 {
|
|
1754 if (!flags || (*flags & ISO_STATE_R2L))
|
|
1755 {
|
|
1756 if (XCODING_SYSTEM_ISO2022_SEVEN (codesys))
|
|
1757 {
|
|
1758 Dynarr_add (dst, ISO_CODE_ESC);
|
|
1759 Dynarr_add (dst, '[');
|
|
1760 }
|
|
1761 else if (internal_p)
|
|
1762 DECODE_ADD_BINARY_CHAR (ISO_CODE_CSI, dst);
|
|
1763 else
|
|
1764 Dynarr_add (dst, ISO_CODE_CSI);
|
|
1765 Dynarr_add (dst, '0');
|
|
1766 Dynarr_add (dst, ']');
|
|
1767 if (flags)
|
|
1768 *flags &= ~ISO_STATE_R2L;
|
|
1769 }
|
|
1770 }
|
|
1771
|
|
1772 /* If FLAGS is a null pointer or specifies a direction different from
|
|
1773 DIRECTION (which should be either CHARSET_RIGHT_TO_LEFT or
|
|
1774 CHARSET_LEFT_TO_RIGHT), output the appropriate switch-dir escape
|
|
1775 sequence to DST. Also update FLAGS if it is not a null pointer.
|
|
1776 If INTERNAL_P is set, we are outputting in internal format and
|
|
1777 need to handle the CSI differently. */
|
|
1778
|
|
1779 static void
|
|
1780 ensure_correct_direction (int direction, Lisp_Object codesys,
|
|
1781 unsigned_char_dynarr *dst, unsigned int *flags,
|
|
1782 int internal_p)
|
|
1783 {
|
|
1784 if ((!flags || (*flags & ISO_STATE_R2L)) &&
|
|
1785 direction == CHARSET_LEFT_TO_RIGHT)
|
|
1786 restore_left_to_right_direction (codesys, dst, flags, internal_p);
|
|
1787 else if (!XCODING_SYSTEM_ISO2022_NO_ISO6429 (codesys)
|
|
1788 && (!flags || !(*flags & ISO_STATE_R2L)) &&
|
|
1789 direction == CHARSET_RIGHT_TO_LEFT)
|
|
1790 {
|
|
1791 if (XCODING_SYSTEM_ISO2022_SEVEN (codesys))
|
|
1792 {
|
|
1793 Dynarr_add (dst, ISO_CODE_ESC);
|
|
1794 Dynarr_add (dst, '[');
|
|
1795 }
|
|
1796 else if (internal_p)
|
|
1797 DECODE_ADD_BINARY_CHAR (ISO_CODE_CSI, dst);
|
|
1798 else
|
|
1799 Dynarr_add (dst, ISO_CODE_CSI);
|
|
1800 Dynarr_add (dst, '2');
|
|
1801 Dynarr_add (dst, ']');
|
|
1802 if (flags)
|
|
1803 *flags |= ISO_STATE_R2L;
|
|
1804 }
|
|
1805 }
|
|
1806
|
|
1807 /* Convert ISO2022-format data to internal format. */
|
|
1808
|
|
1809 static Bytecount
|
|
1810 iso2022_decode (struct coding_stream *str, const UExtbyte *src,
|
|
1811 unsigned_char_dynarr *dst, Bytecount n)
|
|
1812 {
|
|
1813 unsigned int ch = str->ch;
|
|
1814 #ifdef ENABLE_COMPOSITE_CHARS
|
|
1815 unsigned_char_dynarr *real_dst = dst;
|
|
1816 #endif
|
|
1817 struct iso2022_coding_stream *data =
|
|
1818 CODING_STREAM_TYPE_DATA (str, iso2022);
|
|
1819 unsigned int flags = data->flags;
|
|
1820 Bytecount orign = n;
|
|
1821
|
|
1822 #ifdef ENABLE_COMPOSITE_CHARS
|
|
1823 if (flags & ISO_STATE_COMPOSITE)
|
|
1824 dst = data->composite_chars;
|
|
1825 #endif /* ENABLE_COMPOSITE_CHARS */
|
|
1826
|
|
1827 while (n--)
|
|
1828 {
|
|
1829 UExtbyte c = *src++;
|
|
1830 if (flags & ISO_STATE_ESCAPE)
|
|
1831 { /* Within ESC sequence */
|
|
1832 int retval = parse_iso2022_esc (str->codesys, data,
|
|
1833 c, &flags, 1);
|
|
1834
|
|
1835 if (retval)
|
|
1836 {
|
|
1837 switch (data->esc)
|
|
1838 {
|
|
1839 #ifdef ENABLE_COMPOSITE_CHARS
|
|
1840 case ISO_ESC_START_COMPOSITE:
|
|
1841 if (data->composite_chars)
|
|
1842 Dynarr_reset (data->composite_chars);
|
|
1843 else
|
|
1844 data->composite_chars = Dynarr_new (unsigned_char);
|
|
1845 dst = data->composite_chars;
|
|
1846 break;
|
|
1847 case ISO_ESC_END_COMPOSITE:
|
|
1848 {
|
867
|
1849 Ibyte comstr[MAX_ICHAR_LEN];
|
771
|
1850 Bytecount len;
|
867
|
1851 Ichar emch = lookup_composite_char (Dynarr_atp (dst, 0),
|
771
|
1852 Dynarr_length (dst));
|
|
1853 dst = real_dst;
|
867
|
1854 len = set_itext_ichar (comstr, emch);
|
771
|
1855 Dynarr_add_many (dst, comstr, len);
|
|
1856 break;
|
|
1857 }
|
|
1858 #else
|
|
1859 case ISO_ESC_START_COMPOSITE:
|
|
1860 {
|
867
|
1861 Ibyte comstr[MAX_ICHAR_LEN];
|
771
|
1862 Bytecount len;
|
867
|
1863 Ichar emch = make_ichar (Vcharset_composite, c - '0' + ' ',
|
771
|
1864 0);
|
867
|
1865 len = set_itext_ichar (comstr, emch);
|
771
|
1866 Dynarr_add_many (dst, comstr, len);
|
|
1867 break;
|
|
1868 }
|
|
1869 #endif /* ENABLE_COMPOSITE_CHARS */
|
|
1870
|
|
1871 case ISO_ESC_LITERAL:
|
|
1872 DECODE_ADD_BINARY_CHAR (c, dst);
|
|
1873 break;
|
|
1874
|
|
1875 default:
|
|
1876 /* Everything else handled already */
|
|
1877 break;
|
|
1878 }
|
|
1879 }
|
|
1880
|
|
1881 /* Attempted error recovery. */
|
|
1882 if (data->output_direction_sequence)
|
|
1883 ensure_correct_direction (flags & ISO_STATE_R2L ?
|
|
1884 CHARSET_RIGHT_TO_LEFT :
|
|
1885 CHARSET_LEFT_TO_RIGHT,
|
|
1886 str->codesys, dst, 0, 1);
|
|
1887 /* More error recovery. */
|
|
1888 if (!retval || data->output_literally)
|
|
1889 {
|
|
1890 /* Output the (possibly invalid) sequence */
|
|
1891 int i;
|
|
1892 for (i = 0; i < data->esc_bytes_index; i++)
|
|
1893 DECODE_ADD_BINARY_CHAR (data->esc_bytes[i], dst);
|
|
1894 flags &= ISO_STATE_LOCK;
|
|
1895 if (!retval)
|
|
1896 n++, src--;/* Repeat the loop with the same character. */
|
|
1897 else
|
|
1898 {
|
|
1899 /* No sense in reprocessing the final byte of the
|
|
1900 escape sequence; it could mess things up anyway.
|
|
1901 Just add it now. */
|
|
1902 DECODE_ADD_BINARY_CHAR (c, dst);
|
|
1903 }
|
|
1904 }
|
|
1905 ch = 0;
|
|
1906 }
|
3439
|
1907 else if (flags & ISO_STATE_UTF_8)
|
|
1908 {
|
|
1909 unsigned char counter = data->counter;
|
|
1910 Ibyte work[MAX_ICHAR_LEN];
|
|
1911 int len;
|
|
1912 Lisp_Object chr;
|
|
1913
|
|
1914 if (ISO_CODE_ESC == c)
|
|
1915 {
|
|
1916 /* Allow the escape sequence parser to end the UTF-8 state. */
|
|
1917 flags |= ISO_STATE_ESCAPE;
|
|
1918 data->esc = ISO_ESC;
|
|
1919 data->esc_bytes_index = 1;
|
|
1920 continue;
|
|
1921 }
|
|
1922
|
|
1923 switch (counter)
|
|
1924 {
|
|
1925 case 0:
|
|
1926 if (c >= 0xfc)
|
|
1927 {
|
|
1928 ch = c & 0x01;
|
|
1929 counter = 5;
|
|
1930 }
|
|
1931 else if (c >= 0xf8)
|
|
1932 {
|
|
1933 ch = c & 0x03;
|
|
1934 counter = 4;
|
|
1935 }
|
|
1936 else if (c >= 0xf0)
|
|
1937 {
|
|
1938 ch = c & 0x07;
|
|
1939 counter = 3;
|
|
1940 }
|
|
1941 else if (c >= 0xe0)
|
|
1942 {
|
|
1943 ch = c & 0x0f;
|
|
1944 counter = 2;
|
|
1945 }
|
|
1946 else if (c >= 0xc0)
|
|
1947 {
|
|
1948 ch = c & 0x1f;
|
|
1949 counter = 1;
|
|
1950 }
|
|
1951 else
|
3695
|
1952 /* ASCII, or the lower control characters.
|
|
1953
|
|
1954 Perhaps we should signal an error if the character is in
|
|
1955 the range 0x80-0xc0; this is illegal UTF-8. */
|
|
1956 Dynarr_add (dst, (c & 0x7f));
|
3439
|
1957
|
|
1958 break;
|
|
1959 case 1:
|
|
1960 ch = (ch << 6) | (c & 0x3f);
|
|
1961 chr = Funicode_to_char(make_int(ch), Qnil);
|
|
1962
|
|
1963 if (!NILP (chr))
|
|
1964 {
|
|
1965 assert(CHARP(chr));
|
|
1966 len = set_itext_ichar (work, XCHAR(chr));
|
|
1967 Dynarr_add_many (dst, work, len);
|
|
1968 }
|
|
1969 else
|
|
1970 {
|
|
1971 /* Shouldn't happen, this code should only be enabled in
|
|
1972 XEmacsen with support for all of Unicode. */
|
|
1973 Dynarr_add (dst, LEADING_BYTE_JAPANESE_JISX0208);
|
|
1974 Dynarr_add (dst, 34 + 128);
|
|
1975 Dynarr_add (dst, 46 + 128);
|
|
1976 }
|
|
1977
|
|
1978 ch = 0;
|
|
1979 counter = 0;
|
|
1980 break;
|
|
1981 default:
|
|
1982 ch = (ch << 6) | (c & 0x3f);
|
|
1983 counter--;
|
|
1984 }
|
|
1985
|
|
1986 if (str->eof)
|
|
1987 DECODE_OUTPUT_PARTIAL_CHAR (ch, dst);
|
|
1988
|
|
1989 data->counter = counter;
|
|
1990 }
|
826
|
1991 else if (byte_c0_p (c) || byte_c1_p (c))
|
771
|
1992 { /* Control characters */
|
|
1993
|
|
1994 /***** Error-handling *****/
|
|
1995
|
|
1996 /* If we were in the middle of a character, dump out the
|
|
1997 partial character. */
|
|
1998 DECODE_OUTPUT_PARTIAL_CHAR (ch, dst);
|
|
1999
|
|
2000 /* If we just saw a single-shift character, dump it out.
|
|
2001 This may dump out the wrong sort of single-shift character,
|
|
2002 but least it will give an indication that something went
|
|
2003 wrong. */
|
|
2004 if (flags & ISO_STATE_SS2)
|
|
2005 {
|
|
2006 DECODE_ADD_BINARY_CHAR (ISO_CODE_SS2, dst);
|
|
2007 flags &= ~ISO_STATE_SS2;
|
|
2008 }
|
|
2009 if (flags & ISO_STATE_SS3)
|
|
2010 {
|
|
2011 DECODE_ADD_BINARY_CHAR (ISO_CODE_SS3, dst);
|
|
2012 flags &= ~ISO_STATE_SS3;
|
|
2013 }
|
|
2014
|
|
2015 /***** Now handle the control characters. *****/
|
|
2016
|
|
2017 flags &= ISO_STATE_LOCK;
|
|
2018
|
|
2019 if (!parse_iso2022_esc (str->codesys, data, c, &flags, 1))
|
|
2020 DECODE_ADD_BINARY_CHAR (c, dst);
|
|
2021 }
|
|
2022 else
|
|
2023 { /* Graphic characters */
|
|
2024 Lisp_Object charset;
|
|
2025 int lb;
|
|
2026 int reg;
|
|
2027
|
|
2028 /* Now determine the charset. */
|
|
2029 reg = ((flags & ISO_STATE_SS2) ? 2
|
|
2030 : (flags & ISO_STATE_SS3) ? 3
|
826
|
2031 : !byte_ascii_p (c) ? data->register_right
|
771
|
2032 : data->register_left);
|
|
2033 charset = data->charset[reg];
|
|
2034
|
|
2035 /* Error checking: */
|
|
2036 if (! CHARSETP (charset)
|
|
2037 || data->invalid_designated[reg]
|
|
2038 || (((c & 0x7F) == ' ' || (c & 0x7F) == ISO_CODE_DEL)
|
|
2039 && XCHARSET_CHARS (charset) == 94))
|
|
2040 /* Mrmph. We are trying to invoke a register that has no
|
|
2041 or an invalid charset in it, or trying to add a character
|
|
2042 outside the range of the charset. Insert that char literally
|
|
2043 to preserve it for the output. */
|
|
2044 {
|
|
2045 DECODE_OUTPUT_PARTIAL_CHAR (ch, dst);
|
|
2046 DECODE_ADD_BINARY_CHAR (c, dst);
|
|
2047 }
|
|
2048
|
|
2049 else
|
|
2050 {
|
|
2051 /* Things are probably hunky-dorey. */
|
|
2052
|
|
2053 /* Fetch reverse charset, maybe. */
|
|
2054 if (((flags & ISO_STATE_R2L) &&
|
|
2055 XCHARSET_DIRECTION (charset) == CHARSET_LEFT_TO_RIGHT)
|
|
2056 ||
|
|
2057 (!(flags & ISO_STATE_R2L) &&
|
|
2058 XCHARSET_DIRECTION (charset) == CHARSET_RIGHT_TO_LEFT))
|
|
2059 {
|
|
2060 Lisp_Object new_charset =
|
|
2061 XCHARSET_REVERSE_DIRECTION_CHARSET (charset);
|
|
2062 if (!NILP (new_charset))
|
|
2063 charset = new_charset;
|
|
2064 }
|
|
2065
|
|
2066 lb = XCHARSET_LEADING_BYTE (charset);
|
|
2067 switch (XCHARSET_REP_BYTES (charset))
|
|
2068 {
|
|
2069 case 1: /* ASCII */
|
|
2070 DECODE_OUTPUT_PARTIAL_CHAR (ch, dst);
|
|
2071 Dynarr_add (dst, c & 0x7F);
|
|
2072 break;
|
|
2073
|
|
2074 case 2: /* one-byte official */
|
|
2075 DECODE_OUTPUT_PARTIAL_CHAR (ch, dst);
|
|
2076 Dynarr_add (dst, lb);
|
|
2077 Dynarr_add (dst, c | 0x80);
|
|
2078 break;
|
|
2079
|
|
2080 case 3: /* one-byte private or two-byte official */
|
|
2081 if (XCHARSET_PRIVATE_P (charset))
|
|
2082 {
|
|
2083 DECODE_OUTPUT_PARTIAL_CHAR (ch, dst);
|
|
2084 Dynarr_add (dst, PRE_LEADING_BYTE_PRIVATE_1);
|
|
2085 Dynarr_add (dst, lb);
|
|
2086 Dynarr_add (dst, c | 0x80);
|
|
2087 }
|
|
2088 else
|
|
2089 {
|
|
2090 if (ch)
|
|
2091 {
|
|
2092 Dynarr_add (dst, lb);
|
|
2093 Dynarr_add (dst, ch | 0x80);
|
|
2094 Dynarr_add (dst, c | 0x80);
|
|
2095 ch = 0;
|
|
2096 }
|
|
2097 else
|
|
2098 ch = c;
|
|
2099 }
|
|
2100 break;
|
|
2101
|
|
2102 default: /* two-byte private */
|
|
2103 if (ch)
|
|
2104 {
|
|
2105 Dynarr_add (dst, PRE_LEADING_BYTE_PRIVATE_2);
|
|
2106 Dynarr_add (dst, lb);
|
|
2107 Dynarr_add (dst, ch | 0x80);
|
|
2108 Dynarr_add (dst, c | 0x80);
|
|
2109 ch = 0;
|
|
2110 }
|
|
2111 else
|
|
2112 ch = c;
|
|
2113 }
|
|
2114 }
|
|
2115
|
|
2116 if (!ch)
|
|
2117 flags &= ISO_STATE_LOCK;
|
|
2118 }
|
|
2119
|
|
2120 }
|
|
2121
|
|
2122 if (str->eof)
|
|
2123 DECODE_OUTPUT_PARTIAL_CHAR (ch, dst);
|
|
2124
|
|
2125 data->flags = flags;
|
|
2126 str->ch = ch;
|
|
2127 return orign;
|
|
2128 }
|
|
2129
|
|
2130
|
|
2131 /***** ISO2022 encoder *****/
|
|
2132
|
|
2133 /* Designate CHARSET into register REG. */
|
|
2134
|
|
2135 static void
|
|
2136 iso2022_designate (Lisp_Object charset, int reg,
|
|
2137 struct coding_stream *str, unsigned_char_dynarr *dst)
|
|
2138 {
|
|
2139 static const char inter94[] = "()*+";
|
|
2140 static const char inter96[] = ",-./";
|
|
2141 int type;
|
|
2142 unsigned char final;
|
|
2143 struct iso2022_coding_stream *data =
|
|
2144 CODING_STREAM_TYPE_DATA (str, iso2022);
|
|
2145 Lisp_Object old_charset = data->charset[reg];
|
|
2146
|
|
2147 data->charset[reg] = charset;
|
|
2148 if (!CHARSETP (charset))
|
|
2149 /* charset might be an initial nil or t. */
|
|
2150 return;
|
|
2151 type = XCHARSET_TYPE (charset);
|
|
2152 final = XCHARSET_FINAL (charset);
|
|
2153 if (!data->force_charset_on_output[reg] &&
|
|
2154 CHARSETP (old_charset) &&
|
|
2155 XCHARSET_TYPE (old_charset) == type &&
|
|
2156 XCHARSET_FINAL (old_charset) == final)
|
|
2157 return;
|
|
2158
|
|
2159 data->force_charset_on_output[reg] = 0;
|
|
2160
|
|
2161 {
|
|
2162 charset_conversion_spec_dynarr *dyn =
|
|
2163 XCODING_SYSTEM_ISO2022_OUTPUT_CONV (str->codesys);
|
|
2164
|
|
2165 if (dyn)
|
|
2166 {
|
|
2167 int i;
|
|
2168
|
|
2169 for (i = 0; i < Dynarr_length (dyn); i++)
|
|
2170 {
|
|
2171 struct charset_conversion_spec *spec = Dynarr_atp (dyn, i);
|
|
2172 if (EQ (charset, spec->from_charset))
|
|
2173 charset = spec->to_charset;
|
|
2174 }
|
|
2175 }
|
|
2176 }
|
|
2177
|
|
2178 Dynarr_add (dst, ISO_CODE_ESC);
|
3439
|
2179
|
771
|
2180 switch (type)
|
|
2181 {
|
|
2182 case CHARSET_TYPE_94:
|
|
2183 Dynarr_add (dst, inter94[reg]);
|
|
2184 break;
|
|
2185 case CHARSET_TYPE_96:
|
|
2186 Dynarr_add (dst, inter96[reg]);
|
|
2187 break;
|
|
2188 case CHARSET_TYPE_94X94:
|
|
2189 Dynarr_add (dst, '$');
|
|
2190 if (reg != 0
|
|
2191 || !(XCODING_SYSTEM_ISO2022_SHORT (str->codesys))
|
|
2192 || final < '@'
|
|
2193 || final > 'B')
|
|
2194 Dynarr_add (dst, inter94[reg]);
|
|
2195 break;
|
|
2196 case CHARSET_TYPE_96X96:
|
|
2197 Dynarr_add (dst, '$');
|
|
2198 Dynarr_add (dst, inter96[reg]);
|
|
2199 break;
|
|
2200 }
|
|
2201 Dynarr_add (dst, final);
|
|
2202 }
|
|
2203
|
|
2204 static void
|
|
2205 ensure_normal_shift (struct coding_stream *str, unsigned_char_dynarr *dst)
|
|
2206 {
|
|
2207 struct iso2022_coding_stream *data =
|
|
2208 CODING_STREAM_TYPE_DATA (str, iso2022);
|
|
2209
|
|
2210 if (data->register_left != 0)
|
|
2211 {
|
|
2212 Dynarr_add (dst, ISO_CODE_SI);
|
|
2213 data->register_left = 0;
|
|
2214 }
|
|
2215 }
|
|
2216
|
|
2217 static void
|
|
2218 ensure_shift_out (struct coding_stream *str, unsigned_char_dynarr *dst)
|
|
2219 {
|
|
2220 struct iso2022_coding_stream *data =
|
|
2221 CODING_STREAM_TYPE_DATA (str, iso2022);
|
|
2222
|
|
2223 if (data->register_left != 1)
|
|
2224 {
|
|
2225 Dynarr_add (dst, ISO_CODE_SO);
|
|
2226 data->register_left = 1;
|
|
2227 }
|
|
2228 }
|
|
2229
|
|
2230 /* Convert internally-formatted data to ISO2022 format. */
|
|
2231
|
|
2232 static Bytecount
|
867
|
2233 iso2022_encode (struct coding_stream *str, const Ibyte *src,
|
771
|
2234 unsigned_char_dynarr *dst, Bytecount n)
|
|
2235 {
|
|
2236 unsigned char charmask;
|
867
|
2237 Ibyte c;
|
771
|
2238 unsigned char char_boundary;
|
|
2239 unsigned int ch = str->ch;
|
|
2240 Lisp_Object codesys = str->codesys;
|
|
2241 int i;
|
|
2242 Lisp_Object charset;
|
|
2243 int half;
|
|
2244 struct iso2022_coding_stream *data =
|
|
2245 CODING_STREAM_TYPE_DATA (str, iso2022);
|
|
2246 unsigned int flags = data->flags;
|
|
2247 Bytecount orign = n;
|
|
2248
|
|
2249 #ifdef ENABLE_COMPOSITE_CHARS
|
|
2250 /* flags for handling composite chars. We do a little switcheroo
|
|
2251 on the source while we're outputting the composite char. */
|
|
2252 Bytecount saved_n = 0;
|
867
|
2253 const Ibyte *saved_src = NULL;
|
771
|
2254 int in_composite = 0;
|
|
2255 #endif /* ENABLE_COMPOSITE_CHARS */
|
|
2256
|
|
2257 char_boundary = data->current_char_boundary;
|
|
2258 charset = data->current_charset;
|
|
2259 half = data->current_half;
|
|
2260
|
|
2261 #ifdef ENABLE_COMPOSITE_CHARS
|
|
2262 back_to_square_n:
|
|
2263 #endif
|
|
2264 while (n--)
|
|
2265 {
|
|
2266 c = *src++;
|
|
2267
|
826
|
2268 if (byte_ascii_p (c))
|
771
|
2269 { /* Processing ASCII character */
|
|
2270 ch = 0;
|
|
2271
|
3439
|
2272 if (flags & ISO_STATE_UTF_8)
|
|
2273 {
|
|
2274 Dynarr_add (dst, ISO_CODE_ESC);
|
|
2275 Dynarr_add (dst, '%');
|
|
2276 Dynarr_add (dst, '@');
|
|
2277 flags &= ~(ISO_STATE_UTF_8);
|
|
2278 }
|
|
2279
|
771
|
2280 restore_left_to_right_direction (codesys, dst, &flags, 0);
|
|
2281
|
|
2282 /* Make sure G0 contains ASCII */
|
|
2283 if ((c > ' ' && c < ISO_CODE_DEL) ||
|
|
2284 !XCODING_SYSTEM_ISO2022_NO_ASCII_CNTL (codesys))
|
|
2285 {
|
|
2286 ensure_normal_shift (str, dst);
|
|
2287 iso2022_designate (Vcharset_ascii, 0, str, dst);
|
|
2288 }
|
|
2289
|
|
2290 /* If necessary, restore everything to the default state
|
|
2291 at end-of-line */
|
|
2292 if (!(XCODING_SYSTEM_ISO2022_NO_ASCII_EOL (codesys)))
|
|
2293 {
|
|
2294 /* NOTE: CRLF encoding happens *BEFORE* other encoding.
|
|
2295 Thus, even though we're working with internal-format
|
|
2296 data, there may be CR's or CRLF sequences representing
|
|
2297 newlines. */
|
|
2298 if (c == '\r' || (c == '\n' && !(flags & ISO_STATE_CR)))
|
|
2299 {
|
|
2300 restore_left_to_right_direction (codesys, dst, &flags, 0);
|
|
2301
|
|
2302 ensure_normal_shift (str, dst);
|
|
2303
|
|
2304 for (i = 0; i < 4; i++)
|
|
2305 {
|
|
2306 Lisp_Object initial_charset =
|
|
2307 XCODING_SYSTEM_ISO2022_INITIAL_CHARSET (codesys, i);
|
|
2308 iso2022_designate (initial_charset, i, str, dst);
|
|
2309 }
|
|
2310 }
|
|
2311 if (c == '\r')
|
|
2312 flags |= ISO_STATE_CR;
|
|
2313 else
|
|
2314 flags &= ~ISO_STATE_CR;
|
|
2315 }
|
|
2316
|
|
2317 if (XCODING_SYSTEM_ISO2022_ESCAPE_QUOTED (codesys)
|
|
2318 && fit_to_be_escape_quoted (c))
|
|
2319 Dynarr_add (dst, ISO_CODE_ESC);
|
|
2320 Dynarr_add (dst, c);
|
|
2321 char_boundary = 1;
|
|
2322 }
|
867
|
2323 else if (ibyte_leading_byte_p (c) || ibyte_leading_byte_p (ch))
|
771
|
2324 { /* Processing Leading Byte */
|
|
2325 ch = 0;
|
826
|
2326 charset = charset_by_leading_byte (c);
|
|
2327 if (leading_byte_prefix_p (c))
|
3439
|
2328 {
|
|
2329 ch = c;
|
|
2330 }
|
|
2331 else if (XCHARSET_ENCODE_AS_UTF_8 (charset))
|
|
2332 {
|
|
2333 assert (!EQ (charset, Vcharset_control_1)
|
|
2334 && !EQ (charset, Vcharset_composite));
|
|
2335
|
|
2336 /* If the character set is to be encoded as UTF-8, the escape
|
|
2337 is always the same. */
|
|
2338 if (!(flags & ISO_STATE_UTF_8))
|
|
2339 {
|
|
2340 Dynarr_add (dst, ISO_CODE_ESC);
|
|
2341 Dynarr_add (dst, '%');
|
|
2342 Dynarr_add (dst, 'G');
|
|
2343 flags |= ISO_STATE_UTF_8;
|
|
2344 }
|
|
2345 }
|
771
|
2346 else if (!EQ (charset, Vcharset_control_1)
|
|
2347 && !EQ (charset, Vcharset_composite))
|
|
2348 {
|
|
2349 int reg;
|
|
2350
|
3439
|
2351 /* End the UTF-8 state. */
|
|
2352 if (flags & ISO_STATE_UTF_8)
|
|
2353 {
|
|
2354 Dynarr_add (dst, ISO_CODE_ESC);
|
|
2355 Dynarr_add (dst, '%');
|
|
2356 Dynarr_add (dst, '@');
|
|
2357 flags &= ~(ISO_STATE_UTF_8);
|
|
2358 }
|
|
2359
|
771
|
2360 ensure_correct_direction (XCHARSET_DIRECTION (charset),
|
|
2361 codesys, dst, &flags, 0);
|
|
2362
|
|
2363 /* Now determine which register to use. */
|
|
2364 reg = -1;
|
|
2365 for (i = 0; i < 4; i++)
|
|
2366 {
|
|
2367 if (EQ (charset, data->charset[i]) ||
|
|
2368 EQ (charset,
|
|
2369 XCODING_SYSTEM_ISO2022_INITIAL_CHARSET (codesys, i)))
|
|
2370 {
|
|
2371 reg = i;
|
|
2372 break;
|
|
2373 }
|
|
2374 }
|
|
2375
|
|
2376 if (reg == -1)
|
|
2377 {
|
|
2378 if (XCHARSET_GRAPHIC (charset) != 0)
|
|
2379 {
|
|
2380 if (!NILP (data->charset[1]) &&
|
|
2381 (!XCODING_SYSTEM_ISO2022_SEVEN (codesys) ||
|
|
2382 XCODING_SYSTEM_ISO2022_LOCK_SHIFT (codesys)))
|
|
2383 reg = 1;
|
|
2384 else if (!NILP (data->charset[2]))
|
|
2385 reg = 2;
|
|
2386 else if (!NILP (data->charset[3]))
|
|
2387 reg = 3;
|
|
2388 else
|
|
2389 reg = 0;
|
|
2390 }
|
|
2391 else
|
|
2392 reg = 0;
|
|
2393 }
|
|
2394
|
|
2395 iso2022_designate (charset, reg, str, dst);
|
|
2396
|
|
2397 /* Now invoke that register. */
|
|
2398 switch (reg)
|
|
2399 {
|
|
2400 case 0:
|
|
2401 ensure_normal_shift (str, dst);
|
|
2402 half = 0;
|
|
2403 break;
|
|
2404
|
|
2405 case 1:
|
|
2406 if (XCODING_SYSTEM_ISO2022_SEVEN (codesys))
|
|
2407 {
|
|
2408 ensure_shift_out (str, dst);
|
|
2409 half = 0;
|
|
2410 }
|
|
2411 else
|
|
2412 half = 1;
|
|
2413 break;
|
|
2414
|
|
2415 case 2:
|
|
2416 if (XCODING_SYSTEM_ISO2022_SEVEN (str->codesys))
|
|
2417 {
|
|
2418 Dynarr_add (dst, ISO_CODE_ESC);
|
|
2419 Dynarr_add (dst, 'N');
|
|
2420 half = 0;
|
|
2421 }
|
|
2422 else
|
|
2423 {
|
|
2424 Dynarr_add (dst, ISO_CODE_SS2);
|
|
2425 half = 1;
|
|
2426 }
|
|
2427 break;
|
|
2428
|
|
2429 case 3:
|
|
2430 if (XCODING_SYSTEM_ISO2022_SEVEN (str->codesys))
|
|
2431 {
|
|
2432 Dynarr_add (dst, ISO_CODE_ESC);
|
|
2433 Dynarr_add (dst, 'O');
|
|
2434 half = 0;
|
|
2435 }
|
|
2436 else
|
|
2437 {
|
|
2438 Dynarr_add (dst, ISO_CODE_SS3);
|
|
2439 half = 1;
|
|
2440 }
|
|
2441 break;
|
|
2442
|
|
2443 default:
|
2500
|
2444 ABORT ();
|
771
|
2445 }
|
|
2446 }
|
|
2447 char_boundary = 0;
|
|
2448 }
|
|
2449 else
|
|
2450 { /* Processing Non-ASCII character */
|
|
2451 charmask = (half == 0 ? 0x7F : 0xFF);
|
|
2452 char_boundary = 1;
|
|
2453 if (EQ (charset, Vcharset_control_1))
|
|
2454 {
|
|
2455 if (XCODING_SYSTEM_ISO2022_ESCAPE_QUOTED (codesys)
|
|
2456 && fit_to_be_escape_quoted (c))
|
|
2457 Dynarr_add (dst, ISO_CODE_ESC);
|
|
2458 /* you asked for it ... */
|
|
2459 Dynarr_add (dst, c - 0x20);
|
|
2460 }
|
|
2461 #ifndef ENABLE_COMPOSITE_CHARS
|
|
2462 else if (EQ (charset, Vcharset_composite))
|
|
2463 {
|
|
2464 if (c >= 160 || c <= 164) /* Someone might have stuck in
|
|
2465 something else */
|
|
2466 {
|
|
2467 Dynarr_add (dst, ISO_CODE_ESC);
|
|
2468 Dynarr_add (dst, c - 160 + '0');
|
|
2469 }
|
|
2470 }
|
|
2471 #endif
|
|
2472 else
|
|
2473 {
|
|
2474 switch (XCHARSET_REP_BYTES (charset))
|
|
2475 {
|
|
2476 case 2:
|
3439
|
2477 dynarr_add_2022_one_dimension (charset, c,
|
|
2478 charmask, dst);
|
771
|
2479 break;
|
|
2480 case 3:
|
|
2481 if (XCHARSET_PRIVATE_P (charset))
|
|
2482 {
|
3439
|
2483 dynarr_add_2022_one_dimension (charset, c,
|
|
2484 charmask, dst);
|
771
|
2485 ch = 0;
|
|
2486 }
|
|
2487 else if (ch)
|
|
2488 {
|
|
2489 #ifdef ENABLE_COMPOSITE_CHARS
|
|
2490 if (EQ (charset, Vcharset_composite))
|
|
2491 {
|
3439
|
2492 /* #### Hasn't been written to handle composite
|
|
2493 characters yet. */
|
|
2494 assert(!XCHARSET_ENCODE_AS_UTF_8 (charset))
|
771
|
2495 if (in_composite)
|
|
2496 {
|
|
2497 /* #### Bother! We don't know how to
|
|
2498 handle this yet. */
|
|
2499 Dynarr_add (dst, '~');
|
|
2500 }
|
|
2501 else
|
|
2502 {
|
867
|
2503 Ichar emch = make_ichar (Vcharset_composite,
|
771
|
2504 ch & 0x7F, c & 0x7F);
|
|
2505 Lisp_Object lstr = composite_char_string (emch);
|
|
2506 saved_n = n;
|
|
2507 saved_src = src;
|
|
2508 in_composite = 1;
|
|
2509 src = XSTRING_DATA (lstr);
|
|
2510 n = XSTRING_LENGTH (lstr);
|
|
2511 Dynarr_add (dst, ISO_CODE_ESC);
|
|
2512 Dynarr_add (dst, '0'); /* start composing */
|
|
2513 }
|
|
2514 }
|
|
2515 else
|
|
2516 #endif /* ENABLE_COMPOSITE_CHARS */
|
|
2517 {
|
3439
|
2518 dynarr_add_2022_two_dimensions (charset, c, ch,
|
|
2519 charmask, dst);
|
771
|
2520 }
|
|
2521 ch = 0;
|
|
2522 }
|
|
2523 else
|
|
2524 {
|
|
2525 ch = c;
|
|
2526 char_boundary = 0;
|
|
2527 }
|
|
2528 break;
|
|
2529 case 4:
|
|
2530 if (ch)
|
|
2531 {
|
3439
|
2532 dynarr_add_2022_two_dimensions (charset, c, ch,
|
|
2533 charmask, dst);
|
771
|
2534 ch = 0;
|
|
2535 }
|
|
2536 else
|
|
2537 {
|
|
2538 ch = c;
|
|
2539 char_boundary = 0;
|
|
2540 }
|
|
2541 break;
|
|
2542 default:
|
2500
|
2543 ABORT ();
|
771
|
2544 }
|
|
2545 }
|
|
2546 }
|
|
2547 }
|
|
2548
|
|
2549 #ifdef ENABLE_COMPOSITE_CHARS
|
|
2550 if (in_composite)
|
|
2551 {
|
|
2552 n = saved_n;
|
|
2553 src = saved_src;
|
|
2554 in_composite = 0;
|
|
2555 Dynarr_add (dst, ISO_CODE_ESC);
|
|
2556 Dynarr_add (dst, '1'); /* end composing */
|
|
2557 goto back_to_square_n; /* Wheeeeeeeee ..... */
|
|
2558 }
|
|
2559 #endif /* ENABLE_COMPOSITE_CHARS */
|
|
2560
|
|
2561 if (char_boundary && str->eof)
|
|
2562 {
|
|
2563 restore_left_to_right_direction (codesys, dst, &flags, 0);
|
|
2564 ensure_normal_shift (str, dst);
|
|
2565 for (i = 0; i < 4; i++)
|
|
2566 {
|
|
2567 Lisp_Object initial_charset =
|
|
2568 XCODING_SYSTEM_ISO2022_INITIAL_CHARSET (codesys, i);
|
|
2569 iso2022_designate (initial_charset, i, str, dst);
|
|
2570 }
|
|
2571 }
|
|
2572
|
|
2573 data->flags = flags;
|
|
2574 str->ch = ch;
|
|
2575 data->current_char_boundary = char_boundary;
|
|
2576 data->current_charset = charset;
|
|
2577 data->current_half = half;
|
|
2578
|
|
2579 /* Verbum caro factum est! */
|
|
2580 return orign;
|
|
2581 }
|
|
2582
|
|
2583 static Bytecount
|
|
2584 iso2022_convert (struct coding_stream *str,
|
|
2585 const UExtbyte *src,
|
|
2586 unsigned_char_dynarr *dst, Bytecount n)
|
|
2587 {
|
|
2588 if (str->direction == CODING_DECODE)
|
|
2589 return iso2022_decode (str, src, dst, n);
|
|
2590 else
|
|
2591 return iso2022_encode (str, src, dst, n);
|
|
2592 }
|
|
2593
|
|
2594 static void
|
|
2595 iso2022_mark (Lisp_Object codesys)
|
|
2596 {
|
|
2597 int i;
|
|
2598
|
|
2599 for (i = 0; i < 4; i++)
|
|
2600 mark_object (XCODING_SYSTEM_ISO2022_INITIAL_CHARSET (codesys, i));
|
|
2601 if (XCODING_SYSTEM_ISO2022_INPUT_CONV (codesys))
|
|
2602 {
|
|
2603 for (i = 0;
|
|
2604 i < Dynarr_length (XCODING_SYSTEM_ISO2022_INPUT_CONV (codesys));
|
|
2605 i++)
|
|
2606 {
|
|
2607 struct charset_conversion_spec *ccs =
|
|
2608 Dynarr_atp (XCODING_SYSTEM_ISO2022_INPUT_CONV (codesys), i);
|
|
2609 mark_object (ccs->from_charset);
|
|
2610 mark_object (ccs->to_charset);
|
|
2611 }
|
|
2612 }
|
|
2613 if (XCODING_SYSTEM_ISO2022_OUTPUT_CONV (codesys))
|
|
2614 {
|
|
2615 for (i = 0;
|
|
2616 i < Dynarr_length (XCODING_SYSTEM_ISO2022_OUTPUT_CONV (codesys));
|
|
2617 i++)
|
|
2618 {
|
|
2619 struct charset_conversion_spec *ccs =
|
|
2620 Dynarr_atp (XCODING_SYSTEM_ISO2022_OUTPUT_CONV (codesys), i);
|
|
2621 mark_object (ccs->from_charset);
|
|
2622 mark_object (ccs->to_charset);
|
|
2623 }
|
|
2624 }
|
|
2625 }
|
|
2626
|
|
2627 static void
|
|
2628 iso2022_finalize (Lisp_Object cs)
|
|
2629 {
|
|
2630 if (XCODING_SYSTEM_ISO2022_INPUT_CONV (cs))
|
|
2631 {
|
|
2632 Dynarr_free (XCODING_SYSTEM_ISO2022_INPUT_CONV (cs));
|
|
2633 XCODING_SYSTEM_ISO2022_INPUT_CONV (cs) = 0;
|
|
2634 }
|
|
2635 if (XCODING_SYSTEM_ISO2022_OUTPUT_CONV (cs))
|
|
2636 {
|
|
2637 Dynarr_free (XCODING_SYSTEM_ISO2022_OUTPUT_CONV (cs));
|
|
2638 XCODING_SYSTEM_ISO2022_OUTPUT_CONV (cs) = 0;
|
|
2639 }
|
|
2640 }
|
|
2641
|
|
2642 /* Given a list of charset conversion specs as specified in a Lisp
|
|
2643 program, parse it into STORE_HERE. */
|
|
2644
|
|
2645 static void
|
|
2646 parse_charset_conversion_specs (charset_conversion_spec_dynarr *store_here,
|
|
2647 Lisp_Object spec_list)
|
|
2648 {
|
2367
|
2649 EXTERNAL_LIST_LOOP_2 (car, spec_list)
|
771
|
2650 {
|
|
2651 Lisp_Object from, to;
|
|
2652 struct charset_conversion_spec spec;
|
|
2653
|
|
2654 if (!CONSP (car) || !CONSP (XCDR (car)) || !NILP (XCDR (XCDR (car))))
|
|
2655 invalid_argument ("Invalid charset conversion spec", car);
|
|
2656 from = Fget_charset (XCAR (car));
|
|
2657 to = Fget_charset (XCAR (XCDR (car)));
|
|
2658 if (XCHARSET_TYPE (from) != XCHARSET_TYPE (to))
|
|
2659 invalid_operation_2
|
|
2660 ("Attempted conversion between different charset types",
|
|
2661 from, to);
|
|
2662 spec.from_charset = from;
|
|
2663 spec.to_charset = to;
|
|
2664
|
|
2665 Dynarr_add (store_here, spec);
|
|
2666 }
|
|
2667 }
|
|
2668
|
|
2669 /* Given a dynarr LOAD_HERE of internally-stored charset conversion
|
|
2670 specs, return the equivalent as the Lisp programmer would see it.
|
|
2671
|
|
2672 If LOAD_HERE is 0, return Qnil. */
|
|
2673
|
|
2674 static Lisp_Object
|
|
2675 unparse_charset_conversion_specs (charset_conversion_spec_dynarr *load_here,
|
|
2676 int names)
|
|
2677 {
|
|
2678 int i;
|
|
2679 Lisp_Object result;
|
|
2680
|
|
2681 if (!load_here)
|
|
2682 return Qnil;
|
|
2683 for (i = 0, result = Qnil; i < Dynarr_length (load_here); i++)
|
|
2684 {
|
|
2685 struct charset_conversion_spec *ccs = Dynarr_atp (load_here, i);
|
|
2686 if (names)
|
|
2687 result = Fcons (list2 (XCHARSET_NAME (ccs->from_charset),
|
|
2688 XCHARSET_NAME (ccs->to_charset)), result);
|
|
2689 else
|
|
2690 result = Fcons (list2 (ccs->from_charset, ccs->to_charset), result);
|
|
2691 }
|
|
2692
|
|
2693 return Fnreverse (result);
|
|
2694 }
|
|
2695
|
|
2696 static int
|
|
2697 iso2022_putprop (Lisp_Object codesys,
|
|
2698 Lisp_Object key,
|
|
2699 Lisp_Object value)
|
|
2700 {
|
|
2701 #define FROB_INITIAL_CHARSET(charset_num) \
|
|
2702 XCODING_SYSTEM_ISO2022_INITIAL_CHARSET (codesys, charset_num) = \
|
|
2703 ((EQ (value, Qt) || EQ (value, Qnil)) ? value : Fget_charset (value))
|
|
2704
|
|
2705 if (EQ (key, Qcharset_g0)) FROB_INITIAL_CHARSET (0);
|
|
2706 else if (EQ (key, Qcharset_g1)) FROB_INITIAL_CHARSET (1);
|
|
2707 else if (EQ (key, Qcharset_g2)) FROB_INITIAL_CHARSET (2);
|
|
2708 else if (EQ (key, Qcharset_g3)) FROB_INITIAL_CHARSET (3);
|
|
2709
|
|
2710 #define FROB_FORCE_CHARSET(charset_num) \
|
|
2711 XCODING_SYSTEM_ISO2022_FORCE_CHARSET_ON_OUTPUT (codesys, charset_num) = \
|
|
2712 !NILP (value)
|
|
2713
|
|
2714 else if (EQ (key, Qforce_g0_on_output)) FROB_FORCE_CHARSET (0);
|
|
2715 else if (EQ (key, Qforce_g1_on_output)) FROB_FORCE_CHARSET (1);
|
|
2716 else if (EQ (key, Qforce_g2_on_output)) FROB_FORCE_CHARSET (2);
|
|
2717 else if (EQ (key, Qforce_g3_on_output)) FROB_FORCE_CHARSET (3);
|
|
2718
|
|
2719 #define FROB_BOOLEAN_PROPERTY(prop) \
|
|
2720 XCODING_SYSTEM_ISO2022_##prop (codesys) = !NILP (value)
|
|
2721
|
|
2722 else if (EQ (key, Qshort)) FROB_BOOLEAN_PROPERTY (SHORT);
|
|
2723 else if (EQ (key, Qno_ascii_eol)) FROB_BOOLEAN_PROPERTY (NO_ASCII_EOL);
|
|
2724 else if (EQ (key, Qno_ascii_cntl)) FROB_BOOLEAN_PROPERTY (NO_ASCII_CNTL);
|
|
2725 else if (EQ (key, Qseven)) FROB_BOOLEAN_PROPERTY (SEVEN);
|
|
2726 else if (EQ (key, Qlock_shift)) FROB_BOOLEAN_PROPERTY (LOCK_SHIFT);
|
|
2727 else if (EQ (key, Qno_iso6429)) FROB_BOOLEAN_PROPERTY (NO_ISO6429);
|
|
2728 else if (EQ (key, Qescape_quoted)) FROB_BOOLEAN_PROPERTY (ESCAPE_QUOTED);
|
|
2729
|
|
2730 else if (EQ (key, Qinput_charset_conversion))
|
|
2731 {
|
|
2732 XCODING_SYSTEM_ISO2022_INPUT_CONV (codesys) =
|
|
2733 Dynarr_new (charset_conversion_spec);
|
|
2734 parse_charset_conversion_specs
|
|
2735 (XCODING_SYSTEM_ISO2022_INPUT_CONV (codesys), value);
|
|
2736 }
|
|
2737 else if (EQ (key, Qoutput_charset_conversion))
|
|
2738 {
|
|
2739 XCODING_SYSTEM_ISO2022_OUTPUT_CONV (codesys) =
|
|
2740 Dynarr_new (charset_conversion_spec);
|
|
2741 parse_charset_conversion_specs
|
|
2742 (XCODING_SYSTEM_ISO2022_OUTPUT_CONV (codesys), value);
|
|
2743 }
|
|
2744 else
|
|
2745 return 0;
|
|
2746
|
|
2747 return 1;
|
|
2748 }
|
|
2749
|
|
2750 static void
|
2286
|
2751 iso2022_finalize_coding_stream (
|
|
2752 #ifdef ENABLE_COMPOSITE_CHARS
|
|
2753 struct coding_stream *str
|
|
2754 #else
|
|
2755 struct coding_stream *UNUSED (str)
|
|
2756 #endif
|
|
2757 )
|
771
|
2758 {
|
|
2759 #ifdef ENABLE_COMPOSITE_CHARS
|
|
2760 struct iso2022_coding_stream *data =
|
|
2761 CODING_STREAM_TYPE_DATA (str, iso2022);
|
|
2762
|
|
2763 if (data->composite_chars)
|
|
2764 Dynarr_free (data->composite_chars);
|
|
2765 #endif
|
|
2766 }
|
|
2767
|
|
2768 static void
|
|
2769 iso2022_init (Lisp_Object codesys)
|
|
2770 {
|
|
2771 int i;
|
|
2772 for (i = 0; i < 4; i++)
|
|
2773 XCODING_SYSTEM_ISO2022_INITIAL_CHARSET (codesys, i) = Qnil;
|
|
2774 }
|
|
2775
|
|
2776 static Lisp_Object
|
|
2777 coding_system_charset (Lisp_Object coding_system, int gnum)
|
|
2778 {
|
|
2779 Lisp_Object cs
|
|
2780 = XCODING_SYSTEM_ISO2022_INITIAL_CHARSET (coding_system, gnum);
|
|
2781
|
|
2782 return CHARSETP (cs) ? XCHARSET_NAME (cs) : Qnil;
|
|
2783 }
|
|
2784
|
|
2785 static Lisp_Object
|
|
2786 iso2022_getprop (Lisp_Object coding_system, Lisp_Object prop)
|
|
2787 {
|
|
2788 if (EQ (prop, Qcharset_g0))
|
|
2789 return coding_system_charset (coding_system, 0);
|
|
2790 else if (EQ (prop, Qcharset_g1))
|
|
2791 return coding_system_charset (coding_system, 1);
|
|
2792 else if (EQ (prop, Qcharset_g2))
|
|
2793 return coding_system_charset (coding_system, 2);
|
|
2794 else if (EQ (prop, Qcharset_g3))
|
|
2795 return coding_system_charset (coding_system, 3);
|
|
2796
|
|
2797 #define FORCE_CHARSET(charset_num) \
|
|
2798 (XCODING_SYSTEM_ISO2022_FORCE_CHARSET_ON_OUTPUT \
|
|
2799 (coding_system, charset_num) ? Qt : Qnil)
|
|
2800
|
|
2801 else if (EQ (prop, Qforce_g0_on_output))
|
|
2802 return FORCE_CHARSET (0);
|
|
2803 else if (EQ (prop, Qforce_g1_on_output))
|
|
2804 return FORCE_CHARSET (1);
|
|
2805 else if (EQ (prop, Qforce_g2_on_output))
|
|
2806 return FORCE_CHARSET (2);
|
|
2807 else if (EQ (prop, Qforce_g3_on_output))
|
|
2808 return FORCE_CHARSET (3);
|
|
2809
|
|
2810 #define LISP_BOOLEAN(prop) \
|
|
2811 (XCODING_SYSTEM_ISO2022_##prop (coding_system) ? Qt : Qnil)
|
|
2812
|
|
2813 else if (EQ (prop, Qshort)) return LISP_BOOLEAN (SHORT);
|
|
2814 else if (EQ (prop, Qno_ascii_eol)) return LISP_BOOLEAN (NO_ASCII_EOL);
|
|
2815 else if (EQ (prop, Qno_ascii_cntl)) return LISP_BOOLEAN (NO_ASCII_CNTL);
|
|
2816 else if (EQ (prop, Qseven)) return LISP_BOOLEAN (SEVEN);
|
|
2817 else if (EQ (prop, Qlock_shift)) return LISP_BOOLEAN (LOCK_SHIFT);
|
|
2818 else if (EQ (prop, Qno_iso6429)) return LISP_BOOLEAN (NO_ISO6429);
|
|
2819 else if (EQ (prop, Qescape_quoted)) return LISP_BOOLEAN (ESCAPE_QUOTED);
|
|
2820
|
|
2821 else if (EQ (prop, Qinput_charset_conversion))
|
|
2822 return
|
|
2823 unparse_charset_conversion_specs
|
|
2824 (XCODING_SYSTEM_ISO2022_INPUT_CONV (coding_system), 0);
|
|
2825 else if (EQ (prop, Qoutput_charset_conversion))
|
|
2826 return
|
|
2827 unparse_charset_conversion_specs
|
|
2828 (XCODING_SYSTEM_ISO2022_OUTPUT_CONV (coding_system), 0);
|
|
2829 else
|
|
2830 return Qunbound;
|
|
2831 }
|
|
2832
|
|
2833 static void
|
2286
|
2834 iso2022_print (Lisp_Object cs, Lisp_Object printcharfun,
|
|
2835 int UNUSED (escapeflag))
|
771
|
2836 {
|
|
2837 int i;
|
|
2838
|
826
|
2839 write_c_string (printcharfun, "(");
|
771
|
2840 for (i = 0; i < 4; i++)
|
|
2841 {
|
|
2842 Lisp_Object charset = coding_system_charset (cs, i);
|
|
2843 if (i > 0)
|
826
|
2844 write_c_string (printcharfun, ", ");
|
771
|
2845 write_fmt_string (printcharfun, "g%d=", i);
|
800
|
2846 print_internal (CHARSETP (charset) ? XCHARSET_NAME (charset) : charset, printcharfun, 0);
|
771
|
2847 if (XCODING_SYSTEM_ISO2022_FORCE_CHARSET_ON_OUTPUT (cs, i))
|
826
|
2848 write_c_string (printcharfun, "(force)");
|
771
|
2849 }
|
|
2850
|
3084
|
2851 #define FROB(prop) \
|
|
2852 if (!NILP (iso2022_getprop (cs, prop))) \
|
|
2853 { \
|
|
2854 write_fmt_string_lisp (printcharfun, ", %s", 1, prop); \
|
771
|
2855 }
|
|
2856
|
|
2857 FROB (Qshort);
|
|
2858 FROB (Qno_ascii_eol);
|
|
2859 FROB (Qno_ascii_cntl);
|
|
2860 FROB (Qseven);
|
|
2861 FROB (Qlock_shift);
|
|
2862 FROB (Qno_iso6429);
|
|
2863 FROB (Qescape_quoted);
|
|
2864
|
|
2865 {
|
|
2866 Lisp_Object val =
|
|
2867 unparse_charset_conversion_specs
|
|
2868 (XCODING_SYSTEM_ISO2022_INPUT_CONV (cs), 1);
|
|
2869 if (!NILP (val))
|
|
2870 {
|
800
|
2871 write_fmt_string_lisp (printcharfun, ", input-charset-conversion=%s", 1, val);
|
771
|
2872 }
|
|
2873 val =
|
|
2874 unparse_charset_conversion_specs
|
|
2875 (XCODING_SYSTEM_ISO2022_OUTPUT_CONV (cs), 1);
|
|
2876 if (!NILP (val))
|
|
2877 {
|
800
|
2878 write_fmt_string_lisp (printcharfun, ", output-charset-conversion=%s", 1, val);
|
771
|
2879 }
|
826
|
2880 write_c_string (printcharfun, ")");
|
771
|
2881 }
|
|
2882 }
|
|
2883
|
|
2884
|
|
2885 /************************************************************************/
|
|
2886 /* ISO2022 detector */
|
|
2887 /************************************************************************/
|
|
2888
|
|
2889 DEFINE_DETECTOR (iso2022);
|
|
2890 /* ISO2022 system using only seven-bit bytes, no locking shift */
|
|
2891 DEFINE_DETECTOR_CATEGORY (iso2022, iso_7);
|
|
2892 /* ISO2022 system using eight-bit bytes, no locking shift, no single shift,
|
|
2893 using designation to switch charsets */
|
|
2894 DEFINE_DETECTOR_CATEGORY (iso2022, iso_8_designate);
|
|
2895 /* ISO2022 system using eight-bit bytes, no locking shift, no designation
|
|
2896 sequences, one-dimension characters in the upper half. */
|
|
2897 DEFINE_DETECTOR_CATEGORY (iso2022, iso_8_1);
|
|
2898 /* ISO2022 system using eight-bit bytes, no locking shift, no designation
|
|
2899 sequences, two-dimension characters in the upper half. */
|
|
2900 DEFINE_DETECTOR_CATEGORY (iso2022, iso_8_2);
|
|
2901 /* ISO2022 system using locking shift */
|
|
2902 DEFINE_DETECTOR_CATEGORY (iso2022, iso_lock_shift);
|
|
2903
|
|
2904 struct iso2022_detector
|
|
2905 {
|
|
2906 int initted;
|
|
2907 struct iso2022_coding_stream *iso;
|
|
2908 unsigned int flags;
|
|
2909
|
|
2910 /* for keeping temporary track of high-byte groups */
|
|
2911 int high_byte_count;
|
|
2912 unsigned int saw_single_shift_just_now:1;
|
|
2913
|
|
2914 /* running state; we set the likelihoods at the end */
|
|
2915 unsigned int seen_high_byte:1;
|
|
2916 unsigned int seen_single_shift:1;
|
|
2917 unsigned int seen_locking_shift:1;
|
|
2918 unsigned int seen_designate:1;
|
|
2919 unsigned int bad_single_byte_sequences;
|
|
2920 unsigned int bad_multibyte_escape_sequences;
|
|
2921 unsigned int good_multibyte_escape_sequences;
|
|
2922 int even_high_byte_groups;
|
985
|
2923 int longest_even_high_byte;
|
771
|
2924 int odd_high_byte_groups;
|
|
2925 };
|
|
2926
|
|
2927 static void
|
|
2928 iso2022_detect (struct detection_state *st, const UExtbyte *src,
|
|
2929 Bytecount n)
|
|
2930 {
|
|
2931 Bytecount orign = n;
|
|
2932 struct iso2022_detector *data = DETECTION_STATE_DATA (st, iso2022);
|
|
2933
|
|
2934 /* #### There are serious deficiencies in the recognition mechanism
|
|
2935 here. This needs to be much smarter if it's going to cut it.
|
|
2936 The sequence "\xff\x0f" is currently detected as LOCK_SHIFT while
|
|
2937 it should be detected as Latin-1.
|
|
2938 All the ISO2022 stuff in this file should be synced up with the
|
|
2939 code from FSF Emacs-21.0, in which Mule should be more or less stable.
|
|
2940 Perhaps we should wait till R2L works in FSF Emacs? */
|
|
2941
|
|
2942 /* We keep track of running state on our own, and set the categories at the
|
|
2943 end; that way we can reflect the correct state each time we finish, but
|
|
2944 not get confused by those results the next time around. */
|
|
2945
|
|
2946 if (!data->initted)
|
|
2947 {
|
|
2948 xzero (*data);
|
|
2949 data->iso = xnew_and_zero (struct iso2022_coding_stream);
|
|
2950 reset_iso2022_decode (Qnil, data->iso);
|
|
2951 data->initted = 1;
|
|
2952 }
|
|
2953
|
|
2954 while (n--)
|
|
2955 {
|
|
2956 UExtbyte c = *src++;
|
|
2957 if (c >= 0x80)
|
|
2958 data->seen_high_byte = 1;
|
|
2959 if (c >= 0xA0)
|
|
2960 data->high_byte_count++;
|
|
2961 else
|
|
2962 {
|
|
2963 if (data->high_byte_count &&
|
|
2964 !data->saw_single_shift_just_now)
|
|
2965 {
|
|
2966 if (data->high_byte_count & 1)
|
|
2967 data->odd_high_byte_groups++;
|
|
2968 else
|
985
|
2969 {
|
|
2970 data->even_high_byte_groups++;
|
|
2971 if (data->longest_even_high_byte < data->high_byte_count)
|
|
2972 data->longest_even_high_byte = data->high_byte_count;
|
|
2973 }
|
771
|
2974 }
|
|
2975 data->high_byte_count = 0;
|
|
2976 data->saw_single_shift_just_now = 0;
|
|
2977 }
|
|
2978 if (!(data->flags & ISO_STATE_ESCAPE)
|
826
|
2979 && (byte_c0_p (c) || byte_c1_p (c)))
|
771
|
2980 { /* control chars */
|
|
2981 switch (c)
|
|
2982 {
|
|
2983 /* Allow and ignore control characters that you might
|
|
2984 reasonably see in a text file */
|
|
2985 case '\r':
|
|
2986 case '\n':
|
|
2987 case '\t':
|
|
2988 case 7: /* bell */
|
|
2989 case 8: /* backspace */
|
|
2990 case 11: /* vertical tab */
|
|
2991 case 12: /* form feed */
|
|
2992 case 26: /* MS-DOS C-z junk */
|
|
2993 case 31: /* '^_' -- for info */
|
|
2994 goto label_continue_loop;
|
|
2995
|
|
2996 default:
|
|
2997 break;
|
|
2998 }
|
|
2999 }
|
|
3000
|
826
|
3001 if ((data->flags & ISO_STATE_ESCAPE) || byte_c0_p (c)
|
|
3002 || byte_c1_p (c))
|
771
|
3003 {
|
|
3004 switch (parse_iso2022_esc (Qnil, data->iso, c,
|
|
3005 &data->flags, 0))
|
|
3006 {
|
|
3007 case 1: /* done */
|
|
3008 if (data->iso->esc_bytes_index > 0)
|
|
3009 data->good_multibyte_escape_sequences++;
|
|
3010 switch (data->iso->esc)
|
|
3011 {
|
|
3012 case ISO_ESC_DESIGNATE:
|
|
3013 data->seen_designate = 1;
|
|
3014 break;
|
|
3015 case ISO_ESC_LOCKING_SHIFT:
|
|
3016 data->seen_locking_shift = 1;
|
|
3017 break;
|
|
3018 case ISO_ESC_SINGLE_SHIFT:
|
|
3019 data->saw_single_shift_just_now = 1;
|
|
3020 data->seen_single_shift = 1;
|
|
3021 break;
|
|
3022 default:
|
|
3023 break;
|
|
3024 }
|
|
3025 break;
|
|
3026
|
|
3027 case -1: /* not done */
|
|
3028 break;
|
|
3029
|
|
3030 case 0: /* error */
|
|
3031 if (data->iso->esc == ISO_ESC_NOTHING)
|
|
3032 data->bad_single_byte_sequences++;
|
|
3033 else
|
|
3034 data->bad_multibyte_escape_sequences++;
|
|
3035 }
|
|
3036 }
|
|
3037 label_continue_loop:;
|
|
3038 }
|
|
3039
|
985
|
3040 if (data->high_byte_count &&
|
|
3041 !data->saw_single_shift_just_now)
|
|
3042 {
|
|
3043 if (data->high_byte_count & 1)
|
|
3044 data->odd_high_byte_groups++;
|
|
3045 else
|
|
3046 {
|
|
3047 data->even_high_byte_groups++;
|
|
3048 if (data->longest_even_high_byte < data->high_byte_count)
|
|
3049 data->longest_even_high_byte = data->high_byte_count;
|
|
3050 }
|
|
3051 }
|
|
3052
|
771
|
3053 if (data->bad_multibyte_escape_sequences > 2 ||
|
|
3054 (data->bad_multibyte_escape_sequences > 0 &&
|
|
3055 data->good_multibyte_escape_sequences /
|
|
3056 data->bad_multibyte_escape_sequences < 10))
|
|
3057 /* Just making it up ... */
|
|
3058 SET_DET_RESULTS (st, iso2022, DET_NEARLY_IMPOSSIBLE);
|
|
3059 else if (data->bad_single_byte_sequences > 5 ||
|
|
3060 (data->bad_single_byte_sequences > 0 &&
|
|
3061 (data->good_multibyte_escape_sequences +
|
|
3062 data->even_high_byte_groups +
|
|
3063 data->odd_high_byte_groups) /
|
|
3064 data->bad_single_byte_sequences < 10))
|
|
3065 SET_DET_RESULTS (st, iso2022, DET_SOMEWHAT_UNLIKELY);
|
|
3066 else if (data->seen_locking_shift)
|
|
3067 {
|
|
3068 SET_DET_RESULTS (st, iso2022, DET_QUITE_IMPROBABLE);
|
|
3069 DET_RESULT (st, iso_lock_shift) = DET_QUITE_PROBABLE;
|
|
3070 }
|
|
3071 else if (!data->seen_high_byte)
|
|
3072 {
|
|
3073 SET_DET_RESULTS (st, iso2022, DET_SOMEWHAT_UNLIKELY);
|
|
3074 if (data->good_multibyte_escape_sequences)
|
|
3075 DET_RESULT (st, iso_7) = DET_QUITE_PROBABLE;
|
|
3076 else if (data->seen_single_shift)
|
|
3077 DET_RESULT (st, iso_7) = DET_SOMEWHAT_LIKELY;
|
|
3078 else
|
|
3079 {
|
|
3080 /* If we've just seen pure 7-bit data, no escape sequences,
|
|
3081 then we can't give much likelihood; but if we've seen enough
|
|
3082 of this data, we can assume some unlikelihood of any 8-bit
|
|
3083 encoding */
|
|
3084 if (orign + st->bytes_seen >= 1000)
|
|
3085 DET_RESULT (st, iso_7) = DET_AS_LIKELY_AS_UNLIKELY;
|
|
3086 else
|
|
3087 SET_DET_RESULTS (st, iso2022, DET_AS_LIKELY_AS_UNLIKELY);
|
|
3088 }
|
|
3089 }
|
|
3090 else if (data->seen_designate)
|
|
3091 {
|
|
3092 SET_DET_RESULTS (st, iso2022, DET_QUITE_IMPROBABLE);
|
|
3093 if (data->seen_single_shift)
|
|
3094 /* #### Does this really make sense? */
|
|
3095 DET_RESULT (st, iso_8_designate) = DET_SOMEWHAT_UNLIKELY;
|
|
3096 else
|
|
3097 DET_RESULT (st, iso_8_designate) = DET_QUITE_PROBABLE;
|
|
3098 }
|
|
3099 else if (data->odd_high_byte_groups > 0 &&
|
|
3100 data->even_high_byte_groups == 0)
|
|
3101 {
|
|
3102 SET_DET_RESULTS (st, iso2022, DET_SOMEWHAT_UNLIKELY);
|
|
3103 if (data->seen_single_shift)
|
|
3104 DET_RESULT (st, iso_8_1) = DET_QUITE_PROBABLE;
|
|
3105 else
|
|
3106 DET_RESULT (st, iso_8_1) = DET_SOMEWHAT_LIKELY;
|
|
3107 }
|
|
3108 else if (data->odd_high_byte_groups == 0 &&
|
|
3109 data->even_high_byte_groups > 0)
|
|
3110 {
|
985
|
3111 #if 0
|
771
|
3112 SET_DET_RESULTS (st, iso2022, DET_SOMEWHAT_UNLIKELY);
|
|
3113 if (data->even_high_byte_groups > 10)
|
|
3114 {
|
|
3115 if (data->seen_single_shift)
|
|
3116 DET_RESULT (st, iso_8_2) = DET_QUITE_PROBABLE;
|
|
3117 else
|
|
3118 DET_RESULT (st, iso_8_2) = DET_SOMEWHAT_LIKELY;
|
|
3119 if (data->even_high_byte_groups < 50)
|
|
3120 DET_RESULT (st, iso_8_1) = DET_SOMEWHAT_UNLIKELY;
|
|
3121 /* else it stays at quite improbable */
|
|
3122 }
|
985
|
3123 #else
|
|
3124 SET_DET_RESULTS (st, iso2022, DET_SOMEWHAT_UNLIKELY);
|
|
3125 if (data->seen_single_shift)
|
|
3126 DET_RESULT (st, iso_8_2) = DET_QUITE_PROBABLE;
|
|
3127 else if (data->even_high_byte_groups > 10)
|
|
3128 DET_RESULT (st, iso_8_2) = DET_SOMEWHAT_LIKELY;
|
|
3129 else if (data->longest_even_high_byte > 6)
|
|
3130 DET_RESULT (st, iso_8_2) = DET_SLIGHTLY_LIKELY;
|
|
3131 #endif
|
771
|
3132 }
|
|
3133 else if (data->odd_high_byte_groups > 0 &&
|
|
3134 data->even_high_byte_groups > 0)
|
3393
|
3135 {
|
|
3136 /* Well, this could be a Latin-1 text, with most high-byte
|
|
3137 characters single, but sometimes two are together, though
|
|
3138 this happens not as often. This is common for Western
|
|
3139 European languages like German, French, Danish, Swedish, etc.
|
|
3140 Then we would either have a rather small file and
|
|
3141 even_high_byte_groups would be low.
|
|
3142 Or we would have a larger file and the ratio of odd to even
|
|
3143 groups would be very high. */
|
|
3144 SET_DET_RESULTS (st, iso2022, DET_SOMEWHAT_UNLIKELY);
|
|
3145 if (data->even_high_byte_groups <= 3 ||
|
|
3146 data->odd_high_byte_groups >= 10 * data->even_high_byte_groups)
|
|
3147 DET_RESULT (st, iso_8_1) = DET_SOMEWHAT_LIKELY;
|
|
3148 }
|
771
|
3149 else
|
|
3150 SET_DET_RESULTS (st, iso2022, DET_AS_LIKELY_AS_UNLIKELY);
|
|
3151 }
|
|
3152
|
|
3153 static void
|
|
3154 iso2022_finalize_detection_state (struct detection_state *st)
|
|
3155 {
|
|
3156 struct iso2022_detector *data = DETECTION_STATE_DATA (st, iso2022);
|
|
3157 if (data->iso)
|
1726
|
3158 xfree (data->iso, struct iso2022_coding_stream *);
|
771
|
3159 }
|
|
3160
|
|
3161
|
|
3162 /************************************************************************/
|
|
3163 /* CCL methods */
|
|
3164 /************************************************************************/
|
|
3165
|
|
3166 /* Converter written in CCL. */
|
|
3167
|
|
3168 struct ccl_coding_system
|
|
3169 {
|
|
3170 /* For a CCL coding system, these specify the CCL programs used for
|
|
3171 decoding (input) and encoding (output). */
|
|
3172 Lisp_Object decode;
|
|
3173 Lisp_Object encode;
|
|
3174 };
|
|
3175
|
|
3176 #define CODING_SYSTEM_CCL_DECODE(codesys) \
|
|
3177 (CODING_SYSTEM_TYPE_DATA (codesys, ccl)->decode)
|
|
3178 #define CODING_SYSTEM_CCL_ENCODE(codesys) \
|
|
3179 (CODING_SYSTEM_TYPE_DATA (codesys, ccl)->encode)
|
|
3180 #define XCODING_SYSTEM_CCL_DECODE(codesys) \
|
|
3181 CODING_SYSTEM_CCL_DECODE (XCODING_SYSTEM (codesys))
|
|
3182 #define XCODING_SYSTEM_CCL_ENCODE(codesys) \
|
|
3183 CODING_SYSTEM_CCL_ENCODE (XCODING_SYSTEM (codesys))
|
|
3184
|
|
3185 struct ccl_coding_stream
|
|
3186 {
|
|
3187 /* state of the running CCL program */
|
|
3188 struct ccl_program ccl;
|
|
3189 };
|
|
3190
|
1204
|
3191 static const struct memory_description ccl_coding_system_description[] = {
|
|
3192 { XD_LISP_OBJECT, offsetof (struct ccl_coding_system, decode) },
|
|
3193 { XD_LISP_OBJECT, offsetof (struct ccl_coding_system, encode) },
|
771
|
3194 { XD_END }
|
|
3195 };
|
|
3196
|
1204
|
3197 DEFINE_CODING_SYSTEM_TYPE_WITH_DATA (ccl);
|
|
3198
|
771
|
3199 static void
|
|
3200 ccl_mark (Lisp_Object codesys)
|
|
3201 {
|
|
3202 mark_object (XCODING_SYSTEM_CCL_DECODE (codesys));
|
|
3203 mark_object (XCODING_SYSTEM_CCL_ENCODE (codesys));
|
|
3204 }
|
|
3205
|
|
3206 static Bytecount
|
|
3207 ccl_convert (struct coding_stream *str, const UExtbyte *src,
|
|
3208 unsigned_char_dynarr *dst, Bytecount n)
|
|
3209 {
|
|
3210 struct ccl_coding_stream *data =
|
|
3211 CODING_STREAM_TYPE_DATA (str, ccl);
|
|
3212 Bytecount orign = n;
|
|
3213
|
|
3214 data->ccl.last_block = str->eof;
|
|
3215 /* When applying a CCL program to a stream, SRC must not be NULL -- this
|
|
3216 is a special signal to the driver that read and write operations are
|
|
3217 not allowed. The code does not actually look at what SRC points to if
|
|
3218 N == 0.
|
|
3219 */
|
|
3220 ccl_driver (&data->ccl, src ? src : (const unsigned char *) "",
|
|
3221 dst, n, 0,
|
|
3222 str->direction == CODING_DECODE ? CCL_MODE_DECODING :
|
|
3223 CCL_MODE_ENCODING);
|
|
3224 return orign;
|
|
3225 }
|
|
3226
|
|
3227 static void
|
|
3228 ccl_init_coding_stream (struct coding_stream *str)
|
|
3229 {
|
|
3230 struct ccl_coding_stream *data =
|
|
3231 CODING_STREAM_TYPE_DATA (str, ccl);
|
|
3232
|
|
3233 setup_ccl_program (&data->ccl,
|
|
3234 str->direction == CODING_DECODE ?
|
|
3235 XCODING_SYSTEM_CCL_DECODE (str->codesys) :
|
|
3236 XCODING_SYSTEM_CCL_ENCODE (str->codesys));
|
|
3237 }
|
|
3238
|
|
3239 static void
|
|
3240 ccl_rewind_coding_stream (struct coding_stream *str)
|
|
3241 {
|
|
3242 ccl_init_coding_stream (str);
|
|
3243 }
|
|
3244
|
|
3245 static void
|
|
3246 ccl_init (Lisp_Object codesys)
|
|
3247 {
|
|
3248 XCODING_SYSTEM_CCL_DECODE (codesys) = Qnil;
|
|
3249 XCODING_SYSTEM_CCL_ENCODE (codesys) = Qnil;
|
|
3250 }
|
|
3251
|
|
3252 static int
|
|
3253 ccl_putprop (Lisp_Object codesys, Lisp_Object key, Lisp_Object value)
|
|
3254 {
|
|
3255 Lisp_Object sym;
|
|
3256 struct ccl_program test_ccl;
|
2367
|
3257 Ascbyte *suffix;
|
771
|
3258
|
|
3259 /* Check key first. */
|
|
3260 if (EQ (key, Qdecode))
|
|
3261 suffix = "-ccl-decode";
|
|
3262 else if (EQ (key, Qencode))
|
|
3263 suffix = "-ccl-encode";
|
|
3264 else
|
|
3265 return 0;
|
|
3266
|
|
3267 /* If value is vector, register it as a ccl program
|
|
3268 associated with a newly created symbol for
|
|
3269 backward compatibility.
|
|
3270
|
|
3271 #### Bogosity alert! Do we really have to do this crap???? --ben */
|
|
3272 if (VECTORP (value))
|
|
3273 {
|
|
3274 sym = Fintern (concat2 (Fsymbol_name (XCODING_SYSTEM_NAME (codesys)),
|
|
3275 build_string (suffix)),
|
|
3276 Qnil);
|
|
3277 Fregister_ccl_program (sym, value);
|
|
3278 }
|
|
3279 else
|
|
3280 {
|
|
3281 CHECK_SYMBOL (value);
|
|
3282 sym = value;
|
|
3283 }
|
|
3284 /* check if the given ccl programs are valid. */
|
|
3285 if (setup_ccl_program (&test_ccl, sym) < 0)
|
|
3286 invalid_argument ("Invalid CCL program", value);
|
|
3287
|
|
3288 if (EQ (key, Qdecode))
|
|
3289 XCODING_SYSTEM_CCL_DECODE (codesys) = sym;
|
|
3290 else if (EQ (key, Qencode))
|
|
3291 XCODING_SYSTEM_CCL_ENCODE (codesys) = sym;
|
|
3292
|
|
3293 return 1;
|
|
3294 }
|
|
3295
|
|
3296 static Lisp_Object
|
|
3297 ccl_getprop (Lisp_Object coding_system, Lisp_Object prop)
|
|
3298 {
|
|
3299 if (EQ (prop, Qdecode))
|
|
3300 return XCODING_SYSTEM_CCL_DECODE (coding_system);
|
|
3301 else if (EQ (prop, Qencode))
|
|
3302 return XCODING_SYSTEM_CCL_ENCODE (coding_system);
|
|
3303 else
|
|
3304 return Qunbound;
|
|
3305 }
|
|
3306
|
|
3307
|
|
3308 /************************************************************************/
|
|
3309 /* Initialization */
|
|
3310 /************************************************************************/
|
|
3311
|
|
3312 void
|
|
3313 syms_of_mule_coding (void)
|
|
3314 {
|
|
3315 DEFSUBR (Fdecode_shift_jis_char);
|
|
3316 DEFSUBR (Fencode_shift_jis_char);
|
|
3317 DEFSUBR (Fdecode_big5_char);
|
|
3318 DEFSUBR (Fencode_big5_char);
|
|
3319
|
|
3320 DEFSYMBOL (Qbig5);
|
|
3321 DEFSYMBOL (Qshift_jis);
|
|
3322 DEFSYMBOL (Qccl);
|
|
3323 DEFSYMBOL (Qiso2022);
|
|
3324
|
|
3325 DEFSYMBOL (Qcharset_g0);
|
|
3326 DEFSYMBOL (Qcharset_g1);
|
|
3327 DEFSYMBOL (Qcharset_g2);
|
|
3328 DEFSYMBOL (Qcharset_g3);
|
|
3329 DEFSYMBOL (Qforce_g0_on_output);
|
|
3330 DEFSYMBOL (Qforce_g1_on_output);
|
|
3331 DEFSYMBOL (Qforce_g2_on_output);
|
|
3332 DEFSYMBOL (Qforce_g3_on_output);
|
|
3333 DEFSYMBOL (Qno_iso6429);
|
|
3334 DEFSYMBOL (Qinput_charset_conversion);
|
|
3335 DEFSYMBOL (Qoutput_charset_conversion);
|
|
3336
|
|
3337 DEFSYMBOL (Qshort);
|
|
3338 DEFSYMBOL (Qno_ascii_eol);
|
|
3339 DEFSYMBOL (Qno_ascii_cntl);
|
|
3340 DEFSYMBOL (Qseven);
|
|
3341 DEFSYMBOL (Qlock_shift);
|
|
3342
|
|
3343 DEFSYMBOL (Qiso_7);
|
|
3344 DEFSYMBOL (Qiso_8_designate);
|
|
3345 DEFSYMBOL (Qiso_8_1);
|
|
3346 DEFSYMBOL (Qiso_8_2);
|
|
3347 DEFSYMBOL (Qiso_lock_shift);
|
|
3348 }
|
|
3349
|
|
3350 void
|
|
3351 coding_system_type_create_mule_coding (void)
|
|
3352 {
|
|
3353 INITIALIZE_CODING_SYSTEM_TYPE_WITH_DATA (iso2022, "iso2022-coding-system-p");
|
|
3354 CODING_SYSTEM_HAS_METHOD (iso2022, mark);
|
|
3355 CODING_SYSTEM_HAS_METHOD (iso2022, convert);
|
|
3356 CODING_SYSTEM_HAS_METHOD (iso2022, finalize_coding_stream);
|
|
3357 CODING_SYSTEM_HAS_METHOD (iso2022, init_coding_stream);
|
|
3358 CODING_SYSTEM_HAS_METHOD (iso2022, rewind_coding_stream);
|
|
3359 CODING_SYSTEM_HAS_METHOD (iso2022, init);
|
|
3360 CODING_SYSTEM_HAS_METHOD (iso2022, print);
|
|
3361 CODING_SYSTEM_HAS_METHOD (iso2022, finalize);
|
|
3362 CODING_SYSTEM_HAS_METHOD (iso2022, putprop);
|
|
3363 CODING_SYSTEM_HAS_METHOD (iso2022, getprop);
|
|
3364
|
|
3365 INITIALIZE_DETECTOR (iso2022);
|
|
3366 DETECTOR_HAS_METHOD (iso2022, detect);
|
|
3367 DETECTOR_HAS_METHOD (iso2022, finalize_detection_state);
|
|
3368 INITIALIZE_DETECTOR_CATEGORY (iso2022, iso_7);
|
|
3369 INITIALIZE_DETECTOR_CATEGORY (iso2022, iso_8_designate);
|
|
3370 INITIALIZE_DETECTOR_CATEGORY (iso2022, iso_8_1);
|
|
3371 INITIALIZE_DETECTOR_CATEGORY (iso2022, iso_8_2);
|
|
3372 INITIALIZE_DETECTOR_CATEGORY (iso2022, iso_lock_shift);
|
|
3373
|
|
3374 INITIALIZE_CODING_SYSTEM_TYPE_WITH_DATA (ccl, "ccl-coding-system-p");
|
|
3375 CODING_SYSTEM_HAS_METHOD (ccl, mark);
|
|
3376 CODING_SYSTEM_HAS_METHOD (ccl, convert);
|
|
3377 CODING_SYSTEM_HAS_METHOD (ccl, init);
|
|
3378 CODING_SYSTEM_HAS_METHOD (ccl, init_coding_stream);
|
|
3379 CODING_SYSTEM_HAS_METHOD (ccl, rewind_coding_stream);
|
|
3380 CODING_SYSTEM_HAS_METHOD (ccl, putprop);
|
|
3381 CODING_SYSTEM_HAS_METHOD (ccl, getprop);
|
|
3382
|
|
3383 INITIALIZE_CODING_SYSTEM_TYPE (shift_jis, "shift-jis-coding-system-p");
|
|
3384 CODING_SYSTEM_HAS_METHOD (shift_jis, convert);
|
|
3385
|
|
3386 INITIALIZE_DETECTOR (shift_jis);
|
|
3387 DETECTOR_HAS_METHOD (shift_jis, detect);
|
|
3388 INITIALIZE_DETECTOR_CATEGORY (shift_jis, shift_jis);
|
|
3389
|
|
3390 INITIALIZE_CODING_SYSTEM_TYPE (big5, "big5-coding-system-p");
|
|
3391 CODING_SYSTEM_HAS_METHOD (big5, convert);
|
|
3392
|
|
3393 INITIALIZE_DETECTOR (big5);
|
|
3394 DETECTOR_HAS_METHOD (big5, detect);
|
|
3395 INITIALIZE_DETECTOR_CATEGORY (big5, big5);
|
|
3396 }
|
|
3397
|
|
3398 void
|
|
3399 reinit_coding_system_type_create_mule_coding (void)
|
|
3400 {
|
|
3401 REINITIALIZE_CODING_SYSTEM_TYPE (iso2022);
|
|
3402 REINITIALIZE_CODING_SYSTEM_TYPE (ccl);
|
|
3403 REINITIALIZE_CODING_SYSTEM_TYPE (shift_jis);
|
|
3404 REINITIALIZE_CODING_SYSTEM_TYPE (big5);
|
|
3405 }
|
|
3406
|
|
3407 void
|
|
3408 reinit_vars_of_mule_coding (void)
|
|
3409 {
|
|
3410 }
|
|
3411
|
|
3412 void
|
|
3413 vars_of_mule_coding (void)
|
|
3414 {
|
|
3415 }
|