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