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