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