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