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