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