* net/soap-client.el:
[bpt/emacs.git] / src / coding.c
1 /* Coding system handler (conversion, detection, etc).
2 Copyright (C) 2001-2011 Free Software Foundation, Inc.
3 Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 2005, 2006, 2007, 2008, 2009, 2010, 2011
5 National Institute of Advanced Industrial Science and Technology (AIST)
6 Registration Number H14PRO021
7 Copyright (C) 2003
8 National Institute of Advanced Industrial Science and Technology (AIST)
9 Registration Number H13PRO009
10
11 This file is part of GNU Emacs.
12
13 GNU Emacs is free software: you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation, either version 3 of the License, or
16 (at your option) any later version.
17
18 GNU Emacs is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
25
26 /*** TABLE OF CONTENTS ***
27
28 0. General comments
29 1. Preamble
30 2. Emacs' internal format (emacs-utf-8) handlers
31 3. UTF-8 handlers
32 4. UTF-16 handlers
33 5. Charset-base coding systems handlers
34 6. emacs-mule (old Emacs' internal format) handlers
35 7. ISO2022 handlers
36 8. Shift-JIS and BIG5 handlers
37 9. CCL handlers
38 10. C library functions
39 11. Emacs Lisp library functions
40 12. Postamble
41
42 */
43
44 /*** 0. General comments ***
45
46
47 CODING SYSTEM
48
49 A coding system is an object for an encoding mechanism that contains
50 information about how to convert byte sequences to character
51 sequences and vice versa. When we say "decode", it means converting
52 a byte sequence of a specific coding system into a character
53 sequence that is represented by Emacs' internal coding system
54 `emacs-utf-8', and when we say "encode", it means converting a
55 character sequence of emacs-utf-8 to a byte sequence of a specific
56 coding system.
57
58 In Emacs Lisp, a coding system is represented by a Lisp symbol. In
59 C level, a coding system is represented by a vector of attributes
60 stored in the hash table Vcharset_hash_table. The conversion from
61 coding system symbol to attributes vector is done by looking up
62 Vcharset_hash_table by the symbol.
63
64 Coding systems are classified into the following types depending on
65 the encoding mechanism. Here's a brief description of the types.
66
67 o UTF-8
68
69 o UTF-16
70
71 o Charset-base coding system
72
73 A coding system defined by one or more (coded) character sets.
74 Decoding and encoding are done by a code converter defined for each
75 character set.
76
77 o Old Emacs internal format (emacs-mule)
78
79 The coding system adopted by old versions of Emacs (20 and 21).
80
81 o ISO2022-base coding system
82
83 The most famous coding system for multiple character sets. X's
84 Compound Text, various EUCs (Extended Unix Code), and coding systems
85 used in the Internet communication such as ISO-2022-JP are all
86 variants of ISO2022.
87
88 o SJIS (or Shift-JIS or MS-Kanji-Code)
89
90 A coding system to encode character sets: ASCII, JISX0201, and
91 JISX0208. Widely used for PC's in Japan. Details are described in
92 section 8.
93
94 o BIG5
95
96 A coding system to encode character sets: ASCII and Big5. Widely
97 used for Chinese (mainly in Taiwan and Hong Kong). Details are
98 described in section 8. In this file, when we write "big5" (all
99 lowercase), we mean the coding system, and when we write "Big5"
100 (capitalized), we mean the character set.
101
102 o CCL
103
104 If a user wants to decode/encode text encoded in a coding system
105 not listed above, he can supply a decoder and an encoder for it in
106 CCL (Code Conversion Language) programs. Emacs executes the CCL
107 program while decoding/encoding.
108
109 o Raw-text
110
111 A coding system for text containing raw eight-bit data. Emacs
112 treats each byte of source text as a character (except for
113 end-of-line conversion).
114
115 o No-conversion
116
117 Like raw text, but don't do end-of-line conversion.
118
119
120 END-OF-LINE FORMAT
121
122 How text end-of-line is encoded depends on operating system. For
123 instance, Unix's format is just one byte of LF (line-feed) code,
124 whereas DOS's format is two-byte sequence of `carriage-return' and
125 `line-feed' codes. MacOS's format is usually one byte of
126 `carriage-return'.
127
128 Since text character encoding and end-of-line encoding are
129 independent, any coding system described above can take any format
130 of end-of-line (except for no-conversion).
131
132 STRUCT CODING_SYSTEM
133
134 Before using a coding system for code conversion (i.e. decoding and
135 encoding), we setup a structure of type `struct coding_system'.
136 This structure keeps various information about a specific code
137 conversion (e.g. the location of source and destination data).
138
139 */
140
141 /* COMMON MACROS */
142
143
144 /*** GENERAL NOTES on `detect_coding_XXX ()' functions ***
145
146 These functions check if a byte sequence specified as a source in
147 CODING conforms to the format of XXX, and update the members of
148 DETECT_INFO.
149
150 Return 1 if the byte sequence conforms to XXX, otherwise return 0.
151
152 Below is the template of these functions. */
153
154 #if 0
155 static int
156 detect_coding_XXX (struct coding_system *coding,
157 struct coding_detection_info *detect_info)
158 {
159 const unsigned char *src = coding->source;
160 const unsigned char *src_end = coding->source + coding->src_bytes;
161 int multibytep = coding->src_multibyte;
162 int consumed_chars = 0;
163 int found = 0;
164 ...;
165
166 while (1)
167 {
168 /* Get one byte from the source. If the source is exhausted, jump
169 to no_more_source:. */
170 ONE_MORE_BYTE (c);
171
172 if (! __C_conforms_to_XXX___ (c))
173 break;
174 if (! __C_strongly_suggests_XXX__ (c))
175 found = CATEGORY_MASK_XXX;
176 }
177 /* The byte sequence is invalid for XXX. */
178 detect_info->rejected |= CATEGORY_MASK_XXX;
179 return 0;
180
181 no_more_source:
182 /* The source exhausted successfully. */
183 detect_info->found |= found;
184 return 1;
185 }
186 #endif
187
188 /*** GENERAL NOTES on `decode_coding_XXX ()' functions ***
189
190 These functions decode a byte sequence specified as a source by
191 CODING. The resulting multibyte text goes to a place pointed to by
192 CODING->charbuf, the length of which should not exceed
193 CODING->charbuf_size;
194
195 These functions set the information of original and decoded texts in
196 CODING->consumed, CODING->consumed_char, and CODING->charbuf_used.
197 They also set CODING->result to one of CODING_RESULT_XXX indicating
198 how the decoding is finished.
199
200 Below is the template of these functions. */
201
202 #if 0
203 static void
204 decode_coding_XXXX (struct coding_system *coding)
205 {
206 const unsigned char *src = coding->source + coding->consumed;
207 const unsigned char *src_end = coding->source + coding->src_bytes;
208 /* SRC_BASE remembers the start position in source in each loop.
209 The loop will be exited when there's not enough source code, or
210 when there's no room in CHARBUF for a decoded character. */
211 const unsigned char *src_base;
212 /* A buffer to produce decoded characters. */
213 int *charbuf = coding->charbuf + coding->charbuf_used;
214 int *charbuf_end = coding->charbuf + coding->charbuf_size;
215 int multibytep = coding->src_multibyte;
216
217 while (1)
218 {
219 src_base = src;
220 if (charbuf < charbuf_end)
221 /* No more room to produce a decoded character. */
222 break;
223 ONE_MORE_BYTE (c);
224 /* Decode it. */
225 }
226
227 no_more_source:
228 if (src_base < src_end
229 && coding->mode & CODING_MODE_LAST_BLOCK)
230 /* If the source ends by partial bytes to construct a character,
231 treat them as eight-bit raw data. */
232 while (src_base < src_end && charbuf < charbuf_end)
233 *charbuf++ = *src_base++;
234 /* Remember how many bytes and characters we consumed. If the
235 source is multibyte, the bytes and chars are not identical. */
236 coding->consumed = coding->consumed_char = src_base - coding->source;
237 /* Remember how many characters we produced. */
238 coding->charbuf_used = charbuf - coding->charbuf;
239 }
240 #endif
241
242 /*** GENERAL NOTES on `encode_coding_XXX ()' functions ***
243
244 These functions encode SRC_BYTES length text at SOURCE of Emacs'
245 internal multibyte format by CODING. The resulting byte sequence
246 goes to a place pointed to by DESTINATION, the length of which
247 should not exceed DST_BYTES.
248
249 These functions set the information of original and encoded texts in
250 the members produced, produced_char, consumed, and consumed_char of
251 the structure *CODING. They also set the member result to one of
252 CODING_RESULT_XXX indicating how the encoding finished.
253
254 DST_BYTES zero means that source area and destination area are
255 overlapped, which means that we can produce a encoded text until it
256 reaches at the head of not-yet-encoded source text.
257
258 Below is a template of these functions. */
259 #if 0
260 static void
261 encode_coding_XXX (struct coding_system *coding)
262 {
263 int multibytep = coding->dst_multibyte;
264 int *charbuf = coding->charbuf;
265 int *charbuf_end = charbuf->charbuf + coding->charbuf_used;
266 unsigned char *dst = coding->destination + coding->produced;
267 unsigned char *dst_end = coding->destination + coding->dst_bytes;
268 unsigned char *adjusted_dst_end = dst_end - _MAX_BYTES_PRODUCED_IN_LOOP_;
269 int produced_chars = 0;
270
271 for (; charbuf < charbuf_end && dst < adjusted_dst_end; charbuf++)
272 {
273 int c = *charbuf;
274 /* Encode C into DST, and increment DST. */
275 }
276 label_no_more_destination:
277 /* How many chars and bytes we produced. */
278 coding->produced_char += produced_chars;
279 coding->produced = dst - coding->destination;
280 }
281 #endif
282
283 \f
284 /*** 1. Preamble ***/
285
286 #include <config.h>
287 #include <stdio.h>
288 #include <setjmp.h>
289
290 #include "lisp.h"
291 #include "buffer.h"
292 #include "character.h"
293 #include "charset.h"
294 #include "ccl.h"
295 #include "composite.h"
296 #include "coding.h"
297 #include "window.h"
298 #include "frame.h"
299 #include "termhooks.h"
300
301 Lisp_Object Vcoding_system_hash_table;
302
303 Lisp_Object Qcoding_system, Qcoding_aliases, Qeol_type;
304 Lisp_Object Qunix, Qdos;
305 Lisp_Object Qbuffer_file_coding_system;
306 Lisp_Object Qpost_read_conversion, Qpre_write_conversion;
307 Lisp_Object Qdefault_char;
308 Lisp_Object Qno_conversion, Qundecided;
309 Lisp_Object Qcharset, Qiso_2022, Qutf_8, Qutf_16, Qshift_jis, Qbig5;
310 Lisp_Object Qbig, Qlittle;
311 Lisp_Object Qcoding_system_history;
312 Lisp_Object Qvalid_codes;
313 Lisp_Object QCcategory, QCmnemonic, QCdefault_char;
314 Lisp_Object QCdecode_translation_table, QCencode_translation_table;
315 Lisp_Object QCpost_read_conversion, QCpre_write_conversion;
316 Lisp_Object QCascii_compatible_p;
317
318 Lisp_Object Qcall_process, Qcall_process_region;
319 Lisp_Object Qstart_process, Qopen_network_stream;
320 Lisp_Object Qtarget_idx;
321
322 Lisp_Object Qinsufficient_source, Qinconsistent_eol, Qinvalid_source;
323 Lisp_Object Qinterrupted, Qinsufficient_memory;
324
325 /* If a symbol has this property, evaluate the value to define the
326 symbol as a coding system. */
327 static Lisp_Object Qcoding_system_define_form;
328
329 /* Format of end-of-line decided by system. This is Qunix on
330 Unix and Mac, Qdos on DOS/Windows.
331 This has an effect only for external encoding (i.e. for output to
332 file and process), not for in-buffer or Lisp string encoding. */
333 static Lisp_Object system_eol_type;
334
335 #ifdef emacs
336
337 Lisp_Object Qcoding_system_p, Qcoding_system_error;
338
339 /* Coding system emacs-mule and raw-text are for converting only
340 end-of-line format. */
341 Lisp_Object Qemacs_mule, Qraw_text;
342 Lisp_Object Qutf_8_emacs;
343
344 /* Coding-systems are handed between Emacs Lisp programs and C internal
345 routines by the following three variables. */
346 /* Coding system to be used to encode text for terminal display when
347 terminal coding system is nil. */
348 struct coding_system safe_terminal_coding;
349
350 #endif /* emacs */
351
352 Lisp_Object Qtranslation_table;
353 Lisp_Object Qtranslation_table_id;
354 Lisp_Object Qtranslation_table_for_decode;
355 Lisp_Object Qtranslation_table_for_encode;
356
357 /* Two special coding systems. */
358 Lisp_Object Vsjis_coding_system;
359 Lisp_Object Vbig5_coding_system;
360
361 /* ISO2022 section */
362
363 #define CODING_ISO_INITIAL(coding, reg) \
364 (XINT (AREF (AREF (CODING_ID_ATTRS ((coding)->id), \
365 coding_attr_iso_initial), \
366 reg)))
367
368
369 #define CODING_ISO_REQUEST(coding, charset_id) \
370 (((charset_id) <= (coding)->max_charset_id \
371 ? ((coding)->safe_charsets[charset_id] != 255 \
372 ? (coding)->safe_charsets[charset_id] \
373 : -1) \
374 : -1))
375
376
377 #define CODING_ISO_FLAGS(coding) \
378 ((coding)->spec.iso_2022.flags)
379 #define CODING_ISO_DESIGNATION(coding, reg) \
380 ((coding)->spec.iso_2022.current_designation[reg])
381 #define CODING_ISO_INVOCATION(coding, plane) \
382 ((coding)->spec.iso_2022.current_invocation[plane])
383 #define CODING_ISO_SINGLE_SHIFTING(coding) \
384 ((coding)->spec.iso_2022.single_shifting)
385 #define CODING_ISO_BOL(coding) \
386 ((coding)->spec.iso_2022.bol)
387 #define CODING_ISO_INVOKED_CHARSET(coding, plane) \
388 CODING_ISO_DESIGNATION ((coding), CODING_ISO_INVOCATION ((coding), (plane)))
389 #define CODING_ISO_CMP_STATUS(coding) \
390 (&(coding)->spec.iso_2022.cmp_status)
391 #define CODING_ISO_EXTSEGMENT_LEN(coding) \
392 ((coding)->spec.iso_2022.ctext_extended_segment_len)
393 #define CODING_ISO_EMBEDDED_UTF_8(coding) \
394 ((coding)->spec.iso_2022.embedded_utf_8)
395
396 /* Control characters of ISO2022. */
397 /* code */ /* function */
398 #define ISO_CODE_LF 0x0A /* line-feed */
399 #define ISO_CODE_CR 0x0D /* carriage-return */
400 #define ISO_CODE_SO 0x0E /* shift-out */
401 #define ISO_CODE_SI 0x0F /* shift-in */
402 #define ISO_CODE_SS2_7 0x19 /* single-shift-2 for 7-bit code */
403 #define ISO_CODE_ESC 0x1B /* escape */
404 #define ISO_CODE_SS2 0x8E /* single-shift-2 */
405 #define ISO_CODE_SS3 0x8F /* single-shift-3 */
406 #define ISO_CODE_CSI 0x9B /* control-sequence-introducer */
407
408 /* All code (1-byte) of ISO2022 is classified into one of the
409 followings. */
410 enum iso_code_class_type
411 {
412 ISO_control_0, /* Control codes in the range
413 0x00..0x1F and 0x7F, except for the
414 following 5 codes. */
415 ISO_shift_out, /* ISO_CODE_SO (0x0E) */
416 ISO_shift_in, /* ISO_CODE_SI (0x0F) */
417 ISO_single_shift_2_7, /* ISO_CODE_SS2_7 (0x19) */
418 ISO_escape, /* ISO_CODE_SO (0x1B) */
419 ISO_control_1, /* Control codes in the range
420 0x80..0x9F, except for the
421 following 3 codes. */
422 ISO_single_shift_2, /* ISO_CODE_SS2 (0x8E) */
423 ISO_single_shift_3, /* ISO_CODE_SS3 (0x8F) */
424 ISO_control_sequence_introducer, /* ISO_CODE_CSI (0x9B) */
425 ISO_0x20_or_0x7F, /* Codes of the values 0x20 or 0x7F. */
426 ISO_graphic_plane_0, /* Graphic codes in the range 0x21..0x7E. */
427 ISO_0xA0_or_0xFF, /* Codes of the values 0xA0 or 0xFF. */
428 ISO_graphic_plane_1 /* Graphic codes in the range 0xA1..0xFE. */
429 };
430
431 /** The macros CODING_ISO_FLAG_XXX defines a flag bit of the
432 `iso-flags' attribute of an iso2022 coding system. */
433
434 /* If set, produce long-form designation sequence (e.g. ESC $ ( A)
435 instead of the correct short-form sequence (e.g. ESC $ A). */
436 #define CODING_ISO_FLAG_LONG_FORM 0x0001
437
438 /* If set, reset graphic planes and registers at end-of-line to the
439 initial state. */
440 #define CODING_ISO_FLAG_RESET_AT_EOL 0x0002
441
442 /* If set, reset graphic planes and registers before any control
443 characters to the initial state. */
444 #define CODING_ISO_FLAG_RESET_AT_CNTL 0x0004
445
446 /* If set, encode by 7-bit environment. */
447 #define CODING_ISO_FLAG_SEVEN_BITS 0x0008
448
449 /* If set, use locking-shift function. */
450 #define CODING_ISO_FLAG_LOCKING_SHIFT 0x0010
451
452 /* If set, use single-shift function. Overwrite
453 CODING_ISO_FLAG_LOCKING_SHIFT. */
454 #define CODING_ISO_FLAG_SINGLE_SHIFT 0x0020
455
456 /* If set, use designation escape sequence. */
457 #define CODING_ISO_FLAG_DESIGNATION 0x0040
458
459 /* If set, produce revision number sequence. */
460 #define CODING_ISO_FLAG_REVISION 0x0080
461
462 /* If set, produce ISO6429's direction specifying sequence. */
463 #define CODING_ISO_FLAG_DIRECTION 0x0100
464
465 /* If set, assume designation states are reset at beginning of line on
466 output. */
467 #define CODING_ISO_FLAG_INIT_AT_BOL 0x0200
468
469 /* If set, designation sequence should be placed at beginning of line
470 on output. */
471 #define CODING_ISO_FLAG_DESIGNATE_AT_BOL 0x0400
472
473 /* If set, do not encode unsafe characters on output. */
474 #define CODING_ISO_FLAG_SAFE 0x0800
475
476 /* If set, extra latin codes (128..159) are accepted as a valid code
477 on input. */
478 #define CODING_ISO_FLAG_LATIN_EXTRA 0x1000
479
480 #define CODING_ISO_FLAG_COMPOSITION 0x2000
481
482 #define CODING_ISO_FLAG_EUC_TW_SHIFT 0x4000
483
484 #define CODING_ISO_FLAG_USE_ROMAN 0x8000
485
486 #define CODING_ISO_FLAG_USE_OLDJIS 0x10000
487
488 #define CODING_ISO_FLAG_FULL_SUPPORT 0x100000
489
490 /* A character to be produced on output if encoding of the original
491 character is prohibited by CODING_ISO_FLAG_SAFE. */
492 #define CODING_INHIBIT_CHARACTER_SUBSTITUTION '?'
493
494 /* UTF-8 section */
495 #define CODING_UTF_8_BOM(coding) \
496 ((coding)->spec.utf_8_bom)
497
498 /* UTF-16 section */
499 #define CODING_UTF_16_BOM(coding) \
500 ((coding)->spec.utf_16.bom)
501
502 #define CODING_UTF_16_ENDIAN(coding) \
503 ((coding)->spec.utf_16.endian)
504
505 #define CODING_UTF_16_SURROGATE(coding) \
506 ((coding)->spec.utf_16.surrogate)
507
508
509 /* CCL section */
510 #define CODING_CCL_DECODER(coding) \
511 AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_decoder)
512 #define CODING_CCL_ENCODER(coding) \
513 AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_encoder)
514 #define CODING_CCL_VALIDS(coding) \
515 (SDATA (AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_valids)))
516
517 /* Index for each coding category in `coding_categories' */
518
519 enum coding_category
520 {
521 coding_category_iso_7,
522 coding_category_iso_7_tight,
523 coding_category_iso_8_1,
524 coding_category_iso_8_2,
525 coding_category_iso_7_else,
526 coding_category_iso_8_else,
527 coding_category_utf_8_auto,
528 coding_category_utf_8_nosig,
529 coding_category_utf_8_sig,
530 coding_category_utf_16_auto,
531 coding_category_utf_16_be,
532 coding_category_utf_16_le,
533 coding_category_utf_16_be_nosig,
534 coding_category_utf_16_le_nosig,
535 coding_category_charset,
536 coding_category_sjis,
537 coding_category_big5,
538 coding_category_ccl,
539 coding_category_emacs_mule,
540 /* All above are targets of code detection. */
541 coding_category_raw_text,
542 coding_category_undecided,
543 coding_category_max
544 };
545
546 /* Definitions of flag bits used in detect_coding_XXXX. */
547 #define CATEGORY_MASK_ISO_7 (1 << coding_category_iso_7)
548 #define CATEGORY_MASK_ISO_7_TIGHT (1 << coding_category_iso_7_tight)
549 #define CATEGORY_MASK_ISO_8_1 (1 << coding_category_iso_8_1)
550 #define CATEGORY_MASK_ISO_8_2 (1 << coding_category_iso_8_2)
551 #define CATEGORY_MASK_ISO_7_ELSE (1 << coding_category_iso_7_else)
552 #define CATEGORY_MASK_ISO_8_ELSE (1 << coding_category_iso_8_else)
553 #define CATEGORY_MASK_UTF_8_AUTO (1 << coding_category_utf_8_auto)
554 #define CATEGORY_MASK_UTF_8_NOSIG (1 << coding_category_utf_8_nosig)
555 #define CATEGORY_MASK_UTF_8_SIG (1 << coding_category_utf_8_sig)
556 #define CATEGORY_MASK_UTF_16_AUTO (1 << coding_category_utf_16_auto)
557 #define CATEGORY_MASK_UTF_16_BE (1 << coding_category_utf_16_be)
558 #define CATEGORY_MASK_UTF_16_LE (1 << coding_category_utf_16_le)
559 #define CATEGORY_MASK_UTF_16_BE_NOSIG (1 << coding_category_utf_16_be_nosig)
560 #define CATEGORY_MASK_UTF_16_LE_NOSIG (1 << coding_category_utf_16_le_nosig)
561 #define CATEGORY_MASK_CHARSET (1 << coding_category_charset)
562 #define CATEGORY_MASK_SJIS (1 << coding_category_sjis)
563 #define CATEGORY_MASK_BIG5 (1 << coding_category_big5)
564 #define CATEGORY_MASK_CCL (1 << coding_category_ccl)
565 #define CATEGORY_MASK_EMACS_MULE (1 << coding_category_emacs_mule)
566 #define CATEGORY_MASK_RAW_TEXT (1 << coding_category_raw_text)
567
568 /* This value is returned if detect_coding_mask () find nothing other
569 than ASCII characters. */
570 #define CATEGORY_MASK_ANY \
571 (CATEGORY_MASK_ISO_7 \
572 | CATEGORY_MASK_ISO_7_TIGHT \
573 | CATEGORY_MASK_ISO_8_1 \
574 | CATEGORY_MASK_ISO_8_2 \
575 | CATEGORY_MASK_ISO_7_ELSE \
576 | CATEGORY_MASK_ISO_8_ELSE \
577 | CATEGORY_MASK_UTF_8_AUTO \
578 | CATEGORY_MASK_UTF_8_NOSIG \
579 | CATEGORY_MASK_UTF_8_SIG \
580 | CATEGORY_MASK_UTF_16_AUTO \
581 | CATEGORY_MASK_UTF_16_BE \
582 | CATEGORY_MASK_UTF_16_LE \
583 | CATEGORY_MASK_UTF_16_BE_NOSIG \
584 | CATEGORY_MASK_UTF_16_LE_NOSIG \
585 | CATEGORY_MASK_CHARSET \
586 | CATEGORY_MASK_SJIS \
587 | CATEGORY_MASK_BIG5 \
588 | CATEGORY_MASK_CCL \
589 | CATEGORY_MASK_EMACS_MULE)
590
591
592 #define CATEGORY_MASK_ISO_7BIT \
593 (CATEGORY_MASK_ISO_7 | CATEGORY_MASK_ISO_7_TIGHT)
594
595 #define CATEGORY_MASK_ISO_8BIT \
596 (CATEGORY_MASK_ISO_8_1 | CATEGORY_MASK_ISO_8_2)
597
598 #define CATEGORY_MASK_ISO_ELSE \
599 (CATEGORY_MASK_ISO_7_ELSE | CATEGORY_MASK_ISO_8_ELSE)
600
601 #define CATEGORY_MASK_ISO_ESCAPE \
602 (CATEGORY_MASK_ISO_7 \
603 | CATEGORY_MASK_ISO_7_TIGHT \
604 | CATEGORY_MASK_ISO_7_ELSE \
605 | CATEGORY_MASK_ISO_8_ELSE)
606
607 #define CATEGORY_MASK_ISO \
608 ( CATEGORY_MASK_ISO_7BIT \
609 | CATEGORY_MASK_ISO_8BIT \
610 | CATEGORY_MASK_ISO_ELSE)
611
612 #define CATEGORY_MASK_UTF_16 \
613 (CATEGORY_MASK_UTF_16_AUTO \
614 | CATEGORY_MASK_UTF_16_BE \
615 | CATEGORY_MASK_UTF_16_LE \
616 | CATEGORY_MASK_UTF_16_BE_NOSIG \
617 | CATEGORY_MASK_UTF_16_LE_NOSIG)
618
619 #define CATEGORY_MASK_UTF_8 \
620 (CATEGORY_MASK_UTF_8_AUTO \
621 | CATEGORY_MASK_UTF_8_NOSIG \
622 | CATEGORY_MASK_UTF_8_SIG)
623
624 /* Table of coding categories (Lisp symbols). This variable is for
625 internal use only. */
626 static Lisp_Object Vcoding_category_table;
627
628 /* Table of coding-categories ordered by priority. */
629 static enum coding_category coding_priorities[coding_category_max];
630
631 /* Nth element is a coding context for the coding system bound to the
632 Nth coding category. */
633 static struct coding_system coding_categories[coding_category_max];
634
635 /*** Commonly used macros and functions ***/
636
637 #ifndef min
638 #define min(a, b) ((a) < (b) ? (a) : (b))
639 #endif
640 #ifndef max
641 #define max(a, b) ((a) > (b) ? (a) : (b))
642 #endif
643
644 #define CODING_GET_INFO(coding, attrs, charset_list) \
645 do { \
646 (attrs) = CODING_ID_ATTRS ((coding)->id); \
647 (charset_list) = CODING_ATTR_CHARSET_LIST (attrs); \
648 } while (0)
649
650
651 /* Safely get one byte from the source text pointed by SRC which ends
652 at SRC_END, and set C to that byte. If there are not enough bytes
653 in the source, it jumps to `no_more_source'. If multibytep is
654 nonzero, and a multibyte character is found at SRC, set C to the
655 negative value of the character code. The caller should declare
656 and set these variables appropriately in advance:
657 src, src_end, multibytep */
658
659 #define ONE_MORE_BYTE(c) \
660 do { \
661 if (src == src_end) \
662 { \
663 if (src_base < src) \
664 record_conversion_result \
665 (coding, CODING_RESULT_INSUFFICIENT_SRC); \
666 goto no_more_source; \
667 } \
668 c = *src++; \
669 if (multibytep && (c & 0x80)) \
670 { \
671 if ((c & 0xFE) == 0xC0) \
672 c = ((c & 1) << 6) | *src++; \
673 else \
674 { \
675 src--; \
676 c = - string_char (src, &src, NULL); \
677 record_conversion_result \
678 (coding, CODING_RESULT_INVALID_SRC); \
679 } \
680 } \
681 consumed_chars++; \
682 } while (0)
683
684 /* Safely get two bytes from the source text pointed by SRC which ends
685 at SRC_END, and set C1 and C2 to those bytes while skipping the
686 heading multibyte characters. If there are not enough bytes in the
687 source, it jumps to `no_more_source'. If multibytep is nonzero and
688 a multibyte character is found for C2, set C2 to the negative value
689 of the character code. The caller should declare and set these
690 variables appropriately in advance:
691 src, src_end, multibytep
692 It is intended that this macro is used in detect_coding_utf_16. */
693
694 #define TWO_MORE_BYTES(c1, c2) \
695 do { \
696 do { \
697 if (src == src_end) \
698 goto no_more_source; \
699 c1 = *src++; \
700 if (multibytep && (c1 & 0x80)) \
701 { \
702 if ((c1 & 0xFE) == 0xC0) \
703 c1 = ((c1 & 1) << 6) | *src++; \
704 else \
705 { \
706 src += BYTES_BY_CHAR_HEAD (c1) - 1; \
707 c1 = -1; \
708 } \
709 } \
710 } while (c1 < 0); \
711 if (src == src_end) \
712 goto no_more_source; \
713 c2 = *src++; \
714 if (multibytep && (c2 & 0x80)) \
715 { \
716 if ((c2 & 0xFE) == 0xC0) \
717 c2 = ((c2 & 1) << 6) | *src++; \
718 else \
719 c2 = -1; \
720 } \
721 } while (0)
722
723
724 #define ONE_MORE_BYTE_NO_CHECK(c) \
725 do { \
726 c = *src++; \
727 if (multibytep && (c & 0x80)) \
728 { \
729 if ((c & 0xFE) == 0xC0) \
730 c = ((c & 1) << 6) | *src++; \
731 else \
732 { \
733 src--; \
734 c = - string_char (src, &src, NULL); \
735 record_conversion_result \
736 (coding, CODING_RESULT_INVALID_SRC); \
737 } \
738 } \
739 consumed_chars++; \
740 } while (0)
741
742
743 /* Store a byte C in the place pointed by DST and increment DST to the
744 next free point, and increment PRODUCED_CHARS. The caller should
745 assure that C is 0..127, and declare and set the variable `dst'
746 appropriately in advance.
747 */
748
749
750 #define EMIT_ONE_ASCII_BYTE(c) \
751 do { \
752 produced_chars++; \
753 *dst++ = (c); \
754 } while (0)
755
756
757 /* Like EMIT_ONE_ASCII_BYTE but store two bytes; C1 and C2. */
758
759 #define EMIT_TWO_ASCII_BYTES(c1, c2) \
760 do { \
761 produced_chars += 2; \
762 *dst++ = (c1), *dst++ = (c2); \
763 } while (0)
764
765
766 /* Store a byte C in the place pointed by DST and increment DST to the
767 next free point, and increment PRODUCED_CHARS. If MULTIBYTEP is
768 nonzero, store in an appropriate multibyte from. The caller should
769 declare and set the variables `dst' and `multibytep' appropriately
770 in advance. */
771
772 #define EMIT_ONE_BYTE(c) \
773 do { \
774 produced_chars++; \
775 if (multibytep) \
776 { \
777 int ch = (c); \
778 if (ch >= 0x80) \
779 ch = BYTE8_TO_CHAR (ch); \
780 CHAR_STRING_ADVANCE (ch, dst); \
781 } \
782 else \
783 *dst++ = (c); \
784 } while (0)
785
786
787 /* Like EMIT_ONE_BYTE, but emit two bytes; C1 and C2. */
788
789 #define EMIT_TWO_BYTES(c1, c2) \
790 do { \
791 produced_chars += 2; \
792 if (multibytep) \
793 { \
794 int ch; \
795 \
796 ch = (c1); \
797 if (ch >= 0x80) \
798 ch = BYTE8_TO_CHAR (ch); \
799 CHAR_STRING_ADVANCE (ch, dst); \
800 ch = (c2); \
801 if (ch >= 0x80) \
802 ch = BYTE8_TO_CHAR (ch); \
803 CHAR_STRING_ADVANCE (ch, dst); \
804 } \
805 else \
806 { \
807 *dst++ = (c1); \
808 *dst++ = (c2); \
809 } \
810 } while (0)
811
812
813 #define EMIT_THREE_BYTES(c1, c2, c3) \
814 do { \
815 EMIT_ONE_BYTE (c1); \
816 EMIT_TWO_BYTES (c2, c3); \
817 } while (0)
818
819
820 #define EMIT_FOUR_BYTES(c1, c2, c3, c4) \
821 do { \
822 EMIT_TWO_BYTES (c1, c2); \
823 EMIT_TWO_BYTES (c3, c4); \
824 } while (0)
825
826
827 /* Prototypes for static functions. */
828 static void record_conversion_result (struct coding_system *coding,
829 enum coding_result_code result);
830 static int detect_coding_utf_8 (struct coding_system *,
831 struct coding_detection_info *info);
832 static void decode_coding_utf_8 (struct coding_system *);
833 static int encode_coding_utf_8 (struct coding_system *);
834
835 static int detect_coding_utf_16 (struct coding_system *,
836 struct coding_detection_info *info);
837 static void decode_coding_utf_16 (struct coding_system *);
838 static int encode_coding_utf_16 (struct coding_system *);
839
840 static int detect_coding_iso_2022 (struct coding_system *,
841 struct coding_detection_info *info);
842 static void decode_coding_iso_2022 (struct coding_system *);
843 static int encode_coding_iso_2022 (struct coding_system *);
844
845 static int detect_coding_emacs_mule (struct coding_system *,
846 struct coding_detection_info *info);
847 static void decode_coding_emacs_mule (struct coding_system *);
848 static int encode_coding_emacs_mule (struct coding_system *);
849
850 static int detect_coding_sjis (struct coding_system *,
851 struct coding_detection_info *info);
852 static void decode_coding_sjis (struct coding_system *);
853 static int encode_coding_sjis (struct coding_system *);
854
855 static int detect_coding_big5 (struct coding_system *,
856 struct coding_detection_info *info);
857 static void decode_coding_big5 (struct coding_system *);
858 static int encode_coding_big5 (struct coding_system *);
859
860 static int detect_coding_ccl (struct coding_system *,
861 struct coding_detection_info *info);
862 static void decode_coding_ccl (struct coding_system *);
863 static int encode_coding_ccl (struct coding_system *);
864
865 static void decode_coding_raw_text (struct coding_system *);
866 static int encode_coding_raw_text (struct coding_system *);
867
868 static void coding_set_source (struct coding_system *);
869 static void coding_set_destination (struct coding_system *);
870 static void coding_alloc_by_realloc (struct coding_system *, EMACS_INT);
871 static void coding_alloc_by_making_gap (struct coding_system *,
872 EMACS_INT, EMACS_INT);
873 static unsigned char *alloc_destination (struct coding_system *,
874 EMACS_INT, unsigned char *);
875 static void setup_iso_safe_charsets (Lisp_Object);
876 static unsigned char *encode_designation_at_bol (struct coding_system *,
877 int *, int *,
878 unsigned char *);
879 static int detect_eol (const unsigned char *,
880 EMACS_INT, enum coding_category);
881 static Lisp_Object adjust_coding_eol_type (struct coding_system *, int);
882 static void decode_eol (struct coding_system *);
883 static Lisp_Object get_translation_table (Lisp_Object, int, int *);
884 static Lisp_Object get_translation (Lisp_Object, int *, int *);
885 static int produce_chars (struct coding_system *, Lisp_Object, int);
886 static INLINE void produce_charset (struct coding_system *, int *,
887 EMACS_INT);
888 static void produce_annotation (struct coding_system *, EMACS_INT);
889 static int decode_coding (struct coding_system *);
890 static INLINE int *handle_composition_annotation (EMACS_INT, EMACS_INT,
891 struct coding_system *,
892 int *, EMACS_INT *);
893 static INLINE int *handle_charset_annotation (EMACS_INT, EMACS_INT,
894 struct coding_system *,
895 int *, EMACS_INT *);
896 static void consume_chars (struct coding_system *, Lisp_Object, int);
897 static int encode_coding (struct coding_system *);
898 static Lisp_Object make_conversion_work_buffer (int);
899 static Lisp_Object code_conversion_restore (Lisp_Object);
900 static INLINE int char_encodable_p (int, Lisp_Object);
901 static Lisp_Object make_subsidiaries (Lisp_Object);
902
903 static void
904 record_conversion_result (struct coding_system *coding,
905 enum coding_result_code result)
906 {
907 coding->result = result;
908 switch (result)
909 {
910 case CODING_RESULT_INSUFFICIENT_SRC:
911 Vlast_code_conversion_error = Qinsufficient_source;
912 break;
913 case CODING_RESULT_INCONSISTENT_EOL:
914 Vlast_code_conversion_error = Qinconsistent_eol;
915 break;
916 case CODING_RESULT_INVALID_SRC:
917 Vlast_code_conversion_error = Qinvalid_source;
918 break;
919 case CODING_RESULT_INTERRUPT:
920 Vlast_code_conversion_error = Qinterrupted;
921 break;
922 case CODING_RESULT_INSUFFICIENT_MEM:
923 Vlast_code_conversion_error = Qinsufficient_memory;
924 break;
925 case CODING_RESULT_INSUFFICIENT_DST:
926 /* Don't record this error in Vlast_code_conversion_error
927 because it happens just temporarily and is resolved when the
928 whole conversion is finished. */
929 break;
930 case CODING_RESULT_SUCCESS:
931 break;
932 default:
933 Vlast_code_conversion_error = intern ("Unknown error");
934 }
935 }
936
937 /* This wrapper macro is used to preserve validity of pointers into
938 buffer text across calls to decode_char, which could cause
939 relocation of buffers if it loads a charset map, because loading a
940 charset map allocates large structures. */
941 #define CODING_DECODE_CHAR(coding, src, src_base, src_end, charset, code, c) \
942 do { \
943 charset_map_loaded = 0; \
944 c = DECODE_CHAR (charset, code); \
945 if (charset_map_loaded) \
946 { \
947 const unsigned char *orig = coding->source; \
948 EMACS_INT offset; \
949 \
950 coding_set_source (coding); \
951 offset = coding->source - orig; \
952 src += offset; \
953 src_base += offset; \
954 src_end += offset; \
955 } \
956 } while (0)
957
958
959 /* If there are at least BYTES length of room at dst, allocate memory
960 for coding->destination and update dst and dst_end. We don't have
961 to take care of coding->source which will be relocated. It is
962 handled by calling coding_set_source in encode_coding. */
963
964 #define ASSURE_DESTINATION(bytes) \
965 do { \
966 if (dst + (bytes) >= dst_end) \
967 { \
968 int more_bytes = charbuf_end - charbuf + (bytes); \
969 \
970 dst = alloc_destination (coding, more_bytes, dst); \
971 dst_end = coding->destination + coding->dst_bytes; \
972 } \
973 } while (0)
974
975
976 /* Store multibyte form of the character C in P, and advance P to the
977 end of the multibyte form. This is like CHAR_STRING_ADVANCE but it
978 never calls MAYBE_UNIFY_CHAR. */
979
980 #define CHAR_STRING_ADVANCE_NO_UNIFY(c, p) \
981 do { \
982 if ((c) <= MAX_1_BYTE_CHAR) \
983 *(p)++ = (c); \
984 else if ((c) <= MAX_2_BYTE_CHAR) \
985 *(p)++ = (0xC0 | ((c) >> 6)), \
986 *(p)++ = (0x80 | ((c) & 0x3F)); \
987 else if ((c) <= MAX_3_BYTE_CHAR) \
988 *(p)++ = (0xE0 | ((c) >> 12)), \
989 *(p)++ = (0x80 | (((c) >> 6) & 0x3F)), \
990 *(p)++ = (0x80 | ((c) & 0x3F)); \
991 else if ((c) <= MAX_4_BYTE_CHAR) \
992 *(p)++ = (0xF0 | (c >> 18)), \
993 *(p)++ = (0x80 | ((c >> 12) & 0x3F)), \
994 *(p)++ = (0x80 | ((c >> 6) & 0x3F)), \
995 *(p)++ = (0x80 | (c & 0x3F)); \
996 else if ((c) <= MAX_5_BYTE_CHAR) \
997 *(p)++ = 0xF8, \
998 *(p)++ = (0x80 | ((c >> 18) & 0x0F)), \
999 *(p)++ = (0x80 | ((c >> 12) & 0x3F)), \
1000 *(p)++ = (0x80 | ((c >> 6) & 0x3F)), \
1001 *(p)++ = (0x80 | (c & 0x3F)); \
1002 else \
1003 (p) += BYTE8_STRING ((c) - 0x3FFF80, p); \
1004 } while (0)
1005
1006
1007 /* Return the character code of character whose multibyte form is at
1008 P, and advance P to the end of the multibyte form. This is like
1009 STRING_CHAR_ADVANCE, but it never calls MAYBE_UNIFY_CHAR. */
1010
1011 #define STRING_CHAR_ADVANCE_NO_UNIFY(p) \
1012 (!((p)[0] & 0x80) \
1013 ? *(p)++ \
1014 : ! ((p)[0] & 0x20) \
1015 ? ((p) += 2, \
1016 ((((p)[-2] & 0x1F) << 6) \
1017 | ((p)[-1] & 0x3F) \
1018 | ((unsigned char) ((p)[-2]) < 0xC2 ? 0x3FFF80 : 0))) \
1019 : ! ((p)[0] & 0x10) \
1020 ? ((p) += 3, \
1021 ((((p)[-3] & 0x0F) << 12) \
1022 | (((p)[-2] & 0x3F) << 6) \
1023 | ((p)[-1] & 0x3F))) \
1024 : ! ((p)[0] & 0x08) \
1025 ? ((p) += 4, \
1026 ((((p)[-4] & 0xF) << 18) \
1027 | (((p)[-3] & 0x3F) << 12) \
1028 | (((p)[-2] & 0x3F) << 6) \
1029 | ((p)[-1] & 0x3F))) \
1030 : ((p) += 5, \
1031 ((((p)[-4] & 0x3F) << 18) \
1032 | (((p)[-3] & 0x3F) << 12) \
1033 | (((p)[-2] & 0x3F) << 6) \
1034 | ((p)[-1] & 0x3F))))
1035
1036
1037 static void
1038 coding_set_source (struct coding_system *coding)
1039 {
1040 if (BUFFERP (coding->src_object))
1041 {
1042 struct buffer *buf = XBUFFER (coding->src_object);
1043
1044 if (coding->src_pos < 0)
1045 coding->source = BUF_GAP_END_ADDR (buf) + coding->src_pos_byte;
1046 else
1047 coding->source = BUF_BYTE_ADDRESS (buf, coding->src_pos_byte);
1048 }
1049 else if (STRINGP (coding->src_object))
1050 {
1051 coding->source = SDATA (coding->src_object) + coding->src_pos_byte;
1052 }
1053 else
1054 /* Otherwise, the source is C string and is never relocated
1055 automatically. Thus we don't have to update anything. */
1056 ;
1057 }
1058
1059 static void
1060 coding_set_destination (struct coding_system *coding)
1061 {
1062 if (BUFFERP (coding->dst_object))
1063 {
1064 if (coding->src_pos < 0)
1065 {
1066 coding->destination = BEG_ADDR + coding->dst_pos_byte - BEG_BYTE;
1067 coding->dst_bytes = (GAP_END_ADDR
1068 - (coding->src_bytes - coding->consumed)
1069 - coding->destination);
1070 }
1071 else
1072 {
1073 /* We are sure that coding->dst_pos_byte is before the gap
1074 of the buffer. */
1075 coding->destination = (BUF_BEG_ADDR (XBUFFER (coding->dst_object))
1076 + coding->dst_pos_byte - BEG_BYTE);
1077 coding->dst_bytes = (BUF_GAP_END_ADDR (XBUFFER (coding->dst_object))
1078 - coding->destination);
1079 }
1080 }
1081 else
1082 /* Otherwise, the destination is C string and is never relocated
1083 automatically. Thus we don't have to update anything. */
1084 ;
1085 }
1086
1087
1088 static void
1089 coding_alloc_by_realloc (struct coding_system *coding, EMACS_INT bytes)
1090 {
1091 coding->destination = (unsigned char *) xrealloc (coding->destination,
1092 coding->dst_bytes + bytes);
1093 coding->dst_bytes += bytes;
1094 }
1095
1096 static void
1097 coding_alloc_by_making_gap (struct coding_system *coding,
1098 EMACS_INT gap_head_used, EMACS_INT bytes)
1099 {
1100 if (EQ (coding->src_object, coding->dst_object))
1101 {
1102 /* The gap may contain the produced data at the head and not-yet
1103 consumed data at the tail. To preserve those data, we at
1104 first make the gap size to zero, then increase the gap
1105 size. */
1106 EMACS_INT add = GAP_SIZE;
1107
1108 GPT += gap_head_used, GPT_BYTE += gap_head_used;
1109 GAP_SIZE = 0; ZV += add; Z += add; ZV_BYTE += add; Z_BYTE += add;
1110 make_gap (bytes);
1111 GAP_SIZE += add; ZV -= add; Z -= add; ZV_BYTE -= add; Z_BYTE -= add;
1112 GPT -= gap_head_used, GPT_BYTE -= gap_head_used;
1113 }
1114 else
1115 {
1116 Lisp_Object this_buffer;
1117
1118 this_buffer = Fcurrent_buffer ();
1119 set_buffer_internal (XBUFFER (coding->dst_object));
1120 make_gap (bytes);
1121 set_buffer_internal (XBUFFER (this_buffer));
1122 }
1123 }
1124
1125
1126 static unsigned char *
1127 alloc_destination (struct coding_system *coding, EMACS_INT nbytes,
1128 unsigned char *dst)
1129 {
1130 EMACS_INT offset = dst - coding->destination;
1131
1132 if (BUFFERP (coding->dst_object))
1133 {
1134 struct buffer *buf = XBUFFER (coding->dst_object);
1135
1136 coding_alloc_by_making_gap (coding, dst - BUF_GPT_ADDR (buf), nbytes);
1137 }
1138 else
1139 coding_alloc_by_realloc (coding, nbytes);
1140 coding_set_destination (coding);
1141 dst = coding->destination + offset;
1142 return dst;
1143 }
1144
1145 /** Macros for annotations. */
1146
1147 /* An annotation data is stored in the array coding->charbuf in this
1148 format:
1149 [ -LENGTH ANNOTATION_MASK NCHARS ... ]
1150 LENGTH is the number of elements in the annotation.
1151 ANNOTATION_MASK is one of CODING_ANNOTATE_XXX_MASK.
1152 NCHARS is the number of characters in the text annotated.
1153
1154 The format of the following elements depend on ANNOTATION_MASK.
1155
1156 In the case of CODING_ANNOTATE_COMPOSITION_MASK, these elements
1157 follows:
1158 ... NBYTES METHOD [ COMPOSITION-COMPONENTS ... ]
1159
1160 NBYTES is the number of bytes specified in the header part of
1161 old-style emacs-mule encoding, or 0 for the other kind of
1162 composition.
1163
1164 METHOD is one of enum composition_method.
1165
1166 Optional COMPOSITION-COMPONENTS are characters and composition
1167 rules.
1168
1169 In the case of CODING_ANNOTATE_CHARSET_MASK, one element CHARSET-ID
1170 follows.
1171
1172 If ANNOTATION_MASK is 0, this annotation is just a space holder to
1173 recover from an invalid annotation, and should be skipped by
1174 produce_annotation. */
1175
1176 /* Maximum length of the header of annotation data. */
1177 #define MAX_ANNOTATION_LENGTH 5
1178
1179 #define ADD_ANNOTATION_DATA(buf, len, mask, nchars) \
1180 do { \
1181 *(buf)++ = -(len); \
1182 *(buf)++ = (mask); \
1183 *(buf)++ = (nchars); \
1184 coding->annotated = 1; \
1185 } while (0);
1186
1187 #define ADD_COMPOSITION_DATA(buf, nchars, nbytes, method) \
1188 do { \
1189 ADD_ANNOTATION_DATA (buf, 5, CODING_ANNOTATE_COMPOSITION_MASK, nchars); \
1190 *buf++ = nbytes; \
1191 *buf++ = method; \
1192 } while (0)
1193
1194
1195 #define ADD_CHARSET_DATA(buf, nchars, id) \
1196 do { \
1197 ADD_ANNOTATION_DATA (buf, 4, CODING_ANNOTATE_CHARSET_MASK, nchars); \
1198 *buf++ = id; \
1199 } while (0)
1200
1201 \f
1202 /*** 2. Emacs' internal format (emacs-utf-8) ***/
1203
1204
1205
1206 \f
1207 /*** 3. UTF-8 ***/
1208
1209 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1210 Check if a text is encoded in UTF-8. If it is, return 1, else
1211 return 0. */
1212
1213 #define UTF_8_1_OCTET_P(c) ((c) < 0x80)
1214 #define UTF_8_EXTRA_OCTET_P(c) (((c) & 0xC0) == 0x80)
1215 #define UTF_8_2_OCTET_LEADING_P(c) (((c) & 0xE0) == 0xC0)
1216 #define UTF_8_3_OCTET_LEADING_P(c) (((c) & 0xF0) == 0xE0)
1217 #define UTF_8_4_OCTET_LEADING_P(c) (((c) & 0xF8) == 0xF0)
1218 #define UTF_8_5_OCTET_LEADING_P(c) (((c) & 0xFC) == 0xF8)
1219
1220 #define UTF_BOM 0xFEFF
1221 #define UTF_8_BOM_1 0xEF
1222 #define UTF_8_BOM_2 0xBB
1223 #define UTF_8_BOM_3 0xBF
1224
1225 static int
1226 detect_coding_utf_8 (struct coding_system *coding,
1227 struct coding_detection_info *detect_info)
1228 {
1229 const unsigned char *src = coding->source, *src_base;
1230 const unsigned char *src_end = coding->source + coding->src_bytes;
1231 int multibytep = coding->src_multibyte;
1232 int consumed_chars = 0;
1233 int bom_found = 0;
1234 int found = 0;
1235
1236 detect_info->checked |= CATEGORY_MASK_UTF_8;
1237 /* A coding system of this category is always ASCII compatible. */
1238 src += coding->head_ascii;
1239
1240 while (1)
1241 {
1242 int c, c1, c2, c3, c4;
1243
1244 src_base = src;
1245 ONE_MORE_BYTE (c);
1246 if (c < 0 || UTF_8_1_OCTET_P (c))
1247 continue;
1248 ONE_MORE_BYTE (c1);
1249 if (c1 < 0 || ! UTF_8_EXTRA_OCTET_P (c1))
1250 break;
1251 if (UTF_8_2_OCTET_LEADING_P (c))
1252 {
1253 found = 1;
1254 continue;
1255 }
1256 ONE_MORE_BYTE (c2);
1257 if (c2 < 0 || ! UTF_8_EXTRA_OCTET_P (c2))
1258 break;
1259 if (UTF_8_3_OCTET_LEADING_P (c))
1260 {
1261 found = 1;
1262 if (src_base == coding->source
1263 && c == UTF_8_BOM_1 && c1 == UTF_8_BOM_2 && c2 == UTF_8_BOM_3)
1264 bom_found = 1;
1265 continue;
1266 }
1267 ONE_MORE_BYTE (c3);
1268 if (c3 < 0 || ! UTF_8_EXTRA_OCTET_P (c3))
1269 break;
1270 if (UTF_8_4_OCTET_LEADING_P (c))
1271 {
1272 found = 1;
1273 continue;
1274 }
1275 ONE_MORE_BYTE (c4);
1276 if (c4 < 0 || ! UTF_8_EXTRA_OCTET_P (c4))
1277 break;
1278 if (UTF_8_5_OCTET_LEADING_P (c))
1279 {
1280 found = 1;
1281 continue;
1282 }
1283 break;
1284 }
1285 detect_info->rejected |= CATEGORY_MASK_UTF_8;
1286 return 0;
1287
1288 no_more_source:
1289 if (src_base < src && coding->mode & CODING_MODE_LAST_BLOCK)
1290 {
1291 detect_info->rejected |= CATEGORY_MASK_UTF_8;
1292 return 0;
1293 }
1294 if (bom_found)
1295 {
1296 /* The first character 0xFFFE doesn't necessarily mean a BOM. */
1297 detect_info->found |= CATEGORY_MASK_UTF_8_SIG | CATEGORY_MASK_UTF_8_NOSIG;
1298 }
1299 else
1300 {
1301 detect_info->rejected |= CATEGORY_MASK_UTF_8_SIG;
1302 if (found)
1303 detect_info->found |= CATEGORY_MASK_UTF_8_NOSIG;
1304 }
1305 return 1;
1306 }
1307
1308
1309 static void
1310 decode_coding_utf_8 (struct coding_system *coding)
1311 {
1312 const unsigned char *src = coding->source + coding->consumed;
1313 const unsigned char *src_end = coding->source + coding->src_bytes;
1314 const unsigned char *src_base;
1315 int *charbuf = coding->charbuf + coding->charbuf_used;
1316 int *charbuf_end = coding->charbuf + coding->charbuf_size;
1317 int consumed_chars = 0, consumed_chars_base = 0;
1318 int multibytep = coding->src_multibyte;
1319 enum utf_bom_type bom = CODING_UTF_8_BOM (coding);
1320 Lisp_Object attr, charset_list;
1321 int eol_crlf =
1322 !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos);
1323 int byte_after_cr = -1;
1324
1325 CODING_GET_INFO (coding, attr, charset_list);
1326
1327 if (bom != utf_without_bom)
1328 {
1329 int c1, c2, c3;
1330
1331 src_base = src;
1332 ONE_MORE_BYTE (c1);
1333 if (! UTF_8_3_OCTET_LEADING_P (c1))
1334 src = src_base;
1335 else
1336 {
1337 ONE_MORE_BYTE (c2);
1338 if (! UTF_8_EXTRA_OCTET_P (c2))
1339 src = src_base;
1340 else
1341 {
1342 ONE_MORE_BYTE (c3);
1343 if (! UTF_8_EXTRA_OCTET_P (c3))
1344 src = src_base;
1345 else
1346 {
1347 if ((c1 != UTF_8_BOM_1)
1348 || (c2 != UTF_8_BOM_2) || (c3 != UTF_8_BOM_3))
1349 src = src_base;
1350 else
1351 CODING_UTF_8_BOM (coding) = utf_without_bom;
1352 }
1353 }
1354 }
1355 }
1356 CODING_UTF_8_BOM (coding) = utf_without_bom;
1357
1358 while (1)
1359 {
1360 int c, c1, c2, c3, c4, c5;
1361
1362 src_base = src;
1363 consumed_chars_base = consumed_chars;
1364
1365 if (charbuf >= charbuf_end)
1366 {
1367 if (byte_after_cr >= 0)
1368 src_base--;
1369 break;
1370 }
1371
1372 if (byte_after_cr >= 0)
1373 c1 = byte_after_cr, byte_after_cr = -1;
1374 else
1375 ONE_MORE_BYTE (c1);
1376 if (c1 < 0)
1377 {
1378 c = - c1;
1379 }
1380 else if (UTF_8_1_OCTET_P (c1))
1381 {
1382 if (eol_crlf && c1 == '\r')
1383 ONE_MORE_BYTE (byte_after_cr);
1384 c = c1;
1385 }
1386 else
1387 {
1388 ONE_MORE_BYTE (c2);
1389 if (c2 < 0 || ! UTF_8_EXTRA_OCTET_P (c2))
1390 goto invalid_code;
1391 if (UTF_8_2_OCTET_LEADING_P (c1))
1392 {
1393 c = ((c1 & 0x1F) << 6) | (c2 & 0x3F);
1394 /* Reject overlong sequences here and below. Encoders
1395 producing them are incorrect, they can be misleading,
1396 and they mess up read/write invariance. */
1397 if (c < 128)
1398 goto invalid_code;
1399 }
1400 else
1401 {
1402 ONE_MORE_BYTE (c3);
1403 if (c3 < 0 || ! UTF_8_EXTRA_OCTET_P (c3))
1404 goto invalid_code;
1405 if (UTF_8_3_OCTET_LEADING_P (c1))
1406 {
1407 c = (((c1 & 0xF) << 12)
1408 | ((c2 & 0x3F) << 6) | (c3 & 0x3F));
1409 if (c < 0x800
1410 || (c >= 0xd800 && c < 0xe000)) /* surrogates (invalid) */
1411 goto invalid_code;
1412 }
1413 else
1414 {
1415 ONE_MORE_BYTE (c4);
1416 if (c4 < 0 || ! UTF_8_EXTRA_OCTET_P (c4))
1417 goto invalid_code;
1418 if (UTF_8_4_OCTET_LEADING_P (c1))
1419 {
1420 c = (((c1 & 0x7) << 18) | ((c2 & 0x3F) << 12)
1421 | ((c3 & 0x3F) << 6) | (c4 & 0x3F));
1422 if (c < 0x10000)
1423 goto invalid_code;
1424 }
1425 else
1426 {
1427 ONE_MORE_BYTE (c5);
1428 if (c5 < 0 || ! UTF_8_EXTRA_OCTET_P (c5))
1429 goto invalid_code;
1430 if (UTF_8_5_OCTET_LEADING_P (c1))
1431 {
1432 c = (((c1 & 0x3) << 24) | ((c2 & 0x3F) << 18)
1433 | ((c3 & 0x3F) << 12) | ((c4 & 0x3F) << 6)
1434 | (c5 & 0x3F));
1435 if ((c > MAX_CHAR) || (c < 0x200000))
1436 goto invalid_code;
1437 }
1438 else
1439 goto invalid_code;
1440 }
1441 }
1442 }
1443 }
1444
1445 *charbuf++ = c;
1446 continue;
1447
1448 invalid_code:
1449 src = src_base;
1450 consumed_chars = consumed_chars_base;
1451 ONE_MORE_BYTE (c);
1452 *charbuf++ = ASCII_BYTE_P (c) ? c : BYTE8_TO_CHAR (c);
1453 coding->errors++;
1454 }
1455
1456 no_more_source:
1457 coding->consumed_char += consumed_chars_base;
1458 coding->consumed = src_base - coding->source;
1459 coding->charbuf_used = charbuf - coding->charbuf;
1460 }
1461
1462
1463 static int
1464 encode_coding_utf_8 (struct coding_system *coding)
1465 {
1466 int multibytep = coding->dst_multibyte;
1467 int *charbuf = coding->charbuf;
1468 int *charbuf_end = charbuf + coding->charbuf_used;
1469 unsigned char *dst = coding->destination + coding->produced;
1470 unsigned char *dst_end = coding->destination + coding->dst_bytes;
1471 int produced_chars = 0;
1472 int c;
1473
1474 if (CODING_UTF_8_BOM (coding) == utf_with_bom)
1475 {
1476 ASSURE_DESTINATION (3);
1477 EMIT_THREE_BYTES (UTF_8_BOM_1, UTF_8_BOM_2, UTF_8_BOM_3);
1478 CODING_UTF_8_BOM (coding) = utf_without_bom;
1479 }
1480
1481 if (multibytep)
1482 {
1483 int safe_room = MAX_MULTIBYTE_LENGTH * 2;
1484
1485 while (charbuf < charbuf_end)
1486 {
1487 unsigned char str[MAX_MULTIBYTE_LENGTH], *p, *pend = str;
1488
1489 ASSURE_DESTINATION (safe_room);
1490 c = *charbuf++;
1491 if (CHAR_BYTE8_P (c))
1492 {
1493 c = CHAR_TO_BYTE8 (c);
1494 EMIT_ONE_BYTE (c);
1495 }
1496 else
1497 {
1498 CHAR_STRING_ADVANCE_NO_UNIFY (c, pend);
1499 for (p = str; p < pend; p++)
1500 EMIT_ONE_BYTE (*p);
1501 }
1502 }
1503 }
1504 else
1505 {
1506 int safe_room = MAX_MULTIBYTE_LENGTH;
1507
1508 while (charbuf < charbuf_end)
1509 {
1510 ASSURE_DESTINATION (safe_room);
1511 c = *charbuf++;
1512 if (CHAR_BYTE8_P (c))
1513 *dst++ = CHAR_TO_BYTE8 (c);
1514 else
1515 CHAR_STRING_ADVANCE_NO_UNIFY (c, dst);
1516 produced_chars++;
1517 }
1518 }
1519 record_conversion_result (coding, CODING_RESULT_SUCCESS);
1520 coding->produced_char += produced_chars;
1521 coding->produced = dst - coding->destination;
1522 return 0;
1523 }
1524
1525
1526 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1527 Check if a text is encoded in one of UTF-16 based coding systems.
1528 If it is, return 1, else return 0. */
1529
1530 #define UTF_16_HIGH_SURROGATE_P(val) \
1531 (((val) & 0xFC00) == 0xD800)
1532
1533 #define UTF_16_LOW_SURROGATE_P(val) \
1534 (((val) & 0xFC00) == 0xDC00)
1535
1536 #define UTF_16_INVALID_P(val) \
1537 (((val) == 0xFFFE) \
1538 || ((val) == 0xFFFF) \
1539 || UTF_16_LOW_SURROGATE_P (val))
1540
1541
1542 static int
1543 detect_coding_utf_16 (struct coding_system *coding,
1544 struct coding_detection_info *detect_info)
1545 {
1546 const unsigned char *src = coding->source;
1547 const unsigned char *src_end = coding->source + coding->src_bytes;
1548 int multibytep = coding->src_multibyte;
1549 int c1, c2;
1550
1551 detect_info->checked |= CATEGORY_MASK_UTF_16;
1552 if (coding->mode & CODING_MODE_LAST_BLOCK
1553 && (coding->src_chars & 1))
1554 {
1555 detect_info->rejected |= CATEGORY_MASK_UTF_16;
1556 return 0;
1557 }
1558
1559 TWO_MORE_BYTES (c1, c2);
1560 if ((c1 == 0xFF) && (c2 == 0xFE))
1561 {
1562 detect_info->found |= (CATEGORY_MASK_UTF_16_LE
1563 | CATEGORY_MASK_UTF_16_AUTO);
1564 detect_info->rejected |= (CATEGORY_MASK_UTF_16_BE
1565 | CATEGORY_MASK_UTF_16_BE_NOSIG
1566 | CATEGORY_MASK_UTF_16_LE_NOSIG);
1567 }
1568 else if ((c1 == 0xFE) && (c2 == 0xFF))
1569 {
1570 detect_info->found |= (CATEGORY_MASK_UTF_16_BE
1571 | CATEGORY_MASK_UTF_16_AUTO);
1572 detect_info->rejected |= (CATEGORY_MASK_UTF_16_LE
1573 | CATEGORY_MASK_UTF_16_BE_NOSIG
1574 | CATEGORY_MASK_UTF_16_LE_NOSIG);
1575 }
1576 else if (c2 < 0)
1577 {
1578 detect_info->rejected |= CATEGORY_MASK_UTF_16;
1579 return 0;
1580 }
1581 else
1582 {
1583 /* We check the dispersion of Eth and Oth bytes where E is even and
1584 O is odd. If both are high, we assume binary data.*/
1585 unsigned char e[256], o[256];
1586 unsigned e_num = 1, o_num = 1;
1587
1588 memset (e, 0, 256);
1589 memset (o, 0, 256);
1590 e[c1] = 1;
1591 o[c2] = 1;
1592
1593 detect_info->rejected |= (CATEGORY_MASK_UTF_16_AUTO
1594 |CATEGORY_MASK_UTF_16_BE
1595 | CATEGORY_MASK_UTF_16_LE);
1596
1597 while ((detect_info->rejected & CATEGORY_MASK_UTF_16)
1598 != CATEGORY_MASK_UTF_16)
1599 {
1600 TWO_MORE_BYTES (c1, c2);
1601 if (c2 < 0)
1602 break;
1603 if (! e[c1])
1604 {
1605 e[c1] = 1;
1606 e_num++;
1607 if (e_num >= 128)
1608 detect_info->rejected |= CATEGORY_MASK_UTF_16_BE_NOSIG;
1609 }
1610 if (! o[c2])
1611 {
1612 o[c2] = 1;
1613 o_num++;
1614 if (o_num >= 128)
1615 detect_info->rejected |= CATEGORY_MASK_UTF_16_LE_NOSIG;
1616 }
1617 }
1618 return 0;
1619 }
1620
1621 no_more_source:
1622 return 1;
1623 }
1624
1625 static void
1626 decode_coding_utf_16 (struct coding_system *coding)
1627 {
1628 const unsigned char *src = coding->source + coding->consumed;
1629 const unsigned char *src_end = coding->source + coding->src_bytes;
1630 const unsigned char *src_base;
1631 int *charbuf = coding->charbuf + coding->charbuf_used;
1632 /* We may produces at most 3 chars in one loop. */
1633 int *charbuf_end = coding->charbuf + coding->charbuf_size - 2;
1634 int consumed_chars = 0, consumed_chars_base = 0;
1635 int multibytep = coding->src_multibyte;
1636 enum utf_bom_type bom = CODING_UTF_16_BOM (coding);
1637 enum utf_16_endian_type endian = CODING_UTF_16_ENDIAN (coding);
1638 int surrogate = CODING_UTF_16_SURROGATE (coding);
1639 Lisp_Object attr, charset_list;
1640 int eol_crlf =
1641 !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos);
1642 int byte_after_cr1 = -1, byte_after_cr2 = -1;
1643
1644 CODING_GET_INFO (coding, attr, charset_list);
1645
1646 if (bom == utf_with_bom)
1647 {
1648 int c, c1, c2;
1649
1650 src_base = src;
1651 ONE_MORE_BYTE (c1);
1652 ONE_MORE_BYTE (c2);
1653 c = (c1 << 8) | c2;
1654
1655 if (endian == utf_16_big_endian
1656 ? c != 0xFEFF : c != 0xFFFE)
1657 {
1658 /* The first two bytes are not BOM. Treat them as bytes
1659 for a normal character. */
1660 src = src_base;
1661 coding->errors++;
1662 }
1663 CODING_UTF_16_BOM (coding) = utf_without_bom;
1664 }
1665 else if (bom == utf_detect_bom)
1666 {
1667 /* We have already tried to detect BOM and failed in
1668 detect_coding. */
1669 CODING_UTF_16_BOM (coding) = utf_without_bom;
1670 }
1671
1672 while (1)
1673 {
1674 int c, c1, c2;
1675
1676 src_base = src;
1677 consumed_chars_base = consumed_chars;
1678
1679 if (charbuf >= charbuf_end)
1680 {
1681 if (byte_after_cr1 >= 0)
1682 src_base -= 2;
1683 break;
1684 }
1685
1686 if (byte_after_cr1 >= 0)
1687 c1 = byte_after_cr1, byte_after_cr1 = -1;
1688 else
1689 ONE_MORE_BYTE (c1);
1690 if (c1 < 0)
1691 {
1692 *charbuf++ = -c1;
1693 continue;
1694 }
1695 if (byte_after_cr2 >= 0)
1696 c2 = byte_after_cr2, byte_after_cr2 = -1;
1697 else
1698 ONE_MORE_BYTE (c2);
1699 if (c2 < 0)
1700 {
1701 *charbuf++ = ASCII_BYTE_P (c1) ? c1 : BYTE8_TO_CHAR (c1);
1702 *charbuf++ = -c2;
1703 continue;
1704 }
1705 c = (endian == utf_16_big_endian
1706 ? ((c1 << 8) | c2) : ((c2 << 8) | c1));
1707
1708 if (surrogate)
1709 {
1710 if (! UTF_16_LOW_SURROGATE_P (c))
1711 {
1712 if (endian == utf_16_big_endian)
1713 c1 = surrogate >> 8, c2 = surrogate & 0xFF;
1714 else
1715 c1 = surrogate & 0xFF, c2 = surrogate >> 8;
1716 *charbuf++ = c1;
1717 *charbuf++ = c2;
1718 coding->errors++;
1719 if (UTF_16_HIGH_SURROGATE_P (c))
1720 CODING_UTF_16_SURROGATE (coding) = surrogate = c;
1721 else
1722 *charbuf++ = c;
1723 }
1724 else
1725 {
1726 c = ((surrogate - 0xD800) << 10) | (c - 0xDC00);
1727 CODING_UTF_16_SURROGATE (coding) = surrogate = 0;
1728 *charbuf++ = 0x10000 + c;
1729 }
1730 }
1731 else
1732 {
1733 if (UTF_16_HIGH_SURROGATE_P (c))
1734 CODING_UTF_16_SURROGATE (coding) = surrogate = c;
1735 else
1736 {
1737 if (eol_crlf && c == '\r')
1738 {
1739 ONE_MORE_BYTE (byte_after_cr1);
1740 ONE_MORE_BYTE (byte_after_cr2);
1741 }
1742 *charbuf++ = c;
1743 }
1744 }
1745 }
1746
1747 no_more_source:
1748 coding->consumed_char += consumed_chars_base;
1749 coding->consumed = src_base - coding->source;
1750 coding->charbuf_used = charbuf - coding->charbuf;
1751 }
1752
1753 static int
1754 encode_coding_utf_16 (struct coding_system *coding)
1755 {
1756 int multibytep = coding->dst_multibyte;
1757 int *charbuf = coding->charbuf;
1758 int *charbuf_end = charbuf + coding->charbuf_used;
1759 unsigned char *dst = coding->destination + coding->produced;
1760 unsigned char *dst_end = coding->destination + coding->dst_bytes;
1761 int safe_room = 8;
1762 enum utf_bom_type bom = CODING_UTF_16_BOM (coding);
1763 int big_endian = CODING_UTF_16_ENDIAN (coding) == utf_16_big_endian;
1764 int produced_chars = 0;
1765 Lisp_Object attrs, charset_list;
1766 int c;
1767
1768 CODING_GET_INFO (coding, attrs, charset_list);
1769
1770 if (bom != utf_without_bom)
1771 {
1772 ASSURE_DESTINATION (safe_room);
1773 if (big_endian)
1774 EMIT_TWO_BYTES (0xFE, 0xFF);
1775 else
1776 EMIT_TWO_BYTES (0xFF, 0xFE);
1777 CODING_UTF_16_BOM (coding) = utf_without_bom;
1778 }
1779
1780 while (charbuf < charbuf_end)
1781 {
1782 ASSURE_DESTINATION (safe_room);
1783 c = *charbuf++;
1784 if (c > MAX_UNICODE_CHAR)
1785 c = coding->default_char;
1786
1787 if (c < 0x10000)
1788 {
1789 if (big_endian)
1790 EMIT_TWO_BYTES (c >> 8, c & 0xFF);
1791 else
1792 EMIT_TWO_BYTES (c & 0xFF, c >> 8);
1793 }
1794 else
1795 {
1796 int c1, c2;
1797
1798 c -= 0x10000;
1799 c1 = (c >> 10) + 0xD800;
1800 c2 = (c & 0x3FF) + 0xDC00;
1801 if (big_endian)
1802 EMIT_FOUR_BYTES (c1 >> 8, c1 & 0xFF, c2 >> 8, c2 & 0xFF);
1803 else
1804 EMIT_FOUR_BYTES (c1 & 0xFF, c1 >> 8, c2 & 0xFF, c2 >> 8);
1805 }
1806 }
1807 record_conversion_result (coding, CODING_RESULT_SUCCESS);
1808 coding->produced = dst - coding->destination;
1809 coding->produced_char += produced_chars;
1810 return 0;
1811 }
1812
1813 \f
1814 /*** 6. Old Emacs' internal format (emacs-mule) ***/
1815
1816 /* Emacs' internal format for representation of multiple character
1817 sets is a kind of multi-byte encoding, i.e. characters are
1818 represented by variable-length sequences of one-byte codes.
1819
1820 ASCII characters and control characters (e.g. `tab', `newline') are
1821 represented by one-byte sequences which are their ASCII codes, in
1822 the range 0x00 through 0x7F.
1823
1824 8-bit characters of the range 0x80..0x9F are represented by
1825 two-byte sequences of LEADING_CODE_8_BIT_CONTROL and (their 8-bit
1826 code + 0x20).
1827
1828 8-bit characters of the range 0xA0..0xFF are represented by
1829 one-byte sequences which are their 8-bit code.
1830
1831 The other characters are represented by a sequence of `base
1832 leading-code', optional `extended leading-code', and one or two
1833 `position-code's. The length of the sequence is determined by the
1834 base leading-code. Leading-code takes the range 0x81 through 0x9D,
1835 whereas extended leading-code and position-code take the range 0xA0
1836 through 0xFF. See `charset.h' for more details about leading-code
1837 and position-code.
1838
1839 --- CODE RANGE of Emacs' internal format ---
1840 character set range
1841 ------------- -----
1842 ascii 0x00..0x7F
1843 eight-bit-control LEADING_CODE_8_BIT_CONTROL + 0xA0..0xBF
1844 eight-bit-graphic 0xA0..0xBF
1845 ELSE 0x81..0x9D + [0xA0..0xFF]+
1846 ---------------------------------------------
1847
1848 As this is the internal character representation, the format is
1849 usually not used externally (i.e. in a file or in a data sent to a
1850 process). But, it is possible to have a text externally in this
1851 format (i.e. by encoding by the coding system `emacs-mule').
1852
1853 In that case, a sequence of one-byte codes has a slightly different
1854 form.
1855
1856 At first, all characters in eight-bit-control are represented by
1857 one-byte sequences which are their 8-bit code.
1858
1859 Next, character composition data are represented by the byte
1860 sequence of the form: 0x80 METHOD BYTES CHARS COMPONENT ...,
1861 where,
1862 METHOD is 0xF2 plus one of composition method (enum
1863 composition_method),
1864
1865 BYTES is 0xA0 plus a byte length of this composition data,
1866
1867 CHARS is 0xA0 plus a number of characters composed by this
1868 data,
1869
1870 COMPONENTs are characters of multibyte form or composition
1871 rules encoded by two-byte of ASCII codes.
1872
1873 In addition, for backward compatibility, the following formats are
1874 also recognized as composition data on decoding.
1875
1876 0x80 MSEQ ...
1877 0x80 0xFF MSEQ RULE MSEQ RULE ... MSEQ
1878
1879 Here,
1880 MSEQ is a multibyte form but in these special format:
1881 ASCII: 0xA0 ASCII_CODE+0x80,
1882 other: LEADING_CODE+0x20 FOLLOWING-BYTE ...,
1883 RULE is a one byte code of the range 0xA0..0xF0 that
1884 represents a composition rule.
1885 */
1886
1887 char emacs_mule_bytes[256];
1888
1889
1890 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1891 Check if a text is encoded in `emacs-mule'. If it is, return 1,
1892 else return 0. */
1893
1894 static int
1895 detect_coding_emacs_mule (struct coding_system *coding,
1896 struct coding_detection_info *detect_info)
1897 {
1898 const unsigned char *src = coding->source, *src_base;
1899 const unsigned char *src_end = coding->source + coding->src_bytes;
1900 int multibytep = coding->src_multibyte;
1901 int consumed_chars = 0;
1902 int c;
1903 int found = 0;
1904
1905 detect_info->checked |= CATEGORY_MASK_EMACS_MULE;
1906 /* A coding system of this category is always ASCII compatible. */
1907 src += coding->head_ascii;
1908
1909 while (1)
1910 {
1911 src_base = src;
1912 ONE_MORE_BYTE (c);
1913 if (c < 0)
1914 continue;
1915 if (c == 0x80)
1916 {
1917 /* Perhaps the start of composite character. We simply skip
1918 it because analyzing it is too heavy for detecting. But,
1919 at least, we check that the composite character
1920 constitutes of more than 4 bytes. */
1921 const unsigned char *src_base;
1922
1923 repeat:
1924 src_base = src;
1925 do
1926 {
1927 ONE_MORE_BYTE (c);
1928 }
1929 while (c >= 0xA0);
1930
1931 if (src - src_base <= 4)
1932 break;
1933 found = CATEGORY_MASK_EMACS_MULE;
1934 if (c == 0x80)
1935 goto repeat;
1936 }
1937
1938 if (c < 0x80)
1939 {
1940 if (c < 0x20
1941 && (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO))
1942 break;
1943 }
1944 else
1945 {
1946 int more_bytes = emacs_mule_bytes[c] - 1;
1947
1948 while (more_bytes > 0)
1949 {
1950 ONE_MORE_BYTE (c);
1951 if (c < 0xA0)
1952 {
1953 src--; /* Unread the last byte. */
1954 break;
1955 }
1956 more_bytes--;
1957 }
1958 if (more_bytes != 0)
1959 break;
1960 found = CATEGORY_MASK_EMACS_MULE;
1961 }
1962 }
1963 detect_info->rejected |= CATEGORY_MASK_EMACS_MULE;
1964 return 0;
1965
1966 no_more_source:
1967 if (src_base < src && coding->mode & CODING_MODE_LAST_BLOCK)
1968 {
1969 detect_info->rejected |= CATEGORY_MASK_EMACS_MULE;
1970 return 0;
1971 }
1972 detect_info->found |= found;
1973 return 1;
1974 }
1975
1976
1977 /* Parse emacs-mule multibyte sequence at SRC and return the decoded
1978 character. If CMP_STATUS indicates that we must expect MSEQ or
1979 RULE described above, decode it and return the negative value of
1980 the decoded character or rule. If an invalid byte is found, return
1981 -1. If SRC is too short, return -2. */
1982
1983 int
1984 emacs_mule_char (struct coding_system *coding, const unsigned char *src,
1985 int *nbytes, int *nchars, int *id,
1986 struct composition_status *cmp_status)
1987 {
1988 const unsigned char *src_end = coding->source + coding->src_bytes;
1989 const unsigned char *src_base = src;
1990 int multibytep = coding->src_multibyte;
1991 int charset_id;
1992 unsigned code;
1993 int c;
1994 int consumed_chars = 0;
1995 int mseq_found = 0;
1996
1997 ONE_MORE_BYTE (c);
1998 if (c < 0)
1999 {
2000 c = -c;
2001 charset_id = emacs_mule_charset[0];
2002 }
2003 else
2004 {
2005 if (c >= 0xA0)
2006 {
2007 if (cmp_status->state != COMPOSING_NO
2008 && cmp_status->old_form)
2009 {
2010 if (cmp_status->state == COMPOSING_CHAR)
2011 {
2012 if (c == 0xA0)
2013 {
2014 ONE_MORE_BYTE (c);
2015 c -= 0x80;
2016 if (c < 0)
2017 goto invalid_code;
2018 }
2019 else
2020 c -= 0x20;
2021 mseq_found = 1;
2022 }
2023 else
2024 {
2025 *nbytes = src - src_base;
2026 *nchars = consumed_chars;
2027 return -c;
2028 }
2029 }
2030 else
2031 goto invalid_code;
2032 }
2033
2034 switch (emacs_mule_bytes[c])
2035 {
2036 case 2:
2037 if ((charset_id = emacs_mule_charset[c]) < 0)
2038 goto invalid_code;
2039 ONE_MORE_BYTE (c);
2040 if (c < 0xA0)
2041 goto invalid_code;
2042 code = c & 0x7F;
2043 break;
2044
2045 case 3:
2046 if (c == EMACS_MULE_LEADING_CODE_PRIVATE_11
2047 || c == EMACS_MULE_LEADING_CODE_PRIVATE_12)
2048 {
2049 ONE_MORE_BYTE (c);
2050 if (c < 0xA0 || (charset_id = emacs_mule_charset[c]) < 0)
2051 goto invalid_code;
2052 ONE_MORE_BYTE (c);
2053 if (c < 0xA0)
2054 goto invalid_code;
2055 code = c & 0x7F;
2056 }
2057 else
2058 {
2059 if ((charset_id = emacs_mule_charset[c]) < 0)
2060 goto invalid_code;
2061 ONE_MORE_BYTE (c);
2062 if (c < 0xA0)
2063 goto invalid_code;
2064 code = (c & 0x7F) << 8;
2065 ONE_MORE_BYTE (c);
2066 if (c < 0xA0)
2067 goto invalid_code;
2068 code |= c & 0x7F;
2069 }
2070 break;
2071
2072 case 4:
2073 ONE_MORE_BYTE (c);
2074 if (c < 0 || (charset_id = emacs_mule_charset[c]) < 0)
2075 goto invalid_code;
2076 ONE_MORE_BYTE (c);
2077 if (c < 0xA0)
2078 goto invalid_code;
2079 code = (c & 0x7F) << 8;
2080 ONE_MORE_BYTE (c);
2081 if (c < 0xA0)
2082 goto invalid_code;
2083 code |= c & 0x7F;
2084 break;
2085
2086 case 1:
2087 code = c;
2088 charset_id = ASCII_BYTE_P (code) ? charset_ascii : charset_eight_bit;
2089 break;
2090
2091 default:
2092 abort ();
2093 }
2094 CODING_DECODE_CHAR (coding, src, src_base, src_end,
2095 CHARSET_FROM_ID (charset_id), code, c);
2096 if (c < 0)
2097 goto invalid_code;
2098 }
2099 *nbytes = src - src_base;
2100 *nchars = consumed_chars;
2101 if (id)
2102 *id = charset_id;
2103 return (mseq_found ? -c : c);
2104
2105 no_more_source:
2106 return -2;
2107
2108 invalid_code:
2109 return -1;
2110 }
2111
2112
2113 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
2114
2115 /* Handle these composition sequence ('|': the end of header elements,
2116 BYTES and CHARS >= 0xA0):
2117
2118 (1) relative composition: 0x80 0xF2 BYTES CHARS | CHAR ...
2119 (2) altchar composition: 0x80 0xF4 BYTES CHARS | ALT ... ALT CHAR ...
2120 (3) alt&rule composition: 0x80 0xF5 BYTES CHARS | ALT RULE ... ALT CHAR ...
2121
2122 and these old form:
2123
2124 (4) relative composition: 0x80 | MSEQ ... MSEQ
2125 (5) rulebase composition: 0x80 0xFF | MSEQ MRULE ... MSEQ
2126
2127 When the starter 0x80 and the following header elements are found,
2128 this annotation header is produced.
2129
2130 [ -LENGTH(==-5) CODING_ANNOTATE_COMPOSITION_MASK NCHARS NBYTES METHOD ]
2131
2132 NCHARS is CHARS - 0xA0 for (1), (2), (3), and 0 for (4), (5).
2133 NBYTES is BYTES - 0xA0 for (1), (2), (3), and 0 for (4), (5).
2134
2135 Then, upon reading the following elements, these codes are produced
2136 until the composition end is found:
2137
2138 (1) CHAR ... CHAR
2139 (2) ALT ... ALT CHAR ... CHAR
2140 (3) ALT -2 DECODED-RULE ALT -2 DECODED-RULE ... ALT CHAR ... CHAR
2141 (4) CHAR ... CHAR
2142 (5) CHAR -2 DECODED-RULE CHAR -2 DECODED-RULE ... CHAR
2143
2144 When the composition end is found, LENGTH and NCHARS in the
2145 annotation header is updated as below:
2146
2147 (1) LENGTH: unchanged, NCHARS: unchanged
2148 (2) LENGTH: length of the whole sequence minus NCHARS, NCHARS: unchanged
2149 (3) LENGTH: length of the whole sequence minus NCHARS, NCHARS: unchanged
2150 (4) LENGTH: unchanged, NCHARS: number of CHARs
2151 (5) LENGTH: unchanged, NCHARS: number of CHARs
2152
2153 If an error is found while composing, the annotation header is
2154 changed to the original composition header (plus filler -1s) as
2155 below:
2156
2157 (1),(2),(3) [ 0x80 0xF2+METHOD BYTES CHARS -1 ]
2158 (5) [ 0x80 0xFF -1 -1- -1 ]
2159
2160 and the sequence [ -2 DECODED-RULE ] is changed to the original
2161 byte sequence as below:
2162 o the original byte sequence is B: [ B -1 ]
2163 o the original byte sequence is B1 B2: [ B1 B2 ]
2164
2165 Most of the routines are implemented by macros because many
2166 variables and labels in the caller decode_coding_emacs_mule must be
2167 accessible, and they are usually called just once (thus doesn't
2168 increase the size of compiled object). */
2169
2170 /* Decode a composition rule represented by C as a component of
2171 composition sequence of Emacs 20 style. Set RULE to the decoded
2172 rule. */
2173
2174 #define DECODE_EMACS_MULE_COMPOSITION_RULE_20(c, rule) \
2175 do { \
2176 int gref, nref; \
2177 \
2178 c -= 0xA0; \
2179 if (c < 0 || c >= 81) \
2180 goto invalid_code; \
2181 gref = c / 9, nref = c % 9; \
2182 if (gref == 4) gref = 10; \
2183 if (nref == 4) nref = 10; \
2184 rule = COMPOSITION_ENCODE_RULE (gref, nref); \
2185 } while (0)
2186
2187
2188 /* Decode a composition rule represented by C and the following byte
2189 at SRC as a component of composition sequence of Emacs 21 style.
2190 Set RULE to the decoded rule. */
2191
2192 #define DECODE_EMACS_MULE_COMPOSITION_RULE_21(c, rule) \
2193 do { \
2194 int gref, nref; \
2195 \
2196 gref = c - 0x20; \
2197 if (gref < 0 || gref >= 81) \
2198 goto invalid_code; \
2199 ONE_MORE_BYTE (c); \
2200 nref = c - 0x20; \
2201 if (nref < 0 || nref >= 81) \
2202 goto invalid_code; \
2203 rule = COMPOSITION_ENCODE_RULE (gref, nref); \
2204 } while (0)
2205
2206
2207 /* Start of Emacs 21 style format. The first three bytes at SRC are
2208 (METHOD - 0xF2), (BYTES - 0xA0), (CHARS - 0xA0), where BYTES is the
2209 byte length of this composition information, CHARS is the number of
2210 characters composed by this composition. */
2211
2212 #define DECODE_EMACS_MULE_21_COMPOSITION() \
2213 do { \
2214 enum composition_method method = c - 0xF2; \
2215 int nbytes, nchars; \
2216 \
2217 ONE_MORE_BYTE (c); \
2218 if (c < 0) \
2219 goto invalid_code; \
2220 nbytes = c - 0xA0; \
2221 if (nbytes < 3 || (method == COMPOSITION_RELATIVE && nbytes != 4)) \
2222 goto invalid_code; \
2223 ONE_MORE_BYTE (c); \
2224 nchars = c - 0xA0; \
2225 if (nchars <= 0 || nchars >= MAX_COMPOSITION_COMPONENTS) \
2226 goto invalid_code; \
2227 cmp_status->old_form = 0; \
2228 cmp_status->method = method; \
2229 if (method == COMPOSITION_RELATIVE) \
2230 cmp_status->state = COMPOSING_CHAR; \
2231 else \
2232 cmp_status->state = COMPOSING_COMPONENT_CHAR; \
2233 cmp_status->length = MAX_ANNOTATION_LENGTH; \
2234 cmp_status->nchars = nchars; \
2235 cmp_status->ncomps = nbytes - 4; \
2236 ADD_COMPOSITION_DATA (charbuf, nchars, nbytes, method); \
2237 } while (0)
2238
2239
2240 /* Start of Emacs 20 style format for relative composition. */
2241
2242 #define DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION() \
2243 do { \
2244 cmp_status->old_form = 1; \
2245 cmp_status->method = COMPOSITION_RELATIVE; \
2246 cmp_status->state = COMPOSING_CHAR; \
2247 cmp_status->length = MAX_ANNOTATION_LENGTH; \
2248 cmp_status->nchars = cmp_status->ncomps = 0; \
2249 ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \
2250 } while (0)
2251
2252
2253 /* Start of Emacs 20 style format for rule-base composition. */
2254
2255 #define DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION() \
2256 do { \
2257 cmp_status->old_form = 1; \
2258 cmp_status->method = COMPOSITION_WITH_RULE; \
2259 cmp_status->state = COMPOSING_CHAR; \
2260 cmp_status->length = MAX_ANNOTATION_LENGTH; \
2261 cmp_status->nchars = cmp_status->ncomps = 0; \
2262 ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \
2263 } while (0)
2264
2265
2266 #define DECODE_EMACS_MULE_COMPOSITION_START() \
2267 do { \
2268 const unsigned char *current_src = src; \
2269 \
2270 ONE_MORE_BYTE (c); \
2271 if (c < 0) \
2272 goto invalid_code; \
2273 if (c - 0xF2 >= COMPOSITION_RELATIVE \
2274 && c - 0xF2 <= COMPOSITION_WITH_RULE_ALTCHARS) \
2275 DECODE_EMACS_MULE_21_COMPOSITION (); \
2276 else if (c < 0xA0) \
2277 goto invalid_code; \
2278 else if (c < 0xC0) \
2279 { \
2280 DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION (); \
2281 /* Re-read C as a composition component. */ \
2282 src = current_src; \
2283 } \
2284 else if (c == 0xFF) \
2285 DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION (); \
2286 else \
2287 goto invalid_code; \
2288 } while (0)
2289
2290 #define EMACS_MULE_COMPOSITION_END() \
2291 do { \
2292 int idx = - cmp_status->length; \
2293 \
2294 if (cmp_status->old_form) \
2295 charbuf[idx + 2] = cmp_status->nchars; \
2296 else if (cmp_status->method > COMPOSITION_RELATIVE) \
2297 charbuf[idx] = charbuf[idx + 2] - cmp_status->length; \
2298 cmp_status->state = COMPOSING_NO; \
2299 } while (0)
2300
2301
2302 static int
2303 emacs_mule_finish_composition (int *charbuf,
2304 struct composition_status *cmp_status)
2305 {
2306 int idx = - cmp_status->length;
2307 int new_chars;
2308
2309 if (cmp_status->old_form && cmp_status->nchars > 0)
2310 {
2311 charbuf[idx + 2] = cmp_status->nchars;
2312 new_chars = 0;
2313 if (cmp_status->method == COMPOSITION_WITH_RULE
2314 && cmp_status->state == COMPOSING_CHAR)
2315 {
2316 /* The last rule was invalid. */
2317 int rule = charbuf[-1] + 0xA0;
2318
2319 charbuf[-2] = BYTE8_TO_CHAR (rule);
2320 charbuf[-1] = -1;
2321 new_chars = 1;
2322 }
2323 }
2324 else
2325 {
2326 charbuf[idx++] = BYTE8_TO_CHAR (0x80);
2327
2328 if (cmp_status->method == COMPOSITION_WITH_RULE)
2329 {
2330 charbuf[idx++] = BYTE8_TO_CHAR (0xFF);
2331 charbuf[idx++] = -3;
2332 charbuf[idx++] = 0;
2333 new_chars = 1;
2334 }
2335 else
2336 {
2337 int nchars = charbuf[idx + 1] + 0xA0;
2338 int nbytes = charbuf[idx + 2] + 0xA0;
2339
2340 charbuf[idx++] = BYTE8_TO_CHAR (0xF2 + cmp_status->method);
2341 charbuf[idx++] = BYTE8_TO_CHAR (nbytes);
2342 charbuf[idx++] = BYTE8_TO_CHAR (nchars);
2343 charbuf[idx++] = -1;
2344 new_chars = 4;
2345 }
2346 }
2347 cmp_status->state = COMPOSING_NO;
2348 return new_chars;
2349 }
2350
2351 #define EMACS_MULE_MAYBE_FINISH_COMPOSITION() \
2352 do { \
2353 if (cmp_status->state != COMPOSING_NO) \
2354 char_offset += emacs_mule_finish_composition (charbuf, cmp_status); \
2355 } while (0)
2356
2357
2358 static void
2359 decode_coding_emacs_mule (struct coding_system *coding)
2360 {
2361 const unsigned char *src = coding->source + coding->consumed;
2362 const unsigned char *src_end = coding->source + coding->src_bytes;
2363 const unsigned char *src_base;
2364 int *charbuf = coding->charbuf + coding->charbuf_used;
2365 /* We may produce two annotations (charset and composition) in one
2366 loop and one more charset annotation at the end. */
2367 int *charbuf_end
2368 = coding->charbuf + coding->charbuf_size - (MAX_ANNOTATION_LENGTH * 3);
2369 int consumed_chars = 0, consumed_chars_base;
2370 int multibytep = coding->src_multibyte;
2371 Lisp_Object attrs, charset_list;
2372 int char_offset = coding->produced_char;
2373 int last_offset = char_offset;
2374 int last_id = charset_ascii;
2375 int eol_crlf =
2376 !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos);
2377 int byte_after_cr = -1;
2378 struct composition_status *cmp_status = &coding->spec.emacs_mule.cmp_status;
2379
2380 CODING_GET_INFO (coding, attrs, charset_list);
2381
2382 if (cmp_status->state != COMPOSING_NO)
2383 {
2384 int i;
2385
2386 for (i = 0; i < cmp_status->length; i++)
2387 *charbuf++ = cmp_status->carryover[i];
2388 coding->annotated = 1;
2389 }
2390
2391 while (1)
2392 {
2393 int c, id;
2394
2395 src_base = src;
2396 consumed_chars_base = consumed_chars;
2397
2398 if (charbuf >= charbuf_end)
2399 {
2400 if (byte_after_cr >= 0)
2401 src_base--;
2402 break;
2403 }
2404
2405 if (byte_after_cr >= 0)
2406 c = byte_after_cr, byte_after_cr = -1;
2407 else
2408 ONE_MORE_BYTE (c);
2409
2410 if (c < 0 || c == 0x80)
2411 {
2412 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2413 if (c < 0)
2414 {
2415 *charbuf++ = -c;
2416 char_offset++;
2417 }
2418 else
2419 DECODE_EMACS_MULE_COMPOSITION_START ();
2420 continue;
2421 }
2422
2423 if (c < 0x80)
2424 {
2425 if (eol_crlf && c == '\r')
2426 ONE_MORE_BYTE (byte_after_cr);
2427 id = charset_ascii;
2428 if (cmp_status->state != COMPOSING_NO)
2429 {
2430 if (cmp_status->old_form)
2431 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2432 else if (cmp_status->state >= COMPOSING_COMPONENT_CHAR)
2433 cmp_status->ncomps--;
2434 }
2435 }
2436 else
2437 {
2438 int nchars, nbytes;
2439 /* emacs_mule_char can load a charset map from a file, which
2440 allocates a large structure and might cause buffer text
2441 to be relocated as result. Thus, we need to remember the
2442 original pointer to buffer text, and fix up all related
2443 pointers after the call. */
2444 const unsigned char *orig = coding->source;
2445 EMACS_INT offset;
2446
2447 c = emacs_mule_char (coding, src_base, &nbytes, &nchars, &id,
2448 cmp_status);
2449 offset = coding->source - orig;
2450 if (offset)
2451 {
2452 src += offset;
2453 src_base += offset;
2454 src_end += offset;
2455 }
2456 if (c < 0)
2457 {
2458 if (c == -1)
2459 goto invalid_code;
2460 if (c == -2)
2461 break;
2462 }
2463 src = src_base + nbytes;
2464 consumed_chars = consumed_chars_base + nchars;
2465 if (cmp_status->state >= COMPOSING_COMPONENT_CHAR)
2466 cmp_status->ncomps -= nchars;
2467 }
2468
2469 /* Now if C >= 0, we found a normally encoded character, if C <
2470 0, we found an old-style composition component character or
2471 rule. */
2472
2473 if (cmp_status->state == COMPOSING_NO)
2474 {
2475 if (last_id != id)
2476 {
2477 if (last_id != charset_ascii)
2478 ADD_CHARSET_DATA (charbuf, char_offset - last_offset,
2479 last_id);
2480 last_id = id;
2481 last_offset = char_offset;
2482 }
2483 *charbuf++ = c;
2484 char_offset++;
2485 }
2486 else if (cmp_status->state == COMPOSING_CHAR)
2487 {
2488 if (cmp_status->old_form)
2489 {
2490 if (c >= 0)
2491 {
2492 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2493 *charbuf++ = c;
2494 char_offset++;
2495 }
2496 else
2497 {
2498 *charbuf++ = -c;
2499 cmp_status->nchars++;
2500 cmp_status->length++;
2501 if (cmp_status->nchars == MAX_COMPOSITION_COMPONENTS)
2502 EMACS_MULE_COMPOSITION_END ();
2503 else if (cmp_status->method == COMPOSITION_WITH_RULE)
2504 cmp_status->state = COMPOSING_RULE;
2505 }
2506 }
2507 else
2508 {
2509 *charbuf++ = c;
2510 cmp_status->length++;
2511 cmp_status->nchars--;
2512 if (cmp_status->nchars == 0)
2513 EMACS_MULE_COMPOSITION_END ();
2514 }
2515 }
2516 else if (cmp_status->state == COMPOSING_RULE)
2517 {
2518 int rule;
2519
2520 if (c >= 0)
2521 {
2522 EMACS_MULE_COMPOSITION_END ();
2523 *charbuf++ = c;
2524 char_offset++;
2525 }
2526 else
2527 {
2528 c = -c;
2529 DECODE_EMACS_MULE_COMPOSITION_RULE_20 (c, rule);
2530 if (rule < 0)
2531 goto invalid_code;
2532 *charbuf++ = -2;
2533 *charbuf++ = rule;
2534 cmp_status->length += 2;
2535 cmp_status->state = COMPOSING_CHAR;
2536 }
2537 }
2538 else if (cmp_status->state == COMPOSING_COMPONENT_CHAR)
2539 {
2540 *charbuf++ = c;
2541 cmp_status->length++;
2542 if (cmp_status->ncomps == 0)
2543 cmp_status->state = COMPOSING_CHAR;
2544 else if (cmp_status->ncomps > 0)
2545 {
2546 if (cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS)
2547 cmp_status->state = COMPOSING_COMPONENT_RULE;
2548 }
2549 else
2550 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2551 }
2552 else /* COMPOSING_COMPONENT_RULE */
2553 {
2554 int rule;
2555
2556 DECODE_EMACS_MULE_COMPOSITION_RULE_21 (c, rule);
2557 if (rule < 0)
2558 goto invalid_code;
2559 *charbuf++ = -2;
2560 *charbuf++ = rule;
2561 cmp_status->length += 2;
2562 cmp_status->ncomps--;
2563 if (cmp_status->ncomps > 0)
2564 cmp_status->state = COMPOSING_COMPONENT_CHAR;
2565 else
2566 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2567 }
2568 continue;
2569
2570 invalid_code:
2571 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2572 src = src_base;
2573 consumed_chars = consumed_chars_base;
2574 ONE_MORE_BYTE (c);
2575 *charbuf++ = ASCII_BYTE_P (c) ? c : BYTE8_TO_CHAR (c);
2576 char_offset++;
2577 coding->errors++;
2578 }
2579
2580 no_more_source:
2581 if (cmp_status->state != COMPOSING_NO)
2582 {
2583 if (coding->mode & CODING_MODE_LAST_BLOCK)
2584 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2585 else
2586 {
2587 int i;
2588
2589 charbuf -= cmp_status->length;
2590 for (i = 0; i < cmp_status->length; i++)
2591 cmp_status->carryover[i] = charbuf[i];
2592 }
2593 }
2594 if (last_id != charset_ascii)
2595 ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id);
2596 coding->consumed_char += consumed_chars_base;
2597 coding->consumed = src_base - coding->source;
2598 coding->charbuf_used = charbuf - coding->charbuf;
2599 }
2600
2601
2602 #define EMACS_MULE_LEADING_CODES(id, codes) \
2603 do { \
2604 if (id < 0xA0) \
2605 codes[0] = id, codes[1] = 0; \
2606 else if (id < 0xE0) \
2607 codes[0] = 0x9A, codes[1] = id; \
2608 else if (id < 0xF0) \
2609 codes[0] = 0x9B, codes[1] = id; \
2610 else if (id < 0xF5) \
2611 codes[0] = 0x9C, codes[1] = id; \
2612 else \
2613 codes[0] = 0x9D, codes[1] = id; \
2614 } while (0);
2615
2616
2617 static int
2618 encode_coding_emacs_mule (struct coding_system *coding)
2619 {
2620 int multibytep = coding->dst_multibyte;
2621 int *charbuf = coding->charbuf;
2622 int *charbuf_end = charbuf + coding->charbuf_used;
2623 unsigned char *dst = coding->destination + coding->produced;
2624 unsigned char *dst_end = coding->destination + coding->dst_bytes;
2625 int safe_room = 8;
2626 int produced_chars = 0;
2627 Lisp_Object attrs, charset_list;
2628 int c;
2629 int preferred_charset_id = -1;
2630
2631 CODING_GET_INFO (coding, attrs, charset_list);
2632 if (! EQ (charset_list, Vemacs_mule_charset_list))
2633 {
2634 CODING_ATTR_CHARSET_LIST (attrs)
2635 = charset_list = Vemacs_mule_charset_list;
2636 }
2637
2638 while (charbuf < charbuf_end)
2639 {
2640 ASSURE_DESTINATION (safe_room);
2641 c = *charbuf++;
2642
2643 if (c < 0)
2644 {
2645 /* Handle an annotation. */
2646 switch (*charbuf)
2647 {
2648 case CODING_ANNOTATE_COMPOSITION_MASK:
2649 /* Not yet implemented. */
2650 break;
2651 case CODING_ANNOTATE_CHARSET_MASK:
2652 preferred_charset_id = charbuf[3];
2653 if (preferred_charset_id >= 0
2654 && NILP (Fmemq (make_number (preferred_charset_id),
2655 charset_list)))
2656 preferred_charset_id = -1;
2657 break;
2658 default:
2659 abort ();
2660 }
2661 charbuf += -c - 1;
2662 continue;
2663 }
2664
2665 if (ASCII_CHAR_P (c))
2666 EMIT_ONE_ASCII_BYTE (c);
2667 else if (CHAR_BYTE8_P (c))
2668 {
2669 c = CHAR_TO_BYTE8 (c);
2670 EMIT_ONE_BYTE (c);
2671 }
2672 else
2673 {
2674 struct charset *charset;
2675 unsigned code;
2676 int dimension;
2677 int emacs_mule_id;
2678 unsigned char leading_codes[2];
2679
2680 if (preferred_charset_id >= 0)
2681 {
2682 charset = CHARSET_FROM_ID (preferred_charset_id);
2683 if (CHAR_CHARSET_P (c, charset))
2684 code = ENCODE_CHAR (charset, c);
2685 else
2686 charset = char_charset (c, charset_list, &code);
2687 }
2688 else
2689 charset = char_charset (c, charset_list, &code);
2690 if (! charset)
2691 {
2692 c = coding->default_char;
2693 if (ASCII_CHAR_P (c))
2694 {
2695 EMIT_ONE_ASCII_BYTE (c);
2696 continue;
2697 }
2698 charset = char_charset (c, charset_list, &code);
2699 }
2700 dimension = CHARSET_DIMENSION (charset);
2701 emacs_mule_id = CHARSET_EMACS_MULE_ID (charset);
2702 EMACS_MULE_LEADING_CODES (emacs_mule_id, leading_codes);
2703 EMIT_ONE_BYTE (leading_codes[0]);
2704 if (leading_codes[1])
2705 EMIT_ONE_BYTE (leading_codes[1]);
2706 if (dimension == 1)
2707 EMIT_ONE_BYTE (code | 0x80);
2708 else
2709 {
2710 code |= 0x8080;
2711 EMIT_ONE_BYTE (code >> 8);
2712 EMIT_ONE_BYTE (code & 0xFF);
2713 }
2714 }
2715 }
2716 record_conversion_result (coding, CODING_RESULT_SUCCESS);
2717 coding->produced_char += produced_chars;
2718 coding->produced = dst - coding->destination;
2719 return 0;
2720 }
2721
2722 \f
2723 /*** 7. ISO2022 handlers ***/
2724
2725 /* The following note describes the coding system ISO2022 briefly.
2726 Since the intention of this note is to help understand the
2727 functions in this file, some parts are NOT ACCURATE or are OVERLY
2728 SIMPLIFIED. For thorough understanding, please refer to the
2729 original document of ISO2022. This is equivalent to the standard
2730 ECMA-35, obtainable from <URL:http://www.ecma.ch/> (*).
2731
2732 ISO2022 provides many mechanisms to encode several character sets
2733 in 7-bit and 8-bit environments. For 7-bit environments, all text
2734 is encoded using bytes less than 128. This may make the encoded
2735 text a little bit longer, but the text passes more easily through
2736 several types of gateway, some of which strip off the MSB (Most
2737 Significant Bit).
2738
2739 There are two kinds of character sets: control character sets and
2740 graphic character sets. The former contain control characters such
2741 as `newline' and `escape' to provide control functions (control
2742 functions are also provided by escape sequences). The latter
2743 contain graphic characters such as 'A' and '-'. Emacs recognizes
2744 two control character sets and many graphic character sets.
2745
2746 Graphic character sets are classified into one of the following
2747 four classes, according to the number of bytes (DIMENSION) and
2748 number of characters in one dimension (CHARS) of the set:
2749 - DIMENSION1_CHARS94
2750 - DIMENSION1_CHARS96
2751 - DIMENSION2_CHARS94
2752 - DIMENSION2_CHARS96
2753
2754 In addition, each character set is assigned an identification tag,
2755 unique for each set, called the "final character" (denoted as <F>
2756 hereafter). The <F> of each character set is decided by ECMA(*)
2757 when it is registered in ISO. The code range of <F> is 0x30..0x7F
2758 (0x30..0x3F are for private use only).
2759
2760 Note (*): ECMA = European Computer Manufacturers Association
2761
2762 Here are examples of graphic character sets [NAME(<F>)]:
2763 o DIMENSION1_CHARS94 -- ASCII('B'), right-half-of-JISX0201('I'), ...
2764 o DIMENSION1_CHARS96 -- right-half-of-ISO8859-1('A'), ...
2765 o DIMENSION2_CHARS94 -- GB2312('A'), JISX0208('B'), ...
2766 o DIMENSION2_CHARS96 -- none for the moment
2767
2768 A code area (1 byte=8 bits) is divided into 4 areas, C0, GL, C1, and GR.
2769 C0 [0x00..0x1F] -- control character plane 0
2770 GL [0x20..0x7F] -- graphic character plane 0
2771 C1 [0x80..0x9F] -- control character plane 1
2772 GR [0xA0..0xFF] -- graphic character plane 1
2773
2774 A control character set is directly designated and invoked to C0 or
2775 C1 by an escape sequence. The most common case is that:
2776 - ISO646's control character set is designated/invoked to C0, and
2777 - ISO6429's control character set is designated/invoked to C1,
2778 and usually these designations/invocations are omitted in encoded
2779 text. In a 7-bit environment, only C0 can be used, and a control
2780 character for C1 is encoded by an appropriate escape sequence to
2781 fit into the environment. All control characters for C1 are
2782 defined to have corresponding escape sequences.
2783
2784 A graphic character set is at first designated to one of four
2785 graphic registers (G0 through G3), then these graphic registers are
2786 invoked to GL or GR. These designations and invocations can be
2787 done independently. The most common case is that G0 is invoked to
2788 GL, G1 is invoked to GR, and ASCII is designated to G0. Usually
2789 these invocations and designations are omitted in encoded text.
2790 In a 7-bit environment, only GL can be used.
2791
2792 When a graphic character set of CHARS94 is invoked to GL, codes
2793 0x20 and 0x7F of the GL area work as control characters SPACE and
2794 DEL respectively, and codes 0xA0 and 0xFF of the GR area should not
2795 be used.
2796
2797 There are two ways of invocation: locking-shift and single-shift.
2798 With locking-shift, the invocation lasts until the next different
2799 invocation, whereas with single-shift, the invocation affects the
2800 following character only and doesn't affect the locking-shift
2801 state. Invocations are done by the following control characters or
2802 escape sequences:
2803
2804 ----------------------------------------------------------------------
2805 abbrev function cntrl escape seq description
2806 ----------------------------------------------------------------------
2807 SI/LS0 (shift-in) 0x0F none invoke G0 into GL
2808 SO/LS1 (shift-out) 0x0E none invoke G1 into GL
2809 LS2 (locking-shift-2) none ESC 'n' invoke G2 into GL
2810 LS3 (locking-shift-3) none ESC 'o' invoke G3 into GL
2811 LS1R (locking-shift-1 right) none ESC '~' invoke G1 into GR (*)
2812 LS2R (locking-shift-2 right) none ESC '}' invoke G2 into GR (*)
2813 LS3R (locking-shift 3 right) none ESC '|' invoke G3 into GR (*)
2814 SS2 (single-shift-2) 0x8E ESC 'N' invoke G2 for one char
2815 SS3 (single-shift-3) 0x8F ESC 'O' invoke G3 for one char
2816 ----------------------------------------------------------------------
2817 (*) These are not used by any known coding system.
2818
2819 Control characters for these functions are defined by macros
2820 ISO_CODE_XXX in `coding.h'.
2821
2822 Designations are done by the following escape sequences:
2823 ----------------------------------------------------------------------
2824 escape sequence description
2825 ----------------------------------------------------------------------
2826 ESC '(' <F> designate DIMENSION1_CHARS94<F> to G0
2827 ESC ')' <F> designate DIMENSION1_CHARS94<F> to G1
2828 ESC '*' <F> designate DIMENSION1_CHARS94<F> to G2
2829 ESC '+' <F> designate DIMENSION1_CHARS94<F> to G3
2830 ESC ',' <F> designate DIMENSION1_CHARS96<F> to G0 (*)
2831 ESC '-' <F> designate DIMENSION1_CHARS96<F> to G1
2832 ESC '.' <F> designate DIMENSION1_CHARS96<F> to G2
2833 ESC '/' <F> designate DIMENSION1_CHARS96<F> to G3
2834 ESC '$' '(' <F> designate DIMENSION2_CHARS94<F> to G0 (**)
2835 ESC '$' ')' <F> designate DIMENSION2_CHARS94<F> to G1
2836 ESC '$' '*' <F> designate DIMENSION2_CHARS94<F> to G2
2837 ESC '$' '+' <F> designate DIMENSION2_CHARS94<F> to G3
2838 ESC '$' ',' <F> designate DIMENSION2_CHARS96<F> to G0 (*)
2839 ESC '$' '-' <F> designate DIMENSION2_CHARS96<F> to G1
2840 ESC '$' '.' <F> designate DIMENSION2_CHARS96<F> to G2
2841 ESC '$' '/' <F> designate DIMENSION2_CHARS96<F> to G3
2842 ----------------------------------------------------------------------
2843
2844 In this list, "DIMENSION1_CHARS94<F>" means a graphic character set
2845 of dimension 1, chars 94, and final character <F>, etc...
2846
2847 Note (*): Although these designations are not allowed in ISO2022,
2848 Emacs accepts them on decoding, and produces them on encoding
2849 CHARS96 character sets in a coding system which is characterized as
2850 7-bit environment, non-locking-shift, and non-single-shift.
2851
2852 Note (**): If <F> is '@', 'A', or 'B', the intermediate character
2853 '(' must be omitted. We refer to this as "short-form" hereafter.
2854
2855 Now you may notice that there are a lot of ways of encoding the
2856 same multilingual text in ISO2022. Actually, there exist many
2857 coding systems such as Compound Text (used in X11's inter client
2858 communication, ISO-2022-JP (used in Japanese Internet), ISO-2022-KR
2859 (used in Korean Internet), EUC (Extended UNIX Code, used in Asian
2860 localized platforms), and all of these are variants of ISO2022.
2861
2862 In addition to the above, Emacs handles two more kinds of escape
2863 sequences: ISO6429's direction specification and Emacs' private
2864 sequence for specifying character composition.
2865
2866 ISO6429's direction specification takes the following form:
2867 o CSI ']' -- end of the current direction
2868 o CSI '0' ']' -- end of the current direction
2869 o CSI '1' ']' -- start of left-to-right text
2870 o CSI '2' ']' -- start of right-to-left text
2871 The control character CSI (0x9B: control sequence introducer) is
2872 abbreviated to the escape sequence ESC '[' in a 7-bit environment.
2873
2874 Character composition specification takes the following form:
2875 o ESC '0' -- start relative composition
2876 o ESC '1' -- end composition
2877 o ESC '2' -- start rule-base composition (*)
2878 o ESC '3' -- start relative composition with alternate chars (**)
2879 o ESC '4' -- start rule-base composition with alternate chars (**)
2880 Since these are not standard escape sequences of any ISO standard,
2881 the use of them with these meanings is restricted to Emacs only.
2882
2883 (*) This form is used only in Emacs 20.7 and older versions,
2884 but newer versions can safely decode it.
2885 (**) This form is used only in Emacs 21.1 and newer versions,
2886 and older versions can't decode it.
2887
2888 Here's a list of example usages of these composition escape
2889 sequences (categorized by `enum composition_method').
2890
2891 COMPOSITION_RELATIVE:
2892 ESC 0 CHAR [ CHAR ] ESC 1
2893 COMPOSITION_WITH_RULE:
2894 ESC 2 CHAR [ RULE CHAR ] ESC 1
2895 COMPOSITION_WITH_ALTCHARS:
2896 ESC 3 ALTCHAR [ ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1
2897 COMPOSITION_WITH_RULE_ALTCHARS:
2898 ESC 4 ALTCHAR [ RULE ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1 */
2899
2900 enum iso_code_class_type iso_code_class[256];
2901
2902 #define SAFE_CHARSET_P(coding, id) \
2903 ((id) <= (coding)->max_charset_id \
2904 && (coding)->safe_charsets[id] != 255)
2905
2906
2907 #define SHIFT_OUT_OK(category) \
2908 (CODING_ISO_INITIAL (&coding_categories[category], 1) >= 0)
2909
2910 static void
2911 setup_iso_safe_charsets (Lisp_Object attrs)
2912 {
2913 Lisp_Object charset_list, safe_charsets;
2914 Lisp_Object request;
2915 Lisp_Object reg_usage;
2916 Lisp_Object tail;
2917 int reg94, reg96;
2918 int flags = XINT (AREF (attrs, coding_attr_iso_flags));
2919 int max_charset_id;
2920
2921 charset_list = CODING_ATTR_CHARSET_LIST (attrs);
2922 if ((flags & CODING_ISO_FLAG_FULL_SUPPORT)
2923 && ! EQ (charset_list, Viso_2022_charset_list))
2924 {
2925 CODING_ATTR_CHARSET_LIST (attrs)
2926 = charset_list = Viso_2022_charset_list;
2927 ASET (attrs, coding_attr_safe_charsets, Qnil);
2928 }
2929
2930 if (STRINGP (AREF (attrs, coding_attr_safe_charsets)))
2931 return;
2932
2933 max_charset_id = 0;
2934 for (tail = charset_list; CONSP (tail); tail = XCDR (tail))
2935 {
2936 int id = XINT (XCAR (tail));
2937 if (max_charset_id < id)
2938 max_charset_id = id;
2939 }
2940
2941 safe_charsets = make_uninit_string (max_charset_id + 1);
2942 memset (SDATA (safe_charsets), 255, max_charset_id + 1);
2943 request = AREF (attrs, coding_attr_iso_request);
2944 reg_usage = AREF (attrs, coding_attr_iso_usage);
2945 reg94 = XINT (XCAR (reg_usage));
2946 reg96 = XINT (XCDR (reg_usage));
2947
2948 for (tail = charset_list; CONSP (tail); tail = XCDR (tail))
2949 {
2950 Lisp_Object id;
2951 Lisp_Object reg;
2952 struct charset *charset;
2953
2954 id = XCAR (tail);
2955 charset = CHARSET_FROM_ID (XINT (id));
2956 reg = Fcdr (Fassq (id, request));
2957 if (! NILP (reg))
2958 SSET (safe_charsets, XINT (id), XINT (reg));
2959 else if (charset->iso_chars_96)
2960 {
2961 if (reg96 < 4)
2962 SSET (safe_charsets, XINT (id), reg96);
2963 }
2964 else
2965 {
2966 if (reg94 < 4)
2967 SSET (safe_charsets, XINT (id), reg94);
2968 }
2969 }
2970 ASET (attrs, coding_attr_safe_charsets, safe_charsets);
2971 }
2972
2973
2974 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
2975 Check if a text is encoded in one of ISO-2022 based coding systems.
2976 If it is, return 1, else return 0. */
2977
2978 static int
2979 detect_coding_iso_2022 (struct coding_system *coding,
2980 struct coding_detection_info *detect_info)
2981 {
2982 const unsigned char *src = coding->source, *src_base = src;
2983 const unsigned char *src_end = coding->source + coding->src_bytes;
2984 int multibytep = coding->src_multibyte;
2985 int single_shifting = 0;
2986 int id;
2987 int c, c1;
2988 int consumed_chars = 0;
2989 int i;
2990 int rejected = 0;
2991 int found = 0;
2992 int composition_count = -1;
2993
2994 detect_info->checked |= CATEGORY_MASK_ISO;
2995
2996 for (i = coding_category_iso_7; i <= coding_category_iso_8_else; i++)
2997 {
2998 struct coding_system *this = &(coding_categories[i]);
2999 Lisp_Object attrs, val;
3000
3001 if (this->id < 0)
3002 continue;
3003 attrs = CODING_ID_ATTRS (this->id);
3004 if (CODING_ISO_FLAGS (this) & CODING_ISO_FLAG_FULL_SUPPORT
3005 && ! EQ (CODING_ATTR_CHARSET_LIST (attrs), Viso_2022_charset_list))
3006 setup_iso_safe_charsets (attrs);
3007 val = CODING_ATTR_SAFE_CHARSETS (attrs);
3008 this->max_charset_id = SCHARS (val) - 1;
3009 this->safe_charsets = SDATA (val);
3010 }
3011
3012 /* A coding system of this category is always ASCII compatible. */
3013 src += coding->head_ascii;
3014
3015 while (rejected != CATEGORY_MASK_ISO)
3016 {
3017 src_base = src;
3018 ONE_MORE_BYTE (c);
3019 switch (c)
3020 {
3021 case ISO_CODE_ESC:
3022 if (inhibit_iso_escape_detection)
3023 break;
3024 single_shifting = 0;
3025 ONE_MORE_BYTE (c);
3026 if (c >= '(' && c <= '/')
3027 {
3028 /* Designation sequence for a charset of dimension 1. */
3029 ONE_MORE_BYTE (c1);
3030 if (c1 < ' ' || c1 >= 0x80
3031 || (id = iso_charset_table[0][c >= ','][c1]) < 0)
3032 /* Invalid designation sequence. Just ignore. */
3033 break;
3034 }
3035 else if (c == '$')
3036 {
3037 /* Designation sequence for a charset of dimension 2. */
3038 ONE_MORE_BYTE (c);
3039 if (c >= '@' && c <= 'B')
3040 /* Designation for JISX0208.1978, GB2312, or JISX0208. */
3041 id = iso_charset_table[1][0][c];
3042 else if (c >= '(' && c <= '/')
3043 {
3044 ONE_MORE_BYTE (c1);
3045 if (c1 < ' ' || c1 >= 0x80
3046 || (id = iso_charset_table[1][c >= ','][c1]) < 0)
3047 /* Invalid designation sequence. Just ignore. */
3048 break;
3049 }
3050 else
3051 /* Invalid designation sequence. Just ignore it. */
3052 break;
3053 }
3054 else if (c == 'N' || c == 'O')
3055 {
3056 /* ESC <Fe> for SS2 or SS3. */
3057 single_shifting = 1;
3058 rejected |= CATEGORY_MASK_ISO_7BIT | CATEGORY_MASK_ISO_8BIT;
3059 break;
3060 }
3061 else if (c == '1')
3062 {
3063 /* End of composition. */
3064 if (composition_count < 0
3065 || composition_count > MAX_COMPOSITION_COMPONENTS)
3066 /* Invalid */
3067 break;
3068 composition_count = -1;
3069 found |= CATEGORY_MASK_ISO;
3070 }
3071 else if (c >= '0' && c <= '4')
3072 {
3073 /* ESC <Fp> for start/end composition. */
3074 composition_count = 0;
3075 break;
3076 }
3077 else
3078 {
3079 /* Invalid escape sequence. Just ignore it. */
3080 break;
3081 }
3082
3083 /* We found a valid designation sequence for CHARSET. */
3084 rejected |= CATEGORY_MASK_ISO_8BIT;
3085 if (SAFE_CHARSET_P (&coding_categories[coding_category_iso_7],
3086 id))
3087 found |= CATEGORY_MASK_ISO_7;
3088 else
3089 rejected |= CATEGORY_MASK_ISO_7;
3090 if (SAFE_CHARSET_P (&coding_categories[coding_category_iso_7_tight],
3091 id))
3092 found |= CATEGORY_MASK_ISO_7_TIGHT;
3093 else
3094 rejected |= CATEGORY_MASK_ISO_7_TIGHT;
3095 if (SAFE_CHARSET_P (&coding_categories[coding_category_iso_7_else],
3096 id))
3097 found |= CATEGORY_MASK_ISO_7_ELSE;
3098 else
3099 rejected |= CATEGORY_MASK_ISO_7_ELSE;
3100 if (SAFE_CHARSET_P (&coding_categories[coding_category_iso_8_else],
3101 id))
3102 found |= CATEGORY_MASK_ISO_8_ELSE;
3103 else
3104 rejected |= CATEGORY_MASK_ISO_8_ELSE;
3105 break;
3106
3107 case ISO_CODE_SO:
3108 case ISO_CODE_SI:
3109 /* Locking shift out/in. */
3110 if (inhibit_iso_escape_detection)
3111 break;
3112 single_shifting = 0;
3113 rejected |= CATEGORY_MASK_ISO_7BIT | CATEGORY_MASK_ISO_8BIT;
3114 break;
3115
3116 case ISO_CODE_CSI:
3117 /* Control sequence introducer. */
3118 single_shifting = 0;
3119 rejected |= CATEGORY_MASK_ISO_7BIT | CATEGORY_MASK_ISO_7_ELSE;
3120 found |= CATEGORY_MASK_ISO_8_ELSE;
3121 goto check_extra_latin;
3122
3123 case ISO_CODE_SS2:
3124 case ISO_CODE_SS3:
3125 /* Single shift. */
3126 if (inhibit_iso_escape_detection)
3127 break;
3128 single_shifting = 0;
3129 rejected |= CATEGORY_MASK_ISO_7BIT;
3130 if (CODING_ISO_FLAGS (&coding_categories[coding_category_iso_8_1])
3131 & CODING_ISO_FLAG_SINGLE_SHIFT)
3132 found |= CATEGORY_MASK_ISO_8_1, single_shifting = 1;
3133 if (CODING_ISO_FLAGS (&coding_categories[coding_category_iso_8_2])
3134 & CODING_ISO_FLAG_SINGLE_SHIFT)
3135 found |= CATEGORY_MASK_ISO_8_2, single_shifting = 1;
3136 if (single_shifting)
3137 break;
3138 goto check_extra_latin;
3139
3140 default:
3141 if (c < 0)
3142 continue;
3143 if (c < 0x80)
3144 {
3145 if (composition_count >= 0)
3146 composition_count++;
3147 single_shifting = 0;
3148 break;
3149 }
3150 if (c >= 0xA0)
3151 {
3152 rejected |= CATEGORY_MASK_ISO_7BIT | CATEGORY_MASK_ISO_7_ELSE;
3153 found |= CATEGORY_MASK_ISO_8_1;
3154 /* Check the length of succeeding codes of the range
3155 0xA0..0FF. If the byte length is even, we include
3156 CATEGORY_MASK_ISO_8_2 in `found'. We can check this
3157 only when we are not single shifting. */
3158 if (! single_shifting
3159 && ! (rejected & CATEGORY_MASK_ISO_8_2))
3160 {
3161 int i = 1;
3162 while (src < src_end)
3163 {
3164 src_base = src;
3165 ONE_MORE_BYTE (c);
3166 if (c < 0xA0)
3167 {
3168 src = src_base;
3169 break;
3170 }
3171 i++;
3172 }
3173
3174 if (i & 1 && src < src_end)
3175 {
3176 rejected |= CATEGORY_MASK_ISO_8_2;
3177 if (composition_count >= 0)
3178 composition_count += i;
3179 }
3180 else
3181 {
3182 found |= CATEGORY_MASK_ISO_8_2;
3183 if (composition_count >= 0)
3184 composition_count += i / 2;
3185 }
3186 }
3187 break;
3188 }
3189 check_extra_latin:
3190 single_shifting = 0;
3191 if (! VECTORP (Vlatin_extra_code_table)
3192 || NILP (XVECTOR (Vlatin_extra_code_table)->contents[c]))
3193 {
3194 rejected = CATEGORY_MASK_ISO;
3195 break;
3196 }
3197 if (CODING_ISO_FLAGS (&coding_categories[coding_category_iso_8_1])
3198 & CODING_ISO_FLAG_LATIN_EXTRA)
3199 found |= CATEGORY_MASK_ISO_8_1;
3200 else
3201 rejected |= CATEGORY_MASK_ISO_8_1;
3202 rejected |= CATEGORY_MASK_ISO_8_2;
3203 }
3204 }
3205 detect_info->rejected |= CATEGORY_MASK_ISO;
3206 return 0;
3207
3208 no_more_source:
3209 detect_info->rejected |= rejected;
3210 detect_info->found |= (found & ~rejected);
3211 return 1;
3212 }
3213
3214
3215 /* Set designation state into CODING. Set CHARS_96 to -1 if the
3216 escape sequence should be kept. */
3217 #define DECODE_DESIGNATION(reg, dim, chars_96, final) \
3218 do { \
3219 int id, prev; \
3220 \
3221 if (final < '0' || final >= 128 \
3222 || ((id = ISO_CHARSET_TABLE (dim, chars_96, final)) < 0) \
3223 || !SAFE_CHARSET_P (coding, id)) \
3224 { \
3225 CODING_ISO_DESIGNATION (coding, reg) = -2; \
3226 chars_96 = -1; \
3227 break; \
3228 } \
3229 prev = CODING_ISO_DESIGNATION (coding, reg); \
3230 if (id == charset_jisx0201_roman) \
3231 { \
3232 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \
3233 id = charset_ascii; \
3234 } \
3235 else if (id == charset_jisx0208_1978) \
3236 { \
3237 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \
3238 id = charset_jisx0208; \
3239 } \
3240 CODING_ISO_DESIGNATION (coding, reg) = id; \
3241 /* If there was an invalid designation to REG previously, and this \
3242 designation is ASCII to REG, we should keep this designation \
3243 sequence. */ \
3244 if (prev == -2 && id == charset_ascii) \
3245 chars_96 = -1; \
3246 } while (0)
3247
3248
3249 /* Handle these composition sequence (ALT: alternate char):
3250
3251 (1) relative composition: ESC 0 CHAR ... ESC 1
3252 (2) rulebase composition: ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1
3253 (3) altchar composition: ESC 3 ALT ... ALT ESC 0 CHAR ... ESC 1
3254 (4) alt&rule composition: ESC 4 ALT RULE ... ALT ESC 0 CHAR ... ESC 1
3255
3256 When the start sequence (ESC 0/2/3/4) is found, this annotation
3257 header is produced.
3258
3259 [ -LENGTH(==-5) CODING_ANNOTATE_COMPOSITION_MASK NCHARS(==0) 0 METHOD ]
3260
3261 Then, upon reading CHAR or RULE (one or two bytes), these codes are
3262 produced until the end sequence (ESC 1) is found:
3263
3264 (1) CHAR ... CHAR
3265 (2) CHAR -2 DECODED-RULE CHAR -2 DECODED-RULE ... CHAR
3266 (3) ALT ... ALT -1 -1 CHAR ... CHAR
3267 (4) ALT -2 DECODED-RULE ALT -2 DECODED-RULE ... ALT -1 -1 CHAR ... CHAR
3268
3269 When the end sequence (ESC 1) is found, LENGTH and NCHARS in the
3270 annotation header is updated as below:
3271
3272 (1) LENGTH: unchanged, NCHARS: number of CHARs
3273 (2) LENGTH: unchanged, NCHARS: number of CHARs
3274 (3) LENGTH: += number of ALTs + 2, NCHARS: number of CHARs
3275 (4) LENGTH: += number of ALTs * 3, NCHARS: number of CHARs
3276
3277 If an error is found while composing, the annotation header is
3278 changed to:
3279
3280 [ ESC '0'/'2'/'3'/'4' -2 0 ]
3281
3282 and the sequence [ -2 DECODED-RULE ] is changed to the original
3283 byte sequence as below:
3284 o the original byte sequence is B: [ B -1 ]
3285 o the original byte sequence is B1 B2: [ B1 B2 ]
3286 and the sequence [ -1 -1 ] is changed to the original byte
3287 sequence:
3288 [ ESC '0' ]
3289 */
3290
3291 /* Decode a composition rule C1 and maybe one more byte from the
3292 source, and set RULE to the encoded composition rule, NBYTES to the
3293 length of the composition rule. If the rule is invalid, set RULE
3294 to some negative value. */
3295
3296 #define DECODE_COMPOSITION_RULE(rule, nbytes) \
3297 do { \
3298 rule = c1 - 32; \
3299 if (rule < 0) \
3300 break; \
3301 if (rule < 81) /* old format (before ver.21) */ \
3302 { \
3303 int gref = (rule) / 9; \
3304 int nref = (rule) % 9; \
3305 if (gref == 4) gref = 10; \
3306 if (nref == 4) nref = 10; \
3307 rule = COMPOSITION_ENCODE_RULE (gref, nref); \
3308 nbytes = 1; \
3309 } \
3310 else /* new format (after ver.21) */ \
3311 { \
3312 int c; \
3313 \
3314 ONE_MORE_BYTE (c); \
3315 rule = COMPOSITION_ENCODE_RULE (rule - 81, c - 32); \
3316 if (rule >= 0) \
3317 rule += 0x100; /* to destinguish it from the old format */ \
3318 nbytes = 2; \
3319 } \
3320 } while (0)
3321
3322 #define ENCODE_COMPOSITION_RULE(rule) \
3323 do { \
3324 int gref = (rule % 0x100) / 12, nref = (rule % 0x100) % 12; \
3325 \
3326 if (rule < 0x100) /* old format */ \
3327 { \
3328 if (gref == 10) gref = 4; \
3329 if (nref == 10) nref = 4; \
3330 charbuf[idx] = 32 + gref * 9 + nref; \
3331 charbuf[idx + 1] = -1; \
3332 new_chars++; \
3333 } \
3334 else /* new format */ \
3335 { \
3336 charbuf[idx] = 32 + 81 + gref; \
3337 charbuf[idx + 1] = 32 + nref; \
3338 new_chars += 2; \
3339 } \
3340 } while (0)
3341
3342 /* Finish the current composition as invalid. */
3343
3344 static int finish_composition (int *, struct composition_status *);
3345
3346 static int
3347 finish_composition (int *charbuf, struct composition_status *cmp_status)
3348 {
3349 int idx = - cmp_status->length;
3350 int new_chars;
3351
3352 /* Recover the original ESC sequence */
3353 charbuf[idx++] = ISO_CODE_ESC;
3354 charbuf[idx++] = (cmp_status->method == COMPOSITION_RELATIVE ? '0'
3355 : cmp_status->method == COMPOSITION_WITH_RULE ? '2'
3356 : cmp_status->method == COMPOSITION_WITH_ALTCHARS ? '3'
3357 /* cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS */
3358 : '4');
3359 charbuf[idx++] = -2;
3360 charbuf[idx++] = 0;
3361 charbuf[idx++] = -1;
3362 new_chars = cmp_status->nchars;
3363 if (cmp_status->method >= COMPOSITION_WITH_RULE)
3364 for (; idx < 0; idx++)
3365 {
3366 int elt = charbuf[idx];
3367
3368 if (elt == -2)
3369 {
3370 ENCODE_COMPOSITION_RULE (charbuf[idx + 1]);
3371 idx++;
3372 }
3373 else if (elt == -1)
3374 {
3375 charbuf[idx++] = ISO_CODE_ESC;
3376 charbuf[idx] = '0';
3377 new_chars += 2;
3378 }
3379 }
3380 cmp_status->state = COMPOSING_NO;
3381 return new_chars;
3382 }
3383
3384 /* If characters are under composition, finish the composition. */
3385 #define MAYBE_FINISH_COMPOSITION() \
3386 do { \
3387 if (cmp_status->state != COMPOSING_NO) \
3388 char_offset += finish_composition (charbuf, cmp_status); \
3389 } while (0)
3390
3391 /* Handle composition start sequence ESC 0, ESC 2, ESC 3, or ESC 4.
3392
3393 ESC 0 : relative composition : ESC 0 CHAR ... ESC 1
3394 ESC 2 : rulebase composition : ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1
3395 ESC 3 : altchar composition : ESC 3 CHAR ... ESC 0 CHAR ... ESC 1
3396 ESC 4 : alt&rule composition : ESC 4 CHAR RULE ... CHAR ESC 0 CHAR ... ESC 1
3397
3398 Produce this annotation sequence now:
3399
3400 [ -LENGTH(==-4) CODING_ANNOTATE_COMPOSITION_MASK NCHARS(==0) METHOD ]
3401 */
3402
3403 #define DECODE_COMPOSITION_START(c1) \
3404 do { \
3405 if (c1 == '0' \
3406 && ((cmp_status->state == COMPOSING_COMPONENT_CHAR \
3407 && cmp_status->method == COMPOSITION_WITH_ALTCHARS) \
3408 || (cmp_status->state == COMPOSING_COMPONENT_RULE \
3409 && cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS))) \
3410 { \
3411 *charbuf++ = -1; \
3412 *charbuf++= -1; \
3413 cmp_status->state = COMPOSING_CHAR; \
3414 cmp_status->length += 2; \
3415 } \
3416 else \
3417 { \
3418 MAYBE_FINISH_COMPOSITION (); \
3419 cmp_status->method = (c1 == '0' ? COMPOSITION_RELATIVE \
3420 : c1 == '2' ? COMPOSITION_WITH_RULE \
3421 : c1 == '3' ? COMPOSITION_WITH_ALTCHARS \
3422 : COMPOSITION_WITH_RULE_ALTCHARS); \
3423 cmp_status->state \
3424 = (c1 <= '2' ? COMPOSING_CHAR : COMPOSING_COMPONENT_CHAR); \
3425 ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \
3426 cmp_status->length = MAX_ANNOTATION_LENGTH; \
3427 cmp_status->nchars = cmp_status->ncomps = 0; \
3428 coding->annotated = 1; \
3429 } \
3430 } while (0)
3431
3432
3433 /* Handle composition end sequence ESC 1. */
3434
3435 #define DECODE_COMPOSITION_END() \
3436 do { \
3437 if (cmp_status->nchars == 0 \
3438 || ((cmp_status->state == COMPOSING_CHAR) \
3439 == (cmp_status->method == COMPOSITION_WITH_RULE))) \
3440 { \
3441 MAYBE_FINISH_COMPOSITION (); \
3442 goto invalid_code; \
3443 } \
3444 if (cmp_status->method == COMPOSITION_WITH_ALTCHARS) \
3445 charbuf[- cmp_status->length] -= cmp_status->ncomps + 2; \
3446 else if (cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS) \
3447 charbuf[- cmp_status->length] -= cmp_status->ncomps * 3; \
3448 charbuf[- cmp_status->length + 2] = cmp_status->nchars; \
3449 char_offset += cmp_status->nchars; \
3450 cmp_status->state = COMPOSING_NO; \
3451 } while (0)
3452
3453 /* Store a composition rule RULE in charbuf, and update cmp_status. */
3454
3455 #define STORE_COMPOSITION_RULE(rule) \
3456 do { \
3457 *charbuf++ = -2; \
3458 *charbuf++ = rule; \
3459 cmp_status->length += 2; \
3460 cmp_status->state--; \
3461 } while (0)
3462
3463 /* Store a composed char or a component char C in charbuf, and update
3464 cmp_status. */
3465
3466 #define STORE_COMPOSITION_CHAR(c) \
3467 do { \
3468 *charbuf++ = (c); \
3469 cmp_status->length++; \
3470 if (cmp_status->state == COMPOSING_CHAR) \
3471 cmp_status->nchars++; \
3472 else \
3473 cmp_status->ncomps++; \
3474 if (cmp_status->method == COMPOSITION_WITH_RULE \
3475 || (cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS \
3476 && cmp_status->state == COMPOSING_COMPONENT_CHAR)) \
3477 cmp_status->state++; \
3478 } while (0)
3479
3480
3481 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
3482
3483 static void
3484 decode_coding_iso_2022 (struct coding_system *coding)
3485 {
3486 const unsigned char *src = coding->source + coding->consumed;
3487 const unsigned char *src_end = coding->source + coding->src_bytes;
3488 const unsigned char *src_base;
3489 int *charbuf = coding->charbuf + coding->charbuf_used;
3490 /* We may produce two annotations (charset and composition) in one
3491 loop and one more charset annotation at the end. */
3492 int *charbuf_end
3493 = coding->charbuf + coding->charbuf_size - (MAX_ANNOTATION_LENGTH * 3);
3494 int consumed_chars = 0, consumed_chars_base;
3495 int multibytep = coding->src_multibyte;
3496 /* Charsets invoked to graphic plane 0 and 1 respectively. */
3497 int charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0);
3498 int charset_id_1 = CODING_ISO_INVOKED_CHARSET (coding, 1);
3499 int charset_id_2, charset_id_3;
3500 struct charset *charset;
3501 int c;
3502 struct composition_status *cmp_status = CODING_ISO_CMP_STATUS (coding);
3503 Lisp_Object attrs, charset_list;
3504 int char_offset = coding->produced_char;
3505 int last_offset = char_offset;
3506 int last_id = charset_ascii;
3507 int eol_crlf =
3508 !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos);
3509 int byte_after_cr = -1;
3510 int i;
3511
3512 CODING_GET_INFO (coding, attrs, charset_list);
3513 setup_iso_safe_charsets (attrs);
3514 /* Charset list may have been changed. */
3515 charset_list = CODING_ATTR_CHARSET_LIST (attrs);
3516 coding->safe_charsets = SDATA (CODING_ATTR_SAFE_CHARSETS (attrs));
3517
3518 if (cmp_status->state != COMPOSING_NO)
3519 {
3520 for (i = 0; i < cmp_status->length; i++)
3521 *charbuf++ = cmp_status->carryover[i];
3522 coding->annotated = 1;
3523 }
3524
3525 while (1)
3526 {
3527 int c1, c2, c3;
3528
3529 src_base = src;
3530 consumed_chars_base = consumed_chars;
3531
3532 if (charbuf >= charbuf_end)
3533 {
3534 if (byte_after_cr >= 0)
3535 src_base--;
3536 break;
3537 }
3538
3539 if (byte_after_cr >= 0)
3540 c1 = byte_after_cr, byte_after_cr = -1;
3541 else
3542 ONE_MORE_BYTE (c1);
3543 if (c1 < 0)
3544 goto invalid_code;
3545
3546 if (CODING_ISO_EXTSEGMENT_LEN (coding) > 0)
3547 {
3548 *charbuf++ = ASCII_BYTE_P (c1) ? c1 : BYTE8_TO_CHAR (c1);
3549 char_offset++;
3550 CODING_ISO_EXTSEGMENT_LEN (coding)--;
3551 continue;
3552 }
3553
3554 if (CODING_ISO_EMBEDDED_UTF_8 (coding))
3555 {
3556 if (c1 == ISO_CODE_ESC)
3557 {
3558 if (src + 1 >= src_end)
3559 goto no_more_source;
3560 *charbuf++ = ISO_CODE_ESC;
3561 char_offset++;
3562 if (src[0] == '%' && src[1] == '@')
3563 {
3564 src += 2;
3565 consumed_chars += 2;
3566 char_offset += 2;
3567 /* We are sure charbuf can contain two more chars. */
3568 *charbuf++ = '%';
3569 *charbuf++ = '@';
3570 CODING_ISO_EMBEDDED_UTF_8 (coding) = 0;
3571 }
3572 }
3573 else
3574 {
3575 *charbuf++ = ASCII_BYTE_P (c1) ? c1 : BYTE8_TO_CHAR (c1);
3576 char_offset++;
3577 }
3578 continue;
3579 }
3580
3581 if ((cmp_status->state == COMPOSING_RULE
3582 || cmp_status->state == COMPOSING_COMPONENT_RULE)
3583 && c1 != ISO_CODE_ESC)
3584 {
3585 int rule, nbytes;
3586
3587 DECODE_COMPOSITION_RULE (rule, nbytes);
3588 if (rule < 0)
3589 goto invalid_code;
3590 STORE_COMPOSITION_RULE (rule);
3591 continue;
3592 }
3593
3594 /* We produce at most one character. */
3595 switch (iso_code_class [c1])
3596 {
3597 case ISO_0x20_or_0x7F:
3598 if (charset_id_0 < 0
3599 || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_0)))
3600 /* This is SPACE or DEL. */
3601 charset = CHARSET_FROM_ID (charset_ascii);
3602 else
3603 charset = CHARSET_FROM_ID (charset_id_0);
3604 break;
3605
3606 case ISO_graphic_plane_0:
3607 if (charset_id_0 < 0)
3608 charset = CHARSET_FROM_ID (charset_ascii);
3609 else
3610 charset = CHARSET_FROM_ID (charset_id_0);
3611 break;
3612
3613 case ISO_0xA0_or_0xFF:
3614 if (charset_id_1 < 0
3615 || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_1))
3616 || CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS)
3617 goto invalid_code;
3618 /* This is a graphic character, we fall down ... */
3619
3620 case ISO_graphic_plane_1:
3621 if (charset_id_1 < 0)
3622 goto invalid_code;
3623 charset = CHARSET_FROM_ID (charset_id_1);
3624 break;
3625
3626 case ISO_control_0:
3627 if (eol_crlf && c1 == '\r')
3628 ONE_MORE_BYTE (byte_after_cr);
3629 MAYBE_FINISH_COMPOSITION ();
3630 charset = CHARSET_FROM_ID (charset_ascii);
3631 break;
3632
3633 case ISO_control_1:
3634 goto invalid_code;
3635
3636 case ISO_shift_out:
3637 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LOCKING_SHIFT)
3638 || CODING_ISO_DESIGNATION (coding, 1) < 0)
3639 goto invalid_code;
3640 CODING_ISO_INVOCATION (coding, 0) = 1;
3641 charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0);
3642 continue;
3643
3644 case ISO_shift_in:
3645 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LOCKING_SHIFT))
3646 goto invalid_code;
3647 CODING_ISO_INVOCATION (coding, 0) = 0;
3648 charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0);
3649 continue;
3650
3651 case ISO_single_shift_2_7:
3652 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS))
3653 goto invalid_code;
3654 case ISO_single_shift_2:
3655 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT))
3656 goto invalid_code;
3657 /* SS2 is handled as an escape sequence of ESC 'N' */
3658 c1 = 'N';
3659 goto label_escape_sequence;
3660
3661 case ISO_single_shift_3:
3662 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT))
3663 goto invalid_code;
3664 /* SS2 is handled as an escape sequence of ESC 'O' */
3665 c1 = 'O';
3666 goto label_escape_sequence;
3667
3668 case ISO_control_sequence_introducer:
3669 /* CSI is handled as an escape sequence of ESC '[' ... */
3670 c1 = '[';
3671 goto label_escape_sequence;
3672
3673 case ISO_escape:
3674 ONE_MORE_BYTE (c1);
3675 label_escape_sequence:
3676 /* Escape sequences handled here are invocation,
3677 designation, direction specification, and character
3678 composition specification. */
3679 switch (c1)
3680 {
3681 case '&': /* revision of following character set */
3682 ONE_MORE_BYTE (c1);
3683 if (!(c1 >= '@' && c1 <= '~'))
3684 goto invalid_code;
3685 ONE_MORE_BYTE (c1);
3686 if (c1 != ISO_CODE_ESC)
3687 goto invalid_code;
3688 ONE_MORE_BYTE (c1);
3689 goto label_escape_sequence;
3690
3691 case '$': /* designation of 2-byte character set */
3692 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DESIGNATION))
3693 goto invalid_code;
3694 {
3695 int reg, chars96;
3696
3697 ONE_MORE_BYTE (c1);
3698 if (c1 >= '@' && c1 <= 'B')
3699 { /* designation of JISX0208.1978, GB2312.1980,
3700 or JISX0208.1980 */
3701 reg = 0, chars96 = 0;
3702 }
3703 else if (c1 >= 0x28 && c1 <= 0x2B)
3704 { /* designation of DIMENSION2_CHARS94 character set */
3705 reg = c1 - 0x28, chars96 = 0;
3706 ONE_MORE_BYTE (c1);
3707 }
3708 else if (c1 >= 0x2C && c1 <= 0x2F)
3709 { /* designation of DIMENSION2_CHARS96 character set */
3710 reg = c1 - 0x2C, chars96 = 1;
3711 ONE_MORE_BYTE (c1);
3712 }
3713 else
3714 goto invalid_code;
3715 DECODE_DESIGNATION (reg, 2, chars96, c1);
3716 /* We must update these variables now. */
3717 if (reg == 0)
3718 charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0);
3719 else if (reg == 1)
3720 charset_id_1 = CODING_ISO_INVOKED_CHARSET (coding, 1);
3721 if (chars96 < 0)
3722 goto invalid_code;
3723 }
3724 continue;
3725
3726 case 'n': /* invocation of locking-shift-2 */
3727 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LOCKING_SHIFT)
3728 || CODING_ISO_DESIGNATION (coding, 2) < 0)
3729 goto invalid_code;
3730 CODING_ISO_INVOCATION (coding, 0) = 2;
3731 charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0);
3732 continue;
3733
3734 case 'o': /* invocation of locking-shift-3 */
3735 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LOCKING_SHIFT)
3736 || CODING_ISO_DESIGNATION (coding, 3) < 0)
3737 goto invalid_code;
3738 CODING_ISO_INVOCATION (coding, 0) = 3;
3739 charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0);
3740 continue;
3741
3742 case 'N': /* invocation of single-shift-2 */
3743 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT)
3744 || CODING_ISO_DESIGNATION (coding, 2) < 0)
3745 goto invalid_code;
3746 charset_id_2 = CODING_ISO_DESIGNATION (coding, 2);
3747 if (charset_id_2 < 0)
3748 charset = CHARSET_FROM_ID (charset_ascii);
3749 else
3750 charset = CHARSET_FROM_ID (charset_id_2);
3751 ONE_MORE_BYTE (c1);
3752 if (c1 < 0x20 || (c1 >= 0x80 && c1 < 0xA0))
3753 goto invalid_code;
3754 break;
3755
3756 case 'O': /* invocation of single-shift-3 */
3757 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT)
3758 || CODING_ISO_DESIGNATION (coding, 3) < 0)
3759 goto invalid_code;
3760 charset_id_3 = CODING_ISO_DESIGNATION (coding, 3);
3761 if (charset_id_3 < 0)
3762 charset = CHARSET_FROM_ID (charset_ascii);
3763 else
3764 charset = CHARSET_FROM_ID (charset_id_3);
3765 ONE_MORE_BYTE (c1);
3766 if (c1 < 0x20 || (c1 >= 0x80 && c1 < 0xA0))
3767 goto invalid_code;
3768 break;
3769
3770 case '0': case '2': case '3': case '4': /* start composition */
3771 if (! (coding->common_flags & CODING_ANNOTATE_COMPOSITION_MASK))
3772 goto invalid_code;
3773 if (last_id != charset_ascii)
3774 {
3775 ADD_CHARSET_DATA (charbuf, char_offset- last_offset, last_id);
3776 last_id = charset_ascii;
3777 last_offset = char_offset;
3778 }
3779 DECODE_COMPOSITION_START (c1);
3780 continue;
3781
3782 case '1': /* end composition */
3783 if (cmp_status->state == COMPOSING_NO)
3784 goto invalid_code;
3785 DECODE_COMPOSITION_END ();
3786 continue;
3787
3788 case '[': /* specification of direction */
3789 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DIRECTION))
3790 goto invalid_code;
3791 /* For the moment, nested direction is not supported.
3792 So, `coding->mode & CODING_MODE_DIRECTION' zero means
3793 left-to-right, and nonzero means right-to-left. */
3794 ONE_MORE_BYTE (c1);
3795 switch (c1)
3796 {
3797 case ']': /* end of the current direction */
3798 coding->mode &= ~CODING_MODE_DIRECTION;
3799
3800 case '0': /* end of the current direction */
3801 case '1': /* start of left-to-right direction */
3802 ONE_MORE_BYTE (c1);
3803 if (c1 == ']')
3804 coding->mode &= ~CODING_MODE_DIRECTION;
3805 else
3806 goto invalid_code;
3807 break;
3808
3809 case '2': /* start of right-to-left direction */
3810 ONE_MORE_BYTE (c1);
3811 if (c1 == ']')
3812 coding->mode |= CODING_MODE_DIRECTION;
3813 else
3814 goto invalid_code;
3815 break;
3816
3817 default:
3818 goto invalid_code;
3819 }
3820 continue;
3821
3822 case '%':
3823 ONE_MORE_BYTE (c1);
3824 if (c1 == '/')
3825 {
3826 /* CTEXT extended segment:
3827 ESC % / [0-4] M L --ENCODING-NAME-- \002 --BYTES--
3828 We keep these bytes as is for the moment.
3829 They may be decoded by post-read-conversion. */
3830 int dim, M, L;
3831 int size;
3832
3833 ONE_MORE_BYTE (dim);
3834 if (dim < '0' || dim > '4')
3835 goto invalid_code;
3836 ONE_MORE_BYTE (M);
3837 if (M < 128)
3838 goto invalid_code;
3839 ONE_MORE_BYTE (L);
3840 if (L < 128)
3841 goto invalid_code;
3842 size = ((M - 128) * 128) + (L - 128);
3843 if (charbuf + 6 > charbuf_end)
3844 goto break_loop;
3845 *charbuf++ = ISO_CODE_ESC;
3846 *charbuf++ = '%';
3847 *charbuf++ = '/';
3848 *charbuf++ = dim;
3849 *charbuf++ = BYTE8_TO_CHAR (M);
3850 *charbuf++ = BYTE8_TO_CHAR (L);
3851 CODING_ISO_EXTSEGMENT_LEN (coding) = size;
3852 }
3853 else if (c1 == 'G')
3854 {
3855 /* XFree86 extension for embedding UTF-8 in CTEXT:
3856 ESC % G --UTF-8-BYTES-- ESC % @
3857 We keep these bytes as is for the moment.
3858 They may be decoded by post-read-conversion. */
3859 if (charbuf + 3 > charbuf_end)
3860 goto break_loop;
3861 *charbuf++ = ISO_CODE_ESC;
3862 *charbuf++ = '%';
3863 *charbuf++ = 'G';
3864 CODING_ISO_EMBEDDED_UTF_8 (coding) = 1;
3865 }
3866 else
3867 goto invalid_code;
3868 continue;
3869 break;
3870
3871 default:
3872 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DESIGNATION))
3873 goto invalid_code;
3874 {
3875 int reg, chars96;
3876
3877 if (c1 >= 0x28 && c1 <= 0x2B)
3878 { /* designation of DIMENSION1_CHARS94 character set */
3879 reg = c1 - 0x28, chars96 = 0;
3880 ONE_MORE_BYTE (c1);
3881 }
3882 else if (c1 >= 0x2C && c1 <= 0x2F)
3883 { /* designation of DIMENSION1_CHARS96 character set */
3884 reg = c1 - 0x2C, chars96 = 1;
3885 ONE_MORE_BYTE (c1);
3886 }
3887 else
3888 goto invalid_code;
3889 DECODE_DESIGNATION (reg, 1, chars96, c1);
3890 /* We must update these variables now. */
3891 if (reg == 0)
3892 charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0);
3893 else if (reg == 1)
3894 charset_id_1 = CODING_ISO_INVOKED_CHARSET (coding, 1);
3895 if (chars96 < 0)
3896 goto invalid_code;
3897 }
3898 continue;
3899 }
3900 }
3901
3902 if (cmp_status->state == COMPOSING_NO
3903 && charset->id != charset_ascii
3904 && last_id != charset->id)
3905 {
3906 if (last_id != charset_ascii)
3907 ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id);
3908 last_id = charset->id;
3909 last_offset = char_offset;
3910 }
3911
3912 /* Now we know CHARSET and 1st position code C1 of a character.
3913 Produce a decoded character while getting 2nd and 3rd
3914 position codes C2, C3 if necessary. */
3915 if (CHARSET_DIMENSION (charset) > 1)
3916 {
3917 ONE_MORE_BYTE (c2);
3918 if (c2 < 0x20 || (c2 >= 0x80 && c2 < 0xA0)
3919 || ((c1 & 0x80) != (c2 & 0x80)))
3920 /* C2 is not in a valid range. */
3921 goto invalid_code;
3922 if (CHARSET_DIMENSION (charset) == 2)
3923 c1 = (c1 << 8) | c2;
3924 else
3925 {
3926 ONE_MORE_BYTE (c3);
3927 if (c3 < 0x20 || (c3 >= 0x80 && c3 < 0xA0)
3928 || ((c1 & 0x80) != (c3 & 0x80)))
3929 /* C3 is not in a valid range. */
3930 goto invalid_code;
3931 c1 = (c1 << 16) | (c2 << 8) | c2;
3932 }
3933 }
3934 c1 &= 0x7F7F7F;
3935 CODING_DECODE_CHAR (coding, src, src_base, src_end, charset, c1, c);
3936 if (c < 0)
3937 {
3938 MAYBE_FINISH_COMPOSITION ();
3939 for (; src_base < src; src_base++, char_offset++)
3940 {
3941 if (ASCII_BYTE_P (*src_base))
3942 *charbuf++ = *src_base;
3943 else
3944 *charbuf++ = BYTE8_TO_CHAR (*src_base);
3945 }
3946 }
3947 else if (cmp_status->state == COMPOSING_NO)
3948 {
3949 *charbuf++ = c;
3950 char_offset++;
3951 }
3952 else if ((cmp_status->state == COMPOSING_CHAR
3953 ? cmp_status->nchars
3954 : cmp_status->ncomps)
3955 >= MAX_COMPOSITION_COMPONENTS)
3956 {
3957 /* Too long composition. */
3958 MAYBE_FINISH_COMPOSITION ();
3959 *charbuf++ = c;
3960 char_offset++;
3961 }
3962 else
3963 STORE_COMPOSITION_CHAR (c);
3964 continue;
3965
3966 invalid_code:
3967 MAYBE_FINISH_COMPOSITION ();
3968 src = src_base;
3969 consumed_chars = consumed_chars_base;
3970 ONE_MORE_BYTE (c);
3971 *charbuf++ = c < 0 ? -c : ASCII_BYTE_P (c) ? c : BYTE8_TO_CHAR (c);
3972 char_offset++;
3973 coding->errors++;
3974 continue;
3975
3976 break_loop:
3977 break;
3978 }
3979
3980 no_more_source:
3981 if (cmp_status->state != COMPOSING_NO)
3982 {
3983 if (coding->mode & CODING_MODE_LAST_BLOCK)
3984 MAYBE_FINISH_COMPOSITION ();
3985 else
3986 {
3987 charbuf -= cmp_status->length;
3988 for (i = 0; i < cmp_status->length; i++)
3989 cmp_status->carryover[i] = charbuf[i];
3990 }
3991 }
3992 else if (last_id != charset_ascii)
3993 ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id);
3994 coding->consumed_char += consumed_chars_base;
3995 coding->consumed = src_base - coding->source;
3996 coding->charbuf_used = charbuf - coding->charbuf;
3997 }
3998
3999
4000 /* ISO2022 encoding stuff. */
4001
4002 /*
4003 It is not enough to say just "ISO2022" on encoding, we have to
4004 specify more details. In Emacs, each coding system of ISO2022
4005 variant has the following specifications:
4006 1. Initial designation to G0 thru G3.
4007 2. Allows short-form designation?
4008 3. ASCII should be designated to G0 before control characters?
4009 4. ASCII should be designated to G0 at end of line?
4010 5. 7-bit environment or 8-bit environment?
4011 6. Use locking-shift?
4012 7. Use Single-shift?
4013 And the following two are only for Japanese:
4014 8. Use ASCII in place of JIS0201-1976-Roman?
4015 9. Use JISX0208-1983 in place of JISX0208-1978?
4016 These specifications are encoded in CODING_ISO_FLAGS (coding) as flag bits
4017 defined by macros CODING_ISO_FLAG_XXX. See `coding.h' for more
4018 details.
4019 */
4020
4021 /* Produce codes (escape sequence) for designating CHARSET to graphic
4022 register REG at DST, and increment DST. If <final-char> of CHARSET is
4023 '@', 'A', or 'B' and the coding system CODING allows, produce
4024 designation sequence of short-form. */
4025
4026 #define ENCODE_DESIGNATION(charset, reg, coding) \
4027 do { \
4028 unsigned char final_char = CHARSET_ISO_FINAL (charset); \
4029 const char *intermediate_char_94 = "()*+"; \
4030 const char *intermediate_char_96 = ",-./"; \
4031 int revision = -1; \
4032 int c; \
4033 \
4034 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_REVISION) \
4035 revision = CHARSET_ISO_REVISION (charset); \
4036 \
4037 if (revision >= 0) \
4038 { \
4039 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, '&'); \
4040 EMIT_ONE_BYTE ('@' + revision); \
4041 } \
4042 EMIT_ONE_ASCII_BYTE (ISO_CODE_ESC); \
4043 if (CHARSET_DIMENSION (charset) == 1) \
4044 { \
4045 if (! CHARSET_ISO_CHARS_96 (charset)) \
4046 c = intermediate_char_94[reg]; \
4047 else \
4048 c = intermediate_char_96[reg]; \
4049 EMIT_ONE_ASCII_BYTE (c); \
4050 } \
4051 else \
4052 { \
4053 EMIT_ONE_ASCII_BYTE ('$'); \
4054 if (! CHARSET_ISO_CHARS_96 (charset)) \
4055 { \
4056 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LONG_FORM \
4057 || reg != 0 \
4058 || final_char < '@' || final_char > 'B') \
4059 EMIT_ONE_ASCII_BYTE (intermediate_char_94[reg]); \
4060 } \
4061 else \
4062 EMIT_ONE_ASCII_BYTE (intermediate_char_96[reg]); \
4063 } \
4064 EMIT_ONE_ASCII_BYTE (final_char); \
4065 \
4066 CODING_ISO_DESIGNATION (coding, reg) = CHARSET_ID (charset); \
4067 } while (0)
4068
4069
4070 /* The following two macros produce codes (control character or escape
4071 sequence) for ISO2022 single-shift functions (single-shift-2 and
4072 single-shift-3). */
4073
4074 #define ENCODE_SINGLE_SHIFT_2 \
4075 do { \
4076 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
4077 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'N'); \
4078 else \
4079 EMIT_ONE_BYTE (ISO_CODE_SS2); \
4080 CODING_ISO_SINGLE_SHIFTING (coding) = 1; \
4081 } while (0)
4082
4083
4084 #define ENCODE_SINGLE_SHIFT_3 \
4085 do { \
4086 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
4087 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'O'); \
4088 else \
4089 EMIT_ONE_BYTE (ISO_CODE_SS3); \
4090 CODING_ISO_SINGLE_SHIFTING (coding) = 1; \
4091 } while (0)
4092
4093
4094 /* The following four macros produce codes (control character or
4095 escape sequence) for ISO2022 locking-shift functions (shift-in,
4096 shift-out, locking-shift-2, and locking-shift-3). */
4097
4098 #define ENCODE_SHIFT_IN \
4099 do { \
4100 EMIT_ONE_ASCII_BYTE (ISO_CODE_SI); \
4101 CODING_ISO_INVOCATION (coding, 0) = 0; \
4102 } while (0)
4103
4104
4105 #define ENCODE_SHIFT_OUT \
4106 do { \
4107 EMIT_ONE_ASCII_BYTE (ISO_CODE_SO); \
4108 CODING_ISO_INVOCATION (coding, 0) = 1; \
4109 } while (0)
4110
4111
4112 #define ENCODE_LOCKING_SHIFT_2 \
4113 do { \
4114 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \
4115 CODING_ISO_INVOCATION (coding, 0) = 2; \
4116 } while (0)
4117
4118
4119 #define ENCODE_LOCKING_SHIFT_3 \
4120 do { \
4121 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \
4122 CODING_ISO_INVOCATION (coding, 0) = 3; \
4123 } while (0)
4124
4125
4126 /* Produce codes for a DIMENSION1 character whose character set is
4127 CHARSET and whose position-code is C1. Designation and invocation
4128 sequences are also produced in advance if necessary. */
4129
4130 #define ENCODE_ISO_CHARACTER_DIMENSION1(charset, c1) \
4131 do { \
4132 int id = CHARSET_ID (charset); \
4133 \
4134 if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \
4135 && id == charset_ascii) \
4136 { \
4137 id = charset_jisx0201_roman; \
4138 charset = CHARSET_FROM_ID (id); \
4139 } \
4140 \
4141 if (CODING_ISO_SINGLE_SHIFTING (coding)) \
4142 { \
4143 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
4144 EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \
4145 else \
4146 EMIT_ONE_BYTE (c1 | 0x80); \
4147 CODING_ISO_SINGLE_SHIFTING (coding) = 0; \
4148 break; \
4149 } \
4150 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \
4151 { \
4152 EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \
4153 break; \
4154 } \
4155 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \
4156 { \
4157 EMIT_ONE_BYTE (c1 | 0x80); \
4158 break; \
4159 } \
4160 else \
4161 /* Since CHARSET is not yet invoked to any graphic planes, we \
4162 must invoke it, or, at first, designate it to some graphic \
4163 register. Then repeat the loop to actually produce the \
4164 character. */ \
4165 dst = encode_invocation_designation (charset, coding, dst, \
4166 &produced_chars); \
4167 } while (1)
4168
4169
4170 /* Produce codes for a DIMENSION2 character whose character set is
4171 CHARSET and whose position-codes are C1 and C2. Designation and
4172 invocation codes are also produced in advance if necessary. */
4173
4174 #define ENCODE_ISO_CHARACTER_DIMENSION2(charset, c1, c2) \
4175 do { \
4176 int id = CHARSET_ID (charset); \
4177 \
4178 if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \
4179 && id == charset_jisx0208) \
4180 { \
4181 id = charset_jisx0208_1978; \
4182 charset = CHARSET_FROM_ID (id); \
4183 } \
4184 \
4185 if (CODING_ISO_SINGLE_SHIFTING (coding)) \
4186 { \
4187 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
4188 EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \
4189 else \
4190 EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \
4191 CODING_ISO_SINGLE_SHIFTING (coding) = 0; \
4192 break; \
4193 } \
4194 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \
4195 { \
4196 EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \
4197 break; \
4198 } \
4199 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \
4200 { \
4201 EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \
4202 break; \
4203 } \
4204 else \
4205 /* Since CHARSET is not yet invoked to any graphic planes, we \
4206 must invoke it, or, at first, designate it to some graphic \
4207 register. Then repeat the loop to actually produce the \
4208 character. */ \
4209 dst = encode_invocation_designation (charset, coding, dst, \
4210 &produced_chars); \
4211 } while (1)
4212
4213
4214 #define ENCODE_ISO_CHARACTER(charset, c) \
4215 do { \
4216 int code = ENCODE_CHAR ((charset), (c)); \
4217 \
4218 if (CHARSET_DIMENSION (charset) == 1) \
4219 ENCODE_ISO_CHARACTER_DIMENSION1 ((charset), code); \
4220 else \
4221 ENCODE_ISO_CHARACTER_DIMENSION2 ((charset), code >> 8, code & 0xFF); \
4222 } while (0)
4223
4224
4225 /* Produce designation and invocation codes at a place pointed by DST
4226 to use CHARSET. The element `spec.iso_2022' of *CODING is updated.
4227 Return new DST. */
4228
4229 unsigned char *
4230 encode_invocation_designation (struct charset *charset,
4231 struct coding_system *coding,
4232 unsigned char *dst, int *p_nchars)
4233 {
4234 int multibytep = coding->dst_multibyte;
4235 int produced_chars = *p_nchars;
4236 int reg; /* graphic register number */
4237 int id = CHARSET_ID (charset);
4238
4239 /* At first, check designations. */
4240 for (reg = 0; reg < 4; reg++)
4241 if (id == CODING_ISO_DESIGNATION (coding, reg))
4242 break;
4243
4244 if (reg >= 4)
4245 {
4246 /* CHARSET is not yet designated to any graphic registers. */
4247 /* At first check the requested designation. */
4248 reg = CODING_ISO_REQUEST (coding, id);
4249 if (reg < 0)
4250 /* Since CHARSET requests no special designation, designate it
4251 to graphic register 0. */
4252 reg = 0;
4253
4254 ENCODE_DESIGNATION (charset, reg, coding);
4255 }
4256
4257 if (CODING_ISO_INVOCATION (coding, 0) != reg
4258 && CODING_ISO_INVOCATION (coding, 1) != reg)
4259 {
4260 /* Since the graphic register REG is not invoked to any graphic
4261 planes, invoke it to graphic plane 0. */
4262 switch (reg)
4263 {
4264 case 0: /* graphic register 0 */
4265 ENCODE_SHIFT_IN;
4266 break;
4267
4268 case 1: /* graphic register 1 */
4269 ENCODE_SHIFT_OUT;
4270 break;
4271
4272 case 2: /* graphic register 2 */
4273 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT)
4274 ENCODE_SINGLE_SHIFT_2;
4275 else
4276 ENCODE_LOCKING_SHIFT_2;
4277 break;
4278
4279 case 3: /* graphic register 3 */
4280 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT)
4281 ENCODE_SINGLE_SHIFT_3;
4282 else
4283 ENCODE_LOCKING_SHIFT_3;
4284 break;
4285 }
4286 }
4287
4288 *p_nchars = produced_chars;
4289 return dst;
4290 }
4291
4292 /* The following three macros produce codes for indicating direction
4293 of text. */
4294 #define ENCODE_CONTROL_SEQUENCE_INTRODUCER \
4295 do { \
4296 if (CODING_ISO_FLAGS (coding) == CODING_ISO_FLAG_SEVEN_BITS) \
4297 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, '['); \
4298 else \
4299 EMIT_ONE_BYTE (ISO_CODE_CSI); \
4300 } while (0)
4301
4302
4303 #define ENCODE_DIRECTION_R2L() \
4304 do { \
4305 ENCODE_CONTROL_SEQUENCE_INTRODUCER (dst); \
4306 EMIT_TWO_ASCII_BYTES ('2', ']'); \
4307 } while (0)
4308
4309
4310 #define ENCODE_DIRECTION_L2R() \
4311 do { \
4312 ENCODE_CONTROL_SEQUENCE_INTRODUCER (dst); \
4313 EMIT_TWO_ASCII_BYTES ('0', ']'); \
4314 } while (0)
4315
4316
4317 /* Produce codes for designation and invocation to reset the graphic
4318 planes and registers to initial state. */
4319 #define ENCODE_RESET_PLANE_AND_REGISTER() \
4320 do { \
4321 int reg; \
4322 struct charset *charset; \
4323 \
4324 if (CODING_ISO_INVOCATION (coding, 0) != 0) \
4325 ENCODE_SHIFT_IN; \
4326 for (reg = 0; reg < 4; reg++) \
4327 if (CODING_ISO_INITIAL (coding, reg) >= 0 \
4328 && (CODING_ISO_DESIGNATION (coding, reg) \
4329 != CODING_ISO_INITIAL (coding, reg))) \
4330 { \
4331 charset = CHARSET_FROM_ID (CODING_ISO_INITIAL (coding, reg)); \
4332 ENCODE_DESIGNATION (charset, reg, coding); \
4333 } \
4334 } while (0)
4335
4336
4337 /* Produce designation sequences of charsets in the line started from
4338 SRC to a place pointed by DST, and return updated DST.
4339
4340 If the current block ends before any end-of-line, we may fail to
4341 find all the necessary designations. */
4342
4343 static unsigned char *
4344 encode_designation_at_bol (struct coding_system *coding, int *charbuf,
4345 int *charbuf_end, unsigned char *dst)
4346 {
4347 struct charset *charset;
4348 /* Table of charsets to be designated to each graphic register. */
4349 int r[4];
4350 int c, found = 0, reg;
4351 int produced_chars = 0;
4352 int multibytep = coding->dst_multibyte;
4353 Lisp_Object attrs;
4354 Lisp_Object charset_list;
4355
4356 attrs = CODING_ID_ATTRS (coding->id);
4357 charset_list = CODING_ATTR_CHARSET_LIST (attrs);
4358 if (EQ (charset_list, Qiso_2022))
4359 charset_list = Viso_2022_charset_list;
4360
4361 for (reg = 0; reg < 4; reg++)
4362 r[reg] = -1;
4363
4364 while (found < 4)
4365 {
4366 int id;
4367
4368 c = *charbuf++;
4369 if (c == '\n')
4370 break;
4371 charset = char_charset (c, charset_list, NULL);
4372 id = CHARSET_ID (charset);
4373 reg = CODING_ISO_REQUEST (coding, id);
4374 if (reg >= 0 && r[reg] < 0)
4375 {
4376 found++;
4377 r[reg] = id;
4378 }
4379 }
4380
4381 if (found)
4382 {
4383 for (reg = 0; reg < 4; reg++)
4384 if (r[reg] >= 0
4385 && CODING_ISO_DESIGNATION (coding, reg) != r[reg])
4386 ENCODE_DESIGNATION (CHARSET_FROM_ID (r[reg]), reg, coding);
4387 }
4388
4389 return dst;
4390 }
4391
4392 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". */
4393
4394 static int
4395 encode_coding_iso_2022 (struct coding_system *coding)
4396 {
4397 int multibytep = coding->dst_multibyte;
4398 int *charbuf = coding->charbuf;
4399 int *charbuf_end = charbuf + coding->charbuf_used;
4400 unsigned char *dst = coding->destination + coding->produced;
4401 unsigned char *dst_end = coding->destination + coding->dst_bytes;
4402 int safe_room = 16;
4403 int bol_designation
4404 = (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DESIGNATE_AT_BOL
4405 && CODING_ISO_BOL (coding));
4406 int produced_chars = 0;
4407 Lisp_Object attrs, eol_type, charset_list;
4408 int ascii_compatible;
4409 int c;
4410 int preferred_charset_id = -1;
4411
4412 CODING_GET_INFO (coding, attrs, charset_list);
4413 eol_type = inhibit_eol_conversion ? Qunix : CODING_ID_EOL_TYPE (coding->id);
4414 if (VECTORP (eol_type))
4415 eol_type = Qunix;
4416
4417 setup_iso_safe_charsets (attrs);
4418 /* Charset list may have been changed. */
4419 charset_list = CODING_ATTR_CHARSET_LIST (attrs);
4420 coding->safe_charsets = SDATA (CODING_ATTR_SAFE_CHARSETS (attrs));
4421
4422 ascii_compatible
4423 = (! NILP (CODING_ATTR_ASCII_COMPAT (attrs))
4424 && ! (CODING_ISO_FLAGS (coding) & (CODING_ISO_FLAG_DESIGNATION
4425 | CODING_ISO_FLAG_LOCKING_SHIFT)));
4426
4427 while (charbuf < charbuf_end)
4428 {
4429 ASSURE_DESTINATION (safe_room);
4430
4431 if (bol_designation)
4432 {
4433 unsigned char *dst_prev = dst;
4434
4435 /* We have to produce designation sequences if any now. */
4436 dst = encode_designation_at_bol (coding, charbuf, charbuf_end, dst);
4437 bol_designation = 0;
4438 /* We are sure that designation sequences are all ASCII bytes. */
4439 produced_chars += dst - dst_prev;
4440 }
4441
4442 c = *charbuf++;
4443
4444 if (c < 0)
4445 {
4446 /* Handle an annotation. */
4447 switch (*charbuf)
4448 {
4449 case CODING_ANNOTATE_COMPOSITION_MASK:
4450 /* Not yet implemented. */
4451 break;
4452 case CODING_ANNOTATE_CHARSET_MASK:
4453 preferred_charset_id = charbuf[2];
4454 if (preferred_charset_id >= 0
4455 && NILP (Fmemq (make_number (preferred_charset_id),
4456 charset_list)))
4457 preferred_charset_id = -1;
4458 break;
4459 default:
4460 abort ();
4461 }
4462 charbuf += -c - 1;
4463 continue;
4464 }
4465
4466 /* Now encode the character C. */
4467 if (c < 0x20 || c == 0x7F)
4468 {
4469 if (c == '\n'
4470 || (c == '\r' && EQ (eol_type, Qmac)))
4471 {
4472 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_RESET_AT_EOL)
4473 ENCODE_RESET_PLANE_AND_REGISTER ();
4474 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_INIT_AT_BOL)
4475 {
4476 int i;
4477
4478 for (i = 0; i < 4; i++)
4479 CODING_ISO_DESIGNATION (coding, i)
4480 = CODING_ISO_INITIAL (coding, i);
4481 }
4482 bol_designation
4483 = CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DESIGNATE_AT_BOL;
4484 }
4485 else if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_RESET_AT_CNTL)
4486 ENCODE_RESET_PLANE_AND_REGISTER ();
4487 EMIT_ONE_ASCII_BYTE (c);
4488 }
4489 else if (ASCII_CHAR_P (c))
4490 {
4491 if (ascii_compatible)
4492 EMIT_ONE_ASCII_BYTE (c);
4493 else
4494 {
4495 struct charset *charset = CHARSET_FROM_ID (charset_ascii);
4496 ENCODE_ISO_CHARACTER (charset, c);
4497 }
4498 }
4499 else if (CHAR_BYTE8_P (c))
4500 {
4501 c = CHAR_TO_BYTE8 (c);
4502 EMIT_ONE_BYTE (c);
4503 }
4504 else
4505 {
4506 struct charset *charset;
4507
4508 if (preferred_charset_id >= 0)
4509 {
4510 charset = CHARSET_FROM_ID (preferred_charset_id);
4511 if (! CHAR_CHARSET_P (c, charset))
4512 charset = char_charset (c, charset_list, NULL);
4513 }
4514 else
4515 charset = char_charset (c, charset_list, NULL);
4516 if (!charset)
4517 {
4518 if (coding->mode & CODING_MODE_SAFE_ENCODING)
4519 {
4520 c = CODING_INHIBIT_CHARACTER_SUBSTITUTION;
4521 charset = CHARSET_FROM_ID (charset_ascii);
4522 }
4523 else
4524 {
4525 c = coding->default_char;
4526 charset = char_charset (c, charset_list, NULL);
4527 }
4528 }
4529 ENCODE_ISO_CHARACTER (charset, c);
4530 }
4531 }
4532
4533 if (coding->mode & CODING_MODE_LAST_BLOCK
4534 && CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_RESET_AT_EOL)
4535 {
4536 ASSURE_DESTINATION (safe_room);
4537 ENCODE_RESET_PLANE_AND_REGISTER ();
4538 }
4539 record_conversion_result (coding, CODING_RESULT_SUCCESS);
4540 CODING_ISO_BOL (coding) = bol_designation;
4541 coding->produced_char += produced_chars;
4542 coding->produced = dst - coding->destination;
4543 return 0;
4544 }
4545
4546 \f
4547 /*** 8,9. SJIS and BIG5 handlers ***/
4548
4549 /* Although SJIS and BIG5 are not ISO's coding system, they are used
4550 quite widely. So, for the moment, Emacs supports them in the bare
4551 C code. But, in the future, they may be supported only by CCL. */
4552
4553 /* SJIS is a coding system encoding three character sets: ASCII, right
4554 half of JISX0201-Kana, and JISX0208. An ASCII character is encoded
4555 as is. A character of charset katakana-jisx0201 is encoded by
4556 "position-code + 0x80". A character of charset japanese-jisx0208
4557 is encoded in 2-byte but two position-codes are divided and shifted
4558 so that it fit in the range below.
4559
4560 --- CODE RANGE of SJIS ---
4561 (character set) (range)
4562 ASCII 0x00 .. 0x7F
4563 KATAKANA-JISX0201 0xA0 .. 0xDF
4564 JISX0208 (1st byte) 0x81 .. 0x9F and 0xE0 .. 0xEF
4565 (2nd byte) 0x40 .. 0x7E and 0x80 .. 0xFC
4566 -------------------------------
4567
4568 */
4569
4570 /* BIG5 is a coding system encoding two character sets: ASCII and
4571 Big5. An ASCII character is encoded as is. Big5 is a two-byte
4572 character set and is encoded in two-byte.
4573
4574 --- CODE RANGE of BIG5 ---
4575 (character set) (range)
4576 ASCII 0x00 .. 0x7F
4577 Big5 (1st byte) 0xA1 .. 0xFE
4578 (2nd byte) 0x40 .. 0x7E and 0xA1 .. 0xFE
4579 --------------------------
4580
4581 */
4582
4583 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4584 Check if a text is encoded in SJIS. If it is, return
4585 CATEGORY_MASK_SJIS, else return 0. */
4586
4587 static int
4588 detect_coding_sjis (struct coding_system *coding,
4589 struct coding_detection_info *detect_info)
4590 {
4591 const unsigned char *src = coding->source, *src_base;
4592 const unsigned char *src_end = coding->source + coding->src_bytes;
4593 int multibytep = coding->src_multibyte;
4594 int consumed_chars = 0;
4595 int found = 0;
4596 int c;
4597 Lisp_Object attrs, charset_list;
4598 int max_first_byte_of_2_byte_code;
4599
4600 CODING_GET_INFO (coding, attrs, charset_list);
4601 max_first_byte_of_2_byte_code
4602 = (XINT (Flength (charset_list)) > 3 ? 0xFC : 0xEF);
4603
4604 detect_info->checked |= CATEGORY_MASK_SJIS;
4605 /* A coding system of this category is always ASCII compatible. */
4606 src += coding->head_ascii;
4607
4608 while (1)
4609 {
4610 src_base = src;
4611 ONE_MORE_BYTE (c);
4612 if (c < 0x80)
4613 continue;
4614 if ((c >= 0x81 && c <= 0x9F)
4615 || (c >= 0xE0 && c <= max_first_byte_of_2_byte_code))
4616 {
4617 ONE_MORE_BYTE (c);
4618 if (c < 0x40 || c == 0x7F || c > 0xFC)
4619 break;
4620 found = CATEGORY_MASK_SJIS;
4621 }
4622 else if (c >= 0xA0 && c < 0xE0)
4623 found = CATEGORY_MASK_SJIS;
4624 else
4625 break;
4626 }
4627 detect_info->rejected |= CATEGORY_MASK_SJIS;
4628 return 0;
4629
4630 no_more_source:
4631 if (src_base < src && coding->mode & CODING_MODE_LAST_BLOCK)
4632 {
4633 detect_info->rejected |= CATEGORY_MASK_SJIS;
4634 return 0;
4635 }
4636 detect_info->found |= found;
4637 return 1;
4638 }
4639
4640 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4641 Check if a text is encoded in BIG5. If it is, return
4642 CATEGORY_MASK_BIG5, else return 0. */
4643
4644 static int
4645 detect_coding_big5 (struct coding_system *coding,
4646 struct coding_detection_info *detect_info)
4647 {
4648 const unsigned char *src = coding->source, *src_base;
4649 const unsigned char *src_end = coding->source + coding->src_bytes;
4650 int multibytep = coding->src_multibyte;
4651 int consumed_chars = 0;
4652 int found = 0;
4653 int c;
4654
4655 detect_info->checked |= CATEGORY_MASK_BIG5;
4656 /* A coding system of this category is always ASCII compatible. */
4657 src += coding->head_ascii;
4658
4659 while (1)
4660 {
4661 src_base = src;
4662 ONE_MORE_BYTE (c);
4663 if (c < 0x80)
4664 continue;
4665 if (c >= 0xA1)
4666 {
4667 ONE_MORE_BYTE (c);
4668 if (c < 0x40 || (c >= 0x7F && c <= 0xA0))
4669 return 0;
4670 found = CATEGORY_MASK_BIG5;
4671 }
4672 else
4673 break;
4674 }
4675 detect_info->rejected |= CATEGORY_MASK_BIG5;
4676 return 0;
4677
4678 no_more_source:
4679 if (src_base < src && coding->mode & CODING_MODE_LAST_BLOCK)
4680 {
4681 detect_info->rejected |= CATEGORY_MASK_BIG5;
4682 return 0;
4683 }
4684 detect_info->found |= found;
4685 return 1;
4686 }
4687
4688 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions".
4689 If SJIS_P is 1, decode SJIS text, else decode BIG5 test. */
4690
4691 static void
4692 decode_coding_sjis (struct coding_system *coding)
4693 {
4694 const unsigned char *src = coding->source + coding->consumed;
4695 const unsigned char *src_end = coding->source + coding->src_bytes;
4696 const unsigned char *src_base;
4697 int *charbuf = coding->charbuf + coding->charbuf_used;
4698 /* We may produce one charset annotation in one loop and one more at
4699 the end. */
4700 int *charbuf_end
4701 = coding->charbuf + coding->charbuf_size - (MAX_ANNOTATION_LENGTH * 2);
4702 int consumed_chars = 0, consumed_chars_base;
4703 int multibytep = coding->src_multibyte;
4704 struct charset *charset_roman, *charset_kanji, *charset_kana;
4705 struct charset *charset_kanji2;
4706 Lisp_Object attrs, charset_list, val;
4707 int char_offset = coding->produced_char;
4708 int last_offset = char_offset;
4709 int last_id = charset_ascii;
4710 int eol_crlf =
4711 !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos);
4712 int byte_after_cr = -1;
4713
4714 CODING_GET_INFO (coding, attrs, charset_list);
4715
4716 val = charset_list;
4717 charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
4718 charset_kana = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
4719 charset_kanji = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
4720 charset_kanji2 = NILP (val) ? NULL : CHARSET_FROM_ID (XINT (XCAR (val)));
4721
4722 while (1)
4723 {
4724 int c, c1;
4725 struct charset *charset;
4726
4727 src_base = src;
4728 consumed_chars_base = consumed_chars;
4729
4730 if (charbuf >= charbuf_end)
4731 {
4732 if (byte_after_cr >= 0)
4733 src_base--;
4734 break;
4735 }
4736
4737 if (byte_after_cr >= 0)
4738 c = byte_after_cr, byte_after_cr = -1;
4739 else
4740 ONE_MORE_BYTE (c);
4741 if (c < 0)
4742 goto invalid_code;
4743 if (c < 0x80)
4744 {
4745 if (eol_crlf && c == '\r')
4746 ONE_MORE_BYTE (byte_after_cr);
4747 charset = charset_roman;
4748 }
4749 else if (c == 0x80 || c == 0xA0)
4750 goto invalid_code;
4751 else if (c >= 0xA1 && c <= 0xDF)
4752 {
4753 /* SJIS -> JISX0201-Kana */
4754 c &= 0x7F;
4755 charset = charset_kana;
4756 }
4757 else if (c <= 0xEF)
4758 {
4759 /* SJIS -> JISX0208 */
4760 ONE_MORE_BYTE (c1);
4761 if (c1 < 0x40 || c1 == 0x7F || c1 > 0xFC)
4762 goto invalid_code;
4763 c = (c << 8) | c1;
4764 SJIS_TO_JIS (c);
4765 charset = charset_kanji;
4766 }
4767 else if (c <= 0xFC && charset_kanji2)
4768 {
4769 /* SJIS -> JISX0213-2 */
4770 ONE_MORE_BYTE (c1);
4771 if (c1 < 0x40 || c1 == 0x7F || c1 > 0xFC)
4772 goto invalid_code;
4773 c = (c << 8) | c1;
4774 SJIS_TO_JIS2 (c);
4775 charset = charset_kanji2;
4776 }
4777 else
4778 goto invalid_code;
4779 if (charset->id != charset_ascii
4780 && last_id != charset->id)
4781 {
4782 if (last_id != charset_ascii)
4783 ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id);
4784 last_id = charset->id;
4785 last_offset = char_offset;
4786 }
4787 CODING_DECODE_CHAR (coding, src, src_base, src_end, charset, c, c);
4788 *charbuf++ = c;
4789 char_offset++;
4790 continue;
4791
4792 invalid_code:
4793 src = src_base;
4794 consumed_chars = consumed_chars_base;
4795 ONE_MORE_BYTE (c);
4796 *charbuf++ = c < 0 ? -c : BYTE8_TO_CHAR (c);
4797 char_offset++;
4798 coding->errors++;
4799 }
4800
4801 no_more_source:
4802 if (last_id != charset_ascii)
4803 ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id);
4804 coding->consumed_char += consumed_chars_base;
4805 coding->consumed = src_base - coding->source;
4806 coding->charbuf_used = charbuf - coding->charbuf;
4807 }
4808
4809 static void
4810 decode_coding_big5 (struct coding_system *coding)
4811 {
4812 const unsigned char *src = coding->source + coding->consumed;
4813 const unsigned char *src_end = coding->source + coding->src_bytes;
4814 const unsigned char *src_base;
4815 int *charbuf = coding->charbuf + coding->charbuf_used;
4816 /* We may produce one charset annotation in one loop and one more at
4817 the end. */
4818 int *charbuf_end
4819 = coding->charbuf + coding->charbuf_size - (MAX_ANNOTATION_LENGTH * 2);
4820 int consumed_chars = 0, consumed_chars_base;
4821 int multibytep = coding->src_multibyte;
4822 struct charset *charset_roman, *charset_big5;
4823 Lisp_Object attrs, charset_list, val;
4824 int char_offset = coding->produced_char;
4825 int last_offset = char_offset;
4826 int last_id = charset_ascii;
4827 int eol_crlf =
4828 !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos);
4829 int byte_after_cr = -1;
4830
4831 CODING_GET_INFO (coding, attrs, charset_list);
4832 val = charset_list;
4833 charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
4834 charset_big5 = CHARSET_FROM_ID (XINT (XCAR (val)));
4835
4836 while (1)
4837 {
4838 int c, c1;
4839 struct charset *charset;
4840
4841 src_base = src;
4842 consumed_chars_base = consumed_chars;
4843
4844 if (charbuf >= charbuf_end)
4845 {
4846 if (byte_after_cr >= 0)
4847 src_base--;
4848 break;
4849 }
4850
4851 if (byte_after_cr >= 0)
4852 c = byte_after_cr, byte_after_cr = -1;
4853 else
4854 ONE_MORE_BYTE (c);
4855
4856 if (c < 0)
4857 goto invalid_code;
4858 if (c < 0x80)
4859 {
4860 if (eol_crlf && c == '\r')
4861 ONE_MORE_BYTE (byte_after_cr);
4862 charset = charset_roman;
4863 }
4864 else
4865 {
4866 /* BIG5 -> Big5 */
4867 if (c < 0xA1 || c > 0xFE)
4868 goto invalid_code;
4869 ONE_MORE_BYTE (c1);
4870 if (c1 < 0x40 || (c1 > 0x7E && c1 < 0xA1) || c1 > 0xFE)
4871 goto invalid_code;
4872 c = c << 8 | c1;
4873 charset = charset_big5;
4874 }
4875 if (charset->id != charset_ascii
4876 && last_id != charset->id)
4877 {
4878 if (last_id != charset_ascii)
4879 ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id);
4880 last_id = charset->id;
4881 last_offset = char_offset;
4882 }
4883 CODING_DECODE_CHAR (coding, src, src_base, src_end, charset, c, c);
4884 *charbuf++ = c;
4885 char_offset++;
4886 continue;
4887
4888 invalid_code:
4889 src = src_base;
4890 consumed_chars = consumed_chars_base;
4891 ONE_MORE_BYTE (c);
4892 *charbuf++ = c < 0 ? -c : BYTE8_TO_CHAR (c);
4893 char_offset++;
4894 coding->errors++;
4895 }
4896
4897 no_more_source:
4898 if (last_id != charset_ascii)
4899 ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id);
4900 coding->consumed_char += consumed_chars_base;
4901 coding->consumed = src_base - coding->source;
4902 coding->charbuf_used = charbuf - coding->charbuf;
4903 }
4904
4905 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions".
4906 This function can encode charsets `ascii', `katakana-jisx0201',
4907 `japanese-jisx0208', `chinese-big5-1', and `chinese-big5-2'. We
4908 are sure that all these charsets are registered as official charset
4909 (i.e. do not have extended leading-codes). Characters of other
4910 charsets are produced without any encoding. If SJIS_P is 1, encode
4911 SJIS text, else encode BIG5 text. */
4912
4913 static int
4914 encode_coding_sjis (struct coding_system *coding)
4915 {
4916 int multibytep = coding->dst_multibyte;
4917 int *charbuf = coding->charbuf;
4918 int *charbuf_end = charbuf + coding->charbuf_used;
4919 unsigned char *dst = coding->destination + coding->produced;
4920 unsigned char *dst_end = coding->destination + coding->dst_bytes;
4921 int safe_room = 4;
4922 int produced_chars = 0;
4923 Lisp_Object attrs, charset_list, val;
4924 int ascii_compatible;
4925 struct charset *charset_roman, *charset_kanji, *charset_kana;
4926 struct charset *charset_kanji2;
4927 int c;
4928
4929 CODING_GET_INFO (coding, attrs, charset_list);
4930 val = charset_list;
4931 charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
4932 charset_kana = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
4933 charset_kanji = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
4934 charset_kanji2 = NILP (val) ? NULL : CHARSET_FROM_ID (XINT (XCAR (val)));
4935
4936 ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs));
4937
4938 while (charbuf < charbuf_end)
4939 {
4940 ASSURE_DESTINATION (safe_room);
4941 c = *charbuf++;
4942 /* Now encode the character C. */
4943 if (ASCII_CHAR_P (c) && ascii_compatible)
4944 EMIT_ONE_ASCII_BYTE (c);
4945 else if (CHAR_BYTE8_P (c))
4946 {
4947 c = CHAR_TO_BYTE8 (c);
4948 EMIT_ONE_BYTE (c);
4949 }
4950 else
4951 {
4952 unsigned code;
4953 struct charset *charset = char_charset (c, charset_list, &code);
4954
4955 if (!charset)
4956 {
4957 if (coding->mode & CODING_MODE_SAFE_ENCODING)
4958 {
4959 code = CODING_INHIBIT_CHARACTER_SUBSTITUTION;
4960 charset = CHARSET_FROM_ID (charset_ascii);
4961 }
4962 else
4963 {
4964 c = coding->default_char;
4965 charset = char_charset (c, charset_list, &code);
4966 }
4967 }
4968 if (code == CHARSET_INVALID_CODE (charset))
4969 abort ();
4970 if (charset == charset_kanji)
4971 {
4972 int c1, c2;
4973 JIS_TO_SJIS (code);
4974 c1 = code >> 8, c2 = code & 0xFF;
4975 EMIT_TWO_BYTES (c1, c2);
4976 }
4977 else if (charset == charset_kana)
4978 EMIT_ONE_BYTE (code | 0x80);
4979 else if (charset_kanji2 && charset == charset_kanji2)
4980 {
4981 int c1, c2;
4982
4983 c1 = code >> 8;
4984 if (c1 == 0x21 || (c1 >= 0x23 && c1 <= 0x25)
4985 || c1 == 0x28
4986 || (c1 >= 0x2C && c1 <= 0x2F) || c1 >= 0x6E)
4987 {
4988 JIS_TO_SJIS2 (code);
4989 c1 = code >> 8, c2 = code & 0xFF;
4990 EMIT_TWO_BYTES (c1, c2);
4991 }
4992 else
4993 EMIT_ONE_ASCII_BYTE (code & 0x7F);
4994 }
4995 else
4996 EMIT_ONE_ASCII_BYTE (code & 0x7F);
4997 }
4998 }
4999 record_conversion_result (coding, CODING_RESULT_SUCCESS);
5000 coding->produced_char += produced_chars;
5001 coding->produced = dst - coding->destination;
5002 return 0;
5003 }
5004
5005 static int
5006 encode_coding_big5 (struct coding_system *coding)
5007 {
5008 int multibytep = coding->dst_multibyte;
5009 int *charbuf = coding->charbuf;
5010 int *charbuf_end = charbuf + coding->charbuf_used;
5011 unsigned char *dst = coding->destination + coding->produced;
5012 unsigned char *dst_end = coding->destination + coding->dst_bytes;
5013 int safe_room = 4;
5014 int produced_chars = 0;
5015 Lisp_Object attrs, charset_list, val;
5016 int ascii_compatible;
5017 struct charset *charset_roman, *charset_big5;
5018 int c;
5019
5020 CODING_GET_INFO (coding, attrs, charset_list);
5021 val = charset_list;
5022 charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
5023 charset_big5 = CHARSET_FROM_ID (XINT (XCAR (val)));
5024 ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs));
5025
5026 while (charbuf < charbuf_end)
5027 {
5028 ASSURE_DESTINATION (safe_room);
5029 c = *charbuf++;
5030 /* Now encode the character C. */
5031 if (ASCII_CHAR_P (c) && ascii_compatible)
5032 EMIT_ONE_ASCII_BYTE (c);
5033 else if (CHAR_BYTE8_P (c))
5034 {
5035 c = CHAR_TO_BYTE8 (c);
5036 EMIT_ONE_BYTE (c);
5037 }
5038 else
5039 {
5040 unsigned code;
5041 struct charset *charset = char_charset (c, charset_list, &code);
5042
5043 if (! charset)
5044 {
5045 if (coding->mode & CODING_MODE_SAFE_ENCODING)
5046 {
5047 code = CODING_INHIBIT_CHARACTER_SUBSTITUTION;
5048 charset = CHARSET_FROM_ID (charset_ascii);
5049 }
5050 else
5051 {
5052 c = coding->default_char;
5053 charset = char_charset (c, charset_list, &code);
5054 }
5055 }
5056 if (code == CHARSET_INVALID_CODE (charset))
5057 abort ();
5058 if (charset == charset_big5)
5059 {
5060 int c1, c2;
5061
5062 c1 = code >> 8, c2 = code & 0xFF;
5063 EMIT_TWO_BYTES (c1, c2);
5064 }
5065 else
5066 EMIT_ONE_ASCII_BYTE (code & 0x7F);
5067 }
5068 }
5069 record_conversion_result (coding, CODING_RESULT_SUCCESS);
5070 coding->produced_char += produced_chars;
5071 coding->produced = dst - coding->destination;
5072 return 0;
5073 }
5074
5075 \f
5076 /*** 10. CCL handlers ***/
5077
5078 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
5079 Check if a text is encoded in a coding system of which
5080 encoder/decoder are written in CCL program. If it is, return
5081 CATEGORY_MASK_CCL, else return 0. */
5082
5083 static int
5084 detect_coding_ccl (struct coding_system *coding,
5085 struct coding_detection_info *detect_info)
5086 {
5087 const unsigned char *src = coding->source, *src_base;
5088 const unsigned char *src_end = coding->source + coding->src_bytes;
5089 int multibytep = coding->src_multibyte;
5090 int consumed_chars = 0;
5091 int found = 0;
5092 unsigned char *valids;
5093 int head_ascii = coding->head_ascii;
5094 Lisp_Object attrs;
5095
5096 detect_info->checked |= CATEGORY_MASK_CCL;
5097
5098 coding = &coding_categories[coding_category_ccl];
5099 valids = CODING_CCL_VALIDS (coding);
5100 attrs = CODING_ID_ATTRS (coding->id);
5101 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs)))
5102 src += head_ascii;
5103
5104 while (1)
5105 {
5106 int c;
5107
5108 src_base = src;
5109 ONE_MORE_BYTE (c);
5110 if (c < 0 || ! valids[c])
5111 break;
5112 if ((valids[c] > 1))
5113 found = CATEGORY_MASK_CCL;
5114 }
5115 detect_info->rejected |= CATEGORY_MASK_CCL;
5116 return 0;
5117
5118 no_more_source:
5119 detect_info->found |= found;
5120 return 1;
5121 }
5122
5123 static void
5124 decode_coding_ccl (struct coding_system *coding)
5125 {
5126 const unsigned char *src = coding->source + coding->consumed;
5127 const unsigned char *src_end = coding->source + coding->src_bytes;
5128 int *charbuf = coding->charbuf + coding->charbuf_used;
5129 int *charbuf_end = coding->charbuf + coding->charbuf_size;
5130 int consumed_chars = 0;
5131 int multibytep = coding->src_multibyte;
5132 struct ccl_program *ccl = &coding->spec.ccl->ccl;
5133 int source_charbuf[1024];
5134 int source_byteidx[1025];
5135 Lisp_Object attrs, charset_list;
5136
5137 CODING_GET_INFO (coding, attrs, charset_list);
5138
5139 while (1)
5140 {
5141 const unsigned char *p = src;
5142 int i = 0;
5143
5144 if (multibytep)
5145 {
5146 while (i < 1024 && p < src_end)
5147 {
5148 source_byteidx[i] = p - src;
5149 source_charbuf[i++] = STRING_CHAR_ADVANCE (p);
5150 }
5151 source_byteidx[i] = p - src;
5152 }
5153 else
5154 while (i < 1024 && p < src_end)
5155 source_charbuf[i++] = *p++;
5156
5157 if (p == src_end && coding->mode & CODING_MODE_LAST_BLOCK)
5158 ccl->last_block = 1;
5159 ccl_driver (ccl, source_charbuf, charbuf, i, charbuf_end - charbuf,
5160 charset_list);
5161 charbuf += ccl->produced;
5162 if (multibytep)
5163 src += source_byteidx[ccl->consumed];
5164 else
5165 src += ccl->consumed;
5166 consumed_chars += ccl->consumed;
5167 if (p == src_end || ccl->status != CCL_STAT_SUSPEND_BY_SRC)
5168 break;
5169 }
5170
5171 switch (ccl->status)
5172 {
5173 case CCL_STAT_SUSPEND_BY_SRC:
5174 record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_SRC);
5175 break;
5176 case CCL_STAT_SUSPEND_BY_DST:
5177 record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_DST);
5178 break;
5179 case CCL_STAT_QUIT:
5180 case CCL_STAT_INVALID_CMD:
5181 record_conversion_result (coding, CODING_RESULT_INTERRUPT);
5182 break;
5183 default:
5184 record_conversion_result (coding, CODING_RESULT_SUCCESS);
5185 break;
5186 }
5187 coding->consumed_char += consumed_chars;
5188 coding->consumed = src - coding->source;
5189 coding->charbuf_used = charbuf - coding->charbuf;
5190 }
5191
5192 static int
5193 encode_coding_ccl (struct coding_system *coding)
5194 {
5195 struct ccl_program *ccl = &coding->spec.ccl->ccl;
5196 int multibytep = coding->dst_multibyte;
5197 int *charbuf = coding->charbuf;
5198 int *charbuf_end = charbuf + coding->charbuf_used;
5199 unsigned char *dst = coding->destination + coding->produced;
5200 unsigned char *dst_end = coding->destination + coding->dst_bytes;
5201 int destination_charbuf[1024];
5202 int i, produced_chars = 0;
5203 Lisp_Object attrs, charset_list;
5204
5205 CODING_GET_INFO (coding, attrs, charset_list);
5206 if (coding->consumed_char == coding->src_chars
5207 && coding->mode & CODING_MODE_LAST_BLOCK)
5208 ccl->last_block = 1;
5209
5210 while (charbuf < charbuf_end)
5211 {
5212 ccl_driver (ccl, charbuf, destination_charbuf,
5213 charbuf_end - charbuf, 1024, charset_list);
5214 if (multibytep)
5215 {
5216 ASSURE_DESTINATION (ccl->produced * 2);
5217 for (i = 0; i < ccl->produced; i++)
5218 EMIT_ONE_BYTE (destination_charbuf[i] & 0xFF);
5219 }
5220 else
5221 {
5222 ASSURE_DESTINATION (ccl->produced);
5223 for (i = 0; i < ccl->produced; i++)
5224 *dst++ = destination_charbuf[i] & 0xFF;
5225 produced_chars += ccl->produced;
5226 }
5227 charbuf += ccl->consumed;
5228 if (ccl->status == CCL_STAT_QUIT
5229 || ccl->status == CCL_STAT_INVALID_CMD)
5230 break;
5231 }
5232
5233 switch (ccl->status)
5234 {
5235 case CCL_STAT_SUSPEND_BY_SRC:
5236 record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_SRC);
5237 break;
5238 case CCL_STAT_SUSPEND_BY_DST:
5239 record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_DST);
5240 break;
5241 case CCL_STAT_QUIT:
5242 case CCL_STAT_INVALID_CMD:
5243 record_conversion_result (coding, CODING_RESULT_INTERRUPT);
5244 break;
5245 default:
5246 record_conversion_result (coding, CODING_RESULT_SUCCESS);
5247 break;
5248 }
5249
5250 coding->produced_char += produced_chars;
5251 coding->produced = dst - coding->destination;
5252 return 0;
5253 }
5254
5255
5256 \f
5257 /*** 10, 11. no-conversion handlers ***/
5258
5259 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
5260
5261 static void
5262 decode_coding_raw_text (struct coding_system *coding)
5263 {
5264 int eol_crlf =
5265 !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos);
5266
5267 coding->chars_at_source = 1;
5268 coding->consumed_char = coding->src_chars;
5269 coding->consumed = coding->src_bytes;
5270 if (eol_crlf && coding->source[coding->src_bytes - 1] == '\r')
5271 {
5272 coding->consumed_char--;
5273 coding->consumed--;
5274 record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_SRC);
5275 }
5276 else
5277 record_conversion_result (coding, CODING_RESULT_SUCCESS);
5278 }
5279
5280 static int
5281 encode_coding_raw_text (struct coding_system *coding)
5282 {
5283 int multibytep = coding->dst_multibyte;
5284 int *charbuf = coding->charbuf;
5285 int *charbuf_end = coding->charbuf + coding->charbuf_used;
5286 unsigned char *dst = coding->destination + coding->produced;
5287 unsigned char *dst_end = coding->destination + coding->dst_bytes;
5288 int produced_chars = 0;
5289 int c;
5290
5291 if (multibytep)
5292 {
5293 int safe_room = MAX_MULTIBYTE_LENGTH * 2;
5294
5295 if (coding->src_multibyte)
5296 while (charbuf < charbuf_end)
5297 {
5298 ASSURE_DESTINATION (safe_room);
5299 c = *charbuf++;
5300 if (ASCII_CHAR_P (c))
5301 EMIT_ONE_ASCII_BYTE (c);
5302 else if (CHAR_BYTE8_P (c))
5303 {
5304 c = CHAR_TO_BYTE8 (c);
5305 EMIT_ONE_BYTE (c);
5306 }
5307 else
5308 {
5309 unsigned char str[MAX_MULTIBYTE_LENGTH], *p0 = str, *p1 = str;
5310
5311 CHAR_STRING_ADVANCE (c, p1);
5312 while (p0 < p1)
5313 {
5314 EMIT_ONE_BYTE (*p0);
5315 p0++;
5316 }
5317 }
5318 }
5319 else
5320 while (charbuf < charbuf_end)
5321 {
5322 ASSURE_DESTINATION (safe_room);
5323 c = *charbuf++;
5324 EMIT_ONE_BYTE (c);
5325 }
5326 }
5327 else
5328 {
5329 if (coding->src_multibyte)
5330 {
5331 int safe_room = MAX_MULTIBYTE_LENGTH;
5332
5333 while (charbuf < charbuf_end)
5334 {
5335 ASSURE_DESTINATION (safe_room);
5336 c = *charbuf++;
5337 if (ASCII_CHAR_P (c))
5338 *dst++ = c;
5339 else if (CHAR_BYTE8_P (c))
5340 *dst++ = CHAR_TO_BYTE8 (c);
5341 else
5342 CHAR_STRING_ADVANCE (c, dst);
5343 }
5344 }
5345 else
5346 {
5347 ASSURE_DESTINATION (charbuf_end - charbuf);
5348 while (charbuf < charbuf_end && dst < dst_end)
5349 *dst++ = *charbuf++;
5350 }
5351 produced_chars = dst - (coding->destination + coding->produced);
5352 }
5353 record_conversion_result (coding, CODING_RESULT_SUCCESS);
5354 coding->produced_char += produced_chars;
5355 coding->produced = dst - coding->destination;
5356 return 0;
5357 }
5358
5359 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
5360 Check if a text is encoded in a charset-based coding system. If it
5361 is, return 1, else return 0. */
5362
5363 static int
5364 detect_coding_charset (struct coding_system *coding,
5365 struct coding_detection_info *detect_info)
5366 {
5367 const unsigned char *src = coding->source, *src_base;
5368 const unsigned char *src_end = coding->source + coding->src_bytes;
5369 int multibytep = coding->src_multibyte;
5370 int consumed_chars = 0;
5371 Lisp_Object attrs, valids, name;
5372 int found = 0;
5373 int head_ascii = coding->head_ascii;
5374 int check_latin_extra = 0;
5375
5376 detect_info->checked |= CATEGORY_MASK_CHARSET;
5377
5378 coding = &coding_categories[coding_category_charset];
5379 attrs = CODING_ID_ATTRS (coding->id);
5380 valids = AREF (attrs, coding_attr_charset_valids);
5381 name = CODING_ID_NAME (coding->id);
5382 if (strncmp (SSDATA (SYMBOL_NAME (name)),
5383 "iso-8859-", sizeof ("iso-8859-") - 1) == 0
5384 || strncmp (SSDATA (SYMBOL_NAME (name)),
5385 "iso-latin-", sizeof ("iso-latin-") - 1) == 0)
5386 check_latin_extra = 1;
5387
5388 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs)))
5389 src += head_ascii;
5390
5391 while (1)
5392 {
5393 int c;
5394 Lisp_Object val;
5395 struct charset *charset;
5396 int dim, idx;
5397
5398 src_base = src;
5399 ONE_MORE_BYTE (c);
5400 if (c < 0)
5401 continue;
5402 val = AREF (valids, c);
5403 if (NILP (val))
5404 break;
5405 if (c >= 0x80)
5406 {
5407 if (c < 0xA0
5408 && check_latin_extra
5409 && (!VECTORP (Vlatin_extra_code_table)
5410 || NILP (XVECTOR (Vlatin_extra_code_table)->contents[c])))
5411 break;
5412 found = CATEGORY_MASK_CHARSET;
5413 }
5414 if (INTEGERP (val))
5415 {
5416 charset = CHARSET_FROM_ID (XFASTINT (val));
5417 dim = CHARSET_DIMENSION (charset);
5418 for (idx = 1; idx < dim; idx++)
5419 {
5420 if (src == src_end)
5421 goto too_short;
5422 ONE_MORE_BYTE (c);
5423 if (c < charset->code_space[(dim - 1 - idx) * 2]
5424 || c > charset->code_space[(dim - 1 - idx) * 2 + 1])
5425 break;
5426 }
5427 if (idx < dim)
5428 break;
5429 }
5430 else
5431 {
5432 idx = 1;
5433 for (; CONSP (val); val = XCDR (val))
5434 {
5435 charset = CHARSET_FROM_ID (XFASTINT (XCAR (val)));
5436 dim = CHARSET_DIMENSION (charset);
5437 while (idx < dim)
5438 {
5439 if (src == src_end)
5440 goto too_short;
5441 ONE_MORE_BYTE (c);
5442 if (c < charset->code_space[(dim - 1 - idx) * 4]
5443 || c > charset->code_space[(dim - 1 - idx) * 4 + 1])
5444 break;
5445 idx++;
5446 }
5447 if (idx == dim)
5448 {
5449 val = Qnil;
5450 break;
5451 }
5452 }
5453 if (CONSP (val))
5454 break;
5455 }
5456 }
5457 too_short:
5458 detect_info->rejected |= CATEGORY_MASK_CHARSET;
5459 return 0;
5460
5461 no_more_source:
5462 detect_info->found |= found;
5463 return 1;
5464 }
5465
5466 static void
5467 decode_coding_charset (struct coding_system *coding)
5468 {
5469 const unsigned char *src = coding->source + coding->consumed;
5470 const unsigned char *src_end = coding->source + coding->src_bytes;
5471 const unsigned char *src_base;
5472 int *charbuf = coding->charbuf + coding->charbuf_used;
5473 /* We may produce one charset annotation in one loop and one more at
5474 the end. */
5475 int *charbuf_end
5476 = coding->charbuf + coding->charbuf_size - (MAX_ANNOTATION_LENGTH * 2);
5477 int consumed_chars = 0, consumed_chars_base;
5478 int multibytep = coding->src_multibyte;
5479 Lisp_Object attrs, charset_list, valids;
5480 int char_offset = coding->produced_char;
5481 int last_offset = char_offset;
5482 int last_id = charset_ascii;
5483 int eol_crlf =
5484 !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos);
5485 int byte_after_cr = -1;
5486
5487 CODING_GET_INFO (coding, attrs, charset_list);
5488 valids = AREF (attrs, coding_attr_charset_valids);
5489
5490 while (1)
5491 {
5492 int c;
5493 Lisp_Object val;
5494 struct charset *charset;
5495 int dim;
5496 int len = 1;
5497 unsigned code;
5498
5499 src_base = src;
5500 consumed_chars_base = consumed_chars;
5501
5502 if (charbuf >= charbuf_end)
5503 {
5504 if (byte_after_cr >= 0)
5505 src_base--;
5506 break;
5507 }
5508
5509 if (byte_after_cr >= 0)
5510 {
5511 c = byte_after_cr;
5512 byte_after_cr = -1;
5513 }
5514 else
5515 {
5516 ONE_MORE_BYTE (c);
5517 if (eol_crlf && c == '\r')
5518 ONE_MORE_BYTE (byte_after_cr);
5519 }
5520 if (c < 0)
5521 goto invalid_code;
5522 code = c;
5523
5524 val = AREF (valids, c);
5525 if (! INTEGERP (val) && ! CONSP (val))
5526 goto invalid_code;
5527 if (INTEGERP (val))
5528 {
5529 charset = CHARSET_FROM_ID (XFASTINT (val));
5530 dim = CHARSET_DIMENSION (charset);
5531 while (len < dim)
5532 {
5533 ONE_MORE_BYTE (c);
5534 code = (code << 8) | c;
5535 len++;
5536 }
5537 CODING_DECODE_CHAR (coding, src, src_base, src_end,
5538 charset, code, c);
5539 }
5540 else
5541 {
5542 /* VAL is a list of charset IDs. It is assured that the
5543 list is sorted by charset dimensions (smaller one
5544 comes first). */
5545 while (CONSP (val))
5546 {
5547 charset = CHARSET_FROM_ID (XFASTINT (XCAR (val)));
5548 dim = CHARSET_DIMENSION (charset);
5549 while (len < dim)
5550 {
5551 ONE_MORE_BYTE (c);
5552 code = (code << 8) | c;
5553 len++;
5554 }
5555 CODING_DECODE_CHAR (coding, src, src_base,
5556 src_end, charset, code, c);
5557 if (c >= 0)
5558 break;
5559 val = XCDR (val);
5560 }
5561 }
5562 if (c < 0)
5563 goto invalid_code;
5564 if (charset->id != charset_ascii
5565 && last_id != charset->id)
5566 {
5567 if (last_id != charset_ascii)
5568 ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id);
5569 last_id = charset->id;
5570 last_offset = char_offset;
5571 }
5572
5573 *charbuf++ = c;
5574 char_offset++;
5575 continue;
5576
5577 invalid_code:
5578 src = src_base;
5579 consumed_chars = consumed_chars_base;
5580 ONE_MORE_BYTE (c);
5581 *charbuf++ = c < 0 ? -c : ASCII_BYTE_P (c) ? c : BYTE8_TO_CHAR (c);
5582 char_offset++;
5583 coding->errors++;
5584 }
5585
5586 no_more_source:
5587 if (last_id != charset_ascii)
5588 ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id);
5589 coding->consumed_char += consumed_chars_base;
5590 coding->consumed = src_base - coding->source;
5591 coding->charbuf_used = charbuf - coding->charbuf;
5592 }
5593
5594 static int
5595 encode_coding_charset (struct coding_system *coding)
5596 {
5597 int multibytep = coding->dst_multibyte;
5598 int *charbuf = coding->charbuf;
5599 int *charbuf_end = charbuf + coding->charbuf_used;
5600 unsigned char *dst = coding->destination + coding->produced;
5601 unsigned char *dst_end = coding->destination + coding->dst_bytes;
5602 int safe_room = MAX_MULTIBYTE_LENGTH;
5603 int produced_chars = 0;
5604 Lisp_Object attrs, charset_list;
5605 int ascii_compatible;
5606 int c;
5607
5608 CODING_GET_INFO (coding, attrs, charset_list);
5609 ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs));
5610
5611 while (charbuf < charbuf_end)
5612 {
5613 struct charset *charset;
5614 unsigned code;
5615
5616 ASSURE_DESTINATION (safe_room);
5617 c = *charbuf++;
5618 if (ascii_compatible && ASCII_CHAR_P (c))
5619 EMIT_ONE_ASCII_BYTE (c);
5620 else if (CHAR_BYTE8_P (c))
5621 {
5622 c = CHAR_TO_BYTE8 (c);
5623 EMIT_ONE_BYTE (c);
5624 }
5625 else
5626 {
5627 charset = char_charset (c, charset_list, &code);
5628 if (charset)
5629 {
5630 if (CHARSET_DIMENSION (charset) == 1)
5631 EMIT_ONE_BYTE (code);
5632 else if (CHARSET_DIMENSION (charset) == 2)
5633 EMIT_TWO_BYTES (code >> 8, code & 0xFF);
5634 else if (CHARSET_DIMENSION (charset) == 3)
5635 EMIT_THREE_BYTES (code >> 16, (code >> 8) & 0xFF, code & 0xFF);
5636 else
5637 EMIT_FOUR_BYTES (code >> 24, (code >> 16) & 0xFF,
5638 (code >> 8) & 0xFF, code & 0xFF);
5639 }
5640 else
5641 {
5642 if (coding->mode & CODING_MODE_SAFE_ENCODING)
5643 c = CODING_INHIBIT_CHARACTER_SUBSTITUTION;
5644 else
5645 c = coding->default_char;
5646 EMIT_ONE_BYTE (c);
5647 }
5648 }
5649 }
5650
5651 record_conversion_result (coding, CODING_RESULT_SUCCESS);
5652 coding->produced_char += produced_chars;
5653 coding->produced = dst - coding->destination;
5654 return 0;
5655 }
5656
5657 \f
5658 /*** 7. C library functions ***/
5659
5660 /* Setup coding context CODING from information about CODING_SYSTEM.
5661 If CODING_SYSTEM is nil, `no-conversion' is assumed. If
5662 CODING_SYSTEM is invalid, signal an error. */
5663
5664 void
5665 setup_coding_system (Lisp_Object coding_system, struct coding_system *coding)
5666 {
5667 Lisp_Object attrs;
5668 Lisp_Object eol_type;
5669 Lisp_Object coding_type;
5670 Lisp_Object val;
5671
5672 if (NILP (coding_system))
5673 coding_system = Qundecided;
5674
5675 CHECK_CODING_SYSTEM_GET_ID (coding_system, coding->id);
5676
5677 attrs = CODING_ID_ATTRS (coding->id);
5678 eol_type = inhibit_eol_conversion ? Qunix : CODING_ID_EOL_TYPE (coding->id);
5679
5680 coding->mode = 0;
5681 coding->head_ascii = -1;
5682 if (VECTORP (eol_type))
5683 coding->common_flags = (CODING_REQUIRE_DECODING_MASK
5684 | CODING_REQUIRE_DETECTION_MASK);
5685 else if (! EQ (eol_type, Qunix))
5686 coding->common_flags = (CODING_REQUIRE_DECODING_MASK
5687 | CODING_REQUIRE_ENCODING_MASK);
5688 else
5689 coding->common_flags = 0;
5690 if (! NILP (CODING_ATTR_POST_READ (attrs)))
5691 coding->common_flags |= CODING_REQUIRE_DECODING_MASK;
5692 if (! NILP (CODING_ATTR_PRE_WRITE (attrs)))
5693 coding->common_flags |= CODING_REQUIRE_ENCODING_MASK;
5694 if (! NILP (CODING_ATTR_FOR_UNIBYTE (attrs)))
5695 coding->common_flags |= CODING_FOR_UNIBYTE_MASK;
5696
5697 val = CODING_ATTR_SAFE_CHARSETS (attrs);
5698 coding->max_charset_id = SCHARS (val) - 1;
5699 coding->safe_charsets = SDATA (val);
5700 coding->default_char = XINT (CODING_ATTR_DEFAULT_CHAR (attrs));
5701 coding->carryover_bytes = 0;
5702
5703 coding_type = CODING_ATTR_TYPE (attrs);
5704 if (EQ (coding_type, Qundecided))
5705 {
5706 coding->detector = NULL;
5707 coding->decoder = decode_coding_raw_text;
5708 coding->encoder = encode_coding_raw_text;
5709 coding->common_flags |= CODING_REQUIRE_DETECTION_MASK;
5710 }
5711 else if (EQ (coding_type, Qiso_2022))
5712 {
5713 int i;
5714 int flags = XINT (AREF (attrs, coding_attr_iso_flags));
5715
5716 /* Invoke graphic register 0 to plane 0. */
5717 CODING_ISO_INVOCATION (coding, 0) = 0;
5718 /* Invoke graphic register 1 to plane 1 if we can use 8-bit. */
5719 CODING_ISO_INVOCATION (coding, 1)
5720 = (flags & CODING_ISO_FLAG_SEVEN_BITS ? -1 : 1);
5721 /* Setup the initial status of designation. */
5722 for (i = 0; i < 4; i++)
5723 CODING_ISO_DESIGNATION (coding, i) = CODING_ISO_INITIAL (coding, i);
5724 /* Not single shifting initially. */
5725 CODING_ISO_SINGLE_SHIFTING (coding) = 0;
5726 /* Beginning of buffer should also be regarded as bol. */
5727 CODING_ISO_BOL (coding) = 1;
5728 coding->detector = detect_coding_iso_2022;
5729 coding->decoder = decode_coding_iso_2022;
5730 coding->encoder = encode_coding_iso_2022;
5731 if (flags & CODING_ISO_FLAG_SAFE)
5732 coding->mode |= CODING_MODE_SAFE_ENCODING;
5733 coding->common_flags
5734 |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK
5735 | CODING_REQUIRE_FLUSHING_MASK);
5736 if (flags & CODING_ISO_FLAG_COMPOSITION)
5737 coding->common_flags |= CODING_ANNOTATE_COMPOSITION_MASK;
5738 if (flags & CODING_ISO_FLAG_DESIGNATION)
5739 coding->common_flags |= CODING_ANNOTATE_CHARSET_MASK;
5740 if (flags & CODING_ISO_FLAG_FULL_SUPPORT)
5741 {
5742 setup_iso_safe_charsets (attrs);
5743 val = CODING_ATTR_SAFE_CHARSETS (attrs);
5744 coding->max_charset_id = SCHARS (val) - 1;
5745 coding->safe_charsets = SDATA (val);
5746 }
5747 CODING_ISO_FLAGS (coding) = flags;
5748 CODING_ISO_CMP_STATUS (coding)->state = COMPOSING_NO;
5749 CODING_ISO_CMP_STATUS (coding)->method = COMPOSITION_NO;
5750 CODING_ISO_EXTSEGMENT_LEN (coding) = 0;
5751 CODING_ISO_EMBEDDED_UTF_8 (coding) = 0;
5752 }
5753 else if (EQ (coding_type, Qcharset))
5754 {
5755 coding->detector = detect_coding_charset;
5756 coding->decoder = decode_coding_charset;
5757 coding->encoder = encode_coding_charset;
5758 coding->common_flags
5759 |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK);
5760 }
5761 else if (EQ (coding_type, Qutf_8))
5762 {
5763 val = AREF (attrs, coding_attr_utf_bom);
5764 CODING_UTF_8_BOM (coding) = (CONSP (val) ? utf_detect_bom
5765 : EQ (val, Qt) ? utf_with_bom
5766 : utf_without_bom);
5767 coding->detector = detect_coding_utf_8;
5768 coding->decoder = decode_coding_utf_8;
5769 coding->encoder = encode_coding_utf_8;
5770 coding->common_flags
5771 |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK);
5772 if (CODING_UTF_8_BOM (coding) == utf_detect_bom)
5773 coding->common_flags |= CODING_REQUIRE_DETECTION_MASK;
5774 }
5775 else if (EQ (coding_type, Qutf_16))
5776 {
5777 val = AREF (attrs, coding_attr_utf_bom);
5778 CODING_UTF_16_BOM (coding) = (CONSP (val) ? utf_detect_bom
5779 : EQ (val, Qt) ? utf_with_bom
5780 : utf_without_bom);
5781 val = AREF (attrs, coding_attr_utf_16_endian);
5782 CODING_UTF_16_ENDIAN (coding) = (EQ (val, Qbig) ? utf_16_big_endian
5783 : utf_16_little_endian);
5784 CODING_UTF_16_SURROGATE (coding) = 0;
5785 coding->detector = detect_coding_utf_16;
5786 coding->decoder = decode_coding_utf_16;
5787 coding->encoder = encode_coding_utf_16;
5788 coding->common_flags
5789 |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK);
5790 if (CODING_UTF_16_BOM (coding) == utf_detect_bom)
5791 coding->common_flags |= CODING_REQUIRE_DETECTION_MASK;
5792 }
5793 else if (EQ (coding_type, Qccl))
5794 {
5795 coding->detector = detect_coding_ccl;
5796 coding->decoder = decode_coding_ccl;
5797 coding->encoder = encode_coding_ccl;
5798 coding->common_flags
5799 |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK
5800 | CODING_REQUIRE_FLUSHING_MASK);
5801 }
5802 else if (EQ (coding_type, Qemacs_mule))
5803 {
5804 coding->detector = detect_coding_emacs_mule;
5805 coding->decoder = decode_coding_emacs_mule;
5806 coding->encoder = encode_coding_emacs_mule;
5807 coding->common_flags
5808 |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK);
5809 coding->spec.emacs_mule.full_support = 1;
5810 if (! NILP (AREF (attrs, coding_attr_emacs_mule_full))
5811 && ! EQ (CODING_ATTR_CHARSET_LIST (attrs), Vemacs_mule_charset_list))
5812 {
5813 Lisp_Object tail, safe_charsets;
5814 int max_charset_id = 0;
5815
5816 for (tail = Vemacs_mule_charset_list; CONSP (tail);
5817 tail = XCDR (tail))
5818 if (max_charset_id < XFASTINT (XCAR (tail)))
5819 max_charset_id = XFASTINT (XCAR (tail));
5820 safe_charsets = make_uninit_string (max_charset_id + 1);
5821 memset (SDATA (safe_charsets), 255, max_charset_id + 1);
5822 for (tail = Vemacs_mule_charset_list; CONSP (tail);
5823 tail = XCDR (tail))
5824 SSET (safe_charsets, XFASTINT (XCAR (tail)), 0);
5825 coding->max_charset_id = max_charset_id;
5826 coding->safe_charsets = SDATA (safe_charsets);
5827 coding->spec.emacs_mule.full_support = 1;
5828 }
5829 coding->spec.emacs_mule.cmp_status.state = COMPOSING_NO;
5830 coding->spec.emacs_mule.cmp_status.method = COMPOSITION_NO;
5831 }
5832 else if (EQ (coding_type, Qshift_jis))
5833 {
5834 coding->detector = detect_coding_sjis;
5835 coding->decoder = decode_coding_sjis;
5836 coding->encoder = encode_coding_sjis;
5837 coding->common_flags
5838 |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK);
5839 }
5840 else if (EQ (coding_type, Qbig5))
5841 {
5842 coding->detector = detect_coding_big5;
5843 coding->decoder = decode_coding_big5;
5844 coding->encoder = encode_coding_big5;
5845 coding->common_flags
5846 |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK);
5847 }
5848 else /* EQ (coding_type, Qraw_text) */
5849 {
5850 coding->detector = NULL;
5851 coding->decoder = decode_coding_raw_text;
5852 coding->encoder = encode_coding_raw_text;
5853 if (! EQ (eol_type, Qunix))
5854 {
5855 coding->common_flags |= CODING_REQUIRE_DECODING_MASK;
5856 if (! VECTORP (eol_type))
5857 coding->common_flags |= CODING_REQUIRE_ENCODING_MASK;
5858 }
5859
5860 }
5861
5862 return;
5863 }
5864
5865 /* Return a list of charsets supported by CODING. */
5866
5867 Lisp_Object
5868 coding_charset_list (struct coding_system *coding)
5869 {
5870 Lisp_Object attrs, charset_list;
5871
5872 CODING_GET_INFO (coding, attrs, charset_list);
5873 if (EQ (CODING_ATTR_TYPE (attrs), Qiso_2022))
5874 {
5875 int flags = XINT (AREF (attrs, coding_attr_iso_flags));
5876
5877 if (flags & CODING_ISO_FLAG_FULL_SUPPORT)
5878 charset_list = Viso_2022_charset_list;
5879 }
5880 else if (EQ (CODING_ATTR_TYPE (attrs), Qemacs_mule))
5881 {
5882 charset_list = Vemacs_mule_charset_list;
5883 }
5884 return charset_list;
5885 }
5886
5887
5888 /* Return a list of charsets supported by CODING-SYSTEM. */
5889
5890 Lisp_Object
5891 coding_system_charset_list (Lisp_Object coding_system)
5892 {
5893 int id;
5894 Lisp_Object attrs, charset_list;
5895
5896 CHECK_CODING_SYSTEM_GET_ID (coding_system, id);
5897 attrs = CODING_ID_ATTRS (id);
5898
5899 if (EQ (CODING_ATTR_TYPE (attrs), Qiso_2022))
5900 {
5901 int flags = XINT (AREF (attrs, coding_attr_iso_flags));
5902
5903 if (flags & CODING_ISO_FLAG_FULL_SUPPORT)
5904 charset_list = Viso_2022_charset_list;
5905 else
5906 charset_list = CODING_ATTR_CHARSET_LIST (attrs);
5907 }
5908 else if (EQ (CODING_ATTR_TYPE (attrs), Qemacs_mule))
5909 {
5910 charset_list = Vemacs_mule_charset_list;
5911 }
5912 else
5913 {
5914 charset_list = CODING_ATTR_CHARSET_LIST (attrs);
5915 }
5916 return charset_list;
5917 }
5918
5919
5920 /* Return raw-text or one of its subsidiaries that has the same
5921 eol_type as CODING-SYSTEM. */
5922
5923 Lisp_Object
5924 raw_text_coding_system (Lisp_Object coding_system)
5925 {
5926 Lisp_Object spec, attrs;
5927 Lisp_Object eol_type, raw_text_eol_type;
5928
5929 if (NILP (coding_system))
5930 return Qraw_text;
5931 spec = CODING_SYSTEM_SPEC (coding_system);
5932 attrs = AREF (spec, 0);
5933
5934 if (EQ (CODING_ATTR_TYPE (attrs), Qraw_text))
5935 return coding_system;
5936
5937 eol_type = AREF (spec, 2);
5938 if (VECTORP (eol_type))
5939 return Qraw_text;
5940 spec = CODING_SYSTEM_SPEC (Qraw_text);
5941 raw_text_eol_type = AREF (spec, 2);
5942 return (EQ (eol_type, Qunix) ? AREF (raw_text_eol_type, 0)
5943 : EQ (eol_type, Qdos) ? AREF (raw_text_eol_type, 1)
5944 : AREF (raw_text_eol_type, 2));
5945 }
5946
5947
5948 /* If CODING_SYSTEM doesn't specify end-of-line format, return one of
5949 the subsidiary that has the same eol-spec as PARENT (if it is not
5950 nil and specifies end-of-line format) or the system's setting
5951 (system_eol_type). */
5952
5953 Lisp_Object
5954 coding_inherit_eol_type (Lisp_Object coding_system, Lisp_Object parent)
5955 {
5956 Lisp_Object spec, eol_type;
5957
5958 if (NILP (coding_system))
5959 coding_system = Qraw_text;
5960 spec = CODING_SYSTEM_SPEC (coding_system);
5961 eol_type = AREF (spec, 2);
5962 if (VECTORP (eol_type))
5963 {
5964 Lisp_Object parent_eol_type;
5965
5966 if (! NILP (parent))
5967 {
5968 Lisp_Object parent_spec;
5969
5970 parent_spec = CODING_SYSTEM_SPEC (parent);
5971 parent_eol_type = AREF (parent_spec, 2);
5972 if (VECTORP (parent_eol_type))
5973 parent_eol_type = system_eol_type;
5974 }
5975 else
5976 parent_eol_type = system_eol_type;
5977 if (EQ (parent_eol_type, Qunix))
5978 coding_system = AREF (eol_type, 0);
5979 else if (EQ (parent_eol_type, Qdos))
5980 coding_system = AREF (eol_type, 1);
5981 else if (EQ (parent_eol_type, Qmac))
5982 coding_system = AREF (eol_type, 2);
5983 }
5984 return coding_system;
5985 }
5986
5987
5988 /* Check if text-conversion and eol-conversion of CODING_SYSTEM are
5989 decided for writing to a process. If not, complement them, and
5990 return a new coding system. */
5991
5992 Lisp_Object
5993 complement_process_encoding_system (Lisp_Object coding_system)
5994 {
5995 Lisp_Object coding_base = Qnil, eol_base = Qnil;
5996 Lisp_Object spec, attrs;
5997 int i;
5998
5999 for (i = 0; i < 3; i++)
6000 {
6001 if (i == 1)
6002 coding_system = CDR_SAFE (Vdefault_process_coding_system);
6003 else if (i == 2)
6004 coding_system = preferred_coding_system ();
6005 spec = CODING_SYSTEM_SPEC (coding_system);
6006 if (NILP (spec))
6007 continue;
6008 attrs = AREF (spec, 0);
6009 if (NILP (coding_base) && ! EQ (CODING_ATTR_TYPE (attrs), Qundecided))
6010 coding_base = CODING_ATTR_BASE_NAME (attrs);
6011 if (NILP (eol_base) && ! VECTORP (AREF (spec, 2)))
6012 eol_base = coding_system;
6013 if (! NILP (coding_base) && ! NILP (eol_base))
6014 break;
6015 }
6016
6017 if (i > 0)
6018 /* The original CODING_SYSTEM didn't specify text-conversion or
6019 eol-conversion. Be sure that we return a fully complemented
6020 coding system. */
6021 coding_system = coding_inherit_eol_type (coding_base, eol_base);
6022 return coding_system;
6023 }
6024
6025
6026 /* Emacs has a mechanism to automatically detect a coding system if it
6027 is one of Emacs' internal format, ISO2022, SJIS, and BIG5. But,
6028 it's impossible to distinguish some coding systems accurately
6029 because they use the same range of codes. So, at first, coding
6030 systems are categorized into 7, those are:
6031
6032 o coding-category-emacs-mule
6033
6034 The category for a coding system which has the same code range
6035 as Emacs' internal format. Assigned the coding-system (Lisp
6036 symbol) `emacs-mule' by default.
6037
6038 o coding-category-sjis
6039
6040 The category for a coding system which has the same code range
6041 as SJIS. Assigned the coding-system (Lisp
6042 symbol) `japanese-shift-jis' by default.
6043
6044 o coding-category-iso-7
6045
6046 The category for a coding system which has the same code range
6047 as ISO2022 of 7-bit environment. This doesn't use any locking
6048 shift and single shift functions. This can encode/decode all
6049 charsets. Assigned the coding-system (Lisp symbol)
6050 `iso-2022-7bit' by default.
6051
6052 o coding-category-iso-7-tight
6053
6054 Same as coding-category-iso-7 except that this can
6055 encode/decode only the specified charsets.
6056
6057 o coding-category-iso-8-1
6058
6059 The category for a coding system which has the same code range
6060 as ISO2022 of 8-bit environment and graphic plane 1 used only
6061 for DIMENSION1 charset. This doesn't use any locking shift
6062 and single shift functions. Assigned the coding-system (Lisp
6063 symbol) `iso-latin-1' by default.
6064
6065 o coding-category-iso-8-2
6066
6067 The category for a coding system which has the same code range
6068 as ISO2022 of 8-bit environment and graphic plane 1 used only
6069 for DIMENSION2 charset. This doesn't use any locking shift
6070 and single shift functions. Assigned the coding-system (Lisp
6071 symbol) `japanese-iso-8bit' by default.
6072
6073 o coding-category-iso-7-else
6074
6075 The category for a coding system which has the same code range
6076 as ISO2022 of 7-bit environment but uses locking shift or
6077 single shift functions. Assigned the coding-system (Lisp
6078 symbol) `iso-2022-7bit-lock' by default.
6079
6080 o coding-category-iso-8-else
6081
6082 The category for a coding system which has the same code range
6083 as ISO2022 of 8-bit environment but uses locking shift or
6084 single shift functions. Assigned the coding-system (Lisp
6085 symbol) `iso-2022-8bit-ss2' by default.
6086
6087 o coding-category-big5
6088
6089 The category for a coding system which has the same code range
6090 as BIG5. Assigned the coding-system (Lisp symbol)
6091 `cn-big5' by default.
6092
6093 o coding-category-utf-8
6094
6095 The category for a coding system which has the same code range
6096 as UTF-8 (cf. RFC3629). Assigned the coding-system (Lisp
6097 symbol) `utf-8' by default.
6098
6099 o coding-category-utf-16-be
6100
6101 The category for a coding system in which a text has an
6102 Unicode signature (cf. Unicode Standard) in the order of BIG
6103 endian at the head. Assigned the coding-system (Lisp symbol)
6104 `utf-16-be' by default.
6105
6106 o coding-category-utf-16-le
6107
6108 The category for a coding system in which a text has an
6109 Unicode signature (cf. Unicode Standard) in the order of
6110 LITTLE endian at the head. Assigned the coding-system (Lisp
6111 symbol) `utf-16-le' by default.
6112
6113 o coding-category-ccl
6114
6115 The category for a coding system of which encoder/decoder is
6116 written in CCL programs. The default value is nil, i.e., no
6117 coding system is assigned.
6118
6119 o coding-category-binary
6120
6121 The category for a coding system not categorized in any of the
6122 above. Assigned the coding-system (Lisp symbol)
6123 `no-conversion' by default.
6124
6125 Each of them is a Lisp symbol and the value is an actual
6126 `coding-system's (this is also a Lisp symbol) assigned by a user.
6127 What Emacs does actually is to detect a category of coding system.
6128 Then, it uses a `coding-system' assigned to it. If Emacs can't
6129 decide only one possible category, it selects a category of the
6130 highest priority. Priorities of categories are also specified by a
6131 user in a Lisp variable `coding-category-list'.
6132
6133 */
6134
6135 #define EOL_SEEN_NONE 0
6136 #define EOL_SEEN_LF 1
6137 #define EOL_SEEN_CR 2
6138 #define EOL_SEEN_CRLF 4
6139
6140 /* Detect how end-of-line of a text of length SRC_BYTES pointed by
6141 SOURCE is encoded. If CATEGORY is one of
6142 coding_category_utf_16_XXXX, assume that CR and LF are encoded by
6143 two-byte, else they are encoded by one-byte.
6144
6145 Return one of EOL_SEEN_XXX. */
6146
6147 #define MAX_EOL_CHECK_COUNT 3
6148
6149 static int
6150 detect_eol (const unsigned char *source, EMACS_INT src_bytes,
6151 enum coding_category category)
6152 {
6153 const unsigned char *src = source, *src_end = src + src_bytes;
6154 unsigned char c;
6155 int total = 0;
6156 int eol_seen = EOL_SEEN_NONE;
6157
6158 if ((1 << category) & CATEGORY_MASK_UTF_16)
6159 {
6160 int msb, lsb;
6161
6162 msb = category == (coding_category_utf_16_le
6163 | coding_category_utf_16_le_nosig);
6164 lsb = 1 - msb;
6165
6166 while (src + 1 < src_end)
6167 {
6168 c = src[lsb];
6169 if (src[msb] == 0 && (c == '\n' || c == '\r'))
6170 {
6171 int this_eol;
6172
6173 if (c == '\n')
6174 this_eol = EOL_SEEN_LF;
6175 else if (src + 3 >= src_end
6176 || src[msb + 2] != 0
6177 || src[lsb + 2] != '\n')
6178 this_eol = EOL_SEEN_CR;
6179 else
6180 {
6181 this_eol = EOL_SEEN_CRLF;
6182 src += 2;
6183 }
6184
6185 if (eol_seen == EOL_SEEN_NONE)
6186 /* This is the first end-of-line. */
6187 eol_seen = this_eol;
6188 else if (eol_seen != this_eol)
6189 {
6190 /* The found type is different from what found before.
6191 Allow for stray ^M characters in DOS EOL files. */
6192 if ((eol_seen == EOL_SEEN_CR && this_eol == EOL_SEEN_CRLF)
6193 || (eol_seen == EOL_SEEN_CRLF
6194 && this_eol == EOL_SEEN_CR))
6195 eol_seen = EOL_SEEN_CRLF;
6196 else
6197 {
6198 eol_seen = EOL_SEEN_LF;
6199 break;
6200 }
6201 }
6202 if (++total == MAX_EOL_CHECK_COUNT)
6203 break;
6204 }
6205 src += 2;
6206 }
6207 }
6208 else
6209 while (src < src_end)
6210 {
6211 c = *src++;
6212 if (c == '\n' || c == '\r')
6213 {
6214 int this_eol;
6215
6216 if (c == '\n')
6217 this_eol = EOL_SEEN_LF;
6218 else if (src >= src_end || *src != '\n')
6219 this_eol = EOL_SEEN_CR;
6220 else
6221 this_eol = EOL_SEEN_CRLF, src++;
6222
6223 if (eol_seen == EOL_SEEN_NONE)
6224 /* This is the first end-of-line. */
6225 eol_seen = this_eol;
6226 else if (eol_seen != this_eol)
6227 {
6228 /* The found type is different from what found before.
6229 Allow for stray ^M characters in DOS EOL files. */
6230 if ((eol_seen == EOL_SEEN_CR && this_eol == EOL_SEEN_CRLF)
6231 || (eol_seen == EOL_SEEN_CRLF && this_eol == EOL_SEEN_CR))
6232 eol_seen = EOL_SEEN_CRLF;
6233 else
6234 {
6235 eol_seen = EOL_SEEN_LF;
6236 break;
6237 }
6238 }
6239 if (++total == MAX_EOL_CHECK_COUNT)
6240 break;
6241 }
6242 }
6243 return eol_seen;
6244 }
6245
6246
6247 static Lisp_Object
6248 adjust_coding_eol_type (struct coding_system *coding, int eol_seen)
6249 {
6250 Lisp_Object eol_type;
6251
6252 eol_type = CODING_ID_EOL_TYPE (coding->id);
6253 if (eol_seen & EOL_SEEN_LF)
6254 {
6255 coding->id = CODING_SYSTEM_ID (AREF (eol_type, 0));
6256 eol_type = Qunix;
6257 }
6258 else if (eol_seen & EOL_SEEN_CRLF)
6259 {
6260 coding->id = CODING_SYSTEM_ID (AREF (eol_type, 1));
6261 eol_type = Qdos;
6262 }
6263 else if (eol_seen & EOL_SEEN_CR)
6264 {
6265 coding->id = CODING_SYSTEM_ID (AREF (eol_type, 2));
6266 eol_type = Qmac;
6267 }
6268 return eol_type;
6269 }
6270
6271 /* Detect how a text specified in CODING is encoded. If a coding
6272 system is detected, update fields of CODING by the detected coding
6273 system. */
6274
6275 void
6276 detect_coding (struct coding_system *coding)
6277 {
6278 const unsigned char *src, *src_end;
6279 int saved_mode = coding->mode;
6280
6281 coding->consumed = coding->consumed_char = 0;
6282 coding->produced = coding->produced_char = 0;
6283 coding_set_source (coding);
6284
6285 src_end = coding->source + coding->src_bytes;
6286 coding->head_ascii = 0;
6287
6288 /* If we have not yet decided the text encoding type, detect it
6289 now. */
6290 if (EQ (CODING_ATTR_TYPE (CODING_ID_ATTRS (coding->id)), Qundecided))
6291 {
6292 int c, i;
6293 struct coding_detection_info detect_info;
6294 int null_byte_found = 0, eight_bit_found = 0;
6295
6296 detect_info.checked = detect_info.found = detect_info.rejected = 0;
6297 for (src = coding->source; src < src_end; src++)
6298 {
6299 c = *src;
6300 if (c & 0x80)
6301 {
6302 eight_bit_found = 1;
6303 if (null_byte_found)
6304 break;
6305 }
6306 else if (c < 0x20)
6307 {
6308 if ((c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO)
6309 && ! inhibit_iso_escape_detection
6310 && ! detect_info.checked)
6311 {
6312 if (detect_coding_iso_2022 (coding, &detect_info))
6313 {
6314 /* We have scanned the whole data. */
6315 if (! (detect_info.rejected & CATEGORY_MASK_ISO_7_ELSE))
6316 {
6317 /* We didn't find an 8-bit code. We may
6318 have found a null-byte, but it's very
6319 rare that a binary file conforms to
6320 ISO-2022. */
6321 src = src_end;
6322 coding->head_ascii = src - coding->source;
6323 }
6324 detect_info.rejected |= ~CATEGORY_MASK_ISO_ESCAPE;
6325 break;
6326 }
6327 }
6328 else if (! c && !inhibit_null_byte_detection)
6329 {
6330 null_byte_found = 1;
6331 if (eight_bit_found)
6332 break;
6333 }
6334 if (! eight_bit_found)
6335 coding->head_ascii++;
6336 }
6337 else if (! eight_bit_found)
6338 coding->head_ascii++;
6339 }
6340
6341 if (null_byte_found || eight_bit_found
6342 || coding->head_ascii < coding->src_bytes
6343 || detect_info.found)
6344 {
6345 enum coding_category category;
6346 struct coding_system *this;
6347
6348 if (coding->head_ascii == coding->src_bytes)
6349 /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */
6350 for (i = 0; i < coding_category_raw_text; i++)
6351 {
6352 category = coding_priorities[i];
6353 this = coding_categories + category;
6354 if (detect_info.found & (1 << category))
6355 break;
6356 }
6357 else
6358 {
6359 if (null_byte_found)
6360 {
6361 detect_info.checked |= ~CATEGORY_MASK_UTF_16;
6362 detect_info.rejected |= ~CATEGORY_MASK_UTF_16;
6363 }
6364 for (i = 0; i < coding_category_raw_text; i++)
6365 {
6366 category = coding_priorities[i];
6367 this = coding_categories + category;
6368 if (this->id < 0)
6369 {
6370 /* No coding system of this category is defined. */
6371 detect_info.rejected |= (1 << category);
6372 }
6373 else if (category >= coding_category_raw_text)
6374 continue;
6375 else if (detect_info.checked & (1 << category))
6376 {
6377 if (detect_info.found & (1 << category))
6378 break;
6379 }
6380 else if ((*(this->detector)) (coding, &detect_info)
6381 && detect_info.found & (1 << category))
6382 {
6383 if (category == coding_category_utf_16_auto)
6384 {
6385 if (detect_info.found & CATEGORY_MASK_UTF_16_LE)
6386 category = coding_category_utf_16_le;
6387 else
6388 category = coding_category_utf_16_be;
6389 }
6390 break;
6391 }
6392 }
6393 }
6394
6395 if (i < coding_category_raw_text)
6396 setup_coding_system (CODING_ID_NAME (this->id), coding);
6397 else if (null_byte_found)
6398 setup_coding_system (Qno_conversion, coding);
6399 else if ((detect_info.rejected & CATEGORY_MASK_ANY)
6400 == CATEGORY_MASK_ANY)
6401 setup_coding_system (Qraw_text, coding);
6402 else if (detect_info.rejected)
6403 for (i = 0; i < coding_category_raw_text; i++)
6404 if (! (detect_info.rejected & (1 << coding_priorities[i])))
6405 {
6406 this = coding_categories + coding_priorities[i];
6407 setup_coding_system (CODING_ID_NAME (this->id), coding);
6408 break;
6409 }
6410 }
6411 }
6412 else if (XINT (CODING_ATTR_CATEGORY (CODING_ID_ATTRS (coding->id)))
6413 == coding_category_utf_8_auto)
6414 {
6415 Lisp_Object coding_systems;
6416 struct coding_detection_info detect_info;
6417
6418 coding_systems
6419 = AREF (CODING_ID_ATTRS (coding->id), coding_attr_utf_bom);
6420 detect_info.found = detect_info.rejected = 0;
6421 coding->head_ascii = 0;
6422 if (CONSP (coding_systems)
6423 && detect_coding_utf_8 (coding, &detect_info))
6424 {
6425 if (detect_info.found & CATEGORY_MASK_UTF_8_SIG)
6426 setup_coding_system (XCAR (coding_systems), coding);
6427 else
6428 setup_coding_system (XCDR (coding_systems), coding);
6429 }
6430 }
6431 else if (XINT (CODING_ATTR_CATEGORY (CODING_ID_ATTRS (coding->id)))
6432 == coding_category_utf_16_auto)
6433 {
6434 Lisp_Object coding_systems;
6435 struct coding_detection_info detect_info;
6436
6437 coding_systems
6438 = AREF (CODING_ID_ATTRS (coding->id), coding_attr_utf_bom);
6439 detect_info.found = detect_info.rejected = 0;
6440 coding->head_ascii = 0;
6441 if (CONSP (coding_systems)
6442 && detect_coding_utf_16 (coding, &detect_info))
6443 {
6444 if (detect_info.found & CATEGORY_MASK_UTF_16_LE)
6445 setup_coding_system (XCAR (coding_systems), coding);
6446 else if (detect_info.found & CATEGORY_MASK_UTF_16_BE)
6447 setup_coding_system (XCDR (coding_systems), coding);
6448 }
6449 }
6450 coding->mode = saved_mode;
6451 }
6452
6453
6454 static void
6455 decode_eol (struct coding_system *coding)
6456 {
6457 Lisp_Object eol_type;
6458 unsigned char *p, *pbeg, *pend;
6459
6460 eol_type = CODING_ID_EOL_TYPE (coding->id);
6461 if (EQ (eol_type, Qunix) || inhibit_eol_conversion)
6462 return;
6463
6464 if (NILP (coding->dst_object))
6465 pbeg = coding->destination;
6466 else
6467 pbeg = BYTE_POS_ADDR (coding->dst_pos_byte);
6468 pend = pbeg + coding->produced;
6469
6470 if (VECTORP (eol_type))
6471 {
6472 int eol_seen = EOL_SEEN_NONE;
6473
6474 for (p = pbeg; p < pend; p++)
6475 {
6476 if (*p == '\n')
6477 eol_seen |= EOL_SEEN_LF;
6478 else if (*p == '\r')
6479 {
6480 if (p + 1 < pend && *(p + 1) == '\n')
6481 {
6482 eol_seen |= EOL_SEEN_CRLF;
6483 p++;
6484 }
6485 else
6486 eol_seen |= EOL_SEEN_CR;
6487 }
6488 }
6489 /* Handle DOS-style EOLs in a file with stray ^M characters. */
6490 if ((eol_seen & EOL_SEEN_CRLF) != 0
6491 && (eol_seen & EOL_SEEN_CR) != 0
6492 && (eol_seen & EOL_SEEN_LF) == 0)
6493 eol_seen = EOL_SEEN_CRLF;
6494 else if (eol_seen != EOL_SEEN_NONE
6495 && eol_seen != EOL_SEEN_LF
6496 && eol_seen != EOL_SEEN_CRLF
6497 && eol_seen != EOL_SEEN_CR)
6498 eol_seen = EOL_SEEN_LF;
6499 if (eol_seen != EOL_SEEN_NONE)
6500 eol_type = adjust_coding_eol_type (coding, eol_seen);
6501 }
6502
6503 if (EQ (eol_type, Qmac))
6504 {
6505 for (p = pbeg; p < pend; p++)
6506 if (*p == '\r')
6507 *p = '\n';
6508 }
6509 else if (EQ (eol_type, Qdos))
6510 {
6511 int n = 0;
6512
6513 if (NILP (coding->dst_object))
6514 {
6515 /* Start deleting '\r' from the tail to minimize the memory
6516 movement. */
6517 for (p = pend - 2; p >= pbeg; p--)
6518 if (*p == '\r')
6519 {
6520 memmove (p, p + 1, pend-- - p - 1);
6521 n++;
6522 }
6523 }
6524 else
6525 {
6526 int pos_byte = coding->dst_pos_byte;
6527 int pos = coding->dst_pos;
6528 int pos_end = pos + coding->produced_char - 1;
6529
6530 while (pos < pos_end)
6531 {
6532 p = BYTE_POS_ADDR (pos_byte);
6533 if (*p == '\r' && p[1] == '\n')
6534 {
6535 del_range_2 (pos, pos_byte, pos + 1, pos_byte + 1, 0);
6536 n++;
6537 pos_end--;
6538 }
6539 pos++;
6540 if (coding->dst_multibyte)
6541 pos_byte += BYTES_BY_CHAR_HEAD (*p);
6542 else
6543 pos_byte++;
6544 }
6545 }
6546 coding->produced -= n;
6547 coding->produced_char -= n;
6548 }
6549 }
6550
6551
6552 /* Return a translation table (or list of them) from coding system
6553 attribute vector ATTRS for encoding (ENCODEP is nonzero) or
6554 decoding (ENCODEP is zero). */
6555
6556 static Lisp_Object
6557 get_translation_table (Lisp_Object attrs, int encodep, int *max_lookup)
6558 {
6559 Lisp_Object standard, translation_table;
6560 Lisp_Object val;
6561
6562 if (NILP (Venable_character_translation))
6563 {
6564 if (max_lookup)
6565 *max_lookup = 0;
6566 return Qnil;
6567 }
6568 if (encodep)
6569 translation_table = CODING_ATTR_ENCODE_TBL (attrs),
6570 standard = Vstandard_translation_table_for_encode;
6571 else
6572 translation_table = CODING_ATTR_DECODE_TBL (attrs),
6573 standard = Vstandard_translation_table_for_decode;
6574 if (NILP (translation_table))
6575 translation_table = standard;
6576 else
6577 {
6578 if (SYMBOLP (translation_table))
6579 translation_table = Fget (translation_table, Qtranslation_table);
6580 else if (CONSP (translation_table))
6581 {
6582 translation_table = Fcopy_sequence (translation_table);
6583 for (val = translation_table; CONSP (val); val = XCDR (val))
6584 if (SYMBOLP (XCAR (val)))
6585 XSETCAR (val, Fget (XCAR (val), Qtranslation_table));
6586 }
6587 if (CHAR_TABLE_P (standard))
6588 {
6589 if (CONSP (translation_table))
6590 translation_table = nconc2 (translation_table,
6591 Fcons (standard, Qnil));
6592 else
6593 translation_table = Fcons (translation_table,
6594 Fcons (standard, Qnil));
6595 }
6596 }
6597
6598 if (max_lookup)
6599 {
6600 *max_lookup = 1;
6601 if (CHAR_TABLE_P (translation_table)
6602 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (translation_table)) > 1)
6603 {
6604 val = XCHAR_TABLE (translation_table)->extras[1];
6605 if (NATNUMP (val) && *max_lookup < XFASTINT (val))
6606 *max_lookup = XFASTINT (val);
6607 }
6608 else if (CONSP (translation_table))
6609 {
6610 Lisp_Object tail, val;
6611
6612 for (tail = translation_table; CONSP (tail); tail = XCDR (tail))
6613 if (CHAR_TABLE_P (XCAR (tail))
6614 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (XCAR (tail))) > 1)
6615 {
6616 val = XCHAR_TABLE (XCAR (tail))->extras[1];
6617 if (NATNUMP (val) && *max_lookup < XFASTINT (val))
6618 *max_lookup = XFASTINT (val);
6619 }
6620 }
6621 }
6622 return translation_table;
6623 }
6624
6625 #define LOOKUP_TRANSLATION_TABLE(table, c, trans) \
6626 do { \
6627 trans = Qnil; \
6628 if (CHAR_TABLE_P (table)) \
6629 { \
6630 trans = CHAR_TABLE_REF (table, c); \
6631 if (CHARACTERP (trans)) \
6632 c = XFASTINT (trans), trans = Qnil; \
6633 } \
6634 else if (CONSP (table)) \
6635 { \
6636 Lisp_Object tail; \
6637 \
6638 for (tail = table; CONSP (tail); tail = XCDR (tail)) \
6639 if (CHAR_TABLE_P (XCAR (tail))) \
6640 { \
6641 trans = CHAR_TABLE_REF (XCAR (tail), c); \
6642 if (CHARACTERP (trans)) \
6643 c = XFASTINT (trans), trans = Qnil; \
6644 else if (! NILP (trans)) \
6645 break; \
6646 } \
6647 } \
6648 } while (0)
6649
6650
6651 /* Return a translation of character(s) at BUF according to TRANS.
6652 TRANS is TO-CHAR or ((FROM . TO) ...) where
6653 FROM = [FROM-CHAR ...], TO is TO-CHAR or [TO-CHAR ...].
6654 The return value is TO-CHAR or ([FROM-CHAR ...] . TO) if a
6655 translation is found, and Qnil if not found..
6656 If BUF is too short to lookup characters in FROM, return Qt. */
6657
6658 static Lisp_Object
6659 get_translation (Lisp_Object trans, int *buf, int *buf_end)
6660 {
6661
6662 if (INTEGERP (trans))
6663 return trans;
6664 for (; CONSP (trans); trans = XCDR (trans))
6665 {
6666 Lisp_Object val = XCAR (trans);
6667 Lisp_Object from = XCAR (val);
6668 int len = ASIZE (from);
6669 int i;
6670
6671 for (i = 0; i < len; i++)
6672 {
6673 if (buf + i == buf_end)
6674 return Qt;
6675 if (XINT (AREF (from, i)) != buf[i])
6676 break;
6677 }
6678 if (i == len)
6679 return val;
6680 }
6681 return Qnil;
6682 }
6683
6684
6685 static int
6686 produce_chars (struct coding_system *coding, Lisp_Object translation_table,
6687 int last_block)
6688 {
6689 unsigned char *dst = coding->destination + coding->produced;
6690 unsigned char *dst_end = coding->destination + coding->dst_bytes;
6691 EMACS_INT produced;
6692 EMACS_INT produced_chars = 0;
6693 int carryover = 0;
6694
6695 if (! coding->chars_at_source)
6696 {
6697 /* Source characters are in coding->charbuf. */
6698 int *buf = coding->charbuf;
6699 int *buf_end = buf + coding->charbuf_used;
6700
6701 if (EQ (coding->src_object, coding->dst_object))
6702 {
6703 coding_set_source (coding);
6704 dst_end = ((unsigned char *) coding->source) + coding->consumed;
6705 }
6706
6707 while (buf < buf_end)
6708 {
6709 int c = *buf, i;
6710
6711 if (c >= 0)
6712 {
6713 int from_nchars = 1, to_nchars = 1;
6714 Lisp_Object trans = Qnil;
6715
6716 LOOKUP_TRANSLATION_TABLE (translation_table, c, trans);
6717 if (! NILP (trans))
6718 {
6719 trans = get_translation (trans, buf, buf_end);
6720 if (INTEGERP (trans))
6721 c = XINT (trans);
6722 else if (CONSP (trans))
6723 {
6724 from_nchars = ASIZE (XCAR (trans));
6725 trans = XCDR (trans);
6726 if (INTEGERP (trans))
6727 c = XINT (trans);
6728 else
6729 {
6730 to_nchars = ASIZE (trans);
6731 c = XINT (AREF (trans, 0));
6732 }
6733 }
6734 else if (EQ (trans, Qt) && ! last_block)
6735 break;
6736 }
6737
6738 if (dst + MAX_MULTIBYTE_LENGTH * to_nchars > dst_end)
6739 {
6740 dst = alloc_destination (coding,
6741 buf_end - buf
6742 + MAX_MULTIBYTE_LENGTH * to_nchars,
6743 dst);
6744 if (EQ (coding->src_object, coding->dst_object))
6745 {
6746 coding_set_source (coding);
6747 dst_end = (((unsigned char *) coding->source)
6748 + coding->consumed);
6749 }
6750 else
6751 dst_end = coding->destination + coding->dst_bytes;
6752 }
6753
6754 for (i = 0; i < to_nchars; i++)
6755 {
6756 if (i > 0)
6757 c = XINT (AREF (trans, i));
6758 if (coding->dst_multibyte
6759 || ! CHAR_BYTE8_P (c))
6760 CHAR_STRING_ADVANCE_NO_UNIFY (c, dst);
6761 else
6762 *dst++ = CHAR_TO_BYTE8 (c);
6763 }
6764 produced_chars += to_nchars;
6765 buf += from_nchars;
6766 }
6767 else
6768 /* This is an annotation datum. (-C) is the length. */
6769 buf += -c;
6770 }
6771 carryover = buf_end - buf;
6772 }
6773 else
6774 {
6775 /* Source characters are at coding->source. */
6776 const unsigned char *src = coding->source;
6777 const unsigned char *src_end = src + coding->consumed;
6778
6779 if (EQ (coding->dst_object, coding->src_object))
6780 dst_end = (unsigned char *) src;
6781 if (coding->src_multibyte != coding->dst_multibyte)
6782 {
6783 if (coding->src_multibyte)
6784 {
6785 int multibytep = 1;
6786 EMACS_INT consumed_chars = 0;
6787
6788 while (1)
6789 {
6790 const unsigned char *src_base = src;
6791 int c;
6792
6793 ONE_MORE_BYTE (c);
6794 if (dst == dst_end)
6795 {
6796 if (EQ (coding->src_object, coding->dst_object))
6797 dst_end = (unsigned char *) src;
6798 if (dst == dst_end)
6799 {
6800 EMACS_INT offset = src - coding->source;
6801
6802 dst = alloc_destination (coding, src_end - src + 1,
6803 dst);
6804 dst_end = coding->destination + coding->dst_bytes;
6805 coding_set_source (coding);
6806 src = coding->source + offset;
6807 src_end = coding->source + coding->src_bytes;
6808 if (EQ (coding->src_object, coding->dst_object))
6809 dst_end = (unsigned char *) src;
6810 }
6811 }
6812 *dst++ = c;
6813 produced_chars++;
6814 }
6815 no_more_source:
6816 ;
6817 }
6818 else
6819 while (src < src_end)
6820 {
6821 int multibytep = 1;
6822 int c = *src++;
6823
6824 if (dst >= dst_end - 1)
6825 {
6826 if (EQ (coding->src_object, coding->dst_object))
6827 dst_end = (unsigned char *) src;
6828 if (dst >= dst_end - 1)
6829 {
6830 EMACS_INT offset = src - coding->source;
6831 EMACS_INT more_bytes;
6832
6833 if (EQ (coding->src_object, coding->dst_object))
6834 more_bytes = ((src_end - src) / 2) + 2;
6835 else
6836 more_bytes = src_end - src + 2;
6837 dst = alloc_destination (coding, more_bytes, dst);
6838 dst_end = coding->destination + coding->dst_bytes;
6839 coding_set_source (coding);
6840 src = coding->source + offset;
6841 src_end = coding->source + coding->src_bytes;
6842 if (EQ (coding->src_object, coding->dst_object))
6843 dst_end = (unsigned char *) src;
6844 }
6845 }
6846 EMIT_ONE_BYTE (c);
6847 }
6848 }
6849 else
6850 {
6851 if (!EQ (coding->src_object, coding->dst_object))
6852 {
6853 EMACS_INT require = coding->src_bytes - coding->dst_bytes;
6854
6855 if (require > 0)
6856 {
6857 EMACS_INT offset = src - coding->source;
6858
6859 dst = alloc_destination (coding, require, dst);
6860 coding_set_source (coding);
6861 src = coding->source + offset;
6862 src_end = coding->source + coding->src_bytes;
6863 }
6864 }
6865 produced_chars = coding->consumed_char;
6866 while (src < src_end)
6867 *dst++ = *src++;
6868 }
6869 }
6870
6871 produced = dst - (coding->destination + coding->produced);
6872 if (BUFFERP (coding->dst_object) && produced_chars > 0)
6873 insert_from_gap (produced_chars, produced);
6874 coding->produced += produced;
6875 coding->produced_char += produced_chars;
6876 return carryover;
6877 }
6878
6879 /* Compose text in CODING->object according to the annotation data at
6880 CHARBUF. CHARBUF is an array:
6881 [ -LENGTH ANNOTATION_MASK NCHARS NBYTES METHOD [ COMPONENTS... ] ]
6882 */
6883
6884 static INLINE void
6885 produce_composition (struct coding_system *coding, int *charbuf, EMACS_INT pos)
6886 {
6887 int len;
6888 EMACS_INT to;
6889 enum composition_method method;
6890 Lisp_Object components;
6891
6892 len = -charbuf[0] - MAX_ANNOTATION_LENGTH;
6893 to = pos + charbuf[2];
6894 method = (enum composition_method) (charbuf[4]);
6895
6896 if (method == COMPOSITION_RELATIVE)
6897 components = Qnil;
6898 else
6899 {
6900 Lisp_Object args[MAX_COMPOSITION_COMPONENTS * 2 - 1];
6901 int i, j;
6902
6903 if (method == COMPOSITION_WITH_RULE)
6904 len = charbuf[2] * 3 - 2;
6905 charbuf += MAX_ANNOTATION_LENGTH;
6906 /* charbuf = [ CHRA ... CHAR] or [ CHAR -2 RULE ... CHAR ] */
6907 for (i = j = 0; i < len && charbuf[i] != -1; i++, j++)
6908 {
6909 if (charbuf[i] >= 0)
6910 args[j] = make_number (charbuf[i]);
6911 else
6912 {
6913 i++;
6914 args[j] = make_number (charbuf[i] % 0x100);
6915 }
6916 }
6917 components = (i == j ? Fstring (j, args) : Fvector (j, args));
6918 }
6919 compose_text (pos, to, components, Qnil, coding->dst_object);
6920 }
6921
6922
6923 /* Put `charset' property on text in CODING->object according to
6924 the annotation data at CHARBUF. CHARBUF is an array:
6925 [ -LENGTH ANNOTATION_MASK NCHARS CHARSET-ID ]
6926 */
6927
6928 static INLINE void
6929 produce_charset (struct coding_system *coding, int *charbuf, EMACS_INT pos)
6930 {
6931 EMACS_INT from = pos - charbuf[2];
6932 struct charset *charset = CHARSET_FROM_ID (charbuf[3]);
6933
6934 Fput_text_property (make_number (from), make_number (pos),
6935 Qcharset, CHARSET_NAME (charset),
6936 coding->dst_object);
6937 }
6938
6939
6940 #define CHARBUF_SIZE 0x4000
6941
6942 #define ALLOC_CONVERSION_WORK_AREA(coding) \
6943 do { \
6944 int size = CHARBUF_SIZE; \
6945 \
6946 coding->charbuf = NULL; \
6947 while (size > 1024) \
6948 { \
6949 coding->charbuf = (int *) alloca (sizeof (int) * size); \
6950 if (coding->charbuf) \
6951 break; \
6952 size >>= 1; \
6953 } \
6954 if (! coding->charbuf) \
6955 { \
6956 record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_MEM); \
6957 return coding->result; \
6958 } \
6959 coding->charbuf_size = size; \
6960 } while (0)
6961
6962
6963 static void
6964 produce_annotation (struct coding_system *coding, EMACS_INT pos)
6965 {
6966 int *charbuf = coding->charbuf;
6967 int *charbuf_end = charbuf + coding->charbuf_used;
6968
6969 if (NILP (coding->dst_object))
6970 return;
6971
6972 while (charbuf < charbuf_end)
6973 {
6974 if (*charbuf >= 0)
6975 pos++, charbuf++;
6976 else
6977 {
6978 int len = -*charbuf;
6979
6980 if (len > 2)
6981 switch (charbuf[1])
6982 {
6983 case CODING_ANNOTATE_COMPOSITION_MASK:
6984 produce_composition (coding, charbuf, pos);
6985 break;
6986 case CODING_ANNOTATE_CHARSET_MASK:
6987 produce_charset (coding, charbuf, pos);
6988 break;
6989 }
6990 charbuf += len;
6991 }
6992 }
6993 }
6994
6995 /* Decode the data at CODING->src_object into CODING->dst_object.
6996 CODING->src_object is a buffer, a string, or nil.
6997 CODING->dst_object is a buffer.
6998
6999 If CODING->src_object is a buffer, it must be the current buffer.
7000 In this case, if CODING->src_pos is positive, it is a position of
7001 the source text in the buffer, otherwise, the source text is in the
7002 gap area of the buffer, and CODING->src_pos specifies the offset of
7003 the text from GPT (which must be the same as PT). If this is the
7004 same buffer as CODING->dst_object, CODING->src_pos must be
7005 negative.
7006
7007 If CODING->src_object is a string, CODING->src_pos is an index to
7008 that string.
7009
7010 If CODING->src_object is nil, CODING->source must already point to
7011 the non-relocatable memory area. In this case, CODING->src_pos is
7012 an offset from CODING->source.
7013
7014 The decoded data is inserted at the current point of the buffer
7015 CODING->dst_object.
7016 */
7017
7018 static int
7019 decode_coding (struct coding_system *coding)
7020 {
7021 Lisp_Object attrs;
7022 Lisp_Object undo_list;
7023 Lisp_Object translation_table;
7024 struct ccl_spec cclspec;
7025 int carryover;
7026 int i;
7027
7028 if (BUFFERP (coding->src_object)
7029 && coding->src_pos > 0
7030 && coding->src_pos < GPT
7031 && coding->src_pos + coding->src_chars > GPT)
7032 move_gap_both (coding->src_pos, coding->src_pos_byte);
7033
7034 undo_list = Qt;
7035 if (BUFFERP (coding->dst_object))
7036 {
7037 if (current_buffer != XBUFFER (coding->dst_object))
7038 set_buffer_internal (XBUFFER (coding->dst_object));
7039 if (GPT != PT)
7040 move_gap_both (PT, PT_BYTE);
7041 undo_list = B_ (current_buffer, undo_list);
7042 B_ (current_buffer, undo_list) = Qt;
7043 }
7044
7045 coding->consumed = coding->consumed_char = 0;
7046 coding->produced = coding->produced_char = 0;
7047 coding->chars_at_source = 0;
7048 record_conversion_result (coding, CODING_RESULT_SUCCESS);
7049 coding->errors = 0;
7050
7051 ALLOC_CONVERSION_WORK_AREA (coding);
7052
7053 attrs = CODING_ID_ATTRS (coding->id);
7054 translation_table = get_translation_table (attrs, 0, NULL);
7055
7056 carryover = 0;
7057 if (coding->decoder == decode_coding_ccl)
7058 {
7059 coding->spec.ccl = &cclspec;
7060 setup_ccl_program (&cclspec.ccl, CODING_CCL_DECODER (coding));
7061 }
7062 do
7063 {
7064 EMACS_INT pos = coding->dst_pos + coding->produced_char;
7065
7066 coding_set_source (coding);
7067 coding->annotated = 0;
7068 coding->charbuf_used = carryover;
7069 (*(coding->decoder)) (coding);
7070 coding_set_destination (coding);
7071 carryover = produce_chars (coding, translation_table, 0);
7072 if (coding->annotated)
7073 produce_annotation (coding, pos);
7074 for (i = 0; i < carryover; i++)
7075 coding->charbuf[i]
7076 = coding->charbuf[coding->charbuf_used - carryover + i];
7077 }
7078 while (coding->result == CODING_RESULT_INSUFFICIENT_DST
7079 || (coding->consumed < coding->src_bytes
7080 && (coding->result == CODING_RESULT_SUCCESS
7081 || coding->result == CODING_RESULT_INVALID_SRC)));
7082
7083 if (carryover > 0)
7084 {
7085 coding_set_destination (coding);
7086 coding->charbuf_used = carryover;
7087 produce_chars (coding, translation_table, 1);
7088 }
7089
7090 coding->carryover_bytes = 0;
7091 if (coding->consumed < coding->src_bytes)
7092 {
7093 int nbytes = coding->src_bytes - coding->consumed;
7094 const unsigned char *src;
7095
7096 coding_set_source (coding);
7097 coding_set_destination (coding);
7098 src = coding->source + coding->consumed;
7099
7100 if (coding->mode & CODING_MODE_LAST_BLOCK)
7101 {
7102 /* Flush out unprocessed data as binary chars. We are sure
7103 that the number of data is less than the size of
7104 coding->charbuf. */
7105 coding->charbuf_used = 0;
7106 coding->chars_at_source = 0;
7107
7108 while (nbytes-- > 0)
7109 {
7110 int c = *src++;
7111
7112 if (c & 0x80)
7113 c = BYTE8_TO_CHAR (c);
7114 coding->charbuf[coding->charbuf_used++] = c;
7115 }
7116 produce_chars (coding, Qnil, 1);
7117 }
7118 else
7119 {
7120 /* Record unprocessed bytes in coding->carryover. We are
7121 sure that the number of data is less than the size of
7122 coding->carryover. */
7123 unsigned char *p = coding->carryover;
7124
7125 if (nbytes > sizeof coding->carryover)
7126 nbytes = sizeof coding->carryover;
7127 coding->carryover_bytes = nbytes;
7128 while (nbytes-- > 0)
7129 *p++ = *src++;
7130 }
7131 coding->consumed = coding->src_bytes;
7132 }
7133
7134 if (! EQ (CODING_ID_EOL_TYPE (coding->id), Qunix)
7135 && !inhibit_eol_conversion)
7136 decode_eol (coding);
7137 if (BUFFERP (coding->dst_object))
7138 {
7139 B_ (current_buffer, undo_list) = undo_list;
7140 record_insert (coding->dst_pos, coding->produced_char);
7141 }
7142 return coding->result;
7143 }
7144
7145
7146 /* Extract an annotation datum from a composition starting at POS and
7147 ending before LIMIT of CODING->src_object (buffer or string), store
7148 the data in BUF, set *STOP to a starting position of the next
7149 composition (if any) or to LIMIT, and return the address of the
7150 next element of BUF.
7151
7152 If such an annotation is not found, set *STOP to a starting
7153 position of a composition after POS (if any) or to LIMIT, and
7154 return BUF. */
7155
7156 static INLINE int *
7157 handle_composition_annotation (EMACS_INT pos, EMACS_INT limit,
7158 struct coding_system *coding, int *buf,
7159 EMACS_INT *stop)
7160 {
7161 EMACS_INT start, end;
7162 Lisp_Object prop;
7163
7164 if (! find_composition (pos, limit, &start, &end, &prop, coding->src_object)
7165 || end > limit)
7166 *stop = limit;
7167 else if (start > pos)
7168 *stop = start;
7169 else
7170 {
7171 if (start == pos)
7172 {
7173 /* We found a composition. Store the corresponding
7174 annotation data in BUF. */
7175 int *head = buf;
7176 enum composition_method method = COMPOSITION_METHOD (prop);
7177 int nchars = COMPOSITION_LENGTH (prop);
7178
7179 ADD_COMPOSITION_DATA (buf, nchars, 0, method);
7180 if (method != COMPOSITION_RELATIVE)
7181 {
7182 Lisp_Object components;
7183 int len, i, i_byte;
7184
7185 components = COMPOSITION_COMPONENTS (prop);
7186 if (VECTORP (components))
7187 {
7188 len = XVECTOR (components)->size;
7189 for (i = 0; i < len; i++)
7190 *buf++ = XINT (AREF (components, i));
7191 }
7192 else if (STRINGP (components))
7193 {
7194 len = SCHARS (components);
7195 i = i_byte = 0;
7196 while (i < len)
7197 {
7198 FETCH_STRING_CHAR_ADVANCE (*buf, components, i, i_byte);
7199 buf++;
7200 }
7201 }
7202 else if (INTEGERP (components))
7203 {
7204 len = 1;
7205 *buf++ = XINT (components);
7206 }
7207 else if (CONSP (components))
7208 {
7209 for (len = 0; CONSP (components);
7210 len++, components = XCDR (components))
7211 *buf++ = XINT (XCAR (components));
7212 }
7213 else
7214 abort ();
7215 *head -= len;
7216 }
7217 }
7218
7219 if (find_composition (end, limit, &start, &end, &prop,
7220 coding->src_object)
7221 && end <= limit)
7222 *stop = start;
7223 else
7224 *stop = limit;
7225 }
7226 return buf;
7227 }
7228
7229
7230 /* Extract an annotation datum from a text property `charset' at POS of
7231 CODING->src_object (buffer of string), store the data in BUF, set
7232 *STOP to the position where the value of `charset' property changes
7233 (limiting by LIMIT), and return the address of the next element of
7234 BUF.
7235
7236 If the property value is nil, set *STOP to the position where the
7237 property value is non-nil (limiting by LIMIT), and return BUF. */
7238
7239 static INLINE int *
7240 handle_charset_annotation (EMACS_INT pos, EMACS_INT limit,
7241 struct coding_system *coding, int *buf,
7242 EMACS_INT *stop)
7243 {
7244 Lisp_Object val, next;
7245 int id;
7246
7247 val = Fget_text_property (make_number (pos), Qcharset, coding->src_object);
7248 if (! NILP (val) && CHARSETP (val))
7249 id = XINT (CHARSET_SYMBOL_ID (val));
7250 else
7251 id = -1;
7252 ADD_CHARSET_DATA (buf, 0, id);
7253 next = Fnext_single_property_change (make_number (pos), Qcharset,
7254 coding->src_object,
7255 make_number (limit));
7256 *stop = XINT (next);
7257 return buf;
7258 }
7259
7260
7261 static void
7262 consume_chars (struct coding_system *coding, Lisp_Object translation_table,
7263 int max_lookup)
7264 {
7265 int *buf = coding->charbuf;
7266 int *buf_end = coding->charbuf + coding->charbuf_size;
7267 const unsigned char *src = coding->source + coding->consumed;
7268 const unsigned char *src_end = coding->source + coding->src_bytes;
7269 EMACS_INT pos = coding->src_pos + coding->consumed_char;
7270 EMACS_INT end_pos = coding->src_pos + coding->src_chars;
7271 int multibytep = coding->src_multibyte;
7272 Lisp_Object eol_type;
7273 int c;
7274 EMACS_INT stop, stop_composition, stop_charset;
7275 int *lookup_buf = NULL;
7276
7277 if (! NILP (translation_table))
7278 lookup_buf = alloca (sizeof (int) * max_lookup);
7279
7280 eol_type = inhibit_eol_conversion ? Qunix : CODING_ID_EOL_TYPE (coding->id);
7281 if (VECTORP (eol_type))
7282 eol_type = Qunix;
7283
7284 /* Note: composition handling is not yet implemented. */
7285 coding->common_flags &= ~CODING_ANNOTATE_COMPOSITION_MASK;
7286
7287 if (NILP (coding->src_object))
7288 stop = stop_composition = stop_charset = end_pos;
7289 else
7290 {
7291 if (coding->common_flags & CODING_ANNOTATE_COMPOSITION_MASK)
7292 stop = stop_composition = pos;
7293 else
7294 stop = stop_composition = end_pos;
7295 if (coding->common_flags & CODING_ANNOTATE_CHARSET_MASK)
7296 stop = stop_charset = pos;
7297 else
7298 stop_charset = end_pos;
7299 }
7300
7301 /* Compensate for CRLF and conversion. */
7302 buf_end -= 1 + MAX_ANNOTATION_LENGTH;
7303 while (buf < buf_end)
7304 {
7305 Lisp_Object trans;
7306
7307 if (pos == stop)
7308 {
7309 if (pos == end_pos)
7310 break;
7311 if (pos == stop_composition)
7312 buf = handle_composition_annotation (pos, end_pos, coding,
7313 buf, &stop_composition);
7314 if (pos == stop_charset)
7315 buf = handle_charset_annotation (pos, end_pos, coding,
7316 buf, &stop_charset);
7317 stop = (stop_composition < stop_charset
7318 ? stop_composition : stop_charset);
7319 }
7320
7321 if (! multibytep)
7322 {
7323 EMACS_INT bytes;
7324
7325 if (coding->encoder == encode_coding_raw_text
7326 || coding->encoder == encode_coding_ccl)
7327 c = *src++, pos++;
7328 else if ((bytes = MULTIBYTE_LENGTH (src, src_end)) > 0)
7329 c = STRING_CHAR_ADVANCE_NO_UNIFY (src), pos += bytes;
7330 else
7331 c = BYTE8_TO_CHAR (*src), src++, pos++;
7332 }
7333 else
7334 c = STRING_CHAR_ADVANCE_NO_UNIFY (src), pos++;
7335 if ((c == '\r') && (coding->mode & CODING_MODE_SELECTIVE_DISPLAY))
7336 c = '\n';
7337 if (! EQ (eol_type, Qunix))
7338 {
7339 if (c == '\n')
7340 {
7341 if (EQ (eol_type, Qdos))
7342 *buf++ = '\r';
7343 else
7344 c = '\r';
7345 }
7346 }
7347
7348 trans = Qnil;
7349 LOOKUP_TRANSLATION_TABLE (translation_table, c, trans);
7350 if (NILP (trans))
7351 *buf++ = c;
7352 else
7353 {
7354 int from_nchars = 1, to_nchars = 1;
7355 int *lookup_buf_end;
7356 const unsigned char *p = src;
7357 int i;
7358
7359 lookup_buf[0] = c;
7360 for (i = 1; i < max_lookup && p < src_end; i++)
7361 lookup_buf[i] = STRING_CHAR_ADVANCE (p);
7362 lookup_buf_end = lookup_buf + i;
7363 trans = get_translation (trans, lookup_buf, lookup_buf_end);
7364 if (INTEGERP (trans))
7365 c = XINT (trans);
7366 else if (CONSP (trans))
7367 {
7368 from_nchars = ASIZE (XCAR (trans));
7369 trans = XCDR (trans);
7370 if (INTEGERP (trans))
7371 c = XINT (trans);
7372 else
7373 {
7374 to_nchars = ASIZE (trans);
7375 if (buf + to_nchars > buf_end)
7376 break;
7377 c = XINT (AREF (trans, 0));
7378 }
7379 }
7380 else
7381 break;
7382 *buf++ = c;
7383 for (i = 1; i < to_nchars; i++)
7384 *buf++ = XINT (AREF (trans, i));
7385 for (i = 1; i < from_nchars; i++, pos++)
7386 src += MULTIBYTE_LENGTH_NO_CHECK (src);
7387 }
7388 }
7389
7390 coding->consumed = src - coding->source;
7391 coding->consumed_char = pos - coding->src_pos;
7392 coding->charbuf_used = buf - coding->charbuf;
7393 coding->chars_at_source = 0;
7394 }
7395
7396
7397 /* Encode the text at CODING->src_object into CODING->dst_object.
7398 CODING->src_object is a buffer or a string.
7399 CODING->dst_object is a buffer or nil.
7400
7401 If CODING->src_object is a buffer, it must be the current buffer.
7402 In this case, if CODING->src_pos is positive, it is a position of
7403 the source text in the buffer, otherwise. the source text is in the
7404 gap area of the buffer, and coding->src_pos specifies the offset of
7405 the text from GPT (which must be the same as PT). If this is the
7406 same buffer as CODING->dst_object, CODING->src_pos must be
7407 negative and CODING should not have `pre-write-conversion'.
7408
7409 If CODING->src_object is a string, CODING should not have
7410 `pre-write-conversion'.
7411
7412 If CODING->dst_object is a buffer, the encoded data is inserted at
7413 the current point of that buffer.
7414
7415 If CODING->dst_object is nil, the encoded data is placed at the
7416 memory area specified by CODING->destination. */
7417
7418 static int
7419 encode_coding (struct coding_system *coding)
7420 {
7421 Lisp_Object attrs;
7422 Lisp_Object translation_table;
7423 int max_lookup;
7424 struct ccl_spec cclspec;
7425
7426 attrs = CODING_ID_ATTRS (coding->id);
7427 if (coding->encoder == encode_coding_raw_text)
7428 translation_table = Qnil, max_lookup = 0;
7429 else
7430 translation_table = get_translation_table (attrs, 1, &max_lookup);
7431
7432 if (BUFFERP (coding->dst_object))
7433 {
7434 set_buffer_internal (XBUFFER (coding->dst_object));
7435 coding->dst_multibyte
7436 = ! NILP (B_ (current_buffer, enable_multibyte_characters));
7437 }
7438
7439 coding->consumed = coding->consumed_char = 0;
7440 coding->produced = coding->produced_char = 0;
7441 record_conversion_result (coding, CODING_RESULT_SUCCESS);
7442 coding->errors = 0;
7443
7444 ALLOC_CONVERSION_WORK_AREA (coding);
7445
7446 if (coding->encoder == encode_coding_ccl)
7447 {
7448 coding->spec.ccl = &cclspec;
7449 setup_ccl_program (&cclspec.ccl, CODING_CCL_ENCODER (coding));
7450 }
7451 do {
7452 coding_set_source (coding);
7453 consume_chars (coding, translation_table, max_lookup);
7454 coding_set_destination (coding);
7455 (*(coding->encoder)) (coding);
7456 } while (coding->consumed_char < coding->src_chars);
7457
7458 if (BUFFERP (coding->dst_object) && coding->produced_char > 0)
7459 insert_from_gap (coding->produced_char, coding->produced);
7460
7461 return (coding->result);
7462 }
7463
7464
7465 /* Name (or base name) of work buffer for code conversion. */
7466 static Lisp_Object Vcode_conversion_workbuf_name;
7467
7468 /* A working buffer used by the top level conversion. Once it is
7469 created, it is never destroyed. It has the name
7470 Vcode_conversion_workbuf_name. The other working buffers are
7471 destroyed after the use is finished, and their names are modified
7472 versions of Vcode_conversion_workbuf_name. */
7473 static Lisp_Object Vcode_conversion_reused_workbuf;
7474
7475 /* 1 iff Vcode_conversion_reused_workbuf is already in use. */
7476 static int reused_workbuf_in_use;
7477
7478
7479 /* Return a working buffer of code conversion. MULTIBYTE specifies the
7480 multibyteness of returning buffer. */
7481
7482 static Lisp_Object
7483 make_conversion_work_buffer (int multibyte)
7484 {
7485 Lisp_Object name, workbuf;
7486 struct buffer *current;
7487
7488 if (reused_workbuf_in_use++)
7489 {
7490 name = Fgenerate_new_buffer_name (Vcode_conversion_workbuf_name, Qnil);
7491 workbuf = Fget_buffer_create (name);
7492 }
7493 else
7494 {
7495 if (NILP (Fbuffer_live_p (Vcode_conversion_reused_workbuf)))
7496 Vcode_conversion_reused_workbuf
7497 = Fget_buffer_create (Vcode_conversion_workbuf_name);
7498 workbuf = Vcode_conversion_reused_workbuf;
7499 }
7500 current = current_buffer;
7501 set_buffer_internal (XBUFFER (workbuf));
7502 /* We can't allow modification hooks to run in the work buffer. For
7503 instance, directory_files_internal assumes that file decoding
7504 doesn't compile new regexps. */
7505 Fset (Fmake_local_variable (Qinhibit_modification_hooks), Qt);
7506 Ferase_buffer ();
7507 B_ (current_buffer, undo_list) = Qt;
7508 B_ (current_buffer, enable_multibyte_characters) = multibyte ? Qt : Qnil;
7509 set_buffer_internal (current);
7510 return workbuf;
7511 }
7512
7513
7514 static Lisp_Object
7515 code_conversion_restore (Lisp_Object arg)
7516 {
7517 Lisp_Object current, workbuf;
7518 struct gcpro gcpro1;
7519
7520 GCPRO1 (arg);
7521 current = XCAR (arg);
7522 workbuf = XCDR (arg);
7523 if (! NILP (workbuf))
7524 {
7525 if (EQ (workbuf, Vcode_conversion_reused_workbuf))
7526 reused_workbuf_in_use = 0;
7527 else if (! NILP (Fbuffer_live_p (workbuf)))
7528 Fkill_buffer (workbuf);
7529 }
7530 set_buffer_internal (XBUFFER (current));
7531 UNGCPRO;
7532 return Qnil;
7533 }
7534
7535 Lisp_Object
7536 code_conversion_save (int with_work_buf, int multibyte)
7537 {
7538 Lisp_Object workbuf = Qnil;
7539
7540 if (with_work_buf)
7541 workbuf = make_conversion_work_buffer (multibyte);
7542 record_unwind_protect (code_conversion_restore,
7543 Fcons (Fcurrent_buffer (), workbuf));
7544 return workbuf;
7545 }
7546
7547 int
7548 decode_coding_gap (struct coding_system *coding,
7549 EMACS_INT chars, EMACS_INT bytes)
7550 {
7551 int count = SPECPDL_INDEX ();
7552 Lisp_Object attrs;
7553
7554 code_conversion_save (0, 0);
7555
7556 coding->src_object = Fcurrent_buffer ();
7557 coding->src_chars = chars;
7558 coding->src_bytes = bytes;
7559 coding->src_pos = -chars;
7560 coding->src_pos_byte = -bytes;
7561 coding->src_multibyte = chars < bytes;
7562 coding->dst_object = coding->src_object;
7563 coding->dst_pos = PT;
7564 coding->dst_pos_byte = PT_BYTE;
7565 coding->dst_multibyte = ! NILP (B_ (current_buffer, enable_multibyte_characters));
7566
7567 if (CODING_REQUIRE_DETECTION (coding))
7568 detect_coding (coding);
7569
7570 coding->mode |= CODING_MODE_LAST_BLOCK;
7571 current_buffer->text->inhibit_shrinking = 1;
7572 decode_coding (coding);
7573 current_buffer->text->inhibit_shrinking = 0;
7574
7575 attrs = CODING_ID_ATTRS (coding->id);
7576 if (! NILP (CODING_ATTR_POST_READ (attrs)))
7577 {
7578 EMACS_INT prev_Z = Z, prev_Z_BYTE = Z_BYTE;
7579 Lisp_Object val;
7580
7581 TEMP_SET_PT_BOTH (coding->dst_pos, coding->dst_pos_byte);
7582 val = call1 (CODING_ATTR_POST_READ (attrs),
7583 make_number (coding->produced_char));
7584 CHECK_NATNUM (val);
7585 coding->produced_char += Z - prev_Z;
7586 coding->produced += Z_BYTE - prev_Z_BYTE;
7587 }
7588
7589 unbind_to (count, Qnil);
7590 return coding->result;
7591 }
7592
7593 int
7594 encode_coding_gap (struct coding_system *coding,
7595 EMACS_INT chars, EMACS_INT bytes)
7596 {
7597 int count = SPECPDL_INDEX ();
7598
7599 code_conversion_save (0, 0);
7600
7601 coding->src_object = Fcurrent_buffer ();
7602 coding->src_chars = chars;
7603 coding->src_bytes = bytes;
7604 coding->src_pos = -chars;
7605 coding->src_pos_byte = -bytes;
7606 coding->src_multibyte = chars < bytes;
7607 coding->dst_object = coding->src_object;
7608 coding->dst_pos = PT;
7609 coding->dst_pos_byte = PT_BYTE;
7610
7611 encode_coding (coding);
7612
7613 unbind_to (count, Qnil);
7614 return coding->result;
7615 }
7616
7617
7618 /* Decode the text in the range FROM/FROM_BYTE and TO/TO_BYTE in
7619 SRC_OBJECT into DST_OBJECT by coding context CODING.
7620
7621 SRC_OBJECT is a buffer, a string, or Qnil.
7622
7623 If it is a buffer, the text is at point of the buffer. FROM and TO
7624 are positions in the buffer.
7625
7626 If it is a string, the text is at the beginning of the string.
7627 FROM and TO are indices to the string.
7628
7629 If it is nil, the text is at coding->source. FROM and TO are
7630 indices to coding->source.
7631
7632 DST_OBJECT is a buffer, Qt, or Qnil.
7633
7634 If it is a buffer, the decoded text is inserted at point of the
7635 buffer. If the buffer is the same as SRC_OBJECT, the source text
7636 is deleted.
7637
7638 If it is Qt, a string is made from the decoded text, and
7639 set in CODING->dst_object.
7640
7641 If it is Qnil, the decoded text is stored at CODING->destination.
7642 The caller must allocate CODING->dst_bytes bytes at
7643 CODING->destination by xmalloc. If the decoded text is longer than
7644 CODING->dst_bytes, CODING->destination is relocated by xrealloc.
7645 */
7646
7647 void
7648 decode_coding_object (struct coding_system *coding,
7649 Lisp_Object src_object,
7650 EMACS_INT from, EMACS_INT from_byte,
7651 EMACS_INT to, EMACS_INT to_byte,
7652 Lisp_Object dst_object)
7653 {
7654 int count = SPECPDL_INDEX ();
7655 unsigned char *destination;
7656 EMACS_INT dst_bytes;
7657 EMACS_INT chars = to - from;
7658 EMACS_INT bytes = to_byte - from_byte;
7659 Lisp_Object attrs;
7660 int saved_pt = -1, saved_pt_byte;
7661 int need_marker_adjustment = 0;
7662 Lisp_Object old_deactivate_mark;
7663
7664 old_deactivate_mark = Vdeactivate_mark;
7665
7666 if (NILP (dst_object))
7667 {
7668 destination = coding->destination;
7669 dst_bytes = coding->dst_bytes;
7670 }
7671
7672 coding->src_object = src_object;
7673 coding->src_chars = chars;
7674 coding->src_bytes = bytes;
7675 coding->src_multibyte = chars < bytes;
7676
7677 if (STRINGP (src_object))
7678 {
7679 coding->src_pos = from;
7680 coding->src_pos_byte = from_byte;
7681 }
7682 else if (BUFFERP (src_object))
7683 {
7684 set_buffer_internal (XBUFFER (src_object));
7685 if (from != GPT)
7686 move_gap_both (from, from_byte);
7687 if (EQ (src_object, dst_object))
7688 {
7689 struct Lisp_Marker *tail;
7690
7691 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
7692 {
7693 tail->need_adjustment
7694 = tail->charpos == (tail->insertion_type ? from : to);
7695 need_marker_adjustment |= tail->need_adjustment;
7696 }
7697 saved_pt = PT, saved_pt_byte = PT_BYTE;
7698 TEMP_SET_PT_BOTH (from, from_byte);
7699 current_buffer->text->inhibit_shrinking = 1;
7700 del_range_both (from, from_byte, to, to_byte, 1);
7701 coding->src_pos = -chars;
7702 coding->src_pos_byte = -bytes;
7703 }
7704 else
7705 {
7706 coding->src_pos = from;
7707 coding->src_pos_byte = from_byte;
7708 }
7709 }
7710
7711 if (CODING_REQUIRE_DETECTION (coding))
7712 detect_coding (coding);
7713 attrs = CODING_ID_ATTRS (coding->id);
7714
7715 if (EQ (dst_object, Qt)
7716 || (! NILP (CODING_ATTR_POST_READ (attrs))
7717 && NILP (dst_object)))
7718 {
7719 coding->dst_multibyte = !CODING_FOR_UNIBYTE (coding);
7720 coding->dst_object = code_conversion_save (1, coding->dst_multibyte);
7721 coding->dst_pos = BEG;
7722 coding->dst_pos_byte = BEG_BYTE;
7723 }
7724 else if (BUFFERP (dst_object))
7725 {
7726 code_conversion_save (0, 0);
7727 coding->dst_object = dst_object;
7728 coding->dst_pos = BUF_PT (XBUFFER (dst_object));
7729 coding->dst_pos_byte = BUF_PT_BYTE (XBUFFER (dst_object));
7730 coding->dst_multibyte
7731 = ! NILP (B_ (XBUFFER (dst_object), enable_multibyte_characters));
7732 }
7733 else
7734 {
7735 code_conversion_save (0, 0);
7736 coding->dst_object = Qnil;
7737 /* Most callers presume this will return a multibyte result, and they
7738 won't use `binary' or `raw-text' anyway, so let's not worry about
7739 CODING_FOR_UNIBYTE. */
7740 coding->dst_multibyte = 1;
7741 }
7742
7743 decode_coding (coding);
7744
7745 if (BUFFERP (coding->dst_object))
7746 set_buffer_internal (XBUFFER (coding->dst_object));
7747
7748 if (! NILP (CODING_ATTR_POST_READ (attrs)))
7749 {
7750 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
7751 EMACS_INT prev_Z = Z, prev_Z_BYTE = Z_BYTE;
7752 Lisp_Object val;
7753
7754 TEMP_SET_PT_BOTH (coding->dst_pos, coding->dst_pos_byte);
7755 GCPRO5 (coding->src_object, coding->dst_object, src_object, dst_object,
7756 old_deactivate_mark);
7757 val = safe_call1 (CODING_ATTR_POST_READ (attrs),
7758 make_number (coding->produced_char));
7759 UNGCPRO;
7760 CHECK_NATNUM (val);
7761 coding->produced_char += Z - prev_Z;
7762 coding->produced += Z_BYTE - prev_Z_BYTE;
7763 }
7764
7765 if (EQ (dst_object, Qt))
7766 {
7767 coding->dst_object = Fbuffer_string ();
7768 }
7769 else if (NILP (dst_object) && BUFFERP (coding->dst_object))
7770 {
7771 set_buffer_internal (XBUFFER (coding->dst_object));
7772 if (dst_bytes < coding->produced)
7773 {
7774 destination = xrealloc (destination, coding->produced);
7775 if (! destination)
7776 {
7777 record_conversion_result (coding,
7778 CODING_RESULT_INSUFFICIENT_MEM);
7779 unbind_to (count, Qnil);
7780 return;
7781 }
7782 if (BEGV < GPT && GPT < BEGV + coding->produced_char)
7783 move_gap_both (BEGV, BEGV_BYTE);
7784 memcpy (destination, BEGV_ADDR, coding->produced);
7785 coding->destination = destination;
7786 }
7787 }
7788
7789 if (saved_pt >= 0)
7790 {
7791 /* This is the case of:
7792 (BUFFERP (src_object) && EQ (src_object, dst_object))
7793 As we have moved PT while replacing the original buffer
7794 contents, we must recover it now. */
7795 set_buffer_internal (XBUFFER (src_object));
7796 current_buffer->text->inhibit_shrinking = 0;
7797 if (saved_pt < from)
7798 TEMP_SET_PT_BOTH (saved_pt, saved_pt_byte);
7799 else if (saved_pt < from + chars)
7800 TEMP_SET_PT_BOTH (from, from_byte);
7801 else if (! NILP (B_ (current_buffer, enable_multibyte_characters)))
7802 TEMP_SET_PT_BOTH (saved_pt + (coding->produced_char - chars),
7803 saved_pt_byte + (coding->produced - bytes));
7804 else
7805 TEMP_SET_PT_BOTH (saved_pt + (coding->produced - bytes),
7806 saved_pt_byte + (coding->produced - bytes));
7807
7808 if (need_marker_adjustment)
7809 {
7810 struct Lisp_Marker *tail;
7811
7812 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
7813 if (tail->need_adjustment)
7814 {
7815 tail->need_adjustment = 0;
7816 if (tail->insertion_type)
7817 {
7818 tail->bytepos = from_byte;
7819 tail->charpos = from;
7820 }
7821 else
7822 {
7823 tail->bytepos = from_byte + coding->produced;
7824 tail->charpos
7825 = (NILP (B_ (current_buffer, enable_multibyte_characters))
7826 ? tail->bytepos : from + coding->produced_char);
7827 }
7828 }
7829 }
7830 }
7831
7832 Vdeactivate_mark = old_deactivate_mark;
7833 unbind_to (count, coding->dst_object);
7834 }
7835
7836
7837 void
7838 encode_coding_object (struct coding_system *coding,
7839 Lisp_Object src_object,
7840 EMACS_INT from, EMACS_INT from_byte,
7841 EMACS_INT to, EMACS_INT to_byte,
7842 Lisp_Object dst_object)
7843 {
7844 int count = SPECPDL_INDEX ();
7845 EMACS_INT chars = to - from;
7846 EMACS_INT bytes = to_byte - from_byte;
7847 Lisp_Object attrs;
7848 int saved_pt = -1, saved_pt_byte;
7849 int need_marker_adjustment = 0;
7850 int kill_src_buffer = 0;
7851 Lisp_Object old_deactivate_mark;
7852
7853 old_deactivate_mark = Vdeactivate_mark;
7854
7855 coding->src_object = src_object;
7856 coding->src_chars = chars;
7857 coding->src_bytes = bytes;
7858 coding->src_multibyte = chars < bytes;
7859
7860 attrs = CODING_ID_ATTRS (coding->id);
7861
7862 if (EQ (src_object, dst_object))
7863 {
7864 struct Lisp_Marker *tail;
7865
7866 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
7867 {
7868 tail->need_adjustment
7869 = tail->charpos == (tail->insertion_type ? from : to);
7870 need_marker_adjustment |= tail->need_adjustment;
7871 }
7872 }
7873
7874 if (! NILP (CODING_ATTR_PRE_WRITE (attrs)))
7875 {
7876 coding->src_object = code_conversion_save (1, coding->src_multibyte);
7877 set_buffer_internal (XBUFFER (coding->src_object));
7878 if (STRINGP (src_object))
7879 insert_from_string (src_object, from, from_byte, chars, bytes, 0);
7880 else if (BUFFERP (src_object))
7881 insert_from_buffer (XBUFFER (src_object), from, chars, 0);
7882 else
7883 insert_1_both ((char *) coding->source + from, chars, bytes, 0, 0, 0);
7884
7885 if (EQ (src_object, dst_object))
7886 {
7887 set_buffer_internal (XBUFFER (src_object));
7888 saved_pt = PT, saved_pt_byte = PT_BYTE;
7889 del_range_both (from, from_byte, to, to_byte, 1);
7890 set_buffer_internal (XBUFFER (coding->src_object));
7891 }
7892
7893 {
7894 Lisp_Object args[3];
7895 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
7896
7897 GCPRO5 (coding->src_object, coding->dst_object, src_object, dst_object,
7898 old_deactivate_mark);
7899 args[0] = CODING_ATTR_PRE_WRITE (attrs);
7900 args[1] = make_number (BEG);
7901 args[2] = make_number (Z);
7902 safe_call (3, args);
7903 UNGCPRO;
7904 }
7905 if (XBUFFER (coding->src_object) != current_buffer)
7906 kill_src_buffer = 1;
7907 coding->src_object = Fcurrent_buffer ();
7908 if (BEG != GPT)
7909 move_gap_both (BEG, BEG_BYTE);
7910 coding->src_chars = Z - BEG;
7911 coding->src_bytes = Z_BYTE - BEG_BYTE;
7912 coding->src_pos = BEG;
7913 coding->src_pos_byte = BEG_BYTE;
7914 coding->src_multibyte = Z < Z_BYTE;
7915 }
7916 else if (STRINGP (src_object))
7917 {
7918 code_conversion_save (0, 0);
7919 coding->src_pos = from;
7920 coding->src_pos_byte = from_byte;
7921 }
7922 else if (BUFFERP (src_object))
7923 {
7924 code_conversion_save (0, 0);
7925 set_buffer_internal (XBUFFER (src_object));
7926 if (EQ (src_object, dst_object))
7927 {
7928 saved_pt = PT, saved_pt_byte = PT_BYTE;
7929 coding->src_object = del_range_1 (from, to, 1, 1);
7930 coding->src_pos = 0;
7931 coding->src_pos_byte = 0;
7932 }
7933 else
7934 {
7935 if (from < GPT && to >= GPT)
7936 move_gap_both (from, from_byte);
7937 coding->src_pos = from;
7938 coding->src_pos_byte = from_byte;
7939 }
7940 }
7941 else
7942 code_conversion_save (0, 0);
7943
7944 if (BUFFERP (dst_object))
7945 {
7946 coding->dst_object = dst_object;
7947 if (EQ (src_object, dst_object))
7948 {
7949 coding->dst_pos = from;
7950 coding->dst_pos_byte = from_byte;
7951 }
7952 else
7953 {
7954 struct buffer *current = current_buffer;
7955
7956 set_buffer_temp (XBUFFER (dst_object));
7957 coding->dst_pos = PT;
7958 coding->dst_pos_byte = PT_BYTE;
7959 move_gap_both (coding->dst_pos, coding->dst_pos_byte);
7960 set_buffer_temp (current);
7961 }
7962 coding->dst_multibyte
7963 = ! NILP (B_ (XBUFFER (dst_object), enable_multibyte_characters));
7964 }
7965 else if (EQ (dst_object, Qt))
7966 {
7967 coding->dst_object = Qnil;
7968 coding->dst_bytes = coding->src_chars;
7969 if (coding->dst_bytes == 0)
7970 coding->dst_bytes = 1;
7971 coding->destination = (unsigned char *) xmalloc (coding->dst_bytes);
7972 coding->dst_multibyte = 0;
7973 }
7974 else
7975 {
7976 coding->dst_object = Qnil;
7977 coding->dst_multibyte = 0;
7978 }
7979
7980 encode_coding (coding);
7981
7982 if (EQ (dst_object, Qt))
7983 {
7984 if (BUFFERP (coding->dst_object))
7985 coding->dst_object = Fbuffer_string ();
7986 else
7987 {
7988 coding->dst_object
7989 = make_unibyte_string ((char *) coding->destination,
7990 coding->produced);
7991 xfree (coding->destination);
7992 }
7993 }
7994
7995 if (saved_pt >= 0)
7996 {
7997 /* This is the case of:
7998 (BUFFERP (src_object) && EQ (src_object, dst_object))
7999 As we have moved PT while replacing the original buffer
8000 contents, we must recover it now. */
8001 set_buffer_internal (XBUFFER (src_object));
8002 if (saved_pt < from)
8003 TEMP_SET_PT_BOTH (saved_pt, saved_pt_byte);
8004 else if (saved_pt < from + chars)
8005 TEMP_SET_PT_BOTH (from, from_byte);
8006 else if (! NILP (B_ (current_buffer, enable_multibyte_characters)))
8007 TEMP_SET_PT_BOTH (saved_pt + (coding->produced_char - chars),
8008 saved_pt_byte + (coding->produced - bytes));
8009 else
8010 TEMP_SET_PT_BOTH (saved_pt + (coding->produced - bytes),
8011 saved_pt_byte + (coding->produced - bytes));
8012
8013 if (need_marker_adjustment)
8014 {
8015 struct Lisp_Marker *tail;
8016
8017 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
8018 if (tail->need_adjustment)
8019 {
8020 tail->need_adjustment = 0;
8021 if (tail->insertion_type)
8022 {
8023 tail->bytepos = from_byte;
8024 tail->charpos = from;
8025 }
8026 else
8027 {
8028 tail->bytepos = from_byte + coding->produced;
8029 tail->charpos
8030 = (NILP (B_ (current_buffer, enable_multibyte_characters))
8031 ? tail->bytepos : from + coding->produced_char);
8032 }
8033 }
8034 }
8035 }
8036
8037 if (kill_src_buffer)
8038 Fkill_buffer (coding->src_object);
8039
8040 Vdeactivate_mark = old_deactivate_mark;
8041 unbind_to (count, Qnil);
8042 }
8043
8044
8045 Lisp_Object
8046 preferred_coding_system (void)
8047 {
8048 int id = coding_categories[coding_priorities[0]].id;
8049
8050 return CODING_ID_NAME (id);
8051 }
8052
8053 \f
8054 #ifdef emacs
8055 /*** 8. Emacs Lisp library functions ***/
8056
8057 DEFUN ("coding-system-p", Fcoding_system_p, Scoding_system_p, 1, 1, 0,
8058 doc: /* Return t if OBJECT is nil or a coding-system.
8059 See the documentation of `define-coding-system' for information
8060 about coding-system objects. */)
8061 (Lisp_Object object)
8062 {
8063 if (NILP (object)
8064 || CODING_SYSTEM_ID (object) >= 0)
8065 return Qt;
8066 if (! SYMBOLP (object)
8067 || NILP (Fget (object, Qcoding_system_define_form)))
8068 return Qnil;
8069 return Qt;
8070 }
8071
8072 DEFUN ("read-non-nil-coding-system", Fread_non_nil_coding_system,
8073 Sread_non_nil_coding_system, 1, 1, 0,
8074 doc: /* Read a coding system from the minibuffer, prompting with string PROMPT. */)
8075 (Lisp_Object prompt)
8076 {
8077 Lisp_Object val;
8078 do
8079 {
8080 val = Fcompleting_read (prompt, Vcoding_system_alist, Qnil,
8081 Qt, Qnil, Qcoding_system_history, Qnil, Qnil);
8082 }
8083 while (SCHARS (val) == 0);
8084 return (Fintern (val, Qnil));
8085 }
8086
8087 DEFUN ("read-coding-system", Fread_coding_system, Sread_coding_system, 1, 2, 0,
8088 doc: /* Read a coding system from the minibuffer, prompting with string PROMPT.
8089 If the user enters null input, return second argument DEFAULT-CODING-SYSTEM.
8090 Ignores case when completing coding systems (all Emacs coding systems
8091 are lower-case). */)
8092 (Lisp_Object prompt, Lisp_Object default_coding_system)
8093 {
8094 Lisp_Object val;
8095 int count = SPECPDL_INDEX ();
8096
8097 if (SYMBOLP (default_coding_system))
8098 default_coding_system = SYMBOL_NAME (default_coding_system);
8099 specbind (Qcompletion_ignore_case, Qt);
8100 val = Fcompleting_read (prompt, Vcoding_system_alist, Qnil,
8101 Qt, Qnil, Qcoding_system_history,
8102 default_coding_system, Qnil);
8103 unbind_to (count, Qnil);
8104 return (SCHARS (val) == 0 ? Qnil : Fintern (val, Qnil));
8105 }
8106
8107 DEFUN ("check-coding-system", Fcheck_coding_system, Scheck_coding_system,
8108 1, 1, 0,
8109 doc: /* Check validity of CODING-SYSTEM.
8110 If valid, return CODING-SYSTEM, else signal a `coding-system-error' error.
8111 It is valid if it is nil or a symbol defined as a coding system by the
8112 function `define-coding-system'. */)
8113 (Lisp_Object coding_system)
8114 {
8115 Lisp_Object define_form;
8116
8117 define_form = Fget (coding_system, Qcoding_system_define_form);
8118 if (! NILP (define_form))
8119 {
8120 Fput (coding_system, Qcoding_system_define_form, Qnil);
8121 safe_eval (define_form);
8122 }
8123 if (!NILP (Fcoding_system_p (coding_system)))
8124 return coding_system;
8125 xsignal1 (Qcoding_system_error, coding_system);
8126 }
8127
8128 \f
8129 /* Detect how the bytes at SRC of length SRC_BYTES are encoded. If
8130 HIGHEST is nonzero, return the coding system of the highest
8131 priority among the detected coding systems. Otherwise return a
8132 list of detected coding systems sorted by their priorities. If
8133 MULTIBYTEP is nonzero, it is assumed that the bytes are in correct
8134 multibyte form but contains only ASCII and eight-bit chars.
8135 Otherwise, the bytes are raw bytes.
8136
8137 CODING-SYSTEM controls the detection as below:
8138
8139 If it is nil, detect both text-format and eol-format. If the
8140 text-format part of CODING-SYSTEM is already specified
8141 (e.g. `iso-latin-1'), detect only eol-format. If the eol-format
8142 part of CODING-SYSTEM is already specified (e.g. `undecided-unix'),
8143 detect only text-format. */
8144
8145 Lisp_Object
8146 detect_coding_system (const unsigned char *src,
8147 EMACS_INT src_chars, EMACS_INT src_bytes,
8148 int highest, int multibytep,
8149 Lisp_Object coding_system)
8150 {
8151 const unsigned char *src_end = src + src_bytes;
8152 Lisp_Object attrs, eol_type;
8153 Lisp_Object val = Qnil;
8154 struct coding_system coding;
8155 int id;
8156 struct coding_detection_info detect_info;
8157 enum coding_category base_category;
8158 int null_byte_found = 0, eight_bit_found = 0;
8159
8160 if (NILP (coding_system))
8161 coding_system = Qundecided;
8162 setup_coding_system (coding_system, &coding);
8163 attrs = CODING_ID_ATTRS (coding.id);
8164 eol_type = CODING_ID_EOL_TYPE (coding.id);
8165 coding_system = CODING_ATTR_BASE_NAME (attrs);
8166
8167 coding.source = src;
8168 coding.src_chars = src_chars;
8169 coding.src_bytes = src_bytes;
8170 coding.src_multibyte = multibytep;
8171 coding.consumed = 0;
8172 coding.mode |= CODING_MODE_LAST_BLOCK;
8173 coding.head_ascii = 0;
8174
8175 detect_info.checked = detect_info.found = detect_info.rejected = 0;
8176
8177 /* At first, detect text-format if necessary. */
8178 base_category = XINT (CODING_ATTR_CATEGORY (attrs));
8179 if (base_category == coding_category_undecided)
8180 {
8181 enum coding_category category;
8182 struct coding_system *this;
8183 int c, i;
8184
8185 /* Skip all ASCII bytes except for a few ISO2022 controls. */
8186 for (; src < src_end; src++)
8187 {
8188 c = *src;
8189 if (c & 0x80)
8190 {
8191 eight_bit_found = 1;
8192 if (null_byte_found)
8193 break;
8194 }
8195 else if (c < 0x20)
8196 {
8197 if ((c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO)
8198 && ! inhibit_iso_escape_detection
8199 && ! detect_info.checked)
8200 {
8201 if (detect_coding_iso_2022 (&coding, &detect_info))
8202 {
8203 /* We have scanned the whole data. */
8204 if (! (detect_info.rejected & CATEGORY_MASK_ISO_7_ELSE))
8205 {
8206 /* We didn't find an 8-bit code. We may
8207 have found a null-byte, but it's very
8208 rare that a binary file confirm to
8209 ISO-2022. */
8210 src = src_end;
8211 coding.head_ascii = src - coding.source;
8212 }
8213 detect_info.rejected |= ~CATEGORY_MASK_ISO_ESCAPE;
8214 break;
8215 }
8216 }
8217 else if (! c && !inhibit_null_byte_detection)
8218 {
8219 null_byte_found = 1;
8220 if (eight_bit_found)
8221 break;
8222 }
8223 if (! eight_bit_found)
8224 coding.head_ascii++;
8225 }
8226 else if (! eight_bit_found)
8227 coding.head_ascii++;
8228 }
8229
8230 if (null_byte_found || eight_bit_found
8231 || coding.head_ascii < coding.src_bytes
8232 || detect_info.found)
8233 {
8234 if (coding.head_ascii == coding.src_bytes)
8235 /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */
8236 for (i = 0; i < coding_category_raw_text; i++)
8237 {
8238 category = coding_priorities[i];
8239 this = coding_categories + category;
8240 if (detect_info.found & (1 << category))
8241 break;
8242 }
8243 else
8244 {
8245 if (null_byte_found)
8246 {
8247 detect_info.checked |= ~CATEGORY_MASK_UTF_16;
8248 detect_info.rejected |= ~CATEGORY_MASK_UTF_16;
8249 }
8250 for (i = 0; i < coding_category_raw_text; i++)
8251 {
8252 category = coding_priorities[i];
8253 this = coding_categories + category;
8254
8255 if (this->id < 0)
8256 {
8257 /* No coding system of this category is defined. */
8258 detect_info.rejected |= (1 << category);
8259 }
8260 else if (category >= coding_category_raw_text)
8261 continue;
8262 else if (detect_info.checked & (1 << category))
8263 {
8264 if (highest
8265 && (detect_info.found & (1 << category)))
8266 break;
8267 }
8268 else if ((*(this->detector)) (&coding, &detect_info)
8269 && highest
8270 && (detect_info.found & (1 << category)))
8271 {
8272 if (category == coding_category_utf_16_auto)
8273 {
8274 if (detect_info.found & CATEGORY_MASK_UTF_16_LE)
8275 category = coding_category_utf_16_le;
8276 else
8277 category = coding_category_utf_16_be;
8278 }
8279 break;
8280 }
8281 }
8282 }
8283 }
8284
8285 if ((detect_info.rejected & CATEGORY_MASK_ANY) == CATEGORY_MASK_ANY
8286 || null_byte_found)
8287 {
8288 detect_info.found = CATEGORY_MASK_RAW_TEXT;
8289 id = CODING_SYSTEM_ID (Qno_conversion);
8290 val = Fcons (make_number (id), Qnil);
8291 }
8292 else if (! detect_info.rejected && ! detect_info.found)
8293 {
8294 detect_info.found = CATEGORY_MASK_ANY;
8295 id = coding_categories[coding_category_undecided].id;
8296 val = Fcons (make_number (id), Qnil);
8297 }
8298 else if (highest)
8299 {
8300 if (detect_info.found)
8301 {
8302 detect_info.found = 1 << category;
8303 val = Fcons (make_number (this->id), Qnil);
8304 }
8305 else
8306 for (i = 0; i < coding_category_raw_text; i++)
8307 if (! (detect_info.rejected & (1 << coding_priorities[i])))
8308 {
8309 detect_info.found = 1 << coding_priorities[i];
8310 id = coding_categories[coding_priorities[i]].id;
8311 val = Fcons (make_number (id), Qnil);
8312 break;
8313 }
8314 }
8315 else
8316 {
8317 int mask = detect_info.rejected | detect_info.found;
8318 int found = 0;
8319
8320 for (i = coding_category_raw_text - 1; i >= 0; i--)
8321 {
8322 category = coding_priorities[i];
8323 if (! (mask & (1 << category)))
8324 {
8325 found |= 1 << category;
8326 id = coding_categories[category].id;
8327 if (id >= 0)
8328 val = Fcons (make_number (id), val);
8329 }
8330 }
8331 for (i = coding_category_raw_text - 1; i >= 0; i--)
8332 {
8333 category = coding_priorities[i];
8334 if (detect_info.found & (1 << category))
8335 {
8336 id = coding_categories[category].id;
8337 val = Fcons (make_number (id), val);
8338 }
8339 }
8340 detect_info.found |= found;
8341 }
8342 }
8343 else if (base_category == coding_category_utf_8_auto)
8344 {
8345 if (detect_coding_utf_8 (&coding, &detect_info))
8346 {
8347 struct coding_system *this;
8348
8349 if (detect_info.found & CATEGORY_MASK_UTF_8_SIG)
8350 this = coding_categories + coding_category_utf_8_sig;
8351 else
8352 this = coding_categories + coding_category_utf_8_nosig;
8353 val = Fcons (make_number (this->id), Qnil);
8354 }
8355 }
8356 else if (base_category == coding_category_utf_16_auto)
8357 {
8358 if (detect_coding_utf_16 (&coding, &detect_info))
8359 {
8360 struct coding_system *this;
8361
8362 if (detect_info.found & CATEGORY_MASK_UTF_16_LE)
8363 this = coding_categories + coding_category_utf_16_le;
8364 else if (detect_info.found & CATEGORY_MASK_UTF_16_BE)
8365 this = coding_categories + coding_category_utf_16_be;
8366 else if (detect_info.rejected & CATEGORY_MASK_UTF_16_LE_NOSIG)
8367 this = coding_categories + coding_category_utf_16_be_nosig;
8368 else
8369 this = coding_categories + coding_category_utf_16_le_nosig;
8370 val = Fcons (make_number (this->id), Qnil);
8371 }
8372 }
8373 else
8374 {
8375 detect_info.found = 1 << XINT (CODING_ATTR_CATEGORY (attrs));
8376 val = Fcons (make_number (coding.id), Qnil);
8377 }
8378
8379 /* Then, detect eol-format if necessary. */
8380 {
8381 int normal_eol = -1, utf_16_be_eol = -1, utf_16_le_eol = -1;
8382 Lisp_Object tail;
8383
8384 if (VECTORP (eol_type))
8385 {
8386 if (detect_info.found & ~CATEGORY_MASK_UTF_16)
8387 {
8388 if (null_byte_found)
8389 normal_eol = EOL_SEEN_LF;
8390 else
8391 normal_eol = detect_eol (coding.source, src_bytes,
8392 coding_category_raw_text);
8393 }
8394 if (detect_info.found & (CATEGORY_MASK_UTF_16_BE
8395 | CATEGORY_MASK_UTF_16_BE_NOSIG))
8396 utf_16_be_eol = detect_eol (coding.source, src_bytes,
8397 coding_category_utf_16_be);
8398 if (detect_info.found & (CATEGORY_MASK_UTF_16_LE
8399 | CATEGORY_MASK_UTF_16_LE_NOSIG))
8400 utf_16_le_eol = detect_eol (coding.source, src_bytes,
8401 coding_category_utf_16_le);
8402 }
8403 else
8404 {
8405 if (EQ (eol_type, Qunix))
8406 normal_eol = utf_16_be_eol = utf_16_le_eol = EOL_SEEN_LF;
8407 else if (EQ (eol_type, Qdos))
8408 normal_eol = utf_16_be_eol = utf_16_le_eol = EOL_SEEN_CRLF;
8409 else
8410 normal_eol = utf_16_be_eol = utf_16_le_eol = EOL_SEEN_CR;
8411 }
8412
8413 for (tail = val; CONSP (tail); tail = XCDR (tail))
8414 {
8415 enum coding_category category;
8416 int this_eol;
8417
8418 id = XINT (XCAR (tail));
8419 attrs = CODING_ID_ATTRS (id);
8420 category = XINT (CODING_ATTR_CATEGORY (attrs));
8421 eol_type = CODING_ID_EOL_TYPE (id);
8422 if (VECTORP (eol_type))
8423 {
8424 if (category == coding_category_utf_16_be
8425 || category == coding_category_utf_16_be_nosig)
8426 this_eol = utf_16_be_eol;
8427 else if (category == coding_category_utf_16_le
8428 || category == coding_category_utf_16_le_nosig)
8429 this_eol = utf_16_le_eol;
8430 else
8431 this_eol = normal_eol;
8432
8433 if (this_eol == EOL_SEEN_LF)
8434 XSETCAR (tail, AREF (eol_type, 0));
8435 else if (this_eol == EOL_SEEN_CRLF)
8436 XSETCAR (tail, AREF (eol_type, 1));
8437 else if (this_eol == EOL_SEEN_CR)
8438 XSETCAR (tail, AREF (eol_type, 2));
8439 else
8440 XSETCAR (tail, CODING_ID_NAME (id));
8441 }
8442 else
8443 XSETCAR (tail, CODING_ID_NAME (id));
8444 }
8445 }
8446
8447 return (highest ? (CONSP (val) ? XCAR (val) : Qnil) : val);
8448 }
8449
8450
8451 DEFUN ("detect-coding-region", Fdetect_coding_region, Sdetect_coding_region,
8452 2, 3, 0,
8453 doc: /* Detect coding system of the text in the region between START and END.
8454 Return a list of possible coding systems ordered by priority.
8455 The coding systems to try and their priorities follows what
8456 the function `coding-system-priority-list' (which see) returns.
8457
8458 If only ASCII characters are found (except for such ISO-2022 control
8459 characters as ESC), it returns a list of single element `undecided'
8460 or its subsidiary coding system according to a detected end-of-line
8461 format.
8462
8463 If optional argument HIGHEST is non-nil, return the coding system of
8464 highest priority. */)
8465 (Lisp_Object start, Lisp_Object end, Lisp_Object highest)
8466 {
8467 int from, to;
8468 int from_byte, to_byte;
8469
8470 CHECK_NUMBER_COERCE_MARKER (start);
8471 CHECK_NUMBER_COERCE_MARKER (end);
8472
8473 validate_region (&start, &end);
8474 from = XINT (start), to = XINT (end);
8475 from_byte = CHAR_TO_BYTE (from);
8476 to_byte = CHAR_TO_BYTE (to);
8477
8478 if (from < GPT && to >= GPT)
8479 move_gap_both (to, to_byte);
8480
8481 return detect_coding_system (BYTE_POS_ADDR (from_byte),
8482 to - from, to_byte - from_byte,
8483 !NILP (highest),
8484 !NILP (B_ (current_buffer
8485 , enable_multibyte_characters)),
8486 Qnil);
8487 }
8488
8489 DEFUN ("detect-coding-string", Fdetect_coding_string, Sdetect_coding_string,
8490 1, 2, 0,
8491 doc: /* Detect coding system of the text in STRING.
8492 Return a list of possible coding systems ordered by priority.
8493 The coding systems to try and their priorities follows what
8494 the function `coding-system-priority-list' (which see) returns.
8495
8496 If only ASCII characters are found (except for such ISO-2022 control
8497 characters as ESC), it returns a list of single element `undecided'
8498 or its subsidiary coding system according to a detected end-of-line
8499 format.
8500
8501 If optional argument HIGHEST is non-nil, return the coding system of
8502 highest priority. */)
8503 (Lisp_Object string, Lisp_Object highest)
8504 {
8505 CHECK_STRING (string);
8506
8507 return detect_coding_system (SDATA (string),
8508 SCHARS (string), SBYTES (string),
8509 !NILP (highest), STRING_MULTIBYTE (string),
8510 Qnil);
8511 }
8512
8513
8514 static INLINE int
8515 char_encodable_p (int c, Lisp_Object attrs)
8516 {
8517 Lisp_Object tail;
8518 struct charset *charset;
8519 Lisp_Object translation_table;
8520
8521 translation_table = CODING_ATTR_TRANS_TBL (attrs);
8522 if (! NILP (translation_table))
8523 c = translate_char (translation_table, c);
8524 for (tail = CODING_ATTR_CHARSET_LIST (attrs);
8525 CONSP (tail); tail = XCDR (tail))
8526 {
8527 charset = CHARSET_FROM_ID (XINT (XCAR (tail)));
8528 if (CHAR_CHARSET_P (c, charset))
8529 break;
8530 }
8531 return (! NILP (tail));
8532 }
8533
8534
8535 /* Return a list of coding systems that safely encode the text between
8536 START and END. If EXCLUDE is non-nil, it is a list of coding
8537 systems not to check. The returned list doesn't contain any such
8538 coding systems. In any case, if the text contains only ASCII or is
8539 unibyte, return t. */
8540
8541 DEFUN ("find-coding-systems-region-internal",
8542 Ffind_coding_systems_region_internal,
8543 Sfind_coding_systems_region_internal, 2, 3, 0,
8544 doc: /* Internal use only. */)
8545 (Lisp_Object start, Lisp_Object end, Lisp_Object exclude)
8546 {
8547 Lisp_Object coding_attrs_list, safe_codings;
8548 EMACS_INT start_byte, end_byte;
8549 const unsigned char *p, *pbeg, *pend;
8550 int c;
8551 Lisp_Object tail, elt, work_table;
8552
8553 if (STRINGP (start))
8554 {
8555 if (!STRING_MULTIBYTE (start)
8556 || SCHARS (start) == SBYTES (start))
8557 return Qt;
8558 start_byte = 0;
8559 end_byte = SBYTES (start);
8560 }
8561 else
8562 {
8563 CHECK_NUMBER_COERCE_MARKER (start);
8564 CHECK_NUMBER_COERCE_MARKER (end);
8565 if (XINT (start) < BEG || XINT (end) > Z || XINT (start) > XINT (end))
8566 args_out_of_range (start, end);
8567 if (NILP (B_ (current_buffer, enable_multibyte_characters)))
8568 return Qt;
8569 start_byte = CHAR_TO_BYTE (XINT (start));
8570 end_byte = CHAR_TO_BYTE (XINT (end));
8571 if (XINT (end) - XINT (start) == end_byte - start_byte)
8572 return Qt;
8573
8574 if (XINT (start) < GPT && XINT (end) > GPT)
8575 {
8576 if ((GPT - XINT (start)) < (XINT (end) - GPT))
8577 move_gap_both (XINT (start), start_byte);
8578 else
8579 move_gap_both (XINT (end), end_byte);
8580 }
8581 }
8582
8583 coding_attrs_list = Qnil;
8584 for (tail = Vcoding_system_list; CONSP (tail); tail = XCDR (tail))
8585 if (NILP (exclude)
8586 || NILP (Fmemq (XCAR (tail), exclude)))
8587 {
8588 Lisp_Object attrs;
8589
8590 attrs = AREF (CODING_SYSTEM_SPEC (XCAR (tail)), 0);
8591 if (EQ (XCAR (tail), CODING_ATTR_BASE_NAME (attrs))
8592 && ! EQ (CODING_ATTR_TYPE (attrs), Qundecided))
8593 {
8594 ASET (attrs, coding_attr_trans_tbl,
8595 get_translation_table (attrs, 1, NULL));
8596 coding_attrs_list = Fcons (attrs, coding_attrs_list);
8597 }
8598 }
8599
8600 if (STRINGP (start))
8601 p = pbeg = SDATA (start);
8602 else
8603 p = pbeg = BYTE_POS_ADDR (start_byte);
8604 pend = p + (end_byte - start_byte);
8605
8606 while (p < pend && ASCII_BYTE_P (*p)) p++;
8607 while (p < pend && ASCII_BYTE_P (*(pend - 1))) pend--;
8608
8609 work_table = Fmake_char_table (Qnil, Qnil);
8610 while (p < pend)
8611 {
8612 if (ASCII_BYTE_P (*p))
8613 p++;
8614 else
8615 {
8616 c = STRING_CHAR_ADVANCE (p);
8617 if (!NILP (char_table_ref (work_table, c)))
8618 /* This character was already checked. Ignore it. */
8619 continue;
8620
8621 charset_map_loaded = 0;
8622 for (tail = coding_attrs_list; CONSP (tail);)
8623 {
8624 elt = XCAR (tail);
8625 if (NILP (elt))
8626 tail = XCDR (tail);
8627 else if (char_encodable_p (c, elt))
8628 tail = XCDR (tail);
8629 else if (CONSP (XCDR (tail)))
8630 {
8631 XSETCAR (tail, XCAR (XCDR (tail)));
8632 XSETCDR (tail, XCDR (XCDR (tail)));
8633 }
8634 else
8635 {
8636 XSETCAR (tail, Qnil);
8637 tail = XCDR (tail);
8638 }
8639 }
8640 if (charset_map_loaded)
8641 {
8642 EMACS_INT p_offset = p - pbeg, pend_offset = pend - pbeg;
8643
8644 if (STRINGP (start))
8645 pbeg = SDATA (start);
8646 else
8647 pbeg = BYTE_POS_ADDR (start_byte);
8648 p = pbeg + p_offset;
8649 pend = pbeg + pend_offset;
8650 }
8651 char_table_set (work_table, c, Qt);
8652 }
8653 }
8654
8655 safe_codings = list2 (Qraw_text, Qno_conversion);
8656 for (tail = coding_attrs_list; CONSP (tail); tail = XCDR (tail))
8657 if (! NILP (XCAR (tail)))
8658 safe_codings = Fcons (CODING_ATTR_BASE_NAME (XCAR (tail)), safe_codings);
8659
8660 return safe_codings;
8661 }
8662
8663
8664 DEFUN ("unencodable-char-position", Funencodable_char_position,
8665 Sunencodable_char_position, 3, 5, 0,
8666 doc: /*
8667 Return position of first un-encodable character in a region.
8668 START and END specify the region and CODING-SYSTEM specifies the
8669 encoding to check. Return nil if CODING-SYSTEM does encode the region.
8670
8671 If optional 4th argument COUNT is non-nil, it specifies at most how
8672 many un-encodable characters to search. In this case, the value is a
8673 list of positions.
8674
8675 If optional 5th argument STRING is non-nil, it is a string to search
8676 for un-encodable characters. In that case, START and END are indexes
8677 to the string. */)
8678 (Lisp_Object start, Lisp_Object end, Lisp_Object coding_system, Lisp_Object count, Lisp_Object string)
8679 {
8680 int n;
8681 struct coding_system coding;
8682 Lisp_Object attrs, charset_list, translation_table;
8683 Lisp_Object positions;
8684 int from, to;
8685 const unsigned char *p, *stop, *pend;
8686 int ascii_compatible;
8687
8688 setup_coding_system (Fcheck_coding_system (coding_system), &coding);
8689 attrs = CODING_ID_ATTRS (coding.id);
8690 if (EQ (CODING_ATTR_TYPE (attrs), Qraw_text))
8691 return Qnil;
8692 ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs));
8693 charset_list = CODING_ATTR_CHARSET_LIST (attrs);
8694 translation_table = get_translation_table (attrs, 1, NULL);
8695
8696 if (NILP (string))
8697 {
8698 validate_region (&start, &end);
8699 from = XINT (start);
8700 to = XINT (end);
8701 if (NILP (B_ (current_buffer, enable_multibyte_characters))
8702 || (ascii_compatible
8703 && (to - from) == (CHAR_TO_BYTE (to) - (CHAR_TO_BYTE (from)))))
8704 return Qnil;
8705 p = CHAR_POS_ADDR (from);
8706 pend = CHAR_POS_ADDR (to);
8707 if (from < GPT && to >= GPT)
8708 stop = GPT_ADDR;
8709 else
8710 stop = pend;
8711 }
8712 else
8713 {
8714 CHECK_STRING (string);
8715 CHECK_NATNUM (start);
8716 CHECK_NATNUM (end);
8717 from = XINT (start);
8718 to = XINT (end);
8719 if (from > to
8720 || to > SCHARS (string))
8721 args_out_of_range_3 (string, start, end);
8722 if (! STRING_MULTIBYTE (string))
8723 return Qnil;
8724 p = SDATA (string) + string_char_to_byte (string, from);
8725 stop = pend = SDATA (string) + string_char_to_byte (string, to);
8726 if (ascii_compatible && (to - from) == (pend - p))
8727 return Qnil;
8728 }
8729
8730 if (NILP (count))
8731 n = 1;
8732 else
8733 {
8734 CHECK_NATNUM (count);
8735 n = XINT (count);
8736 }
8737
8738 positions = Qnil;
8739 while (1)
8740 {
8741 int c;
8742
8743 if (ascii_compatible)
8744 while (p < stop && ASCII_BYTE_P (*p))
8745 p++, from++;
8746 if (p >= stop)
8747 {
8748 if (p >= pend)
8749 break;
8750 stop = pend;
8751 p = GAP_END_ADDR;
8752 }
8753
8754 c = STRING_CHAR_ADVANCE (p);
8755 if (! (ASCII_CHAR_P (c) && ascii_compatible)
8756 && ! char_charset (translate_char (translation_table, c),
8757 charset_list, NULL))
8758 {
8759 positions = Fcons (make_number (from), positions);
8760 n--;
8761 if (n == 0)
8762 break;
8763 }
8764
8765 from++;
8766 }
8767
8768 return (NILP (count) ? Fcar (positions) : Fnreverse (positions));
8769 }
8770
8771
8772 DEFUN ("check-coding-systems-region", Fcheck_coding_systems_region,
8773 Scheck_coding_systems_region, 3, 3, 0,
8774 doc: /* Check if the region is encodable by coding systems.
8775
8776 START and END are buffer positions specifying the region.
8777 CODING-SYSTEM-LIST is a list of coding systems to check.
8778
8779 The value is an alist ((CODING-SYSTEM POS0 POS1 ...) ...), where
8780 CODING-SYSTEM is a member of CODING-SYSTEM-LIST and can't encode the
8781 whole region, POS0, POS1, ... are buffer positions where non-encodable
8782 characters are found.
8783
8784 If all coding systems in CODING-SYSTEM-LIST can encode the region, the
8785 value is nil.
8786
8787 START may be a string. In that case, check if the string is
8788 encodable, and the value contains indices to the string instead of
8789 buffer positions. END is ignored.
8790
8791 If the current buffer (or START if it is a string) is unibyte, the value
8792 is nil. */)
8793 (Lisp_Object start, Lisp_Object end, Lisp_Object coding_system_list)
8794 {
8795 Lisp_Object list;
8796 EMACS_INT start_byte, end_byte;
8797 int pos;
8798 const unsigned char *p, *pbeg, *pend;
8799 int c;
8800 Lisp_Object tail, elt, attrs;
8801
8802 if (STRINGP (start))
8803 {
8804 if (!STRING_MULTIBYTE (start)
8805 || SCHARS (start) == SBYTES (start))
8806 return Qnil;
8807 start_byte = 0;
8808 end_byte = SBYTES (start);
8809 pos = 0;
8810 }
8811 else
8812 {
8813 CHECK_NUMBER_COERCE_MARKER (start);
8814 CHECK_NUMBER_COERCE_MARKER (end);
8815 if (XINT (start) < BEG || XINT (end) > Z || XINT (start) > XINT (end))
8816 args_out_of_range (start, end);
8817 if (NILP (B_ (current_buffer, enable_multibyte_characters)))
8818 return Qnil;
8819 start_byte = CHAR_TO_BYTE (XINT (start));
8820 end_byte = CHAR_TO_BYTE (XINT (end));
8821 if (XINT (end) - XINT (start) == end_byte - start_byte)
8822 return Qnil;
8823
8824 if (XINT (start) < GPT && XINT (end) > GPT)
8825 {
8826 if ((GPT - XINT (start)) < (XINT (end) - GPT))
8827 move_gap_both (XINT (start), start_byte);
8828 else
8829 move_gap_both (XINT (end), end_byte);
8830 }
8831 pos = XINT (start);
8832 }
8833
8834 list = Qnil;
8835 for (tail = coding_system_list; CONSP (tail); tail = XCDR (tail))
8836 {
8837 elt = XCAR (tail);
8838 attrs = AREF (CODING_SYSTEM_SPEC (elt), 0);
8839 ASET (attrs, coding_attr_trans_tbl,
8840 get_translation_table (attrs, 1, NULL));
8841 list = Fcons (Fcons (elt, Fcons (attrs, Qnil)), list);
8842 }
8843
8844 if (STRINGP (start))
8845 p = pbeg = SDATA (start);
8846 else
8847 p = pbeg = BYTE_POS_ADDR (start_byte);
8848 pend = p + (end_byte - start_byte);
8849
8850 while (p < pend && ASCII_BYTE_P (*p)) p++, pos++;
8851 while (p < pend && ASCII_BYTE_P (*(pend - 1))) pend--;
8852
8853 while (p < pend)
8854 {
8855 if (ASCII_BYTE_P (*p))
8856 p++;
8857 else
8858 {
8859 c = STRING_CHAR_ADVANCE (p);
8860
8861 charset_map_loaded = 0;
8862 for (tail = list; CONSP (tail); tail = XCDR (tail))
8863 {
8864 elt = XCDR (XCAR (tail));
8865 if (! char_encodable_p (c, XCAR (elt)))
8866 XSETCDR (elt, Fcons (make_number (pos), XCDR (elt)));
8867 }
8868 if (charset_map_loaded)
8869 {
8870 EMACS_INT p_offset = p - pbeg, pend_offset = pend - pbeg;
8871
8872 if (STRINGP (start))
8873 pbeg = SDATA (start);
8874 else
8875 pbeg = BYTE_POS_ADDR (start_byte);
8876 p = pbeg + p_offset;
8877 pend = pbeg + pend_offset;
8878 }
8879 }
8880 pos++;
8881 }
8882
8883 tail = list;
8884 list = Qnil;
8885 for (; CONSP (tail); tail = XCDR (tail))
8886 {
8887 elt = XCAR (tail);
8888 if (CONSP (XCDR (XCDR (elt))))
8889 list = Fcons (Fcons (XCAR (elt), Fnreverse (XCDR (XCDR (elt)))),
8890 list);
8891 }
8892
8893 return list;
8894 }
8895
8896
8897 Lisp_Object
8898 code_convert_region (Lisp_Object start, Lisp_Object end,
8899 Lisp_Object coding_system, Lisp_Object dst_object,
8900 int encodep, int norecord)
8901 {
8902 struct coding_system coding;
8903 EMACS_INT from, from_byte, to, to_byte;
8904 Lisp_Object src_object;
8905
8906 CHECK_NUMBER_COERCE_MARKER (start);
8907 CHECK_NUMBER_COERCE_MARKER (end);
8908 if (NILP (coding_system))
8909 coding_system = Qno_conversion;
8910 else
8911 CHECK_CODING_SYSTEM (coding_system);
8912 src_object = Fcurrent_buffer ();
8913 if (NILP (dst_object))
8914 dst_object = src_object;
8915 else if (! EQ (dst_object, Qt))
8916 CHECK_BUFFER (dst_object);
8917
8918 validate_region (&start, &end);
8919 from = XFASTINT (start);
8920 from_byte = CHAR_TO_BYTE (from);
8921 to = XFASTINT (end);
8922 to_byte = CHAR_TO_BYTE (to);
8923
8924 setup_coding_system (coding_system, &coding);
8925 coding.mode |= CODING_MODE_LAST_BLOCK;
8926
8927 if (encodep)
8928 encode_coding_object (&coding, src_object, from, from_byte, to, to_byte,
8929 dst_object);
8930 else
8931 decode_coding_object (&coding, src_object, from, from_byte, to, to_byte,
8932 dst_object);
8933 if (! norecord)
8934 Vlast_coding_system_used = CODING_ID_NAME (coding.id);
8935
8936 return (BUFFERP (dst_object)
8937 ? make_number (coding.produced_char)
8938 : coding.dst_object);
8939 }
8940
8941
8942 DEFUN ("decode-coding-region", Fdecode_coding_region, Sdecode_coding_region,
8943 3, 4, "r\nzCoding system: ",
8944 doc: /* Decode the current region from the specified coding system.
8945 When called from a program, takes four arguments:
8946 START, END, CODING-SYSTEM, and DESTINATION.
8947 START and END are buffer positions.
8948
8949 Optional 4th arguments DESTINATION specifies where the decoded text goes.
8950 If nil, the region between START and END is replaced by the decoded text.
8951 If buffer, the decoded text is inserted in that buffer after point (point
8952 does not move).
8953 In those cases, the length of the decoded text is returned.
8954 If DESTINATION is t, the decoded text is returned.
8955
8956 This function sets `last-coding-system-used' to the precise coding system
8957 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8958 not fully specified.) */)
8959 (Lisp_Object start, Lisp_Object end, Lisp_Object coding_system, Lisp_Object destination)
8960 {
8961 return code_convert_region (start, end, coding_system, destination, 0, 0);
8962 }
8963
8964 DEFUN ("encode-coding-region", Fencode_coding_region, Sencode_coding_region,
8965 3, 4, "r\nzCoding system: ",
8966 doc: /* Encode the current region by specified coding system.
8967 When called from a program, takes four arguments:
8968 START, END, CODING-SYSTEM and DESTINATION.
8969 START and END are buffer positions.
8970
8971 Optional 4th arguments DESTINATION specifies where the encoded text goes.
8972 If nil, the region between START and END is replace by the encoded text.
8973 If buffer, the encoded text is inserted in that buffer after point (point
8974 does not move).
8975 In those cases, the length of the encoded text is returned.
8976 If DESTINATION is t, the encoded text is returned.
8977
8978 This function sets `last-coding-system-used' to the precise coding system
8979 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8980 not fully specified.) */)
8981 (Lisp_Object start, Lisp_Object end, Lisp_Object coding_system, Lisp_Object destination)
8982 {
8983 return code_convert_region (start, end, coding_system, destination, 1, 0);
8984 }
8985
8986 Lisp_Object
8987 code_convert_string (Lisp_Object string, Lisp_Object coding_system,
8988 Lisp_Object dst_object, int encodep, int nocopy, int norecord)
8989 {
8990 struct coding_system coding;
8991 EMACS_INT chars, bytes;
8992
8993 CHECK_STRING (string);
8994 if (NILP (coding_system))
8995 {
8996 if (! norecord)
8997 Vlast_coding_system_used = Qno_conversion;
8998 if (NILP (dst_object))
8999 return (nocopy ? Fcopy_sequence (string) : string);
9000 }
9001
9002 if (NILP (coding_system))
9003 coding_system = Qno_conversion;
9004 else
9005 CHECK_CODING_SYSTEM (coding_system);
9006 if (NILP (dst_object))
9007 dst_object = Qt;
9008 else if (! EQ (dst_object, Qt))
9009 CHECK_BUFFER (dst_object);
9010
9011 setup_coding_system (coding_system, &coding);
9012 coding.mode |= CODING_MODE_LAST_BLOCK;
9013 chars = SCHARS (string);
9014 bytes = SBYTES (string);
9015 if (encodep)
9016 encode_coding_object (&coding, string, 0, 0, chars, bytes, dst_object);
9017 else
9018 decode_coding_object (&coding, string, 0, 0, chars, bytes, dst_object);
9019 if (! norecord)
9020 Vlast_coding_system_used = CODING_ID_NAME (coding.id);
9021
9022 return (BUFFERP (dst_object)
9023 ? make_number (coding.produced_char)
9024 : coding.dst_object);
9025 }
9026
9027
9028 /* Encode or decode STRING according to CODING_SYSTEM.
9029 Do not set Vlast_coding_system_used.
9030
9031 This function is called only from macros DECODE_FILE and
9032 ENCODE_FILE, thus we ignore character composition. */
9033
9034 Lisp_Object
9035 code_convert_string_norecord (Lisp_Object string, Lisp_Object coding_system,
9036 int encodep)
9037 {
9038 return code_convert_string (string, coding_system, Qt, encodep, 0, 1);
9039 }
9040
9041
9042 DEFUN ("decode-coding-string", Fdecode_coding_string, Sdecode_coding_string,
9043 2, 4, 0,
9044 doc: /* Decode STRING which is encoded in CODING-SYSTEM, and return the result.
9045
9046 Optional third arg NOCOPY non-nil means it is OK to return STRING itself
9047 if the decoding operation is trivial.
9048
9049 Optional fourth arg BUFFER non-nil means that the decoded text is
9050 inserted in that buffer after point (point does not move). In this
9051 case, the return value is the length of the decoded text.
9052
9053 This function sets `last-coding-system-used' to the precise coding system
9054 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
9055 not fully specified.) */)
9056 (Lisp_Object string, Lisp_Object coding_system, Lisp_Object nocopy, Lisp_Object buffer)
9057 {
9058 return code_convert_string (string, coding_system, buffer,
9059 0, ! NILP (nocopy), 0);
9060 }
9061
9062 DEFUN ("encode-coding-string", Fencode_coding_string, Sencode_coding_string,
9063 2, 4, 0,
9064 doc: /* Encode STRING to CODING-SYSTEM, and return the result.
9065
9066 Optional third arg NOCOPY non-nil means it is OK to return STRING
9067 itself if the encoding operation is trivial.
9068
9069 Optional fourth arg BUFFER non-nil means that the encoded text is
9070 inserted in that buffer after point (point does not move). In this
9071 case, the return value is the length of the encoded text.
9072
9073 This function sets `last-coding-system-used' to the precise coding system
9074 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
9075 not fully specified.) */)
9076 (Lisp_Object string, Lisp_Object coding_system, Lisp_Object nocopy, Lisp_Object buffer)
9077 {
9078 return code_convert_string (string, coding_system, buffer,
9079 1, ! NILP (nocopy), 1);
9080 }
9081
9082 \f
9083 DEFUN ("decode-sjis-char", Fdecode_sjis_char, Sdecode_sjis_char, 1, 1, 0,
9084 doc: /* Decode a Japanese character which has CODE in shift_jis encoding.
9085 Return the corresponding character. */)
9086 (Lisp_Object code)
9087 {
9088 Lisp_Object spec, attrs, val;
9089 struct charset *charset_roman, *charset_kanji, *charset_kana, *charset;
9090 int c;
9091
9092 CHECK_NATNUM (code);
9093 c = XFASTINT (code);
9094 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system, spec);
9095 attrs = AREF (spec, 0);
9096
9097 if (ASCII_BYTE_P (c)
9098 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)))
9099 return code;
9100
9101 val = CODING_ATTR_CHARSET_LIST (attrs);
9102 charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
9103 charset_kana = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
9104 charset_kanji = CHARSET_FROM_ID (XINT (XCAR (val)));
9105
9106 if (c <= 0x7F)
9107 charset = charset_roman;
9108 else if (c >= 0xA0 && c < 0xDF)
9109 {
9110 charset = charset_kana;
9111 c -= 0x80;
9112 }
9113 else
9114 {
9115 int s1 = c >> 8, s2 = c & 0xFF;
9116
9117 if (s1 < 0x81 || (s1 > 0x9F && s1 < 0xE0) || s1 > 0xEF
9118 || s2 < 0x40 || s2 == 0x7F || s2 > 0xFC)
9119 error ("Invalid code: %d", code);
9120 SJIS_TO_JIS (c);
9121 charset = charset_kanji;
9122 }
9123 c = DECODE_CHAR (charset, c);
9124 if (c < 0)
9125 error ("Invalid code: %d", code);
9126 return make_number (c);
9127 }
9128
9129
9130 DEFUN ("encode-sjis-char", Fencode_sjis_char, Sencode_sjis_char, 1, 1, 0,
9131 doc: /* Encode a Japanese character CH to shift_jis encoding.
9132 Return the corresponding code in SJIS. */)
9133 (Lisp_Object ch)
9134 {
9135 Lisp_Object spec, attrs, charset_list;
9136 int c;
9137 struct charset *charset;
9138 unsigned code;
9139
9140 CHECK_CHARACTER (ch);
9141 c = XFASTINT (ch);
9142 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system, spec);
9143 attrs = AREF (spec, 0);
9144
9145 if (ASCII_CHAR_P (c)
9146 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)))
9147 return ch;
9148
9149 charset_list = CODING_ATTR_CHARSET_LIST (attrs);
9150 charset = char_charset (c, charset_list, &code);
9151 if (code == CHARSET_INVALID_CODE (charset))
9152 error ("Can't encode by shift_jis encoding: %d", c);
9153 JIS_TO_SJIS (code);
9154
9155 return make_number (code);
9156 }
9157
9158 DEFUN ("decode-big5-char", Fdecode_big5_char, Sdecode_big5_char, 1, 1, 0,
9159 doc: /* Decode a Big5 character which has CODE in BIG5 coding system.
9160 Return the corresponding character. */)
9161 (Lisp_Object code)
9162 {
9163 Lisp_Object spec, attrs, val;
9164 struct charset *charset_roman, *charset_big5, *charset;
9165 int c;
9166
9167 CHECK_NATNUM (code);
9168 c = XFASTINT (code);
9169 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system, spec);
9170 attrs = AREF (spec, 0);
9171
9172 if (ASCII_BYTE_P (c)
9173 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)))
9174 return code;
9175
9176 val = CODING_ATTR_CHARSET_LIST (attrs);
9177 charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
9178 charset_big5 = CHARSET_FROM_ID (XINT (XCAR (val)));
9179
9180 if (c <= 0x7F)
9181 charset = charset_roman;
9182 else
9183 {
9184 int b1 = c >> 8, b2 = c & 0x7F;
9185 if (b1 < 0xA1 || b1 > 0xFE
9186 || b2 < 0x40 || (b2 > 0x7E && b2 < 0xA1) || b2 > 0xFE)
9187 error ("Invalid code: %d", code);
9188 charset = charset_big5;
9189 }
9190 c = DECODE_CHAR (charset, (unsigned )c);
9191 if (c < 0)
9192 error ("Invalid code: %d", code);
9193 return make_number (c);
9194 }
9195
9196 DEFUN ("encode-big5-char", Fencode_big5_char, Sencode_big5_char, 1, 1, 0,
9197 doc: /* Encode the Big5 character CH to BIG5 coding system.
9198 Return the corresponding character code in Big5. */)
9199 (Lisp_Object ch)
9200 {
9201 Lisp_Object spec, attrs, charset_list;
9202 struct charset *charset;
9203 int c;
9204 unsigned code;
9205
9206 CHECK_CHARACTER (ch);
9207 c = XFASTINT (ch);
9208 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system, spec);
9209 attrs = AREF (spec, 0);
9210 if (ASCII_CHAR_P (c)
9211 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)))
9212 return ch;
9213
9214 charset_list = CODING_ATTR_CHARSET_LIST (attrs);
9215 charset = char_charset (c, charset_list, &code);
9216 if (code == CHARSET_INVALID_CODE (charset))
9217 error ("Can't encode by Big5 encoding: %d", c);
9218
9219 return make_number (code);
9220 }
9221
9222 \f
9223 DEFUN ("set-terminal-coding-system-internal", Fset_terminal_coding_system_internal,
9224 Sset_terminal_coding_system_internal, 1, 2, 0,
9225 doc: /* Internal use only. */)
9226 (Lisp_Object coding_system, Lisp_Object terminal)
9227 {
9228 struct terminal *term = get_terminal (terminal, 1);
9229 struct coding_system *terminal_coding = TERMINAL_TERMINAL_CODING (term);
9230 CHECK_SYMBOL (coding_system);
9231 setup_coding_system (Fcheck_coding_system (coding_system), terminal_coding);
9232 /* We had better not send unsafe characters to terminal. */
9233 terminal_coding->mode |= CODING_MODE_SAFE_ENCODING;
9234 /* Character composition should be disabled. */
9235 terminal_coding->common_flags &= ~CODING_ANNOTATE_COMPOSITION_MASK;
9236 terminal_coding->src_multibyte = 1;
9237 terminal_coding->dst_multibyte = 0;
9238 if (terminal_coding->common_flags & CODING_REQUIRE_ENCODING_MASK)
9239 term->charset_list = coding_charset_list (terminal_coding);
9240 else
9241 term->charset_list = Fcons (make_number (charset_ascii), Qnil);
9242 return Qnil;
9243 }
9244
9245 DEFUN ("set-safe-terminal-coding-system-internal",
9246 Fset_safe_terminal_coding_system_internal,
9247 Sset_safe_terminal_coding_system_internal, 1, 1, 0,
9248 doc: /* Internal use only. */)
9249 (Lisp_Object coding_system)
9250 {
9251 CHECK_SYMBOL (coding_system);
9252 setup_coding_system (Fcheck_coding_system (coding_system),
9253 &safe_terminal_coding);
9254 /* Character composition should be disabled. */
9255 safe_terminal_coding.common_flags &= ~CODING_ANNOTATE_COMPOSITION_MASK;
9256 safe_terminal_coding.src_multibyte = 1;
9257 safe_terminal_coding.dst_multibyte = 0;
9258 return Qnil;
9259 }
9260
9261 DEFUN ("terminal-coding-system", Fterminal_coding_system,
9262 Sterminal_coding_system, 0, 1, 0,
9263 doc: /* Return coding system specified for terminal output on the given terminal.
9264 TERMINAL may be a terminal object, a frame, or nil for the selected
9265 frame's terminal device. */)
9266 (Lisp_Object terminal)
9267 {
9268 struct coding_system *terminal_coding
9269 = TERMINAL_TERMINAL_CODING (get_terminal (terminal, 1));
9270 Lisp_Object coding_system = CODING_ID_NAME (terminal_coding->id);
9271
9272 /* For backward compatibility, return nil if it is `undecided'. */
9273 return (! EQ (coding_system, Qundecided) ? coding_system : Qnil);
9274 }
9275
9276 DEFUN ("set-keyboard-coding-system-internal", Fset_keyboard_coding_system_internal,
9277 Sset_keyboard_coding_system_internal, 1, 2, 0,
9278 doc: /* Internal use only. */)
9279 (Lisp_Object coding_system, Lisp_Object terminal)
9280 {
9281 struct terminal *t = get_terminal (terminal, 1);
9282 CHECK_SYMBOL (coding_system);
9283 if (NILP (coding_system))
9284 coding_system = Qno_conversion;
9285 else
9286 Fcheck_coding_system (coding_system);
9287 setup_coding_system (coding_system, TERMINAL_KEYBOARD_CODING (t));
9288 /* Character composition should be disabled. */
9289 TERMINAL_KEYBOARD_CODING (t)->common_flags
9290 &= ~CODING_ANNOTATE_COMPOSITION_MASK;
9291 return Qnil;
9292 }
9293
9294 DEFUN ("keyboard-coding-system",
9295 Fkeyboard_coding_system, Skeyboard_coding_system, 0, 1, 0,
9296 doc: /* Return coding system specified for decoding keyboard input. */)
9297 (Lisp_Object terminal)
9298 {
9299 return CODING_ID_NAME (TERMINAL_KEYBOARD_CODING
9300 (get_terminal (terminal, 1))->id);
9301 }
9302
9303 \f
9304 DEFUN ("find-operation-coding-system", Ffind_operation_coding_system,
9305 Sfind_operation_coding_system, 1, MANY, 0,
9306 doc: /* Choose a coding system for an operation based on the target name.
9307 The value names a pair of coding systems: (DECODING-SYSTEM . ENCODING-SYSTEM).
9308 DECODING-SYSTEM is the coding system to use for decoding
9309 \(in case OPERATION does decoding), and ENCODING-SYSTEM is the coding system
9310 for encoding (in case OPERATION does encoding).
9311
9312 The first argument OPERATION specifies an I/O primitive:
9313 For file I/O, `insert-file-contents' or `write-region'.
9314 For process I/O, `call-process', `call-process-region', or `start-process'.
9315 For network I/O, `open-network-stream'.
9316
9317 The remaining arguments should be the same arguments that were passed
9318 to the primitive. Depending on which primitive, one of those arguments
9319 is selected as the TARGET. For example, if OPERATION does file I/O,
9320 whichever argument specifies the file name is TARGET.
9321
9322 TARGET has a meaning which depends on OPERATION:
9323 For file I/O, TARGET is a file name (except for the special case below).
9324 For process I/O, TARGET is a process name.
9325 For network I/O, TARGET is a service name or a port number.
9326
9327 This function looks up what is specified for TARGET in
9328 `file-coding-system-alist', `process-coding-system-alist',
9329 or `network-coding-system-alist' depending on OPERATION.
9330 They may specify a coding system, a cons of coding systems,
9331 or a function symbol to call.
9332 In the last case, we call the function with one argument,
9333 which is a list of all the arguments given to this function.
9334 If the function can't decide a coding system, it can return
9335 `undecided' so that the normal code-detection is performed.
9336
9337 If OPERATION is `insert-file-contents', the argument corresponding to
9338 TARGET may be a cons (FILENAME . BUFFER). In that case, FILENAME is a
9339 file name to look up, and BUFFER is a buffer that contains the file's
9340 contents (not yet decoded). If `file-coding-system-alist' specifies a
9341 function to call for FILENAME, that function should examine the
9342 contents of BUFFER instead of reading the file.
9343
9344 usage: (find-operation-coding-system OPERATION ARGUMENTS...) */)
9345 (int nargs, Lisp_Object *args)
9346 {
9347 Lisp_Object operation, target_idx, target, val;
9348 register Lisp_Object chain;
9349
9350 if (nargs < 2)
9351 error ("Too few arguments");
9352 operation = args[0];
9353 if (!SYMBOLP (operation)
9354 || !INTEGERP (target_idx = Fget (operation, Qtarget_idx)))
9355 error ("Invalid first argument");
9356 if (nargs < 1 + XINT (target_idx))
9357 error ("Too few arguments for operation: %s",
9358 SDATA (SYMBOL_NAME (operation)));
9359 target = args[XINT (target_idx) + 1];
9360 if (!(STRINGP (target)
9361 || (EQ (operation, Qinsert_file_contents) && CONSP (target)
9362 && STRINGP (XCAR (target)) && BUFFERP (XCDR (target)))
9363 || (EQ (operation, Qopen_network_stream) && INTEGERP (target))))
9364 error ("Invalid %dth argument", XINT (target_idx) + 1);
9365 if (CONSP (target))
9366 target = XCAR (target);
9367
9368 chain = ((EQ (operation, Qinsert_file_contents)
9369 || EQ (operation, Qwrite_region))
9370 ? Vfile_coding_system_alist
9371 : (EQ (operation, Qopen_network_stream)
9372 ? Vnetwork_coding_system_alist
9373 : Vprocess_coding_system_alist));
9374 if (NILP (chain))
9375 return Qnil;
9376
9377 for (; CONSP (chain); chain = XCDR (chain))
9378 {
9379 Lisp_Object elt;
9380
9381 elt = XCAR (chain);
9382 if (CONSP (elt)
9383 && ((STRINGP (target)
9384 && STRINGP (XCAR (elt))
9385 && fast_string_match (XCAR (elt), target) >= 0)
9386 || (INTEGERP (target) && EQ (target, XCAR (elt)))))
9387 {
9388 val = XCDR (elt);
9389 /* Here, if VAL is both a valid coding system and a valid
9390 function symbol, we return VAL as a coding system. */
9391 if (CONSP (val))
9392 return val;
9393 if (! SYMBOLP (val))
9394 return Qnil;
9395 if (! NILP (Fcoding_system_p (val)))
9396 return Fcons (val, val);
9397 if (! NILP (Ffboundp (val)))
9398 {
9399 /* We use call1 rather than safe_call1
9400 so as to get bug reports about functions called here
9401 which don't handle the current interface. */
9402 val = call1 (val, Flist (nargs, args));
9403 if (CONSP (val))
9404 return val;
9405 if (SYMBOLP (val) && ! NILP (Fcoding_system_p (val)))
9406 return Fcons (val, val);
9407 }
9408 return Qnil;
9409 }
9410 }
9411 return Qnil;
9412 }
9413
9414 DEFUN ("set-coding-system-priority", Fset_coding_system_priority,
9415 Sset_coding_system_priority, 0, MANY, 0,
9416 doc: /* Assign higher priority to the coding systems given as arguments.
9417 If multiple coding systems belong to the same category,
9418 all but the first one are ignored.
9419
9420 usage: (set-coding-system-priority &rest coding-systems) */)
9421 (int nargs, Lisp_Object *args)
9422 {
9423 int i, j;
9424 int changed[coding_category_max];
9425 enum coding_category priorities[coding_category_max];
9426
9427 memset (changed, 0, sizeof changed);
9428
9429 for (i = j = 0; i < nargs; i++)
9430 {
9431 enum coding_category category;
9432 Lisp_Object spec, attrs;
9433
9434 CHECK_CODING_SYSTEM_GET_SPEC (args[i], spec);
9435 attrs = AREF (spec, 0);
9436 category = XINT (CODING_ATTR_CATEGORY (attrs));
9437 if (changed[category])
9438 /* Ignore this coding system because a coding system of the
9439 same category already had a higher priority. */
9440 continue;
9441 changed[category] = 1;
9442 priorities[j++] = category;
9443 if (coding_categories[category].id >= 0
9444 && ! EQ (args[i], CODING_ID_NAME (coding_categories[category].id)))
9445 setup_coding_system (args[i], &coding_categories[category]);
9446 Fset (AREF (Vcoding_category_table, category), args[i]);
9447 }
9448
9449 /* Now we have decided top J priorities. Reflect the order of the
9450 original priorities to the remaining priorities. */
9451
9452 for (i = j, j = 0; i < coding_category_max; i++, j++)
9453 {
9454 while (j < coding_category_max
9455 && changed[coding_priorities[j]])
9456 j++;
9457 if (j == coding_category_max)
9458 abort ();
9459 priorities[i] = coding_priorities[j];
9460 }
9461
9462 memcpy (coding_priorities, priorities, sizeof priorities);
9463
9464 /* Update `coding-category-list'. */
9465 Vcoding_category_list = Qnil;
9466 for (i = coding_category_max - 1; i >= 0; i--)
9467 Vcoding_category_list
9468 = Fcons (AREF (Vcoding_category_table, priorities[i]),
9469 Vcoding_category_list);
9470
9471 return Qnil;
9472 }
9473
9474 DEFUN ("coding-system-priority-list", Fcoding_system_priority_list,
9475 Scoding_system_priority_list, 0, 1, 0,
9476 doc: /* Return a list of coding systems ordered by their priorities.
9477 The list contains a subset of coding systems; i.e. coding systems
9478 assigned to each coding category (see `coding-category-list').
9479
9480 HIGHESTP non-nil means just return the highest priority one. */)
9481 (Lisp_Object highestp)
9482 {
9483 int i;
9484 Lisp_Object val;
9485
9486 for (i = 0, val = Qnil; i < coding_category_max; i++)
9487 {
9488 enum coding_category category = coding_priorities[i];
9489 int id = coding_categories[category].id;
9490 Lisp_Object attrs;
9491
9492 if (id < 0)
9493 continue;
9494 attrs = CODING_ID_ATTRS (id);
9495 if (! NILP (highestp))
9496 return CODING_ATTR_BASE_NAME (attrs);
9497 val = Fcons (CODING_ATTR_BASE_NAME (attrs), val);
9498 }
9499 return Fnreverse (val);
9500 }
9501
9502 static const char *const suffixes[] = { "-unix", "-dos", "-mac" };
9503
9504 static Lisp_Object
9505 make_subsidiaries (Lisp_Object base)
9506 {
9507 Lisp_Object subsidiaries;
9508 int base_name_len = SBYTES (SYMBOL_NAME (base));
9509 char *buf = (char *) alloca (base_name_len + 6);
9510 int i;
9511
9512 memcpy (buf, SDATA (SYMBOL_NAME (base)), base_name_len);
9513 subsidiaries = Fmake_vector (make_number (3), Qnil);
9514 for (i = 0; i < 3; i++)
9515 {
9516 memcpy (buf + base_name_len, suffixes[i], strlen (suffixes[i]) + 1);
9517 ASET (subsidiaries, i, intern (buf));
9518 }
9519 return subsidiaries;
9520 }
9521
9522
9523 DEFUN ("define-coding-system-internal", Fdefine_coding_system_internal,
9524 Sdefine_coding_system_internal, coding_arg_max, MANY, 0,
9525 doc: /* For internal use only.
9526 usage: (define-coding-system-internal ...) */)
9527 (int nargs, Lisp_Object *args)
9528 {
9529 Lisp_Object name;
9530 Lisp_Object spec_vec; /* [ ATTRS ALIASE EOL_TYPE ] */
9531 Lisp_Object attrs; /* Vector of attributes. */
9532 Lisp_Object eol_type;
9533 Lisp_Object aliases;
9534 Lisp_Object coding_type, charset_list, safe_charsets;
9535 enum coding_category category;
9536 Lisp_Object tail, val;
9537 int max_charset_id = 0;
9538 int i;
9539
9540 if (nargs < coding_arg_max)
9541 goto short_args;
9542
9543 attrs = Fmake_vector (make_number (coding_attr_last_index), Qnil);
9544
9545 name = args[coding_arg_name];
9546 CHECK_SYMBOL (name);
9547 CODING_ATTR_BASE_NAME (attrs) = name;
9548
9549 val = args[coding_arg_mnemonic];
9550 if (! STRINGP (val))
9551 CHECK_CHARACTER (val);
9552 CODING_ATTR_MNEMONIC (attrs) = val;
9553
9554 coding_type = args[coding_arg_coding_type];
9555 CHECK_SYMBOL (coding_type);
9556 CODING_ATTR_TYPE (attrs) = coding_type;
9557
9558 charset_list = args[coding_arg_charset_list];
9559 if (SYMBOLP (charset_list))
9560 {
9561 if (EQ (charset_list, Qiso_2022))
9562 {
9563 if (! EQ (coding_type, Qiso_2022))
9564 error ("Invalid charset-list");
9565 charset_list = Viso_2022_charset_list;
9566 }
9567 else if (EQ (charset_list, Qemacs_mule))
9568 {
9569 if (! EQ (coding_type, Qemacs_mule))
9570 error ("Invalid charset-list");
9571 charset_list = Vemacs_mule_charset_list;
9572 }
9573 for (tail = charset_list; CONSP (tail); tail = XCDR (tail))
9574 if (max_charset_id < XFASTINT (XCAR (tail)))
9575 max_charset_id = XFASTINT (XCAR (tail));
9576 }
9577 else
9578 {
9579 charset_list = Fcopy_sequence (charset_list);
9580 for (tail = charset_list; CONSP (tail); tail = XCDR (tail))
9581 {
9582 struct charset *charset;
9583
9584 val = XCAR (tail);
9585 CHECK_CHARSET_GET_CHARSET (val, charset);
9586 if (EQ (coding_type, Qiso_2022)
9587 ? CHARSET_ISO_FINAL (charset) < 0
9588 : EQ (coding_type, Qemacs_mule)
9589 ? CHARSET_EMACS_MULE_ID (charset) < 0
9590 : 0)
9591 error ("Can't handle charset `%s'",
9592 SDATA (SYMBOL_NAME (CHARSET_NAME (charset))));
9593
9594 XSETCAR (tail, make_number (charset->id));
9595 if (max_charset_id < charset->id)
9596 max_charset_id = charset->id;
9597 }
9598 }
9599 CODING_ATTR_CHARSET_LIST (attrs) = charset_list;
9600
9601 safe_charsets = make_uninit_string (max_charset_id + 1);
9602 memset (SDATA (safe_charsets), 255, max_charset_id + 1);
9603 for (tail = charset_list; CONSP (tail); tail = XCDR (tail))
9604 SSET (safe_charsets, XFASTINT (XCAR (tail)), 0);
9605 CODING_ATTR_SAFE_CHARSETS (attrs) = safe_charsets;
9606
9607 CODING_ATTR_ASCII_COMPAT (attrs) = args[coding_arg_ascii_compatible_p];
9608
9609 val = args[coding_arg_decode_translation_table];
9610 if (! CHAR_TABLE_P (val) && ! CONSP (val))
9611 CHECK_SYMBOL (val);
9612 CODING_ATTR_DECODE_TBL (attrs) = val;
9613
9614 val = args[coding_arg_encode_translation_table];
9615 if (! CHAR_TABLE_P (val) && ! CONSP (val))
9616 CHECK_SYMBOL (val);
9617 CODING_ATTR_ENCODE_TBL (attrs) = val;
9618
9619 val = args[coding_arg_post_read_conversion];
9620 CHECK_SYMBOL (val);
9621 CODING_ATTR_POST_READ (attrs) = val;
9622
9623 val = args[coding_arg_pre_write_conversion];
9624 CHECK_SYMBOL (val);
9625 CODING_ATTR_PRE_WRITE (attrs) = val;
9626
9627 val = args[coding_arg_default_char];
9628 if (NILP (val))
9629 CODING_ATTR_DEFAULT_CHAR (attrs) = make_number (' ');
9630 else
9631 {
9632 CHECK_CHARACTER (val);
9633 CODING_ATTR_DEFAULT_CHAR (attrs) = val;
9634 }
9635
9636 val = args[coding_arg_for_unibyte];
9637 CODING_ATTR_FOR_UNIBYTE (attrs) = NILP (val) ? Qnil : Qt;
9638
9639 val = args[coding_arg_plist];
9640 CHECK_LIST (val);
9641 CODING_ATTR_PLIST (attrs) = val;
9642
9643 if (EQ (coding_type, Qcharset))
9644 {
9645 /* Generate a lisp vector of 256 elements. Each element is nil,
9646 integer, or a list of charset IDs.
9647
9648 If Nth element is nil, the byte code N is invalid in this
9649 coding system.
9650
9651 If Nth element is a number NUM, N is the first byte of a
9652 charset whose ID is NUM.
9653
9654 If Nth element is a list of charset IDs, N is the first byte
9655 of one of them. The list is sorted by dimensions of the
9656 charsets. A charset of smaller dimension comes first. */
9657 val = Fmake_vector (make_number (256), Qnil);
9658
9659 for (tail = charset_list; CONSP (tail); tail = XCDR (tail))
9660 {
9661 struct charset *charset = CHARSET_FROM_ID (XFASTINT (XCAR (tail)));
9662 int dim = CHARSET_DIMENSION (charset);
9663 int idx = (dim - 1) * 4;
9664
9665 if (CHARSET_ASCII_COMPATIBLE_P (charset))
9666 CODING_ATTR_ASCII_COMPAT (attrs) = Qt;
9667
9668 for (i = charset->code_space[idx];
9669 i <= charset->code_space[idx + 1]; i++)
9670 {
9671 Lisp_Object tmp, tmp2;
9672 int dim2;
9673
9674 tmp = AREF (val, i);
9675 if (NILP (tmp))
9676 tmp = XCAR (tail);
9677 else if (NUMBERP (tmp))
9678 {
9679 dim2 = CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (tmp)));
9680 if (dim < dim2)
9681 tmp = Fcons (XCAR (tail), Fcons (tmp, Qnil));
9682 else
9683 tmp = Fcons (tmp, Fcons (XCAR (tail), Qnil));
9684 }
9685 else
9686 {
9687 for (tmp2 = tmp; CONSP (tmp2); tmp2 = XCDR (tmp2))
9688 {
9689 dim2 = CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (XCAR (tmp2))));
9690 if (dim < dim2)
9691 break;
9692 }
9693 if (NILP (tmp2))
9694 tmp = nconc2 (tmp, Fcons (XCAR (tail), Qnil));
9695 else
9696 {
9697 XSETCDR (tmp2, Fcons (XCAR (tmp2), XCDR (tmp2)));
9698 XSETCAR (tmp2, XCAR (tail));
9699 }
9700 }
9701 ASET (val, i, tmp);
9702 }
9703 }
9704 ASET (attrs, coding_attr_charset_valids, val);
9705 category = coding_category_charset;
9706 }
9707 else if (EQ (coding_type, Qccl))
9708 {
9709 Lisp_Object valids;
9710
9711 if (nargs < coding_arg_ccl_max)
9712 goto short_args;
9713
9714 val = args[coding_arg_ccl_decoder];
9715 CHECK_CCL_PROGRAM (val);
9716 if (VECTORP (val))
9717 val = Fcopy_sequence (val);
9718 ASET (attrs, coding_attr_ccl_decoder, val);
9719
9720 val = args[coding_arg_ccl_encoder];
9721 CHECK_CCL_PROGRAM (val);
9722 if (VECTORP (val))
9723 val = Fcopy_sequence (val);
9724 ASET (attrs, coding_attr_ccl_encoder, val);
9725
9726 val = args[coding_arg_ccl_valids];
9727 valids = Fmake_string (make_number (256), make_number (0));
9728 for (tail = val; !NILP (tail); tail = Fcdr (tail))
9729 {
9730 int from, to;
9731
9732 val = Fcar (tail);
9733 if (INTEGERP (val))
9734 {
9735 from = to = XINT (val);
9736 if (from < 0 || from > 255)
9737 args_out_of_range_3 (val, make_number (0), make_number (255));
9738 }
9739 else
9740 {
9741 CHECK_CONS (val);
9742 CHECK_NATNUM_CAR (val);
9743 CHECK_NATNUM_CDR (val);
9744 from = XINT (XCAR (val));
9745 if (from > 255)
9746 args_out_of_range_3 (XCAR (val),
9747 make_number (0), make_number (255));
9748 to = XINT (XCDR (val));
9749 if (to < from || to > 255)
9750 args_out_of_range_3 (XCDR (val),
9751 XCAR (val), make_number (255));
9752 }
9753 for (i = from; i <= to; i++)
9754 SSET (valids, i, 1);
9755 }
9756 ASET (attrs, coding_attr_ccl_valids, valids);
9757
9758 category = coding_category_ccl;
9759 }
9760 else if (EQ (coding_type, Qutf_16))
9761 {
9762 Lisp_Object bom, endian;
9763
9764 CODING_ATTR_ASCII_COMPAT (attrs) = Qnil;
9765
9766 if (nargs < coding_arg_utf16_max)
9767 goto short_args;
9768
9769 bom = args[coding_arg_utf16_bom];
9770 if (! NILP (bom) && ! EQ (bom, Qt))
9771 {
9772 CHECK_CONS (bom);
9773 val = XCAR (bom);
9774 CHECK_CODING_SYSTEM (val);
9775 val = XCDR (bom);
9776 CHECK_CODING_SYSTEM (val);
9777 }
9778 ASET (attrs, coding_attr_utf_bom, bom);
9779
9780 endian = args[coding_arg_utf16_endian];
9781 CHECK_SYMBOL (endian);
9782 if (NILP (endian))
9783 endian = Qbig;
9784 else if (! EQ (endian, Qbig) && ! EQ (endian, Qlittle))
9785 error ("Invalid endian: %s", SDATA (SYMBOL_NAME (endian)));
9786 ASET (attrs, coding_attr_utf_16_endian, endian);
9787
9788 category = (CONSP (bom)
9789 ? coding_category_utf_16_auto
9790 : NILP (bom)
9791 ? (EQ (endian, Qbig)
9792 ? coding_category_utf_16_be_nosig
9793 : coding_category_utf_16_le_nosig)
9794 : (EQ (endian, Qbig)
9795 ? coding_category_utf_16_be
9796 : coding_category_utf_16_le));
9797 }
9798 else if (EQ (coding_type, Qiso_2022))
9799 {
9800 Lisp_Object initial, reg_usage, request, flags;
9801 int i;
9802
9803 if (nargs < coding_arg_iso2022_max)
9804 goto short_args;
9805
9806 initial = Fcopy_sequence (args[coding_arg_iso2022_initial]);
9807 CHECK_VECTOR (initial);
9808 for (i = 0; i < 4; i++)
9809 {
9810 val = Faref (initial, make_number (i));
9811 if (! NILP (val))
9812 {
9813 struct charset *charset;
9814
9815 CHECK_CHARSET_GET_CHARSET (val, charset);
9816 ASET (initial, i, make_number (CHARSET_ID (charset)));
9817 if (i == 0 && CHARSET_ASCII_COMPATIBLE_P (charset))
9818 CODING_ATTR_ASCII_COMPAT (attrs) = Qt;
9819 }
9820 else
9821 ASET (initial, i, make_number (-1));
9822 }
9823
9824 reg_usage = args[coding_arg_iso2022_reg_usage];
9825 CHECK_CONS (reg_usage);
9826 CHECK_NUMBER_CAR (reg_usage);
9827 CHECK_NUMBER_CDR (reg_usage);
9828
9829 request = Fcopy_sequence (args[coding_arg_iso2022_request]);
9830 for (tail = request; ! NILP (tail); tail = Fcdr (tail))
9831 {
9832 int id;
9833 Lisp_Object tmp;
9834
9835 val = Fcar (tail);
9836 CHECK_CONS (val);
9837 tmp = XCAR (val);
9838 CHECK_CHARSET_GET_ID (tmp, id);
9839 CHECK_NATNUM_CDR (val);
9840 if (XINT (XCDR (val)) >= 4)
9841 error ("Invalid graphic register number: %d", XINT (XCDR (val)));
9842 XSETCAR (val, make_number (id));
9843 }
9844
9845 flags = args[coding_arg_iso2022_flags];
9846 CHECK_NATNUM (flags);
9847 i = XINT (flags);
9848 if (EQ (args[coding_arg_charset_list], Qiso_2022))
9849 flags = make_number (i | CODING_ISO_FLAG_FULL_SUPPORT);
9850
9851 ASET (attrs, coding_attr_iso_initial, initial);
9852 ASET (attrs, coding_attr_iso_usage, reg_usage);
9853 ASET (attrs, coding_attr_iso_request, request);
9854 ASET (attrs, coding_attr_iso_flags, flags);
9855 setup_iso_safe_charsets (attrs);
9856
9857 if (i & CODING_ISO_FLAG_SEVEN_BITS)
9858 category = ((i & (CODING_ISO_FLAG_LOCKING_SHIFT
9859 | CODING_ISO_FLAG_SINGLE_SHIFT))
9860 ? coding_category_iso_7_else
9861 : EQ (args[coding_arg_charset_list], Qiso_2022)
9862 ? coding_category_iso_7
9863 : coding_category_iso_7_tight);
9864 else
9865 {
9866 int id = XINT (AREF (initial, 1));
9867
9868 category = (((i & CODING_ISO_FLAG_LOCKING_SHIFT)
9869 || EQ (args[coding_arg_charset_list], Qiso_2022)
9870 || id < 0)
9871 ? coding_category_iso_8_else
9872 : (CHARSET_DIMENSION (CHARSET_FROM_ID (id)) == 1)
9873 ? coding_category_iso_8_1
9874 : coding_category_iso_8_2);
9875 }
9876 if (category != coding_category_iso_8_1
9877 && category != coding_category_iso_8_2)
9878 CODING_ATTR_ASCII_COMPAT (attrs) = Qnil;
9879 }
9880 else if (EQ (coding_type, Qemacs_mule))
9881 {
9882 if (EQ (args[coding_arg_charset_list], Qemacs_mule))
9883 ASET (attrs, coding_attr_emacs_mule_full, Qt);
9884 CODING_ATTR_ASCII_COMPAT (attrs) = Qt;
9885 category = coding_category_emacs_mule;
9886 }
9887 else if (EQ (coding_type, Qshift_jis))
9888 {
9889
9890 struct charset *charset;
9891
9892 if (XINT (Flength (charset_list)) != 3
9893 && XINT (Flength (charset_list)) != 4)
9894 error ("There should be three or four charsets");
9895
9896 charset = CHARSET_FROM_ID (XINT (XCAR (charset_list)));
9897 if (CHARSET_DIMENSION (charset) != 1)
9898 error ("Dimension of charset %s is not one",
9899 SDATA (SYMBOL_NAME (CHARSET_NAME (charset))));
9900 if (CHARSET_ASCII_COMPATIBLE_P (charset))
9901 CODING_ATTR_ASCII_COMPAT (attrs) = Qt;
9902
9903 charset_list = XCDR (charset_list);
9904 charset = CHARSET_FROM_ID (XINT (XCAR (charset_list)));
9905 if (CHARSET_DIMENSION (charset) != 1)
9906 error ("Dimension of charset %s is not one",
9907 SDATA (SYMBOL_NAME (CHARSET_NAME (charset))));
9908
9909 charset_list = XCDR (charset_list);
9910 charset = CHARSET_FROM_ID (XINT (XCAR (charset_list)));
9911 if (CHARSET_DIMENSION (charset) != 2)
9912 error ("Dimension of charset %s is not two",
9913 SDATA (SYMBOL_NAME (CHARSET_NAME (charset))));
9914
9915 charset_list = XCDR (charset_list);
9916 if (! NILP (charset_list))
9917 {
9918 charset = CHARSET_FROM_ID (XINT (XCAR (charset_list)));
9919 if (CHARSET_DIMENSION (charset) != 2)
9920 error ("Dimension of charset %s is not two",
9921 SDATA (SYMBOL_NAME (CHARSET_NAME (charset))));
9922 }
9923
9924 category = coding_category_sjis;
9925 Vsjis_coding_system = name;
9926 }
9927 else if (EQ (coding_type, Qbig5))
9928 {
9929 struct charset *charset;
9930
9931 if (XINT (Flength (charset_list)) != 2)
9932 error ("There should be just two charsets");
9933
9934 charset = CHARSET_FROM_ID (XINT (XCAR (charset_list)));
9935 if (CHARSET_DIMENSION (charset) != 1)
9936 error ("Dimension of charset %s is not one",
9937 SDATA (SYMBOL_NAME (CHARSET_NAME (charset))));
9938 if (CHARSET_ASCII_COMPATIBLE_P (charset))
9939 CODING_ATTR_ASCII_COMPAT (attrs) = Qt;
9940
9941 charset_list = XCDR (charset_list);
9942 charset = CHARSET_FROM_ID (XINT (XCAR (charset_list)));
9943 if (CHARSET_DIMENSION (charset) != 2)
9944 error ("Dimension of charset %s is not two",
9945 SDATA (SYMBOL_NAME (CHARSET_NAME (charset))));
9946
9947 category = coding_category_big5;
9948 Vbig5_coding_system = name;
9949 }
9950 else if (EQ (coding_type, Qraw_text))
9951 {
9952 category = coding_category_raw_text;
9953 CODING_ATTR_ASCII_COMPAT (attrs) = Qt;
9954 }
9955 else if (EQ (coding_type, Qutf_8))
9956 {
9957 Lisp_Object bom;
9958
9959 CODING_ATTR_ASCII_COMPAT (attrs) = Qt;
9960
9961 if (nargs < coding_arg_utf8_max)
9962 goto short_args;
9963
9964 bom = args[coding_arg_utf8_bom];
9965 if (! NILP (bom) && ! EQ (bom, Qt))
9966 {
9967 CHECK_CONS (bom);
9968 val = XCAR (bom);
9969 CHECK_CODING_SYSTEM (val);
9970 val = XCDR (bom);
9971 CHECK_CODING_SYSTEM (val);
9972 }
9973 ASET (attrs, coding_attr_utf_bom, bom);
9974
9975 category = (CONSP (bom) ? coding_category_utf_8_auto
9976 : NILP (bom) ? coding_category_utf_8_nosig
9977 : coding_category_utf_8_sig);
9978 }
9979 else if (EQ (coding_type, Qundecided))
9980 category = coding_category_undecided;
9981 else
9982 error ("Invalid coding system type: %s",
9983 SDATA (SYMBOL_NAME (coding_type)));
9984
9985 CODING_ATTR_CATEGORY (attrs) = make_number (category);
9986 CODING_ATTR_PLIST (attrs)
9987 = Fcons (QCcategory, Fcons (AREF (Vcoding_category_table, category),
9988 CODING_ATTR_PLIST (attrs)));
9989 CODING_ATTR_PLIST (attrs)
9990 = Fcons (QCascii_compatible_p,
9991 Fcons (CODING_ATTR_ASCII_COMPAT (attrs),
9992 CODING_ATTR_PLIST (attrs)));
9993
9994 eol_type = args[coding_arg_eol_type];
9995 if (! NILP (eol_type)
9996 && ! EQ (eol_type, Qunix)
9997 && ! EQ (eol_type, Qdos)
9998 && ! EQ (eol_type, Qmac))
9999 error ("Invalid eol-type");
10000
10001 aliases = Fcons (name, Qnil);
10002
10003 if (NILP (eol_type))
10004 {
10005 eol_type = make_subsidiaries (name);
10006 for (i = 0; i < 3; i++)
10007 {
10008 Lisp_Object this_spec, this_name, this_aliases, this_eol_type;
10009
10010 this_name = AREF (eol_type, i);
10011 this_aliases = Fcons (this_name, Qnil);
10012 this_eol_type = (i == 0 ? Qunix : i == 1 ? Qdos : Qmac);
10013 this_spec = Fmake_vector (make_number (3), attrs);
10014 ASET (this_spec, 1, this_aliases);
10015 ASET (this_spec, 2, this_eol_type);
10016 Fputhash (this_name, this_spec, Vcoding_system_hash_table);
10017 Vcoding_system_list = Fcons (this_name, Vcoding_system_list);
10018 val = Fassoc (Fsymbol_name (this_name), Vcoding_system_alist);
10019 if (NILP (val))
10020 Vcoding_system_alist
10021 = Fcons (Fcons (Fsymbol_name (this_name), Qnil),
10022 Vcoding_system_alist);
10023 }
10024 }
10025
10026 spec_vec = Fmake_vector (make_number (3), attrs);
10027 ASET (spec_vec, 1, aliases);
10028 ASET (spec_vec, 2, eol_type);
10029
10030 Fputhash (name, spec_vec, Vcoding_system_hash_table);
10031 Vcoding_system_list = Fcons (name, Vcoding_system_list);
10032 val = Fassoc (Fsymbol_name (name), Vcoding_system_alist);
10033 if (NILP (val))
10034 Vcoding_system_alist = Fcons (Fcons (Fsymbol_name (name), Qnil),
10035 Vcoding_system_alist);
10036
10037 {
10038 int id = coding_categories[category].id;
10039
10040 if (id < 0 || EQ (name, CODING_ID_NAME (id)))
10041 setup_coding_system (name, &coding_categories[category]);
10042 }
10043
10044 return Qnil;
10045
10046 short_args:
10047 return Fsignal (Qwrong_number_of_arguments,
10048 Fcons (intern ("define-coding-system-internal"),
10049 make_number (nargs)));
10050 }
10051
10052
10053 DEFUN ("coding-system-put", Fcoding_system_put, Scoding_system_put,
10054 3, 3, 0,
10055 doc: /* Change value in CODING-SYSTEM's property list PROP to VAL. */)
10056 (Lisp_Object coding_system, Lisp_Object prop, Lisp_Object val)
10057 {
10058 Lisp_Object spec, attrs;
10059
10060 CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec);
10061 attrs = AREF (spec, 0);
10062 if (EQ (prop, QCmnemonic))
10063 {
10064 if (! STRINGP (val))
10065 CHECK_CHARACTER (val);
10066 CODING_ATTR_MNEMONIC (attrs) = val;
10067 }
10068 else if (EQ (prop, QCdefault_char))
10069 {
10070 if (NILP (val))
10071 val = make_number (' ');
10072 else
10073 CHECK_CHARACTER (val);
10074 CODING_ATTR_DEFAULT_CHAR (attrs) = val;
10075 }
10076 else if (EQ (prop, QCdecode_translation_table))
10077 {
10078 if (! CHAR_TABLE_P (val) && ! CONSP (val))
10079 CHECK_SYMBOL (val);
10080 CODING_ATTR_DECODE_TBL (attrs) = val;
10081 }
10082 else if (EQ (prop, QCencode_translation_table))
10083 {
10084 if (! CHAR_TABLE_P (val) && ! CONSP (val))
10085 CHECK_SYMBOL (val);
10086 CODING_ATTR_ENCODE_TBL (attrs) = val;
10087 }
10088 else if (EQ (prop, QCpost_read_conversion))
10089 {
10090 CHECK_SYMBOL (val);
10091 CODING_ATTR_POST_READ (attrs) = val;
10092 }
10093 else if (EQ (prop, QCpre_write_conversion))
10094 {
10095 CHECK_SYMBOL (val);
10096 CODING_ATTR_PRE_WRITE (attrs) = val;
10097 }
10098 else if (EQ (prop, QCascii_compatible_p))
10099 {
10100 CODING_ATTR_ASCII_COMPAT (attrs) = val;
10101 }
10102
10103 CODING_ATTR_PLIST (attrs)
10104 = Fplist_put (CODING_ATTR_PLIST (attrs), prop, val);
10105 return val;
10106 }
10107
10108
10109 DEFUN ("define-coding-system-alias", Fdefine_coding_system_alias,
10110 Sdefine_coding_system_alias, 2, 2, 0,
10111 doc: /* Define ALIAS as an alias for CODING-SYSTEM. */)
10112 (Lisp_Object alias, Lisp_Object coding_system)
10113 {
10114 Lisp_Object spec, aliases, eol_type, val;
10115
10116 CHECK_SYMBOL (alias);
10117 CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec);
10118 aliases = AREF (spec, 1);
10119 /* ALIASES should be a list of length more than zero, and the first
10120 element is a base coding system. Append ALIAS at the tail of the
10121 list. */
10122 while (!NILP (XCDR (aliases)))
10123 aliases = XCDR (aliases);
10124 XSETCDR (aliases, Fcons (alias, Qnil));
10125
10126 eol_type = AREF (spec, 2);
10127 if (VECTORP (eol_type))
10128 {
10129 Lisp_Object subsidiaries;
10130 int i;
10131
10132 subsidiaries = make_subsidiaries (alias);
10133 for (i = 0; i < 3; i++)
10134 Fdefine_coding_system_alias (AREF (subsidiaries, i),
10135 AREF (eol_type, i));
10136 }
10137
10138 Fputhash (alias, spec, Vcoding_system_hash_table);
10139 Vcoding_system_list = Fcons (alias, Vcoding_system_list);
10140 val = Fassoc (Fsymbol_name (alias), Vcoding_system_alist);
10141 if (NILP (val))
10142 Vcoding_system_alist = Fcons (Fcons (Fsymbol_name (alias), Qnil),
10143 Vcoding_system_alist);
10144
10145 return Qnil;
10146 }
10147
10148 DEFUN ("coding-system-base", Fcoding_system_base, Scoding_system_base,
10149 1, 1, 0,
10150 doc: /* Return the base of CODING-SYSTEM.
10151 Any alias or subsidiary coding system is not a base coding system. */)
10152 (Lisp_Object coding_system)
10153 {
10154 Lisp_Object spec, attrs;
10155
10156 if (NILP (coding_system))
10157 return (Qno_conversion);
10158 CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec);
10159 attrs = AREF (spec, 0);
10160 return CODING_ATTR_BASE_NAME (attrs);
10161 }
10162
10163 DEFUN ("coding-system-plist", Fcoding_system_plist, Scoding_system_plist,
10164 1, 1, 0,
10165 doc: "Return the property list of CODING-SYSTEM.")
10166 (Lisp_Object coding_system)
10167 {
10168 Lisp_Object spec, attrs;
10169
10170 if (NILP (coding_system))
10171 coding_system = Qno_conversion;
10172 CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec);
10173 attrs = AREF (spec, 0);
10174 return CODING_ATTR_PLIST (attrs);
10175 }
10176
10177
10178 DEFUN ("coding-system-aliases", Fcoding_system_aliases, Scoding_system_aliases,
10179 1, 1, 0,
10180 doc: /* Return the list of aliases of CODING-SYSTEM. */)
10181 (Lisp_Object coding_system)
10182 {
10183 Lisp_Object spec;
10184
10185 if (NILP (coding_system))
10186 coding_system = Qno_conversion;
10187 CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec);
10188 return AREF (spec, 1);
10189 }
10190
10191 DEFUN ("coding-system-eol-type", Fcoding_system_eol_type,
10192 Scoding_system_eol_type, 1, 1, 0,
10193 doc: /* Return eol-type of CODING-SYSTEM.
10194 An eol-type is an integer 0, 1, 2, or a vector of coding systems.
10195
10196 Integer values 0, 1, and 2 indicate a format of end-of-line; LF, CRLF,
10197 and CR respectively.
10198
10199 A vector value indicates that a format of end-of-line should be
10200 detected automatically. Nth element of the vector is the subsidiary
10201 coding system whose eol-type is N. */)
10202 (Lisp_Object coding_system)
10203 {
10204 Lisp_Object spec, eol_type;
10205 int n;
10206
10207 if (NILP (coding_system))
10208 coding_system = Qno_conversion;
10209 if (! CODING_SYSTEM_P (coding_system))
10210 return Qnil;
10211 spec = CODING_SYSTEM_SPEC (coding_system);
10212 eol_type = AREF (spec, 2);
10213 if (VECTORP (eol_type))
10214 return Fcopy_sequence (eol_type);
10215 n = EQ (eol_type, Qunix) ? 0 : EQ (eol_type, Qdos) ? 1 : 2;
10216 return make_number (n);
10217 }
10218
10219 #endif /* emacs */
10220
10221 \f
10222 /*** 9. Post-amble ***/
10223
10224 void
10225 init_coding_once (void)
10226 {
10227 int i;
10228
10229 for (i = 0; i < coding_category_max; i++)
10230 {
10231 coding_categories[i].id = -1;
10232 coding_priorities[i] = i;
10233 }
10234
10235 /* ISO2022 specific initialize routine. */
10236 for (i = 0; i < 0x20; i++)
10237 iso_code_class[i] = ISO_control_0;
10238 for (i = 0x21; i < 0x7F; i++)
10239 iso_code_class[i] = ISO_graphic_plane_0;
10240 for (i = 0x80; i < 0xA0; i++)
10241 iso_code_class[i] = ISO_control_1;
10242 for (i = 0xA1; i < 0xFF; i++)
10243 iso_code_class[i] = ISO_graphic_plane_1;
10244 iso_code_class[0x20] = iso_code_class[0x7F] = ISO_0x20_or_0x7F;
10245 iso_code_class[0xA0] = iso_code_class[0xFF] = ISO_0xA0_or_0xFF;
10246 iso_code_class[ISO_CODE_SO] = ISO_shift_out;
10247 iso_code_class[ISO_CODE_SI] = ISO_shift_in;
10248 iso_code_class[ISO_CODE_SS2_7] = ISO_single_shift_2_7;
10249 iso_code_class[ISO_CODE_ESC] = ISO_escape;
10250 iso_code_class[ISO_CODE_SS2] = ISO_single_shift_2;
10251 iso_code_class[ISO_CODE_SS3] = ISO_single_shift_3;
10252 iso_code_class[ISO_CODE_CSI] = ISO_control_sequence_introducer;
10253
10254 for (i = 0; i < 256; i++)
10255 {
10256 emacs_mule_bytes[i] = 1;
10257 }
10258 emacs_mule_bytes[EMACS_MULE_LEADING_CODE_PRIVATE_11] = 3;
10259 emacs_mule_bytes[EMACS_MULE_LEADING_CODE_PRIVATE_12] = 3;
10260 emacs_mule_bytes[EMACS_MULE_LEADING_CODE_PRIVATE_21] = 4;
10261 emacs_mule_bytes[EMACS_MULE_LEADING_CODE_PRIVATE_22] = 4;
10262 }
10263
10264 #ifdef emacs
10265
10266 void
10267 syms_of_coding (void)
10268 {
10269 staticpro (&Vcoding_system_hash_table);
10270 {
10271 Lisp_Object args[2];
10272 args[0] = QCtest;
10273 args[1] = Qeq;
10274 Vcoding_system_hash_table = Fmake_hash_table (2, args);
10275 }
10276
10277 staticpro (&Vsjis_coding_system);
10278 Vsjis_coding_system = Qnil;
10279
10280 staticpro (&Vbig5_coding_system);
10281 Vbig5_coding_system = Qnil;
10282
10283 staticpro (&Vcode_conversion_reused_workbuf);
10284 Vcode_conversion_reused_workbuf = Qnil;
10285
10286 staticpro (&Vcode_conversion_workbuf_name);
10287 Vcode_conversion_workbuf_name = make_pure_c_string (" *code-conversion-work*");
10288
10289 reused_workbuf_in_use = 0;
10290
10291 DEFSYM (Qcharset, "charset");
10292 DEFSYM (Qtarget_idx, "target-idx");
10293 DEFSYM (Qcoding_system_history, "coding-system-history");
10294 Fset (Qcoding_system_history, Qnil);
10295
10296 /* Target FILENAME is the first argument. */
10297 Fput (Qinsert_file_contents, Qtarget_idx, make_number (0));
10298 /* Target FILENAME is the third argument. */
10299 Fput (Qwrite_region, Qtarget_idx, make_number (2));
10300
10301 DEFSYM (Qcall_process, "call-process");
10302 /* Target PROGRAM is the first argument. */
10303 Fput (Qcall_process, Qtarget_idx, make_number (0));
10304
10305 DEFSYM (Qcall_process_region, "call-process-region");
10306 /* Target PROGRAM is the third argument. */
10307 Fput (Qcall_process_region, Qtarget_idx, make_number (2));
10308
10309 DEFSYM (Qstart_process, "start-process");
10310 /* Target PROGRAM is the third argument. */
10311 Fput (Qstart_process, Qtarget_idx, make_number (2));
10312
10313 DEFSYM (Qopen_network_stream, "open-network-stream");
10314 /* Target SERVICE is the fourth argument. */
10315 Fput (Qopen_network_stream, Qtarget_idx, make_number (3));
10316
10317 DEFSYM (Qcoding_system, "coding-system");
10318 DEFSYM (Qcoding_aliases, "coding-aliases");
10319
10320 DEFSYM (Qeol_type, "eol-type");
10321 DEFSYM (Qunix, "unix");
10322 DEFSYM (Qdos, "dos");
10323
10324 DEFSYM (Qbuffer_file_coding_system, "buffer-file-coding-system");
10325 DEFSYM (Qpost_read_conversion, "post-read-conversion");
10326 DEFSYM (Qpre_write_conversion, "pre-write-conversion");
10327 DEFSYM (Qdefault_char, "default-char");
10328 DEFSYM (Qundecided, "undecided");
10329 DEFSYM (Qno_conversion, "no-conversion");
10330 DEFSYM (Qraw_text, "raw-text");
10331
10332 DEFSYM (Qiso_2022, "iso-2022");
10333
10334 DEFSYM (Qutf_8, "utf-8");
10335 DEFSYM (Qutf_8_emacs, "utf-8-emacs");
10336
10337 DEFSYM (Qutf_16, "utf-16");
10338 DEFSYM (Qbig, "big");
10339 DEFSYM (Qlittle, "little");
10340
10341 DEFSYM (Qshift_jis, "shift-jis");
10342 DEFSYM (Qbig5, "big5");
10343
10344 DEFSYM (Qcoding_system_p, "coding-system-p");
10345
10346 DEFSYM (Qcoding_system_error, "coding-system-error");
10347 Fput (Qcoding_system_error, Qerror_conditions,
10348 pure_cons (Qcoding_system_error, pure_cons (Qerror, Qnil)));
10349 Fput (Qcoding_system_error, Qerror_message,
10350 make_pure_c_string ("Invalid coding system"));
10351
10352 /* Intern this now in case it isn't already done.
10353 Setting this variable twice is harmless.
10354 But don't staticpro it here--that is done in alloc.c. */
10355 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
10356
10357 DEFSYM (Qtranslation_table, "translation-table");
10358 Fput (Qtranslation_table, Qchar_table_extra_slots, make_number (2));
10359 DEFSYM (Qtranslation_table_id, "translation-table-id");
10360 DEFSYM (Qtranslation_table_for_decode, "translation-table-for-decode");
10361 DEFSYM (Qtranslation_table_for_encode, "translation-table-for-encode");
10362
10363 DEFSYM (Qvalid_codes, "valid-codes");
10364
10365 DEFSYM (Qemacs_mule, "emacs-mule");
10366
10367 DEFSYM (QCcategory, ":category");
10368 DEFSYM (QCmnemonic, ":mnemonic");
10369 DEFSYM (QCdefault_char, ":default-char");
10370 DEFSYM (QCdecode_translation_table, ":decode-translation-table");
10371 DEFSYM (QCencode_translation_table, ":encode-translation-table");
10372 DEFSYM (QCpost_read_conversion, ":post-read-conversion");
10373 DEFSYM (QCpre_write_conversion, ":pre-write-conversion");
10374 DEFSYM (QCascii_compatible_p, ":ascii-compatible-p");
10375
10376 Vcoding_category_table
10377 = Fmake_vector (make_number (coding_category_max), Qnil);
10378 staticpro (&Vcoding_category_table);
10379 /* Followings are target of code detection. */
10380 ASET (Vcoding_category_table, coding_category_iso_7,
10381 intern_c_string ("coding-category-iso-7"));
10382 ASET (Vcoding_category_table, coding_category_iso_7_tight,
10383 intern_c_string ("coding-category-iso-7-tight"));
10384 ASET (Vcoding_category_table, coding_category_iso_8_1,
10385 intern_c_string ("coding-category-iso-8-1"));
10386 ASET (Vcoding_category_table, coding_category_iso_8_2,
10387 intern_c_string ("coding-category-iso-8-2"));
10388 ASET (Vcoding_category_table, coding_category_iso_7_else,
10389 intern_c_string ("coding-category-iso-7-else"));
10390 ASET (Vcoding_category_table, coding_category_iso_8_else,
10391 intern_c_string ("coding-category-iso-8-else"));
10392 ASET (Vcoding_category_table, coding_category_utf_8_auto,
10393 intern_c_string ("coding-category-utf-8-auto"));
10394 ASET (Vcoding_category_table, coding_category_utf_8_nosig,
10395 intern_c_string ("coding-category-utf-8"));
10396 ASET (Vcoding_category_table, coding_category_utf_8_sig,
10397 intern_c_string ("coding-category-utf-8-sig"));
10398 ASET (Vcoding_category_table, coding_category_utf_16_be,
10399 intern_c_string ("coding-category-utf-16-be"));
10400 ASET (Vcoding_category_table, coding_category_utf_16_auto,
10401 intern_c_string ("coding-category-utf-16-auto"));
10402 ASET (Vcoding_category_table, coding_category_utf_16_le,
10403 intern_c_string ("coding-category-utf-16-le"));
10404 ASET (Vcoding_category_table, coding_category_utf_16_be_nosig,
10405 intern_c_string ("coding-category-utf-16-be-nosig"));
10406 ASET (Vcoding_category_table, coding_category_utf_16_le_nosig,
10407 intern_c_string ("coding-category-utf-16-le-nosig"));
10408 ASET (Vcoding_category_table, coding_category_charset,
10409 intern_c_string ("coding-category-charset"));
10410 ASET (Vcoding_category_table, coding_category_sjis,
10411 intern_c_string ("coding-category-sjis"));
10412 ASET (Vcoding_category_table, coding_category_big5,
10413 intern_c_string ("coding-category-big5"));
10414 ASET (Vcoding_category_table, coding_category_ccl,
10415 intern_c_string ("coding-category-ccl"));
10416 ASET (Vcoding_category_table, coding_category_emacs_mule,
10417 intern_c_string ("coding-category-emacs-mule"));
10418 /* Followings are NOT target of code detection. */
10419 ASET (Vcoding_category_table, coding_category_raw_text,
10420 intern_c_string ("coding-category-raw-text"));
10421 ASET (Vcoding_category_table, coding_category_undecided,
10422 intern_c_string ("coding-category-undecided"));
10423
10424 DEFSYM (Qinsufficient_source, "insufficient-source");
10425 DEFSYM (Qinconsistent_eol, "inconsistent-eol");
10426 DEFSYM (Qinvalid_source, "invalid-source");
10427 DEFSYM (Qinterrupted, "interrupted");
10428 DEFSYM (Qinsufficient_memory, "insufficient-memory");
10429 DEFSYM (Qcoding_system_define_form, "coding-system-define-form");
10430
10431 defsubr (&Scoding_system_p);
10432 defsubr (&Sread_coding_system);
10433 defsubr (&Sread_non_nil_coding_system);
10434 defsubr (&Scheck_coding_system);
10435 defsubr (&Sdetect_coding_region);
10436 defsubr (&Sdetect_coding_string);
10437 defsubr (&Sfind_coding_systems_region_internal);
10438 defsubr (&Sunencodable_char_position);
10439 defsubr (&Scheck_coding_systems_region);
10440 defsubr (&Sdecode_coding_region);
10441 defsubr (&Sencode_coding_region);
10442 defsubr (&Sdecode_coding_string);
10443 defsubr (&Sencode_coding_string);
10444 defsubr (&Sdecode_sjis_char);
10445 defsubr (&Sencode_sjis_char);
10446 defsubr (&Sdecode_big5_char);
10447 defsubr (&Sencode_big5_char);
10448 defsubr (&Sset_terminal_coding_system_internal);
10449 defsubr (&Sset_safe_terminal_coding_system_internal);
10450 defsubr (&Sterminal_coding_system);
10451 defsubr (&Sset_keyboard_coding_system_internal);
10452 defsubr (&Skeyboard_coding_system);
10453 defsubr (&Sfind_operation_coding_system);
10454 defsubr (&Sset_coding_system_priority);
10455 defsubr (&Sdefine_coding_system_internal);
10456 defsubr (&Sdefine_coding_system_alias);
10457 defsubr (&Scoding_system_put);
10458 defsubr (&Scoding_system_base);
10459 defsubr (&Scoding_system_plist);
10460 defsubr (&Scoding_system_aliases);
10461 defsubr (&Scoding_system_eol_type);
10462 defsubr (&Scoding_system_priority_list);
10463
10464 DEFVAR_LISP ("coding-system-list", Vcoding_system_list,
10465 doc: /* List of coding systems.
10466
10467 Do not alter the value of this variable manually. This variable should be
10468 updated by the functions `define-coding-system' and
10469 `define-coding-system-alias'. */);
10470 Vcoding_system_list = Qnil;
10471
10472 DEFVAR_LISP ("coding-system-alist", Vcoding_system_alist,
10473 doc: /* Alist of coding system names.
10474 Each element is one element list of coding system name.
10475 This variable is given to `completing-read' as COLLECTION argument.
10476
10477 Do not alter the value of this variable manually. This variable should be
10478 updated by the functions `make-coding-system' and
10479 `define-coding-system-alias'. */);
10480 Vcoding_system_alist = Qnil;
10481
10482 DEFVAR_LISP ("coding-category-list", Vcoding_category_list,
10483 doc: /* List of coding-categories (symbols) ordered by priority.
10484
10485 On detecting a coding system, Emacs tries code detection algorithms
10486 associated with each coding-category one by one in this order. When
10487 one algorithm agrees with a byte sequence of source text, the coding
10488 system bound to the corresponding coding-category is selected.
10489
10490 Don't modify this variable directly, but use `set-coding-system-priority'. */);
10491 {
10492 int i;
10493
10494 Vcoding_category_list = Qnil;
10495 for (i = coding_category_max - 1; i >= 0; i--)
10496 Vcoding_category_list
10497 = Fcons (XVECTOR (Vcoding_category_table)->contents[i],
10498 Vcoding_category_list);
10499 }
10500
10501 DEFVAR_LISP ("coding-system-for-read", Vcoding_system_for_read,
10502 doc: /* Specify the coding system for read operations.
10503 It is useful to bind this variable with `let', but do not set it globally.
10504 If the value is a coding system, it is used for decoding on read operation.
10505 If not, an appropriate element is used from one of the coding system alists.
10506 There are three such tables: `file-coding-system-alist',
10507 `process-coding-system-alist', and `network-coding-system-alist'. */);
10508 Vcoding_system_for_read = Qnil;
10509
10510 DEFVAR_LISP ("coding-system-for-write", Vcoding_system_for_write,
10511 doc: /* Specify the coding system for write operations.
10512 Programs bind this variable with `let', but you should not set it globally.
10513 If the value is a coding system, it is used for encoding of output,
10514 when writing it to a file and when sending it to a file or subprocess.
10515
10516 If this does not specify a coding system, an appropriate element
10517 is used from one of the coding system alists.
10518 There are three such tables: `file-coding-system-alist',
10519 `process-coding-system-alist', and `network-coding-system-alist'.
10520 For output to files, if the above procedure does not specify a coding system,
10521 the value of `buffer-file-coding-system' is used. */);
10522 Vcoding_system_for_write = Qnil;
10523
10524 DEFVAR_LISP ("last-coding-system-used", Vlast_coding_system_used,
10525 doc: /*
10526 Coding system used in the latest file or process I/O. */);
10527 Vlast_coding_system_used = Qnil;
10528
10529 DEFVAR_LISP ("last-code-conversion-error", Vlast_code_conversion_error,
10530 doc: /*
10531 Error status of the last code conversion.
10532
10533 When an error was detected in the last code conversion, this variable
10534 is set to one of the following symbols.
10535 `insufficient-source'
10536 `inconsistent-eol'
10537 `invalid-source'
10538 `interrupted'
10539 `insufficient-memory'
10540 When no error was detected, the value doesn't change. So, to check
10541 the error status of a code conversion by this variable, you must
10542 explicitly set this variable to nil before performing code
10543 conversion. */);
10544 Vlast_code_conversion_error = Qnil;
10545
10546 DEFVAR_BOOL ("inhibit-eol-conversion", inhibit_eol_conversion,
10547 doc: /*
10548 *Non-nil means always inhibit code conversion of end-of-line format.
10549 See info node `Coding Systems' and info node `Text and Binary' concerning
10550 such conversion. */);
10551 inhibit_eol_conversion = 0;
10552
10553 DEFVAR_BOOL ("inherit-process-coding-system", inherit_process_coding_system,
10554 doc: /*
10555 Non-nil means process buffer inherits coding system of process output.
10556 Bind it to t if the process output is to be treated as if it were a file
10557 read from some filesystem. */);
10558 inherit_process_coding_system = 0;
10559
10560 DEFVAR_LISP ("file-coding-system-alist", Vfile_coding_system_alist,
10561 doc: /*
10562 Alist to decide a coding system to use for a file I/O operation.
10563 The format is ((PATTERN . VAL) ...),
10564 where PATTERN is a regular expression matching a file name,
10565 VAL is a coding system, a cons of coding systems, or a function symbol.
10566 If VAL is a coding system, it is used for both decoding and encoding
10567 the file contents.
10568 If VAL is a cons of coding systems, the car part is used for decoding,
10569 and the cdr part is used for encoding.
10570 If VAL is a function symbol, the function must return a coding system
10571 or a cons of coding systems which are used as above. The function is
10572 called with an argument that is a list of the arguments with which
10573 `find-operation-coding-system' was called. If the function can't decide
10574 a coding system, it can return `undecided' so that the normal
10575 code-detection is performed.
10576
10577 See also the function `find-operation-coding-system'
10578 and the variable `auto-coding-alist'. */);
10579 Vfile_coding_system_alist = Qnil;
10580
10581 DEFVAR_LISP ("process-coding-system-alist", Vprocess_coding_system_alist,
10582 doc: /*
10583 Alist to decide a coding system to use for a process I/O operation.
10584 The format is ((PATTERN . VAL) ...),
10585 where PATTERN is a regular expression matching a program name,
10586 VAL is a coding system, a cons of coding systems, or a function symbol.
10587 If VAL is a coding system, it is used for both decoding what received
10588 from the program and encoding what sent to the program.
10589 If VAL is a cons of coding systems, the car part is used for decoding,
10590 and the cdr part is used for encoding.
10591 If VAL is a function symbol, the function must return a coding system
10592 or a cons of coding systems which are used as above.
10593
10594 See also the function `find-operation-coding-system'. */);
10595 Vprocess_coding_system_alist = Qnil;
10596
10597 DEFVAR_LISP ("network-coding-system-alist", Vnetwork_coding_system_alist,
10598 doc: /*
10599 Alist to decide a coding system to use for a network I/O operation.
10600 The format is ((PATTERN . VAL) ...),
10601 where PATTERN is a regular expression matching a network service name
10602 or is a port number to connect to,
10603 VAL is a coding system, a cons of coding systems, or a function symbol.
10604 If VAL is a coding system, it is used for both decoding what received
10605 from the network stream and encoding what sent to the network stream.
10606 If VAL is a cons of coding systems, the car part is used for decoding,
10607 and the cdr part is used for encoding.
10608 If VAL is a function symbol, the function must return a coding system
10609 or a cons of coding systems which are used as above.
10610
10611 See also the function `find-operation-coding-system'. */);
10612 Vnetwork_coding_system_alist = Qnil;
10613
10614 DEFVAR_LISP ("locale-coding-system", Vlocale_coding_system,
10615 doc: /* Coding system to use with system messages.
10616 Also used for decoding keyboard input on X Window system. */);
10617 Vlocale_coding_system = Qnil;
10618
10619 /* The eol mnemonics are reset in startup.el system-dependently. */
10620 DEFVAR_LISP ("eol-mnemonic-unix", eol_mnemonic_unix,
10621 doc: /*
10622 *String displayed in mode line for UNIX-like (LF) end-of-line format. */);
10623 eol_mnemonic_unix = make_pure_c_string (":");
10624
10625 DEFVAR_LISP ("eol-mnemonic-dos", eol_mnemonic_dos,
10626 doc: /*
10627 *String displayed in mode line for DOS-like (CRLF) end-of-line format. */);
10628 eol_mnemonic_dos = make_pure_c_string ("\\");
10629
10630 DEFVAR_LISP ("eol-mnemonic-mac", eol_mnemonic_mac,
10631 doc: /*
10632 *String displayed in mode line for MAC-like (CR) end-of-line format. */);
10633 eol_mnemonic_mac = make_pure_c_string ("/");
10634
10635 DEFVAR_LISP ("eol-mnemonic-undecided", eol_mnemonic_undecided,
10636 doc: /*
10637 *String displayed in mode line when end-of-line format is not yet determined. */);
10638 eol_mnemonic_undecided = make_pure_c_string (":");
10639
10640 DEFVAR_LISP ("enable-character-translation", Venable_character_translation,
10641 doc: /*
10642 *Non-nil enables character translation while encoding and decoding. */);
10643 Venable_character_translation = Qt;
10644
10645 DEFVAR_LISP ("standard-translation-table-for-decode",
10646 Vstandard_translation_table_for_decode,
10647 doc: /* Table for translating characters while decoding. */);
10648 Vstandard_translation_table_for_decode = Qnil;
10649
10650 DEFVAR_LISP ("standard-translation-table-for-encode",
10651 Vstandard_translation_table_for_encode,
10652 doc: /* Table for translating characters while encoding. */);
10653 Vstandard_translation_table_for_encode = Qnil;
10654
10655 DEFVAR_LISP ("charset-revision-table", Vcharset_revision_table,
10656 doc: /* Alist of charsets vs revision numbers.
10657 While encoding, if a charset (car part of an element) is found,
10658 designate it with the escape sequence identifying revision (cdr part
10659 of the element). */);
10660 Vcharset_revision_table = Qnil;
10661
10662 DEFVAR_LISP ("default-process-coding-system",
10663 Vdefault_process_coding_system,
10664 doc: /* Cons of coding systems used for process I/O by default.
10665 The car part is used for decoding a process output,
10666 the cdr part is used for encoding a text to be sent to a process. */);
10667 Vdefault_process_coding_system = Qnil;
10668
10669 DEFVAR_LISP ("latin-extra-code-table", Vlatin_extra_code_table,
10670 doc: /*
10671 Table of extra Latin codes in the range 128..159 (inclusive).
10672 This is a vector of length 256.
10673 If Nth element is non-nil, the existence of code N in a file
10674 \(or output of subprocess) doesn't prevent it to be detected as
10675 a coding system of ISO 2022 variant which has a flag
10676 `accept-latin-extra-code' t (e.g. iso-latin-1) on reading a file
10677 or reading output of a subprocess.
10678 Only 128th through 159th elements have a meaning. */);
10679 Vlatin_extra_code_table = Fmake_vector (make_number (256), Qnil);
10680
10681 DEFVAR_LISP ("select-safe-coding-system-function",
10682 Vselect_safe_coding_system_function,
10683 doc: /*
10684 Function to call to select safe coding system for encoding a text.
10685
10686 If set, this function is called to force a user to select a proper
10687 coding system which can encode the text in the case that a default
10688 coding system used in each operation can't encode the text. The
10689 function should take care that the buffer is not modified while
10690 the coding system is being selected.
10691
10692 The default value is `select-safe-coding-system' (which see). */);
10693 Vselect_safe_coding_system_function = Qnil;
10694
10695 DEFVAR_BOOL ("coding-system-require-warning",
10696 coding_system_require_warning,
10697 doc: /* Internal use only.
10698 If non-nil, on writing a file, `select-safe-coding-system-function' is
10699 called even if `coding-system-for-write' is non-nil. The command
10700 `universal-coding-system-argument' binds this variable to t temporarily. */);
10701 coding_system_require_warning = 0;
10702
10703
10704 DEFVAR_BOOL ("inhibit-iso-escape-detection",
10705 inhibit_iso_escape_detection,
10706 doc: /*
10707 If non-nil, Emacs ignores ISO-2022 escape sequences during code detection.
10708
10709 When Emacs reads text, it tries to detect how the text is encoded.
10710 This code detection is sensitive to escape sequences. If Emacs sees
10711 a valid ISO-2022 escape sequence, it assumes the text is encoded in one
10712 of the ISO2022 encodings, and decodes text by the corresponding coding
10713 system (e.g. `iso-2022-7bit').
10714
10715 However, there may be a case that you want to read escape sequences in
10716 a file as is. In such a case, you can set this variable to non-nil.
10717 Then the code detection will ignore any escape sequences, and no text is
10718 detected as encoded in some ISO-2022 encoding. The result is that all
10719 escape sequences become visible in a buffer.
10720
10721 The default value is nil, and it is strongly recommended not to change
10722 it. That is because many Emacs Lisp source files that contain
10723 non-ASCII characters are encoded by the coding system `iso-2022-7bit'
10724 in Emacs's distribution, and they won't be decoded correctly on
10725 reading if you suppress escape sequence detection.
10726
10727 The other way to read escape sequences in a file without decoding is
10728 to explicitly specify some coding system that doesn't use ISO-2022
10729 escape sequence (e.g `latin-1') on reading by \\[universal-coding-system-argument]. */);
10730 inhibit_iso_escape_detection = 0;
10731
10732 DEFVAR_BOOL ("inhibit-null-byte-detection",
10733 inhibit_null_byte_detection,
10734 doc: /* If non-nil, Emacs ignores null bytes on code detection.
10735 By default, Emacs treats it as binary data, and does not attempt to
10736 decode it. The effect is as if you specified `no-conversion' for
10737 reading that text.
10738
10739 Set this to non-nil when a regular text happens to include null bytes.
10740 Examples are Index nodes of Info files and null-byte delimited output
10741 from GNU Find and GNU Grep. Emacs will then ignore the null bytes and
10742 decode text as usual. */);
10743 inhibit_null_byte_detection = 0;
10744
10745 DEFVAR_LISP ("translation-table-for-input", Vtranslation_table_for_input,
10746 doc: /* Char table for translating self-inserting characters.
10747 This is applied to the result of input methods, not their input.
10748 See also `keyboard-translate-table'.
10749
10750 Use of this variable for character code unification was rendered
10751 obsolete in Emacs 23.1 and later, since Unicode is now the basis of
10752 internal character representation. */);
10753 Vtranslation_table_for_input = Qnil;
10754
10755 {
10756 Lisp_Object args[coding_arg_max];
10757 Lisp_Object plist[16];
10758 int i;
10759
10760 for (i = 0; i < coding_arg_max; i++)
10761 args[i] = Qnil;
10762
10763 plist[0] = intern_c_string (":name");
10764 plist[1] = args[coding_arg_name] = Qno_conversion;
10765 plist[2] = intern_c_string (":mnemonic");
10766 plist[3] = args[coding_arg_mnemonic] = make_number ('=');
10767 plist[4] = intern_c_string (":coding-type");
10768 plist[5] = args[coding_arg_coding_type] = Qraw_text;
10769 plist[6] = intern_c_string (":ascii-compatible-p");
10770 plist[7] = args[coding_arg_ascii_compatible_p] = Qt;
10771 plist[8] = intern_c_string (":default-char");
10772 plist[9] = args[coding_arg_default_char] = make_number (0);
10773 plist[10] = intern_c_string (":for-unibyte");
10774 plist[11] = args[coding_arg_for_unibyte] = Qt;
10775 plist[12] = intern_c_string (":docstring");
10776 plist[13] = make_pure_c_string ("Do no conversion.\n\
10777 \n\
10778 When you visit a file with this coding, the file is read into a\n\
10779 unibyte buffer as is, thus each byte of a file is treated as a\n\
10780 character.");
10781 plist[14] = intern_c_string (":eol-type");
10782 plist[15] = args[coding_arg_eol_type] = Qunix;
10783 args[coding_arg_plist] = Flist (16, plist);
10784 Fdefine_coding_system_internal (coding_arg_max, args);
10785
10786 plist[1] = args[coding_arg_name] = Qundecided;
10787 plist[3] = args[coding_arg_mnemonic] = make_number ('-');
10788 plist[5] = args[coding_arg_coding_type] = Qundecided;
10789 /* This is already set.
10790 plist[7] = args[coding_arg_ascii_compatible_p] = Qt; */
10791 plist[8] = intern_c_string (":charset-list");
10792 plist[9] = args[coding_arg_charset_list] = Fcons (Qascii, Qnil);
10793 plist[11] = args[coding_arg_for_unibyte] = Qnil;
10794 plist[13] = make_pure_c_string ("No conversion on encoding, automatic conversion on decoding.");
10795 plist[15] = args[coding_arg_eol_type] = Qnil;
10796 args[coding_arg_plist] = Flist (16, plist);
10797 Fdefine_coding_system_internal (coding_arg_max, args);
10798 }
10799
10800 setup_coding_system (Qno_conversion, &safe_terminal_coding);
10801
10802 {
10803 int i;
10804
10805 for (i = 0; i < coding_category_max; i++)
10806 Fset (AREF (Vcoding_category_table, i), Qno_conversion);
10807 }
10808 #if defined (DOS_NT)
10809 system_eol_type = Qdos;
10810 #else
10811 system_eol_type = Qunix;
10812 #endif
10813 staticpro (&system_eol_type);
10814 }
10815
10816 char *
10817 emacs_strerror (int error_number)
10818 {
10819 char *str;
10820
10821 synchronize_system_messages_locale ();
10822 str = strerror (error_number);
10823
10824 if (! NILP (Vlocale_coding_system))
10825 {
10826 Lisp_Object dec = code_convert_string_norecord (build_string (str),
10827 Vlocale_coding_system,
10828 0);
10829 str = SSDATA (dec);
10830 }
10831
10832 return str;
10833 }
10834
10835 #endif /* emacs */