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