merge trunk
[emacs.git] / src / coding.c
blob98af4ddcef7cfbaf1035156424ca6e62894642bf
1 /* Coding system handler (conversion, detection, etc).
2 Copyright (C) 2001-2013 Free Software Foundation, Inc.
3 Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 2005, 2006, 2007, 2008, 2009, 2010, 2011
5 National Institute of Advanced Industrial Science and Technology (AIST)
6 Registration Number H14PRO021
7 Copyright (C) 2003
8 National Institute of Advanced Industrial Science and Technology (AIST)
9 Registration Number H13PRO009
11 This file is part of GNU Emacs.
13 GNU Emacs is free software: you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation, either version 3 of the License, or
16 (at your option) any later version.
18 GNU Emacs is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
26 /*** TABLE OF CONTENTS ***
28 0. General comments
29 1. Preamble
30 2. Emacs' internal format (emacs-utf-8) handlers
31 3. UTF-8 handlers
32 4. UTF-16 handlers
33 5. Charset-base coding systems handlers
34 6. emacs-mule (old Emacs' internal format) handlers
35 7. ISO2022 handlers
36 8. Shift-JIS and BIG5 handlers
37 9. CCL handlers
38 10. C library functions
39 11. Emacs Lisp library functions
40 12. Postamble
44 /*** 0. General comments ***
47 CODING SYSTEM
49 A coding system is an object for an encoding mechanism that contains
50 information about how to convert byte sequences to character
51 sequences and vice versa. When we say "decode", it means converting
52 a byte sequence of a specific coding system into a character
53 sequence that is represented by Emacs' internal coding system
54 `emacs-utf-8', and when we say "encode", it means converting a
55 character sequence of emacs-utf-8 to a byte sequence of a specific
56 coding system.
58 In Emacs Lisp, a coding system is represented by a Lisp symbol. On
59 the C level, a coding system is represented by a vector of attributes
60 stored in the hash table Vcharset_hash_table. The conversion from
61 coding system symbol to attributes vector is done by looking up
62 Vcharset_hash_table by the symbol.
64 Coding systems are classified into the following types depending on
65 the encoding mechanism. Here's a brief description of the types.
67 o UTF-8
69 o UTF-16
71 o Charset-base coding system
73 A coding system defined by one or more (coded) character sets.
74 Decoding and encoding are done by a code converter defined for each
75 character set.
77 o Old Emacs internal format (emacs-mule)
79 The coding system adopted by old versions of Emacs (20 and 21).
81 o ISO2022-base coding system
83 The most famous coding system for multiple character sets. X's
84 Compound Text, various EUCs (Extended Unix Code), and coding systems
85 used in the Internet communication such as ISO-2022-JP are all
86 variants of ISO2022.
88 o SJIS (or Shift-JIS or MS-Kanji-Code)
90 A coding system to encode character sets: ASCII, JISX0201, and
91 JISX0208. Widely used for PC's in Japan. Details are described in
92 section 8.
94 o BIG5
96 A coding system to encode character sets: ASCII and Big5. Widely
97 used for Chinese (mainly in Taiwan and Hong Kong). Details are
98 described in section 8. In this file, when we write "big5" (all
99 lowercase), we mean the coding system, and when we write "Big5"
100 (capitalized), we mean the character set.
102 o CCL
104 If a user wants to decode/encode text encoded in a coding system
105 not listed above, he can supply a decoder and an encoder for it in
106 CCL (Code Conversion Language) programs. Emacs executes the CCL
107 program while decoding/encoding.
109 o Raw-text
111 A coding system for text containing raw eight-bit data. Emacs
112 treats each byte of source text as a character (except for
113 end-of-line conversion).
115 o No-conversion
117 Like raw text, but don't do end-of-line conversion.
120 END-OF-LINE FORMAT
122 How text end-of-line is encoded depends on operating system. For
123 instance, Unix's format is just one byte of LF (line-feed) code,
124 whereas DOS's format is two-byte sequence of `carriage-return' and
125 `line-feed' codes. MacOS's format is usually one byte of
126 `carriage-return'.
128 Since text character encoding and end-of-line encoding are
129 independent, any coding system described above can take any format
130 of end-of-line (except for no-conversion).
132 STRUCT CODING_SYSTEM
134 Before using a coding system for code conversion (i.e. decoding and
135 encoding), we setup a structure of type `struct coding_system'.
136 This structure keeps various information about a specific code
137 conversion (e.g. the location of source and destination data).
141 /* COMMON MACROS */
144 /*** GENERAL NOTES on `detect_coding_XXX ()' functions ***
146 These functions check if a byte sequence specified as a source in
147 CODING conforms to the format of XXX, and update the members of
148 DETECT_INFO.
150 Return true if the byte sequence conforms to XXX.
152 Below is the template of these functions. */
154 #if 0
155 static bool
156 detect_coding_XXX (struct coding_system *coding,
157 struct coding_detection_info *detect_info)
159 const unsigned char *src = coding->source;
160 const unsigned char *src_end = coding->source + coding->src_bytes;
161 bool multibytep = coding->src_multibyte;
162 ptrdiff_t consumed_chars = 0;
163 int found = 0;
164 ...;
166 while (1)
168 /* Get one byte from the source. If the source is exhausted, jump
169 to no_more_source:. */
170 ONE_MORE_BYTE (c);
172 if (! __C_conforms_to_XXX___ (c))
173 break;
174 if (! __C_strongly_suggests_XXX__ (c))
175 found = CATEGORY_MASK_XXX;
177 /* The byte sequence is invalid for XXX. */
178 detect_info->rejected |= CATEGORY_MASK_XXX;
179 return 0;
181 no_more_source:
182 /* The source exhausted successfully. */
183 detect_info->found |= found;
184 return 1;
186 #endif
188 /*** GENERAL NOTES on `decode_coding_XXX ()' functions ***
190 These functions decode a byte sequence specified as a source by
191 CODING. The resulting multibyte text goes to a place pointed to by
192 CODING->charbuf, the length of which should not exceed
193 CODING->charbuf_size;
195 These functions set the information of original and decoded texts in
196 CODING->consumed, CODING->consumed_char, and CODING->charbuf_used.
197 They also set CODING->result to one of CODING_RESULT_XXX indicating
198 how the decoding is finished.
200 Below is the template of these functions. */
202 #if 0
203 static void
204 decode_coding_XXXX (struct coding_system *coding)
206 const unsigned char *src = coding->source + coding->consumed;
207 const unsigned char *src_end = coding->source + coding->src_bytes;
208 /* SRC_BASE remembers the start position in source in each loop.
209 The loop will be exited when there's not enough source code, or
210 when there's no room in CHARBUF for a decoded character. */
211 const unsigned char *src_base;
212 /* A buffer to produce decoded characters. */
213 int *charbuf = coding->charbuf + coding->charbuf_used;
214 int *charbuf_end = coding->charbuf + coding->charbuf_size;
215 bool multibytep = coding->src_multibyte;
217 while (1)
219 src_base = src;
220 if (charbuf < charbuf_end)
221 /* No more room to produce a decoded character. */
222 break;
223 ONE_MORE_BYTE (c);
224 /* Decode it. */
227 no_more_source:
228 if (src_base < src_end
229 && coding->mode & CODING_MODE_LAST_BLOCK)
230 /* If the source ends by partial bytes to construct a character,
231 treat them as eight-bit raw data. */
232 while (src_base < src_end && charbuf < charbuf_end)
233 *charbuf++ = *src_base++;
234 /* Remember how many bytes and characters we consumed. If the
235 source is multibyte, the bytes and chars are not identical. */
236 coding->consumed = coding->consumed_char = src_base - coding->source;
237 /* Remember how many characters we produced. */
238 coding->charbuf_used = charbuf - coding->charbuf;
240 #endif
242 /*** GENERAL NOTES on `encode_coding_XXX ()' functions ***
244 These functions encode SRC_BYTES length text at SOURCE of Emacs'
245 internal multibyte format by CODING. The resulting byte sequence
246 goes to a place pointed to by DESTINATION, the length of which
247 should not exceed DST_BYTES.
249 These functions set the information of original and encoded texts in
250 the members produced, produced_char, consumed, and consumed_char of
251 the structure *CODING. They also set the member result to one of
252 CODING_RESULT_XXX indicating how the encoding finished.
254 DST_BYTES zero means that source area and destination area are
255 overlapped, which means that we can produce a encoded text until it
256 reaches at the head of not-yet-encoded source text.
258 Below is a template of these functions. */
259 #if 0
260 static void
261 encode_coding_XXX (struct coding_system *coding)
263 bool multibytep = coding->dst_multibyte;
264 int *charbuf = coding->charbuf;
265 int *charbuf_end = charbuf->charbuf + coding->charbuf_used;
266 unsigned char *dst = coding->destination + coding->produced;
267 unsigned char *dst_end = coding->destination + coding->dst_bytes;
268 unsigned char *adjusted_dst_end = dst_end - _MAX_BYTES_PRODUCED_IN_LOOP_;
269 ptrdiff_t produced_chars = 0;
271 for (; charbuf < charbuf_end && dst < adjusted_dst_end; charbuf++)
273 int c = *charbuf;
274 /* Encode C into DST, and increment DST. */
276 label_no_more_destination:
277 /* How many chars and bytes we produced. */
278 coding->produced_char += produced_chars;
279 coding->produced = dst - coding->destination;
281 #endif
284 /*** 1. Preamble ***/
286 #include <config.h>
287 #include <stdio.h>
289 #include "lisp.h"
290 #include "character.h"
291 #include "buffer.h"
292 #include "charset.h"
293 #include "ccl.h"
294 #include "composite.h"
295 #include "coding.h"
296 #include "window.h"
297 #include "frame.h"
298 #include "termhooks.h"
300 Lisp_Object Vcoding_system_hash_table;
302 static Lisp_Object Qcoding_system, Qeol_type;
303 static Lisp_Object Qcoding_aliases;
304 Lisp_Object Qunix, Qdos;
305 static Lisp_Object Qmac;
306 Lisp_Object Qbuffer_file_coding_system;
307 static Lisp_Object Qpost_read_conversion, Qpre_write_conversion;
308 static Lisp_Object Qdefault_char;
309 Lisp_Object Qno_conversion, Qundecided;
310 Lisp_Object Qcharset, Qutf_8;
311 static Lisp_Object Qiso_2022;
312 static Lisp_Object Qutf_16, Qshift_jis, Qbig5;
313 static Lisp_Object Qbig, Qlittle;
314 static Lisp_Object Qcoding_system_history;
315 static Lisp_Object Qvalid_codes;
316 static Lisp_Object QCcategory, QCmnemonic, QCdefault_char;
317 static Lisp_Object QCdecode_translation_table, QCencode_translation_table;
318 static Lisp_Object QCpost_read_conversion, QCpre_write_conversion;
319 static Lisp_Object QCascii_compatible_p;
321 Lisp_Object Qcall_process, Qcall_process_region;
322 Lisp_Object Qstart_process, Qopen_network_stream;
323 static Lisp_Object Qtarget_idx;
325 static Lisp_Object Qinsufficient_source, Qinvalid_source, Qinterrupted;
327 /* If a symbol has this property, evaluate the value to define the
328 symbol as a coding system. */
329 static Lisp_Object Qcoding_system_define_form;
331 /* Format of end-of-line decided by system. This is Qunix on
332 Unix and Mac, Qdos on DOS/Windows.
333 This has an effect only for external encoding (i.e. for output to
334 file and process), not for in-buffer or Lisp string encoding. */
335 static Lisp_Object system_eol_type;
337 #ifdef emacs
339 Lisp_Object Qcoding_system_p, Qcoding_system_error;
341 /* Coding system emacs-mule and raw-text are for converting only
342 end-of-line format. */
343 Lisp_Object Qemacs_mule, Qraw_text;
344 Lisp_Object Qutf_8_emacs;
346 #if defined (WINDOWSNT) || defined (CYGWIN)
347 static Lisp_Object Qutf_16le;
348 #endif
350 /* Coding-systems are handed between Emacs Lisp programs and C internal
351 routines by the following three variables. */
352 /* Coding system to be used to encode text for terminal display when
353 terminal coding system is nil. */
354 struct coding_system safe_terminal_coding;
356 #endif /* emacs */
358 Lisp_Object Qtranslation_table;
359 Lisp_Object Qtranslation_table_id;
360 static Lisp_Object Qtranslation_table_for_decode;
361 static Lisp_Object Qtranslation_table_for_encode;
363 /* Two special coding systems. */
364 static Lisp_Object Vsjis_coding_system;
365 static Lisp_Object Vbig5_coding_system;
367 /* ISO2022 section */
369 #define CODING_ISO_INITIAL(coding, reg) \
370 (XINT (AREF (AREF (CODING_ID_ATTRS ((coding)->id), \
371 coding_attr_iso_initial), \
372 reg)))
375 #define CODING_ISO_REQUEST(coding, charset_id) \
376 (((charset_id) <= (coding)->max_charset_id \
377 ? ((coding)->safe_charsets[charset_id] != 255 \
378 ? (coding)->safe_charsets[charset_id] \
379 : -1) \
380 : -1))
383 #define CODING_ISO_FLAGS(coding) \
384 ((coding)->spec.iso_2022.flags)
385 #define CODING_ISO_DESIGNATION(coding, reg) \
386 ((coding)->spec.iso_2022.current_designation[reg])
387 #define CODING_ISO_INVOCATION(coding, plane) \
388 ((coding)->spec.iso_2022.current_invocation[plane])
389 #define CODING_ISO_SINGLE_SHIFTING(coding) \
390 ((coding)->spec.iso_2022.single_shifting)
391 #define CODING_ISO_BOL(coding) \
392 ((coding)->spec.iso_2022.bol)
393 #define CODING_ISO_INVOKED_CHARSET(coding, plane) \
394 CODING_ISO_DESIGNATION ((coding), CODING_ISO_INVOCATION ((coding), (plane)))
395 #define CODING_ISO_CMP_STATUS(coding) \
396 (&(coding)->spec.iso_2022.cmp_status)
397 #define CODING_ISO_EXTSEGMENT_LEN(coding) \
398 ((coding)->spec.iso_2022.ctext_extended_segment_len)
399 #define CODING_ISO_EMBEDDED_UTF_8(coding) \
400 ((coding)->spec.iso_2022.embedded_utf_8)
402 /* Control characters of ISO2022. */
403 /* code */ /* function */
404 #define ISO_CODE_SO 0x0E /* shift-out */
405 #define ISO_CODE_SI 0x0F /* shift-in */
406 #define ISO_CODE_SS2_7 0x19 /* single-shift-2 for 7-bit code */
407 #define ISO_CODE_ESC 0x1B /* escape */
408 #define ISO_CODE_SS2 0x8E /* single-shift-2 */
409 #define ISO_CODE_SS3 0x8F /* single-shift-3 */
410 #define ISO_CODE_CSI 0x9B /* control-sequence-introducer */
412 /* All code (1-byte) of ISO2022 is classified into one of the
413 followings. */
414 enum iso_code_class_type
416 ISO_control_0, /* Control codes in the range
417 0x00..0x1F and 0x7F, except for the
418 following 5 codes. */
419 ISO_shift_out, /* ISO_CODE_SO (0x0E) */
420 ISO_shift_in, /* ISO_CODE_SI (0x0F) */
421 ISO_single_shift_2_7, /* ISO_CODE_SS2_7 (0x19) */
422 ISO_escape, /* ISO_CODE_ESC (0x1B) */
423 ISO_control_1, /* Control codes in the range
424 0x80..0x9F, except for the
425 following 3 codes. */
426 ISO_single_shift_2, /* ISO_CODE_SS2 (0x8E) */
427 ISO_single_shift_3, /* ISO_CODE_SS3 (0x8F) */
428 ISO_control_sequence_introducer, /* ISO_CODE_CSI (0x9B) */
429 ISO_0x20_or_0x7F, /* Codes of the values 0x20 or 0x7F. */
430 ISO_graphic_plane_0, /* Graphic codes in the range 0x21..0x7E. */
431 ISO_0xA0_or_0xFF, /* Codes of the values 0xA0 or 0xFF. */
432 ISO_graphic_plane_1 /* Graphic codes in the range 0xA1..0xFE. */
435 /** The macros CODING_ISO_FLAG_XXX defines a flag bit of the
436 `iso-flags' attribute of an iso2022 coding system. */
438 /* If set, produce long-form designation sequence (e.g. ESC $ ( A)
439 instead of the correct short-form sequence (e.g. ESC $ A). */
440 #define CODING_ISO_FLAG_LONG_FORM 0x0001
442 /* If set, reset graphic planes and registers at end-of-line to the
443 initial state. */
444 #define CODING_ISO_FLAG_RESET_AT_EOL 0x0002
446 /* If set, reset graphic planes and registers before any control
447 characters to the initial state. */
448 #define CODING_ISO_FLAG_RESET_AT_CNTL 0x0004
450 /* If set, encode by 7-bit environment. */
451 #define CODING_ISO_FLAG_SEVEN_BITS 0x0008
453 /* If set, use locking-shift function. */
454 #define CODING_ISO_FLAG_LOCKING_SHIFT 0x0010
456 /* If set, use single-shift function. Overwrite
457 CODING_ISO_FLAG_LOCKING_SHIFT. */
458 #define CODING_ISO_FLAG_SINGLE_SHIFT 0x0020
460 /* If set, use designation escape sequence. */
461 #define CODING_ISO_FLAG_DESIGNATION 0x0040
463 /* If set, produce revision number sequence. */
464 #define CODING_ISO_FLAG_REVISION 0x0080
466 /* If set, produce ISO6429's direction specifying sequence. */
467 #define CODING_ISO_FLAG_DIRECTION 0x0100
469 /* If set, assume designation states are reset at beginning of line on
470 output. */
471 #define CODING_ISO_FLAG_INIT_AT_BOL 0x0200
473 /* If set, designation sequence should be placed at beginning of line
474 on output. */
475 #define CODING_ISO_FLAG_DESIGNATE_AT_BOL 0x0400
477 /* If set, do not encode unsafe characters on output. */
478 #define CODING_ISO_FLAG_SAFE 0x0800
480 /* If set, extra latin codes (128..159) are accepted as a valid code
481 on input. */
482 #define CODING_ISO_FLAG_LATIN_EXTRA 0x1000
484 #define CODING_ISO_FLAG_COMPOSITION 0x2000
486 /* #define CODING_ISO_FLAG_EUC_TW_SHIFT 0x4000 */
488 #define CODING_ISO_FLAG_USE_ROMAN 0x8000
490 #define CODING_ISO_FLAG_USE_OLDJIS 0x10000
492 #define CODING_ISO_FLAG_FULL_SUPPORT 0x100000
494 /* A character to be produced on output if encoding of the original
495 character is prohibited by CODING_ISO_FLAG_SAFE. */
496 #define CODING_INHIBIT_CHARACTER_SUBSTITUTION '?'
498 /* UTF-8 section */
499 #define CODING_UTF_8_BOM(coding) \
500 ((coding)->spec.utf_8_bom)
502 /* UTF-16 section */
503 #define CODING_UTF_16_BOM(coding) \
504 ((coding)->spec.utf_16.bom)
506 #define CODING_UTF_16_ENDIAN(coding) \
507 ((coding)->spec.utf_16.endian)
509 #define CODING_UTF_16_SURROGATE(coding) \
510 ((coding)->spec.utf_16.surrogate)
513 /* CCL section */
514 #define CODING_CCL_DECODER(coding) \
515 AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_decoder)
516 #define CODING_CCL_ENCODER(coding) \
517 AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_encoder)
518 #define CODING_CCL_VALIDS(coding) \
519 (SDATA (AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_valids)))
521 /* Index for each coding category in `coding_categories' */
523 enum coding_category
525 coding_category_iso_7,
526 coding_category_iso_7_tight,
527 coding_category_iso_8_1,
528 coding_category_iso_8_2,
529 coding_category_iso_7_else,
530 coding_category_iso_8_else,
531 coding_category_utf_8_auto,
532 coding_category_utf_8_nosig,
533 coding_category_utf_8_sig,
534 coding_category_utf_16_auto,
535 coding_category_utf_16_be,
536 coding_category_utf_16_le,
537 coding_category_utf_16_be_nosig,
538 coding_category_utf_16_le_nosig,
539 coding_category_charset,
540 coding_category_sjis,
541 coding_category_big5,
542 coding_category_ccl,
543 coding_category_emacs_mule,
544 /* All above are targets of code detection. */
545 coding_category_raw_text,
546 coding_category_undecided,
547 coding_category_max
550 /* Definitions of flag bits used in detect_coding_XXXX. */
551 #define CATEGORY_MASK_ISO_7 (1 << coding_category_iso_7)
552 #define CATEGORY_MASK_ISO_7_TIGHT (1 << coding_category_iso_7_tight)
553 #define CATEGORY_MASK_ISO_8_1 (1 << coding_category_iso_8_1)
554 #define CATEGORY_MASK_ISO_8_2 (1 << coding_category_iso_8_2)
555 #define CATEGORY_MASK_ISO_7_ELSE (1 << coding_category_iso_7_else)
556 #define CATEGORY_MASK_ISO_8_ELSE (1 << coding_category_iso_8_else)
557 #define CATEGORY_MASK_UTF_8_AUTO (1 << coding_category_utf_8_auto)
558 #define CATEGORY_MASK_UTF_8_NOSIG (1 << coding_category_utf_8_nosig)
559 #define CATEGORY_MASK_UTF_8_SIG (1 << coding_category_utf_8_sig)
560 #define CATEGORY_MASK_UTF_16_AUTO (1 << coding_category_utf_16_auto)
561 #define CATEGORY_MASK_UTF_16_BE (1 << coding_category_utf_16_be)
562 #define CATEGORY_MASK_UTF_16_LE (1 << coding_category_utf_16_le)
563 #define CATEGORY_MASK_UTF_16_BE_NOSIG (1 << coding_category_utf_16_be_nosig)
564 #define CATEGORY_MASK_UTF_16_LE_NOSIG (1 << coding_category_utf_16_le_nosig)
565 #define CATEGORY_MASK_CHARSET (1 << coding_category_charset)
566 #define CATEGORY_MASK_SJIS (1 << coding_category_sjis)
567 #define CATEGORY_MASK_BIG5 (1 << coding_category_big5)
568 #define CATEGORY_MASK_CCL (1 << coding_category_ccl)
569 #define CATEGORY_MASK_EMACS_MULE (1 << coding_category_emacs_mule)
570 #define CATEGORY_MASK_RAW_TEXT (1 << coding_category_raw_text)
572 /* This value is returned if detect_coding_mask () find nothing other
573 than ASCII characters. */
574 #define CATEGORY_MASK_ANY \
575 (CATEGORY_MASK_ISO_7 \
576 | CATEGORY_MASK_ISO_7_TIGHT \
577 | CATEGORY_MASK_ISO_8_1 \
578 | CATEGORY_MASK_ISO_8_2 \
579 | CATEGORY_MASK_ISO_7_ELSE \
580 | CATEGORY_MASK_ISO_8_ELSE \
581 | CATEGORY_MASK_UTF_8_AUTO \
582 | CATEGORY_MASK_UTF_8_NOSIG \
583 | CATEGORY_MASK_UTF_8_SIG \
584 | CATEGORY_MASK_UTF_16_AUTO \
585 | CATEGORY_MASK_UTF_16_BE \
586 | CATEGORY_MASK_UTF_16_LE \
587 | CATEGORY_MASK_UTF_16_BE_NOSIG \
588 | CATEGORY_MASK_UTF_16_LE_NOSIG \
589 | CATEGORY_MASK_CHARSET \
590 | CATEGORY_MASK_SJIS \
591 | CATEGORY_MASK_BIG5 \
592 | CATEGORY_MASK_CCL \
593 | CATEGORY_MASK_EMACS_MULE)
596 #define CATEGORY_MASK_ISO_7BIT \
597 (CATEGORY_MASK_ISO_7 | CATEGORY_MASK_ISO_7_TIGHT)
599 #define CATEGORY_MASK_ISO_8BIT \
600 (CATEGORY_MASK_ISO_8_1 | CATEGORY_MASK_ISO_8_2)
602 #define CATEGORY_MASK_ISO_ELSE \
603 (CATEGORY_MASK_ISO_7_ELSE | CATEGORY_MASK_ISO_8_ELSE)
605 #define CATEGORY_MASK_ISO_ESCAPE \
606 (CATEGORY_MASK_ISO_7 \
607 | CATEGORY_MASK_ISO_7_TIGHT \
608 | CATEGORY_MASK_ISO_7_ELSE \
609 | CATEGORY_MASK_ISO_8_ELSE)
611 #define CATEGORY_MASK_ISO \
612 ( CATEGORY_MASK_ISO_7BIT \
613 | CATEGORY_MASK_ISO_8BIT \
614 | CATEGORY_MASK_ISO_ELSE)
616 #define CATEGORY_MASK_UTF_16 \
617 (CATEGORY_MASK_UTF_16_AUTO \
618 | CATEGORY_MASK_UTF_16_BE \
619 | CATEGORY_MASK_UTF_16_LE \
620 | CATEGORY_MASK_UTF_16_BE_NOSIG \
621 | CATEGORY_MASK_UTF_16_LE_NOSIG)
623 #define CATEGORY_MASK_UTF_8 \
624 (CATEGORY_MASK_UTF_8_AUTO \
625 | CATEGORY_MASK_UTF_8_NOSIG \
626 | CATEGORY_MASK_UTF_8_SIG)
628 /* Table of coding categories (Lisp symbols). This variable is for
629 internal use only. */
630 static Lisp_Object Vcoding_category_table;
632 /* Table of coding-categories ordered by priority. */
633 static enum coding_category coding_priorities[coding_category_max];
635 /* Nth element is a coding context for the coding system bound to the
636 Nth coding category. */
637 static struct coding_system coding_categories[coding_category_max];
639 /*** Commonly used macros and functions ***/
641 #ifndef min
642 #define min(a, b) ((a) < (b) ? (a) : (b))
643 #endif
644 #ifndef max
645 #define max(a, b) ((a) > (b) ? (a) : (b))
646 #endif
648 #define CODING_GET_INFO(coding, attrs, charset_list) \
649 do { \
650 (attrs) = CODING_ID_ATTRS ((coding)->id); \
651 (charset_list) = CODING_ATTR_CHARSET_LIST (attrs); \
652 } while (0)
655 /* Safely get one byte from the source text pointed by SRC which ends
656 at SRC_END, and set C to that byte. If there are not enough bytes
657 in the source, it jumps to 'no_more_source'. If MULTIBYTEP,
658 and a multibyte character is found at SRC, set C to the
659 negative value of the character code. The caller should declare
660 and set these variables appropriately in advance:
661 src, src_end, multibytep */
663 #define ONE_MORE_BYTE(c) \
664 do { \
665 if (src == src_end) \
667 if (src_base < src) \
668 record_conversion_result \
669 (coding, CODING_RESULT_INSUFFICIENT_SRC); \
670 goto no_more_source; \
672 c = *src++; \
673 if (multibytep && (c & 0x80)) \
675 if ((c & 0xFE) == 0xC0) \
676 c = ((c & 1) << 6) | *src++; \
677 else \
679 src--; \
680 c = - string_char (src, &src, NULL); \
681 record_conversion_result \
682 (coding, CODING_RESULT_INVALID_SRC); \
685 consumed_chars++; \
686 } while (0)
688 /* Safely get two bytes from the source text pointed by SRC which ends
689 at SRC_END, and set C1 and C2 to those bytes while skipping the
690 heading multibyte characters. If there are not enough bytes in the
691 source, it jumps to 'no_more_source'. If MULTIBYTEP and
692 a multibyte character is found for C2, set C2 to the negative value
693 of the character code. The caller should declare and set these
694 variables appropriately in advance:
695 src, src_end, multibytep
696 It is intended that this macro is used in detect_coding_utf_16. */
698 #define TWO_MORE_BYTES(c1, c2) \
699 do { \
700 do { \
701 if (src == src_end) \
702 goto no_more_source; \
703 c1 = *src++; \
704 if (multibytep && (c1 & 0x80)) \
706 if ((c1 & 0xFE) == 0xC0) \
707 c1 = ((c1 & 1) << 6) | *src++; \
708 else \
710 src += BYTES_BY_CHAR_HEAD (c1) - 1; \
711 c1 = -1; \
714 } while (c1 < 0); \
715 if (src == src_end) \
716 goto no_more_source; \
717 c2 = *src++; \
718 if (multibytep && (c2 & 0x80)) \
720 if ((c2 & 0xFE) == 0xC0) \
721 c2 = ((c2 & 1) << 6) | *src++; \
722 else \
723 c2 = -1; \
725 } while (0)
728 /* Store a byte C in the place pointed by DST and increment DST to the
729 next free point, and increment PRODUCED_CHARS. The caller should
730 assure that C is 0..127, and declare and set the variable `dst'
731 appropriately in advance.
735 #define EMIT_ONE_ASCII_BYTE(c) \
736 do { \
737 produced_chars++; \
738 *dst++ = (c); \
739 } while (0)
742 /* Like EMIT_ONE_ASCII_BYTE but store two bytes; C1 and C2. */
744 #define EMIT_TWO_ASCII_BYTES(c1, c2) \
745 do { \
746 produced_chars += 2; \
747 *dst++ = (c1), *dst++ = (c2); \
748 } while (0)
751 /* Store a byte C in the place pointed by DST and increment DST to the
752 next free point, and increment PRODUCED_CHARS. If MULTIBYTEP,
753 store in an appropriate multibyte form. The caller should
754 declare and set the variables `dst' and `multibytep' appropriately
755 in advance. */
757 #define EMIT_ONE_BYTE(c) \
758 do { \
759 produced_chars++; \
760 if (multibytep) \
762 unsigned ch = (c); \
763 if (ch >= 0x80) \
764 ch = BYTE8_TO_CHAR (ch); \
765 CHAR_STRING_ADVANCE (ch, dst); \
767 else \
768 *dst++ = (c); \
769 } while (0)
772 /* Like EMIT_ONE_BYTE, but emit two bytes; C1 and C2. */
774 #define EMIT_TWO_BYTES(c1, c2) \
775 do { \
776 produced_chars += 2; \
777 if (multibytep) \
779 unsigned ch; \
781 ch = (c1); \
782 if (ch >= 0x80) \
783 ch = BYTE8_TO_CHAR (ch); \
784 CHAR_STRING_ADVANCE (ch, dst); \
785 ch = (c2); \
786 if (ch >= 0x80) \
787 ch = BYTE8_TO_CHAR (ch); \
788 CHAR_STRING_ADVANCE (ch, dst); \
790 else \
792 *dst++ = (c1); \
793 *dst++ = (c2); \
795 } while (0)
798 #define EMIT_THREE_BYTES(c1, c2, c3) \
799 do { \
800 EMIT_ONE_BYTE (c1); \
801 EMIT_TWO_BYTES (c2, c3); \
802 } while (0)
805 #define EMIT_FOUR_BYTES(c1, c2, c3, c4) \
806 do { \
807 EMIT_TWO_BYTES (c1, c2); \
808 EMIT_TWO_BYTES (c3, c4); \
809 } while (0)
812 static void
813 record_conversion_result (struct coding_system *coding,
814 enum coding_result_code result)
816 coding->result = result;
817 switch (result)
819 case CODING_RESULT_INSUFFICIENT_SRC:
820 Vlast_code_conversion_error = Qinsufficient_source;
821 break;
822 case CODING_RESULT_INVALID_SRC:
823 Vlast_code_conversion_error = Qinvalid_source;
824 break;
825 case CODING_RESULT_INTERRUPT:
826 Vlast_code_conversion_error = Qinterrupted;
827 break;
828 case CODING_RESULT_INSUFFICIENT_DST:
829 /* Don't record this error in Vlast_code_conversion_error
830 because it happens just temporarily and is resolved when the
831 whole conversion is finished. */
832 break;
833 case CODING_RESULT_SUCCESS:
834 break;
835 default:
836 Vlast_code_conversion_error = intern ("Unknown error");
840 /* These wrapper macros are used to preserve validity of pointers into
841 buffer text across calls to decode_char, encode_char, etc, which
842 could cause relocation of buffers if it loads a charset map,
843 because loading a charset map allocates large structures. */
845 #define CODING_DECODE_CHAR(coding, src, src_base, src_end, charset, code, c) \
846 do { \
847 ptrdiff_t offset; \
849 charset_map_loaded = 0; \
850 c = DECODE_CHAR (charset, code); \
851 if (charset_map_loaded \
852 && (offset = coding_change_source (coding))) \
854 src += offset; \
855 src_base += offset; \
856 src_end += offset; \
858 } while (0)
860 #define CODING_ENCODE_CHAR(coding, dst, dst_end, charset, c, code) \
861 do { \
862 ptrdiff_t offset; \
864 charset_map_loaded = 0; \
865 code = ENCODE_CHAR (charset, c); \
866 if (charset_map_loaded \
867 && (offset = coding_change_destination (coding))) \
869 dst += offset; \
870 dst_end += offset; \
872 } while (0)
874 #define CODING_CHAR_CHARSET(coding, dst, dst_end, c, charset_list, code_return, charset) \
875 do { \
876 ptrdiff_t offset; \
878 charset_map_loaded = 0; \
879 charset = char_charset (c, charset_list, code_return); \
880 if (charset_map_loaded \
881 && (offset = coding_change_destination (coding))) \
883 dst += offset; \
884 dst_end += offset; \
886 } while (0)
888 #define CODING_CHAR_CHARSET_P(coding, dst, dst_end, c, charset, result) \
889 do { \
890 ptrdiff_t offset; \
892 charset_map_loaded = 0; \
893 result = CHAR_CHARSET_P (c, charset); \
894 if (charset_map_loaded \
895 && (offset = coding_change_destination (coding))) \
897 dst += offset; \
898 dst_end += offset; \
900 } while (0)
903 /* If there are at least BYTES length of room at dst, allocate memory
904 for coding->destination and update dst and dst_end. We don't have
905 to take care of coding->source which will be relocated. It is
906 handled by calling coding_set_source in encode_coding. */
908 #define ASSURE_DESTINATION(bytes) \
909 do { \
910 if (dst + (bytes) >= dst_end) \
912 ptrdiff_t more_bytes = charbuf_end - charbuf + (bytes); \
914 dst = alloc_destination (coding, more_bytes, dst); \
915 dst_end = coding->destination + coding->dst_bytes; \
917 } while (0)
920 /* Store multibyte form of the character C in P, and advance P to the
921 end of the multibyte form. This used to be like CHAR_STRING_ADVANCE
922 without ever calling MAYBE_UNIFY_CHAR, but nowadays we don't call
923 MAYBE_UNIFY_CHAR in CHAR_STRING_ADVANCE. */
925 #define CHAR_STRING_ADVANCE_NO_UNIFY(c, p) CHAR_STRING_ADVANCE(c, p)
927 /* Return the character code of character whose multibyte form is at
928 P, and advance P to the end of the multibyte form. This used to be
929 like STRING_CHAR_ADVANCE without ever calling MAYBE_UNIFY_CHAR, but
930 nowadays STRING_CHAR_ADVANCE doesn't call MAYBE_UNIFY_CHAR. */
932 #define STRING_CHAR_ADVANCE_NO_UNIFY(p) STRING_CHAR_ADVANCE(p)
934 /* Set coding->source from coding->src_object. */
936 static void
937 coding_set_source (struct coding_system *coding)
939 if (BUFFERP (coding->src_object))
941 struct buffer *buf = XBUFFER (coding->src_object);
943 if (coding->src_pos < 0)
944 coding->source = BUF_GAP_END_ADDR (buf) + coding->src_pos_byte;
945 else
946 coding->source = BUF_BYTE_ADDRESS (buf, coding->src_pos_byte);
948 else if (STRINGP (coding->src_object))
950 coding->source = SDATA (coding->src_object) + coding->src_pos_byte;
952 else
954 /* Otherwise, the source is C string and is never relocated
955 automatically. Thus we don't have to update anything. */
960 /* Set coding->source from coding->src_object, and return how many
961 bytes coding->source was changed. */
963 static ptrdiff_t
964 coding_change_source (struct coding_system *coding)
966 const unsigned char *orig = coding->source;
967 coding_set_source (coding);
968 return coding->source - orig;
972 /* Set coding->destination from coding->dst_object. */
974 static void
975 coding_set_destination (struct coding_system *coding)
977 if (BUFFERP (coding->dst_object))
979 if (BUFFERP (coding->src_object) && coding->src_pos < 0)
981 coding->destination = BEG_ADDR + coding->dst_pos_byte - BEG_BYTE;
982 coding->dst_bytes = (GAP_END_ADDR
983 - (coding->src_bytes - coding->consumed)
984 - coding->destination);
986 else
988 /* We are sure that coding->dst_pos_byte is before the gap
989 of the buffer. */
990 coding->destination = (BUF_BEG_ADDR (XBUFFER (coding->dst_object))
991 + coding->dst_pos_byte - BEG_BYTE);
992 coding->dst_bytes = (BUF_GAP_END_ADDR (XBUFFER (coding->dst_object))
993 - coding->destination);
996 else
998 /* Otherwise, the destination is C string and is never relocated
999 automatically. Thus we don't have to update anything. */
1004 /* Set coding->destination from coding->dst_object, and return how
1005 many bytes coding->destination was changed. */
1007 static ptrdiff_t
1008 coding_change_destination (struct coding_system *coding)
1010 const unsigned char *orig = coding->destination;
1011 coding_set_destination (coding);
1012 return coding->destination - orig;
1016 static void
1017 coding_alloc_by_realloc (struct coding_system *coding, ptrdiff_t bytes)
1019 if (STRING_BYTES_BOUND - coding->dst_bytes < bytes)
1020 string_overflow ();
1021 coding->destination = xrealloc (coding->destination,
1022 coding->dst_bytes + bytes);
1023 coding->dst_bytes += bytes;
1026 static void
1027 coding_alloc_by_making_gap (struct coding_system *coding,
1028 ptrdiff_t gap_head_used, ptrdiff_t bytes)
1030 if (EQ (coding->src_object, coding->dst_object))
1032 /* The gap may contain the produced data at the head and not-yet
1033 consumed data at the tail. To preserve those data, we at
1034 first make the gap size to zero, then increase the gap
1035 size. */
1036 ptrdiff_t add = GAP_SIZE;
1038 GPT += gap_head_used, GPT_BYTE += gap_head_used;
1039 GAP_SIZE = 0; ZV += add; Z += add; ZV_BYTE += add; Z_BYTE += add;
1040 make_gap (bytes);
1041 GAP_SIZE += add; ZV -= add; Z -= add; ZV_BYTE -= add; Z_BYTE -= add;
1042 GPT -= gap_head_used, GPT_BYTE -= gap_head_used;
1044 else
1045 make_gap_1 (XBUFFER (coding->dst_object), bytes);
1049 static unsigned char *
1050 alloc_destination (struct coding_system *coding, ptrdiff_t nbytes,
1051 unsigned char *dst)
1053 ptrdiff_t offset = dst - coding->destination;
1055 if (BUFFERP (coding->dst_object))
1057 struct buffer *buf = XBUFFER (coding->dst_object);
1059 coding_alloc_by_making_gap (coding, dst - BUF_GPT_ADDR (buf), nbytes);
1061 else
1062 coding_alloc_by_realloc (coding, nbytes);
1063 coding_set_destination (coding);
1064 dst = coding->destination + offset;
1065 return dst;
1068 /** Macros for annotations. */
1070 /* An annotation data is stored in the array coding->charbuf in this
1071 format:
1072 [ -LENGTH ANNOTATION_MASK NCHARS ... ]
1073 LENGTH is the number of elements in the annotation.
1074 ANNOTATION_MASK is one of CODING_ANNOTATE_XXX_MASK.
1075 NCHARS is the number of characters in the text annotated.
1077 The format of the following elements depend on ANNOTATION_MASK.
1079 In the case of CODING_ANNOTATE_COMPOSITION_MASK, these elements
1080 follows:
1081 ... NBYTES METHOD [ COMPOSITION-COMPONENTS ... ]
1083 NBYTES is the number of bytes specified in the header part of
1084 old-style emacs-mule encoding, or 0 for the other kind of
1085 composition.
1087 METHOD is one of enum composition_method.
1089 Optional COMPOSITION-COMPONENTS are characters and composition
1090 rules.
1092 In the case of CODING_ANNOTATE_CHARSET_MASK, one element CHARSET-ID
1093 follows.
1095 If ANNOTATION_MASK is 0, this annotation is just a space holder to
1096 recover from an invalid annotation, and should be skipped by
1097 produce_annotation. */
1099 /* Maximum length of the header of annotation data. */
1100 #define MAX_ANNOTATION_LENGTH 5
1102 #define ADD_ANNOTATION_DATA(buf, len, mask, nchars) \
1103 do { \
1104 *(buf)++ = -(len); \
1105 *(buf)++ = (mask); \
1106 *(buf)++ = (nchars); \
1107 coding->annotated = 1; \
1108 } while (0);
1110 #define ADD_COMPOSITION_DATA(buf, nchars, nbytes, method) \
1111 do { \
1112 ADD_ANNOTATION_DATA (buf, 5, CODING_ANNOTATE_COMPOSITION_MASK, nchars); \
1113 *buf++ = nbytes; \
1114 *buf++ = method; \
1115 } while (0)
1118 #define ADD_CHARSET_DATA(buf, nchars, id) \
1119 do { \
1120 ADD_ANNOTATION_DATA (buf, 4, CODING_ANNOTATE_CHARSET_MASK, nchars); \
1121 *buf++ = id; \
1122 } while (0)
1125 /*** 2. Emacs' internal format (emacs-utf-8) ***/
1130 /*** 3. UTF-8 ***/
1132 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1133 Return true if a text is encoded in UTF-8. */
1135 #define UTF_8_1_OCTET_P(c) ((c) < 0x80)
1136 #define UTF_8_EXTRA_OCTET_P(c) (((c) & 0xC0) == 0x80)
1137 #define UTF_8_2_OCTET_LEADING_P(c) (((c) & 0xE0) == 0xC0)
1138 #define UTF_8_3_OCTET_LEADING_P(c) (((c) & 0xF0) == 0xE0)
1139 #define UTF_8_4_OCTET_LEADING_P(c) (((c) & 0xF8) == 0xF0)
1140 #define UTF_8_5_OCTET_LEADING_P(c) (((c) & 0xFC) == 0xF8)
1142 #define UTF_8_BOM_1 0xEF
1143 #define UTF_8_BOM_2 0xBB
1144 #define UTF_8_BOM_3 0xBF
1146 static bool
1147 detect_coding_utf_8 (struct coding_system *coding,
1148 struct coding_detection_info *detect_info)
1150 const unsigned char *src = coding->source, *src_base;
1151 const unsigned char *src_end = coding->source + coding->src_bytes;
1152 bool multibytep = coding->src_multibyte;
1153 ptrdiff_t consumed_chars = 0;
1154 bool bom_found = 0;
1155 bool found = 0;
1157 detect_info->checked |= CATEGORY_MASK_UTF_8;
1158 /* A coding system of this category is always ASCII compatible. */
1159 src += coding->head_ascii;
1161 while (1)
1163 int c, c1, c2, c3, c4;
1165 src_base = src;
1166 ONE_MORE_BYTE (c);
1167 if (c < 0 || UTF_8_1_OCTET_P (c))
1168 continue;
1169 ONE_MORE_BYTE (c1);
1170 if (c1 < 0 || ! UTF_8_EXTRA_OCTET_P (c1))
1171 break;
1172 if (UTF_8_2_OCTET_LEADING_P (c))
1174 found = 1;
1175 continue;
1177 ONE_MORE_BYTE (c2);
1178 if (c2 < 0 || ! UTF_8_EXTRA_OCTET_P (c2))
1179 break;
1180 if (UTF_8_3_OCTET_LEADING_P (c))
1182 found = 1;
1183 if (src_base == coding->source
1184 && c == UTF_8_BOM_1 && c1 == UTF_8_BOM_2 && c2 == UTF_8_BOM_3)
1185 bom_found = 1;
1186 continue;
1188 ONE_MORE_BYTE (c3);
1189 if (c3 < 0 || ! UTF_8_EXTRA_OCTET_P (c3))
1190 break;
1191 if (UTF_8_4_OCTET_LEADING_P (c))
1193 found = 1;
1194 continue;
1196 ONE_MORE_BYTE (c4);
1197 if (c4 < 0 || ! UTF_8_EXTRA_OCTET_P (c4))
1198 break;
1199 if (UTF_8_5_OCTET_LEADING_P (c))
1201 found = 1;
1202 continue;
1204 break;
1206 detect_info->rejected |= CATEGORY_MASK_UTF_8;
1207 return 0;
1209 no_more_source:
1210 if (src_base < src && coding->mode & CODING_MODE_LAST_BLOCK)
1212 detect_info->rejected |= CATEGORY_MASK_UTF_8;
1213 return 0;
1215 if (bom_found)
1217 /* The first character 0xFFFE doesn't necessarily mean a BOM. */
1218 detect_info->found |= CATEGORY_MASK_UTF_8_SIG | CATEGORY_MASK_UTF_8_NOSIG;
1220 else
1222 detect_info->rejected |= CATEGORY_MASK_UTF_8_SIG;
1223 if (found)
1224 detect_info->found |= CATEGORY_MASK_UTF_8_NOSIG;
1226 return 1;
1230 static void
1231 decode_coding_utf_8 (struct coding_system *coding)
1233 const unsigned char *src = coding->source + coding->consumed;
1234 const unsigned char *src_end = coding->source + coding->src_bytes;
1235 const unsigned char *src_base;
1236 int *charbuf = coding->charbuf + coding->charbuf_used;
1237 int *charbuf_end = coding->charbuf + coding->charbuf_size;
1238 ptrdiff_t consumed_chars = 0, consumed_chars_base = 0;
1239 bool multibytep = coding->src_multibyte;
1240 enum utf_bom_type bom = CODING_UTF_8_BOM (coding);
1241 bool eol_dos
1242 = !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos);
1243 int byte_after_cr = -1;
1245 if (bom != utf_without_bom)
1247 int c1, c2, c3;
1249 src_base = src;
1250 ONE_MORE_BYTE (c1);
1251 if (! UTF_8_3_OCTET_LEADING_P (c1))
1252 src = src_base;
1253 else
1255 ONE_MORE_BYTE (c2);
1256 if (! UTF_8_EXTRA_OCTET_P (c2))
1257 src = src_base;
1258 else
1260 ONE_MORE_BYTE (c3);
1261 if (! UTF_8_EXTRA_OCTET_P (c3))
1262 src = src_base;
1263 else
1265 if ((c1 != UTF_8_BOM_1)
1266 || (c2 != UTF_8_BOM_2) || (c3 != UTF_8_BOM_3))
1267 src = src_base;
1268 else
1269 CODING_UTF_8_BOM (coding) = utf_without_bom;
1274 CODING_UTF_8_BOM (coding) = utf_without_bom;
1276 while (1)
1278 int c, c1, c2, c3, c4, c5;
1280 src_base = src;
1281 consumed_chars_base = consumed_chars;
1283 if (charbuf >= charbuf_end)
1285 if (byte_after_cr >= 0)
1286 src_base--;
1287 break;
1290 if (byte_after_cr >= 0)
1291 c1 = byte_after_cr, byte_after_cr = -1;
1292 else
1293 ONE_MORE_BYTE (c1);
1294 if (c1 < 0)
1296 c = - c1;
1298 else if (UTF_8_1_OCTET_P (c1))
1300 if (eol_dos && c1 == '\r')
1301 ONE_MORE_BYTE (byte_after_cr);
1302 c = c1;
1304 else
1306 ONE_MORE_BYTE (c2);
1307 if (c2 < 0 || ! UTF_8_EXTRA_OCTET_P (c2))
1308 goto invalid_code;
1309 if (UTF_8_2_OCTET_LEADING_P (c1))
1311 c = ((c1 & 0x1F) << 6) | (c2 & 0x3F);
1312 /* Reject overlong sequences here and below. Encoders
1313 producing them are incorrect, they can be misleading,
1314 and they mess up read/write invariance. */
1315 if (c < 128)
1316 goto invalid_code;
1318 else
1320 ONE_MORE_BYTE (c3);
1321 if (c3 < 0 || ! UTF_8_EXTRA_OCTET_P (c3))
1322 goto invalid_code;
1323 if (UTF_8_3_OCTET_LEADING_P (c1))
1325 c = (((c1 & 0xF) << 12)
1326 | ((c2 & 0x3F) << 6) | (c3 & 0x3F));
1327 if (c < 0x800
1328 || (c >= 0xd800 && c < 0xe000)) /* surrogates (invalid) */
1329 goto invalid_code;
1331 else
1333 ONE_MORE_BYTE (c4);
1334 if (c4 < 0 || ! UTF_8_EXTRA_OCTET_P (c4))
1335 goto invalid_code;
1336 if (UTF_8_4_OCTET_LEADING_P (c1))
1338 c = (((c1 & 0x7) << 18) | ((c2 & 0x3F) << 12)
1339 | ((c3 & 0x3F) << 6) | (c4 & 0x3F));
1340 if (c < 0x10000)
1341 goto invalid_code;
1343 else
1345 ONE_MORE_BYTE (c5);
1346 if (c5 < 0 || ! UTF_8_EXTRA_OCTET_P (c5))
1347 goto invalid_code;
1348 if (UTF_8_5_OCTET_LEADING_P (c1))
1350 c = (((c1 & 0x3) << 24) | ((c2 & 0x3F) << 18)
1351 | ((c3 & 0x3F) << 12) | ((c4 & 0x3F) << 6)
1352 | (c5 & 0x3F));
1353 if ((c > MAX_CHAR) || (c < 0x200000))
1354 goto invalid_code;
1356 else
1357 goto invalid_code;
1363 *charbuf++ = c;
1364 continue;
1366 invalid_code:
1367 src = src_base;
1368 consumed_chars = consumed_chars_base;
1369 ONE_MORE_BYTE (c);
1370 *charbuf++ = ASCII_BYTE_P (c) ? c : BYTE8_TO_CHAR (c);
1371 coding->errors++;
1374 no_more_source:
1375 coding->consumed_char += consumed_chars_base;
1376 coding->consumed = src_base - coding->source;
1377 coding->charbuf_used = charbuf - coding->charbuf;
1381 static bool
1382 encode_coding_utf_8 (struct coding_system *coding)
1384 bool multibytep = coding->dst_multibyte;
1385 int *charbuf = coding->charbuf;
1386 int *charbuf_end = charbuf + coding->charbuf_used;
1387 unsigned char *dst = coding->destination + coding->produced;
1388 unsigned char *dst_end = coding->destination + coding->dst_bytes;
1389 ptrdiff_t produced_chars = 0;
1390 int c;
1392 if (CODING_UTF_8_BOM (coding) == utf_with_bom)
1394 ASSURE_DESTINATION (3);
1395 EMIT_THREE_BYTES (UTF_8_BOM_1, UTF_8_BOM_2, UTF_8_BOM_3);
1396 CODING_UTF_8_BOM (coding) = utf_without_bom;
1399 if (multibytep)
1401 int safe_room = MAX_MULTIBYTE_LENGTH * 2;
1403 while (charbuf < charbuf_end)
1405 unsigned char str[MAX_MULTIBYTE_LENGTH], *p, *pend = str;
1407 ASSURE_DESTINATION (safe_room);
1408 c = *charbuf++;
1409 if (CHAR_BYTE8_P (c))
1411 c = CHAR_TO_BYTE8 (c);
1412 EMIT_ONE_BYTE (c);
1414 else
1416 CHAR_STRING_ADVANCE_NO_UNIFY (c, pend);
1417 for (p = str; p < pend; p++)
1418 EMIT_ONE_BYTE (*p);
1422 else
1424 int safe_room = MAX_MULTIBYTE_LENGTH;
1426 while (charbuf < charbuf_end)
1428 ASSURE_DESTINATION (safe_room);
1429 c = *charbuf++;
1430 if (CHAR_BYTE8_P (c))
1431 *dst++ = CHAR_TO_BYTE8 (c);
1432 else
1433 CHAR_STRING_ADVANCE_NO_UNIFY (c, dst);
1434 produced_chars++;
1437 record_conversion_result (coding, CODING_RESULT_SUCCESS);
1438 coding->produced_char += produced_chars;
1439 coding->produced = dst - coding->destination;
1440 return 0;
1444 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1445 Return true if a text is encoded in one of UTF-16 based coding systems. */
1447 #define UTF_16_HIGH_SURROGATE_P(val) \
1448 (((val) & 0xFC00) == 0xD800)
1450 #define UTF_16_LOW_SURROGATE_P(val) \
1451 (((val) & 0xFC00) == 0xDC00)
1454 static bool
1455 detect_coding_utf_16 (struct coding_system *coding,
1456 struct coding_detection_info *detect_info)
1458 const unsigned char *src = coding->source;
1459 const unsigned char *src_end = coding->source + coding->src_bytes;
1460 bool multibytep = coding->src_multibyte;
1461 int c1, c2;
1463 detect_info->checked |= CATEGORY_MASK_UTF_16;
1464 if (coding->mode & CODING_MODE_LAST_BLOCK
1465 && (coding->src_chars & 1))
1467 detect_info->rejected |= CATEGORY_MASK_UTF_16;
1468 return 0;
1471 TWO_MORE_BYTES (c1, c2);
1472 if ((c1 == 0xFF) && (c2 == 0xFE))
1474 detect_info->found |= (CATEGORY_MASK_UTF_16_LE
1475 | CATEGORY_MASK_UTF_16_AUTO);
1476 detect_info->rejected |= (CATEGORY_MASK_UTF_16_BE
1477 | CATEGORY_MASK_UTF_16_BE_NOSIG
1478 | CATEGORY_MASK_UTF_16_LE_NOSIG);
1480 else if ((c1 == 0xFE) && (c2 == 0xFF))
1482 detect_info->found |= (CATEGORY_MASK_UTF_16_BE
1483 | CATEGORY_MASK_UTF_16_AUTO);
1484 detect_info->rejected |= (CATEGORY_MASK_UTF_16_LE
1485 | CATEGORY_MASK_UTF_16_BE_NOSIG
1486 | CATEGORY_MASK_UTF_16_LE_NOSIG);
1488 else if (c2 < 0)
1490 detect_info->rejected |= CATEGORY_MASK_UTF_16;
1491 return 0;
1493 else
1495 /* We check the dispersion of Eth and Oth bytes where E is even and
1496 O is odd. If both are high, we assume binary data.*/
1497 unsigned char e[256], o[256];
1498 unsigned e_num = 1, o_num = 1;
1500 memset (e, 0, 256);
1501 memset (o, 0, 256);
1502 e[c1] = 1;
1503 o[c2] = 1;
1505 detect_info->rejected |= (CATEGORY_MASK_UTF_16_AUTO
1506 |CATEGORY_MASK_UTF_16_BE
1507 | CATEGORY_MASK_UTF_16_LE);
1509 while ((detect_info->rejected & CATEGORY_MASK_UTF_16)
1510 != CATEGORY_MASK_UTF_16)
1512 TWO_MORE_BYTES (c1, c2);
1513 if (c2 < 0)
1514 break;
1515 if (! e[c1])
1517 e[c1] = 1;
1518 e_num++;
1519 if (e_num >= 128)
1520 detect_info->rejected |= CATEGORY_MASK_UTF_16_BE_NOSIG;
1522 if (! o[c2])
1524 o[c2] = 1;
1525 o_num++;
1526 if (o_num >= 128)
1527 detect_info->rejected |= CATEGORY_MASK_UTF_16_LE_NOSIG;
1530 return 0;
1533 no_more_source:
1534 return 1;
1537 static void
1538 decode_coding_utf_16 (struct coding_system *coding)
1540 const unsigned char *src = coding->source + coding->consumed;
1541 const unsigned char *src_end = coding->source + coding->src_bytes;
1542 const unsigned char *src_base;
1543 int *charbuf = coding->charbuf + coding->charbuf_used;
1544 /* We may produces at most 3 chars in one loop. */
1545 int *charbuf_end = coding->charbuf + coding->charbuf_size - 2;
1546 ptrdiff_t consumed_chars = 0, consumed_chars_base = 0;
1547 bool multibytep = coding->src_multibyte;
1548 enum utf_bom_type bom = CODING_UTF_16_BOM (coding);
1549 enum utf_16_endian_type endian = CODING_UTF_16_ENDIAN (coding);
1550 int surrogate = CODING_UTF_16_SURROGATE (coding);
1551 bool eol_dos
1552 = !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos);
1553 int byte_after_cr1 = -1, byte_after_cr2 = -1;
1555 if (bom == utf_with_bom)
1557 int c, c1, c2;
1559 src_base = src;
1560 ONE_MORE_BYTE (c1);
1561 ONE_MORE_BYTE (c2);
1562 c = (c1 << 8) | c2;
1564 if (endian == utf_16_big_endian
1565 ? c != 0xFEFF : c != 0xFFFE)
1567 /* The first two bytes are not BOM. Treat them as bytes
1568 for a normal character. */
1569 src = src_base;
1570 coding->errors++;
1572 CODING_UTF_16_BOM (coding) = utf_without_bom;
1574 else if (bom == utf_detect_bom)
1576 /* We have already tried to detect BOM and failed in
1577 detect_coding. */
1578 CODING_UTF_16_BOM (coding) = utf_without_bom;
1581 while (1)
1583 int c, c1, c2;
1585 src_base = src;
1586 consumed_chars_base = consumed_chars;
1588 if (charbuf >= charbuf_end)
1590 if (byte_after_cr1 >= 0)
1591 src_base -= 2;
1592 break;
1595 if (byte_after_cr1 >= 0)
1596 c1 = byte_after_cr1, byte_after_cr1 = -1;
1597 else
1598 ONE_MORE_BYTE (c1);
1599 if (c1 < 0)
1601 *charbuf++ = -c1;
1602 continue;
1604 if (byte_after_cr2 >= 0)
1605 c2 = byte_after_cr2, byte_after_cr2 = -1;
1606 else
1607 ONE_MORE_BYTE (c2);
1608 if (c2 < 0)
1610 *charbuf++ = ASCII_BYTE_P (c1) ? c1 : BYTE8_TO_CHAR (c1);
1611 *charbuf++ = -c2;
1612 continue;
1614 c = (endian == utf_16_big_endian
1615 ? ((c1 << 8) | c2) : ((c2 << 8) | c1));
1617 if (surrogate)
1619 if (! UTF_16_LOW_SURROGATE_P (c))
1621 if (endian == utf_16_big_endian)
1622 c1 = surrogate >> 8, c2 = surrogate & 0xFF;
1623 else
1624 c1 = surrogate & 0xFF, c2 = surrogate >> 8;
1625 *charbuf++ = c1;
1626 *charbuf++ = c2;
1627 coding->errors++;
1628 if (UTF_16_HIGH_SURROGATE_P (c))
1629 CODING_UTF_16_SURROGATE (coding) = surrogate = c;
1630 else
1631 *charbuf++ = c;
1633 else
1635 c = ((surrogate - 0xD800) << 10) | (c - 0xDC00);
1636 CODING_UTF_16_SURROGATE (coding) = surrogate = 0;
1637 *charbuf++ = 0x10000 + c;
1640 else
1642 if (UTF_16_HIGH_SURROGATE_P (c))
1643 CODING_UTF_16_SURROGATE (coding) = surrogate = c;
1644 else
1646 if (eol_dos && c == '\r')
1648 ONE_MORE_BYTE (byte_after_cr1);
1649 ONE_MORE_BYTE (byte_after_cr2);
1651 *charbuf++ = c;
1656 no_more_source:
1657 coding->consumed_char += consumed_chars_base;
1658 coding->consumed = src_base - coding->source;
1659 coding->charbuf_used = charbuf - coding->charbuf;
1662 static bool
1663 encode_coding_utf_16 (struct coding_system *coding)
1665 bool multibytep = coding->dst_multibyte;
1666 int *charbuf = coding->charbuf;
1667 int *charbuf_end = charbuf + coding->charbuf_used;
1668 unsigned char *dst = coding->destination + coding->produced;
1669 unsigned char *dst_end = coding->destination + coding->dst_bytes;
1670 int safe_room = 8;
1671 enum utf_bom_type bom = CODING_UTF_16_BOM (coding);
1672 bool big_endian = CODING_UTF_16_ENDIAN (coding) == utf_16_big_endian;
1673 ptrdiff_t produced_chars = 0;
1674 int c;
1676 if (bom != utf_without_bom)
1678 ASSURE_DESTINATION (safe_room);
1679 if (big_endian)
1680 EMIT_TWO_BYTES (0xFE, 0xFF);
1681 else
1682 EMIT_TWO_BYTES (0xFF, 0xFE);
1683 CODING_UTF_16_BOM (coding) = utf_without_bom;
1686 while (charbuf < charbuf_end)
1688 ASSURE_DESTINATION (safe_room);
1689 c = *charbuf++;
1690 if (c > MAX_UNICODE_CHAR)
1691 c = coding->default_char;
1693 if (c < 0x10000)
1695 if (big_endian)
1696 EMIT_TWO_BYTES (c >> 8, c & 0xFF);
1697 else
1698 EMIT_TWO_BYTES (c & 0xFF, c >> 8);
1700 else
1702 int c1, c2;
1704 c -= 0x10000;
1705 c1 = (c >> 10) + 0xD800;
1706 c2 = (c & 0x3FF) + 0xDC00;
1707 if (big_endian)
1708 EMIT_FOUR_BYTES (c1 >> 8, c1 & 0xFF, c2 >> 8, c2 & 0xFF);
1709 else
1710 EMIT_FOUR_BYTES (c1 & 0xFF, c1 >> 8, c2 & 0xFF, c2 >> 8);
1713 record_conversion_result (coding, CODING_RESULT_SUCCESS);
1714 coding->produced = dst - coding->destination;
1715 coding->produced_char += produced_chars;
1716 return 0;
1720 /*** 6. Old Emacs' internal format (emacs-mule) ***/
1722 /* Emacs' internal format for representation of multiple character
1723 sets is a kind of multi-byte encoding, i.e. characters are
1724 represented by variable-length sequences of one-byte codes.
1726 ASCII characters and control characters (e.g. `tab', `newline') are
1727 represented by one-byte sequences which are their ASCII codes, in
1728 the range 0x00 through 0x7F.
1730 8-bit characters of the range 0x80..0x9F are represented by
1731 two-byte sequences of LEADING_CODE_8_BIT_CONTROL and (their 8-bit
1732 code + 0x20).
1734 8-bit characters of the range 0xA0..0xFF are represented by
1735 one-byte sequences which are their 8-bit code.
1737 The other characters are represented by a sequence of `base
1738 leading-code', optional `extended leading-code', and one or two
1739 `position-code's. The length of the sequence is determined by the
1740 base leading-code. Leading-code takes the range 0x81 through 0x9D,
1741 whereas extended leading-code and position-code take the range 0xA0
1742 through 0xFF. See `charset.h' for more details about leading-code
1743 and position-code.
1745 --- CODE RANGE of Emacs' internal format ---
1746 character set range
1747 ------------- -----
1748 ascii 0x00..0x7F
1749 eight-bit-control LEADING_CODE_8_BIT_CONTROL + 0xA0..0xBF
1750 eight-bit-graphic 0xA0..0xBF
1751 ELSE 0x81..0x9D + [0xA0..0xFF]+
1752 ---------------------------------------------
1754 As this is the internal character representation, the format is
1755 usually not used externally (i.e. in a file or in a data sent to a
1756 process). But, it is possible to have a text externally in this
1757 format (i.e. by encoding by the coding system `emacs-mule').
1759 In that case, a sequence of one-byte codes has a slightly different
1760 form.
1762 At first, all characters in eight-bit-control are represented by
1763 one-byte sequences which are their 8-bit code.
1765 Next, character composition data are represented by the byte
1766 sequence of the form: 0x80 METHOD BYTES CHARS COMPONENT ...,
1767 where,
1768 METHOD is 0xF2 plus one of composition method (enum
1769 composition_method),
1771 BYTES is 0xA0 plus a byte length of this composition data,
1773 CHARS is 0xA0 plus a number of characters composed by this
1774 data,
1776 COMPONENTs are characters of multibyte form or composition
1777 rules encoded by two-byte of ASCII codes.
1779 In addition, for backward compatibility, the following formats are
1780 also recognized as composition data on decoding.
1782 0x80 MSEQ ...
1783 0x80 0xFF MSEQ RULE MSEQ RULE ... MSEQ
1785 Here,
1786 MSEQ is a multibyte form but in these special format:
1787 ASCII: 0xA0 ASCII_CODE+0x80,
1788 other: LEADING_CODE+0x20 FOLLOWING-BYTE ...,
1789 RULE is a one byte code of the range 0xA0..0xF0 that
1790 represents a composition rule.
1793 char emacs_mule_bytes[256];
1796 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1797 Return true if a text is encoded in 'emacs-mule'. */
1799 static bool
1800 detect_coding_emacs_mule (struct coding_system *coding,
1801 struct coding_detection_info *detect_info)
1803 const unsigned char *src = coding->source, *src_base;
1804 const unsigned char *src_end = coding->source + coding->src_bytes;
1805 bool multibytep = coding->src_multibyte;
1806 ptrdiff_t consumed_chars = 0;
1807 int c;
1808 int found = 0;
1810 detect_info->checked |= CATEGORY_MASK_EMACS_MULE;
1811 /* A coding system of this category is always ASCII compatible. */
1812 src += coding->head_ascii;
1814 while (1)
1816 src_base = src;
1817 ONE_MORE_BYTE (c);
1818 if (c < 0)
1819 continue;
1820 if (c == 0x80)
1822 /* Perhaps the start of composite character. We simply skip
1823 it because analyzing it is too heavy for detecting. But,
1824 at least, we check that the composite character
1825 constitutes of more than 4 bytes. */
1826 const unsigned char *src_start;
1828 repeat:
1829 src_start = src;
1832 ONE_MORE_BYTE (c);
1834 while (c >= 0xA0);
1836 if (src - src_start <= 4)
1837 break;
1838 found = CATEGORY_MASK_EMACS_MULE;
1839 if (c == 0x80)
1840 goto repeat;
1843 if (c < 0x80)
1845 if (c < 0x20
1846 && (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO))
1847 break;
1849 else
1851 int more_bytes = emacs_mule_bytes[c] - 1;
1853 while (more_bytes > 0)
1855 ONE_MORE_BYTE (c);
1856 if (c < 0xA0)
1858 src--; /* Unread the last byte. */
1859 break;
1861 more_bytes--;
1863 if (more_bytes != 0)
1864 break;
1865 found = CATEGORY_MASK_EMACS_MULE;
1868 detect_info->rejected |= CATEGORY_MASK_EMACS_MULE;
1869 return 0;
1871 no_more_source:
1872 if (src_base < src && coding->mode & CODING_MODE_LAST_BLOCK)
1874 detect_info->rejected |= CATEGORY_MASK_EMACS_MULE;
1875 return 0;
1877 detect_info->found |= found;
1878 return 1;
1882 /* Parse emacs-mule multibyte sequence at SRC and return the decoded
1883 character. If CMP_STATUS indicates that we must expect MSEQ or
1884 RULE described above, decode it and return the negative value of
1885 the decoded character or rule. If an invalid byte is found, return
1886 -1. If SRC is too short, return -2. */
1888 static int
1889 emacs_mule_char (struct coding_system *coding, const unsigned char *src,
1890 int *nbytes, int *nchars, int *id,
1891 struct composition_status *cmp_status)
1893 const unsigned char *src_end = coding->source + coding->src_bytes;
1894 const unsigned char *src_base = src;
1895 bool multibytep = coding->src_multibyte;
1896 int charset_ID;
1897 unsigned code;
1898 int c;
1899 int consumed_chars = 0;
1900 bool mseq_found = 0;
1902 ONE_MORE_BYTE (c);
1903 if (c < 0)
1905 c = -c;
1906 charset_ID = emacs_mule_charset[0];
1908 else
1910 if (c >= 0xA0)
1912 if (cmp_status->state != COMPOSING_NO
1913 && cmp_status->old_form)
1915 if (cmp_status->state == COMPOSING_CHAR)
1917 if (c == 0xA0)
1919 ONE_MORE_BYTE (c);
1920 c -= 0x80;
1921 if (c < 0)
1922 goto invalid_code;
1924 else
1925 c -= 0x20;
1926 mseq_found = 1;
1928 else
1930 *nbytes = src - src_base;
1931 *nchars = consumed_chars;
1932 return -c;
1935 else
1936 goto invalid_code;
1939 switch (emacs_mule_bytes[c])
1941 case 2:
1942 if ((charset_ID = emacs_mule_charset[c]) < 0)
1943 goto invalid_code;
1944 ONE_MORE_BYTE (c);
1945 if (c < 0xA0)
1946 goto invalid_code;
1947 code = c & 0x7F;
1948 break;
1950 case 3:
1951 if (c == EMACS_MULE_LEADING_CODE_PRIVATE_11
1952 || c == EMACS_MULE_LEADING_CODE_PRIVATE_12)
1954 ONE_MORE_BYTE (c);
1955 if (c < 0xA0 || (charset_ID = emacs_mule_charset[c]) < 0)
1956 goto invalid_code;
1957 ONE_MORE_BYTE (c);
1958 if (c < 0xA0)
1959 goto invalid_code;
1960 code = c & 0x7F;
1962 else
1964 if ((charset_ID = emacs_mule_charset[c]) < 0)
1965 goto invalid_code;
1966 ONE_MORE_BYTE (c);
1967 if (c < 0xA0)
1968 goto invalid_code;
1969 code = (c & 0x7F) << 8;
1970 ONE_MORE_BYTE (c);
1971 if (c < 0xA0)
1972 goto invalid_code;
1973 code |= c & 0x7F;
1975 break;
1977 case 4:
1978 ONE_MORE_BYTE (c);
1979 if (c < 0 || (charset_ID = emacs_mule_charset[c]) < 0)
1980 goto invalid_code;
1981 ONE_MORE_BYTE (c);
1982 if (c < 0xA0)
1983 goto invalid_code;
1984 code = (c & 0x7F) << 8;
1985 ONE_MORE_BYTE (c);
1986 if (c < 0xA0)
1987 goto invalid_code;
1988 code |= c & 0x7F;
1989 break;
1991 case 1:
1992 code = c;
1993 charset_ID = ASCII_BYTE_P (code) ? charset_ascii : charset_eight_bit;
1994 break;
1996 default:
1997 emacs_abort ();
1999 CODING_DECODE_CHAR (coding, src, src_base, src_end,
2000 CHARSET_FROM_ID (charset_ID), code, c);
2001 if (c < 0)
2002 goto invalid_code;
2004 *nbytes = src - src_base;
2005 *nchars = consumed_chars;
2006 if (id)
2007 *id = charset_ID;
2008 return (mseq_found ? -c : c);
2010 no_more_source:
2011 return -2;
2013 invalid_code:
2014 return -1;
2018 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
2020 /* Handle these composition sequence ('|': the end of header elements,
2021 BYTES and CHARS >= 0xA0):
2023 (1) relative composition: 0x80 0xF2 BYTES CHARS | CHAR ...
2024 (2) altchar composition: 0x80 0xF4 BYTES CHARS | ALT ... ALT CHAR ...
2025 (3) alt&rule composition: 0x80 0xF5 BYTES CHARS | ALT RULE ... ALT CHAR ...
2027 and these old form:
2029 (4) relative composition: 0x80 | MSEQ ... MSEQ
2030 (5) rulebase composition: 0x80 0xFF | MSEQ MRULE ... MSEQ
2032 When the starter 0x80 and the following header elements are found,
2033 this annotation header is produced.
2035 [ -LENGTH(==-5) CODING_ANNOTATE_COMPOSITION_MASK NCHARS NBYTES METHOD ]
2037 NCHARS is CHARS - 0xA0 for (1), (2), (3), and 0 for (4), (5).
2038 NBYTES is BYTES - 0xA0 for (1), (2), (3), and 0 for (4), (5).
2040 Then, upon reading the following elements, these codes are produced
2041 until the composition end is found:
2043 (1) CHAR ... CHAR
2044 (2) ALT ... ALT CHAR ... CHAR
2045 (3) ALT -2 DECODED-RULE ALT -2 DECODED-RULE ... ALT CHAR ... CHAR
2046 (4) CHAR ... CHAR
2047 (5) CHAR -2 DECODED-RULE CHAR -2 DECODED-RULE ... CHAR
2049 When the composition end is found, LENGTH and NCHARS in the
2050 annotation header is updated as below:
2052 (1) LENGTH: unchanged, NCHARS: unchanged
2053 (2) LENGTH: length of the whole sequence minus NCHARS, NCHARS: unchanged
2054 (3) LENGTH: length of the whole sequence minus NCHARS, NCHARS: unchanged
2055 (4) LENGTH: unchanged, NCHARS: number of CHARs
2056 (5) LENGTH: unchanged, NCHARS: number of CHARs
2058 If an error is found while composing, the annotation header is
2059 changed to the original composition header (plus filler -1s) as
2060 below:
2062 (1),(2),(3) [ 0x80 0xF2+METHOD BYTES CHARS -1 ]
2063 (5) [ 0x80 0xFF -1 -1- -1 ]
2065 and the sequence [ -2 DECODED-RULE ] is changed to the original
2066 byte sequence as below:
2067 o the original byte sequence is B: [ B -1 ]
2068 o the original byte sequence is B1 B2: [ B1 B2 ]
2070 Most of the routines are implemented by macros because many
2071 variables and labels in the caller decode_coding_emacs_mule must be
2072 accessible, and they are usually called just once (thus doesn't
2073 increase the size of compiled object). */
2075 /* Decode a composition rule represented by C as a component of
2076 composition sequence of Emacs 20 style. Set RULE to the decoded
2077 rule. */
2079 #define DECODE_EMACS_MULE_COMPOSITION_RULE_20(c, rule) \
2080 do { \
2081 int gref, nref; \
2083 c -= 0xA0; \
2084 if (c < 0 || c >= 81) \
2085 goto invalid_code; \
2086 gref = c / 9, nref = c % 9; \
2087 if (gref == 4) gref = 10; \
2088 if (nref == 4) nref = 10; \
2089 rule = COMPOSITION_ENCODE_RULE (gref, nref); \
2090 } while (0)
2093 /* Decode a composition rule represented by C and the following byte
2094 at SRC as a component of composition sequence of Emacs 21 style.
2095 Set RULE to the decoded rule. */
2097 #define DECODE_EMACS_MULE_COMPOSITION_RULE_21(c, rule) \
2098 do { \
2099 int gref, nref; \
2101 gref = c - 0x20; \
2102 if (gref < 0 || gref >= 81) \
2103 goto invalid_code; \
2104 ONE_MORE_BYTE (c); \
2105 nref = c - 0x20; \
2106 if (nref < 0 || nref >= 81) \
2107 goto invalid_code; \
2108 rule = COMPOSITION_ENCODE_RULE (gref, nref); \
2109 } while (0)
2112 /* Start of Emacs 21 style format. The first three bytes at SRC are
2113 (METHOD - 0xF2), (BYTES - 0xA0), (CHARS - 0xA0), where BYTES is the
2114 byte length of this composition information, CHARS is the number of
2115 characters composed by this composition. */
2117 #define DECODE_EMACS_MULE_21_COMPOSITION() \
2118 do { \
2119 enum composition_method method = c - 0xF2; \
2120 int nbytes, nchars; \
2122 ONE_MORE_BYTE (c); \
2123 if (c < 0) \
2124 goto invalid_code; \
2125 nbytes = c - 0xA0; \
2126 if (nbytes < 3 || (method == COMPOSITION_RELATIVE && nbytes != 4)) \
2127 goto invalid_code; \
2128 ONE_MORE_BYTE (c); \
2129 nchars = c - 0xA0; \
2130 if (nchars <= 0 || nchars >= MAX_COMPOSITION_COMPONENTS) \
2131 goto invalid_code; \
2132 cmp_status->old_form = 0; \
2133 cmp_status->method = method; \
2134 if (method == COMPOSITION_RELATIVE) \
2135 cmp_status->state = COMPOSING_CHAR; \
2136 else \
2137 cmp_status->state = COMPOSING_COMPONENT_CHAR; \
2138 cmp_status->length = MAX_ANNOTATION_LENGTH; \
2139 cmp_status->nchars = nchars; \
2140 cmp_status->ncomps = nbytes - 4; \
2141 ADD_COMPOSITION_DATA (charbuf, nchars, nbytes, method); \
2142 } while (0)
2145 /* Start of Emacs 20 style format for relative composition. */
2147 #define DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION() \
2148 do { \
2149 cmp_status->old_form = 1; \
2150 cmp_status->method = COMPOSITION_RELATIVE; \
2151 cmp_status->state = COMPOSING_CHAR; \
2152 cmp_status->length = MAX_ANNOTATION_LENGTH; \
2153 cmp_status->nchars = cmp_status->ncomps = 0; \
2154 ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \
2155 } while (0)
2158 /* Start of Emacs 20 style format for rule-base composition. */
2160 #define DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION() \
2161 do { \
2162 cmp_status->old_form = 1; \
2163 cmp_status->method = COMPOSITION_WITH_RULE; \
2164 cmp_status->state = COMPOSING_CHAR; \
2165 cmp_status->length = MAX_ANNOTATION_LENGTH; \
2166 cmp_status->nchars = cmp_status->ncomps = 0; \
2167 ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \
2168 } while (0)
2171 #define DECODE_EMACS_MULE_COMPOSITION_START() \
2172 do { \
2173 const unsigned char *current_src = src; \
2175 ONE_MORE_BYTE (c); \
2176 if (c < 0) \
2177 goto invalid_code; \
2178 if (c - 0xF2 >= COMPOSITION_RELATIVE \
2179 && c - 0xF2 <= COMPOSITION_WITH_RULE_ALTCHARS) \
2180 DECODE_EMACS_MULE_21_COMPOSITION (); \
2181 else if (c < 0xA0) \
2182 goto invalid_code; \
2183 else if (c < 0xC0) \
2185 DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION (); \
2186 /* Re-read C as a composition component. */ \
2187 src = current_src; \
2189 else if (c == 0xFF) \
2190 DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION (); \
2191 else \
2192 goto invalid_code; \
2193 } while (0)
2195 #define EMACS_MULE_COMPOSITION_END() \
2196 do { \
2197 int idx = - cmp_status->length; \
2199 if (cmp_status->old_form) \
2200 charbuf[idx + 2] = cmp_status->nchars; \
2201 else if (cmp_status->method > COMPOSITION_RELATIVE) \
2202 charbuf[idx] = charbuf[idx + 2] - cmp_status->length; \
2203 cmp_status->state = COMPOSING_NO; \
2204 } while (0)
2207 static int
2208 emacs_mule_finish_composition (int *charbuf,
2209 struct composition_status *cmp_status)
2211 int idx = - cmp_status->length;
2212 int new_chars;
2214 if (cmp_status->old_form && cmp_status->nchars > 0)
2216 charbuf[idx + 2] = cmp_status->nchars;
2217 new_chars = 0;
2218 if (cmp_status->method == COMPOSITION_WITH_RULE
2219 && cmp_status->state == COMPOSING_CHAR)
2221 /* The last rule was invalid. */
2222 int rule = charbuf[-1] + 0xA0;
2224 charbuf[-2] = BYTE8_TO_CHAR (rule);
2225 charbuf[-1] = -1;
2226 new_chars = 1;
2229 else
2231 charbuf[idx++] = BYTE8_TO_CHAR (0x80);
2233 if (cmp_status->method == COMPOSITION_WITH_RULE)
2235 charbuf[idx++] = BYTE8_TO_CHAR (0xFF);
2236 charbuf[idx++] = -3;
2237 charbuf[idx++] = 0;
2238 new_chars = 1;
2240 else
2242 int nchars = charbuf[idx + 1] + 0xA0;
2243 int nbytes = charbuf[idx + 2] + 0xA0;
2245 charbuf[idx++] = BYTE8_TO_CHAR (0xF2 + cmp_status->method);
2246 charbuf[idx++] = BYTE8_TO_CHAR (nbytes);
2247 charbuf[idx++] = BYTE8_TO_CHAR (nchars);
2248 charbuf[idx++] = -1;
2249 new_chars = 4;
2252 cmp_status->state = COMPOSING_NO;
2253 return new_chars;
2256 #define EMACS_MULE_MAYBE_FINISH_COMPOSITION() \
2257 do { \
2258 if (cmp_status->state != COMPOSING_NO) \
2259 char_offset += emacs_mule_finish_composition (charbuf, cmp_status); \
2260 } while (0)
2263 static void
2264 decode_coding_emacs_mule (struct coding_system *coding)
2266 const unsigned char *src = coding->source + coding->consumed;
2267 const unsigned char *src_end = coding->source + coding->src_bytes;
2268 const unsigned char *src_base;
2269 int *charbuf = coding->charbuf + coding->charbuf_used;
2270 /* We may produce two annotations (charset and composition) in one
2271 loop and one more charset annotation at the end. */
2272 int *charbuf_end
2273 = coding->charbuf + coding->charbuf_size - (MAX_ANNOTATION_LENGTH * 3)
2274 /* We can produce up to 2 characters in a loop. */
2275 - 1;
2276 ptrdiff_t consumed_chars = 0, consumed_chars_base;
2277 bool multibytep = coding->src_multibyte;
2278 ptrdiff_t char_offset = coding->produced_char;
2279 ptrdiff_t last_offset = char_offset;
2280 int last_id = charset_ascii;
2281 bool eol_dos
2282 = !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos);
2283 int byte_after_cr = -1;
2284 struct composition_status *cmp_status = &coding->spec.emacs_mule.cmp_status;
2286 if (cmp_status->state != COMPOSING_NO)
2288 int i;
2290 if (charbuf_end - charbuf < cmp_status->length)
2291 emacs_abort ();
2292 for (i = 0; i < cmp_status->length; i++)
2293 *charbuf++ = cmp_status->carryover[i];
2294 coding->annotated = 1;
2297 while (1)
2299 int c, id IF_LINT (= 0);
2301 src_base = src;
2302 consumed_chars_base = consumed_chars;
2304 if (charbuf >= charbuf_end)
2306 if (byte_after_cr >= 0)
2307 src_base--;
2308 break;
2311 if (byte_after_cr >= 0)
2312 c = byte_after_cr, byte_after_cr = -1;
2313 else
2314 ONE_MORE_BYTE (c);
2316 if (c < 0 || c == 0x80)
2318 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2319 if (c < 0)
2321 *charbuf++ = -c;
2322 char_offset++;
2324 else
2325 DECODE_EMACS_MULE_COMPOSITION_START ();
2326 continue;
2329 if (c < 0x80)
2331 if (eol_dos && c == '\r')
2332 ONE_MORE_BYTE (byte_after_cr);
2333 id = charset_ascii;
2334 if (cmp_status->state != COMPOSING_NO)
2336 if (cmp_status->old_form)
2337 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2338 else if (cmp_status->state >= COMPOSING_COMPONENT_CHAR)
2339 cmp_status->ncomps--;
2342 else
2344 int nchars IF_LINT (= 0), nbytes IF_LINT (= 0);
2345 /* emacs_mule_char can load a charset map from a file, which
2346 allocates a large structure and might cause buffer text
2347 to be relocated as result. Thus, we need to remember the
2348 original pointer to buffer text, and fix up all related
2349 pointers after the call. */
2350 const unsigned char *orig = coding->source;
2351 ptrdiff_t offset;
2353 c = emacs_mule_char (coding, src_base, &nbytes, &nchars, &id,
2354 cmp_status);
2355 offset = coding->source - orig;
2356 if (offset)
2358 src += offset;
2359 src_base += offset;
2360 src_end += offset;
2362 if (c < 0)
2364 if (c == -1)
2365 goto invalid_code;
2366 if (c == -2)
2367 break;
2369 src = src_base + nbytes;
2370 consumed_chars = consumed_chars_base + nchars;
2371 if (cmp_status->state >= COMPOSING_COMPONENT_CHAR)
2372 cmp_status->ncomps -= nchars;
2375 /* Now if C >= 0, we found a normally encoded character, if C <
2376 0, we found an old-style composition component character or
2377 rule. */
2379 if (cmp_status->state == COMPOSING_NO)
2381 if (last_id != id)
2383 if (last_id != charset_ascii)
2384 ADD_CHARSET_DATA (charbuf, char_offset - last_offset,
2385 last_id);
2386 last_id = id;
2387 last_offset = char_offset;
2389 *charbuf++ = c;
2390 char_offset++;
2392 else if (cmp_status->state == COMPOSING_CHAR)
2394 if (cmp_status->old_form)
2396 if (c >= 0)
2398 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2399 *charbuf++ = c;
2400 char_offset++;
2402 else
2404 *charbuf++ = -c;
2405 cmp_status->nchars++;
2406 cmp_status->length++;
2407 if (cmp_status->nchars == MAX_COMPOSITION_COMPONENTS)
2408 EMACS_MULE_COMPOSITION_END ();
2409 else if (cmp_status->method == COMPOSITION_WITH_RULE)
2410 cmp_status->state = COMPOSING_RULE;
2413 else
2415 *charbuf++ = c;
2416 cmp_status->length++;
2417 cmp_status->nchars--;
2418 if (cmp_status->nchars == 0)
2419 EMACS_MULE_COMPOSITION_END ();
2422 else if (cmp_status->state == COMPOSING_RULE)
2424 int rule;
2426 if (c >= 0)
2428 EMACS_MULE_COMPOSITION_END ();
2429 *charbuf++ = c;
2430 char_offset++;
2432 else
2434 c = -c;
2435 DECODE_EMACS_MULE_COMPOSITION_RULE_20 (c, rule);
2436 if (rule < 0)
2437 goto invalid_code;
2438 *charbuf++ = -2;
2439 *charbuf++ = rule;
2440 cmp_status->length += 2;
2441 cmp_status->state = COMPOSING_CHAR;
2444 else if (cmp_status->state == COMPOSING_COMPONENT_CHAR)
2446 *charbuf++ = c;
2447 cmp_status->length++;
2448 if (cmp_status->ncomps == 0)
2449 cmp_status->state = COMPOSING_CHAR;
2450 else if (cmp_status->ncomps > 0)
2452 if (cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS)
2453 cmp_status->state = COMPOSING_COMPONENT_RULE;
2455 else
2456 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2458 else /* COMPOSING_COMPONENT_RULE */
2460 int rule;
2462 DECODE_EMACS_MULE_COMPOSITION_RULE_21 (c, rule);
2463 if (rule < 0)
2464 goto invalid_code;
2465 *charbuf++ = -2;
2466 *charbuf++ = rule;
2467 cmp_status->length += 2;
2468 cmp_status->ncomps--;
2469 if (cmp_status->ncomps > 0)
2470 cmp_status->state = COMPOSING_COMPONENT_CHAR;
2471 else
2472 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2474 continue;
2476 invalid_code:
2477 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2478 src = src_base;
2479 consumed_chars = consumed_chars_base;
2480 ONE_MORE_BYTE (c);
2481 *charbuf++ = ASCII_BYTE_P (c) ? c : BYTE8_TO_CHAR (c);
2482 char_offset++;
2483 coding->errors++;
2486 no_more_source:
2487 if (cmp_status->state != COMPOSING_NO)
2489 if (coding->mode & CODING_MODE_LAST_BLOCK)
2490 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2491 else
2493 int i;
2495 charbuf -= cmp_status->length;
2496 for (i = 0; i < cmp_status->length; i++)
2497 cmp_status->carryover[i] = charbuf[i];
2500 if (last_id != charset_ascii)
2501 ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id);
2502 coding->consumed_char += consumed_chars_base;
2503 coding->consumed = src_base - coding->source;
2504 coding->charbuf_used = charbuf - coding->charbuf;
2508 #define EMACS_MULE_LEADING_CODES(id, codes) \
2509 do { \
2510 if (id < 0xA0) \
2511 codes[0] = id, codes[1] = 0; \
2512 else if (id < 0xE0) \
2513 codes[0] = 0x9A, codes[1] = id; \
2514 else if (id < 0xF0) \
2515 codes[0] = 0x9B, codes[1] = id; \
2516 else if (id < 0xF5) \
2517 codes[0] = 0x9C, codes[1] = id; \
2518 else \
2519 codes[0] = 0x9D, codes[1] = id; \
2520 } while (0);
2523 static bool
2524 encode_coding_emacs_mule (struct coding_system *coding)
2526 bool multibytep = coding->dst_multibyte;
2527 int *charbuf = coding->charbuf;
2528 int *charbuf_end = charbuf + coding->charbuf_used;
2529 unsigned char *dst = coding->destination + coding->produced;
2530 unsigned char *dst_end = coding->destination + coding->dst_bytes;
2531 int safe_room = 8;
2532 ptrdiff_t produced_chars = 0;
2533 Lisp_Object attrs, charset_list;
2534 int c;
2535 int preferred_charset_id = -1;
2537 CODING_GET_INFO (coding, attrs, charset_list);
2538 if (! EQ (charset_list, Vemacs_mule_charset_list))
2540 charset_list = Vemacs_mule_charset_list;
2541 ASET (attrs, coding_attr_charset_list, charset_list);
2544 while (charbuf < charbuf_end)
2546 ASSURE_DESTINATION (safe_room);
2547 c = *charbuf++;
2549 if (c < 0)
2551 /* Handle an annotation. */
2552 switch (*charbuf)
2554 case CODING_ANNOTATE_COMPOSITION_MASK:
2555 /* Not yet implemented. */
2556 break;
2557 case CODING_ANNOTATE_CHARSET_MASK:
2558 preferred_charset_id = charbuf[3];
2559 if (preferred_charset_id >= 0
2560 && NILP (Fmemq (make_number (preferred_charset_id),
2561 charset_list)))
2562 preferred_charset_id = -1;
2563 break;
2564 default:
2565 emacs_abort ();
2567 charbuf += -c - 1;
2568 continue;
2571 if (ASCII_CHAR_P (c))
2572 EMIT_ONE_ASCII_BYTE (c);
2573 else if (CHAR_BYTE8_P (c))
2575 c = CHAR_TO_BYTE8 (c);
2576 EMIT_ONE_BYTE (c);
2578 else
2580 struct charset *charset;
2581 unsigned code;
2582 int dimension;
2583 int emacs_mule_id;
2584 unsigned char leading_codes[2];
2586 if (preferred_charset_id >= 0)
2588 bool result;
2590 charset = CHARSET_FROM_ID (preferred_charset_id);
2591 CODING_CHAR_CHARSET_P (coding, dst, dst_end, c, charset, result);
2592 if (result)
2593 code = ENCODE_CHAR (charset, c);
2594 else
2595 CODING_CHAR_CHARSET (coding, dst, dst_end, c, charset_list,
2596 &code, charset);
2598 else
2599 CODING_CHAR_CHARSET (coding, dst, dst_end, c, charset_list,
2600 &code, charset);
2601 if (! charset)
2603 c = coding->default_char;
2604 if (ASCII_CHAR_P (c))
2606 EMIT_ONE_ASCII_BYTE (c);
2607 continue;
2609 CODING_CHAR_CHARSET (coding, dst, dst_end, c, charset_list,
2610 &code, charset);
2612 dimension = CHARSET_DIMENSION (charset);
2613 emacs_mule_id = CHARSET_EMACS_MULE_ID (charset);
2614 EMACS_MULE_LEADING_CODES (emacs_mule_id, leading_codes);
2615 EMIT_ONE_BYTE (leading_codes[0]);
2616 if (leading_codes[1])
2617 EMIT_ONE_BYTE (leading_codes[1]);
2618 if (dimension == 1)
2619 EMIT_ONE_BYTE (code | 0x80);
2620 else
2622 code |= 0x8080;
2623 EMIT_ONE_BYTE (code >> 8);
2624 EMIT_ONE_BYTE (code & 0xFF);
2628 record_conversion_result (coding, CODING_RESULT_SUCCESS);
2629 coding->produced_char += produced_chars;
2630 coding->produced = dst - coding->destination;
2631 return 0;
2635 /*** 7. ISO2022 handlers ***/
2637 /* The following note describes the coding system ISO2022 briefly.
2638 Since the intention of this note is to help understand the
2639 functions in this file, some parts are NOT ACCURATE or are OVERLY
2640 SIMPLIFIED. For thorough understanding, please refer to the
2641 original document of ISO2022. This is equivalent to the standard
2642 ECMA-35, obtainable from <URL:http://www.ecma.ch/> (*).
2644 ISO2022 provides many mechanisms to encode several character sets
2645 in 7-bit and 8-bit environments. For 7-bit environments, all text
2646 is encoded using bytes less than 128. This may make the encoded
2647 text a little bit longer, but the text passes more easily through
2648 several types of gateway, some of which strip off the MSB (Most
2649 Significant Bit).
2651 There are two kinds of character sets: control character sets and
2652 graphic character sets. The former contain control characters such
2653 as `newline' and `escape' to provide control functions (control
2654 functions are also provided by escape sequences). The latter
2655 contain graphic characters such as 'A' and '-'. Emacs recognizes
2656 two control character sets and many graphic character sets.
2658 Graphic character sets are classified into one of the following
2659 four classes, according to the number of bytes (DIMENSION) and
2660 number of characters in one dimension (CHARS) of the set:
2661 - DIMENSION1_CHARS94
2662 - DIMENSION1_CHARS96
2663 - DIMENSION2_CHARS94
2664 - DIMENSION2_CHARS96
2666 In addition, each character set is assigned an identification tag,
2667 unique for each set, called the "final character" (denoted as <F>
2668 hereafter). The <F> of each character set is decided by ECMA(*)
2669 when it is registered in ISO. The code range of <F> is 0x30..0x7F
2670 (0x30..0x3F are for private use only).
2672 Note (*): ECMA = European Computer Manufacturers Association
2674 Here are examples of graphic character sets [NAME(<F>)]:
2675 o DIMENSION1_CHARS94 -- ASCII('B'), right-half-of-JISX0201('I'), ...
2676 o DIMENSION1_CHARS96 -- right-half-of-ISO8859-1('A'), ...
2677 o DIMENSION2_CHARS94 -- GB2312('A'), JISX0208('B'), ...
2678 o DIMENSION2_CHARS96 -- none for the moment
2680 A code area (1 byte=8 bits) is divided into 4 areas, C0, GL, C1, and GR.
2681 C0 [0x00..0x1F] -- control character plane 0
2682 GL [0x20..0x7F] -- graphic character plane 0
2683 C1 [0x80..0x9F] -- control character plane 1
2684 GR [0xA0..0xFF] -- graphic character plane 1
2686 A control character set is directly designated and invoked to C0 or
2687 C1 by an escape sequence. The most common case is that:
2688 - ISO646's control character set is designated/invoked to C0, and
2689 - ISO6429's control character set is designated/invoked to C1,
2690 and usually these designations/invocations are omitted in encoded
2691 text. In a 7-bit environment, only C0 can be used, and a control
2692 character for C1 is encoded by an appropriate escape sequence to
2693 fit into the environment. All control characters for C1 are
2694 defined to have corresponding escape sequences.
2696 A graphic character set is at first designated to one of four
2697 graphic registers (G0 through G3), then these graphic registers are
2698 invoked to GL or GR. These designations and invocations can be
2699 done independently. The most common case is that G0 is invoked to
2700 GL, G1 is invoked to GR, and ASCII is designated to G0. Usually
2701 these invocations and designations are omitted in encoded text.
2702 In a 7-bit environment, only GL can be used.
2704 When a graphic character set of CHARS94 is invoked to GL, codes
2705 0x20 and 0x7F of the GL area work as control characters SPACE and
2706 DEL respectively, and codes 0xA0 and 0xFF of the GR area should not
2707 be used.
2709 There are two ways of invocation: locking-shift and single-shift.
2710 With locking-shift, the invocation lasts until the next different
2711 invocation, whereas with single-shift, the invocation affects the
2712 following character only and doesn't affect the locking-shift
2713 state. Invocations are done by the following control characters or
2714 escape sequences:
2716 ----------------------------------------------------------------------
2717 abbrev function cntrl escape seq description
2718 ----------------------------------------------------------------------
2719 SI/LS0 (shift-in) 0x0F none invoke G0 into GL
2720 SO/LS1 (shift-out) 0x0E none invoke G1 into GL
2721 LS2 (locking-shift-2) none ESC 'n' invoke G2 into GL
2722 LS3 (locking-shift-3) none ESC 'o' invoke G3 into GL
2723 LS1R (locking-shift-1 right) none ESC '~' invoke G1 into GR (*)
2724 LS2R (locking-shift-2 right) none ESC '}' invoke G2 into GR (*)
2725 LS3R (locking-shift 3 right) none ESC '|' invoke G3 into GR (*)
2726 SS2 (single-shift-2) 0x8E ESC 'N' invoke G2 for one char
2727 SS3 (single-shift-3) 0x8F ESC 'O' invoke G3 for one char
2728 ----------------------------------------------------------------------
2729 (*) These are not used by any known coding system.
2731 Control characters for these functions are defined by macros
2732 ISO_CODE_XXX in `coding.h'.
2734 Designations are done by the following escape sequences:
2735 ----------------------------------------------------------------------
2736 escape sequence description
2737 ----------------------------------------------------------------------
2738 ESC '(' <F> designate DIMENSION1_CHARS94<F> to G0
2739 ESC ')' <F> designate DIMENSION1_CHARS94<F> to G1
2740 ESC '*' <F> designate DIMENSION1_CHARS94<F> to G2
2741 ESC '+' <F> designate DIMENSION1_CHARS94<F> to G3
2742 ESC ',' <F> designate DIMENSION1_CHARS96<F> to G0 (*)
2743 ESC '-' <F> designate DIMENSION1_CHARS96<F> to G1
2744 ESC '.' <F> designate DIMENSION1_CHARS96<F> to G2
2745 ESC '/' <F> designate DIMENSION1_CHARS96<F> to G3
2746 ESC '$' '(' <F> designate DIMENSION2_CHARS94<F> to G0 (**)
2747 ESC '$' ')' <F> designate DIMENSION2_CHARS94<F> to G1
2748 ESC '$' '*' <F> designate DIMENSION2_CHARS94<F> to G2
2749 ESC '$' '+' <F> designate DIMENSION2_CHARS94<F> to G3
2750 ESC '$' ',' <F> designate DIMENSION2_CHARS96<F> to G0 (*)
2751 ESC '$' '-' <F> designate DIMENSION2_CHARS96<F> to G1
2752 ESC '$' '.' <F> designate DIMENSION2_CHARS96<F> to G2
2753 ESC '$' '/' <F> designate DIMENSION2_CHARS96<F> to G3
2754 ----------------------------------------------------------------------
2756 In this list, "DIMENSION1_CHARS94<F>" means a graphic character set
2757 of dimension 1, chars 94, and final character <F>, etc...
2759 Note (*): Although these designations are not allowed in ISO2022,
2760 Emacs accepts them on decoding, and produces them on encoding
2761 CHARS96 character sets in a coding system which is characterized as
2762 7-bit environment, non-locking-shift, and non-single-shift.
2764 Note (**): If <F> is '@', 'A', or 'B', the intermediate character
2765 '(' must be omitted. We refer to this as "short-form" hereafter.
2767 Now you may notice that there are a lot of ways of encoding the
2768 same multilingual text in ISO2022. Actually, there exist many
2769 coding systems such as Compound Text (used in X11's inter client
2770 communication, ISO-2022-JP (used in Japanese Internet), ISO-2022-KR
2771 (used in Korean Internet), EUC (Extended UNIX Code, used in Asian
2772 localized platforms), and all of these are variants of ISO2022.
2774 In addition to the above, Emacs handles two more kinds of escape
2775 sequences: ISO6429's direction specification and Emacs' private
2776 sequence for specifying character composition.
2778 ISO6429's direction specification takes the following form:
2779 o CSI ']' -- end of the current direction
2780 o CSI '0' ']' -- end of the current direction
2781 o CSI '1' ']' -- start of left-to-right text
2782 o CSI '2' ']' -- start of right-to-left text
2783 The control character CSI (0x9B: control sequence introducer) is
2784 abbreviated to the escape sequence ESC '[' in a 7-bit environment.
2786 Character composition specification takes the following form:
2787 o ESC '0' -- start relative composition
2788 o ESC '1' -- end composition
2789 o ESC '2' -- start rule-base composition (*)
2790 o ESC '3' -- start relative composition with alternate chars (**)
2791 o ESC '4' -- start rule-base composition with alternate chars (**)
2792 Since these are not standard escape sequences of any ISO standard,
2793 the use of them with these meanings is restricted to Emacs only.
2795 (*) This form is used only in Emacs 20.7 and older versions,
2796 but newer versions can safely decode it.
2797 (**) This form is used only in Emacs 21.1 and newer versions,
2798 and older versions can't decode it.
2800 Here's a list of example usages of these composition escape
2801 sequences (categorized by `enum composition_method').
2803 COMPOSITION_RELATIVE:
2804 ESC 0 CHAR [ CHAR ] ESC 1
2805 COMPOSITION_WITH_RULE:
2806 ESC 2 CHAR [ RULE CHAR ] ESC 1
2807 COMPOSITION_WITH_ALTCHARS:
2808 ESC 3 ALTCHAR [ ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1
2809 COMPOSITION_WITH_RULE_ALTCHARS:
2810 ESC 4 ALTCHAR [ RULE ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1 */
2812 static enum iso_code_class_type iso_code_class[256];
2814 #define SAFE_CHARSET_P(coding, id) \
2815 ((id) <= (coding)->max_charset_id \
2816 && (coding)->safe_charsets[id] != 255)
2818 static void
2819 setup_iso_safe_charsets (Lisp_Object attrs)
2821 Lisp_Object charset_list, safe_charsets;
2822 Lisp_Object request;
2823 Lisp_Object reg_usage;
2824 Lisp_Object tail;
2825 EMACS_INT reg94, reg96;
2826 int flags = XINT (AREF (attrs, coding_attr_iso_flags));
2827 int max_charset_id;
2829 charset_list = CODING_ATTR_CHARSET_LIST (attrs);
2830 if ((flags & CODING_ISO_FLAG_FULL_SUPPORT)
2831 && ! EQ (charset_list, Viso_2022_charset_list))
2833 charset_list = Viso_2022_charset_list;
2834 ASET (attrs, coding_attr_charset_list, charset_list);
2835 ASET (attrs, coding_attr_safe_charsets, Qnil);
2838 if (STRINGP (AREF (attrs, coding_attr_safe_charsets)))
2839 return;
2841 max_charset_id = 0;
2842 for (tail = charset_list; CONSP (tail); tail = XCDR (tail))
2844 int id = XINT (XCAR (tail));
2845 if (max_charset_id < id)
2846 max_charset_id = id;
2849 safe_charsets = make_uninit_string (max_charset_id + 1);
2850 memset (SDATA (safe_charsets), 255, max_charset_id + 1);
2851 request = AREF (attrs, coding_attr_iso_request);
2852 reg_usage = AREF (attrs, coding_attr_iso_usage);
2853 reg94 = XINT (XCAR (reg_usage));
2854 reg96 = XINT (XCDR (reg_usage));
2856 for (tail = charset_list; CONSP (tail); tail = XCDR (tail))
2858 Lisp_Object id;
2859 Lisp_Object reg;
2860 struct charset *charset;
2862 id = XCAR (tail);
2863 charset = CHARSET_FROM_ID (XINT (id));
2864 reg = Fcdr (Fassq (id, request));
2865 if (! NILP (reg))
2866 SSET (safe_charsets, XINT (id), XINT (reg));
2867 else if (charset->iso_chars_96)
2869 if (reg96 < 4)
2870 SSET (safe_charsets, XINT (id), reg96);
2872 else
2874 if (reg94 < 4)
2875 SSET (safe_charsets, XINT (id), reg94);
2878 ASET (attrs, coding_attr_safe_charsets, safe_charsets);
2882 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
2883 Return true if a text is encoded in one of ISO-2022 based coding
2884 systems. */
2886 static bool
2887 detect_coding_iso_2022 (struct coding_system *coding,
2888 struct coding_detection_info *detect_info)
2890 const unsigned char *src = coding->source, *src_base = src;
2891 const unsigned char *src_end = coding->source + coding->src_bytes;
2892 bool multibytep = coding->src_multibyte;
2893 bool single_shifting = 0;
2894 int id;
2895 int c, c1;
2896 ptrdiff_t consumed_chars = 0;
2897 int i;
2898 int rejected = 0;
2899 int found = 0;
2900 int composition_count = -1;
2902 detect_info->checked |= CATEGORY_MASK_ISO;
2904 for (i = coding_category_iso_7; i <= coding_category_iso_8_else; i++)
2906 struct coding_system *this = &(coding_categories[i]);
2907 Lisp_Object attrs, val;
2909 if (this->id < 0)
2910 continue;
2911 attrs = CODING_ID_ATTRS (this->id);
2912 if (CODING_ISO_FLAGS (this) & CODING_ISO_FLAG_FULL_SUPPORT
2913 && ! EQ (CODING_ATTR_CHARSET_LIST (attrs), Viso_2022_charset_list))
2914 setup_iso_safe_charsets (attrs);
2915 val = CODING_ATTR_SAFE_CHARSETS (attrs);
2916 this->max_charset_id = SCHARS (val) - 1;
2917 this->safe_charsets = SDATA (val);
2920 /* A coding system of this category is always ASCII compatible. */
2921 src += coding->head_ascii;
2923 while (rejected != CATEGORY_MASK_ISO)
2925 src_base = src;
2926 ONE_MORE_BYTE (c);
2927 switch (c)
2929 case ISO_CODE_ESC:
2930 if (inhibit_iso_escape_detection)
2931 break;
2932 single_shifting = 0;
2933 ONE_MORE_BYTE (c);
2934 if (c == 'N' || c == 'O')
2936 /* ESC <Fe> for SS2 or SS3. */
2937 single_shifting = 1;
2938 rejected |= CATEGORY_MASK_ISO_7BIT | CATEGORY_MASK_ISO_8BIT;
2940 else if (c == '1')
2942 /* End of composition. */
2943 if (composition_count < 0
2944 || composition_count > MAX_COMPOSITION_COMPONENTS)
2945 /* Invalid */
2946 break;
2947 composition_count = -1;
2948 found |= CATEGORY_MASK_ISO;
2950 else if (c >= '0' && c <= '4')
2952 /* ESC <Fp> for start/end composition. */
2953 composition_count = 0;
2955 else
2957 if (c >= '(' && c <= '/')
2959 /* Designation sequence for a charset of dimension 1. */
2960 ONE_MORE_BYTE (c1);
2961 if (c1 < ' ' || c1 >= 0x80
2962 || (id = iso_charset_table[0][c >= ','][c1]) < 0)
2963 /* Invalid designation sequence. Just ignore. */
2964 break;
2966 else if (c == '$')
2968 /* Designation sequence for a charset of dimension 2. */
2969 ONE_MORE_BYTE (c);
2970 if (c >= '@' && c <= 'B')
2971 /* Designation for JISX0208.1978, GB2312, or JISX0208. */
2972 id = iso_charset_table[1][0][c];
2973 else if (c >= '(' && c <= '/')
2975 ONE_MORE_BYTE (c1);
2976 if (c1 < ' ' || c1 >= 0x80
2977 || (id = iso_charset_table[1][c >= ','][c1]) < 0)
2978 /* Invalid designation sequence. Just ignore. */
2979 break;
2981 else
2982 /* Invalid designation sequence. Just ignore it. */
2983 break;
2985 else
2987 /* Invalid escape sequence. Just ignore it. */
2988 break;
2991 /* We found a valid designation sequence for CHARSET. */
2992 rejected |= CATEGORY_MASK_ISO_8BIT;
2993 if (SAFE_CHARSET_P (&coding_categories[coding_category_iso_7],
2994 id))
2995 found |= CATEGORY_MASK_ISO_7;
2996 else
2997 rejected |= CATEGORY_MASK_ISO_7;
2998 if (SAFE_CHARSET_P (&coding_categories[coding_category_iso_7_tight],
2999 id))
3000 found |= CATEGORY_MASK_ISO_7_TIGHT;
3001 else
3002 rejected |= CATEGORY_MASK_ISO_7_TIGHT;
3003 if (SAFE_CHARSET_P (&coding_categories[coding_category_iso_7_else],
3004 id))
3005 found |= CATEGORY_MASK_ISO_7_ELSE;
3006 else
3007 rejected |= CATEGORY_MASK_ISO_7_ELSE;
3008 if (SAFE_CHARSET_P (&coding_categories[coding_category_iso_8_else],
3009 id))
3010 found |= CATEGORY_MASK_ISO_8_ELSE;
3011 else
3012 rejected |= CATEGORY_MASK_ISO_8_ELSE;
3014 break;
3016 case ISO_CODE_SO:
3017 case ISO_CODE_SI:
3018 /* Locking shift out/in. */
3019 if (inhibit_iso_escape_detection)
3020 break;
3021 single_shifting = 0;
3022 rejected |= CATEGORY_MASK_ISO_7BIT | CATEGORY_MASK_ISO_8BIT;
3023 break;
3025 case ISO_CODE_CSI:
3026 /* Control sequence introducer. */
3027 single_shifting = 0;
3028 rejected |= CATEGORY_MASK_ISO_7BIT | CATEGORY_MASK_ISO_7_ELSE;
3029 found |= CATEGORY_MASK_ISO_8_ELSE;
3030 goto check_extra_latin;
3032 case ISO_CODE_SS2:
3033 case ISO_CODE_SS3:
3034 /* Single shift. */
3035 if (inhibit_iso_escape_detection)
3036 break;
3037 single_shifting = 0;
3038 rejected |= CATEGORY_MASK_ISO_7BIT;
3039 if (CODING_ISO_FLAGS (&coding_categories[coding_category_iso_8_1])
3040 & CODING_ISO_FLAG_SINGLE_SHIFT)
3042 found |= CATEGORY_MASK_ISO_8_1;
3043 single_shifting = 1;
3045 if (CODING_ISO_FLAGS (&coding_categories[coding_category_iso_8_2])
3046 & CODING_ISO_FLAG_SINGLE_SHIFT)
3048 found |= CATEGORY_MASK_ISO_8_2;
3049 single_shifting = 1;
3051 if (single_shifting)
3052 break;
3053 goto check_extra_latin;
3055 default:
3056 if (c < 0)
3057 continue;
3058 if (c < 0x80)
3060 if (composition_count >= 0)
3061 composition_count++;
3062 single_shifting = 0;
3063 break;
3065 if (c >= 0xA0)
3067 rejected |= CATEGORY_MASK_ISO_7BIT | CATEGORY_MASK_ISO_7_ELSE;
3068 found |= CATEGORY_MASK_ISO_8_1;
3069 /* Check the length of succeeding codes of the range
3070 0xA0..0FF. If the byte length is even, we include
3071 CATEGORY_MASK_ISO_8_2 in `found'. We can check this
3072 only when we are not single shifting. */
3073 if (! single_shifting
3074 && ! (rejected & CATEGORY_MASK_ISO_8_2))
3076 int len = 1;
3077 while (src < src_end)
3079 src_base = src;
3080 ONE_MORE_BYTE (c);
3081 if (c < 0xA0)
3083 src = src_base;
3084 break;
3086 len++;
3089 if (len & 1 && src < src_end)
3091 rejected |= CATEGORY_MASK_ISO_8_2;
3092 if (composition_count >= 0)
3093 composition_count += len;
3095 else
3097 found |= CATEGORY_MASK_ISO_8_2;
3098 if (composition_count >= 0)
3099 composition_count += len / 2;
3102 break;
3104 check_extra_latin:
3105 if (! VECTORP (Vlatin_extra_code_table)
3106 || NILP (AREF (Vlatin_extra_code_table, c)))
3108 rejected = CATEGORY_MASK_ISO;
3109 break;
3111 if (CODING_ISO_FLAGS (&coding_categories[coding_category_iso_8_1])
3112 & CODING_ISO_FLAG_LATIN_EXTRA)
3113 found |= CATEGORY_MASK_ISO_8_1;
3114 else
3115 rejected |= CATEGORY_MASK_ISO_8_1;
3116 rejected |= CATEGORY_MASK_ISO_8_2;
3117 break;
3120 detect_info->rejected |= CATEGORY_MASK_ISO;
3121 return 0;
3123 no_more_source:
3124 detect_info->rejected |= rejected;
3125 detect_info->found |= (found & ~rejected);
3126 return 1;
3130 /* Set designation state into CODING. Set CHARS_96 to -1 if the
3131 escape sequence should be kept. */
3132 #define DECODE_DESIGNATION(reg, dim, chars_96, final) \
3133 do { \
3134 int id, prev; \
3136 if (final < '0' || final >= 128 \
3137 || ((id = ISO_CHARSET_TABLE (dim, chars_96, final)) < 0) \
3138 || !SAFE_CHARSET_P (coding, id)) \
3140 CODING_ISO_DESIGNATION (coding, reg) = -2; \
3141 chars_96 = -1; \
3142 break; \
3144 prev = CODING_ISO_DESIGNATION (coding, reg); \
3145 if (id == charset_jisx0201_roman) \
3147 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \
3148 id = charset_ascii; \
3150 else if (id == charset_jisx0208_1978) \
3152 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \
3153 id = charset_jisx0208; \
3155 CODING_ISO_DESIGNATION (coding, reg) = id; \
3156 /* If there was an invalid designation to REG previously, and this \
3157 designation is ASCII to REG, we should keep this designation \
3158 sequence. */ \
3159 if (prev == -2 && id == charset_ascii) \
3160 chars_96 = -1; \
3161 } while (0)
3164 /* Handle these composition sequence (ALT: alternate char):
3166 (1) relative composition: ESC 0 CHAR ... ESC 1
3167 (2) rulebase composition: ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1
3168 (3) altchar composition: ESC 3 ALT ... ALT ESC 0 CHAR ... ESC 1
3169 (4) alt&rule composition: ESC 4 ALT RULE ... ALT ESC 0 CHAR ... ESC 1
3171 When the start sequence (ESC 0/2/3/4) is found, this annotation
3172 header is produced.
3174 [ -LENGTH(==-5) CODING_ANNOTATE_COMPOSITION_MASK NCHARS(==0) 0 METHOD ]
3176 Then, upon reading CHAR or RULE (one or two bytes), these codes are
3177 produced until the end sequence (ESC 1) is found:
3179 (1) CHAR ... CHAR
3180 (2) CHAR -2 DECODED-RULE CHAR -2 DECODED-RULE ... CHAR
3181 (3) ALT ... ALT -1 -1 CHAR ... CHAR
3182 (4) ALT -2 DECODED-RULE ALT -2 DECODED-RULE ... ALT -1 -1 CHAR ... CHAR
3184 When the end sequence (ESC 1) is found, LENGTH and NCHARS in the
3185 annotation header is updated as below:
3187 (1) LENGTH: unchanged, NCHARS: number of CHARs
3188 (2) LENGTH: unchanged, NCHARS: number of CHARs
3189 (3) LENGTH: += number of ALTs + 2, NCHARS: number of CHARs
3190 (4) LENGTH: += number of ALTs * 3, NCHARS: number of CHARs
3192 If an error is found while composing, the annotation header is
3193 changed to:
3195 [ ESC '0'/'2'/'3'/'4' -2 0 ]
3197 and the sequence [ -2 DECODED-RULE ] is changed to the original
3198 byte sequence as below:
3199 o the original byte sequence is B: [ B -1 ]
3200 o the original byte sequence is B1 B2: [ B1 B2 ]
3201 and the sequence [ -1 -1 ] is changed to the original byte
3202 sequence:
3203 [ ESC '0' ]
3206 /* Decode a composition rule C1 and maybe one more byte from the
3207 source, and set RULE to the encoded composition rule. If the rule
3208 is invalid, goto invalid_code. */
3210 #define DECODE_COMPOSITION_RULE(rule) \
3211 do { \
3212 rule = c1 - 32; \
3213 if (rule < 0) \
3214 goto invalid_code; \
3215 if (rule < 81) /* old format (before ver.21) */ \
3217 int gref = (rule) / 9; \
3218 int nref = (rule) % 9; \
3219 if (gref == 4) gref = 10; \
3220 if (nref == 4) nref = 10; \
3221 rule = COMPOSITION_ENCODE_RULE (gref, nref); \
3223 else /* new format (after ver.21) */ \
3225 int b; \
3227 ONE_MORE_BYTE (b); \
3228 if (! COMPOSITION_ENCODE_RULE_VALID (rule - 81, b - 32)) \
3229 goto invalid_code; \
3230 rule = COMPOSITION_ENCODE_RULE (rule - 81, b - 32); \
3231 rule += 0x100; /* Distinguish it from the old format. */ \
3233 } while (0)
3235 #define ENCODE_COMPOSITION_RULE(rule) \
3236 do { \
3237 int gref = (rule % 0x100) / 12, nref = (rule % 0x100) % 12; \
3239 if (rule < 0x100) /* old format */ \
3241 if (gref == 10) gref = 4; \
3242 if (nref == 10) nref = 4; \
3243 charbuf[idx] = 32 + gref * 9 + nref; \
3244 charbuf[idx + 1] = -1; \
3245 new_chars++; \
3247 else /* new format */ \
3249 charbuf[idx] = 32 + 81 + gref; \
3250 charbuf[idx + 1] = 32 + nref; \
3251 new_chars += 2; \
3253 } while (0)
3255 /* Finish the current composition as invalid. */
3257 static int
3258 finish_composition (int *charbuf, struct composition_status *cmp_status)
3260 int idx = - cmp_status->length;
3261 int new_chars;
3263 /* Recover the original ESC sequence */
3264 charbuf[idx++] = ISO_CODE_ESC;
3265 charbuf[idx++] = (cmp_status->method == COMPOSITION_RELATIVE ? '0'
3266 : cmp_status->method == COMPOSITION_WITH_RULE ? '2'
3267 : cmp_status->method == COMPOSITION_WITH_ALTCHARS ? '3'
3268 /* cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS */
3269 : '4');
3270 charbuf[idx++] = -2;
3271 charbuf[idx++] = 0;
3272 charbuf[idx++] = -1;
3273 new_chars = cmp_status->nchars;
3274 if (cmp_status->method >= COMPOSITION_WITH_RULE)
3275 for (; idx < 0; idx++)
3277 int elt = charbuf[idx];
3279 if (elt == -2)
3281 ENCODE_COMPOSITION_RULE (charbuf[idx + 1]);
3282 idx++;
3284 else if (elt == -1)
3286 charbuf[idx++] = ISO_CODE_ESC;
3287 charbuf[idx] = '0';
3288 new_chars += 2;
3291 cmp_status->state = COMPOSING_NO;
3292 return new_chars;
3295 /* If characters are under composition, finish the composition. */
3296 #define MAYBE_FINISH_COMPOSITION() \
3297 do { \
3298 if (cmp_status->state != COMPOSING_NO) \
3299 char_offset += finish_composition (charbuf, cmp_status); \
3300 } while (0)
3302 /* Handle composition start sequence ESC 0, ESC 2, ESC 3, or ESC 4.
3304 ESC 0 : relative composition : ESC 0 CHAR ... ESC 1
3305 ESC 2 : rulebase composition : ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1
3306 ESC 3 : altchar composition : ESC 3 CHAR ... ESC 0 CHAR ... ESC 1
3307 ESC 4 : alt&rule composition : ESC 4 CHAR RULE ... CHAR ESC 0 CHAR ... ESC 1
3309 Produce this annotation sequence now:
3311 [ -LENGTH(==-4) CODING_ANNOTATE_COMPOSITION_MASK NCHARS(==0) METHOD ]
3314 #define DECODE_COMPOSITION_START(c1) \
3315 do { \
3316 if (c1 == '0' \
3317 && ((cmp_status->state == COMPOSING_COMPONENT_CHAR \
3318 && cmp_status->method == COMPOSITION_WITH_ALTCHARS) \
3319 || (cmp_status->state == COMPOSING_COMPONENT_RULE \
3320 && cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS))) \
3322 *charbuf++ = -1; \
3323 *charbuf++= -1; \
3324 cmp_status->state = COMPOSING_CHAR; \
3325 cmp_status->length += 2; \
3327 else \
3329 MAYBE_FINISH_COMPOSITION (); \
3330 cmp_status->method = (c1 == '0' ? COMPOSITION_RELATIVE \
3331 : c1 == '2' ? COMPOSITION_WITH_RULE \
3332 : c1 == '3' ? COMPOSITION_WITH_ALTCHARS \
3333 : COMPOSITION_WITH_RULE_ALTCHARS); \
3334 cmp_status->state \
3335 = (c1 <= '2' ? COMPOSING_CHAR : COMPOSING_COMPONENT_CHAR); \
3336 ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \
3337 cmp_status->length = MAX_ANNOTATION_LENGTH; \
3338 cmp_status->nchars = cmp_status->ncomps = 0; \
3339 coding->annotated = 1; \
3341 } while (0)
3344 /* Handle composition end sequence ESC 1. */
3346 #define DECODE_COMPOSITION_END() \
3347 do { \
3348 if (cmp_status->nchars == 0 \
3349 || ((cmp_status->state == COMPOSING_CHAR) \
3350 == (cmp_status->method == COMPOSITION_WITH_RULE))) \
3352 MAYBE_FINISH_COMPOSITION (); \
3353 goto invalid_code; \
3355 if (cmp_status->method == COMPOSITION_WITH_ALTCHARS) \
3356 charbuf[- cmp_status->length] -= cmp_status->ncomps + 2; \
3357 else if (cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS) \
3358 charbuf[- cmp_status->length] -= cmp_status->ncomps * 3; \
3359 charbuf[- cmp_status->length + 2] = cmp_status->nchars; \
3360 char_offset += cmp_status->nchars; \
3361 cmp_status->state = COMPOSING_NO; \
3362 } while (0)
3364 /* Store a composition rule RULE in charbuf, and update cmp_status. */
3366 #define STORE_COMPOSITION_RULE(rule) \
3367 do { \
3368 *charbuf++ = -2; \
3369 *charbuf++ = rule; \
3370 cmp_status->length += 2; \
3371 cmp_status->state--; \
3372 } while (0)
3374 /* Store a composed char or a component char C in charbuf, and update
3375 cmp_status. */
3377 #define STORE_COMPOSITION_CHAR(c) \
3378 do { \
3379 *charbuf++ = (c); \
3380 cmp_status->length++; \
3381 if (cmp_status->state == COMPOSING_CHAR) \
3382 cmp_status->nchars++; \
3383 else \
3384 cmp_status->ncomps++; \
3385 if (cmp_status->method == COMPOSITION_WITH_RULE \
3386 || (cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS \
3387 && cmp_status->state == COMPOSING_COMPONENT_CHAR)) \
3388 cmp_status->state++; \
3389 } while (0)
3392 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
3394 static void
3395 decode_coding_iso_2022 (struct coding_system *coding)
3397 const unsigned char *src = coding->source + coding->consumed;
3398 const unsigned char *src_end = coding->source + coding->src_bytes;
3399 const unsigned char *src_base;
3400 int *charbuf = coding->charbuf + coding->charbuf_used;
3401 /* We may produce two annotations (charset and composition) in one
3402 loop and one more charset annotation at the end. */
3403 int *charbuf_end
3404 = coding->charbuf + coding->charbuf_size - (MAX_ANNOTATION_LENGTH * 3);
3405 ptrdiff_t consumed_chars = 0, consumed_chars_base;
3406 bool multibytep = coding->src_multibyte;
3407 /* Charsets invoked to graphic plane 0 and 1 respectively. */
3408 int charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0);
3409 int charset_id_1 = CODING_ISO_INVOKED_CHARSET (coding, 1);
3410 int charset_id_2, charset_id_3;
3411 struct charset *charset;
3412 int c;
3413 struct composition_status *cmp_status = CODING_ISO_CMP_STATUS (coding);
3414 Lisp_Object attrs = CODING_ID_ATTRS (coding->id);
3415 ptrdiff_t char_offset = coding->produced_char;
3416 ptrdiff_t last_offset = char_offset;
3417 int last_id = charset_ascii;
3418 bool eol_dos
3419 = !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos);
3420 int byte_after_cr = -1;
3421 int i;
3423 setup_iso_safe_charsets (attrs);
3424 coding->safe_charsets = SDATA (CODING_ATTR_SAFE_CHARSETS (attrs));
3426 if (cmp_status->state != COMPOSING_NO)
3428 if (charbuf_end - charbuf < cmp_status->length)
3429 emacs_abort ();
3430 for (i = 0; i < cmp_status->length; i++)
3431 *charbuf++ = cmp_status->carryover[i];
3432 coding->annotated = 1;
3435 while (1)
3437 int c1, c2, c3;
3439 src_base = src;
3440 consumed_chars_base = consumed_chars;
3442 if (charbuf >= charbuf_end)
3444 if (byte_after_cr >= 0)
3445 src_base--;
3446 break;
3449 if (byte_after_cr >= 0)
3450 c1 = byte_after_cr, byte_after_cr = -1;
3451 else
3452 ONE_MORE_BYTE (c1);
3453 if (c1 < 0)
3454 goto invalid_code;
3456 if (CODING_ISO_EXTSEGMENT_LEN (coding) > 0)
3458 *charbuf++ = ASCII_BYTE_P (c1) ? c1 : BYTE8_TO_CHAR (c1);
3459 char_offset++;
3460 CODING_ISO_EXTSEGMENT_LEN (coding)--;
3461 continue;
3464 if (CODING_ISO_EMBEDDED_UTF_8 (coding))
3466 if (c1 == ISO_CODE_ESC)
3468 if (src + 1 >= src_end)
3469 goto no_more_source;
3470 *charbuf++ = ISO_CODE_ESC;
3471 char_offset++;
3472 if (src[0] == '%' && src[1] == '@')
3474 src += 2;
3475 consumed_chars += 2;
3476 char_offset += 2;
3477 /* We are sure charbuf can contain two more chars. */
3478 *charbuf++ = '%';
3479 *charbuf++ = '@';
3480 CODING_ISO_EMBEDDED_UTF_8 (coding) = 0;
3483 else
3485 *charbuf++ = ASCII_BYTE_P (c1) ? c1 : BYTE8_TO_CHAR (c1);
3486 char_offset++;
3488 continue;
3491 if ((cmp_status->state == COMPOSING_RULE
3492 || cmp_status->state == COMPOSING_COMPONENT_RULE)
3493 && c1 != ISO_CODE_ESC)
3495 int rule;
3497 DECODE_COMPOSITION_RULE (rule);
3498 STORE_COMPOSITION_RULE (rule);
3499 continue;
3502 /* We produce at most one character. */
3503 switch (iso_code_class [c1])
3505 case ISO_0x20_or_0x7F:
3506 if (charset_id_0 < 0
3507 || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_0)))
3508 /* This is SPACE or DEL. */
3509 charset = CHARSET_FROM_ID (charset_ascii);
3510 else
3511 charset = CHARSET_FROM_ID (charset_id_0);
3512 break;
3514 case ISO_graphic_plane_0:
3515 if (charset_id_0 < 0)
3516 charset = CHARSET_FROM_ID (charset_ascii);
3517 else
3518 charset = CHARSET_FROM_ID (charset_id_0);
3519 break;
3521 case ISO_0xA0_or_0xFF:
3522 if (charset_id_1 < 0
3523 || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_1))
3524 || CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS)
3525 goto invalid_code;
3526 /* This is a graphic character, we fall down ... */
3528 case ISO_graphic_plane_1:
3529 if (charset_id_1 < 0)
3530 goto invalid_code;
3531 charset = CHARSET_FROM_ID (charset_id_1);
3532 break;
3534 case ISO_control_0:
3535 if (eol_dos && c1 == '\r')
3536 ONE_MORE_BYTE (byte_after_cr);
3537 MAYBE_FINISH_COMPOSITION ();
3538 charset = CHARSET_FROM_ID (charset_ascii);
3539 break;
3541 case ISO_control_1:
3542 goto invalid_code;
3544 case ISO_shift_out:
3545 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LOCKING_SHIFT)
3546 || CODING_ISO_DESIGNATION (coding, 1) < 0)
3547 goto invalid_code;
3548 CODING_ISO_INVOCATION (coding, 0) = 1;
3549 charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0);
3550 continue;
3552 case ISO_shift_in:
3553 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LOCKING_SHIFT))
3554 goto invalid_code;
3555 CODING_ISO_INVOCATION (coding, 0) = 0;
3556 charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0);
3557 continue;
3559 case ISO_single_shift_2_7:
3560 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS))
3561 goto invalid_code;
3562 case ISO_single_shift_2:
3563 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT))
3564 goto invalid_code;
3565 /* SS2 is handled as an escape sequence of ESC 'N' */
3566 c1 = 'N';
3567 goto label_escape_sequence;
3569 case ISO_single_shift_3:
3570 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT))
3571 goto invalid_code;
3572 /* SS2 is handled as an escape sequence of ESC 'O' */
3573 c1 = 'O';
3574 goto label_escape_sequence;
3576 case ISO_control_sequence_introducer:
3577 /* CSI is handled as an escape sequence of ESC '[' ... */
3578 c1 = '[';
3579 goto label_escape_sequence;
3581 case ISO_escape:
3582 ONE_MORE_BYTE (c1);
3583 label_escape_sequence:
3584 /* Escape sequences handled here are invocation,
3585 designation, direction specification, and character
3586 composition specification. */
3587 switch (c1)
3589 case '&': /* revision of following character set */
3590 ONE_MORE_BYTE (c1);
3591 if (!(c1 >= '@' && c1 <= '~'))
3592 goto invalid_code;
3593 ONE_MORE_BYTE (c1);
3594 if (c1 != ISO_CODE_ESC)
3595 goto invalid_code;
3596 ONE_MORE_BYTE (c1);
3597 goto label_escape_sequence;
3599 case '$': /* designation of 2-byte character set */
3600 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DESIGNATION))
3601 goto invalid_code;
3603 int reg, chars96;
3605 ONE_MORE_BYTE (c1);
3606 if (c1 >= '@' && c1 <= 'B')
3607 { /* designation of JISX0208.1978, GB2312.1980,
3608 or JISX0208.1980 */
3609 reg = 0, chars96 = 0;
3611 else if (c1 >= 0x28 && c1 <= 0x2B)
3612 { /* designation of DIMENSION2_CHARS94 character set */
3613 reg = c1 - 0x28, chars96 = 0;
3614 ONE_MORE_BYTE (c1);
3616 else if (c1 >= 0x2C && c1 <= 0x2F)
3617 { /* designation of DIMENSION2_CHARS96 character set */
3618 reg = c1 - 0x2C, chars96 = 1;
3619 ONE_MORE_BYTE (c1);
3621 else
3622 goto invalid_code;
3623 DECODE_DESIGNATION (reg, 2, chars96, c1);
3624 /* We must update these variables now. */
3625 if (reg == 0)
3626 charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0);
3627 else if (reg == 1)
3628 charset_id_1 = CODING_ISO_INVOKED_CHARSET (coding, 1);
3629 if (chars96 < 0)
3630 goto invalid_code;
3632 continue;
3634 case 'n': /* invocation of locking-shift-2 */
3635 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LOCKING_SHIFT)
3636 || CODING_ISO_DESIGNATION (coding, 2) < 0)
3637 goto invalid_code;
3638 CODING_ISO_INVOCATION (coding, 0) = 2;
3639 charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0);
3640 continue;
3642 case 'o': /* invocation of locking-shift-3 */
3643 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LOCKING_SHIFT)
3644 || CODING_ISO_DESIGNATION (coding, 3) < 0)
3645 goto invalid_code;
3646 CODING_ISO_INVOCATION (coding, 0) = 3;
3647 charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0);
3648 continue;
3650 case 'N': /* invocation of single-shift-2 */
3651 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT)
3652 || CODING_ISO_DESIGNATION (coding, 2) < 0)
3653 goto invalid_code;
3654 charset_id_2 = CODING_ISO_DESIGNATION (coding, 2);
3655 if (charset_id_2 < 0)
3656 charset = CHARSET_FROM_ID (charset_ascii);
3657 else
3658 charset = CHARSET_FROM_ID (charset_id_2);
3659 ONE_MORE_BYTE (c1);
3660 if (c1 < 0x20 || (c1 >= 0x80 && c1 < 0xA0))
3661 goto invalid_code;
3662 break;
3664 case 'O': /* invocation of single-shift-3 */
3665 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT)
3666 || CODING_ISO_DESIGNATION (coding, 3) < 0)
3667 goto invalid_code;
3668 charset_id_3 = CODING_ISO_DESIGNATION (coding, 3);
3669 if (charset_id_3 < 0)
3670 charset = CHARSET_FROM_ID (charset_ascii);
3671 else
3672 charset = CHARSET_FROM_ID (charset_id_3);
3673 ONE_MORE_BYTE (c1);
3674 if (c1 < 0x20 || (c1 >= 0x80 && c1 < 0xA0))
3675 goto invalid_code;
3676 break;
3678 case '0': case '2': case '3': case '4': /* start composition */
3679 if (! (coding->common_flags & CODING_ANNOTATE_COMPOSITION_MASK))
3680 goto invalid_code;
3681 if (last_id != charset_ascii)
3683 ADD_CHARSET_DATA (charbuf, char_offset- last_offset, last_id);
3684 last_id = charset_ascii;
3685 last_offset = char_offset;
3687 DECODE_COMPOSITION_START (c1);
3688 continue;
3690 case '1': /* end composition */
3691 if (cmp_status->state == COMPOSING_NO)
3692 goto invalid_code;
3693 DECODE_COMPOSITION_END ();
3694 continue;
3696 case '[': /* specification of direction */
3697 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DIRECTION))
3698 goto invalid_code;
3699 /* For the moment, nested direction is not supported.
3700 So, `coding->mode & CODING_MODE_DIRECTION' zero means
3701 left-to-right, and nonzero means right-to-left. */
3702 ONE_MORE_BYTE (c1);
3703 switch (c1)
3705 case ']': /* end of the current direction */
3706 coding->mode &= ~CODING_MODE_DIRECTION;
3708 case '0': /* end of the current direction */
3709 case '1': /* start of left-to-right direction */
3710 ONE_MORE_BYTE (c1);
3711 if (c1 == ']')
3712 coding->mode &= ~CODING_MODE_DIRECTION;
3713 else
3714 goto invalid_code;
3715 break;
3717 case '2': /* start of right-to-left direction */
3718 ONE_MORE_BYTE (c1);
3719 if (c1 == ']')
3720 coding->mode |= CODING_MODE_DIRECTION;
3721 else
3722 goto invalid_code;
3723 break;
3725 default:
3726 goto invalid_code;
3728 continue;
3730 case '%':
3731 ONE_MORE_BYTE (c1);
3732 if (c1 == '/')
3734 /* CTEXT extended segment:
3735 ESC % / [0-4] M L --ENCODING-NAME-- \002 --BYTES--
3736 We keep these bytes as is for the moment.
3737 They may be decoded by post-read-conversion. */
3738 int dim, M, L;
3739 int size;
3741 ONE_MORE_BYTE (dim);
3742 if (dim < '0' || dim > '4')
3743 goto invalid_code;
3744 ONE_MORE_BYTE (M);
3745 if (M < 128)
3746 goto invalid_code;
3747 ONE_MORE_BYTE (L);
3748 if (L < 128)
3749 goto invalid_code;
3750 size = ((M - 128) * 128) + (L - 128);
3751 if (charbuf + 6 > charbuf_end)
3752 goto break_loop;
3753 *charbuf++ = ISO_CODE_ESC;
3754 *charbuf++ = '%';
3755 *charbuf++ = '/';
3756 *charbuf++ = dim;
3757 *charbuf++ = BYTE8_TO_CHAR (M);
3758 *charbuf++ = BYTE8_TO_CHAR (L);
3759 CODING_ISO_EXTSEGMENT_LEN (coding) = size;
3761 else if (c1 == 'G')
3763 /* XFree86 extension for embedding UTF-8 in CTEXT:
3764 ESC % G --UTF-8-BYTES-- ESC % @
3765 We keep these bytes as is for the moment.
3766 They may be decoded by post-read-conversion. */
3767 if (charbuf + 3 > charbuf_end)
3768 goto break_loop;
3769 *charbuf++ = ISO_CODE_ESC;
3770 *charbuf++ = '%';
3771 *charbuf++ = 'G';
3772 CODING_ISO_EMBEDDED_UTF_8 (coding) = 1;
3774 else
3775 goto invalid_code;
3776 continue;
3777 break;
3779 default:
3780 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DESIGNATION))
3781 goto invalid_code;
3783 int reg, chars96;
3785 if (c1 >= 0x28 && c1 <= 0x2B)
3786 { /* designation of DIMENSION1_CHARS94 character set */
3787 reg = c1 - 0x28, chars96 = 0;
3788 ONE_MORE_BYTE (c1);
3790 else if (c1 >= 0x2C && c1 <= 0x2F)
3791 { /* designation of DIMENSION1_CHARS96 character set */
3792 reg = c1 - 0x2C, chars96 = 1;
3793 ONE_MORE_BYTE (c1);
3795 else
3796 goto invalid_code;
3797 DECODE_DESIGNATION (reg, 1, chars96, c1);
3798 /* We must update these variables now. */
3799 if (reg == 0)
3800 charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0);
3801 else if (reg == 1)
3802 charset_id_1 = CODING_ISO_INVOKED_CHARSET (coding, 1);
3803 if (chars96 < 0)
3804 goto invalid_code;
3806 continue;
3808 break;
3810 default:
3811 emacs_abort ();
3814 if (cmp_status->state == COMPOSING_NO
3815 && charset->id != charset_ascii
3816 && last_id != charset->id)
3818 if (last_id != charset_ascii)
3819 ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id);
3820 last_id = charset->id;
3821 last_offset = char_offset;
3824 /* Now we know CHARSET and 1st position code C1 of a character.
3825 Produce a decoded character while getting 2nd and 3rd
3826 position codes C2, C3 if necessary. */
3827 if (CHARSET_DIMENSION (charset) > 1)
3829 ONE_MORE_BYTE (c2);
3830 if (c2 < 0x20 || (c2 >= 0x80 && c2 < 0xA0)
3831 || ((c1 & 0x80) != (c2 & 0x80)))
3832 /* C2 is not in a valid range. */
3833 goto invalid_code;
3834 if (CHARSET_DIMENSION (charset) == 2)
3835 c1 = (c1 << 8) | c2;
3836 else
3838 ONE_MORE_BYTE (c3);
3839 if (c3 < 0x20 || (c3 >= 0x80 && c3 < 0xA0)
3840 || ((c1 & 0x80) != (c3 & 0x80)))
3841 /* C3 is not in a valid range. */
3842 goto invalid_code;
3843 c1 = (c1 << 16) | (c2 << 8) | c2;
3846 c1 &= 0x7F7F7F;
3847 CODING_DECODE_CHAR (coding, src, src_base, src_end, charset, c1, c);
3848 if (c < 0)
3850 MAYBE_FINISH_COMPOSITION ();
3851 for (; src_base < src; src_base++, char_offset++)
3853 if (ASCII_BYTE_P (*src_base))
3854 *charbuf++ = *src_base;
3855 else
3856 *charbuf++ = BYTE8_TO_CHAR (*src_base);
3859 else if (cmp_status->state == COMPOSING_NO)
3861 *charbuf++ = c;
3862 char_offset++;
3864 else if ((cmp_status->state == COMPOSING_CHAR
3865 ? cmp_status->nchars
3866 : cmp_status->ncomps)
3867 >= MAX_COMPOSITION_COMPONENTS)
3869 /* Too long composition. */
3870 MAYBE_FINISH_COMPOSITION ();
3871 *charbuf++ = c;
3872 char_offset++;
3874 else
3875 STORE_COMPOSITION_CHAR (c);
3876 continue;
3878 invalid_code:
3879 MAYBE_FINISH_COMPOSITION ();
3880 src = src_base;
3881 consumed_chars = consumed_chars_base;
3882 ONE_MORE_BYTE (c);
3883 *charbuf++ = c < 0 ? -c : ASCII_BYTE_P (c) ? c : BYTE8_TO_CHAR (c);
3884 char_offset++;
3885 coding->errors++;
3886 continue;
3888 break_loop:
3889 break;
3892 no_more_source:
3893 if (cmp_status->state != COMPOSING_NO)
3895 if (coding->mode & CODING_MODE_LAST_BLOCK)
3896 MAYBE_FINISH_COMPOSITION ();
3897 else
3899 charbuf -= cmp_status->length;
3900 for (i = 0; i < cmp_status->length; i++)
3901 cmp_status->carryover[i] = charbuf[i];
3904 else if (last_id != charset_ascii)
3905 ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id);
3906 coding->consumed_char += consumed_chars_base;
3907 coding->consumed = src_base - coding->source;
3908 coding->charbuf_used = charbuf - coding->charbuf;
3912 /* ISO2022 encoding stuff. */
3915 It is not enough to say just "ISO2022" on encoding, we have to
3916 specify more details. In Emacs, each coding system of ISO2022
3917 variant has the following specifications:
3918 1. Initial designation to G0 thru G3.
3919 2. Allows short-form designation?
3920 3. ASCII should be designated to G0 before control characters?
3921 4. ASCII should be designated to G0 at end of line?
3922 5. 7-bit environment or 8-bit environment?
3923 6. Use locking-shift?
3924 7. Use Single-shift?
3925 And the following two are only for Japanese:
3926 8. Use ASCII in place of JIS0201-1976-Roman?
3927 9. Use JISX0208-1983 in place of JISX0208-1978?
3928 These specifications are encoded in CODING_ISO_FLAGS (coding) as flag bits
3929 defined by macros CODING_ISO_FLAG_XXX. See `coding.h' for more
3930 details.
3933 /* Produce codes (escape sequence) for designating CHARSET to graphic
3934 register REG at DST, and increment DST. If <final-char> of CHARSET is
3935 '@', 'A', or 'B' and the coding system CODING allows, produce
3936 designation sequence of short-form. */
3938 #define ENCODE_DESIGNATION(charset, reg, coding) \
3939 do { \
3940 unsigned char final_char = CHARSET_ISO_FINAL (charset); \
3941 const char *intermediate_char_94 = "()*+"; \
3942 const char *intermediate_char_96 = ",-./"; \
3943 int revision = -1; \
3945 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_REVISION) \
3946 revision = CHARSET_ISO_REVISION (charset); \
3948 if (revision >= 0) \
3950 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, '&'); \
3951 EMIT_ONE_BYTE ('@' + revision); \
3953 EMIT_ONE_ASCII_BYTE (ISO_CODE_ESC); \
3954 if (CHARSET_DIMENSION (charset) == 1) \
3956 int b; \
3957 if (! CHARSET_ISO_CHARS_96 (charset)) \
3958 b = intermediate_char_94[reg]; \
3959 else \
3960 b = intermediate_char_96[reg]; \
3961 EMIT_ONE_ASCII_BYTE (b); \
3963 else \
3965 EMIT_ONE_ASCII_BYTE ('$'); \
3966 if (! CHARSET_ISO_CHARS_96 (charset)) \
3968 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LONG_FORM \
3969 || reg != 0 \
3970 || final_char < '@' || final_char > 'B') \
3971 EMIT_ONE_ASCII_BYTE (intermediate_char_94[reg]); \
3973 else \
3974 EMIT_ONE_ASCII_BYTE (intermediate_char_96[reg]); \
3976 EMIT_ONE_ASCII_BYTE (final_char); \
3978 CODING_ISO_DESIGNATION (coding, reg) = CHARSET_ID (charset); \
3979 } while (0)
3982 /* The following two macros produce codes (control character or escape
3983 sequence) for ISO2022 single-shift functions (single-shift-2 and
3984 single-shift-3). */
3986 #define ENCODE_SINGLE_SHIFT_2 \
3987 do { \
3988 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
3989 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'N'); \
3990 else \
3991 EMIT_ONE_BYTE (ISO_CODE_SS2); \
3992 CODING_ISO_SINGLE_SHIFTING (coding) = 1; \
3993 } while (0)
3996 #define ENCODE_SINGLE_SHIFT_3 \
3997 do { \
3998 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
3999 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'O'); \
4000 else \
4001 EMIT_ONE_BYTE (ISO_CODE_SS3); \
4002 CODING_ISO_SINGLE_SHIFTING (coding) = 1; \
4003 } while (0)
4006 /* The following four macros produce codes (control character or
4007 escape sequence) for ISO2022 locking-shift functions (shift-in,
4008 shift-out, locking-shift-2, and locking-shift-3). */
4010 #define ENCODE_SHIFT_IN \
4011 do { \
4012 EMIT_ONE_ASCII_BYTE (ISO_CODE_SI); \
4013 CODING_ISO_INVOCATION (coding, 0) = 0; \
4014 } while (0)
4017 #define ENCODE_SHIFT_OUT \
4018 do { \
4019 EMIT_ONE_ASCII_BYTE (ISO_CODE_SO); \
4020 CODING_ISO_INVOCATION (coding, 0) = 1; \
4021 } while (0)
4024 #define ENCODE_LOCKING_SHIFT_2 \
4025 do { \
4026 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \
4027 CODING_ISO_INVOCATION (coding, 0) = 2; \
4028 } while (0)
4031 #define ENCODE_LOCKING_SHIFT_3 \
4032 do { \
4033 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \
4034 CODING_ISO_INVOCATION (coding, 0) = 3; \
4035 } while (0)
4038 /* Produce codes for a DIMENSION1 character whose character set is
4039 CHARSET and whose position-code is C1. Designation and invocation
4040 sequences are also produced in advance if necessary. */
4042 #define ENCODE_ISO_CHARACTER_DIMENSION1(charset, c1) \
4043 do { \
4044 int id = CHARSET_ID (charset); \
4046 if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \
4047 && id == charset_ascii) \
4049 id = charset_jisx0201_roman; \
4050 charset = CHARSET_FROM_ID (id); \
4053 if (CODING_ISO_SINGLE_SHIFTING (coding)) \
4055 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
4056 EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \
4057 else \
4058 EMIT_ONE_BYTE (c1 | 0x80); \
4059 CODING_ISO_SINGLE_SHIFTING (coding) = 0; \
4060 break; \
4062 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \
4064 EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \
4065 break; \
4067 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \
4069 EMIT_ONE_BYTE (c1 | 0x80); \
4070 break; \
4072 else \
4073 /* Since CHARSET is not yet invoked to any graphic planes, we \
4074 must invoke it, or, at first, designate it to some graphic \
4075 register. Then repeat the loop to actually produce the \
4076 character. */ \
4077 dst = encode_invocation_designation (charset, coding, dst, \
4078 &produced_chars); \
4079 } while (1)
4082 /* Produce codes for a DIMENSION2 character whose character set is
4083 CHARSET and whose position-codes are C1 and C2. Designation and
4084 invocation codes are also produced in advance if necessary. */
4086 #define ENCODE_ISO_CHARACTER_DIMENSION2(charset, c1, c2) \
4087 do { \
4088 int id = CHARSET_ID (charset); \
4090 if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \
4091 && id == charset_jisx0208) \
4093 id = charset_jisx0208_1978; \
4094 charset = CHARSET_FROM_ID (id); \
4097 if (CODING_ISO_SINGLE_SHIFTING (coding)) \
4099 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
4100 EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \
4101 else \
4102 EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \
4103 CODING_ISO_SINGLE_SHIFTING (coding) = 0; \
4104 break; \
4106 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \
4108 EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \
4109 break; \
4111 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \
4113 EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \
4114 break; \
4116 else \
4117 /* Since CHARSET is not yet invoked to any graphic planes, we \
4118 must invoke it, or, at first, designate it to some graphic \
4119 register. Then repeat the loop to actually produce the \
4120 character. */ \
4121 dst = encode_invocation_designation (charset, coding, dst, \
4122 &produced_chars); \
4123 } while (1)
4126 #define ENCODE_ISO_CHARACTER(charset, c) \
4127 do { \
4128 unsigned code; \
4129 CODING_ENCODE_CHAR (coding, dst, dst_end, (charset), (c), code); \
4131 if (CHARSET_DIMENSION (charset) == 1) \
4132 ENCODE_ISO_CHARACTER_DIMENSION1 ((charset), code); \
4133 else \
4134 ENCODE_ISO_CHARACTER_DIMENSION2 ((charset), code >> 8, code & 0xFF); \
4135 } while (0)
4138 /* Produce designation and invocation codes at a place pointed by DST
4139 to use CHARSET. The element `spec.iso_2022' of *CODING is updated.
4140 Return new DST. */
4142 static unsigned char *
4143 encode_invocation_designation (struct charset *charset,
4144 struct coding_system *coding,
4145 unsigned char *dst, ptrdiff_t *p_nchars)
4147 bool multibytep = coding->dst_multibyte;
4148 ptrdiff_t produced_chars = *p_nchars;
4149 int reg; /* graphic register number */
4150 int id = CHARSET_ID (charset);
4152 /* At first, check designations. */
4153 for (reg = 0; reg < 4; reg++)
4154 if (id == CODING_ISO_DESIGNATION (coding, reg))
4155 break;
4157 if (reg >= 4)
4159 /* CHARSET is not yet designated to any graphic registers. */
4160 /* At first check the requested designation. */
4161 reg = CODING_ISO_REQUEST (coding, id);
4162 if (reg < 0)
4163 /* Since CHARSET requests no special designation, designate it
4164 to graphic register 0. */
4165 reg = 0;
4167 ENCODE_DESIGNATION (charset, reg, coding);
4170 if (CODING_ISO_INVOCATION (coding, 0) != reg
4171 && CODING_ISO_INVOCATION (coding, 1) != reg)
4173 /* Since the graphic register REG is not invoked to any graphic
4174 planes, invoke it to graphic plane 0. */
4175 switch (reg)
4177 case 0: /* graphic register 0 */
4178 ENCODE_SHIFT_IN;
4179 break;
4181 case 1: /* graphic register 1 */
4182 ENCODE_SHIFT_OUT;
4183 break;
4185 case 2: /* graphic register 2 */
4186 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT)
4187 ENCODE_SINGLE_SHIFT_2;
4188 else
4189 ENCODE_LOCKING_SHIFT_2;
4190 break;
4192 case 3: /* graphic register 3 */
4193 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT)
4194 ENCODE_SINGLE_SHIFT_3;
4195 else
4196 ENCODE_LOCKING_SHIFT_3;
4197 break;
4201 *p_nchars = produced_chars;
4202 return dst;
4206 /* Produce codes for designation and invocation to reset the graphic
4207 planes and registers to initial state. */
4208 #define ENCODE_RESET_PLANE_AND_REGISTER() \
4209 do { \
4210 int reg; \
4211 struct charset *charset; \
4213 if (CODING_ISO_INVOCATION (coding, 0) != 0) \
4214 ENCODE_SHIFT_IN; \
4215 for (reg = 0; reg < 4; reg++) \
4216 if (CODING_ISO_INITIAL (coding, reg) >= 0 \
4217 && (CODING_ISO_DESIGNATION (coding, reg) \
4218 != CODING_ISO_INITIAL (coding, reg))) \
4220 charset = CHARSET_FROM_ID (CODING_ISO_INITIAL (coding, reg)); \
4221 ENCODE_DESIGNATION (charset, reg, coding); \
4223 } while (0)
4226 /* Produce designation sequences of charsets in the line started from
4227 CHARBUF to a place pointed by DST, and return the number of
4228 produced bytes. DST should not directly point a buffer text area
4229 which may be relocated by char_charset call.
4231 If the current block ends before any end-of-line, we may fail to
4232 find all the necessary designations. */
4234 static ptrdiff_t
4235 encode_designation_at_bol (struct coding_system *coding,
4236 int *charbuf, int *charbuf_end,
4237 unsigned char *dst)
4239 unsigned char *orig = dst;
4240 struct charset *charset;
4241 /* Table of charsets to be designated to each graphic register. */
4242 int r[4];
4243 int c, found = 0, reg;
4244 ptrdiff_t produced_chars = 0;
4245 bool multibytep = coding->dst_multibyte;
4246 Lisp_Object attrs;
4247 Lisp_Object charset_list;
4249 attrs = CODING_ID_ATTRS (coding->id);
4250 charset_list = CODING_ATTR_CHARSET_LIST (attrs);
4251 if (EQ (charset_list, Qiso_2022))
4252 charset_list = Viso_2022_charset_list;
4254 for (reg = 0; reg < 4; reg++)
4255 r[reg] = -1;
4257 while (charbuf < charbuf_end && found < 4)
4259 int id;
4261 c = *charbuf++;
4262 if (c == '\n')
4263 break;
4264 charset = char_charset (c, charset_list, NULL);
4265 id = CHARSET_ID (charset);
4266 reg = CODING_ISO_REQUEST (coding, id);
4267 if (reg >= 0 && r[reg] < 0)
4269 found++;
4270 r[reg] = id;
4274 if (found)
4276 for (reg = 0; reg < 4; reg++)
4277 if (r[reg] >= 0
4278 && CODING_ISO_DESIGNATION (coding, reg) != r[reg])
4279 ENCODE_DESIGNATION (CHARSET_FROM_ID (r[reg]), reg, coding);
4282 return dst - orig;
4285 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". */
4287 static bool
4288 encode_coding_iso_2022 (struct coding_system *coding)
4290 bool multibytep = coding->dst_multibyte;
4291 int *charbuf = coding->charbuf;
4292 int *charbuf_end = charbuf + coding->charbuf_used;
4293 unsigned char *dst = coding->destination + coding->produced;
4294 unsigned char *dst_end = coding->destination + coding->dst_bytes;
4295 int safe_room = 16;
4296 bool bol_designation
4297 = (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DESIGNATE_AT_BOL
4298 && CODING_ISO_BOL (coding));
4299 ptrdiff_t produced_chars = 0;
4300 Lisp_Object attrs, eol_type, charset_list;
4301 bool ascii_compatible;
4302 int c;
4303 int preferred_charset_id = -1;
4305 CODING_GET_INFO (coding, attrs, charset_list);
4306 eol_type = inhibit_eol_conversion ? Qunix : CODING_ID_EOL_TYPE (coding->id);
4307 if (VECTORP (eol_type))
4308 eol_type = Qunix;
4310 setup_iso_safe_charsets (attrs);
4311 /* Charset list may have been changed. */
4312 charset_list = CODING_ATTR_CHARSET_LIST (attrs);
4313 coding->safe_charsets = SDATA (CODING_ATTR_SAFE_CHARSETS (attrs));
4315 ascii_compatible
4316 = (! NILP (CODING_ATTR_ASCII_COMPAT (attrs))
4317 && ! (CODING_ISO_FLAGS (coding) & (CODING_ISO_FLAG_DESIGNATION
4318 | CODING_ISO_FLAG_LOCKING_SHIFT)));
4320 while (charbuf < charbuf_end)
4322 ASSURE_DESTINATION (safe_room);
4324 if (bol_designation)
4326 /* We have to produce designation sequences if any now. */
4327 unsigned char desig_buf[16];
4328 int nbytes;
4329 ptrdiff_t offset;
4331 charset_map_loaded = 0;
4332 nbytes = encode_designation_at_bol (coding, charbuf, charbuf_end,
4333 desig_buf);
4334 if (charset_map_loaded
4335 && (offset = coding_change_destination (coding)))
4337 dst += offset;
4338 dst_end += offset;
4340 memcpy (dst, desig_buf, nbytes);
4341 dst += nbytes;
4342 /* We are sure that designation sequences are all ASCII bytes. */
4343 produced_chars += nbytes;
4344 bol_designation = 0;
4345 ASSURE_DESTINATION (safe_room);
4348 c = *charbuf++;
4350 if (c < 0)
4352 /* Handle an annotation. */
4353 switch (*charbuf)
4355 case CODING_ANNOTATE_COMPOSITION_MASK:
4356 /* Not yet implemented. */
4357 break;
4358 case CODING_ANNOTATE_CHARSET_MASK:
4359 preferred_charset_id = charbuf[2];
4360 if (preferred_charset_id >= 0
4361 && NILP (Fmemq (make_number (preferred_charset_id),
4362 charset_list)))
4363 preferred_charset_id = -1;
4364 break;
4365 default:
4366 emacs_abort ();
4368 charbuf += -c - 1;
4369 continue;
4372 /* Now encode the character C. */
4373 if (c < 0x20 || c == 0x7F)
4375 if (c == '\n'
4376 || (c == '\r' && EQ (eol_type, Qmac)))
4378 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_RESET_AT_EOL)
4379 ENCODE_RESET_PLANE_AND_REGISTER ();
4380 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_INIT_AT_BOL)
4382 int i;
4384 for (i = 0; i < 4; i++)
4385 CODING_ISO_DESIGNATION (coding, i)
4386 = CODING_ISO_INITIAL (coding, i);
4388 bol_designation = ((CODING_ISO_FLAGS (coding)
4389 & CODING_ISO_FLAG_DESIGNATE_AT_BOL)
4390 != 0);
4392 else if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_RESET_AT_CNTL)
4393 ENCODE_RESET_PLANE_AND_REGISTER ();
4394 EMIT_ONE_ASCII_BYTE (c);
4396 else if (ASCII_CHAR_P (c))
4398 if (ascii_compatible)
4399 EMIT_ONE_ASCII_BYTE (c);
4400 else
4402 struct charset *charset = CHARSET_FROM_ID (charset_ascii);
4403 ENCODE_ISO_CHARACTER (charset, c);
4406 else if (CHAR_BYTE8_P (c))
4408 c = CHAR_TO_BYTE8 (c);
4409 EMIT_ONE_BYTE (c);
4411 else
4413 struct charset *charset;
4415 if (preferred_charset_id >= 0)
4417 bool result;
4419 charset = CHARSET_FROM_ID (preferred_charset_id);
4420 CODING_CHAR_CHARSET_P (coding, dst, dst_end, c, charset, result);
4421 if (! result)
4422 CODING_CHAR_CHARSET (coding, dst, dst_end, c, charset_list,
4423 NULL, charset);
4425 else
4426 CODING_CHAR_CHARSET (coding, dst, dst_end, c, charset_list,
4427 NULL, charset);
4428 if (!charset)
4430 if (coding->mode & CODING_MODE_SAFE_ENCODING)
4432 c = CODING_INHIBIT_CHARACTER_SUBSTITUTION;
4433 charset = CHARSET_FROM_ID (charset_ascii);
4435 else
4437 c = coding->default_char;
4438 CODING_CHAR_CHARSET (coding, dst, dst_end, c,
4439 charset_list, NULL, charset);
4442 ENCODE_ISO_CHARACTER (charset, c);
4446 if (coding->mode & CODING_MODE_LAST_BLOCK
4447 && CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_RESET_AT_EOL)
4449 ASSURE_DESTINATION (safe_room);
4450 ENCODE_RESET_PLANE_AND_REGISTER ();
4452 record_conversion_result (coding, CODING_RESULT_SUCCESS);
4453 CODING_ISO_BOL (coding) = bol_designation;
4454 coding->produced_char += produced_chars;
4455 coding->produced = dst - coding->destination;
4456 return 0;
4460 /*** 8,9. SJIS and BIG5 handlers ***/
4462 /* Although SJIS and BIG5 are not ISO's coding system, they are used
4463 quite widely. So, for the moment, Emacs supports them in the bare
4464 C code. But, in the future, they may be supported only by CCL. */
4466 /* SJIS is a coding system encoding three character sets: ASCII, right
4467 half of JISX0201-Kana, and JISX0208. An ASCII character is encoded
4468 as is. A character of charset katakana-jisx0201 is encoded by
4469 "position-code + 0x80". A character of charset japanese-jisx0208
4470 is encoded in 2-byte but two position-codes are divided and shifted
4471 so that it fit in the range below.
4473 --- CODE RANGE of SJIS ---
4474 (character set) (range)
4475 ASCII 0x00 .. 0x7F
4476 KATAKANA-JISX0201 0xA0 .. 0xDF
4477 JISX0208 (1st byte) 0x81 .. 0x9F and 0xE0 .. 0xEF
4478 (2nd byte) 0x40 .. 0x7E and 0x80 .. 0xFC
4479 -------------------------------
4483 /* BIG5 is a coding system encoding two character sets: ASCII and
4484 Big5. An ASCII character is encoded as is. Big5 is a two-byte
4485 character set and is encoded in two-byte.
4487 --- CODE RANGE of BIG5 ---
4488 (character set) (range)
4489 ASCII 0x00 .. 0x7F
4490 Big5 (1st byte) 0xA1 .. 0xFE
4491 (2nd byte) 0x40 .. 0x7E and 0xA1 .. 0xFE
4492 --------------------------
4496 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4497 Return true if a text is encoded in SJIS. */
4499 static bool
4500 detect_coding_sjis (struct coding_system *coding,
4501 struct coding_detection_info *detect_info)
4503 const unsigned char *src = coding->source, *src_base;
4504 const unsigned char *src_end = coding->source + coding->src_bytes;
4505 bool multibytep = coding->src_multibyte;
4506 ptrdiff_t consumed_chars = 0;
4507 int found = 0;
4508 int c;
4509 Lisp_Object attrs, charset_list;
4510 int max_first_byte_of_2_byte_code;
4512 CODING_GET_INFO (coding, attrs, charset_list);
4513 max_first_byte_of_2_byte_code
4514 = (XINT (Flength (charset_list)) > 3 ? 0xFC : 0xEF);
4516 detect_info->checked |= CATEGORY_MASK_SJIS;
4517 /* A coding system of this category is always ASCII compatible. */
4518 src += coding->head_ascii;
4520 while (1)
4522 src_base = src;
4523 ONE_MORE_BYTE (c);
4524 if (c < 0x80)
4525 continue;
4526 if ((c >= 0x81 && c <= 0x9F)
4527 || (c >= 0xE0 && c <= max_first_byte_of_2_byte_code))
4529 ONE_MORE_BYTE (c);
4530 if (c < 0x40 || c == 0x7F || c > 0xFC)
4531 break;
4532 found = CATEGORY_MASK_SJIS;
4534 else if (c >= 0xA0 && c < 0xE0)
4535 found = CATEGORY_MASK_SJIS;
4536 else
4537 break;
4539 detect_info->rejected |= CATEGORY_MASK_SJIS;
4540 return 0;
4542 no_more_source:
4543 if (src_base < src && coding->mode & CODING_MODE_LAST_BLOCK)
4545 detect_info->rejected |= CATEGORY_MASK_SJIS;
4546 return 0;
4548 detect_info->found |= found;
4549 return 1;
4552 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4553 Return true if a text is encoded in BIG5. */
4555 static bool
4556 detect_coding_big5 (struct coding_system *coding,
4557 struct coding_detection_info *detect_info)
4559 const unsigned char *src = coding->source, *src_base;
4560 const unsigned char *src_end = coding->source + coding->src_bytes;
4561 bool multibytep = coding->src_multibyte;
4562 ptrdiff_t consumed_chars = 0;
4563 int found = 0;
4564 int c;
4566 detect_info->checked |= CATEGORY_MASK_BIG5;
4567 /* A coding system of this category is always ASCII compatible. */
4568 src += coding->head_ascii;
4570 while (1)
4572 src_base = src;
4573 ONE_MORE_BYTE (c);
4574 if (c < 0x80)
4575 continue;
4576 if (c >= 0xA1)
4578 ONE_MORE_BYTE (c);
4579 if (c < 0x40 || (c >= 0x7F && c <= 0xA0))
4580 return 0;
4581 found = CATEGORY_MASK_BIG5;
4583 else
4584 break;
4586 detect_info->rejected |= CATEGORY_MASK_BIG5;
4587 return 0;
4589 no_more_source:
4590 if (src_base < src && coding->mode & CODING_MODE_LAST_BLOCK)
4592 detect_info->rejected |= CATEGORY_MASK_BIG5;
4593 return 0;
4595 detect_info->found |= found;
4596 return 1;
4599 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
4601 static void
4602 decode_coding_sjis (struct coding_system *coding)
4604 const unsigned char *src = coding->source + coding->consumed;
4605 const unsigned char *src_end = coding->source + coding->src_bytes;
4606 const unsigned char *src_base;
4607 int *charbuf = coding->charbuf + coding->charbuf_used;
4608 /* We may produce one charset annotation in one loop and one more at
4609 the end. */
4610 int *charbuf_end
4611 = coding->charbuf + coding->charbuf_size - (MAX_ANNOTATION_LENGTH * 2);
4612 ptrdiff_t consumed_chars = 0, consumed_chars_base;
4613 bool multibytep = coding->src_multibyte;
4614 struct charset *charset_roman, *charset_kanji, *charset_kana;
4615 struct charset *charset_kanji2;
4616 Lisp_Object attrs, charset_list, val;
4617 ptrdiff_t char_offset = coding->produced_char;
4618 ptrdiff_t last_offset = char_offset;
4619 int last_id = charset_ascii;
4620 bool eol_dos
4621 = !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos);
4622 int byte_after_cr = -1;
4624 CODING_GET_INFO (coding, attrs, charset_list);
4626 val = charset_list;
4627 charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
4628 charset_kana = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
4629 charset_kanji = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
4630 charset_kanji2 = NILP (val) ? NULL : CHARSET_FROM_ID (XINT (XCAR (val)));
4632 while (1)
4634 int c, c1;
4635 struct charset *charset;
4637 src_base = src;
4638 consumed_chars_base = consumed_chars;
4640 if (charbuf >= charbuf_end)
4642 if (byte_after_cr >= 0)
4643 src_base--;
4644 break;
4647 if (byte_after_cr >= 0)
4648 c = byte_after_cr, byte_after_cr = -1;
4649 else
4650 ONE_MORE_BYTE (c);
4651 if (c < 0)
4652 goto invalid_code;
4653 if (c < 0x80)
4655 if (eol_dos && c == '\r')
4656 ONE_MORE_BYTE (byte_after_cr);
4657 charset = charset_roman;
4659 else if (c == 0x80 || c == 0xA0)
4660 goto invalid_code;
4661 else if (c >= 0xA1 && c <= 0xDF)
4663 /* SJIS -> JISX0201-Kana */
4664 c &= 0x7F;
4665 charset = charset_kana;
4667 else if (c <= 0xEF)
4669 /* SJIS -> JISX0208 */
4670 ONE_MORE_BYTE (c1);
4671 if (c1 < 0x40 || c1 == 0x7F || c1 > 0xFC)
4672 goto invalid_code;
4673 c = (c << 8) | c1;
4674 SJIS_TO_JIS (c);
4675 charset = charset_kanji;
4677 else if (c <= 0xFC && charset_kanji2)
4679 /* SJIS -> JISX0213-2 */
4680 ONE_MORE_BYTE (c1);
4681 if (c1 < 0x40 || c1 == 0x7F || c1 > 0xFC)
4682 goto invalid_code;
4683 c = (c << 8) | c1;
4684 SJIS_TO_JIS2 (c);
4685 charset = charset_kanji2;
4687 else
4688 goto invalid_code;
4689 if (charset->id != charset_ascii
4690 && last_id != charset->id)
4692 if (last_id != charset_ascii)
4693 ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id);
4694 last_id = charset->id;
4695 last_offset = char_offset;
4697 CODING_DECODE_CHAR (coding, src, src_base, src_end, charset, c, c);
4698 *charbuf++ = c;
4699 char_offset++;
4700 continue;
4702 invalid_code:
4703 src = src_base;
4704 consumed_chars = consumed_chars_base;
4705 ONE_MORE_BYTE (c);
4706 *charbuf++ = c < 0 ? -c : BYTE8_TO_CHAR (c);
4707 char_offset++;
4708 coding->errors++;
4711 no_more_source:
4712 if (last_id != charset_ascii)
4713 ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id);
4714 coding->consumed_char += consumed_chars_base;
4715 coding->consumed = src_base - coding->source;
4716 coding->charbuf_used = charbuf - coding->charbuf;
4719 static void
4720 decode_coding_big5 (struct coding_system *coding)
4722 const unsigned char *src = coding->source + coding->consumed;
4723 const unsigned char *src_end = coding->source + coding->src_bytes;
4724 const unsigned char *src_base;
4725 int *charbuf = coding->charbuf + coding->charbuf_used;
4726 /* We may produce one charset annotation in one loop and one more at
4727 the end. */
4728 int *charbuf_end
4729 = coding->charbuf + coding->charbuf_size - (MAX_ANNOTATION_LENGTH * 2);
4730 ptrdiff_t consumed_chars = 0, consumed_chars_base;
4731 bool multibytep = coding->src_multibyte;
4732 struct charset *charset_roman, *charset_big5;
4733 Lisp_Object attrs, charset_list, val;
4734 ptrdiff_t char_offset = coding->produced_char;
4735 ptrdiff_t last_offset = char_offset;
4736 int last_id = charset_ascii;
4737 bool eol_dos
4738 = !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos);
4739 int byte_after_cr = -1;
4741 CODING_GET_INFO (coding, attrs, charset_list);
4742 val = charset_list;
4743 charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
4744 charset_big5 = CHARSET_FROM_ID (XINT (XCAR (val)));
4746 while (1)
4748 int c, c1;
4749 struct charset *charset;
4751 src_base = src;
4752 consumed_chars_base = consumed_chars;
4754 if (charbuf >= charbuf_end)
4756 if (byte_after_cr >= 0)
4757 src_base--;
4758 break;
4761 if (byte_after_cr >= 0)
4762 c = byte_after_cr, byte_after_cr = -1;
4763 else
4764 ONE_MORE_BYTE (c);
4766 if (c < 0)
4767 goto invalid_code;
4768 if (c < 0x80)
4770 if (eol_dos && c == '\r')
4771 ONE_MORE_BYTE (byte_after_cr);
4772 charset = charset_roman;
4774 else
4776 /* BIG5 -> Big5 */
4777 if (c < 0xA1 || c > 0xFE)
4778 goto invalid_code;
4779 ONE_MORE_BYTE (c1);
4780 if (c1 < 0x40 || (c1 > 0x7E && c1 < 0xA1) || c1 > 0xFE)
4781 goto invalid_code;
4782 c = c << 8 | c1;
4783 charset = charset_big5;
4785 if (charset->id != charset_ascii
4786 && last_id != charset->id)
4788 if (last_id != charset_ascii)
4789 ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id);
4790 last_id = charset->id;
4791 last_offset = char_offset;
4793 CODING_DECODE_CHAR (coding, src, src_base, src_end, charset, c, c);
4794 *charbuf++ = c;
4795 char_offset++;
4796 continue;
4798 invalid_code:
4799 src = src_base;
4800 consumed_chars = consumed_chars_base;
4801 ONE_MORE_BYTE (c);
4802 *charbuf++ = c < 0 ? -c : BYTE8_TO_CHAR (c);
4803 char_offset++;
4804 coding->errors++;
4807 no_more_source:
4808 if (last_id != charset_ascii)
4809 ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id);
4810 coding->consumed_char += consumed_chars_base;
4811 coding->consumed = src_base - coding->source;
4812 coding->charbuf_used = charbuf - coding->charbuf;
4815 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions".
4816 This function can encode charsets `ascii', `katakana-jisx0201',
4817 `japanese-jisx0208', `chinese-big5-1', and `chinese-big5-2'. We
4818 are sure that all these charsets are registered as official charset
4819 (i.e. do not have extended leading-codes). Characters of other
4820 charsets are produced without any encoding. */
4822 static bool
4823 encode_coding_sjis (struct coding_system *coding)
4825 bool multibytep = coding->dst_multibyte;
4826 int *charbuf = coding->charbuf;
4827 int *charbuf_end = charbuf + coding->charbuf_used;
4828 unsigned char *dst = coding->destination + coding->produced;
4829 unsigned char *dst_end = coding->destination + coding->dst_bytes;
4830 int safe_room = 4;
4831 ptrdiff_t produced_chars = 0;
4832 Lisp_Object attrs, charset_list, val;
4833 bool ascii_compatible;
4834 struct charset *charset_kanji, *charset_kana;
4835 struct charset *charset_kanji2;
4836 int c;
4838 CODING_GET_INFO (coding, attrs, charset_list);
4839 val = XCDR (charset_list);
4840 charset_kana = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
4841 charset_kanji = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
4842 charset_kanji2 = NILP (val) ? NULL : CHARSET_FROM_ID (XINT (XCAR (val)));
4844 ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs));
4846 while (charbuf < charbuf_end)
4848 ASSURE_DESTINATION (safe_room);
4849 c = *charbuf++;
4850 /* Now encode the character C. */
4851 if (ASCII_CHAR_P (c) && ascii_compatible)
4852 EMIT_ONE_ASCII_BYTE (c);
4853 else if (CHAR_BYTE8_P (c))
4855 c = CHAR_TO_BYTE8 (c);
4856 EMIT_ONE_BYTE (c);
4858 else
4860 unsigned code;
4861 struct charset *charset;
4862 CODING_CHAR_CHARSET (coding, dst, dst_end, c, charset_list,
4863 &code, charset);
4865 if (!charset)
4867 if (coding->mode & CODING_MODE_SAFE_ENCODING)
4869 code = CODING_INHIBIT_CHARACTER_SUBSTITUTION;
4870 charset = CHARSET_FROM_ID (charset_ascii);
4872 else
4874 c = coding->default_char;
4875 CODING_CHAR_CHARSET (coding, dst, dst_end, c,
4876 charset_list, &code, charset);
4879 if (code == CHARSET_INVALID_CODE (charset))
4880 emacs_abort ();
4881 if (charset == charset_kanji)
4883 int c1, c2;
4884 JIS_TO_SJIS (code);
4885 c1 = code >> 8, c2 = code & 0xFF;
4886 EMIT_TWO_BYTES (c1, c2);
4888 else if (charset == charset_kana)
4889 EMIT_ONE_BYTE (code | 0x80);
4890 else if (charset_kanji2 && charset == charset_kanji2)
4892 int c1, c2;
4894 c1 = code >> 8;
4895 if (c1 == 0x21 || (c1 >= 0x23 && c1 <= 0x25)
4896 || c1 == 0x28
4897 || (c1 >= 0x2C && c1 <= 0x2F) || c1 >= 0x6E)
4899 JIS_TO_SJIS2 (code);
4900 c1 = code >> 8, c2 = code & 0xFF;
4901 EMIT_TWO_BYTES (c1, c2);
4903 else
4904 EMIT_ONE_ASCII_BYTE (code & 0x7F);
4906 else
4907 EMIT_ONE_ASCII_BYTE (code & 0x7F);
4910 record_conversion_result (coding, CODING_RESULT_SUCCESS);
4911 coding->produced_char += produced_chars;
4912 coding->produced = dst - coding->destination;
4913 return 0;
4916 static bool
4917 encode_coding_big5 (struct coding_system *coding)
4919 bool multibytep = coding->dst_multibyte;
4920 int *charbuf = coding->charbuf;
4921 int *charbuf_end = charbuf + coding->charbuf_used;
4922 unsigned char *dst = coding->destination + coding->produced;
4923 unsigned char *dst_end = coding->destination + coding->dst_bytes;
4924 int safe_room = 4;
4925 ptrdiff_t produced_chars = 0;
4926 Lisp_Object attrs, charset_list, val;
4927 bool ascii_compatible;
4928 struct charset *charset_big5;
4929 int c;
4931 CODING_GET_INFO (coding, attrs, charset_list);
4932 val = XCDR (charset_list);
4933 charset_big5 = CHARSET_FROM_ID (XINT (XCAR (val)));
4934 ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs));
4936 while (charbuf < charbuf_end)
4938 ASSURE_DESTINATION (safe_room);
4939 c = *charbuf++;
4940 /* Now encode the character C. */
4941 if (ASCII_CHAR_P (c) && ascii_compatible)
4942 EMIT_ONE_ASCII_BYTE (c);
4943 else if (CHAR_BYTE8_P (c))
4945 c = CHAR_TO_BYTE8 (c);
4946 EMIT_ONE_BYTE (c);
4948 else
4950 unsigned code;
4951 struct charset *charset;
4952 CODING_CHAR_CHARSET (coding, dst, dst_end, c, charset_list,
4953 &code, charset);
4955 if (! charset)
4957 if (coding->mode & CODING_MODE_SAFE_ENCODING)
4959 code = CODING_INHIBIT_CHARACTER_SUBSTITUTION;
4960 charset = CHARSET_FROM_ID (charset_ascii);
4962 else
4964 c = coding->default_char;
4965 CODING_CHAR_CHARSET (coding, dst, dst_end, c,
4966 charset_list, &code, charset);
4969 if (code == CHARSET_INVALID_CODE (charset))
4970 emacs_abort ();
4971 if (charset == charset_big5)
4973 int c1, c2;
4975 c1 = code >> 8, c2 = code & 0xFF;
4976 EMIT_TWO_BYTES (c1, c2);
4978 else
4979 EMIT_ONE_ASCII_BYTE (code & 0x7F);
4982 record_conversion_result (coding, CODING_RESULT_SUCCESS);
4983 coding->produced_char += produced_chars;
4984 coding->produced = dst - coding->destination;
4985 return 0;
4989 /*** 10. CCL handlers ***/
4991 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4992 Return true if a text is encoded in a coding system of which
4993 encoder/decoder are written in CCL program. */
4995 static bool
4996 detect_coding_ccl (struct coding_system *coding,
4997 struct coding_detection_info *detect_info)
4999 const unsigned char *src = coding->source, *src_base;
5000 const unsigned char *src_end = coding->source + coding->src_bytes;
5001 bool multibytep = coding->src_multibyte;
5002 ptrdiff_t consumed_chars = 0;
5003 int found = 0;
5004 unsigned char *valids;
5005 ptrdiff_t head_ascii = coding->head_ascii;
5006 Lisp_Object attrs;
5008 detect_info->checked |= CATEGORY_MASK_CCL;
5010 coding = &coding_categories[coding_category_ccl];
5011 valids = CODING_CCL_VALIDS (coding);
5012 attrs = CODING_ID_ATTRS (coding->id);
5013 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs)))
5014 src += head_ascii;
5016 while (1)
5018 int c;
5020 src_base = src;
5021 ONE_MORE_BYTE (c);
5022 if (c < 0 || ! valids[c])
5023 break;
5024 if ((valids[c] > 1))
5025 found = CATEGORY_MASK_CCL;
5027 detect_info->rejected |= CATEGORY_MASK_CCL;
5028 return 0;
5030 no_more_source:
5031 detect_info->found |= found;
5032 return 1;
5035 static void
5036 decode_coding_ccl (struct coding_system *coding)
5038 const unsigned char *src = coding->source + coding->consumed;
5039 const unsigned char *src_end = coding->source + coding->src_bytes;
5040 int *charbuf = coding->charbuf + coding->charbuf_used;
5041 int *charbuf_end = coding->charbuf + coding->charbuf_size;
5042 ptrdiff_t consumed_chars = 0;
5043 bool multibytep = coding->src_multibyte;
5044 struct ccl_program *ccl = &coding->spec.ccl->ccl;
5045 int source_charbuf[1024];
5046 int source_byteidx[1025];
5047 Lisp_Object attrs, charset_list;
5049 CODING_GET_INFO (coding, attrs, charset_list);
5051 while (1)
5053 const unsigned char *p = src;
5054 ptrdiff_t offset;
5055 int i = 0;
5057 if (multibytep)
5059 while (i < 1024 && p < src_end)
5061 source_byteidx[i] = p - src;
5062 source_charbuf[i++] = STRING_CHAR_ADVANCE (p);
5064 source_byteidx[i] = p - src;
5066 else
5067 while (i < 1024 && p < src_end)
5068 source_charbuf[i++] = *p++;
5070 if (p == src_end && coding->mode & CODING_MODE_LAST_BLOCK)
5071 ccl->last_block = 1;
5072 /* As ccl_driver calls DECODE_CHAR, buffer may be relocated. */
5073 charset_map_loaded = 0;
5074 ccl_driver (ccl, source_charbuf, charbuf, i, charbuf_end - charbuf,
5075 charset_list);
5076 if (charset_map_loaded
5077 && (offset = coding_change_source (coding)))
5079 p += offset;
5080 src += offset;
5081 src_end += offset;
5083 charbuf += ccl->produced;
5084 if (multibytep)
5085 src += source_byteidx[ccl->consumed];
5086 else
5087 src += ccl->consumed;
5088 consumed_chars += ccl->consumed;
5089 if (p == src_end || ccl->status != CCL_STAT_SUSPEND_BY_SRC)
5090 break;
5093 switch (ccl->status)
5095 case CCL_STAT_SUSPEND_BY_SRC:
5096 record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_SRC);
5097 break;
5098 case CCL_STAT_SUSPEND_BY_DST:
5099 record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_DST);
5100 break;
5101 case CCL_STAT_QUIT:
5102 case CCL_STAT_INVALID_CMD:
5103 record_conversion_result (coding, CODING_RESULT_INTERRUPT);
5104 break;
5105 default:
5106 record_conversion_result (coding, CODING_RESULT_SUCCESS);
5107 break;
5109 coding->consumed_char += consumed_chars;
5110 coding->consumed = src - coding->source;
5111 coding->charbuf_used = charbuf - coding->charbuf;
5114 static bool
5115 encode_coding_ccl (struct coding_system *coding)
5117 struct ccl_program *ccl = &coding->spec.ccl->ccl;
5118 bool multibytep = coding->dst_multibyte;
5119 int *charbuf = coding->charbuf;
5120 int *charbuf_end = charbuf + coding->charbuf_used;
5121 unsigned char *dst = coding->destination + coding->produced;
5122 unsigned char *dst_end = coding->destination + coding->dst_bytes;
5123 int destination_charbuf[1024];
5124 ptrdiff_t produced_chars = 0;
5125 int i;
5126 Lisp_Object attrs, charset_list;
5128 CODING_GET_INFO (coding, attrs, charset_list);
5129 if (coding->consumed_char == coding->src_chars
5130 && coding->mode & CODING_MODE_LAST_BLOCK)
5131 ccl->last_block = 1;
5135 ptrdiff_t offset;
5137 /* As ccl_driver calls DECODE_CHAR, buffer may be relocated. */
5138 charset_map_loaded = 0;
5139 ccl_driver (ccl, charbuf, destination_charbuf,
5140 charbuf_end - charbuf, 1024, charset_list);
5141 if (charset_map_loaded
5142 && (offset = coding_change_destination (coding)))
5143 dst += offset;
5144 if (multibytep)
5146 ASSURE_DESTINATION (ccl->produced * 2);
5147 for (i = 0; i < ccl->produced; i++)
5148 EMIT_ONE_BYTE (destination_charbuf[i] & 0xFF);
5150 else
5152 ASSURE_DESTINATION (ccl->produced);
5153 for (i = 0; i < ccl->produced; i++)
5154 *dst++ = destination_charbuf[i] & 0xFF;
5155 produced_chars += ccl->produced;
5157 charbuf += ccl->consumed;
5158 if (ccl->status == CCL_STAT_QUIT
5159 || ccl->status == CCL_STAT_INVALID_CMD)
5160 break;
5162 while (charbuf < charbuf_end);
5164 switch (ccl->status)
5166 case CCL_STAT_SUSPEND_BY_SRC:
5167 record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_SRC);
5168 break;
5169 case CCL_STAT_SUSPEND_BY_DST:
5170 record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_DST);
5171 break;
5172 case CCL_STAT_QUIT:
5173 case CCL_STAT_INVALID_CMD:
5174 record_conversion_result (coding, CODING_RESULT_INTERRUPT);
5175 break;
5176 default:
5177 record_conversion_result (coding, CODING_RESULT_SUCCESS);
5178 break;
5181 coding->produced_char += produced_chars;
5182 coding->produced = dst - coding->destination;
5183 return 0;
5187 /*** 10, 11. no-conversion handlers ***/
5189 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
5191 static void
5192 decode_coding_raw_text (struct coding_system *coding)
5194 bool eol_dos
5195 = !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos);
5197 coding->chars_at_source = 1;
5198 coding->consumed_char = coding->src_chars;
5199 coding->consumed = coding->src_bytes;
5200 if (eol_dos && coding->source[coding->src_bytes - 1] == '\r')
5202 coding->consumed_char--;
5203 coding->consumed--;
5204 record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_SRC);
5206 else
5207 record_conversion_result (coding, CODING_RESULT_SUCCESS);
5210 static bool
5211 encode_coding_raw_text (struct coding_system *coding)
5213 bool multibytep = coding->dst_multibyte;
5214 int *charbuf = coding->charbuf;
5215 int *charbuf_end = coding->charbuf + coding->charbuf_used;
5216 unsigned char *dst = coding->destination + coding->produced;
5217 unsigned char *dst_end = coding->destination + coding->dst_bytes;
5218 ptrdiff_t produced_chars = 0;
5219 int c;
5221 if (multibytep)
5223 int safe_room = MAX_MULTIBYTE_LENGTH * 2;
5225 if (coding->src_multibyte)
5226 while (charbuf < charbuf_end)
5228 ASSURE_DESTINATION (safe_room);
5229 c = *charbuf++;
5230 if (ASCII_CHAR_P (c))
5231 EMIT_ONE_ASCII_BYTE (c);
5232 else if (CHAR_BYTE8_P (c))
5234 c = CHAR_TO_BYTE8 (c);
5235 EMIT_ONE_BYTE (c);
5237 else
5239 unsigned char str[MAX_MULTIBYTE_LENGTH], *p0 = str, *p1 = str;
5241 CHAR_STRING_ADVANCE (c, p1);
5244 EMIT_ONE_BYTE (*p0);
5245 p0++;
5247 while (p0 < p1);
5250 else
5251 while (charbuf < charbuf_end)
5253 ASSURE_DESTINATION (safe_room);
5254 c = *charbuf++;
5255 EMIT_ONE_BYTE (c);
5258 else
5260 if (coding->src_multibyte)
5262 int safe_room = MAX_MULTIBYTE_LENGTH;
5264 while (charbuf < charbuf_end)
5266 ASSURE_DESTINATION (safe_room);
5267 c = *charbuf++;
5268 if (ASCII_CHAR_P (c))
5269 *dst++ = c;
5270 else if (CHAR_BYTE8_P (c))
5271 *dst++ = CHAR_TO_BYTE8 (c);
5272 else
5273 CHAR_STRING_ADVANCE (c, dst);
5276 else
5278 ASSURE_DESTINATION (charbuf_end - charbuf);
5279 while (charbuf < charbuf_end && dst < dst_end)
5280 *dst++ = *charbuf++;
5282 produced_chars = dst - (coding->destination + coding->produced);
5284 record_conversion_result (coding, CODING_RESULT_SUCCESS);
5285 coding->produced_char += produced_chars;
5286 coding->produced = dst - coding->destination;
5287 return 0;
5290 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
5291 Return true if a text is encoded in a charset-based coding system. */
5293 static bool
5294 detect_coding_charset (struct coding_system *coding,
5295 struct coding_detection_info *detect_info)
5297 const unsigned char *src = coding->source, *src_base;
5298 const unsigned char *src_end = coding->source + coding->src_bytes;
5299 bool multibytep = coding->src_multibyte;
5300 ptrdiff_t consumed_chars = 0;
5301 Lisp_Object attrs, valids, name;
5302 int found = 0;
5303 ptrdiff_t head_ascii = coding->head_ascii;
5304 bool check_latin_extra = 0;
5306 detect_info->checked |= CATEGORY_MASK_CHARSET;
5308 coding = &coding_categories[coding_category_charset];
5309 attrs = CODING_ID_ATTRS (coding->id);
5310 valids = AREF (attrs, coding_attr_charset_valids);
5311 name = CODING_ID_NAME (coding->id);
5312 if (strncmp (SSDATA (SYMBOL_NAME (name)),
5313 "iso-8859-", sizeof ("iso-8859-") - 1) == 0
5314 || strncmp (SSDATA (SYMBOL_NAME (name)),
5315 "iso-latin-", sizeof ("iso-latin-") - 1) == 0)
5316 check_latin_extra = 1;
5318 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs)))
5319 src += head_ascii;
5321 while (1)
5323 int c;
5324 Lisp_Object val;
5325 struct charset *charset;
5326 int dim, idx;
5328 src_base = src;
5329 ONE_MORE_BYTE (c);
5330 if (c < 0)
5331 continue;
5332 val = AREF (valids, c);
5333 if (NILP (val))
5334 break;
5335 if (c >= 0x80)
5337 if (c < 0xA0
5338 && check_latin_extra
5339 && (!VECTORP (Vlatin_extra_code_table)
5340 || NILP (AREF (Vlatin_extra_code_table, c))))
5341 break;
5342 found = CATEGORY_MASK_CHARSET;
5344 if (INTEGERP (val))
5346 charset = CHARSET_FROM_ID (XFASTINT (val));
5347 dim = CHARSET_DIMENSION (charset);
5348 for (idx = 1; idx < dim; idx++)
5350 if (src == src_end)
5351 goto too_short;
5352 ONE_MORE_BYTE (c);
5353 if (c < charset->code_space[(dim - 1 - idx) * 4]
5354 || c > charset->code_space[(dim - 1 - idx) * 4 + 1])
5355 break;
5357 if (idx < dim)
5358 break;
5360 else
5362 idx = 1;
5363 for (; CONSP (val); val = XCDR (val))
5365 charset = CHARSET_FROM_ID (XFASTINT (XCAR (val)));
5366 dim = CHARSET_DIMENSION (charset);
5367 while (idx < dim)
5369 if (src == src_end)
5370 goto too_short;
5371 ONE_MORE_BYTE (c);
5372 if (c < charset->code_space[(dim - 1 - idx) * 4]
5373 || c > charset->code_space[(dim - 1 - idx) * 4 + 1])
5374 break;
5375 idx++;
5377 if (idx == dim)
5379 val = Qnil;
5380 break;
5383 if (CONSP (val))
5384 break;
5387 too_short:
5388 detect_info->rejected |= CATEGORY_MASK_CHARSET;
5389 return 0;
5391 no_more_source:
5392 detect_info->found |= found;
5393 return 1;
5396 static void
5397 decode_coding_charset (struct coding_system *coding)
5399 const unsigned char *src = coding->source + coding->consumed;
5400 const unsigned char *src_end = coding->source + coding->src_bytes;
5401 const unsigned char *src_base;
5402 int *charbuf = coding->charbuf + coding->charbuf_used;
5403 /* We may produce one charset annotation in one loop and one more at
5404 the end. */
5405 int *charbuf_end
5406 = coding->charbuf + coding->charbuf_size - (MAX_ANNOTATION_LENGTH * 2);
5407 ptrdiff_t consumed_chars = 0, consumed_chars_base;
5408 bool multibytep = coding->src_multibyte;
5409 Lisp_Object attrs = CODING_ID_ATTRS (coding->id);
5410 Lisp_Object valids;
5411 ptrdiff_t char_offset = coding->produced_char;
5412 ptrdiff_t last_offset = char_offset;
5413 int last_id = charset_ascii;
5414 bool eol_dos
5415 = !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos);
5416 int byte_after_cr = -1;
5418 valids = AREF (attrs, coding_attr_charset_valids);
5420 while (1)
5422 int c;
5423 Lisp_Object val;
5424 struct charset *charset;
5425 int dim;
5426 int len = 1;
5427 unsigned code;
5429 src_base = src;
5430 consumed_chars_base = consumed_chars;
5432 if (charbuf >= charbuf_end)
5434 if (byte_after_cr >= 0)
5435 src_base--;
5436 break;
5439 if (byte_after_cr >= 0)
5441 c = byte_after_cr;
5442 byte_after_cr = -1;
5444 else
5446 ONE_MORE_BYTE (c);
5447 if (eol_dos && c == '\r')
5448 ONE_MORE_BYTE (byte_after_cr);
5450 if (c < 0)
5451 goto invalid_code;
5452 code = c;
5454 val = AREF (valids, c);
5455 if (! INTEGERP (val) && ! CONSP (val))
5456 goto invalid_code;
5457 if (INTEGERP (val))
5459 charset = CHARSET_FROM_ID (XFASTINT (val));
5460 dim = CHARSET_DIMENSION (charset);
5461 while (len < dim)
5463 ONE_MORE_BYTE (c);
5464 code = (code << 8) | c;
5465 len++;
5467 CODING_DECODE_CHAR (coding, src, src_base, src_end,
5468 charset, code, c);
5470 else
5472 /* VAL is a list of charset IDs. It is assured that the
5473 list is sorted by charset dimensions (smaller one
5474 comes first). */
5475 while (CONSP (val))
5477 charset = CHARSET_FROM_ID (XFASTINT (XCAR (val)));
5478 dim = CHARSET_DIMENSION (charset);
5479 while (len < dim)
5481 ONE_MORE_BYTE (c);
5482 code = (code << 8) | c;
5483 len++;
5485 CODING_DECODE_CHAR (coding, src, src_base,
5486 src_end, charset, code, c);
5487 if (c >= 0)
5488 break;
5489 val = XCDR (val);
5492 if (c < 0)
5493 goto invalid_code;
5494 if (charset->id != charset_ascii
5495 && last_id != charset->id)
5497 if (last_id != charset_ascii)
5498 ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id);
5499 last_id = charset->id;
5500 last_offset = char_offset;
5503 *charbuf++ = c;
5504 char_offset++;
5505 continue;
5507 invalid_code:
5508 src = src_base;
5509 consumed_chars = consumed_chars_base;
5510 ONE_MORE_BYTE (c);
5511 *charbuf++ = c < 0 ? -c : ASCII_BYTE_P (c) ? c : BYTE8_TO_CHAR (c);
5512 char_offset++;
5513 coding->errors++;
5516 no_more_source:
5517 if (last_id != charset_ascii)
5518 ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id);
5519 coding->consumed_char += consumed_chars_base;
5520 coding->consumed = src_base - coding->source;
5521 coding->charbuf_used = charbuf - coding->charbuf;
5524 static bool
5525 encode_coding_charset (struct coding_system *coding)
5527 bool multibytep = coding->dst_multibyte;
5528 int *charbuf = coding->charbuf;
5529 int *charbuf_end = charbuf + coding->charbuf_used;
5530 unsigned char *dst = coding->destination + coding->produced;
5531 unsigned char *dst_end = coding->destination + coding->dst_bytes;
5532 int safe_room = MAX_MULTIBYTE_LENGTH;
5533 ptrdiff_t produced_chars = 0;
5534 Lisp_Object attrs, charset_list;
5535 bool ascii_compatible;
5536 int c;
5538 CODING_GET_INFO (coding, attrs, charset_list);
5539 ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs));
5541 while (charbuf < charbuf_end)
5543 struct charset *charset;
5544 unsigned code;
5546 ASSURE_DESTINATION (safe_room);
5547 c = *charbuf++;
5548 if (ascii_compatible && ASCII_CHAR_P (c))
5549 EMIT_ONE_ASCII_BYTE (c);
5550 else if (CHAR_BYTE8_P (c))
5552 c = CHAR_TO_BYTE8 (c);
5553 EMIT_ONE_BYTE (c);
5555 else
5557 CODING_CHAR_CHARSET (coding, dst, dst_end, c, charset_list,
5558 &code, charset);
5560 if (charset)
5562 if (CHARSET_DIMENSION (charset) == 1)
5563 EMIT_ONE_BYTE (code);
5564 else if (CHARSET_DIMENSION (charset) == 2)
5565 EMIT_TWO_BYTES (code >> 8, code & 0xFF);
5566 else if (CHARSET_DIMENSION (charset) == 3)
5567 EMIT_THREE_BYTES (code >> 16, (code >> 8) & 0xFF, code & 0xFF);
5568 else
5569 EMIT_FOUR_BYTES (code >> 24, (code >> 16) & 0xFF,
5570 (code >> 8) & 0xFF, code & 0xFF);
5572 else
5574 if (coding->mode & CODING_MODE_SAFE_ENCODING)
5575 c = CODING_INHIBIT_CHARACTER_SUBSTITUTION;
5576 else
5577 c = coding->default_char;
5578 EMIT_ONE_BYTE (c);
5583 record_conversion_result (coding, CODING_RESULT_SUCCESS);
5584 coding->produced_char += produced_chars;
5585 coding->produced = dst - coding->destination;
5586 return 0;
5590 /*** 7. C library functions ***/
5592 /* Setup coding context CODING from information about CODING_SYSTEM.
5593 If CODING_SYSTEM is nil, `no-conversion' is assumed. If
5594 CODING_SYSTEM is invalid, signal an error. */
5596 void
5597 setup_coding_system (Lisp_Object coding_system, struct coding_system *coding)
5599 Lisp_Object attrs;
5600 Lisp_Object eol_type;
5601 Lisp_Object coding_type;
5602 Lisp_Object val;
5604 if (NILP (coding_system))
5605 coding_system = Qundecided;
5607 CHECK_CODING_SYSTEM_GET_ID (coding_system, coding->id);
5609 attrs = CODING_ID_ATTRS (coding->id);
5610 eol_type = inhibit_eol_conversion ? Qunix : CODING_ID_EOL_TYPE (coding->id);
5612 coding->mode = 0;
5613 coding->head_ascii = -1;
5614 if (VECTORP (eol_type))
5615 coding->common_flags = (CODING_REQUIRE_DECODING_MASK
5616 | CODING_REQUIRE_DETECTION_MASK);
5617 else if (! EQ (eol_type, Qunix))
5618 coding->common_flags = (CODING_REQUIRE_DECODING_MASK
5619 | CODING_REQUIRE_ENCODING_MASK);
5620 else
5621 coding->common_flags = 0;
5622 if (! NILP (CODING_ATTR_POST_READ (attrs)))
5623 coding->common_flags |= CODING_REQUIRE_DECODING_MASK;
5624 if (! NILP (CODING_ATTR_PRE_WRITE (attrs)))
5625 coding->common_flags |= CODING_REQUIRE_ENCODING_MASK;
5626 if (! NILP (CODING_ATTR_FOR_UNIBYTE (attrs)))
5627 coding->common_flags |= CODING_FOR_UNIBYTE_MASK;
5629 val = CODING_ATTR_SAFE_CHARSETS (attrs);
5630 coding->max_charset_id = SCHARS (val) - 1;
5631 coding->safe_charsets = SDATA (val);
5632 coding->default_char = XINT (CODING_ATTR_DEFAULT_CHAR (attrs));
5633 coding->carryover_bytes = 0;
5635 coding_type = CODING_ATTR_TYPE (attrs);
5636 if (EQ (coding_type, Qundecided))
5638 coding->detector = NULL;
5639 coding->decoder = decode_coding_raw_text;
5640 coding->encoder = encode_coding_raw_text;
5641 coding->common_flags |= CODING_REQUIRE_DETECTION_MASK;
5643 else if (EQ (coding_type, Qiso_2022))
5645 int i;
5646 int flags = XINT (AREF (attrs, coding_attr_iso_flags));
5648 /* Invoke graphic register 0 to plane 0. */
5649 CODING_ISO_INVOCATION (coding, 0) = 0;
5650 /* Invoke graphic register 1 to plane 1 if we can use 8-bit. */
5651 CODING_ISO_INVOCATION (coding, 1)
5652 = (flags & CODING_ISO_FLAG_SEVEN_BITS ? -1 : 1);
5653 /* Setup the initial status of designation. */
5654 for (i = 0; i < 4; i++)
5655 CODING_ISO_DESIGNATION (coding, i) = CODING_ISO_INITIAL (coding, i);
5656 /* Not single shifting initially. */
5657 CODING_ISO_SINGLE_SHIFTING (coding) = 0;
5658 /* Beginning of buffer should also be regarded as bol. */
5659 CODING_ISO_BOL (coding) = 1;
5660 coding->detector = detect_coding_iso_2022;
5661 coding->decoder = decode_coding_iso_2022;
5662 coding->encoder = encode_coding_iso_2022;
5663 if (flags & CODING_ISO_FLAG_SAFE)
5664 coding->mode |= CODING_MODE_SAFE_ENCODING;
5665 coding->common_flags
5666 |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK
5667 | CODING_REQUIRE_FLUSHING_MASK);
5668 if (flags & CODING_ISO_FLAG_COMPOSITION)
5669 coding->common_flags |= CODING_ANNOTATE_COMPOSITION_MASK;
5670 if (flags & CODING_ISO_FLAG_DESIGNATION)
5671 coding->common_flags |= CODING_ANNOTATE_CHARSET_MASK;
5672 if (flags & CODING_ISO_FLAG_FULL_SUPPORT)
5674 setup_iso_safe_charsets (attrs);
5675 val = CODING_ATTR_SAFE_CHARSETS (attrs);
5676 coding->max_charset_id = SCHARS (val) - 1;
5677 coding->safe_charsets = SDATA (val);
5679 CODING_ISO_FLAGS (coding) = flags;
5680 CODING_ISO_CMP_STATUS (coding)->state = COMPOSING_NO;
5681 CODING_ISO_CMP_STATUS (coding)->method = COMPOSITION_NO;
5682 CODING_ISO_EXTSEGMENT_LEN (coding) = 0;
5683 CODING_ISO_EMBEDDED_UTF_8 (coding) = 0;
5685 else if (EQ (coding_type, Qcharset))
5687 coding->detector = detect_coding_charset;
5688 coding->decoder = decode_coding_charset;
5689 coding->encoder = encode_coding_charset;
5690 coding->common_flags
5691 |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK);
5693 else if (EQ (coding_type, Qutf_8))
5695 val = AREF (attrs, coding_attr_utf_bom);
5696 CODING_UTF_8_BOM (coding) = (CONSP (val) ? utf_detect_bom
5697 : EQ (val, Qt) ? utf_with_bom
5698 : utf_without_bom);
5699 coding->detector = detect_coding_utf_8;
5700 coding->decoder = decode_coding_utf_8;
5701 coding->encoder = encode_coding_utf_8;
5702 coding->common_flags
5703 |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK);
5704 if (CODING_UTF_8_BOM (coding) == utf_detect_bom)
5705 coding->common_flags |= CODING_REQUIRE_DETECTION_MASK;
5707 else if (EQ (coding_type, Qutf_16))
5709 val = AREF (attrs, coding_attr_utf_bom);
5710 CODING_UTF_16_BOM (coding) = (CONSP (val) ? utf_detect_bom
5711 : EQ (val, Qt) ? utf_with_bom
5712 : utf_without_bom);
5713 val = AREF (attrs, coding_attr_utf_16_endian);
5714 CODING_UTF_16_ENDIAN (coding) = (EQ (val, Qbig) ? utf_16_big_endian
5715 : utf_16_little_endian);
5716 CODING_UTF_16_SURROGATE (coding) = 0;
5717 coding->detector = detect_coding_utf_16;
5718 coding->decoder = decode_coding_utf_16;
5719 coding->encoder = encode_coding_utf_16;
5720 coding->common_flags
5721 |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK);
5722 if (CODING_UTF_16_BOM (coding) == utf_detect_bom)
5723 coding->common_flags |= CODING_REQUIRE_DETECTION_MASK;
5725 else if (EQ (coding_type, Qccl))
5727 coding->detector = detect_coding_ccl;
5728 coding->decoder = decode_coding_ccl;
5729 coding->encoder = encode_coding_ccl;
5730 coding->common_flags
5731 |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK
5732 | CODING_REQUIRE_FLUSHING_MASK);
5734 else if (EQ (coding_type, Qemacs_mule))
5736 coding->detector = detect_coding_emacs_mule;
5737 coding->decoder = decode_coding_emacs_mule;
5738 coding->encoder = encode_coding_emacs_mule;
5739 coding->common_flags
5740 |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK);
5741 if (! NILP (AREF (attrs, coding_attr_emacs_mule_full))
5742 && ! EQ (CODING_ATTR_CHARSET_LIST (attrs), Vemacs_mule_charset_list))
5744 Lisp_Object tail, safe_charsets;
5745 int max_charset_id = 0;
5747 for (tail = Vemacs_mule_charset_list; CONSP (tail);
5748 tail = XCDR (tail))
5749 if (max_charset_id < XFASTINT (XCAR (tail)))
5750 max_charset_id = XFASTINT (XCAR (tail));
5751 safe_charsets = make_uninit_string (max_charset_id + 1);
5752 memset (SDATA (safe_charsets), 255, max_charset_id + 1);
5753 for (tail = Vemacs_mule_charset_list; CONSP (tail);
5754 tail = XCDR (tail))
5755 SSET (safe_charsets, XFASTINT (XCAR (tail)), 0);
5756 coding->max_charset_id = max_charset_id;
5757 coding->safe_charsets = SDATA (safe_charsets);
5759 coding->spec.emacs_mule.cmp_status.state = COMPOSING_NO;
5760 coding->spec.emacs_mule.cmp_status.method = COMPOSITION_NO;
5762 else if (EQ (coding_type, Qshift_jis))
5764 coding->detector = detect_coding_sjis;
5765 coding->decoder = decode_coding_sjis;
5766 coding->encoder = encode_coding_sjis;
5767 coding->common_flags
5768 |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK);
5770 else if (EQ (coding_type, Qbig5))
5772 coding->detector = detect_coding_big5;
5773 coding->decoder = decode_coding_big5;
5774 coding->encoder = encode_coding_big5;
5775 coding->common_flags
5776 |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK);
5778 else /* EQ (coding_type, Qraw_text) */
5780 coding->detector = NULL;
5781 coding->decoder = decode_coding_raw_text;
5782 coding->encoder = encode_coding_raw_text;
5783 if (! EQ (eol_type, Qunix))
5785 coding->common_flags |= CODING_REQUIRE_DECODING_MASK;
5786 if (! VECTORP (eol_type))
5787 coding->common_flags |= CODING_REQUIRE_ENCODING_MASK;
5792 return;
5795 /* Return a list of charsets supported by CODING. */
5797 Lisp_Object
5798 coding_charset_list (struct coding_system *coding)
5800 Lisp_Object attrs, charset_list;
5802 CODING_GET_INFO (coding, attrs, charset_list);
5803 if (EQ (CODING_ATTR_TYPE (attrs), Qiso_2022))
5805 int flags = XINT (AREF (attrs, coding_attr_iso_flags));
5807 if (flags & CODING_ISO_FLAG_FULL_SUPPORT)
5808 charset_list = Viso_2022_charset_list;
5810 else if (EQ (CODING_ATTR_TYPE (attrs), Qemacs_mule))
5812 charset_list = Vemacs_mule_charset_list;
5814 return charset_list;
5818 /* Return a list of charsets supported by CODING-SYSTEM. */
5820 Lisp_Object
5821 coding_system_charset_list (Lisp_Object coding_system)
5823 ptrdiff_t id;
5824 Lisp_Object attrs, charset_list;
5826 CHECK_CODING_SYSTEM_GET_ID (coding_system, id);
5827 attrs = CODING_ID_ATTRS (id);
5829 if (EQ (CODING_ATTR_TYPE (attrs), Qiso_2022))
5831 int flags = XINT (AREF (attrs, coding_attr_iso_flags));
5833 if (flags & CODING_ISO_FLAG_FULL_SUPPORT)
5834 charset_list = Viso_2022_charset_list;
5835 else
5836 charset_list = CODING_ATTR_CHARSET_LIST (attrs);
5838 else if (EQ (CODING_ATTR_TYPE (attrs), Qemacs_mule))
5840 charset_list = Vemacs_mule_charset_list;
5842 else
5844 charset_list = CODING_ATTR_CHARSET_LIST (attrs);
5846 return charset_list;
5850 /* Return raw-text or one of its subsidiaries that has the same
5851 eol_type as CODING-SYSTEM. */
5853 Lisp_Object
5854 raw_text_coding_system (Lisp_Object coding_system)
5856 Lisp_Object spec, attrs;
5857 Lisp_Object eol_type, raw_text_eol_type;
5859 if (NILP (coding_system))
5860 return Qraw_text;
5861 spec = CODING_SYSTEM_SPEC (coding_system);
5862 attrs = AREF (spec, 0);
5864 if (EQ (CODING_ATTR_TYPE (attrs), Qraw_text))
5865 return coding_system;
5867 eol_type = AREF (spec, 2);
5868 if (VECTORP (eol_type))
5869 return Qraw_text;
5870 spec = CODING_SYSTEM_SPEC (Qraw_text);
5871 raw_text_eol_type = AREF (spec, 2);
5872 return (EQ (eol_type, Qunix) ? AREF (raw_text_eol_type, 0)
5873 : EQ (eol_type, Qdos) ? AREF (raw_text_eol_type, 1)
5874 : AREF (raw_text_eol_type, 2));
5878 /* If CODING_SYSTEM doesn't specify end-of-line format, return one of
5879 the subsidiary that has the same eol-spec as PARENT (if it is not
5880 nil and specifies end-of-line format) or the system's setting
5881 (system_eol_type). */
5883 Lisp_Object
5884 coding_inherit_eol_type (Lisp_Object coding_system, Lisp_Object parent)
5886 Lisp_Object spec, eol_type;
5888 if (NILP (coding_system))
5889 coding_system = Qraw_text;
5890 spec = CODING_SYSTEM_SPEC (coding_system);
5891 eol_type = AREF (spec, 2);
5892 if (VECTORP (eol_type))
5894 Lisp_Object parent_eol_type;
5896 if (! NILP (parent))
5898 Lisp_Object parent_spec;
5900 parent_spec = CODING_SYSTEM_SPEC (parent);
5901 parent_eol_type = AREF (parent_spec, 2);
5902 if (VECTORP (parent_eol_type))
5903 parent_eol_type = system_eol_type;
5905 else
5906 parent_eol_type = system_eol_type;
5907 if (EQ (parent_eol_type, Qunix))
5908 coding_system = AREF (eol_type, 0);
5909 else if (EQ (parent_eol_type, Qdos))
5910 coding_system = AREF (eol_type, 1);
5911 else if (EQ (parent_eol_type, Qmac))
5912 coding_system = AREF (eol_type, 2);
5914 return coding_system;
5918 /* Check if text-conversion and eol-conversion of CODING_SYSTEM are
5919 decided for writing to a process. If not, complement them, and
5920 return a new coding system. */
5922 Lisp_Object
5923 complement_process_encoding_system (Lisp_Object coding_system)
5925 Lisp_Object coding_base = Qnil, eol_base = Qnil;
5926 Lisp_Object spec, attrs;
5927 int i;
5929 for (i = 0; i < 3; i++)
5931 if (i == 1)
5932 coding_system = CDR_SAFE (Vdefault_process_coding_system);
5933 else if (i == 2)
5934 coding_system = preferred_coding_system ();
5935 spec = CODING_SYSTEM_SPEC (coding_system);
5936 if (NILP (spec))
5937 continue;
5938 attrs = AREF (spec, 0);
5939 if (NILP (coding_base) && ! EQ (CODING_ATTR_TYPE (attrs), Qundecided))
5940 coding_base = CODING_ATTR_BASE_NAME (attrs);
5941 if (NILP (eol_base) && ! VECTORP (AREF (spec, 2)))
5942 eol_base = coding_system;
5943 if (! NILP (coding_base) && ! NILP (eol_base))
5944 break;
5947 if (i > 0)
5948 /* The original CODING_SYSTEM didn't specify text-conversion or
5949 eol-conversion. Be sure that we return a fully complemented
5950 coding system. */
5951 coding_system = coding_inherit_eol_type (coding_base, eol_base);
5952 return coding_system;
5956 /* Emacs has a mechanism to automatically detect a coding system if it
5957 is one of Emacs' internal format, ISO2022, SJIS, and BIG5. But,
5958 it's impossible to distinguish some coding systems accurately
5959 because they use the same range of codes. So, at first, coding
5960 systems are categorized into 7, those are:
5962 o coding-category-emacs-mule
5964 The category for a coding system which has the same code range
5965 as Emacs' internal format. Assigned the coding-system (Lisp
5966 symbol) `emacs-mule' by default.
5968 o coding-category-sjis
5970 The category for a coding system which has the same code range
5971 as SJIS. Assigned the coding-system (Lisp
5972 symbol) `japanese-shift-jis' by default.
5974 o coding-category-iso-7
5976 The category for a coding system which has the same code range
5977 as ISO2022 of 7-bit environment. This doesn't use any locking
5978 shift and single shift functions. This can encode/decode all
5979 charsets. Assigned the coding-system (Lisp symbol)
5980 `iso-2022-7bit' by default.
5982 o coding-category-iso-7-tight
5984 Same as coding-category-iso-7 except that this can
5985 encode/decode only the specified charsets.
5987 o coding-category-iso-8-1
5989 The category for a coding system which has the same code range
5990 as ISO2022 of 8-bit environment and graphic plane 1 used only
5991 for DIMENSION1 charset. This doesn't use any locking shift
5992 and single shift functions. Assigned the coding-system (Lisp
5993 symbol) `iso-latin-1' by default.
5995 o coding-category-iso-8-2
5997 The category for a coding system which has the same code range
5998 as ISO2022 of 8-bit environment and graphic plane 1 used only
5999 for DIMENSION2 charset. This doesn't use any locking shift
6000 and single shift functions. Assigned the coding-system (Lisp
6001 symbol) `japanese-iso-8bit' by default.
6003 o coding-category-iso-7-else
6005 The category for a coding system which has the same code range
6006 as ISO2022 of 7-bit environment but uses locking shift or
6007 single shift functions. Assigned the coding-system (Lisp
6008 symbol) `iso-2022-7bit-lock' by default.
6010 o coding-category-iso-8-else
6012 The category for a coding system which has the same code range
6013 as ISO2022 of 8-bit environment but uses locking shift or
6014 single shift functions. Assigned the coding-system (Lisp
6015 symbol) `iso-2022-8bit-ss2' by default.
6017 o coding-category-big5
6019 The category for a coding system which has the same code range
6020 as BIG5. Assigned the coding-system (Lisp symbol)
6021 `cn-big5' by default.
6023 o coding-category-utf-8
6025 The category for a coding system which has the same code range
6026 as UTF-8 (cf. RFC3629). Assigned the coding-system (Lisp
6027 symbol) `utf-8' by default.
6029 o coding-category-utf-16-be
6031 The category for a coding system in which a text has an
6032 Unicode signature (cf. Unicode Standard) in the order of BIG
6033 endian at the head. Assigned the coding-system (Lisp symbol)
6034 `utf-16-be' by default.
6036 o coding-category-utf-16-le
6038 The category for a coding system in which a text has an
6039 Unicode signature (cf. Unicode Standard) in the order of
6040 LITTLE endian at the head. Assigned the coding-system (Lisp
6041 symbol) `utf-16-le' by default.
6043 o coding-category-ccl
6045 The category for a coding system of which encoder/decoder is
6046 written in CCL programs. The default value is nil, i.e., no
6047 coding system is assigned.
6049 o coding-category-binary
6051 The category for a coding system not categorized in any of the
6052 above. Assigned the coding-system (Lisp symbol)
6053 `no-conversion' by default.
6055 Each of them is a Lisp symbol and the value is an actual
6056 `coding-system's (this is also a Lisp symbol) assigned by a user.
6057 What Emacs does actually is to detect a category of coding system.
6058 Then, it uses a `coding-system' assigned to it. If Emacs can't
6059 decide only one possible category, it selects a category of the
6060 highest priority. Priorities of categories are also specified by a
6061 user in a Lisp variable `coding-category-list'.
6065 #define EOL_SEEN_NONE 0
6066 #define EOL_SEEN_LF 1
6067 #define EOL_SEEN_CR 2
6068 #define EOL_SEEN_CRLF 4
6070 /* Detect how end-of-line of a text of length SRC_BYTES pointed by
6071 SOURCE is encoded. If CATEGORY is one of
6072 coding_category_utf_16_XXXX, assume that CR and LF are encoded by
6073 two-byte, else they are encoded by one-byte.
6075 Return one of EOL_SEEN_XXX. */
6077 #define MAX_EOL_CHECK_COUNT 3
6079 static int
6080 detect_eol (const unsigned char *source, ptrdiff_t src_bytes,
6081 enum coding_category category)
6083 const unsigned char *src = source, *src_end = src + src_bytes;
6084 unsigned char c;
6085 int total = 0;
6086 int eol_seen = EOL_SEEN_NONE;
6088 if ((1 << category) & CATEGORY_MASK_UTF_16)
6090 bool msb = category == (coding_category_utf_16_le
6091 | coding_category_utf_16_le_nosig);
6092 bool lsb = !msb;
6094 while (src + 1 < src_end)
6096 c = src[lsb];
6097 if (src[msb] == 0 && (c == '\n' || c == '\r'))
6099 int this_eol;
6101 if (c == '\n')
6102 this_eol = EOL_SEEN_LF;
6103 else if (src + 3 >= src_end
6104 || src[msb + 2] != 0
6105 || src[lsb + 2] != '\n')
6106 this_eol = EOL_SEEN_CR;
6107 else
6109 this_eol = EOL_SEEN_CRLF;
6110 src += 2;
6113 if (eol_seen == EOL_SEEN_NONE)
6114 /* This is the first end-of-line. */
6115 eol_seen = this_eol;
6116 else if (eol_seen != this_eol)
6118 /* The found type is different from what found before.
6119 Allow for stray ^M characters in DOS EOL files. */
6120 if ((eol_seen == EOL_SEEN_CR && this_eol == EOL_SEEN_CRLF)
6121 || (eol_seen == EOL_SEEN_CRLF
6122 && this_eol == EOL_SEEN_CR))
6123 eol_seen = EOL_SEEN_CRLF;
6124 else
6126 eol_seen = EOL_SEEN_LF;
6127 break;
6130 if (++total == MAX_EOL_CHECK_COUNT)
6131 break;
6133 src += 2;
6136 else
6137 while (src < src_end)
6139 c = *src++;
6140 if (c == '\n' || c == '\r')
6142 int this_eol;
6144 if (c == '\n')
6145 this_eol = EOL_SEEN_LF;
6146 else if (src >= src_end || *src != '\n')
6147 this_eol = EOL_SEEN_CR;
6148 else
6149 this_eol = EOL_SEEN_CRLF, src++;
6151 if (eol_seen == EOL_SEEN_NONE)
6152 /* This is the first end-of-line. */
6153 eol_seen = this_eol;
6154 else if (eol_seen != this_eol)
6156 /* The found type is different from what found before.
6157 Allow for stray ^M characters in DOS EOL files. */
6158 if ((eol_seen == EOL_SEEN_CR && this_eol == EOL_SEEN_CRLF)
6159 || (eol_seen == EOL_SEEN_CRLF && this_eol == EOL_SEEN_CR))
6160 eol_seen = EOL_SEEN_CRLF;
6161 else
6163 eol_seen = EOL_SEEN_LF;
6164 break;
6167 if (++total == MAX_EOL_CHECK_COUNT)
6168 break;
6171 return eol_seen;
6175 static Lisp_Object
6176 adjust_coding_eol_type (struct coding_system *coding, int eol_seen)
6178 Lisp_Object eol_type;
6180 eol_type = CODING_ID_EOL_TYPE (coding->id);
6181 if (eol_seen & EOL_SEEN_LF)
6183 coding->id = CODING_SYSTEM_ID (AREF (eol_type, 0));
6184 eol_type = Qunix;
6186 else if (eol_seen & EOL_SEEN_CRLF)
6188 coding->id = CODING_SYSTEM_ID (AREF (eol_type, 1));
6189 eol_type = Qdos;
6191 else if (eol_seen & EOL_SEEN_CR)
6193 coding->id = CODING_SYSTEM_ID (AREF (eol_type, 2));
6194 eol_type = Qmac;
6196 return eol_type;
6199 /* Detect how a text specified in CODING is encoded. If a coding
6200 system is detected, update fields of CODING by the detected coding
6201 system. */
6203 static void
6204 detect_coding (struct coding_system *coding)
6206 const unsigned char *src, *src_end;
6207 unsigned int saved_mode = coding->mode;
6209 coding->consumed = coding->consumed_char = 0;
6210 coding->produced = coding->produced_char = 0;
6211 coding_set_source (coding);
6213 src_end = coding->source + coding->src_bytes;
6214 coding->head_ascii = 0;
6216 /* If we have not yet decided the text encoding type, detect it
6217 now. */
6218 if (EQ (CODING_ATTR_TYPE (CODING_ID_ATTRS (coding->id)), Qundecided))
6220 int c, i;
6221 struct coding_detection_info detect_info;
6222 bool null_byte_found = 0, eight_bit_found = 0;
6224 detect_info.checked = detect_info.found = detect_info.rejected = 0;
6225 for (src = coding->source; src < src_end; src++)
6227 c = *src;
6228 if (c & 0x80)
6230 eight_bit_found = 1;
6231 if (null_byte_found)
6232 break;
6234 else if (c < 0x20)
6236 if ((c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO)
6237 && ! inhibit_iso_escape_detection
6238 && ! detect_info.checked)
6240 if (detect_coding_iso_2022 (coding, &detect_info))
6242 /* We have scanned the whole data. */
6243 if (! (detect_info.rejected & CATEGORY_MASK_ISO_7_ELSE))
6245 /* We didn't find an 8-bit code. We may
6246 have found a null-byte, but it's very
6247 rare that a binary file conforms to
6248 ISO-2022. */
6249 src = src_end;
6250 coding->head_ascii = src - coding->source;
6252 detect_info.rejected |= ~CATEGORY_MASK_ISO_ESCAPE;
6253 break;
6256 else if (! c && !inhibit_null_byte_detection)
6258 null_byte_found = 1;
6259 if (eight_bit_found)
6260 break;
6262 if (! eight_bit_found)
6263 coding->head_ascii++;
6265 else if (! eight_bit_found)
6266 coding->head_ascii++;
6269 if (null_byte_found || eight_bit_found
6270 || coding->head_ascii < coding->src_bytes
6271 || detect_info.found)
6273 enum coding_category category;
6274 struct coding_system *this;
6276 if (coding->head_ascii == coding->src_bytes)
6277 /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */
6278 for (i = 0; i < coding_category_raw_text; i++)
6280 category = coding_priorities[i];
6281 this = coding_categories + category;
6282 if (detect_info.found & (1 << category))
6283 break;
6285 else
6287 if (null_byte_found)
6289 detect_info.checked |= ~CATEGORY_MASK_UTF_16;
6290 detect_info.rejected |= ~CATEGORY_MASK_UTF_16;
6292 for (i = 0; i < coding_category_raw_text; i++)
6294 category = coding_priorities[i];
6295 this = coding_categories + category;
6296 /* Some of this->detector (e.g. detect_coding_sjis)
6297 require this information. */
6298 coding->id = this->id;
6299 if (this->id < 0)
6301 /* No coding system of this category is defined. */
6302 detect_info.rejected |= (1 << category);
6304 else if (category >= coding_category_raw_text)
6305 continue;
6306 else if (detect_info.checked & (1 << category))
6308 if (detect_info.found & (1 << category))
6309 break;
6311 else if ((*(this->detector)) (coding, &detect_info)
6312 && detect_info.found & (1 << category))
6314 if (category == coding_category_utf_16_auto)
6316 if (detect_info.found & CATEGORY_MASK_UTF_16_LE)
6317 category = coding_category_utf_16_le;
6318 else
6319 category = coding_category_utf_16_be;
6321 break;
6326 if (i < coding_category_raw_text)
6327 setup_coding_system (CODING_ID_NAME (this->id), coding);
6328 else if (null_byte_found)
6329 setup_coding_system (Qno_conversion, coding);
6330 else if ((detect_info.rejected & CATEGORY_MASK_ANY)
6331 == CATEGORY_MASK_ANY)
6332 setup_coding_system (Qraw_text, coding);
6333 else if (detect_info.rejected)
6334 for (i = 0; i < coding_category_raw_text; i++)
6335 if (! (detect_info.rejected & (1 << coding_priorities[i])))
6337 this = coding_categories + coding_priorities[i];
6338 setup_coding_system (CODING_ID_NAME (this->id), coding);
6339 break;
6343 else if (XINT (CODING_ATTR_CATEGORY (CODING_ID_ATTRS (coding->id)))
6344 == coding_category_utf_8_auto)
6346 Lisp_Object coding_systems;
6347 struct coding_detection_info detect_info;
6349 coding_systems
6350 = AREF (CODING_ID_ATTRS (coding->id), coding_attr_utf_bom);
6351 detect_info.found = detect_info.rejected = 0;
6352 for (src = coding->source; src < src_end; src++)
6354 if (*src & 0x80)
6355 break;
6357 coding->head_ascii = src - coding->source;
6358 if (CONSP (coding_systems)
6359 && detect_coding_utf_8 (coding, &detect_info))
6361 if (detect_info.found & CATEGORY_MASK_UTF_8_SIG)
6362 setup_coding_system (XCAR (coding_systems), coding);
6363 else
6364 setup_coding_system (XCDR (coding_systems), coding);
6367 else if (XINT (CODING_ATTR_CATEGORY (CODING_ID_ATTRS (coding->id)))
6368 == coding_category_utf_16_auto)
6370 Lisp_Object coding_systems;
6371 struct coding_detection_info detect_info;
6373 coding_systems
6374 = AREF (CODING_ID_ATTRS (coding->id), coding_attr_utf_bom);
6375 detect_info.found = detect_info.rejected = 0;
6376 coding->head_ascii = 0;
6377 if (CONSP (coding_systems)
6378 && detect_coding_utf_16 (coding, &detect_info))
6380 if (detect_info.found & CATEGORY_MASK_UTF_16_LE)
6381 setup_coding_system (XCAR (coding_systems), coding);
6382 else if (detect_info.found & CATEGORY_MASK_UTF_16_BE)
6383 setup_coding_system (XCDR (coding_systems), coding);
6386 coding->mode = saved_mode;
6390 static void
6391 decode_eol (struct coding_system *coding)
6393 Lisp_Object eol_type;
6394 unsigned char *p, *pbeg, *pend;
6396 eol_type = CODING_ID_EOL_TYPE (coding->id);
6397 if (EQ (eol_type, Qunix) || inhibit_eol_conversion)
6398 return;
6400 if (NILP (coding->dst_object))
6401 pbeg = coding->destination;
6402 else
6403 pbeg = BYTE_POS_ADDR (coding->dst_pos_byte);
6404 pend = pbeg + coding->produced;
6406 if (VECTORP (eol_type))
6408 int eol_seen = EOL_SEEN_NONE;
6410 for (p = pbeg; p < pend; p++)
6412 if (*p == '\n')
6413 eol_seen |= EOL_SEEN_LF;
6414 else if (*p == '\r')
6416 if (p + 1 < pend && *(p + 1) == '\n')
6418 eol_seen |= EOL_SEEN_CRLF;
6419 p++;
6421 else
6422 eol_seen |= EOL_SEEN_CR;
6425 /* Handle DOS-style EOLs in a file with stray ^M characters. */
6426 if ((eol_seen & EOL_SEEN_CRLF) != 0
6427 && (eol_seen & EOL_SEEN_CR) != 0
6428 && (eol_seen & EOL_SEEN_LF) == 0)
6429 eol_seen = EOL_SEEN_CRLF;
6430 else if (eol_seen != EOL_SEEN_NONE
6431 && eol_seen != EOL_SEEN_LF
6432 && eol_seen != EOL_SEEN_CRLF
6433 && eol_seen != EOL_SEEN_CR)
6434 eol_seen = EOL_SEEN_LF;
6435 if (eol_seen != EOL_SEEN_NONE)
6436 eol_type = adjust_coding_eol_type (coding, eol_seen);
6439 if (EQ (eol_type, Qmac))
6441 for (p = pbeg; p < pend; p++)
6442 if (*p == '\r')
6443 *p = '\n';
6445 else if (EQ (eol_type, Qdos))
6447 ptrdiff_t n = 0;
6449 if (NILP (coding->dst_object))
6451 /* Start deleting '\r' from the tail to minimize the memory
6452 movement. */
6453 for (p = pend - 2; p >= pbeg; p--)
6454 if (*p == '\r')
6456 memmove (p, p + 1, pend-- - p - 1);
6457 n++;
6460 else
6462 ptrdiff_t pos_byte = coding->dst_pos_byte;
6463 ptrdiff_t pos = coding->dst_pos;
6464 ptrdiff_t pos_end = pos + coding->produced_char - 1;
6466 while (pos < pos_end)
6468 p = BYTE_POS_ADDR (pos_byte);
6469 if (*p == '\r' && p[1] == '\n')
6471 del_range_2 (pos, pos_byte, pos + 1, pos_byte + 1, 0);
6472 n++;
6473 pos_end--;
6475 pos++;
6476 if (coding->dst_multibyte)
6477 pos_byte += BYTES_BY_CHAR_HEAD (*p);
6478 else
6479 pos_byte++;
6482 coding->produced -= n;
6483 coding->produced_char -= n;
6488 /* Return a translation table (or list of them) from coding system
6489 attribute vector ATTRS for encoding (if ENCODEP) or decoding (if
6490 not ENCODEP). */
6492 static Lisp_Object
6493 get_translation_table (Lisp_Object attrs, bool encodep, int *max_lookup)
6495 Lisp_Object standard, translation_table;
6496 Lisp_Object val;
6498 if (NILP (Venable_character_translation))
6500 if (max_lookup)
6501 *max_lookup = 0;
6502 return Qnil;
6504 if (encodep)
6505 translation_table = CODING_ATTR_ENCODE_TBL (attrs),
6506 standard = Vstandard_translation_table_for_encode;
6507 else
6508 translation_table = CODING_ATTR_DECODE_TBL (attrs),
6509 standard = Vstandard_translation_table_for_decode;
6510 if (NILP (translation_table))
6511 translation_table = standard;
6512 else
6514 if (SYMBOLP (translation_table))
6515 translation_table = Fget (translation_table, Qtranslation_table);
6516 else if (CONSP (translation_table))
6518 translation_table = Fcopy_sequence (translation_table);
6519 for (val = translation_table; CONSP (val); val = XCDR (val))
6520 if (SYMBOLP (XCAR (val)))
6521 XSETCAR (val, Fget (XCAR (val), Qtranslation_table));
6523 if (CHAR_TABLE_P (standard))
6525 if (CONSP (translation_table))
6526 translation_table = nconc2 (translation_table,
6527 Fcons (standard, Qnil));
6528 else
6529 translation_table = Fcons (translation_table,
6530 Fcons (standard, Qnil));
6534 if (max_lookup)
6536 *max_lookup = 1;
6537 if (CHAR_TABLE_P (translation_table)
6538 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (translation_table)) > 1)
6540 val = XCHAR_TABLE (translation_table)->extras[1];
6541 if (NATNUMP (val) && *max_lookup < XFASTINT (val))
6542 *max_lookup = XFASTINT (val);
6544 else if (CONSP (translation_table))
6546 Lisp_Object tail;
6548 for (tail = translation_table; CONSP (tail); tail = XCDR (tail))
6549 if (CHAR_TABLE_P (XCAR (tail))
6550 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (XCAR (tail))) > 1)
6552 Lisp_Object tailval = XCHAR_TABLE (XCAR (tail))->extras[1];
6553 if (NATNUMP (tailval) && *max_lookup < XFASTINT (tailval))
6554 *max_lookup = XFASTINT (tailval);
6558 return translation_table;
6561 #define LOOKUP_TRANSLATION_TABLE(table, c, trans) \
6562 do { \
6563 trans = Qnil; \
6564 if (CHAR_TABLE_P (table)) \
6566 trans = CHAR_TABLE_REF (table, c); \
6567 if (CHARACTERP (trans)) \
6568 c = XFASTINT (trans), trans = Qnil; \
6570 else if (CONSP (table)) \
6572 Lisp_Object tail; \
6574 for (tail = table; CONSP (tail); tail = XCDR (tail)) \
6575 if (CHAR_TABLE_P (XCAR (tail))) \
6577 trans = CHAR_TABLE_REF (XCAR (tail), c); \
6578 if (CHARACTERP (trans)) \
6579 c = XFASTINT (trans), trans = Qnil; \
6580 else if (! NILP (trans)) \
6581 break; \
6584 } while (0)
6587 /* Return a translation of character(s) at BUF according to TRANS.
6588 TRANS is TO-CHAR or ((FROM . TO) ...) where
6589 FROM = [FROM-CHAR ...], TO is TO-CHAR or [TO-CHAR ...].
6590 The return value is TO-CHAR or ([FROM-CHAR ...] . TO) if a
6591 translation is found, and Qnil if not found..
6592 If BUF is too short to lookup characters in FROM, return Qt. */
6594 static Lisp_Object
6595 get_translation (Lisp_Object trans, int *buf, int *buf_end)
6598 if (INTEGERP (trans))
6599 return trans;
6600 for (; CONSP (trans); trans = XCDR (trans))
6602 Lisp_Object val = XCAR (trans);
6603 Lisp_Object from = XCAR (val);
6604 ptrdiff_t len = ASIZE (from);
6605 ptrdiff_t i;
6607 for (i = 0; i < len; i++)
6609 if (buf + i == buf_end)
6610 return Qt;
6611 if (XINT (AREF (from, i)) != buf[i])
6612 break;
6614 if (i == len)
6615 return val;
6617 return Qnil;
6621 static int
6622 produce_chars (struct coding_system *coding, Lisp_Object translation_table,
6623 bool last_block)
6625 unsigned char *dst = coding->destination + coding->produced;
6626 unsigned char *dst_end = coding->destination + coding->dst_bytes;
6627 ptrdiff_t produced;
6628 ptrdiff_t produced_chars = 0;
6629 int carryover = 0;
6631 if (! coding->chars_at_source)
6633 /* Source characters are in coding->charbuf. */
6634 int *buf = coding->charbuf;
6635 int *buf_end = buf + coding->charbuf_used;
6637 if (EQ (coding->src_object, coding->dst_object))
6639 coding_set_source (coding);
6640 dst_end = ((unsigned char *) coding->source) + coding->consumed;
6643 while (buf < buf_end)
6645 int c = *buf;
6646 ptrdiff_t i;
6648 if (c >= 0)
6650 ptrdiff_t from_nchars = 1, to_nchars = 1;
6651 Lisp_Object trans = Qnil;
6653 LOOKUP_TRANSLATION_TABLE (translation_table, c, trans);
6654 if (! NILP (trans))
6656 trans = get_translation (trans, buf, buf_end);
6657 if (INTEGERP (trans))
6658 c = XINT (trans);
6659 else if (CONSP (trans))
6661 from_nchars = ASIZE (XCAR (trans));
6662 trans = XCDR (trans);
6663 if (INTEGERP (trans))
6664 c = XINT (trans);
6665 else
6667 to_nchars = ASIZE (trans);
6668 c = XINT (AREF (trans, 0));
6671 else if (EQ (trans, Qt) && ! last_block)
6672 break;
6675 if ((dst_end - dst) / MAX_MULTIBYTE_LENGTH < to_nchars)
6677 if (((min (PTRDIFF_MAX, SIZE_MAX) - (buf_end - buf))
6678 / MAX_MULTIBYTE_LENGTH)
6679 < to_nchars)
6680 memory_full (SIZE_MAX);
6681 dst = alloc_destination (coding,
6682 buf_end - buf
6683 + MAX_MULTIBYTE_LENGTH * to_nchars,
6684 dst);
6685 if (EQ (coding->src_object, coding->dst_object))
6687 coding_set_source (coding);
6688 dst_end = (((unsigned char *) coding->source)
6689 + coding->consumed);
6691 else
6692 dst_end = coding->destination + coding->dst_bytes;
6695 for (i = 0; i < to_nchars; i++)
6697 if (i > 0)
6698 c = XINT (AREF (trans, i));
6699 if (coding->dst_multibyte
6700 || ! CHAR_BYTE8_P (c))
6701 CHAR_STRING_ADVANCE_NO_UNIFY (c, dst);
6702 else
6703 *dst++ = CHAR_TO_BYTE8 (c);
6705 produced_chars += to_nchars;
6706 buf += from_nchars;
6708 else
6709 /* This is an annotation datum. (-C) is the length. */
6710 buf += -c;
6712 carryover = buf_end - buf;
6714 else
6716 /* Source characters are at coding->source. */
6717 const unsigned char *src = coding->source;
6718 const unsigned char *src_end = src + coding->consumed;
6720 if (EQ (coding->dst_object, coding->src_object))
6721 dst_end = (unsigned char *) src;
6722 if (coding->src_multibyte != coding->dst_multibyte)
6724 if (coding->src_multibyte)
6726 bool multibytep = 1;
6727 ptrdiff_t consumed_chars = 0;
6729 while (1)
6731 const unsigned char *src_base = src;
6732 int c;
6734 ONE_MORE_BYTE (c);
6735 if (dst == dst_end)
6737 if (EQ (coding->src_object, coding->dst_object))
6738 dst_end = (unsigned char *) src;
6739 if (dst == dst_end)
6741 ptrdiff_t offset = src - coding->source;
6743 dst = alloc_destination (coding, src_end - src + 1,
6744 dst);
6745 dst_end = coding->destination + coding->dst_bytes;
6746 coding_set_source (coding);
6747 src = coding->source + offset;
6748 src_end = coding->source + coding->consumed;
6749 if (EQ (coding->src_object, coding->dst_object))
6750 dst_end = (unsigned char *) src;
6753 *dst++ = c;
6754 produced_chars++;
6756 no_more_source:
6759 else
6760 while (src < src_end)
6762 bool multibytep = 1;
6763 int c = *src++;
6765 if (dst >= dst_end - 1)
6767 if (EQ (coding->src_object, coding->dst_object))
6768 dst_end = (unsigned char *) src;
6769 if (dst >= dst_end - 1)
6771 ptrdiff_t offset = src - coding->source;
6772 ptrdiff_t more_bytes;
6774 if (EQ (coding->src_object, coding->dst_object))
6775 more_bytes = ((src_end - src) / 2) + 2;
6776 else
6777 more_bytes = src_end - src + 2;
6778 dst = alloc_destination (coding, more_bytes, dst);
6779 dst_end = coding->destination + coding->dst_bytes;
6780 coding_set_source (coding);
6781 src = coding->source + offset;
6782 src_end = coding->source + coding->consumed;
6783 if (EQ (coding->src_object, coding->dst_object))
6784 dst_end = (unsigned char *) src;
6787 EMIT_ONE_BYTE (c);
6790 else
6792 if (!EQ (coding->src_object, coding->dst_object))
6794 ptrdiff_t require = coding->src_bytes - coding->dst_bytes;
6796 if (require > 0)
6798 ptrdiff_t offset = src - coding->source;
6800 dst = alloc_destination (coding, require, dst);
6801 coding_set_source (coding);
6802 src = coding->source + offset;
6803 src_end = coding->source + coding->consumed;
6806 produced_chars = coding->consumed_char;
6807 while (src < src_end)
6808 *dst++ = *src++;
6812 produced = dst - (coding->destination + coding->produced);
6813 if (BUFFERP (coding->dst_object) && produced_chars > 0)
6814 insert_from_gap (produced_chars, produced);
6815 coding->produced += produced;
6816 coding->produced_char += produced_chars;
6817 return carryover;
6820 /* Compose text in CODING->object according to the annotation data at
6821 CHARBUF. CHARBUF is an array:
6822 [ -LENGTH ANNOTATION_MASK NCHARS NBYTES METHOD [ COMPONENTS... ] ]
6825 static void
6826 produce_composition (struct coding_system *coding, int *charbuf, ptrdiff_t pos)
6828 int len;
6829 ptrdiff_t to;
6830 enum composition_method method;
6831 Lisp_Object components;
6833 len = -charbuf[0] - MAX_ANNOTATION_LENGTH;
6834 to = pos + charbuf[2];
6835 method = (enum composition_method) (charbuf[4]);
6837 if (method == COMPOSITION_RELATIVE)
6838 components = Qnil;
6839 else
6841 Lisp_Object args[MAX_COMPOSITION_COMPONENTS * 2 - 1];
6842 int i, j;
6844 if (method == COMPOSITION_WITH_RULE)
6845 len = charbuf[2] * 3 - 2;
6846 charbuf += MAX_ANNOTATION_LENGTH;
6847 /* charbuf = [ CHRA ... CHAR] or [ CHAR -2 RULE ... CHAR ] */
6848 for (i = j = 0; i < len && charbuf[i] != -1; i++, j++)
6850 if (charbuf[i] >= 0)
6851 args[j] = make_number (charbuf[i]);
6852 else
6854 i++;
6855 args[j] = make_number (charbuf[i] % 0x100);
6858 components = (i == j ? Fstring (j, args) : Fvector (j, args));
6860 compose_text (pos, to, components, Qnil, coding->dst_object);
6864 /* Put `charset' property on text in CODING->object according to
6865 the annotation data at CHARBUF. CHARBUF is an array:
6866 [ -LENGTH ANNOTATION_MASK NCHARS CHARSET-ID ]
6869 static void
6870 produce_charset (struct coding_system *coding, int *charbuf, ptrdiff_t pos)
6872 ptrdiff_t from = pos - charbuf[2];
6873 struct charset *charset = CHARSET_FROM_ID (charbuf[3]);
6875 Fput_text_property (make_number (from), make_number (pos),
6876 Qcharset, CHARSET_NAME (charset),
6877 coding->dst_object);
6881 #define CHARBUF_SIZE 0x4000
6883 #define ALLOC_CONVERSION_WORK_AREA(coding) \
6884 do { \
6885 coding->charbuf = SAFE_ALLOCA (CHARBUF_SIZE * sizeof (int)); \
6886 coding->charbuf_size = CHARBUF_SIZE; \
6887 } while (0)
6890 static void
6891 produce_annotation (struct coding_system *coding, ptrdiff_t pos)
6893 int *charbuf = coding->charbuf;
6894 int *charbuf_end = charbuf + coding->charbuf_used;
6896 if (NILP (coding->dst_object))
6897 return;
6899 while (charbuf < charbuf_end)
6901 if (*charbuf >= 0)
6902 pos++, charbuf++;
6903 else
6905 int len = -*charbuf;
6907 if (len > 2)
6908 switch (charbuf[1])
6910 case CODING_ANNOTATE_COMPOSITION_MASK:
6911 produce_composition (coding, charbuf, pos);
6912 break;
6913 case CODING_ANNOTATE_CHARSET_MASK:
6914 produce_charset (coding, charbuf, pos);
6915 break;
6917 charbuf += len;
6922 /* Decode the data at CODING->src_object into CODING->dst_object.
6923 CODING->src_object is a buffer, a string, or nil.
6924 CODING->dst_object is a buffer.
6926 If CODING->src_object is a buffer, it must be the current buffer.
6927 In this case, if CODING->src_pos is positive, it is a position of
6928 the source text in the buffer, otherwise, the source text is in the
6929 gap area of the buffer, and CODING->src_pos specifies the offset of
6930 the text from GPT (which must be the same as PT). If this is the
6931 same buffer as CODING->dst_object, CODING->src_pos must be
6932 negative.
6934 If CODING->src_object is a string, CODING->src_pos is an index to
6935 that string.
6937 If CODING->src_object is nil, CODING->source must already point to
6938 the non-relocatable memory area. In this case, CODING->src_pos is
6939 an offset from CODING->source.
6941 The decoded data is inserted at the current point of the buffer
6942 CODING->dst_object.
6945 static void
6946 decode_coding (struct coding_system *coding)
6948 Lisp_Object attrs;
6949 Lisp_Object undo_list;
6950 Lisp_Object translation_table;
6951 struct ccl_spec cclspec;
6952 int carryover;
6953 int i;
6955 USE_SAFE_ALLOCA;
6957 if (BUFFERP (coding->src_object)
6958 && coding->src_pos > 0
6959 && coding->src_pos < GPT
6960 && coding->src_pos + coding->src_chars > GPT)
6961 move_gap_both (coding->src_pos, coding->src_pos_byte);
6963 undo_list = Qt;
6964 if (BUFFERP (coding->dst_object))
6966 set_buffer_internal (XBUFFER (coding->dst_object));
6967 if (GPT != PT)
6968 move_gap_both (PT, PT_BYTE);
6970 /* We must disable undo_list in order to record the whole insert
6971 transaction via record_insert at the end. But doing so also
6972 disables the recording of the first change to the undo_list.
6973 Therefore we check for first change here and record it via
6974 record_first_change if needed. */
6975 if (MODIFF <= SAVE_MODIFF)
6976 record_first_change ();
6978 undo_list = BVAR (current_buffer, undo_list);
6979 bset_undo_list (current_buffer, Qt);
6982 coding->consumed = coding->consumed_char = 0;
6983 coding->produced = coding->produced_char = 0;
6984 coding->chars_at_source = 0;
6985 record_conversion_result (coding, CODING_RESULT_SUCCESS);
6986 coding->errors = 0;
6988 ALLOC_CONVERSION_WORK_AREA (coding);
6990 attrs = CODING_ID_ATTRS (coding->id);
6991 translation_table = get_translation_table (attrs, 0, NULL);
6993 carryover = 0;
6994 if (coding->decoder == decode_coding_ccl)
6996 coding->spec.ccl = &cclspec;
6997 setup_ccl_program (&cclspec.ccl, CODING_CCL_DECODER (coding));
7001 ptrdiff_t pos = coding->dst_pos + coding->produced_char;
7003 coding_set_source (coding);
7004 coding->annotated = 0;
7005 coding->charbuf_used = carryover;
7006 (*(coding->decoder)) (coding);
7007 coding_set_destination (coding);
7008 carryover = produce_chars (coding, translation_table, 0);
7009 if (coding->annotated)
7010 produce_annotation (coding, pos);
7011 for (i = 0; i < carryover; i++)
7012 coding->charbuf[i]
7013 = coding->charbuf[coding->charbuf_used - carryover + i];
7015 while (coding->result == CODING_RESULT_INSUFFICIENT_DST
7016 || (coding->consumed < coding->src_bytes
7017 && (coding->result == CODING_RESULT_SUCCESS
7018 || coding->result == CODING_RESULT_INVALID_SRC)));
7020 if (carryover > 0)
7022 coding_set_destination (coding);
7023 coding->charbuf_used = carryover;
7024 produce_chars (coding, translation_table, 1);
7027 coding->carryover_bytes = 0;
7028 if (coding->consumed < coding->src_bytes)
7030 int nbytes = coding->src_bytes - coding->consumed;
7031 const unsigned char *src;
7033 coding_set_source (coding);
7034 coding_set_destination (coding);
7035 src = coding->source + coding->consumed;
7037 if (coding->mode & CODING_MODE_LAST_BLOCK)
7039 /* Flush out unprocessed data as binary chars. We are sure
7040 that the number of data is less than the size of
7041 coding->charbuf. */
7042 coding->charbuf_used = 0;
7043 coding->chars_at_source = 0;
7045 while (nbytes-- > 0)
7047 int c = *src++;
7049 if (c & 0x80)
7050 c = BYTE8_TO_CHAR (c);
7051 coding->charbuf[coding->charbuf_used++] = c;
7053 produce_chars (coding, Qnil, 1);
7055 else
7057 /* Record unprocessed bytes in coding->carryover. We are
7058 sure that the number of data is less than the size of
7059 coding->carryover. */
7060 unsigned char *p = coding->carryover;
7062 if (nbytes > sizeof coding->carryover)
7063 nbytes = sizeof coding->carryover;
7064 coding->carryover_bytes = nbytes;
7065 while (nbytes-- > 0)
7066 *p++ = *src++;
7068 coding->consumed = coding->src_bytes;
7071 if (! EQ (CODING_ID_EOL_TYPE (coding->id), Qunix)
7072 && !inhibit_eol_conversion)
7073 decode_eol (coding);
7074 if (BUFFERP (coding->dst_object))
7076 bset_undo_list (current_buffer, undo_list);
7077 record_insert (coding->dst_pos, coding->produced_char);
7080 SAFE_FREE ();
7084 /* Extract an annotation datum from a composition starting at POS and
7085 ending before LIMIT of CODING->src_object (buffer or string), store
7086 the data in BUF, set *STOP to a starting position of the next
7087 composition (if any) or to LIMIT, and return the address of the
7088 next element of BUF.
7090 If such an annotation is not found, set *STOP to a starting
7091 position of a composition after POS (if any) or to LIMIT, and
7092 return BUF. */
7094 static int *
7095 handle_composition_annotation (ptrdiff_t pos, ptrdiff_t limit,
7096 struct coding_system *coding, int *buf,
7097 ptrdiff_t *stop)
7099 ptrdiff_t start, end;
7100 Lisp_Object prop;
7102 if (! find_composition (pos, limit, &start, &end, &prop, coding->src_object)
7103 || end > limit)
7104 *stop = limit;
7105 else if (start > pos)
7106 *stop = start;
7107 else
7109 if (start == pos)
7111 /* We found a composition. Store the corresponding
7112 annotation data in BUF. */
7113 int *head = buf;
7114 enum composition_method method = COMPOSITION_METHOD (prop);
7115 int nchars = COMPOSITION_LENGTH (prop);
7117 ADD_COMPOSITION_DATA (buf, nchars, 0, method);
7118 if (method != COMPOSITION_RELATIVE)
7120 Lisp_Object components;
7121 ptrdiff_t i, len, i_byte;
7123 components = COMPOSITION_COMPONENTS (prop);
7124 if (VECTORP (components))
7126 len = ASIZE (components);
7127 for (i = 0; i < len; i++)
7128 *buf++ = XINT (AREF (components, i));
7130 else if (STRINGP (components))
7132 len = SCHARS (components);
7133 i = i_byte = 0;
7134 while (i < len)
7136 FETCH_STRING_CHAR_ADVANCE (*buf, components, i, i_byte);
7137 buf++;
7140 else if (INTEGERP (components))
7142 len = 1;
7143 *buf++ = XINT (components);
7145 else if (CONSP (components))
7147 for (len = 0; CONSP (components);
7148 len++, components = XCDR (components))
7149 *buf++ = XINT (XCAR (components));
7151 else
7152 emacs_abort ();
7153 *head -= len;
7157 if (find_composition (end, limit, &start, &end, &prop,
7158 coding->src_object)
7159 && end <= limit)
7160 *stop = start;
7161 else
7162 *stop = limit;
7164 return buf;
7168 /* Extract an annotation datum from a text property `charset' at POS of
7169 CODING->src_object (buffer of string), store the data in BUF, set
7170 *STOP to the position where the value of `charset' property changes
7171 (limiting by LIMIT), and return the address of the next element of
7172 BUF.
7174 If the property value is nil, set *STOP to the position where the
7175 property value is non-nil (limiting by LIMIT), and return BUF. */
7177 static int *
7178 handle_charset_annotation (ptrdiff_t pos, ptrdiff_t limit,
7179 struct coding_system *coding, int *buf,
7180 ptrdiff_t *stop)
7182 Lisp_Object val, next;
7183 int id;
7185 val = Fget_text_property (make_number (pos), Qcharset, coding->src_object);
7186 if (! NILP (val) && CHARSETP (val))
7187 id = XINT (CHARSET_SYMBOL_ID (val));
7188 else
7189 id = -1;
7190 ADD_CHARSET_DATA (buf, 0, id);
7191 next = Fnext_single_property_change (make_number (pos), Qcharset,
7192 coding->src_object,
7193 make_number (limit));
7194 *stop = XINT (next);
7195 return buf;
7199 static void
7200 consume_chars (struct coding_system *coding, Lisp_Object translation_table,
7201 int max_lookup)
7203 int *buf = coding->charbuf;
7204 int *buf_end = coding->charbuf + coding->charbuf_size;
7205 const unsigned char *src = coding->source + coding->consumed;
7206 const unsigned char *src_end = coding->source + coding->src_bytes;
7207 ptrdiff_t pos = coding->src_pos + coding->consumed_char;
7208 ptrdiff_t end_pos = coding->src_pos + coding->src_chars;
7209 bool multibytep = coding->src_multibyte;
7210 Lisp_Object eol_type;
7211 int c;
7212 ptrdiff_t stop, stop_composition, stop_charset;
7213 int *lookup_buf = NULL;
7215 if (! NILP (translation_table))
7216 lookup_buf = alloca (sizeof (int) * max_lookup);
7218 eol_type = inhibit_eol_conversion ? Qunix : CODING_ID_EOL_TYPE (coding->id);
7219 if (VECTORP (eol_type))
7220 eol_type = Qunix;
7222 /* Note: composition handling is not yet implemented. */
7223 coding->common_flags &= ~CODING_ANNOTATE_COMPOSITION_MASK;
7225 if (NILP (coding->src_object))
7226 stop = stop_composition = stop_charset = end_pos;
7227 else
7229 if (coding->common_flags & CODING_ANNOTATE_COMPOSITION_MASK)
7230 stop = stop_composition = pos;
7231 else
7232 stop = stop_composition = end_pos;
7233 if (coding->common_flags & CODING_ANNOTATE_CHARSET_MASK)
7234 stop = stop_charset = pos;
7235 else
7236 stop_charset = end_pos;
7239 /* Compensate for CRLF and conversion. */
7240 buf_end -= 1 + MAX_ANNOTATION_LENGTH;
7241 while (buf < buf_end)
7243 Lisp_Object trans;
7245 if (pos == stop)
7247 if (pos == end_pos)
7248 break;
7249 if (pos == stop_composition)
7250 buf = handle_composition_annotation (pos, end_pos, coding,
7251 buf, &stop_composition);
7252 if (pos == stop_charset)
7253 buf = handle_charset_annotation (pos, end_pos, coding,
7254 buf, &stop_charset);
7255 stop = (stop_composition < stop_charset
7256 ? stop_composition : stop_charset);
7259 if (! multibytep)
7261 int bytes;
7263 if (coding->encoder == encode_coding_raw_text
7264 || coding->encoder == encode_coding_ccl)
7265 c = *src++, pos++;
7266 else if ((bytes = MULTIBYTE_LENGTH (src, src_end)) > 0)
7267 c = STRING_CHAR_ADVANCE_NO_UNIFY (src), pos += bytes;
7268 else
7269 c = BYTE8_TO_CHAR (*src), src++, pos++;
7271 else
7272 c = STRING_CHAR_ADVANCE_NO_UNIFY (src), pos++;
7273 if ((c == '\r') && (coding->mode & CODING_MODE_SELECTIVE_DISPLAY))
7274 c = '\n';
7275 if (! EQ (eol_type, Qunix))
7277 if (c == '\n')
7279 if (EQ (eol_type, Qdos))
7280 *buf++ = '\r';
7281 else
7282 c = '\r';
7286 trans = Qnil;
7287 LOOKUP_TRANSLATION_TABLE (translation_table, c, trans);
7288 if (NILP (trans))
7289 *buf++ = c;
7290 else
7292 ptrdiff_t from_nchars = 1, to_nchars = 1;
7293 int *lookup_buf_end;
7294 const unsigned char *p = src;
7295 int i;
7297 lookup_buf[0] = c;
7298 for (i = 1; i < max_lookup && p < src_end; i++)
7299 lookup_buf[i] = STRING_CHAR_ADVANCE (p);
7300 lookup_buf_end = lookup_buf + i;
7301 trans = get_translation (trans, lookup_buf, lookup_buf_end);
7302 if (INTEGERP (trans))
7303 c = XINT (trans);
7304 else if (CONSP (trans))
7306 from_nchars = ASIZE (XCAR (trans));
7307 trans = XCDR (trans);
7308 if (INTEGERP (trans))
7309 c = XINT (trans);
7310 else
7312 to_nchars = ASIZE (trans);
7313 if (buf_end - buf < to_nchars)
7314 break;
7315 c = XINT (AREF (trans, 0));
7318 else
7319 break;
7320 *buf++ = c;
7321 for (i = 1; i < to_nchars; i++)
7322 *buf++ = XINT (AREF (trans, i));
7323 for (i = 1; i < from_nchars; i++, pos++)
7324 src += MULTIBYTE_LENGTH_NO_CHECK (src);
7328 coding->consumed = src - coding->source;
7329 coding->consumed_char = pos - coding->src_pos;
7330 coding->charbuf_used = buf - coding->charbuf;
7331 coding->chars_at_source = 0;
7335 /* Encode the text at CODING->src_object into CODING->dst_object.
7336 CODING->src_object is a buffer or a string.
7337 CODING->dst_object is a buffer or nil.
7339 If CODING->src_object is a buffer, it must be the current buffer.
7340 In this case, if CODING->src_pos is positive, it is a position of
7341 the source text in the buffer, otherwise. the source text is in the
7342 gap area of the buffer, and coding->src_pos specifies the offset of
7343 the text from GPT (which must be the same as PT). If this is the
7344 same buffer as CODING->dst_object, CODING->src_pos must be
7345 negative and CODING should not have `pre-write-conversion'.
7347 If CODING->src_object is a string, CODING should not have
7348 `pre-write-conversion'.
7350 If CODING->dst_object is a buffer, the encoded data is inserted at
7351 the current point of that buffer.
7353 If CODING->dst_object is nil, the encoded data is placed at the
7354 memory area specified by CODING->destination. */
7356 static void
7357 encode_coding (struct coding_system *coding)
7359 Lisp_Object attrs;
7360 Lisp_Object translation_table;
7361 int max_lookup;
7362 struct ccl_spec cclspec;
7364 USE_SAFE_ALLOCA;
7366 attrs = CODING_ID_ATTRS (coding->id);
7367 if (coding->encoder == encode_coding_raw_text)
7368 translation_table = Qnil, max_lookup = 0;
7369 else
7370 translation_table = get_translation_table (attrs, 1, &max_lookup);
7372 if (BUFFERP (coding->dst_object))
7374 set_buffer_internal (XBUFFER (coding->dst_object));
7375 coding->dst_multibyte
7376 = ! NILP (BVAR (current_buffer, enable_multibyte_characters));
7379 coding->consumed = coding->consumed_char = 0;
7380 coding->produced = coding->produced_char = 0;
7381 record_conversion_result (coding, CODING_RESULT_SUCCESS);
7382 coding->errors = 0;
7384 ALLOC_CONVERSION_WORK_AREA (coding);
7386 if (coding->encoder == encode_coding_ccl)
7388 coding->spec.ccl = &cclspec;
7389 setup_ccl_program (&cclspec.ccl, CODING_CCL_ENCODER (coding));
7391 do {
7392 coding_set_source (coding);
7393 consume_chars (coding, translation_table, max_lookup);
7394 coding_set_destination (coding);
7395 (*(coding->encoder)) (coding);
7396 } while (coding->consumed_char < coding->src_chars);
7398 if (BUFFERP (coding->dst_object) && coding->produced_char > 0)
7399 insert_from_gap (coding->produced_char, coding->produced);
7401 SAFE_FREE ();
7405 /* Name (or base name) of work buffer for code conversion. */
7406 static Lisp_Object Vcode_conversion_workbuf_name;
7408 /* A working buffer used by the top level conversion. Once it is
7409 created, it is never destroyed. It has the name
7410 Vcode_conversion_workbuf_name. The other working buffers are
7411 destroyed after the use is finished, and their names are modified
7412 versions of Vcode_conversion_workbuf_name. */
7413 static Lisp_Object Vcode_conversion_reused_workbuf;
7415 /* True iff Vcode_conversion_reused_workbuf is already in use. */
7416 static bool reused_workbuf_in_use;
7419 /* Return a working buffer of code conversion. MULTIBYTE specifies the
7420 multibyteness of returning buffer. */
7422 static Lisp_Object
7423 make_conversion_work_buffer (bool multibyte)
7425 Lisp_Object name, workbuf;
7426 struct buffer *current;
7428 if (reused_workbuf_in_use)
7430 name = Fgenerate_new_buffer_name (Vcode_conversion_workbuf_name, Qnil);
7431 workbuf = Fget_buffer_create (name);
7433 else
7435 reused_workbuf_in_use = 1;
7436 if (NILP (Fbuffer_live_p (Vcode_conversion_reused_workbuf)))
7437 Vcode_conversion_reused_workbuf
7438 = Fget_buffer_create (Vcode_conversion_workbuf_name);
7439 workbuf = Vcode_conversion_reused_workbuf;
7441 current = current_buffer;
7442 set_buffer_internal (XBUFFER (workbuf));
7443 /* We can't allow modification hooks to run in the work buffer. For
7444 instance, directory_files_internal assumes that file decoding
7445 doesn't compile new regexps. */
7446 Fset (Fmake_local_variable (Qinhibit_modification_hooks), Qt);
7447 Ferase_buffer ();
7448 bset_undo_list (current_buffer, Qt);
7449 bset_enable_multibyte_characters (current_buffer, multibyte ? Qt : Qnil);
7450 set_buffer_internal (current);
7451 return workbuf;
7455 static Lisp_Object
7456 code_conversion_restore (Lisp_Object arg)
7458 Lisp_Object current, workbuf;
7459 struct gcpro gcpro1;
7461 GCPRO1 (arg);
7462 current = XCAR (arg);
7463 workbuf = XCDR (arg);
7464 if (! NILP (workbuf))
7466 if (EQ (workbuf, Vcode_conversion_reused_workbuf))
7467 reused_workbuf_in_use = 0;
7468 else
7469 Fkill_buffer (workbuf);
7471 set_buffer_internal (XBUFFER (current));
7472 UNGCPRO;
7473 return Qnil;
7476 Lisp_Object
7477 code_conversion_save (bool with_work_buf, bool multibyte)
7479 Lisp_Object workbuf = Qnil;
7481 if (with_work_buf)
7482 workbuf = make_conversion_work_buffer (multibyte);
7483 record_unwind_protect (code_conversion_restore,
7484 Fcons (Fcurrent_buffer (), workbuf));
7485 return workbuf;
7488 void
7489 decode_coding_gap (struct coding_system *coding,
7490 ptrdiff_t chars, ptrdiff_t bytes)
7492 ptrdiff_t count = SPECPDL_INDEX ();
7493 Lisp_Object attrs;
7495 coding->src_object = Fcurrent_buffer ();
7496 coding->src_chars = chars;
7497 coding->src_bytes = bytes;
7498 coding->src_pos = -chars;
7499 coding->src_pos_byte = -bytes;
7500 coding->src_multibyte = chars < bytes;
7501 coding->dst_object = coding->src_object;
7502 coding->dst_pos = PT;
7503 coding->dst_pos_byte = PT_BYTE;
7504 coding->dst_multibyte = ! NILP (BVAR (current_buffer, enable_multibyte_characters));
7506 if (CODING_REQUIRE_DETECTION (coding))
7507 detect_coding (coding);
7508 attrs = CODING_ID_ATTRS (coding->id);
7509 #ifndef CODING_DISABLE_ASCII_OPTIMIZATION
7510 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs))
7511 && NILP (CODING_ATTR_POST_READ (attrs))
7512 && NILP (get_translation_table (attrs, 0, NULL)))
7514 /* We can skip the conversion if all source bytes are ASCII. */
7515 if (coding->head_ascii < 0)
7517 /* We have not yet counted the number of ASCII bytes at the
7518 head of the source. Do it now. */
7519 const unsigned char *src, *src_end;
7521 coding_set_source (coding);
7522 src_end = coding->source + coding->src_bytes;
7523 for (src = coding->source; src < src_end; src++)
7525 if (*src & 0x80)
7526 break;
7528 coding->head_ascii = src - coding->source;
7530 if (coding->src_bytes == coding->head_ascii)
7532 /* No need of conversion. Use the data in the gap as is. */
7533 coding->produced_char = chars;
7534 coding->produced = bytes;
7535 adjust_after_replace (PT, PT_BYTE, Qnil, chars, bytes, 1);
7536 return;
7539 #endif /* not CODING_DISABLE_ASCII_OPTIMIZATION */
7540 code_conversion_save (0, 0);
7542 coding->mode |= CODING_MODE_LAST_BLOCK;
7543 current_buffer->text->inhibit_shrinking = 1;
7544 decode_coding (coding);
7545 current_buffer->text->inhibit_shrinking = 0;
7547 if (! NILP (CODING_ATTR_POST_READ (attrs)))
7549 ptrdiff_t prev_Z = Z, prev_Z_BYTE = Z_BYTE;
7550 Lisp_Object val;
7552 TEMP_SET_PT_BOTH (coding->dst_pos, coding->dst_pos_byte);
7553 val = call1 (CODING_ATTR_POST_READ (attrs),
7554 make_number (coding->produced_char));
7555 CHECK_NATNUM (val);
7556 coding->produced_char += Z - prev_Z;
7557 coding->produced += Z_BYTE - prev_Z_BYTE;
7560 unbind_to (count, Qnil);
7564 /* Decode the text in the range FROM/FROM_BYTE and TO/TO_BYTE in
7565 SRC_OBJECT into DST_OBJECT by coding context CODING.
7567 SRC_OBJECT is a buffer, a string, or Qnil.
7569 If it is a buffer, the text is at point of the buffer. FROM and TO
7570 are positions in the buffer.
7572 If it is a string, the text is at the beginning of the string.
7573 FROM and TO are indices to the string.
7575 If it is nil, the text is at coding->source. FROM and TO are
7576 indices to coding->source.
7578 DST_OBJECT is a buffer, Qt, or Qnil.
7580 If it is a buffer, the decoded text is inserted at point of the
7581 buffer. If the buffer is the same as SRC_OBJECT, the source text
7582 is deleted.
7584 If it is Qt, a string is made from the decoded text, and
7585 set in CODING->dst_object.
7587 If it is Qnil, the decoded text is stored at CODING->destination.
7588 The caller must allocate CODING->dst_bytes bytes at
7589 CODING->destination by xmalloc. If the decoded text is longer than
7590 CODING->dst_bytes, CODING->destination is relocated by xrealloc.
7593 void
7594 decode_coding_object (struct coding_system *coding,
7595 Lisp_Object src_object,
7596 ptrdiff_t from, ptrdiff_t from_byte,
7597 ptrdiff_t to, ptrdiff_t to_byte,
7598 Lisp_Object dst_object)
7600 ptrdiff_t count = SPECPDL_INDEX ();
7601 unsigned char *destination IF_LINT (= NULL);
7602 ptrdiff_t dst_bytes IF_LINT (= 0);
7603 ptrdiff_t chars = to - from;
7604 ptrdiff_t bytes = to_byte - from_byte;
7605 Lisp_Object attrs;
7606 ptrdiff_t saved_pt = -1, saved_pt_byte IF_LINT (= 0);
7607 bool need_marker_adjustment = 0;
7608 Lisp_Object old_deactivate_mark;
7610 old_deactivate_mark = Vdeactivate_mark;
7612 if (NILP (dst_object))
7614 destination = coding->destination;
7615 dst_bytes = coding->dst_bytes;
7618 coding->src_object = src_object;
7619 coding->src_chars = chars;
7620 coding->src_bytes = bytes;
7621 coding->src_multibyte = chars < bytes;
7623 if (STRINGP (src_object))
7625 coding->src_pos = from;
7626 coding->src_pos_byte = from_byte;
7628 else if (BUFFERP (src_object))
7630 set_buffer_internal (XBUFFER (src_object));
7631 if (from != GPT)
7632 move_gap_both (from, from_byte);
7633 if (EQ (src_object, dst_object))
7635 struct Lisp_Marker *tail;
7637 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
7639 tail->need_adjustment
7640 = tail->charpos == (tail->insertion_type ? from : to);
7641 need_marker_adjustment |= tail->need_adjustment;
7643 saved_pt = PT, saved_pt_byte = PT_BYTE;
7644 TEMP_SET_PT_BOTH (from, from_byte);
7645 current_buffer->text->inhibit_shrinking = 1;
7646 del_range_both (from, from_byte, to, to_byte, 1);
7647 coding->src_pos = -chars;
7648 coding->src_pos_byte = -bytes;
7650 else
7652 coding->src_pos = from;
7653 coding->src_pos_byte = from_byte;
7657 if (CODING_REQUIRE_DETECTION (coding))
7658 detect_coding (coding);
7659 attrs = CODING_ID_ATTRS (coding->id);
7661 if (EQ (dst_object, Qt)
7662 || (! NILP (CODING_ATTR_POST_READ (attrs))
7663 && NILP (dst_object)))
7665 coding->dst_multibyte = !CODING_FOR_UNIBYTE (coding);
7666 coding->dst_object = code_conversion_save (1, coding->dst_multibyte);
7667 coding->dst_pos = BEG;
7668 coding->dst_pos_byte = BEG_BYTE;
7670 else if (BUFFERP (dst_object))
7672 code_conversion_save (0, 0);
7673 coding->dst_object = dst_object;
7674 coding->dst_pos = BUF_PT (XBUFFER (dst_object));
7675 coding->dst_pos_byte = BUF_PT_BYTE (XBUFFER (dst_object));
7676 coding->dst_multibyte
7677 = ! NILP (BVAR (XBUFFER (dst_object), enable_multibyte_characters));
7679 else
7681 code_conversion_save (0, 0);
7682 coding->dst_object = Qnil;
7683 /* Most callers presume this will return a multibyte result, and they
7684 won't use `binary' or `raw-text' anyway, so let's not worry about
7685 CODING_FOR_UNIBYTE. */
7686 coding->dst_multibyte = 1;
7689 decode_coding (coding);
7691 if (BUFFERP (coding->dst_object))
7692 set_buffer_internal (XBUFFER (coding->dst_object));
7694 if (! NILP (CODING_ATTR_POST_READ (attrs)))
7696 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
7697 ptrdiff_t prev_Z = Z, prev_Z_BYTE = Z_BYTE;
7698 Lisp_Object val;
7700 TEMP_SET_PT_BOTH (coding->dst_pos, coding->dst_pos_byte);
7701 GCPRO5 (coding->src_object, coding->dst_object, src_object, dst_object,
7702 old_deactivate_mark);
7703 val = safe_call1 (CODING_ATTR_POST_READ (attrs),
7704 make_number (coding->produced_char));
7705 UNGCPRO;
7706 CHECK_NATNUM (val);
7707 coding->produced_char += Z - prev_Z;
7708 coding->produced += Z_BYTE - prev_Z_BYTE;
7711 if (EQ (dst_object, Qt))
7713 coding->dst_object = Fbuffer_string ();
7715 else if (NILP (dst_object) && BUFFERP (coding->dst_object))
7717 set_buffer_internal (XBUFFER (coding->dst_object));
7718 if (dst_bytes < coding->produced)
7720 eassert (coding->produced > 0);
7721 destination = xrealloc (destination, coding->produced);
7722 if (BEGV < GPT && GPT < BEGV + coding->produced_char)
7723 move_gap_both (BEGV, BEGV_BYTE);
7724 memcpy (destination, BEGV_ADDR, coding->produced);
7725 coding->destination = destination;
7729 if (saved_pt >= 0)
7731 /* This is the case of:
7732 (BUFFERP (src_object) && EQ (src_object, dst_object))
7733 As we have moved PT while replacing the original buffer
7734 contents, we must recover it now. */
7735 set_buffer_internal (XBUFFER (src_object));
7736 current_buffer->text->inhibit_shrinking = 0;
7737 if (saved_pt < from)
7738 TEMP_SET_PT_BOTH (saved_pt, saved_pt_byte);
7739 else if (saved_pt < from + chars)
7740 TEMP_SET_PT_BOTH (from, from_byte);
7741 else if (! NILP (BVAR (current_buffer, enable_multibyte_characters)))
7742 TEMP_SET_PT_BOTH (saved_pt + (coding->produced_char - chars),
7743 saved_pt_byte + (coding->produced - bytes));
7744 else
7745 TEMP_SET_PT_BOTH (saved_pt + (coding->produced - bytes),
7746 saved_pt_byte + (coding->produced - bytes));
7748 if (need_marker_adjustment)
7750 struct Lisp_Marker *tail;
7752 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
7753 if (tail->need_adjustment)
7755 tail->need_adjustment = 0;
7756 if (tail->insertion_type)
7758 tail->bytepos = from_byte;
7759 tail->charpos = from;
7761 else
7763 tail->bytepos = from_byte + coding->produced;
7764 tail->charpos
7765 = (NILP (BVAR (current_buffer, enable_multibyte_characters))
7766 ? tail->bytepos : from + coding->produced_char);
7772 Vdeactivate_mark = old_deactivate_mark;
7773 unbind_to (count, coding->dst_object);
7777 void
7778 encode_coding_object (struct coding_system *coding,
7779 Lisp_Object src_object,
7780 ptrdiff_t from, ptrdiff_t from_byte,
7781 ptrdiff_t to, ptrdiff_t to_byte,
7782 Lisp_Object dst_object)
7784 ptrdiff_t count = SPECPDL_INDEX ();
7785 ptrdiff_t chars = to - from;
7786 ptrdiff_t bytes = to_byte - from_byte;
7787 Lisp_Object attrs;
7788 ptrdiff_t saved_pt = -1, saved_pt_byte IF_LINT (= 0);
7789 bool need_marker_adjustment = 0;
7790 bool kill_src_buffer = 0;
7791 Lisp_Object old_deactivate_mark;
7793 old_deactivate_mark = Vdeactivate_mark;
7795 coding->src_object = src_object;
7796 coding->src_chars = chars;
7797 coding->src_bytes = bytes;
7798 coding->src_multibyte = chars < bytes;
7800 attrs = CODING_ID_ATTRS (coding->id);
7802 if (EQ (src_object, dst_object))
7804 struct Lisp_Marker *tail;
7806 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
7808 tail->need_adjustment
7809 = tail->charpos == (tail->insertion_type ? from : to);
7810 need_marker_adjustment |= tail->need_adjustment;
7814 if (! NILP (CODING_ATTR_PRE_WRITE (attrs)))
7816 coding->src_object = code_conversion_save (1, coding->src_multibyte);
7817 set_buffer_internal (XBUFFER (coding->src_object));
7818 if (STRINGP (src_object))
7819 insert_from_string (src_object, from, from_byte, chars, bytes, 0);
7820 else if (BUFFERP (src_object))
7821 insert_from_buffer (XBUFFER (src_object), from, chars, 0);
7822 else
7823 insert_1_both ((char *) coding->source + from, chars, bytes, 0, 0, 0);
7825 if (EQ (src_object, dst_object))
7827 set_buffer_internal (XBUFFER (src_object));
7828 saved_pt = PT, saved_pt_byte = PT_BYTE;
7829 del_range_both (from, from_byte, to, to_byte, 1);
7830 set_buffer_internal (XBUFFER (coding->src_object));
7834 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
7836 GCPRO5 (coding->src_object, coding->dst_object, src_object, dst_object,
7837 old_deactivate_mark);
7838 safe_call2 (CODING_ATTR_PRE_WRITE (attrs),
7839 make_number (BEG), make_number (Z));
7840 UNGCPRO;
7842 if (XBUFFER (coding->src_object) != current_buffer)
7843 kill_src_buffer = 1;
7844 coding->src_object = Fcurrent_buffer ();
7845 if (BEG != GPT)
7846 move_gap_both (BEG, BEG_BYTE);
7847 coding->src_chars = Z - BEG;
7848 coding->src_bytes = Z_BYTE - BEG_BYTE;
7849 coding->src_pos = BEG;
7850 coding->src_pos_byte = BEG_BYTE;
7851 coding->src_multibyte = Z < Z_BYTE;
7853 else if (STRINGP (src_object))
7855 code_conversion_save (0, 0);
7856 coding->src_pos = from;
7857 coding->src_pos_byte = from_byte;
7859 else if (BUFFERP (src_object))
7861 code_conversion_save (0, 0);
7862 set_buffer_internal (XBUFFER (src_object));
7863 if (EQ (src_object, dst_object))
7865 saved_pt = PT, saved_pt_byte = PT_BYTE;
7866 coding->src_object = del_range_1 (from, to, 1, 1);
7867 coding->src_pos = 0;
7868 coding->src_pos_byte = 0;
7870 else
7872 if (from < GPT && to >= GPT)
7873 move_gap_both (from, from_byte);
7874 coding->src_pos = from;
7875 coding->src_pos_byte = from_byte;
7878 else
7879 code_conversion_save (0, 0);
7881 if (BUFFERP (dst_object))
7883 coding->dst_object = dst_object;
7884 if (EQ (src_object, dst_object))
7886 coding->dst_pos = from;
7887 coding->dst_pos_byte = from_byte;
7889 else
7891 struct buffer *current = current_buffer;
7893 set_buffer_temp (XBUFFER (dst_object));
7894 coding->dst_pos = PT;
7895 coding->dst_pos_byte = PT_BYTE;
7896 move_gap_both (coding->dst_pos, coding->dst_pos_byte);
7897 set_buffer_temp (current);
7899 coding->dst_multibyte
7900 = ! NILP (BVAR (XBUFFER (dst_object), enable_multibyte_characters));
7902 else if (EQ (dst_object, Qt))
7904 ptrdiff_t dst_bytes = max (1, coding->src_chars);
7905 coding->dst_object = Qnil;
7906 coding->destination = xmalloc (dst_bytes);
7907 coding->dst_bytes = dst_bytes;
7908 coding->dst_multibyte = 0;
7910 else
7912 coding->dst_object = Qnil;
7913 coding->dst_multibyte = 0;
7916 encode_coding (coding);
7918 if (EQ (dst_object, Qt))
7920 if (BUFFERP (coding->dst_object))
7921 coding->dst_object = Fbuffer_string ();
7922 else
7924 coding->dst_object
7925 = make_unibyte_string ((char *) coding->destination,
7926 coding->produced);
7927 xfree (coding->destination);
7931 if (saved_pt >= 0)
7933 /* This is the case of:
7934 (BUFFERP (src_object) && EQ (src_object, dst_object))
7935 As we have moved PT while replacing the original buffer
7936 contents, we must recover it now. */
7937 set_buffer_internal (XBUFFER (src_object));
7938 if (saved_pt < from)
7939 TEMP_SET_PT_BOTH (saved_pt, saved_pt_byte);
7940 else if (saved_pt < from + chars)
7941 TEMP_SET_PT_BOTH (from, from_byte);
7942 else if (! NILP (BVAR (current_buffer, enable_multibyte_characters)))
7943 TEMP_SET_PT_BOTH (saved_pt + (coding->produced_char - chars),
7944 saved_pt_byte + (coding->produced - bytes));
7945 else
7946 TEMP_SET_PT_BOTH (saved_pt + (coding->produced - bytes),
7947 saved_pt_byte + (coding->produced - bytes));
7949 if (need_marker_adjustment)
7951 struct Lisp_Marker *tail;
7953 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
7954 if (tail->need_adjustment)
7956 tail->need_adjustment = 0;
7957 if (tail->insertion_type)
7959 tail->bytepos = from_byte;
7960 tail->charpos = from;
7962 else
7964 tail->bytepos = from_byte + coding->produced;
7965 tail->charpos
7966 = (NILP (BVAR (current_buffer, enable_multibyte_characters))
7967 ? tail->bytepos : from + coding->produced_char);
7973 if (kill_src_buffer)
7974 Fkill_buffer (coding->src_object);
7976 Vdeactivate_mark = old_deactivate_mark;
7977 unbind_to (count, Qnil);
7981 Lisp_Object
7982 preferred_coding_system (void)
7984 int id = coding_categories[coding_priorities[0]].id;
7986 return CODING_ID_NAME (id);
7989 #if defined (WINDOWSNT) || defined (CYGWIN)
7991 Lisp_Object
7992 from_unicode (Lisp_Object str)
7994 CHECK_STRING (str);
7995 if (!STRING_MULTIBYTE (str) &&
7996 SBYTES (str) & 1)
7998 str = Fsubstring (str, make_number (0), make_number (-1));
8001 return code_convert_string_norecord (str, Qutf_16le, 0);
8004 wchar_t *
8005 to_unicode (Lisp_Object str, Lisp_Object *buf)
8007 *buf = code_convert_string_norecord (str, Qutf_16le, 1);
8008 /* We need to make another copy (in addition to the one made by
8009 code_convert_string_norecord) to ensure that the final string is
8010 _doubly_ zero terminated --- that is, that the string is
8011 terminated by two zero bytes and one utf-16le null character.
8012 Because strings are already terminated with a single zero byte,
8013 we just add one additional zero. */
8014 str = make_uninit_string (SBYTES (*buf) + 1);
8015 memcpy (SDATA (str), SDATA (*buf), SBYTES (*buf));
8016 SDATA (str) [SBYTES (*buf)] = '\0';
8017 *buf = str;
8018 return WCSDATA (*buf);
8021 #endif /* WINDOWSNT || CYGWIN */
8024 #ifdef emacs
8025 /*** 8. Emacs Lisp library functions ***/
8027 DEFUN ("coding-system-p", Fcoding_system_p, Scoding_system_p, 1, 1, 0,
8028 doc: /* Return t if OBJECT is nil or a coding-system.
8029 See the documentation of `define-coding-system' for information
8030 about coding-system objects. */)
8031 (Lisp_Object object)
8033 if (NILP (object)
8034 || CODING_SYSTEM_ID (object) >= 0)
8035 return Qt;
8036 if (! SYMBOLP (object)
8037 || NILP (Fget (object, Qcoding_system_define_form)))
8038 return Qnil;
8039 return Qt;
8042 DEFUN ("read-non-nil-coding-system", Fread_non_nil_coding_system,
8043 Sread_non_nil_coding_system, 1, 1, 0,
8044 doc: /* Read a coding system from the minibuffer, prompting with string PROMPT. */)
8045 (Lisp_Object prompt)
8047 Lisp_Object val;
8050 val = Fcompleting_read (prompt, Vcoding_system_alist, Qnil,
8051 Qt, Qnil, Qcoding_system_history, Qnil, Qnil);
8053 while (SCHARS (val) == 0);
8054 return (Fintern (val, Qnil));
8057 DEFUN ("read-coding-system", Fread_coding_system, Sread_coding_system, 1, 2, 0,
8058 doc: /* Read a coding system from the minibuffer, prompting with string PROMPT.
8059 If the user enters null input, return second argument DEFAULT-CODING-SYSTEM.
8060 Ignores case when completing coding systems (all Emacs coding systems
8061 are lower-case). */)
8062 (Lisp_Object prompt, Lisp_Object default_coding_system)
8064 Lisp_Object val;
8065 ptrdiff_t count = SPECPDL_INDEX ();
8067 if (SYMBOLP (default_coding_system))
8068 default_coding_system = SYMBOL_NAME (default_coding_system);
8069 specbind (Qcompletion_ignore_case, Qt);
8070 val = Fcompleting_read (prompt, Vcoding_system_alist, Qnil,
8071 Qt, Qnil, Qcoding_system_history,
8072 default_coding_system, Qnil);
8073 unbind_to (count, Qnil);
8074 return (SCHARS (val) == 0 ? Qnil : Fintern (val, Qnil));
8077 DEFUN ("check-coding-system", Fcheck_coding_system, Scheck_coding_system,
8078 1, 1, 0,
8079 doc: /* Check validity of CODING-SYSTEM.
8080 If valid, return CODING-SYSTEM, else signal a `coding-system-error' error.
8081 It is valid if it is nil or a symbol defined as a coding system by the
8082 function `define-coding-system'. */)
8083 (Lisp_Object coding_system)
8085 Lisp_Object define_form;
8087 define_form = Fget (coding_system, Qcoding_system_define_form);
8088 if (! NILP (define_form))
8090 Fput (coding_system, Qcoding_system_define_form, Qnil);
8091 safe_eval (define_form);
8093 if (!NILP (Fcoding_system_p (coding_system)))
8094 return coding_system;
8095 xsignal1 (Qcoding_system_error, coding_system);
8099 /* Detect how the bytes at SRC of length SRC_BYTES are encoded. If
8100 HIGHEST, return the coding system of the highest
8101 priority among the detected coding systems. Otherwise return a
8102 list of detected coding systems sorted by their priorities. If
8103 MULTIBYTEP, it is assumed that the bytes are in correct
8104 multibyte form but contains only ASCII and eight-bit chars.
8105 Otherwise, the bytes are raw bytes.
8107 CODING-SYSTEM controls the detection as below:
8109 If it is nil, detect both text-format and eol-format. If the
8110 text-format part of CODING-SYSTEM is already specified
8111 (e.g. `iso-latin-1'), detect only eol-format. If the eol-format
8112 part of CODING-SYSTEM is already specified (e.g. `undecided-unix'),
8113 detect only text-format. */
8115 Lisp_Object
8116 detect_coding_system (const unsigned char *src,
8117 ptrdiff_t src_chars, ptrdiff_t src_bytes,
8118 bool highest, bool multibytep,
8119 Lisp_Object coding_system)
8121 const unsigned char *src_end = src + src_bytes;
8122 Lisp_Object attrs, eol_type;
8123 Lisp_Object val = Qnil;
8124 struct coding_system coding;
8125 ptrdiff_t id;
8126 struct coding_detection_info detect_info;
8127 enum coding_category base_category;
8128 bool null_byte_found = 0, eight_bit_found = 0;
8130 if (NILP (coding_system))
8131 coding_system = Qundecided;
8132 setup_coding_system (coding_system, &coding);
8133 attrs = CODING_ID_ATTRS (coding.id);
8134 eol_type = CODING_ID_EOL_TYPE (coding.id);
8135 coding_system = CODING_ATTR_BASE_NAME (attrs);
8137 coding.source = src;
8138 coding.src_chars = src_chars;
8139 coding.src_bytes = src_bytes;
8140 coding.src_multibyte = multibytep;
8141 coding.consumed = 0;
8142 coding.mode |= CODING_MODE_LAST_BLOCK;
8143 coding.head_ascii = 0;
8145 detect_info.checked = detect_info.found = detect_info.rejected = 0;
8147 /* At first, detect text-format if necessary. */
8148 base_category = XINT (CODING_ATTR_CATEGORY (attrs));
8149 if (base_category == coding_category_undecided)
8151 enum coding_category category IF_LINT (= 0);
8152 struct coding_system *this IF_LINT (= NULL);
8153 int c, i;
8155 /* Skip all ASCII bytes except for a few ISO2022 controls. */
8156 for (; src < src_end; src++)
8158 c = *src;
8159 if (c & 0x80)
8161 eight_bit_found = 1;
8162 if (null_byte_found)
8163 break;
8165 else if (c < 0x20)
8167 if ((c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO)
8168 && ! inhibit_iso_escape_detection
8169 && ! detect_info.checked)
8171 if (detect_coding_iso_2022 (&coding, &detect_info))
8173 /* We have scanned the whole data. */
8174 if (! (detect_info.rejected & CATEGORY_MASK_ISO_7_ELSE))
8176 /* We didn't find an 8-bit code. We may
8177 have found a null-byte, but it's very
8178 rare that a binary file confirm to
8179 ISO-2022. */
8180 src = src_end;
8181 coding.head_ascii = src - coding.source;
8183 detect_info.rejected |= ~CATEGORY_MASK_ISO_ESCAPE;
8184 break;
8187 else if (! c && !inhibit_null_byte_detection)
8189 null_byte_found = 1;
8190 if (eight_bit_found)
8191 break;
8193 if (! eight_bit_found)
8194 coding.head_ascii++;
8196 else if (! eight_bit_found)
8197 coding.head_ascii++;
8200 if (null_byte_found || eight_bit_found
8201 || coding.head_ascii < coding.src_bytes
8202 || detect_info.found)
8204 if (coding.head_ascii == coding.src_bytes)
8205 /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */
8206 for (i = 0; i < coding_category_raw_text; i++)
8208 category = coding_priorities[i];
8209 this = coding_categories + category;
8210 if (detect_info.found & (1 << category))
8211 break;
8213 else
8215 if (null_byte_found)
8217 detect_info.checked |= ~CATEGORY_MASK_UTF_16;
8218 detect_info.rejected |= ~CATEGORY_MASK_UTF_16;
8220 for (i = 0; i < coding_category_raw_text; i++)
8222 category = coding_priorities[i];
8223 this = coding_categories + category;
8225 if (this->id < 0)
8227 /* No coding system of this category is defined. */
8228 detect_info.rejected |= (1 << category);
8230 else if (category >= coding_category_raw_text)
8231 continue;
8232 else if (detect_info.checked & (1 << category))
8234 if (highest
8235 && (detect_info.found & (1 << category)))
8236 break;
8238 else if ((*(this->detector)) (&coding, &detect_info)
8239 && highest
8240 && (detect_info.found & (1 << category)))
8242 if (category == coding_category_utf_16_auto)
8244 if (detect_info.found & CATEGORY_MASK_UTF_16_LE)
8245 category = coding_category_utf_16_le;
8246 else
8247 category = coding_category_utf_16_be;
8249 break;
8255 if ((detect_info.rejected & CATEGORY_MASK_ANY) == CATEGORY_MASK_ANY
8256 || null_byte_found)
8258 detect_info.found = CATEGORY_MASK_RAW_TEXT;
8259 id = CODING_SYSTEM_ID (Qno_conversion);
8260 val = Fcons (make_number (id), Qnil);
8262 else if (! detect_info.rejected && ! detect_info.found)
8264 detect_info.found = CATEGORY_MASK_ANY;
8265 id = coding_categories[coding_category_undecided].id;
8266 val = Fcons (make_number (id), Qnil);
8268 else if (highest)
8270 if (detect_info.found)
8272 detect_info.found = 1 << category;
8273 val = Fcons (make_number (this->id), Qnil);
8275 else
8276 for (i = 0; i < coding_category_raw_text; i++)
8277 if (! (detect_info.rejected & (1 << coding_priorities[i])))
8279 detect_info.found = 1 << coding_priorities[i];
8280 id = coding_categories[coding_priorities[i]].id;
8281 val = Fcons (make_number (id), Qnil);
8282 break;
8285 else
8287 int mask = detect_info.rejected | detect_info.found;
8288 int found = 0;
8290 for (i = coding_category_raw_text - 1; i >= 0; i--)
8292 category = coding_priorities[i];
8293 if (! (mask & (1 << category)))
8295 found |= 1 << category;
8296 id = coding_categories[category].id;
8297 if (id >= 0)
8298 val = Fcons (make_number (id), val);
8301 for (i = coding_category_raw_text - 1; i >= 0; i--)
8303 category = coding_priorities[i];
8304 if (detect_info.found & (1 << category))
8306 id = coding_categories[category].id;
8307 val = Fcons (make_number (id), val);
8310 detect_info.found |= found;
8313 else if (base_category == coding_category_utf_8_auto)
8315 if (detect_coding_utf_8 (&coding, &detect_info))
8317 struct coding_system *this;
8319 if (detect_info.found & CATEGORY_MASK_UTF_8_SIG)
8320 this = coding_categories + coding_category_utf_8_sig;
8321 else
8322 this = coding_categories + coding_category_utf_8_nosig;
8323 val = Fcons (make_number (this->id), Qnil);
8326 else if (base_category == coding_category_utf_16_auto)
8328 if (detect_coding_utf_16 (&coding, &detect_info))
8330 struct coding_system *this;
8332 if (detect_info.found & CATEGORY_MASK_UTF_16_LE)
8333 this = coding_categories + coding_category_utf_16_le;
8334 else if (detect_info.found & CATEGORY_MASK_UTF_16_BE)
8335 this = coding_categories + coding_category_utf_16_be;
8336 else if (detect_info.rejected & CATEGORY_MASK_UTF_16_LE_NOSIG)
8337 this = coding_categories + coding_category_utf_16_be_nosig;
8338 else
8339 this = coding_categories + coding_category_utf_16_le_nosig;
8340 val = Fcons (make_number (this->id), Qnil);
8343 else
8345 detect_info.found = 1 << XINT (CODING_ATTR_CATEGORY (attrs));
8346 val = Fcons (make_number (coding.id), Qnil);
8349 /* Then, detect eol-format if necessary. */
8351 int normal_eol = -1, utf_16_be_eol = -1, utf_16_le_eol = -1;
8352 Lisp_Object tail;
8354 if (VECTORP (eol_type))
8356 if (detect_info.found & ~CATEGORY_MASK_UTF_16)
8358 if (null_byte_found)
8359 normal_eol = EOL_SEEN_LF;
8360 else
8361 normal_eol = detect_eol (coding.source, src_bytes,
8362 coding_category_raw_text);
8364 if (detect_info.found & (CATEGORY_MASK_UTF_16_BE
8365 | CATEGORY_MASK_UTF_16_BE_NOSIG))
8366 utf_16_be_eol = detect_eol (coding.source, src_bytes,
8367 coding_category_utf_16_be);
8368 if (detect_info.found & (CATEGORY_MASK_UTF_16_LE
8369 | CATEGORY_MASK_UTF_16_LE_NOSIG))
8370 utf_16_le_eol = detect_eol (coding.source, src_bytes,
8371 coding_category_utf_16_le);
8373 else
8375 if (EQ (eol_type, Qunix))
8376 normal_eol = utf_16_be_eol = utf_16_le_eol = EOL_SEEN_LF;
8377 else if (EQ (eol_type, Qdos))
8378 normal_eol = utf_16_be_eol = utf_16_le_eol = EOL_SEEN_CRLF;
8379 else
8380 normal_eol = utf_16_be_eol = utf_16_le_eol = EOL_SEEN_CR;
8383 for (tail = val; CONSP (tail); tail = XCDR (tail))
8385 enum coding_category category;
8386 int this_eol;
8388 id = XINT (XCAR (tail));
8389 attrs = CODING_ID_ATTRS (id);
8390 category = XINT (CODING_ATTR_CATEGORY (attrs));
8391 eol_type = CODING_ID_EOL_TYPE (id);
8392 if (VECTORP (eol_type))
8394 if (category == coding_category_utf_16_be
8395 || category == coding_category_utf_16_be_nosig)
8396 this_eol = utf_16_be_eol;
8397 else if (category == coding_category_utf_16_le
8398 || category == coding_category_utf_16_le_nosig)
8399 this_eol = utf_16_le_eol;
8400 else
8401 this_eol = normal_eol;
8403 if (this_eol == EOL_SEEN_LF)
8404 XSETCAR (tail, AREF (eol_type, 0));
8405 else if (this_eol == EOL_SEEN_CRLF)
8406 XSETCAR (tail, AREF (eol_type, 1));
8407 else if (this_eol == EOL_SEEN_CR)
8408 XSETCAR (tail, AREF (eol_type, 2));
8409 else
8410 XSETCAR (tail, CODING_ID_NAME (id));
8412 else
8413 XSETCAR (tail, CODING_ID_NAME (id));
8417 return (highest ? (CONSP (val) ? XCAR (val) : Qnil) : val);
8421 DEFUN ("detect-coding-region", Fdetect_coding_region, Sdetect_coding_region,
8422 2, 3, 0,
8423 doc: /* Detect coding system of the text in the region between START and END.
8424 Return a list of possible coding systems ordered by priority.
8425 The coding systems to try and their priorities follows what
8426 the function `coding-system-priority-list' (which see) returns.
8428 If only ASCII characters are found (except for such ISO-2022 control
8429 characters as ESC), it returns a list of single element `undecided'
8430 or its subsidiary coding system according to a detected end-of-line
8431 format.
8433 If optional argument HIGHEST is non-nil, return the coding system of
8434 highest priority. */)
8435 (Lisp_Object start, Lisp_Object end, Lisp_Object highest)
8437 ptrdiff_t from, to;
8438 ptrdiff_t from_byte, to_byte;
8440 validate_region (&start, &end);
8441 from = XINT (start), to = XINT (end);
8442 from_byte = CHAR_TO_BYTE (from);
8443 to_byte = CHAR_TO_BYTE (to);
8445 if (from < GPT && to >= GPT)
8446 move_gap_both (to, to_byte);
8448 return detect_coding_system (BYTE_POS_ADDR (from_byte),
8449 to - from, to_byte - from_byte,
8450 !NILP (highest),
8451 !NILP (BVAR (current_buffer
8452 , enable_multibyte_characters)),
8453 Qnil);
8456 DEFUN ("detect-coding-string", Fdetect_coding_string, Sdetect_coding_string,
8457 1, 2, 0,
8458 doc: /* Detect coding system of the text in STRING.
8459 Return a list of possible coding systems ordered by priority.
8460 The coding systems to try and their priorities follows what
8461 the function `coding-system-priority-list' (which see) returns.
8463 If only ASCII characters are found (except for such ISO-2022 control
8464 characters as ESC), it returns a list of single element `undecided'
8465 or its subsidiary coding system according to a detected end-of-line
8466 format.
8468 If optional argument HIGHEST is non-nil, return the coding system of
8469 highest priority. */)
8470 (Lisp_Object string, Lisp_Object highest)
8472 CHECK_STRING (string);
8474 return detect_coding_system (SDATA (string),
8475 SCHARS (string), SBYTES (string),
8476 !NILP (highest), STRING_MULTIBYTE (string),
8477 Qnil);
8481 static bool
8482 char_encodable_p (int c, Lisp_Object attrs)
8484 Lisp_Object tail;
8485 struct charset *charset;
8486 Lisp_Object translation_table;
8488 translation_table = CODING_ATTR_TRANS_TBL (attrs);
8489 if (! NILP (translation_table))
8490 c = translate_char (translation_table, c);
8491 for (tail = CODING_ATTR_CHARSET_LIST (attrs);
8492 CONSP (tail); tail = XCDR (tail))
8494 charset = CHARSET_FROM_ID (XINT (XCAR (tail)));
8495 if (CHAR_CHARSET_P (c, charset))
8496 break;
8498 return (! NILP (tail));
8502 /* Return a list of coding systems that safely encode the text between
8503 START and END. If EXCLUDE is non-nil, it is a list of coding
8504 systems not to check. The returned list doesn't contain any such
8505 coding systems. In any case, if the text contains only ASCII or is
8506 unibyte, return t. */
8508 DEFUN ("find-coding-systems-region-internal",
8509 Ffind_coding_systems_region_internal,
8510 Sfind_coding_systems_region_internal, 2, 3, 0,
8511 doc: /* Internal use only. */)
8512 (Lisp_Object start, Lisp_Object end, Lisp_Object exclude)
8514 Lisp_Object coding_attrs_list, safe_codings;
8515 ptrdiff_t start_byte, end_byte;
8516 const unsigned char *p, *pbeg, *pend;
8517 int c;
8518 Lisp_Object tail, elt, work_table;
8520 if (STRINGP (start))
8522 if (!STRING_MULTIBYTE (start)
8523 || SCHARS (start) == SBYTES (start))
8524 return Qt;
8525 start_byte = 0;
8526 end_byte = SBYTES (start);
8528 else
8530 CHECK_NUMBER_COERCE_MARKER (start);
8531 CHECK_NUMBER_COERCE_MARKER (end);
8532 if (XINT (start) < BEG || XINT (end) > Z || XINT (start) > XINT (end))
8533 args_out_of_range (start, end);
8534 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
8535 return Qt;
8536 start_byte = CHAR_TO_BYTE (XINT (start));
8537 end_byte = CHAR_TO_BYTE (XINT (end));
8538 if (XINT (end) - XINT (start) == end_byte - start_byte)
8539 return Qt;
8541 if (XINT (start) < GPT && XINT (end) > GPT)
8543 if ((GPT - XINT (start)) < (XINT (end) - GPT))
8544 move_gap_both (XINT (start), start_byte);
8545 else
8546 move_gap_both (XINT (end), end_byte);
8550 coding_attrs_list = Qnil;
8551 for (tail = Vcoding_system_list; CONSP (tail); tail = XCDR (tail))
8552 if (NILP (exclude)
8553 || NILP (Fmemq (XCAR (tail), exclude)))
8555 Lisp_Object attrs;
8557 attrs = AREF (CODING_SYSTEM_SPEC (XCAR (tail)), 0);
8558 if (EQ (XCAR (tail), CODING_ATTR_BASE_NAME (attrs))
8559 && ! EQ (CODING_ATTR_TYPE (attrs), Qundecided))
8561 ASET (attrs, coding_attr_trans_tbl,
8562 get_translation_table (attrs, 1, NULL));
8563 coding_attrs_list = Fcons (attrs, coding_attrs_list);
8567 if (STRINGP (start))
8568 p = pbeg = SDATA (start);
8569 else
8570 p = pbeg = BYTE_POS_ADDR (start_byte);
8571 pend = p + (end_byte - start_byte);
8573 while (p < pend && ASCII_BYTE_P (*p)) p++;
8574 while (p < pend && ASCII_BYTE_P (*(pend - 1))) pend--;
8576 work_table = Fmake_char_table (Qnil, Qnil);
8577 while (p < pend)
8579 if (ASCII_BYTE_P (*p))
8580 p++;
8581 else
8583 c = STRING_CHAR_ADVANCE (p);
8584 if (!NILP (char_table_ref (work_table, c)))
8585 /* This character was already checked. Ignore it. */
8586 continue;
8588 charset_map_loaded = 0;
8589 for (tail = coding_attrs_list; CONSP (tail);)
8591 elt = XCAR (tail);
8592 if (NILP (elt))
8593 tail = XCDR (tail);
8594 else if (char_encodable_p (c, elt))
8595 tail = XCDR (tail);
8596 else if (CONSP (XCDR (tail)))
8598 XSETCAR (tail, XCAR (XCDR (tail)));
8599 XSETCDR (tail, XCDR (XCDR (tail)));
8601 else
8603 XSETCAR (tail, Qnil);
8604 tail = XCDR (tail);
8607 if (charset_map_loaded)
8609 ptrdiff_t p_offset = p - pbeg, pend_offset = pend - pbeg;
8611 if (STRINGP (start))
8612 pbeg = SDATA (start);
8613 else
8614 pbeg = BYTE_POS_ADDR (start_byte);
8615 p = pbeg + p_offset;
8616 pend = pbeg + pend_offset;
8618 char_table_set (work_table, c, Qt);
8622 safe_codings = list2 (Qraw_text, Qno_conversion);
8623 for (tail = coding_attrs_list; CONSP (tail); tail = XCDR (tail))
8624 if (! NILP (XCAR (tail)))
8625 safe_codings = Fcons (CODING_ATTR_BASE_NAME (XCAR (tail)), safe_codings);
8627 return safe_codings;
8631 DEFUN ("unencodable-char-position", Funencodable_char_position,
8632 Sunencodable_char_position, 3, 5, 0,
8633 doc: /*
8634 Return position of first un-encodable character in a region.
8635 START and END specify the region and CODING-SYSTEM specifies the
8636 encoding to check. Return nil if CODING-SYSTEM does encode the region.
8638 If optional 4th argument COUNT is non-nil, it specifies at most how
8639 many un-encodable characters to search. In this case, the value is a
8640 list of positions.
8642 If optional 5th argument STRING is non-nil, it is a string to search
8643 for un-encodable characters. In that case, START and END are indexes
8644 to the string. */)
8645 (Lisp_Object start, Lisp_Object end, Lisp_Object coding_system, Lisp_Object count, Lisp_Object string)
8647 EMACS_INT n;
8648 struct coding_system coding;
8649 Lisp_Object attrs, charset_list, translation_table;
8650 Lisp_Object positions;
8651 ptrdiff_t from, to;
8652 const unsigned char *p, *stop, *pend;
8653 bool ascii_compatible;
8655 setup_coding_system (Fcheck_coding_system (coding_system), &coding);
8656 attrs = CODING_ID_ATTRS (coding.id);
8657 if (EQ (CODING_ATTR_TYPE (attrs), Qraw_text))
8658 return Qnil;
8659 ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs));
8660 charset_list = CODING_ATTR_CHARSET_LIST (attrs);
8661 translation_table = get_translation_table (attrs, 1, NULL);
8663 if (NILP (string))
8665 validate_region (&start, &end);
8666 from = XINT (start);
8667 to = XINT (end);
8668 if (NILP (BVAR (current_buffer, enable_multibyte_characters))
8669 || (ascii_compatible
8670 && (to - from) == (CHAR_TO_BYTE (to) - (CHAR_TO_BYTE (from)))))
8671 return Qnil;
8672 p = CHAR_POS_ADDR (from);
8673 pend = CHAR_POS_ADDR (to);
8674 if (from < GPT && to >= GPT)
8675 stop = GPT_ADDR;
8676 else
8677 stop = pend;
8679 else
8681 CHECK_STRING (string);
8682 CHECK_NATNUM (start);
8683 CHECK_NATNUM (end);
8684 if (! (XINT (start) <= XINT (end) && XINT (end) <= SCHARS (string)))
8685 args_out_of_range_3 (string, start, end);
8686 from = XINT (start);
8687 to = XINT (end);
8688 if (! STRING_MULTIBYTE (string))
8689 return Qnil;
8690 p = SDATA (string) + string_char_to_byte (string, from);
8691 stop = pend = SDATA (string) + string_char_to_byte (string, to);
8692 if (ascii_compatible && (to - from) == (pend - p))
8693 return Qnil;
8696 if (NILP (count))
8697 n = 1;
8698 else
8700 CHECK_NATNUM (count);
8701 n = XINT (count);
8704 positions = Qnil;
8705 charset_map_loaded = 0;
8706 while (1)
8708 int c;
8710 if (ascii_compatible)
8711 while (p < stop && ASCII_BYTE_P (*p))
8712 p++, from++;
8713 if (p >= stop)
8715 if (p >= pend)
8716 break;
8717 stop = pend;
8718 p = GAP_END_ADDR;
8721 c = STRING_CHAR_ADVANCE (p);
8722 if (! (ASCII_CHAR_P (c) && ascii_compatible)
8723 && ! char_charset (translate_char (translation_table, c),
8724 charset_list, NULL))
8726 positions = Fcons (make_number (from), positions);
8727 n--;
8728 if (n == 0)
8729 break;
8732 from++;
8733 if (charset_map_loaded && NILP (string))
8735 p = CHAR_POS_ADDR (from);
8736 pend = CHAR_POS_ADDR (to);
8737 if (from < GPT && to >= GPT)
8738 stop = GPT_ADDR;
8739 else
8740 stop = pend;
8741 charset_map_loaded = 0;
8745 return (NILP (count) ? Fcar (positions) : Fnreverse (positions));
8749 DEFUN ("check-coding-systems-region", Fcheck_coding_systems_region,
8750 Scheck_coding_systems_region, 3, 3, 0,
8751 doc: /* Check if the region is encodable by coding systems.
8753 START and END are buffer positions specifying the region.
8754 CODING-SYSTEM-LIST is a list of coding systems to check.
8756 The value is an alist ((CODING-SYSTEM POS0 POS1 ...) ...), where
8757 CODING-SYSTEM is a member of CODING-SYSTEM-LIST and can't encode the
8758 whole region, POS0, POS1, ... are buffer positions where non-encodable
8759 characters are found.
8761 If all coding systems in CODING-SYSTEM-LIST can encode the region, the
8762 value is nil.
8764 START may be a string. In that case, check if the string is
8765 encodable, and the value contains indices to the string instead of
8766 buffer positions. END is ignored.
8768 If the current buffer (or START if it is a string) is unibyte, the value
8769 is nil. */)
8770 (Lisp_Object start, Lisp_Object end, Lisp_Object coding_system_list)
8772 Lisp_Object list;
8773 ptrdiff_t start_byte, end_byte;
8774 ptrdiff_t pos;
8775 const unsigned char *p, *pbeg, *pend;
8776 int c;
8777 Lisp_Object tail, elt, attrs;
8779 if (STRINGP (start))
8781 if (!STRING_MULTIBYTE (start)
8782 || SCHARS (start) == SBYTES (start))
8783 return Qnil;
8784 start_byte = 0;
8785 end_byte = SBYTES (start);
8786 pos = 0;
8788 else
8790 CHECK_NUMBER_COERCE_MARKER (start);
8791 CHECK_NUMBER_COERCE_MARKER (end);
8792 if (XINT (start) < BEG || XINT (end) > Z || XINT (start) > XINT (end))
8793 args_out_of_range (start, end);
8794 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
8795 return Qnil;
8796 start_byte = CHAR_TO_BYTE (XINT (start));
8797 end_byte = CHAR_TO_BYTE (XINT (end));
8798 if (XINT (end) - XINT (start) == end_byte - start_byte)
8799 return Qnil;
8801 if (XINT (start) < GPT && XINT (end) > GPT)
8803 if ((GPT - XINT (start)) < (XINT (end) - GPT))
8804 move_gap_both (XINT (start), start_byte);
8805 else
8806 move_gap_both (XINT (end), end_byte);
8808 pos = XINT (start);
8811 list = Qnil;
8812 for (tail = coding_system_list; CONSP (tail); tail = XCDR (tail))
8814 elt = XCAR (tail);
8815 attrs = AREF (CODING_SYSTEM_SPEC (elt), 0);
8816 ASET (attrs, coding_attr_trans_tbl,
8817 get_translation_table (attrs, 1, NULL));
8818 list = Fcons (Fcons (elt, Fcons (attrs, Qnil)), list);
8821 if (STRINGP (start))
8822 p = pbeg = SDATA (start);
8823 else
8824 p = pbeg = BYTE_POS_ADDR (start_byte);
8825 pend = p + (end_byte - start_byte);
8827 while (p < pend && ASCII_BYTE_P (*p)) p++, pos++;
8828 while (p < pend && ASCII_BYTE_P (*(pend - 1))) pend--;
8830 while (p < pend)
8832 if (ASCII_BYTE_P (*p))
8833 p++;
8834 else
8836 c = STRING_CHAR_ADVANCE (p);
8838 charset_map_loaded = 0;
8839 for (tail = list; CONSP (tail); tail = XCDR (tail))
8841 elt = XCDR (XCAR (tail));
8842 if (! char_encodable_p (c, XCAR (elt)))
8843 XSETCDR (elt, Fcons (make_number (pos), XCDR (elt)));
8845 if (charset_map_loaded)
8847 ptrdiff_t p_offset = p - pbeg, pend_offset = pend - pbeg;
8849 if (STRINGP (start))
8850 pbeg = SDATA (start);
8851 else
8852 pbeg = BYTE_POS_ADDR (start_byte);
8853 p = pbeg + p_offset;
8854 pend = pbeg + pend_offset;
8857 pos++;
8860 tail = list;
8861 list = Qnil;
8862 for (; CONSP (tail); tail = XCDR (tail))
8864 elt = XCAR (tail);
8865 if (CONSP (XCDR (XCDR (elt))))
8866 list = Fcons (Fcons (XCAR (elt), Fnreverse (XCDR (XCDR (elt)))),
8867 list);
8870 return list;
8874 static Lisp_Object
8875 code_convert_region (Lisp_Object start, Lisp_Object end,
8876 Lisp_Object coding_system, Lisp_Object dst_object,
8877 bool encodep, bool norecord)
8879 struct coding_system coding;
8880 ptrdiff_t from, from_byte, to, to_byte;
8881 Lisp_Object src_object;
8883 if (NILP (coding_system))
8884 coding_system = Qno_conversion;
8885 else
8886 CHECK_CODING_SYSTEM (coding_system);
8887 src_object = Fcurrent_buffer ();
8888 if (NILP (dst_object))
8889 dst_object = src_object;
8890 else if (! EQ (dst_object, Qt))
8891 CHECK_BUFFER (dst_object);
8893 validate_region (&start, &end);
8894 from = XFASTINT (start);
8895 from_byte = CHAR_TO_BYTE (from);
8896 to = XFASTINT (end);
8897 to_byte = CHAR_TO_BYTE (to);
8899 setup_coding_system (coding_system, &coding);
8900 coding.mode |= CODING_MODE_LAST_BLOCK;
8902 if (encodep)
8903 encode_coding_object (&coding, src_object, from, from_byte, to, to_byte,
8904 dst_object);
8905 else
8906 decode_coding_object (&coding, src_object, from, from_byte, to, to_byte,
8907 dst_object);
8908 if (! norecord)
8909 Vlast_coding_system_used = CODING_ID_NAME (coding.id);
8911 return (BUFFERP (dst_object)
8912 ? make_number (coding.produced_char)
8913 : coding.dst_object);
8917 DEFUN ("decode-coding-region", Fdecode_coding_region, Sdecode_coding_region,
8918 3, 4, "r\nzCoding system: ",
8919 doc: /* Decode the current region from the specified coding system.
8920 When called from a program, takes four arguments:
8921 START, END, CODING-SYSTEM, and DESTINATION.
8922 START and END are buffer positions.
8924 Optional 4th arguments DESTINATION specifies where the decoded text goes.
8925 If nil, the region between START and END is replaced by the decoded text.
8926 If buffer, the decoded text is inserted in that buffer after point (point
8927 does not move).
8928 In those cases, the length of the decoded text is returned.
8929 If DESTINATION is t, the decoded text is returned.
8931 This function sets `last-coding-system-used' to the precise coding system
8932 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8933 not fully specified.) */)
8934 (Lisp_Object start, Lisp_Object end, Lisp_Object coding_system, Lisp_Object destination)
8936 return code_convert_region (start, end, coding_system, destination, 0, 0);
8939 DEFUN ("encode-coding-region", Fencode_coding_region, Sencode_coding_region,
8940 3, 4, "r\nzCoding system: ",
8941 doc: /* Encode the current region by specified coding system.
8942 When called from a program, takes four arguments:
8943 START, END, CODING-SYSTEM and DESTINATION.
8944 START and END are buffer positions.
8946 Optional 4th arguments DESTINATION specifies where the encoded text goes.
8947 If nil, the region between START and END is replace by the encoded text.
8948 If buffer, the encoded text is inserted in that buffer after point (point
8949 does not move).
8950 In those cases, the length of the encoded text is returned.
8951 If DESTINATION is t, the encoded text is returned.
8953 This function sets `last-coding-system-used' to the precise coding system
8954 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8955 not fully specified.) */)
8956 (Lisp_Object start, Lisp_Object end, Lisp_Object coding_system, Lisp_Object destination)
8958 return code_convert_region (start, end, coding_system, destination, 1, 0);
8961 Lisp_Object
8962 code_convert_string (Lisp_Object string, Lisp_Object coding_system,
8963 Lisp_Object dst_object, bool encodep, bool nocopy,
8964 bool norecord)
8966 struct coding_system coding;
8967 ptrdiff_t chars, bytes;
8969 CHECK_STRING (string);
8970 if (NILP (coding_system))
8972 if (! norecord)
8973 Vlast_coding_system_used = Qno_conversion;
8974 if (NILP (dst_object))
8975 return (nocopy ? Fcopy_sequence (string) : string);
8978 if (NILP (coding_system))
8979 coding_system = Qno_conversion;
8980 else
8981 CHECK_CODING_SYSTEM (coding_system);
8982 if (NILP (dst_object))
8983 dst_object = Qt;
8984 else if (! EQ (dst_object, Qt))
8985 CHECK_BUFFER (dst_object);
8987 setup_coding_system (coding_system, &coding);
8988 coding.mode |= CODING_MODE_LAST_BLOCK;
8989 chars = SCHARS (string);
8990 bytes = SBYTES (string);
8991 if (encodep)
8992 encode_coding_object (&coding, string, 0, 0, chars, bytes, dst_object);
8993 else
8994 decode_coding_object (&coding, string, 0, 0, chars, bytes, dst_object);
8995 if (! norecord)
8996 Vlast_coding_system_used = CODING_ID_NAME (coding.id);
8998 return (BUFFERP (dst_object)
8999 ? make_number (coding.produced_char)
9000 : coding.dst_object);
9004 /* Encode or decode STRING according to CODING_SYSTEM.
9005 Do not set Vlast_coding_system_used.
9007 This function is called only from macros DECODE_FILE and
9008 ENCODE_FILE, thus we ignore character composition. */
9010 Lisp_Object
9011 code_convert_string_norecord (Lisp_Object string, Lisp_Object coding_system,
9012 bool encodep)
9014 return code_convert_string (string, coding_system, Qt, encodep, 0, 1);
9018 DEFUN ("decode-coding-string", Fdecode_coding_string, Sdecode_coding_string,
9019 2, 4, 0,
9020 doc: /* Decode STRING which is encoded in CODING-SYSTEM, and return the result.
9022 Optional third arg NOCOPY non-nil means it is OK to return STRING itself
9023 if the decoding operation is trivial.
9025 Optional fourth arg BUFFER non-nil means that the decoded text is
9026 inserted in that buffer after point (point does not move). In this
9027 case, the return value is the length of the decoded text.
9029 This function sets `last-coding-system-used' to the precise coding system
9030 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
9031 not fully specified.) */)
9032 (Lisp_Object string, Lisp_Object coding_system, Lisp_Object nocopy, Lisp_Object buffer)
9034 return code_convert_string (string, coding_system, buffer,
9035 0, ! NILP (nocopy), 0);
9038 DEFUN ("encode-coding-string", Fencode_coding_string, Sencode_coding_string,
9039 2, 4, 0,
9040 doc: /* Encode STRING to CODING-SYSTEM, and return the result.
9042 Optional third arg NOCOPY non-nil means it is OK to return STRING
9043 itself if the encoding operation is trivial.
9045 Optional fourth arg BUFFER non-nil means that the encoded text is
9046 inserted in that buffer after point (point does not move). In this
9047 case, the return value is the length of the encoded text.
9049 This function sets `last-coding-system-used' to the precise coding system
9050 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
9051 not fully specified.) */)
9052 (Lisp_Object string, Lisp_Object coding_system, Lisp_Object nocopy, Lisp_Object buffer)
9054 return code_convert_string (string, coding_system, buffer,
9055 1, ! NILP (nocopy), 0);
9059 DEFUN ("decode-sjis-char", Fdecode_sjis_char, Sdecode_sjis_char, 1, 1, 0,
9060 doc: /* Decode a Japanese character which has CODE in shift_jis encoding.
9061 Return the corresponding character. */)
9062 (Lisp_Object code)
9064 Lisp_Object spec, attrs, val;
9065 struct charset *charset_roman, *charset_kanji, *charset_kana, *charset;
9066 EMACS_INT ch;
9067 int c;
9069 CHECK_NATNUM (code);
9070 ch = XFASTINT (code);
9071 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system, spec);
9072 attrs = AREF (spec, 0);
9074 if (ASCII_BYTE_P (ch)
9075 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)))
9076 return code;
9078 val = CODING_ATTR_CHARSET_LIST (attrs);
9079 charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
9080 charset_kana = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
9081 charset_kanji = CHARSET_FROM_ID (XINT (XCAR (val)));
9083 if (ch <= 0x7F)
9085 c = ch;
9086 charset = charset_roman;
9088 else if (ch >= 0xA0 && ch < 0xDF)
9090 c = ch - 0x80;
9091 charset = charset_kana;
9093 else
9095 EMACS_INT c1 = ch >> 8;
9096 int c2 = ch & 0xFF;
9098 if (c1 < 0x81 || (c1 > 0x9F && c1 < 0xE0) || c1 > 0xEF
9099 || c2 < 0x40 || c2 == 0x7F || c2 > 0xFC)
9100 error ("Invalid code: %"pI"d", ch);
9101 c = ch;
9102 SJIS_TO_JIS (c);
9103 charset = charset_kanji;
9105 c = DECODE_CHAR (charset, c);
9106 if (c < 0)
9107 error ("Invalid code: %"pI"d", ch);
9108 return make_number (c);
9112 DEFUN ("encode-sjis-char", Fencode_sjis_char, Sencode_sjis_char, 1, 1, 0,
9113 doc: /* Encode a Japanese character CH to shift_jis encoding.
9114 Return the corresponding code in SJIS. */)
9115 (Lisp_Object ch)
9117 Lisp_Object spec, attrs, charset_list;
9118 int c;
9119 struct charset *charset;
9120 unsigned code;
9122 CHECK_CHARACTER (ch);
9123 c = XFASTINT (ch);
9124 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system, spec);
9125 attrs = AREF (spec, 0);
9127 if (ASCII_CHAR_P (c)
9128 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)))
9129 return ch;
9131 charset_list = CODING_ATTR_CHARSET_LIST (attrs);
9132 charset = char_charset (c, charset_list, &code);
9133 if (code == CHARSET_INVALID_CODE (charset))
9134 error ("Can't encode by shift_jis encoding: %c", c);
9135 JIS_TO_SJIS (code);
9137 return make_number (code);
9140 DEFUN ("decode-big5-char", Fdecode_big5_char, Sdecode_big5_char, 1, 1, 0,
9141 doc: /* Decode a Big5 character which has CODE in BIG5 coding system.
9142 Return the corresponding character. */)
9143 (Lisp_Object code)
9145 Lisp_Object spec, attrs, val;
9146 struct charset *charset_roman, *charset_big5, *charset;
9147 EMACS_INT ch;
9148 int c;
9150 CHECK_NATNUM (code);
9151 ch = XFASTINT (code);
9152 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system, spec);
9153 attrs = AREF (spec, 0);
9155 if (ASCII_BYTE_P (ch)
9156 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)))
9157 return code;
9159 val = CODING_ATTR_CHARSET_LIST (attrs);
9160 charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
9161 charset_big5 = CHARSET_FROM_ID (XINT (XCAR (val)));
9163 if (ch <= 0x7F)
9165 c = ch;
9166 charset = charset_roman;
9168 else
9170 EMACS_INT b1 = ch >> 8;
9171 int b2 = ch & 0x7F;
9172 if (b1 < 0xA1 || b1 > 0xFE
9173 || b2 < 0x40 || (b2 > 0x7E && b2 < 0xA1) || b2 > 0xFE)
9174 error ("Invalid code: %"pI"d", ch);
9175 c = ch;
9176 charset = charset_big5;
9178 c = DECODE_CHAR (charset, c);
9179 if (c < 0)
9180 error ("Invalid code: %"pI"d", ch);
9181 return make_number (c);
9184 DEFUN ("encode-big5-char", Fencode_big5_char, Sencode_big5_char, 1, 1, 0,
9185 doc: /* Encode the Big5 character CH to BIG5 coding system.
9186 Return the corresponding character code in Big5. */)
9187 (Lisp_Object ch)
9189 Lisp_Object spec, attrs, charset_list;
9190 struct charset *charset;
9191 int c;
9192 unsigned code;
9194 CHECK_CHARACTER (ch);
9195 c = XFASTINT (ch);
9196 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system, spec);
9197 attrs = AREF (spec, 0);
9198 if (ASCII_CHAR_P (c)
9199 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)))
9200 return ch;
9202 charset_list = CODING_ATTR_CHARSET_LIST (attrs);
9203 charset = char_charset (c, charset_list, &code);
9204 if (code == CHARSET_INVALID_CODE (charset))
9205 error ("Can't encode by Big5 encoding: %c", c);
9207 return make_number (code);
9211 DEFUN ("set-terminal-coding-system-internal", Fset_terminal_coding_system_internal,
9212 Sset_terminal_coding_system_internal, 1, 2, 0,
9213 doc: /* Internal use only. */)
9214 (Lisp_Object coding_system, Lisp_Object terminal)
9216 struct terminal *term = get_terminal (terminal, 1);
9217 struct coding_system *terminal_coding = TERMINAL_TERMINAL_CODING (term);
9218 CHECK_SYMBOL (coding_system);
9219 setup_coding_system (Fcheck_coding_system (coding_system), terminal_coding);
9220 /* We had better not send unsafe characters to terminal. */
9221 terminal_coding->mode |= CODING_MODE_SAFE_ENCODING;
9222 /* Character composition should be disabled. */
9223 terminal_coding->common_flags &= ~CODING_ANNOTATE_COMPOSITION_MASK;
9224 terminal_coding->src_multibyte = 1;
9225 terminal_coding->dst_multibyte = 0;
9226 tset_charset_list
9227 (term, (terminal_coding->common_flags & CODING_REQUIRE_ENCODING_MASK
9228 ? coding_charset_list (terminal_coding)
9229 : Fcons (make_number (charset_ascii), Qnil)));
9230 return Qnil;
9233 DEFUN ("set-safe-terminal-coding-system-internal",
9234 Fset_safe_terminal_coding_system_internal,
9235 Sset_safe_terminal_coding_system_internal, 1, 1, 0,
9236 doc: /* Internal use only. */)
9237 (Lisp_Object coding_system)
9239 CHECK_SYMBOL (coding_system);
9240 setup_coding_system (Fcheck_coding_system (coding_system),
9241 &safe_terminal_coding);
9242 /* Character composition should be disabled. */
9243 safe_terminal_coding.common_flags &= ~CODING_ANNOTATE_COMPOSITION_MASK;
9244 safe_terminal_coding.src_multibyte = 1;
9245 safe_terminal_coding.dst_multibyte = 0;
9246 return Qnil;
9249 DEFUN ("terminal-coding-system", Fterminal_coding_system,
9250 Sterminal_coding_system, 0, 1, 0,
9251 doc: /* Return coding system specified for terminal output on the given terminal.
9252 TERMINAL may be a terminal object, a frame, or nil for the selected
9253 frame's terminal device. */)
9254 (Lisp_Object terminal)
9256 struct coding_system *terminal_coding
9257 = TERMINAL_TERMINAL_CODING (get_terminal (terminal, 1));
9258 Lisp_Object coding_system = CODING_ID_NAME (terminal_coding->id);
9260 /* For backward compatibility, return nil if it is `undecided'. */
9261 return (! EQ (coding_system, Qundecided) ? coding_system : Qnil);
9264 DEFUN ("set-keyboard-coding-system-internal", Fset_keyboard_coding_system_internal,
9265 Sset_keyboard_coding_system_internal, 1, 2, 0,
9266 doc: /* Internal use only. */)
9267 (Lisp_Object coding_system, Lisp_Object terminal)
9269 struct terminal *t = get_terminal (terminal, 1);
9270 CHECK_SYMBOL (coding_system);
9271 if (NILP (coding_system))
9272 coding_system = Qno_conversion;
9273 else
9274 Fcheck_coding_system (coding_system);
9275 setup_coding_system (coding_system, TERMINAL_KEYBOARD_CODING (t));
9276 /* Character composition should be disabled. */
9277 TERMINAL_KEYBOARD_CODING (t)->common_flags
9278 &= ~CODING_ANNOTATE_COMPOSITION_MASK;
9279 return Qnil;
9282 DEFUN ("keyboard-coding-system",
9283 Fkeyboard_coding_system, Skeyboard_coding_system, 0, 1, 0,
9284 doc: /* Return coding system specified for decoding keyboard input. */)
9285 (Lisp_Object terminal)
9287 return CODING_ID_NAME (TERMINAL_KEYBOARD_CODING
9288 (get_terminal (terminal, 1))->id);
9292 DEFUN ("find-operation-coding-system", Ffind_operation_coding_system,
9293 Sfind_operation_coding_system, 1, MANY, 0,
9294 doc: /* Choose a coding system for an operation based on the target name.
9295 The value names a pair of coding systems: (DECODING-SYSTEM . ENCODING-SYSTEM).
9296 DECODING-SYSTEM is the coding system to use for decoding
9297 \(in case OPERATION does decoding), and ENCODING-SYSTEM is the coding system
9298 for encoding (in case OPERATION does encoding).
9300 The first argument OPERATION specifies an I/O primitive:
9301 For file I/O, `insert-file-contents' or `write-region'.
9302 For process I/O, `call-process', `call-process-region', or `start-process'.
9303 For network I/O, `open-network-stream'.
9305 The remaining arguments should be the same arguments that were passed
9306 to the primitive. Depending on which primitive, one of those arguments
9307 is selected as the TARGET. For example, if OPERATION does file I/O,
9308 whichever argument specifies the file name is TARGET.
9310 TARGET has a meaning which depends on OPERATION:
9311 For file I/O, TARGET is a file name (except for the special case below).
9312 For process I/O, TARGET is a process name.
9313 For network I/O, TARGET is a service name or a port number.
9315 This function looks up what is specified for TARGET in
9316 `file-coding-system-alist', `process-coding-system-alist',
9317 or `network-coding-system-alist' depending on OPERATION.
9318 They may specify a coding system, a cons of coding systems,
9319 or a function symbol to call.
9320 In the last case, we call the function with one argument,
9321 which is a list of all the arguments given to this function.
9322 If the function can't decide a coding system, it can return
9323 `undecided' so that the normal code-detection is performed.
9325 If OPERATION is `insert-file-contents', the argument corresponding to
9326 TARGET may be a cons (FILENAME . BUFFER). In that case, FILENAME is a
9327 file name to look up, and BUFFER is a buffer that contains the file's
9328 contents (not yet decoded). If `file-coding-system-alist' specifies a
9329 function to call for FILENAME, that function should examine the
9330 contents of BUFFER instead of reading the file.
9332 usage: (find-operation-coding-system OPERATION ARGUMENTS...) */)
9333 (ptrdiff_t nargs, Lisp_Object *args)
9335 Lisp_Object operation, target_idx, target, val;
9336 register Lisp_Object chain;
9338 if (nargs < 2)
9339 error ("Too few arguments");
9340 operation = args[0];
9341 if (!SYMBOLP (operation)
9342 || (target_idx = Fget (operation, Qtarget_idx), !NATNUMP (target_idx)))
9343 error ("Invalid first argument");
9344 if (nargs <= 1 + XFASTINT (target_idx))
9345 error ("Too few arguments for operation `%s'",
9346 SDATA (SYMBOL_NAME (operation)));
9347 target = args[XFASTINT (target_idx) + 1];
9348 if (!(STRINGP (target)
9349 || (EQ (operation, Qinsert_file_contents) && CONSP (target)
9350 && STRINGP (XCAR (target)) && BUFFERP (XCDR (target)))
9351 || (EQ (operation, Qopen_network_stream) && INTEGERP (target))))
9352 error ("Invalid argument %"pI"d of operation `%s'",
9353 XFASTINT (target_idx) + 1, SDATA (SYMBOL_NAME (operation)));
9354 if (CONSP (target))
9355 target = XCAR (target);
9357 chain = ((EQ (operation, Qinsert_file_contents)
9358 || EQ (operation, Qwrite_region))
9359 ? Vfile_coding_system_alist
9360 : (EQ (operation, Qopen_network_stream)
9361 ? Vnetwork_coding_system_alist
9362 : Vprocess_coding_system_alist));
9363 if (NILP (chain))
9364 return Qnil;
9366 for (; CONSP (chain); chain = XCDR (chain))
9368 Lisp_Object elt;
9370 elt = XCAR (chain);
9371 if (CONSP (elt)
9372 && ((STRINGP (target)
9373 && STRINGP (XCAR (elt))
9374 && fast_string_match (XCAR (elt), target) >= 0)
9375 || (INTEGERP (target) && EQ (target, XCAR (elt)))))
9377 val = XCDR (elt);
9378 /* Here, if VAL is both a valid coding system and a valid
9379 function symbol, we return VAL as a coding system. */
9380 if (CONSP (val))
9381 return val;
9382 if (! SYMBOLP (val))
9383 return Qnil;
9384 if (! NILP (Fcoding_system_p (val)))
9385 return Fcons (val, val);
9386 if (! NILP (Ffboundp (val)))
9388 /* We use call1 rather than safe_call1
9389 so as to get bug reports about functions called here
9390 which don't handle the current interface. */
9391 val = call1 (val, Flist (nargs, args));
9392 if (CONSP (val))
9393 return val;
9394 if (SYMBOLP (val) && ! NILP (Fcoding_system_p (val)))
9395 return Fcons (val, val);
9397 return Qnil;
9400 return Qnil;
9403 DEFUN ("set-coding-system-priority", Fset_coding_system_priority,
9404 Sset_coding_system_priority, 0, MANY, 0,
9405 doc: /* Assign higher priority to the coding systems given as arguments.
9406 If multiple coding systems belong to the same category,
9407 all but the first one are ignored.
9409 usage: (set-coding-system-priority &rest coding-systems) */)
9410 (ptrdiff_t nargs, Lisp_Object *args)
9412 ptrdiff_t i, j;
9413 bool changed[coding_category_max];
9414 enum coding_category priorities[coding_category_max];
9416 memset (changed, 0, sizeof changed);
9418 for (i = j = 0; i < nargs; i++)
9420 enum coding_category category;
9421 Lisp_Object spec, attrs;
9423 CHECK_CODING_SYSTEM_GET_SPEC (args[i], spec);
9424 attrs = AREF (spec, 0);
9425 category = XINT (CODING_ATTR_CATEGORY (attrs));
9426 if (changed[category])
9427 /* Ignore this coding system because a coding system of the
9428 same category already had a higher priority. */
9429 continue;
9430 changed[category] = 1;
9431 priorities[j++] = category;
9432 if (coding_categories[category].id >= 0
9433 && ! EQ (args[i], CODING_ID_NAME (coding_categories[category].id)))
9434 setup_coding_system (args[i], &coding_categories[category]);
9435 Fset (AREF (Vcoding_category_table, category), args[i]);
9438 /* Now we have decided top J priorities. Reflect the order of the
9439 original priorities to the remaining priorities. */
9441 for (i = j, j = 0; i < coding_category_max; i++, j++)
9443 while (j < coding_category_max
9444 && changed[coding_priorities[j]])
9445 j++;
9446 if (j == coding_category_max)
9447 emacs_abort ();
9448 priorities[i] = coding_priorities[j];
9451 memcpy (coding_priorities, priorities, sizeof priorities);
9453 /* Update `coding-category-list'. */
9454 Vcoding_category_list = Qnil;
9455 for (i = coding_category_max; i-- > 0; )
9456 Vcoding_category_list
9457 = Fcons (AREF (Vcoding_category_table, priorities[i]),
9458 Vcoding_category_list);
9460 return Qnil;
9463 DEFUN ("coding-system-priority-list", Fcoding_system_priority_list,
9464 Scoding_system_priority_list, 0, 1, 0,
9465 doc: /* Return a list of coding systems ordered by their priorities.
9466 The list contains a subset of coding systems; i.e. coding systems
9467 assigned to each coding category (see `coding-category-list').
9469 HIGHESTP non-nil means just return the highest priority one. */)
9470 (Lisp_Object highestp)
9472 int i;
9473 Lisp_Object val;
9475 for (i = 0, val = Qnil; i < coding_category_max; i++)
9477 enum coding_category category = coding_priorities[i];
9478 int id = coding_categories[category].id;
9479 Lisp_Object attrs;
9481 if (id < 0)
9482 continue;
9483 attrs = CODING_ID_ATTRS (id);
9484 if (! NILP (highestp))
9485 return CODING_ATTR_BASE_NAME (attrs);
9486 val = Fcons (CODING_ATTR_BASE_NAME (attrs), val);
9488 return Fnreverse (val);
9491 static const char *const suffixes[] = { "-unix", "-dos", "-mac" };
9493 static Lisp_Object
9494 make_subsidiaries (Lisp_Object base)
9496 Lisp_Object subsidiaries;
9497 ptrdiff_t base_name_len = SBYTES (SYMBOL_NAME (base));
9498 char *buf = alloca (base_name_len + 6);
9499 int i;
9501 memcpy (buf, SDATA (SYMBOL_NAME (base)), base_name_len);
9502 subsidiaries = make_uninit_vector (3);
9503 for (i = 0; i < 3; i++)
9505 strcpy (buf + base_name_len, suffixes[i]);
9506 ASET (subsidiaries, i, intern (buf));
9508 return subsidiaries;
9512 DEFUN ("define-coding-system-internal", Fdefine_coding_system_internal,
9513 Sdefine_coding_system_internal, coding_arg_max, MANY, 0,
9514 doc: /* For internal use only.
9515 usage: (define-coding-system-internal ...) */)
9516 (ptrdiff_t nargs, Lisp_Object *args)
9518 Lisp_Object name;
9519 Lisp_Object spec_vec; /* [ ATTRS ALIASE EOL_TYPE ] */
9520 Lisp_Object attrs; /* Vector of attributes. */
9521 Lisp_Object eol_type;
9522 Lisp_Object aliases;
9523 Lisp_Object coding_type, charset_list, safe_charsets;
9524 enum coding_category category;
9525 Lisp_Object tail, val;
9526 int max_charset_id = 0;
9527 int i;
9529 if (nargs < coding_arg_max)
9530 goto short_args;
9532 attrs = Fmake_vector (make_number (coding_attr_last_index), Qnil);
9534 name = args[coding_arg_name];
9535 CHECK_SYMBOL (name);
9536 ASET (attrs, coding_attr_base_name, name);
9538 val = args[coding_arg_mnemonic];
9539 if (! STRINGP (val))
9540 CHECK_CHARACTER (val);
9541 ASET (attrs, coding_attr_mnemonic, val);
9543 coding_type = args[coding_arg_coding_type];
9544 CHECK_SYMBOL (coding_type);
9545 ASET (attrs, coding_attr_type, coding_type);
9547 charset_list = args[coding_arg_charset_list];
9548 if (SYMBOLP (charset_list))
9550 if (EQ (charset_list, Qiso_2022))
9552 if (! EQ (coding_type, Qiso_2022))
9553 error ("Invalid charset-list");
9554 charset_list = Viso_2022_charset_list;
9556 else if (EQ (charset_list, Qemacs_mule))
9558 if (! EQ (coding_type, Qemacs_mule))
9559 error ("Invalid charset-list");
9560 charset_list = Vemacs_mule_charset_list;
9562 for (tail = charset_list; CONSP (tail); tail = XCDR (tail))
9564 if (! RANGED_INTEGERP (0, XCAR (tail), INT_MAX - 1))
9565 error ("Invalid charset-list");
9566 if (max_charset_id < XFASTINT (XCAR (tail)))
9567 max_charset_id = XFASTINT (XCAR (tail));
9570 else
9572 charset_list = Fcopy_sequence (charset_list);
9573 for (tail = charset_list; CONSP (tail); tail = XCDR (tail))
9575 struct charset *charset;
9577 val = XCAR (tail);
9578 CHECK_CHARSET_GET_CHARSET (val, charset);
9579 if (EQ (coding_type, Qiso_2022)
9580 ? CHARSET_ISO_FINAL (charset) < 0
9581 : EQ (coding_type, Qemacs_mule)
9582 ? CHARSET_EMACS_MULE_ID (charset) < 0
9583 : 0)
9584 error ("Can't handle charset `%s'",
9585 SDATA (SYMBOL_NAME (CHARSET_NAME (charset))));
9587 XSETCAR (tail, make_number (charset->id));
9588 if (max_charset_id < charset->id)
9589 max_charset_id = charset->id;
9592 ASET (attrs, coding_attr_charset_list, charset_list);
9594 safe_charsets = make_uninit_string (max_charset_id + 1);
9595 memset (SDATA (safe_charsets), 255, max_charset_id + 1);
9596 for (tail = charset_list; CONSP (tail); tail = XCDR (tail))
9597 SSET (safe_charsets, XFASTINT (XCAR (tail)), 0);
9598 ASET (attrs, coding_attr_safe_charsets, safe_charsets);
9600 ASET (attrs, coding_attr_ascii_compat, args[coding_arg_ascii_compatible_p]);
9602 val = args[coding_arg_decode_translation_table];
9603 if (! CHAR_TABLE_P (val) && ! CONSP (val))
9604 CHECK_SYMBOL (val);
9605 ASET (attrs, coding_attr_decode_tbl, val);
9607 val = args[coding_arg_encode_translation_table];
9608 if (! CHAR_TABLE_P (val) && ! CONSP (val))
9609 CHECK_SYMBOL (val);
9610 ASET (attrs, coding_attr_encode_tbl, val);
9612 val = args[coding_arg_post_read_conversion];
9613 CHECK_SYMBOL (val);
9614 ASET (attrs, coding_attr_post_read, val);
9616 val = args[coding_arg_pre_write_conversion];
9617 CHECK_SYMBOL (val);
9618 ASET (attrs, coding_attr_pre_write, val);
9620 val = args[coding_arg_default_char];
9621 if (NILP (val))
9622 ASET (attrs, coding_attr_default_char, make_number (' '));
9623 else
9625 CHECK_CHARACTER (val);
9626 ASET (attrs, coding_attr_default_char, val);
9629 val = args[coding_arg_for_unibyte];
9630 ASET (attrs, coding_attr_for_unibyte, NILP (val) ? Qnil : Qt);
9632 val = args[coding_arg_plist];
9633 CHECK_LIST (val);
9634 ASET (attrs, coding_attr_plist, val);
9636 if (EQ (coding_type, Qcharset))
9638 /* Generate a lisp vector of 256 elements. Each element is nil,
9639 integer, or a list of charset IDs.
9641 If Nth element is nil, the byte code N is invalid in this
9642 coding system.
9644 If Nth element is a number NUM, N is the first byte of a
9645 charset whose ID is NUM.
9647 If Nth element is a list of charset IDs, N is the first byte
9648 of one of them. The list is sorted by dimensions of the
9649 charsets. A charset of smaller dimension comes first. */
9650 val = Fmake_vector (make_number (256), Qnil);
9652 for (tail = charset_list; CONSP (tail); tail = XCDR (tail))
9654 struct charset *charset = CHARSET_FROM_ID (XFASTINT (XCAR (tail)));
9655 int dim = CHARSET_DIMENSION (charset);
9656 int idx = (dim - 1) * 4;
9658 if (CHARSET_ASCII_COMPATIBLE_P (charset))
9659 ASET (attrs, coding_attr_ascii_compat, Qt);
9661 for (i = charset->code_space[idx];
9662 i <= charset->code_space[idx + 1]; i++)
9664 Lisp_Object tmp, tmp2;
9665 int dim2;
9667 tmp = AREF (val, i);
9668 if (NILP (tmp))
9669 tmp = XCAR (tail);
9670 else if (NUMBERP (tmp))
9672 dim2 = CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (tmp)));
9673 if (dim < dim2)
9674 tmp = Fcons (XCAR (tail), Fcons (tmp, Qnil));
9675 else
9676 tmp = Fcons (tmp, Fcons (XCAR (tail), Qnil));
9678 else
9680 for (tmp2 = tmp; CONSP (tmp2); tmp2 = XCDR (tmp2))
9682 dim2 = CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (XCAR (tmp2))));
9683 if (dim < dim2)
9684 break;
9686 if (NILP (tmp2))
9687 tmp = nconc2 (tmp, Fcons (XCAR (tail), Qnil));
9688 else
9690 XSETCDR (tmp2, Fcons (XCAR (tmp2), XCDR (tmp2)));
9691 XSETCAR (tmp2, XCAR (tail));
9694 ASET (val, i, tmp);
9697 ASET (attrs, coding_attr_charset_valids, val);
9698 category = coding_category_charset;
9700 else if (EQ (coding_type, Qccl))
9702 Lisp_Object valids;
9704 if (nargs < coding_arg_ccl_max)
9705 goto short_args;
9707 val = args[coding_arg_ccl_decoder];
9708 CHECK_CCL_PROGRAM (val);
9709 if (VECTORP (val))
9710 val = Fcopy_sequence (val);
9711 ASET (attrs, coding_attr_ccl_decoder, val);
9713 val = args[coding_arg_ccl_encoder];
9714 CHECK_CCL_PROGRAM (val);
9715 if (VECTORP (val))
9716 val = Fcopy_sequence (val);
9717 ASET (attrs, coding_attr_ccl_encoder, val);
9719 val = args[coding_arg_ccl_valids];
9720 valids = Fmake_string (make_number (256), make_number (0));
9721 for (tail = val; CONSP (tail); tail = XCDR (tail))
9723 int from, to;
9725 val = XCAR (tail);
9726 if (INTEGERP (val))
9728 if (! (0 <= XINT (val) && XINT (val) <= 255))
9729 args_out_of_range_3 (val, make_number (0), make_number (255));
9730 from = to = XINT (val);
9732 else
9734 CHECK_CONS (val);
9735 CHECK_NATNUM_CAR (val);
9736 CHECK_NUMBER_CDR (val);
9737 if (XINT (XCAR (val)) > 255)
9738 args_out_of_range_3 (XCAR (val),
9739 make_number (0), make_number (255));
9740 from = XINT (XCAR (val));
9741 if (! (from <= XINT (XCDR (val)) && XINT (XCDR (val)) <= 255))
9742 args_out_of_range_3 (XCDR (val),
9743 XCAR (val), make_number (255));
9744 to = XINT (XCDR (val));
9746 for (i = from; i <= to; i++)
9747 SSET (valids, i, 1);
9749 ASET (attrs, coding_attr_ccl_valids, valids);
9751 category = coding_category_ccl;
9753 else if (EQ (coding_type, Qutf_16))
9755 Lisp_Object bom, endian;
9757 ASET (attrs, coding_attr_ascii_compat, Qnil);
9759 if (nargs < coding_arg_utf16_max)
9760 goto short_args;
9762 bom = args[coding_arg_utf16_bom];
9763 if (! NILP (bom) && ! EQ (bom, Qt))
9765 CHECK_CONS (bom);
9766 val = XCAR (bom);
9767 CHECK_CODING_SYSTEM (val);
9768 val = XCDR (bom);
9769 CHECK_CODING_SYSTEM (val);
9771 ASET (attrs, coding_attr_utf_bom, bom);
9773 endian = args[coding_arg_utf16_endian];
9774 CHECK_SYMBOL (endian);
9775 if (NILP (endian))
9776 endian = Qbig;
9777 else if (! EQ (endian, Qbig) && ! EQ (endian, Qlittle))
9778 error ("Invalid endian: %s", SDATA (SYMBOL_NAME (endian)));
9779 ASET (attrs, coding_attr_utf_16_endian, endian);
9781 category = (CONSP (bom)
9782 ? coding_category_utf_16_auto
9783 : NILP (bom)
9784 ? (EQ (endian, Qbig)
9785 ? coding_category_utf_16_be_nosig
9786 : coding_category_utf_16_le_nosig)
9787 : (EQ (endian, Qbig)
9788 ? coding_category_utf_16_be
9789 : coding_category_utf_16_le));
9791 else if (EQ (coding_type, Qiso_2022))
9793 Lisp_Object initial, reg_usage, request, flags;
9795 if (nargs < coding_arg_iso2022_max)
9796 goto short_args;
9798 initial = Fcopy_sequence (args[coding_arg_iso2022_initial]);
9799 CHECK_VECTOR (initial);
9800 for (i = 0; i < 4; i++)
9802 val = AREF (initial, i);
9803 if (! NILP (val))
9805 struct charset *charset;
9807 CHECK_CHARSET_GET_CHARSET (val, charset);
9808 ASET (initial, i, make_number (CHARSET_ID (charset)));
9809 if (i == 0 && CHARSET_ASCII_COMPATIBLE_P (charset))
9810 ASET (attrs, coding_attr_ascii_compat, Qt);
9812 else
9813 ASET (initial, i, make_number (-1));
9816 reg_usage = args[coding_arg_iso2022_reg_usage];
9817 CHECK_CONS (reg_usage);
9818 CHECK_NUMBER_CAR (reg_usage);
9819 CHECK_NUMBER_CDR (reg_usage);
9821 request = Fcopy_sequence (args[coding_arg_iso2022_request]);
9822 for (tail = request; CONSP (tail); tail = XCDR (tail))
9824 int id;
9825 Lisp_Object tmp1;
9827 val = XCAR (tail);
9828 CHECK_CONS (val);
9829 tmp1 = XCAR (val);
9830 CHECK_CHARSET_GET_ID (tmp1, id);
9831 CHECK_NATNUM_CDR (val);
9832 if (XINT (XCDR (val)) >= 4)
9833 error ("Invalid graphic register number: %"pI"d", XINT (XCDR (val)));
9834 XSETCAR (val, make_number (id));
9837 flags = args[coding_arg_iso2022_flags];
9838 CHECK_NATNUM (flags);
9839 i = XINT (flags) & INT_MAX;
9840 if (EQ (args[coding_arg_charset_list], Qiso_2022))
9841 i |= CODING_ISO_FLAG_FULL_SUPPORT;
9842 flags = make_number (i);
9844 ASET (attrs, coding_attr_iso_initial, initial);
9845 ASET (attrs, coding_attr_iso_usage, reg_usage);
9846 ASET (attrs, coding_attr_iso_request, request);
9847 ASET (attrs, coding_attr_iso_flags, flags);
9848 setup_iso_safe_charsets (attrs);
9850 if (i & CODING_ISO_FLAG_SEVEN_BITS)
9851 category = ((i & (CODING_ISO_FLAG_LOCKING_SHIFT
9852 | CODING_ISO_FLAG_SINGLE_SHIFT))
9853 ? coding_category_iso_7_else
9854 : EQ (args[coding_arg_charset_list], Qiso_2022)
9855 ? coding_category_iso_7
9856 : coding_category_iso_7_tight);
9857 else
9859 int id = XINT (AREF (initial, 1));
9861 category = (((i & CODING_ISO_FLAG_LOCKING_SHIFT)
9862 || EQ (args[coding_arg_charset_list], Qiso_2022)
9863 || id < 0)
9864 ? coding_category_iso_8_else
9865 : (CHARSET_DIMENSION (CHARSET_FROM_ID (id)) == 1)
9866 ? coding_category_iso_8_1
9867 : coding_category_iso_8_2);
9869 if (category != coding_category_iso_8_1
9870 && category != coding_category_iso_8_2)
9871 ASET (attrs, coding_attr_ascii_compat, Qnil);
9873 else if (EQ (coding_type, Qemacs_mule))
9875 if (EQ (args[coding_arg_charset_list], Qemacs_mule))
9876 ASET (attrs, coding_attr_emacs_mule_full, Qt);
9877 ASET (attrs, coding_attr_ascii_compat, Qt);
9878 category = coding_category_emacs_mule;
9880 else if (EQ (coding_type, Qshift_jis))
9883 struct charset *charset;
9885 if (XINT (Flength (charset_list)) != 3
9886 && XINT (Flength (charset_list)) != 4)
9887 error ("There should be three or four charsets");
9889 charset = CHARSET_FROM_ID (XINT (XCAR (charset_list)));
9890 if (CHARSET_DIMENSION (charset) != 1)
9891 error ("Dimension of charset %s is not one",
9892 SDATA (SYMBOL_NAME (CHARSET_NAME (charset))));
9893 if (CHARSET_ASCII_COMPATIBLE_P (charset))
9894 ASET (attrs, coding_attr_ascii_compat, Qt);
9896 charset_list = XCDR (charset_list);
9897 charset = CHARSET_FROM_ID (XINT (XCAR (charset_list)));
9898 if (CHARSET_DIMENSION (charset) != 1)
9899 error ("Dimension of charset %s is not one",
9900 SDATA (SYMBOL_NAME (CHARSET_NAME (charset))));
9902 charset_list = XCDR (charset_list);
9903 charset = CHARSET_FROM_ID (XINT (XCAR (charset_list)));
9904 if (CHARSET_DIMENSION (charset) != 2)
9905 error ("Dimension of charset %s is not two",
9906 SDATA (SYMBOL_NAME (CHARSET_NAME (charset))));
9908 charset_list = XCDR (charset_list);
9909 if (! NILP (charset_list))
9911 charset = CHARSET_FROM_ID (XINT (XCAR (charset_list)));
9912 if (CHARSET_DIMENSION (charset) != 2)
9913 error ("Dimension of charset %s is not two",
9914 SDATA (SYMBOL_NAME (CHARSET_NAME (charset))));
9917 category = coding_category_sjis;
9918 Vsjis_coding_system = name;
9920 else if (EQ (coding_type, Qbig5))
9922 struct charset *charset;
9924 if (XINT (Flength (charset_list)) != 2)
9925 error ("There should be just two charsets");
9927 charset = CHARSET_FROM_ID (XINT (XCAR (charset_list)));
9928 if (CHARSET_DIMENSION (charset) != 1)
9929 error ("Dimension of charset %s is not one",
9930 SDATA (SYMBOL_NAME (CHARSET_NAME (charset))));
9931 if (CHARSET_ASCII_COMPATIBLE_P (charset))
9932 ASET (attrs, coding_attr_ascii_compat, Qt);
9934 charset_list = XCDR (charset_list);
9935 charset = CHARSET_FROM_ID (XINT (XCAR (charset_list)));
9936 if (CHARSET_DIMENSION (charset) != 2)
9937 error ("Dimension of charset %s is not two",
9938 SDATA (SYMBOL_NAME (CHARSET_NAME (charset))));
9940 category = coding_category_big5;
9941 Vbig5_coding_system = name;
9943 else if (EQ (coding_type, Qraw_text))
9945 category = coding_category_raw_text;
9946 ASET (attrs, coding_attr_ascii_compat, Qt);
9948 else if (EQ (coding_type, Qutf_8))
9950 Lisp_Object bom;
9952 if (nargs < coding_arg_utf8_max)
9953 goto short_args;
9955 bom = args[coding_arg_utf8_bom];
9956 if (! NILP (bom) && ! EQ (bom, Qt))
9958 CHECK_CONS (bom);
9959 val = XCAR (bom);
9960 CHECK_CODING_SYSTEM (val);
9961 val = XCDR (bom);
9962 CHECK_CODING_SYSTEM (val);
9964 ASET (attrs, coding_attr_utf_bom, bom);
9965 if (NILP (bom))
9966 ASET (attrs, coding_attr_ascii_compat, Qt);
9968 category = (CONSP (bom) ? coding_category_utf_8_auto
9969 : NILP (bom) ? coding_category_utf_8_nosig
9970 : coding_category_utf_8_sig);
9972 else if (EQ (coding_type, Qundecided))
9973 category = coding_category_undecided;
9974 else
9975 error ("Invalid coding system type: %s",
9976 SDATA (SYMBOL_NAME (coding_type)));
9978 ASET (attrs, coding_attr_category, make_number (category));
9979 ASET (attrs, coding_attr_plist,
9980 Fcons (QCcategory,
9981 Fcons (AREF (Vcoding_category_table, category),
9982 CODING_ATTR_PLIST (attrs))));
9983 ASET (attrs, coding_attr_plist,
9984 Fcons (QCascii_compatible_p,
9985 Fcons (CODING_ATTR_ASCII_COMPAT (attrs),
9986 CODING_ATTR_PLIST (attrs))));
9988 eol_type = args[coding_arg_eol_type];
9989 if (! NILP (eol_type)
9990 && ! EQ (eol_type, Qunix)
9991 && ! EQ (eol_type, Qdos)
9992 && ! EQ (eol_type, Qmac))
9993 error ("Invalid eol-type");
9995 aliases = Fcons (name, Qnil);
9997 if (NILP (eol_type))
9999 eol_type = make_subsidiaries (name);
10000 for (i = 0; i < 3; i++)
10002 Lisp_Object this_spec, this_name, this_aliases, this_eol_type;
10004 this_name = AREF (eol_type, i);
10005 this_aliases = Fcons (this_name, Qnil);
10006 this_eol_type = (i == 0 ? Qunix : i == 1 ? Qdos : Qmac);
10007 this_spec = make_uninit_vector (3);
10008 ASET (this_spec, 0, attrs);
10009 ASET (this_spec, 1, this_aliases);
10010 ASET (this_spec, 2, this_eol_type);
10011 Fputhash (this_name, this_spec, Vcoding_system_hash_table);
10012 Vcoding_system_list = Fcons (this_name, Vcoding_system_list);
10013 val = Fassoc (Fsymbol_name (this_name), Vcoding_system_alist);
10014 if (NILP (val))
10015 Vcoding_system_alist
10016 = Fcons (Fcons (Fsymbol_name (this_name), Qnil),
10017 Vcoding_system_alist);
10021 spec_vec = make_uninit_vector (3);
10022 ASET (spec_vec, 0, attrs);
10023 ASET (spec_vec, 1, aliases);
10024 ASET (spec_vec, 2, eol_type);
10026 Fputhash (name, spec_vec, Vcoding_system_hash_table);
10027 Vcoding_system_list = Fcons (name, Vcoding_system_list);
10028 val = Fassoc (Fsymbol_name (name), Vcoding_system_alist);
10029 if (NILP (val))
10030 Vcoding_system_alist = Fcons (Fcons (Fsymbol_name (name), Qnil),
10031 Vcoding_system_alist);
10034 int id = coding_categories[category].id;
10036 if (id < 0 || EQ (name, CODING_ID_NAME (id)))
10037 setup_coding_system (name, &coding_categories[category]);
10040 return Qnil;
10042 short_args:
10043 return Fsignal (Qwrong_number_of_arguments,
10044 Fcons (intern ("define-coding-system-internal"),
10045 make_number (nargs)));
10049 DEFUN ("coding-system-put", Fcoding_system_put, Scoding_system_put,
10050 3, 3, 0,
10051 doc: /* Change value in CODING-SYSTEM's property list PROP to VAL. */)
10052 (Lisp_Object coding_system, Lisp_Object prop, Lisp_Object val)
10054 Lisp_Object spec, attrs;
10056 CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec);
10057 attrs = AREF (spec, 0);
10058 if (EQ (prop, QCmnemonic))
10060 if (! STRINGP (val))
10061 CHECK_CHARACTER (val);
10062 ASET (attrs, coding_attr_mnemonic, val);
10064 else if (EQ (prop, QCdefault_char))
10066 if (NILP (val))
10067 val = make_number (' ');
10068 else
10069 CHECK_CHARACTER (val);
10070 ASET (attrs, coding_attr_default_char, val);
10072 else if (EQ (prop, QCdecode_translation_table))
10074 if (! CHAR_TABLE_P (val) && ! CONSP (val))
10075 CHECK_SYMBOL (val);
10076 ASET (attrs, coding_attr_decode_tbl, val);
10078 else if (EQ (prop, QCencode_translation_table))
10080 if (! CHAR_TABLE_P (val) && ! CONSP (val))
10081 CHECK_SYMBOL (val);
10082 ASET (attrs, coding_attr_encode_tbl, val);
10084 else if (EQ (prop, QCpost_read_conversion))
10086 CHECK_SYMBOL (val);
10087 ASET (attrs, coding_attr_post_read, val);
10089 else if (EQ (prop, QCpre_write_conversion))
10091 CHECK_SYMBOL (val);
10092 ASET (attrs, coding_attr_pre_write, val);
10094 else if (EQ (prop, QCascii_compatible_p))
10096 ASET (attrs, coding_attr_ascii_compat, val);
10099 ASET (attrs, coding_attr_plist,
10100 Fplist_put (CODING_ATTR_PLIST (attrs), prop, val));
10101 return val;
10105 DEFUN ("define-coding-system-alias", Fdefine_coding_system_alias,
10106 Sdefine_coding_system_alias, 2, 2, 0,
10107 doc: /* Define ALIAS as an alias for CODING-SYSTEM. */)
10108 (Lisp_Object alias, Lisp_Object coding_system)
10110 Lisp_Object spec, aliases, eol_type, val;
10112 CHECK_SYMBOL (alias);
10113 CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec);
10114 aliases = AREF (spec, 1);
10115 /* ALIASES should be a list of length more than zero, and the first
10116 element is a base coding system. Append ALIAS at the tail of the
10117 list. */
10118 while (!NILP (XCDR (aliases)))
10119 aliases = XCDR (aliases);
10120 XSETCDR (aliases, Fcons (alias, Qnil));
10122 eol_type = AREF (spec, 2);
10123 if (VECTORP (eol_type))
10125 Lisp_Object subsidiaries;
10126 int i;
10128 subsidiaries = make_subsidiaries (alias);
10129 for (i = 0; i < 3; i++)
10130 Fdefine_coding_system_alias (AREF (subsidiaries, i),
10131 AREF (eol_type, i));
10134 Fputhash (alias, spec, Vcoding_system_hash_table);
10135 Vcoding_system_list = Fcons (alias, Vcoding_system_list);
10136 val = Fassoc (Fsymbol_name (alias), Vcoding_system_alist);
10137 if (NILP (val))
10138 Vcoding_system_alist = Fcons (Fcons (Fsymbol_name (alias), Qnil),
10139 Vcoding_system_alist);
10141 return Qnil;
10144 DEFUN ("coding-system-base", Fcoding_system_base, Scoding_system_base,
10145 1, 1, 0,
10146 doc: /* Return the base of CODING-SYSTEM.
10147 Any alias or subsidiary coding system is not a base coding system. */)
10148 (Lisp_Object coding_system)
10150 Lisp_Object spec, attrs;
10152 if (NILP (coding_system))
10153 return (Qno_conversion);
10154 CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec);
10155 attrs = AREF (spec, 0);
10156 return CODING_ATTR_BASE_NAME (attrs);
10159 DEFUN ("coding-system-plist", Fcoding_system_plist, Scoding_system_plist,
10160 1, 1, 0,
10161 doc: "Return the property list of CODING-SYSTEM.")
10162 (Lisp_Object coding_system)
10164 Lisp_Object spec, attrs;
10166 if (NILP (coding_system))
10167 coding_system = Qno_conversion;
10168 CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec);
10169 attrs = AREF (spec, 0);
10170 return CODING_ATTR_PLIST (attrs);
10174 DEFUN ("coding-system-aliases", Fcoding_system_aliases, Scoding_system_aliases,
10175 1, 1, 0,
10176 doc: /* Return the list of aliases of CODING-SYSTEM. */)
10177 (Lisp_Object coding_system)
10179 Lisp_Object spec;
10181 if (NILP (coding_system))
10182 coding_system = Qno_conversion;
10183 CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec);
10184 return AREF (spec, 1);
10187 DEFUN ("coding-system-eol-type", Fcoding_system_eol_type,
10188 Scoding_system_eol_type, 1, 1, 0,
10189 doc: /* Return eol-type of CODING-SYSTEM.
10190 An eol-type is an integer 0, 1, 2, or a vector of coding systems.
10192 Integer values 0, 1, and 2 indicate a format of end-of-line; LF, CRLF,
10193 and CR respectively.
10195 A vector value indicates that a format of end-of-line should be
10196 detected automatically. Nth element of the vector is the subsidiary
10197 coding system whose eol-type is N. */)
10198 (Lisp_Object coding_system)
10200 Lisp_Object spec, eol_type;
10201 int n;
10203 if (NILP (coding_system))
10204 coding_system = Qno_conversion;
10205 if (! CODING_SYSTEM_P (coding_system))
10206 return Qnil;
10207 spec = CODING_SYSTEM_SPEC (coding_system);
10208 eol_type = AREF (spec, 2);
10209 if (VECTORP (eol_type))
10210 return Fcopy_sequence (eol_type);
10211 n = EQ (eol_type, Qunix) ? 0 : EQ (eol_type, Qdos) ? 1 : 2;
10212 return make_number (n);
10215 #endif /* emacs */
10218 /*** 9. Post-amble ***/
10220 void
10221 init_coding_once (void)
10223 int i;
10225 for (i = 0; i < coding_category_max; i++)
10227 coding_categories[i].id = -1;
10228 coding_priorities[i] = i;
10231 /* ISO2022 specific initialize routine. */
10232 for (i = 0; i < 0x20; i++)
10233 iso_code_class[i] = ISO_control_0;
10234 for (i = 0x21; i < 0x7F; i++)
10235 iso_code_class[i] = ISO_graphic_plane_0;
10236 for (i = 0x80; i < 0xA0; i++)
10237 iso_code_class[i] = ISO_control_1;
10238 for (i = 0xA1; i < 0xFF; i++)
10239 iso_code_class[i] = ISO_graphic_plane_1;
10240 iso_code_class[0x20] = iso_code_class[0x7F] = ISO_0x20_or_0x7F;
10241 iso_code_class[0xA0] = iso_code_class[0xFF] = ISO_0xA0_or_0xFF;
10242 iso_code_class[ISO_CODE_SO] = ISO_shift_out;
10243 iso_code_class[ISO_CODE_SI] = ISO_shift_in;
10244 iso_code_class[ISO_CODE_SS2_7] = ISO_single_shift_2_7;
10245 iso_code_class[ISO_CODE_ESC] = ISO_escape;
10246 iso_code_class[ISO_CODE_SS2] = ISO_single_shift_2;
10247 iso_code_class[ISO_CODE_SS3] = ISO_single_shift_3;
10248 iso_code_class[ISO_CODE_CSI] = ISO_control_sequence_introducer;
10250 for (i = 0; i < 256; i++)
10252 emacs_mule_bytes[i] = 1;
10254 emacs_mule_bytes[EMACS_MULE_LEADING_CODE_PRIVATE_11] = 3;
10255 emacs_mule_bytes[EMACS_MULE_LEADING_CODE_PRIVATE_12] = 3;
10256 emacs_mule_bytes[EMACS_MULE_LEADING_CODE_PRIVATE_21] = 4;
10257 emacs_mule_bytes[EMACS_MULE_LEADING_CODE_PRIVATE_22] = 4;
10260 #ifdef emacs
10262 void
10263 syms_of_coding (void)
10265 staticpro (&Vcoding_system_hash_table);
10267 Lisp_Object args[2];
10268 args[0] = QCtest;
10269 args[1] = Qeq;
10270 Vcoding_system_hash_table = Fmake_hash_table (2, args);
10273 staticpro (&Vsjis_coding_system);
10274 Vsjis_coding_system = Qnil;
10276 staticpro (&Vbig5_coding_system);
10277 Vbig5_coding_system = Qnil;
10279 staticpro (&Vcode_conversion_reused_workbuf);
10280 Vcode_conversion_reused_workbuf = Qnil;
10282 staticpro (&Vcode_conversion_workbuf_name);
10283 Vcode_conversion_workbuf_name = build_pure_c_string (" *code-conversion-work*");
10285 reused_workbuf_in_use = 0;
10287 DEFSYM (Qcharset, "charset");
10288 DEFSYM (Qtarget_idx, "target-idx");
10289 DEFSYM (Qcoding_system_history, "coding-system-history");
10290 Fset (Qcoding_system_history, Qnil);
10292 /* Target FILENAME is the first argument. */
10293 Fput (Qinsert_file_contents, Qtarget_idx, make_number (0));
10294 /* Target FILENAME is the third argument. */
10295 Fput (Qwrite_region, Qtarget_idx, make_number (2));
10297 DEFSYM (Qcall_process, "call-process");
10298 /* Target PROGRAM is the first argument. */
10299 Fput (Qcall_process, Qtarget_idx, make_number (0));
10301 DEFSYM (Qcall_process_region, "call-process-region");
10302 /* Target PROGRAM is the third argument. */
10303 Fput (Qcall_process_region, Qtarget_idx, make_number (2));
10305 DEFSYM (Qstart_process, "start-process");
10306 /* Target PROGRAM is the third argument. */
10307 Fput (Qstart_process, Qtarget_idx, make_number (2));
10309 DEFSYM (Qopen_network_stream, "open-network-stream");
10310 /* Target SERVICE is the fourth argument. */
10311 Fput (Qopen_network_stream, Qtarget_idx, make_number (3));
10313 DEFSYM (Qcoding_system, "coding-system");
10314 DEFSYM (Qcoding_aliases, "coding-aliases");
10316 DEFSYM (Qeol_type, "eol-type");
10317 DEFSYM (Qunix, "unix");
10318 DEFSYM (Qdos, "dos");
10319 DEFSYM (Qmac, "mac");
10321 DEFSYM (Qbuffer_file_coding_system, "buffer-file-coding-system");
10322 DEFSYM (Qpost_read_conversion, "post-read-conversion");
10323 DEFSYM (Qpre_write_conversion, "pre-write-conversion");
10324 DEFSYM (Qdefault_char, "default-char");
10325 DEFSYM (Qundecided, "undecided");
10326 DEFSYM (Qno_conversion, "no-conversion");
10327 DEFSYM (Qraw_text, "raw-text");
10329 DEFSYM (Qiso_2022, "iso-2022");
10331 DEFSYM (Qutf_8, "utf-8");
10332 DEFSYM (Qutf_8_emacs, "utf-8-emacs");
10334 #if defined (WINDOWSNT) || defined (CYGWIN)
10335 /* No, not utf-16-le: that one has a BOM. */
10336 DEFSYM (Qutf_16le, "utf-16le");
10337 #endif
10339 DEFSYM (Qutf_16, "utf-16");
10340 DEFSYM (Qbig, "big");
10341 DEFSYM (Qlittle, "little");
10343 DEFSYM (Qshift_jis, "shift-jis");
10344 DEFSYM (Qbig5, "big5");
10346 DEFSYM (Qcoding_system_p, "coding-system-p");
10348 DEFSYM (Qcoding_system_error, "coding-system-error");
10349 Fput (Qcoding_system_error, Qerror_conditions,
10350 listn (CONSTYPE_PURE, 2, Qcoding_system_error, Qerror));
10351 Fput (Qcoding_system_error, Qerror_message,
10352 build_pure_c_string ("Invalid coding system"));
10354 /* Intern this now in case it isn't already done.
10355 Setting this variable twice is harmless.
10356 But don't staticpro it here--that is done in alloc.c. */
10357 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
10359 DEFSYM (Qtranslation_table, "translation-table");
10360 Fput (Qtranslation_table, Qchar_table_extra_slots, make_number (2));
10361 DEFSYM (Qtranslation_table_id, "translation-table-id");
10362 DEFSYM (Qtranslation_table_for_decode, "translation-table-for-decode");
10363 DEFSYM (Qtranslation_table_for_encode, "translation-table-for-encode");
10365 DEFSYM (Qvalid_codes, "valid-codes");
10367 DEFSYM (Qemacs_mule, "emacs-mule");
10369 DEFSYM (QCcategory, ":category");
10370 DEFSYM (QCmnemonic, ":mnemonic");
10371 DEFSYM (QCdefault_char, ":default-char");
10372 DEFSYM (QCdecode_translation_table, ":decode-translation-table");
10373 DEFSYM (QCencode_translation_table, ":encode-translation-table");
10374 DEFSYM (QCpost_read_conversion, ":post-read-conversion");
10375 DEFSYM (QCpre_write_conversion, ":pre-write-conversion");
10376 DEFSYM (QCascii_compatible_p, ":ascii-compatible-p");
10378 Vcoding_category_table
10379 = Fmake_vector (make_number (coding_category_max), Qnil);
10380 staticpro (&Vcoding_category_table);
10381 /* Followings are target of code detection. */
10382 ASET (Vcoding_category_table, coding_category_iso_7,
10383 intern_c_string ("coding-category-iso-7"));
10384 ASET (Vcoding_category_table, coding_category_iso_7_tight,
10385 intern_c_string ("coding-category-iso-7-tight"));
10386 ASET (Vcoding_category_table, coding_category_iso_8_1,
10387 intern_c_string ("coding-category-iso-8-1"));
10388 ASET (Vcoding_category_table, coding_category_iso_8_2,
10389 intern_c_string ("coding-category-iso-8-2"));
10390 ASET (Vcoding_category_table, coding_category_iso_7_else,
10391 intern_c_string ("coding-category-iso-7-else"));
10392 ASET (Vcoding_category_table, coding_category_iso_8_else,
10393 intern_c_string ("coding-category-iso-8-else"));
10394 ASET (Vcoding_category_table, coding_category_utf_8_auto,
10395 intern_c_string ("coding-category-utf-8-auto"));
10396 ASET (Vcoding_category_table, coding_category_utf_8_nosig,
10397 intern_c_string ("coding-category-utf-8"));
10398 ASET (Vcoding_category_table, coding_category_utf_8_sig,
10399 intern_c_string ("coding-category-utf-8-sig"));
10400 ASET (Vcoding_category_table, coding_category_utf_16_be,
10401 intern_c_string ("coding-category-utf-16-be"));
10402 ASET (Vcoding_category_table, coding_category_utf_16_auto,
10403 intern_c_string ("coding-category-utf-16-auto"));
10404 ASET (Vcoding_category_table, coding_category_utf_16_le,
10405 intern_c_string ("coding-category-utf-16-le"));
10406 ASET (Vcoding_category_table, coding_category_utf_16_be_nosig,
10407 intern_c_string ("coding-category-utf-16-be-nosig"));
10408 ASET (Vcoding_category_table, coding_category_utf_16_le_nosig,
10409 intern_c_string ("coding-category-utf-16-le-nosig"));
10410 ASET (Vcoding_category_table, coding_category_charset,
10411 intern_c_string ("coding-category-charset"));
10412 ASET (Vcoding_category_table, coding_category_sjis,
10413 intern_c_string ("coding-category-sjis"));
10414 ASET (Vcoding_category_table, coding_category_big5,
10415 intern_c_string ("coding-category-big5"));
10416 ASET (Vcoding_category_table, coding_category_ccl,
10417 intern_c_string ("coding-category-ccl"));
10418 ASET (Vcoding_category_table, coding_category_emacs_mule,
10419 intern_c_string ("coding-category-emacs-mule"));
10420 /* Followings are NOT target of code detection. */
10421 ASET (Vcoding_category_table, coding_category_raw_text,
10422 intern_c_string ("coding-category-raw-text"));
10423 ASET (Vcoding_category_table, coding_category_undecided,
10424 intern_c_string ("coding-category-undecided"));
10426 DEFSYM (Qinsufficient_source, "insufficient-source");
10427 DEFSYM (Qinvalid_source, "invalid-source");
10428 DEFSYM (Qinterrupted, "interrupted");
10429 DEFSYM (Qcoding_system_define_form, "coding-system-define-form");
10431 defsubr (&Scoding_system_p);
10432 defsubr (&Sread_coding_system);
10433 defsubr (&Sread_non_nil_coding_system);
10434 defsubr (&Scheck_coding_system);
10435 defsubr (&Sdetect_coding_region);
10436 defsubr (&Sdetect_coding_string);
10437 defsubr (&Sfind_coding_systems_region_internal);
10438 defsubr (&Sunencodable_char_position);
10439 defsubr (&Scheck_coding_systems_region);
10440 defsubr (&Sdecode_coding_region);
10441 defsubr (&Sencode_coding_region);
10442 defsubr (&Sdecode_coding_string);
10443 defsubr (&Sencode_coding_string);
10444 defsubr (&Sdecode_sjis_char);
10445 defsubr (&Sencode_sjis_char);
10446 defsubr (&Sdecode_big5_char);
10447 defsubr (&Sencode_big5_char);
10448 defsubr (&Sset_terminal_coding_system_internal);
10449 defsubr (&Sset_safe_terminal_coding_system_internal);
10450 defsubr (&Sterminal_coding_system);
10451 defsubr (&Sset_keyboard_coding_system_internal);
10452 defsubr (&Skeyboard_coding_system);
10453 defsubr (&Sfind_operation_coding_system);
10454 defsubr (&Sset_coding_system_priority);
10455 defsubr (&Sdefine_coding_system_internal);
10456 defsubr (&Sdefine_coding_system_alias);
10457 defsubr (&Scoding_system_put);
10458 defsubr (&Scoding_system_base);
10459 defsubr (&Scoding_system_plist);
10460 defsubr (&Scoding_system_aliases);
10461 defsubr (&Scoding_system_eol_type);
10462 defsubr (&Scoding_system_priority_list);
10464 DEFVAR_LISP ("coding-system-list", Vcoding_system_list,
10465 doc: /* List of coding systems.
10467 Do not alter the value of this variable manually. This variable should be
10468 updated by the functions `define-coding-system' and
10469 `define-coding-system-alias'. */);
10470 Vcoding_system_list = Qnil;
10472 DEFVAR_LISP ("coding-system-alist", Vcoding_system_alist,
10473 doc: /* Alist of coding system names.
10474 Each element is one element list of coding system name.
10475 This variable is given to `completing-read' as COLLECTION argument.
10477 Do not alter the value of this variable manually. This variable should be
10478 updated by the functions `make-coding-system' and
10479 `define-coding-system-alias'. */);
10480 Vcoding_system_alist = Qnil;
10482 DEFVAR_LISP ("coding-category-list", Vcoding_category_list,
10483 doc: /* List of coding-categories (symbols) ordered by priority.
10485 On detecting a coding system, Emacs tries code detection algorithms
10486 associated with each coding-category one by one in this order. When
10487 one algorithm agrees with a byte sequence of source text, the coding
10488 system bound to the corresponding coding-category is selected.
10490 Don't modify this variable directly, but use `set-coding-system-priority'. */);
10492 int i;
10494 Vcoding_category_list = Qnil;
10495 for (i = coding_category_max - 1; i >= 0; i--)
10496 Vcoding_category_list
10497 = Fcons (AREF (Vcoding_category_table, i),
10498 Vcoding_category_list);
10501 DEFVAR_LISP ("coding-system-for-read", Vcoding_system_for_read,
10502 doc: /* Specify the coding system for read operations.
10503 It is useful to bind this variable with `let', but do not set it globally.
10504 If the value is a coding system, it is used for decoding on read operation.
10505 If not, an appropriate element is used from one of the coding system alists.
10506 There are three such tables: `file-coding-system-alist',
10507 `process-coding-system-alist', and `network-coding-system-alist'. */);
10508 Vcoding_system_for_read = Qnil;
10510 DEFVAR_LISP ("coding-system-for-write", Vcoding_system_for_write,
10511 doc: /* Specify the coding system for write operations.
10512 Programs bind this variable with `let', but you should not set it globally.
10513 If the value is a coding system, it is used for encoding of output,
10514 when writing it to a file and when sending it to a file or subprocess.
10516 If this does not specify a coding system, an appropriate element
10517 is used from one of the coding system alists.
10518 There are three such tables: `file-coding-system-alist',
10519 `process-coding-system-alist', and `network-coding-system-alist'.
10520 For output to files, if the above procedure does not specify a coding system,
10521 the value of `buffer-file-coding-system' is used. */);
10522 Vcoding_system_for_write = Qnil;
10524 DEFVAR_LISP ("last-coding-system-used", Vlast_coding_system_used,
10525 doc: /*
10526 Coding system used in the latest file or process I/O. */);
10527 Vlast_coding_system_used = Qnil;
10529 DEFVAR_LISP ("last-code-conversion-error", Vlast_code_conversion_error,
10530 doc: /*
10531 Error status of the last code conversion.
10533 When an error was detected in the last code conversion, this variable
10534 is set to one of the following symbols.
10535 `insufficient-source'
10536 `inconsistent-eol'
10537 `invalid-source'
10538 `interrupted'
10539 `insufficient-memory'
10540 When no error was detected, the value doesn't change. So, to check
10541 the error status of a code conversion by this variable, you must
10542 explicitly set this variable to nil before performing code
10543 conversion. */);
10544 Vlast_code_conversion_error = Qnil;
10546 DEFVAR_BOOL ("inhibit-eol-conversion", inhibit_eol_conversion,
10547 doc: /*
10548 *Non-nil means always inhibit code conversion of end-of-line format.
10549 See info node `Coding Systems' and info node `Text and Binary' concerning
10550 such conversion. */);
10551 inhibit_eol_conversion = 0;
10553 DEFVAR_BOOL ("inherit-process-coding-system", inherit_process_coding_system,
10554 doc: /*
10555 Non-nil means process buffer inherits coding system of process output.
10556 Bind it to t if the process output is to be treated as if it were a file
10557 read from some filesystem. */);
10558 inherit_process_coding_system = 0;
10560 DEFVAR_LISP ("file-coding-system-alist", Vfile_coding_system_alist,
10561 doc: /*
10562 Alist to decide a coding system to use for a file I/O operation.
10563 The format is ((PATTERN . VAL) ...),
10564 where PATTERN is a regular expression matching a file name,
10565 VAL is a coding system, a cons of coding systems, or a function symbol.
10566 If VAL is a coding system, it is used for both decoding and encoding
10567 the file contents.
10568 If VAL is a cons of coding systems, the car part is used for decoding,
10569 and the cdr part is used for encoding.
10570 If VAL is a function symbol, the function must return a coding system
10571 or a cons of coding systems which are used as above. The function is
10572 called with an argument that is a list of the arguments with which
10573 `find-operation-coding-system' was called. If the function can't decide
10574 a coding system, it can return `undecided' so that the normal
10575 code-detection is performed.
10577 See also the function `find-operation-coding-system'
10578 and the variable `auto-coding-alist'. */);
10579 Vfile_coding_system_alist = Qnil;
10581 DEFVAR_LISP ("process-coding-system-alist", Vprocess_coding_system_alist,
10582 doc: /*
10583 Alist to decide a coding system to use for a process I/O operation.
10584 The format is ((PATTERN . VAL) ...),
10585 where PATTERN is a regular expression matching a program name,
10586 VAL is a coding system, a cons of coding systems, or a function symbol.
10587 If VAL is a coding system, it is used for both decoding what received
10588 from the program and encoding what sent to the program.
10589 If VAL is a cons of coding systems, the car part is used for decoding,
10590 and the cdr part is used for encoding.
10591 If VAL is a function symbol, the function must return a coding system
10592 or a cons of coding systems which are used as above.
10594 See also the function `find-operation-coding-system'. */);
10595 Vprocess_coding_system_alist = Qnil;
10597 DEFVAR_LISP ("network-coding-system-alist", Vnetwork_coding_system_alist,
10598 doc: /*
10599 Alist to decide a coding system to use for a network I/O operation.
10600 The format is ((PATTERN . VAL) ...),
10601 where PATTERN is a regular expression matching a network service name
10602 or is a port number to connect to,
10603 VAL is a coding system, a cons of coding systems, or a function symbol.
10604 If VAL is a coding system, it is used for both decoding what received
10605 from the network stream and encoding what sent to the network stream.
10606 If VAL is a cons of coding systems, the car part is used for decoding,
10607 and the cdr part is used for encoding.
10608 If VAL is a function symbol, the function must return a coding system
10609 or a cons of coding systems which are used as above.
10611 See also the function `find-operation-coding-system'. */);
10612 Vnetwork_coding_system_alist = Qnil;
10614 DEFVAR_LISP ("locale-coding-system", Vlocale_coding_system,
10615 doc: /* Coding system to use with system messages.
10616 Also used for decoding keyboard input on X Window system. */);
10617 Vlocale_coding_system = Qnil;
10619 /* The eol mnemonics are reset in startup.el system-dependently. */
10620 DEFVAR_LISP ("eol-mnemonic-unix", eol_mnemonic_unix,
10621 doc: /*
10622 *String displayed in mode line for UNIX-like (LF) end-of-line format. */);
10623 eol_mnemonic_unix = build_pure_c_string (":");
10625 DEFVAR_LISP ("eol-mnemonic-dos", eol_mnemonic_dos,
10626 doc: /*
10627 *String displayed in mode line for DOS-like (CRLF) end-of-line format. */);
10628 eol_mnemonic_dos = build_pure_c_string ("\\");
10630 DEFVAR_LISP ("eol-mnemonic-mac", eol_mnemonic_mac,
10631 doc: /*
10632 *String displayed in mode line for MAC-like (CR) end-of-line format. */);
10633 eol_mnemonic_mac = build_pure_c_string ("/");
10635 DEFVAR_LISP ("eol-mnemonic-undecided", eol_mnemonic_undecided,
10636 doc: /*
10637 *String displayed in mode line when end-of-line format is not yet determined. */);
10638 eol_mnemonic_undecided = build_pure_c_string (":");
10640 DEFVAR_LISP ("enable-character-translation", Venable_character_translation,
10641 doc: /*
10642 *Non-nil enables character translation while encoding and decoding. */);
10643 Venable_character_translation = Qt;
10645 DEFVAR_LISP ("standard-translation-table-for-decode",
10646 Vstandard_translation_table_for_decode,
10647 doc: /* Table for translating characters while decoding. */);
10648 Vstandard_translation_table_for_decode = Qnil;
10650 DEFVAR_LISP ("standard-translation-table-for-encode",
10651 Vstandard_translation_table_for_encode,
10652 doc: /* Table for translating characters while encoding. */);
10653 Vstandard_translation_table_for_encode = Qnil;
10655 DEFVAR_LISP ("charset-revision-table", Vcharset_revision_table,
10656 doc: /* Alist of charsets vs revision numbers.
10657 While encoding, if a charset (car part of an element) is found,
10658 designate it with the escape sequence identifying revision (cdr part
10659 of the element). */);
10660 Vcharset_revision_table = Qnil;
10662 DEFVAR_LISP ("default-process-coding-system",
10663 Vdefault_process_coding_system,
10664 doc: /* Cons of coding systems used for process I/O by default.
10665 The car part is used for decoding a process output,
10666 the cdr part is used for encoding a text to be sent to a process. */);
10667 Vdefault_process_coding_system = Qnil;
10669 DEFVAR_LISP ("latin-extra-code-table", Vlatin_extra_code_table,
10670 doc: /*
10671 Table of extra Latin codes in the range 128..159 (inclusive).
10672 This is a vector of length 256.
10673 If Nth element is non-nil, the existence of code N in a file
10674 \(or output of subprocess) doesn't prevent it to be detected as
10675 a coding system of ISO 2022 variant which has a flag
10676 `accept-latin-extra-code' t (e.g. iso-latin-1) on reading a file
10677 or reading output of a subprocess.
10678 Only 128th through 159th elements have a meaning. */);
10679 Vlatin_extra_code_table = Fmake_vector (make_number (256), Qnil);
10681 DEFVAR_LISP ("select-safe-coding-system-function",
10682 Vselect_safe_coding_system_function,
10683 doc: /*
10684 Function to call to select safe coding system for encoding a text.
10686 If set, this function is called to force a user to select a proper
10687 coding system which can encode the text in the case that a default
10688 coding system used in each operation can't encode the text. The
10689 function should take care that the buffer is not modified while
10690 the coding system is being selected.
10692 The default value is `select-safe-coding-system' (which see). */);
10693 Vselect_safe_coding_system_function = Qnil;
10695 DEFVAR_BOOL ("coding-system-require-warning",
10696 coding_system_require_warning,
10697 doc: /* Internal use only.
10698 If non-nil, on writing a file, `select-safe-coding-system-function' is
10699 called even if `coding-system-for-write' is non-nil. The command
10700 `universal-coding-system-argument' binds this variable to t temporarily. */);
10701 coding_system_require_warning = 0;
10704 DEFVAR_BOOL ("inhibit-iso-escape-detection",
10705 inhibit_iso_escape_detection,
10706 doc: /*
10707 If non-nil, Emacs ignores ISO-2022 escape sequences during code detection.
10709 When Emacs reads text, it tries to detect how the text is encoded.
10710 This code detection is sensitive to escape sequences. If Emacs sees
10711 a valid ISO-2022 escape sequence, it assumes the text is encoded in one
10712 of the ISO2022 encodings, and decodes text by the corresponding coding
10713 system (e.g. `iso-2022-7bit').
10715 However, there may be a case that you want to read escape sequences in
10716 a file as is. In such a case, you can set this variable to non-nil.
10717 Then the code detection will ignore any escape sequences, and no text is
10718 detected as encoded in some ISO-2022 encoding. The result is that all
10719 escape sequences become visible in a buffer.
10721 The default value is nil, and it is strongly recommended not to change
10722 it. That is because many Emacs Lisp source files that contain
10723 non-ASCII characters are encoded by the coding system `iso-2022-7bit'
10724 in Emacs's distribution, and they won't be decoded correctly on
10725 reading if you suppress escape sequence detection.
10727 The other way to read escape sequences in a file without decoding is
10728 to explicitly specify some coding system that doesn't use ISO-2022
10729 escape sequence (e.g., `latin-1') on reading by \\[universal-coding-system-argument]. */);
10730 inhibit_iso_escape_detection = 0;
10732 DEFVAR_BOOL ("inhibit-null-byte-detection",
10733 inhibit_null_byte_detection,
10734 doc: /* If non-nil, Emacs ignores null bytes on code detection.
10735 By default, Emacs treats it as binary data, and does not attempt to
10736 decode it. The effect is as if you specified `no-conversion' for
10737 reading that text.
10739 Set this to non-nil when a regular text happens to include null bytes.
10740 Examples are Index nodes of Info files and null-byte delimited output
10741 from GNU Find and GNU Grep. Emacs will then ignore the null bytes and
10742 decode text as usual. */);
10743 inhibit_null_byte_detection = 0;
10745 DEFVAR_LISP ("translation-table-for-input", Vtranslation_table_for_input,
10746 doc: /* Char table for translating self-inserting characters.
10747 This is applied to the result of input methods, not their input.
10748 See also `keyboard-translate-table'.
10750 Use of this variable for character code unification was rendered
10751 obsolete in Emacs 23.1 and later, since Unicode is now the basis of
10752 internal character representation. */);
10753 Vtranslation_table_for_input = Qnil;
10756 Lisp_Object args[coding_arg_max];
10757 Lisp_Object plist[16];
10758 int i;
10760 for (i = 0; i < coding_arg_max; i++)
10761 args[i] = Qnil;
10763 plist[0] = intern_c_string (":name");
10764 plist[1] = args[coding_arg_name] = Qno_conversion;
10765 plist[2] = intern_c_string (":mnemonic");
10766 plist[3] = args[coding_arg_mnemonic] = make_number ('=');
10767 plist[4] = intern_c_string (":coding-type");
10768 plist[5] = args[coding_arg_coding_type] = Qraw_text;
10769 plist[6] = intern_c_string (":ascii-compatible-p");
10770 plist[7] = args[coding_arg_ascii_compatible_p] = Qt;
10771 plist[8] = intern_c_string (":default-char");
10772 plist[9] = args[coding_arg_default_char] = make_number (0);
10773 plist[10] = intern_c_string (":for-unibyte");
10774 plist[11] = args[coding_arg_for_unibyte] = Qt;
10775 plist[12] = intern_c_string (":docstring");
10776 plist[13] = build_pure_c_string ("Do no conversion.\n\
10778 When you visit a file with this coding, the file is read into a\n\
10779 unibyte buffer as is, thus each byte of a file is treated as a\n\
10780 character.");
10781 plist[14] = intern_c_string (":eol-type");
10782 plist[15] = args[coding_arg_eol_type] = Qunix;
10783 args[coding_arg_plist] = Flist (16, plist);
10784 Fdefine_coding_system_internal (coding_arg_max, args);
10786 plist[1] = args[coding_arg_name] = Qundecided;
10787 plist[3] = args[coding_arg_mnemonic] = make_number ('-');
10788 plist[5] = args[coding_arg_coding_type] = Qundecided;
10789 /* This is already set.
10790 plist[7] = args[coding_arg_ascii_compatible_p] = Qt; */
10791 plist[8] = intern_c_string (":charset-list");
10792 plist[9] = args[coding_arg_charset_list] = Fcons (Qascii, Qnil);
10793 plist[11] = args[coding_arg_for_unibyte] = Qnil;
10794 plist[13] = build_pure_c_string ("No conversion on encoding, automatic conversion on decoding.");
10795 plist[15] = args[coding_arg_eol_type] = Qnil;
10796 args[coding_arg_plist] = Flist (16, plist);
10797 Fdefine_coding_system_internal (coding_arg_max, args);
10800 setup_coding_system (Qno_conversion, &safe_terminal_coding);
10803 int i;
10805 for (i = 0; i < coding_category_max; i++)
10806 Fset (AREF (Vcoding_category_table, i), Qno_conversion);
10808 #if defined (DOS_NT)
10809 system_eol_type = Qdos;
10810 #else
10811 system_eol_type = Qunix;
10812 #endif
10813 staticpro (&system_eol_type);
10816 char *
10817 emacs_strerror (int error_number)
10819 char *str;
10821 synchronize_system_messages_locale ();
10822 str = strerror (error_number);
10824 if (! NILP (Vlocale_coding_system))
10826 Lisp_Object dec = code_convert_string_norecord (build_string (str),
10827 Vlocale_coding_system,
10829 str = SSDATA (dec);
10832 return str;
10835 #endif /* emacs */