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