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