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