(encode_coding_raw_text): Set coding->produced_char for all branches.
[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 c;
4822
4823 if (multibytep)
4824 {
4825 int safe_room = MAX_MULTIBYTE_LENGTH * 2;
4826
4827 if (coding->src_multibyte)
4828 while (charbuf < charbuf_end)
4829 {
4830 ASSURE_DESTINATION (safe_room);
4831 c = *charbuf++;
4832 if (ASCII_CHAR_P (c))
4833 EMIT_ONE_ASCII_BYTE (c);
4834 else if (CHAR_BYTE8_P (c))
4835 {
4836 c = CHAR_TO_BYTE8 (c);
4837 EMIT_ONE_BYTE (c);
4838 }
4839 else
4840 {
4841 unsigned char str[MAX_MULTIBYTE_LENGTH], *p0 = str, *p1 = str;
4842
4843 CHAR_STRING_ADVANCE (c, p1);
4844 while (p0 < p1)
4845 {
4846 EMIT_ONE_BYTE (*p0);
4847 p0++;
4848 }
4849 }
4850 }
4851 else
4852 while (charbuf < charbuf_end)
4853 {
4854 ASSURE_DESTINATION (safe_room);
4855 c = *charbuf++;
4856 EMIT_ONE_BYTE (c);
4857 }
4858 }
4859 else
4860 {
4861 if (coding->src_multibyte)
4862 {
4863 int safe_room = MAX_MULTIBYTE_LENGTH;
4864
4865 while (charbuf < charbuf_end)
4866 {
4867 ASSURE_DESTINATION (safe_room);
4868 c = *charbuf++;
4869 if (ASCII_CHAR_P (c))
4870 *dst++ = c;
4871 else if (CHAR_BYTE8_P (c))
4872 *dst++ = CHAR_TO_BYTE8 (c);
4873 else
4874 CHAR_STRING_ADVANCE (c, dst);
4875 }
4876 }
4877 else
4878 {
4879 ASSURE_DESTINATION (charbuf_end - charbuf);
4880 while (charbuf < charbuf_end && dst < dst_end)
4881 *dst++ = *charbuf++;
4882 }
4883 }
4884 record_conversion_result (coding, CODING_RESULT_SUCCESS);
4885 coding->produced_char += charbuf - coding->charbuf;
4886 coding->produced = dst - coding->destination;
4887 return 0;
4888 }
4889
4890 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4891 Check if a text is encoded in a charset-based coding system. If it
4892 is, return 1, else return 0. */
4893
4894 static int
4895 detect_coding_charset (coding, detect_info)
4896 struct coding_system *coding;
4897 struct coding_detection_info *detect_info;
4898 {
4899 const unsigned char *src = coding->source, *src_base;
4900 const unsigned char *src_end = coding->source + coding->src_bytes;
4901 int multibytep = coding->src_multibyte;
4902 int consumed_chars = 0;
4903 Lisp_Object attrs, valids;
4904 int found = 0;
4905 int head_ascii = coding->head_ascii;
4906
4907 detect_info->checked |= CATEGORY_MASK_CHARSET;
4908
4909 coding = &coding_categories[coding_category_charset];
4910 attrs = CODING_ID_ATTRS (coding->id);
4911 valids = AREF (attrs, coding_attr_charset_valids);
4912
4913 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs)))
4914 src += head_ascii;
4915
4916 while (1)
4917 {
4918 int c;
4919 Lisp_Object val;
4920 struct charset *charset;
4921 int dim, idx;
4922
4923 src_base = src;
4924 ONE_MORE_BYTE (c);
4925 if (c < 0)
4926 continue;
4927 val = AREF (valids, c);
4928 if (NILP (val))
4929 break;
4930 if (c >= 0x80)
4931 found = CATEGORY_MASK_CHARSET;
4932 if (INTEGERP (val))
4933 {
4934 charset = CHARSET_FROM_ID (XFASTINT (val));
4935 dim = CHARSET_DIMENSION (charset);
4936 for (idx = 1; idx < dim; idx++)
4937 {
4938 if (src == src_end)
4939 goto too_short;
4940 ONE_MORE_BYTE (c);
4941 if (c < charset->code_space[(dim - 1 - idx) * 2]
4942 || c > charset->code_space[(dim - 1 - idx) * 2 + 1])
4943 break;
4944 }
4945 if (idx < dim)
4946 break;
4947 }
4948 else
4949 {
4950 idx = 1;
4951 for (; CONSP (val); val = XCDR (val))
4952 {
4953 charset = CHARSET_FROM_ID (XFASTINT (XCAR (val)));
4954 dim = CHARSET_DIMENSION (charset);
4955 while (idx < dim)
4956 {
4957 if (src == src_end)
4958 goto too_short;
4959 ONE_MORE_BYTE (c);
4960 if (c < charset->code_space[(dim - 1 - idx) * 4]
4961 || c > charset->code_space[(dim - 1 - idx) * 4 + 1])
4962 break;
4963 idx++;
4964 }
4965 if (idx == dim)
4966 {
4967 val = Qnil;
4968 break;
4969 }
4970 }
4971 if (CONSP (val))
4972 break;
4973 }
4974 }
4975 too_short:
4976 detect_info->rejected |= CATEGORY_MASK_CHARSET;
4977 return 0;
4978
4979 no_more_source:
4980 detect_info->found |= found;
4981 return 1;
4982 }
4983
4984 static void
4985 decode_coding_charset (coding)
4986 struct coding_system *coding;
4987 {
4988 const unsigned char *src = coding->source + coding->consumed;
4989 const unsigned char *src_end = coding->source + coding->src_bytes;
4990 const unsigned char *src_base;
4991 int *charbuf = coding->charbuf + coding->charbuf_used;
4992 int *charbuf_end
4993 = coding->charbuf + coding->charbuf_size - MAX_ANNOTATION_LENGTH;
4994 int consumed_chars = 0, consumed_chars_base;
4995 int multibytep = coding->src_multibyte;
4996 Lisp_Object attrs, charset_list, valids;
4997 int char_offset = coding->produced_char;
4998 int last_offset = char_offset;
4999 int last_id = charset_ascii;
5000 int eol_crlf = EQ (CODING_ID_EOL_TYPE (coding->id), Qdos);
5001 int byte_after_cr = -1;
5002
5003 CODING_GET_INFO (coding, attrs, charset_list);
5004 valids = AREF (attrs, coding_attr_charset_valids);
5005
5006 while (1)
5007 {
5008 int c;
5009 Lisp_Object val;
5010 struct charset *charset;
5011 int dim;
5012 int len = 1;
5013 unsigned code;
5014
5015 src_base = src;
5016 consumed_chars_base = consumed_chars;
5017
5018 if (charbuf >= charbuf_end)
5019 break;
5020
5021 if (byte_after_cr >= 0)
5022 {
5023 c = byte_after_cr;
5024 byte_after_cr = -1;
5025 }
5026 else
5027 {
5028 ONE_MORE_BYTE (c);
5029 if (eol_crlf && c == '\r')
5030 ONE_MORE_BYTE (byte_after_cr);
5031 }
5032 if (c < 0)
5033 goto invalid_code;
5034 code = c;
5035
5036 val = AREF (valids, c);
5037 if (NILP (val))
5038 goto invalid_code;
5039 if (INTEGERP (val))
5040 {
5041 charset = CHARSET_FROM_ID (XFASTINT (val));
5042 dim = CHARSET_DIMENSION (charset);
5043 while (len < dim)
5044 {
5045 ONE_MORE_BYTE (c);
5046 code = (code << 8) | c;
5047 len++;
5048 }
5049 CODING_DECODE_CHAR (coding, src, src_base, src_end,
5050 charset, code, c);
5051 }
5052 else
5053 {
5054 /* VAL is a list of charset IDs. It is assured that the
5055 list is sorted by charset dimensions (smaller one
5056 comes first). */
5057 while (CONSP (val))
5058 {
5059 charset = CHARSET_FROM_ID (XFASTINT (XCAR (val)));
5060 dim = CHARSET_DIMENSION (charset);
5061 while (len < dim)
5062 {
5063 ONE_MORE_BYTE (c);
5064 code = (code << 8) | c;
5065 len++;
5066 }
5067 CODING_DECODE_CHAR (coding, src, src_base,
5068 src_end, charset, code, c);
5069 if (c >= 0)
5070 break;
5071 val = XCDR (val);
5072 }
5073 }
5074 if (c < 0)
5075 goto invalid_code;
5076 if (charset->id != charset_ascii
5077 && last_id != charset->id)
5078 {
5079 if (last_id != charset_ascii)
5080 ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id);
5081 last_id = charset->id;
5082 last_offset = char_offset;
5083 }
5084
5085 *charbuf++ = c;
5086 char_offset++;
5087 continue;
5088
5089 invalid_code:
5090 src = src_base;
5091 consumed_chars = consumed_chars_base;
5092 ONE_MORE_BYTE (c);
5093 *charbuf++ = c < 0 ? -c : ASCII_BYTE_P (c) ? c : BYTE8_TO_CHAR (c);
5094 char_offset++;
5095 coding->errors++;
5096 }
5097
5098 no_more_source:
5099 if (last_id != charset_ascii)
5100 ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id);
5101 coding->consumed_char += consumed_chars_base;
5102 coding->consumed = src_base - coding->source;
5103 coding->charbuf_used = charbuf - coding->charbuf;
5104 }
5105
5106 static int
5107 encode_coding_charset (coding)
5108 struct coding_system *coding;
5109 {
5110 int multibytep = coding->dst_multibyte;
5111 int *charbuf = coding->charbuf;
5112 int *charbuf_end = charbuf + coding->charbuf_used;
5113 unsigned char *dst = coding->destination + coding->produced;
5114 unsigned char *dst_end = coding->destination + coding->dst_bytes;
5115 int safe_room = MAX_MULTIBYTE_LENGTH;
5116 int produced_chars = 0;
5117 Lisp_Object attrs, charset_list;
5118 int ascii_compatible;
5119 int c;
5120
5121 CODING_GET_INFO (coding, attrs, charset_list);
5122 ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs));
5123
5124 while (charbuf < charbuf_end)
5125 {
5126 struct charset *charset;
5127 unsigned code;
5128
5129 ASSURE_DESTINATION (safe_room);
5130 c = *charbuf++;
5131 if (ascii_compatible && ASCII_CHAR_P (c))
5132 EMIT_ONE_ASCII_BYTE (c);
5133 else if (CHAR_BYTE8_P (c))
5134 {
5135 c = CHAR_TO_BYTE8 (c);
5136 EMIT_ONE_BYTE (c);
5137 }
5138 else
5139 {
5140 charset = char_charset (c, charset_list, &code);
5141 if (charset)
5142 {
5143 if (CHARSET_DIMENSION (charset) == 1)
5144 EMIT_ONE_BYTE (code);
5145 else if (CHARSET_DIMENSION (charset) == 2)
5146 EMIT_TWO_BYTES (code >> 8, code & 0xFF);
5147 else if (CHARSET_DIMENSION (charset) == 3)
5148 EMIT_THREE_BYTES (code >> 16, (code >> 8) & 0xFF, code & 0xFF);
5149 else
5150 EMIT_FOUR_BYTES (code >> 24, (code >> 16) & 0xFF,
5151 (code >> 8) & 0xFF, code & 0xFF);
5152 }
5153 else
5154 {
5155 if (coding->mode & CODING_MODE_SAFE_ENCODING)
5156 c = CODING_INHIBIT_CHARACTER_SUBSTITUTION;
5157 else
5158 c = coding->default_char;
5159 EMIT_ONE_BYTE (c);
5160 }
5161 }
5162 }
5163
5164 record_conversion_result (coding, CODING_RESULT_SUCCESS);
5165 coding->produced_char += produced_chars;
5166 coding->produced = dst - coding->destination;
5167 return 0;
5168 }
5169
5170 \f
5171 /*** 7. C library functions ***/
5172
5173 /* Setup coding context CODING from information about CODING_SYSTEM.
5174 If CODING_SYSTEM is nil, `no-conversion' is assumed. If
5175 CODING_SYSTEM is invalid, signal an error. */
5176
5177 void
5178 setup_coding_system (coding_system, coding)
5179 Lisp_Object coding_system;
5180 struct coding_system *coding;
5181 {
5182 Lisp_Object attrs;
5183 Lisp_Object eol_type;
5184 Lisp_Object coding_type;
5185 Lisp_Object val;
5186
5187 if (NILP (coding_system))
5188 coding_system = Qundecided;
5189
5190 CHECK_CODING_SYSTEM_GET_ID (coding_system, coding->id);
5191
5192 attrs = CODING_ID_ATTRS (coding->id);
5193 eol_type = CODING_ID_EOL_TYPE (coding->id);
5194
5195 coding->mode = 0;
5196 coding->head_ascii = -1;
5197 if (VECTORP (eol_type))
5198 coding->common_flags = (CODING_REQUIRE_DECODING_MASK
5199 | CODING_REQUIRE_DETECTION_MASK);
5200 else if (! EQ (eol_type, Qunix))
5201 coding->common_flags = (CODING_REQUIRE_DECODING_MASK
5202 | CODING_REQUIRE_ENCODING_MASK);
5203 else
5204 coding->common_flags = 0;
5205 if (! NILP (CODING_ATTR_POST_READ (attrs)))
5206 coding->common_flags |= CODING_REQUIRE_DECODING_MASK;
5207 if (! NILP (CODING_ATTR_PRE_WRITE (attrs)))
5208 coding->common_flags |= CODING_REQUIRE_ENCODING_MASK;
5209 if (! NILP (CODING_ATTR_FOR_UNIBYTE (attrs)))
5210 coding->common_flags |= CODING_FOR_UNIBYTE_MASK;
5211
5212 val = CODING_ATTR_SAFE_CHARSETS (attrs);
5213 coding->max_charset_id = SCHARS (val) - 1;
5214 coding->safe_charsets = (char *) SDATA (val);
5215 coding->default_char = XINT (CODING_ATTR_DEFAULT_CHAR (attrs));
5216
5217 coding_type = CODING_ATTR_TYPE (attrs);
5218 if (EQ (coding_type, Qundecided))
5219 {
5220 coding->detector = NULL;
5221 coding->decoder = decode_coding_raw_text;
5222 coding->encoder = encode_coding_raw_text;
5223 coding->common_flags |= CODING_REQUIRE_DETECTION_MASK;
5224 }
5225 else if (EQ (coding_type, Qiso_2022))
5226 {
5227 int i;
5228 int flags = XINT (AREF (attrs, coding_attr_iso_flags));
5229
5230 /* Invoke graphic register 0 to plane 0. */
5231 CODING_ISO_INVOCATION (coding, 0) = 0;
5232 /* Invoke graphic register 1 to plane 1 if we can use 8-bit. */
5233 CODING_ISO_INVOCATION (coding, 1)
5234 = (flags & CODING_ISO_FLAG_SEVEN_BITS ? -1 : 1);
5235 /* Setup the initial status of designation. */
5236 for (i = 0; i < 4; i++)
5237 CODING_ISO_DESIGNATION (coding, i) = CODING_ISO_INITIAL (coding, i);
5238 /* Not single shifting initially. */
5239 CODING_ISO_SINGLE_SHIFTING (coding) = 0;
5240 /* Beginning of buffer should also be regarded as bol. */
5241 CODING_ISO_BOL (coding) = 1;
5242 coding->detector = detect_coding_iso_2022;
5243 coding->decoder = decode_coding_iso_2022;
5244 coding->encoder = encode_coding_iso_2022;
5245 if (flags & CODING_ISO_FLAG_SAFE)
5246 coding->mode |= CODING_MODE_SAFE_ENCODING;
5247 coding->common_flags
5248 |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK
5249 | CODING_REQUIRE_FLUSHING_MASK);
5250 if (flags & CODING_ISO_FLAG_COMPOSITION)
5251 coding->common_flags |= CODING_ANNOTATE_COMPOSITION_MASK;
5252 if (flags & CODING_ISO_FLAG_DESIGNATION)
5253 coding->common_flags |= CODING_ANNOTATE_CHARSET_MASK;
5254 if (flags & CODING_ISO_FLAG_FULL_SUPPORT)
5255 {
5256 setup_iso_safe_charsets (attrs);
5257 val = CODING_ATTR_SAFE_CHARSETS (attrs);
5258 coding->max_charset_id = SCHARS (val) - 1;
5259 coding->safe_charsets = (char *) SDATA (val);
5260 }
5261 CODING_ISO_FLAGS (coding) = flags;
5262 }
5263 else if (EQ (coding_type, Qcharset))
5264 {
5265 coding->detector = detect_coding_charset;
5266 coding->decoder = decode_coding_charset;
5267 coding->encoder = encode_coding_charset;
5268 coding->common_flags
5269 |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK);
5270 }
5271 else if (EQ (coding_type, Qutf_8))
5272 {
5273 coding->detector = detect_coding_utf_8;
5274 coding->decoder = decode_coding_utf_8;
5275 coding->encoder = encode_coding_utf_8;
5276 coding->common_flags
5277 |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK);
5278 }
5279 else if (EQ (coding_type, Qutf_16))
5280 {
5281 val = AREF (attrs, coding_attr_utf_16_bom);
5282 CODING_UTF_16_BOM (coding) = (CONSP (val) ? utf_16_detect_bom
5283 : EQ (val, Qt) ? utf_16_with_bom
5284 : utf_16_without_bom);
5285 val = AREF (attrs, coding_attr_utf_16_endian);
5286 CODING_UTF_16_ENDIAN (coding) = (EQ (val, Qbig) ? utf_16_big_endian
5287 : utf_16_little_endian);
5288 CODING_UTF_16_SURROGATE (coding) = 0;
5289 coding->detector = detect_coding_utf_16;
5290 coding->decoder = decode_coding_utf_16;
5291 coding->encoder = encode_coding_utf_16;
5292 coding->common_flags
5293 |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK);
5294 if (CODING_UTF_16_BOM (coding) == utf_16_detect_bom)
5295 coding->common_flags |= CODING_REQUIRE_DETECTION_MASK;
5296 }
5297 else if (EQ (coding_type, Qccl))
5298 {
5299 coding->detector = detect_coding_ccl;
5300 coding->decoder = decode_coding_ccl;
5301 coding->encoder = encode_coding_ccl;
5302 coding->common_flags
5303 |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK
5304 | CODING_REQUIRE_FLUSHING_MASK);
5305 }
5306 else if (EQ (coding_type, Qemacs_mule))
5307 {
5308 coding->detector = detect_coding_emacs_mule;
5309 coding->decoder = decode_coding_emacs_mule;
5310 coding->encoder = encode_coding_emacs_mule;
5311 coding->common_flags
5312 |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK);
5313 if (! NILP (AREF (attrs, coding_attr_emacs_mule_full))
5314 && ! EQ (CODING_ATTR_CHARSET_LIST (attrs), Vemacs_mule_charset_list))
5315 {
5316 Lisp_Object tail, safe_charsets;
5317 int max_charset_id = 0;
5318
5319 for (tail = Vemacs_mule_charset_list; CONSP (tail);
5320 tail = XCDR (tail))
5321 if (max_charset_id < XFASTINT (XCAR (tail)))
5322 max_charset_id = XFASTINT (XCAR (tail));
5323 safe_charsets = Fmake_string (make_number (max_charset_id + 1),
5324 make_number (255));
5325 for (tail = Vemacs_mule_charset_list; CONSP (tail);
5326 tail = XCDR (tail))
5327 SSET (safe_charsets, XFASTINT (XCAR (tail)), 0);
5328 coding->max_charset_id = max_charset_id;
5329 coding->safe_charsets = (char *) SDATA (safe_charsets);
5330 }
5331 }
5332 else if (EQ (coding_type, Qshift_jis))
5333 {
5334 coding->detector = detect_coding_sjis;
5335 coding->decoder = decode_coding_sjis;
5336 coding->encoder = encode_coding_sjis;
5337 coding->common_flags
5338 |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK);
5339 }
5340 else if (EQ (coding_type, Qbig5))
5341 {
5342 coding->detector = detect_coding_big5;
5343 coding->decoder = decode_coding_big5;
5344 coding->encoder = encode_coding_big5;
5345 coding->common_flags
5346 |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK);
5347 }
5348 else /* EQ (coding_type, Qraw_text) */
5349 {
5350 coding->detector = NULL;
5351 coding->decoder = decode_coding_raw_text;
5352 coding->encoder = encode_coding_raw_text;
5353 if (! EQ (eol_type, Qunix))
5354 {
5355 coding->common_flags |= CODING_REQUIRE_DECODING_MASK;
5356 if (! VECTORP (eol_type))
5357 coding->common_flags |= CODING_REQUIRE_ENCODING_MASK;
5358 }
5359
5360 }
5361
5362 return;
5363 }
5364
5365 /* Return a list of charsets supported by CODING. */
5366
5367 Lisp_Object
5368 coding_charset_list (coding)
5369 struct coding_system *coding;
5370 {
5371 Lisp_Object attrs, charset_list;
5372
5373 CODING_GET_INFO (coding, attrs, charset_list);
5374 if (EQ (CODING_ATTR_TYPE (attrs), Qiso_2022))
5375 {
5376 int flags = XINT (AREF (attrs, coding_attr_iso_flags));
5377
5378 if (flags & CODING_ISO_FLAG_FULL_SUPPORT)
5379 charset_list = Viso_2022_charset_list;
5380 }
5381 else if (EQ (CODING_ATTR_TYPE (attrs), Qemacs_mule))
5382 {
5383 charset_list = Vemacs_mule_charset_list;
5384 }
5385 return charset_list;
5386 }
5387
5388
5389 /* Return raw-text or one of its subsidiaries that has the same
5390 eol_type as CODING-SYSTEM. */
5391
5392 Lisp_Object
5393 raw_text_coding_system (coding_system)
5394 Lisp_Object coding_system;
5395 {
5396 Lisp_Object spec, attrs;
5397 Lisp_Object eol_type, raw_text_eol_type;
5398
5399 if (NILP (coding_system))
5400 return Qraw_text;
5401 spec = CODING_SYSTEM_SPEC (coding_system);
5402 attrs = AREF (spec, 0);
5403
5404 if (EQ (CODING_ATTR_TYPE (attrs), Qraw_text))
5405 return coding_system;
5406
5407 eol_type = AREF (spec, 2);
5408 if (VECTORP (eol_type))
5409 return Qraw_text;
5410 spec = CODING_SYSTEM_SPEC (Qraw_text);
5411 raw_text_eol_type = AREF (spec, 2);
5412 return (EQ (eol_type, Qunix) ? AREF (raw_text_eol_type, 0)
5413 : EQ (eol_type, Qdos) ? AREF (raw_text_eol_type, 1)
5414 : AREF (raw_text_eol_type, 2));
5415 }
5416
5417
5418 /* If CODING_SYSTEM doesn't specify end-of-line format but PARENT
5419 does, return one of the subsidiary that has the same eol-spec as
5420 PARENT. Otherwise, return CODING_SYSTEM. If PARENT is nil,
5421 inherit end-of-line format from the system's setting
5422 (system_eol_type). */
5423
5424 Lisp_Object
5425 coding_inherit_eol_type (coding_system, parent)
5426 Lisp_Object coding_system, parent;
5427 {
5428 Lisp_Object spec, eol_type;
5429
5430 if (NILP (coding_system))
5431 coding_system = Qraw_text;
5432 spec = CODING_SYSTEM_SPEC (coding_system);
5433 eol_type = AREF (spec, 2);
5434 if (VECTORP (eol_type))
5435 {
5436 Lisp_Object parent_eol_type;
5437
5438 if (! NILP (parent))
5439 {
5440 Lisp_Object parent_spec;
5441
5442 parent_spec = CODING_SYSTEM_SPEC (parent);
5443 parent_eol_type = AREF (parent_spec, 2);
5444 }
5445 else
5446 parent_eol_type = system_eol_type;
5447 if (EQ (parent_eol_type, Qunix))
5448 coding_system = AREF (eol_type, 0);
5449 else if (EQ (parent_eol_type, Qdos))
5450 coding_system = AREF (eol_type, 1);
5451 else if (EQ (parent_eol_type, Qmac))
5452 coding_system = AREF (eol_type, 2);
5453 }
5454 return coding_system;
5455 }
5456
5457 /* Emacs has a mechanism to automatically detect a coding system if it
5458 is one of Emacs' internal format, ISO2022, SJIS, and BIG5. But,
5459 it's impossible to distinguish some coding systems accurately
5460 because they use the same range of codes. So, at first, coding
5461 systems are categorized into 7, those are:
5462
5463 o coding-category-emacs-mule
5464
5465 The category for a coding system which has the same code range
5466 as Emacs' internal format. Assigned the coding-system (Lisp
5467 symbol) `emacs-mule' by default.
5468
5469 o coding-category-sjis
5470
5471 The category for a coding system which has the same code range
5472 as SJIS. Assigned the coding-system (Lisp
5473 symbol) `japanese-shift-jis' by default.
5474
5475 o coding-category-iso-7
5476
5477 The category for a coding system which has the same code range
5478 as ISO2022 of 7-bit environment. This doesn't use any locking
5479 shift and single shift functions. This can encode/decode all
5480 charsets. Assigned the coding-system (Lisp symbol)
5481 `iso-2022-7bit' by default.
5482
5483 o coding-category-iso-7-tight
5484
5485 Same as coding-category-iso-7 except that this can
5486 encode/decode only the specified charsets.
5487
5488 o coding-category-iso-8-1
5489
5490 The category for a coding system which has the same code range
5491 as ISO2022 of 8-bit environment and graphic plane 1 used only
5492 for DIMENSION1 charset. This doesn't use any locking shift
5493 and single shift functions. Assigned the coding-system (Lisp
5494 symbol) `iso-latin-1' by default.
5495
5496 o coding-category-iso-8-2
5497
5498 The category for a coding system which has the same code range
5499 as ISO2022 of 8-bit environment and graphic plane 1 used only
5500 for DIMENSION2 charset. This doesn't use any locking shift
5501 and single shift functions. Assigned the coding-system (Lisp
5502 symbol) `japanese-iso-8bit' by default.
5503
5504 o coding-category-iso-7-else
5505
5506 The category for a coding system which has the same code range
5507 as ISO2022 of 7-bit environemnt but uses locking shift or
5508 single shift functions. Assigned the coding-system (Lisp
5509 symbol) `iso-2022-7bit-lock' by default.
5510
5511 o coding-category-iso-8-else
5512
5513 The category for a coding system which has the same code range
5514 as ISO2022 of 8-bit environemnt but uses locking shift or
5515 single shift functions. Assigned the coding-system (Lisp
5516 symbol) `iso-2022-8bit-ss2' by default.
5517
5518 o coding-category-big5
5519
5520 The category for a coding system which has the same code range
5521 as BIG5. Assigned the coding-system (Lisp symbol)
5522 `cn-big5' by default.
5523
5524 o coding-category-utf-8
5525
5526 The category for a coding system which has the same code range
5527 as UTF-8 (cf. RFC3629). Assigned the coding-system (Lisp
5528 symbol) `utf-8' by default.
5529
5530 o coding-category-utf-16-be
5531
5532 The category for a coding system in which a text has an
5533 Unicode signature (cf. Unicode Standard) in the order of BIG
5534 endian at the head. Assigned the coding-system (Lisp symbol)
5535 `utf-16-be' by default.
5536
5537 o coding-category-utf-16-le
5538
5539 The category for a coding system in which a text has an
5540 Unicode signature (cf. Unicode Standard) in the order of
5541 LITTLE endian at the head. Assigned the coding-system (Lisp
5542 symbol) `utf-16-le' by default.
5543
5544 o coding-category-ccl
5545
5546 The category for a coding system of which encoder/decoder is
5547 written in CCL programs. The default value is nil, i.e., no
5548 coding system is assigned.
5549
5550 o coding-category-binary
5551
5552 The category for a coding system not categorized in any of the
5553 above. Assigned the coding-system (Lisp symbol)
5554 `no-conversion' by default.
5555
5556 Each of them is a Lisp symbol and the value is an actual
5557 `coding-system's (this is also a Lisp symbol) assigned by a user.
5558 What Emacs does actually is to detect a category of coding system.
5559 Then, it uses a `coding-system' assigned to it. If Emacs can't
5560 decide only one possible category, it selects a category of the
5561 highest priority. Priorities of categories are also specified by a
5562 user in a Lisp variable `coding-category-list'.
5563
5564 */
5565
5566 #define EOL_SEEN_NONE 0
5567 #define EOL_SEEN_LF 1
5568 #define EOL_SEEN_CR 2
5569 #define EOL_SEEN_CRLF 4
5570
5571 /* Detect how end-of-line of a text of length SRC_BYTES pointed by
5572 SOURCE is encoded. If CATEGORY is one of
5573 coding_category_utf_16_XXXX, assume that CR and LF are encoded by
5574 two-byte, else they are encoded by one-byte.
5575
5576 Return one of EOL_SEEN_XXX. */
5577
5578 #define MAX_EOL_CHECK_COUNT 3
5579
5580 static int
5581 detect_eol (source, src_bytes, category)
5582 const unsigned char *source;
5583 EMACS_INT src_bytes;
5584 enum coding_category category;
5585 {
5586 const unsigned char *src = source, *src_end = src + src_bytes;
5587 unsigned char c;
5588 int total = 0;
5589 int eol_seen = EOL_SEEN_NONE;
5590
5591 if ((1 << category) & CATEGORY_MASK_UTF_16)
5592 {
5593 int msb, lsb;
5594
5595 msb = category == (coding_category_utf_16_le
5596 | coding_category_utf_16_le_nosig);
5597 lsb = 1 - msb;
5598
5599 while (src + 1 < src_end)
5600 {
5601 c = src[lsb];
5602 if (src[msb] == 0 && (c == '\n' || c == '\r'))
5603 {
5604 int this_eol;
5605
5606 if (c == '\n')
5607 this_eol = EOL_SEEN_LF;
5608 else if (src + 3 >= src_end
5609 || src[msb + 2] != 0
5610 || src[lsb + 2] != '\n')
5611 this_eol = EOL_SEEN_CR;
5612 else
5613 this_eol = EOL_SEEN_CRLF;
5614
5615 if (eol_seen == EOL_SEEN_NONE)
5616 /* This is the first end-of-line. */
5617 eol_seen = this_eol;
5618 else if (eol_seen != this_eol)
5619 {
5620 /* The found type is different from what found before. */
5621 eol_seen = EOL_SEEN_LF;
5622 break;
5623 }
5624 if (++total == MAX_EOL_CHECK_COUNT)
5625 break;
5626 }
5627 src += 2;
5628 }
5629 }
5630 else
5631 {
5632 while (src < src_end)
5633 {
5634 c = *src++;
5635 if (c == '\n' || c == '\r')
5636 {
5637 int this_eol;
5638
5639 if (c == '\n')
5640 this_eol = EOL_SEEN_LF;
5641 else if (src >= src_end || *src != '\n')
5642 this_eol = EOL_SEEN_CR;
5643 else
5644 this_eol = EOL_SEEN_CRLF, src++;
5645
5646 if (eol_seen == EOL_SEEN_NONE)
5647 /* This is the first end-of-line. */
5648 eol_seen = this_eol;
5649 else if (eol_seen != this_eol)
5650 {
5651 /* The found type is different from what found before. */
5652 eol_seen = EOL_SEEN_LF;
5653 break;
5654 }
5655 if (++total == MAX_EOL_CHECK_COUNT)
5656 break;
5657 }
5658 }
5659 }
5660 return eol_seen;
5661 }
5662
5663
5664 static Lisp_Object
5665 adjust_coding_eol_type (coding, eol_seen)
5666 struct coding_system *coding;
5667 int eol_seen;
5668 {
5669 Lisp_Object eol_type;
5670
5671 eol_type = CODING_ID_EOL_TYPE (coding->id);
5672 if (eol_seen & EOL_SEEN_LF)
5673 {
5674 coding->id = CODING_SYSTEM_ID (AREF (eol_type, 0));
5675 eol_type = Qunix;
5676 }
5677 else if (eol_seen & EOL_SEEN_CRLF)
5678 {
5679 coding->id = CODING_SYSTEM_ID (AREF (eol_type, 1));
5680 eol_type = Qdos;
5681 }
5682 else if (eol_seen & EOL_SEEN_CR)
5683 {
5684 coding->id = CODING_SYSTEM_ID (AREF (eol_type, 2));
5685 eol_type = Qmac;
5686 }
5687 return eol_type;
5688 }
5689
5690 /* Detect how a text specified in CODING is encoded. If a coding
5691 system is detected, update fields of CODING by the detected coding
5692 system. */
5693
5694 void
5695 detect_coding (coding)
5696 struct coding_system *coding;
5697 {
5698 const unsigned char *src, *src_end;
5699
5700 coding->consumed = coding->consumed_char = 0;
5701 coding->produced = coding->produced_char = 0;
5702 coding_set_source (coding);
5703
5704 src_end = coding->source + coding->src_bytes;
5705
5706 /* If we have not yet decided the text encoding type, detect it
5707 now. */
5708 if (EQ (CODING_ATTR_TYPE (CODING_ID_ATTRS (coding->id)), Qundecided))
5709 {
5710 int c, i;
5711 struct coding_detection_info detect_info;
5712 int null_byte_found = 0, eight_bit_found = 0;
5713
5714 detect_info.checked = detect_info.found = detect_info.rejected = 0;
5715 coding->head_ascii = -1;
5716 for (src = coding->source; src < src_end; src++)
5717 {
5718 c = *src;
5719 if (c & 0x80)
5720 {
5721 eight_bit_found = 1;
5722 if (coding->head_ascii < 0)
5723 coding->head_ascii = src - coding->source;
5724 if (null_byte_found)
5725 break;
5726 }
5727 else if (c < 0x20)
5728 {
5729 if ((c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO)
5730 && ! inhibit_iso_escape_detection
5731 && ! detect_info.checked)
5732 {
5733 if (coding->head_ascii < 0)
5734 coding->head_ascii = src - coding->source;
5735 if (detect_coding_iso_2022 (coding, &detect_info))
5736 {
5737 /* We have scanned the whole data. */
5738 if (! (detect_info.rejected & CATEGORY_MASK_ISO_7_ELSE))
5739 /* We didn't find an 8-bit code. We may have
5740 found a null-byte, but it's very rare that
5741 a binary file confirm to ISO-2022. */
5742 src = src_end;
5743 break;
5744 }
5745 }
5746 else if (! c)
5747 {
5748 null_byte_found = 1;
5749 if (eight_bit_found)
5750 break;
5751 }
5752 }
5753 }
5754 if (coding->head_ascii < 0)
5755 coding->head_ascii = src - coding->source;
5756
5757 if (null_byte_found || eight_bit_found
5758 || coding->head_ascii < coding->src_bytes
5759 || detect_info.found)
5760 {
5761 enum coding_category category;
5762 struct coding_system *this;
5763
5764 if (coding->head_ascii == coding->src_bytes)
5765 /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */
5766 for (i = 0; i < coding_category_raw_text; i++)
5767 {
5768 category = coding_priorities[i];
5769 this = coding_categories + category;
5770 if (detect_info.found & (1 << category))
5771 break;
5772 }
5773 else
5774 {
5775 if (null_byte_found)
5776 {
5777 detect_info.checked |= ~CATEGORY_MASK_UTF_16;
5778 detect_info.rejected |= ~CATEGORY_MASK_UTF_16;
5779 }
5780 for (i = 0; i < coding_category_raw_text; i++)
5781 {
5782 category = coding_priorities[i];
5783 this = coding_categories + category;
5784 if (this->id < 0)
5785 {
5786 /* No coding system of this category is defined. */
5787 detect_info.rejected |= (1 << category);
5788 }
5789 else if (category >= coding_category_raw_text)
5790 continue;
5791 else if (detect_info.checked & (1 << category))
5792 {
5793 if (detect_info.found & (1 << category))
5794 break;
5795 }
5796 else if ((*(this->detector)) (coding, &detect_info)
5797 && detect_info.found & (1 << category))
5798 {
5799 if (category == coding_category_utf_16_auto)
5800 {
5801 if (detect_info.found & CATEGORY_MASK_UTF_16_LE)
5802 category = coding_category_utf_16_le;
5803 else
5804 category = coding_category_utf_16_be;
5805 }
5806 break;
5807 }
5808 }
5809
5810 if (i < coding_category_raw_text)
5811 setup_coding_system (CODING_ID_NAME (this->id), coding);
5812 else if (null_byte_found)
5813 setup_coding_system (Qno_conversion, coding);
5814 else if ((detect_info.rejected & CATEGORY_MASK_ANY)
5815 == CATEGORY_MASK_ANY)
5816 setup_coding_system (Qraw_text, coding);
5817 else if (detect_info.rejected)
5818 for (i = 0; i < coding_category_raw_text; i++)
5819 if (! (detect_info.rejected & (1 << coding_priorities[i])))
5820 {
5821 this = coding_categories + coding_priorities[i];
5822 setup_coding_system (CODING_ID_NAME (this->id), coding);
5823 break;
5824 }
5825 }
5826 }
5827 }
5828 else if (XINT (CODING_ATTR_CATEGORY (CODING_ID_ATTRS (coding->id)))
5829 == coding_category_utf_16_auto)
5830 {
5831 Lisp_Object coding_systems;
5832 struct coding_detection_info detect_info;
5833
5834 coding_systems
5835 = AREF (CODING_ID_ATTRS (coding->id), coding_attr_utf_16_bom);
5836 detect_info.found = detect_info.rejected = 0;
5837 if (CONSP (coding_systems)
5838 && detect_coding_utf_16 (coding, &detect_info))
5839 {
5840 if (detect_info.found & CATEGORY_MASK_UTF_16_LE)
5841 setup_coding_system (XCAR (coding_systems), coding);
5842 else if (detect_info.found & CATEGORY_MASK_UTF_16_BE)
5843 setup_coding_system (XCDR (coding_systems), coding);
5844 }
5845 }
5846 }
5847
5848
5849 static void
5850 decode_eol (coding)
5851 struct coding_system *coding;
5852 {
5853 Lisp_Object eol_type;
5854 unsigned char *p, *pbeg, *pend;
5855
5856 eol_type = CODING_ID_EOL_TYPE (coding->id);
5857 if (EQ (eol_type, Qunix))
5858 return;
5859
5860 if (NILP (coding->dst_object))
5861 pbeg = coding->destination;
5862 else
5863 pbeg = BYTE_POS_ADDR (coding->dst_pos_byte);
5864 pend = pbeg + coding->produced;
5865
5866 if (VECTORP (eol_type))
5867 {
5868 int eol_seen = EOL_SEEN_NONE;
5869
5870 for (p = pbeg; p < pend; p++)
5871 {
5872 if (*p == '\n')
5873 eol_seen |= EOL_SEEN_LF;
5874 else if (*p == '\r')
5875 {
5876 if (p + 1 < pend && *(p + 1) == '\n')
5877 {
5878 eol_seen |= EOL_SEEN_CRLF;
5879 p++;
5880 }
5881 else
5882 eol_seen |= EOL_SEEN_CR;
5883 }
5884 }
5885 if (eol_seen != EOL_SEEN_NONE
5886 && eol_seen != EOL_SEEN_LF
5887 && eol_seen != EOL_SEEN_CRLF
5888 && eol_seen != EOL_SEEN_CR)
5889 eol_seen = EOL_SEEN_LF;
5890 if (eol_seen != EOL_SEEN_NONE)
5891 eol_type = adjust_coding_eol_type (coding, eol_seen);
5892 }
5893
5894 if (EQ (eol_type, Qmac))
5895 {
5896 for (p = pbeg; p < pend; p++)
5897 if (*p == '\r')
5898 *p = '\n';
5899 }
5900 else if (EQ (eol_type, Qdos))
5901 {
5902 int n = 0;
5903
5904 if (NILP (coding->dst_object))
5905 {
5906 /* Start deleting '\r' from the tail to minimize the memory
5907 movement. */
5908 for (p = pend - 2; p >= pbeg; p--)
5909 if (*p == '\r')
5910 {
5911 safe_bcopy ((char *) (p + 1), (char *) p, pend-- - p - 1);
5912 n++;
5913 }
5914 }
5915 else
5916 {
5917 int pos_byte = coding->dst_pos_byte;
5918 int pos = coding->dst_pos;
5919 int pos_end = pos + coding->produced_char - 1;
5920
5921 while (pos < pos_end)
5922 {
5923 p = BYTE_POS_ADDR (pos_byte);
5924 if (*p == '\r' && p[1] == '\n')
5925 {
5926 del_range_2 (pos, pos_byte, pos + 1, pos_byte + 1, 0);
5927 n++;
5928 pos_end--;
5929 }
5930 pos++;
5931 if (coding->dst_multibyte)
5932 pos_byte += BYTES_BY_CHAR_HEAD (*p);
5933 else
5934 pos_byte++;
5935 }
5936 }
5937 coding->produced -= n;
5938 coding->produced_char -= n;
5939 }
5940 }
5941
5942
5943 /* Return a translation table (or list of them) from coding system
5944 attribute vector ATTRS for encoding (ENCODEP is nonzero) or
5945 decoding (ENCODEP is zero). */
5946
5947 static Lisp_Object
5948 get_translation_table (attrs, encodep, max_lookup)
5949 Lisp_Object attrs;
5950 int encodep, *max_lookup;
5951 {
5952 Lisp_Object standard, translation_table;
5953 Lisp_Object val;
5954
5955 if (encodep)
5956 translation_table = CODING_ATTR_ENCODE_TBL (attrs),
5957 standard = Vstandard_translation_table_for_encode;
5958 else
5959 translation_table = CODING_ATTR_DECODE_TBL (attrs),
5960 standard = Vstandard_translation_table_for_decode;
5961 if (NILP (translation_table))
5962 translation_table = standard;
5963 else
5964 {
5965 if (SYMBOLP (translation_table))
5966 translation_table = Fget (translation_table, Qtranslation_table);
5967 else if (CONSP (translation_table))
5968 {
5969 translation_table = Fcopy_sequence (translation_table);
5970 for (val = translation_table; CONSP (val); val = XCDR (val))
5971 if (SYMBOLP (XCAR (val)))
5972 XSETCAR (val, Fget (XCAR (val), Qtranslation_table));
5973 }
5974 if (CHAR_TABLE_P (standard))
5975 {
5976 if (CONSP (translation_table))
5977 translation_table = nconc2 (translation_table,
5978 Fcons (standard, Qnil));
5979 else
5980 translation_table = Fcons (translation_table,
5981 Fcons (standard, Qnil));
5982 }
5983 }
5984
5985 if (max_lookup)
5986 {
5987 *max_lookup = 1;
5988 if (CHAR_TABLE_P (translation_table)
5989 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (translation_table)) > 1)
5990 {
5991 val = XCHAR_TABLE (translation_table)->extras[1];
5992 if (NATNUMP (val) && *max_lookup < XFASTINT (val))
5993 *max_lookup = XFASTINT (val);
5994 }
5995 else if (CONSP (translation_table))
5996 {
5997 Lisp_Object tail, val;
5998
5999 for (tail = translation_table; CONSP (tail); tail = XCDR (tail))
6000 if (CHAR_TABLE_P (XCAR (tail))
6001 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (XCAR (tail))) > 1)
6002 {
6003 val = XCHAR_TABLE (XCAR (tail))->extras[1];
6004 if (NATNUMP (val) && *max_lookup < XFASTINT (val))
6005 *max_lookup = XFASTINT (val);
6006 }
6007 }
6008 }
6009 return translation_table;
6010 }
6011
6012 #define LOOKUP_TRANSLATION_TABLE(table, c, trans) \
6013 do { \
6014 trans = Qnil; \
6015 if (CHAR_TABLE_P (table)) \
6016 { \
6017 trans = CHAR_TABLE_REF (table, c); \
6018 if (CHARACTERP (trans)) \
6019 c = XFASTINT (trans), trans = Qnil; \
6020 } \
6021 else if (CONSP (table)) \
6022 { \
6023 Lisp_Object tail; \
6024 \
6025 for (tail = table; CONSP (tail); tail = XCDR (tail)) \
6026 if (CHAR_TABLE_P (XCAR (tail))) \
6027 { \
6028 trans = CHAR_TABLE_REF (XCAR (tail), c); \
6029 if (CHARACTERP (trans)) \
6030 c = XFASTINT (trans), trans = Qnil; \
6031 else if (! NILP (trans)) \
6032 break; \
6033 } \
6034 } \
6035 } while (0)
6036
6037
6038 static Lisp_Object
6039 get_translation (val, buf, buf_end, last_block, from_nchars, to_nchars)
6040 Lisp_Object val;
6041 int *buf, *buf_end;
6042 int last_block;
6043 int *from_nchars, *to_nchars;
6044 {
6045 /* VAL is TO or (([FROM-CHAR ...] . TO) ...) where TO is TO-CHAR or
6046 [TO-CHAR ...]. */
6047 if (CONSP (val))
6048 {
6049 Lisp_Object from, tail;
6050 int i, len;
6051
6052 for (tail = val; CONSP (tail); tail = XCDR (tail))
6053 {
6054 val = XCAR (tail);
6055 from = XCAR (val);
6056 len = ASIZE (from);
6057 for (i = 0; i < len; i++)
6058 {
6059 if (buf + i == buf_end)
6060 {
6061 if (! last_block)
6062 return Qt;
6063 break;
6064 }
6065 if (XINT (AREF (from, i)) != buf[i])
6066 break;
6067 }
6068 if (i == len)
6069 {
6070 val = XCDR (val);
6071 *from_nchars = len;
6072 break;
6073 }
6074 }
6075 if (! CONSP (tail))
6076 return Qnil;
6077 }
6078 if (VECTORP (val))
6079 *buf = XINT (AREF (val, 0)), *to_nchars = ASIZE (val);
6080 else
6081 *buf = XINT (val);
6082 return val;
6083 }
6084
6085
6086 static int
6087 produce_chars (coding, translation_table, last_block)
6088 struct coding_system *coding;
6089 Lisp_Object translation_table;
6090 int last_block;
6091 {
6092 unsigned char *dst = coding->destination + coding->produced;
6093 unsigned char *dst_end = coding->destination + coding->dst_bytes;
6094 EMACS_INT produced;
6095 EMACS_INT produced_chars = 0;
6096 int carryover = 0;
6097
6098 if (! coding->chars_at_source)
6099 {
6100 /* Source characters are in coding->charbuf. */
6101 int *buf = coding->charbuf;
6102 int *buf_end = buf + coding->charbuf_used;
6103
6104 if (EQ (coding->src_object, coding->dst_object))
6105 {
6106 coding_set_source (coding);
6107 dst_end = ((unsigned char *) coding->source) + coding->consumed;
6108 }
6109
6110 while (buf < buf_end)
6111 {
6112 int c = *buf, i;
6113
6114 if (c >= 0)
6115 {
6116 int from_nchars = 1, to_nchars = 1;
6117 Lisp_Object trans = Qnil;
6118
6119 LOOKUP_TRANSLATION_TABLE (translation_table, c, trans);
6120 if (! NILP (trans))
6121 {
6122 trans = get_translation (trans, buf, buf_end, last_block,
6123 &from_nchars, &to_nchars);
6124 if (EQ (trans, Qt))
6125 break;
6126 c = *buf;
6127 }
6128
6129 if (dst + MAX_MULTIBYTE_LENGTH * to_nchars > dst_end)
6130 {
6131 dst = alloc_destination (coding,
6132 buf_end - buf
6133 + MAX_MULTIBYTE_LENGTH * to_nchars,
6134 dst);
6135 if (EQ (coding->src_object, coding->dst_object))
6136 {
6137 coding_set_source (coding);
6138 dst_end = ((unsigned char *) coding->source) + coding->consumed;
6139 }
6140 else
6141 dst_end = coding->destination + coding->dst_bytes;
6142 }
6143
6144 for (i = 0; i < to_nchars; i++)
6145 {
6146 if (i > 0)
6147 c = XINT (AREF (trans, i));
6148 if (coding->dst_multibyte
6149 || ! CHAR_BYTE8_P (c))
6150 CHAR_STRING_ADVANCE_NO_UNIFY (c, dst);
6151 else
6152 *dst++ = CHAR_TO_BYTE8 (c);
6153 }
6154 produced_chars += to_nchars;
6155 *buf++ = to_nchars;
6156 while (--from_nchars > 0)
6157 *buf++ = 0;
6158 }
6159 else
6160 /* This is an annotation datum. (-C) is the length. */
6161 buf += -c;
6162 }
6163 carryover = buf_end - buf;
6164 }
6165 else
6166 {
6167 /* Source characters are at coding->source. */
6168 const unsigned char *src = coding->source;
6169 const unsigned char *src_end = src + coding->consumed;
6170
6171 if (EQ (coding->dst_object, coding->src_object))
6172 dst_end = (unsigned char *) src;
6173 if (coding->src_multibyte != coding->dst_multibyte)
6174 {
6175 if (coding->src_multibyte)
6176 {
6177 int multibytep = 1;
6178 EMACS_INT consumed_chars;
6179
6180 while (1)
6181 {
6182 const unsigned char *src_base = src;
6183 int c;
6184
6185 ONE_MORE_BYTE (c);
6186 if (dst == dst_end)
6187 {
6188 if (EQ (coding->src_object, coding->dst_object))
6189 dst_end = (unsigned char *) src;
6190 if (dst == dst_end)
6191 {
6192 EMACS_INT offset = src - coding->source;
6193
6194 dst = alloc_destination (coding, src_end - src + 1,
6195 dst);
6196 dst_end = coding->destination + coding->dst_bytes;
6197 coding_set_source (coding);
6198 src = coding->source + offset;
6199 src_end = coding->source + coding->src_bytes;
6200 if (EQ (coding->src_object, coding->dst_object))
6201 dst_end = (unsigned char *) src;
6202 }
6203 }
6204 *dst++ = c;
6205 produced_chars++;
6206 }
6207 no_more_source:
6208 ;
6209 }
6210 else
6211 while (src < src_end)
6212 {
6213 int multibytep = 1;
6214 int c = *src++;
6215
6216 if (dst >= dst_end - 1)
6217 {
6218 if (EQ (coding->src_object, coding->dst_object))
6219 dst_end = (unsigned char *) src;
6220 if (dst >= dst_end - 1)
6221 {
6222 EMACS_INT offset = src - coding->source;
6223 EMACS_INT more_bytes;
6224
6225 if (EQ (coding->src_object, coding->dst_object))
6226 more_bytes = ((src_end - src) / 2) + 2;
6227 else
6228 more_bytes = src_end - src + 2;
6229 dst = alloc_destination (coding, more_bytes, dst);
6230 dst_end = coding->destination + coding->dst_bytes;
6231 coding_set_source (coding);
6232 src = coding->source + offset;
6233 src_end = coding->source + coding->src_bytes;
6234 if (EQ (coding->src_object, coding->dst_object))
6235 dst_end = (unsigned char *) src;
6236 }
6237 }
6238 EMIT_ONE_BYTE (c);
6239 }
6240 }
6241 else
6242 {
6243 if (!EQ (coding->src_object, coding->dst_object))
6244 {
6245 EMACS_INT require = coding->src_bytes - coding->dst_bytes;
6246
6247 if (require > 0)
6248 {
6249 EMACS_INT offset = src - coding->source;
6250
6251 dst = alloc_destination (coding, require, dst);
6252 coding_set_source (coding);
6253 src = coding->source + offset;
6254 src_end = coding->source + coding->src_bytes;
6255 }
6256 }
6257 produced_chars = coding->consumed_char;
6258 while (src < src_end)
6259 *dst++ = *src++;
6260 }
6261 }
6262
6263 produced = dst - (coding->destination + coding->produced);
6264 if (BUFFERP (coding->dst_object) && produced_chars > 0)
6265 insert_from_gap (produced_chars, produced);
6266 coding->produced += produced;
6267 coding->produced_char += produced_chars;
6268 return carryover;
6269 }
6270
6271 /* Compose text in CODING->object according to the annotation data at
6272 CHARBUF. CHARBUF is an array:
6273 [ -LENGTH ANNOTATION_MASK FROM TO METHOD COMP_LEN [ COMPONENTS... ] ]
6274 */
6275
6276 static INLINE void
6277 produce_composition (coding, charbuf, pos)
6278 struct coding_system *coding;
6279 int *charbuf;
6280 EMACS_INT pos;
6281 {
6282 int len;
6283 EMACS_INT to;
6284 enum composition_method method;
6285 Lisp_Object components;
6286
6287 len = -charbuf[0];
6288 to = pos + charbuf[2];
6289 if (to <= pos)
6290 return;
6291 method = (enum composition_method) (charbuf[3]);
6292
6293 if (method == COMPOSITION_RELATIVE)
6294 components = Qnil;
6295 else if (method >= COMPOSITION_WITH_RULE
6296 && method <= COMPOSITION_WITH_RULE_ALTCHARS)
6297 {
6298 Lisp_Object args[MAX_COMPOSITION_COMPONENTS * 2 - 1];
6299 int i;
6300
6301 len -= 4;
6302 charbuf += 4;
6303 for (i = 0; i < len; i++)
6304 {
6305 args[i] = make_number (charbuf[i]);
6306 if (charbuf[i] < 0)
6307 return;
6308 }
6309 components = (method == COMPOSITION_WITH_ALTCHARS
6310 ? Fstring (len, args) : Fvector (len, args));
6311 }
6312 else
6313 return;
6314 compose_text (pos, to, components, Qnil, coding->dst_object);
6315 }
6316
6317
6318 /* Put `charset' property on text in CODING->object according to
6319 the annotation data at CHARBUF. CHARBUF is an array:
6320 [ -LENGTH ANNOTATION_MASK NCHARS CHARSET-ID ]
6321 */
6322
6323 static INLINE void
6324 produce_charset (coding, charbuf, pos)
6325 struct coding_system *coding;
6326 int *charbuf;
6327 EMACS_INT pos;
6328 {
6329 EMACS_INT from = pos - charbuf[2];
6330 struct charset *charset = CHARSET_FROM_ID (charbuf[3]);
6331
6332 Fput_text_property (make_number (from), make_number (pos),
6333 Qcharset, CHARSET_NAME (charset),
6334 coding->dst_object);
6335 }
6336
6337
6338 #define CHARBUF_SIZE 0x4000
6339
6340 #define ALLOC_CONVERSION_WORK_AREA(coding) \
6341 do { \
6342 int size = CHARBUF_SIZE;; \
6343 \
6344 coding->charbuf = NULL; \
6345 while (size > 1024) \
6346 { \
6347 coding->charbuf = (int *) alloca (sizeof (int) * size); \
6348 if (coding->charbuf) \
6349 break; \
6350 size >>= 1; \
6351 } \
6352 if (! coding->charbuf) \
6353 { \
6354 record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_MEM); \
6355 return coding->result; \
6356 } \
6357 coding->charbuf_size = size; \
6358 } while (0)
6359
6360
6361 static void
6362 produce_annotation (coding, pos)
6363 struct coding_system *coding;
6364 EMACS_INT pos;
6365 {
6366 int *charbuf = coding->charbuf;
6367 int *charbuf_end = charbuf + coding->charbuf_used;
6368
6369 if (NILP (coding->dst_object))
6370 return;
6371
6372 while (charbuf < charbuf_end)
6373 {
6374 if (*charbuf >= 0)
6375 pos += *charbuf++;
6376 else
6377 {
6378 int len = -*charbuf;
6379 switch (charbuf[1])
6380 {
6381 case CODING_ANNOTATE_COMPOSITION_MASK:
6382 produce_composition (coding, charbuf, pos);
6383 break;
6384 case CODING_ANNOTATE_CHARSET_MASK:
6385 produce_charset (coding, charbuf, pos);
6386 break;
6387 default:
6388 abort ();
6389 }
6390 charbuf += len;
6391 }
6392 }
6393 }
6394
6395 /* Decode the data at CODING->src_object into CODING->dst_object.
6396 CODING->src_object is a buffer, a string, or nil.
6397 CODING->dst_object is a buffer.
6398
6399 If CODING->src_object is a buffer, it must be the current buffer.
6400 In this case, if CODING->src_pos is positive, it is a position of
6401 the source text in the buffer, otherwise, the source text is in the
6402 gap area of the buffer, and CODING->src_pos specifies the offset of
6403 the text from GPT (which must be the same as PT). If this is the
6404 same buffer as CODING->dst_object, CODING->src_pos must be
6405 negative.
6406
6407 If CODING->src_object is a string, CODING->src_pos is an index to
6408 that string.
6409
6410 If CODING->src_object is nil, CODING->source must already point to
6411 the non-relocatable memory area. In this case, CODING->src_pos is
6412 an offset from CODING->source.
6413
6414 The decoded data is inserted at the current point of the buffer
6415 CODING->dst_object.
6416 */
6417
6418 static int
6419 decode_coding (coding)
6420 struct coding_system *coding;
6421 {
6422 Lisp_Object attrs;
6423 Lisp_Object undo_list;
6424 Lisp_Object translation_table;
6425 int carryover;
6426 int i;
6427
6428 if (BUFFERP (coding->src_object)
6429 && coding->src_pos > 0
6430 && coding->src_pos < GPT
6431 && coding->src_pos + coding->src_chars > GPT)
6432 move_gap_both (coding->src_pos, coding->src_pos_byte);
6433
6434 undo_list = Qt;
6435 if (BUFFERP (coding->dst_object))
6436 {
6437 if (current_buffer != XBUFFER (coding->dst_object))
6438 set_buffer_internal (XBUFFER (coding->dst_object));
6439 if (GPT != PT)
6440 move_gap_both (PT, PT_BYTE);
6441 undo_list = current_buffer->undo_list;
6442 current_buffer->undo_list = Qt;
6443 }
6444
6445 coding->consumed = coding->consumed_char = 0;
6446 coding->produced = coding->produced_char = 0;
6447 coding->chars_at_source = 0;
6448 record_conversion_result (coding, CODING_RESULT_SUCCESS);
6449 coding->errors = 0;
6450
6451 ALLOC_CONVERSION_WORK_AREA (coding);
6452
6453 attrs = CODING_ID_ATTRS (coding->id);
6454 translation_table = get_translation_table (attrs, 0, NULL);
6455
6456 carryover = 0;
6457 do
6458 {
6459 EMACS_INT pos = coding->dst_pos + coding->produced_char;
6460
6461 coding_set_source (coding);
6462 coding->annotated = 0;
6463 coding->charbuf_used = carryover;
6464 (*(coding->decoder)) (coding);
6465 coding_set_destination (coding);
6466 carryover = produce_chars (coding, translation_table, 0);
6467 if (coding->annotated)
6468 produce_annotation (coding, pos);
6469 for (i = 0; i < carryover; i++)
6470 coding->charbuf[i]
6471 = coding->charbuf[coding->charbuf_used - carryover + i];
6472 }
6473 while (coding->consumed < coding->src_bytes
6474 && (coding->result == CODING_RESULT_SUCCESS
6475 || coding->result == CODING_RESULT_INVALID_SRC));
6476
6477 if (carryover > 0)
6478 {
6479 coding_set_destination (coding);
6480 coding->charbuf_used = carryover;
6481 produce_chars (coding, translation_table, 1);
6482 }
6483
6484 coding->carryover_bytes = 0;
6485 if (coding->consumed < coding->src_bytes)
6486 {
6487 int nbytes = coding->src_bytes - coding->consumed;
6488 const unsigned char *src;
6489
6490 coding_set_source (coding);
6491 coding_set_destination (coding);
6492 src = coding->source + coding->consumed;
6493
6494 if (coding->mode & CODING_MODE_LAST_BLOCK)
6495 {
6496 /* Flush out unprocessed data as binary chars. We are sure
6497 that the number of data is less than the size of
6498 coding->charbuf. */
6499 coding->charbuf_used = 0;
6500 while (nbytes-- > 0)
6501 {
6502 int c = *src++;
6503
6504 if (c & 0x80)
6505 c = BYTE8_TO_CHAR (c);
6506 coding->charbuf[coding->charbuf_used++] = c;
6507 }
6508 produce_chars (coding, Qnil, 1);
6509 }
6510 else
6511 {
6512 /* Record unprocessed bytes in coding->carryover. We are
6513 sure that the number of data is less than the size of
6514 coding->carryover. */
6515 unsigned char *p = coding->carryover;
6516
6517 coding->carryover_bytes = nbytes;
6518 while (nbytes-- > 0)
6519 *p++ = *src++;
6520 }
6521 coding->consumed = coding->src_bytes;
6522 }
6523
6524 if (! EQ (CODING_ID_EOL_TYPE (coding->id), Qunix))
6525 decode_eol (coding);
6526 if (BUFFERP (coding->dst_object))
6527 {
6528 current_buffer->undo_list = undo_list;
6529 record_insert (coding->dst_pos, coding->produced_char);
6530 }
6531 return coding->result;
6532 }
6533
6534
6535 /* Extract an annotation datum from a composition starting at POS and
6536 ending before LIMIT of CODING->src_object (buffer or string), store
6537 the data in BUF, set *STOP to a starting position of the next
6538 composition (if any) or to LIMIT, and return the address of the
6539 next element of BUF.
6540
6541 If such an annotation is not found, set *STOP to a starting
6542 position of a composition after POS (if any) or to LIMIT, and
6543 return BUF. */
6544
6545 static INLINE int *
6546 handle_composition_annotation (pos, limit, coding, buf, stop)
6547 EMACS_INT pos, limit;
6548 struct coding_system *coding;
6549 int *buf;
6550 EMACS_INT *stop;
6551 {
6552 EMACS_INT start, end;
6553 Lisp_Object prop;
6554
6555 if (! find_composition (pos, limit, &start, &end, &prop, coding->src_object)
6556 || end > limit)
6557 *stop = limit;
6558 else if (start > pos)
6559 *stop = start;
6560 else
6561 {
6562 if (start == pos)
6563 {
6564 /* We found a composition. Store the corresponding
6565 annotation data in BUF. */
6566 int *head = buf;
6567 enum composition_method method = COMPOSITION_METHOD (prop);
6568 int nchars = COMPOSITION_LENGTH (prop);
6569
6570 ADD_COMPOSITION_DATA (buf, nchars, method);
6571 if (method != COMPOSITION_RELATIVE)
6572 {
6573 Lisp_Object components;
6574 int len, i, i_byte;
6575
6576 components = COMPOSITION_COMPONENTS (prop);
6577 if (VECTORP (components))
6578 {
6579 len = XVECTOR (components)->size;
6580 for (i = 0; i < len; i++)
6581 *buf++ = XINT (AREF (components, i));
6582 }
6583 else if (STRINGP (components))
6584 {
6585 len = SCHARS (components);
6586 i = i_byte = 0;
6587 while (i < len)
6588 {
6589 FETCH_STRING_CHAR_ADVANCE (*buf, components, i, i_byte);
6590 buf++;
6591 }
6592 }
6593 else if (INTEGERP (components))
6594 {
6595 len = 1;
6596 *buf++ = XINT (components);
6597 }
6598 else if (CONSP (components))
6599 {
6600 for (len = 0; CONSP (components);
6601 len++, components = XCDR (components))
6602 *buf++ = XINT (XCAR (components));
6603 }
6604 else
6605 abort ();
6606 *head -= len;
6607 }
6608 }
6609
6610 if (find_composition (end, limit, &start, &end, &prop,
6611 coding->src_object)
6612 && end <= limit)
6613 *stop = start;
6614 else
6615 *stop = limit;
6616 }
6617 return buf;
6618 }
6619
6620
6621 /* Extract an annotation datum from a text property `charset' at POS of
6622 CODING->src_object (buffer of string), store the data in BUF, set
6623 *STOP to the position where the value of `charset' property changes
6624 (limiting by LIMIT), and return the address of the next element of
6625 BUF.
6626
6627 If the property value is nil, set *STOP to the position where the
6628 property value is non-nil (limiting by LIMIT), and return BUF. */
6629
6630 static INLINE int *
6631 handle_charset_annotation (pos, limit, coding, buf, stop)
6632 EMACS_INT pos, limit;
6633 struct coding_system *coding;
6634 int *buf;
6635 EMACS_INT *stop;
6636 {
6637 Lisp_Object val, next;
6638 int id;
6639
6640 val = Fget_text_property (make_number (pos), Qcharset, coding->src_object);
6641 if (! NILP (val) && CHARSETP (val))
6642 id = XINT (CHARSET_SYMBOL_ID (val));
6643 else
6644 id = -1;
6645 ADD_CHARSET_DATA (buf, 0, id);
6646 next = Fnext_single_property_change (make_number (pos), Qcharset,
6647 coding->src_object,
6648 make_number (limit));
6649 *stop = XINT (next);
6650 return buf;
6651 }
6652
6653
6654 static void
6655 consume_chars (coding, translation_table, max_lookup)
6656 struct coding_system *coding;
6657 Lisp_Object translation_table;
6658 int max_lookup;
6659 {
6660 int *buf = coding->charbuf;
6661 int *buf_end = coding->charbuf + coding->charbuf_size;
6662 const unsigned char *src = coding->source + coding->consumed;
6663 const unsigned char *src_end = coding->source + coding->src_bytes;
6664 EMACS_INT pos = coding->src_pos + coding->consumed_char;
6665 EMACS_INT end_pos = coding->src_pos + coding->src_chars;
6666 int multibytep = coding->src_multibyte;
6667 Lisp_Object eol_type;
6668 int c;
6669 EMACS_INT stop, stop_composition, stop_charset;
6670 int *lookup_buf = NULL;
6671
6672 if (! NILP (translation_table))
6673 lookup_buf = alloca (sizeof (int) * max_lookup);
6674
6675 eol_type = CODING_ID_EOL_TYPE (coding->id);
6676 if (VECTORP (eol_type))
6677 eol_type = Qunix;
6678
6679 /* Note: composition handling is not yet implemented. */
6680 coding->common_flags &= ~CODING_ANNOTATE_COMPOSITION_MASK;
6681
6682 if (NILP (coding->src_object))
6683 stop = stop_composition = stop_charset = end_pos;
6684 else
6685 {
6686 if (coding->common_flags & CODING_ANNOTATE_COMPOSITION_MASK)
6687 stop = stop_composition = pos;
6688 else
6689 stop = stop_composition = end_pos;
6690 if (coding->common_flags & CODING_ANNOTATE_CHARSET_MASK)
6691 stop = stop_charset = pos;
6692 else
6693 stop_charset = end_pos;
6694 }
6695
6696 /* Compensate for CRLF and conversion. */
6697 buf_end -= 1 + MAX_ANNOTATION_LENGTH;
6698 while (buf < buf_end)
6699 {
6700 Lisp_Object trans;
6701
6702 if (pos == stop)
6703 {
6704 if (pos == end_pos)
6705 break;
6706 if (pos == stop_composition)
6707 buf = handle_composition_annotation (pos, end_pos, coding,
6708 buf, &stop_composition);
6709 if (pos == stop_charset)
6710 buf = handle_charset_annotation (pos, end_pos, coding,
6711 buf, &stop_charset);
6712 stop = (stop_composition < stop_charset
6713 ? stop_composition : stop_charset);
6714 }
6715
6716 if (! multibytep)
6717 {
6718 EMACS_INT bytes;
6719
6720 if (coding->encoder == encode_coding_raw_text)
6721 c = *src++, pos++;
6722 else if ((bytes = MULTIBYTE_LENGTH (src, src_end)) > 0)
6723 c = STRING_CHAR_ADVANCE_NO_UNIFY (src), pos += bytes;
6724 else
6725 c = BYTE8_TO_CHAR (*src), src++, pos++;
6726 }
6727 else
6728 c = STRING_CHAR_ADVANCE_NO_UNIFY (src), pos++;
6729 if ((c == '\r') && (coding->mode & CODING_MODE_SELECTIVE_DISPLAY))
6730 c = '\n';
6731 if (! EQ (eol_type, Qunix))
6732 {
6733 if (c == '\n')
6734 {
6735 if (EQ (eol_type, Qdos))
6736 *buf++ = '\r';
6737 else
6738 c = '\r';
6739 }
6740 }
6741
6742 trans = Qnil;
6743 LOOKUP_TRANSLATION_TABLE (translation_table, c, trans);
6744 if (NILP (trans))
6745 *buf++ = c;
6746 else
6747 {
6748 int from_nchars = 1, to_nchars = 1;
6749 int *lookup_buf_end;
6750 const unsigned char *p = src;
6751 int i;
6752
6753 lookup_buf[0] = c;
6754 for (i = 1; i < max_lookup && p < src_end; i++)
6755 lookup_buf[i] = STRING_CHAR_ADVANCE (p);
6756 lookup_buf_end = lookup_buf + i;
6757 trans = get_translation (trans, lookup_buf, lookup_buf_end, 1,
6758 &from_nchars, &to_nchars);
6759 if (EQ (trans, Qt)
6760 || buf + to_nchars > buf_end)
6761 break;
6762 *buf++ = *lookup_buf;
6763 for (i = 1; i < to_nchars; i++)
6764 *buf++ = XINT (AREF (trans, i));
6765 for (i = 1; i < from_nchars; i++, pos++)
6766 src += MULTIBYTE_LENGTH_NO_CHECK (src);
6767 }
6768 }
6769
6770 coding->consumed = src - coding->source;
6771 coding->consumed_char = pos - coding->src_pos;
6772 coding->charbuf_used = buf - coding->charbuf;
6773 coding->chars_at_source = 0;
6774 }
6775
6776
6777 /* Encode the text at CODING->src_object into CODING->dst_object.
6778 CODING->src_object is a buffer or a string.
6779 CODING->dst_object is a buffer or nil.
6780
6781 If CODING->src_object is a buffer, it must be the current buffer.
6782 In this case, if CODING->src_pos is positive, it is a position of
6783 the source text in the buffer, otherwise. the source text is in the
6784 gap area of the buffer, and coding->src_pos specifies the offset of
6785 the text from GPT (which must be the same as PT). If this is the
6786 same buffer as CODING->dst_object, CODING->src_pos must be
6787 negative and CODING should not have `pre-write-conversion'.
6788
6789 If CODING->src_object is a string, CODING should not have
6790 `pre-write-conversion'.
6791
6792 If CODING->dst_object is a buffer, the encoded data is inserted at
6793 the current point of that buffer.
6794
6795 If CODING->dst_object is nil, the encoded data is placed at the
6796 memory area specified by CODING->destination. */
6797
6798 static int
6799 encode_coding (coding)
6800 struct coding_system *coding;
6801 {
6802 Lisp_Object attrs;
6803 Lisp_Object translation_table;
6804 int max_lookup;
6805
6806 attrs = CODING_ID_ATTRS (coding->id);
6807 if (coding->encoder == encode_coding_raw_text)
6808 translation_table = Qnil, max_lookup = 0;
6809 else
6810 translation_table = get_translation_table (attrs, 1, &max_lookup);
6811
6812 if (BUFFERP (coding->dst_object))
6813 {
6814 set_buffer_internal (XBUFFER (coding->dst_object));
6815 coding->dst_multibyte
6816 = ! NILP (current_buffer->enable_multibyte_characters);
6817 }
6818
6819 coding->consumed = coding->consumed_char = 0;
6820 coding->produced = coding->produced_char = 0;
6821 record_conversion_result (coding, CODING_RESULT_SUCCESS);
6822 coding->errors = 0;
6823
6824 ALLOC_CONVERSION_WORK_AREA (coding);
6825
6826 do {
6827 coding_set_source (coding);
6828 consume_chars (coding, translation_table, max_lookup);
6829 coding_set_destination (coding);
6830 (*(coding->encoder)) (coding);
6831 } while (coding->consumed_char < coding->src_chars);
6832
6833 if (BUFFERP (coding->dst_object) && coding->produced_char > 0)
6834 insert_from_gap (coding->produced_char, coding->produced);
6835
6836 return (coding->result);
6837 }
6838
6839
6840 /* Name (or base name) of work buffer for code conversion. */
6841 static Lisp_Object Vcode_conversion_workbuf_name;
6842
6843 /* A working buffer used by the top level conversion. Once it is
6844 created, it is never destroyed. It has the name
6845 Vcode_conversion_workbuf_name. The other working buffers are
6846 destroyed after the use is finished, and their names are modified
6847 versions of Vcode_conversion_workbuf_name. */
6848 static Lisp_Object Vcode_conversion_reused_workbuf;
6849
6850 /* 1 iff Vcode_conversion_reused_workbuf is already in use. */
6851 static int reused_workbuf_in_use;
6852
6853
6854 /* Return a working buffer of code convesion. MULTIBYTE specifies the
6855 multibyteness of returning buffer. */
6856
6857 static Lisp_Object
6858 make_conversion_work_buffer (multibyte)
6859 int multibyte;
6860 {
6861 Lisp_Object name, workbuf;
6862 struct buffer *current;
6863
6864 if (reused_workbuf_in_use++)
6865 {
6866 name = Fgenerate_new_buffer_name (Vcode_conversion_workbuf_name, Qnil);
6867 workbuf = Fget_buffer_create (name);
6868 }
6869 else
6870 {
6871 name = Vcode_conversion_workbuf_name;
6872 workbuf = Fget_buffer_create (name);
6873 if (NILP (Vcode_conversion_reused_workbuf))
6874 Vcode_conversion_reused_workbuf = workbuf;
6875 }
6876 current = current_buffer;
6877 set_buffer_internal (XBUFFER (workbuf));
6878 Ferase_buffer ();
6879 current_buffer->undo_list = Qt;
6880 current_buffer->enable_multibyte_characters = multibyte ? Qt : Qnil;
6881 set_buffer_internal (current);
6882 return workbuf;
6883 }
6884
6885
6886 static Lisp_Object
6887 code_conversion_restore (arg)
6888 Lisp_Object arg;
6889 {
6890 Lisp_Object current, workbuf;
6891 struct gcpro gcpro1;
6892
6893 GCPRO1 (arg);
6894 current = XCAR (arg);
6895 workbuf = XCDR (arg);
6896 if (! NILP (workbuf))
6897 {
6898 if (EQ (workbuf, Vcode_conversion_reused_workbuf))
6899 reused_workbuf_in_use = 0;
6900 else if (! NILP (Fbuffer_live_p (workbuf)))
6901 Fkill_buffer (workbuf);
6902 }
6903 set_buffer_internal (XBUFFER (current));
6904 UNGCPRO;
6905 return Qnil;
6906 }
6907
6908 Lisp_Object
6909 code_conversion_save (with_work_buf, multibyte)
6910 int with_work_buf, multibyte;
6911 {
6912 Lisp_Object workbuf = Qnil;
6913
6914 if (with_work_buf)
6915 workbuf = make_conversion_work_buffer (multibyte);
6916 record_unwind_protect (code_conversion_restore,
6917 Fcons (Fcurrent_buffer (), workbuf));
6918 return workbuf;
6919 }
6920
6921 int
6922 decode_coding_gap (coding, chars, bytes)
6923 struct coding_system *coding;
6924 EMACS_INT chars, bytes;
6925 {
6926 int count = specpdl_ptr - specpdl;
6927 Lisp_Object attrs;
6928
6929 code_conversion_save (0, 0);
6930
6931 coding->src_object = Fcurrent_buffer ();
6932 coding->src_chars = chars;
6933 coding->src_bytes = bytes;
6934 coding->src_pos = -chars;
6935 coding->src_pos_byte = -bytes;
6936 coding->src_multibyte = chars < bytes;
6937 coding->dst_object = coding->src_object;
6938 coding->dst_pos = PT;
6939 coding->dst_pos_byte = PT_BYTE;
6940 coding->dst_multibyte = ! NILP (current_buffer->enable_multibyte_characters);
6941
6942 if (CODING_REQUIRE_DETECTION (coding))
6943 detect_coding (coding);
6944
6945 coding->mode |= CODING_MODE_LAST_BLOCK;
6946 current_buffer->text->inhibit_shrinking = 1;
6947 decode_coding (coding);
6948 current_buffer->text->inhibit_shrinking = 0;
6949
6950 attrs = CODING_ID_ATTRS (coding->id);
6951 if (! NILP (CODING_ATTR_POST_READ (attrs)))
6952 {
6953 EMACS_INT prev_Z = Z, prev_Z_BYTE = Z_BYTE;
6954 Lisp_Object val;
6955
6956 TEMP_SET_PT_BOTH (coding->dst_pos, coding->dst_pos_byte);
6957 val = call1 (CODING_ATTR_POST_READ (attrs),
6958 make_number (coding->produced_char));
6959 CHECK_NATNUM (val);
6960 coding->produced_char += Z - prev_Z;
6961 coding->produced += Z_BYTE - prev_Z_BYTE;
6962 }
6963
6964 unbind_to (count, Qnil);
6965 return coding->result;
6966 }
6967
6968 int
6969 encode_coding_gap (coding, chars, bytes)
6970 struct coding_system *coding;
6971 EMACS_INT chars, bytes;
6972 {
6973 int count = specpdl_ptr - specpdl;
6974
6975 code_conversion_save (0, 0);
6976
6977 coding->src_object = Fcurrent_buffer ();
6978 coding->src_chars = chars;
6979 coding->src_bytes = bytes;
6980 coding->src_pos = -chars;
6981 coding->src_pos_byte = -bytes;
6982 coding->src_multibyte = chars < bytes;
6983 coding->dst_object = coding->src_object;
6984 coding->dst_pos = PT;
6985 coding->dst_pos_byte = PT_BYTE;
6986
6987 encode_coding (coding);
6988
6989 unbind_to (count, Qnil);
6990 return coding->result;
6991 }
6992
6993
6994 /* Decode the text in the range FROM/FROM_BYTE and TO/TO_BYTE in
6995 SRC_OBJECT into DST_OBJECT by coding context CODING.
6996
6997 SRC_OBJECT is a buffer, a string, or Qnil.
6998
6999 If it is a buffer, the text is at point of the buffer. FROM and TO
7000 are positions in the buffer.
7001
7002 If it is a string, the text is at the beginning of the string.
7003 FROM and TO are indices to the string.
7004
7005 If it is nil, the text is at coding->source. FROM and TO are
7006 indices to coding->source.
7007
7008 DST_OBJECT is a buffer, Qt, or Qnil.
7009
7010 If it is a buffer, the decoded text is inserted at point of the
7011 buffer. If the buffer is the same as SRC_OBJECT, the source text
7012 is deleted.
7013
7014 If it is Qt, a string is made from the decoded text, and
7015 set in CODING->dst_object.
7016
7017 If it is Qnil, the decoded text is stored at CODING->destination.
7018 The caller must allocate CODING->dst_bytes bytes at
7019 CODING->destination by xmalloc. If the decoded text is longer than
7020 CODING->dst_bytes, CODING->destination is relocated by xrealloc.
7021 */
7022
7023 void
7024 decode_coding_object (coding, src_object, from, from_byte, to, to_byte,
7025 dst_object)
7026 struct coding_system *coding;
7027 Lisp_Object src_object;
7028 EMACS_INT from, from_byte, to, to_byte;
7029 Lisp_Object dst_object;
7030 {
7031 int count = specpdl_ptr - specpdl;
7032 unsigned char *destination;
7033 EMACS_INT dst_bytes;
7034 EMACS_INT chars = to - from;
7035 EMACS_INT bytes = to_byte - from_byte;
7036 Lisp_Object attrs;
7037 int saved_pt = -1, saved_pt_byte;
7038 int need_marker_adjustment = 0;
7039 Lisp_Object old_deactivate_mark;
7040
7041 old_deactivate_mark = Vdeactivate_mark;
7042
7043 if (NILP (dst_object))
7044 {
7045 destination = coding->destination;
7046 dst_bytes = coding->dst_bytes;
7047 }
7048
7049 coding->src_object = src_object;
7050 coding->src_chars = chars;
7051 coding->src_bytes = bytes;
7052 coding->src_multibyte = chars < bytes;
7053
7054 if (STRINGP (src_object))
7055 {
7056 coding->src_pos = from;
7057 coding->src_pos_byte = from_byte;
7058 }
7059 else if (BUFFERP (src_object))
7060 {
7061 set_buffer_internal (XBUFFER (src_object));
7062 if (from != GPT)
7063 move_gap_both (from, from_byte);
7064 if (EQ (src_object, dst_object))
7065 {
7066 struct Lisp_Marker *tail;
7067
7068 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
7069 {
7070 tail->need_adjustment
7071 = tail->charpos == (tail->insertion_type ? from : to);
7072 need_marker_adjustment |= tail->need_adjustment;
7073 }
7074 saved_pt = PT, saved_pt_byte = PT_BYTE;
7075 TEMP_SET_PT_BOTH (from, from_byte);
7076 current_buffer->text->inhibit_shrinking = 1;
7077 del_range_both (from, from_byte, to, to_byte, 1);
7078 coding->src_pos = -chars;
7079 coding->src_pos_byte = -bytes;
7080 }
7081 else
7082 {
7083 coding->src_pos = from;
7084 coding->src_pos_byte = from_byte;
7085 }
7086 }
7087
7088 if (CODING_REQUIRE_DETECTION (coding))
7089 detect_coding (coding);
7090 attrs = CODING_ID_ATTRS (coding->id);
7091
7092 if (EQ (dst_object, Qt)
7093 || (! NILP (CODING_ATTR_POST_READ (attrs))
7094 && NILP (dst_object)))
7095 {
7096 coding->dst_multibyte = !CODING_FOR_UNIBYTE (coding);
7097 coding->dst_object = code_conversion_save (1, coding->dst_multibyte);
7098 coding->dst_pos = BEG;
7099 coding->dst_pos_byte = BEG_BYTE;
7100 }
7101 else if (BUFFERP (dst_object))
7102 {
7103 code_conversion_save (0, 0);
7104 coding->dst_object = dst_object;
7105 coding->dst_pos = BUF_PT (XBUFFER (dst_object));
7106 coding->dst_pos_byte = BUF_PT_BYTE (XBUFFER (dst_object));
7107 coding->dst_multibyte
7108 = ! NILP (XBUFFER (dst_object)->enable_multibyte_characters);
7109 }
7110 else
7111 {
7112 code_conversion_save (0, 0);
7113 coding->dst_object = Qnil;
7114 /* Most callers presume this will return a multibyte result, and they
7115 won't use `binary' or `raw-text' anyway, so let's not worry about
7116 CODING_FOR_UNIBYTE. */
7117 coding->dst_multibyte = 1;
7118 }
7119
7120 decode_coding (coding);
7121
7122 if (BUFFERP (coding->dst_object))
7123 set_buffer_internal (XBUFFER (coding->dst_object));
7124
7125 if (! NILP (CODING_ATTR_POST_READ (attrs)))
7126 {
7127 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
7128 EMACS_INT prev_Z = Z, prev_Z_BYTE = Z_BYTE;
7129 Lisp_Object val;
7130
7131 TEMP_SET_PT_BOTH (coding->dst_pos, coding->dst_pos_byte);
7132 GCPRO5 (coding->src_object, coding->dst_object, src_object, dst_object,
7133 old_deactivate_mark);
7134 val = safe_call1 (CODING_ATTR_POST_READ (attrs),
7135 make_number (coding->produced_char));
7136 UNGCPRO;
7137 CHECK_NATNUM (val);
7138 coding->produced_char += Z - prev_Z;
7139 coding->produced += Z_BYTE - prev_Z_BYTE;
7140 }
7141
7142 if (EQ (dst_object, Qt))
7143 {
7144 coding->dst_object = Fbuffer_string ();
7145 }
7146 else if (NILP (dst_object) && BUFFERP (coding->dst_object))
7147 {
7148 set_buffer_internal (XBUFFER (coding->dst_object));
7149 if (dst_bytes < coding->produced)
7150 {
7151 destination = xrealloc (destination, coding->produced);
7152 if (! destination)
7153 {
7154 record_conversion_result (coding,
7155 CODING_RESULT_INSUFFICIENT_DST);
7156 unbind_to (count, Qnil);
7157 return;
7158 }
7159 if (BEGV < GPT && GPT < BEGV + coding->produced_char)
7160 move_gap_both (BEGV, BEGV_BYTE);
7161 bcopy (BEGV_ADDR, destination, coding->produced);
7162 coding->destination = destination;
7163 }
7164 }
7165
7166 if (saved_pt >= 0)
7167 {
7168 /* This is the case of:
7169 (BUFFERP (src_object) && EQ (src_object, dst_object))
7170 As we have moved PT while replacing the original buffer
7171 contents, we must recover it now. */
7172 set_buffer_internal (XBUFFER (src_object));
7173 current_buffer->text->inhibit_shrinking = 0;
7174 if (saved_pt < from)
7175 TEMP_SET_PT_BOTH (saved_pt, saved_pt_byte);
7176 else if (saved_pt < from + chars)
7177 TEMP_SET_PT_BOTH (from, from_byte);
7178 else if (! NILP (current_buffer->enable_multibyte_characters))
7179 TEMP_SET_PT_BOTH (saved_pt + (coding->produced_char - chars),
7180 saved_pt_byte + (coding->produced - bytes));
7181 else
7182 TEMP_SET_PT_BOTH (saved_pt + (coding->produced - bytes),
7183 saved_pt_byte + (coding->produced - bytes));
7184
7185 if (need_marker_adjustment)
7186 {
7187 struct Lisp_Marker *tail;
7188
7189 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
7190 if (tail->need_adjustment)
7191 {
7192 tail->need_adjustment = 0;
7193 if (tail->insertion_type)
7194 {
7195 tail->bytepos = from_byte;
7196 tail->charpos = from;
7197 }
7198 else
7199 {
7200 tail->bytepos = from_byte + coding->produced;
7201 tail->charpos
7202 = (NILP (current_buffer->enable_multibyte_characters)
7203 ? tail->bytepos : from + coding->produced_char);
7204 }
7205 }
7206 }
7207 }
7208
7209 Vdeactivate_mark = old_deactivate_mark;
7210 unbind_to (count, coding->dst_object);
7211 }
7212
7213
7214 void
7215 encode_coding_object (coding, src_object, from, from_byte, to, to_byte,
7216 dst_object)
7217 struct coding_system *coding;
7218 Lisp_Object src_object;
7219 EMACS_INT from, from_byte, to, to_byte;
7220 Lisp_Object dst_object;
7221 {
7222 int count = specpdl_ptr - specpdl;
7223 EMACS_INT chars = to - from;
7224 EMACS_INT bytes = to_byte - from_byte;
7225 Lisp_Object attrs;
7226 int saved_pt = -1, saved_pt_byte;
7227 int need_marker_adjustment = 0;
7228 int kill_src_buffer = 0;
7229 Lisp_Object old_deactivate_mark;
7230
7231 old_deactivate_mark = Vdeactivate_mark;
7232
7233 coding->src_object = src_object;
7234 coding->src_chars = chars;
7235 coding->src_bytes = bytes;
7236 coding->src_multibyte = chars < bytes;
7237
7238 attrs = CODING_ID_ATTRS (coding->id);
7239
7240 if (EQ (src_object, dst_object))
7241 {
7242 struct Lisp_Marker *tail;
7243
7244 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
7245 {
7246 tail->need_adjustment
7247 = tail->charpos == (tail->insertion_type ? from : to);
7248 need_marker_adjustment |= tail->need_adjustment;
7249 }
7250 }
7251
7252 if (! NILP (CODING_ATTR_PRE_WRITE (attrs)))
7253 {
7254 coding->src_object = code_conversion_save (1, coding->src_multibyte);
7255 set_buffer_internal (XBUFFER (coding->src_object));
7256 if (STRINGP (src_object))
7257 insert_from_string (src_object, from, from_byte, chars, bytes, 0);
7258 else if (BUFFERP (src_object))
7259 insert_from_buffer (XBUFFER (src_object), from, chars, 0);
7260 else
7261 insert_1_both (coding->source + from, chars, bytes, 0, 0, 0);
7262
7263 if (EQ (src_object, dst_object))
7264 {
7265 set_buffer_internal (XBUFFER (src_object));
7266 saved_pt = PT, saved_pt_byte = PT_BYTE;
7267 del_range_both (from, from_byte, to, to_byte, 1);
7268 set_buffer_internal (XBUFFER (coding->src_object));
7269 }
7270
7271 {
7272 Lisp_Object args[3];
7273 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
7274
7275 GCPRO5 (coding->src_object, coding->dst_object, src_object, dst_object,
7276 old_deactivate_mark);
7277 args[0] = CODING_ATTR_PRE_WRITE (attrs);
7278 args[1] = make_number (BEG);
7279 args[2] = make_number (Z);
7280 safe_call (3, args);
7281 UNGCPRO;
7282 }
7283 if (XBUFFER (coding->src_object) != current_buffer)
7284 kill_src_buffer = 1;
7285 coding->src_object = Fcurrent_buffer ();
7286 if (BEG != GPT)
7287 move_gap_both (BEG, BEG_BYTE);
7288 coding->src_chars = Z - BEG;
7289 coding->src_bytes = Z_BYTE - BEG_BYTE;
7290 coding->src_pos = BEG;
7291 coding->src_pos_byte = BEG_BYTE;
7292 coding->src_multibyte = Z < Z_BYTE;
7293 }
7294 else if (STRINGP (src_object))
7295 {
7296 code_conversion_save (0, 0);
7297 coding->src_pos = from;
7298 coding->src_pos_byte = from_byte;
7299 }
7300 else if (BUFFERP (src_object))
7301 {
7302 code_conversion_save (0, 0);
7303 set_buffer_internal (XBUFFER (src_object));
7304 if (EQ (src_object, dst_object))
7305 {
7306 saved_pt = PT, saved_pt_byte = PT_BYTE;
7307 coding->src_object = del_range_1 (from, to, 1, 1);
7308 coding->src_pos = 0;
7309 coding->src_pos_byte = 0;
7310 }
7311 else
7312 {
7313 if (from < GPT && to >= GPT)
7314 move_gap_both (from, from_byte);
7315 coding->src_pos = from;
7316 coding->src_pos_byte = from_byte;
7317 }
7318 }
7319 else
7320 code_conversion_save (0, 0);
7321
7322 if (BUFFERP (dst_object))
7323 {
7324 coding->dst_object = dst_object;
7325 if (EQ (src_object, dst_object))
7326 {
7327 coding->dst_pos = from;
7328 coding->dst_pos_byte = from_byte;
7329 }
7330 else
7331 {
7332 coding->dst_pos = BUF_PT (XBUFFER (dst_object));
7333 coding->dst_pos_byte = BUF_PT_BYTE (XBUFFER (dst_object));
7334 }
7335 coding->dst_multibyte
7336 = ! NILP (XBUFFER (dst_object)->enable_multibyte_characters);
7337 }
7338 else if (EQ (dst_object, Qt))
7339 {
7340 coding->dst_object = Qnil;
7341 coding->dst_bytes = coding->src_chars;
7342 if (coding->dst_bytes == 0)
7343 coding->dst_bytes = 1;
7344 coding->destination = (unsigned char *) xmalloc (coding->dst_bytes);
7345 coding->dst_multibyte = 0;
7346 }
7347 else
7348 {
7349 coding->dst_object = Qnil;
7350 coding->dst_multibyte = 0;
7351 }
7352
7353 encode_coding (coding);
7354
7355 if (EQ (dst_object, Qt))
7356 {
7357 if (BUFFERP (coding->dst_object))
7358 coding->dst_object = Fbuffer_string ();
7359 else
7360 {
7361 coding->dst_object
7362 = make_unibyte_string ((char *) coding->destination,
7363 coding->produced);
7364 xfree (coding->destination);
7365 }
7366 }
7367
7368 if (saved_pt >= 0)
7369 {
7370 /* This is the case of:
7371 (BUFFERP (src_object) && EQ (src_object, dst_object))
7372 As we have moved PT while replacing the original buffer
7373 contents, we must recover it now. */
7374 set_buffer_internal (XBUFFER (src_object));
7375 if (saved_pt < from)
7376 TEMP_SET_PT_BOTH (saved_pt, saved_pt_byte);
7377 else if (saved_pt < from + chars)
7378 TEMP_SET_PT_BOTH (from, from_byte);
7379 else if (! NILP (current_buffer->enable_multibyte_characters))
7380 TEMP_SET_PT_BOTH (saved_pt + (coding->produced_char - chars),
7381 saved_pt_byte + (coding->produced - bytes));
7382 else
7383 TEMP_SET_PT_BOTH (saved_pt + (coding->produced - bytes),
7384 saved_pt_byte + (coding->produced - bytes));
7385
7386 if (need_marker_adjustment)
7387 {
7388 struct Lisp_Marker *tail;
7389
7390 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
7391 if (tail->need_adjustment)
7392 {
7393 tail->need_adjustment = 0;
7394 if (tail->insertion_type)
7395 {
7396 tail->bytepos = from_byte;
7397 tail->charpos = from;
7398 }
7399 else
7400 {
7401 tail->bytepos = from_byte + coding->produced;
7402 tail->charpos
7403 = (NILP (current_buffer->enable_multibyte_characters)
7404 ? tail->bytepos : from + coding->produced_char);
7405 }
7406 }
7407 }
7408 }
7409
7410 if (kill_src_buffer)
7411 Fkill_buffer (coding->src_object);
7412
7413 Vdeactivate_mark = old_deactivate_mark;
7414 unbind_to (count, Qnil);
7415 }
7416
7417
7418 Lisp_Object
7419 preferred_coding_system ()
7420 {
7421 int id = coding_categories[coding_priorities[0]].id;
7422
7423 return CODING_ID_NAME (id);
7424 }
7425
7426 \f
7427 #ifdef emacs
7428 /*** 8. Emacs Lisp library functions ***/
7429
7430 DEFUN ("coding-system-p", Fcoding_system_p, Scoding_system_p, 1, 1, 0,
7431 doc: /* Return t if OBJECT is nil or a coding-system.
7432 See the documentation of `define-coding-system' for information
7433 about coding-system objects. */)
7434 (object)
7435 Lisp_Object object;
7436 {
7437 if (NILP (object)
7438 || CODING_SYSTEM_ID (object) >= 0)
7439 return Qt;
7440 if (! SYMBOLP (object)
7441 || NILP (Fget (object, Qcoding_system_define_form)))
7442 return Qnil;
7443 return Qt;
7444 }
7445
7446 DEFUN ("read-non-nil-coding-system", Fread_non_nil_coding_system,
7447 Sread_non_nil_coding_system, 1, 1, 0,
7448 doc: /* Read a coding system from the minibuffer, prompting with string PROMPT. */)
7449 (prompt)
7450 Lisp_Object prompt;
7451 {
7452 Lisp_Object val;
7453 do
7454 {
7455 val = Fcompleting_read (prompt, Vcoding_system_alist, Qnil,
7456 Qt, Qnil, Qcoding_system_history, Qnil, Qnil);
7457 }
7458 while (SCHARS (val) == 0);
7459 return (Fintern (val, Qnil));
7460 }
7461
7462 DEFUN ("read-coding-system", Fread_coding_system, Sread_coding_system, 1, 2, 0,
7463 doc: /* Read a coding system from the minibuffer, prompting with string PROMPT.
7464 If the user enters null input, return second argument DEFAULT-CODING-SYSTEM.
7465 Ignores case when completing coding systems (all Emacs coding systems
7466 are lower-case). */)
7467 (prompt, default_coding_system)
7468 Lisp_Object prompt, default_coding_system;
7469 {
7470 Lisp_Object val;
7471 int count = SPECPDL_INDEX ();
7472
7473 if (SYMBOLP (default_coding_system))
7474 default_coding_system = SYMBOL_NAME (default_coding_system);
7475 specbind (Qcompletion_ignore_case, Qt);
7476 val = Fcompleting_read (prompt, Vcoding_system_alist, Qnil,
7477 Qt, Qnil, Qcoding_system_history,
7478 default_coding_system, Qnil);
7479 unbind_to (count, Qnil);
7480 return (SCHARS (val) == 0 ? Qnil : Fintern (val, Qnil));
7481 }
7482
7483 DEFUN ("check-coding-system", Fcheck_coding_system, Scheck_coding_system,
7484 1, 1, 0,
7485 doc: /* Check validity of CODING-SYSTEM.
7486 If valid, return CODING-SYSTEM, else signal a `coding-system-error' error.
7487 It is valid if it is nil or a symbol defined as a coding system by the
7488 function `define-coding-system'. */)
7489 (coding_system)
7490 Lisp_Object coding_system;
7491 {
7492 Lisp_Object define_form;
7493
7494 define_form = Fget (coding_system, Qcoding_system_define_form);
7495 if (! NILP (define_form))
7496 {
7497 Fput (coding_system, Qcoding_system_define_form, Qnil);
7498 safe_eval (define_form);
7499 }
7500 if (!NILP (Fcoding_system_p (coding_system)))
7501 return coding_system;
7502 xsignal1 (Qcoding_system_error, coding_system);
7503 }
7504
7505 \f
7506 /* Detect how the bytes at SRC of length SRC_BYTES are encoded. If
7507 HIGHEST is nonzero, return the coding system of the highest
7508 priority among the detected coding systems. Otherwize return a
7509 list of detected coding systems sorted by their priorities. If
7510 MULTIBYTEP is nonzero, it is assumed that the bytes are in correct
7511 multibyte form but contains only ASCII and eight-bit chars.
7512 Otherwise, the bytes are raw bytes.
7513
7514 CODING-SYSTEM controls the detection as below:
7515
7516 If it is nil, detect both text-format and eol-format. If the
7517 text-format part of CODING-SYSTEM is already specified
7518 (e.g. `iso-latin-1'), detect only eol-format. If the eol-format
7519 part of CODING-SYSTEM is already specified (e.g. `undecided-unix'),
7520 detect only text-format. */
7521
7522 Lisp_Object
7523 detect_coding_system (src, src_chars, src_bytes, highest, multibytep,
7524 coding_system)
7525 const unsigned char *src;
7526 EMACS_INT src_chars, src_bytes;
7527 int highest;
7528 int multibytep;
7529 Lisp_Object coding_system;
7530 {
7531 const unsigned char *src_end = src + src_bytes;
7532 Lisp_Object attrs, eol_type;
7533 Lisp_Object val;
7534 struct coding_system coding;
7535 int id;
7536 struct coding_detection_info detect_info;
7537 enum coding_category base_category;
7538 int null_byte_found = 0, eight_bit_found = 0;
7539
7540 if (NILP (coding_system))
7541 coding_system = Qundecided;
7542 setup_coding_system (coding_system, &coding);
7543 attrs = CODING_ID_ATTRS (coding.id);
7544 eol_type = CODING_ID_EOL_TYPE (coding.id);
7545 coding_system = CODING_ATTR_BASE_NAME (attrs);
7546
7547 coding.source = src;
7548 coding.src_chars = src_chars;
7549 coding.src_bytes = src_bytes;
7550 coding.src_multibyte = multibytep;
7551 coding.consumed = 0;
7552 coding.mode |= CODING_MODE_LAST_BLOCK;
7553
7554 detect_info.checked = detect_info.found = detect_info.rejected = 0;
7555
7556 /* At first, detect text-format if necessary. */
7557 base_category = XINT (CODING_ATTR_CATEGORY (attrs));
7558 if (base_category == coding_category_undecided)
7559 {
7560 enum coding_category category;
7561 struct coding_system *this;
7562 int c, i;
7563
7564 coding.head_ascii = -1;
7565 /* Skip all ASCII bytes except for a few ISO2022 controls. */
7566 for (; src < src_end; src++)
7567 {
7568 c = *src;
7569 if (c & 0x80)
7570 {
7571 eight_bit_found = 1;
7572 if (coding.head_ascii < 0)
7573 coding.head_ascii = src - coding.source;
7574 if (null_byte_found)
7575 break;
7576 }
7577 if (c < 0x20)
7578 {
7579 if ((c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO)
7580 && ! inhibit_iso_escape_detection
7581 && ! detect_info.checked)
7582 {
7583 if (coding.head_ascii < 0)
7584 coding.head_ascii = src - coding.source;
7585 if (detect_coding_iso_2022 (&coding, &detect_info))
7586 {
7587 /* We have scanned the whole data. */
7588 if (! (detect_info.rejected & CATEGORY_MASK_ISO_7_ELSE))
7589 /* We didn't find an 8-bit code. We may have
7590 found a null-byte, but it's very rare that
7591 a binary file confirm to ISO-2022. */
7592 src = src_end;
7593 break;
7594 }
7595 }
7596 else if (! c)
7597 {
7598 null_byte_found = 1;
7599 if (eight_bit_found)
7600 break;
7601 }
7602 }
7603 }
7604 if (coding.head_ascii < 0)
7605 coding.head_ascii = src - coding.source;
7606
7607 if (null_byte_found || eight_bit_found
7608 || coding.head_ascii < coding.src_bytes
7609 || detect_info.found)
7610 {
7611 if (coding.head_ascii == coding.src_bytes)
7612 /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */
7613 for (i = 0; i < coding_category_raw_text; i++)
7614 {
7615 category = coding_priorities[i];
7616 this = coding_categories + category;
7617 if (detect_info.found & (1 << category))
7618 break;
7619 }
7620 else
7621 {
7622 if (null_byte_found)
7623 {
7624 detect_info.checked |= ~CATEGORY_MASK_UTF_16;
7625 detect_info.rejected |= ~CATEGORY_MASK_UTF_16;
7626 }
7627 for (i = 0; i < coding_category_raw_text; i++)
7628 {
7629 category = coding_priorities[i];
7630 this = coding_categories + category;
7631
7632 if (this->id < 0)
7633 {
7634 /* No coding system of this category is defined. */
7635 detect_info.rejected |= (1 << category);
7636 }
7637 else if (category >= coding_category_raw_text)
7638 continue;
7639 else if (detect_info.checked & (1 << category))
7640 {
7641 if (highest
7642 && (detect_info.found & (1 << category)))
7643 break;
7644 }
7645 else if ((*(this->detector)) (&coding, &detect_info)
7646 && highest
7647 && (detect_info.found & (1 << category)))
7648 {
7649 if (category == coding_category_utf_16_auto)
7650 {
7651 if (detect_info.found & CATEGORY_MASK_UTF_16_LE)
7652 category = coding_category_utf_16_le;
7653 else
7654 category = coding_category_utf_16_be;
7655 }
7656 break;
7657 }
7658 }
7659 }
7660 }
7661
7662 if ((detect_info.rejected & CATEGORY_MASK_ANY) == CATEGORY_MASK_ANY)
7663 {
7664 detect_info.found = CATEGORY_MASK_RAW_TEXT;
7665 id = coding_categories[coding_category_raw_text].id;
7666 val = Fcons (make_number (id), Qnil);
7667 }
7668 else if (! detect_info.rejected && ! detect_info.found)
7669 {
7670 detect_info.found = CATEGORY_MASK_ANY;
7671 id = coding_categories[coding_category_undecided].id;
7672 val = Fcons (make_number (id), Qnil);
7673 }
7674 else if (highest)
7675 {
7676 if (detect_info.found)
7677 {
7678 detect_info.found = 1 << category;
7679 val = Fcons (make_number (this->id), Qnil);
7680 }
7681 else
7682 for (i = 0; i < coding_category_raw_text; i++)
7683 if (! (detect_info.rejected & (1 << coding_priorities[i])))
7684 {
7685 detect_info.found = 1 << coding_priorities[i];
7686 id = coding_categories[coding_priorities[i]].id;
7687 val = Fcons (make_number (id), Qnil);
7688 break;
7689 }
7690 }
7691 else
7692 {
7693 int mask = detect_info.rejected | detect_info.found;
7694 int found = 0;
7695 val = Qnil;
7696
7697 for (i = coding_category_raw_text - 1; i >= 0; i--)
7698 {
7699 category = coding_priorities[i];
7700 if (! (mask & (1 << category)))
7701 {
7702 found |= 1 << category;
7703 id = coding_categories[category].id;
7704 if (id >= 0)
7705 val = Fcons (make_number (id), val);
7706 }
7707 }
7708 for (i = coding_category_raw_text - 1; i >= 0; i--)
7709 {
7710 category = coding_priorities[i];
7711 if (detect_info.found & (1 << category))
7712 {
7713 id = coding_categories[category].id;
7714 val = Fcons (make_number (id), val);
7715 }
7716 }
7717 detect_info.found |= found;
7718 }
7719 }
7720 else if (base_category == coding_category_utf_16_auto)
7721 {
7722 if (detect_coding_utf_16 (&coding, &detect_info))
7723 {
7724 struct coding_system *this;
7725
7726 if (detect_info.found & CATEGORY_MASK_UTF_16_LE)
7727 this = coding_categories + coding_category_utf_16_le;
7728 else if (detect_info.found & CATEGORY_MASK_UTF_16_BE)
7729 this = coding_categories + coding_category_utf_16_be;
7730 else if (detect_info.rejected & CATEGORY_MASK_UTF_16_LE_NOSIG)
7731 this = coding_categories + coding_category_utf_16_be_nosig;
7732 else
7733 this = coding_categories + coding_category_utf_16_le_nosig;
7734 val = Fcons (make_number (this->id), Qnil);
7735 }
7736 }
7737 else
7738 {
7739 detect_info.found = 1 << XINT (CODING_ATTR_CATEGORY (attrs));
7740 val = Fcons (make_number (coding.id), Qnil);
7741 }
7742
7743 /* Then, detect eol-format if necessary. */
7744 {
7745 int normal_eol = -1, utf_16_be_eol = -1, utf_16_le_eol;
7746 Lisp_Object tail;
7747
7748 if (VECTORP (eol_type))
7749 {
7750 if (detect_info.found & ~CATEGORY_MASK_UTF_16)
7751 {
7752 if (null_byte_found)
7753 normal_eol = EOL_SEEN_LF;
7754 else
7755 normal_eol = detect_eol (coding.source, src_bytes,
7756 coding_category_raw_text);
7757 }
7758 if (detect_info.found & (CATEGORY_MASK_UTF_16_BE
7759 | CATEGORY_MASK_UTF_16_BE_NOSIG))
7760 utf_16_be_eol = detect_eol (coding.source, src_bytes,
7761 coding_category_utf_16_be);
7762 if (detect_info.found & (CATEGORY_MASK_UTF_16_LE
7763 | CATEGORY_MASK_UTF_16_LE_NOSIG))
7764 utf_16_le_eol = detect_eol (coding.source, src_bytes,
7765 coding_category_utf_16_le);
7766 }
7767 else
7768 {
7769 if (EQ (eol_type, Qunix))
7770 normal_eol = utf_16_be_eol = utf_16_le_eol = EOL_SEEN_LF;
7771 else if (EQ (eol_type, Qdos))
7772 normal_eol = utf_16_be_eol = utf_16_le_eol = EOL_SEEN_CRLF;
7773 else
7774 normal_eol = utf_16_be_eol = utf_16_le_eol = EOL_SEEN_CR;
7775 }
7776
7777 for (tail = val; CONSP (tail); tail = XCDR (tail))
7778 {
7779 enum coding_category category;
7780 int this_eol;
7781
7782 id = XINT (XCAR (tail));
7783 attrs = CODING_ID_ATTRS (id);
7784 category = XINT (CODING_ATTR_CATEGORY (attrs));
7785 eol_type = CODING_ID_EOL_TYPE (id);
7786 if (VECTORP (eol_type))
7787 {
7788 if (category == coding_category_utf_16_be
7789 || category == coding_category_utf_16_be_nosig)
7790 this_eol = utf_16_be_eol;
7791 else if (category == coding_category_utf_16_le
7792 || category == coding_category_utf_16_le_nosig)
7793 this_eol = utf_16_le_eol;
7794 else
7795 this_eol = normal_eol;
7796
7797 if (this_eol == EOL_SEEN_LF)
7798 XSETCAR (tail, AREF (eol_type, 0));
7799 else if (this_eol == EOL_SEEN_CRLF)
7800 XSETCAR (tail, AREF (eol_type, 1));
7801 else if (this_eol == EOL_SEEN_CR)
7802 XSETCAR (tail, AREF (eol_type, 2));
7803 else
7804 XSETCAR (tail, CODING_ID_NAME (id));
7805 }
7806 else
7807 XSETCAR (tail, CODING_ID_NAME (id));
7808 }
7809 }
7810
7811 return (highest ? XCAR (val) : val);
7812 }
7813
7814
7815 DEFUN ("detect-coding-region", Fdetect_coding_region, Sdetect_coding_region,
7816 2, 3, 0,
7817 doc: /* Detect coding system of the text in the region between START and END.
7818 Return a list of possible coding systems ordered by priority.
7819
7820 If only ASCII characters are found (except for such ISO-2022 control
7821 characters as ESC), it returns a list of single element `undecided'
7822 or its subsidiary coding system according to a detected end-of-line
7823 format.
7824
7825 If optional argument HIGHEST is non-nil, return the coding system of
7826 highest priority. */)
7827 (start, end, highest)
7828 Lisp_Object start, end, highest;
7829 {
7830 int from, to;
7831 int from_byte, to_byte;
7832
7833 CHECK_NUMBER_COERCE_MARKER (start);
7834 CHECK_NUMBER_COERCE_MARKER (end);
7835
7836 validate_region (&start, &end);
7837 from = XINT (start), to = XINT (end);
7838 from_byte = CHAR_TO_BYTE (from);
7839 to_byte = CHAR_TO_BYTE (to);
7840
7841 if (from < GPT && to >= GPT)
7842 move_gap_both (to, to_byte);
7843
7844 return detect_coding_system (BYTE_POS_ADDR (from_byte),
7845 to - from, to_byte - from_byte,
7846 !NILP (highest),
7847 !NILP (current_buffer
7848 ->enable_multibyte_characters),
7849 Qnil);
7850 }
7851
7852 DEFUN ("detect-coding-string", Fdetect_coding_string, Sdetect_coding_string,
7853 1, 2, 0,
7854 doc: /* Detect coding system of the text in STRING.
7855 Return a list of possible coding systems ordered by priority.
7856
7857 If only ASCII characters are found (except for such ISO-2022 control
7858 characters as ESC), it returns a list of single element `undecided'
7859 or its subsidiary coding system according to a detected end-of-line
7860 format.
7861
7862 If optional argument HIGHEST is non-nil, return the coding system of
7863 highest priority. */)
7864 (string, highest)
7865 Lisp_Object string, highest;
7866 {
7867 CHECK_STRING (string);
7868
7869 return detect_coding_system (SDATA (string),
7870 SCHARS (string), SBYTES (string),
7871 !NILP (highest), STRING_MULTIBYTE (string),
7872 Qnil);
7873 }
7874
7875
7876 static INLINE int
7877 char_encodable_p (c, attrs)
7878 int c;
7879 Lisp_Object attrs;
7880 {
7881 Lisp_Object tail;
7882 struct charset *charset;
7883 Lisp_Object translation_table;
7884
7885 translation_table = CODING_ATTR_TRANS_TBL (attrs);
7886 if (! NILP (translation_table))
7887 c = translate_char (translation_table, c);
7888 for (tail = CODING_ATTR_CHARSET_LIST (attrs);
7889 CONSP (tail); tail = XCDR (tail))
7890 {
7891 charset = CHARSET_FROM_ID (XINT (XCAR (tail)));
7892 if (CHAR_CHARSET_P (c, charset))
7893 break;
7894 }
7895 return (! NILP (tail));
7896 }
7897
7898
7899 /* Return a list of coding systems that safely encode the text between
7900 START and END. If EXCLUDE is non-nil, it is a list of coding
7901 systems not to check. The returned list doesn't contain any such
7902 coding systems. In any case, if the text contains only ASCII or is
7903 unibyte, return t. */
7904
7905 DEFUN ("find-coding-systems-region-internal",
7906 Ffind_coding_systems_region_internal,
7907 Sfind_coding_systems_region_internal, 2, 3, 0,
7908 doc: /* Internal use only. */)
7909 (start, end, exclude)
7910 Lisp_Object start, end, exclude;
7911 {
7912 Lisp_Object coding_attrs_list, safe_codings;
7913 EMACS_INT start_byte, end_byte;
7914 const unsigned char *p, *pbeg, *pend;
7915 int c;
7916 Lisp_Object tail, elt;
7917
7918 if (STRINGP (start))
7919 {
7920 if (!STRING_MULTIBYTE (start)
7921 || SCHARS (start) == SBYTES (start))
7922 return Qt;
7923 start_byte = 0;
7924 end_byte = SBYTES (start);
7925 }
7926 else
7927 {
7928 CHECK_NUMBER_COERCE_MARKER (start);
7929 CHECK_NUMBER_COERCE_MARKER (end);
7930 if (XINT (start) < BEG || XINT (end) > Z || XINT (start) > XINT (end))
7931 args_out_of_range (start, end);
7932 if (NILP (current_buffer->enable_multibyte_characters))
7933 return Qt;
7934 start_byte = CHAR_TO_BYTE (XINT (start));
7935 end_byte = CHAR_TO_BYTE (XINT (end));
7936 if (XINT (end) - XINT (start) == end_byte - start_byte)
7937 return Qt;
7938
7939 if (XINT (start) < GPT && XINT (end) > GPT)
7940 {
7941 if ((GPT - XINT (start)) < (XINT (end) - GPT))
7942 move_gap_both (XINT (start), start_byte);
7943 else
7944 move_gap_both (XINT (end), end_byte);
7945 }
7946 }
7947
7948 coding_attrs_list = Qnil;
7949 for (tail = Vcoding_system_list; CONSP (tail); tail = XCDR (tail))
7950 if (NILP (exclude)
7951 || NILP (Fmemq (XCAR (tail), exclude)))
7952 {
7953 Lisp_Object attrs;
7954
7955 attrs = AREF (CODING_SYSTEM_SPEC (XCAR (tail)), 0);
7956 if (EQ (XCAR (tail), CODING_ATTR_BASE_NAME (attrs))
7957 && ! EQ (CODING_ATTR_TYPE (attrs), Qundecided))
7958 {
7959 ASET (attrs, coding_attr_trans_tbl,
7960 get_translation_table (attrs, 1, NULL));
7961 coding_attrs_list = Fcons (attrs, coding_attrs_list);
7962 }
7963 }
7964
7965 if (STRINGP (start))
7966 p = pbeg = SDATA (start);
7967 else
7968 p = pbeg = BYTE_POS_ADDR (start_byte);
7969 pend = p + (end_byte - start_byte);
7970
7971 while (p < pend && ASCII_BYTE_P (*p)) p++;
7972 while (p < pend && ASCII_BYTE_P (*(pend - 1))) pend--;
7973
7974 while (p < pend)
7975 {
7976 if (ASCII_BYTE_P (*p))
7977 p++;
7978 else
7979 {
7980 c = STRING_CHAR_ADVANCE (p);
7981
7982 charset_map_loaded = 0;
7983 for (tail = coding_attrs_list; CONSP (tail);)
7984 {
7985 elt = XCAR (tail);
7986 if (NILP (elt))
7987 tail = XCDR (tail);
7988 else if (char_encodable_p (c, elt))
7989 tail = XCDR (tail);
7990 else if (CONSP (XCDR (tail)))
7991 {
7992 XSETCAR (tail, XCAR (XCDR (tail)));
7993 XSETCDR (tail, XCDR (XCDR (tail)));
7994 }
7995 else
7996 {
7997 XSETCAR (tail, Qnil);
7998 tail = XCDR (tail);
7999 }
8000 }
8001 if (charset_map_loaded)
8002 {
8003 EMACS_INT p_offset = p - pbeg, pend_offset = pend - pbeg;
8004
8005 if (STRINGP (start))
8006 pbeg = SDATA (start);
8007 else
8008 pbeg = BYTE_POS_ADDR (start_byte);
8009 p = pbeg + p_offset;
8010 pend = pbeg + pend_offset;
8011 }
8012 }
8013 }
8014
8015 safe_codings = list2 (Qraw_text, Qno_conversion);
8016 for (tail = coding_attrs_list; CONSP (tail); tail = XCDR (tail))
8017 if (! NILP (XCAR (tail)))
8018 safe_codings = Fcons (CODING_ATTR_BASE_NAME (XCAR (tail)), safe_codings);
8019
8020 return safe_codings;
8021 }
8022
8023
8024 DEFUN ("unencodable-char-position", Funencodable_char_position,
8025 Sunencodable_char_position, 3, 5, 0,
8026 doc: /*
8027 Return position of first un-encodable character in a region.
8028 START and END specify the region and CODING-SYSTEM specifies the
8029 encoding to check. Return nil if CODING-SYSTEM does encode the region.
8030
8031 If optional 4th argument COUNT is non-nil, it specifies at most how
8032 many un-encodable characters to search. In this case, the value is a
8033 list of positions.
8034
8035 If optional 5th argument STRING is non-nil, it is a string to search
8036 for un-encodable characters. In that case, START and END are indexes
8037 to the string. */)
8038 (start, end, coding_system, count, string)
8039 Lisp_Object start, end, coding_system, count, string;
8040 {
8041 int n;
8042 struct coding_system coding;
8043 Lisp_Object attrs, charset_list, translation_table;
8044 Lisp_Object positions;
8045 int from, to;
8046 const unsigned char *p, *stop, *pend;
8047 int ascii_compatible;
8048
8049 setup_coding_system (Fcheck_coding_system (coding_system), &coding);
8050 attrs = CODING_ID_ATTRS (coding.id);
8051 if (EQ (CODING_ATTR_TYPE (attrs), Qraw_text))
8052 return Qnil;
8053 ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs));
8054 charset_list = CODING_ATTR_CHARSET_LIST (attrs);
8055 translation_table = get_translation_table (attrs, 1, NULL);
8056
8057 if (NILP (string))
8058 {
8059 validate_region (&start, &end);
8060 from = XINT (start);
8061 to = XINT (end);
8062 if (NILP (current_buffer->enable_multibyte_characters)
8063 || (ascii_compatible
8064 && (to - from) == (CHAR_TO_BYTE (to) - (CHAR_TO_BYTE (from)))))
8065 return Qnil;
8066 p = CHAR_POS_ADDR (from);
8067 pend = CHAR_POS_ADDR (to);
8068 if (from < GPT && to >= GPT)
8069 stop = GPT_ADDR;
8070 else
8071 stop = pend;
8072 }
8073 else
8074 {
8075 CHECK_STRING (string);
8076 CHECK_NATNUM (start);
8077 CHECK_NATNUM (end);
8078 from = XINT (start);
8079 to = XINT (end);
8080 if (from > to
8081 || to > SCHARS (string))
8082 args_out_of_range_3 (string, start, end);
8083 if (! STRING_MULTIBYTE (string))
8084 return Qnil;
8085 p = SDATA (string) + string_char_to_byte (string, from);
8086 stop = pend = SDATA (string) + string_char_to_byte (string, to);
8087 if (ascii_compatible && (to - from) == (pend - p))
8088 return Qnil;
8089 }
8090
8091 if (NILP (count))
8092 n = 1;
8093 else
8094 {
8095 CHECK_NATNUM (count);
8096 n = XINT (count);
8097 }
8098
8099 positions = Qnil;
8100 while (1)
8101 {
8102 int c;
8103
8104 if (ascii_compatible)
8105 while (p < stop && ASCII_BYTE_P (*p))
8106 p++, from++;
8107 if (p >= stop)
8108 {
8109 if (p >= pend)
8110 break;
8111 stop = pend;
8112 p = GAP_END_ADDR;
8113 }
8114
8115 c = STRING_CHAR_ADVANCE (p);
8116 if (! (ASCII_CHAR_P (c) && ascii_compatible)
8117 && ! char_charset (translate_char (translation_table, c),
8118 charset_list, NULL))
8119 {
8120 positions = Fcons (make_number (from), positions);
8121 n--;
8122 if (n == 0)
8123 break;
8124 }
8125
8126 from++;
8127 }
8128
8129 return (NILP (count) ? Fcar (positions) : Fnreverse (positions));
8130 }
8131
8132
8133 DEFUN ("check-coding-systems-region", Fcheck_coding_systems_region,
8134 Scheck_coding_systems_region, 3, 3, 0,
8135 doc: /* Check if the region is encodable by coding systems.
8136
8137 START and END are buffer positions specifying the region.
8138 CODING-SYSTEM-LIST is a list of coding systems to check.
8139
8140 The value is an alist ((CODING-SYSTEM POS0 POS1 ...) ...), where
8141 CODING-SYSTEM is a member of CODING-SYSTEM-LIST and can't encode the
8142 whole region, POS0, POS1, ... are buffer positions where non-encodable
8143 characters are found.
8144
8145 If all coding systems in CODING-SYSTEM-LIST can encode the region, the
8146 value is nil.
8147
8148 START may be a string. In that case, check if the string is
8149 encodable, and the value contains indices to the string instead of
8150 buffer positions. END is ignored. */)
8151 (start, end, coding_system_list)
8152 Lisp_Object start, end, coding_system_list;
8153 {
8154 Lisp_Object list;
8155 EMACS_INT start_byte, end_byte;
8156 int pos;
8157 const unsigned char *p, *pbeg, *pend;
8158 int c;
8159 Lisp_Object tail, elt, attrs;
8160
8161 if (STRINGP (start))
8162 {
8163 if (!STRING_MULTIBYTE (start)
8164 && SCHARS (start) != SBYTES (start))
8165 return Qnil;
8166 start_byte = 0;
8167 end_byte = SBYTES (start);
8168 pos = 0;
8169 }
8170 else
8171 {
8172 CHECK_NUMBER_COERCE_MARKER (start);
8173 CHECK_NUMBER_COERCE_MARKER (end);
8174 if (XINT (start) < BEG || XINT (end) > Z || XINT (start) > XINT (end))
8175 args_out_of_range (start, end);
8176 if (NILP (current_buffer->enable_multibyte_characters))
8177 return Qnil;
8178 start_byte = CHAR_TO_BYTE (XINT (start));
8179 end_byte = CHAR_TO_BYTE (XINT (end));
8180 if (XINT (end) - XINT (start) == end_byte - start_byte)
8181 return Qt;
8182
8183 if (XINT (start) < GPT && XINT (end) > GPT)
8184 {
8185 if ((GPT - XINT (start)) < (XINT (end) - GPT))
8186 move_gap_both (XINT (start), start_byte);
8187 else
8188 move_gap_both (XINT (end), end_byte);
8189 }
8190 pos = XINT (start);
8191 }
8192
8193 list = Qnil;
8194 for (tail = coding_system_list; CONSP (tail); tail = XCDR (tail))
8195 {
8196 elt = XCAR (tail);
8197 attrs = AREF (CODING_SYSTEM_SPEC (elt), 0);
8198 ASET (attrs, coding_attr_trans_tbl,
8199 get_translation_table (attrs, 1, NULL));
8200 list = Fcons (Fcons (elt, Fcons (attrs, Qnil)), list);
8201 }
8202
8203 if (STRINGP (start))
8204 p = pbeg = SDATA (start);
8205 else
8206 p = pbeg = BYTE_POS_ADDR (start_byte);
8207 pend = p + (end_byte - start_byte);
8208
8209 while (p < pend && ASCII_BYTE_P (*p)) p++, pos++;
8210 while (p < pend && ASCII_BYTE_P (*(pend - 1))) pend--;
8211
8212 while (p < pend)
8213 {
8214 if (ASCII_BYTE_P (*p))
8215 p++;
8216 else
8217 {
8218 c = STRING_CHAR_ADVANCE (p);
8219
8220 charset_map_loaded = 0;
8221 for (tail = list; CONSP (tail); tail = XCDR (tail))
8222 {
8223 elt = XCDR (XCAR (tail));
8224 if (! char_encodable_p (c, XCAR (elt)))
8225 XSETCDR (elt, Fcons (make_number (pos), XCDR (elt)));
8226 }
8227 if (charset_map_loaded)
8228 {
8229 EMACS_INT p_offset = p - pbeg, pend_offset = pend - pbeg;
8230
8231 if (STRINGP (start))
8232 pbeg = SDATA (start);
8233 else
8234 pbeg = BYTE_POS_ADDR (start_byte);
8235 p = pbeg + p_offset;
8236 pend = pbeg + pend_offset;
8237 }
8238 }
8239 pos++;
8240 }
8241
8242 tail = list;
8243 list = Qnil;
8244 for (; CONSP (tail); tail = XCDR (tail))
8245 {
8246 elt = XCAR (tail);
8247 if (CONSP (XCDR (XCDR (elt))))
8248 list = Fcons (Fcons (XCAR (elt), Fnreverse (XCDR (XCDR (elt)))),
8249 list);
8250 }
8251
8252 return list;
8253 }
8254
8255
8256 Lisp_Object
8257 code_convert_region (start, end, coding_system, dst_object, encodep, norecord)
8258 Lisp_Object start, end, coding_system, dst_object;
8259 int encodep, norecord;
8260 {
8261 struct coding_system coding;
8262 EMACS_INT from, from_byte, to, to_byte;
8263 Lisp_Object src_object;
8264
8265 CHECK_NUMBER_COERCE_MARKER (start);
8266 CHECK_NUMBER_COERCE_MARKER (end);
8267 if (NILP (coding_system))
8268 coding_system = Qno_conversion;
8269 else
8270 CHECK_CODING_SYSTEM (coding_system);
8271 src_object = Fcurrent_buffer ();
8272 if (NILP (dst_object))
8273 dst_object = src_object;
8274 else if (! EQ (dst_object, Qt))
8275 CHECK_BUFFER (dst_object);
8276
8277 validate_region (&start, &end);
8278 from = XFASTINT (start);
8279 from_byte = CHAR_TO_BYTE (from);
8280 to = XFASTINT (end);
8281 to_byte = CHAR_TO_BYTE (to);
8282
8283 setup_coding_system (coding_system, &coding);
8284 coding.mode |= CODING_MODE_LAST_BLOCK;
8285
8286 if (encodep)
8287 encode_coding_object (&coding, src_object, from, from_byte, to, to_byte,
8288 dst_object);
8289 else
8290 decode_coding_object (&coding, src_object, from, from_byte, to, to_byte,
8291 dst_object);
8292 if (! norecord)
8293 Vlast_coding_system_used = CODING_ID_NAME (coding.id);
8294
8295 return (BUFFERP (dst_object)
8296 ? make_number (coding.produced_char)
8297 : coding.dst_object);
8298 }
8299
8300
8301 DEFUN ("decode-coding-region", Fdecode_coding_region, Sdecode_coding_region,
8302 3, 4, "r\nzCoding system: ",
8303 doc: /* Decode the current region from the specified coding system.
8304 When called from a program, takes four arguments:
8305 START, END, CODING-SYSTEM, and DESTINATION.
8306 START and END are buffer positions.
8307
8308 Optional 4th arguments DESTINATION specifies where the decoded text goes.
8309 If nil, the region between START and END is replaced by the decoded text.
8310 If buffer, the decoded text is inserted in the buffer.
8311 If t, the decoded text is returned.
8312
8313 This function sets `last-coding-system-used' to the precise coding system
8314 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8315 not fully specified.)
8316 It returns the length of the decoded text. */)
8317 (start, end, coding_system, destination)
8318 Lisp_Object start, end, coding_system, destination;
8319 {
8320 return code_convert_region (start, end, coding_system, destination, 0, 0);
8321 }
8322
8323 DEFUN ("encode-coding-region", Fencode_coding_region, Sencode_coding_region,
8324 3, 4, "r\nzCoding system: ",
8325 doc: /* Encode the current region by specified coding system.
8326 When called from a program, takes four arguments:
8327 START, END, CODING-SYSTEM and DESTINATION.
8328 START and END are buffer positions.
8329
8330 Optional 4th arguments DESTINATION specifies where the encoded text goes.
8331 If nil, the region between START and END is replace by the encoded text.
8332 If buffer, the encoded text is inserted in the buffer.
8333 If t, the encoded text is returned.
8334
8335 This function sets `last-coding-system-used' to the precise coding system
8336 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8337 not fully specified.)
8338 It returns the length of the encoded text. */)
8339 (start, end, coding_system, destination)
8340 Lisp_Object start, end, coding_system, destination;
8341 {
8342 return code_convert_region (start, end, coding_system, destination, 1, 0);
8343 }
8344
8345 Lisp_Object
8346 code_convert_string (string, coding_system, dst_object,
8347 encodep, nocopy, norecord)
8348 Lisp_Object string, coding_system, dst_object;
8349 int encodep, nocopy, norecord;
8350 {
8351 struct coding_system coding;
8352 EMACS_INT chars, bytes;
8353
8354 CHECK_STRING (string);
8355 if (NILP (coding_system))
8356 {
8357 if (! norecord)
8358 Vlast_coding_system_used = Qno_conversion;
8359 if (NILP (dst_object))
8360 return (nocopy ? Fcopy_sequence (string) : string);
8361 }
8362
8363 if (NILP (coding_system))
8364 coding_system = Qno_conversion;
8365 else
8366 CHECK_CODING_SYSTEM (coding_system);
8367 if (NILP (dst_object))
8368 dst_object = Qt;
8369 else if (! EQ (dst_object, Qt))
8370 CHECK_BUFFER (dst_object);
8371
8372 setup_coding_system (coding_system, &coding);
8373 coding.mode |= CODING_MODE_LAST_BLOCK;
8374 chars = SCHARS (string);
8375 bytes = SBYTES (string);
8376 if (encodep)
8377 encode_coding_object (&coding, string, 0, 0, chars, bytes, dst_object);
8378 else
8379 decode_coding_object (&coding, string, 0, 0, chars, bytes, dst_object);
8380 if (! norecord)
8381 Vlast_coding_system_used = CODING_ID_NAME (coding.id);
8382
8383 return (BUFFERP (dst_object)
8384 ? make_number (coding.produced_char)
8385 : coding.dst_object);
8386 }
8387
8388
8389 /* Encode or decode STRING according to CODING_SYSTEM.
8390 Do not set Vlast_coding_system_used.
8391
8392 This function is called only from macros DECODE_FILE and
8393 ENCODE_FILE, thus we ignore character composition. */
8394
8395 Lisp_Object
8396 code_convert_string_norecord (string, coding_system, encodep)
8397 Lisp_Object string, coding_system;
8398 int encodep;
8399 {
8400 return code_convert_string (string, coding_system, Qt, encodep, 0, 1);
8401 }
8402
8403
8404 DEFUN ("decode-coding-string", Fdecode_coding_string, Sdecode_coding_string,
8405 2, 4, 0,
8406 doc: /* Decode STRING which is encoded in CODING-SYSTEM, and return the result.
8407
8408 Optional third arg NOCOPY non-nil means it is OK to return STRING itself
8409 if the decoding operation is trivial.
8410
8411 Optional fourth arg BUFFER non-nil means that the decoded text is
8412 inserted in BUFFER instead of returned as a string. In this case,
8413 the return value is BUFFER.
8414
8415 This function sets `last-coding-system-used' to the precise coding system
8416 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8417 not fully specified.) */)
8418 (string, coding_system, nocopy, buffer)
8419 Lisp_Object string, coding_system, nocopy, buffer;
8420 {
8421 return code_convert_string (string, coding_system, buffer,
8422 0, ! NILP (nocopy), 0);
8423 }
8424
8425 DEFUN ("encode-coding-string", Fencode_coding_string, Sencode_coding_string,
8426 2, 4, 0,
8427 doc: /* Encode STRING to CODING-SYSTEM, and return the result.
8428
8429 Optional third arg NOCOPY non-nil means it is OK to return STRING
8430 itself if the encoding operation is trivial.
8431
8432 Optional fourth arg BUFFER non-nil means that the encoded text is
8433 inserted in BUFFER instead of returned as a string. In this case,
8434 the return value is BUFFER.
8435
8436 This function sets `last-coding-system-used' to the precise coding system
8437 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8438 not fully specified.) */)
8439 (string, coding_system, nocopy, buffer)
8440 Lisp_Object string, coding_system, nocopy, buffer;
8441 {
8442 return code_convert_string (string, coding_system, buffer,
8443 1, ! NILP (nocopy), 1);
8444 }
8445
8446 \f
8447 DEFUN ("decode-sjis-char", Fdecode_sjis_char, Sdecode_sjis_char, 1, 1, 0,
8448 doc: /* Decode a Japanese character which has CODE in shift_jis encoding.
8449 Return the corresponding character. */)
8450 (code)
8451 Lisp_Object code;
8452 {
8453 Lisp_Object spec, attrs, val;
8454 struct charset *charset_roman, *charset_kanji, *charset_kana, *charset;
8455 int c;
8456
8457 CHECK_NATNUM (code);
8458 c = XFASTINT (code);
8459 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system, spec);
8460 attrs = AREF (spec, 0);
8461
8462 if (ASCII_BYTE_P (c)
8463 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)))
8464 return code;
8465
8466 val = CODING_ATTR_CHARSET_LIST (attrs);
8467 charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
8468 charset_kana = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
8469 charset_kanji = CHARSET_FROM_ID (XINT (XCAR (val)));
8470
8471 if (c <= 0x7F)
8472 charset = charset_roman;
8473 else if (c >= 0xA0 && c < 0xDF)
8474 {
8475 charset = charset_kana;
8476 c -= 0x80;
8477 }
8478 else
8479 {
8480 int s1 = c >> 8, s2 = c & 0xFF;
8481
8482 if (s1 < 0x81 || (s1 > 0x9F && s1 < 0xE0) || s1 > 0xEF
8483 || s2 < 0x40 || s2 == 0x7F || s2 > 0xFC)
8484 error ("Invalid code: %d", code);
8485 SJIS_TO_JIS (c);
8486 charset = charset_kanji;
8487 }
8488 c = DECODE_CHAR (charset, c);
8489 if (c < 0)
8490 error ("Invalid code: %d", code);
8491 return make_number (c);
8492 }
8493
8494
8495 DEFUN ("encode-sjis-char", Fencode_sjis_char, Sencode_sjis_char, 1, 1, 0,
8496 doc: /* Encode a Japanese character CH to shift_jis encoding.
8497 Return the corresponding code in SJIS. */)
8498 (ch)
8499 Lisp_Object ch;
8500 {
8501 Lisp_Object spec, attrs, charset_list;
8502 int c;
8503 struct charset *charset;
8504 unsigned code;
8505
8506 CHECK_CHARACTER (ch);
8507 c = XFASTINT (ch);
8508 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system, spec);
8509 attrs = AREF (spec, 0);
8510
8511 if (ASCII_CHAR_P (c)
8512 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)))
8513 return ch;
8514
8515 charset_list = CODING_ATTR_CHARSET_LIST (attrs);
8516 charset = char_charset (c, charset_list, &code);
8517 if (code == CHARSET_INVALID_CODE (charset))
8518 error ("Can't encode by shift_jis encoding: %d", c);
8519 JIS_TO_SJIS (code);
8520
8521 return make_number (code);
8522 }
8523
8524 DEFUN ("decode-big5-char", Fdecode_big5_char, Sdecode_big5_char, 1, 1, 0,
8525 doc: /* Decode a Big5 character which has CODE in BIG5 coding system.
8526 Return the corresponding character. */)
8527 (code)
8528 Lisp_Object code;
8529 {
8530 Lisp_Object spec, attrs, val;
8531 struct charset *charset_roman, *charset_big5, *charset;
8532 int c;
8533
8534 CHECK_NATNUM (code);
8535 c = XFASTINT (code);
8536 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system, spec);
8537 attrs = AREF (spec, 0);
8538
8539 if (ASCII_BYTE_P (c)
8540 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)))
8541 return code;
8542
8543 val = CODING_ATTR_CHARSET_LIST (attrs);
8544 charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
8545 charset_big5 = CHARSET_FROM_ID (XINT (XCAR (val)));
8546
8547 if (c <= 0x7F)
8548 charset = charset_roman;
8549 else
8550 {
8551 int b1 = c >> 8, b2 = c & 0x7F;
8552 if (b1 < 0xA1 || b1 > 0xFE
8553 || b2 < 0x40 || (b2 > 0x7E && b2 < 0xA1) || b2 > 0xFE)
8554 error ("Invalid code: %d", code);
8555 charset = charset_big5;
8556 }
8557 c = DECODE_CHAR (charset, (unsigned )c);
8558 if (c < 0)
8559 error ("Invalid code: %d", code);
8560 return make_number (c);
8561 }
8562
8563 DEFUN ("encode-big5-char", Fencode_big5_char, Sencode_big5_char, 1, 1, 0,
8564 doc: /* Encode the Big5 character CH to BIG5 coding system.
8565 Return the corresponding character code in Big5. */)
8566 (ch)
8567 Lisp_Object ch;
8568 {
8569 Lisp_Object spec, attrs, charset_list;
8570 struct charset *charset;
8571 int c;
8572 unsigned code;
8573
8574 CHECK_CHARACTER (ch);
8575 c = XFASTINT (ch);
8576 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system, spec);
8577 attrs = AREF (spec, 0);
8578 if (ASCII_CHAR_P (c)
8579 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)))
8580 return ch;
8581
8582 charset_list = CODING_ATTR_CHARSET_LIST (attrs);
8583 charset = char_charset (c, charset_list, &code);
8584 if (code == CHARSET_INVALID_CODE (charset))
8585 error ("Can't encode by Big5 encoding: %d", c);
8586
8587 return make_number (code);
8588 }
8589
8590 \f
8591 DEFUN ("set-terminal-coding-system-internal", Fset_terminal_coding_system_internal,
8592 Sset_terminal_coding_system_internal, 1, 2, 0,
8593 doc: /* Internal use only. */)
8594 (coding_system, terminal)
8595 Lisp_Object coding_system;
8596 Lisp_Object terminal;
8597 {
8598 struct coding_system *terminal_coding = TERMINAL_TERMINAL_CODING (get_terminal (terminal, 1));
8599 CHECK_SYMBOL (coding_system);
8600 setup_coding_system (Fcheck_coding_system (coding_system), terminal_coding);
8601 /* We had better not send unsafe characters to terminal. */
8602 terminal_coding->mode |= CODING_MODE_SAFE_ENCODING;
8603 /* Characer composition should be disabled. */
8604 terminal_coding->common_flags &= ~CODING_ANNOTATE_COMPOSITION_MASK;
8605 terminal_coding->src_multibyte = 1;
8606 terminal_coding->dst_multibyte = 0;
8607 return Qnil;
8608 }
8609
8610 DEFUN ("set-safe-terminal-coding-system-internal",
8611 Fset_safe_terminal_coding_system_internal,
8612 Sset_safe_terminal_coding_system_internal, 1, 1, 0,
8613 doc: /* Internal use only. */)
8614 (coding_system)
8615 Lisp_Object coding_system;
8616 {
8617 CHECK_SYMBOL (coding_system);
8618 setup_coding_system (Fcheck_coding_system (coding_system),
8619 &safe_terminal_coding);
8620 /* Characer composition should be disabled. */
8621 safe_terminal_coding.common_flags &= ~CODING_ANNOTATE_COMPOSITION_MASK;
8622 safe_terminal_coding.src_multibyte = 1;
8623 safe_terminal_coding.dst_multibyte = 0;
8624 return Qnil;
8625 }
8626
8627 DEFUN ("terminal-coding-system", Fterminal_coding_system,
8628 Sterminal_coding_system, 0, 1, 0,
8629 doc: /* Return coding system specified for terminal output on the given terminal.
8630 TERMINAL may be a terminal id, a frame, or nil for the selected
8631 frame's terminal device. */)
8632 (terminal)
8633 Lisp_Object terminal;
8634 {
8635 struct coding_system *terminal_coding
8636 = TERMINAL_TERMINAL_CODING (get_terminal (terminal, 1));
8637 Lisp_Object coding_system = CODING_ID_NAME (terminal_coding->id);
8638
8639 /* For backward compatibility, return nil if it is `undecided'. */
8640 return (! EQ (coding_system, Qundecided) ? coding_system : Qnil);
8641 }
8642
8643 DEFUN ("set-keyboard-coding-system-internal", Fset_keyboard_coding_system_internal,
8644 Sset_keyboard_coding_system_internal, 1, 2, 0,
8645 doc: /* Internal use only. */)
8646 (coding_system, terminal)
8647 Lisp_Object coding_system;
8648 Lisp_Object terminal;
8649 {
8650 struct terminal *t = get_terminal (terminal, 1);
8651 CHECK_SYMBOL (coding_system);
8652 setup_coding_system (Fcheck_coding_system (coding_system),
8653 TERMINAL_KEYBOARD_CODING (t));
8654 /* Characer composition should be disabled. */
8655 TERMINAL_KEYBOARD_CODING (t)->common_flags
8656 &= ~CODING_ANNOTATE_COMPOSITION_MASK;
8657 return Qnil;
8658 }
8659
8660 DEFUN ("keyboard-coding-system",
8661 Fkeyboard_coding_system, Skeyboard_coding_system, 0, 1, 0,
8662 doc: /* Return coding system specified for decoding keyboard input. */)
8663 (terminal)
8664 Lisp_Object terminal;
8665 {
8666 return CODING_ID_NAME (TERMINAL_KEYBOARD_CODING
8667 (get_terminal (terminal, 1))->id);
8668 }
8669
8670 \f
8671 DEFUN ("find-operation-coding-system", Ffind_operation_coding_system,
8672 Sfind_operation_coding_system, 1, MANY, 0,
8673 doc: /* Choose a coding system for an operation based on the target name.
8674 The value names a pair of coding systems: (DECODING-SYSTEM . ENCODING-SYSTEM).
8675 DECODING-SYSTEM is the coding system to use for decoding
8676 \(in case OPERATION does decoding), and ENCODING-SYSTEM is the coding system
8677 for encoding (in case OPERATION does encoding).
8678
8679 The first argument OPERATION specifies an I/O primitive:
8680 For file I/O, `insert-file-contents' or `write-region'.
8681 For process I/O, `call-process', `call-process-region', or `start-process'.
8682 For network I/O, `open-network-stream'.
8683
8684 The remaining arguments should be the same arguments that were passed
8685 to the primitive. Depending on which primitive, one of those arguments
8686 is selected as the TARGET. For example, if OPERATION does file I/O,
8687 whichever argument specifies the file name is TARGET.
8688
8689 TARGET has a meaning which depends on OPERATION:
8690 For file I/O, TARGET is a file name (except for the special case below).
8691 For process I/O, TARGET is a process name.
8692 For network I/O, TARGET is a service name or a port number.
8693
8694 This function looks up what is specified for TARGET in
8695 `file-coding-system-alist', `process-coding-system-alist',
8696 or `network-coding-system-alist' depending on OPERATION.
8697 They may specify a coding system, a cons of coding systems,
8698 or a function symbol to call.
8699 In the last case, we call the function with one argument,
8700 which is a list of all the arguments given to this function.
8701 If the function can't decide a coding system, it can return
8702 `undecided' so that the normal code-detection is performed.
8703
8704 If OPERATION is `insert-file-contents', the argument corresponding to
8705 TARGET may be a cons (FILENAME . BUFFER). In that case, FILENAME is a
8706 file name to look up, and BUFFER is a buffer that contains the file's
8707 contents (not yet decoded). If `file-coding-system-alist' specifies a
8708 function to call for FILENAME, that function should examine the
8709 contents of BUFFER instead of reading the file.
8710
8711 usage: (find-operation-coding-system OPERATION ARGUMENTS...) */)
8712 (nargs, args)
8713 int nargs;
8714 Lisp_Object *args;
8715 {
8716 Lisp_Object operation, target_idx, target, val;
8717 register Lisp_Object chain;
8718
8719 if (nargs < 2)
8720 error ("Too few arguments");
8721 operation = args[0];
8722 if (!SYMBOLP (operation)
8723 || !INTEGERP (target_idx = Fget (operation, Qtarget_idx)))
8724 error ("Invalid first argument");
8725 if (nargs < 1 + XINT (target_idx))
8726 error ("Too few arguments for operation: %s",
8727 SDATA (SYMBOL_NAME (operation)));
8728 target = args[XINT (target_idx) + 1];
8729 if (!(STRINGP (target)
8730 || (EQ (operation, Qinsert_file_contents) && CONSP (target)
8731 && STRINGP (XCAR (target)) && BUFFERP (XCDR (target)))
8732 || (EQ (operation, Qopen_network_stream) && INTEGERP (target))))
8733 error ("Invalid %dth argument", XINT (target_idx) + 1);
8734 if (CONSP (target))
8735 target = XCAR (target);
8736
8737 chain = ((EQ (operation, Qinsert_file_contents)
8738 || EQ (operation, Qwrite_region))
8739 ? Vfile_coding_system_alist
8740 : (EQ (operation, Qopen_network_stream)
8741 ? Vnetwork_coding_system_alist
8742 : Vprocess_coding_system_alist));
8743 if (NILP (chain))
8744 return Qnil;
8745
8746 for (; CONSP (chain); chain = XCDR (chain))
8747 {
8748 Lisp_Object elt;
8749
8750 elt = XCAR (chain);
8751 if (CONSP (elt)
8752 && ((STRINGP (target)
8753 && STRINGP (XCAR (elt))
8754 && fast_string_match (XCAR (elt), target) >= 0)
8755 || (INTEGERP (target) && EQ (target, XCAR (elt)))))
8756 {
8757 val = XCDR (elt);
8758 /* Here, if VAL is both a valid coding system and a valid
8759 function symbol, we return VAL as a coding system. */
8760 if (CONSP (val))
8761 return val;
8762 if (! SYMBOLP (val))
8763 return Qnil;
8764 if (! NILP (Fcoding_system_p (val)))
8765 return Fcons (val, val);
8766 if (! NILP (Ffboundp (val)))
8767 {
8768 /* We use call1 rather than safe_call1
8769 so as to get bug reports about functions called here
8770 which don't handle the current interface. */
8771 val = call1 (val, Flist (nargs, args));
8772 if (CONSP (val))
8773 return val;
8774 if (SYMBOLP (val) && ! NILP (Fcoding_system_p (val)))
8775 return Fcons (val, val);
8776 }
8777 return Qnil;
8778 }
8779 }
8780 return Qnil;
8781 }
8782
8783 DEFUN ("set-coding-system-priority", Fset_coding_system_priority,
8784 Sset_coding_system_priority, 0, MANY, 0,
8785 doc: /* Assign higher priority to the coding systems given as arguments.
8786 If multiple coding systems belong to the same category,
8787 all but the first one are ignored.
8788
8789 usage: (set-coding-system-priority &rest coding-systems) */)
8790 (nargs, args)
8791 int nargs;
8792 Lisp_Object *args;
8793 {
8794 int i, j;
8795 int changed[coding_category_max];
8796 enum coding_category priorities[coding_category_max];
8797
8798 bzero (changed, sizeof changed);
8799
8800 for (i = j = 0; i < nargs; i++)
8801 {
8802 enum coding_category category;
8803 Lisp_Object spec, attrs;
8804
8805 CHECK_CODING_SYSTEM_GET_SPEC (args[i], spec);
8806 attrs = AREF (spec, 0);
8807 category = XINT (CODING_ATTR_CATEGORY (attrs));
8808 if (changed[category])
8809 /* Ignore this coding system because a coding system of the
8810 same category already had a higher priority. */
8811 continue;
8812 changed[category] = 1;
8813 priorities[j++] = category;
8814 if (coding_categories[category].id >= 0
8815 && ! EQ (args[i], CODING_ID_NAME (coding_categories[category].id)))
8816 setup_coding_system (args[i], &coding_categories[category]);
8817 Fset (AREF (Vcoding_category_table, category), args[i]);
8818 }
8819
8820 /* Now we have decided top J priorities. Reflect the order of the
8821 original priorities to the remaining priorities. */
8822
8823 for (i = j, j = 0; i < coding_category_max; i++, j++)
8824 {
8825 while (j < coding_category_max
8826 && changed[coding_priorities[j]])
8827 j++;
8828 if (j == coding_category_max)
8829 abort ();
8830 priorities[i] = coding_priorities[j];
8831 }
8832
8833 bcopy (priorities, coding_priorities, sizeof priorities);
8834
8835 /* Update `coding-category-list'. */
8836 Vcoding_category_list = Qnil;
8837 for (i = coding_category_max - 1; i >= 0; i--)
8838 Vcoding_category_list
8839 = Fcons (AREF (Vcoding_category_table, priorities[i]),
8840 Vcoding_category_list);
8841
8842 return Qnil;
8843 }
8844
8845 DEFUN ("coding-system-priority-list", Fcoding_system_priority_list,
8846 Scoding_system_priority_list, 0, 1, 0,
8847 doc: /* Return a list of coding systems ordered by their priorities.
8848 HIGHESTP non-nil means just return the highest priority one. */)
8849 (highestp)
8850 Lisp_Object highestp;
8851 {
8852 int i;
8853 Lisp_Object val;
8854
8855 for (i = 0, val = Qnil; i < coding_category_max; i++)
8856 {
8857 enum coding_category category = coding_priorities[i];
8858 int id = coding_categories[category].id;
8859 Lisp_Object attrs;
8860
8861 if (id < 0)
8862 continue;
8863 attrs = CODING_ID_ATTRS (id);
8864 if (! NILP (highestp))
8865 return CODING_ATTR_BASE_NAME (attrs);
8866 val = Fcons (CODING_ATTR_BASE_NAME (attrs), val);
8867 }
8868 return Fnreverse (val);
8869 }
8870
8871 static char *suffixes[] = { "-unix", "-dos", "-mac" };
8872
8873 static Lisp_Object
8874 make_subsidiaries (base)
8875 Lisp_Object base;
8876 {
8877 Lisp_Object subsidiaries;
8878 int base_name_len = SBYTES (SYMBOL_NAME (base));
8879 char *buf = (char *) alloca (base_name_len + 6);
8880 int i;
8881
8882 bcopy (SDATA (SYMBOL_NAME (base)), buf, base_name_len);
8883 subsidiaries = Fmake_vector (make_number (3), Qnil);
8884 for (i = 0; i < 3; i++)
8885 {
8886 bcopy (suffixes[i], buf + base_name_len, strlen (suffixes[i]) + 1);
8887 ASET (subsidiaries, i, intern (buf));
8888 }
8889 return subsidiaries;
8890 }
8891
8892
8893 DEFUN ("define-coding-system-internal", Fdefine_coding_system_internal,
8894 Sdefine_coding_system_internal, coding_arg_max, MANY, 0,
8895 doc: /* For internal use only.
8896 usage: (define-coding-system-internal ...) */)
8897 (nargs, args)
8898 int nargs;
8899 Lisp_Object *args;
8900 {
8901 Lisp_Object name;
8902 Lisp_Object spec_vec; /* [ ATTRS ALIASE EOL_TYPE ] */
8903 Lisp_Object attrs; /* Vector of attributes. */
8904 Lisp_Object eol_type;
8905 Lisp_Object aliases;
8906 Lisp_Object coding_type, charset_list, safe_charsets;
8907 enum coding_category category;
8908 Lisp_Object tail, val;
8909 int max_charset_id = 0;
8910 int i;
8911
8912 if (nargs < coding_arg_max)
8913 goto short_args;
8914
8915 attrs = Fmake_vector (make_number (coding_attr_last_index), Qnil);
8916
8917 name = args[coding_arg_name];
8918 CHECK_SYMBOL (name);
8919 CODING_ATTR_BASE_NAME (attrs) = name;
8920
8921 val = args[coding_arg_mnemonic];
8922 if (! STRINGP (val))
8923 CHECK_CHARACTER (val);
8924 CODING_ATTR_MNEMONIC (attrs) = val;
8925
8926 coding_type = args[coding_arg_coding_type];
8927 CHECK_SYMBOL (coding_type);
8928 CODING_ATTR_TYPE (attrs) = coding_type;
8929
8930 charset_list = args[coding_arg_charset_list];
8931 if (SYMBOLP (charset_list))
8932 {
8933 if (EQ (charset_list, Qiso_2022))
8934 {
8935 if (! EQ (coding_type, Qiso_2022))
8936 error ("Invalid charset-list");
8937 charset_list = Viso_2022_charset_list;
8938 }
8939 else if (EQ (charset_list, Qemacs_mule))
8940 {
8941 if (! EQ (coding_type, Qemacs_mule))
8942 error ("Invalid charset-list");
8943 charset_list = Vemacs_mule_charset_list;
8944 }
8945 for (tail = charset_list; CONSP (tail); tail = XCDR (tail))
8946 if (max_charset_id < XFASTINT (XCAR (tail)))
8947 max_charset_id = XFASTINT (XCAR (tail));
8948 }
8949 else
8950 {
8951 charset_list = Fcopy_sequence (charset_list);
8952 for (tail = charset_list; CONSP (tail); tail = XCDR (tail))
8953 {
8954 struct charset *charset;
8955
8956 val = XCAR (tail);
8957 CHECK_CHARSET_GET_CHARSET (val, charset);
8958 if (EQ (coding_type, Qiso_2022)
8959 ? CHARSET_ISO_FINAL (charset) < 0
8960 : EQ (coding_type, Qemacs_mule)
8961 ? CHARSET_EMACS_MULE_ID (charset) < 0
8962 : 0)
8963 error ("Can't handle charset `%s'",
8964 SDATA (SYMBOL_NAME (CHARSET_NAME (charset))));
8965
8966 XSETCAR (tail, make_number (charset->id));
8967 if (max_charset_id < charset->id)
8968 max_charset_id = charset->id;
8969 }
8970 }
8971 CODING_ATTR_CHARSET_LIST (attrs) = charset_list;
8972
8973 safe_charsets = Fmake_string (make_number (max_charset_id + 1),
8974 make_number (255));
8975 for (tail = charset_list; CONSP (tail); tail = XCDR (tail))
8976 SSET (safe_charsets, XFASTINT (XCAR (tail)), 0);
8977 CODING_ATTR_SAFE_CHARSETS (attrs) = safe_charsets;
8978
8979 CODING_ATTR_ASCII_COMPAT (attrs) = args[coding_arg_ascii_compatible_p];
8980
8981 val = args[coding_arg_decode_translation_table];
8982 if (! CHAR_TABLE_P (val) && ! CONSP (val))
8983 CHECK_SYMBOL (val);
8984 CODING_ATTR_DECODE_TBL (attrs) = val;
8985
8986 val = args[coding_arg_encode_translation_table];
8987 if (! CHAR_TABLE_P (val) && ! CONSP (val))
8988 CHECK_SYMBOL (val);
8989 CODING_ATTR_ENCODE_TBL (attrs) = val;
8990
8991 val = args[coding_arg_post_read_conversion];
8992 CHECK_SYMBOL (val);
8993 CODING_ATTR_POST_READ (attrs) = val;
8994
8995 val = args[coding_arg_pre_write_conversion];
8996 CHECK_SYMBOL (val);
8997 CODING_ATTR_PRE_WRITE (attrs) = val;
8998
8999 val = args[coding_arg_default_char];
9000 if (NILP (val))
9001 CODING_ATTR_DEFAULT_CHAR (attrs) = make_number (' ');
9002 else
9003 {
9004 CHECK_CHARACTER (val);
9005 CODING_ATTR_DEFAULT_CHAR (attrs) = val;
9006 }
9007
9008 val = args[coding_arg_for_unibyte];
9009 CODING_ATTR_FOR_UNIBYTE (attrs) = NILP (val) ? Qnil : Qt;
9010
9011 val = args[coding_arg_plist];
9012 CHECK_LIST (val);
9013 CODING_ATTR_PLIST (attrs) = val;
9014
9015 if (EQ (coding_type, Qcharset))
9016 {
9017 /* Generate a lisp vector of 256 elements. Each element is nil,
9018 integer, or a list of charset IDs.
9019
9020 If Nth element is nil, the byte code N is invalid in this
9021 coding system.
9022
9023 If Nth element is a number NUM, N is the first byte of a
9024 charset whose ID is NUM.
9025
9026 If Nth element is a list of charset IDs, N is the first byte
9027 of one of them. The list is sorted by dimensions of the
9028 charsets. A charset of smaller dimension comes firtst. */
9029 val = Fmake_vector (make_number (256), Qnil);
9030
9031 for (tail = charset_list; CONSP (tail); tail = XCDR (tail))
9032 {
9033 struct charset *charset = CHARSET_FROM_ID (XFASTINT (XCAR (tail)));
9034 int dim = CHARSET_DIMENSION (charset);
9035 int idx = (dim - 1) * 4;
9036
9037 if (CHARSET_ASCII_COMPATIBLE_P (charset))
9038 CODING_ATTR_ASCII_COMPAT (attrs) = Qt;
9039
9040 for (i = charset->code_space[idx];
9041 i <= charset->code_space[idx + 1]; i++)
9042 {
9043 Lisp_Object tmp, tmp2;
9044 int dim2;
9045
9046 tmp = AREF (val, i);
9047 if (NILP (tmp))
9048 tmp = XCAR (tail);
9049 else if (NUMBERP (tmp))
9050 {
9051 dim2 = CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (tmp)));
9052 if (dim < dim2)
9053 tmp = Fcons (XCAR (tail), Fcons (tmp, Qnil));
9054 else
9055 tmp = Fcons (tmp, Fcons (XCAR (tail), Qnil));
9056 }
9057 else
9058 {
9059 for (tmp2 = tmp; CONSP (tmp2); tmp2 = XCDR (tmp2))
9060 {
9061 dim2 = CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (XCAR (tmp2))));
9062 if (dim < dim2)
9063 break;
9064 }
9065 if (NILP (tmp2))
9066 tmp = nconc2 (tmp, Fcons (XCAR (tail), Qnil));
9067 else
9068 {
9069 XSETCDR (tmp2, Fcons (XCAR (tmp2), XCDR (tmp2)));
9070 XSETCAR (tmp2, XCAR (tail));
9071 }
9072 }
9073 ASET (val, i, tmp);
9074 }
9075 }
9076 ASET (attrs, coding_attr_charset_valids, val);
9077 category = coding_category_charset;
9078 }
9079 else if (EQ (coding_type, Qccl))
9080 {
9081 Lisp_Object valids;
9082
9083 if (nargs < coding_arg_ccl_max)
9084 goto short_args;
9085
9086 val = args[coding_arg_ccl_decoder];
9087 CHECK_CCL_PROGRAM (val);
9088 if (VECTORP (val))
9089 val = Fcopy_sequence (val);
9090 ASET (attrs, coding_attr_ccl_decoder, val);
9091
9092 val = args[coding_arg_ccl_encoder];
9093 CHECK_CCL_PROGRAM (val);
9094 if (VECTORP (val))
9095 val = Fcopy_sequence (val);
9096 ASET (attrs, coding_attr_ccl_encoder, val);
9097
9098 val = args[coding_arg_ccl_valids];
9099 valids = Fmake_string (make_number (256), make_number (0));
9100 for (tail = val; !NILP (tail); tail = Fcdr (tail))
9101 {
9102 int from, to;
9103
9104 val = Fcar (tail);
9105 if (INTEGERP (val))
9106 {
9107 from = to = XINT (val);
9108 if (from < 0 || from > 255)
9109 args_out_of_range_3 (val, make_number (0), make_number (255));
9110 }
9111 else
9112 {
9113 CHECK_CONS (val);
9114 CHECK_NATNUM_CAR (val);
9115 CHECK_NATNUM_CDR (val);
9116 from = XINT (XCAR (val));
9117 if (from > 255)
9118 args_out_of_range_3 (XCAR (val),
9119 make_number (0), make_number (255));
9120 to = XINT (XCDR (val));
9121 if (to < from || to > 255)
9122 args_out_of_range_3 (XCDR (val),
9123 XCAR (val), make_number (255));
9124 }
9125 for (i = from; i <= to; i++)
9126 SSET (valids, i, 1);
9127 }
9128 ASET (attrs, coding_attr_ccl_valids, valids);
9129
9130 category = coding_category_ccl;
9131 }
9132 else if (EQ (coding_type, Qutf_16))
9133 {
9134 Lisp_Object bom, endian;
9135
9136 CODING_ATTR_ASCII_COMPAT (attrs) = Qnil;
9137
9138 if (nargs < coding_arg_utf16_max)
9139 goto short_args;
9140
9141 bom = args[coding_arg_utf16_bom];
9142 if (! NILP (bom) && ! EQ (bom, Qt))
9143 {
9144 CHECK_CONS (bom);
9145 val = XCAR (bom);
9146 CHECK_CODING_SYSTEM (val);
9147 val = XCDR (bom);
9148 CHECK_CODING_SYSTEM (val);
9149 }
9150 ASET (attrs, coding_attr_utf_16_bom, bom);
9151
9152 endian = args[coding_arg_utf16_endian];
9153 CHECK_SYMBOL (endian);
9154 if (NILP (endian))
9155 endian = Qbig;
9156 else if (! EQ (endian, Qbig) && ! EQ (endian, Qlittle))
9157 error ("Invalid endian: %s", SDATA (SYMBOL_NAME (endian)));
9158 ASET (attrs, coding_attr_utf_16_endian, endian);
9159
9160 category = (CONSP (bom)
9161 ? coding_category_utf_16_auto
9162 : NILP (bom)
9163 ? (EQ (endian, Qbig)
9164 ? coding_category_utf_16_be_nosig
9165 : coding_category_utf_16_le_nosig)
9166 : (EQ (endian, Qbig)
9167 ? coding_category_utf_16_be
9168 : coding_category_utf_16_le));
9169 }
9170 else if (EQ (coding_type, Qiso_2022))
9171 {
9172 Lisp_Object initial, reg_usage, request, flags;
9173 int i;
9174
9175 if (nargs < coding_arg_iso2022_max)
9176 goto short_args;
9177
9178 initial = Fcopy_sequence (args[coding_arg_iso2022_initial]);
9179 CHECK_VECTOR (initial);
9180 for (i = 0; i < 4; i++)
9181 {
9182 val = Faref (initial, make_number (i));
9183 if (! NILP (val))
9184 {
9185 struct charset *charset;
9186
9187 CHECK_CHARSET_GET_CHARSET (val, charset);
9188 ASET (initial, i, make_number (CHARSET_ID (charset)));
9189 if (i == 0 && CHARSET_ASCII_COMPATIBLE_P (charset))
9190 CODING_ATTR_ASCII_COMPAT (attrs) = Qt;
9191 }
9192 else
9193 ASET (initial, i, make_number (-1));
9194 }
9195
9196 reg_usage = args[coding_arg_iso2022_reg_usage];
9197 CHECK_CONS (reg_usage);
9198 CHECK_NUMBER_CAR (reg_usage);
9199 CHECK_NUMBER_CDR (reg_usage);
9200
9201 request = Fcopy_sequence (args[coding_arg_iso2022_request]);
9202 for (tail = request; ! NILP (tail); tail = Fcdr (tail))
9203 {
9204 int id;
9205 Lisp_Object tmp;
9206
9207 val = Fcar (tail);
9208 CHECK_CONS (val);
9209 tmp = XCAR (val);
9210 CHECK_CHARSET_GET_ID (tmp, id);
9211 CHECK_NATNUM_CDR (val);
9212 if (XINT (XCDR (val)) >= 4)
9213 error ("Invalid graphic register number: %d", XINT (XCDR (val)));
9214 XSETCAR (val, make_number (id));
9215 }
9216
9217 flags = args[coding_arg_iso2022_flags];
9218 CHECK_NATNUM (flags);
9219 i = XINT (flags);
9220 if (EQ (args[coding_arg_charset_list], Qiso_2022))
9221 flags = make_number (i | CODING_ISO_FLAG_FULL_SUPPORT);
9222
9223 ASET (attrs, coding_attr_iso_initial, initial);
9224 ASET (attrs, coding_attr_iso_usage, reg_usage);
9225 ASET (attrs, coding_attr_iso_request, request);
9226 ASET (attrs, coding_attr_iso_flags, flags);
9227 setup_iso_safe_charsets (attrs);
9228
9229 if (i & CODING_ISO_FLAG_SEVEN_BITS)
9230 category = ((i & (CODING_ISO_FLAG_LOCKING_SHIFT
9231 | CODING_ISO_FLAG_SINGLE_SHIFT))
9232 ? coding_category_iso_7_else
9233 : EQ (args[coding_arg_charset_list], Qiso_2022)
9234 ? coding_category_iso_7
9235 : coding_category_iso_7_tight);
9236 else
9237 {
9238 int id = XINT (AREF (initial, 1));
9239
9240 category = (((i & CODING_ISO_FLAG_LOCKING_SHIFT)
9241 || EQ (args[coding_arg_charset_list], Qiso_2022)
9242 || id < 0)
9243 ? coding_category_iso_8_else
9244 : (CHARSET_DIMENSION (CHARSET_FROM_ID (id)) == 1)
9245 ? coding_category_iso_8_1
9246 : coding_category_iso_8_2);
9247 }
9248 if (category != coding_category_iso_8_1
9249 && category != coding_category_iso_8_2)
9250 CODING_ATTR_ASCII_COMPAT (attrs) = Qnil;
9251 }
9252 else if (EQ (coding_type, Qemacs_mule))
9253 {
9254 if (EQ (args[coding_arg_charset_list], Qemacs_mule))
9255 ASET (attrs, coding_attr_emacs_mule_full, Qt);
9256 CODING_ATTR_ASCII_COMPAT (attrs) = Qt;
9257 category = coding_category_emacs_mule;
9258 }
9259 else if (EQ (coding_type, Qshift_jis))
9260 {
9261
9262 struct charset *charset;
9263
9264 if (XINT (Flength (charset_list)) != 3
9265 && XINT (Flength (charset_list)) != 4)
9266 error ("There should be three or four charsets");
9267
9268 charset = CHARSET_FROM_ID (XINT (XCAR (charset_list)));
9269 if (CHARSET_DIMENSION (charset) != 1)
9270 error ("Dimension of charset %s is not one",
9271 SDATA (SYMBOL_NAME (CHARSET_NAME (charset))));
9272 if (CHARSET_ASCII_COMPATIBLE_P (charset))
9273 CODING_ATTR_ASCII_COMPAT (attrs) = Qt;
9274
9275 charset_list = XCDR (charset_list);
9276 charset = CHARSET_FROM_ID (XINT (XCAR (charset_list)));
9277 if (CHARSET_DIMENSION (charset) != 1)
9278 error ("Dimension of charset %s is not one",
9279 SDATA (SYMBOL_NAME (CHARSET_NAME (charset))));
9280
9281 charset_list = XCDR (charset_list);
9282 charset = CHARSET_FROM_ID (XINT (XCAR (charset_list)));
9283 if (CHARSET_DIMENSION (charset) != 2)
9284 error ("Dimension of charset %s is not two",
9285 SDATA (SYMBOL_NAME (CHARSET_NAME (charset))));
9286
9287 charset_list = XCDR (charset_list);
9288 if (! NILP (charset_list))
9289 {
9290 charset = CHARSET_FROM_ID (XINT (XCAR (charset_list)));
9291 if (CHARSET_DIMENSION (charset) != 2)
9292 error ("Dimension of charset %s is not two",
9293 SDATA (SYMBOL_NAME (CHARSET_NAME (charset))));
9294 }
9295
9296 category = coding_category_sjis;
9297 Vsjis_coding_system = name;
9298 }
9299 else if (EQ (coding_type, Qbig5))
9300 {
9301 struct charset *charset;
9302
9303 if (XINT (Flength (charset_list)) != 2)
9304 error ("There should be just two charsets");
9305
9306 charset = CHARSET_FROM_ID (XINT (XCAR (charset_list)));
9307 if (CHARSET_DIMENSION (charset) != 1)
9308 error ("Dimension of charset %s is not one",
9309 SDATA (SYMBOL_NAME (CHARSET_NAME (charset))));
9310 if (CHARSET_ASCII_COMPATIBLE_P (charset))
9311 CODING_ATTR_ASCII_COMPAT (attrs) = Qt;
9312
9313 charset_list = XCDR (charset_list);
9314 charset = CHARSET_FROM_ID (XINT (XCAR (charset_list)));
9315 if (CHARSET_DIMENSION (charset) != 2)
9316 error ("Dimension of charset %s is not two",
9317 SDATA (SYMBOL_NAME (CHARSET_NAME (charset))));
9318
9319 category = coding_category_big5;
9320 Vbig5_coding_system = name;
9321 }
9322 else if (EQ (coding_type, Qraw_text))
9323 {
9324 category = coding_category_raw_text;
9325 CODING_ATTR_ASCII_COMPAT (attrs) = Qt;
9326 }
9327 else if (EQ (coding_type, Qutf_8))
9328 {
9329 category = coding_category_utf_8;
9330 CODING_ATTR_ASCII_COMPAT (attrs) = Qt;
9331 }
9332 else if (EQ (coding_type, Qundecided))
9333 category = coding_category_undecided;
9334 else
9335 error ("Invalid coding system type: %s",
9336 SDATA (SYMBOL_NAME (coding_type)));
9337
9338 CODING_ATTR_CATEGORY (attrs) = make_number (category);
9339 CODING_ATTR_PLIST (attrs)
9340 = Fcons (QCcategory, Fcons (AREF (Vcoding_category_table, category),
9341 CODING_ATTR_PLIST (attrs)));
9342 CODING_ATTR_PLIST (attrs)
9343 = Fcons (QCascii_compatible_p,
9344 Fcons (CODING_ATTR_ASCII_COMPAT (attrs),
9345 CODING_ATTR_PLIST (attrs)));
9346
9347 eol_type = args[coding_arg_eol_type];
9348 if (! NILP (eol_type)
9349 && ! EQ (eol_type, Qunix)
9350 && ! EQ (eol_type, Qdos)
9351 && ! EQ (eol_type, Qmac))
9352 error ("Invalid eol-type");
9353
9354 aliases = Fcons (name, Qnil);
9355
9356 if (NILP (eol_type))
9357 {
9358 eol_type = make_subsidiaries (name);
9359 for (i = 0; i < 3; i++)
9360 {
9361 Lisp_Object this_spec, this_name, this_aliases, this_eol_type;
9362
9363 this_name = AREF (eol_type, i);
9364 this_aliases = Fcons (this_name, Qnil);
9365 this_eol_type = (i == 0 ? Qunix : i == 1 ? Qdos : Qmac);
9366 this_spec = Fmake_vector (make_number (3), attrs);
9367 ASET (this_spec, 1, this_aliases);
9368 ASET (this_spec, 2, this_eol_type);
9369 Fputhash (this_name, this_spec, Vcoding_system_hash_table);
9370 Vcoding_system_list = Fcons (this_name, Vcoding_system_list);
9371 val = Fassoc (Fsymbol_name (this_name), Vcoding_system_alist);
9372 if (NILP (val))
9373 Vcoding_system_alist
9374 = Fcons (Fcons (Fsymbol_name (this_name), Qnil),
9375 Vcoding_system_alist);
9376 }
9377 }
9378
9379 spec_vec = Fmake_vector (make_number (3), attrs);
9380 ASET (spec_vec, 1, aliases);
9381 ASET (spec_vec, 2, eol_type);
9382
9383 Fputhash (name, spec_vec, Vcoding_system_hash_table);
9384 Vcoding_system_list = Fcons (name, Vcoding_system_list);
9385 val = Fassoc (Fsymbol_name (name), Vcoding_system_alist);
9386 if (NILP (val))
9387 Vcoding_system_alist = Fcons (Fcons (Fsymbol_name (name), Qnil),
9388 Vcoding_system_alist);
9389
9390 {
9391 int id = coding_categories[category].id;
9392
9393 if (id < 0 || EQ (name, CODING_ID_NAME (id)))
9394 setup_coding_system (name, &coding_categories[category]);
9395 }
9396
9397 return Qnil;
9398
9399 short_args:
9400 return Fsignal (Qwrong_number_of_arguments,
9401 Fcons (intern ("define-coding-system-internal"),
9402 make_number (nargs)));
9403 }
9404
9405
9406 DEFUN ("coding-system-put", Fcoding_system_put, Scoding_system_put,
9407 3, 3, 0,
9408 doc: /* Change value in CODING-SYSTEM's property list PROP to VAL. */)
9409 (coding_system, prop, val)
9410 Lisp_Object coding_system, prop, val;
9411 {
9412 Lisp_Object spec, attrs;
9413
9414 CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec);
9415 attrs = AREF (spec, 0);
9416 if (EQ (prop, QCmnemonic))
9417 {
9418 if (! STRINGP (val))
9419 CHECK_CHARACTER (val);
9420 CODING_ATTR_MNEMONIC (attrs) = val;
9421 }
9422 else if (EQ (prop, QCdefalut_char))
9423 {
9424 if (NILP (val))
9425 val = make_number (' ');
9426 else
9427 CHECK_CHARACTER (val);
9428 CODING_ATTR_DEFAULT_CHAR (attrs) = val;
9429 }
9430 else if (EQ (prop, QCdecode_translation_table))
9431 {
9432 if (! CHAR_TABLE_P (val) && ! CONSP (val))
9433 CHECK_SYMBOL (val);
9434 CODING_ATTR_DECODE_TBL (attrs) = val;
9435 }
9436 else if (EQ (prop, QCencode_translation_table))
9437 {
9438 if (! CHAR_TABLE_P (val) && ! CONSP (val))
9439 CHECK_SYMBOL (val);
9440 CODING_ATTR_ENCODE_TBL (attrs) = val;
9441 }
9442 else if (EQ (prop, QCpost_read_conversion))
9443 {
9444 CHECK_SYMBOL (val);
9445 CODING_ATTR_POST_READ (attrs) = val;
9446 }
9447 else if (EQ (prop, QCpre_write_conversion))
9448 {
9449 CHECK_SYMBOL (val);
9450 CODING_ATTR_PRE_WRITE (attrs) = val;
9451 }
9452 else if (EQ (prop, QCascii_compatible_p))
9453 {
9454 CODING_ATTR_ASCII_COMPAT (attrs) = val;
9455 }
9456
9457 CODING_ATTR_PLIST (attrs)
9458 = Fplist_put (CODING_ATTR_PLIST (attrs), prop, val);
9459 return val;
9460 }
9461
9462
9463 DEFUN ("define-coding-system-alias", Fdefine_coding_system_alias,
9464 Sdefine_coding_system_alias, 2, 2, 0,
9465 doc: /* Define ALIAS as an alias for CODING-SYSTEM. */)
9466 (alias, coding_system)
9467 Lisp_Object alias, coding_system;
9468 {
9469 Lisp_Object spec, aliases, eol_type, val;
9470
9471 CHECK_SYMBOL (alias);
9472 CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec);
9473 aliases = AREF (spec, 1);
9474 /* ALIASES should be a list of length more than zero, and the first
9475 element is a base coding system. Append ALIAS at the tail of the
9476 list. */
9477 while (!NILP (XCDR (aliases)))
9478 aliases = XCDR (aliases);
9479 XSETCDR (aliases, Fcons (alias, Qnil));
9480
9481 eol_type = AREF (spec, 2);
9482 if (VECTORP (eol_type))
9483 {
9484 Lisp_Object subsidiaries;
9485 int i;
9486
9487 subsidiaries = make_subsidiaries (alias);
9488 for (i = 0; i < 3; i++)
9489 Fdefine_coding_system_alias (AREF (subsidiaries, i),
9490 AREF (eol_type, i));
9491 }
9492
9493 Fputhash (alias, spec, Vcoding_system_hash_table);
9494 Vcoding_system_list = Fcons (alias, Vcoding_system_list);
9495 val = Fassoc (Fsymbol_name (alias), Vcoding_system_alist);
9496 if (NILP (val))
9497 Vcoding_system_alist = Fcons (Fcons (Fsymbol_name (alias), Qnil),
9498 Vcoding_system_alist);
9499
9500 return Qnil;
9501 }
9502
9503 DEFUN ("coding-system-base", Fcoding_system_base, Scoding_system_base,
9504 1, 1, 0,
9505 doc: /* Return the base of CODING-SYSTEM.
9506 Any alias or subsidiary coding system is not a base coding system. */)
9507 (coding_system)
9508 Lisp_Object coding_system;
9509 {
9510 Lisp_Object spec, attrs;
9511
9512 if (NILP (coding_system))
9513 return (Qno_conversion);
9514 CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec);
9515 attrs = AREF (spec, 0);
9516 return CODING_ATTR_BASE_NAME (attrs);
9517 }
9518
9519 DEFUN ("coding-system-plist", Fcoding_system_plist, Scoding_system_plist,
9520 1, 1, 0,
9521 doc: "Return the property list of CODING-SYSTEM.")
9522 (coding_system)
9523 Lisp_Object coding_system;
9524 {
9525 Lisp_Object spec, attrs;
9526
9527 if (NILP (coding_system))
9528 coding_system = Qno_conversion;
9529 CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec);
9530 attrs = AREF (spec, 0);
9531 return CODING_ATTR_PLIST (attrs);
9532 }
9533
9534
9535 DEFUN ("coding-system-aliases", Fcoding_system_aliases, Scoding_system_aliases,
9536 1, 1, 0,
9537 doc: /* Return the list of aliases of CODING-SYSTEM. */)
9538 (coding_system)
9539 Lisp_Object coding_system;
9540 {
9541 Lisp_Object spec;
9542
9543 if (NILP (coding_system))
9544 coding_system = Qno_conversion;
9545 CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec);
9546 return AREF (spec, 1);
9547 }
9548
9549 DEFUN ("coding-system-eol-type", Fcoding_system_eol_type,
9550 Scoding_system_eol_type, 1, 1, 0,
9551 doc: /* Return eol-type of CODING-SYSTEM.
9552 An eol-type is an integer 0, 1, 2, or a vector of coding systems.
9553
9554 Integer values 0, 1, and 2 indicate a format of end-of-line; LF, CRLF,
9555 and CR respectively.
9556
9557 A vector value indicates that a format of end-of-line should be
9558 detected automatically. Nth element of the vector is the subsidiary
9559 coding system whose eol-type is N. */)
9560 (coding_system)
9561 Lisp_Object coding_system;
9562 {
9563 Lisp_Object spec, eol_type;
9564 int n;
9565
9566 if (NILP (coding_system))
9567 coding_system = Qno_conversion;
9568 if (! CODING_SYSTEM_P (coding_system))
9569 return Qnil;
9570 spec = CODING_SYSTEM_SPEC (coding_system);
9571 eol_type = AREF (spec, 2);
9572 if (VECTORP (eol_type))
9573 return Fcopy_sequence (eol_type);
9574 n = EQ (eol_type, Qunix) ? 0 : EQ (eol_type, Qdos) ? 1 : 2;
9575 return make_number (n);
9576 }
9577
9578 #endif /* emacs */
9579
9580 \f
9581 /*** 9. Post-amble ***/
9582
9583 void
9584 init_coding_once ()
9585 {
9586 int i;
9587
9588 for (i = 0; i < coding_category_max; i++)
9589 {
9590 coding_categories[i].id = -1;
9591 coding_priorities[i] = i;
9592 }
9593
9594 /* ISO2022 specific initialize routine. */
9595 for (i = 0; i < 0x20; i++)
9596 iso_code_class[i] = ISO_control_0;
9597 for (i = 0x21; i < 0x7F; i++)
9598 iso_code_class[i] = ISO_graphic_plane_0;
9599 for (i = 0x80; i < 0xA0; i++)
9600 iso_code_class[i] = ISO_control_1;
9601 for (i = 0xA1; i < 0xFF; i++)
9602 iso_code_class[i] = ISO_graphic_plane_1;
9603 iso_code_class[0x20] = iso_code_class[0x7F] = ISO_0x20_or_0x7F;
9604 iso_code_class[0xA0] = iso_code_class[0xFF] = ISO_0xA0_or_0xFF;
9605 iso_code_class[ISO_CODE_SO] = ISO_shift_out;
9606 iso_code_class[ISO_CODE_SI] = ISO_shift_in;
9607 iso_code_class[ISO_CODE_SS2_7] = ISO_single_shift_2_7;
9608 iso_code_class[ISO_CODE_ESC] = ISO_escape;
9609 iso_code_class[ISO_CODE_SS2] = ISO_single_shift_2;
9610 iso_code_class[ISO_CODE_SS3] = ISO_single_shift_3;
9611 iso_code_class[ISO_CODE_CSI] = ISO_control_sequence_introducer;
9612
9613 for (i = 0; i < 256; i++)
9614 {
9615 emacs_mule_bytes[i] = 1;
9616 }
9617 emacs_mule_bytes[EMACS_MULE_LEADING_CODE_PRIVATE_11] = 3;
9618 emacs_mule_bytes[EMACS_MULE_LEADING_CODE_PRIVATE_12] = 3;
9619 emacs_mule_bytes[EMACS_MULE_LEADING_CODE_PRIVATE_21] = 4;
9620 emacs_mule_bytes[EMACS_MULE_LEADING_CODE_PRIVATE_22] = 4;
9621 }
9622
9623 #ifdef emacs
9624
9625 void
9626 syms_of_coding ()
9627 {
9628 staticpro (&Vcoding_system_hash_table);
9629 {
9630 Lisp_Object args[2];
9631 args[0] = QCtest;
9632 args[1] = Qeq;
9633 Vcoding_system_hash_table = Fmake_hash_table (2, args);
9634 }
9635
9636 staticpro (&Vsjis_coding_system);
9637 Vsjis_coding_system = Qnil;
9638
9639 staticpro (&Vbig5_coding_system);
9640 Vbig5_coding_system = Qnil;
9641
9642 staticpro (&Vcode_conversion_reused_workbuf);
9643 Vcode_conversion_reused_workbuf = Qnil;
9644
9645 staticpro (&Vcode_conversion_workbuf_name);
9646 Vcode_conversion_workbuf_name = build_string (" *code-conversion-work*");
9647
9648 reused_workbuf_in_use = 0;
9649
9650 DEFSYM (Qcharset, "charset");
9651 DEFSYM (Qtarget_idx, "target-idx");
9652 DEFSYM (Qcoding_system_history, "coding-system-history");
9653 Fset (Qcoding_system_history, Qnil);
9654
9655 /* Target FILENAME is the first argument. */
9656 Fput (Qinsert_file_contents, Qtarget_idx, make_number (0));
9657 /* Target FILENAME is the third argument. */
9658 Fput (Qwrite_region, Qtarget_idx, make_number (2));
9659
9660 DEFSYM (Qcall_process, "call-process");
9661 /* Target PROGRAM is the first argument. */
9662 Fput (Qcall_process, Qtarget_idx, make_number (0));
9663
9664 DEFSYM (Qcall_process_region, "call-process-region");
9665 /* Target PROGRAM is the third argument. */
9666 Fput (Qcall_process_region, Qtarget_idx, make_number (2));
9667
9668 DEFSYM (Qstart_process, "start-process");
9669 /* Target PROGRAM is the third argument. */
9670 Fput (Qstart_process, Qtarget_idx, make_number (2));
9671
9672 DEFSYM (Qopen_network_stream, "open-network-stream");
9673 /* Target SERVICE is the fourth argument. */
9674 Fput (Qopen_network_stream, Qtarget_idx, make_number (3));
9675
9676 DEFSYM (Qcoding_system, "coding-system");
9677 DEFSYM (Qcoding_aliases, "coding-aliases");
9678
9679 DEFSYM (Qeol_type, "eol-type");
9680 DEFSYM (Qunix, "unix");
9681 DEFSYM (Qdos, "dos");
9682
9683 DEFSYM (Qbuffer_file_coding_system, "buffer-file-coding-system");
9684 DEFSYM (Qpost_read_conversion, "post-read-conversion");
9685 DEFSYM (Qpre_write_conversion, "pre-write-conversion");
9686 DEFSYM (Qdefault_char, "default-char");
9687 DEFSYM (Qundecided, "undecided");
9688 DEFSYM (Qno_conversion, "no-conversion");
9689 DEFSYM (Qraw_text, "raw-text");
9690
9691 DEFSYM (Qiso_2022, "iso-2022");
9692
9693 DEFSYM (Qutf_8, "utf-8");
9694 DEFSYM (Qutf_8_emacs, "utf-8-emacs");
9695
9696 DEFSYM (Qutf_16, "utf-16");
9697 DEFSYM (Qbig, "big");
9698 DEFSYM (Qlittle, "little");
9699
9700 DEFSYM (Qshift_jis, "shift-jis");
9701 DEFSYM (Qbig5, "big5");
9702
9703 DEFSYM (Qcoding_system_p, "coding-system-p");
9704
9705 DEFSYM (Qcoding_system_error, "coding-system-error");
9706 Fput (Qcoding_system_error, Qerror_conditions,
9707 Fcons (Qcoding_system_error, Fcons (Qerror, Qnil)));
9708 Fput (Qcoding_system_error, Qerror_message,
9709 build_string ("Invalid coding system"));
9710
9711 /* Intern this now in case it isn't already done.
9712 Setting this variable twice is harmless.
9713 But don't staticpro it here--that is done in alloc.c. */
9714 Qchar_table_extra_slots = intern ("char-table-extra-slots");
9715
9716 DEFSYM (Qtranslation_table, "translation-table");
9717 Fput (Qtranslation_table, Qchar_table_extra_slots, make_number (2));
9718 DEFSYM (Qtranslation_table_id, "translation-table-id");
9719 DEFSYM (Qtranslation_table_for_decode, "translation-table-for-decode");
9720 DEFSYM (Qtranslation_table_for_encode, "translation-table-for-encode");
9721
9722 DEFSYM (Qvalid_codes, "valid-codes");
9723
9724 DEFSYM (Qemacs_mule, "emacs-mule");
9725
9726 DEFSYM (QCcategory, ":category");
9727 DEFSYM (QCmnemonic, ":mnemonic");
9728 DEFSYM (QCdefalut_char, ":default-char");
9729 DEFSYM (QCdecode_translation_table, ":decode-translation-table");
9730 DEFSYM (QCencode_translation_table, ":encode-translation-table");
9731 DEFSYM (QCpost_read_conversion, ":post-read-conversion");
9732 DEFSYM (QCpre_write_conversion, ":pre-write-conversion");
9733 DEFSYM (QCascii_compatible_p, ":ascii-compatible-p");
9734
9735 Vcoding_category_table
9736 = Fmake_vector (make_number (coding_category_max), Qnil);
9737 staticpro (&Vcoding_category_table);
9738 /* Followings are target of code detection. */
9739 ASET (Vcoding_category_table, coding_category_iso_7,
9740 intern ("coding-category-iso-7"));
9741 ASET (Vcoding_category_table, coding_category_iso_7_tight,
9742 intern ("coding-category-iso-7-tight"));
9743 ASET (Vcoding_category_table, coding_category_iso_8_1,
9744 intern ("coding-category-iso-8-1"));
9745 ASET (Vcoding_category_table, coding_category_iso_8_2,
9746 intern ("coding-category-iso-8-2"));
9747 ASET (Vcoding_category_table, coding_category_iso_7_else,
9748 intern ("coding-category-iso-7-else"));
9749 ASET (Vcoding_category_table, coding_category_iso_8_else,
9750 intern ("coding-category-iso-8-else"));
9751 ASET (Vcoding_category_table, coding_category_utf_8,
9752 intern ("coding-category-utf-8"));
9753 ASET (Vcoding_category_table, coding_category_utf_16_be,
9754 intern ("coding-category-utf-16-be"));
9755 ASET (Vcoding_category_table, coding_category_utf_16_auto,
9756 intern ("coding-category-utf-16-auto"));
9757 ASET (Vcoding_category_table, coding_category_utf_16_le,
9758 intern ("coding-category-utf-16-le"));
9759 ASET (Vcoding_category_table, coding_category_utf_16_be_nosig,
9760 intern ("coding-category-utf-16-be-nosig"));
9761 ASET (Vcoding_category_table, coding_category_utf_16_le_nosig,
9762 intern ("coding-category-utf-16-le-nosig"));
9763 ASET (Vcoding_category_table, coding_category_charset,
9764 intern ("coding-category-charset"));
9765 ASET (Vcoding_category_table, coding_category_sjis,
9766 intern ("coding-category-sjis"));
9767 ASET (Vcoding_category_table, coding_category_big5,
9768 intern ("coding-category-big5"));
9769 ASET (Vcoding_category_table, coding_category_ccl,
9770 intern ("coding-category-ccl"));
9771 ASET (Vcoding_category_table, coding_category_emacs_mule,
9772 intern ("coding-category-emacs-mule"));
9773 /* Followings are NOT target of code detection. */
9774 ASET (Vcoding_category_table, coding_category_raw_text,
9775 intern ("coding-category-raw-text"));
9776 ASET (Vcoding_category_table, coding_category_undecided,
9777 intern ("coding-category-undecided"));
9778
9779 DEFSYM (Qinsufficient_source, "insufficient-source");
9780 DEFSYM (Qinconsistent_eol, "inconsistent-eol");
9781 DEFSYM (Qinvalid_source, "invalid-source");
9782 DEFSYM (Qinterrupted, "interrupted");
9783 DEFSYM (Qinsufficient_memory, "insufficient-memory");
9784 DEFSYM (Qcoding_system_define_form, "coding-system-define-form");
9785
9786 defsubr (&Scoding_system_p);
9787 defsubr (&Sread_coding_system);
9788 defsubr (&Sread_non_nil_coding_system);
9789 defsubr (&Scheck_coding_system);
9790 defsubr (&Sdetect_coding_region);
9791 defsubr (&Sdetect_coding_string);
9792 defsubr (&Sfind_coding_systems_region_internal);
9793 defsubr (&Sunencodable_char_position);
9794 defsubr (&Scheck_coding_systems_region);
9795 defsubr (&Sdecode_coding_region);
9796 defsubr (&Sencode_coding_region);
9797 defsubr (&Sdecode_coding_string);
9798 defsubr (&Sencode_coding_string);
9799 defsubr (&Sdecode_sjis_char);
9800 defsubr (&Sencode_sjis_char);
9801 defsubr (&Sdecode_big5_char);
9802 defsubr (&Sencode_big5_char);
9803 defsubr (&Sset_terminal_coding_system_internal);
9804 defsubr (&Sset_safe_terminal_coding_system_internal);
9805 defsubr (&Sterminal_coding_system);
9806 defsubr (&Sset_keyboard_coding_system_internal);
9807 defsubr (&Skeyboard_coding_system);
9808 defsubr (&Sfind_operation_coding_system);
9809 defsubr (&Sset_coding_system_priority);
9810 defsubr (&Sdefine_coding_system_internal);
9811 defsubr (&Sdefine_coding_system_alias);
9812 defsubr (&Scoding_system_put);
9813 defsubr (&Scoding_system_base);
9814 defsubr (&Scoding_system_plist);
9815 defsubr (&Scoding_system_aliases);
9816 defsubr (&Scoding_system_eol_type);
9817 defsubr (&Scoding_system_priority_list);
9818
9819 DEFVAR_LISP ("coding-system-list", &Vcoding_system_list,
9820 doc: /* List of coding systems.
9821
9822 Do not alter the value of this variable manually. This variable should be
9823 updated by the functions `define-coding-system' and
9824 `define-coding-system-alias'. */);
9825 Vcoding_system_list = Qnil;
9826
9827 DEFVAR_LISP ("coding-system-alist", &Vcoding_system_alist,
9828 doc: /* Alist of coding system names.
9829 Each element is one element list of coding system name.
9830 This variable is given to `completing-read' as TABLE argument.
9831
9832 Do not alter the value of this variable manually. This variable should be
9833 updated by the functions `make-coding-system' and
9834 `define-coding-system-alias'. */);
9835 Vcoding_system_alist = Qnil;
9836
9837 DEFVAR_LISP ("coding-category-list", &Vcoding_category_list,
9838 doc: /* List of coding-categories (symbols) ordered by priority.
9839
9840 On detecting a coding system, Emacs tries code detection algorithms
9841 associated with each coding-category one by one in this order. When
9842 one algorithm agrees with a byte sequence of source text, the coding
9843 system bound to the corresponding coding-category is selected.
9844
9845 Don't modify this variable directly, but use `set-coding-priority'. */);
9846 {
9847 int i;
9848
9849 Vcoding_category_list = Qnil;
9850 for (i = coding_category_max - 1; i >= 0; i--)
9851 Vcoding_category_list
9852 = Fcons (XVECTOR (Vcoding_category_table)->contents[i],
9853 Vcoding_category_list);
9854 }
9855
9856 DEFVAR_LISP ("coding-system-for-read", &Vcoding_system_for_read,
9857 doc: /* Specify the coding system for read operations.
9858 It is useful to bind this variable with `let', but do not set it globally.
9859 If the value is a coding system, it is used for decoding on read operation.
9860 If not, an appropriate element is used from one of the coding system alists:
9861 There are three such tables, `file-coding-system-alist',
9862 `process-coding-system-alist', and `network-coding-system-alist'. */);
9863 Vcoding_system_for_read = Qnil;
9864
9865 DEFVAR_LISP ("coding-system-for-write", &Vcoding_system_for_write,
9866 doc: /* Specify the coding system for write operations.
9867 Programs bind this variable with `let', but you should not set it globally.
9868 If the value is a coding system, it is used for encoding of output,
9869 when writing it to a file and when sending it to a file or subprocess.
9870
9871 If this does not specify a coding system, an appropriate element
9872 is used from one of the coding system alists:
9873 There are three such tables, `file-coding-system-alist',
9874 `process-coding-system-alist', and `network-coding-system-alist'.
9875 For output to files, if the above procedure does not specify a coding system,
9876 the value of `buffer-file-coding-system' is used. */);
9877 Vcoding_system_for_write = Qnil;
9878
9879 DEFVAR_LISP ("last-coding-system-used", &Vlast_coding_system_used,
9880 doc: /*
9881 Coding system used in the latest file or process I/O. */);
9882 Vlast_coding_system_used = Qnil;
9883
9884 DEFVAR_LISP ("last-code-conversion-error", &Vlast_code_conversion_error,
9885 doc: /*
9886 Error status of the last code conversion.
9887
9888 When an error was detected in the last code conversion, this variable
9889 is set to one of the following symbols.
9890 `insufficient-source'
9891 `inconsistent-eol'
9892 `invalid-source'
9893 `interrupted'
9894 `insufficient-memory'
9895 When no error was detected, the value doesn't change. So, to check
9896 the error status of a code conversion by this variable, you must
9897 explicitly set this variable to nil before performing code
9898 conversion. */);
9899 Vlast_code_conversion_error = Qnil;
9900
9901 DEFVAR_BOOL ("inhibit-eol-conversion", &inhibit_eol_conversion,
9902 doc: /*
9903 *Non-nil means always inhibit code conversion of end-of-line format.
9904 See info node `Coding Systems' and info node `Text and Binary' concerning
9905 such conversion. */);
9906 inhibit_eol_conversion = 0;
9907
9908 DEFVAR_BOOL ("inherit-process-coding-system", &inherit_process_coding_system,
9909 doc: /*
9910 Non-nil means process buffer inherits coding system of process output.
9911 Bind it to t if the process output is to be treated as if it were a file
9912 read from some filesystem. */);
9913 inherit_process_coding_system = 0;
9914
9915 DEFVAR_LISP ("file-coding-system-alist", &Vfile_coding_system_alist,
9916 doc: /*
9917 Alist to decide a coding system to use for a file I/O operation.
9918 The format is ((PATTERN . VAL) ...),
9919 where PATTERN is a regular expression matching a file name,
9920 VAL is a coding system, a cons of coding systems, or a function symbol.
9921 If VAL is a coding system, it is used for both decoding and encoding
9922 the file contents.
9923 If VAL is a cons of coding systems, the car part is used for decoding,
9924 and the cdr part is used for encoding.
9925 If VAL is a function symbol, the function must return a coding system
9926 or a cons of coding systems which are used as above. The function is
9927 called with an argument that is a list of the arguments with which
9928 `find-operation-coding-system' was called. If the function can't decide
9929 a coding system, it can return `undecided' so that the normal
9930 code-detection is performed.
9931
9932 See also the function `find-operation-coding-system'
9933 and the variable `auto-coding-alist'. */);
9934 Vfile_coding_system_alist = Qnil;
9935
9936 DEFVAR_LISP ("process-coding-system-alist", &Vprocess_coding_system_alist,
9937 doc: /*
9938 Alist to decide a coding system to use for a process I/O operation.
9939 The format is ((PATTERN . VAL) ...),
9940 where PATTERN is a regular expression matching a program name,
9941 VAL is a coding system, a cons of coding systems, or a function symbol.
9942 If VAL is a coding system, it is used for both decoding what received
9943 from the program and encoding what sent to the program.
9944 If VAL is a cons of coding systems, the car part is used for decoding,
9945 and the cdr part is used for encoding.
9946 If VAL is a function symbol, the function must return a coding system
9947 or a cons of coding systems which are used as above.
9948
9949 See also the function `find-operation-coding-system'. */);
9950 Vprocess_coding_system_alist = Qnil;
9951
9952 DEFVAR_LISP ("network-coding-system-alist", &Vnetwork_coding_system_alist,
9953 doc: /*
9954 Alist to decide a coding system to use for a network I/O operation.
9955 The format is ((PATTERN . VAL) ...),
9956 where PATTERN is a regular expression matching a network service name
9957 or is a port number to connect to,
9958 VAL is a coding system, a cons of coding systems, or a function symbol.
9959 If VAL is a coding system, it is used for both decoding what received
9960 from the network stream and encoding what sent to the network stream.
9961 If VAL is a cons of coding systems, the car part is used for decoding,
9962 and the cdr part is used for encoding.
9963 If VAL is a function symbol, the function must return a coding system
9964 or a cons of coding systems which are used as above.
9965
9966 See also the function `find-operation-coding-system'. */);
9967 Vnetwork_coding_system_alist = Qnil;
9968
9969 DEFVAR_LISP ("locale-coding-system", &Vlocale_coding_system,
9970 doc: /* Coding system to use with system messages.
9971 Also used for decoding keyboard input on X Window system. */);
9972 Vlocale_coding_system = Qnil;
9973
9974 /* The eol mnemonics are reset in startup.el system-dependently. */
9975 DEFVAR_LISP ("eol-mnemonic-unix", &eol_mnemonic_unix,
9976 doc: /*
9977 *String displayed in mode line for UNIX-like (LF) end-of-line format. */);
9978 eol_mnemonic_unix = build_string (":");
9979
9980 DEFVAR_LISP ("eol-mnemonic-dos", &eol_mnemonic_dos,
9981 doc: /*
9982 *String displayed in mode line for DOS-like (CRLF) end-of-line format. */);
9983 eol_mnemonic_dos = build_string ("\\");
9984
9985 DEFVAR_LISP ("eol-mnemonic-mac", &eol_mnemonic_mac,
9986 doc: /*
9987 *String displayed in mode line for MAC-like (CR) end-of-line format. */);
9988 eol_mnemonic_mac = build_string ("/");
9989
9990 DEFVAR_LISP ("eol-mnemonic-undecided", &eol_mnemonic_undecided,
9991 doc: /*
9992 *String displayed in mode line when end-of-line format is not yet determined. */);
9993 eol_mnemonic_undecided = build_string (":");
9994
9995 DEFVAR_LISP ("enable-character-translation", &Venable_character_translation,
9996 doc: /*
9997 *Non-nil enables character translation while encoding and decoding. */);
9998 Venable_character_translation = Qt;
9999
10000 DEFVAR_LISP ("standard-translation-table-for-decode",
10001 &Vstandard_translation_table_for_decode,
10002 doc: /* Table for translating characters while decoding. */);
10003 Vstandard_translation_table_for_decode = Qnil;
10004
10005 DEFVAR_LISP ("standard-translation-table-for-encode",
10006 &Vstandard_translation_table_for_encode,
10007 doc: /* Table for translating characters while encoding. */);
10008 Vstandard_translation_table_for_encode = Qnil;
10009
10010 DEFVAR_LISP ("charset-revision-table", &Vcharset_revision_table,
10011 doc: /* Alist of charsets vs revision numbers.
10012 While encoding, if a charset (car part of an element) is found,
10013 designate it with the escape sequence identifying revision (cdr part
10014 of the element). */);
10015 Vcharset_revision_table = Qnil;
10016
10017 DEFVAR_LISP ("default-process-coding-system",
10018 &Vdefault_process_coding_system,
10019 doc: /* Cons of coding systems used for process I/O by default.
10020 The car part is used for decoding a process output,
10021 the cdr part is used for encoding a text to be sent to a process. */);
10022 Vdefault_process_coding_system = Qnil;
10023
10024 DEFVAR_LISP ("latin-extra-code-table", &Vlatin_extra_code_table,
10025 doc: /*
10026 Table of extra Latin codes in the range 128..159 (inclusive).
10027 This is a vector of length 256.
10028 If Nth element is non-nil, the existence of code N in a file
10029 \(or output of subprocess) doesn't prevent it to be detected as
10030 a coding system of ISO 2022 variant which has a flag
10031 `accept-latin-extra-code' t (e.g. iso-latin-1) on reading a file
10032 or reading output of a subprocess.
10033 Only 128th through 159th elements has a meaning. */);
10034 Vlatin_extra_code_table = Fmake_vector (make_number (256), Qnil);
10035
10036 DEFVAR_LISP ("select-safe-coding-system-function",
10037 &Vselect_safe_coding_system_function,
10038 doc: /*
10039 Function to call to select safe coding system for encoding a text.
10040
10041 If set, this function is called to force a user to select a proper
10042 coding system which can encode the text in the case that a default
10043 coding system used in each operation can't encode the text. The
10044 function should take care that the buffer is not modified while
10045 the coding system is being selected.
10046
10047 The default value is `select-safe-coding-system' (which see). */);
10048 Vselect_safe_coding_system_function = Qnil;
10049
10050 DEFVAR_BOOL ("coding-system-require-warning",
10051 &coding_system_require_warning,
10052 doc: /* Internal use only.
10053 If non-nil, on writing a file, `select-safe-coding-system-function' is
10054 called even if `coding-system-for-write' is non-nil. The command
10055 `universal-coding-system-argument' binds this variable to t temporarily. */);
10056 coding_system_require_warning = 0;
10057
10058
10059 DEFVAR_BOOL ("inhibit-iso-escape-detection",
10060 &inhibit_iso_escape_detection,
10061 doc: /*
10062 If non-nil, Emacs ignores ISO2022's escape sequence on code detection.
10063
10064 By default, on reading a file, Emacs tries to detect how the text is
10065 encoded. This code detection is sensitive to escape sequences. If
10066 the sequence is valid as ISO2022, the code is determined as one of
10067 the ISO2022 encodings, and the file is decoded by the corresponding
10068 coding system (e.g. `iso-2022-7bit').
10069
10070 However, there may be a case that you want to read escape sequences in
10071 a file as is. In such a case, you can set this variable to non-nil.
10072 Then, as the code detection ignores any escape sequences, no file is
10073 detected as encoded in some ISO2022 encoding. The result is that all
10074 escape sequences become visible in a buffer.
10075
10076 The default value is nil, and it is strongly recommended not to change
10077 it. That is because many Emacs Lisp source files that contain
10078 non-ASCII characters are encoded by the coding system `iso-2022-7bit'
10079 in Emacs's distribution, and they won't be decoded correctly on
10080 reading if you suppress escape sequence detection.
10081
10082 The other way to read escape sequences in a file without decoding is
10083 to explicitly specify some coding system that doesn't use ISO2022's
10084 escape sequence (e.g `latin-1') on reading by \\[universal-coding-system-argument]. */);
10085 inhibit_iso_escape_detection = 0;
10086
10087 DEFVAR_LISP ("translation-table-for-input", &Vtranslation_table_for_input,
10088 doc: /* Char table for translating self-inserting characters.
10089 This is applied to the result of input methods, not their input. See also
10090 `keyboard-translate-table'. */);
10091 Vtranslation_table_for_input = Qnil;
10092
10093 {
10094 Lisp_Object args[coding_arg_max];
10095 Lisp_Object plist[16];
10096 int i;
10097
10098 for (i = 0; i < coding_arg_max; i++)
10099 args[i] = Qnil;
10100
10101 plist[0] = intern (":name");
10102 plist[1] = args[coding_arg_name] = Qno_conversion;
10103 plist[2] = intern (":mnemonic");
10104 plist[3] = args[coding_arg_mnemonic] = make_number ('=');
10105 plist[4] = intern (":coding-type");
10106 plist[5] = args[coding_arg_coding_type] = Qraw_text;
10107 plist[6] = intern (":ascii-compatible-p");
10108 plist[7] = args[coding_arg_ascii_compatible_p] = Qt;
10109 plist[8] = intern (":default-char");
10110 plist[9] = args[coding_arg_default_char] = make_number (0);
10111 plist[10] = intern (":for-unibyte");
10112 plist[11] = args[coding_arg_for_unibyte] = Qt;
10113 plist[12] = intern (":docstring");
10114 plist[13] = build_string ("Do no conversion.\n\
10115 \n\
10116 When you visit a file with this coding, the file is read into a\n\
10117 unibyte buffer as is, thus each byte of a file is treated as a\n\
10118 character.");
10119 plist[14] = intern (":eol-type");
10120 plist[15] = args[coding_arg_eol_type] = Qunix;
10121 args[coding_arg_plist] = Flist (16, plist);
10122 Fdefine_coding_system_internal (coding_arg_max, args);
10123
10124 plist[1] = args[coding_arg_name] = Qundecided;
10125 plist[3] = args[coding_arg_mnemonic] = make_number ('-');
10126 plist[5] = args[coding_arg_coding_type] = Qundecided;
10127 /* This is already set.
10128 plist[7] = args[coding_arg_ascii_compatible_p] = Qt; */
10129 plist[8] = intern (":charset-list");
10130 plist[9] = args[coding_arg_charset_list] = Fcons (Qascii, Qnil);
10131 plist[11] = args[coding_arg_for_unibyte] = Qnil;
10132 plist[13] = build_string ("No conversion on encoding, automatic conversion on decoding.");
10133 plist[15] = args[coding_arg_eol_type] = Qnil;
10134 args[coding_arg_plist] = Flist (16, plist);
10135 Fdefine_coding_system_internal (coding_arg_max, args);
10136 }
10137
10138 setup_coding_system (Qno_conversion, &safe_terminal_coding);
10139
10140 {
10141 int i;
10142
10143 for (i = 0; i < coding_category_max; i++)
10144 Fset (AREF (Vcoding_category_table, i), Qno_conversion);
10145 }
10146 #if defined (MSDOS) || defined (WINDOWSNT)
10147 system_eol_type = Qdos;
10148 #else
10149 system_eol_type = Qunix;
10150 #endif
10151 staticpro (&system_eol_type);
10152 }
10153
10154 char *
10155 emacs_strerror (error_number)
10156 int error_number;
10157 {
10158 char *str;
10159
10160 synchronize_system_messages_locale ();
10161 str = strerror (error_number);
10162
10163 if (! NILP (Vlocale_coding_system))
10164 {
10165 Lisp_Object dec = code_convert_string_norecord (build_string (str),
10166 Vlocale_coding_system,
10167 0);
10168 str = (char *) SDATA (dec);
10169 }
10170
10171 return str;
10172 }
10173
10174 #endif /* emacs */
10175
10176 /* arch-tag: 3a3a2b01-5ff6-4071-9afe-f5b808d9229d
10177 (do not change this comment) */