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