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