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