Fix openp errno handling.
[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))
6867 translation_table = nconc2 (translation_table,
6868 Fcons (standard, Qnil));
6869 else
6870 translation_table = Fcons (translation_table,
6871 Fcons (standard, Qnil));
6872 }
a6f87d34 6873 }
2170c8f0
KH
6874
6875 if (max_lookup)
09ee6fdd 6876 {
2170c8f0
KH
6877 *max_lookup = 1;
6878 if (CHAR_TABLE_P (translation_table)
6879 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (translation_table)) > 1)
6880 {
6881 val = XCHAR_TABLE (translation_table)->extras[1];
6882 if (NATNUMP (val) && *max_lookup < XFASTINT (val))
6883 *max_lookup = XFASTINT (val);
6884 }
6885 else if (CONSP (translation_table))
6886 {
2735d060 6887 Lisp_Object tail;
09ee6fdd 6888
2170c8f0
KH
6889 for (tail = translation_table; CONSP (tail); tail = XCDR (tail))
6890 if (CHAR_TABLE_P (XCAR (tail))
6891 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (XCAR (tail))) > 1)
6892 {
2735d060
PE
6893 Lisp_Object tailval = XCHAR_TABLE (XCAR (tail))->extras[1];
6894 if (NATNUMP (tailval) && *max_lookup < XFASTINT (tailval))
6895 *max_lookup = XFASTINT (tailval);
2170c8f0
KH
6896 }
6897 }
a6f87d34 6898 }
7d64c6ad
KH
6899 return translation_table;
6900}
6901
09ee6fdd
KH
6902#define LOOKUP_TRANSLATION_TABLE(table, c, trans) \
6903 do { \
6904 trans = Qnil; \
6905 if (CHAR_TABLE_P (table)) \
6906 { \
6907 trans = CHAR_TABLE_REF (table, c); \
6908 if (CHARACTERP (trans)) \
6909 c = XFASTINT (trans), trans = Qnil; \
6910 } \
6911 else if (CONSP (table)) \
6912 { \
6913 Lisp_Object tail; \
6914 \
6915 for (tail = table; CONSP (tail); tail = XCDR (tail)) \
6916 if (CHAR_TABLE_P (XCAR (tail))) \
6917 { \
6918 trans = CHAR_TABLE_REF (XCAR (tail), c); \
6919 if (CHARACTERP (trans)) \
6920 c = XFASTINT (trans), trans = Qnil; \
6921 else if (! NILP (trans)) \
6922 break; \
6923 } \
6924 } \
e6a54062
KH
6925 } while (0)
6926
7d64c6ad 6927
e951386e
KH
6928/* Return a translation of character(s) at BUF according to TRANS.
6929 TRANS is TO-CHAR or ((FROM . TO) ...) where
6930 FROM = [FROM-CHAR ...], TO is TO-CHAR or [TO-CHAR ...].
6931 The return value is TO-CHAR or ([FROM-CHAR ...] . TO) if a
6932 translation is found, and Qnil if not found..
6933 If BUF is too short to lookup characters in FROM, return Qt. */
6934
69a80ea3 6935static Lisp_Object
971de7fb 6936get_translation (Lisp_Object trans, int *buf, int *buf_end)
69a80ea3 6937{
e951386e
KH
6938
6939 if (INTEGERP (trans))
6940 return trans;
6941 for (; CONSP (trans); trans = XCDR (trans))
69a80ea3 6942 {
e951386e
KH
6943 Lisp_Object val = XCAR (trans);
6944 Lisp_Object from = XCAR (val);
2c6a9faa
PE
6945 ptrdiff_t len = ASIZE (from);
6946 ptrdiff_t i;
69a80ea3 6947
e951386e 6948 for (i = 0; i < len; i++)
69a80ea3 6949 {
e951386e
KH
6950 if (buf + i == buf_end)
6951 return Qt;
6952 if (XINT (AREF (from, i)) != buf[i])
6953 break;
69a80ea3 6954 }
e951386e
KH
6955 if (i == len)
6956 return val;
69a80ea3 6957 }
e951386e 6958 return Qnil;
69a80ea3
KH
6959}
6960
6961
d46c5b12 6962static int
cf84bb53 6963produce_chars (struct coding_system *coding, Lisp_Object translation_table,
f10fe38f 6964 bool last_block)
4ed46869 6965{
df7492f9
KH
6966 unsigned char *dst = coding->destination + coding->produced;
6967 unsigned char *dst_end = coding->destination + coding->dst_bytes;
d311d28c
PE
6968 ptrdiff_t produced;
6969 ptrdiff_t produced_chars = 0;
69a80ea3 6970 int carryover = 0;
4ed46869 6971
df7492f9 6972 if (! coding->chars_at_source)
4ed46869 6973 {
119852e7 6974 /* Source characters are in coding->charbuf. */
fba4576f
AS
6975 int *buf = coding->charbuf;
6976 int *buf_end = buf + coding->charbuf_used;
4ed46869 6977
db274c7a
KH
6978 if (EQ (coding->src_object, coding->dst_object))
6979 {
6980 coding_set_source (coding);
6981 dst_end = ((unsigned char *) coding->source) + coding->consumed;
6982 }
4ed46869 6983
df7492f9 6984 while (buf < buf_end)
4ed46869 6985 {
27bb1ca4
PE
6986 int c = *buf;
6987 ptrdiff_t i;
bc4bc72a 6988
df7492f9
KH
6989 if (c >= 0)
6990 {
d311d28c 6991 ptrdiff_t from_nchars = 1, to_nchars = 1;
69a80ea3
KH
6992 Lisp_Object trans = Qnil;
6993
09ee6fdd 6994 LOOKUP_TRANSLATION_TABLE (translation_table, c, trans);
e6a54062 6995 if (! NILP (trans))
69a80ea3 6996 {
e951386e
KH
6997 trans = get_translation (trans, buf, buf_end);
6998 if (INTEGERP (trans))
6999 c = XINT (trans);
7000 else if (CONSP (trans))
7001 {
7002 from_nchars = ASIZE (XCAR (trans));
7003 trans = XCDR (trans);
7004 if (INTEGERP (trans))
7005 c = XINT (trans);
7006 else
7007 {
7008 to_nchars = ASIZE (trans);
7009 c = XINT (AREF (trans, 0));
7010 }
7011 }
7012 else if (EQ (trans, Qt) && ! last_block)
69a80ea3 7013 break;
69a80ea3
KH
7014 }
7015
5d009b3a 7016 if ((dst_end - dst) / MAX_MULTIBYTE_LENGTH < to_nchars)
69a80ea3 7017 {
5d009b3a
PE
7018 if (((min (PTRDIFF_MAX, SIZE_MAX) - (buf_end - buf))
7019 / MAX_MULTIBYTE_LENGTH)
7020 < to_nchars)
7021 memory_full (SIZE_MAX);
69a80ea3
KH
7022 dst = alloc_destination (coding,
7023 buf_end - buf
7024 + MAX_MULTIBYTE_LENGTH * to_nchars,
7025 dst);
db274c7a
KH
7026 if (EQ (coding->src_object, coding->dst_object))
7027 {
7028 coding_set_source (coding);
e951386e
KH
7029 dst_end = (((unsigned char *) coding->source)
7030 + coding->consumed);
db274c7a
KH
7031 }
7032 else
7033 dst_end = coding->destination + coding->dst_bytes;
69a80ea3
KH
7034 }
7035
433f7f87 7036 for (i = 0; i < to_nchars; i++)
69a80ea3 7037 {
433f7f87
KH
7038 if (i > 0)
7039 c = XINT (AREF (trans, i));
69a80ea3
KH
7040 if (coding->dst_multibyte
7041 || ! CHAR_BYTE8_P (c))
db274c7a 7042 CHAR_STRING_ADVANCE_NO_UNIFY (c, dst);
69a80ea3
KH
7043 else
7044 *dst++ = CHAR_TO_BYTE8 (c);
7045 }
7046 produced_chars += to_nchars;
e951386e 7047 buf += from_nchars;
d46c5b12 7048 }
df7492f9 7049 else
69a80ea3
KH
7050 /* This is an annotation datum. (-C) is the length. */
7051 buf += -c;
4ed46869 7052 }
69a80ea3 7053 carryover = buf_end - buf;
4ed46869 7054 }
fa42c37f 7055 else
fa42c37f 7056 {
119852e7 7057 /* Source characters are at coding->source. */
8f924df7 7058 const unsigned char *src = coding->source;
119852e7 7059 const unsigned char *src_end = src + coding->consumed;
4ed46869 7060
db274c7a
KH
7061 if (EQ (coding->dst_object, coding->src_object))
7062 dst_end = (unsigned char *) src;
df7492f9 7063 if (coding->src_multibyte != coding->dst_multibyte)
fa42c37f 7064 {
df7492f9 7065 if (coding->src_multibyte)
fa42c37f 7066 {
f10fe38f 7067 bool multibytep = 1;
d311d28c 7068 ptrdiff_t consumed_chars = 0;
d46c5b12 7069
df7492f9
KH
7070 while (1)
7071 {
8f924df7 7072 const unsigned char *src_base = src;
df7492f9 7073 int c;
b73bfc1c 7074
df7492f9 7075 ONE_MORE_BYTE (c);
119852e7 7076 if (dst == dst_end)
df7492f9 7077 {
119852e7
KH
7078 if (EQ (coding->src_object, coding->dst_object))
7079 dst_end = (unsigned char *) src;
7080 if (dst == dst_end)
df7492f9 7081 {
d311d28c 7082 ptrdiff_t offset = src - coding->source;
119852e7
KH
7083
7084 dst = alloc_destination (coding, src_end - src + 1,
7085 dst);
7086 dst_end = coding->destination + coding->dst_bytes;
7087 coding_set_source (coding);
7088 src = coding->source + offset;
5c1ca13d 7089 src_end = coding->source + coding->consumed;
db274c7a
KH
7090 if (EQ (coding->src_object, coding->dst_object))
7091 dst_end = (unsigned char *) src;
df7492f9 7092 }
df7492f9
KH
7093 }
7094 *dst++ = c;
7095 produced_chars++;
7096 }
7097 no_more_source:
7098 ;
fa42c37f
KH
7099 }
7100 else
df7492f9
KH
7101 while (src < src_end)
7102 {
f10fe38f 7103 bool multibytep = 1;
df7492f9 7104 int c = *src++;
b73bfc1c 7105
df7492f9
KH
7106 if (dst >= dst_end - 1)
7107 {
2c78b7e1 7108 if (EQ (coding->src_object, coding->dst_object))
8f924df7 7109 dst_end = (unsigned char *) src;
2c78b7e1
KH
7110 if (dst >= dst_end - 1)
7111 {
d311d28c
PE
7112 ptrdiff_t offset = src - coding->source;
7113 ptrdiff_t more_bytes;
119852e7 7114
db274c7a
KH
7115 if (EQ (coding->src_object, coding->dst_object))
7116 more_bytes = ((src_end - src) / 2) + 2;
7117 else
7118 more_bytes = src_end - src + 2;
7119 dst = alloc_destination (coding, more_bytes, dst);
2c78b7e1
KH
7120 dst_end = coding->destination + coding->dst_bytes;
7121 coding_set_source (coding);
119852e7 7122 src = coding->source + offset;
5c1ca13d 7123 src_end = coding->source + coding->consumed;
db274c7a
KH
7124 if (EQ (coding->src_object, coding->dst_object))
7125 dst_end = (unsigned char *) src;
2c78b7e1 7126 }
df7492f9
KH
7127 }
7128 EMIT_ONE_BYTE (c);
7129 }
d46c5b12 7130 }
df7492f9
KH
7131 else
7132 {
7133 if (!EQ (coding->src_object, coding->dst_object))
fa42c37f 7134 {
d311d28c 7135 ptrdiff_t require = coding->src_bytes - coding->dst_bytes;
4ed46869 7136
df7492f9 7137 if (require > 0)
fa42c37f 7138 {
d311d28c 7139 ptrdiff_t offset = src - coding->source;
df7492f9
KH
7140
7141 dst = alloc_destination (coding, require, dst);
7142 coding_set_source (coding);
7143 src = coding->source + offset;
5c1ca13d 7144 src_end = coding->source + coding->consumed;
fa42c37f
KH
7145 }
7146 }
119852e7 7147 produced_chars = coding->consumed_char;
df7492f9 7148 while (src < src_end)
14daee73 7149 *dst++ = *src++;
fa42c37f
KH
7150 }
7151 }
7152
df7492f9 7153 produced = dst - (coding->destination + coding->produced);
284201e4 7154 if (BUFFERP (coding->dst_object) && produced_chars > 0)
8a44e6d1 7155 insert_from_gap (produced_chars, produced, 0);
df7492f9
KH
7156 coding->produced += produced;
7157 coding->produced_char += produced_chars;
69a80ea3 7158 return carryover;
fa42c37f
KH
7159}
7160
ff0dacd7
KH
7161/* Compose text in CODING->object according to the annotation data at
7162 CHARBUF. CHARBUF is an array:
e951386e 7163 [ -LENGTH ANNOTATION_MASK NCHARS NBYTES METHOD [ COMPONENTS... ] ]
df7492f9 7164 */
4ed46869 7165
b0ab8123 7166static void
d311d28c 7167produce_composition (struct coding_system *coding, int *charbuf, ptrdiff_t pos)
4ed46869 7168{
df7492f9 7169 int len;
d311d28c 7170 ptrdiff_t to;
df7492f9 7171 enum composition_method method;
df7492f9 7172 Lisp_Object components;
fa42c37f 7173
e951386e 7174 len = -charbuf[0] - MAX_ANNOTATION_LENGTH;
69a80ea3 7175 to = pos + charbuf[2];
e951386e 7176 method = (enum composition_method) (charbuf[4]);
d46c5b12 7177
df7492f9
KH
7178 if (method == COMPOSITION_RELATIVE)
7179 components = Qnil;
e951386e 7180 else
d46c5b12 7181 {
df7492f9 7182 Lisp_Object args[MAX_COMPOSITION_COMPONENTS * 2 - 1];
e951386e 7183 int i, j;
b73bfc1c 7184
e951386e
KH
7185 if (method == COMPOSITION_WITH_RULE)
7186 len = charbuf[2] * 3 - 2;
7187 charbuf += MAX_ANNOTATION_LENGTH;
7188 /* charbuf = [ CHRA ... CHAR] or [ CHAR -2 RULE ... CHAR ] */
7189 for (i = j = 0; i < len && charbuf[i] != -1; i++, j++)
9ffd559c 7190 {
e951386e
KH
7191 if (charbuf[i] >= 0)
7192 args[j] = make_number (charbuf[i]);
7193 else
7194 {
7195 i++;
7196 args[j] = make_number (charbuf[i] % 0x100);
7197 }
9ffd559c 7198 }
e951386e 7199 components = (i == j ? Fstring (j, args) : Fvector (j, args));
d46c5b12 7200 }
69a80ea3 7201 compose_text (pos, to, components, Qnil, coding->dst_object);
d46c5b12
KH
7202}
7203
d46c5b12 7204
ff0dacd7
KH
7205/* Put `charset' property on text in CODING->object according to
7206 the annotation data at CHARBUF. CHARBUF is an array:
69a80ea3 7207 [ -LENGTH ANNOTATION_MASK NCHARS CHARSET-ID ]
ff0dacd7 7208 */
d46c5b12 7209
b0ab8123 7210static void
d311d28c 7211produce_charset (struct coding_system *coding, int *charbuf, ptrdiff_t pos)
d46c5b12 7212{
d311d28c 7213 ptrdiff_t from = pos - charbuf[2];
69a80ea3 7214 struct charset *charset = CHARSET_FROM_ID (charbuf[3]);
b73bfc1c 7215
69a80ea3 7216 Fput_text_property (make_number (from), make_number (pos),
ff0dacd7
KH
7217 Qcharset, CHARSET_NAME (charset),
7218 coding->dst_object);
d46c5b12
KH
7219}
7220
d46c5b12 7221
df7492f9
KH
7222#define CHARBUF_SIZE 0x4000
7223
7224#define ALLOC_CONVERSION_WORK_AREA(coding) \
7225 do { \
1af1a51a
DA
7226 coding->charbuf = SAFE_ALLOCA (CHARBUF_SIZE * sizeof (int)); \
7227 coding->charbuf_size = CHARBUF_SIZE; \
df7492f9 7228 } while (0)
4ed46869 7229
d46c5b12
KH
7230
7231static void
d311d28c 7232produce_annotation (struct coding_system *coding, ptrdiff_t pos)
d46c5b12 7233{
df7492f9
KH
7234 int *charbuf = coding->charbuf;
7235 int *charbuf_end = charbuf + coding->charbuf_used;
d46c5b12 7236
ff0dacd7
KH
7237 if (NILP (coding->dst_object))
7238 return;
d46c5b12 7239
df7492f9 7240 while (charbuf < charbuf_end)
a84f1519 7241 {
df7492f9 7242 if (*charbuf >= 0)
e951386e 7243 pos++, charbuf++;
d46c5b12 7244 else
d46c5b12 7245 {
df7492f9 7246 int len = -*charbuf;
e951386e
KH
7247
7248 if (len > 2)
7249 switch (charbuf[1])
7250 {
7251 case CODING_ANNOTATE_COMPOSITION_MASK:
7252 produce_composition (coding, charbuf, pos);
7253 break;
7254 case CODING_ANNOTATE_CHARSET_MASK:
7255 produce_charset (coding, charbuf, pos);
7256 break;
7257 }
df7492f9 7258 charbuf += len;
d46c5b12 7259 }
a84f1519 7260 }
d46c5b12
KH
7261}
7262
df7492f9
KH
7263/* Decode the data at CODING->src_object into CODING->dst_object.
7264 CODING->src_object is a buffer, a string, or nil.
7265 CODING->dst_object is a buffer.
d46c5b12 7266
df7492f9
KH
7267 If CODING->src_object is a buffer, it must be the current buffer.
7268 In this case, if CODING->src_pos is positive, it is a position of
7269 the source text in the buffer, otherwise, the source text is in the
7270 gap area of the buffer, and CODING->src_pos specifies the offset of
7271 the text from GPT (which must be the same as PT). If this is the
7272 same buffer as CODING->dst_object, CODING->src_pos must be
7273 negative.
d46c5b12 7274
b6828792 7275 If CODING->src_object is a string, CODING->src_pos is an index to
df7492f9 7276 that string.
d46c5b12 7277
df7492f9
KH
7278 If CODING->src_object is nil, CODING->source must already point to
7279 the non-relocatable memory area. In this case, CODING->src_pos is
7280 an offset from CODING->source.
73be902c 7281
df7492f9
KH
7282 The decoded data is inserted at the current point of the buffer
7283 CODING->dst_object.
7284*/
d46c5b12 7285
f10fe38f 7286static void
971de7fb 7287decode_coding (struct coding_system *coding)
d46c5b12 7288{
df7492f9 7289 Lisp_Object attrs;
24a73b0a 7290 Lisp_Object undo_list;
7d64c6ad 7291 Lisp_Object translation_table;
d0396581 7292 struct ccl_spec cclspec;
69a80ea3
KH
7293 int carryover;
7294 int i;
d46c5b12 7295
1af1a51a
DA
7296 USE_SAFE_ALLOCA;
7297
df7492f9
KH
7298 if (BUFFERP (coding->src_object)
7299 && coding->src_pos > 0
7300 && coding->src_pos < GPT
7301 && coding->src_pos + coding->src_chars > GPT)
7302 move_gap_both (coding->src_pos, coding->src_pos_byte);
d46c5b12 7303
24a73b0a 7304 undo_list = Qt;
df7492f9 7305 if (BUFFERP (coding->dst_object))
1c3478b0 7306 {
a3d794a1 7307 set_buffer_internal (XBUFFER (coding->dst_object));
df7492f9
KH
7308 if (GPT != PT)
7309 move_gap_both (PT, PT_BYTE);
f48b82fd
GR
7310
7311 /* We must disable undo_list in order to record the whole insert
7312 transaction via record_insert at the end. But doing so also
7313 disables the recording of the first change to the undo_list.
7314 Therefore we check for first change here and record it via
7315 record_first_change if needed. */
7316 if (MODIFF <= SAVE_MODIFF)
7317 record_first_change ();
7318
4b4deea2 7319 undo_list = BVAR (current_buffer, undo_list);
39eb03f1 7320 bset_undo_list (current_buffer, Qt);
1c3478b0
KH
7321 }
7322
df7492f9
KH
7323 coding->consumed = coding->consumed_char = 0;
7324 coding->produced = coding->produced_char = 0;
7325 coding->chars_at_source = 0;
065e3595 7326 record_conversion_result (coding, CODING_RESULT_SUCCESS);
df7492f9 7327 coding->errors = 0;
1c3478b0 7328
df7492f9
KH
7329 ALLOC_CONVERSION_WORK_AREA (coding);
7330
7331 attrs = CODING_ID_ATTRS (coding->id);
2170c8f0 7332 translation_table = get_translation_table (attrs, 0, NULL);
df7492f9 7333
69a80ea3 7334 carryover = 0;
d0396581
KH
7335 if (coding->decoder == decode_coding_ccl)
7336 {
7337 coding->spec.ccl = &cclspec;
7338 setup_ccl_program (&cclspec.ccl, CODING_CCL_DECODER (coding));
7339 }
df7492f9 7340 do
b73bfc1c 7341 {
d311d28c 7342 ptrdiff_t pos = coding->dst_pos + coding->produced_char;
69a80ea3 7343
df7492f9
KH
7344 coding_set_source (coding);
7345 coding->annotated = 0;
69a80ea3 7346 coding->charbuf_used = carryover;
df7492f9 7347 (*(coding->decoder)) (coding);
df7492f9 7348 coding_set_destination (coding);
69a80ea3 7349 carryover = produce_chars (coding, translation_table, 0);
df7492f9 7350 if (coding->annotated)
69a80ea3
KH
7351 produce_annotation (coding, pos);
7352 for (i = 0; i < carryover; i++)
7353 coding->charbuf[i]
7354 = coding->charbuf[coding->charbuf_used - carryover + i];
d46c5b12 7355 }
d0396581
KH
7356 while (coding->result == CODING_RESULT_INSUFFICIENT_DST
7357 || (coding->consumed < coding->src_bytes
7358 && (coding->result == CODING_RESULT_SUCCESS
7359 || coding->result == CODING_RESULT_INVALID_SRC)));
d46c5b12 7360
69a80ea3
KH
7361 if (carryover > 0)
7362 {
7363 coding_set_destination (coding);
7364 coding->charbuf_used = carryover;
7365 produce_chars (coding, translation_table, 1);
7366 }
7367
df7492f9
KH
7368 coding->carryover_bytes = 0;
7369 if (coding->consumed < coding->src_bytes)
d46c5b12 7370 {
df7492f9 7371 int nbytes = coding->src_bytes - coding->consumed;
8f924df7 7372 const unsigned char *src;
df7492f9
KH
7373
7374 coding_set_source (coding);
7375 coding_set_destination (coding);
7376 src = coding->source + coding->consumed;
7377
7378 if (coding->mode & CODING_MODE_LAST_BLOCK)
1c3478b0 7379 {
df7492f9
KH
7380 /* Flush out unprocessed data as binary chars. We are sure
7381 that the number of data is less than the size of
7382 coding->charbuf. */
065e3595 7383 coding->charbuf_used = 0;
b2dab6c8
JR
7384 coding->chars_at_source = 0;
7385
df7492f9 7386 while (nbytes-- > 0)
1c3478b0 7387 {
df7492f9 7388 int c = *src++;
98725083 7389
1c91457d
KH
7390 if (c & 0x80)
7391 c = BYTE8_TO_CHAR (c);
7392 coding->charbuf[coding->charbuf_used++] = c;
1c3478b0 7393 }
f6cbaf43 7394 produce_chars (coding, Qnil, 1);
d46c5b12 7395 }
d46c5b12 7396 else
df7492f9
KH
7397 {
7398 /* Record unprocessed bytes in coding->carryover. We are
7399 sure that the number of data is less than the size of
7400 coding->carryover. */
7401 unsigned char *p = coding->carryover;
7402
f289d375
KH
7403 if (nbytes > sizeof coding->carryover)
7404 nbytes = sizeof coding->carryover;
df7492f9
KH
7405 coding->carryover_bytes = nbytes;
7406 while (nbytes-- > 0)
7407 *p++ = *src++;
1c3478b0 7408 }
df7492f9 7409 coding->consumed = coding->src_bytes;
b73bfc1c 7410 }
69f76525 7411
0a9564cb
EZ
7412 if (! EQ (CODING_ID_EOL_TYPE (coding->id), Qunix)
7413 && !inhibit_eol_conversion)
4347441b 7414 decode_eol (coding);
24a73b0a
KH
7415 if (BUFFERP (coding->dst_object))
7416 {
39eb03f1 7417 bset_undo_list (current_buffer, undo_list);
24a73b0a
KH
7418 record_insert (coding->dst_pos, coding->produced_char);
7419 }
1af1a51a
DA
7420
7421 SAFE_FREE ();
4ed46869
KH
7422}
7423
aaaf0b1e 7424
e1c23804 7425/* Extract an annotation datum from a composition starting at POS and
ff0dacd7
KH
7426 ending before LIMIT of CODING->src_object (buffer or string), store
7427 the data in BUF, set *STOP to a starting position of the next
7428 composition (if any) or to LIMIT, and return the address of the
7429 next element of BUF.
7430
7431 If such an annotation is not found, set *STOP to a starting
7432 position of a composition after POS (if any) or to LIMIT, and
7433 return BUF. */
7434
b0ab8123 7435static int *
d311d28c 7436handle_composition_annotation (ptrdiff_t pos, ptrdiff_t limit,
cf84bb53 7437 struct coding_system *coding, int *buf,
d311d28c 7438 ptrdiff_t *stop)
aaaf0b1e 7439{
d311d28c 7440 ptrdiff_t start, end;
ff0dacd7 7441 Lisp_Object prop;
aaaf0b1e 7442
ff0dacd7
KH
7443 if (! find_composition (pos, limit, &start, &end, &prop, coding->src_object)
7444 || end > limit)
7445 *stop = limit;
7446 else if (start > pos)
7447 *stop = start;
7448 else
aaaf0b1e 7449 {
ff0dacd7 7450 if (start == pos)
aaaf0b1e 7451 {
ff0dacd7
KH
7452 /* We found a composition. Store the corresponding
7453 annotation data in BUF. */
7454 int *head = buf;
7455 enum composition_method method = COMPOSITION_METHOD (prop);
7456 int nchars = COMPOSITION_LENGTH (prop);
7457
e951386e 7458 ADD_COMPOSITION_DATA (buf, nchars, 0, method);
ff0dacd7 7459 if (method != COMPOSITION_RELATIVE)
aaaf0b1e 7460 {
ff0dacd7 7461 Lisp_Object components;
2c6a9faa 7462 ptrdiff_t i, len, i_byte;
ff0dacd7
KH
7463
7464 components = COMPOSITION_COMPONENTS (prop);
7465 if (VECTORP (components))
aaaf0b1e 7466 {
77b37c05 7467 len = ASIZE (components);
ff0dacd7
KH
7468 for (i = 0; i < len; i++)
7469 *buf++ = XINT (AREF (components, i));
aaaf0b1e 7470 }
ff0dacd7 7471 else if (STRINGP (components))
aaaf0b1e 7472 {
8f924df7 7473 len = SCHARS (components);
ff0dacd7
KH
7474 i = i_byte = 0;
7475 while (i < len)
7476 {
7477 FETCH_STRING_CHAR_ADVANCE (*buf, components, i, i_byte);
7478 buf++;
7479 }
7480 }
7481 else if (INTEGERP (components))
7482 {
7483 len = 1;
7484 *buf++ = XINT (components);
7485 }
7486 else if (CONSP (components))
7487 {
7488 for (len = 0; CONSP (components);
7489 len++, components = XCDR (components))
7490 *buf++ = XINT (XCAR (components));
aaaf0b1e 7491 }
aaaf0b1e 7492 else
1088b922 7493 emacs_abort ();
ff0dacd7 7494 *head -= len;
aaaf0b1e 7495 }
aaaf0b1e 7496 }
ff0dacd7
KH
7497
7498 if (find_composition (end, limit, &start, &end, &prop,
7499 coding->src_object)
7500 && end <= limit)
7501 *stop = start;
7502 else
7503 *stop = limit;
aaaf0b1e 7504 }
ff0dacd7
KH
7505 return buf;
7506}
7507
7508
e1c23804 7509/* Extract an annotation datum from a text property `charset' at POS of
ff0dacd7
KH
7510 CODING->src_object (buffer of string), store the data in BUF, set
7511 *STOP to the position where the value of `charset' property changes
7512 (limiting by LIMIT), and return the address of the next element of
7513 BUF.
7514
7515 If the property value is nil, set *STOP to the position where the
7516 property value is non-nil (limiting by LIMIT), and return BUF. */
7517
b0ab8123 7518static int *
d311d28c 7519handle_charset_annotation (ptrdiff_t pos, ptrdiff_t limit,
cf84bb53 7520 struct coding_system *coding, int *buf,
d311d28c 7521 ptrdiff_t *stop)
ff0dacd7
KH
7522{
7523 Lisp_Object val, next;
7524 int id;
7525
7526 val = Fget_text_property (make_number (pos), Qcharset, coding->src_object);
7527 if (! NILP (val) && CHARSETP (val))
7528 id = XINT (CHARSET_SYMBOL_ID (val));
7529 else
7530 id = -1;
69a80ea3 7531 ADD_CHARSET_DATA (buf, 0, id);
ff0dacd7
KH
7532 next = Fnext_single_property_change (make_number (pos), Qcharset,
7533 coding->src_object,
7534 make_number (limit));
7535 *stop = XINT (next);
7536 return buf;
7537}
7538
7539
df7492f9 7540static void
cf84bb53
JB
7541consume_chars (struct coding_system *coding, Lisp_Object translation_table,
7542 int max_lookup)
df7492f9
KH
7543{
7544 int *buf = coding->charbuf;
ff0dacd7 7545 int *buf_end = coding->charbuf + coding->charbuf_size;
7c78e542 7546 const unsigned char *src = coding->source + coding->consumed;
4776e638 7547 const unsigned char *src_end = coding->source + coding->src_bytes;
d311d28c
PE
7548 ptrdiff_t pos = coding->src_pos + coding->consumed_char;
7549 ptrdiff_t end_pos = coding->src_pos + coding->src_chars;
f10fe38f 7550 bool multibytep = coding->src_multibyte;
df7492f9
KH
7551 Lisp_Object eol_type;
7552 int c;
d311d28c 7553 ptrdiff_t stop, stop_composition, stop_charset;
09ee6fdd 7554 int *lookup_buf = NULL;
433f7f87
KH
7555
7556 if (! NILP (translation_table))
09ee6fdd 7557 lookup_buf = alloca (sizeof (int) * max_lookup);
88993dfd 7558
0a9564cb 7559 eol_type = inhibit_eol_conversion ? Qunix : CODING_ID_EOL_TYPE (coding->id);
df7492f9
KH
7560 if (VECTORP (eol_type))
7561 eol_type = Qunix;
88993dfd 7562
df7492f9
KH
7563 /* Note: composition handling is not yet implemented. */
7564 coding->common_flags &= ~CODING_ANNOTATE_COMPOSITION_MASK;
ec6d2bb8 7565
0b5670c9
KH
7566 if (NILP (coding->src_object))
7567 stop = stop_composition = stop_charset = end_pos;
ff0dacd7 7568 else
0b5670c9
KH
7569 {
7570 if (coding->common_flags & CODING_ANNOTATE_COMPOSITION_MASK)
7571 stop = stop_composition = pos;
7572 else
7573 stop = stop_composition = end_pos;
7574 if (coding->common_flags & CODING_ANNOTATE_CHARSET_MASK)
7575 stop = stop_charset = pos;
7576 else
7577 stop_charset = end_pos;
7578 }
ec6d2bb8 7579
24a73b0a 7580 /* Compensate for CRLF and conversion. */
ff0dacd7 7581 buf_end -= 1 + MAX_ANNOTATION_LENGTH;
df7492f9 7582 while (buf < buf_end)
aaaf0b1e 7583 {
433f7f87
KH
7584 Lisp_Object trans;
7585
df7492f9 7586 if (pos == stop)
ec6d2bb8 7587 {
df7492f9
KH
7588 if (pos == end_pos)
7589 break;
ff0dacd7
KH
7590 if (pos == stop_composition)
7591 buf = handle_composition_annotation (pos, end_pos, coding,
7592 buf, &stop_composition);
7593 if (pos == stop_charset)
7594 buf = handle_charset_annotation (pos, end_pos, coding,
7595 buf, &stop_charset);
7596 stop = (stop_composition < stop_charset
7597 ? stop_composition : stop_charset);
df7492f9
KH
7598 }
7599
7600 if (! multibytep)
4776e638 7601 {
d311d28c 7602 int bytes;
aaaf0b1e 7603
4d1e6632
KH
7604 if (coding->encoder == encode_coding_raw_text
7605 || coding->encoder == encode_coding_ccl)
ea29edf2
KH
7606 c = *src++, pos++;
7607 else if ((bytes = MULTIBYTE_LENGTH (src, src_end)) > 0)
db274c7a 7608 c = STRING_CHAR_ADVANCE_NO_UNIFY (src), pos += bytes;
4776e638 7609 else
f03caae0 7610 c = BYTE8_TO_CHAR (*src), src++, pos++;
4776e638 7611 }
df7492f9 7612 else
db274c7a 7613 c = STRING_CHAR_ADVANCE_NO_UNIFY (src), pos++;
df7492f9
KH
7614 if ((c == '\r') && (coding->mode & CODING_MODE_SELECTIVE_DISPLAY))
7615 c = '\n';
7616 if (! EQ (eol_type, Qunix))
aaaf0b1e 7617 {
df7492f9 7618 if (c == '\n')
aaaf0b1e 7619 {
df7492f9
KH
7620 if (EQ (eol_type, Qdos))
7621 *buf++ = '\r';
7622 else
7623 c = '\r';
aaaf0b1e
KH
7624 }
7625 }
433f7f87 7626
e6a54062 7627 trans = Qnil;
09ee6fdd 7628 LOOKUP_TRANSLATION_TABLE (translation_table, c, trans);
e6a54062 7629 if (NILP (trans))
433f7f87
KH
7630 *buf++ = c;
7631 else
7632 {
2c6a9faa 7633 ptrdiff_t from_nchars = 1, to_nchars = 1;
433f7f87
KH
7634 int *lookup_buf_end;
7635 const unsigned char *p = src;
7636 int i;
7637
7638 lookup_buf[0] = c;
7639 for (i = 1; i < max_lookup && p < src_end; i++)
7640 lookup_buf[i] = STRING_CHAR_ADVANCE (p);
7641 lookup_buf_end = lookup_buf + i;
e951386e
KH
7642 trans = get_translation (trans, lookup_buf, lookup_buf_end);
7643 if (INTEGERP (trans))
7644 c = XINT (trans);
7645 else if (CONSP (trans))
7646 {
7647 from_nchars = ASIZE (XCAR (trans));
7648 trans = XCDR (trans);
7649 if (INTEGERP (trans))
7650 c = XINT (trans);
7651 else
7652 {
7653 to_nchars = ASIZE (trans);
2c6a9faa 7654 if (buf_end - buf < to_nchars)
e951386e
KH
7655 break;
7656 c = XINT (AREF (trans, 0));
7657 }
7658 }
7659 else
433f7f87 7660 break;
e951386e 7661 *buf++ = c;
433f7f87
KH
7662 for (i = 1; i < to_nchars; i++)
7663 *buf++ = XINT (AREF (trans, i));
7664 for (i = 1; i < from_nchars; i++, pos++)
7665 src += MULTIBYTE_LENGTH_NO_CHECK (src);
7666 }
aaaf0b1e 7667 }
ec6d2bb8 7668
df7492f9
KH
7669 coding->consumed = src - coding->source;
7670 coding->consumed_char = pos - coding->src_pos;
7671 coding->charbuf_used = buf - coding->charbuf;
7672 coding->chars_at_source = 0;
aaaf0b1e
KH
7673}
7674
4ed46869 7675
df7492f9
KH
7676/* Encode the text at CODING->src_object into CODING->dst_object.
7677 CODING->src_object is a buffer or a string.
7678 CODING->dst_object is a buffer or nil.
7679
7680 If CODING->src_object is a buffer, it must be the current buffer.
7681 In this case, if CODING->src_pos is positive, it is a position of
7682 the source text in the buffer, otherwise. the source text is in the
7683 gap area of the buffer, and coding->src_pos specifies the offset of
7684 the text from GPT (which must be the same as PT). If this is the
7685 same buffer as CODING->dst_object, CODING->src_pos must be
7686 negative and CODING should not have `pre-write-conversion'.
7687
7688 If CODING->src_object is a string, CODING should not have
7689 `pre-write-conversion'.
7690
7691 If CODING->dst_object is a buffer, the encoded data is inserted at
7692 the current point of that buffer.
7693
7694 If CODING->dst_object is nil, the encoded data is placed at the
7695 memory area specified by CODING->destination. */
7696
f10fe38f 7697static void
971de7fb 7698encode_coding (struct coding_system *coding)
4ed46869 7699{
df7492f9 7700 Lisp_Object attrs;
7d64c6ad 7701 Lisp_Object translation_table;
09ee6fdd 7702 int max_lookup;
fb608df3 7703 struct ccl_spec cclspec;
9861e777 7704
1af1a51a
DA
7705 USE_SAFE_ALLOCA;
7706
df7492f9 7707 attrs = CODING_ID_ATTRS (coding->id);
ea29edf2
KH
7708 if (coding->encoder == encode_coding_raw_text)
7709 translation_table = Qnil, max_lookup = 0;
7710 else
7711 translation_table = get_translation_table (attrs, 1, &max_lookup);
4ed46869 7712
df7492f9 7713 if (BUFFERP (coding->dst_object))
8844fa83 7714 {
df7492f9
KH
7715 set_buffer_internal (XBUFFER (coding->dst_object));
7716 coding->dst_multibyte
4b4deea2 7717 = ! NILP (BVAR (current_buffer, enable_multibyte_characters));
8844fa83 7718 }
4ed46869 7719
b73bfc1c 7720 coding->consumed = coding->consumed_char = 0;
df7492f9 7721 coding->produced = coding->produced_char = 0;
065e3595 7722 record_conversion_result (coding, CODING_RESULT_SUCCESS);
b73bfc1c 7723 coding->errors = 0;
b73bfc1c 7724
df7492f9 7725 ALLOC_CONVERSION_WORK_AREA (coding);
4ed46869 7726
fb608df3
KH
7727 if (coding->encoder == encode_coding_ccl)
7728 {
7729 coding->spec.ccl = &cclspec;
7730 setup_ccl_program (&cclspec.ccl, CODING_CCL_ENCODER (coding));
7731 }
df7492f9
KH
7732 do {
7733 coding_set_source (coding);
09ee6fdd 7734 consume_chars (coding, translation_table, max_lookup);
df7492f9
KH
7735 coding_set_destination (coding);
7736 (*(coding->encoder)) (coding);
7737 } while (coding->consumed_char < coding->src_chars);
7738
284201e4 7739 if (BUFFERP (coding->dst_object) && coding->produced_char > 0)
8a44e6d1 7740 insert_from_gap (coding->produced_char, coding->produced, 0);
1af1a51a
DA
7741
7742 SAFE_FREE ();
ec6d2bb8
KH
7743}
7744
fb88bf2d 7745
24a73b0a
KH
7746/* Name (or base name) of work buffer for code conversion. */
7747static Lisp_Object Vcode_conversion_workbuf_name;
d46c5b12 7748
24a73b0a
KH
7749/* A working buffer used by the top level conversion. Once it is
7750 created, it is never destroyed. It has the name
7751 Vcode_conversion_workbuf_name. The other working buffers are
7752 destroyed after the use is finished, and their names are modified
7753 versions of Vcode_conversion_workbuf_name. */
7754static Lisp_Object Vcode_conversion_reused_workbuf;
b73bfc1c 7755
f10fe38f
PE
7756/* True iff Vcode_conversion_reused_workbuf is already in use. */
7757static bool reused_workbuf_in_use;
4ed46869 7758
24a73b0a 7759
ad1746f5 7760/* Return a working buffer of code conversion. MULTIBYTE specifies the
24a73b0a 7761 multibyteness of returning buffer. */
b73bfc1c 7762
f6cbaf43 7763static Lisp_Object
f10fe38f 7764make_conversion_work_buffer (bool multibyte)
df7492f9 7765{
24a73b0a
KH
7766 Lisp_Object name, workbuf;
7767 struct buffer *current;
4ed46869 7768
f10fe38f 7769 if (reused_workbuf_in_use)
065e3595
KH
7770 {
7771 name = Fgenerate_new_buffer_name (Vcode_conversion_workbuf_name, Qnil);
7772 workbuf = Fget_buffer_create (name);
7773 }
df7492f9 7774 else
065e3595 7775 {
f10fe38f 7776 reused_workbuf_in_use = 1;
159bd5a2 7777 if (NILP (Fbuffer_live_p (Vcode_conversion_reused_workbuf)))
a993c7a1
KH
7778 Vcode_conversion_reused_workbuf
7779 = Fget_buffer_create (Vcode_conversion_workbuf_name);
7780 workbuf = Vcode_conversion_reused_workbuf;
065e3595 7781 }
24a73b0a
KH
7782 current = current_buffer;
7783 set_buffer_internal (XBUFFER (workbuf));
df36ff1f
CY
7784 /* We can't allow modification hooks to run in the work buffer. For
7785 instance, directory_files_internal assumes that file decoding
7786 doesn't compile new regexps. */
7787 Fset (Fmake_local_variable (Qinhibit_modification_hooks), Qt);
3ed051d4 7788 Ferase_buffer ();
39eb03f1
PE
7789 bset_undo_list (current_buffer, Qt);
7790 bset_enable_multibyte_characters (current_buffer, multibyte ? Qt : Qnil);
df7492f9 7791 set_buffer_internal (current);
24a73b0a 7792 return workbuf;
df7492f9 7793}
d46c5b12 7794
24a73b0a 7795
4776e638 7796static Lisp_Object
971de7fb 7797code_conversion_restore (Lisp_Object arg)
4776e638 7798{
24a73b0a 7799 Lisp_Object current, workbuf;
948bdcf3 7800 struct gcpro gcpro1;
24a73b0a 7801
948bdcf3 7802 GCPRO1 (arg);
24a73b0a
KH
7803 current = XCAR (arg);
7804 workbuf = XCDR (arg);
7805 if (! NILP (workbuf))
7806 {
7807 if (EQ (workbuf, Vcode_conversion_reused_workbuf))
7808 reused_workbuf_in_use = 0;
d17337e5 7809 else
24a73b0a
KH
7810 Fkill_buffer (workbuf);
7811 }
7812 set_buffer_internal (XBUFFER (current));
948bdcf3 7813 UNGCPRO;
4776e638
KH
7814 return Qnil;
7815}
b73bfc1c 7816
24a73b0a 7817Lisp_Object
f10fe38f 7818code_conversion_save (bool with_work_buf, bool multibyte)
df7492f9 7819{
24a73b0a 7820 Lisp_Object workbuf = Qnil;
b73bfc1c 7821
4776e638 7822 if (with_work_buf)
24a73b0a
KH
7823 workbuf = make_conversion_work_buffer (multibyte);
7824 record_unwind_protect (code_conversion_restore,
7825 Fcons (Fcurrent_buffer (), workbuf));
4776e638 7826 return workbuf;
df7492f9 7827}
d46c5b12 7828
f10fe38f 7829void
cf84bb53 7830decode_coding_gap (struct coding_system *coding,
d311d28c 7831 ptrdiff_t chars, ptrdiff_t bytes)
df7492f9 7832{
d311d28c 7833 ptrdiff_t count = SPECPDL_INDEX ();
5e5c78be 7834 Lisp_Object attrs;
fb88bf2d 7835
24a73b0a 7836 coding->src_object = Fcurrent_buffer ();
df7492f9
KH
7837 coding->src_chars = chars;
7838 coding->src_bytes = bytes;
7839 coding->src_pos = -chars;
7840 coding->src_pos_byte = -bytes;
7841 coding->src_multibyte = chars < bytes;
24a73b0a 7842 coding->dst_object = coding->src_object;
df7492f9
KH
7843 coding->dst_pos = PT;
7844 coding->dst_pos_byte = PT_BYTE;
4b4deea2 7845 coding->dst_multibyte = ! NILP (BVAR (current_buffer, enable_multibyte_characters));
4ed46869 7846
e6d2f155
KH
7847 coding->head_ascii = -1;
7848 coding->detected_utf8_chars = -1;
7849 coding->eol_seen = EOL_SEEN_NONE;
df7492f9
KH
7850 if (CODING_REQUIRE_DETECTION (coding))
7851 detect_coding (coding);
c230dd7d 7852 attrs = CODING_ID_ATTRS (coding->id);
251e9147
KH
7853 if (! disable_ascii_optimization
7854 && ! coding->src_multibyte
7855 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs))
7856 && NILP (CODING_ATTR_POST_READ (attrs))
7857 && NILP (get_translation_table (attrs, 0, NULL)))
7858 {
7859 chars = coding->head_ascii;
7860 if (chars < 0)
7861 chars = check_ascii (coding);
7862 if (chars != bytes)
7863 {
e6d2f155 7864 /* There exists a non-ASCII byte. */
251e9147 7865 if (EQ (CODING_ATTR_TYPE (attrs), Qutf_8))
e6d2f155
KH
7866 {
7867 if (coding->detected_utf8_chars >= 0)
7868 chars = coding->detected_utf8_chars;
7869 else
7870 chars = check_utf_8 (coding);
7871 if (CODING_UTF_8_BOM (coding) != utf_without_bom
7872 && coding->head_ascii == 0
7873 && coding->source[0] == UTF_8_BOM_1
7874 && coding->source[1] == UTF_8_BOM_2
7875 && coding->source[2] == UTF_8_BOM_3)
7876 {
7877 chars--;
7878 bytes -= 3;
7879 coding->src_bytes -= 3;
7880 }
7881 }
251e9147
KH
7882 else
7883 chars = -1;
7884 }
7885 if (chars >= 0)
c230dd7d 7886 {
e6d2f155 7887 Lisp_Object eol_type;
251e9147 7888
e6d2f155
KH
7889 eol_type = CODING_ID_EOL_TYPE (coding->id);
7890 if (VECTORP (eol_type))
7891 {
7892 if (coding->eol_seen != EOL_SEEN_NONE)
7893 eol_type = adjust_coding_eol_type (coding, coding->eol_seen);
7894 }
7895 if (EQ (eol_type, Qmac))
8a44e6d1
KH
7896 {
7897 unsigned char *src_end = GAP_END_ADDR;
cded56c1 7898 unsigned char *src = src_end - coding->src_bytes;
c230dd7d 7899
8a44e6d1
KH
7900 while (src < src_end)
7901 {
7902 if (*src++ == '\r')
7903 src[-1] = '\n';
7904 }
7905 }
e6d2f155 7906 else if (EQ (eol_type, Qdos))
c230dd7d 7907 {
8a44e6d1
KH
7908 unsigned char *src = GAP_END_ADDR;
7909 unsigned char *src_beg = src - coding->src_bytes;
7910 unsigned char *dst = src;
251e9147 7911 ptrdiff_t diff;
8a44e6d1
KH
7912
7913 while (src_beg < src)
7914 {
7915 *--dst = *--src;
9e63b4a5 7916 if (*src == '\n' && src > src_beg && src[-1] == '\r')
8a44e6d1
KH
7917 src--;
7918 }
251e9147
KH
7919 diff = dst - src;
7920 bytes -= diff;
7921 chars -= diff;
c230dd7d 7922 }
251e9147
KH
7923 coding->produced = bytes;
7924 coding->produced_char = chars;
7925 insert_from_gap (chars, bytes, 1);
c230dd7d
KH
7926 return;
7927 }
7928 }
c230dd7d 7929 code_conversion_save (0, 0);
8f924df7 7930
9286b333 7931 coding->mode |= CODING_MODE_LAST_BLOCK;
287c57d7 7932 current_buffer->text->inhibit_shrinking = 1;
df7492f9 7933 decode_coding (coding);
287c57d7 7934 current_buffer->text->inhibit_shrinking = 0;
d46c5b12 7935
5e5c78be 7936 if (! NILP (CODING_ATTR_POST_READ (attrs)))
b73bfc1c 7937 {
d311d28c 7938 ptrdiff_t prev_Z = Z, prev_Z_BYTE = Z_BYTE;
5e5c78be
KH
7939 Lisp_Object val;
7940
7941 TEMP_SET_PT_BOTH (coding->dst_pos, coding->dst_pos_byte);
5e5c78be
KH
7942 val = call1 (CODING_ATTR_POST_READ (attrs),
7943 make_number (coding->produced_char));
5e5c78be
KH
7944 CHECK_NATNUM (val);
7945 coding->produced_char += Z - prev_Z;
7946 coding->produced += Z_BYTE - prev_Z_BYTE;
b73bfc1c 7947 }
4ed46869 7948
df7492f9 7949 unbind_to (count, Qnil);
b73bfc1c 7950}
52d41803 7951
d46c5b12 7952
df7492f9
KH
7953/* Decode the text in the range FROM/FROM_BYTE and TO/TO_BYTE in
7954 SRC_OBJECT into DST_OBJECT by coding context CODING.
b73bfc1c 7955
df7492f9 7956 SRC_OBJECT is a buffer, a string, or Qnil.
b73bfc1c 7957
df7492f9
KH
7958 If it is a buffer, the text is at point of the buffer. FROM and TO
7959 are positions in the buffer.
b73bfc1c 7960
df7492f9
KH
7961 If it is a string, the text is at the beginning of the string.
7962 FROM and TO are indices to the string.
4ed46869 7963
df7492f9
KH
7964 If it is nil, the text is at coding->source. FROM and TO are
7965 indices to coding->source.
bb10be8b 7966
df7492f9 7967 DST_OBJECT is a buffer, Qt, or Qnil.
4ed46869 7968
df7492f9
KH
7969 If it is a buffer, the decoded text is inserted at point of the
7970 buffer. If the buffer is the same as SRC_OBJECT, the source text
7971 is deleted.
4ed46869 7972
df7492f9
KH
7973 If it is Qt, a string is made from the decoded text, and
7974 set in CODING->dst_object.
d46c5b12 7975
df7492f9 7976 If it is Qnil, the decoded text is stored at CODING->destination.
2cb26057 7977 The caller must allocate CODING->dst_bytes bytes at
df7492f9
KH
7978 CODING->destination by xmalloc. If the decoded text is longer than
7979 CODING->dst_bytes, CODING->destination is relocated by xrealloc.
7980 */
d46c5b12 7981
df7492f9 7982void
cf84bb53
JB
7983decode_coding_object (struct coding_system *coding,
7984 Lisp_Object src_object,
d311d28c
PE
7985 ptrdiff_t from, ptrdiff_t from_byte,
7986 ptrdiff_t to, ptrdiff_t to_byte,
cf84bb53 7987 Lisp_Object dst_object)
d46c5b12 7988{
d311d28c 7989 ptrdiff_t count = SPECPDL_INDEX ();
c4a63b12 7990 unsigned char *destination IF_LINT (= NULL);
d311d28c
PE
7991 ptrdiff_t dst_bytes IF_LINT (= 0);
7992 ptrdiff_t chars = to - from;
7993 ptrdiff_t bytes = to_byte - from_byte;
df7492f9 7994 Lisp_Object attrs;
f10fe38f
PE
7995 ptrdiff_t saved_pt = -1, saved_pt_byte IF_LINT (= 0);
7996 bool need_marker_adjustment = 0;
b3bfad50 7997 Lisp_Object old_deactivate_mark;
d46c5b12 7998
b3bfad50 7999 old_deactivate_mark = Vdeactivate_mark;
93dec019 8000
df7492f9 8001 if (NILP (dst_object))
d46c5b12 8002 {
df7492f9
KH
8003 destination = coding->destination;
8004 dst_bytes = coding->dst_bytes;
d46c5b12 8005 }
93dec019 8006
df7492f9
KH
8007 coding->src_object = src_object;
8008 coding->src_chars = chars;
8009 coding->src_bytes = bytes;
8010 coding->src_multibyte = chars < bytes;
70ad9fc4 8011
df7492f9 8012 if (STRINGP (src_object))
d46c5b12 8013 {
df7492f9
KH
8014 coding->src_pos = from;
8015 coding->src_pos_byte = from_byte;
d46c5b12 8016 }
df7492f9 8017 else if (BUFFERP (src_object))
88993dfd 8018 {
df7492f9
KH
8019 set_buffer_internal (XBUFFER (src_object));
8020 if (from != GPT)
8021 move_gap_both (from, from_byte);
8022 if (EQ (src_object, dst_object))
fb88bf2d 8023 {
64cedb0c
KH
8024 struct Lisp_Marker *tail;
8025
8026 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
8027 {
8028 tail->need_adjustment
8029 = tail->charpos == (tail->insertion_type ? from : to);
8030 need_marker_adjustment |= tail->need_adjustment;
8031 }
4776e638 8032 saved_pt = PT, saved_pt_byte = PT_BYTE;
df7492f9 8033 TEMP_SET_PT_BOTH (from, from_byte);
f4a3cc44 8034 current_buffer->text->inhibit_shrinking = 1;
df7492f9
KH
8035 del_range_both (from, from_byte, to, to_byte, 1);
8036 coding->src_pos = -chars;
8037 coding->src_pos_byte = -bytes;
fb88bf2d 8038 }
df7492f9 8039 else
fb88bf2d 8040 {
df7492f9
KH
8041 coding->src_pos = from;
8042 coding->src_pos_byte = from_byte;
fb88bf2d 8043 }
88993dfd
KH
8044 }
8045
df7492f9
KH
8046 if (CODING_REQUIRE_DETECTION (coding))
8047 detect_coding (coding);
8048 attrs = CODING_ID_ATTRS (coding->id);
d46c5b12 8049
2cb26057
KH
8050 if (EQ (dst_object, Qt)
8051 || (! NILP (CODING_ATTR_POST_READ (attrs))
8052 && NILP (dst_object)))
b73bfc1c 8053 {
a1567c45
SM
8054 coding->dst_multibyte = !CODING_FOR_UNIBYTE (coding);
8055 coding->dst_object = code_conversion_save (1, coding->dst_multibyte);
df7492f9
KH
8056 coding->dst_pos = BEG;
8057 coding->dst_pos_byte = BEG_BYTE;
b73bfc1c 8058 }
df7492f9 8059 else if (BUFFERP (dst_object))
d46c5b12 8060 {
24a73b0a 8061 code_conversion_save (0, 0);
df7492f9
KH
8062 coding->dst_object = dst_object;
8063 coding->dst_pos = BUF_PT (XBUFFER (dst_object));
8064 coding->dst_pos_byte = BUF_PT_BYTE (XBUFFER (dst_object));
8065 coding->dst_multibyte
4b4deea2 8066 = ! NILP (BVAR (XBUFFER (dst_object), enable_multibyte_characters));
d46c5b12
KH
8067 }
8068 else
8069 {
24a73b0a 8070 code_conversion_save (0, 0);
df7492f9 8071 coding->dst_object = Qnil;
0154725e
SM
8072 /* Most callers presume this will return a multibyte result, and they
8073 won't use `binary' or `raw-text' anyway, so let's not worry about
8074 CODING_FOR_UNIBYTE. */
bb555731 8075 coding->dst_multibyte = 1;
d46c5b12
KH
8076 }
8077
df7492f9 8078 decode_coding (coding);
fa46990e 8079
df7492f9
KH
8080 if (BUFFERP (coding->dst_object))
8081 set_buffer_internal (XBUFFER (coding->dst_object));
d46c5b12 8082
df7492f9 8083 if (! NILP (CODING_ATTR_POST_READ (attrs)))
d46c5b12 8084 {
b3bfad50 8085 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
d311d28c 8086 ptrdiff_t prev_Z = Z, prev_Z_BYTE = Z_BYTE;
2b4f9037 8087 Lisp_Object val;
d46c5b12 8088
c0cc7f7f 8089 TEMP_SET_PT_BOTH (coding->dst_pos, coding->dst_pos_byte);
b3bfad50
KH
8090 GCPRO5 (coding->src_object, coding->dst_object, src_object, dst_object,
8091 old_deactivate_mark);
d4850d67
KH
8092 val = safe_call1 (CODING_ATTR_POST_READ (attrs),
8093 make_number (coding->produced_char));
df7492f9
KH
8094 UNGCPRO;
8095 CHECK_NATNUM (val);
8096 coding->produced_char += Z - prev_Z;
8097 coding->produced += Z_BYTE - prev_Z_BYTE;
d46c5b12 8098 }
de79a6a5 8099
df7492f9 8100 if (EQ (dst_object, Qt))
ec6d2bb8 8101 {
df7492f9
KH
8102 coding->dst_object = Fbuffer_string ();
8103 }
8104 else if (NILP (dst_object) && BUFFERP (coding->dst_object))
8105 {
8106 set_buffer_internal (XBUFFER (coding->dst_object));
8107 if (dst_bytes < coding->produced)
8108 {
1af1a51a 8109 eassert (coding->produced > 0);
b3bfad50 8110 destination = xrealloc (destination, coding->produced);
df7492f9
KH
8111 if (BEGV < GPT && GPT < BEGV + coding->produced_char)
8112 move_gap_both (BEGV, BEGV_BYTE);
72af86bd 8113 memcpy (destination, BEGV_ADDR, coding->produced);
df7492f9 8114 coding->destination = destination;
d46c5b12 8115 }
ec6d2bb8 8116 }
b73bfc1c 8117
4776e638
KH
8118 if (saved_pt >= 0)
8119 {
8120 /* This is the case of:
8121 (BUFFERP (src_object) && EQ (src_object, dst_object))
8122 As we have moved PT while replacing the original buffer
8123 contents, we must recover it now. */
8124 set_buffer_internal (XBUFFER (src_object));
f4a3cc44 8125 current_buffer->text->inhibit_shrinking = 0;
4776e638
KH
8126 if (saved_pt < from)
8127 TEMP_SET_PT_BOTH (saved_pt, saved_pt_byte);
8128 else if (saved_pt < from + chars)
8129 TEMP_SET_PT_BOTH (from, from_byte);
4b4deea2 8130 else if (! NILP (BVAR (current_buffer, enable_multibyte_characters)))
4776e638
KH
8131 TEMP_SET_PT_BOTH (saved_pt + (coding->produced_char - chars),
8132 saved_pt_byte + (coding->produced - bytes));
8133 else
8134 TEMP_SET_PT_BOTH (saved_pt + (coding->produced - bytes),
8135 saved_pt_byte + (coding->produced - bytes));
64cedb0c
KH
8136
8137 if (need_marker_adjustment)
8138 {
8139 struct Lisp_Marker *tail;
8140
8141 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
8142 if (tail->need_adjustment)
8143 {
8144 tail->need_adjustment = 0;
8145 if (tail->insertion_type)
8146 {
8147 tail->bytepos = from_byte;
8148 tail->charpos = from;
8149 }
8150 else
8151 {
8152 tail->bytepos = from_byte + coding->produced;
8153 tail->charpos
4b4deea2 8154 = (NILP (BVAR (current_buffer, enable_multibyte_characters))
64cedb0c
KH
8155 ? tail->bytepos : from + coding->produced_char);
8156 }
8157 }
8158 }
d46c5b12 8159 }
4776e638 8160
b3bfad50 8161 Vdeactivate_mark = old_deactivate_mark;
065e3595 8162 unbind_to (count, coding->dst_object);
d46c5b12
KH
8163}
8164
d46c5b12 8165
df7492f9 8166void
cf84bb53
JB
8167encode_coding_object (struct coding_system *coding,
8168 Lisp_Object src_object,
d311d28c
PE
8169 ptrdiff_t from, ptrdiff_t from_byte,
8170 ptrdiff_t to, ptrdiff_t to_byte,
cf84bb53 8171 Lisp_Object dst_object)
d46c5b12 8172{
d311d28c
PE
8173 ptrdiff_t count = SPECPDL_INDEX ();
8174 ptrdiff_t chars = to - from;
8175 ptrdiff_t bytes = to_byte - from_byte;
df7492f9 8176 Lisp_Object attrs;
f10fe38f
PE
8177 ptrdiff_t saved_pt = -1, saved_pt_byte IF_LINT (= 0);
8178 bool need_marker_adjustment = 0;
8179 bool kill_src_buffer = 0;
b3bfad50 8180 Lisp_Object old_deactivate_mark;
df7492f9 8181
b3bfad50 8182 old_deactivate_mark = Vdeactivate_mark;
df7492f9
KH
8183
8184 coding->src_object = src_object;
8185 coding->src_chars = chars;
8186 coding->src_bytes = bytes;
8187 coding->src_multibyte = chars < bytes;
8188
8189 attrs = CODING_ID_ATTRS (coding->id);
8190
64cedb0c
KH
8191 if (EQ (src_object, dst_object))
8192 {
8193 struct Lisp_Marker *tail;
8194
8195 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
8196 {
8197 tail->need_adjustment
8198 = tail->charpos == (tail->insertion_type ? from : to);
8199 need_marker_adjustment |= tail->need_adjustment;
8200 }
8201 }
8202
df7492f9 8203 if (! NILP (CODING_ATTR_PRE_WRITE (attrs)))
6bac5b12 8204 {
24a73b0a 8205 coding->src_object = code_conversion_save (1, coding->src_multibyte);
df7492f9
KH
8206 set_buffer_internal (XBUFFER (coding->src_object));
8207 if (STRINGP (src_object))
8208 insert_from_string (src_object, from, from_byte, chars, bytes, 0);
8209 else if (BUFFERP (src_object))
8210 insert_from_buffer (XBUFFER (src_object), from, chars, 0);
8211 else
b68864e5 8212 insert_1_both ((char *) coding->source + from, chars, bytes, 0, 0, 0);
d46c5b12 8213
df7492f9
KH
8214 if (EQ (src_object, dst_object))
8215 {
8216 set_buffer_internal (XBUFFER (src_object));
4776e638 8217 saved_pt = PT, saved_pt_byte = PT_BYTE;
df7492f9
KH
8218 del_range_both (from, from_byte, to, to_byte, 1);
8219 set_buffer_internal (XBUFFER (coding->src_object));
8220 }
8221
d4850d67 8222 {
b3bfad50 8223 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
d4850d67 8224
b3bfad50
KH
8225 GCPRO5 (coding->src_object, coding->dst_object, src_object, dst_object,
8226 old_deactivate_mark);
6cd7a139
DA
8227 safe_call2 (CODING_ATTR_PRE_WRITE (attrs),
8228 make_number (BEG), make_number (Z));
b3bfad50 8229 UNGCPRO;
d4850d67 8230 }
c02d943b
KH
8231 if (XBUFFER (coding->src_object) != current_buffer)
8232 kill_src_buffer = 1;
ac87bbef 8233 coding->src_object = Fcurrent_buffer ();
df7492f9
KH
8234 if (BEG != GPT)
8235 move_gap_both (BEG, BEG_BYTE);
8236 coding->src_chars = Z - BEG;
8237 coding->src_bytes = Z_BYTE - BEG_BYTE;
8238 coding->src_pos = BEG;
8239 coding->src_pos_byte = BEG_BYTE;
8240 coding->src_multibyte = Z < Z_BYTE;
8241 }
8242 else if (STRINGP (src_object))
d46c5b12 8243 {
24a73b0a 8244 code_conversion_save (0, 0);
df7492f9
KH
8245 coding->src_pos = from;
8246 coding->src_pos_byte = from_byte;
b73bfc1c 8247 }
df7492f9 8248 else if (BUFFERP (src_object))
b73bfc1c 8249 {
24a73b0a 8250 code_conversion_save (0, 0);
df7492f9 8251 set_buffer_internal (XBUFFER (src_object));
df7492f9 8252 if (EQ (src_object, dst_object))
d46c5b12 8253 {
4776e638 8254 saved_pt = PT, saved_pt_byte = PT_BYTE;
ff0dacd7
KH
8255 coding->src_object = del_range_1 (from, to, 1, 1);
8256 coding->src_pos = 0;
8257 coding->src_pos_byte = 0;
d46c5b12 8258 }
df7492f9 8259 else
d46c5b12 8260 {
ff0dacd7
KH
8261 if (from < GPT && to >= GPT)
8262 move_gap_both (from, from_byte);
df7492f9
KH
8263 coding->src_pos = from;
8264 coding->src_pos_byte = from_byte;
d46c5b12 8265 }
d46c5b12 8266 }
4776e638 8267 else
24a73b0a 8268 code_conversion_save (0, 0);
d46c5b12 8269
df7492f9 8270 if (BUFFERP (dst_object))
88993dfd 8271 {
df7492f9 8272 coding->dst_object = dst_object;
28f67a95
KH
8273 if (EQ (src_object, dst_object))
8274 {
8275 coding->dst_pos = from;
8276 coding->dst_pos_byte = from_byte;
8277 }
8278 else
8279 {
319a3947
KH
8280 struct buffer *current = current_buffer;
8281
8282 set_buffer_temp (XBUFFER (dst_object));
8283 coding->dst_pos = PT;
8284 coding->dst_pos_byte = PT_BYTE;
8285 move_gap_both (coding->dst_pos, coding->dst_pos_byte);
8286 set_buffer_temp (current);
28f67a95 8287 }
df7492f9 8288 coding->dst_multibyte
4b4deea2 8289 = ! NILP (BVAR (XBUFFER (dst_object), enable_multibyte_characters));
88993dfd 8290 }
df7492f9 8291 else if (EQ (dst_object, Qt))
d46c5b12 8292 {
5d009b3a 8293 ptrdiff_t dst_bytes = max (1, coding->src_chars);
df7492f9 8294 coding->dst_object = Qnil;
23f86fce 8295 coding->destination = xmalloc (dst_bytes);
5d009b3a 8296 coding->dst_bytes = dst_bytes;
df7492f9 8297 coding->dst_multibyte = 0;
d46c5b12
KH
8298 }
8299 else
8300 {
df7492f9
KH
8301 coding->dst_object = Qnil;
8302 coding->dst_multibyte = 0;
d46c5b12
KH
8303 }
8304
df7492f9 8305 encode_coding (coding);
d46c5b12 8306
df7492f9 8307 if (EQ (dst_object, Qt))
d46c5b12 8308 {
df7492f9
KH
8309 if (BUFFERP (coding->dst_object))
8310 coding->dst_object = Fbuffer_string ();
8311 else
d46c5b12 8312 {
df7492f9
KH
8313 coding->dst_object
8314 = make_unibyte_string ((char *) coding->destination,
8315 coding->produced);
8316 xfree (coding->destination);
d46c5b12 8317 }
4ed46869 8318 }
d46c5b12 8319
4776e638
KH
8320 if (saved_pt >= 0)
8321 {
8322 /* This is the case of:
8323 (BUFFERP (src_object) && EQ (src_object, dst_object))
8324 As we have moved PT while replacing the original buffer
8325 contents, we must recover it now. */
8326 set_buffer_internal (XBUFFER (src_object));
8327 if (saved_pt < from)
8328 TEMP_SET_PT_BOTH (saved_pt, saved_pt_byte);
8329 else if (saved_pt < from + chars)
8330 TEMP_SET_PT_BOTH (from, from_byte);
4b4deea2 8331 else if (! NILP (BVAR (current_buffer, enable_multibyte_characters)))
4776e638
KH
8332 TEMP_SET_PT_BOTH (saved_pt + (coding->produced_char - chars),
8333 saved_pt_byte + (coding->produced - bytes));
d46c5b12 8334 else
4776e638
KH
8335 TEMP_SET_PT_BOTH (saved_pt + (coding->produced - bytes),
8336 saved_pt_byte + (coding->produced - bytes));
64cedb0c
KH
8337
8338 if (need_marker_adjustment)
8339 {
8340 struct Lisp_Marker *tail;
8341
8342 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
8343 if (tail->need_adjustment)
8344 {
8345 tail->need_adjustment = 0;
8346 if (tail->insertion_type)
8347 {
8348 tail->bytepos = from_byte;
8349 tail->charpos = from;
8350 }
8351 else
8352 {
8353 tail->bytepos = from_byte + coding->produced;
8354 tail->charpos
4b4deea2 8355 = (NILP (BVAR (current_buffer, enable_multibyte_characters))
64cedb0c
KH
8356 ? tail->bytepos : from + coding->produced_char);
8357 }
8358 }
8359 }
4776e638
KH
8360 }
8361
c02d943b
KH
8362 if (kill_src_buffer)
8363 Fkill_buffer (coding->src_object);
b3bfad50
KH
8364
8365 Vdeactivate_mark = old_deactivate_mark;
df7492f9 8366 unbind_to (count, Qnil);
b73bfc1c
KH
8367}
8368
df7492f9 8369
b73bfc1c 8370Lisp_Object
971de7fb 8371preferred_coding_system (void)
b73bfc1c 8372{
df7492f9 8373 int id = coding_categories[coding_priorities[0]].id;
2391eaa4 8374
df7492f9 8375 return CODING_ID_NAME (id);
4ed46869
KH
8376}
8377
7f590b0c 8378#if defined (WINDOWSNT) || defined (CYGWIN)
ba116008
DC
8379
8380Lisp_Object
8381from_unicode (Lisp_Object str)
8382{
8383 CHECK_STRING (str);
8384 if (!STRING_MULTIBYTE (str) &&
8385 SBYTES (str) & 1)
8386 {
8387 str = Fsubstring (str, make_number (0), make_number (-1));
8388 }
8389
8390 return code_convert_string_norecord (str, Qutf_16le, 0);
8391}
8392
819e2da9
DC
8393Lisp_Object
8394from_unicode_buffer (const wchar_t* wstr)
8395{
8396 return from_unicode (
8397 make_unibyte_string (
8398 (char*) wstr,
8399 /* we get one of the two final 0 bytes for free. */
8400 1 + sizeof (wchar_t) * wcslen (wstr)));
8401}
8402
ba116008
DC
8403wchar_t *
8404to_unicode (Lisp_Object str, Lisp_Object *buf)
8405{
8406 *buf = code_convert_string_norecord (str, Qutf_16le, 1);
bc989a58 8407 /* We need to make another copy (in addition to the one made by
ba116008
DC
8408 code_convert_string_norecord) to ensure that the final string is
8409 _doubly_ zero terminated --- that is, that the string is
8410 terminated by two zero bytes and one utf-16le null character.
8411 Because strings are already terminated with a single zero byte,
8412 we just add one additional zero. */
8413 str = make_uninit_string (SBYTES (*buf) + 1);
8414 memcpy (SDATA (str), SDATA (*buf), SBYTES (*buf));
8415 SDATA (str) [SBYTES (*buf)] = '\0';
8416 *buf = str;
8417 return WCSDATA (*buf);
8418}
7f590b0c
DC
8419
8420#endif /* WINDOWSNT || CYGWIN */
ba116008 8421
4ed46869
KH
8422\f
8423#ifdef emacs
1397dc18 8424/*** 8. Emacs Lisp library functions ***/
4ed46869 8425
a7ca3326 8426DEFUN ("coding-system-p", Fcoding_system_p, Scoding_system_p, 1, 1, 0,
48b0f3ae 8427 doc: /* Return t if OBJECT is nil or a coding-system.
df7492f9 8428See the documentation of `define-coding-system' for information
48b0f3ae 8429about coding-system objects. */)
5842a27b 8430 (Lisp_Object object)
4ed46869 8431{
d4a1d553
JB
8432 if (NILP (object)
8433 || CODING_SYSTEM_ID (object) >= 0)
44e8490d 8434 return Qt;
d4a1d553
JB
8435 if (! SYMBOLP (object)
8436 || NILP (Fget (object, Qcoding_system_define_form)))
44e8490d
KH
8437 return Qnil;
8438 return Qt;
4ed46869
KH
8439}
8440
a7ca3326 8441DEFUN ("read-non-nil-coding-system", Fread_non_nil_coding_system,
9d991de8 8442 Sread_non_nil_coding_system, 1, 1, 0,
48b0f3ae 8443 doc: /* Read a coding system from the minibuffer, prompting with string PROMPT. */)
5842a27b 8444 (Lisp_Object prompt)
4ed46869 8445{
e0e989f6 8446 Lisp_Object val;
9d991de8
RS
8447 do
8448 {
4608c386
KH
8449 val = Fcompleting_read (prompt, Vcoding_system_alist, Qnil,
8450 Qt, Qnil, Qcoding_system_history, Qnil, Qnil);
9d991de8 8451 }
8f924df7 8452 while (SCHARS (val) == 0);
e0e989f6 8453 return (Fintern (val, Qnil));
4ed46869
KH
8454}
8455
a7ca3326 8456DEFUN ("read-coding-system", Fread_coding_system, Sread_coding_system, 1, 2, 0,
48b0f3ae 8457 doc: /* Read a coding system from the minibuffer, prompting with string PROMPT.
c7183fb8
GM
8458If the user enters null input, return second argument DEFAULT-CODING-SYSTEM.
8459Ignores case when completing coding systems (all Emacs coding systems
8460are lower-case). */)
5842a27b 8461 (Lisp_Object prompt, Lisp_Object default_coding_system)
4ed46869 8462{
f44d27ce 8463 Lisp_Object val;
d311d28c 8464 ptrdiff_t count = SPECPDL_INDEX ();
c7183fb8 8465
9b787f3e 8466 if (SYMBOLP (default_coding_system))
57d25e6f 8467 default_coding_system = SYMBOL_NAME (default_coding_system);
c7183fb8 8468 specbind (Qcompletion_ignore_case, Qt);
4608c386 8469 val = Fcompleting_read (prompt, Vcoding_system_alist, Qnil,
9b787f3e
RS
8470 Qt, Qnil, Qcoding_system_history,
8471 default_coding_system, Qnil);
c7183fb8 8472 unbind_to (count, Qnil);
8f924df7 8473 return (SCHARS (val) == 0 ? Qnil : Fintern (val, Qnil));
4ed46869
KH
8474}
8475
a7ca3326 8476DEFUN ("check-coding-system", Fcheck_coding_system, Scheck_coding_system,
4ed46869 8477 1, 1, 0,
48b0f3ae 8478 doc: /* Check validity of CODING-SYSTEM.
9ffd559c
KH
8479If valid, return CODING-SYSTEM, else signal a `coding-system-error' error.
8480It is valid if it is nil or a symbol defined as a coding system by the
8481function `define-coding-system'. */)
5842a27b 8482 (Lisp_Object coding_system)
4ed46869 8483{
44e8490d
KH
8484 Lisp_Object define_form;
8485
8486 define_form = Fget (coding_system, Qcoding_system_define_form);
8487 if (! NILP (define_form))
8488 {
8489 Fput (coding_system, Qcoding_system_define_form, Qnil);
8490 safe_eval (define_form);
8491 }
4ed46869
KH
8492 if (!NILP (Fcoding_system_p (coding_system)))
8493 return coding_system;
fcad4ec4 8494 xsignal1 (Qcoding_system_error, coding_system);
4ed46869 8495}
df7492f9 8496
3a73fa5d 8497\f
89528eb3 8498/* Detect how the bytes at SRC of length SRC_BYTES are encoded. If
f10fe38f 8499 HIGHEST, return the coding system of the highest
ad1746f5 8500 priority among the detected coding systems. Otherwise return a
89528eb3 8501 list of detected coding systems sorted by their priorities. If
f10fe38f 8502 MULTIBYTEP, it is assumed that the bytes are in correct
89528eb3
KH
8503 multibyte form but contains only ASCII and eight-bit chars.
8504 Otherwise, the bytes are raw bytes.
8505
8506 CODING-SYSTEM controls the detection as below:
8507
8508 If it is nil, detect both text-format and eol-format. If the
8509 text-format part of CODING-SYSTEM is already specified
8510 (e.g. `iso-latin-1'), detect only eol-format. If the eol-format
8511 part of CODING-SYSTEM is already specified (e.g. `undecided-unix'),
8512 detect only text-format. */
8513
d46c5b12 8514Lisp_Object
cf84bb53 8515detect_coding_system (const unsigned char *src,
d311d28c 8516 ptrdiff_t src_chars, ptrdiff_t src_bytes,
f10fe38f 8517 bool highest, bool multibytep,
cf84bb53 8518 Lisp_Object coding_system)
4ed46869 8519{
8f924df7 8520 const unsigned char *src_end = src + src_bytes;
df7492f9 8521 Lisp_Object attrs, eol_type;
4533845d 8522 Lisp_Object val = Qnil;
df7492f9 8523 struct coding_system coding;
d3411f89 8524 ptrdiff_t id;
ff0dacd7 8525 struct coding_detection_info detect_info;
24a73b0a 8526 enum coding_category base_category;
f10fe38f 8527 bool null_byte_found = 0, eight_bit_found = 0;
b73bfc1c 8528
df7492f9
KH
8529 if (NILP (coding_system))
8530 coding_system = Qundecided;
8531 setup_coding_system (coding_system, &coding);
8532 attrs = CODING_ID_ATTRS (coding.id);
8533 eol_type = CODING_ID_EOL_TYPE (coding.id);
89528eb3 8534 coding_system = CODING_ATTR_BASE_NAME (attrs);
4ed46869 8535
df7492f9 8536 coding.source = src;
24a73b0a 8537 coding.src_chars = src_chars;
df7492f9
KH
8538 coding.src_bytes = src_bytes;
8539 coding.src_multibyte = multibytep;
8540 coding.consumed = 0;
89528eb3 8541 coding.mode |= CODING_MODE_LAST_BLOCK;
c0e16b14 8542 coding.head_ascii = 0;
d46c5b12 8543
ff0dacd7 8544 detect_info.checked = detect_info.found = detect_info.rejected = 0;
d46c5b12 8545
89528eb3 8546 /* At first, detect text-format if necessary. */
24a73b0a
KH
8547 base_category = XINT (CODING_ATTR_CATEGORY (attrs));
8548 if (base_category == coding_category_undecided)
4ed46869 8549 {
c4a63b12
PE
8550 enum coding_category category IF_LINT (= 0);
8551 struct coding_system *this IF_LINT (= NULL);
ff0dacd7 8552 int c, i;
9c90cc06
PE
8553 bool inhibit_nbd = inhibit_flag (coding.spec.undecided.inhibit_nbd,
8554 inhibit_null_byte_detection);
8555 bool inhibit_ied = inhibit_flag (coding.spec.undecided.inhibit_ied,
8556 inhibit_iso_escape_detection);
8557 bool prefer_utf_8 = coding.spec.undecided.prefer_utf_8;
88993dfd 8558
24a73b0a 8559 /* Skip all ASCII bytes except for a few ISO2022 controls. */
2f3cbb32 8560 for (; src < src_end; src++)
4ed46869 8561 {
df7492f9 8562 c = *src;
6cb21a4f 8563 if (c & 0x80)
6cb21a4f 8564 {
2f3cbb32 8565 eight_bit_found = 1;
2f3cbb32
KH
8566 if (null_byte_found)
8567 break;
8568 }
c0e16b14 8569 else if (c < 0x20)
2f3cbb32
KH
8570 {
8571 if ((c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO)
c3a7b3f4 8572 && ! inhibit_ied
2f3cbb32 8573 && ! detect_info.checked)
6cb21a4f 8574 {
2f3cbb32
KH
8575 if (detect_coding_iso_2022 (&coding, &detect_info))
8576 {
8577 /* We have scanned the whole data. */
8578 if (! (detect_info.rejected & CATEGORY_MASK_ISO_7_ELSE))
c0e16b14
KH
8579 {
8580 /* We didn't find an 8-bit code. We may
8581 have found a null-byte, but it's very
8582 rare that a binary file confirm to
8583 ISO-2022. */
8584 src = src_end;
8585 coding.head_ascii = src - coding.source;
8586 }
8587 detect_info.rejected |= ~CATEGORY_MASK_ISO_ESCAPE;
2f3cbb32
KH
8588 break;
8589 }
8590 }
c3a7b3f4 8591 else if (! c && !inhibit_nbd)
2f3cbb32
KH
8592 {
8593 null_byte_found = 1;
8594 if (eight_bit_found)
8595 break;
6cb21a4f 8596 }
c006c0c8
KH
8597 if (! eight_bit_found)
8598 coding.head_ascii++;
6cb21a4f 8599 }
c006c0c8 8600 else if (! eight_bit_found)
c0e16b14 8601 coding.head_ascii++;
4ed46869 8602 }
88993dfd 8603
2f3cbb32
KH
8604 if (null_byte_found || eight_bit_found
8605 || coding.head_ascii < coding.src_bytes
6cb21a4f
KH
8606 || detect_info.found)
8607 {
2f3cbb32 8608 if (coding.head_ascii == coding.src_bytes)
6cb21a4f
KH
8609 /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */
8610 for (i = 0; i < coding_category_raw_text; i++)
ff0dacd7 8611 {
6cb21a4f 8612 category = coding_priorities[i];
c7266f4a 8613 this = coding_categories + category;
6cb21a4f 8614 if (detect_info.found & (1 << category))
ff0dacd7
KH
8615 break;
8616 }
6cb21a4f 8617 else
2f3cbb32
KH
8618 {
8619 if (null_byte_found)
8620 {
8621 detect_info.checked |= ~CATEGORY_MASK_UTF_16;
8622 detect_info.rejected |= ~CATEGORY_MASK_UTF_16;
8623 }
c3a7b3f4
KH
8624 else if (prefer_utf_8
8625 && detect_coding_utf_8 (&coding, &detect_info))
8626 {
8627 detect_info.checked |= ~CATEGORY_MASK_UTF_8;
8628 detect_info.rejected |= ~CATEGORY_MASK_UTF_8;
8629 }
2f3cbb32
KH
8630 for (i = 0; i < coding_category_raw_text; i++)
8631 {
8632 category = coding_priorities[i];
8633 this = coding_categories + category;
6cb21a4f 8634
2f3cbb32
KH
8635 if (this->id < 0)
8636 {
8637 /* No coding system of this category is defined. */
8638 detect_info.rejected |= (1 << category);
8639 }
8640 else if (category >= coding_category_raw_text)
8641 continue;
8642 else if (detect_info.checked & (1 << category))
8643 {
8644 if (highest
8645 && (detect_info.found & (1 << category)))
6cb21a4f 8646 break;
2f3cbb32
KH
8647 }
8648 else if ((*(this->detector)) (&coding, &detect_info)
8649 && highest
8650 && (detect_info.found & (1 << category)))
8651 {
8652 if (category == coding_category_utf_16_auto)
8653 {
8654 if (detect_info.found & CATEGORY_MASK_UTF_16_LE)
8655 category = coding_category_utf_16_le;
8656 else
8657 category = coding_category_utf_16_be;
8658 }
8659 break;
8660 }
8661 }
8662 }
6cb21a4f 8663 }
ec6d2bb8 8664
4cddb209
KH
8665 if ((detect_info.rejected & CATEGORY_MASK_ANY) == CATEGORY_MASK_ANY
8666 || null_byte_found)
ec6d2bb8 8667 {
ff0dacd7 8668 detect_info.found = CATEGORY_MASK_RAW_TEXT;
4cddb209 8669 id = CODING_SYSTEM_ID (Qno_conversion);
89528eb3
KH
8670 val = Fcons (make_number (id), Qnil);
8671 }
ff0dacd7 8672 else if (! detect_info.rejected && ! detect_info.found)
89528eb3 8673 {
ff0dacd7 8674 detect_info.found = CATEGORY_MASK_ANY;
89528eb3
KH
8675 id = coding_categories[coding_category_undecided].id;
8676 val = Fcons (make_number (id), Qnil);
8677 }
8678 else if (highest)
8679 {
ff0dacd7 8680 if (detect_info.found)
ec6d2bb8 8681 {
ff0dacd7
KH
8682 detect_info.found = 1 << category;
8683 val = Fcons (make_number (this->id), Qnil);
8684 }
8685 else
8686 for (i = 0; i < coding_category_raw_text; i++)
8687 if (! (detect_info.rejected & (1 << coding_priorities[i])))
8688 {
8689 detect_info.found = 1 << coding_priorities[i];
8690 id = coding_categories[coding_priorities[i]].id;
8691 val = Fcons (make_number (id), Qnil);
8692 break;
8693 }
8694 }
89528eb3
KH
8695 else
8696 {
ff0dacd7
KH
8697 int mask = detect_info.rejected | detect_info.found;
8698 int found = 0;
ec6d2bb8 8699
89528eb3 8700 for (i = coding_category_raw_text - 1; i >= 0; i--)
ff0dacd7
KH
8701 {
8702 category = coding_priorities[i];
8703 if (! (mask & (1 << category)))
ec6d2bb8 8704 {
ff0dacd7
KH
8705 found |= 1 << category;
8706 id = coding_categories[category].id;
c7266f4a
KH
8707 if (id >= 0)
8708 val = Fcons (make_number (id), val);
ff0dacd7
KH
8709 }
8710 }
8711 for (i = coding_category_raw_text - 1; i >= 0; i--)
8712 {
8713 category = coding_priorities[i];
8714 if (detect_info.found & (1 << category))
8715 {
8716 id = coding_categories[category].id;
8717 val = Fcons (make_number (id), val);
ec6d2bb8 8718 }
ec6d2bb8 8719 }
ff0dacd7 8720 detect_info.found |= found;
ec6d2bb8 8721 }
ec6d2bb8 8722 }
a470d443
KH
8723 else if (base_category == coding_category_utf_8_auto)
8724 {
8725 if (detect_coding_utf_8 (&coding, &detect_info))
8726 {
8727 struct coding_system *this;
8728
8729 if (detect_info.found & CATEGORY_MASK_UTF_8_SIG)
8730 this = coding_categories + coding_category_utf_8_sig;
8731 else
8732 this = coding_categories + coding_category_utf_8_nosig;
8733 val = Fcons (make_number (this->id), Qnil);
8734 }
8735 }
24a73b0a
KH
8736 else if (base_category == coding_category_utf_16_auto)
8737 {
8738 if (detect_coding_utf_16 (&coding, &detect_info))
8739 {
24a73b0a
KH
8740 struct coding_system *this;
8741
8742 if (detect_info.found & CATEGORY_MASK_UTF_16_LE)
8743 this = coding_categories + coding_category_utf_16_le;
8744 else if (detect_info.found & CATEGORY_MASK_UTF_16_BE)
8745 this = coding_categories + coding_category_utf_16_be;
8746 else if (detect_info.rejected & CATEGORY_MASK_UTF_16_LE_NOSIG)
8747 this = coding_categories + coding_category_utf_16_be_nosig;
8748 else
8749 this = coding_categories + coding_category_utf_16_le_nosig;
8750 val = Fcons (make_number (this->id), Qnil);
8751 }
8752 }
df7492f9
KH
8753 else
8754 {
ff0dacd7 8755 detect_info.found = 1 << XINT (CODING_ATTR_CATEGORY (attrs));
89528eb3 8756 val = Fcons (make_number (coding.id), Qnil);
4ed46869 8757 }
df7492f9 8758
89528eb3 8759 /* Then, detect eol-format if necessary. */
df7492f9 8760 {
4533845d 8761 int normal_eol = -1, utf_16_be_eol = -1, utf_16_le_eol = -1;
df7492f9
KH
8762 Lisp_Object tail;
8763
89528eb3
KH
8764 if (VECTORP (eol_type))
8765 {
ff0dacd7 8766 if (detect_info.found & ~CATEGORY_MASK_UTF_16)
2f3cbb32
KH
8767 {
8768 if (null_byte_found)
8769 normal_eol = EOL_SEEN_LF;
8770 else
8771 normal_eol = detect_eol (coding.source, src_bytes,
8772 coding_category_raw_text);
8773 }
ff0dacd7
KH
8774 if (detect_info.found & (CATEGORY_MASK_UTF_16_BE
8775 | CATEGORY_MASK_UTF_16_BE_NOSIG))
89528eb3
KH
8776 utf_16_be_eol = detect_eol (coding.source, src_bytes,
8777 coding_category_utf_16_be);
ff0dacd7
KH
8778 if (detect_info.found & (CATEGORY_MASK_UTF_16_LE
8779 | CATEGORY_MASK_UTF_16_LE_NOSIG))
89528eb3
KH
8780 utf_16_le_eol = detect_eol (coding.source, src_bytes,
8781 coding_category_utf_16_le);
8782 }
8783 else
8784 {
8785 if (EQ (eol_type, Qunix))
8786 normal_eol = utf_16_be_eol = utf_16_le_eol = EOL_SEEN_LF;
8787 else if (EQ (eol_type, Qdos))
8788 normal_eol = utf_16_be_eol = utf_16_le_eol = EOL_SEEN_CRLF;
8789 else
8790 normal_eol = utf_16_be_eol = utf_16_le_eol = EOL_SEEN_CR;
8791 }
8792
df7492f9
KH
8793 for (tail = val; CONSP (tail); tail = XCDR (tail))
8794 {
89528eb3 8795 enum coding_category category;
df7492f9 8796 int this_eol;
89528eb3
KH
8797
8798 id = XINT (XCAR (tail));
8799 attrs = CODING_ID_ATTRS (id);
8800 category = XINT (CODING_ATTR_CATEGORY (attrs));
8801 eol_type = CODING_ID_EOL_TYPE (id);
df7492f9
KH
8802 if (VECTORP (eol_type))
8803 {
89528eb3
KH
8804 if (category == coding_category_utf_16_be
8805 || category == coding_category_utf_16_be_nosig)
8806 this_eol = utf_16_be_eol;
8807 else if (category == coding_category_utf_16_le
8808 || category == coding_category_utf_16_le_nosig)
8809 this_eol = utf_16_le_eol;
df7492f9 8810 else
89528eb3
KH
8811 this_eol = normal_eol;
8812
df7492f9
KH
8813 if (this_eol == EOL_SEEN_LF)
8814 XSETCAR (tail, AREF (eol_type, 0));
8815 else if (this_eol == EOL_SEEN_CRLF)
8816 XSETCAR (tail, AREF (eol_type, 1));
8817 else if (this_eol == EOL_SEEN_CR)
8818 XSETCAR (tail, AREF (eol_type, 2));
89528eb3
KH
8819 else
8820 XSETCAR (tail, CODING_ID_NAME (id));
df7492f9 8821 }
89528eb3
KH
8822 else
8823 XSETCAR (tail, CODING_ID_NAME (id));
df7492f9
KH
8824 }
8825 }
ec6d2bb8 8826
4533845d 8827 return (highest ? (CONSP (val) ? XCAR (val) : Qnil) : val);
ec6d2bb8
KH
8828}
8829
ec6d2bb8 8830
d46c5b12
KH
8831DEFUN ("detect-coding-region", Fdetect_coding_region, Sdetect_coding_region,
8832 2, 3, 0,
48b0f3ae
PJ
8833 doc: /* Detect coding system of the text in the region between START and END.
8834Return a list of possible coding systems ordered by priority.
b811c52b
KH
8835The coding systems to try and their priorities follows what
8836the function `coding-system-priority-list' (which see) returns.
ec6d2bb8 8837
12e0131a 8838If only ASCII characters are found (except for such ISO-2022 control
d4a1d553
JB
8839characters as ESC), it returns a list of single element `undecided'
8840or its subsidiary coding system according to a detected end-of-line
8841format.
ec6d2bb8 8842
48b0f3ae
PJ
8843If optional argument HIGHEST is non-nil, return the coding system of
8844highest priority. */)
5842a27b 8845 (Lisp_Object start, Lisp_Object end, Lisp_Object highest)
d46c5b12 8846{
d311d28c
PE
8847 ptrdiff_t from, to;
8848 ptrdiff_t from_byte, to_byte;
ec6d2bb8 8849
d46c5b12
KH
8850 validate_region (&start, &end);
8851 from = XINT (start), to = XINT (end);
8852 from_byte = CHAR_TO_BYTE (from);
8853 to_byte = CHAR_TO_BYTE (to);
ec6d2bb8 8854
d46c5b12
KH
8855 if (from < GPT && to >= GPT)
8856 move_gap_both (to, to_byte);
c210f766 8857
d46c5b12 8858 return detect_coding_system (BYTE_POS_ADDR (from_byte),
24a73b0a 8859 to - from, to_byte - from_byte,
0a28aafb 8860 !NILP (highest),
4b4deea2 8861 !NILP (BVAR (current_buffer
5d8ea120 8862 , enable_multibyte_characters)),
df7492f9 8863 Qnil);
ec6d2bb8
KH
8864}
8865
d46c5b12
KH
8866DEFUN ("detect-coding-string", Fdetect_coding_string, Sdetect_coding_string,
8867 1, 2, 0,
48b0f3ae
PJ
8868 doc: /* Detect coding system of the text in STRING.
8869Return a list of possible coding systems ordered by priority.
67ceab9d
KH
8870The coding systems to try and their priorities follows what
8871the function `coding-system-priority-list' (which see) returns.
fb88bf2d 8872
12e0131a 8873If only ASCII characters are found (except for such ISO-2022 control
d4a1d553
JB
8874characters as ESC), it returns a list of single element `undecided'
8875or its subsidiary coding system according to a detected end-of-line
8876format.
d46c5b12 8877
48b0f3ae
PJ
8878If optional argument HIGHEST is non-nil, return the coding system of
8879highest priority. */)
5842a27b 8880 (Lisp_Object string, Lisp_Object highest)
d46c5b12 8881{
b7826503 8882 CHECK_STRING (string);
b73bfc1c 8883
24a73b0a
KH
8884 return detect_coding_system (SDATA (string),
8885 SCHARS (string), SBYTES (string),
8f924df7 8886 !NILP (highest), STRING_MULTIBYTE (string),
df7492f9 8887 Qnil);
4ed46869 8888}
4ed46869 8889
b73bfc1c 8890
b0ab8123 8891static bool
971de7fb 8892char_encodable_p (int c, Lisp_Object attrs)
05e6f5dc 8893{
df7492f9 8894 Lisp_Object tail;
df7492f9 8895 struct charset *charset;
7d64c6ad 8896 Lisp_Object translation_table;
d46c5b12 8897
7d64c6ad 8898 translation_table = CODING_ATTR_TRANS_TBL (attrs);
a6f87d34 8899 if (! NILP (translation_table))
7d64c6ad 8900 c = translate_char (translation_table, c);
df7492f9
KH
8901 for (tail = CODING_ATTR_CHARSET_LIST (attrs);
8902 CONSP (tail); tail = XCDR (tail))
e133c8fa 8903 {
df7492f9
KH
8904 charset = CHARSET_FROM_ID (XINT (XCAR (tail)));
8905 if (CHAR_CHARSET_P (c, charset))
8906 break;
e133c8fa 8907 }
df7492f9 8908 return (! NILP (tail));
05e6f5dc 8909}
83fa074f 8910
fb88bf2d 8911
df7492f9
KH
8912/* Return a list of coding systems that safely encode the text between
8913 START and END. If EXCLUDE is non-nil, it is a list of coding
8914 systems not to check. The returned list doesn't contain any such
48468dac 8915 coding systems. In any case, if the text contains only ASCII or is
df7492f9 8916 unibyte, return t. */
e077cc80 8917
df7492f9
KH
8918DEFUN ("find-coding-systems-region-internal",
8919 Ffind_coding_systems_region_internal,
8920 Sfind_coding_systems_region_internal, 2, 3, 0,
8921 doc: /* Internal use only. */)
5842a27b 8922 (Lisp_Object start, Lisp_Object end, Lisp_Object exclude)
df7492f9
KH
8923{
8924 Lisp_Object coding_attrs_list, safe_codings;
d311d28c 8925 ptrdiff_t start_byte, end_byte;
7c78e542 8926 const unsigned char *p, *pbeg, *pend;
df7492f9 8927 int c;
0e727afa 8928 Lisp_Object tail, elt, work_table;
d46c5b12 8929
df7492f9
KH
8930 if (STRINGP (start))
8931 {
8932 if (!STRING_MULTIBYTE (start)
8f924df7 8933 || SCHARS (start) == SBYTES (start))
df7492f9
KH
8934 return Qt;
8935 start_byte = 0;
8f924df7 8936 end_byte = SBYTES (start);
df7492f9
KH
8937 }
8938 else
d46c5b12 8939 {
df7492f9
KH
8940 CHECK_NUMBER_COERCE_MARKER (start);
8941 CHECK_NUMBER_COERCE_MARKER (end);
8942 if (XINT (start) < BEG || XINT (end) > Z || XINT (start) > XINT (end))
8943 args_out_of_range (start, end);
4b4deea2 8944 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
df7492f9
KH
8945 return Qt;
8946 start_byte = CHAR_TO_BYTE (XINT (start));
8947 end_byte = CHAR_TO_BYTE (XINT (end));
8948 if (XINT (end) - XINT (start) == end_byte - start_byte)
8949 return Qt;
d46c5b12 8950
e1c23804 8951 if (XINT (start) < GPT && XINT (end) > GPT)
d46c5b12 8952 {
e1c23804
DL
8953 if ((GPT - XINT (start)) < (XINT (end) - GPT))
8954 move_gap_both (XINT (start), start_byte);
df7492f9 8955 else
e1c23804 8956 move_gap_both (XINT (end), end_byte);
d46c5b12
KH
8957 }
8958 }
8959
df7492f9
KH
8960 coding_attrs_list = Qnil;
8961 for (tail = Vcoding_system_list; CONSP (tail); tail = XCDR (tail))
8962 if (NILP (exclude)
8963 || NILP (Fmemq (XCAR (tail), exclude)))
8964 {
8965 Lisp_Object attrs;
d46c5b12 8966
df7492f9 8967 attrs = AREF (CODING_SYSTEM_SPEC (XCAR (tail)), 0);
c3a7b3f4 8968 if (EQ (XCAR (tail), CODING_ATTR_BASE_NAME (attrs)))
7d64c6ad
KH
8969 {
8970 ASET (attrs, coding_attr_trans_tbl,
2170c8f0 8971 get_translation_table (attrs, 1, NULL));
7d64c6ad
KH
8972 coding_attrs_list = Fcons (attrs, coding_attrs_list);
8973 }
df7492f9 8974 }
d46c5b12 8975
df7492f9 8976 if (STRINGP (start))
8f924df7 8977 p = pbeg = SDATA (start);
df7492f9
KH
8978 else
8979 p = pbeg = BYTE_POS_ADDR (start_byte);
8980 pend = p + (end_byte - start_byte);
b843d1ae 8981
df7492f9
KH
8982 while (p < pend && ASCII_BYTE_P (*p)) p++;
8983 while (p < pend && ASCII_BYTE_P (*(pend - 1))) pend--;
d46c5b12 8984
0e727afa 8985 work_table = Fmake_char_table (Qnil, Qnil);
05e6f5dc 8986 while (p < pend)
72d1a715 8987 {
df7492f9
KH
8988 if (ASCII_BYTE_P (*p))
8989 p++;
72d1a715
RS
8990 else
8991 {
df7492f9 8992 c = STRING_CHAR_ADVANCE (p);
0e727afa
YM
8993 if (!NILP (char_table_ref (work_table, c)))
8994 /* This character was already checked. Ignore it. */
8995 continue;
12410ef1 8996
df7492f9
KH
8997 charset_map_loaded = 0;
8998 for (tail = coding_attrs_list; CONSP (tail);)
8999 {
9000 elt = XCAR (tail);
9001 if (NILP (elt))
9002 tail = XCDR (tail);
9003 else if (char_encodable_p (c, elt))
9004 tail = XCDR (tail);
9005 else if (CONSP (XCDR (tail)))
9006 {
9007 XSETCAR (tail, XCAR (XCDR (tail)));
9008 XSETCDR (tail, XCDR (XCDR (tail)));
9009 }
9010 else
9011 {
9012 XSETCAR (tail, Qnil);
9013 tail = XCDR (tail);
9014 }
9015 }
9016 if (charset_map_loaded)
9017 {
d311d28c 9018 ptrdiff_t p_offset = p - pbeg, pend_offset = pend - pbeg;
05e6f5dc 9019
df7492f9 9020 if (STRINGP (start))
8f924df7 9021 pbeg = SDATA (start);
df7492f9
KH
9022 else
9023 pbeg = BYTE_POS_ADDR (start_byte);
9024 p = pbeg + p_offset;
9025 pend = pbeg + pend_offset;
9026 }
0e727afa 9027 char_table_set (work_table, c, Qt);
df7492f9 9028 }
ec6d2bb8 9029 }
fb88bf2d 9030
988b3759 9031 safe_codings = list2 (Qraw_text, Qno_conversion);
df7492f9
KH
9032 for (tail = coding_attrs_list; CONSP (tail); tail = XCDR (tail))
9033 if (! NILP (XCAR (tail)))
9034 safe_codings = Fcons (CODING_ATTR_BASE_NAME (XCAR (tail)), safe_codings);
ec6d2bb8 9035
05e6f5dc
KH
9036 return safe_codings;
9037}
4956c225 9038
d46c5b12 9039
8f924df7
KH
9040DEFUN ("unencodable-char-position", Funencodable_char_position,
9041 Sunencodable_char_position, 3, 5, 0,
9042 doc: /*
9043Return position of first un-encodable character in a region.
d4a1d553 9044START and END specify the region and CODING-SYSTEM specifies the
8f924df7 9045encoding to check. Return nil if CODING-SYSTEM does encode the region.
d46c5b12 9046
8f924df7
KH
9047If optional 4th argument COUNT is non-nil, it specifies at most how
9048many un-encodable characters to search. In this case, the value is a
9049list of positions.
d46c5b12 9050
8f924df7
KH
9051If optional 5th argument STRING is non-nil, it is a string to search
9052for un-encodable characters. In that case, START and END are indexes
9053to the string. */)
5842a27b 9054 (Lisp_Object start, Lisp_Object end, Lisp_Object coding_system, Lisp_Object count, Lisp_Object string)
8f924df7 9055{
d311d28c 9056 EMACS_INT n;
8f924df7 9057 struct coding_system coding;
7d64c6ad 9058 Lisp_Object attrs, charset_list, translation_table;
8f924df7 9059 Lisp_Object positions;
d311d28c 9060 ptrdiff_t from, to;
8f924df7 9061 const unsigned char *p, *stop, *pend;
f10fe38f 9062 bool ascii_compatible;
fb88bf2d 9063
8f924df7
KH
9064 setup_coding_system (Fcheck_coding_system (coding_system), &coding);
9065 attrs = CODING_ID_ATTRS (coding.id);
9066 if (EQ (CODING_ATTR_TYPE (attrs), Qraw_text))
9067 return Qnil;
9068 ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs));
9069 charset_list = CODING_ATTR_CHARSET_LIST (attrs);
2170c8f0 9070 translation_table = get_translation_table (attrs, 1, NULL);
fb88bf2d 9071
8f924df7
KH
9072 if (NILP (string))
9073 {
9074 validate_region (&start, &end);
9075 from = XINT (start);
9076 to = XINT (end);
4b4deea2 9077 if (NILP (BVAR (current_buffer, enable_multibyte_characters))
8f924df7
KH
9078 || (ascii_compatible
9079 && (to - from) == (CHAR_TO_BYTE (to) - (CHAR_TO_BYTE (from)))))
9080 return Qnil;
9081 p = CHAR_POS_ADDR (from);
9082 pend = CHAR_POS_ADDR (to);
9083 if (from < GPT && to >= GPT)
9084 stop = GPT_ADDR;
9085 else
9086 stop = pend;
9087 }
9088 else
9089 {
9090 CHECK_STRING (string);
9091 CHECK_NATNUM (start);
9092 CHECK_NATNUM (end);
d311d28c
PE
9093 if (! (XINT (start) <= XINT (end) && XINT (end) <= SCHARS (string)))
9094 args_out_of_range_3 (string, start, end);
8f924df7
KH
9095 from = XINT (start);
9096 to = XINT (end);
8f924df7
KH
9097 if (! STRING_MULTIBYTE (string))
9098 return Qnil;
9099 p = SDATA (string) + string_char_to_byte (string, from);
9100 stop = pend = SDATA (string) + string_char_to_byte (string, to);
9101 if (ascii_compatible && (to - from) == (pend - p))
9102 return Qnil;
9103 }
f2558efd 9104
8f924df7
KH
9105 if (NILP (count))
9106 n = 1;
9107 else
b73bfc1c 9108 {
8f924df7
KH
9109 CHECK_NATNUM (count);
9110 n = XINT (count);
b73bfc1c
KH
9111 }
9112
8f924df7 9113 positions = Qnil;
3633e3aa 9114 charset_map_loaded = 0;
8f924df7 9115 while (1)
d46c5b12 9116 {
8f924df7 9117 int c;
ec6d2bb8 9118
8f924df7
KH
9119 if (ascii_compatible)
9120 while (p < stop && ASCII_BYTE_P (*p))
9121 p++, from++;
9122 if (p >= stop)
0e79d667 9123 {
8f924df7
KH
9124 if (p >= pend)
9125 break;
9126 stop = pend;
9127 p = GAP_END_ADDR;
0e79d667 9128 }
ec6d2bb8 9129
8f924df7
KH
9130 c = STRING_CHAR_ADVANCE (p);
9131 if (! (ASCII_CHAR_P (c) && ascii_compatible)
7d64c6ad
KH
9132 && ! char_charset (translate_char (translation_table, c),
9133 charset_list, NULL))
ec6d2bb8 9134 {
8f924df7
KH
9135 positions = Fcons (make_number (from), positions);
9136 n--;
9137 if (n == 0)
9138 break;
ec6d2bb8
KH
9139 }
9140
8f924df7 9141 from++;
3633e3aa
KH
9142 if (charset_map_loaded && NILP (string))
9143 {
9144 p = CHAR_POS_ADDR (from);
9145 pend = CHAR_POS_ADDR (to);
9146 if (from < GPT && to >= GPT)
9147 stop = GPT_ADDR;
9148 else
9149 stop = pend;
9150 charset_map_loaded = 0;
9151 }
8f924df7 9152 }
d46c5b12 9153
8f924df7
KH
9154 return (NILP (count) ? Fcar (positions) : Fnreverse (positions));
9155}
d46c5b12 9156
d46c5b12 9157
df7492f9
KH
9158DEFUN ("check-coding-systems-region", Fcheck_coding_systems_region,
9159 Scheck_coding_systems_region, 3, 3, 0,
9160 doc: /* Check if the region is encodable by coding systems.
d46c5b12 9161
df7492f9
KH
9162START and END are buffer positions specifying the region.
9163CODING-SYSTEM-LIST is a list of coding systems to check.
d46c5b12 9164
df7492f9 9165The value is an alist ((CODING-SYSTEM POS0 POS1 ...) ...), where
d4a1d553 9166CODING-SYSTEM is a member of CODING-SYSTEM-LIST and can't encode the
df7492f9
KH
9167whole region, POS0, POS1, ... are buffer positions where non-encodable
9168characters are found.
93dec019 9169
df7492f9
KH
9170If all coding systems in CODING-SYSTEM-LIST can encode the region, the
9171value is nil.
93dec019 9172
df7492f9
KH
9173START may be a string. In that case, check if the string is
9174encodable, and the value contains indices to the string instead of
5704f39a
KH
9175buffer positions. END is ignored.
9176
4c1958f4 9177If the current buffer (or START if it is a string) is unibyte, the value
5704f39a 9178is nil. */)
5842a27b 9179 (Lisp_Object start, Lisp_Object end, Lisp_Object coding_system_list)
05e6f5dc 9180{
df7492f9 9181 Lisp_Object list;
d311d28c
PE
9182 ptrdiff_t start_byte, end_byte;
9183 ptrdiff_t pos;
7c78e542 9184 const unsigned char *p, *pbeg, *pend;
df7492f9 9185 int c;
7d64c6ad 9186 Lisp_Object tail, elt, attrs;
70ad9fc4 9187
05e6f5dc
KH
9188 if (STRINGP (start))
9189 {
df7492f9 9190 if (!STRING_MULTIBYTE (start)
4c1958f4 9191 || SCHARS (start) == SBYTES (start))
df7492f9
KH
9192 return Qnil;
9193 start_byte = 0;
8f924df7 9194 end_byte = SBYTES (start);
df7492f9 9195 pos = 0;
d46c5b12 9196 }
05e6f5dc 9197 else
b73bfc1c 9198 {
b7826503
PJ
9199 CHECK_NUMBER_COERCE_MARKER (start);
9200 CHECK_NUMBER_COERCE_MARKER (end);
05e6f5dc
KH
9201 if (XINT (start) < BEG || XINT (end) > Z || XINT (start) > XINT (end))
9202 args_out_of_range (start, end);
4b4deea2 9203 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
df7492f9
KH
9204 return Qnil;
9205 start_byte = CHAR_TO_BYTE (XINT (start));
9206 end_byte = CHAR_TO_BYTE (XINT (end));
9207 if (XINT (end) - XINT (start) == end_byte - start_byte)
5704f39a 9208 return Qnil;
df7492f9 9209
e1c23804 9210 if (XINT (start) < GPT && XINT (end) > GPT)
b73bfc1c 9211 {
e1c23804
DL
9212 if ((GPT - XINT (start)) < (XINT (end) - GPT))
9213 move_gap_both (XINT (start), start_byte);
df7492f9 9214 else
e1c23804 9215 move_gap_both (XINT (end), end_byte);
b73bfc1c 9216 }
e1c23804 9217 pos = XINT (start);
b73bfc1c 9218 }
7553d0e1 9219
df7492f9
KH
9220 list = Qnil;
9221 for (tail = coding_system_list; CONSP (tail); tail = XCDR (tail))
12410ef1 9222 {
df7492f9 9223 elt = XCAR (tail);
7d64c6ad 9224 attrs = AREF (CODING_SYSTEM_SPEC (elt), 0);
2170c8f0
KH
9225 ASET (attrs, coding_attr_trans_tbl,
9226 get_translation_table (attrs, 1, NULL));
7d64c6ad 9227 list = Fcons (Fcons (elt, Fcons (attrs, Qnil)), list);
12410ef1
KH
9228 }
9229
df7492f9 9230 if (STRINGP (start))
8f924df7 9231 p = pbeg = SDATA (start);
72d1a715 9232 else
df7492f9
KH
9233 p = pbeg = BYTE_POS_ADDR (start_byte);
9234 pend = p + (end_byte - start_byte);
4ed46869 9235
df7492f9
KH
9236 while (p < pend && ASCII_BYTE_P (*p)) p++, pos++;
9237 while (p < pend && ASCII_BYTE_P (*(pend - 1))) pend--;
ec6d2bb8 9238
df7492f9 9239 while (p < pend)
d46c5b12 9240 {
df7492f9
KH
9241 if (ASCII_BYTE_P (*p))
9242 p++;
e133c8fa 9243 else
05e6f5dc 9244 {
df7492f9
KH
9245 c = STRING_CHAR_ADVANCE (p);
9246
9247 charset_map_loaded = 0;
9248 for (tail = list; CONSP (tail); tail = XCDR (tail))
9249 {
9250 elt = XCDR (XCAR (tail));
9251 if (! char_encodable_p (c, XCAR (elt)))
9252 XSETCDR (elt, Fcons (make_number (pos), XCDR (elt)));
9253 }
9254 if (charset_map_loaded)
9255 {
d311d28c 9256 ptrdiff_t p_offset = p - pbeg, pend_offset = pend - pbeg;
df7492f9
KH
9257
9258 if (STRINGP (start))
8f924df7 9259 pbeg = SDATA (start);
df7492f9
KH
9260 else
9261 pbeg = BYTE_POS_ADDR (start_byte);
9262 p = pbeg + p_offset;
9263 pend = pbeg + pend_offset;
9264 }
05e6f5dc 9265 }
df7492f9 9266 pos++;
d46c5b12 9267 }
4ed46869 9268
df7492f9
KH
9269 tail = list;
9270 list = Qnil;
9271 for (; CONSP (tail); tail = XCDR (tail))
ec6d2bb8 9272 {
df7492f9
KH
9273 elt = XCAR (tail);
9274 if (CONSP (XCDR (XCDR (elt))))
9275 list = Fcons (Fcons (XCAR (elt), Fnreverse (XCDR (XCDR (elt)))),
9276 list);
ec6d2bb8 9277 }
2b4f9037 9278
df7492f9 9279 return list;
d46c5b12
KH
9280}
9281
3fd9494b 9282
74ab6df5 9283static Lisp_Object
cf84bb53
JB
9284code_convert_region (Lisp_Object start, Lisp_Object end,
9285 Lisp_Object coding_system, Lisp_Object dst_object,
f10fe38f 9286 bool encodep, bool norecord)
4ed46869 9287{
3a73fa5d 9288 struct coding_system coding;
d311d28c 9289 ptrdiff_t from, from_byte, to, to_byte;
df7492f9 9290 Lisp_Object src_object;
4ed46869 9291
df7492f9
KH
9292 if (NILP (coding_system))
9293 coding_system = Qno_conversion;
9294 else
9295 CHECK_CODING_SYSTEM (coding_system);
9296 src_object = Fcurrent_buffer ();
9297 if (NILP (dst_object))
9298 dst_object = src_object;
9299 else if (! EQ (dst_object, Qt))
9300 CHECK_BUFFER (dst_object);
3a73fa5d 9301
d46c5b12
KH
9302 validate_region (&start, &end);
9303 from = XFASTINT (start);
df7492f9 9304 from_byte = CHAR_TO_BYTE (from);
d46c5b12 9305 to = XFASTINT (end);
df7492f9 9306 to_byte = CHAR_TO_BYTE (to);
764ca8da 9307
df7492f9
KH
9308 setup_coding_system (coding_system, &coding);
9309 coding.mode |= CODING_MODE_LAST_BLOCK;
ec6d2bb8 9310
df7492f9
KH
9311 if (encodep)
9312 encode_coding_object (&coding, src_object, from, from_byte, to, to_byte,
9313 dst_object);
9314 else
9315 decode_coding_object (&coding, src_object, from, from_byte, to, to_byte,
9316 dst_object);
9317 if (! norecord)
9318 Vlast_coding_system_used = CODING_ID_NAME (coding.id);
ec6d2bb8 9319
df7492f9
KH
9320 return (BUFFERP (dst_object)
9321 ? make_number (coding.produced_char)
9322 : coding.dst_object);
4031e2bf 9323}
78108bcd 9324
4ed46869 9325
4031e2bf 9326DEFUN ("decode-coding-region", Fdecode_coding_region, Sdecode_coding_region,
df7492f9 9327 3, 4, "r\nzCoding system: ",
48b0f3ae 9328 doc: /* Decode the current region from the specified coding system.
df7492f9
KH
9329When called from a program, takes four arguments:
9330 START, END, CODING-SYSTEM, and DESTINATION.
9331START and END are buffer positions.
8844fa83 9332
df7492f9 9333Optional 4th arguments DESTINATION specifies where the decoded text goes.
2354b80b 9334If nil, the region between START and END is replaced by the decoded text.
1560f91a
EZ
9335If buffer, the decoded text is inserted in that buffer after point (point
9336does not move).
446dcd75 9337In those cases, the length of the decoded text is returned.
319a3947 9338If DESTINATION is t, the decoded text is returned.
8844fa83 9339
48b0f3ae
PJ
9340This function sets `last-coding-system-used' to the precise coding system
9341used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
319a3947 9342not fully specified.) */)
5842a27b 9343 (Lisp_Object start, Lisp_Object end, Lisp_Object coding_system, Lisp_Object destination)
4031e2bf 9344{
df7492f9 9345 return code_convert_region (start, end, coding_system, destination, 0, 0);
3a73fa5d 9346}
8844fa83 9347
3a73fa5d 9348DEFUN ("encode-coding-region", Fencode_coding_region, Sencode_coding_region,
df7492f9
KH
9349 3, 4, "r\nzCoding system: ",
9350 doc: /* Encode the current region by specified coding system.
d4a1d553
JB
9351When called from a program, takes four arguments:
9352 START, END, CODING-SYSTEM and DESTINATION.
9353START and END are buffer positions.
d46c5b12 9354
df7492f9
KH
9355Optional 4th arguments DESTINATION specifies where the encoded text goes.
9356If nil, the region between START and END is replace by the encoded text.
1560f91a
EZ
9357If buffer, the encoded text is inserted in that buffer after point (point
9358does not move).
446dcd75 9359In those cases, the length of the encoded text is returned.
319a3947 9360If DESTINATION is t, the encoded text is returned.
2391eaa4 9361
48b0f3ae
PJ
9362This function sets `last-coding-system-used' to the precise coding system
9363used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
319a3947 9364not fully specified.) */)
5842a27b 9365 (Lisp_Object start, Lisp_Object end, Lisp_Object coding_system, Lisp_Object destination)
3a73fa5d 9366{
df7492f9 9367 return code_convert_region (start, end, coding_system, destination, 1, 0);
b73bfc1c
KH
9368}
9369
9370Lisp_Object
6f704c76 9371code_convert_string (Lisp_Object string, Lisp_Object coding_system,
f10fe38f
PE
9372 Lisp_Object dst_object, bool encodep, bool nocopy,
9373 bool norecord)
b73bfc1c 9374{
4031e2bf 9375 struct coding_system coding;
d311d28c 9376 ptrdiff_t chars, bytes;
ec6d2bb8 9377
b7826503 9378 CHECK_STRING (string);
d46c5b12 9379 if (NILP (coding_system))
4956c225 9380 {
df7492f9
KH
9381 if (! norecord)
9382 Vlast_coding_system_used = Qno_conversion;
9383 if (NILP (dst_object))
9384 return (nocopy ? Fcopy_sequence (string) : string);
4956c225 9385 }
b73bfc1c 9386
df7492f9
KH
9387 if (NILP (coding_system))
9388 coding_system = Qno_conversion;
9389 else
9390 CHECK_CODING_SYSTEM (coding_system);
9391 if (NILP (dst_object))
9392 dst_object = Qt;
9393 else if (! EQ (dst_object, Qt))
9394 CHECK_BUFFER (dst_object);
73be902c 9395
df7492f9 9396 setup_coding_system (coding_system, &coding);
d46c5b12 9397 coding.mode |= CODING_MODE_LAST_BLOCK;
8f924df7
KH
9398 chars = SCHARS (string);
9399 bytes = SBYTES (string);
df7492f9
KH
9400 if (encodep)
9401 encode_coding_object (&coding, string, 0, 0, chars, bytes, dst_object);
9402 else
9403 decode_coding_object (&coding, string, 0, 0, chars, bytes, dst_object);
9404 if (! norecord)
9405 Vlast_coding_system_used = CODING_ID_NAME (coding.id);
73be902c 9406
df7492f9
KH
9407 return (BUFFERP (dst_object)
9408 ? make_number (coding.produced_char)
9409 : coding.dst_object);
4ed46869 9410}
73be902c 9411
b73bfc1c 9412
ecec61c1 9413/* Encode or decode STRING according to CODING_SYSTEM.
ec6d2bb8 9414 Do not set Vlast_coding_system_used.
4ed46869 9415
ec6d2bb8
KH
9416 This function is called only from macros DECODE_FILE and
9417 ENCODE_FILE, thus we ignore character composition. */
4ed46869 9418
ecec61c1 9419Lisp_Object
cf84bb53 9420code_convert_string_norecord (Lisp_Object string, Lisp_Object coding_system,
f10fe38f 9421 bool encodep)
4ed46869 9422{
0be8721c 9423 return code_convert_string (string, coding_system, Qt, encodep, 0, 1);
4ed46869
KH
9424}
9425
4ed46869 9426
a7ca3326 9427DEFUN ("decode-coding-string", Fdecode_coding_string, Sdecode_coding_string,
df7492f9
KH
9428 2, 4, 0,
9429 doc: /* Decode STRING which is encoded in CODING-SYSTEM, and return the result.
9430
9431Optional third arg NOCOPY non-nil means it is OK to return STRING itself
9432if the decoding operation is trivial.
ecec61c1 9433
d4a1d553 9434Optional fourth arg BUFFER non-nil means that the decoded text is
1560f91a
EZ
9435inserted in that buffer after point (point does not move). In this
9436case, the return value is the length of the decoded text.
ecec61c1 9437
df7492f9
KH
9438This function sets `last-coding-system-used' to the precise coding system
9439used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
d4a1d553 9440not fully specified.) */)
5842a27b 9441 (Lisp_Object string, Lisp_Object coding_system, Lisp_Object nocopy, Lisp_Object buffer)
4ed46869 9442{
df7492f9
KH
9443 return code_convert_string (string, coding_system, buffer,
9444 0, ! NILP (nocopy), 0);
4ed46869
KH
9445}
9446
df7492f9
KH
9447DEFUN ("encode-coding-string", Fencode_coding_string, Sencode_coding_string,
9448 2, 4, 0,
9449 doc: /* Encode STRING to CODING-SYSTEM, and return the result.
9450
9451Optional third arg NOCOPY non-nil means it is OK to return STRING
9452itself if the encoding operation is trivial.
9453
d4a1d553 9454Optional fourth arg BUFFER non-nil means that the encoded text is
1560f91a
EZ
9455inserted in that buffer after point (point does not move). In this
9456case, the return value is the length of the encoded text.
df7492f9
KH
9457
9458This function sets `last-coding-system-used' to the precise coding system
9459used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
9460not fully specified.) */)
5842a27b 9461 (Lisp_Object string, Lisp_Object coding_system, Lisp_Object nocopy, Lisp_Object buffer)
4ed46869 9462{
df7492f9 9463 return code_convert_string (string, coding_system, buffer,
4550efdf 9464 1, ! NILP (nocopy), 0);
4ed46869 9465}
df7492f9 9466
3a73fa5d 9467\f
4ed46869 9468DEFUN ("decode-sjis-char", Fdecode_sjis_char, Sdecode_sjis_char, 1, 1, 0,
48b0f3ae
PJ
9469 doc: /* Decode a Japanese character which has CODE in shift_jis encoding.
9470Return the corresponding character. */)
5842a27b 9471 (Lisp_Object code)
4ed46869 9472{
df7492f9
KH
9473 Lisp_Object spec, attrs, val;
9474 struct charset *charset_roman, *charset_kanji, *charset_kana, *charset;
5fdb398c
PE
9475 EMACS_INT ch;
9476 int c;
4ed46869 9477
df7492f9 9478 CHECK_NATNUM (code);
5fdb398c 9479 ch = XFASTINT (code);
df7492f9
KH
9480 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system, spec);
9481 attrs = AREF (spec, 0);
4ed46869 9482
5fdb398c 9483 if (ASCII_BYTE_P (ch)
df7492f9
KH
9484 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)))
9485 return code;
4ed46869 9486
df7492f9
KH
9487 val = CODING_ATTR_CHARSET_LIST (attrs);
9488 charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
004068e4
KH
9489 charset_kana = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
9490 charset_kanji = CHARSET_FROM_ID (XINT (XCAR (val)));
fa42c37f 9491
5fdb398c
PE
9492 if (ch <= 0x7F)
9493 {
9494 c = ch;
9495 charset = charset_roman;
9496 }
9497 else if (ch >= 0xA0 && ch < 0xDF)
55ab7be3 9498 {
5fdb398c 9499 c = ch - 0x80;
df7492f9 9500 charset = charset_kana;
4ed46869 9501 }
55ab7be3 9502 else
4ed46869 9503 {
5fdb398c
PE
9504 EMACS_INT c1 = ch >> 8;
9505 int c2 = ch & 0xFF;
df7492f9 9506
2735d060
PE
9507 if (c1 < 0x81 || (c1 > 0x9F && c1 < 0xE0) || c1 > 0xEF
9508 || c2 < 0x40 || c2 == 0x7F || c2 > 0xFC)
c2982e87 9509 error ("Invalid code: %"pI"d", ch);
5fdb398c 9510 c = ch;
df7492f9
KH
9511 SJIS_TO_JIS (c);
9512 charset = charset_kanji;
4ed46869 9513 }
df7492f9
KH
9514 c = DECODE_CHAR (charset, c);
9515 if (c < 0)
c2982e87 9516 error ("Invalid code: %"pI"d", ch);
df7492f9 9517 return make_number (c);
93dec019 9518}
4ed46869 9519
48b0f3ae 9520
4ed46869 9521DEFUN ("encode-sjis-char", Fencode_sjis_char, Sencode_sjis_char, 1, 1, 0,
8acf0c0e 9522 doc: /* Encode a Japanese character CH to shift_jis encoding.
48b0f3ae 9523Return the corresponding code in SJIS. */)
5842a27b 9524 (Lisp_Object ch)
4ed46869 9525{
df7492f9
KH
9526 Lisp_Object spec, attrs, charset_list;
9527 int c;
9528 struct charset *charset;
9529 unsigned code;
48b0f3ae 9530
df7492f9
KH
9531 CHECK_CHARACTER (ch);
9532 c = XFASTINT (ch);
9533 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system, spec);
9534 attrs = AREF (spec, 0);
9535
9536 if (ASCII_CHAR_P (c)
9537 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)))
9538 return ch;
9539
9540 charset_list = CODING_ATTR_CHARSET_LIST (attrs);
9541 charset = char_charset (c, charset_list, &code);
9542 if (code == CHARSET_INVALID_CODE (charset))
e6c3da20 9543 error ("Can't encode by shift_jis encoding: %c", c);
df7492f9
KH
9544 JIS_TO_SJIS (code);
9545
9546 return make_number (code);
4ed46869
KH
9547}
9548
9549DEFUN ("decode-big5-char", Fdecode_big5_char, Sdecode_big5_char, 1, 1, 0,
48b0f3ae
PJ
9550 doc: /* Decode a Big5 character which has CODE in BIG5 coding system.
9551Return the corresponding character. */)
5842a27b 9552 (Lisp_Object code)
d46c5b12 9553{
df7492f9
KH
9554 Lisp_Object spec, attrs, val;
9555 struct charset *charset_roman, *charset_big5, *charset;
5fdb398c 9556 EMACS_INT ch;
df7492f9 9557 int c;
6289dd10 9558
df7492f9 9559 CHECK_NATNUM (code);
5fdb398c 9560 ch = XFASTINT (code);
df7492f9
KH
9561 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system, spec);
9562 attrs = AREF (spec, 0);
4ed46869 9563
5fdb398c 9564 if (ASCII_BYTE_P (ch)
df7492f9
KH
9565 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)))
9566 return code;
6289dd10 9567
df7492f9
KH
9568 val = CODING_ATTR_CHARSET_LIST (attrs);
9569 charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
9570 charset_big5 = CHARSET_FROM_ID (XINT (XCAR (val)));
c210f766 9571
5fdb398c
PE
9572 if (ch <= 0x7F)
9573 {
9574 c = ch;
9575 charset = charset_roman;
9576 }
c28a9453
KH
9577 else
9578 {
5fdb398c
PE
9579 EMACS_INT b1 = ch >> 8;
9580 int b2 = ch & 0x7F;
df7492f9
KH
9581 if (b1 < 0xA1 || b1 > 0xFE
9582 || b2 < 0x40 || (b2 > 0x7E && b2 < 0xA1) || b2 > 0xFE)
c2982e87 9583 error ("Invalid code: %"pI"d", ch);
5fdb398c 9584 c = ch;
df7492f9 9585 charset = charset_big5;
c28a9453 9586 }
5fdb398c 9587 c = DECODE_CHAR (charset, c);
df7492f9 9588 if (c < 0)
c2982e87 9589 error ("Invalid code: %"pI"d", ch);
df7492f9 9590 return make_number (c);
d46c5b12 9591}
6289dd10 9592
4ed46869 9593DEFUN ("encode-big5-char", Fencode_big5_char, Sencode_big5_char, 1, 1, 0,
8acf0c0e 9594 doc: /* Encode the Big5 character CH to BIG5 coding system.
48b0f3ae 9595Return the corresponding character code in Big5. */)
5842a27b 9596 (Lisp_Object ch)
4ed46869 9597{
df7492f9
KH
9598 Lisp_Object spec, attrs, charset_list;
9599 struct charset *charset;
9600 int c;
9601 unsigned code;
9602
9603 CHECK_CHARACTER (ch);
9604 c = XFASTINT (ch);
9605 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system, spec);
9606 attrs = AREF (spec, 0);
9607 if (ASCII_CHAR_P (c)
9608 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)))
9609 return ch;
9610
9611 charset_list = CODING_ATTR_CHARSET_LIST (attrs);
9612 charset = char_charset (c, charset_list, &code);
9613 if (code == CHARSET_INVALID_CODE (charset))
e6c3da20 9614 error ("Can't encode by Big5 encoding: %c", c);
df7492f9
KH
9615
9616 return make_number (code);
4ed46869 9617}
48b0f3ae 9618
3a73fa5d 9619\f
002fdb44 9620DEFUN ("set-terminal-coding-system-internal", Fset_terminal_coding_system_internal,
68bba4e4 9621 Sset_terminal_coding_system_internal, 1, 2, 0,
48b0f3ae 9622 doc: /* Internal use only. */)
5842a27b 9623 (Lisp_Object coding_system, Lisp_Object terminal)
4ed46869 9624{
b18fad6d
KH
9625 struct terminal *term = get_terminal (terminal, 1);
9626 struct coding_system *terminal_coding = TERMINAL_TERMINAL_CODING (term);
b7826503 9627 CHECK_SYMBOL (coding_system);
b8299c66 9628 setup_coding_system (Fcheck_coding_system (coding_system), terminal_coding);
70c22245 9629 /* We had better not send unsafe characters to terminal. */
c73bd236 9630 terminal_coding->mode |= CODING_MODE_SAFE_ENCODING;
ad1746f5 9631 /* Character composition should be disabled. */
c73bd236 9632 terminal_coding->common_flags &= ~CODING_ANNOTATE_COMPOSITION_MASK;
b8299c66
KL
9633 terminal_coding->src_multibyte = 1;
9634 terminal_coding->dst_multibyte = 0;
3f22b86f
PE
9635 tset_charset_list
9636 (term, (terminal_coding->common_flags & CODING_REQUIRE_ENCODING_MASK
9637 ? coding_charset_list (terminal_coding)
9638 : Fcons (make_number (charset_ascii), Qnil)));
4ed46869
KH
9639 return Qnil;
9640}
9641
c4825358
KH
9642DEFUN ("set-safe-terminal-coding-system-internal",
9643 Fset_safe_terminal_coding_system_internal,
48b0f3ae 9644 Sset_safe_terminal_coding_system_internal, 1, 1, 0,
ddb67bdc 9645 doc: /* Internal use only. */)
5842a27b 9646 (Lisp_Object coding_system)
d46c5b12 9647{
b7826503 9648 CHECK_SYMBOL (coding_system);
c4825358
KH
9649 setup_coding_system (Fcheck_coding_system (coding_system),
9650 &safe_terminal_coding);
ad1746f5 9651 /* Character composition should be disabled. */
df7492f9 9652 safe_terminal_coding.common_flags &= ~CODING_ANNOTATE_COMPOSITION_MASK;
b73bfc1c
KH
9653 safe_terminal_coding.src_multibyte = 1;
9654 safe_terminal_coding.dst_multibyte = 0;
c4825358
KH
9655 return Qnil;
9656}
4ed46869 9657
002fdb44 9658DEFUN ("terminal-coding-system", Fterminal_coding_system,
68bba4e4 9659 Sterminal_coding_system, 0, 1, 0,
6ed8eeff 9660 doc: /* Return coding system specified for terminal output on the given terminal.
708e05dc 9661TERMINAL may be a terminal object, a frame, or nil for the selected
6ed8eeff 9662frame's terminal device. */)
5842a27b 9663 (Lisp_Object terminal)
4ed46869 9664{
985773c9
MB
9665 struct coding_system *terminal_coding
9666 = TERMINAL_TERMINAL_CODING (get_terminal (terminal, 1));
9667 Lisp_Object coding_system = CODING_ID_NAME (terminal_coding->id);
ae6f73fa 9668
6d5eb5b0 9669 /* For backward compatibility, return nil if it is `undecided'. */
f75c90a9 9670 return (! EQ (coding_system, Qundecided) ? coding_system : Qnil);
4ed46869
KH
9671}
9672
002fdb44 9673DEFUN ("set-keyboard-coding-system-internal", Fset_keyboard_coding_system_internal,
68bba4e4 9674 Sset_keyboard_coding_system_internal, 1, 2, 0,
48b0f3ae 9675 doc: /* Internal use only. */)
5842a27b 9676 (Lisp_Object coding_system, Lisp_Object terminal)
4ed46869 9677{
6ed8eeff 9678 struct terminal *t = get_terminal (terminal, 1);
b7826503 9679 CHECK_SYMBOL (coding_system);
624bda09
KH
9680 if (NILP (coding_system))
9681 coding_system = Qno_conversion;
9682 else
9683 Fcheck_coding_system (coding_system);
9684 setup_coding_system (coding_system, TERMINAL_KEYBOARD_CODING (t));
ad1746f5 9685 /* Character composition should be disabled. */
c73bd236
MB
9686 TERMINAL_KEYBOARD_CODING (t)->common_flags
9687 &= ~CODING_ANNOTATE_COMPOSITION_MASK;
4ed46869
KH
9688 return Qnil;
9689}
9690
9691DEFUN ("keyboard-coding-system",
985773c9 9692 Fkeyboard_coding_system, Skeyboard_coding_system, 0, 1, 0,
48b0f3ae 9693 doc: /* Return coding system specified for decoding keyboard input. */)
5842a27b 9694 (Lisp_Object terminal)
4ed46869 9695{
985773c9
MB
9696 return CODING_ID_NAME (TERMINAL_KEYBOARD_CODING
9697 (get_terminal (terminal, 1))->id);
4ed46869
KH
9698}
9699
4ed46869 9700\f
a7ca3326 9701DEFUN ("find-operation-coding-system", Ffind_operation_coding_system,
a5d301df 9702 Sfind_operation_coding_system, 1, MANY, 0,
48b0f3ae
PJ
9703 doc: /* Choose a coding system for an operation based on the target name.
9704The value names a pair of coding systems: (DECODING-SYSTEM . ENCODING-SYSTEM).
9705DECODING-SYSTEM is the coding system to use for decoding
9706\(in case OPERATION does decoding), and ENCODING-SYSTEM is the coding system
9707for encoding (in case OPERATION does encoding).
05e6f5dc 9708
48b0f3ae
PJ
9709The first argument OPERATION specifies an I/O primitive:
9710 For file I/O, `insert-file-contents' or `write-region'.
9711 For process I/O, `call-process', `call-process-region', or `start-process'.
9712 For network I/O, `open-network-stream'.
05e6f5dc 9713
48b0f3ae
PJ
9714The remaining arguments should be the same arguments that were passed
9715to the primitive. Depending on which primitive, one of those arguments
9716is selected as the TARGET. For example, if OPERATION does file I/O,
9717whichever argument specifies the file name is TARGET.
05e6f5dc 9718
48b0f3ae 9719TARGET has a meaning which depends on OPERATION:
b883cdb2 9720 For file I/O, TARGET is a file name (except for the special case below).
48b0f3ae 9721 For process I/O, TARGET is a process name.
d4a1d553 9722 For network I/O, TARGET is a service name or a port number.
05e6f5dc 9723
d4a1d553 9724This function looks up what is specified for TARGET in
48b0f3ae
PJ
9725`file-coding-system-alist', `process-coding-system-alist',
9726or `network-coding-system-alist' depending on OPERATION.
9727They may specify a coding system, a cons of coding systems,
9728or a function symbol to call.
9729In the last case, we call the function with one argument,
9730which is a list of all the arguments given to this function.
1011c487
MB
9731If the function can't decide a coding system, it can return
9732`undecided' so that the normal code-detection is performed.
48b0f3ae 9733
b883cdb2
MB
9734If OPERATION is `insert-file-contents', the argument corresponding to
9735TARGET may be a cons (FILENAME . BUFFER). In that case, FILENAME is a
9736file name to look up, and BUFFER is a buffer that contains the file's
9737contents (not yet decoded). If `file-coding-system-alist' specifies a
9738function to call for FILENAME, that function should examine the
9739contents of BUFFER instead of reading the file.
9740
d918f936 9741usage: (find-operation-coding-system OPERATION ARGUMENTS...) */)
f66c7cf8 9742 (ptrdiff_t nargs, Lisp_Object *args)
6b89e3aa 9743{
4ed46869
KH
9744 Lisp_Object operation, target_idx, target, val;
9745 register Lisp_Object chain;
177c0ea7 9746
4ed46869
KH
9747 if (nargs < 2)
9748 error ("Too few arguments");
9749 operation = args[0];
9750 if (!SYMBOLP (operation)
d311d28c 9751 || (target_idx = Fget (operation, Qtarget_idx), !NATNUMP (target_idx)))
3ed051d4 9752 error ("Invalid first argument");
7b09a37a 9753 if (nargs <= 1 + XFASTINT (target_idx))
94dcfacf 9754 error ("Too few arguments for operation `%s'",
8f924df7 9755 SDATA (SYMBOL_NAME (operation)));
c5101a77 9756 target = args[XFASTINT (target_idx) + 1];
4ed46869 9757 if (!(STRINGP (target)
091a0ff0
KH
9758 || (EQ (operation, Qinsert_file_contents) && CONSP (target)
9759 && STRINGP (XCAR (target)) && BUFFERP (XCDR (target)))
4ed46869 9760 || (EQ (operation, Qopen_network_stream) && INTEGERP (target))))
94dcfacf
EZ
9761 error ("Invalid argument %"pI"d of operation `%s'",
9762 XFASTINT (target_idx) + 1, SDATA (SYMBOL_NAME (operation)));
091a0ff0
KH
9763 if (CONSP (target))
9764 target = XCAR (target);
4ed46869 9765
2e34157c
RS
9766 chain = ((EQ (operation, Qinsert_file_contents)
9767 || EQ (operation, Qwrite_region))
02ba4723 9768 ? Vfile_coding_system_alist
2e34157c 9769 : (EQ (operation, Qopen_network_stream)
02ba4723
KH
9770 ? Vnetwork_coding_system_alist
9771 : Vprocess_coding_system_alist));
4ed46869
KH
9772 if (NILP (chain))
9773 return Qnil;
9774
03699b14 9775 for (; CONSP (chain); chain = XCDR (chain))
6b89e3aa 9776 {
f44d27ce 9777 Lisp_Object elt;
6b89e3aa 9778
df7492f9 9779 elt = XCAR (chain);
4ed46869
KH
9780 if (CONSP (elt)
9781 && ((STRINGP (target)
03699b14
KR
9782 && STRINGP (XCAR (elt))
9783 && fast_string_match (XCAR (elt), target) >= 0)
9784 || (INTEGERP (target) && EQ (target, XCAR (elt)))))
6b89e3aa 9785 {
03699b14 9786 val = XCDR (elt);
b19fd4c5
KH
9787 /* Here, if VAL is both a valid coding system and a valid
9788 function symbol, we return VAL as a coding system. */
02ba4723
KH
9789 if (CONSP (val))
9790 return val;
9791 if (! SYMBOLP (val))
9792 return Qnil;
9793 if (! NILP (Fcoding_system_p (val)))
9794 return Fcons (val, val);
b19fd4c5 9795 if (! NILP (Ffboundp (val)))
6b89e3aa 9796 {
e2b97060
MB
9797 /* We use call1 rather than safe_call1
9798 so as to get bug reports about functions called here
9799 which don't handle the current interface. */
9800 val = call1 (val, Flist (nargs, args));
b19fd4c5
KH
9801 if (CONSP (val))
9802 return val;
9803 if (SYMBOLP (val) && ! NILP (Fcoding_system_p (val)))
9804 return Fcons (val, val);
6b89e3aa 9805 }
02ba4723 9806 return Qnil;
6b89e3aa
KH
9807 }
9808 }
4ed46869 9809 return Qnil;
6b89e3aa
KH
9810}
9811
df7492f9 9812DEFUN ("set-coding-system-priority", Fset_coding_system_priority,
a3f6ee6d 9813 Sset_coding_system_priority, 0, MANY, 0,
da7db224 9814 doc: /* Assign higher priority to the coding systems given as arguments.
d4a1d553 9815If multiple coding systems belong to the same category,
a3181084
DL
9816all but the first one are ignored.
9817
d4a1d553 9818usage: (set-coding-system-priority &rest coding-systems) */)
f66c7cf8 9819 (ptrdiff_t nargs, Lisp_Object *args)
df7492f9 9820{
f66c7cf8 9821 ptrdiff_t i, j;
f10fe38f 9822 bool changed[coding_category_max];
df7492f9
KH
9823 enum coding_category priorities[coding_category_max];
9824
72af86bd 9825 memset (changed, 0, sizeof changed);
6b89e3aa 9826
df7492f9 9827 for (i = j = 0; i < nargs; i++)
6b89e3aa 9828 {
df7492f9
KH
9829 enum coding_category category;
9830 Lisp_Object spec, attrs;
6b89e3aa 9831
df7492f9
KH
9832 CHECK_CODING_SYSTEM_GET_SPEC (args[i], spec);
9833 attrs = AREF (spec, 0);
9834 category = XINT (CODING_ATTR_CATEGORY (attrs));
9835 if (changed[category])
9836 /* Ignore this coding system because a coding system of the
9837 same category already had a higher priority. */
9838 continue;
9839 changed[category] = 1;
9840 priorities[j++] = category;
9841 if (coding_categories[category].id >= 0
9842 && ! EQ (args[i], CODING_ID_NAME (coding_categories[category].id)))
9843 setup_coding_system (args[i], &coding_categories[category]);
ff563fce 9844 Fset (AREF (Vcoding_category_table, category), args[i]);
df7492f9 9845 }
6b89e3aa 9846
df7492f9
KH
9847 /* Now we have decided top J priorities. Reflect the order of the
9848 original priorities to the remaining priorities. */
6b89e3aa 9849
df7492f9 9850 for (i = j, j = 0; i < coding_category_max; i++, j++)
6b89e3aa 9851 {
df7492f9
KH
9852 while (j < coding_category_max
9853 && changed[coding_priorities[j]])
9854 j++;
9855 if (j == coding_category_max)
1088b922 9856 emacs_abort ();
df7492f9
KH
9857 priorities[i] = coding_priorities[j];
9858 }
6b89e3aa 9859
72af86bd 9860 memcpy (coding_priorities, priorities, sizeof priorities);
177c0ea7 9861
ff563fce
KH
9862 /* Update `coding-category-list'. */
9863 Vcoding_category_list = Qnil;
c5101a77 9864 for (i = coding_category_max; i-- > 0; )
ff563fce
KH
9865 Vcoding_category_list
9866 = Fcons (AREF (Vcoding_category_table, priorities[i]),
9867 Vcoding_category_list);
6b89e3aa 9868
df7492f9 9869 return Qnil;
6b89e3aa
KH
9870}
9871
df7492f9
KH
9872DEFUN ("coding-system-priority-list", Fcoding_system_priority_list,
9873 Scoding_system_priority_list, 0, 1, 0,
da7db224 9874 doc: /* Return a list of coding systems ordered by their priorities.
b811c52b
KH
9875The list contains a subset of coding systems; i.e. coding systems
9876assigned to each coding category (see `coding-category-list').
9877
da7db224 9878HIGHESTP non-nil means just return the highest priority one. */)
5842a27b 9879 (Lisp_Object highestp)
d46c5b12
KH
9880{
9881 int i;
df7492f9 9882 Lisp_Object val;
6b89e3aa 9883
df7492f9 9884 for (i = 0, val = Qnil; i < coding_category_max; i++)
d46c5b12 9885 {
df7492f9
KH
9886 enum coding_category category = coding_priorities[i];
9887 int id = coding_categories[category].id;
9888 Lisp_Object attrs;
068a9dbd 9889
df7492f9
KH
9890 if (id < 0)
9891 continue;
9892 attrs = CODING_ID_ATTRS (id);
9893 if (! NILP (highestp))
9894 return CODING_ATTR_BASE_NAME (attrs);
9895 val = Fcons (CODING_ATTR_BASE_NAME (attrs), val);
9896 }
9897 return Fnreverse (val);
9898}
068a9dbd 9899
91433552 9900static const char *const suffixes[] = { "-unix", "-dos", "-mac" };
068a9dbd
KH
9901
9902static Lisp_Object
971de7fb 9903make_subsidiaries (Lisp_Object base)
068a9dbd 9904{
df7492f9 9905 Lisp_Object subsidiaries;
1bfdaf10 9906 ptrdiff_t base_name_len = SBYTES (SYMBOL_NAME (base));
38182d90 9907 char *buf = alloca (base_name_len + 6);
df7492f9 9908 int i;
068a9dbd 9909
72af86bd 9910 memcpy (buf, SDATA (SYMBOL_NAME (base)), base_name_len);
25721f5b 9911 subsidiaries = make_uninit_vector (3);
df7492f9 9912 for (i = 0; i < 3; i++)
068a9dbd 9913 {
1bfdaf10 9914 strcpy (buf + base_name_len, suffixes[i]);
df7492f9 9915 ASET (subsidiaries, i, intern (buf));
068a9dbd 9916 }
df7492f9 9917 return subsidiaries;
068a9dbd
KH
9918}
9919
9920
df7492f9
KH
9921DEFUN ("define-coding-system-internal", Fdefine_coding_system_internal,
9922 Sdefine_coding_system_internal, coding_arg_max, MANY, 0,
1fcd6c8b
DL
9923 doc: /* For internal use only.
9924usage: (define-coding-system-internal ...) */)
f66c7cf8 9925 (ptrdiff_t nargs, Lisp_Object *args)
068a9dbd 9926{
df7492f9
KH
9927 Lisp_Object name;
9928 Lisp_Object spec_vec; /* [ ATTRS ALIASE EOL_TYPE ] */
9929 Lisp_Object attrs; /* Vector of attributes. */
9930 Lisp_Object eol_type;
9931 Lisp_Object aliases;
9932 Lisp_Object coding_type, charset_list, safe_charsets;
9933 enum coding_category category;
9934 Lisp_Object tail, val;
9935 int max_charset_id = 0;
9936 int i;
068a9dbd 9937
df7492f9
KH
9938 if (nargs < coding_arg_max)
9939 goto short_args;
068a9dbd 9940
df7492f9 9941 attrs = Fmake_vector (make_number (coding_attr_last_index), Qnil);
068a9dbd 9942
df7492f9
KH
9943 name = args[coding_arg_name];
9944 CHECK_SYMBOL (name);
4939150c 9945 ASET (attrs, coding_attr_base_name, name);
068a9dbd 9946
df7492f9
KH
9947 val = args[coding_arg_mnemonic];
9948 if (! STRINGP (val))
9949 CHECK_CHARACTER (val);
4939150c 9950 ASET (attrs, coding_attr_mnemonic, val);
068a9dbd 9951
df7492f9
KH
9952 coding_type = args[coding_arg_coding_type];
9953 CHECK_SYMBOL (coding_type);
4939150c 9954 ASET (attrs, coding_attr_type, coding_type);
068a9dbd 9955
df7492f9
KH
9956 charset_list = args[coding_arg_charset_list];
9957 if (SYMBOLP (charset_list))
9958 {
9959 if (EQ (charset_list, Qiso_2022))
9960 {
9961 if (! EQ (coding_type, Qiso_2022))
9962 error ("Invalid charset-list");
9963 charset_list = Viso_2022_charset_list;
9964 }
9965 else if (EQ (charset_list, Qemacs_mule))
9966 {
9967 if (! EQ (coding_type, Qemacs_mule))
9968 error ("Invalid charset-list");
9969 charset_list = Vemacs_mule_charset_list;
9970 }
9971 for (tail = charset_list; CONSP (tail); tail = XCDR (tail))
d311d28c
PE
9972 {
9973 if (! RANGED_INTEGERP (0, XCAR (tail), INT_MAX - 1))
9974 error ("Invalid charset-list");
9975 if (max_charset_id < XFASTINT (XCAR (tail)))
9976 max_charset_id = XFASTINT (XCAR (tail));
9977 }
df7492f9 9978 }
068a9dbd
KH
9979 else
9980 {
df7492f9 9981 charset_list = Fcopy_sequence (charset_list);
985773c9 9982 for (tail = charset_list; CONSP (tail); tail = XCDR (tail))
068a9dbd 9983 {
df7492f9
KH
9984 struct charset *charset;
9985
985773c9 9986 val = XCAR (tail);
df7492f9
KH
9987 CHECK_CHARSET_GET_CHARSET (val, charset);
9988 if (EQ (coding_type, Qiso_2022)
9989 ? CHARSET_ISO_FINAL (charset) < 0
9990 : EQ (coding_type, Qemacs_mule)
9991 ? CHARSET_EMACS_MULE_ID (charset) < 0
9992 : 0)
9993 error ("Can't handle charset `%s'",
8f924df7 9994 SDATA (SYMBOL_NAME (CHARSET_NAME (charset))));
df7492f9 9995
8f924df7 9996 XSETCAR (tail, make_number (charset->id));
df7492f9
KH
9997 if (max_charset_id < charset->id)
9998 max_charset_id = charset->id;
068a9dbd
KH
9999 }
10000 }
4939150c 10001 ASET (attrs, coding_attr_charset_list, charset_list);
068a9dbd 10002
1b3b981b
AS
10003 safe_charsets = make_uninit_string (max_charset_id + 1);
10004 memset (SDATA (safe_charsets), 255, max_charset_id + 1);
df7492f9 10005 for (tail = charset_list; CONSP (tail); tail = XCDR (tail))
8f924df7 10006 SSET (safe_charsets, XFASTINT (XCAR (tail)), 0);
4939150c 10007 ASET (attrs, coding_attr_safe_charsets, safe_charsets);
068a9dbd 10008
4939150c 10009 ASET (attrs, coding_attr_ascii_compat, args[coding_arg_ascii_compatible_p]);
3a73fa5d 10010
df7492f9 10011 val = args[coding_arg_decode_translation_table];
a6f87d34 10012 if (! CHAR_TABLE_P (val) && ! CONSP (val))
7d64c6ad 10013 CHECK_SYMBOL (val);
4939150c 10014 ASET (attrs, coding_attr_decode_tbl, val);
3a73fa5d 10015
df7492f9 10016 val = args[coding_arg_encode_translation_table];
a6f87d34 10017 if (! CHAR_TABLE_P (val) && ! CONSP (val))
7d64c6ad 10018 CHECK_SYMBOL (val);
4939150c 10019 ASET (attrs, coding_attr_encode_tbl, val);
d46c5b12 10020
df7492f9
KH
10021 val = args[coding_arg_post_read_conversion];
10022 CHECK_SYMBOL (val);
4939150c 10023 ASET (attrs, coding_attr_post_read, val);
d46c5b12 10024
df7492f9
KH
10025 val = args[coding_arg_pre_write_conversion];
10026 CHECK_SYMBOL (val);
4939150c 10027 ASET (attrs, coding_attr_pre_write, val);
3a73fa5d 10028
df7492f9
KH
10029 val = args[coding_arg_default_char];
10030 if (NILP (val))
4939150c 10031 ASET (attrs, coding_attr_default_char, make_number (' '));
df7492f9
KH
10032 else
10033 {
8f924df7 10034 CHECK_CHARACTER (val);
4939150c 10035 ASET (attrs, coding_attr_default_char, val);
df7492f9 10036 }
4031e2bf 10037
8f924df7 10038 val = args[coding_arg_for_unibyte];
4939150c 10039 ASET (attrs, coding_attr_for_unibyte, NILP (val) ? Qnil : Qt);
3a73fa5d 10040
df7492f9
KH
10041 val = args[coding_arg_plist];
10042 CHECK_LIST (val);
4939150c 10043 ASET (attrs, coding_attr_plist, val);
3a73fa5d 10044
df7492f9
KH
10045 if (EQ (coding_type, Qcharset))
10046 {
c7c66a95
KH
10047 /* Generate a lisp vector of 256 elements. Each element is nil,
10048 integer, or a list of charset IDs.
3a73fa5d 10049
c7c66a95
KH
10050 If Nth element is nil, the byte code N is invalid in this
10051 coding system.
4ed46869 10052
c7c66a95
KH
10053 If Nth element is a number NUM, N is the first byte of a
10054 charset whose ID is NUM.
4ed46869 10055
c7c66a95
KH
10056 If Nth element is a list of charset IDs, N is the first byte
10057 of one of them. The list is sorted by dimensions of the
ad1746f5 10058 charsets. A charset of smaller dimension comes first. */
df7492f9 10059 val = Fmake_vector (make_number (256), Qnil);
4ed46869 10060
5c99c2e6 10061 for (tail = charset_list; CONSP (tail); tail = XCDR (tail))
df7492f9 10062 {
c7c66a95
KH
10063 struct charset *charset = CHARSET_FROM_ID (XFASTINT (XCAR (tail)));
10064 int dim = CHARSET_DIMENSION (charset);
10065 int idx = (dim - 1) * 4;
4ed46869 10066
5c99c2e6 10067 if (CHARSET_ASCII_COMPATIBLE_P (charset))
4939150c 10068 ASET (attrs, coding_attr_ascii_compat, Qt);
4031e2bf 10069
15d143f7
KH
10070 for (i = charset->code_space[idx];
10071 i <= charset->code_space[idx + 1]; i++)
10072 {
c7c66a95
KH
10073 Lisp_Object tmp, tmp2;
10074 int dim2;
ec6d2bb8 10075
c7c66a95
KH
10076 tmp = AREF (val, i);
10077 if (NILP (tmp))
10078 tmp = XCAR (tail);
10079 else if (NUMBERP (tmp))
10080 {
10081 dim2 = CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (tmp)));
10082 if (dim < dim2)
c7c66a95 10083 tmp = Fcons (XCAR (tail), Fcons (tmp, Qnil));
f9d71dcd
KH
10084 else
10085 tmp = Fcons (tmp, Fcons (XCAR (tail), Qnil));
c7c66a95 10086 }
15d143f7 10087 else
c7c66a95
KH
10088 {
10089 for (tmp2 = tmp; CONSP (tmp2); tmp2 = XCDR (tmp2))
10090 {
10091 dim2 = CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (XCAR (tmp2))));
10092 if (dim < dim2)
10093 break;
10094 }
10095 if (NILP (tmp2))
10096 tmp = nconc2 (tmp, Fcons (XCAR (tail), Qnil));
10097 else
10098 {
10099 XSETCDR (tmp2, Fcons (XCAR (tmp2), XCDR (tmp2)));
10100 XSETCAR (tmp2, XCAR (tail));
10101 }
10102 }
10103 ASET (val, i, tmp);
15d143f7 10104 }
df7492f9
KH
10105 }
10106 ASET (attrs, coding_attr_charset_valids, val);
10107 category = coding_category_charset;
10108 }
10109 else if (EQ (coding_type, Qccl))
10110 {
10111 Lisp_Object valids;
ecec61c1 10112
df7492f9
KH
10113 if (nargs < coding_arg_ccl_max)
10114 goto short_args;
ecec61c1 10115
df7492f9
KH
10116 val = args[coding_arg_ccl_decoder];
10117 CHECK_CCL_PROGRAM (val);
10118 if (VECTORP (val))
10119 val = Fcopy_sequence (val);
10120 ASET (attrs, coding_attr_ccl_decoder, val);
ecec61c1 10121
df7492f9
KH
10122 val = args[coding_arg_ccl_encoder];
10123 CHECK_CCL_PROGRAM (val);
10124 if (VECTORP (val))
10125 val = Fcopy_sequence (val);
10126 ASET (attrs, coding_attr_ccl_encoder, val);
ecec61c1 10127
df7492f9
KH
10128 val = args[coding_arg_ccl_valids];
10129 valids = Fmake_string (make_number (256), make_number (0));
7d7bbefd 10130 for (tail = val; CONSP (tail); tail = XCDR (tail))
df7492f9 10131 {
8dcbea82 10132 int from, to;
ecec61c1 10133
34348bd4 10134 val = XCAR (tail);
df7492f9 10135 if (INTEGERP (val))
8dcbea82 10136 {
d311d28c 10137 if (! (0 <= XINT (val) && XINT (val) <= 255))
8dcbea82 10138 args_out_of_range_3 (val, make_number (0), make_number (255));
d311d28c 10139 from = to = XINT (val);
8dcbea82 10140 }
df7492f9
KH
10141 else
10142 {
df7492f9 10143 CHECK_CONS (val);
8f924df7 10144 CHECK_NATNUM_CAR (val);
d311d28c
PE
10145 CHECK_NUMBER_CDR (val);
10146 if (XINT (XCAR (val)) > 255)
8dcbea82
KH
10147 args_out_of_range_3 (XCAR (val),
10148 make_number (0), make_number (255));
d311d28c
PE
10149 from = XINT (XCAR (val));
10150 if (! (from <= XINT (XCDR (val)) && XINT (XCDR (val)) <= 255))
8dcbea82
KH
10151 args_out_of_range_3 (XCDR (val),
10152 XCAR (val), make_number (255));
d311d28c 10153 to = XINT (XCDR (val));
df7492f9 10154 }
8dcbea82 10155 for (i = from; i <= to; i++)
8f924df7 10156 SSET (valids, i, 1);
df7492f9
KH
10157 }
10158 ASET (attrs, coding_attr_ccl_valids, valids);
4ed46869 10159
df7492f9 10160 category = coding_category_ccl;
55ab7be3 10161 }
df7492f9 10162 else if (EQ (coding_type, Qutf_16))
55ab7be3 10163 {
df7492f9 10164 Lisp_Object bom, endian;
4ed46869 10165
4939150c 10166 ASET (attrs, coding_attr_ascii_compat, Qnil);
4ed46869 10167
df7492f9
KH
10168 if (nargs < coding_arg_utf16_max)
10169 goto short_args;
4ed46869 10170
df7492f9
KH
10171 bom = args[coding_arg_utf16_bom];
10172 if (! NILP (bom) && ! EQ (bom, Qt))
10173 {
10174 CHECK_CONS (bom);
8f924df7
KH
10175 val = XCAR (bom);
10176 CHECK_CODING_SYSTEM (val);
10177 val = XCDR (bom);
10178 CHECK_CODING_SYSTEM (val);
df7492f9 10179 }
a470d443 10180 ASET (attrs, coding_attr_utf_bom, bom);
df7492f9
KH
10181
10182 endian = args[coding_arg_utf16_endian];
b49a1807
KH
10183 CHECK_SYMBOL (endian);
10184 if (NILP (endian))
10185 endian = Qbig;
10186 else if (! EQ (endian, Qbig) && ! EQ (endian, Qlittle))
8f924df7 10187 error ("Invalid endian: %s", SDATA (SYMBOL_NAME (endian)));
df7492f9
KH
10188 ASET (attrs, coding_attr_utf_16_endian, endian);
10189
10190 category = (CONSP (bom)
10191 ? coding_category_utf_16_auto
10192 : NILP (bom)
b49a1807 10193 ? (EQ (endian, Qbig)
df7492f9
KH
10194 ? coding_category_utf_16_be_nosig
10195 : coding_category_utf_16_le_nosig)
b49a1807 10196 : (EQ (endian, Qbig)
df7492f9
KH
10197 ? coding_category_utf_16_be
10198 : coding_category_utf_16_le));
10199 }
10200 else if (EQ (coding_type, Qiso_2022))
10201 {
10202 Lisp_Object initial, reg_usage, request, flags;
1397dc18 10203
df7492f9
KH
10204 if (nargs < coding_arg_iso2022_max)
10205 goto short_args;
10206
10207 initial = Fcopy_sequence (args[coding_arg_iso2022_initial]);
10208 CHECK_VECTOR (initial);
10209 for (i = 0; i < 4; i++)
10210 {
9a9d91d9 10211 val = AREF (initial, i);
df7492f9
KH
10212 if (! NILP (val))
10213 {
584948ac
KH
10214 struct charset *charset;
10215
10216 CHECK_CHARSET_GET_CHARSET (val, charset);
10217 ASET (initial, i, make_number (CHARSET_ID (charset)));
10218 if (i == 0 && CHARSET_ASCII_COMPATIBLE_P (charset))
4939150c 10219 ASET (attrs, coding_attr_ascii_compat, Qt);
df7492f9
KH
10220 }
10221 else
10222 ASET (initial, i, make_number (-1));
10223 }
10224
10225 reg_usage = args[coding_arg_iso2022_reg_usage];
10226 CHECK_CONS (reg_usage);
8f924df7
KH
10227 CHECK_NUMBER_CAR (reg_usage);
10228 CHECK_NUMBER_CDR (reg_usage);
df7492f9
KH
10229
10230 request = Fcopy_sequence (args[coding_arg_iso2022_request]);
7d7bbefd 10231 for (tail = request; CONSP (tail); tail = XCDR (tail))
1397dc18 10232 {
df7492f9 10233 int id;
2735d060 10234 Lisp_Object tmp1;
df7492f9 10235
34348bd4 10236 val = XCAR (tail);
df7492f9 10237 CHECK_CONS (val);
2735d060
PE
10238 tmp1 = XCAR (val);
10239 CHECK_CHARSET_GET_ID (tmp1, id);
8f924df7 10240 CHECK_NATNUM_CDR (val);
df7492f9 10241 if (XINT (XCDR (val)) >= 4)
c2982e87 10242 error ("Invalid graphic register number: %"pI"d", XINT (XCDR (val)));
8f924df7 10243 XSETCAR (val, make_number (id));
1397dc18 10244 }
4ed46869 10245
df7492f9
KH
10246 flags = args[coding_arg_iso2022_flags];
10247 CHECK_NATNUM (flags);
d311d28c 10248 i = XINT (flags) & INT_MAX;
df7492f9 10249 if (EQ (args[coding_arg_charset_list], Qiso_2022))
d311d28c
PE
10250 i |= CODING_ISO_FLAG_FULL_SUPPORT;
10251 flags = make_number (i);
df7492f9
KH
10252
10253 ASET (attrs, coding_attr_iso_initial, initial);
10254 ASET (attrs, coding_attr_iso_usage, reg_usage);
10255 ASET (attrs, coding_attr_iso_request, request);
10256 ASET (attrs, coding_attr_iso_flags, flags);
10257 setup_iso_safe_charsets (attrs);
10258
10259 if (i & CODING_ISO_FLAG_SEVEN_BITS)
10260 category = ((i & (CODING_ISO_FLAG_LOCKING_SHIFT
10261 | CODING_ISO_FLAG_SINGLE_SHIFT))
10262 ? coding_category_iso_7_else
10263 : EQ (args[coding_arg_charset_list], Qiso_2022)
10264 ? coding_category_iso_7
10265 : coding_category_iso_7_tight);
10266 else
10267 {
10268 int id = XINT (AREF (initial, 1));
10269
c6fb6e98 10270 category = (((i & CODING_ISO_FLAG_LOCKING_SHIFT)
df7492f9
KH
10271 || EQ (args[coding_arg_charset_list], Qiso_2022)
10272 || id < 0)
10273 ? coding_category_iso_8_else
10274 : (CHARSET_DIMENSION (CHARSET_FROM_ID (id)) == 1)
10275 ? coding_category_iso_8_1
10276 : coding_category_iso_8_2);
10277 }
0ce7886f
KH
10278 if (category != coding_category_iso_8_1
10279 && category != coding_category_iso_8_2)
4939150c 10280 ASET (attrs, coding_attr_ascii_compat, Qnil);
df7492f9
KH
10281 }
10282 else if (EQ (coding_type, Qemacs_mule))
c28a9453 10283 {
df7492f9
KH
10284 if (EQ (args[coding_arg_charset_list], Qemacs_mule))
10285 ASET (attrs, coding_attr_emacs_mule_full, Qt);
4939150c 10286 ASET (attrs, coding_attr_ascii_compat, Qt);
df7492f9 10287 category = coding_category_emacs_mule;
c28a9453 10288 }
df7492f9 10289 else if (EQ (coding_type, Qshift_jis))
c28a9453 10290 {
df7492f9
KH
10291
10292 struct charset *charset;
10293
7d64c6ad 10294 if (XINT (Flength (charset_list)) != 3
6e07c25f 10295 && XINT (Flength (charset_list)) != 4)
7d64c6ad 10296 error ("There should be three or four charsets");
df7492f9
KH
10297
10298 charset = CHARSET_FROM_ID (XINT (XCAR (charset_list)));
10299 if (CHARSET_DIMENSION (charset) != 1)
10300 error ("Dimension of charset %s is not one",
8f924df7 10301 SDATA (SYMBOL_NAME (CHARSET_NAME (charset))));
584948ac 10302 if (CHARSET_ASCII_COMPATIBLE_P (charset))
4939150c 10303 ASET (attrs, coding_attr_ascii_compat, Qt);
df7492f9
KH
10304
10305 charset_list = XCDR (charset_list);
10306 charset = CHARSET_FROM_ID (XINT (XCAR (charset_list)));
10307 if (CHARSET_DIMENSION (charset) != 1)
10308 error ("Dimension of charset %s is not one",
8f924df7 10309 SDATA (SYMBOL_NAME (CHARSET_NAME (charset))));
df7492f9
KH
10310
10311 charset_list = XCDR (charset_list);
10312 charset = CHARSET_FROM_ID (XINT (XCAR (charset_list)));
10313 if (CHARSET_DIMENSION (charset) != 2)
7d64c6ad
KH
10314 error ("Dimension of charset %s is not two",
10315 SDATA (SYMBOL_NAME (CHARSET_NAME (charset))));
10316
10317 charset_list = XCDR (charset_list);
2b917a06
KH
10318 if (! NILP (charset_list))
10319 {
10320 charset = CHARSET_FROM_ID (XINT (XCAR (charset_list)));
10321 if (CHARSET_DIMENSION (charset) != 2)
10322 error ("Dimension of charset %s is not two",
10323 SDATA (SYMBOL_NAME (CHARSET_NAME (charset))));
10324 }
df7492f9
KH
10325
10326 category = coding_category_sjis;
10327 Vsjis_coding_system = name;
c28a9453 10328 }
df7492f9
KH
10329 else if (EQ (coding_type, Qbig5))
10330 {
10331 struct charset *charset;
4ed46869 10332
df7492f9
KH
10333 if (XINT (Flength (charset_list)) != 2)
10334 error ("There should be just two charsets");
10335
10336 charset = CHARSET_FROM_ID (XINT (XCAR (charset_list)));
10337 if (CHARSET_DIMENSION (charset) != 1)
10338 error ("Dimension of charset %s is not one",
8f924df7 10339 SDATA (SYMBOL_NAME (CHARSET_NAME (charset))));
584948ac 10340 if (CHARSET_ASCII_COMPATIBLE_P (charset))
4939150c 10341 ASET (attrs, coding_attr_ascii_compat, Qt);
df7492f9
KH
10342
10343 charset_list = XCDR (charset_list);
10344 charset = CHARSET_FROM_ID (XINT (XCAR (charset_list)));
10345 if (CHARSET_DIMENSION (charset) != 2)
10346 error ("Dimension of charset %s is not two",
8f924df7 10347 SDATA (SYMBOL_NAME (CHARSET_NAME (charset))));
4ed46869 10348
df7492f9
KH
10349 category = coding_category_big5;
10350 Vbig5_coding_system = name;
10351 }
10352 else if (EQ (coding_type, Qraw_text))
c28a9453 10353 {
584948ac 10354 category = coding_category_raw_text;
4939150c 10355 ASET (attrs, coding_attr_ascii_compat, Qt);
c28a9453 10356 }
df7492f9 10357 else if (EQ (coding_type, Qutf_8))
4ed46869 10358 {
a470d443
KH
10359 Lisp_Object bom;
10360
a470d443
KH
10361 if (nargs < coding_arg_utf8_max)
10362 goto short_args;
10363
10364 bom = args[coding_arg_utf8_bom];
10365 if (! NILP (bom) && ! EQ (bom, Qt))
10366 {
10367 CHECK_CONS (bom);
10368 val = XCAR (bom);
10369 CHECK_CODING_SYSTEM (val);
10370 val = XCDR (bom);
10371 CHECK_CODING_SYSTEM (val);
10372 }
10373 ASET (attrs, coding_attr_utf_bom, bom);
0e5317f7 10374 if (NILP (bom))
4939150c 10375 ASET (attrs, coding_attr_ascii_compat, Qt);
a470d443
KH
10376
10377 category = (CONSP (bom) ? coding_category_utf_8_auto
10378 : NILP (bom) ? coding_category_utf_8_nosig
10379 : coding_category_utf_8_sig);
4ed46869 10380 }
df7492f9 10381 else if (EQ (coding_type, Qundecided))
c3a7b3f4
KH
10382 {
10383 if (nargs < coding_arg_undecided_max)
10384 goto short_args;
10385 ASET (attrs, coding_attr_undecided_inhibit_null_byte_detection,
10386 args[coding_arg_undecided_inhibit_null_byte_detection]);
10387 ASET (attrs, coding_attr_undecided_inhibit_iso_escape_detection,
10388 args[coding_arg_undecided_inhibit_iso_escape_detection]);
10389 ASET (attrs, coding_attr_undecided_prefer_utf_8,
10390 args[coding_arg_undecided_prefer_utf_8]);
10391 category = coding_category_undecided;
10392 }
4ed46869 10393 else
df7492f9 10394 error ("Invalid coding system type: %s",
8f924df7 10395 SDATA (SYMBOL_NAME (coding_type)));
4ed46869 10396
4939150c
PE
10397 ASET (attrs, coding_attr_category, make_number (category));
10398 ASET (attrs, coding_attr_plist,
10399 Fcons (QCcategory,
10400 Fcons (AREF (Vcoding_category_table, category),
10401 CODING_ATTR_PLIST (attrs))));
10402 ASET (attrs, coding_attr_plist,
10403 Fcons (QCascii_compatible_p,
10404 Fcons (CODING_ATTR_ASCII_COMPAT (attrs),
10405 CODING_ATTR_PLIST (attrs))));
c4825358 10406
df7492f9
KH
10407 eol_type = args[coding_arg_eol_type];
10408 if (! NILP (eol_type)
10409 && ! EQ (eol_type, Qunix)
10410 && ! EQ (eol_type, Qdos)
10411 && ! EQ (eol_type, Qmac))
10412 error ("Invalid eol-type");
4ed46869 10413
df7492f9 10414 aliases = Fcons (name, Qnil);
4ed46869 10415
df7492f9
KH
10416 if (NILP (eol_type))
10417 {
10418 eol_type = make_subsidiaries (name);
10419 for (i = 0; i < 3; i++)
1397dc18 10420 {
df7492f9
KH
10421 Lisp_Object this_spec, this_name, this_aliases, this_eol_type;
10422
10423 this_name = AREF (eol_type, i);
10424 this_aliases = Fcons (this_name, Qnil);
10425 this_eol_type = (i == 0 ? Qunix : i == 1 ? Qdos : Qmac);
25721f5b
DA
10426 this_spec = make_uninit_vector (3);
10427 ASET (this_spec, 0, attrs);
df7492f9
KH
10428 ASET (this_spec, 1, this_aliases);
10429 ASET (this_spec, 2, this_eol_type);
10430 Fputhash (this_name, this_spec, Vcoding_system_hash_table);
10431 Vcoding_system_list = Fcons (this_name, Vcoding_system_list);
583f71ca
KH
10432 val = Fassoc (Fsymbol_name (this_name), Vcoding_system_alist);
10433 if (NILP (val))
10434 Vcoding_system_alist
10435 = Fcons (Fcons (Fsymbol_name (this_name), Qnil),
10436 Vcoding_system_alist);
1397dc18 10437 }
d46c5b12 10438 }
4ed46869 10439
25721f5b
DA
10440 spec_vec = make_uninit_vector (3);
10441 ASET (spec_vec, 0, attrs);
df7492f9
KH
10442 ASET (spec_vec, 1, aliases);
10443 ASET (spec_vec, 2, eol_type);
48b0f3ae 10444
df7492f9
KH
10445 Fputhash (name, spec_vec, Vcoding_system_hash_table);
10446 Vcoding_system_list = Fcons (name, Vcoding_system_list);
583f71ca
KH
10447 val = Fassoc (Fsymbol_name (name), Vcoding_system_alist);
10448 if (NILP (val))
10449 Vcoding_system_alist = Fcons (Fcons (Fsymbol_name (name), Qnil),
10450 Vcoding_system_alist);
48b0f3ae 10451
df7492f9
KH
10452 {
10453 int id = coding_categories[category].id;
48b0f3ae 10454
df7492f9
KH
10455 if (id < 0 || EQ (name, CODING_ID_NAME (id)))
10456 setup_coding_system (name, &coding_categories[category]);
10457 }
48b0f3ae 10458
d46c5b12 10459 return Qnil;
48b0f3ae 10460
df7492f9
KH
10461 short_args:
10462 return Fsignal (Qwrong_number_of_arguments,
10463 Fcons (intern ("define-coding-system-internal"),
10464 make_number (nargs)));
d46c5b12 10465}
4ed46869 10466
d6925f38 10467
a6f87d34
KH
10468DEFUN ("coding-system-put", Fcoding_system_put, Scoding_system_put,
10469 3, 3, 0,
10470 doc: /* Change value in CODING-SYSTEM's property list PROP to VAL. */)
5842a27b 10471 (Lisp_Object coding_system, Lisp_Object prop, Lisp_Object val)
a6f87d34 10472{
3dbe7859 10473 Lisp_Object spec, attrs;
a6f87d34
KH
10474
10475 CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec);
10476 attrs = AREF (spec, 0);
10477 if (EQ (prop, QCmnemonic))
10478 {
10479 if (! STRINGP (val))
10480 CHECK_CHARACTER (val);
4939150c 10481 ASET (attrs, coding_attr_mnemonic, val);
a6f87d34 10482 }
2133e2d1 10483 else if (EQ (prop, QCdefault_char))
a6f87d34
KH
10484 {
10485 if (NILP (val))
10486 val = make_number (' ');
10487 else
10488 CHECK_CHARACTER (val);
4939150c 10489 ASET (attrs, coding_attr_default_char, val);
a6f87d34
KH
10490 }
10491 else if (EQ (prop, QCdecode_translation_table))
10492 {
10493 if (! CHAR_TABLE_P (val) && ! CONSP (val))
10494 CHECK_SYMBOL (val);
4939150c 10495 ASET (attrs, coding_attr_decode_tbl, val);
a6f87d34
KH
10496 }
10497 else if (EQ (prop, QCencode_translation_table))
10498 {
10499 if (! CHAR_TABLE_P (val) && ! CONSP (val))
10500 CHECK_SYMBOL (val);
4939150c 10501 ASET (attrs, coding_attr_encode_tbl, val);
a6f87d34
KH
10502 }
10503 else if (EQ (prop, QCpost_read_conversion))
10504 {
10505 CHECK_SYMBOL (val);
4939150c 10506 ASET (attrs, coding_attr_post_read, val);
a6f87d34
KH
10507 }
10508 else if (EQ (prop, QCpre_write_conversion))
10509 {
10510 CHECK_SYMBOL (val);
4939150c 10511 ASET (attrs, coding_attr_pre_write, val);
a6f87d34 10512 }
35befdaa
KH
10513 else if (EQ (prop, QCascii_compatible_p))
10514 {
4939150c 10515 ASET (attrs, coding_attr_ascii_compat, val);
35befdaa 10516 }
a6f87d34 10517
4939150c
PE
10518 ASET (attrs, coding_attr_plist,
10519 Fplist_put (CODING_ATTR_PLIST (attrs), prop, val));
a6f87d34
KH
10520 return val;
10521}
10522
10523
df7492f9
KH
10524DEFUN ("define-coding-system-alias", Fdefine_coding_system_alias,
10525 Sdefine_coding_system_alias, 2, 2, 0,
10526 doc: /* Define ALIAS as an alias for CODING-SYSTEM. */)
5842a27b 10527 (Lisp_Object alias, Lisp_Object coding_system)
66cfb530 10528{
583f71ca 10529 Lisp_Object spec, aliases, eol_type, val;
4ed46869 10530
df7492f9
KH
10531 CHECK_SYMBOL (alias);
10532 CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec);
10533 aliases = AREF (spec, 1);
d4a1d553 10534 /* ALIASES should be a list of length more than zero, and the first
d6925f38
KH
10535 element is a base coding system. Append ALIAS at the tail of the
10536 list. */
df7492f9
KH
10537 while (!NILP (XCDR (aliases)))
10538 aliases = XCDR (aliases);
8f924df7 10539 XSETCDR (aliases, Fcons (alias, Qnil));
4ed46869 10540
df7492f9
KH
10541 eol_type = AREF (spec, 2);
10542 if (VECTORP (eol_type))
4ed46869 10543 {
df7492f9
KH
10544 Lisp_Object subsidiaries;
10545 int i;
4ed46869 10546
df7492f9
KH
10547 subsidiaries = make_subsidiaries (alias);
10548 for (i = 0; i < 3; i++)
10549 Fdefine_coding_system_alias (AREF (subsidiaries, i),
10550 AREF (eol_type, i));
4ed46869 10551 }
df7492f9
KH
10552
10553 Fputhash (alias, spec, Vcoding_system_hash_table);
d6925f38 10554 Vcoding_system_list = Fcons (alias, Vcoding_system_list);
583f71ca
KH
10555 val = Fassoc (Fsymbol_name (alias), Vcoding_system_alist);
10556 if (NILP (val))
10557 Vcoding_system_alist = Fcons (Fcons (Fsymbol_name (alias), Qnil),
10558 Vcoding_system_alist);
66cfb530 10559
4ed46869
KH
10560 return Qnil;
10561}
10562
a7ca3326 10563DEFUN ("coding-system-base", Fcoding_system_base, Scoding_system_base,
df7492f9
KH
10564 1, 1, 0,
10565 doc: /* Return the base of CODING-SYSTEM.
da7db224 10566Any alias or subsidiary coding system is not a base coding system. */)
5842a27b 10567 (Lisp_Object coding_system)
d46c5b12 10568{
df7492f9 10569 Lisp_Object spec, attrs;
d46c5b12 10570
df7492f9
KH
10571 if (NILP (coding_system))
10572 return (Qno_conversion);
10573 CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec);
10574 attrs = AREF (spec, 0);
10575 return CODING_ATTR_BASE_NAME (attrs);
10576}
1397dc18 10577
df7492f9
KH
10578DEFUN ("coding-system-plist", Fcoding_system_plist, Scoding_system_plist,
10579 1, 1, 0,
10580 doc: "Return the property list of CODING-SYSTEM.")
5842a27b 10581 (Lisp_Object coding_system)
df7492f9
KH
10582{
10583 Lisp_Object spec, attrs;
1397dc18 10584
df7492f9
KH
10585 if (NILP (coding_system))
10586 coding_system = Qno_conversion;
10587 CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec);
10588 attrs = AREF (spec, 0);
10589 return CODING_ATTR_PLIST (attrs);
d46c5b12
KH
10590}
10591
df7492f9
KH
10592
10593DEFUN ("coding-system-aliases", Fcoding_system_aliases, Scoding_system_aliases,
10594 1, 1, 0,
da7db224 10595 doc: /* Return the list of aliases of CODING-SYSTEM. */)
5842a27b 10596 (Lisp_Object coding_system)
66cfb530 10597{
df7492f9 10598 Lisp_Object spec;
84d60297 10599
df7492f9
KH
10600 if (NILP (coding_system))
10601 coding_system = Qno_conversion;
10602 CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec);
da7db224 10603 return AREF (spec, 1);
df7492f9 10604}
66cfb530 10605
a7ca3326 10606DEFUN ("coding-system-eol-type", Fcoding_system_eol_type,
df7492f9
KH
10607 Scoding_system_eol_type, 1, 1, 0,
10608 doc: /* Return eol-type of CODING-SYSTEM.
d4a1d553 10609An eol-type is an integer 0, 1, 2, or a vector of coding systems.
66cfb530 10610
df7492f9
KH
10611Integer values 0, 1, and 2 indicate a format of end-of-line; LF, CRLF,
10612and CR respectively.
66cfb530 10613
df7492f9
KH
10614A vector value indicates that a format of end-of-line should be
10615detected automatically. Nth element of the vector is the subsidiary
10616coding system whose eol-type is N. */)
5842a27b 10617 (Lisp_Object coding_system)
6b89e3aa 10618{
df7492f9
KH
10619 Lisp_Object spec, eol_type;
10620 int n;
6b89e3aa 10621
df7492f9
KH
10622 if (NILP (coding_system))
10623 coding_system = Qno_conversion;
10624 if (! CODING_SYSTEM_P (coding_system))
10625 return Qnil;
10626 spec = CODING_SYSTEM_SPEC (coding_system);
10627 eol_type = AREF (spec, 2);
10628 if (VECTORP (eol_type))
10629 return Fcopy_sequence (eol_type);
10630 n = EQ (eol_type, Qunix) ? 0 : EQ (eol_type, Qdos) ? 1 : 2;
10631 return make_number (n);
6b89e3aa
KH
10632}
10633
4ed46869
KH
10634#endif /* emacs */
10635
10636\f
1397dc18 10637/*** 9. Post-amble ***/
4ed46869 10638
dfcf069d 10639void
971de7fb 10640init_coding_once (void)
4ed46869
KH
10641{
10642 int i;
10643
df7492f9
KH
10644 for (i = 0; i < coding_category_max; i++)
10645 {
10646 coding_categories[i].id = -1;
10647 coding_priorities[i] = i;
10648 }
4ed46869
KH
10649
10650 /* ISO2022 specific initialize routine. */
10651 for (i = 0; i < 0x20; i++)
b73bfc1c 10652 iso_code_class[i] = ISO_control_0;
4ed46869
KH
10653 for (i = 0x21; i < 0x7F; i++)
10654 iso_code_class[i] = ISO_graphic_plane_0;
10655 for (i = 0x80; i < 0xA0; i++)
b73bfc1c 10656 iso_code_class[i] = ISO_control_1;
4ed46869
KH
10657 for (i = 0xA1; i < 0xFF; i++)
10658 iso_code_class[i] = ISO_graphic_plane_1;
10659 iso_code_class[0x20] = iso_code_class[0x7F] = ISO_0x20_or_0x7F;
10660 iso_code_class[0xA0] = iso_code_class[0xFF] = ISO_0xA0_or_0xFF;
4ed46869
KH
10661 iso_code_class[ISO_CODE_SO] = ISO_shift_out;
10662 iso_code_class[ISO_CODE_SI] = ISO_shift_in;
10663 iso_code_class[ISO_CODE_SS2_7] = ISO_single_shift_2_7;
10664 iso_code_class[ISO_CODE_ESC] = ISO_escape;
10665 iso_code_class[ISO_CODE_SS2] = ISO_single_shift_2;
10666 iso_code_class[ISO_CODE_SS3] = ISO_single_shift_3;
10667 iso_code_class[ISO_CODE_CSI] = ISO_control_sequence_introducer;
10668
df7492f9
KH
10669 for (i = 0; i < 256; i++)
10670 {
10671 emacs_mule_bytes[i] = 1;
10672 }
7c78e542
KH
10673 emacs_mule_bytes[EMACS_MULE_LEADING_CODE_PRIVATE_11] = 3;
10674 emacs_mule_bytes[EMACS_MULE_LEADING_CODE_PRIVATE_12] = 3;
10675 emacs_mule_bytes[EMACS_MULE_LEADING_CODE_PRIVATE_21] = 4;
10676 emacs_mule_bytes[EMACS_MULE_LEADING_CODE_PRIVATE_22] = 4;
e0e989f6
KH
10677}
10678
10679#ifdef emacs
10680
dfcf069d 10681void
971de7fb 10682syms_of_coding (void)
e0e989f6 10683{
df7492f9 10684 staticpro (&Vcoding_system_hash_table);
8f924df7
KH
10685 {
10686 Lisp_Object args[2];
10687 args[0] = QCtest;
10688 args[1] = Qeq;
10689 Vcoding_system_hash_table = Fmake_hash_table (2, args);
10690 }
df7492f9
KH
10691
10692 staticpro (&Vsjis_coding_system);
10693 Vsjis_coding_system = Qnil;
e0e989f6 10694
df7492f9
KH
10695 staticpro (&Vbig5_coding_system);
10696 Vbig5_coding_system = Qnil;
10697
24a73b0a
KH
10698 staticpro (&Vcode_conversion_reused_workbuf);
10699 Vcode_conversion_reused_workbuf = Qnil;
10700
10701 staticpro (&Vcode_conversion_workbuf_name);
2a0213a6 10702 Vcode_conversion_workbuf_name = build_pure_c_string (" *code-conversion-work*");
e0e989f6 10703
24a73b0a 10704 reused_workbuf_in_use = 0;
df7492f9
KH
10705
10706 DEFSYM (Qcharset, "charset");
10707 DEFSYM (Qtarget_idx, "target-idx");
10708 DEFSYM (Qcoding_system_history, "coding-system-history");
bb0115a2
RS
10709 Fset (Qcoding_system_history, Qnil);
10710
9ce27fde 10711 /* Target FILENAME is the first argument. */
e0e989f6 10712 Fput (Qinsert_file_contents, Qtarget_idx, make_number (0));
9ce27fde 10713 /* Target FILENAME is the third argument. */
e0e989f6
KH
10714 Fput (Qwrite_region, Qtarget_idx, make_number (2));
10715
df7492f9 10716 DEFSYM (Qcall_process, "call-process");
9ce27fde 10717 /* Target PROGRAM is the first argument. */
e0e989f6
KH
10718 Fput (Qcall_process, Qtarget_idx, make_number (0));
10719
df7492f9 10720 DEFSYM (Qcall_process_region, "call-process-region");
9ce27fde 10721 /* Target PROGRAM is the third argument. */
e0e989f6
KH
10722 Fput (Qcall_process_region, Qtarget_idx, make_number (2));
10723
df7492f9 10724 DEFSYM (Qstart_process, "start-process");
9ce27fde 10725 /* Target PROGRAM is the third argument. */
e0e989f6
KH
10726 Fput (Qstart_process, Qtarget_idx, make_number (2));
10727
df7492f9 10728 DEFSYM (Qopen_network_stream, "open-network-stream");
9ce27fde 10729 /* Target SERVICE is the fourth argument. */
e0e989f6
KH
10730 Fput (Qopen_network_stream, Qtarget_idx, make_number (3));
10731
df7492f9
KH
10732 DEFSYM (Qcoding_system, "coding-system");
10733 DEFSYM (Qcoding_aliases, "coding-aliases");
4ed46869 10734
df7492f9
KH
10735 DEFSYM (Qeol_type, "eol-type");
10736 DEFSYM (Qunix, "unix");
10737 DEFSYM (Qdos, "dos");
4b298d5a 10738 DEFSYM (Qmac, "mac");
4ed46869 10739
df7492f9
KH
10740 DEFSYM (Qbuffer_file_coding_system, "buffer-file-coding-system");
10741 DEFSYM (Qpost_read_conversion, "post-read-conversion");
10742 DEFSYM (Qpre_write_conversion, "pre-write-conversion");
10743 DEFSYM (Qdefault_char, "default-char");
10744 DEFSYM (Qundecided, "undecided");
10745 DEFSYM (Qno_conversion, "no-conversion");
10746 DEFSYM (Qraw_text, "raw-text");
4ed46869 10747
df7492f9 10748 DEFSYM (Qiso_2022, "iso-2022");
4ed46869 10749
df7492f9 10750 DEFSYM (Qutf_8, "utf-8");
8f924df7 10751 DEFSYM (Qutf_8_emacs, "utf-8-emacs");
27901516 10752
7f590b0c 10753#if defined (WINDOWSNT) || defined (CYGWIN)
ba116008
DC
10754 /* No, not utf-16-le: that one has a BOM. */
10755 DEFSYM (Qutf_16le, "utf-16le");
10756#endif
10757
df7492f9 10758 DEFSYM (Qutf_16, "utf-16");
df7492f9
KH
10759 DEFSYM (Qbig, "big");
10760 DEFSYM (Qlittle, "little");
27901516 10761
df7492f9
KH
10762 DEFSYM (Qshift_jis, "shift-jis");
10763 DEFSYM (Qbig5, "big5");
4ed46869 10764
df7492f9 10765 DEFSYM (Qcoding_system_p, "coding-system-p");
4ed46869 10766
df7492f9 10767 DEFSYM (Qcoding_system_error, "coding-system-error");
4ed46869 10768 Fput (Qcoding_system_error, Qerror_conditions,
3438fe21 10769 listn (CONSTYPE_PURE, 2, Qcoding_system_error, Qerror));
4ed46869 10770 Fput (Qcoding_system_error, Qerror_message,
2a0213a6 10771 build_pure_c_string ("Invalid coding system"));
4ed46869 10772
05e6f5dc
KH
10773 /* Intern this now in case it isn't already done.
10774 Setting this variable twice is harmless.
10775 But don't staticpro it here--that is done in alloc.c. */
d67b4f80 10776 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
70c22245 10777
df7492f9 10778 DEFSYM (Qtranslation_table, "translation-table");
433f7f87 10779 Fput (Qtranslation_table, Qchar_table_extra_slots, make_number (2));
df7492f9
KH
10780 DEFSYM (Qtranslation_table_id, "translation-table-id");
10781 DEFSYM (Qtranslation_table_for_decode, "translation-table-for-decode");
10782 DEFSYM (Qtranslation_table_for_encode, "translation-table-for-encode");
1397dc18 10783
df7492f9 10784 DEFSYM (Qvalid_codes, "valid-codes");
9ce27fde 10785
df7492f9 10786 DEFSYM (Qemacs_mule, "emacs-mule");
d46c5b12 10787
01378f49 10788 DEFSYM (QCcategory, ":category");
a6f87d34 10789 DEFSYM (QCmnemonic, ":mnemonic");
2133e2d1 10790 DEFSYM (QCdefault_char, ":default-char");
a6f87d34
KH
10791 DEFSYM (QCdecode_translation_table, ":decode-translation-table");
10792 DEFSYM (QCencode_translation_table, ":encode-translation-table");
10793 DEFSYM (QCpost_read_conversion, ":post-read-conversion");
10794 DEFSYM (QCpre_write_conversion, ":pre-write-conversion");
35befdaa 10795 DEFSYM (QCascii_compatible_p, ":ascii-compatible-p");
01378f49 10796
df7492f9
KH
10797 Vcoding_category_table
10798 = Fmake_vector (make_number (coding_category_max), Qnil);
10799 staticpro (&Vcoding_category_table);
10800 /* Followings are target of code detection. */
10801 ASET (Vcoding_category_table, coding_category_iso_7,
d67b4f80 10802 intern_c_string ("coding-category-iso-7"));
df7492f9 10803 ASET (Vcoding_category_table, coding_category_iso_7_tight,
d67b4f80 10804 intern_c_string ("coding-category-iso-7-tight"));
df7492f9 10805 ASET (Vcoding_category_table, coding_category_iso_8_1,
d67b4f80 10806 intern_c_string ("coding-category-iso-8-1"));
df7492f9 10807 ASET (Vcoding_category_table, coding_category_iso_8_2,
d67b4f80 10808 intern_c_string ("coding-category-iso-8-2"));
df7492f9 10809 ASET (Vcoding_category_table, coding_category_iso_7_else,
d67b4f80 10810 intern_c_string ("coding-category-iso-7-else"));
df7492f9 10811 ASET (Vcoding_category_table, coding_category_iso_8_else,
d67b4f80 10812 intern_c_string ("coding-category-iso-8-else"));
a470d443 10813 ASET (Vcoding_category_table, coding_category_utf_8_auto,
d67b4f80 10814 intern_c_string ("coding-category-utf-8-auto"));
a470d443 10815 ASET (Vcoding_category_table, coding_category_utf_8_nosig,
d67b4f80 10816 intern_c_string ("coding-category-utf-8"));
a470d443 10817 ASET (Vcoding_category_table, coding_category_utf_8_sig,
d67b4f80 10818 intern_c_string ("coding-category-utf-8-sig"));
df7492f9 10819 ASET (Vcoding_category_table, coding_category_utf_16_be,
d67b4f80 10820 intern_c_string ("coding-category-utf-16-be"));
ff563fce 10821 ASET (Vcoding_category_table, coding_category_utf_16_auto,
d67b4f80 10822 intern_c_string ("coding-category-utf-16-auto"));
df7492f9 10823 ASET (Vcoding_category_table, coding_category_utf_16_le,
d67b4f80 10824 intern_c_string ("coding-category-utf-16-le"));
df7492f9 10825 ASET (Vcoding_category_table, coding_category_utf_16_be_nosig,
d67b4f80 10826 intern_c_string ("coding-category-utf-16-be-nosig"));
df7492f9 10827 ASET (Vcoding_category_table, coding_category_utf_16_le_nosig,
d67b4f80 10828 intern_c_string ("coding-category-utf-16-le-nosig"));
df7492f9 10829 ASET (Vcoding_category_table, coding_category_charset,
d67b4f80 10830 intern_c_string ("coding-category-charset"));
df7492f9 10831 ASET (Vcoding_category_table, coding_category_sjis,
d67b4f80 10832 intern_c_string ("coding-category-sjis"));
df7492f9 10833 ASET (Vcoding_category_table, coding_category_big5,
d67b4f80 10834 intern_c_string ("coding-category-big5"));
df7492f9 10835 ASET (Vcoding_category_table, coding_category_ccl,
d67b4f80 10836 intern_c_string ("coding-category-ccl"));
df7492f9 10837 ASET (Vcoding_category_table, coding_category_emacs_mule,
d67b4f80 10838 intern_c_string ("coding-category-emacs-mule"));
df7492f9
KH
10839 /* Followings are NOT target of code detection. */
10840 ASET (Vcoding_category_table, coding_category_raw_text,
d67b4f80 10841 intern_c_string ("coding-category-raw-text"));
df7492f9 10842 ASET (Vcoding_category_table, coding_category_undecided,
d67b4f80 10843 intern_c_string ("coding-category-undecided"));
ecf488bc 10844
065e3595 10845 DEFSYM (Qinsufficient_source, "insufficient-source");
065e3595
KH
10846 DEFSYM (Qinvalid_source, "invalid-source");
10847 DEFSYM (Qinterrupted, "interrupted");
44e8490d 10848 DEFSYM (Qcoding_system_define_form, "coding-system-define-form");
065e3595 10849
4ed46869
KH
10850 defsubr (&Scoding_system_p);
10851 defsubr (&Sread_coding_system);
10852 defsubr (&Sread_non_nil_coding_system);
10853 defsubr (&Scheck_coding_system);
10854 defsubr (&Sdetect_coding_region);
d46c5b12 10855 defsubr (&Sdetect_coding_string);
05e6f5dc 10856 defsubr (&Sfind_coding_systems_region_internal);
068a9dbd 10857 defsubr (&Sunencodable_char_position);
df7492f9 10858 defsubr (&Scheck_coding_systems_region);
4ed46869
KH
10859 defsubr (&Sdecode_coding_region);
10860 defsubr (&Sencode_coding_region);
10861 defsubr (&Sdecode_coding_string);
10862 defsubr (&Sencode_coding_string);
10863 defsubr (&Sdecode_sjis_char);
10864 defsubr (&Sencode_sjis_char);
10865 defsubr (&Sdecode_big5_char);
10866 defsubr (&Sencode_big5_char);
1ba9e4ab 10867 defsubr (&Sset_terminal_coding_system_internal);
c4825358 10868 defsubr (&Sset_safe_terminal_coding_system_internal);
4ed46869 10869 defsubr (&Sterminal_coding_system);
1ba9e4ab 10870 defsubr (&Sset_keyboard_coding_system_internal);
4ed46869 10871 defsubr (&Skeyboard_coding_system);
a5d301df 10872 defsubr (&Sfind_operation_coding_system);
df7492f9 10873 defsubr (&Sset_coding_system_priority);
6b89e3aa 10874 defsubr (&Sdefine_coding_system_internal);
df7492f9 10875 defsubr (&Sdefine_coding_system_alias);
a6f87d34 10876 defsubr (&Scoding_system_put);
df7492f9
KH
10877 defsubr (&Scoding_system_base);
10878 defsubr (&Scoding_system_plist);
10879 defsubr (&Scoding_system_aliases);
10880 defsubr (&Scoding_system_eol_type);
10881 defsubr (&Scoding_system_priority_list);
4ed46869 10882
29208e82 10883 DEFVAR_LISP ("coding-system-list", Vcoding_system_list,
48b0f3ae
PJ
10884 doc: /* List of coding systems.
10885
10886Do not alter the value of this variable manually. This variable should be
df7492f9 10887updated by the functions `define-coding-system' and
48b0f3ae 10888`define-coding-system-alias'. */);
4608c386
KH
10889 Vcoding_system_list = Qnil;
10890
29208e82 10891 DEFVAR_LISP ("coding-system-alist", Vcoding_system_alist,
48b0f3ae
PJ
10892 doc: /* Alist of coding system names.
10893Each element is one element list of coding system name.
446dcd75 10894This variable is given to `completing-read' as COLLECTION argument.
48b0f3ae
PJ
10895
10896Do not alter the value of this variable manually. This variable should be
10897updated by the functions `make-coding-system' and
10898`define-coding-system-alias'. */);
4608c386
KH
10899 Vcoding_system_alist = Qnil;
10900
29208e82 10901 DEFVAR_LISP ("coding-category-list", Vcoding_category_list,
48b0f3ae
PJ
10902 doc: /* List of coding-categories (symbols) ordered by priority.
10903
10904On detecting a coding system, Emacs tries code detection algorithms
10905associated with each coding-category one by one in this order. When
10906one algorithm agrees with a byte sequence of source text, the coding
0ec31faf
KH
10907system bound to the corresponding coding-category is selected.
10908
448e17d6 10909Don't modify this variable directly, but use `set-coding-system-priority'. */);
4ed46869
KH
10910 {
10911 int i;
10912
10913 Vcoding_category_list = Qnil;
df7492f9 10914 for (i = coding_category_max - 1; i >= 0; i--)
4ed46869 10915 Vcoding_category_list
28be1ada 10916 = Fcons (AREF (Vcoding_category_table, i),
d46c5b12 10917 Vcoding_category_list);
4ed46869
KH
10918 }
10919
29208e82 10920 DEFVAR_LISP ("coding-system-for-read", Vcoding_system_for_read,
48b0f3ae
PJ
10921 doc: /* Specify the coding system for read operations.
10922It is useful to bind this variable with `let', but do not set it globally.
10923If the value is a coding system, it is used for decoding on read operation.
446dcd75
JB
10924If not, an appropriate element is used from one of the coding system alists.
10925There are three such tables: `file-coding-system-alist',
48b0f3ae 10926`process-coding-system-alist', and `network-coding-system-alist'. */);
4ed46869
KH
10927 Vcoding_system_for_read = Qnil;
10928
29208e82 10929 DEFVAR_LISP ("coding-system-for-write", Vcoding_system_for_write,
48b0f3ae
PJ
10930 doc: /* Specify the coding system for write operations.
10931Programs bind this variable with `let', but you should not set it globally.
10932If the value is a coding system, it is used for encoding of output,
10933when writing it to a file and when sending it to a file or subprocess.
10934
10935If this does not specify a coding system, an appropriate element
446dcd75
JB
10936is used from one of the coding system alists.
10937There are three such tables: `file-coding-system-alist',
48b0f3ae
PJ
10938`process-coding-system-alist', and `network-coding-system-alist'.
10939For output to files, if the above procedure does not specify a coding system,
10940the value of `buffer-file-coding-system' is used. */);
4ed46869
KH
10941 Vcoding_system_for_write = Qnil;
10942
29208e82 10943 DEFVAR_LISP ("last-coding-system-used", Vlast_coding_system_used,
df7492f9
KH
10944 doc: /*
10945Coding system used in the latest file or process I/O. */);
4ed46869
KH
10946 Vlast_coding_system_used = Qnil;
10947
29208e82 10948 DEFVAR_LISP ("last-code-conversion-error", Vlast_code_conversion_error,
065e3595
KH
10949 doc: /*
10950Error status of the last code conversion.
10951
10952When an error was detected in the last code conversion, this variable
10953is set to one of the following symbols.
10954 `insufficient-source'
10955 `inconsistent-eol'
10956 `invalid-source'
10957 `interrupted'
10958 `insufficient-memory'
10959When no error was detected, the value doesn't change. So, to check
10960the error status of a code conversion by this variable, you must
10961explicitly set this variable to nil before performing code
10962conversion. */);
10963 Vlast_code_conversion_error = Qnil;
10964
29208e82 10965 DEFVAR_BOOL ("inhibit-eol-conversion", inhibit_eol_conversion,
df7492f9
KH
10966 doc: /*
10967*Non-nil means always inhibit code conversion of end-of-line format.
48b0f3ae
PJ
10968See info node `Coding Systems' and info node `Text and Binary' concerning
10969such conversion. */);
9ce27fde
KH
10970 inhibit_eol_conversion = 0;
10971
29208e82 10972 DEFVAR_BOOL ("inherit-process-coding-system", inherit_process_coding_system,
df7492f9
KH
10973 doc: /*
10974Non-nil means process buffer inherits coding system of process output.
48b0f3ae
PJ
10975Bind it to t if the process output is to be treated as if it were a file
10976read from some filesystem. */);
ed29121d
EZ
10977 inherit_process_coding_system = 0;
10978
29208e82 10979 DEFVAR_LISP ("file-coding-system-alist", Vfile_coding_system_alist,
df7492f9
KH
10980 doc: /*
10981Alist to decide a coding system to use for a file I/O operation.
48b0f3ae
PJ
10982The format is ((PATTERN . VAL) ...),
10983where PATTERN is a regular expression matching a file name,
10984VAL is a coding system, a cons of coding systems, or a function symbol.
10985If VAL is a coding system, it is used for both decoding and encoding
10986the file contents.
10987If VAL is a cons of coding systems, the car part is used for decoding,
10988and the cdr part is used for encoding.
10989If VAL is a function symbol, the function must return a coding system
2c53e699
KH
10990or a cons of coding systems which are used as above. The function is
10991called with an argument that is a list of the arguments with which
5a0bbd9a
KH
10992`find-operation-coding-system' was called. If the function can't decide
10993a coding system, it can return `undecided' so that the normal
10994code-detection is performed.
48b0f3ae
PJ
10995
10996See also the function `find-operation-coding-system'
10997and the variable `auto-coding-alist'. */);
02ba4723
KH
10998 Vfile_coding_system_alist = Qnil;
10999
29208e82 11000 DEFVAR_LISP ("process-coding-system-alist", Vprocess_coding_system_alist,
df7492f9
KH
11001 doc: /*
11002Alist to decide a coding system to use for a process I/O operation.
48b0f3ae
PJ
11003The format is ((PATTERN . VAL) ...),
11004where PATTERN is a regular expression matching a program name,
11005VAL is a coding system, a cons of coding systems, or a function symbol.
11006If VAL is a coding system, it is used for both decoding what received
11007from the program and encoding what sent to the program.
11008If VAL is a cons of coding systems, the car part is used for decoding,
11009and the cdr part is used for encoding.
11010If VAL is a function symbol, the function must return a coding system
11011or a cons of coding systems which are used as above.
11012
11013See also the function `find-operation-coding-system'. */);
02ba4723
KH
11014 Vprocess_coding_system_alist = Qnil;
11015
29208e82 11016 DEFVAR_LISP ("network-coding-system-alist", Vnetwork_coding_system_alist,
df7492f9
KH
11017 doc: /*
11018Alist to decide a coding system to use for a network I/O operation.
48b0f3ae
PJ
11019The format is ((PATTERN . VAL) ...),
11020where PATTERN is a regular expression matching a network service name
11021or is a port number to connect to,
11022VAL is a coding system, a cons of coding systems, or a function symbol.
11023If VAL is a coding system, it is used for both decoding what received
11024from the network stream and encoding what sent to the network stream.
11025If VAL is a cons of coding systems, the car part is used for decoding,
11026and the cdr part is used for encoding.
11027If VAL is a function symbol, the function must return a coding system
11028or a cons of coding systems which are used as above.
11029
11030See also the function `find-operation-coding-system'. */);
02ba4723 11031 Vnetwork_coding_system_alist = Qnil;
4ed46869 11032
29208e82 11033 DEFVAR_LISP ("locale-coding-system", Vlocale_coding_system,
75205970
RS
11034 doc: /* Coding system to use with system messages.
11035Also used for decoding keyboard input on X Window system. */);
68c45bf0
PE
11036 Vlocale_coding_system = Qnil;
11037
005f0d35 11038 /* The eol mnemonics are reset in startup.el system-dependently. */
29208e82 11039 DEFVAR_LISP ("eol-mnemonic-unix", eol_mnemonic_unix,
df7492f9
KH
11040 doc: /*
11041*String displayed in mode line for UNIX-like (LF) end-of-line format. */);
2a0213a6 11042 eol_mnemonic_unix = build_pure_c_string (":");
4ed46869 11043
29208e82 11044 DEFVAR_LISP ("eol-mnemonic-dos", eol_mnemonic_dos,
df7492f9
KH
11045 doc: /*
11046*String displayed in mode line for DOS-like (CRLF) end-of-line format. */);
2a0213a6 11047 eol_mnemonic_dos = build_pure_c_string ("\\");
4ed46869 11048
29208e82 11049 DEFVAR_LISP ("eol-mnemonic-mac", eol_mnemonic_mac,
df7492f9
KH
11050 doc: /*
11051*String displayed in mode line for MAC-like (CR) end-of-line format. */);
2a0213a6 11052 eol_mnemonic_mac = build_pure_c_string ("/");
4ed46869 11053
29208e82 11054 DEFVAR_LISP ("eol-mnemonic-undecided", eol_mnemonic_undecided,
df7492f9
KH
11055 doc: /*
11056*String displayed in mode line when end-of-line format is not yet determined. */);
2a0213a6 11057 eol_mnemonic_undecided = build_pure_c_string (":");
4ed46869 11058
29208e82 11059 DEFVAR_LISP ("enable-character-translation", Venable_character_translation,
df7492f9
KH
11060 doc: /*
11061*Non-nil enables character translation while encoding and decoding. */);
84fbb8a0 11062 Venable_character_translation = Qt;
bdd9fb48 11063
f967223b 11064 DEFVAR_LISP ("standard-translation-table-for-decode",
29208e82 11065 Vstandard_translation_table_for_decode,
48b0f3ae 11066 doc: /* Table for translating characters while decoding. */);
f967223b 11067 Vstandard_translation_table_for_decode = Qnil;
bdd9fb48 11068
f967223b 11069 DEFVAR_LISP ("standard-translation-table-for-encode",
29208e82 11070 Vstandard_translation_table_for_encode,
48b0f3ae 11071 doc: /* Table for translating characters while encoding. */);
f967223b 11072 Vstandard_translation_table_for_encode = Qnil;
4ed46869 11073
29208e82 11074 DEFVAR_LISP ("charset-revision-table", Vcharset_revision_table,
48b0f3ae
PJ
11075 doc: /* Alist of charsets vs revision numbers.
11076While encoding, if a charset (car part of an element) is found,
df7492f9
KH
11077designate it with the escape sequence identifying revision (cdr part
11078of the element). */);
11079 Vcharset_revision_table = Qnil;
02ba4723
KH
11080
11081 DEFVAR_LISP ("default-process-coding-system",
29208e82 11082 Vdefault_process_coding_system,
48b0f3ae
PJ
11083 doc: /* Cons of coding systems used for process I/O by default.
11084The car part is used for decoding a process output,
11085the cdr part is used for encoding a text to be sent to a process. */);
02ba4723 11086 Vdefault_process_coding_system = Qnil;
c4825358 11087
29208e82 11088 DEFVAR_LISP ("latin-extra-code-table", Vlatin_extra_code_table,
df7492f9
KH
11089 doc: /*
11090Table of extra Latin codes in the range 128..159 (inclusive).
48b0f3ae
PJ
11091This is a vector of length 256.
11092If Nth element is non-nil, the existence of code N in a file
11093\(or output of subprocess) doesn't prevent it to be detected as
11094a coding system of ISO 2022 variant which has a flag
11095`accept-latin-extra-code' t (e.g. iso-latin-1) on reading a file
11096or reading output of a subprocess.
446dcd75 11097Only 128th through 159th elements have a meaning. */);
3f003981 11098 Vlatin_extra_code_table = Fmake_vector (make_number (256), Qnil);
d46c5b12
KH
11099
11100 DEFVAR_LISP ("select-safe-coding-system-function",
29208e82 11101 Vselect_safe_coding_system_function,
df7492f9
KH
11102 doc: /*
11103Function to call to select safe coding system for encoding a text.
48b0f3ae
PJ
11104
11105If set, this function is called to force a user to select a proper
11106coding system which can encode the text in the case that a default
fdecf907
GM
11107coding system used in each operation can't encode the text. The
11108function should take care that the buffer is not modified while
11109the coding system is being selected.
48b0f3ae
PJ
11110
11111The default value is `select-safe-coding-system' (which see). */);
d46c5b12
KH
11112 Vselect_safe_coding_system_function = Qnil;
11113
5d5bf4d8 11114 DEFVAR_BOOL ("coding-system-require-warning",
29208e82 11115 coding_system_require_warning,
5d5bf4d8 11116 doc: /* Internal use only.
6b89e3aa
KH
11117If non-nil, on writing a file, `select-safe-coding-system-function' is
11118called even if `coding-system-for-write' is non-nil. The command
11119`universal-coding-system-argument' binds this variable to t temporarily. */);
5d5bf4d8
KH
11120 coding_system_require_warning = 0;
11121
11122
22ab2303 11123 DEFVAR_BOOL ("inhibit-iso-escape-detection",
29208e82 11124 inhibit_iso_escape_detection,
df7492f9 11125 doc: /*
97b1b294 11126If non-nil, Emacs ignores ISO-2022 escape sequences during code detection.
48b0f3ae 11127
97b1b294
EZ
11128When Emacs reads text, it tries to detect how the text is encoded.
11129This code detection is sensitive to escape sequences. If Emacs sees
11130a valid ISO-2022 escape sequence, it assumes the text is encoded in one
11131of the ISO2022 encodings, and decodes text by the corresponding coding
11132system (e.g. `iso-2022-7bit').
48b0f3ae
PJ
11133
11134However, there may be a case that you want to read escape sequences in
11135a file as is. In such a case, you can set this variable to non-nil.
97b1b294
EZ
11136Then the code detection will ignore any escape sequences, and no text is
11137detected as encoded in some ISO-2022 encoding. The result is that all
48b0f3ae
PJ
11138escape sequences become visible in a buffer.
11139
11140The default value is nil, and it is strongly recommended not to change
11141it. That is because many Emacs Lisp source files that contain
11142non-ASCII characters are encoded by the coding system `iso-2022-7bit'
11143in Emacs's distribution, and they won't be decoded correctly on
11144reading if you suppress escape sequence detection.
11145
11146The other way to read escape sequences in a file without decoding is
97b1b294 11147to explicitly specify some coding system that doesn't use ISO-2022
65e7ca35 11148escape sequence (e.g., `latin-1') on reading by \\[universal-coding-system-argument]. */);
74383408 11149 inhibit_iso_escape_detection = 0;
002fdb44 11150
97b1b294 11151 DEFVAR_BOOL ("inhibit-null-byte-detection",
29208e82 11152 inhibit_null_byte_detection,
97b1b294
EZ
11153 doc: /* If non-nil, Emacs ignores null bytes on code detection.
11154By default, Emacs treats it as binary data, and does not attempt to
11155decode it. The effect is as if you specified `no-conversion' for
11156reading that text.
11157
11158Set this to non-nil when a regular text happens to include null bytes.
11159Examples are Index nodes of Info files and null-byte delimited output
11160from GNU Find and GNU Grep. Emacs will then ignore the null bytes and
11161decode text as usual. */);
11162 inhibit_null_byte_detection = 0;
11163
8a44e6d1
KH
11164 DEFVAR_BOOL ("disable-ascii-optimization", disable_ascii_optimization,
11165 doc: /* If non-nil, Emacs does not optimize code decoder for ASCII files.
11166Internal use only. Removed after the experimental optimizer gets stable. */);
8bc369d4 11167 disable_ascii_optimization = 0;
8a44e6d1 11168
29208e82 11169 DEFVAR_LISP ("translation-table-for-input", Vtranslation_table_for_input,
15c8f9d1 11170 doc: /* Char table for translating self-inserting characters.
446dcd75 11171This is applied to the result of input methods, not their input.
8434d0b8
EZ
11172See also `keyboard-translate-table'.
11173
11174Use of this variable for character code unification was rendered
11175obsolete in Emacs 23.1 and later, since Unicode is now the basis of
11176internal character representation. */);
002fdb44 11177 Vtranslation_table_for_input = Qnil;
8f924df7 11178
2c78b7e1 11179 {
c3a7b3f4 11180 Lisp_Object args[coding_arg_undecided_max];
8f924df7 11181 Lisp_Object plist[16];
2c78b7e1
KH
11182 int i;
11183
c3a7b3f4 11184 for (i = 0; i < coding_arg_undecided_max; i++)
2c78b7e1
KH
11185 args[i] = Qnil;
11186
d67b4f80 11187 plist[0] = intern_c_string (":name");
2c78b7e1 11188 plist[1] = args[coding_arg_name] = Qno_conversion;
d67b4f80 11189 plist[2] = intern_c_string (":mnemonic");
2c78b7e1 11190 plist[3] = args[coding_arg_mnemonic] = make_number ('=');
d67b4f80 11191 plist[4] = intern_c_string (":coding-type");
2c78b7e1 11192 plist[5] = args[coding_arg_coding_type] = Qraw_text;
d67b4f80 11193 plist[6] = intern_c_string (":ascii-compatible-p");
2c78b7e1 11194 plist[7] = args[coding_arg_ascii_compatible_p] = Qt;
d67b4f80 11195 plist[8] = intern_c_string (":default-char");
2c78b7e1 11196 plist[9] = args[coding_arg_default_char] = make_number (0);
d67b4f80 11197 plist[10] = intern_c_string (":for-unibyte");
8f924df7 11198 plist[11] = args[coding_arg_for_unibyte] = Qt;
d67b4f80 11199 plist[12] = intern_c_string (":docstring");
2a0213a6 11200 plist[13] = build_pure_c_string ("Do no conversion.\n\
2c78b7e1
KH
11201\n\
11202When you visit a file with this coding, the file is read into a\n\
11203unibyte buffer as is, thus each byte of a file is treated as a\n\
11204character.");
d67b4f80 11205 plist[14] = intern_c_string (":eol-type");
8f924df7
KH
11206 plist[15] = args[coding_arg_eol_type] = Qunix;
11207 args[coding_arg_plist] = Flist (16, plist);
2c78b7e1 11208 Fdefine_coding_system_internal (coding_arg_max, args);
ae6f73fa
KH
11209
11210 plist[1] = args[coding_arg_name] = Qundecided;
11211 plist[3] = args[coding_arg_mnemonic] = make_number ('-');
11212 plist[5] = args[coding_arg_coding_type] = Qundecided;
11213 /* This is already set.
35befdaa 11214 plist[7] = args[coding_arg_ascii_compatible_p] = Qt; */
d67b4f80 11215 plist[8] = intern_c_string (":charset-list");
ae6f73fa
KH
11216 plist[9] = args[coding_arg_charset_list] = Fcons (Qascii, Qnil);
11217 plist[11] = args[coding_arg_for_unibyte] = Qnil;
2a0213a6 11218 plist[13] = build_pure_c_string ("No conversion on encoding, automatic conversion on decoding.");
ae6f73fa
KH
11219 plist[15] = args[coding_arg_eol_type] = Qnil;
11220 args[coding_arg_plist] = Flist (16, plist);
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 */