* longlines.el (longlines-wrap-region): Avoid marking buffer as
[bpt/emacs.git] / src / coding.c
... / ...
CommitLineData
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
12This file is part of GNU Emacs.
13
14GNU Emacs is free software; you can redistribute it and/or modify
15it under the terms of the GNU General Public License as published by
16the Free Software Foundation; either version 2, or (at your option)
17any later version.
18
19GNU Emacs is distributed in the hope that it will be useful,
20but WITHOUT ANY WARRANTY; without even the implied warranty of
21MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22GNU General Public License for more details.
23
24You should have received a copy of the GNU General Public License
25along with GNU Emacs; see the file COPYING. If not, write to
26the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
27Boston, 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
50CODING 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
123END-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
135STRUCT 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
158static int
159detect_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
207static void
208decode_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
265static void
266encode_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
304Lisp_Object Vcoding_system_hash_table;
305
306Lisp_Object Qcoding_system, Qcoding_aliases, Qeol_type;
307Lisp_Object Qunix, Qdos;
308extern Lisp_Object Qmac; /* frame.c */
309Lisp_Object Qbuffer_file_coding_system;
310Lisp_Object Qpost_read_conversion, Qpre_write_conversion;
311Lisp_Object Qdefault_char;
312Lisp_Object Qno_conversion, Qundecided;
313Lisp_Object Qcharset, Qiso_2022, Qutf_8, Qutf_16, Qshift_jis, Qbig5;
314Lisp_Object Qbig, Qlittle;
315Lisp_Object Qcoding_system_history;
316Lisp_Object Qvalid_codes;
317Lisp_Object QCcategory, QCmnemonic, QCdefalut_char;
318Lisp_Object QCdecode_translation_table, QCencode_translation_table;
319Lisp_Object QCpost_read_conversion, QCpre_write_conversion;
320Lisp_Object QCascii_compatible_p;
321
322extern Lisp_Object Qinsert_file_contents, Qwrite_region;
323Lisp_Object Qcall_process, Qcall_process_region;
324Lisp_Object Qstart_process, Qopen_network_stream;
325Lisp_Object Qtarget_idx;
326
327Lisp_Object Qinsufficient_source, Qinconsistent_eol, Qinvalid_source;
328Lisp_Object Qinterrupted, Qinsufficient_memory;
329
330/* If a symbol has this property, evaluate the value to define the
331 symbol as a coding system. */
332static Lisp_Object Qcoding_system_define_form;
333
334int coding_system_require_warning;
335
336Lisp_Object Vselect_safe_coding_system_function;
337
338/* Mnemonic string for each format of end-of-line. */
339Lisp_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. */
342Lisp_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. */
348static Lisp_Object system_eol_type;
349
350#ifdef emacs
351
352Lisp_Object Vcoding_system_list, Vcoding_system_alist;
353
354Lisp_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. */
358Lisp_Object Qemacs_mule, Qraw_text;
359Lisp_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. */
364Lisp_Object Vcoding_system_for_read;
365/* Coding-system for writing files and sending data to process. */
366Lisp_Object Vcoding_system_for_write;
367/* Coding-system actually used in the latest I/O. */
368Lisp_Object Vlast_coding_system_used;
369/* Set to non-nil when an error is detected while code conversion. */
370Lisp_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). */
373Lisp_Object Vlatin_extra_code_table;
374
375/* Flag to inhibit code conversion of end-of-line format. */
376int inhibit_eol_conversion;
377
378/* Flag to inhibit ISO2022 escape sequence detection. */
379int inhibit_iso_escape_detection;
380
381/* Flag to make buffer-file-coding-system inherit from process-coding. */
382int inherit_process_coding_system;
383
384/* Coding system to be used to encode text for terminal display. */
385struct coding_system terminal_coding;
386
387/* Coding system to be used to encode text for terminal display when
388 terminal coding system is nil. */
389struct coding_system safe_terminal_coding;
390
391/* Coding system of what is sent from terminal keyboard. */
392struct coding_system keyboard_coding;
393
394Lisp_Object Vfile_coding_system_alist;
395Lisp_Object Vprocess_coding_system_alist;
396Lisp_Object Vnetwork_coding_system_alist;
397
398Lisp_Object Vlocale_coding_system;
399
400#endif /* emacs */
401
402/* Flag to tell if we look up translation table on character code
403 conversion. */
404Lisp_Object Venable_character_translation;
405/* Standard translation table to look up on decoding (reading). */
406Lisp_Object Vstandard_translation_table_for_decode;
407/* Standard translation table to look up on encoding (writing). */
408Lisp_Object Vstandard_translation_table_for_encode;
409
410Lisp_Object Qtranslation_table;
411Lisp_Object Qtranslation_table_id;
412Lisp_Object Qtranslation_table_for_decode;
413Lisp_Object Qtranslation_table_for_encode;
414
415/* Alist of charsets vs revision number. */
416static Lisp_Object Vcharset_revision_table;
417
418/* Default coding systems used for process I/O. */
419Lisp_Object Vdefault_process_coding_system;
420
421/* Char table for translating Quail and self-inserting input. */
422Lisp_Object Vtranslation_table_for_input;
423
424/* Two special coding systems. */
425Lisp_Object Vsjis_coding_system;
426Lisp_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. */
469enum 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
575enum 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. */
670static Lisp_Object Vcoding_category_list;
671
672/* Table of coding categories (Lisp symbols). This variable is for
673 internal use oly. */
674static Lisp_Object Vcoding_category_table;
675
676/* Table of coding-categories ordered by priority. */
677static 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. */
681static 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. */
837static void record_conversion_result P_ ((struct coding_system *coding,
838 enum coding_result_code result));
839static int detect_coding_utf_8 P_ ((struct coding_system *,
840 struct coding_detection_info *info));
841static void decode_coding_utf_8 P_ ((struct coding_system *));
842static int encode_coding_utf_8 P_ ((struct coding_system *));
843
844static int detect_coding_utf_16 P_ ((struct coding_system *,
845 struct coding_detection_info *info));
846static void decode_coding_utf_16 P_ ((struct coding_system *));
847static int encode_coding_utf_16 P_ ((struct coding_system *));
848
849static int detect_coding_iso_2022 P_ ((struct coding_system *,
850 struct coding_detection_info *info));
851static void decode_coding_iso_2022 P_ ((struct coding_system *));
852static int encode_coding_iso_2022 P_ ((struct coding_system *));
853
854static int detect_coding_emacs_mule P_ ((struct coding_system *,
855 struct coding_detection_info *info));
856static void decode_coding_emacs_mule P_ ((struct coding_system *));
857static int encode_coding_emacs_mule P_ ((struct coding_system *));
858
859static int detect_coding_sjis P_ ((struct coding_system *,
860 struct coding_detection_info *info));
861static void decode_coding_sjis P_ ((struct coding_system *));
862static int encode_coding_sjis P_ ((struct coding_system *));
863
864static int detect_coding_big5 P_ ((struct coding_system *,
865 struct coding_detection_info *info));
866static void decode_coding_big5 P_ ((struct coding_system *));
867static int encode_coding_big5 P_ ((struct coding_system *));
868
869static int detect_coding_ccl P_ ((struct coding_system *,
870 struct coding_detection_info *info));
871static void decode_coding_ccl P_ ((struct coding_system *));
872static int encode_coding_ccl P_ ((struct coding_system *));
873
874static void decode_coding_raw_text P_ ((struct coding_system *));
875static int encode_coding_raw_text P_ ((struct coding_system *));
876
877static void coding_set_source P_ ((struct coding_system *));
878static void coding_set_destination P_ ((struct coding_system *));
879static void coding_alloc_by_realloc P_ ((struct coding_system *, EMACS_INT));
880static void coding_alloc_by_making_gap P_ ((struct coding_system *,
881 EMACS_INT, EMACS_INT));
882static unsigned char *alloc_destination P_ ((struct coding_system *,
883 EMACS_INT, unsigned char *));
884static void setup_iso_safe_charsets P_ ((Lisp_Object));
885static unsigned char *encode_designation_at_bol P_ ((struct coding_system *,
886 int *, int *,
887 unsigned char *));
888static int detect_eol P_ ((const unsigned char *,
889 EMACS_INT, enum coding_category));
890static Lisp_Object adjust_coding_eol_type P_ ((struct coding_system *, int));
891static void decode_eol P_ ((struct coding_system *));
892static Lisp_Object get_translation_table P_ ((Lisp_Object, int, int *));
893static Lisp_Object get_translation P_ ((Lisp_Object, int *, int *,
894 int, int *, int *));
895static int produce_chars P_ ((struct coding_system *, Lisp_Object, int));
896static INLINE void produce_composition P_ ((struct coding_system *, int *,
897 EMACS_INT));
898static INLINE void produce_charset P_ ((struct coding_system *, int *,
899 EMACS_INT));
900static void produce_annotation P_ ((struct coding_system *, EMACS_INT));
901static int decode_coding P_ ((struct coding_system *));
902static INLINE int *handle_composition_annotation P_ ((EMACS_INT, EMACS_INT,
903 struct coding_system *,
904 int *, EMACS_INT *));
905static INLINE int *handle_charset_annotation P_ ((EMACS_INT, EMACS_INT,
906 struct coding_system *,
907 int *, EMACS_INT *));
908static void consume_chars P_ ((struct coding_system *, Lisp_Object, int));
909static int encode_coding P_ ((struct coding_system *));
910static Lisp_Object make_conversion_work_buffer P_ ((int));
911static Lisp_Object code_conversion_restore P_ ((Lisp_Object));
912static INLINE int char_encodable_p P_ ((int, Lisp_Object));
913static Lisp_Object make_subsidiaries P_ ((Lisp_Object));
914
915static void
916record_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
973static void
974coding_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
996static void
997coding_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
1026static void
1027coding_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
1036static void
1037coding_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
1064static unsigned char *
1065alloc_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
1147static int
1148detect_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
1218static void
1219decode_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
1329static int
1330encode_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
1402static int
1403detect_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
1448static void
1449decode_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
1555static int
1556encode_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
1690char emacs_mule_bytes[256];
1691
1692int
1693emacs_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
1809static int
1810detect_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
2061static void
2062decode_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
2179static int
2180encode_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
2461enum 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
2471static void
2472setup_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
2540static int
2541detect_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 found |= CATEGORY_MASK_ISO_ELSE;
2665 break;
2666
2667 case ISO_CODE_CSI:
2668 /* Control sequence introducer. */
2669 single_shifting = 0;
2670 rejected |= CATEGORY_MASK_ISO_7BIT | CATEGORY_MASK_ISO_7_ELSE;
2671 found |= CATEGORY_MASK_ISO_8_ELSE;
2672 goto check_extra_latin;
2673
2674 case ISO_CODE_SS2:
2675 case ISO_CODE_SS3:
2676 /* Single shift. */
2677 if (inhibit_iso_escape_detection)
2678 break;
2679 single_shifting = 0;
2680 rejected |= CATEGORY_MASK_ISO_7BIT;
2681 if (CODING_ISO_FLAGS (&coding_categories[coding_category_iso_8_1])
2682 & CODING_ISO_FLAG_SINGLE_SHIFT)
2683 found |= CATEGORY_MASK_ISO_8_1, single_shifting = 1;
2684 if (CODING_ISO_FLAGS (&coding_categories[coding_category_iso_8_2])
2685 & CODING_ISO_FLAG_SINGLE_SHIFT)
2686 found |= CATEGORY_MASK_ISO_8_2, single_shifting = 1;
2687 if (single_shifting)
2688 break;
2689 goto check_extra_latin;
2690
2691 default:
2692 if (c < 0)
2693 continue;
2694 if (c < 0x80)
2695 {
2696 single_shifting = 0;
2697 break;
2698 }
2699 if (c >= 0xA0)
2700 {
2701 rejected |= CATEGORY_MASK_ISO_7BIT | CATEGORY_MASK_ISO_7_ELSE;
2702 found |= CATEGORY_MASK_ISO_8_1;
2703 /* Check the length of succeeding codes of the range
2704 0xA0..0FF. If the byte length is even, we include
2705 CATEGORY_MASK_ISO_8_2 in `found'. We can check this
2706 only when we are not single shifting. */
2707 if (! single_shifting
2708 && ! (rejected & CATEGORY_MASK_ISO_8_2))
2709 {
2710 int i = 1;
2711 while (src < src_end)
2712 {
2713 ONE_MORE_BYTE (c);
2714 if (c < 0xA0)
2715 break;
2716 i++;
2717 }
2718
2719 if (i & 1 && src < src_end)
2720 rejected |= CATEGORY_MASK_ISO_8_2;
2721 else
2722 found |= CATEGORY_MASK_ISO_8_2;
2723 }
2724 break;
2725 }
2726 check_extra_latin:
2727 single_shifting = 0;
2728 if (! VECTORP (Vlatin_extra_code_table)
2729 || NILP (XVECTOR (Vlatin_extra_code_table)->contents[c]))
2730 {
2731 rejected = CATEGORY_MASK_ISO;
2732 break;
2733 }
2734 if (CODING_ISO_FLAGS (&coding_categories[coding_category_iso_8_1])
2735 & CODING_ISO_FLAG_LATIN_EXTRA)
2736 found |= CATEGORY_MASK_ISO_8_1;
2737 else
2738 rejected |= CATEGORY_MASK_ISO_8_1;
2739 rejected |= CATEGORY_MASK_ISO_8_2;
2740 }
2741 }
2742 detect_info->rejected |= CATEGORY_MASK_ISO;
2743 return 0;
2744
2745 no_more_source:
2746 detect_info->rejected |= rejected;
2747 detect_info->found |= (found & ~rejected);
2748 return 1;
2749}
2750
2751
2752/* Set designation state into CODING. Set CHARS_96 to -1 if the
2753 escape sequence should be kept. */
2754#define DECODE_DESIGNATION(reg, dim, chars_96, final) \
2755 do { \
2756 int id, prev; \
2757 \
2758 if (final < '0' || final >= 128 \
2759 || ((id = ISO_CHARSET_TABLE (dim, chars_96, final)) < 0) \
2760 || !SAFE_CHARSET_P (coding, id)) \
2761 { \
2762 CODING_ISO_DESIGNATION (coding, reg) = -2; \
2763 chars_96 = -1; \
2764 break; \
2765 } \
2766 prev = CODING_ISO_DESIGNATION (coding, reg); \
2767 if (id == charset_jisx0201_roman) \
2768 { \
2769 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \
2770 id = charset_ascii; \
2771 } \
2772 else if (id == charset_jisx0208_1978) \
2773 { \
2774 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \
2775 id = charset_jisx0208; \
2776 } \
2777 CODING_ISO_DESIGNATION (coding, reg) = id; \
2778 /* If there was an invalid designation to REG previously, and this \
2779 designation is ASCII to REG, we should keep this designation \
2780 sequence. */ \
2781 if (prev == -2 && id == charset_ascii) \
2782 chars_96 = -1; \
2783 } while (0)
2784
2785
2786#define MAYBE_FINISH_COMPOSITION() \
2787 do { \
2788 int i; \
2789 if (composition_state == COMPOSING_NO) \
2790 break; \
2791 /* It is assured that we have enough room for producing \
2792 characters stored in the table `components'. */ \
2793 if (charbuf + component_idx > charbuf_end) \
2794 goto no_more_source; \
2795 composition_state = COMPOSING_NO; \
2796 if (method == COMPOSITION_RELATIVE \
2797 || method == COMPOSITION_WITH_ALTCHARS) \
2798 { \
2799 for (i = 0; i < component_idx; i++) \
2800 *charbuf++ = components[i]; \
2801 char_offset += component_idx; \
2802 } \
2803 else \
2804 { \
2805 for (i = 0; i < component_idx; i += 2) \
2806 *charbuf++ = components[i]; \
2807 char_offset += (component_idx / 2) + 1; \
2808 } \
2809 } while (0)
2810
2811
2812/* Handle composition start sequence ESC 0, ESC 2, ESC 3, or ESC 4.
2813 ESC 0 : relative composition : ESC 0 CHAR ... ESC 1
2814 ESC 2 : rulebase composition : ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1
2815 ESC 3 : altchar composition : ESC 3 CHAR ... ESC 0 CHAR ... ESC 1
2816 ESC 4 : alt&rule composition : ESC 4 CHAR RULE ... CHAR ESC 0 CHAR ... ESC 1
2817 */
2818
2819#define DECODE_COMPOSITION_START(c1) \
2820 do { \
2821 if (c1 == '0' \
2822 && composition_state == COMPOSING_COMPONENT_RULE) \
2823 { \
2824 component_len = component_idx; \
2825 composition_state = COMPOSING_CHAR; \
2826 } \
2827 else \
2828 { \
2829 const unsigned char *p; \
2830 \
2831 MAYBE_FINISH_COMPOSITION (); \
2832 if (charbuf + MAX_COMPOSITION_COMPONENTS > charbuf_end) \
2833 goto no_more_source; \
2834 for (p = src; p < src_end - 1; p++) \
2835 if (*p == ISO_CODE_ESC && p[1] == '1') \
2836 break; \
2837 if (p == src_end - 1) \
2838 { \
2839 /* The current composition doesn't end in the current \
2840 source. */ \
2841 record_conversion_result \
2842 (coding, CODING_RESULT_INSUFFICIENT_SRC); \
2843 goto no_more_source; \
2844 } \
2845 \
2846 /* This is surely the start of a composition. */ \
2847 method = (c1 == '0' ? COMPOSITION_RELATIVE \
2848 : c1 == '2' ? COMPOSITION_WITH_RULE \
2849 : c1 == '3' ? COMPOSITION_WITH_ALTCHARS \
2850 : COMPOSITION_WITH_RULE_ALTCHARS); \
2851 composition_state = (c1 <= '2' ? COMPOSING_CHAR \
2852 : COMPOSING_COMPONENT_CHAR); \
2853 component_idx = component_len = 0; \
2854 } \
2855 } while (0)
2856
2857
2858/* Handle compositoin end sequence ESC 1. */
2859
2860#define DECODE_COMPOSITION_END() \
2861 do { \
2862 int nchars = (component_len > 0 ? component_idx - component_len \
2863 : method == COMPOSITION_RELATIVE ? component_idx \
2864 : (component_idx + 1) / 2); \
2865 int i; \
2866 int *saved_charbuf = charbuf; \
2867 \
2868 ADD_COMPOSITION_DATA (charbuf, nchars, method); \
2869 if (method != COMPOSITION_RELATIVE) \
2870 { \
2871 if (component_len == 0) \
2872 for (i = 0; i < component_idx; i++) \
2873 *charbuf++ = components[i]; \
2874 else \
2875 for (i = 0; i < component_len; i++) \
2876 *charbuf++ = components[i]; \
2877 *saved_charbuf = saved_charbuf - charbuf; \
2878 } \
2879 if (method == COMPOSITION_WITH_RULE) \
2880 for (i = 0; i < component_idx; i += 2, char_offset++) \
2881 *charbuf++ = components[i]; \
2882 else \
2883 for (i = component_len; i < component_idx; i++, char_offset++) \
2884 *charbuf++ = components[i]; \
2885 coding->annotated = 1; \
2886 composition_state = COMPOSING_NO; \
2887 } while (0)
2888
2889
2890/* Decode a composition rule from the byte C1 (and maybe one more byte
2891 from SRC) and store one encoded composition rule in
2892 coding->cmp_data. */
2893
2894#define DECODE_COMPOSITION_RULE(c1) \
2895 do { \
2896 (c1) -= 32; \
2897 if (c1 < 81) /* old format (before ver.21) */ \
2898 { \
2899 int gref = (c1) / 9; \
2900 int nref = (c1) % 9; \
2901 if (gref == 4) gref = 10; \
2902 if (nref == 4) nref = 10; \
2903 c1 = COMPOSITION_ENCODE_RULE (gref, nref); \
2904 } \
2905 else if (c1 < 93) /* new format (after ver.21) */ \
2906 { \
2907 ONE_MORE_BYTE (c2); \
2908 c1 = COMPOSITION_ENCODE_RULE (c1 - 81, c2 - 32); \
2909 } \
2910 else \
2911 c1 = 0; \
2912 } while (0)
2913
2914
2915/* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
2916
2917static void
2918decode_coding_iso_2022 (coding)
2919 struct coding_system *coding;
2920{
2921 const unsigned char *src = coding->source + coding->consumed;
2922 const unsigned char *src_end = coding->source + coding->src_bytes;
2923 const unsigned char *src_base;
2924 int *charbuf = coding->charbuf + coding->charbuf_used;
2925 int *charbuf_end
2926 = coding->charbuf + coding->charbuf_size - 4 - MAX_ANNOTATION_LENGTH;
2927 int consumed_chars = 0, consumed_chars_base;
2928 int multibytep = coding->src_multibyte;
2929 /* Charsets invoked to graphic plane 0 and 1 respectively. */
2930 int charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0);
2931 int charset_id_1 = CODING_ISO_INVOKED_CHARSET (coding, 1);
2932 int charset_id_2, charset_id_3;
2933 struct charset *charset;
2934 int c;
2935 /* For handling composition sequence. */
2936#define COMPOSING_NO 0
2937#define COMPOSING_CHAR 1
2938#define COMPOSING_RULE 2
2939#define COMPOSING_COMPONENT_CHAR 3
2940#define COMPOSING_COMPONENT_RULE 4
2941
2942 int composition_state = COMPOSING_NO;
2943 enum composition_method method;
2944 int components[MAX_COMPOSITION_COMPONENTS * 2 + 1];
2945 int component_idx;
2946 int component_len;
2947 Lisp_Object attrs, charset_list;
2948 int char_offset = coding->produced_char;
2949 int last_offset = char_offset;
2950 int last_id = charset_ascii;
2951
2952 CODING_GET_INFO (coding, attrs, charset_list);
2953 setup_iso_safe_charsets (attrs);
2954 /* Charset list may have been changed. */
2955 charset_list = CODING_ATTR_CHARSET_LIST (attrs);
2956 coding->safe_charsets = (char *) SDATA (CODING_ATTR_SAFE_CHARSETS(attrs));
2957
2958 while (1)
2959 {
2960 int c1, c2;
2961
2962 src_base = src;
2963 consumed_chars_base = consumed_chars;
2964
2965 if (charbuf >= charbuf_end)
2966 break;
2967
2968 ONE_MORE_BYTE (c1);
2969 if (c1 < 0)
2970 goto invalid_code;
2971
2972 /* We produce at most one character. */
2973 switch (iso_code_class [c1])
2974 {
2975 case ISO_0x20_or_0x7F:
2976 if (composition_state != COMPOSING_NO)
2977 {
2978 if (composition_state == COMPOSING_RULE
2979 || composition_state == COMPOSING_COMPONENT_RULE)
2980 {
2981 DECODE_COMPOSITION_RULE (c1);
2982 components[component_idx++] = c1;
2983 composition_state--;
2984 continue;
2985 }
2986 }
2987 if (charset_id_0 < 0
2988 || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_0)))
2989 /* This is SPACE or DEL. */
2990 charset = CHARSET_FROM_ID (charset_ascii);
2991 else
2992 charset = CHARSET_FROM_ID (charset_id_0);
2993 break;
2994
2995 case ISO_graphic_plane_0:
2996 if (composition_state != COMPOSING_NO)
2997 {
2998 if (composition_state == COMPOSING_RULE
2999 || composition_state == COMPOSING_COMPONENT_RULE)
3000 {
3001 DECODE_COMPOSITION_RULE (c1);
3002 components[component_idx++] = c1;
3003 composition_state--;
3004 continue;
3005 }
3006 }
3007 if (charset_id_0 < 0)
3008 charset = CHARSET_FROM_ID (charset_ascii);
3009 else
3010 charset = CHARSET_FROM_ID (charset_id_0);
3011 break;
3012
3013 case ISO_0xA0_or_0xFF:
3014 if (charset_id_1 < 0
3015 || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_1))
3016 || CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS)
3017 goto invalid_code;
3018 /* This is a graphic character, we fall down ... */
3019
3020 case ISO_graphic_plane_1:
3021 if (charset_id_1 < 0)
3022 goto invalid_code;
3023 charset = CHARSET_FROM_ID (charset_id_1);
3024 break;
3025
3026 case ISO_control_0:
3027 MAYBE_FINISH_COMPOSITION ();
3028 charset = CHARSET_FROM_ID (charset_ascii);
3029 break;
3030
3031 case ISO_control_1:
3032 MAYBE_FINISH_COMPOSITION ();
3033 goto invalid_code;
3034
3035 case ISO_shift_out:
3036 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LOCKING_SHIFT)
3037 || CODING_ISO_DESIGNATION (coding, 1) < 0)
3038 goto invalid_code;
3039 CODING_ISO_INVOCATION (coding, 0) = 1;
3040 charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0);
3041 continue;
3042
3043 case ISO_shift_in:
3044 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LOCKING_SHIFT))
3045 goto invalid_code;
3046 CODING_ISO_INVOCATION (coding, 0) = 0;
3047 charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0);
3048 continue;
3049
3050 case ISO_single_shift_2_7:
3051 case ISO_single_shift_2:
3052 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT))
3053 goto invalid_code;
3054 /* SS2 is handled as an escape sequence of ESC 'N' */
3055 c1 = 'N';
3056 goto label_escape_sequence;
3057
3058 case ISO_single_shift_3:
3059 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT))
3060 goto invalid_code;
3061 /* SS2 is handled as an escape sequence of ESC 'O' */
3062 c1 = 'O';
3063 goto label_escape_sequence;
3064
3065 case ISO_control_sequence_introducer:
3066 /* CSI is handled as an escape sequence of ESC '[' ... */
3067 c1 = '[';
3068 goto label_escape_sequence;
3069
3070 case ISO_escape:
3071 ONE_MORE_BYTE (c1);
3072 label_escape_sequence:
3073 /* Escape sequences handled here are invocation,
3074 designation, direction specification, and character
3075 composition specification. */
3076 switch (c1)
3077 {
3078 case '&': /* revision of following character set */
3079 ONE_MORE_BYTE (c1);
3080 if (!(c1 >= '@' && c1 <= '~'))
3081 goto invalid_code;
3082 ONE_MORE_BYTE (c1);
3083 if (c1 != ISO_CODE_ESC)
3084 goto invalid_code;
3085 ONE_MORE_BYTE (c1);
3086 goto label_escape_sequence;
3087
3088 case '$': /* designation of 2-byte character set */
3089 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DESIGNATION))
3090 goto invalid_code;
3091 {
3092 int reg, chars96;
3093
3094 ONE_MORE_BYTE (c1);
3095 if (c1 >= '@' && c1 <= 'B')
3096 { /* designation of JISX0208.1978, GB2312.1980,
3097 or JISX0208.1980 */
3098 reg = 0, chars96 = 0;
3099 }
3100 else if (c1 >= 0x28 && c1 <= 0x2B)
3101 { /* designation of DIMENSION2_CHARS94 character set */
3102 reg = c1 - 0x28, chars96 = 0;
3103 ONE_MORE_BYTE (c1);
3104 }
3105 else if (c1 >= 0x2C && c1 <= 0x2F)
3106 { /* designation of DIMENSION2_CHARS96 character set */
3107 reg = c1 - 0x2C, chars96 = 1;
3108 ONE_MORE_BYTE (c1);
3109 }
3110 else
3111 goto invalid_code;
3112 DECODE_DESIGNATION (reg, 2, chars96, c1);
3113 /* We must update these variables now. */
3114 if (reg == 0)
3115 charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0);
3116 else if (reg == 1)
3117 charset_id_1 = CODING_ISO_INVOKED_CHARSET (coding, 1);
3118 if (chars96 < 0)
3119 goto invalid_code;
3120 }
3121 continue;
3122
3123 case 'n': /* invocation of locking-shift-2 */
3124 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LOCKING_SHIFT)
3125 || CODING_ISO_DESIGNATION (coding, 2) < 0)
3126 goto invalid_code;
3127 CODING_ISO_INVOCATION (coding, 0) = 2;
3128 charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0);
3129 continue;
3130
3131 case 'o': /* invocation of locking-shift-3 */
3132 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LOCKING_SHIFT)
3133 || CODING_ISO_DESIGNATION (coding, 3) < 0)
3134 goto invalid_code;
3135 CODING_ISO_INVOCATION (coding, 0) = 3;
3136 charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0);
3137 continue;
3138
3139 case 'N': /* invocation of single-shift-2 */
3140 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT)
3141 || CODING_ISO_DESIGNATION (coding, 2) < 0)
3142 goto invalid_code;
3143 charset_id_2 = CODING_ISO_DESIGNATION (coding, 2);
3144 if (charset_id_2 < 0)
3145 charset = CHARSET_FROM_ID (charset_ascii);
3146 else
3147 charset = CHARSET_FROM_ID (charset_id_2);
3148 ONE_MORE_BYTE (c1);
3149 if (c1 < 0x20 || (c1 >= 0x80 && c1 < 0xA0))
3150 goto invalid_code;
3151 break;
3152
3153 case 'O': /* invocation of single-shift-3 */
3154 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT)
3155 || CODING_ISO_DESIGNATION (coding, 3) < 0)
3156 goto invalid_code;
3157 charset_id_3 = CODING_ISO_DESIGNATION (coding, 3);
3158 if (charset_id_3 < 0)
3159 charset = CHARSET_FROM_ID (charset_ascii);
3160 else
3161 charset = CHARSET_FROM_ID (charset_id_3);
3162 ONE_MORE_BYTE (c1);
3163 if (c1 < 0x20 || (c1 >= 0x80 && c1 < 0xA0))
3164 goto invalid_code;
3165 break;
3166
3167 case '0': case '2': case '3': case '4': /* start composition */
3168 if (! (coding->common_flags & CODING_ANNOTATE_COMPOSITION_MASK))
3169 goto invalid_code;
3170 DECODE_COMPOSITION_START (c1);
3171 continue;
3172
3173 case '1': /* end composition */
3174 if (composition_state == COMPOSING_NO)
3175 goto invalid_code;
3176 DECODE_COMPOSITION_END ();
3177 continue;
3178
3179 case '[': /* specification of direction */
3180 if (! CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DIRECTION)
3181 goto invalid_code;
3182 /* For the moment, nested direction is not supported.
3183 So, `coding->mode & CODING_MODE_DIRECTION' zero means
3184 left-to-right, and nozero means right-to-left. */
3185 ONE_MORE_BYTE (c1);
3186 switch (c1)
3187 {
3188 case ']': /* end of the current direction */
3189 coding->mode &= ~CODING_MODE_DIRECTION;
3190
3191 case '0': /* end of the current direction */
3192 case '1': /* start of left-to-right direction */
3193 ONE_MORE_BYTE (c1);
3194 if (c1 == ']')
3195 coding->mode &= ~CODING_MODE_DIRECTION;
3196 else
3197 goto invalid_code;
3198 break;
3199
3200 case '2': /* start of right-to-left direction */
3201 ONE_MORE_BYTE (c1);
3202 if (c1 == ']')
3203 coding->mode |= CODING_MODE_DIRECTION;
3204 else
3205 goto invalid_code;
3206 break;
3207
3208 default:
3209 goto invalid_code;
3210 }
3211 continue;
3212
3213 case '%':
3214 ONE_MORE_BYTE (c1);
3215 if (c1 == '/')
3216 {
3217 /* CTEXT extended segment:
3218 ESC % / [0-4] M L --ENCODING-NAME-- \002 --BYTES--
3219 We keep these bytes as is for the moment.
3220 They may be decoded by post-read-conversion. */
3221 int dim, M, L;
3222 int size;
3223
3224 ONE_MORE_BYTE (dim);
3225 ONE_MORE_BYTE (M);
3226 ONE_MORE_BYTE (L);
3227 size = ((M - 128) * 128) + (L - 128);
3228 if (charbuf + 8 + size > charbuf_end)
3229 goto break_loop;
3230 *charbuf++ = ISO_CODE_ESC;
3231 *charbuf++ = '%';
3232 *charbuf++ = '/';
3233 *charbuf++ = dim;
3234 *charbuf++ = BYTE8_TO_CHAR (M);
3235 *charbuf++ = BYTE8_TO_CHAR (L);
3236 while (size-- > 0)
3237 {
3238 ONE_MORE_BYTE (c1);
3239 *charbuf++ = ASCII_BYTE_P (c1) ? c1 : BYTE8_TO_CHAR (c1);
3240 }
3241 }
3242 else if (c1 == 'G')
3243 {
3244 /* XFree86 extension for embedding UTF-8 in CTEXT:
3245 ESC % G --UTF-8-BYTES-- ESC % @
3246 We keep these bytes as is for the moment.
3247 They may be decoded by post-read-conversion. */
3248 int *p = charbuf;
3249
3250 if (p + 6 > charbuf_end)
3251 goto break_loop;
3252 *p++ = ISO_CODE_ESC;
3253 *p++ = '%';
3254 *p++ = 'G';
3255 while (p < charbuf_end)
3256 {
3257 ONE_MORE_BYTE (c1);
3258 if (c1 == ISO_CODE_ESC
3259 && src + 1 < src_end
3260 && src[0] == '%'
3261 && src[1] == '@')
3262 {
3263 src += 2;
3264 break;
3265 }
3266 *p++ = ASCII_BYTE_P (c1) ? c1 : BYTE8_TO_CHAR (c1);
3267 }
3268 if (p + 3 > charbuf_end)
3269 goto break_loop;
3270 *p++ = ISO_CODE_ESC;
3271 *p++ = '%';
3272 *p++ = '@';
3273 charbuf = p;
3274 }
3275 else
3276 goto invalid_code;
3277 continue;
3278 break;
3279
3280 default:
3281 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DESIGNATION))
3282 goto invalid_code;
3283 {
3284 int reg, chars96;
3285
3286 if (c1 >= 0x28 && c1 <= 0x2B)
3287 { /* designation of DIMENSION1_CHARS94 character set */
3288 reg = c1 - 0x28, chars96 = 0;
3289 ONE_MORE_BYTE (c1);
3290 }
3291 else if (c1 >= 0x2C && c1 <= 0x2F)
3292 { /* designation of DIMENSION1_CHARS96 character set */
3293 reg = c1 - 0x2C, chars96 = 1;
3294 ONE_MORE_BYTE (c1);
3295 }
3296 else
3297 goto invalid_code;
3298 DECODE_DESIGNATION (reg, 1, chars96, c1);
3299 /* We must update these variables now. */
3300 if (reg == 0)
3301 charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0);
3302 else if (reg == 1)
3303 charset_id_1 = CODING_ISO_INVOKED_CHARSET (coding, 1);
3304 if (chars96 < 0)
3305 goto invalid_code;
3306 }
3307 continue;
3308 }
3309 }
3310
3311 if (charset->id != charset_ascii
3312 && last_id != charset->id)
3313 {
3314 if (last_id != charset_ascii)
3315 ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id);
3316 last_id = charset->id;
3317 last_offset = char_offset;
3318 }
3319
3320 /* Now we know CHARSET and 1st position code C1 of a character.
3321 Produce a decoded character while getting 2nd position code
3322 C2 if necessary. */
3323 c1 &= 0x7F;
3324 if (CHARSET_DIMENSION (charset) > 1)
3325 {
3326 ONE_MORE_BYTE (c2);
3327 if (c2 < 0x20 || (c2 >= 0x80 && c2 < 0xA0))
3328 /* C2 is not in a valid range. */
3329 goto invalid_code;
3330 c1 = (c1 << 8) | (c2 & 0x7F);
3331 if (CHARSET_DIMENSION (charset) > 2)
3332 {
3333 ONE_MORE_BYTE (c2);
3334 if (c2 < 0x20 || (c2 >= 0x80 && c2 < 0xA0))
3335 /* C2 is not in a valid range. */
3336 goto invalid_code;
3337 c1 = (c1 << 8) | (c2 & 0x7F);
3338 }
3339 }
3340
3341 CODING_DECODE_CHAR (coding, src, src_base, src_end, charset, c1, c);
3342 if (c < 0)
3343 {
3344 MAYBE_FINISH_COMPOSITION ();
3345 for (; src_base < src; src_base++, char_offset++)
3346 {
3347 if (ASCII_BYTE_P (*src_base))
3348 *charbuf++ = *src_base;
3349 else
3350 *charbuf++ = BYTE8_TO_CHAR (*src_base);
3351 }
3352 }
3353 else if (composition_state == COMPOSING_NO)
3354 {
3355 *charbuf++ = c;
3356 char_offset++;
3357 }
3358 else
3359 {
3360 components[component_idx++] = c;
3361 if (method == COMPOSITION_WITH_RULE
3362 || (method == COMPOSITION_WITH_RULE_ALTCHARS
3363 && composition_state == COMPOSING_COMPONENT_CHAR))
3364 composition_state++;
3365 }
3366 continue;
3367
3368 invalid_code:
3369 MAYBE_FINISH_COMPOSITION ();
3370 src = src_base;
3371 consumed_chars = consumed_chars_base;
3372 ONE_MORE_BYTE (c);
3373 *charbuf++ = c < 0 ? -c : ASCII_BYTE_P (c) ? c : BYTE8_TO_CHAR (c);
3374 char_offset++;
3375 coding->errors++;
3376 continue;
3377
3378 break_loop:
3379 break;
3380 }
3381
3382 no_more_source:
3383 if (last_id != charset_ascii)
3384 ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id);
3385 coding->consumed_char += consumed_chars_base;
3386 coding->consumed = src_base - coding->source;
3387 coding->charbuf_used = charbuf - coding->charbuf;
3388}
3389
3390
3391/* ISO2022 encoding stuff. */
3392
3393/*
3394 It is not enough to say just "ISO2022" on encoding, we have to
3395 specify more details. In Emacs, each coding system of ISO2022
3396 variant has the following specifications:
3397 1. Initial designation to G0 thru G3.
3398 2. Allows short-form designation?
3399 3. ASCII should be designated to G0 before control characters?
3400 4. ASCII should be designated to G0 at end of line?
3401 5. 7-bit environment or 8-bit environment?
3402 6. Use locking-shift?
3403 7. Use Single-shift?
3404 And the following two are only for Japanese:
3405 8. Use ASCII in place of JIS0201-1976-Roman?
3406 9. Use JISX0208-1983 in place of JISX0208-1978?
3407 These specifications are encoded in CODING_ISO_FLAGS (coding) as flag bits
3408 defined by macros CODING_ISO_FLAG_XXX. See `coding.h' for more
3409 details.
3410*/
3411
3412/* Produce codes (escape sequence) for designating CHARSET to graphic
3413 register REG at DST, and increment DST. If <final-char> of CHARSET is
3414 '@', 'A', or 'B' and the coding system CODING allows, produce
3415 designation sequence of short-form. */
3416
3417#define ENCODE_DESIGNATION(charset, reg, coding) \
3418 do { \
3419 unsigned char final_char = CHARSET_ISO_FINAL (charset); \
3420 char *intermediate_char_94 = "()*+"; \
3421 char *intermediate_char_96 = ",-./"; \
3422 int revision = -1; \
3423 int c; \
3424 \
3425 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_REVISION) \
3426 revision = CHARSET_ISO_REVISION (charset); \
3427 \
3428 if (revision >= 0) \
3429 { \
3430 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, '&'); \
3431 EMIT_ONE_BYTE ('@' + revision); \
3432 } \
3433 EMIT_ONE_ASCII_BYTE (ISO_CODE_ESC); \
3434 if (CHARSET_DIMENSION (charset) == 1) \
3435 { \
3436 if (! CHARSET_ISO_CHARS_96 (charset)) \
3437 c = intermediate_char_94[reg]; \
3438 else \
3439 c = intermediate_char_96[reg]; \
3440 EMIT_ONE_ASCII_BYTE (c); \
3441 } \
3442 else \
3443 { \
3444 EMIT_ONE_ASCII_BYTE ('$'); \
3445 if (! CHARSET_ISO_CHARS_96 (charset)) \
3446 { \
3447 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LONG_FORM \
3448 || reg != 0 \
3449 || final_char < '@' || final_char > 'B') \
3450 EMIT_ONE_ASCII_BYTE (intermediate_char_94[reg]); \
3451 } \
3452 else \
3453 EMIT_ONE_ASCII_BYTE (intermediate_char_96[reg]); \
3454 } \
3455 EMIT_ONE_ASCII_BYTE (final_char); \
3456 \
3457 CODING_ISO_DESIGNATION (coding, reg) = CHARSET_ID (charset); \
3458 } while (0)
3459
3460
3461/* The following two macros produce codes (control character or escape
3462 sequence) for ISO2022 single-shift functions (single-shift-2 and
3463 single-shift-3). */
3464
3465#define ENCODE_SINGLE_SHIFT_2 \
3466 do { \
3467 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
3468 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'N'); \
3469 else \
3470 EMIT_ONE_BYTE (ISO_CODE_SS2); \
3471 CODING_ISO_SINGLE_SHIFTING (coding) = 1; \
3472 } while (0)
3473
3474
3475#define ENCODE_SINGLE_SHIFT_3 \
3476 do { \
3477 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
3478 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'O'); \
3479 else \
3480 EMIT_ONE_BYTE (ISO_CODE_SS3); \
3481 CODING_ISO_SINGLE_SHIFTING (coding) = 1; \
3482 } while (0)
3483
3484
3485/* The following four macros produce codes (control character or
3486 escape sequence) for ISO2022 locking-shift functions (shift-in,
3487 shift-out, locking-shift-2, and locking-shift-3). */
3488
3489#define ENCODE_SHIFT_IN \
3490 do { \
3491 EMIT_ONE_ASCII_BYTE (ISO_CODE_SI); \
3492 CODING_ISO_INVOCATION (coding, 0) = 0; \
3493 } while (0)
3494
3495
3496#define ENCODE_SHIFT_OUT \
3497 do { \
3498 EMIT_ONE_ASCII_BYTE (ISO_CODE_SO); \
3499 CODING_ISO_INVOCATION (coding, 0) = 1; \
3500 } while (0)
3501
3502
3503#define ENCODE_LOCKING_SHIFT_2 \
3504 do { \
3505 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \
3506 CODING_ISO_INVOCATION (coding, 0) = 2; \
3507 } while (0)
3508
3509
3510#define ENCODE_LOCKING_SHIFT_3 \
3511 do { \
3512 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \
3513 CODING_ISO_INVOCATION (coding, 0) = 3; \
3514 } while (0)
3515
3516
3517/* Produce codes for a DIMENSION1 character whose character set is
3518 CHARSET and whose position-code is C1. Designation and invocation
3519 sequences are also produced in advance if necessary. */
3520
3521#define ENCODE_ISO_CHARACTER_DIMENSION1(charset, c1) \
3522 do { \
3523 int id = CHARSET_ID (charset); \
3524 \
3525 if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \
3526 && id == charset_ascii) \
3527 { \
3528 id = charset_jisx0201_roman; \
3529 charset = CHARSET_FROM_ID (id); \
3530 } \
3531 \
3532 if (CODING_ISO_SINGLE_SHIFTING (coding)) \
3533 { \
3534 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
3535 EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \
3536 else \
3537 EMIT_ONE_BYTE (c1 | 0x80); \
3538 CODING_ISO_SINGLE_SHIFTING (coding) = 0; \
3539 break; \
3540 } \
3541 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \
3542 { \
3543 EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \
3544 break; \
3545 } \
3546 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \
3547 { \
3548 EMIT_ONE_BYTE (c1 | 0x80); \
3549 break; \
3550 } \
3551 else \
3552 /* Since CHARSET is not yet invoked to any graphic planes, we \
3553 must invoke it, or, at first, designate it to some graphic \
3554 register. Then repeat the loop to actually produce the \
3555 character. */ \
3556 dst = encode_invocation_designation (charset, coding, dst, \
3557 &produced_chars); \
3558 } while (1)
3559
3560
3561/* Produce codes for a DIMENSION2 character whose character set is
3562 CHARSET and whose position-codes are C1 and C2. Designation and
3563 invocation codes are also produced in advance if necessary. */
3564
3565#define ENCODE_ISO_CHARACTER_DIMENSION2(charset, c1, c2) \
3566 do { \
3567 int id = CHARSET_ID (charset); \
3568 \
3569 if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \
3570 && id == charset_jisx0208) \
3571 { \
3572 id = charset_jisx0208_1978; \
3573 charset = CHARSET_FROM_ID (id); \
3574 } \
3575 \
3576 if (CODING_ISO_SINGLE_SHIFTING (coding)) \
3577 { \
3578 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
3579 EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \
3580 else \
3581 EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \
3582 CODING_ISO_SINGLE_SHIFTING (coding) = 0; \
3583 break; \
3584 } \
3585 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \
3586 { \
3587 EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \
3588 break; \
3589 } \
3590 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \
3591 { \
3592 EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \
3593 break; \
3594 } \
3595 else \
3596 /* Since CHARSET is not yet invoked to any graphic planes, we \
3597 must invoke it, or, at first, designate it to some graphic \
3598 register. Then repeat the loop to actually produce the \
3599 character. */ \
3600 dst = encode_invocation_designation (charset, coding, dst, \
3601 &produced_chars); \
3602 } while (1)
3603
3604
3605#define ENCODE_ISO_CHARACTER(charset, c) \
3606 do { \
3607 int code = ENCODE_CHAR ((charset),(c)); \
3608 \
3609 if (CHARSET_DIMENSION (charset) == 1) \
3610 ENCODE_ISO_CHARACTER_DIMENSION1 ((charset), code); \
3611 else \
3612 ENCODE_ISO_CHARACTER_DIMENSION2 ((charset), code >> 8, code & 0xFF); \
3613 } while (0)
3614
3615
3616/* Produce designation and invocation codes at a place pointed by DST
3617 to use CHARSET. The element `spec.iso_2022' of *CODING is updated.
3618 Return new DST. */
3619
3620unsigned char *
3621encode_invocation_designation (charset, coding, dst, p_nchars)
3622 struct charset *charset;
3623 struct coding_system *coding;
3624 unsigned char *dst;
3625 int *p_nchars;
3626{
3627 int multibytep = coding->dst_multibyte;
3628 int produced_chars = *p_nchars;
3629 int reg; /* graphic register number */
3630 int id = CHARSET_ID (charset);
3631
3632 /* At first, check designations. */
3633 for (reg = 0; reg < 4; reg++)
3634 if (id == CODING_ISO_DESIGNATION (coding, reg))
3635 break;
3636
3637 if (reg >= 4)
3638 {
3639 /* CHARSET is not yet designated to any graphic registers. */
3640 /* At first check the requested designation. */
3641 reg = CODING_ISO_REQUEST (coding, id);
3642 if (reg < 0)
3643 /* Since CHARSET requests no special designation, designate it
3644 to graphic register 0. */
3645 reg = 0;
3646
3647 ENCODE_DESIGNATION (charset, reg, coding);
3648 }
3649
3650 if (CODING_ISO_INVOCATION (coding, 0) != reg
3651 && CODING_ISO_INVOCATION (coding, 1) != reg)
3652 {
3653 /* Since the graphic register REG is not invoked to any graphic
3654 planes, invoke it to graphic plane 0. */
3655 switch (reg)
3656 {
3657 case 0: /* graphic register 0 */
3658 ENCODE_SHIFT_IN;
3659 break;
3660
3661 case 1: /* graphic register 1 */
3662 ENCODE_SHIFT_OUT;
3663 break;
3664
3665 case 2: /* graphic register 2 */
3666 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT)
3667 ENCODE_SINGLE_SHIFT_2;
3668 else
3669 ENCODE_LOCKING_SHIFT_2;
3670 break;
3671
3672 case 3: /* graphic register 3 */
3673 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT)
3674 ENCODE_SINGLE_SHIFT_3;
3675 else
3676 ENCODE_LOCKING_SHIFT_3;
3677 break;
3678 }
3679 }
3680
3681 *p_nchars = produced_chars;
3682 return dst;
3683}
3684
3685/* The following three macros produce codes for indicating direction
3686 of text. */
3687#define ENCODE_CONTROL_SEQUENCE_INTRODUCER \
3688 do { \
3689 if (CODING_ISO_FLAGS (coding) == CODING_ISO_FLAG_SEVEN_BITS) \
3690 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, '['); \
3691 else \
3692 EMIT_ONE_BYTE (ISO_CODE_CSI); \
3693 } while (0)
3694
3695
3696#define ENCODE_DIRECTION_R2L() \
3697 do { \
3698 ENCODE_CONTROL_SEQUENCE_INTRODUCER (dst); \
3699 EMIT_TWO_ASCII_BYTES ('2', ']'); \
3700 } while (0)
3701
3702
3703#define ENCODE_DIRECTION_L2R() \
3704 do { \
3705 ENCODE_CONTROL_SEQUENCE_INTRODUCER (dst); \
3706 EMIT_TWO_ASCII_BYTES ('0', ']'); \
3707 } while (0)
3708
3709
3710/* Produce codes for designation and invocation to reset the graphic
3711 planes and registers to initial state. */
3712#define ENCODE_RESET_PLANE_AND_REGISTER() \
3713 do { \
3714 int reg; \
3715 struct charset *charset; \
3716 \
3717 if (CODING_ISO_INVOCATION (coding, 0) != 0) \
3718 ENCODE_SHIFT_IN; \
3719 for (reg = 0; reg < 4; reg++) \
3720 if (CODING_ISO_INITIAL (coding, reg) >= 0 \
3721 && (CODING_ISO_DESIGNATION (coding, reg) \
3722 != CODING_ISO_INITIAL (coding, reg))) \
3723 { \
3724 charset = CHARSET_FROM_ID (CODING_ISO_INITIAL (coding, reg)); \
3725 ENCODE_DESIGNATION (charset, reg, coding); \
3726 } \
3727 } while (0)
3728
3729
3730/* Produce designation sequences of charsets in the line started from
3731 SRC to a place pointed by DST, and return updated DST.
3732
3733 If the current block ends before any end-of-line, we may fail to
3734 find all the necessary designations. */
3735
3736static unsigned char *
3737encode_designation_at_bol (coding, charbuf, charbuf_end, dst)
3738 struct coding_system *coding;
3739 int *charbuf, *charbuf_end;
3740 unsigned char *dst;
3741{
3742 struct charset *charset;
3743 /* Table of charsets to be designated to each graphic register. */
3744 int r[4];
3745 int c, found = 0, reg;
3746 int produced_chars = 0;
3747 int multibytep = coding->dst_multibyte;
3748 Lisp_Object attrs;
3749 Lisp_Object charset_list;
3750
3751 attrs = CODING_ID_ATTRS (coding->id);
3752 charset_list = CODING_ATTR_CHARSET_LIST (attrs);
3753 if (EQ (charset_list, Qiso_2022))
3754 charset_list = Viso_2022_charset_list;
3755
3756 for (reg = 0; reg < 4; reg++)
3757 r[reg] = -1;
3758
3759 while (found < 4)
3760 {
3761 int id;
3762
3763 c = *charbuf++;
3764 if (c == '\n')
3765 break;
3766 charset = char_charset (c, charset_list, NULL);
3767 id = CHARSET_ID (charset);
3768 reg = CODING_ISO_REQUEST (coding, id);
3769 if (reg >= 0 && r[reg] < 0)
3770 {
3771 found++;
3772 r[reg] = id;
3773 }
3774 }
3775
3776 if (found)
3777 {
3778 for (reg = 0; reg < 4; reg++)
3779 if (r[reg] >= 0
3780 && CODING_ISO_DESIGNATION (coding, reg) != r[reg])
3781 ENCODE_DESIGNATION (CHARSET_FROM_ID (r[reg]), reg, coding);
3782 }
3783
3784 return dst;
3785}
3786
3787/* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". */
3788
3789static int
3790encode_coding_iso_2022 (coding)
3791 struct coding_system *coding;
3792{
3793 int multibytep = coding->dst_multibyte;
3794 int *charbuf = coding->charbuf;
3795 int *charbuf_end = charbuf + coding->charbuf_used;
3796 unsigned char *dst = coding->destination + coding->produced;
3797 unsigned char *dst_end = coding->destination + coding->dst_bytes;
3798 int safe_room = 16;
3799 int bol_designation
3800 = (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DESIGNATE_AT_BOL
3801 && CODING_ISO_BOL (coding));
3802 int produced_chars = 0;
3803 Lisp_Object attrs, eol_type, charset_list;
3804 int ascii_compatible;
3805 int c;
3806 int preferred_charset_id = -1;
3807
3808 CODING_GET_INFO (coding, attrs, charset_list);
3809 eol_type = CODING_ID_EOL_TYPE (coding->id);
3810 if (VECTORP (eol_type))
3811 eol_type = Qunix;
3812
3813 setup_iso_safe_charsets (attrs);
3814 /* Charset list may have been changed. */
3815 charset_list = CODING_ATTR_CHARSET_LIST (attrs);
3816 coding->safe_charsets = (char *) SDATA (CODING_ATTR_SAFE_CHARSETS(attrs));
3817
3818 ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs));
3819
3820 while (charbuf < charbuf_end)
3821 {
3822 ASSURE_DESTINATION (safe_room);
3823
3824 if (bol_designation)
3825 {
3826 unsigned char *dst_prev = dst;
3827
3828 /* We have to produce designation sequences if any now. */
3829 dst = encode_designation_at_bol (coding, charbuf, charbuf_end, dst);
3830 bol_designation = 0;
3831 /* We are sure that designation sequences are all ASCII bytes. */
3832 produced_chars += dst - dst_prev;
3833 }
3834
3835 c = *charbuf++;
3836
3837 if (c < 0)
3838 {
3839 /* Handle an annotation. */
3840 switch (*charbuf)
3841 {
3842 case CODING_ANNOTATE_COMPOSITION_MASK:
3843 /* Not yet implemented. */
3844 break;
3845 case CODING_ANNOTATE_CHARSET_MASK:
3846 preferred_charset_id = charbuf[2];
3847 if (preferred_charset_id >= 0
3848 && NILP (Fmemq (make_number (preferred_charset_id),
3849 charset_list)))
3850 preferred_charset_id = -1;
3851 break;
3852 default:
3853 abort ();
3854 }
3855 charbuf += -c - 1;
3856 continue;
3857 }
3858
3859 /* Now encode the character C. */
3860 if (c < 0x20 || c == 0x7F)
3861 {
3862 if (c == '\n'
3863 || (c == '\r' && EQ (eol_type, Qmac)))
3864 {
3865 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_RESET_AT_EOL)
3866 ENCODE_RESET_PLANE_AND_REGISTER ();
3867 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_INIT_AT_BOL)
3868 {
3869 int i;
3870
3871 for (i = 0; i < 4; i++)
3872 CODING_ISO_DESIGNATION (coding, i)
3873 = CODING_ISO_INITIAL (coding, i);
3874 }
3875 bol_designation
3876 = CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DESIGNATE_AT_BOL;
3877 }
3878 else if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_RESET_AT_CNTL)
3879 ENCODE_RESET_PLANE_AND_REGISTER ();
3880 EMIT_ONE_ASCII_BYTE (c);
3881 }
3882 else if (ASCII_CHAR_P (c))
3883 {
3884 if (ascii_compatible)
3885 EMIT_ONE_ASCII_BYTE (c);
3886 else
3887 {
3888 struct charset *charset = CHARSET_FROM_ID (charset_ascii);
3889 ENCODE_ISO_CHARACTER (charset, c);
3890 }
3891 }
3892 else if (CHAR_BYTE8_P (c))
3893 {
3894 c = CHAR_TO_BYTE8 (c);
3895 EMIT_ONE_BYTE (c);
3896 }
3897 else
3898 {
3899 struct charset *charset;
3900
3901 if (preferred_charset_id >= 0)
3902 {
3903 charset = CHARSET_FROM_ID (preferred_charset_id);
3904 if (! CHAR_CHARSET_P (c, charset))
3905 charset = char_charset (c, charset_list, NULL);
3906 }
3907 else
3908 charset = char_charset (c, charset_list, NULL);
3909 if (!charset)
3910 {
3911 if (coding->mode & CODING_MODE_SAFE_ENCODING)
3912 {
3913 c = CODING_INHIBIT_CHARACTER_SUBSTITUTION;
3914 charset = CHARSET_FROM_ID (charset_ascii);
3915 }
3916 else
3917 {
3918 c = coding->default_char;
3919 charset = char_charset (c, charset_list, NULL);
3920 }
3921 }
3922 ENCODE_ISO_CHARACTER (charset, c);
3923 }
3924 }
3925
3926 if (coding->mode & CODING_MODE_LAST_BLOCK
3927 && CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_RESET_AT_EOL)
3928 {
3929 ASSURE_DESTINATION (safe_room);
3930 ENCODE_RESET_PLANE_AND_REGISTER ();
3931 }
3932 record_conversion_result (coding, CODING_RESULT_SUCCESS);
3933 CODING_ISO_BOL (coding) = bol_designation;
3934 coding->produced_char += produced_chars;
3935 coding->produced = dst - coding->destination;
3936 return 0;
3937}
3938
3939\f
3940/*** 8,9. SJIS and BIG5 handlers ***/
3941
3942/* Although SJIS and BIG5 are not ISO's coding system, they are used
3943 quite widely. So, for the moment, Emacs supports them in the bare
3944 C code. But, in the future, they may be supported only by CCL. */
3945
3946/* SJIS is a coding system encoding three character sets: ASCII, right
3947 half of JISX0201-Kana, and JISX0208. An ASCII character is encoded
3948 as is. A character of charset katakana-jisx0201 is encoded by
3949 "position-code + 0x80". A character of charset japanese-jisx0208
3950 is encoded in 2-byte but two position-codes are divided and shifted
3951 so that it fit in the range below.
3952
3953 --- CODE RANGE of SJIS ---
3954 (character set) (range)
3955 ASCII 0x00 .. 0x7F
3956 KATAKANA-JISX0201 0xA0 .. 0xDF
3957 JISX0208 (1st byte) 0x81 .. 0x9F and 0xE0 .. 0xEF
3958 (2nd byte) 0x40 .. 0x7E and 0x80 .. 0xFC
3959 -------------------------------
3960
3961*/
3962
3963/* BIG5 is a coding system encoding two character sets: ASCII and
3964 Big5. An ASCII character is encoded as is. Big5 is a two-byte
3965 character set and is encoded in two-byte.
3966
3967 --- CODE RANGE of BIG5 ---
3968 (character set) (range)
3969 ASCII 0x00 .. 0x7F
3970 Big5 (1st byte) 0xA1 .. 0xFE
3971 (2nd byte) 0x40 .. 0x7E and 0xA1 .. 0xFE
3972 --------------------------
3973
3974 */
3975
3976/* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
3977 Check if a text is encoded in SJIS. If it is, return
3978 CATEGORY_MASK_SJIS, else return 0. */
3979
3980static int
3981detect_coding_sjis (coding, detect_info)
3982 struct coding_system *coding;
3983 struct coding_detection_info *detect_info;
3984{
3985 const unsigned char *src = coding->source, *src_base;
3986 const unsigned char *src_end = coding->source + coding->src_bytes;
3987 int multibytep = coding->src_multibyte;
3988 int consumed_chars = 0;
3989 int found = 0;
3990 int c;
3991
3992 detect_info->checked |= CATEGORY_MASK_SJIS;
3993 /* A coding system of this category is always ASCII compatible. */
3994 src += coding->head_ascii;
3995
3996 while (1)
3997 {
3998 src_base = src;
3999 ONE_MORE_BYTE (c);
4000 if (c < 0x80)
4001 continue;
4002 if ((c >= 0x81 && c <= 0x9F) || (c >= 0xE0 && c <= 0xEF))
4003 {
4004 ONE_MORE_BYTE (c);
4005 if (c < 0x40 || c == 0x7F || c > 0xFC)
4006 break;
4007 found = CATEGORY_MASK_SJIS;
4008 }
4009 else if (c >= 0xA0 && c < 0xE0)
4010 found = CATEGORY_MASK_SJIS;
4011 else
4012 break;
4013 }
4014 detect_info->rejected |= CATEGORY_MASK_SJIS;
4015 return 0;
4016
4017 no_more_source:
4018 if (src_base < src && coding->mode & CODING_MODE_LAST_BLOCK)
4019 {
4020 detect_info->rejected |= CATEGORY_MASK_SJIS;
4021 return 0;
4022 }
4023 detect_info->found |= found;
4024 return 1;
4025}
4026
4027/* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4028 Check if a text is encoded in BIG5. If it is, return
4029 CATEGORY_MASK_BIG5, else return 0. */
4030
4031static int
4032detect_coding_big5 (coding, detect_info)
4033 struct coding_system *coding;
4034 struct coding_detection_info *detect_info;
4035{
4036 const unsigned char *src = coding->source, *src_base;
4037 const unsigned char *src_end = coding->source + coding->src_bytes;
4038 int multibytep = coding->src_multibyte;
4039 int consumed_chars = 0;
4040 int found = 0;
4041 int c;
4042
4043 detect_info->checked |= CATEGORY_MASK_BIG5;
4044 /* A coding system of this category is always ASCII compatible. */
4045 src += coding->head_ascii;
4046
4047 while (1)
4048 {
4049 src_base = src;
4050 ONE_MORE_BYTE (c);
4051 if (c < 0x80)
4052 continue;
4053 if (c >= 0xA1)
4054 {
4055 ONE_MORE_BYTE (c);
4056 if (c < 0x40 || (c >= 0x7F && c <= 0xA0))
4057 return 0;
4058 found = CATEGORY_MASK_BIG5;
4059 }
4060 else
4061 break;
4062 }
4063 detect_info->rejected |= CATEGORY_MASK_BIG5;
4064 return 0;
4065
4066 no_more_source:
4067 if (src_base < src && coding->mode & CODING_MODE_LAST_BLOCK)
4068 {
4069 detect_info->rejected |= CATEGORY_MASK_BIG5;
4070 return 0;
4071 }
4072 detect_info->found |= found;
4073 return 1;
4074}
4075
4076/* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions".
4077 If SJIS_P is 1, decode SJIS text, else decode BIG5 test. */
4078
4079static void
4080decode_coding_sjis (coding)
4081 struct coding_system *coding;
4082{
4083 const unsigned char *src = coding->source + coding->consumed;
4084 const unsigned char *src_end = coding->source + coding->src_bytes;
4085 const unsigned char *src_base;
4086 int *charbuf = coding->charbuf + coding->charbuf_used;
4087 int *charbuf_end
4088 = coding->charbuf + coding->charbuf_size - MAX_ANNOTATION_LENGTH;
4089 int consumed_chars = 0, consumed_chars_base;
4090 int multibytep = coding->src_multibyte;
4091 struct charset *charset_roman, *charset_kanji, *charset_kana;
4092 struct charset *charset_kanji2;
4093 Lisp_Object attrs, charset_list, val;
4094 int char_offset = coding->produced_char;
4095 int last_offset = char_offset;
4096 int last_id = charset_ascii;
4097
4098 CODING_GET_INFO (coding, attrs, charset_list);
4099
4100 val = charset_list;
4101 charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
4102 charset_kana = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
4103 charset_kanji = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
4104 charset_kanji2 = NILP (val) ? NULL : CHARSET_FROM_ID (XINT (XCAR (val)));
4105
4106 while (1)
4107 {
4108 int c, c1;
4109 struct charset *charset;
4110
4111 src_base = src;
4112 consumed_chars_base = consumed_chars;
4113
4114 if (charbuf >= charbuf_end)
4115 break;
4116
4117 ONE_MORE_BYTE (c);
4118 if (c < 0)
4119 goto invalid_code;
4120 if (c < 0x80)
4121 charset = charset_roman;
4122 else if (c == 0x80 || c == 0xA0)
4123 goto invalid_code;
4124 else if (c >= 0xA1 && c <= 0xDF)
4125 {
4126 /* SJIS -> JISX0201-Kana */
4127 c &= 0x7F;
4128 charset = charset_kana;
4129 }
4130 else if (c <= 0xEF)
4131 {
4132 /* SJIS -> JISX0208 */
4133 ONE_MORE_BYTE (c1);
4134 if (c1 < 0x40 || c1 == 0x7F || c1 > 0xFC)
4135 goto invalid_code;
4136 c = (c << 8) | c1;
4137 SJIS_TO_JIS (c);
4138 charset = charset_kanji;
4139 }
4140 else if (c <= 0xFC && charset_kanji2)
4141 {
4142 /* SJIS -> JISX0213-2 */
4143 ONE_MORE_BYTE (c1);
4144 if (c1 < 0x40 || c1 == 0x7F || c1 > 0xFC)
4145 goto invalid_code;
4146 c = (c << 8) | c1;
4147 SJIS_TO_JIS2 (c);
4148 charset = charset_kanji2;
4149 }
4150 else
4151 goto invalid_code;
4152 if (charset->id != charset_ascii
4153 && last_id != charset->id)
4154 {
4155 if (last_id != charset_ascii)
4156 ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id);
4157 last_id = charset->id;
4158 last_offset = char_offset;
4159 }
4160 CODING_DECODE_CHAR (coding, src, src_base, src_end, charset, c, c);
4161 *charbuf++ = c;
4162 char_offset++;
4163 continue;
4164
4165 invalid_code:
4166 src = src_base;
4167 consumed_chars = consumed_chars_base;
4168 ONE_MORE_BYTE (c);
4169 *charbuf++ = c < 0 ? -c : BYTE8_TO_CHAR (c);
4170 char_offset++;
4171 coding->errors++;
4172 }
4173
4174 no_more_source:
4175 if (last_id != charset_ascii)
4176 ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id);
4177 coding->consumed_char += consumed_chars_base;
4178 coding->consumed = src_base - coding->source;
4179 coding->charbuf_used = charbuf - coding->charbuf;
4180}
4181
4182static void
4183decode_coding_big5 (coding)
4184 struct coding_system *coding;
4185{
4186 const unsigned char *src = coding->source + coding->consumed;
4187 const unsigned char *src_end = coding->source + coding->src_bytes;
4188 const unsigned char *src_base;
4189 int *charbuf = coding->charbuf + coding->charbuf_used;
4190 int *charbuf_end
4191 = coding->charbuf + coding->charbuf_size - MAX_ANNOTATION_LENGTH;
4192 int consumed_chars = 0, consumed_chars_base;
4193 int multibytep = coding->src_multibyte;
4194 struct charset *charset_roman, *charset_big5;
4195 Lisp_Object attrs, charset_list, val;
4196 int char_offset = coding->produced_char;
4197 int last_offset = char_offset;
4198 int last_id = charset_ascii;
4199
4200 CODING_GET_INFO (coding, attrs, charset_list);
4201 val = charset_list;
4202 charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
4203 charset_big5 = CHARSET_FROM_ID (XINT (XCAR (val)));
4204
4205 while (1)
4206 {
4207 int c, c1;
4208 struct charset *charset;
4209
4210 src_base = src;
4211 consumed_chars_base = consumed_chars;
4212
4213 if (charbuf >= charbuf_end)
4214 break;
4215
4216 ONE_MORE_BYTE (c);
4217
4218 if (c < 0)
4219 goto invalid_code;
4220 if (c < 0x80)
4221 charset = charset_roman;
4222 else
4223 {
4224 /* BIG5 -> Big5 */
4225 if (c < 0xA1 || c > 0xFE)
4226 goto invalid_code;
4227 ONE_MORE_BYTE (c1);
4228 if (c1 < 0x40 || (c1 > 0x7E && c1 < 0xA1) || c1 > 0xFE)
4229 goto invalid_code;
4230 c = c << 8 | c1;
4231 charset = charset_big5;
4232 }
4233 if (charset->id != charset_ascii
4234 && last_id != charset->id)
4235 {
4236 if (last_id != charset_ascii)
4237 ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id);
4238 last_id = charset->id;
4239 last_offset = char_offset;
4240 }
4241 CODING_DECODE_CHAR (coding, src, src_base, src_end, charset, c, c);
4242 *charbuf++ = c;
4243 char_offset++;
4244 continue;
4245
4246 invalid_code:
4247 src = src_base;
4248 consumed_chars = consumed_chars_base;
4249 ONE_MORE_BYTE (c);
4250 *charbuf++ = c < 0 ? -c : BYTE8_TO_CHAR (c);
4251 char_offset++;
4252 coding->errors++;
4253 }
4254
4255 no_more_source:
4256 if (last_id != charset_ascii)
4257 ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id);
4258 coding->consumed_char += consumed_chars_base;
4259 coding->consumed = src_base - coding->source;
4260 coding->charbuf_used = charbuf - coding->charbuf;
4261}
4262
4263/* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions".
4264 This function can encode charsets `ascii', `katakana-jisx0201',
4265 `japanese-jisx0208', `chinese-big5-1', and `chinese-big5-2'. We
4266 are sure that all these charsets are registered as official charset
4267 (i.e. do not have extended leading-codes). Characters of other
4268 charsets are produced without any encoding. If SJIS_P is 1, encode
4269 SJIS text, else encode BIG5 text. */
4270
4271static int
4272encode_coding_sjis (coding)
4273 struct coding_system *coding;
4274{
4275 int multibytep = coding->dst_multibyte;
4276 int *charbuf = coding->charbuf;
4277 int *charbuf_end = charbuf + coding->charbuf_used;
4278 unsigned char *dst = coding->destination + coding->produced;
4279 unsigned char *dst_end = coding->destination + coding->dst_bytes;
4280 int safe_room = 4;
4281 int produced_chars = 0;
4282 Lisp_Object attrs, charset_list, val;
4283 int ascii_compatible;
4284 struct charset *charset_roman, *charset_kanji, *charset_kana;
4285 struct charset *charset_kanji2;
4286 int c;
4287
4288 CODING_GET_INFO (coding, attrs, charset_list);
4289 val = charset_list;
4290 charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
4291 charset_kana = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
4292 charset_kanji = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
4293 charset_kanji2 = NILP (val) ? NULL : CHARSET_FROM_ID (XINT (XCAR (val)));
4294
4295 ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs));
4296
4297 while (charbuf < charbuf_end)
4298 {
4299 ASSURE_DESTINATION (safe_room);
4300 c = *charbuf++;
4301 /* Now encode the character C. */
4302 if (ASCII_CHAR_P (c) && ascii_compatible)
4303 EMIT_ONE_ASCII_BYTE (c);
4304 else if (CHAR_BYTE8_P (c))
4305 {
4306 c = CHAR_TO_BYTE8 (c);
4307 EMIT_ONE_BYTE (c);
4308 }
4309 else
4310 {
4311 unsigned code;
4312 struct charset *charset = char_charset (c, charset_list, &code);
4313
4314 if (!charset)
4315 {
4316 if (coding->mode & CODING_MODE_SAFE_ENCODING)
4317 {
4318 code = CODING_INHIBIT_CHARACTER_SUBSTITUTION;
4319 charset = CHARSET_FROM_ID (charset_ascii);
4320 }
4321 else
4322 {
4323 c = coding->default_char;
4324 charset = char_charset (c, charset_list, &code);
4325 }
4326 }
4327 if (code == CHARSET_INVALID_CODE (charset))
4328 abort ();
4329 if (charset == charset_kanji)
4330 {
4331 int c1, c2;
4332 JIS_TO_SJIS (code);
4333 c1 = code >> 8, c2 = code & 0xFF;
4334 EMIT_TWO_BYTES (c1, c2);
4335 }
4336 else if (charset == charset_kana)
4337 EMIT_ONE_BYTE (code | 0x80);
4338 else if (charset_kanji2 && charset == charset_kanji2)
4339 {
4340 int c1, c2;
4341
4342 c1 = code >> 8;
4343 if (c1 == 0x21 || (c1 >= 0x23 && c1 < 0x25)
4344 || (c1 >= 0x2C && c1 <= 0x2F) || c1 >= 0x6E)
4345 {
4346 JIS_TO_SJIS2 (code);
4347 c1 = code >> 8, c2 = code & 0xFF;
4348 EMIT_TWO_BYTES (c1, c2);
4349 }
4350 else
4351 EMIT_ONE_ASCII_BYTE (code & 0x7F);
4352 }
4353 else
4354 EMIT_ONE_ASCII_BYTE (code & 0x7F);
4355 }
4356 }
4357 record_conversion_result (coding, CODING_RESULT_SUCCESS);
4358 coding->produced_char += produced_chars;
4359 coding->produced = dst - coding->destination;
4360 return 0;
4361}
4362
4363static int
4364encode_coding_big5 (coding)
4365 struct coding_system *coding;
4366{
4367 int multibytep = coding->dst_multibyte;
4368 int *charbuf = coding->charbuf;
4369 int *charbuf_end = charbuf + coding->charbuf_used;
4370 unsigned char *dst = coding->destination + coding->produced;
4371 unsigned char *dst_end = coding->destination + coding->dst_bytes;
4372 int safe_room = 4;
4373 int produced_chars = 0;
4374 Lisp_Object attrs, charset_list, val;
4375 int ascii_compatible;
4376 struct charset *charset_roman, *charset_big5;
4377 int c;
4378
4379 CODING_GET_INFO (coding, attrs, charset_list);
4380 val = charset_list;
4381 charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
4382 charset_big5 = CHARSET_FROM_ID (XINT (XCAR (val)));
4383 ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs));
4384
4385 while (charbuf < charbuf_end)
4386 {
4387 ASSURE_DESTINATION (safe_room);
4388 c = *charbuf++;
4389 /* Now encode the character C. */
4390 if (ASCII_CHAR_P (c) && ascii_compatible)
4391 EMIT_ONE_ASCII_BYTE (c);
4392 else if (CHAR_BYTE8_P (c))
4393 {
4394 c = CHAR_TO_BYTE8 (c);
4395 EMIT_ONE_BYTE (c);
4396 }
4397 else
4398 {
4399 unsigned code;
4400 struct charset *charset = char_charset (c, charset_list, &code);
4401
4402 if (! charset)
4403 {
4404 if (coding->mode & CODING_MODE_SAFE_ENCODING)
4405 {
4406 code = CODING_INHIBIT_CHARACTER_SUBSTITUTION;
4407 charset = CHARSET_FROM_ID (charset_ascii);
4408 }
4409 else
4410 {
4411 c = coding->default_char;
4412 charset = char_charset (c, charset_list, &code);
4413 }
4414 }
4415 if (code == CHARSET_INVALID_CODE (charset))
4416 abort ();
4417 if (charset == charset_big5)
4418 {
4419 int c1, c2;
4420
4421 c1 = code >> 8, c2 = code & 0xFF;
4422 EMIT_TWO_BYTES (c1, c2);
4423 }
4424 else
4425 EMIT_ONE_ASCII_BYTE (code & 0x7F);
4426 }
4427 }
4428 record_conversion_result (coding, CODING_RESULT_SUCCESS);
4429 coding->produced_char += produced_chars;
4430 coding->produced = dst - coding->destination;
4431 return 0;
4432}
4433
4434\f
4435/*** 10. CCL handlers ***/
4436
4437/* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4438 Check if a text is encoded in a coding system of which
4439 encoder/decoder are written in CCL program. If it is, return
4440 CATEGORY_MASK_CCL, else return 0. */
4441
4442static int
4443detect_coding_ccl (coding, detect_info)
4444 struct coding_system *coding;
4445 struct coding_detection_info *detect_info;
4446{
4447 const unsigned char *src = coding->source, *src_base;
4448 const unsigned char *src_end = coding->source + coding->src_bytes;
4449 int multibytep = coding->src_multibyte;
4450 int consumed_chars = 0;
4451 int found = 0;
4452 unsigned char *valids;
4453 int head_ascii = coding->head_ascii;
4454 Lisp_Object attrs;
4455
4456 detect_info->checked |= CATEGORY_MASK_CCL;
4457
4458 coding = &coding_categories[coding_category_ccl];
4459 valids = CODING_CCL_VALIDS (coding);
4460 attrs = CODING_ID_ATTRS (coding->id);
4461 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs)))
4462 src += head_ascii;
4463
4464 while (1)
4465 {
4466 int c;
4467
4468 src_base = src;
4469 ONE_MORE_BYTE (c);
4470 if (c < 0 || ! valids[c])
4471 break;
4472 if ((valids[c] > 1))
4473 found = CATEGORY_MASK_CCL;
4474 }
4475 detect_info->rejected |= CATEGORY_MASK_CCL;
4476 return 0;
4477
4478 no_more_source:
4479 detect_info->found |= found;
4480 return 1;
4481}
4482
4483static void
4484decode_coding_ccl (coding)
4485 struct coding_system *coding;
4486{
4487 const unsigned char *src = coding->source + coding->consumed;
4488 const unsigned char *src_end = coding->source + coding->src_bytes;
4489 int *charbuf = coding->charbuf + coding->charbuf_used;
4490 int *charbuf_end = coding->charbuf + coding->charbuf_size;
4491 int consumed_chars = 0;
4492 int multibytep = coding->src_multibyte;
4493 struct ccl_program ccl;
4494 int source_charbuf[1024];
4495 int source_byteidx[1024];
4496 Lisp_Object attrs, charset_list;
4497
4498 CODING_GET_INFO (coding, attrs, charset_list);
4499 setup_ccl_program (&ccl, CODING_CCL_DECODER (coding));
4500
4501 while (src < src_end)
4502 {
4503 const unsigned char *p = src;
4504 int *source, *source_end;
4505 int i = 0;
4506
4507 if (multibytep)
4508 while (i < 1024 && p < src_end)
4509 {
4510 source_byteidx[i] = p - src;
4511 source_charbuf[i++] = STRING_CHAR_ADVANCE (p);
4512 }
4513 else
4514 while (i < 1024 && p < src_end)
4515 source_charbuf[i++] = *p++;
4516
4517 if (p == src_end && coding->mode & CODING_MODE_LAST_BLOCK)
4518 ccl.last_block = 1;
4519
4520 source = source_charbuf;
4521 source_end = source + i;
4522 while (source < source_end)
4523 {
4524 ccl_driver (&ccl, source, charbuf,
4525 source_end - source, charbuf_end - charbuf,
4526 charset_list);
4527 source += ccl.consumed;
4528 charbuf += ccl.produced;
4529 if (ccl.status != CCL_STAT_SUSPEND_BY_DST)
4530 break;
4531 }
4532 if (source < source_end)
4533 src += source_byteidx[source - source_charbuf];
4534 else
4535 src = p;
4536 consumed_chars += source - source_charbuf;
4537
4538 if (ccl.status != CCL_STAT_SUSPEND_BY_SRC
4539 && ccl.status != CODING_RESULT_INSUFFICIENT_SRC)
4540 break;
4541 }
4542
4543 switch (ccl.status)
4544 {
4545 case CCL_STAT_SUSPEND_BY_SRC:
4546 record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_SRC);
4547 break;
4548 case CCL_STAT_SUSPEND_BY_DST:
4549 break;
4550 case CCL_STAT_QUIT:
4551 case CCL_STAT_INVALID_CMD:
4552 record_conversion_result (coding, CODING_RESULT_INTERRUPT);
4553 break;
4554 default:
4555 record_conversion_result (coding, CODING_RESULT_SUCCESS);
4556 break;
4557 }
4558 coding->consumed_char += consumed_chars;
4559 coding->consumed = src - coding->source;
4560 coding->charbuf_used = charbuf - coding->charbuf;
4561}
4562
4563static int
4564encode_coding_ccl (coding)
4565 struct coding_system *coding;
4566{
4567 struct ccl_program ccl;
4568 int multibytep = coding->dst_multibyte;
4569 int *charbuf = coding->charbuf;
4570 int *charbuf_end = charbuf + coding->charbuf_used;
4571 unsigned char *dst = coding->destination + coding->produced;
4572 unsigned char *dst_end = coding->destination + coding->dst_bytes;
4573 int destination_charbuf[1024];
4574 int i, produced_chars = 0;
4575 Lisp_Object attrs, charset_list;
4576
4577 CODING_GET_INFO (coding, attrs, charset_list);
4578 setup_ccl_program (&ccl, CODING_CCL_ENCODER (coding));
4579
4580 ccl.last_block = coding->mode & CODING_MODE_LAST_BLOCK;
4581 ccl.dst_multibyte = coding->dst_multibyte;
4582
4583 while (charbuf < charbuf_end)
4584 {
4585 ccl_driver (&ccl, charbuf, destination_charbuf,
4586 charbuf_end - charbuf, 1024, charset_list);
4587 if (multibytep)
4588 {
4589 ASSURE_DESTINATION (ccl.produced * 2);
4590 for (i = 0; i < ccl.produced; i++)
4591 EMIT_ONE_BYTE (destination_charbuf[i] & 0xFF);
4592 }
4593 else
4594 {
4595 ASSURE_DESTINATION (ccl.produced);
4596 for (i = 0; i < ccl.produced; i++)
4597 *dst++ = destination_charbuf[i] & 0xFF;
4598 produced_chars += ccl.produced;
4599 }
4600 charbuf += ccl.consumed;
4601 if (ccl.status == CCL_STAT_QUIT
4602 || ccl.status == CCL_STAT_INVALID_CMD)
4603 break;
4604 }
4605
4606 switch (ccl.status)
4607 {
4608 case CCL_STAT_SUSPEND_BY_SRC:
4609 record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_SRC);
4610 break;
4611 case CCL_STAT_SUSPEND_BY_DST:
4612 record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_DST);
4613 break;
4614 case CCL_STAT_QUIT:
4615 case CCL_STAT_INVALID_CMD:
4616 record_conversion_result (coding, CODING_RESULT_INTERRUPT);
4617 break;
4618 default:
4619 record_conversion_result (coding, CODING_RESULT_SUCCESS);
4620 break;
4621 }
4622
4623 coding->produced_char += produced_chars;
4624 coding->produced = dst - coding->destination;
4625 return 0;
4626}
4627
4628
4629\f
4630/*** 10, 11. no-conversion handlers ***/
4631
4632/* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
4633
4634static void
4635decode_coding_raw_text (coding)
4636 struct coding_system *coding;
4637{
4638 coding->chars_at_source = 1;
4639 coding->consumed_char = 0;
4640 coding->consumed = 0;
4641 record_conversion_result (coding, CODING_RESULT_SUCCESS);
4642}
4643
4644static int
4645encode_coding_raw_text (coding)
4646 struct coding_system *coding;
4647{
4648 int multibytep = coding->dst_multibyte;
4649 int *charbuf = coding->charbuf;
4650 int *charbuf_end = coding->charbuf + coding->charbuf_used;
4651 unsigned char *dst = coding->destination + coding->produced;
4652 unsigned char *dst_end = coding->destination + coding->dst_bytes;
4653 int produced_chars = 0;
4654 int c;
4655
4656 if (multibytep)
4657 {
4658 int safe_room = MAX_MULTIBYTE_LENGTH * 2;
4659
4660 if (coding->src_multibyte)
4661 while (charbuf < charbuf_end)
4662 {
4663 ASSURE_DESTINATION (safe_room);
4664 c = *charbuf++;
4665 if (ASCII_CHAR_P (c))
4666 EMIT_ONE_ASCII_BYTE (c);
4667 else if (CHAR_BYTE8_P (c))
4668 {
4669 c = CHAR_TO_BYTE8 (c);
4670 EMIT_ONE_BYTE (c);
4671 }
4672 else
4673 {
4674 unsigned char str[MAX_MULTIBYTE_LENGTH], *p0 = str, *p1 = str;
4675
4676 CHAR_STRING_ADVANCE (c, p1);
4677 while (p0 < p1)
4678 {
4679 EMIT_ONE_BYTE (*p0);
4680 p0++;
4681 }
4682 }
4683 }
4684 else
4685 while (charbuf < charbuf_end)
4686 {
4687 ASSURE_DESTINATION (safe_room);
4688 c = *charbuf++;
4689 EMIT_ONE_BYTE (c);
4690 }
4691 }
4692 else
4693 {
4694 if (coding->src_multibyte)
4695 {
4696 int safe_room = MAX_MULTIBYTE_LENGTH;
4697
4698 while (charbuf < charbuf_end)
4699 {
4700 ASSURE_DESTINATION (safe_room);
4701 c = *charbuf++;
4702 if (ASCII_CHAR_P (c))
4703 *dst++ = c;
4704 else if (CHAR_BYTE8_P (c))
4705 *dst++ = CHAR_TO_BYTE8 (c);
4706 else
4707 CHAR_STRING_ADVANCE (c, dst);
4708 produced_chars++;
4709 }
4710 }
4711 else
4712 {
4713 ASSURE_DESTINATION (charbuf_end - charbuf);
4714 while (charbuf < charbuf_end && dst < dst_end)
4715 *dst++ = *charbuf++;
4716 produced_chars = dst - (coding->destination + coding->dst_bytes);
4717 }
4718 }
4719 record_conversion_result (coding, CODING_RESULT_SUCCESS);
4720 coding->produced_char += produced_chars;
4721 coding->produced = dst - coding->destination;
4722 return 0;
4723}
4724
4725/* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4726 Check if a text is encoded in a charset-based coding system. If it
4727 is, return 1, else return 0. */
4728
4729static int
4730detect_coding_charset (coding, detect_info)
4731 struct coding_system *coding;
4732 struct coding_detection_info *detect_info;
4733{
4734 const unsigned char *src = coding->source, *src_base;
4735 const unsigned char *src_end = coding->source + coding->src_bytes;
4736 int multibytep = coding->src_multibyte;
4737 int consumed_chars = 0;
4738 Lisp_Object attrs, valids;
4739 int found = 0;
4740 int head_ascii = coding->head_ascii;
4741
4742 detect_info->checked |= CATEGORY_MASK_CHARSET;
4743
4744 coding = &coding_categories[coding_category_charset];
4745 attrs = CODING_ID_ATTRS (coding->id);
4746 valids = AREF (attrs, coding_attr_charset_valids);
4747
4748 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs)))
4749 src += head_ascii;
4750
4751 while (1)
4752 {
4753 int c;
4754 Lisp_Object val;
4755 struct charset *charset;
4756 int dim, idx;
4757
4758 src_base = src;
4759 ONE_MORE_BYTE (c);
4760 if (c < 0)
4761 continue;
4762 val = AREF (valids, c);
4763 if (NILP (val))
4764 break;
4765 if (c >= 0x80)
4766 found = CATEGORY_MASK_CHARSET;
4767 if (INTEGERP (val))
4768 {
4769 charset = CHARSET_FROM_ID (XFASTINT (val));
4770 dim = CHARSET_DIMENSION (charset);
4771 for (idx = 1; idx < dim; idx++)
4772 {
4773 if (src == src_end)
4774 goto too_short;
4775 ONE_MORE_BYTE (c);
4776 if (c < charset->code_space[(dim - 1 - idx) * 2]
4777 || c > charset->code_space[(dim - 1 - idx) * 2 + 1])
4778 break;
4779 }
4780 if (idx < dim)
4781 break;
4782 }
4783 else
4784 {
4785 idx = 1;
4786 for (; CONSP (val); val = XCDR (val))
4787 {
4788 charset = CHARSET_FROM_ID (XFASTINT (XCAR (val)));
4789 dim = CHARSET_DIMENSION (charset);
4790 while (idx < dim)
4791 {
4792 if (src == src_end)
4793 goto too_short;
4794 ONE_MORE_BYTE (c);
4795 if (c < charset->code_space[(dim - 1 - idx) * 4]
4796 || c > charset->code_space[(dim - 1 - idx) * 4 + 1])
4797 break;
4798 idx++;
4799 }
4800 if (idx == dim)
4801 {
4802 val = Qnil;
4803 break;
4804 }
4805 }
4806 if (CONSP (val))
4807 break;
4808 }
4809 }
4810 too_short:
4811 detect_info->rejected |= CATEGORY_MASK_CHARSET;
4812 return 0;
4813
4814 no_more_source:
4815 detect_info->found |= found;
4816 return 1;
4817}
4818
4819static void
4820decode_coding_charset (coding)
4821 struct coding_system *coding;
4822{
4823 const unsigned char *src = coding->source + coding->consumed;
4824 const unsigned char *src_end = coding->source + coding->src_bytes;
4825 const unsigned char *src_base;
4826 int *charbuf = coding->charbuf + coding->charbuf_used;
4827 int *charbuf_end
4828 = coding->charbuf + coding->charbuf_size - MAX_ANNOTATION_LENGTH;
4829 int consumed_chars = 0, consumed_chars_base;
4830 int multibytep = coding->src_multibyte;
4831 Lisp_Object attrs, charset_list, valids;
4832 int char_offset = coding->produced_char;
4833 int last_offset = char_offset;
4834 int last_id = charset_ascii;
4835
4836 CODING_GET_INFO (coding, attrs, charset_list);
4837 valids = AREF (attrs, coding_attr_charset_valids);
4838
4839 while (1)
4840 {
4841 int c;
4842 Lisp_Object val;
4843 struct charset *charset;
4844 int dim;
4845 int len = 1;
4846 unsigned code;
4847
4848 src_base = src;
4849 consumed_chars_base = consumed_chars;
4850
4851 if (charbuf >= charbuf_end)
4852 break;
4853
4854 ONE_MORE_BYTE (c);
4855 if (c < 0)
4856 goto invalid_code;
4857 code = c;
4858
4859 val = AREF (valids, c);
4860 if (NILP (val))
4861 goto invalid_code;
4862 if (INTEGERP (val))
4863 {
4864 charset = CHARSET_FROM_ID (XFASTINT (val));
4865 dim = CHARSET_DIMENSION (charset);
4866 while (len < dim)
4867 {
4868 ONE_MORE_BYTE (c);
4869 code = (code << 8) | c;
4870 len++;
4871 }
4872 CODING_DECODE_CHAR (coding, src, src_base, src_end,
4873 charset, code, c);
4874 }
4875 else
4876 {
4877 /* VAL is a list of charset IDs. It is assured that the
4878 list is sorted by charset dimensions (smaller one
4879 comes first). */
4880 while (CONSP (val))
4881 {
4882 charset = CHARSET_FROM_ID (XFASTINT (XCAR (val)));
4883 dim = CHARSET_DIMENSION (charset);
4884 while (len < dim)
4885 {
4886 ONE_MORE_BYTE (c);
4887 code = (code << 8) | c;
4888 len++;
4889 }
4890 CODING_DECODE_CHAR (coding, src, src_base,
4891 src_end, charset, code, c);
4892 if (c >= 0)
4893 break;
4894 val = XCDR (val);
4895 }
4896 }
4897 if (c < 0)
4898 goto invalid_code;
4899 if (charset->id != charset_ascii
4900 && last_id != charset->id)
4901 {
4902 if (last_id != charset_ascii)
4903 ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id);
4904 last_id = charset->id;
4905 last_offset = char_offset;
4906 }
4907
4908 *charbuf++ = c;
4909 char_offset++;
4910 continue;
4911
4912 invalid_code:
4913 src = src_base;
4914 consumed_chars = consumed_chars_base;
4915 ONE_MORE_BYTE (c);
4916 *charbuf++ = c < 0 ? -c : ASCII_BYTE_P (c) ? c : BYTE8_TO_CHAR (c);
4917 char_offset++;
4918 coding->errors++;
4919 }
4920
4921 no_more_source:
4922 if (last_id != charset_ascii)
4923 ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id);
4924 coding->consumed_char += consumed_chars_base;
4925 coding->consumed = src_base - coding->source;
4926 coding->charbuf_used = charbuf - coding->charbuf;
4927}
4928
4929static int
4930encode_coding_charset (coding)
4931 struct coding_system *coding;
4932{
4933 int multibytep = coding->dst_multibyte;
4934 int *charbuf = coding->charbuf;
4935 int *charbuf_end = charbuf + coding->charbuf_used;
4936 unsigned char *dst = coding->destination + coding->produced;
4937 unsigned char *dst_end = coding->destination + coding->dst_bytes;
4938 int safe_room = MAX_MULTIBYTE_LENGTH;
4939 int produced_chars = 0;
4940 Lisp_Object attrs, charset_list;
4941 int ascii_compatible;
4942 int c;
4943
4944 CODING_GET_INFO (coding, attrs, charset_list);
4945 ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs));
4946
4947 while (charbuf < charbuf_end)
4948 {
4949 struct charset *charset;
4950 unsigned code;
4951
4952 ASSURE_DESTINATION (safe_room);
4953 c = *charbuf++;
4954 if (ascii_compatible && ASCII_CHAR_P (c))
4955 EMIT_ONE_ASCII_BYTE (c);
4956 else if (CHAR_BYTE8_P (c))
4957 {
4958 c = CHAR_TO_BYTE8 (c);
4959 EMIT_ONE_BYTE (c);
4960 }
4961 else
4962 {
4963 charset = char_charset (c, charset_list, &code);
4964 if (charset)
4965 {
4966 if (CHARSET_DIMENSION (charset) == 1)
4967 EMIT_ONE_BYTE (code);
4968 else if (CHARSET_DIMENSION (charset) == 2)
4969 EMIT_TWO_BYTES (code >> 8, code & 0xFF);
4970 else if (CHARSET_DIMENSION (charset) == 3)
4971 EMIT_THREE_BYTES (code >> 16, (code >> 8) & 0xFF, code & 0xFF);
4972 else
4973 EMIT_FOUR_BYTES (code >> 24, (code >> 16) & 0xFF,
4974 (code >> 8) & 0xFF, code & 0xFF);
4975 }
4976 else
4977 {
4978 if (coding->mode & CODING_MODE_SAFE_ENCODING)
4979 c = CODING_INHIBIT_CHARACTER_SUBSTITUTION;
4980 else
4981 c = coding->default_char;
4982 EMIT_ONE_BYTE (c);
4983 }
4984 }
4985 }
4986
4987 record_conversion_result (coding, CODING_RESULT_SUCCESS);
4988 coding->produced_char += produced_chars;
4989 coding->produced = dst - coding->destination;
4990 return 0;
4991}
4992
4993\f
4994/*** 7. C library functions ***/
4995
4996/* Setup coding context CODING from information about CODING_SYSTEM.
4997 If CODING_SYSTEM is nil, `no-conversion' is assumed. If
4998 CODING_SYSTEM is invalid, signal an error. */
4999
5000void
5001setup_coding_system (coding_system, coding)
5002 Lisp_Object coding_system;
5003 struct coding_system *coding;
5004{
5005 Lisp_Object attrs;
5006 Lisp_Object eol_type;
5007 Lisp_Object coding_type;
5008 Lisp_Object val;
5009
5010 if (NILP (coding_system))
5011 coding_system = Qundecided;
5012
5013 CHECK_CODING_SYSTEM_GET_ID (coding_system, coding->id);
5014
5015 attrs = CODING_ID_ATTRS (coding->id);
5016 eol_type = CODING_ID_EOL_TYPE (coding->id);
5017
5018 coding->mode = 0;
5019 coding->head_ascii = -1;
5020 if (VECTORP (eol_type))
5021 coding->common_flags = (CODING_REQUIRE_DECODING_MASK
5022 | CODING_REQUIRE_DETECTION_MASK);
5023 else if (! EQ (eol_type, Qunix))
5024 coding->common_flags = (CODING_REQUIRE_DECODING_MASK
5025 | CODING_REQUIRE_ENCODING_MASK);
5026 else
5027 coding->common_flags = 0;
5028 if (! NILP (CODING_ATTR_POST_READ (attrs)))
5029 coding->common_flags |= CODING_REQUIRE_DECODING_MASK;
5030 if (! NILP (CODING_ATTR_PRE_WRITE (attrs)))
5031 coding->common_flags |= CODING_REQUIRE_ENCODING_MASK;
5032 if (! NILP (CODING_ATTR_FOR_UNIBYTE (attrs)))
5033 coding->common_flags |= CODING_FOR_UNIBYTE_MASK;
5034
5035 val = CODING_ATTR_SAFE_CHARSETS (attrs);
5036 coding->max_charset_id = SCHARS (val) - 1;
5037 coding->safe_charsets = (char *) SDATA (val);
5038 coding->default_char = XINT (CODING_ATTR_DEFAULT_CHAR (attrs));
5039
5040 coding_type = CODING_ATTR_TYPE (attrs);
5041 if (EQ (coding_type, Qundecided))
5042 {
5043 coding->detector = NULL;
5044 coding->decoder = decode_coding_raw_text;
5045 coding->encoder = encode_coding_raw_text;
5046 coding->common_flags |= CODING_REQUIRE_DETECTION_MASK;
5047 }
5048 else if (EQ (coding_type, Qiso_2022))
5049 {
5050 int i;
5051 int flags = XINT (AREF (attrs, coding_attr_iso_flags));
5052
5053 /* Invoke graphic register 0 to plane 0. */
5054 CODING_ISO_INVOCATION (coding, 0) = 0;
5055 /* Invoke graphic register 1 to plane 1 if we can use 8-bit. */
5056 CODING_ISO_INVOCATION (coding, 1)
5057 = (flags & CODING_ISO_FLAG_SEVEN_BITS ? -1 : 1);
5058 /* Setup the initial status of designation. */
5059 for (i = 0; i < 4; i++)
5060 CODING_ISO_DESIGNATION (coding, i) = CODING_ISO_INITIAL (coding, i);
5061 /* Not single shifting initially. */
5062 CODING_ISO_SINGLE_SHIFTING (coding) = 0;
5063 /* Beginning of buffer should also be regarded as bol. */
5064 CODING_ISO_BOL (coding) = 1;
5065 coding->detector = detect_coding_iso_2022;
5066 coding->decoder = decode_coding_iso_2022;
5067 coding->encoder = encode_coding_iso_2022;
5068 if (flags & CODING_ISO_FLAG_SAFE)
5069 coding->mode |= CODING_MODE_SAFE_ENCODING;
5070 coding->common_flags
5071 |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK
5072 | CODING_REQUIRE_FLUSHING_MASK);
5073 if (flags & CODING_ISO_FLAG_COMPOSITION)
5074 coding->common_flags |= CODING_ANNOTATE_COMPOSITION_MASK;
5075 if (flags & CODING_ISO_FLAG_DESIGNATION)
5076 coding->common_flags |= CODING_ANNOTATE_CHARSET_MASK;
5077 if (flags & CODING_ISO_FLAG_FULL_SUPPORT)
5078 {
5079 setup_iso_safe_charsets (attrs);
5080 val = CODING_ATTR_SAFE_CHARSETS (attrs);
5081 coding->max_charset_id = SCHARS (val) - 1;
5082 coding->safe_charsets = (char *) SDATA (val);
5083 }
5084 CODING_ISO_FLAGS (coding) = flags;
5085 }
5086 else if (EQ (coding_type, Qcharset))
5087 {
5088 coding->detector = detect_coding_charset;
5089 coding->decoder = decode_coding_charset;
5090 coding->encoder = encode_coding_charset;
5091 coding->common_flags
5092 |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK);
5093 }
5094 else if (EQ (coding_type, Qutf_8))
5095 {
5096 coding->detector = detect_coding_utf_8;
5097 coding->decoder = decode_coding_utf_8;
5098 coding->encoder = encode_coding_utf_8;
5099 coding->common_flags
5100 |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK);
5101 }
5102 else if (EQ (coding_type, Qutf_16))
5103 {
5104 val = AREF (attrs, coding_attr_utf_16_bom);
5105 CODING_UTF_16_BOM (coding) = (CONSP (val) ? utf_16_detect_bom
5106 : EQ (val, Qt) ? utf_16_with_bom
5107 : utf_16_without_bom);
5108 val = AREF (attrs, coding_attr_utf_16_endian);
5109 CODING_UTF_16_ENDIAN (coding) = (EQ (val, Qbig) ? utf_16_big_endian
5110 : utf_16_little_endian);
5111 CODING_UTF_16_SURROGATE (coding) = 0;
5112 coding->detector = detect_coding_utf_16;
5113 coding->decoder = decode_coding_utf_16;
5114 coding->encoder = encode_coding_utf_16;
5115 coding->common_flags
5116 |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK);
5117 if (CODING_UTF_16_BOM (coding) == utf_16_detect_bom)
5118 coding->common_flags |= CODING_REQUIRE_DETECTION_MASK;
5119 }
5120 else if (EQ (coding_type, Qccl))
5121 {
5122 coding->detector = detect_coding_ccl;
5123 coding->decoder = decode_coding_ccl;
5124 coding->encoder = encode_coding_ccl;
5125 coding->common_flags
5126 |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK
5127 | CODING_REQUIRE_FLUSHING_MASK);
5128 }
5129 else if (EQ (coding_type, Qemacs_mule))
5130 {
5131 coding->detector = detect_coding_emacs_mule;
5132 coding->decoder = decode_coding_emacs_mule;
5133 coding->encoder = encode_coding_emacs_mule;
5134 coding->common_flags
5135 |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK);
5136 if (! NILP (AREF (attrs, coding_attr_emacs_mule_full))
5137 && ! EQ (CODING_ATTR_CHARSET_LIST (attrs), Vemacs_mule_charset_list))
5138 {
5139 Lisp_Object tail, safe_charsets;
5140 int max_charset_id = 0;
5141
5142 for (tail = Vemacs_mule_charset_list; CONSP (tail);
5143 tail = XCDR (tail))
5144 if (max_charset_id < XFASTINT (XCAR (tail)))
5145 max_charset_id = XFASTINT (XCAR (tail));
5146 safe_charsets = Fmake_string (make_number (max_charset_id + 1),
5147 make_number (255));
5148 for (tail = Vemacs_mule_charset_list; CONSP (tail);
5149 tail = XCDR (tail))
5150 SSET (safe_charsets, XFASTINT (XCAR (tail)), 0);
5151 coding->max_charset_id = max_charset_id;
5152 coding->safe_charsets = (char *) SDATA (safe_charsets);
5153 }
5154 }
5155 else if (EQ (coding_type, Qshift_jis))
5156 {
5157 coding->detector = detect_coding_sjis;
5158 coding->decoder = decode_coding_sjis;
5159 coding->encoder = encode_coding_sjis;
5160 coding->common_flags
5161 |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK);
5162 }
5163 else if (EQ (coding_type, Qbig5))
5164 {
5165 coding->detector = detect_coding_big5;
5166 coding->decoder = decode_coding_big5;
5167 coding->encoder = encode_coding_big5;
5168 coding->common_flags
5169 |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK);
5170 }
5171 else /* EQ (coding_type, Qraw_text) */
5172 {
5173 coding->detector = NULL;
5174 coding->decoder = decode_coding_raw_text;
5175 coding->encoder = encode_coding_raw_text;
5176 if (! EQ (eol_type, Qunix))
5177 {
5178 coding->common_flags |= CODING_REQUIRE_DECODING_MASK;
5179 if (! VECTORP (eol_type))
5180 coding->common_flags |= CODING_REQUIRE_ENCODING_MASK;
5181 }
5182
5183 }
5184
5185 return;
5186}
5187
5188/* Return a list of charsets supported by CODING. */
5189
5190Lisp_Object
5191coding_charset_list (coding)
5192 struct coding_system *coding;
5193{
5194 Lisp_Object attrs, charset_list;
5195
5196 CODING_GET_INFO (coding, attrs, charset_list);
5197 if (EQ (CODING_ATTR_TYPE (attrs), Qiso_2022))
5198 {
5199 int flags = XINT (AREF (attrs, coding_attr_iso_flags));
5200
5201 if (flags & CODING_ISO_FLAG_FULL_SUPPORT)
5202 charset_list = Viso_2022_charset_list;
5203 }
5204 else if (EQ (CODING_ATTR_TYPE (attrs), Qemacs_mule))
5205 {
5206 charset_list = Vemacs_mule_charset_list;
5207 }
5208 return charset_list;
5209}
5210
5211
5212/* Return raw-text or one of its subsidiaries that has the same
5213 eol_type as CODING-SYSTEM. */
5214
5215Lisp_Object
5216raw_text_coding_system (coding_system)
5217 Lisp_Object coding_system;
5218{
5219 Lisp_Object spec, attrs;
5220 Lisp_Object eol_type, raw_text_eol_type;
5221
5222 if (NILP (coding_system))
5223 return Qraw_text;
5224 spec = CODING_SYSTEM_SPEC (coding_system);
5225 attrs = AREF (spec, 0);
5226
5227 if (EQ (CODING_ATTR_TYPE (attrs), Qraw_text))
5228 return coding_system;
5229
5230 eol_type = AREF (spec, 2);
5231 if (VECTORP (eol_type))
5232 return Qraw_text;
5233 spec = CODING_SYSTEM_SPEC (Qraw_text);
5234 raw_text_eol_type = AREF (spec, 2);
5235 return (EQ (eol_type, Qunix) ? AREF (raw_text_eol_type, 0)
5236 : EQ (eol_type, Qdos) ? AREF (raw_text_eol_type, 1)
5237 : AREF (raw_text_eol_type, 2));
5238}
5239
5240
5241/* If CODING_SYSTEM doesn't specify end-of-line format but PARENT
5242 does, return one of the subsidiary that has the same eol-spec as
5243 PARENT. Otherwise, return CODING_SYSTEM. If PARENT is nil,
5244 inherit end-of-line format from the system's setting
5245 (system_eol_type). */
5246
5247Lisp_Object
5248coding_inherit_eol_type (coding_system, parent)
5249 Lisp_Object coding_system, parent;
5250{
5251 Lisp_Object spec, eol_type;
5252
5253 if (NILP (coding_system))
5254 coding_system = Qraw_text;
5255 spec = CODING_SYSTEM_SPEC (coding_system);
5256 eol_type = AREF (spec, 2);
5257 if (VECTORP (eol_type))
5258 {
5259 Lisp_Object parent_eol_type;
5260
5261 if (! NILP (parent))
5262 {
5263 Lisp_Object parent_spec;
5264
5265 parent_spec = CODING_SYSTEM_SPEC (parent);
5266 parent_eol_type = AREF (parent_spec, 2);
5267 }
5268 else
5269 parent_eol_type = system_eol_type;
5270 if (EQ (parent_eol_type, Qunix))
5271 coding_system = AREF (eol_type, 0);
5272 else if (EQ (parent_eol_type, Qdos))
5273 coding_system = AREF (eol_type, 1);
5274 else if (EQ (parent_eol_type, Qmac))
5275 coding_system = AREF (eol_type, 2);
5276 }
5277 return coding_system;
5278}
5279
5280/* Emacs has a mechanism to automatically detect a coding system if it
5281 is one of Emacs' internal format, ISO2022, SJIS, and BIG5. But,
5282 it's impossible to distinguish some coding systems accurately
5283 because they use the same range of codes. So, at first, coding
5284 systems are categorized into 7, those are:
5285
5286 o coding-category-emacs-mule
5287
5288 The category for a coding system which has the same code range
5289 as Emacs' internal format. Assigned the coding-system (Lisp
5290 symbol) `emacs-mule' by default.
5291
5292 o coding-category-sjis
5293
5294 The category for a coding system which has the same code range
5295 as SJIS. Assigned the coding-system (Lisp
5296 symbol) `japanese-shift-jis' by default.
5297
5298 o coding-category-iso-7
5299
5300 The category for a coding system which has the same code range
5301 as ISO2022 of 7-bit environment. This doesn't use any locking
5302 shift and single shift functions. This can encode/decode all
5303 charsets. Assigned the coding-system (Lisp symbol)
5304 `iso-2022-7bit' by default.
5305
5306 o coding-category-iso-7-tight
5307
5308 Same as coding-category-iso-7 except that this can
5309 encode/decode only the specified charsets.
5310
5311 o coding-category-iso-8-1
5312
5313 The category for a coding system which has the same code range
5314 as ISO2022 of 8-bit environment and graphic plane 1 used only
5315 for DIMENSION1 charset. This doesn't use any locking shift
5316 and single shift functions. Assigned the coding-system (Lisp
5317 symbol) `iso-latin-1' by default.
5318
5319 o coding-category-iso-8-2
5320
5321 The category for a coding system which has the same code range
5322 as ISO2022 of 8-bit environment and graphic plane 1 used only
5323 for DIMENSION2 charset. This doesn't use any locking shift
5324 and single shift functions. Assigned the coding-system (Lisp
5325 symbol) `japanese-iso-8bit' by default.
5326
5327 o coding-category-iso-7-else
5328
5329 The category for a coding system which has the same code range
5330 as ISO2022 of 7-bit environemnt but uses locking shift or
5331 single shift functions. Assigned the coding-system (Lisp
5332 symbol) `iso-2022-7bit-lock' by default.
5333
5334 o coding-category-iso-8-else
5335
5336 The category for a coding system which has the same code range
5337 as ISO2022 of 8-bit environemnt but uses locking shift or
5338 single shift functions. Assigned the coding-system (Lisp
5339 symbol) `iso-2022-8bit-ss2' by default.
5340
5341 o coding-category-big5
5342
5343 The category for a coding system which has the same code range
5344 as BIG5. Assigned the coding-system (Lisp symbol)
5345 `cn-big5' by default.
5346
5347 o coding-category-utf-8
5348
5349 The category for a coding system which has the same code range
5350 as UTF-8 (cf. RFC3629). Assigned the coding-system (Lisp
5351 symbol) `utf-8' by default.
5352
5353 o coding-category-utf-16-be
5354
5355 The category for a coding system in which a text has an
5356 Unicode signature (cf. Unicode Standard) in the order of BIG
5357 endian at the head. Assigned the coding-system (Lisp symbol)
5358 `utf-16-be' by default.
5359
5360 o coding-category-utf-16-le
5361
5362 The category for a coding system in which a text has an
5363 Unicode signature (cf. Unicode Standard) in the order of
5364 LITTLE endian at the head. Assigned the coding-system (Lisp
5365 symbol) `utf-16-le' by default.
5366
5367 o coding-category-ccl
5368
5369 The category for a coding system of which encoder/decoder is
5370 written in CCL programs. The default value is nil, i.e., no
5371 coding system is assigned.
5372
5373 o coding-category-binary
5374
5375 The category for a coding system not categorized in any of the
5376 above. Assigned the coding-system (Lisp symbol)
5377 `no-conversion' by default.
5378
5379 Each of them is a Lisp symbol and the value is an actual
5380 `coding-system's (this is also a Lisp symbol) assigned by a user.
5381 What Emacs does actually is to detect a category of coding system.
5382 Then, it uses a `coding-system' assigned to it. If Emacs can't
5383 decide only one possible category, it selects a category of the
5384 highest priority. Priorities of categories are also specified by a
5385 user in a Lisp variable `coding-category-list'.
5386
5387*/
5388
5389#define EOL_SEEN_NONE 0
5390#define EOL_SEEN_LF 1
5391#define EOL_SEEN_CR 2
5392#define EOL_SEEN_CRLF 4
5393
5394/* Detect how end-of-line of a text of length SRC_BYTES pointed by
5395 SOURCE is encoded. If CATEGORY is one of
5396 coding_category_utf_16_XXXX, assume that CR and LF are encoded by
5397 two-byte, else they are encoded by one-byte.
5398
5399 Return one of EOL_SEEN_XXX. */
5400
5401#define MAX_EOL_CHECK_COUNT 3
5402
5403static int
5404detect_eol (source, src_bytes, category)
5405 const unsigned char *source;
5406 EMACS_INT src_bytes;
5407 enum coding_category category;
5408{
5409 const unsigned char *src = source, *src_end = src + src_bytes;
5410 unsigned char c;
5411 int total = 0;
5412 int eol_seen = EOL_SEEN_NONE;
5413
5414 if ((1 << category) & CATEGORY_MASK_UTF_16)
5415 {
5416 int msb, lsb;
5417
5418 msb = category == (coding_category_utf_16_le
5419 | coding_category_utf_16_le_nosig);
5420 lsb = 1 - msb;
5421
5422 while (src + 1 < src_end)
5423 {
5424 c = src[lsb];
5425 if (src[msb] == 0 && (c == '\n' || c == '\r'))
5426 {
5427 int this_eol;
5428
5429 if (c == '\n')
5430 this_eol = EOL_SEEN_LF;
5431 else if (src + 3 >= src_end
5432 || src[msb + 2] != 0
5433 || src[lsb + 2] != '\n')
5434 this_eol = EOL_SEEN_CR;
5435 else
5436 this_eol = EOL_SEEN_CRLF;
5437
5438 if (eol_seen == EOL_SEEN_NONE)
5439 /* This is the first end-of-line. */
5440 eol_seen = this_eol;
5441 else if (eol_seen != this_eol)
5442 {
5443 /* The found type is different from what found before. */
5444 eol_seen = EOL_SEEN_LF;
5445 break;
5446 }
5447 if (++total == MAX_EOL_CHECK_COUNT)
5448 break;
5449 }
5450 src += 2;
5451 }
5452 }
5453 else
5454 {
5455 while (src < src_end)
5456 {
5457 c = *src++;
5458 if (c == '\n' || c == '\r')
5459 {
5460 int this_eol;
5461
5462 if (c == '\n')
5463 this_eol = EOL_SEEN_LF;
5464 else if (src >= src_end || *src != '\n')
5465 this_eol = EOL_SEEN_CR;
5466 else
5467 this_eol = EOL_SEEN_CRLF, src++;
5468
5469 if (eol_seen == EOL_SEEN_NONE)
5470 /* This is the first end-of-line. */
5471 eol_seen = this_eol;
5472 else if (eol_seen != this_eol)
5473 {
5474 /* The found type is different from what found before. */
5475 eol_seen = EOL_SEEN_LF;
5476 break;
5477 }
5478 if (++total == MAX_EOL_CHECK_COUNT)
5479 break;
5480 }
5481 }
5482 }
5483 return eol_seen;
5484}
5485
5486
5487static Lisp_Object
5488adjust_coding_eol_type (coding, eol_seen)
5489 struct coding_system *coding;
5490 int eol_seen;
5491{
5492 Lisp_Object eol_type;
5493
5494 eol_type = CODING_ID_EOL_TYPE (coding->id);
5495 if (eol_seen & EOL_SEEN_LF)
5496 {
5497 coding->id = CODING_SYSTEM_ID (AREF (eol_type, 0));
5498 eol_type = Qunix;
5499 }
5500 else if (eol_seen & EOL_SEEN_CRLF)
5501 {
5502 coding->id = CODING_SYSTEM_ID (AREF (eol_type, 1));
5503 eol_type = Qdos;
5504 }
5505 else if (eol_seen & EOL_SEEN_CR)
5506 {
5507 coding->id = CODING_SYSTEM_ID (AREF (eol_type, 2));
5508 eol_type = Qmac;
5509 }
5510 return eol_type;
5511}
5512
5513/* Detect how a text specified in CODING is encoded. If a coding
5514 system is detected, update fields of CODING by the detected coding
5515 system. */
5516
5517void
5518detect_coding (coding)
5519 struct coding_system *coding;
5520{
5521 const unsigned char *src, *src_end;
5522
5523 coding->consumed = coding->consumed_char = 0;
5524 coding->produced = coding->produced_char = 0;
5525 coding_set_source (coding);
5526
5527 src_end = coding->source + coding->src_bytes;
5528
5529 /* If we have not yet decided the text encoding type, detect it
5530 now. */
5531 if (EQ (CODING_ATTR_TYPE (CODING_ID_ATTRS (coding->id)), Qundecided))
5532 {
5533 int c, i;
5534 struct coding_detection_info detect_info;
5535
5536 detect_info.checked = detect_info.found = detect_info.rejected = 0;
5537 for (i = 0, src = coding->source; src < src_end; i++, src++)
5538 {
5539 c = *src;
5540 if (c & 0x80)
5541 break;
5542 if (c < 0x20
5543 && (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO)
5544 && ! inhibit_iso_escape_detection
5545 && ! detect_info.checked)
5546 {
5547 coding->head_ascii = src - (coding->source + coding->consumed);
5548 if (detect_coding_iso_2022 (coding, &detect_info))
5549 {
5550 /* We have scanned the whole data. */
5551 if (! (detect_info.rejected & CATEGORY_MASK_ISO_7_ELSE))
5552 /* We didn't find an 8-bit code. */
5553 src = src_end;
5554 break;
5555 }
5556 }
5557 }
5558 coding->head_ascii = src - (coding->source + coding->consumed);
5559
5560 if (coding->head_ascii < coding->src_bytes
5561 || detect_info.found)
5562 {
5563 enum coding_category category;
5564 struct coding_system *this;
5565
5566 if (coding->head_ascii == coding->src_bytes)
5567 /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */
5568 for (i = 0; i < coding_category_raw_text; i++)
5569 {
5570 category = coding_priorities[i];
5571 this = coding_categories + category;
5572 if (detect_info.found & (1 << category))
5573 break;
5574 }
5575 else
5576 for (i = 0; i < coding_category_raw_text; i++)
5577 {
5578 category = coding_priorities[i];
5579 this = coding_categories + category;
5580 if (this->id < 0)
5581 {
5582 /* No coding system of this category is defined. */
5583 detect_info.rejected |= (1 << category);
5584 }
5585 else if (category >= coding_category_raw_text)
5586 continue;
5587 else if (detect_info.checked & (1 << category))
5588 {
5589 if (detect_info.found & (1 << category))
5590 break;
5591 }
5592 else if ((*(this->detector)) (coding, &detect_info)
5593 && detect_info.found & (1 << category))
5594 {
5595 if (category == coding_category_utf_16_auto)
5596 {
5597 if (detect_info.found & CATEGORY_MASK_UTF_16_LE)
5598 category = coding_category_utf_16_le;
5599 else
5600 category = coding_category_utf_16_be;
5601 }
5602 break;
5603 }
5604 }
5605
5606 if (i < coding_category_raw_text)
5607 setup_coding_system (CODING_ID_NAME (this->id), coding);
5608 else if (detect_info.rejected == CATEGORY_MASK_ANY)
5609 setup_coding_system (Qraw_text, coding);
5610 else if (detect_info.rejected)
5611 for (i = 0; i < coding_category_raw_text; i++)
5612 if (! (detect_info.rejected & (1 << coding_priorities[i])))
5613 {
5614 this = coding_categories + coding_priorities[i];
5615 setup_coding_system (CODING_ID_NAME (this->id), coding);
5616 break;
5617 }
5618 }
5619 }
5620 else if (XINT (CODING_ATTR_CATEGORY (CODING_ID_ATTRS (coding->id)))
5621 == coding_category_utf_16_auto)
5622 {
5623 Lisp_Object coding_systems;
5624 struct coding_detection_info detect_info;
5625
5626 coding_systems
5627 = AREF (CODING_ID_ATTRS (coding->id), coding_attr_utf_16_bom);
5628 detect_info.found = detect_info.rejected = 0;
5629 if (CONSP (coding_systems)
5630 && detect_coding_utf_16 (coding, &detect_info))
5631 {
5632 if (detect_info.found & CATEGORY_MASK_UTF_16_LE)
5633 setup_coding_system (XCAR (coding_systems), coding);
5634 else if (detect_info.found & CATEGORY_MASK_UTF_16_BE)
5635 setup_coding_system (XCDR (coding_systems), coding);
5636 }
5637 }
5638}
5639
5640
5641static void
5642decode_eol (coding)
5643 struct coding_system *coding;
5644{
5645 Lisp_Object eol_type;
5646 unsigned char *p, *pbeg, *pend;
5647
5648 eol_type = CODING_ID_EOL_TYPE (coding->id);
5649 if (EQ (eol_type, Qunix))
5650 return;
5651
5652 if (NILP (coding->dst_object))
5653 pbeg = coding->destination;
5654 else
5655 pbeg = BYTE_POS_ADDR (coding->dst_pos_byte);
5656 pend = pbeg + coding->produced;
5657
5658 if (VECTORP (eol_type))
5659 {
5660 int eol_seen = EOL_SEEN_NONE;
5661
5662 for (p = pbeg; p < pend; p++)
5663 {
5664 if (*p == '\n')
5665 eol_seen |= EOL_SEEN_LF;
5666 else if (*p == '\r')
5667 {
5668 if (p + 1 < pend && *(p + 1) == '\n')
5669 {
5670 eol_seen |= EOL_SEEN_CRLF;
5671 p++;
5672 }
5673 else
5674 eol_seen |= EOL_SEEN_CR;
5675 }
5676 }
5677 if (eol_seen != EOL_SEEN_NONE
5678 && eol_seen != EOL_SEEN_LF
5679 && eol_seen != EOL_SEEN_CRLF
5680 && eol_seen != EOL_SEEN_CR)
5681 eol_seen = EOL_SEEN_LF;
5682 if (eol_seen != EOL_SEEN_NONE)
5683 eol_type = adjust_coding_eol_type (coding, eol_seen);
5684 }
5685
5686 if (EQ (eol_type, Qmac))
5687 {
5688 for (p = pbeg; p < pend; p++)
5689 if (*p == '\r')
5690 *p = '\n';
5691 }
5692 else if (EQ (eol_type, Qdos))
5693 {
5694 int n = 0;
5695
5696 if (NILP (coding->dst_object))
5697 {
5698 /* Start deleting '\r' from the tail to minimize the memory
5699 movement. */
5700 for (p = pend - 2; p >= pbeg; p--)
5701 if (*p == '\r')
5702 {
5703 safe_bcopy ((char *) (p + 1), (char *) p, pend-- - p - 1);
5704 n++;
5705 }
5706 }
5707 else
5708 {
5709 int pos_byte = coding->dst_pos_byte;
5710 int pos = coding->dst_pos;
5711 int pos_end = pos + coding->produced_char - 1;
5712
5713 while (pos < pos_end)
5714 {
5715 p = BYTE_POS_ADDR (pos_byte);
5716 if (*p == '\r' && p[1] == '\n')
5717 {
5718 del_range_2 (pos, pos_byte, pos + 1, pos_byte + 1, 0);
5719 n++;
5720 pos_end--;
5721 }
5722 pos++;
5723 pos_byte += BYTES_BY_CHAR_HEAD (*p);
5724 }
5725 }
5726 coding->produced -= n;
5727 coding->produced_char -= n;
5728 }
5729}
5730
5731
5732/* Return a translation table (or list of them) from coding system
5733 attribute vector ATTRS for encoding (ENCODEP is nonzero) or
5734 decoding (ENCODEP is zero). */
5735
5736static Lisp_Object
5737get_translation_table (attrs, encodep, max_lookup)
5738 Lisp_Object attrs;
5739 int encodep, *max_lookup;
5740{
5741 Lisp_Object standard, translation_table;
5742 Lisp_Object val;
5743
5744 if (encodep)
5745 translation_table = CODING_ATTR_ENCODE_TBL (attrs),
5746 standard = Vstandard_translation_table_for_encode;
5747 else
5748 translation_table = CODING_ATTR_DECODE_TBL (attrs),
5749 standard = Vstandard_translation_table_for_decode;
5750 if (NILP (translation_table))
5751 translation_table = standard;
5752 else
5753 {
5754 if (SYMBOLP (translation_table))
5755 translation_table = Fget (translation_table, Qtranslation_table);
5756 else if (CONSP (translation_table))
5757 {
5758 translation_table = Fcopy_sequence (translation_table);
5759 for (val = translation_table; CONSP (val); val = XCDR (val))
5760 if (SYMBOLP (XCAR (val)))
5761 XSETCAR (val, Fget (XCAR (val), Qtranslation_table));
5762 }
5763 if (CHAR_TABLE_P (standard))
5764 {
5765 if (CONSP (translation_table))
5766 translation_table = nconc2 (translation_table,
5767 Fcons (standard, Qnil));
5768 else
5769 translation_table = Fcons (translation_table,
5770 Fcons (standard, Qnil));
5771 }
5772 }
5773
5774 if (max_lookup)
5775 {
5776 *max_lookup = 1;
5777 if (CHAR_TABLE_P (translation_table)
5778 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (translation_table)) > 1)
5779 {
5780 val = XCHAR_TABLE (translation_table)->extras[1];
5781 if (NATNUMP (val) && *max_lookup < XFASTINT (val))
5782 *max_lookup = XFASTINT (val);
5783 }
5784 else if (CONSP (translation_table))
5785 {
5786 Lisp_Object tail, val;
5787
5788 for (tail = translation_table; CONSP (tail); tail = XCDR (tail))
5789 if (CHAR_TABLE_P (XCAR (tail))
5790 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (XCAR (tail))) > 1)
5791 {
5792 val = XCHAR_TABLE (XCAR (tail))->extras[1];
5793 if (NATNUMP (val) && *max_lookup < XFASTINT (val))
5794 *max_lookup = XFASTINT (val);
5795 }
5796 }
5797 }
5798 return translation_table;
5799}
5800
5801#define LOOKUP_TRANSLATION_TABLE(table, c, trans) \
5802 do { \
5803 trans = Qnil; \
5804 if (CHAR_TABLE_P (table)) \
5805 { \
5806 trans = CHAR_TABLE_REF (table, c); \
5807 if (CHARACTERP (trans)) \
5808 c = XFASTINT (trans), trans = Qnil; \
5809 } \
5810 else if (CONSP (table)) \
5811 { \
5812 Lisp_Object tail; \
5813 \
5814 for (tail = table; CONSP (tail); tail = XCDR (tail)) \
5815 if (CHAR_TABLE_P (XCAR (tail))) \
5816 { \
5817 trans = CHAR_TABLE_REF (XCAR (tail), c); \
5818 if (CHARACTERP (trans)) \
5819 c = XFASTINT (trans), trans = Qnil; \
5820 else if (! NILP (trans)) \
5821 break; \
5822 } \
5823 } \
5824 } while (0)
5825
5826
5827static Lisp_Object
5828get_translation (val, buf, buf_end, last_block, from_nchars, to_nchars)
5829 Lisp_Object val;
5830 int *buf, *buf_end;
5831 int last_block;
5832 int *from_nchars, *to_nchars;
5833{
5834 /* VAL is TO or (([FROM-CHAR ...] . TO) ...) where TO is TO-CHAR or
5835 [TO-CHAR ...]. */
5836 if (CONSP (val))
5837 {
5838 Lisp_Object from, tail;
5839 int i, len;
5840
5841 for (tail = val; CONSP (tail); tail = XCDR (tail))
5842 {
5843 val = XCAR (tail);
5844 from = XCAR (val);
5845 len = ASIZE (from);
5846 for (i = 0; i < len; i++)
5847 {
5848 if (buf + i == buf_end)
5849 {
5850 if (! last_block)
5851 return Qt;
5852 break;
5853 }
5854 if (XINT (AREF (from, i)) != buf[i])
5855 break;
5856 }
5857 if (i == len)
5858 {
5859 val = XCDR (val);
5860 *from_nchars = len;
5861 break;
5862 }
5863 }
5864 if (! CONSP (tail))
5865 return Qnil;
5866 }
5867 if (VECTORP (val))
5868 *buf = XINT (AREF (val, 0)), *to_nchars = ASIZE (val);
5869 else
5870 *buf = XINT (val);
5871 return val;
5872}
5873
5874
5875static int
5876produce_chars (coding, translation_table, last_block)
5877 struct coding_system *coding;
5878 Lisp_Object translation_table;
5879 int last_block;
5880{
5881 unsigned char *dst = coding->destination + coding->produced;
5882 unsigned char *dst_end = coding->destination + coding->dst_bytes;
5883 int produced;
5884 int produced_chars = 0;
5885 int carryover = 0;
5886
5887 if (! coding->chars_at_source)
5888 {
5889 /* Characters are in coding->charbuf. */
5890 int *buf = coding->charbuf;
5891 int *buf_end = buf + coding->charbuf_used;
5892
5893 if (BUFFERP (coding->src_object)
5894 && EQ (coding->src_object, coding->dst_object))
5895 dst_end = ((unsigned char *) coding->source) + coding->consumed;
5896
5897 while (buf < buf_end)
5898 {
5899 int c = *buf, i;
5900
5901 if (c >= 0)
5902 {
5903 int from_nchars = 1, to_nchars = 1;
5904 Lisp_Object trans = Qnil;
5905
5906 LOOKUP_TRANSLATION_TABLE (translation_table, c, trans);
5907 if (! NILP (trans))
5908 {
5909 trans = get_translation (trans, buf, buf_end, last_block,
5910 &from_nchars, &to_nchars);
5911 if (EQ (trans, Qt))
5912 break;
5913 c = *buf;
5914 }
5915
5916 if (dst + MAX_MULTIBYTE_LENGTH * to_nchars > dst_end)
5917 {
5918 dst = alloc_destination (coding,
5919 buf_end - buf
5920 + MAX_MULTIBYTE_LENGTH * to_nchars,
5921 dst);
5922 dst_end = coding->destination + coding->dst_bytes;
5923 }
5924
5925 for (i = 0; i < to_nchars; i++)
5926 {
5927 if (i > 0)
5928 c = XINT (AREF (trans, i));
5929 if (coding->dst_multibyte
5930 || ! CHAR_BYTE8_P (c))
5931 CHAR_STRING_ADVANCE (c, dst);
5932 else
5933 *dst++ = CHAR_TO_BYTE8 (c);
5934 }
5935 produced_chars += to_nchars;
5936 *buf++ = to_nchars;
5937 while (--from_nchars > 0)
5938 *buf++ = 0;
5939 }
5940 else
5941 /* This is an annotation datum. (-C) is the length. */
5942 buf += -c;
5943 }
5944 carryover = buf_end - buf;
5945 }
5946 else
5947 {
5948 const unsigned char *src = coding->source;
5949 const unsigned char *src_end = src + coding->src_bytes;
5950 Lisp_Object eol_type;
5951
5952 eol_type = CODING_ID_EOL_TYPE (coding->id);
5953
5954 if (coding->src_multibyte != coding->dst_multibyte)
5955 {
5956 if (coding->src_multibyte)
5957 {
5958 int multibytep = 1;
5959 int consumed_chars;
5960
5961 while (1)
5962 {
5963 const unsigned char *src_base = src;
5964 int c;
5965
5966 ONE_MORE_BYTE (c);
5967 if (c == '\r')
5968 {
5969 if (EQ (eol_type, Qdos))
5970 {
5971 if (src == src_end)
5972 {
5973 record_conversion_result
5974 (coding, CODING_RESULT_INSUFFICIENT_SRC);
5975 goto no_more_source;
5976 }
5977 if (*src == '\n')
5978 c = *src++;
5979 }
5980 else if (EQ (eol_type, Qmac))
5981 c = '\n';
5982 }
5983 if (dst == dst_end)
5984 {
5985 coding->consumed = src - coding->source;
5986
5987 if (EQ (coding->src_object, coding->dst_object))
5988 dst_end = (unsigned char *) src;
5989 if (dst == dst_end)
5990 {
5991 dst = alloc_destination (coding, src_end - src + 1,
5992 dst);
5993 dst_end = coding->destination + coding->dst_bytes;
5994 coding_set_source (coding);
5995 src = coding->source + coding->consumed;
5996 src_end = coding->source + coding->src_bytes;
5997 }
5998 }
5999 *dst++ = c;
6000 produced_chars++;
6001 }
6002 no_more_source:
6003 ;
6004 }
6005 else
6006 while (src < src_end)
6007 {
6008 int multibytep = 1;
6009 int c = *src++;
6010
6011 if (c == '\r')
6012 {
6013 if (EQ (eol_type, Qdos))
6014 {
6015 if (src < src_end
6016 && *src == '\n')
6017 c = *src++;
6018 }
6019 else if (EQ (eol_type, Qmac))
6020 c = '\n';
6021 }
6022 if (dst >= dst_end - 1)
6023 {
6024 coding->consumed = src - coding->source;
6025
6026 if (EQ (coding->src_object, coding->dst_object))
6027 dst_end = (unsigned char *) src;
6028 if (dst >= dst_end - 1)
6029 {
6030 dst = alloc_destination (coding, src_end - src + 2,
6031 dst);
6032 dst_end = coding->destination + coding->dst_bytes;
6033 coding_set_source (coding);
6034 src = coding->source + coding->consumed;
6035 src_end = coding->source + coding->src_bytes;
6036 }
6037 }
6038 EMIT_ONE_BYTE (c);
6039 }
6040 }
6041 else
6042 {
6043 if (!EQ (coding->src_object, coding->dst_object))
6044 {
6045 int require = coding->src_bytes - coding->dst_bytes;
6046
6047 if (require > 0)
6048 {
6049 EMACS_INT offset = src - coding->source;
6050
6051 dst = alloc_destination (coding, require, dst);
6052 coding_set_source (coding);
6053 src = coding->source + offset;
6054 src_end = coding->source + coding->src_bytes;
6055 }
6056 }
6057 produced_chars = coding->src_chars;
6058 while (src < src_end)
6059 {
6060 int c = *src++;
6061
6062 if (c == '\r')
6063 {
6064 if (EQ (eol_type, Qdos))
6065 {
6066 if (src < src_end
6067 && *src == '\n')
6068 c = *src++;
6069 produced_chars--;
6070 }
6071 else if (EQ (eol_type, Qmac))
6072 c = '\n';
6073 }
6074 *dst++ = c;
6075 }
6076 }
6077 coding->consumed = coding->src_bytes;
6078 coding->consumed_char = coding->src_chars;
6079 }
6080
6081 produced = dst - (coding->destination + coding->produced);
6082 if (BUFFERP (coding->dst_object) && produced_chars > 0)
6083 insert_from_gap (produced_chars, produced);
6084 coding->produced += produced;
6085 coding->produced_char += produced_chars;
6086 return carryover;
6087}
6088
6089/* Compose text in CODING->object according to the annotation data at
6090 CHARBUF. CHARBUF is an array:
6091 [ -LENGTH ANNOTATION_MASK FROM TO METHOD COMP_LEN [ COMPONENTS... ] ]
6092 */
6093
6094static INLINE void
6095produce_composition (coding, charbuf, pos)
6096 struct coding_system *coding;
6097 int *charbuf;
6098 EMACS_INT pos;
6099{
6100 int len;
6101 EMACS_INT to;
6102 enum composition_method method;
6103 Lisp_Object components;
6104
6105 len = -charbuf[0];
6106 to = pos + charbuf[2];
6107 if (to <= pos)
6108 return;
6109 method = (enum composition_method) (charbuf[3]);
6110
6111 if (method == COMPOSITION_RELATIVE)
6112 components = Qnil;
6113 else if (method >= COMPOSITION_WITH_RULE
6114 && method <= COMPOSITION_WITH_RULE_ALTCHARS)
6115 {
6116 Lisp_Object args[MAX_COMPOSITION_COMPONENTS * 2 - 1];
6117 int i;
6118
6119 len -= 4;
6120 charbuf += 4;
6121 for (i = 0; i < len; i++)
6122 {
6123 args[i] = make_number (charbuf[i]);
6124 if (charbuf[i] < 0)
6125 return;
6126 }
6127 components = (method == COMPOSITION_WITH_ALTCHARS
6128 ? Fstring (len, args) : Fvector (len, args));
6129 }
6130 else
6131 return;
6132 compose_text (pos, to, components, Qnil, coding->dst_object);
6133}
6134
6135
6136/* Put `charset' property on text in CODING->object according to
6137 the annotation data at CHARBUF. CHARBUF is an array:
6138 [ -LENGTH ANNOTATION_MASK NCHARS CHARSET-ID ]
6139 */
6140
6141static INLINE void
6142produce_charset (coding, charbuf, pos)
6143 struct coding_system *coding;
6144 int *charbuf;
6145 EMACS_INT pos;
6146{
6147 EMACS_INT from = pos - charbuf[2];
6148 struct charset *charset = CHARSET_FROM_ID (charbuf[3]);
6149
6150 Fput_text_property (make_number (from), make_number (pos),
6151 Qcharset, CHARSET_NAME (charset),
6152 coding->dst_object);
6153}
6154
6155
6156#define CHARBUF_SIZE 0x4000
6157
6158#define ALLOC_CONVERSION_WORK_AREA(coding) \
6159 do { \
6160 int size = CHARBUF_SIZE;; \
6161 \
6162 coding->charbuf = NULL; \
6163 while (size > 1024) \
6164 { \
6165 coding->charbuf = (int *) alloca (sizeof (int) * size); \
6166 if (coding->charbuf) \
6167 break; \
6168 size >>= 1; \
6169 } \
6170 if (! coding->charbuf) \
6171 { \
6172 record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_MEM); \
6173 return coding->result; \
6174 } \
6175 coding->charbuf_size = size; \
6176 } while (0)
6177
6178
6179static void
6180produce_annotation (coding, pos)
6181 struct coding_system *coding;
6182 EMACS_INT pos;
6183{
6184 int *charbuf = coding->charbuf;
6185 int *charbuf_end = charbuf + coding->charbuf_used;
6186
6187 if (NILP (coding->dst_object))
6188 return;
6189
6190 while (charbuf < charbuf_end)
6191 {
6192 if (*charbuf >= 0)
6193 pos += *charbuf++;
6194 else
6195 {
6196 int len = -*charbuf;
6197 switch (charbuf[1])
6198 {
6199 case CODING_ANNOTATE_COMPOSITION_MASK:
6200 produce_composition (coding, charbuf, pos);
6201 break;
6202 case CODING_ANNOTATE_CHARSET_MASK:
6203 produce_charset (coding, charbuf, pos);
6204 break;
6205 default:
6206 abort ();
6207 }
6208 charbuf += len;
6209 }
6210 }
6211}
6212
6213/* Decode the data at CODING->src_object into CODING->dst_object.
6214 CODING->src_object is a buffer, a string, or nil.
6215 CODING->dst_object is a buffer.
6216
6217 If CODING->src_object is a buffer, it must be the current buffer.
6218 In this case, if CODING->src_pos is positive, it is a position of
6219 the source text in the buffer, otherwise, the source text is in the
6220 gap area of the buffer, and CODING->src_pos specifies the offset of
6221 the text from GPT (which must be the same as PT). If this is the
6222 same buffer as CODING->dst_object, CODING->src_pos must be
6223 negative.
6224
6225 If CODING->src_object is a string, CODING->src_pos is an index to
6226 that string.
6227
6228 If CODING->src_object is nil, CODING->source must already point to
6229 the non-relocatable memory area. In this case, CODING->src_pos is
6230 an offset from CODING->source.
6231
6232 The decoded data is inserted at the current point of the buffer
6233 CODING->dst_object.
6234*/
6235
6236static int
6237decode_coding (coding)
6238 struct coding_system *coding;
6239{
6240 Lisp_Object attrs;
6241 Lisp_Object undo_list;
6242 Lisp_Object translation_table;
6243 int carryover;
6244 int i;
6245
6246 if (BUFFERP (coding->src_object)
6247 && coding->src_pos > 0
6248 && coding->src_pos < GPT
6249 && coding->src_pos + coding->src_chars > GPT)
6250 move_gap_both (coding->src_pos, coding->src_pos_byte);
6251
6252 undo_list = Qt;
6253 if (BUFFERP (coding->dst_object))
6254 {
6255 if (current_buffer != XBUFFER (coding->dst_object))
6256 set_buffer_internal (XBUFFER (coding->dst_object));
6257 if (GPT != PT)
6258 move_gap_both (PT, PT_BYTE);
6259 undo_list = current_buffer->undo_list;
6260 current_buffer->undo_list = Qt;
6261 }
6262
6263 coding->consumed = coding->consumed_char = 0;
6264 coding->produced = coding->produced_char = 0;
6265 coding->chars_at_source = 0;
6266 record_conversion_result (coding, CODING_RESULT_SUCCESS);
6267 coding->errors = 0;
6268
6269 ALLOC_CONVERSION_WORK_AREA (coding);
6270
6271 attrs = CODING_ID_ATTRS (coding->id);
6272 translation_table = get_translation_table (attrs, 0, NULL);
6273
6274 carryover = 0;
6275 do
6276 {
6277 EMACS_INT pos = coding->dst_pos + coding->produced_char;
6278
6279 coding_set_source (coding);
6280 coding->annotated = 0;
6281 coding->charbuf_used = carryover;
6282 (*(coding->decoder)) (coding);
6283 coding_set_destination (coding);
6284 carryover = produce_chars (coding, translation_table, 0);
6285 if (coding->annotated)
6286 produce_annotation (coding, pos);
6287 for (i = 0; i < carryover; i++)
6288 coding->charbuf[i]
6289 = coding->charbuf[coding->charbuf_used - carryover + i];
6290 }
6291 while (coding->consumed < coding->src_bytes
6292 && (coding->result == CODING_RESULT_SUCCESS
6293 || coding->result == CODING_RESULT_INVALID_SRC));
6294
6295 if (carryover > 0)
6296 {
6297 coding_set_destination (coding);
6298 coding->charbuf_used = carryover;
6299 produce_chars (coding, translation_table, 1);
6300 }
6301
6302 coding->carryover_bytes = 0;
6303 if (coding->consumed < coding->src_bytes)
6304 {
6305 int nbytes = coding->src_bytes - coding->consumed;
6306 const unsigned char *src;
6307
6308 coding_set_source (coding);
6309 coding_set_destination (coding);
6310 src = coding->source + coding->consumed;
6311
6312 if (coding->mode & CODING_MODE_LAST_BLOCK)
6313 {
6314 /* Flush out unprocessed data as binary chars. We are sure
6315 that the number of data is less than the size of
6316 coding->charbuf. */
6317 coding->charbuf_used = 0;
6318 while (nbytes-- > 0)
6319 {
6320 int c = *src++;
6321
6322 if (c & 0x80)
6323 c = BYTE8_TO_CHAR (c);
6324 coding->charbuf[coding->charbuf_used++] = c;
6325 }
6326 produce_chars (coding, Qnil, 1);
6327 }
6328 else
6329 {
6330 /* Record unprocessed bytes in coding->carryover. We are
6331 sure that the number of data is less than the size of
6332 coding->carryover. */
6333 unsigned char *p = coding->carryover;
6334
6335 coding->carryover_bytes = nbytes;
6336 while (nbytes-- > 0)
6337 *p++ = *src++;
6338 }
6339 coding->consumed = coding->src_bytes;
6340 }
6341
6342 if (! EQ (CODING_ID_EOL_TYPE (coding->id), Qunix))
6343 decode_eol (coding);
6344 if (BUFFERP (coding->dst_object))
6345 {
6346 current_buffer->undo_list = undo_list;
6347 record_insert (coding->dst_pos, coding->produced_char);
6348 }
6349 return coding->result;
6350}
6351
6352
6353/* Extract an annotation datum from a composition starting at POS and
6354 ending before LIMIT of CODING->src_object (buffer or string), store
6355 the data in BUF, set *STOP to a starting position of the next
6356 composition (if any) or to LIMIT, and return the address of the
6357 next element of BUF.
6358
6359 If such an annotation is not found, set *STOP to a starting
6360 position of a composition after POS (if any) or to LIMIT, and
6361 return BUF. */
6362
6363static INLINE int *
6364handle_composition_annotation (pos, limit, coding, buf, stop)
6365 EMACS_INT pos, limit;
6366 struct coding_system *coding;
6367 int *buf;
6368 EMACS_INT *stop;
6369{
6370 EMACS_INT start, end;
6371 Lisp_Object prop;
6372
6373 if (! find_composition (pos, limit, &start, &end, &prop, coding->src_object)
6374 || end > limit)
6375 *stop = limit;
6376 else if (start > pos)
6377 *stop = start;
6378 else
6379 {
6380 if (start == pos)
6381 {
6382 /* We found a composition. Store the corresponding
6383 annotation data in BUF. */
6384 int *head = buf;
6385 enum composition_method method = COMPOSITION_METHOD (prop);
6386 int nchars = COMPOSITION_LENGTH (prop);
6387
6388 ADD_COMPOSITION_DATA (buf, nchars, method);
6389 if (method != COMPOSITION_RELATIVE)
6390 {
6391 Lisp_Object components;
6392 int len, i, i_byte;
6393
6394 components = COMPOSITION_COMPONENTS (prop);
6395 if (VECTORP (components))
6396 {
6397 len = XVECTOR (components)->size;
6398 for (i = 0; i < len; i++)
6399 *buf++ = XINT (AREF (components, i));
6400 }
6401 else if (STRINGP (components))
6402 {
6403 len = SCHARS (components);
6404 i = i_byte = 0;
6405 while (i < len)
6406 {
6407 FETCH_STRING_CHAR_ADVANCE (*buf, components, i, i_byte);
6408 buf++;
6409 }
6410 }
6411 else if (INTEGERP (components))
6412 {
6413 len = 1;
6414 *buf++ = XINT (components);
6415 }
6416 else if (CONSP (components))
6417 {
6418 for (len = 0; CONSP (components);
6419 len++, components = XCDR (components))
6420 *buf++ = XINT (XCAR (components));
6421 }
6422 else
6423 abort ();
6424 *head -= len;
6425 }
6426 }
6427
6428 if (find_composition (end, limit, &start, &end, &prop,
6429 coding->src_object)
6430 && end <= limit)
6431 *stop = start;
6432 else
6433 *stop = limit;
6434 }
6435 return buf;
6436}
6437
6438
6439/* Extract an annotation datum from a text property `charset' at POS of
6440 CODING->src_object (buffer of string), store the data in BUF, set
6441 *STOP to the position where the value of `charset' property changes
6442 (limiting by LIMIT), and return the address of the next element of
6443 BUF.
6444
6445 If the property value is nil, set *STOP to the position where the
6446 property value is non-nil (limiting by LIMIT), and return BUF. */
6447
6448static INLINE int *
6449handle_charset_annotation (pos, limit, coding, buf, stop)
6450 EMACS_INT pos, limit;
6451 struct coding_system *coding;
6452 int *buf;
6453 EMACS_INT *stop;
6454{
6455 Lisp_Object val, next;
6456 int id;
6457
6458 val = Fget_text_property (make_number (pos), Qcharset, coding->src_object);
6459 if (! NILP (val) && CHARSETP (val))
6460 id = XINT (CHARSET_SYMBOL_ID (val));
6461 else
6462 id = -1;
6463 ADD_CHARSET_DATA (buf, 0, id);
6464 next = Fnext_single_property_change (make_number (pos), Qcharset,
6465 coding->src_object,
6466 make_number (limit));
6467 *stop = XINT (next);
6468 return buf;
6469}
6470
6471
6472static void
6473consume_chars (coding, translation_table, max_lookup)
6474 struct coding_system *coding;
6475 Lisp_Object translation_table;
6476 int max_lookup;
6477{
6478 int *buf = coding->charbuf;
6479 int *buf_end = coding->charbuf + coding->charbuf_size;
6480 const unsigned char *src = coding->source + coding->consumed;
6481 const unsigned char *src_end = coding->source + coding->src_bytes;
6482 EMACS_INT pos = coding->src_pos + coding->consumed_char;
6483 EMACS_INT end_pos = coding->src_pos + coding->src_chars;
6484 int multibytep = coding->src_multibyte;
6485 Lisp_Object eol_type;
6486 int c;
6487 EMACS_INT stop, stop_composition, stop_charset;
6488 int *lookup_buf = NULL;
6489
6490 if (! NILP (translation_table))
6491 lookup_buf = alloca (sizeof (int) * max_lookup);
6492
6493 eol_type = CODING_ID_EOL_TYPE (coding->id);
6494 if (VECTORP (eol_type))
6495 eol_type = Qunix;
6496
6497 /* Note: composition handling is not yet implemented. */
6498 coding->common_flags &= ~CODING_ANNOTATE_COMPOSITION_MASK;
6499
6500 if (NILP (coding->src_object))
6501 stop = stop_composition = stop_charset = end_pos;
6502 else
6503 {
6504 if (coding->common_flags & CODING_ANNOTATE_COMPOSITION_MASK)
6505 stop = stop_composition = pos;
6506 else
6507 stop = stop_composition = end_pos;
6508 if (coding->common_flags & CODING_ANNOTATE_CHARSET_MASK)
6509 stop = stop_charset = pos;
6510 else
6511 stop_charset = end_pos;
6512 }
6513
6514 /* Compensate for CRLF and conversion. */
6515 buf_end -= 1 + MAX_ANNOTATION_LENGTH;
6516 while (buf < buf_end)
6517 {
6518 Lisp_Object trans;
6519
6520 if (pos == stop)
6521 {
6522 if (pos == end_pos)
6523 break;
6524 if (pos == stop_composition)
6525 buf = handle_composition_annotation (pos, end_pos, coding,
6526 buf, &stop_composition);
6527 if (pos == stop_charset)
6528 buf = handle_charset_annotation (pos, end_pos, coding,
6529 buf, &stop_charset);
6530 stop = (stop_composition < stop_charset
6531 ? stop_composition : stop_charset);
6532 }
6533
6534 if (! multibytep)
6535 {
6536 EMACS_INT bytes;
6537
6538 if (coding->encoder == encode_coding_raw_text)
6539 c = *src++, pos++;
6540 else if ((bytes = MULTIBYTE_LENGTH (src, src_end)) > 0)
6541 c = STRING_CHAR_ADVANCE (src), pos += bytes;
6542 else
6543 c = BYTE8_TO_CHAR (*src), src++, pos++;
6544 }
6545 else
6546 c = STRING_CHAR_ADVANCE (src), pos++;
6547 if ((c == '\r') && (coding->mode & CODING_MODE_SELECTIVE_DISPLAY))
6548 c = '\n';
6549 if (! EQ (eol_type, Qunix))
6550 {
6551 if (c == '\n')
6552 {
6553 if (EQ (eol_type, Qdos))
6554 *buf++ = '\r';
6555 else
6556 c = '\r';
6557 }
6558 }
6559
6560 trans = Qnil;
6561 LOOKUP_TRANSLATION_TABLE (translation_table, c, trans);
6562 if (NILP (trans))
6563 *buf++ = c;
6564 else
6565 {
6566 int from_nchars = 1, to_nchars = 1;
6567 int *lookup_buf_end;
6568 const unsigned char *p = src;
6569 int i;
6570
6571 lookup_buf[0] = c;
6572 for (i = 1; i < max_lookup && p < src_end; i++)
6573 lookup_buf[i] = STRING_CHAR_ADVANCE (p);
6574 lookup_buf_end = lookup_buf + i;
6575 trans = get_translation (trans, lookup_buf, lookup_buf_end, 1,
6576 &from_nchars, &to_nchars);
6577 if (EQ (trans, Qt)
6578 || buf + to_nchars > buf_end)
6579 break;
6580 *buf++ = *lookup_buf;
6581 for (i = 1; i < to_nchars; i++)
6582 *buf++ = XINT (AREF (trans, i));
6583 for (i = 1; i < from_nchars; i++, pos++)
6584 src += MULTIBYTE_LENGTH_NO_CHECK (src);
6585 }
6586 }
6587
6588 coding->consumed = src - coding->source;
6589 coding->consumed_char = pos - coding->src_pos;
6590 coding->charbuf_used = buf - coding->charbuf;
6591 coding->chars_at_source = 0;
6592}
6593
6594
6595/* Encode the text at CODING->src_object into CODING->dst_object.
6596 CODING->src_object is a buffer or a string.
6597 CODING->dst_object is a buffer or nil.
6598
6599 If CODING->src_object is a buffer, it must be the current buffer.
6600 In this case, if CODING->src_pos is positive, it is a position of
6601 the source text in the buffer, otherwise. the source text is in the
6602 gap area of the buffer, and coding->src_pos specifies the offset of
6603 the text from GPT (which must be the same as PT). If this is the
6604 same buffer as CODING->dst_object, CODING->src_pos must be
6605 negative and CODING should not have `pre-write-conversion'.
6606
6607 If CODING->src_object is a string, CODING should not have
6608 `pre-write-conversion'.
6609
6610 If CODING->dst_object is a buffer, the encoded data is inserted at
6611 the current point of that buffer.
6612
6613 If CODING->dst_object is nil, the encoded data is placed at the
6614 memory area specified by CODING->destination. */
6615
6616static int
6617encode_coding (coding)
6618 struct coding_system *coding;
6619{
6620 Lisp_Object attrs;
6621 Lisp_Object translation_table;
6622 int max_lookup;
6623
6624 attrs = CODING_ID_ATTRS (coding->id);
6625 if (coding->encoder == encode_coding_raw_text)
6626 translation_table = Qnil, max_lookup = 0;
6627 else
6628 translation_table = get_translation_table (attrs, 1, &max_lookup);
6629
6630 if (BUFFERP (coding->dst_object))
6631 {
6632 set_buffer_internal (XBUFFER (coding->dst_object));
6633 coding->dst_multibyte
6634 = ! NILP (current_buffer->enable_multibyte_characters);
6635 }
6636
6637 coding->consumed = coding->consumed_char = 0;
6638 coding->produced = coding->produced_char = 0;
6639 record_conversion_result (coding, CODING_RESULT_SUCCESS);
6640 coding->errors = 0;
6641
6642 ALLOC_CONVERSION_WORK_AREA (coding);
6643
6644 do {
6645 coding_set_source (coding);
6646 consume_chars (coding, translation_table, max_lookup);
6647 coding_set_destination (coding);
6648 (*(coding->encoder)) (coding);
6649 } while (coding->consumed_char < coding->src_chars);
6650
6651 if (BUFFERP (coding->dst_object) && coding->produced_char > 0)
6652 insert_from_gap (coding->produced_char, coding->produced);
6653
6654 return (coding->result);
6655}
6656
6657
6658/* Name (or base name) of work buffer for code conversion. */
6659static Lisp_Object Vcode_conversion_workbuf_name;
6660
6661/* A working buffer used by the top level conversion. Once it is
6662 created, it is never destroyed. It has the name
6663 Vcode_conversion_workbuf_name. The other working buffers are
6664 destroyed after the use is finished, and their names are modified
6665 versions of Vcode_conversion_workbuf_name. */
6666static Lisp_Object Vcode_conversion_reused_workbuf;
6667
6668/* 1 iff Vcode_conversion_reused_workbuf is already in use. */
6669static int reused_workbuf_in_use;
6670
6671
6672/* Return a working buffer of code convesion. MULTIBYTE specifies the
6673 multibyteness of returning buffer. */
6674
6675static Lisp_Object
6676make_conversion_work_buffer (multibyte)
6677 int multibyte;
6678{
6679 Lisp_Object name, workbuf;
6680 struct buffer *current;
6681
6682 if (reused_workbuf_in_use++)
6683 {
6684 name = Fgenerate_new_buffer_name (Vcode_conversion_workbuf_name, Qnil);
6685 workbuf = Fget_buffer_create (name);
6686 }
6687 else
6688 {
6689 name = Vcode_conversion_workbuf_name;
6690 workbuf = Fget_buffer_create (name);
6691 if (NILP (Vcode_conversion_reused_workbuf))
6692 Vcode_conversion_reused_workbuf = workbuf;
6693 }
6694 current = current_buffer;
6695 set_buffer_internal (XBUFFER (workbuf));
6696 Ferase_buffer ();
6697 current_buffer->undo_list = Qt;
6698 current_buffer->enable_multibyte_characters = multibyte ? Qt : Qnil;
6699 set_buffer_internal (current);
6700 return workbuf;
6701}
6702
6703
6704static Lisp_Object
6705code_conversion_restore (arg)
6706 Lisp_Object arg;
6707{
6708 Lisp_Object current, workbuf;
6709 struct gcpro gcpro1;
6710
6711 GCPRO1 (arg);
6712 current = XCAR (arg);
6713 workbuf = XCDR (arg);
6714 if (! NILP (workbuf))
6715 {
6716 if (EQ (workbuf, Vcode_conversion_reused_workbuf))
6717 reused_workbuf_in_use = 0;
6718 else if (! NILP (Fbuffer_live_p (workbuf)))
6719 Fkill_buffer (workbuf);
6720 }
6721 set_buffer_internal (XBUFFER (current));
6722 UNGCPRO;
6723 return Qnil;
6724}
6725
6726Lisp_Object
6727code_conversion_save (with_work_buf, multibyte)
6728 int with_work_buf, multibyte;
6729{
6730 Lisp_Object workbuf = Qnil;
6731
6732 if (with_work_buf)
6733 workbuf = make_conversion_work_buffer (multibyte);
6734 record_unwind_protect (code_conversion_restore,
6735 Fcons (Fcurrent_buffer (), workbuf));
6736 return workbuf;
6737}
6738
6739int
6740decode_coding_gap (coding, chars, bytes)
6741 struct coding_system *coding;
6742 EMACS_INT chars, bytes;
6743{
6744 int count = specpdl_ptr - specpdl;
6745 Lisp_Object attrs;
6746
6747 code_conversion_save (0, 0);
6748
6749 coding->src_object = Fcurrent_buffer ();
6750 coding->src_chars = chars;
6751 coding->src_bytes = bytes;
6752 coding->src_pos = -chars;
6753 coding->src_pos_byte = -bytes;
6754 coding->src_multibyte = chars < bytes;
6755 coding->dst_object = coding->src_object;
6756 coding->dst_pos = PT;
6757 coding->dst_pos_byte = PT_BYTE;
6758 coding->dst_multibyte = ! NILP (current_buffer->enable_multibyte_characters);
6759
6760 if (CODING_REQUIRE_DETECTION (coding))
6761 detect_coding (coding);
6762
6763 coding->mode |= CODING_MODE_LAST_BLOCK;
6764 current_buffer->text->inhibit_shrinking = 1;
6765 decode_coding (coding);
6766 current_buffer->text->inhibit_shrinking = 0;
6767
6768 attrs = CODING_ID_ATTRS (coding->id);
6769 if (! NILP (CODING_ATTR_POST_READ (attrs)))
6770 {
6771 EMACS_INT prev_Z = Z, prev_Z_BYTE = Z_BYTE;
6772 Lisp_Object val;
6773
6774 TEMP_SET_PT_BOTH (coding->dst_pos, coding->dst_pos_byte);
6775 val = call1 (CODING_ATTR_POST_READ (attrs),
6776 make_number (coding->produced_char));
6777 CHECK_NATNUM (val);
6778 coding->produced_char += Z - prev_Z;
6779 coding->produced += Z_BYTE - prev_Z_BYTE;
6780 }
6781
6782 unbind_to (count, Qnil);
6783 return coding->result;
6784}
6785
6786int
6787encode_coding_gap (coding, chars, bytes)
6788 struct coding_system *coding;
6789 EMACS_INT chars, bytes;
6790{
6791 int count = specpdl_ptr - specpdl;
6792
6793 code_conversion_save (0, 0);
6794
6795 coding->src_object = Fcurrent_buffer ();
6796 coding->src_chars = chars;
6797 coding->src_bytes = bytes;
6798 coding->src_pos = -chars;
6799 coding->src_pos_byte = -bytes;
6800 coding->src_multibyte = chars < bytes;
6801 coding->dst_object = coding->src_object;
6802 coding->dst_pos = PT;
6803 coding->dst_pos_byte = PT_BYTE;
6804
6805 encode_coding (coding);
6806
6807 unbind_to (count, Qnil);
6808 return coding->result;
6809}
6810
6811
6812/* Decode the text in the range FROM/FROM_BYTE and TO/TO_BYTE in
6813 SRC_OBJECT into DST_OBJECT by coding context CODING.
6814
6815 SRC_OBJECT is a buffer, a string, or Qnil.
6816
6817 If it is a buffer, the text is at point of the buffer. FROM and TO
6818 are positions in the buffer.
6819
6820 If it is a string, the text is at the beginning of the string.
6821 FROM and TO are indices to the string.
6822
6823 If it is nil, the text is at coding->source. FROM and TO are
6824 indices to coding->source.
6825
6826 DST_OBJECT is a buffer, Qt, or Qnil.
6827
6828 If it is a buffer, the decoded text is inserted at point of the
6829 buffer. If the buffer is the same as SRC_OBJECT, the source text
6830 is deleted.
6831
6832 If it is Qt, a string is made from the decoded text, and
6833 set in CODING->dst_object.
6834
6835 If it is Qnil, the decoded text is stored at CODING->destination.
6836 The caller must allocate CODING->dst_bytes bytes at
6837 CODING->destination by xmalloc. If the decoded text is longer than
6838 CODING->dst_bytes, CODING->destination is relocated by xrealloc.
6839 */
6840
6841void
6842decode_coding_object (coding, src_object, from, from_byte, to, to_byte,
6843 dst_object)
6844 struct coding_system *coding;
6845 Lisp_Object src_object;
6846 EMACS_INT from, from_byte, to, to_byte;
6847 Lisp_Object dst_object;
6848{
6849 int count = specpdl_ptr - specpdl;
6850 unsigned char *destination;
6851 EMACS_INT dst_bytes;
6852 EMACS_INT chars = to - from;
6853 EMACS_INT bytes = to_byte - from_byte;
6854 Lisp_Object attrs;
6855 Lisp_Object buffer;
6856 int saved_pt = -1, saved_pt_byte;
6857
6858 buffer = Fcurrent_buffer ();
6859
6860 if (NILP (dst_object))
6861 {
6862 destination = coding->destination;
6863 dst_bytes = coding->dst_bytes;
6864 }
6865
6866 coding->src_object = src_object;
6867 coding->src_chars = chars;
6868 coding->src_bytes = bytes;
6869 coding->src_multibyte = chars < bytes;
6870
6871 if (STRINGP (src_object))
6872 {
6873 coding->src_pos = from;
6874 coding->src_pos_byte = from_byte;
6875 }
6876 else if (BUFFERP (src_object))
6877 {
6878 set_buffer_internal (XBUFFER (src_object));
6879 if (from != GPT)
6880 move_gap_both (from, from_byte);
6881 if (EQ (src_object, dst_object))
6882 {
6883 saved_pt = PT, saved_pt_byte = PT_BYTE;
6884 TEMP_SET_PT_BOTH (from, from_byte);
6885 del_range_both (from, from_byte, to, to_byte, 1);
6886 coding->src_pos = -chars;
6887 coding->src_pos_byte = -bytes;
6888 }
6889 else
6890 {
6891 coding->src_pos = from;
6892 coding->src_pos_byte = from_byte;
6893 }
6894 }
6895
6896 if (CODING_REQUIRE_DETECTION (coding))
6897 detect_coding (coding);
6898 attrs = CODING_ID_ATTRS (coding->id);
6899
6900 if (EQ (dst_object, Qt)
6901 || (! NILP (CODING_ATTR_POST_READ (attrs))
6902 && NILP (dst_object)))
6903 {
6904 coding->dst_object = code_conversion_save (1, 1);
6905 coding->dst_pos = BEG;
6906 coding->dst_pos_byte = BEG_BYTE;
6907 coding->dst_multibyte = 1;
6908 }
6909 else if (BUFFERP (dst_object))
6910 {
6911 code_conversion_save (0, 0);
6912 coding->dst_object = dst_object;
6913 coding->dst_pos = BUF_PT (XBUFFER (dst_object));
6914 coding->dst_pos_byte = BUF_PT_BYTE (XBUFFER (dst_object));
6915 coding->dst_multibyte
6916 = ! NILP (XBUFFER (dst_object)->enable_multibyte_characters);
6917 }
6918 else
6919 {
6920 code_conversion_save (0, 0);
6921 coding->dst_object = Qnil;
6922 coding->dst_multibyte = 1;
6923 }
6924
6925 decode_coding (coding);
6926
6927 if (BUFFERP (coding->dst_object))
6928 set_buffer_internal (XBUFFER (coding->dst_object));
6929
6930 if (! NILP (CODING_ATTR_POST_READ (attrs)))
6931 {
6932 struct gcpro gcpro1, gcpro2;
6933 EMACS_INT prev_Z = Z, prev_Z_BYTE = Z_BYTE;
6934 Lisp_Object val;
6935
6936 TEMP_SET_PT_BOTH (coding->dst_pos, coding->dst_pos_byte);
6937 GCPRO2 (coding->src_object, coding->dst_object);
6938 val = safe_call1 (CODING_ATTR_POST_READ (attrs),
6939 make_number (coding->produced_char));
6940 UNGCPRO;
6941 CHECK_NATNUM (val);
6942 coding->produced_char += Z - prev_Z;
6943 coding->produced += Z_BYTE - prev_Z_BYTE;
6944 }
6945
6946 if (EQ (dst_object, Qt))
6947 {
6948 coding->dst_object = Fbuffer_string ();
6949 }
6950 else if (NILP (dst_object) && BUFFERP (coding->dst_object))
6951 {
6952 set_buffer_internal (XBUFFER (coding->dst_object));
6953 if (dst_bytes < coding->produced)
6954 {
6955 destination
6956 = (unsigned char *) xrealloc (destination, coding->produced);
6957 if (! destination)
6958 {
6959 record_conversion_result (coding,
6960 CODING_RESULT_INSUFFICIENT_DST);
6961 unbind_to (count, Qnil);
6962 return;
6963 }
6964 if (BEGV < GPT && GPT < BEGV + coding->produced_char)
6965 move_gap_both (BEGV, BEGV_BYTE);
6966 bcopy (BEGV_ADDR, destination, coding->produced);
6967 coding->destination = destination;
6968 }
6969 }
6970
6971 if (saved_pt >= 0)
6972 {
6973 /* This is the case of:
6974 (BUFFERP (src_object) && EQ (src_object, dst_object))
6975 As we have moved PT while replacing the original buffer
6976 contents, we must recover it now. */
6977 set_buffer_internal (XBUFFER (src_object));
6978 if (saved_pt < from)
6979 TEMP_SET_PT_BOTH (saved_pt, saved_pt_byte);
6980 else if (saved_pt < from + chars)
6981 TEMP_SET_PT_BOTH (from, from_byte);
6982 else if (! NILP (current_buffer->enable_multibyte_characters))
6983 TEMP_SET_PT_BOTH (saved_pt + (coding->produced_char - chars),
6984 saved_pt_byte + (coding->produced - bytes));
6985 else
6986 TEMP_SET_PT_BOTH (saved_pt + (coding->produced - bytes),
6987 saved_pt_byte + (coding->produced - bytes));
6988 }
6989
6990 unbind_to (count, coding->dst_object);
6991}
6992
6993
6994void
6995encode_coding_object (coding, src_object, from, from_byte, to, to_byte,
6996 dst_object)
6997 struct coding_system *coding;
6998 Lisp_Object src_object;
6999 EMACS_INT from, from_byte, to, to_byte;
7000 Lisp_Object dst_object;
7001{
7002 int count = specpdl_ptr - specpdl;
7003 EMACS_INT chars = to - from;
7004 EMACS_INT bytes = to_byte - from_byte;
7005 Lisp_Object attrs;
7006 Lisp_Object buffer;
7007 int saved_pt = -1, saved_pt_byte;
7008 int kill_src_buffer = 0;
7009
7010 buffer = Fcurrent_buffer ();
7011
7012 coding->src_object = src_object;
7013 coding->src_chars = chars;
7014 coding->src_bytes = bytes;
7015 coding->src_multibyte = chars < bytes;
7016
7017 attrs = CODING_ID_ATTRS (coding->id);
7018
7019 if (! NILP (CODING_ATTR_PRE_WRITE (attrs)))
7020 {
7021 coding->src_object = code_conversion_save (1, coding->src_multibyte);
7022 set_buffer_internal (XBUFFER (coding->src_object));
7023 if (STRINGP (src_object))
7024 insert_from_string (src_object, from, from_byte, chars, bytes, 0);
7025 else if (BUFFERP (src_object))
7026 insert_from_buffer (XBUFFER (src_object), from, chars, 0);
7027 else
7028 insert_1_both (coding->source + from, chars, bytes, 0, 0, 0);
7029
7030 if (EQ (src_object, dst_object))
7031 {
7032 set_buffer_internal (XBUFFER (src_object));
7033 saved_pt = PT, saved_pt_byte = PT_BYTE;
7034 del_range_both (from, from_byte, to, to_byte, 1);
7035 set_buffer_internal (XBUFFER (coding->src_object));
7036 }
7037
7038 {
7039 Lisp_Object args[3];
7040
7041 args[0] = CODING_ATTR_PRE_WRITE (attrs);
7042 args[1] = make_number (BEG);
7043 args[2] = make_number (Z);
7044 safe_call (3, args);
7045 }
7046 if (XBUFFER (coding->src_object) != current_buffer)
7047 kill_src_buffer = 1;
7048 coding->src_object = Fcurrent_buffer ();
7049 if (BEG != GPT)
7050 move_gap_both (BEG, BEG_BYTE);
7051 coding->src_chars = Z - BEG;
7052 coding->src_bytes = Z_BYTE - BEG_BYTE;
7053 coding->src_pos = BEG;
7054 coding->src_pos_byte = BEG_BYTE;
7055 coding->src_multibyte = Z < Z_BYTE;
7056 }
7057 else if (STRINGP (src_object))
7058 {
7059 code_conversion_save (0, 0);
7060 coding->src_pos = from;
7061 coding->src_pos_byte = from_byte;
7062 }
7063 else if (BUFFERP (src_object))
7064 {
7065 code_conversion_save (0, 0);
7066 set_buffer_internal (XBUFFER (src_object));
7067 if (EQ (src_object, dst_object))
7068 {
7069 saved_pt = PT, saved_pt_byte = PT_BYTE;
7070 coding->src_object = del_range_1 (from, to, 1, 1);
7071 coding->src_pos = 0;
7072 coding->src_pos_byte = 0;
7073 }
7074 else
7075 {
7076 if (from < GPT && to >= GPT)
7077 move_gap_both (from, from_byte);
7078 coding->src_pos = from;
7079 coding->src_pos_byte = from_byte;
7080 }
7081 }
7082 else
7083 code_conversion_save (0, 0);
7084
7085 if (BUFFERP (dst_object))
7086 {
7087 coding->dst_object = dst_object;
7088 if (EQ (src_object, dst_object))
7089 {
7090 coding->dst_pos = from;
7091 coding->dst_pos_byte = from_byte;
7092 }
7093 else
7094 {
7095 coding->dst_pos = BUF_PT (XBUFFER (dst_object));
7096 coding->dst_pos_byte = BUF_PT_BYTE (XBUFFER (dst_object));
7097 }
7098 coding->dst_multibyte
7099 = ! NILP (XBUFFER (dst_object)->enable_multibyte_characters);
7100 }
7101 else if (EQ (dst_object, Qt))
7102 {
7103 coding->dst_object = Qnil;
7104 coding->dst_bytes = coding->src_chars;
7105 if (coding->dst_bytes == 0)
7106 coding->dst_bytes = 1;
7107 coding->destination = (unsigned char *) xmalloc (coding->dst_bytes);
7108 coding->dst_multibyte = 0;
7109 }
7110 else
7111 {
7112 coding->dst_object = Qnil;
7113 coding->dst_multibyte = 0;
7114 }
7115
7116 encode_coding (coding);
7117
7118 if (EQ (dst_object, Qt))
7119 {
7120 if (BUFFERP (coding->dst_object))
7121 coding->dst_object = Fbuffer_string ();
7122 else
7123 {
7124 coding->dst_object
7125 = make_unibyte_string ((char *) coding->destination,
7126 coding->produced);
7127 xfree (coding->destination);
7128 }
7129 }
7130
7131 if (saved_pt >= 0)
7132 {
7133 /* This is the case of:
7134 (BUFFERP (src_object) && EQ (src_object, dst_object))
7135 As we have moved PT while replacing the original buffer
7136 contents, we must recover it now. */
7137 set_buffer_internal (XBUFFER (src_object));
7138 if (saved_pt < from)
7139 TEMP_SET_PT_BOTH (saved_pt, saved_pt_byte);
7140 else if (saved_pt < from + chars)
7141 TEMP_SET_PT_BOTH (from, from_byte);
7142 else if (! NILP (current_buffer->enable_multibyte_characters))
7143 TEMP_SET_PT_BOTH (saved_pt + (coding->produced_char - chars),
7144 saved_pt_byte + (coding->produced - bytes));
7145 else
7146 TEMP_SET_PT_BOTH (saved_pt + (coding->produced - bytes),
7147 saved_pt_byte + (coding->produced - bytes));
7148 }
7149
7150 if (kill_src_buffer)
7151 Fkill_buffer (coding->src_object);
7152 unbind_to (count, Qnil);
7153}
7154
7155
7156Lisp_Object
7157preferred_coding_system ()
7158{
7159 int id = coding_categories[coding_priorities[0]].id;
7160
7161 return CODING_ID_NAME (id);
7162}
7163
7164\f
7165#ifdef emacs
7166/*** 8. Emacs Lisp library functions ***/
7167
7168DEFUN ("coding-system-p", Fcoding_system_p, Scoding_system_p, 1, 1, 0,
7169 doc: /* Return t if OBJECT is nil or a coding-system.
7170See the documentation of `define-coding-system' for information
7171about coding-system objects. */)
7172 (obj)
7173 Lisp_Object obj;
7174{
7175 if (NILP (obj)
7176 || CODING_SYSTEM_ID (obj) >= 0)
7177 return Qt;
7178 if (! SYMBOLP (obj)
7179 || NILP (Fget (obj, Qcoding_system_define_form)))
7180 return Qnil;
7181 return Qt;
7182}
7183
7184DEFUN ("read-non-nil-coding-system", Fread_non_nil_coding_system,
7185 Sread_non_nil_coding_system, 1, 1, 0,
7186 doc: /* Read a coding system from the minibuffer, prompting with string PROMPT. */)
7187 (prompt)
7188 Lisp_Object prompt;
7189{
7190 Lisp_Object val;
7191 do
7192 {
7193 val = Fcompleting_read (prompt, Vcoding_system_alist, Qnil,
7194 Qt, Qnil, Qcoding_system_history, Qnil, Qnil);
7195 }
7196 while (SCHARS (val) == 0);
7197 return (Fintern (val, Qnil));
7198}
7199
7200DEFUN ("read-coding-system", Fread_coding_system, Sread_coding_system, 1, 2, 0,
7201 doc: /* Read a coding system from the minibuffer, prompting with string PROMPT.
7202If the user enters null input, return second argument DEFAULT-CODING-SYSTEM. */)
7203 (prompt, default_coding_system)
7204 Lisp_Object prompt, default_coding_system;
7205{
7206 Lisp_Object val;
7207 if (SYMBOLP (default_coding_system))
7208 XSETSTRING (default_coding_system, XPNTR (SYMBOL_NAME (default_coding_system)));
7209 val = Fcompleting_read (prompt, Vcoding_system_alist, Qnil,
7210 Qt, Qnil, Qcoding_system_history,
7211 default_coding_system, Qnil);
7212 return (SCHARS (val) == 0 ? Qnil : Fintern (val, Qnil));
7213}
7214
7215DEFUN ("check-coding-system", Fcheck_coding_system, Scheck_coding_system,
7216 1, 1, 0,
7217 doc: /* Check validity of CODING-SYSTEM.
7218If valid, return CODING-SYSTEM, else signal a `coding-system-error' error.
7219It is valid if it is nil or a symbol defined as a coding system by the
7220function `define-coding-system'. */)
7221 (coding_system)
7222 Lisp_Object coding_system;
7223{
7224 Lisp_Object define_form;
7225
7226 define_form = Fget (coding_system, Qcoding_system_define_form);
7227 if (! NILP (define_form))
7228 {
7229 Fput (coding_system, Qcoding_system_define_form, Qnil);
7230 safe_eval (define_form);
7231 }
7232 if (!NILP (Fcoding_system_p (coding_system)))
7233 return coding_system;
7234 xsignal1 (Qcoding_system_error, coding_system);
7235}
7236
7237\f
7238/* Detect how the bytes at SRC of length SRC_BYTES are encoded. If
7239 HIGHEST is nonzero, return the coding system of the highest
7240 priority among the detected coding systems. Otherwize return a
7241 list of detected coding systems sorted by their priorities. If
7242 MULTIBYTEP is nonzero, it is assumed that the bytes are in correct
7243 multibyte form but contains only ASCII and eight-bit chars.
7244 Otherwise, the bytes are raw bytes.
7245
7246 CODING-SYSTEM controls the detection as below:
7247
7248 If it is nil, detect both text-format and eol-format. If the
7249 text-format part of CODING-SYSTEM is already specified
7250 (e.g. `iso-latin-1'), detect only eol-format. If the eol-format
7251 part of CODING-SYSTEM is already specified (e.g. `undecided-unix'),
7252 detect only text-format. */
7253
7254Lisp_Object
7255detect_coding_system (src, src_chars, src_bytes, highest, multibytep,
7256 coding_system)
7257 const unsigned char *src;
7258 int src_chars, src_bytes, highest;
7259 int multibytep;
7260 Lisp_Object coding_system;
7261{
7262 const unsigned char *src_end = src + src_bytes;
7263 Lisp_Object attrs, eol_type;
7264 Lisp_Object val;
7265 struct coding_system coding;
7266 int id;
7267 struct coding_detection_info detect_info;
7268 enum coding_category base_category;
7269
7270 if (NILP (coding_system))
7271 coding_system = Qundecided;
7272 setup_coding_system (coding_system, &coding);
7273 attrs = CODING_ID_ATTRS (coding.id);
7274 eol_type = CODING_ID_EOL_TYPE (coding.id);
7275 coding_system = CODING_ATTR_BASE_NAME (attrs);
7276
7277 coding.source = src;
7278 coding.src_chars = src_chars;
7279 coding.src_bytes = src_bytes;
7280 coding.src_multibyte = multibytep;
7281 coding.consumed = 0;
7282 coding.mode |= CODING_MODE_LAST_BLOCK;
7283
7284 detect_info.checked = detect_info.found = detect_info.rejected = 0;
7285
7286 /* At first, detect text-format if necessary. */
7287 base_category = XINT (CODING_ATTR_CATEGORY (attrs));
7288 if (base_category == coding_category_undecided)
7289 {
7290 enum coding_category category;
7291 struct coding_system *this;
7292 int c, i;
7293
7294 /* Skip all ASCII bytes except for a few ISO2022 controls. */
7295 for (i = 0; src < src_end; i++, src++)
7296 {
7297 c = *src;
7298 if (c & 0x80)
7299 break;
7300 if (c < 0x20
7301 && (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO)
7302 && ! inhibit_iso_escape_detection)
7303 {
7304 coding.head_ascii = src - coding.source;
7305 if (detect_coding_iso_2022 (&coding, &detect_info))
7306 {
7307 /* We have scanned the whole data. */
7308 if (! (detect_info.rejected & CATEGORY_MASK_ISO_7_ELSE))
7309 /* We didn't find an 8-bit code. */
7310 src = src_end;
7311 break;
7312 }
7313 }
7314 }
7315 coding.head_ascii = src - coding.source;
7316
7317 if (src < src_end
7318 || detect_info.found)
7319 {
7320 if (src == src_end)
7321 /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */
7322 for (i = 0; i < coding_category_raw_text; i++)
7323 {
7324 category = coding_priorities[i];
7325 this = coding_categories + category;
7326 if (detect_info.found & (1 << category))
7327 break;
7328 }
7329 else
7330 for (i = 0; i < coding_category_raw_text; i++)
7331 {
7332 category = coding_priorities[i];
7333 this = coding_categories + category;
7334
7335 if (this->id < 0)
7336 {
7337 /* No coding system of this category is defined. */
7338 detect_info.rejected |= (1 << category);
7339 }
7340 else if (category >= coding_category_raw_text)
7341 continue;
7342 else if (detect_info.checked & (1 << category))
7343 {
7344 if (highest
7345 && (detect_info.found & (1 << category)))
7346 break;
7347 }
7348 else
7349 {
7350 if ((*(this->detector)) (&coding, &detect_info)
7351 && highest
7352 && (detect_info.found & (1 << category)))
7353 {
7354 if (category == coding_category_utf_16_auto)
7355 {
7356 if (detect_info.found & CATEGORY_MASK_UTF_16_LE)
7357 category = coding_category_utf_16_le;
7358 else
7359 category = coding_category_utf_16_be;
7360 }
7361 break;
7362 }
7363 }
7364 }
7365 }
7366
7367 if (detect_info.rejected == CATEGORY_MASK_ANY)
7368 {
7369 detect_info.found = CATEGORY_MASK_RAW_TEXT;
7370 id = coding_categories[coding_category_raw_text].id;
7371 val = Fcons (make_number (id), Qnil);
7372 }
7373 else if (! detect_info.rejected && ! detect_info.found)
7374 {
7375 detect_info.found = CATEGORY_MASK_ANY;
7376 id = coding_categories[coding_category_undecided].id;
7377 val = Fcons (make_number (id), Qnil);
7378 }
7379 else if (highest)
7380 {
7381 if (detect_info.found)
7382 {
7383 detect_info.found = 1 << category;
7384 val = Fcons (make_number (this->id), Qnil);
7385 }
7386 else
7387 for (i = 0; i < coding_category_raw_text; i++)
7388 if (! (detect_info.rejected & (1 << coding_priorities[i])))
7389 {
7390 detect_info.found = 1 << coding_priorities[i];
7391 id = coding_categories[coding_priorities[i]].id;
7392 val = Fcons (make_number (id), Qnil);
7393 break;
7394 }
7395 }
7396 else
7397 {
7398 int mask = detect_info.rejected | detect_info.found;
7399 int found = 0;
7400 val = Qnil;
7401
7402 for (i = coding_category_raw_text - 1; i >= 0; i--)
7403 {
7404 category = coding_priorities[i];
7405 if (! (mask & (1 << category)))
7406 {
7407 found |= 1 << category;
7408 id = coding_categories[category].id;
7409 if (id >= 0)
7410 val = Fcons (make_number (id), val);
7411 }
7412 }
7413 for (i = coding_category_raw_text - 1; i >= 0; i--)
7414 {
7415 category = coding_priorities[i];
7416 if (detect_info.found & (1 << category))
7417 {
7418 id = coding_categories[category].id;
7419 val = Fcons (make_number (id), val);
7420 }
7421 }
7422 detect_info.found |= found;
7423 }
7424 }
7425 else if (base_category == coding_category_utf_16_auto)
7426 {
7427 if (detect_coding_utf_16 (&coding, &detect_info))
7428 {
7429 struct coding_system *this;
7430
7431 if (detect_info.found & CATEGORY_MASK_UTF_16_LE)
7432 this = coding_categories + coding_category_utf_16_le;
7433 else if (detect_info.found & CATEGORY_MASK_UTF_16_BE)
7434 this = coding_categories + coding_category_utf_16_be;
7435 else if (detect_info.rejected & CATEGORY_MASK_UTF_16_LE_NOSIG)
7436 this = coding_categories + coding_category_utf_16_be_nosig;
7437 else
7438 this = coding_categories + coding_category_utf_16_le_nosig;
7439 val = Fcons (make_number (this->id), Qnil);
7440 }
7441 }
7442 else
7443 {
7444 detect_info.found = 1 << XINT (CODING_ATTR_CATEGORY (attrs));
7445 val = Fcons (make_number (coding.id), Qnil);
7446 }
7447
7448 /* Then, detect eol-format if necessary. */
7449 {
7450 int normal_eol = -1, utf_16_be_eol = -1, utf_16_le_eol;
7451 Lisp_Object tail;
7452
7453 if (VECTORP (eol_type))
7454 {
7455 if (detect_info.found & ~CATEGORY_MASK_UTF_16)
7456 normal_eol = detect_eol (coding.source, src_bytes,
7457 coding_category_raw_text);
7458 if (detect_info.found & (CATEGORY_MASK_UTF_16_BE
7459 | CATEGORY_MASK_UTF_16_BE_NOSIG))
7460 utf_16_be_eol = detect_eol (coding.source, src_bytes,
7461 coding_category_utf_16_be);
7462 if (detect_info.found & (CATEGORY_MASK_UTF_16_LE
7463 | CATEGORY_MASK_UTF_16_LE_NOSIG))
7464 utf_16_le_eol = detect_eol (coding.source, src_bytes,
7465 coding_category_utf_16_le);
7466 }
7467 else
7468 {
7469 if (EQ (eol_type, Qunix))
7470 normal_eol = utf_16_be_eol = utf_16_le_eol = EOL_SEEN_LF;
7471 else if (EQ (eol_type, Qdos))
7472 normal_eol = utf_16_be_eol = utf_16_le_eol = EOL_SEEN_CRLF;
7473 else
7474 normal_eol = utf_16_be_eol = utf_16_le_eol = EOL_SEEN_CR;
7475 }
7476
7477 for (tail = val; CONSP (tail); tail = XCDR (tail))
7478 {
7479 enum coding_category category;
7480 int this_eol;
7481
7482 id = XINT (XCAR (tail));
7483 attrs = CODING_ID_ATTRS (id);
7484 category = XINT (CODING_ATTR_CATEGORY (attrs));
7485 eol_type = CODING_ID_EOL_TYPE (id);
7486 if (VECTORP (eol_type))
7487 {
7488 if (category == coding_category_utf_16_be
7489 || category == coding_category_utf_16_be_nosig)
7490 this_eol = utf_16_be_eol;
7491 else if (category == coding_category_utf_16_le
7492 || category == coding_category_utf_16_le_nosig)
7493 this_eol = utf_16_le_eol;
7494 else
7495 this_eol = normal_eol;
7496
7497 if (this_eol == EOL_SEEN_LF)
7498 XSETCAR (tail, AREF (eol_type, 0));
7499 else if (this_eol == EOL_SEEN_CRLF)
7500 XSETCAR (tail, AREF (eol_type, 1));
7501 else if (this_eol == EOL_SEEN_CR)
7502 XSETCAR (tail, AREF (eol_type, 2));
7503 else
7504 XSETCAR (tail, CODING_ID_NAME (id));
7505 }
7506 else
7507 XSETCAR (tail, CODING_ID_NAME (id));
7508 }
7509 }
7510
7511 return (highest ? XCAR (val) : val);
7512}
7513
7514
7515DEFUN ("detect-coding-region", Fdetect_coding_region, Sdetect_coding_region,
7516 2, 3, 0,
7517 doc: /* Detect coding system of the text in the region between START and END.
7518Return a list of possible coding systems ordered by priority.
7519
7520If only ASCII characters are found (except for such ISO-2022 control
7521characters ISO-2022 as ESC), it returns a list of single element
7522`undecided' or its subsidiary coding system according to a detected
7523end-of-line format.
7524
7525If optional argument HIGHEST is non-nil, return the coding system of
7526highest priority. */)
7527 (start, end, highest)
7528 Lisp_Object start, end, highest;
7529{
7530 int from, to;
7531 int from_byte, to_byte;
7532
7533 CHECK_NUMBER_COERCE_MARKER (start);
7534 CHECK_NUMBER_COERCE_MARKER (end);
7535
7536 validate_region (&start, &end);
7537 from = XINT (start), to = XINT (end);
7538 from_byte = CHAR_TO_BYTE (from);
7539 to_byte = CHAR_TO_BYTE (to);
7540
7541 if (from < GPT && to >= GPT)
7542 move_gap_both (to, to_byte);
7543
7544 return detect_coding_system (BYTE_POS_ADDR (from_byte),
7545 to - from, to_byte - from_byte,
7546 !NILP (highest),
7547 !NILP (current_buffer
7548 ->enable_multibyte_characters),
7549 Qnil);
7550}
7551
7552DEFUN ("detect-coding-string", Fdetect_coding_string, Sdetect_coding_string,
7553 1, 2, 0,
7554 doc: /* Detect coding system of the text in STRING.
7555Return a list of possible coding systems ordered by priority.
7556
7557If only ASCII characters are found (except for such ISO-2022 control
7558characters ISO-2022 as ESC), it returns a list of single element
7559`undecided' or its subsidiary coding system according to a detected
7560end-of-line format.
7561
7562If optional argument HIGHEST is non-nil, return the coding system of
7563highest priority. */)
7564 (string, highest)
7565 Lisp_Object string, highest;
7566{
7567 CHECK_STRING (string);
7568
7569 return detect_coding_system (SDATA (string),
7570 SCHARS (string), SBYTES (string),
7571 !NILP (highest), STRING_MULTIBYTE (string),
7572 Qnil);
7573}
7574
7575
7576static INLINE int
7577char_encodable_p (c, attrs)
7578 int c;
7579 Lisp_Object attrs;
7580{
7581 Lisp_Object tail;
7582 struct charset *charset;
7583 Lisp_Object translation_table;
7584
7585 translation_table = CODING_ATTR_TRANS_TBL (attrs);
7586 if (! NILP (translation_table))
7587 c = translate_char (translation_table, c);
7588 for (tail = CODING_ATTR_CHARSET_LIST (attrs);
7589 CONSP (tail); tail = XCDR (tail))
7590 {
7591 charset = CHARSET_FROM_ID (XINT (XCAR (tail)));
7592 if (CHAR_CHARSET_P (c, charset))
7593 break;
7594 }
7595 return (! NILP (tail));
7596}
7597
7598
7599/* Return a list of coding systems that safely encode the text between
7600 START and END. If EXCLUDE is non-nil, it is a list of coding
7601 systems not to check. The returned list doesn't contain any such
7602 coding systems. In any case, if the text contains only ASCII or is
7603 unibyte, return t. */
7604
7605DEFUN ("find-coding-systems-region-internal",
7606 Ffind_coding_systems_region_internal,
7607 Sfind_coding_systems_region_internal, 2, 3, 0,
7608 doc: /* Internal use only. */)
7609 (start, end, exclude)
7610 Lisp_Object start, end, exclude;
7611{
7612 Lisp_Object coding_attrs_list, safe_codings;
7613 EMACS_INT start_byte, end_byte;
7614 const unsigned char *p, *pbeg, *pend;
7615 int c;
7616 Lisp_Object tail, elt;
7617
7618 if (STRINGP (start))
7619 {
7620 if (!STRING_MULTIBYTE (start)
7621 || SCHARS (start) == SBYTES (start))
7622 return Qt;
7623 start_byte = 0;
7624 end_byte = SBYTES (start);
7625 }
7626 else
7627 {
7628 CHECK_NUMBER_COERCE_MARKER (start);
7629 CHECK_NUMBER_COERCE_MARKER (end);
7630 if (XINT (start) < BEG || XINT (end) > Z || XINT (start) > XINT (end))
7631 args_out_of_range (start, end);
7632 if (NILP (current_buffer->enable_multibyte_characters))
7633 return Qt;
7634 start_byte = CHAR_TO_BYTE (XINT (start));
7635 end_byte = CHAR_TO_BYTE (XINT (end));
7636 if (XINT (end) - XINT (start) == end_byte - start_byte)
7637 return Qt;
7638
7639 if (XINT (start) < GPT && XINT (end) > GPT)
7640 {
7641 if ((GPT - XINT (start)) < (XINT (end) - GPT))
7642 move_gap_both (XINT (start), start_byte);
7643 else
7644 move_gap_both (XINT (end), end_byte);
7645 }
7646 }
7647
7648 coding_attrs_list = Qnil;
7649 for (tail = Vcoding_system_list; CONSP (tail); tail = XCDR (tail))
7650 if (NILP (exclude)
7651 || NILP (Fmemq (XCAR (tail), exclude)))
7652 {
7653 Lisp_Object attrs;
7654
7655 attrs = AREF (CODING_SYSTEM_SPEC (XCAR (tail)), 0);
7656 if (EQ (XCAR (tail), CODING_ATTR_BASE_NAME (attrs))
7657 && ! EQ (CODING_ATTR_TYPE (attrs), Qundecided))
7658 {
7659 ASET (attrs, coding_attr_trans_tbl,
7660 get_translation_table (attrs, 1, NULL));
7661 coding_attrs_list = Fcons (attrs, coding_attrs_list);
7662 }
7663 }
7664
7665 if (STRINGP (start))
7666 p = pbeg = SDATA (start);
7667 else
7668 p = pbeg = BYTE_POS_ADDR (start_byte);
7669 pend = p + (end_byte - start_byte);
7670
7671 while (p < pend && ASCII_BYTE_P (*p)) p++;
7672 while (p < pend && ASCII_BYTE_P (*(pend - 1))) pend--;
7673
7674 while (p < pend)
7675 {
7676 if (ASCII_BYTE_P (*p))
7677 p++;
7678 else
7679 {
7680 c = STRING_CHAR_ADVANCE (p);
7681
7682 charset_map_loaded = 0;
7683 for (tail = coding_attrs_list; CONSP (tail);)
7684 {
7685 elt = XCAR (tail);
7686 if (NILP (elt))
7687 tail = XCDR (tail);
7688 else if (char_encodable_p (c, elt))
7689 tail = XCDR (tail);
7690 else if (CONSP (XCDR (tail)))
7691 {
7692 XSETCAR (tail, XCAR (XCDR (tail)));
7693 XSETCDR (tail, XCDR (XCDR (tail)));
7694 }
7695 else
7696 {
7697 XSETCAR (tail, Qnil);
7698 tail = XCDR (tail);
7699 }
7700 }
7701 if (charset_map_loaded)
7702 {
7703 EMACS_INT p_offset = p - pbeg, pend_offset = pend - pbeg;
7704
7705 if (STRINGP (start))
7706 pbeg = SDATA (start);
7707 else
7708 pbeg = BYTE_POS_ADDR (start_byte);
7709 p = pbeg + p_offset;
7710 pend = pbeg + pend_offset;
7711 }
7712 }
7713 }
7714
7715 safe_codings = list2 (Qraw_text, Qno_conversion);
7716 for (tail = coding_attrs_list; CONSP (tail); tail = XCDR (tail))
7717 if (! NILP (XCAR (tail)))
7718 safe_codings = Fcons (CODING_ATTR_BASE_NAME (XCAR (tail)), safe_codings);
7719
7720 return safe_codings;
7721}
7722
7723
7724DEFUN ("unencodable-char-position", Funencodable_char_position,
7725 Sunencodable_char_position, 3, 5, 0,
7726 doc: /*
7727Return position of first un-encodable character in a region.
7728START and END specfiy the region and CODING-SYSTEM specifies the
7729encoding to check. Return nil if CODING-SYSTEM does encode the region.
7730
7731If optional 4th argument COUNT is non-nil, it specifies at most how
7732many un-encodable characters to search. In this case, the value is a
7733list of positions.
7734
7735If optional 5th argument STRING is non-nil, it is a string to search
7736for un-encodable characters. In that case, START and END are indexes
7737to the string. */)
7738 (start, end, coding_system, count, string)
7739 Lisp_Object start, end, coding_system, count, string;
7740{
7741 int n;
7742 struct coding_system coding;
7743 Lisp_Object attrs, charset_list, translation_table;
7744 Lisp_Object positions;
7745 int from, to;
7746 const unsigned char *p, *stop, *pend;
7747 int ascii_compatible;
7748
7749 setup_coding_system (Fcheck_coding_system (coding_system), &coding);
7750 attrs = CODING_ID_ATTRS (coding.id);
7751 if (EQ (CODING_ATTR_TYPE (attrs), Qraw_text))
7752 return Qnil;
7753 ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs));
7754 charset_list = CODING_ATTR_CHARSET_LIST (attrs);
7755 translation_table = get_translation_table (attrs, 1, NULL);
7756
7757 if (NILP (string))
7758 {
7759 validate_region (&start, &end);
7760 from = XINT (start);
7761 to = XINT (end);
7762 if (NILP (current_buffer->enable_multibyte_characters)
7763 || (ascii_compatible
7764 && (to - from) == (CHAR_TO_BYTE (to) - (CHAR_TO_BYTE (from)))))
7765 return Qnil;
7766 p = CHAR_POS_ADDR (from);
7767 pend = CHAR_POS_ADDR (to);
7768 if (from < GPT && to >= GPT)
7769 stop = GPT_ADDR;
7770 else
7771 stop = pend;
7772 }
7773 else
7774 {
7775 CHECK_STRING (string);
7776 CHECK_NATNUM (start);
7777 CHECK_NATNUM (end);
7778 from = XINT (start);
7779 to = XINT (end);
7780 if (from > to
7781 || to > SCHARS (string))
7782 args_out_of_range_3 (string, start, end);
7783 if (! STRING_MULTIBYTE (string))
7784 return Qnil;
7785 p = SDATA (string) + string_char_to_byte (string, from);
7786 stop = pend = SDATA (string) + string_char_to_byte (string, to);
7787 if (ascii_compatible && (to - from) == (pend - p))
7788 return Qnil;
7789 }
7790
7791 if (NILP (count))
7792 n = 1;
7793 else
7794 {
7795 CHECK_NATNUM (count);
7796 n = XINT (count);
7797 }
7798
7799 positions = Qnil;
7800 while (1)
7801 {
7802 int c;
7803
7804 if (ascii_compatible)
7805 while (p < stop && ASCII_BYTE_P (*p))
7806 p++, from++;
7807 if (p >= stop)
7808 {
7809 if (p >= pend)
7810 break;
7811 stop = pend;
7812 p = GAP_END_ADDR;
7813 }
7814
7815 c = STRING_CHAR_ADVANCE (p);
7816 if (! (ASCII_CHAR_P (c) && ascii_compatible)
7817 && ! char_charset (translate_char (translation_table, c),
7818 charset_list, NULL))
7819 {
7820 positions = Fcons (make_number (from), positions);
7821 n--;
7822 if (n == 0)
7823 break;
7824 }
7825
7826 from++;
7827 }
7828
7829 return (NILP (count) ? Fcar (positions) : Fnreverse (positions));
7830}
7831
7832
7833DEFUN ("check-coding-systems-region", Fcheck_coding_systems_region,
7834 Scheck_coding_systems_region, 3, 3, 0,
7835 doc: /* Check if the region is encodable by coding systems.
7836
7837START and END are buffer positions specifying the region.
7838CODING-SYSTEM-LIST is a list of coding systems to check.
7839
7840The value is an alist ((CODING-SYSTEM POS0 POS1 ...) ...), where
7841CODING-SYSTEM is a member of CODING-SYSTEM-LIst and can't encode the
7842whole region, POS0, POS1, ... are buffer positions where non-encodable
7843characters are found.
7844
7845If all coding systems in CODING-SYSTEM-LIST can encode the region, the
7846value is nil.
7847
7848START may be a string. In that case, check if the string is
7849encodable, and the value contains indices to the string instead of
7850buffer positions. END is ignored. */)
7851 (start, end, coding_system_list)
7852 Lisp_Object start, end, coding_system_list;
7853{
7854 Lisp_Object list;
7855 EMACS_INT start_byte, end_byte;
7856 int pos;
7857 const unsigned char *p, *pbeg, *pend;
7858 int c;
7859 Lisp_Object tail, elt, attrs;
7860
7861 if (STRINGP (start))
7862 {
7863 if (!STRING_MULTIBYTE (start)
7864 && SCHARS (start) != SBYTES (start))
7865 return Qnil;
7866 start_byte = 0;
7867 end_byte = SBYTES (start);
7868 pos = 0;
7869 }
7870 else
7871 {
7872 CHECK_NUMBER_COERCE_MARKER (start);
7873 CHECK_NUMBER_COERCE_MARKER (end);
7874 if (XINT (start) < BEG || XINT (end) > Z || XINT (start) > XINT (end))
7875 args_out_of_range (start, end);
7876 if (NILP (current_buffer->enable_multibyte_characters))
7877 return Qnil;
7878 start_byte = CHAR_TO_BYTE (XINT (start));
7879 end_byte = CHAR_TO_BYTE (XINT (end));
7880 if (XINT (end) - XINT (start) == end_byte - start_byte)
7881 return Qt;
7882
7883 if (XINT (start) < GPT && XINT (end) > GPT)
7884 {
7885 if ((GPT - XINT (start)) < (XINT (end) - GPT))
7886 move_gap_both (XINT (start), start_byte);
7887 else
7888 move_gap_both (XINT (end), end_byte);
7889 }
7890 pos = XINT (start);
7891 }
7892
7893 list = Qnil;
7894 for (tail = coding_system_list; CONSP (tail); tail = XCDR (tail))
7895 {
7896 elt = XCAR (tail);
7897 attrs = AREF (CODING_SYSTEM_SPEC (elt), 0);
7898 ASET (attrs, coding_attr_trans_tbl,
7899 get_translation_table (attrs, 1, NULL));
7900 list = Fcons (Fcons (elt, Fcons (attrs, Qnil)), list);
7901 }
7902
7903 if (STRINGP (start))
7904 p = pbeg = SDATA (start);
7905 else
7906 p = pbeg = BYTE_POS_ADDR (start_byte);
7907 pend = p + (end_byte - start_byte);
7908
7909 while (p < pend && ASCII_BYTE_P (*p)) p++, pos++;
7910 while (p < pend && ASCII_BYTE_P (*(pend - 1))) pend--;
7911
7912 while (p < pend)
7913 {
7914 if (ASCII_BYTE_P (*p))
7915 p++;
7916 else
7917 {
7918 c = STRING_CHAR_ADVANCE (p);
7919
7920 charset_map_loaded = 0;
7921 for (tail = list; CONSP (tail); tail = XCDR (tail))
7922 {
7923 elt = XCDR (XCAR (tail));
7924 if (! char_encodable_p (c, XCAR (elt)))
7925 XSETCDR (elt, Fcons (make_number (pos), XCDR (elt)));
7926 }
7927 if (charset_map_loaded)
7928 {
7929 EMACS_INT p_offset = p - pbeg, pend_offset = pend - pbeg;
7930
7931 if (STRINGP (start))
7932 pbeg = SDATA (start);
7933 else
7934 pbeg = BYTE_POS_ADDR (start_byte);
7935 p = pbeg + p_offset;
7936 pend = pbeg + pend_offset;
7937 }
7938 }
7939 pos++;
7940 }
7941
7942 tail = list;
7943 list = Qnil;
7944 for (; CONSP (tail); tail = XCDR (tail))
7945 {
7946 elt = XCAR (tail);
7947 if (CONSP (XCDR (XCDR (elt))))
7948 list = Fcons (Fcons (XCAR (elt), Fnreverse (XCDR (XCDR (elt)))),
7949 list);
7950 }
7951
7952 return list;
7953}
7954
7955
7956Lisp_Object
7957code_convert_region (start, end, coding_system, dst_object, encodep, norecord)
7958 Lisp_Object start, end, coding_system, dst_object;
7959 int encodep, norecord;
7960{
7961 struct coding_system coding;
7962 EMACS_INT from, from_byte, to, to_byte;
7963 Lisp_Object src_object;
7964
7965 CHECK_NUMBER_COERCE_MARKER (start);
7966 CHECK_NUMBER_COERCE_MARKER (end);
7967 if (NILP (coding_system))
7968 coding_system = Qno_conversion;
7969 else
7970 CHECK_CODING_SYSTEM (coding_system);
7971 src_object = Fcurrent_buffer ();
7972 if (NILP (dst_object))
7973 dst_object = src_object;
7974 else if (! EQ (dst_object, Qt))
7975 CHECK_BUFFER (dst_object);
7976
7977 validate_region (&start, &end);
7978 from = XFASTINT (start);
7979 from_byte = CHAR_TO_BYTE (from);
7980 to = XFASTINT (end);
7981 to_byte = CHAR_TO_BYTE (to);
7982
7983 setup_coding_system (coding_system, &coding);
7984 coding.mode |= CODING_MODE_LAST_BLOCK;
7985
7986 if (encodep)
7987 encode_coding_object (&coding, src_object, from, from_byte, to, to_byte,
7988 dst_object);
7989 else
7990 decode_coding_object (&coding, src_object, from, from_byte, to, to_byte,
7991 dst_object);
7992 if (! norecord)
7993 Vlast_coding_system_used = CODING_ID_NAME (coding.id);
7994
7995 return (BUFFERP (dst_object)
7996 ? make_number (coding.produced_char)
7997 : coding.dst_object);
7998}
7999
8000
8001DEFUN ("decode-coding-region", Fdecode_coding_region, Sdecode_coding_region,
8002 3, 4, "r\nzCoding system: ",
8003 doc: /* Decode the current region from the specified coding system.
8004When called from a program, takes four arguments:
8005 START, END, CODING-SYSTEM, and DESTINATION.
8006START and END are buffer positions.
8007
8008Optional 4th arguments DESTINATION specifies where the decoded text goes.
8009If nil, the region between START and END is replace by the decoded text.
8010If buffer, the decoded text is inserted in the buffer.
8011If t, the decoded text is returned.
8012
8013This function sets `last-coding-system-used' to the precise coding system
8014used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8015not fully specified.)
8016It returns the length of the decoded text. */)
8017 (start, end, coding_system, destination)
8018 Lisp_Object start, end, coding_system, destination;
8019{
8020 return code_convert_region (start, end, coding_system, destination, 0, 0);
8021}
8022
8023DEFUN ("encode-coding-region", Fencode_coding_region, Sencode_coding_region,
8024 3, 4, "r\nzCoding system: ",
8025 doc: /* Encode the current region by specified coding system.
8026When called from a program, takes three arguments:
8027START, END, and CODING-SYSTEM. START and END are buffer positions.
8028
8029Optional 4th arguments DESTINATION specifies where the encoded text goes.
8030If nil, the region between START and END is replace by the encoded text.
8031If buffer, the encoded text is inserted in the buffer.
8032If t, the encoded text is returned.
8033
8034This function sets `last-coding-system-used' to the precise coding system
8035used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8036not fully specified.)
8037It returns the length of the encoded text. */)
8038 (start, end, coding_system, destination)
8039 Lisp_Object start, end, coding_system, destination;
8040{
8041 return code_convert_region (start, end, coding_system, destination, 1, 0);
8042}
8043
8044Lisp_Object
8045code_convert_string (string, coding_system, dst_object,
8046 encodep, nocopy, norecord)
8047 Lisp_Object string, coding_system, dst_object;
8048 int encodep, nocopy, norecord;
8049{
8050 struct coding_system coding;
8051 EMACS_INT chars, bytes;
8052
8053 CHECK_STRING (string);
8054 if (NILP (coding_system))
8055 {
8056 if (! norecord)
8057 Vlast_coding_system_used = Qno_conversion;
8058 if (NILP (dst_object))
8059 return (nocopy ? Fcopy_sequence (string) : string);
8060 }
8061
8062 if (NILP (coding_system))
8063 coding_system = Qno_conversion;
8064 else
8065 CHECK_CODING_SYSTEM (coding_system);
8066 if (NILP (dst_object))
8067 dst_object = Qt;
8068 else if (! EQ (dst_object, Qt))
8069 CHECK_BUFFER (dst_object);
8070
8071 setup_coding_system (coding_system, &coding);
8072 coding.mode |= CODING_MODE_LAST_BLOCK;
8073 chars = SCHARS (string);
8074 bytes = SBYTES (string);
8075 if (encodep)
8076 encode_coding_object (&coding, string, 0, 0, chars, bytes, dst_object);
8077 else
8078 decode_coding_object (&coding, string, 0, 0, chars, bytes, dst_object);
8079 if (! norecord)
8080 Vlast_coding_system_used = CODING_ID_NAME (coding.id);
8081
8082 return (BUFFERP (dst_object)
8083 ? make_number (coding.produced_char)
8084 : coding.dst_object);
8085}
8086
8087
8088/* Encode or decode STRING according to CODING_SYSTEM.
8089 Do not set Vlast_coding_system_used.
8090
8091 This function is called only from macros DECODE_FILE and
8092 ENCODE_FILE, thus we ignore character composition. */
8093
8094Lisp_Object
8095code_convert_string_norecord (string, coding_system, encodep)
8096 Lisp_Object string, coding_system;
8097 int encodep;
8098{
8099 return code_convert_string (string, coding_system, Qt, encodep, 0, 1);
8100}
8101
8102
8103DEFUN ("decode-coding-string", Fdecode_coding_string, Sdecode_coding_string,
8104 2, 4, 0,
8105 doc: /* Decode STRING which is encoded in CODING-SYSTEM, and return the result.
8106
8107Optional third arg NOCOPY non-nil means it is OK to return STRING itself
8108if the decoding operation is trivial.
8109
8110Optional fourth arg BUFFER non-nil meant that the decoded text is
8111inserted in BUFFER instead of returned as a string. In this case,
8112the return value is BUFFER.
8113
8114This function sets `last-coding-system-used' to the precise coding system
8115used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8116not fully specified. */)
8117 (string, coding_system, nocopy, buffer)
8118 Lisp_Object string, coding_system, nocopy, buffer;
8119{
8120 return code_convert_string (string, coding_system, buffer,
8121 0, ! NILP (nocopy), 0);
8122}
8123
8124DEFUN ("encode-coding-string", Fencode_coding_string, Sencode_coding_string,
8125 2, 4, 0,
8126 doc: /* Encode STRING to CODING-SYSTEM, and return the result.
8127
8128Optional third arg NOCOPY non-nil means it is OK to return STRING
8129itself if the encoding operation is trivial.
8130
8131Optional fourth arg BUFFER non-nil meant that the encoded text is
8132inserted in BUFFER instead of returned as a string. In this case,
8133the return value is BUFFER.
8134
8135This function sets `last-coding-system-used' to the precise coding system
8136used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8137not fully specified.) */)
8138 (string, coding_system, nocopy, buffer)
8139 Lisp_Object string, coding_system, nocopy, buffer;
8140{
8141 return code_convert_string (string, coding_system, buffer,
8142 1, ! NILP (nocopy), 1);
8143}
8144
8145\f
8146DEFUN ("decode-sjis-char", Fdecode_sjis_char, Sdecode_sjis_char, 1, 1, 0,
8147 doc: /* Decode a Japanese character which has CODE in shift_jis encoding.
8148Return the corresponding character. */)
8149 (code)
8150 Lisp_Object code;
8151{
8152 Lisp_Object spec, attrs, val;
8153 struct charset *charset_roman, *charset_kanji, *charset_kana, *charset;
8154 int c;
8155
8156 CHECK_NATNUM (code);
8157 c = XFASTINT (code);
8158 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system, spec);
8159 attrs = AREF (spec, 0);
8160
8161 if (ASCII_BYTE_P (c)
8162 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)))
8163 return code;
8164
8165 val = CODING_ATTR_CHARSET_LIST (attrs);
8166 charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
8167 charset_kana = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
8168 charset_kanji = CHARSET_FROM_ID (XINT (XCAR (val)));
8169
8170 if (c <= 0x7F)
8171 charset = charset_roman;
8172 else if (c >= 0xA0 && c < 0xDF)
8173 {
8174 charset = charset_kana;
8175 c -= 0x80;
8176 }
8177 else
8178 {
8179 int s1 = c >> 8, s2 = c & 0xFF;
8180
8181 if (s1 < 0x81 || (s1 > 0x9F && s1 < 0xE0) || s1 > 0xEF
8182 || s2 < 0x40 || s2 == 0x7F || s2 > 0xFC)
8183 error ("Invalid code: %d", code);
8184 SJIS_TO_JIS (c);
8185 charset = charset_kanji;
8186 }
8187 c = DECODE_CHAR (charset, c);
8188 if (c < 0)
8189 error ("Invalid code: %d", code);
8190 return make_number (c);
8191}
8192
8193
8194DEFUN ("encode-sjis-char", Fencode_sjis_char, Sencode_sjis_char, 1, 1, 0,
8195 doc: /* Encode a Japanese character CH to shift_jis encoding.
8196Return the corresponding code in SJIS. */)
8197 (ch)
8198 Lisp_Object ch;
8199{
8200 Lisp_Object spec, attrs, charset_list;
8201 int c;
8202 struct charset *charset;
8203 unsigned code;
8204
8205 CHECK_CHARACTER (ch);
8206 c = XFASTINT (ch);
8207 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system, spec);
8208 attrs = AREF (spec, 0);
8209
8210 if (ASCII_CHAR_P (c)
8211 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)))
8212 return ch;
8213
8214 charset_list = CODING_ATTR_CHARSET_LIST (attrs);
8215 charset = char_charset (c, charset_list, &code);
8216 if (code == CHARSET_INVALID_CODE (charset))
8217 error ("Can't encode by shift_jis encoding: %d", c);
8218 JIS_TO_SJIS (code);
8219
8220 return make_number (code);
8221}
8222
8223DEFUN ("decode-big5-char", Fdecode_big5_char, Sdecode_big5_char, 1, 1, 0,
8224 doc: /* Decode a Big5 character which has CODE in BIG5 coding system.
8225Return the corresponding character. */)
8226 (code)
8227 Lisp_Object code;
8228{
8229 Lisp_Object spec, attrs, val;
8230 struct charset *charset_roman, *charset_big5, *charset;
8231 int c;
8232
8233 CHECK_NATNUM (code);
8234 c = XFASTINT (code);
8235 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system, spec);
8236 attrs = AREF (spec, 0);
8237
8238 if (ASCII_BYTE_P (c)
8239 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)))
8240 return code;
8241
8242 val = CODING_ATTR_CHARSET_LIST (attrs);
8243 charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
8244 charset_big5 = CHARSET_FROM_ID (XINT (XCAR (val)));
8245
8246 if (c <= 0x7F)
8247 charset = charset_roman;
8248 else
8249 {
8250 int b1 = c >> 8, b2 = c & 0x7F;
8251 if (b1 < 0xA1 || b1 > 0xFE
8252 || b2 < 0x40 || (b2 > 0x7E && b2 < 0xA1) || b2 > 0xFE)
8253 error ("Invalid code: %d", code);
8254 charset = charset_big5;
8255 }
8256 c = DECODE_CHAR (charset, (unsigned )c);
8257 if (c < 0)
8258 error ("Invalid code: %d", code);
8259 return make_number (c);
8260}
8261
8262DEFUN ("encode-big5-char", Fencode_big5_char, Sencode_big5_char, 1, 1, 0,
8263 doc: /* Encode the Big5 character CH to BIG5 coding system.
8264Return the corresponding character code in Big5. */)
8265 (ch)
8266 Lisp_Object ch;
8267{
8268 Lisp_Object spec, attrs, charset_list;
8269 struct charset *charset;
8270 int c;
8271 unsigned code;
8272
8273 CHECK_CHARACTER (ch);
8274 c = XFASTINT (ch);
8275 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system, spec);
8276 attrs = AREF (spec, 0);
8277 if (ASCII_CHAR_P (c)
8278 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)))
8279 return ch;
8280
8281 charset_list = CODING_ATTR_CHARSET_LIST (attrs);
8282 charset = char_charset (c, charset_list, &code);
8283 if (code == CHARSET_INVALID_CODE (charset))
8284 error ("Can't encode by Big5 encoding: %d", c);
8285
8286 return make_number (code);
8287}
8288
8289\f
8290DEFUN ("set-terminal-coding-system-internal",
8291 Fset_terminal_coding_system_internal,
8292 Sset_terminal_coding_system_internal, 1, 1, 0,
8293 doc: /* Internal use only. */)
8294 (coding_system)
8295 Lisp_Object coding_system;
8296{
8297 CHECK_SYMBOL (coding_system);
8298 setup_coding_system (Fcheck_coding_system (coding_system),
8299 &terminal_coding);
8300
8301 /* We had better not send unsafe characters to terminal. */
8302 terminal_coding.mode |= CODING_MODE_SAFE_ENCODING;
8303 /* Characer composition should be disabled. */
8304 terminal_coding.common_flags &= ~CODING_ANNOTATE_COMPOSITION_MASK;
8305 terminal_coding.src_multibyte = 1;
8306 terminal_coding.dst_multibyte = 0;
8307 return Qnil;
8308}
8309
8310DEFUN ("set-safe-terminal-coding-system-internal",
8311 Fset_safe_terminal_coding_system_internal,
8312 Sset_safe_terminal_coding_system_internal, 1, 1, 0,
8313 doc: /* Internal use only. */)
8314 (coding_system)
8315 Lisp_Object coding_system;
8316{
8317 CHECK_SYMBOL (coding_system);
8318 setup_coding_system (Fcheck_coding_system (coding_system),
8319 &safe_terminal_coding);
8320 /* Characer composition should be disabled. */
8321 safe_terminal_coding.common_flags &= ~CODING_ANNOTATE_COMPOSITION_MASK;
8322 safe_terminal_coding.src_multibyte = 1;
8323 safe_terminal_coding.dst_multibyte = 0;
8324 return Qnil;
8325}
8326
8327DEFUN ("terminal-coding-system",
8328 Fterminal_coding_system, Sterminal_coding_system, 0, 0, 0,
8329 doc: /* Return coding system specified for terminal output. */)
8330 ()
8331{
8332 Lisp_Object coding_system;
8333
8334 coding_system = CODING_ID_NAME (terminal_coding.id);
8335 /* For backward compatibility, return nil if it is `undecided'. */
8336 return (! EQ (coding_system, Qundecided) ? coding_system : Qnil);
8337}
8338
8339DEFUN ("set-keyboard-coding-system-internal",
8340 Fset_keyboard_coding_system_internal,
8341 Sset_keyboard_coding_system_internal, 1, 1, 0,
8342 doc: /* Internal use only. */)
8343 (coding_system)
8344 Lisp_Object coding_system;
8345{
8346 CHECK_SYMBOL (coding_system);
8347 setup_coding_system (Fcheck_coding_system (coding_system),
8348 &keyboard_coding);
8349 /* Characer composition should be disabled. */
8350 keyboard_coding.common_flags &= ~CODING_ANNOTATE_COMPOSITION_MASK;
8351 return Qnil;
8352}
8353
8354DEFUN ("keyboard-coding-system",
8355 Fkeyboard_coding_system, Skeyboard_coding_system, 0, 0, 0,
8356 doc: /* Return coding system specified for decoding keyboard input. */)
8357 ()
8358{
8359 return CODING_ID_NAME (keyboard_coding.id);
8360}
8361
8362\f
8363DEFUN ("find-operation-coding-system", Ffind_operation_coding_system,
8364 Sfind_operation_coding_system, 1, MANY, 0,
8365 doc: /* Choose a coding system for an operation based on the target name.
8366The value names a pair of coding systems: (DECODING-SYSTEM . ENCODING-SYSTEM).
8367DECODING-SYSTEM is the coding system to use for decoding
8368\(in case OPERATION does decoding), and ENCODING-SYSTEM is the coding system
8369for encoding (in case OPERATION does encoding).
8370
8371The first argument OPERATION specifies an I/O primitive:
8372 For file I/O, `insert-file-contents' or `write-region'.
8373 For process I/O, `call-process', `call-process-region', or `start-process'.
8374 For network I/O, `open-network-stream'.
8375
8376The remaining arguments should be the same arguments that were passed
8377to the primitive. Depending on which primitive, one of those arguments
8378is selected as the TARGET. For example, if OPERATION does file I/O,
8379whichever argument specifies the file name is TARGET.
8380
8381TARGET has a meaning which depends on OPERATION:
8382 For file I/O, TARGET is a file name (except for the special case below).
8383 For process I/O, TARGET is a process name.
8384 For network I/O, TARGET is a service name or a port number
8385
8386This function looks up what specified for TARGET in,
8387`file-coding-system-alist', `process-coding-system-alist',
8388or `network-coding-system-alist' depending on OPERATION.
8389They may specify a coding system, a cons of coding systems,
8390or a function symbol to call.
8391In the last case, we call the function with one argument,
8392which is a list of all the arguments given to this function.
8393
8394If OPERATION is `insert-file-contents', the argument corresponding to
8395TARGET may be a cons (FILENAME . BUFFER). In that case, FILENAME is a
8396file name to look up, and BUFFER is a buffer that contains the file's
8397contents (not yet decoded). If `file-coding-system-alist' specifies a
8398function to call for FILENAME, that function should examine the
8399contents of BUFFER instead of reading the file.
8400
8401usage: (find-operation-coding-system OPERATION ARGUMENTS ...) */)
8402 (nargs, args)
8403 int nargs;
8404 Lisp_Object *args;
8405{
8406 Lisp_Object operation, target_idx, target, val;
8407 register Lisp_Object chain;
8408
8409 if (nargs < 2)
8410 error ("Too few arguments");
8411 operation = args[0];
8412 if (!SYMBOLP (operation)
8413 || !INTEGERP (target_idx = Fget (operation, Qtarget_idx)))
8414 error ("Invalid first arguement");
8415 if (nargs < 1 + XINT (target_idx))
8416 error ("Too few arguments for operation: %s",
8417 SDATA (SYMBOL_NAME (operation)));
8418 target = args[XINT (target_idx) + 1];
8419 if (!(STRINGP (target)
8420 || (EQ (operation, Qinsert_file_contents) && CONSP (target)
8421 && STRINGP (XCAR (target)) && BUFFERP (XCDR (target)))
8422 || (EQ (operation, Qopen_network_stream) && INTEGERP (target))))
8423 error ("Invalid %dth argument", XINT (target_idx) + 1);
8424 if (CONSP (target))
8425 target = XCAR (target);
8426
8427 chain = ((EQ (operation, Qinsert_file_contents)
8428 || EQ (operation, Qwrite_region))
8429 ? Vfile_coding_system_alist
8430 : (EQ (operation, Qopen_network_stream)
8431 ? Vnetwork_coding_system_alist
8432 : Vprocess_coding_system_alist));
8433 if (NILP (chain))
8434 return Qnil;
8435
8436 for (; CONSP (chain); chain = XCDR (chain))
8437 {
8438 Lisp_Object elt;
8439
8440 elt = XCAR (chain);
8441 if (CONSP (elt)
8442 && ((STRINGP (target)
8443 && STRINGP (XCAR (elt))
8444 && fast_string_match (XCAR (elt), target) >= 0)
8445 || (INTEGERP (target) && EQ (target, XCAR (elt)))))
8446 {
8447 val = XCDR (elt);
8448 /* Here, if VAL is both a valid coding system and a valid
8449 function symbol, we return VAL as a coding system. */
8450 if (CONSP (val))
8451 return val;
8452 if (! SYMBOLP (val))
8453 return Qnil;
8454 if (! NILP (Fcoding_system_p (val)))
8455 return Fcons (val, val);
8456 if (! NILP (Ffboundp (val)))
8457 {
8458 /* We use call1 rather than safe_call1
8459 so as to get bug reports about functions called here
8460 which don't handle the current interface. */
8461 val = call1 (val, Flist (nargs, args));
8462 if (CONSP (val))
8463 return val;
8464 if (SYMBOLP (val) && ! NILP (Fcoding_system_p (val)))
8465 return Fcons (val, val);
8466 }
8467 return Qnil;
8468 }
8469 }
8470 return Qnil;
8471}
8472
8473DEFUN ("set-coding-system-priority", Fset_coding_system_priority,
8474 Sset_coding_system_priority, 0, MANY, 0,
8475 doc: /* Assign higher priority to the coding systems given as arguments.
8476If multiple coding systems belongs to the same category,
8477all but the first one are ignored.
8478
8479usage: (set-coding-system-priority ...) */)
8480 (nargs, args)
8481 int nargs;
8482 Lisp_Object *args;
8483{
8484 int i, j;
8485 int changed[coding_category_max];
8486 enum coding_category priorities[coding_category_max];
8487
8488 bzero (changed, sizeof changed);
8489
8490 for (i = j = 0; i < nargs; i++)
8491 {
8492 enum coding_category category;
8493 Lisp_Object spec, attrs;
8494
8495 CHECK_CODING_SYSTEM_GET_SPEC (args[i], spec);
8496 attrs = AREF (spec, 0);
8497 category = XINT (CODING_ATTR_CATEGORY (attrs));
8498 if (changed[category])
8499 /* Ignore this coding system because a coding system of the
8500 same category already had a higher priority. */
8501 continue;
8502 changed[category] = 1;
8503 priorities[j++] = category;
8504 if (coding_categories[category].id >= 0
8505 && ! EQ (args[i], CODING_ID_NAME (coding_categories[category].id)))
8506 setup_coding_system (args[i], &coding_categories[category]);
8507 Fset (AREF (Vcoding_category_table, category), args[i]);
8508 }
8509
8510 /* Now we have decided top J priorities. Reflect the order of the
8511 original priorities to the remaining priorities. */
8512
8513 for (i = j, j = 0; i < coding_category_max; i++, j++)
8514 {
8515 while (j < coding_category_max
8516 && changed[coding_priorities[j]])
8517 j++;
8518 if (j == coding_category_max)
8519 abort ();
8520 priorities[i] = coding_priorities[j];
8521 }
8522
8523 bcopy (priorities, coding_priorities, sizeof priorities);
8524
8525 /* Update `coding-category-list'. */
8526 Vcoding_category_list = Qnil;
8527 for (i = coding_category_max - 1; i >= 0; i--)
8528 Vcoding_category_list
8529 = Fcons (AREF (Vcoding_category_table, priorities[i]),
8530 Vcoding_category_list);
8531
8532 return Qnil;
8533}
8534
8535DEFUN ("coding-system-priority-list", Fcoding_system_priority_list,
8536 Scoding_system_priority_list, 0, 1, 0,
8537 doc: /* Return a list of coding systems ordered by their priorities.
8538HIGHESTP non-nil means just return the highest priority one. */)
8539 (highestp)
8540 Lisp_Object highestp;
8541{
8542 int i;
8543 Lisp_Object val;
8544
8545 for (i = 0, val = Qnil; i < coding_category_max; i++)
8546 {
8547 enum coding_category category = coding_priorities[i];
8548 int id = coding_categories[category].id;
8549 Lisp_Object attrs;
8550
8551 if (id < 0)
8552 continue;
8553 attrs = CODING_ID_ATTRS (id);
8554 if (! NILP (highestp))
8555 return CODING_ATTR_BASE_NAME (attrs);
8556 val = Fcons (CODING_ATTR_BASE_NAME (attrs), val);
8557 }
8558 return Fnreverse (val);
8559}
8560
8561static char *suffixes[] = { "-unix", "-dos", "-mac" };
8562
8563static Lisp_Object
8564make_subsidiaries (base)
8565 Lisp_Object base;
8566{
8567 Lisp_Object subsidiaries;
8568 int base_name_len = SBYTES (SYMBOL_NAME (base));
8569 char *buf = (char *) alloca (base_name_len + 6);
8570 int i;
8571
8572 bcopy (SDATA (SYMBOL_NAME (base)), buf, base_name_len);
8573 subsidiaries = Fmake_vector (make_number (3), Qnil);
8574 for (i = 0; i < 3; i++)
8575 {
8576 bcopy (suffixes[i], buf + base_name_len, strlen (suffixes[i]) + 1);
8577 ASET (subsidiaries, i, intern (buf));
8578 }
8579 return subsidiaries;
8580}
8581
8582
8583DEFUN ("define-coding-system-internal", Fdefine_coding_system_internal,
8584 Sdefine_coding_system_internal, coding_arg_max, MANY, 0,
8585 doc: /* For internal use only.
8586usage: (define-coding-system-internal ...) */)
8587 (nargs, args)
8588 int nargs;
8589 Lisp_Object *args;
8590{
8591 Lisp_Object name;
8592 Lisp_Object spec_vec; /* [ ATTRS ALIASE EOL_TYPE ] */
8593 Lisp_Object attrs; /* Vector of attributes. */
8594 Lisp_Object eol_type;
8595 Lisp_Object aliases;
8596 Lisp_Object coding_type, charset_list, safe_charsets;
8597 enum coding_category category;
8598 Lisp_Object tail, val;
8599 int max_charset_id = 0;
8600 int i;
8601
8602 if (nargs < coding_arg_max)
8603 goto short_args;
8604
8605 attrs = Fmake_vector (make_number (coding_attr_last_index), Qnil);
8606
8607 name = args[coding_arg_name];
8608 CHECK_SYMBOL (name);
8609 CODING_ATTR_BASE_NAME (attrs) = name;
8610
8611 val = args[coding_arg_mnemonic];
8612 if (! STRINGP (val))
8613 CHECK_CHARACTER (val);
8614 CODING_ATTR_MNEMONIC (attrs) = val;
8615
8616 coding_type = args[coding_arg_coding_type];
8617 CHECK_SYMBOL (coding_type);
8618 CODING_ATTR_TYPE (attrs) = coding_type;
8619
8620 charset_list = args[coding_arg_charset_list];
8621 if (SYMBOLP (charset_list))
8622 {
8623 if (EQ (charset_list, Qiso_2022))
8624 {
8625 if (! EQ (coding_type, Qiso_2022))
8626 error ("Invalid charset-list");
8627 charset_list = Viso_2022_charset_list;
8628 }
8629 else if (EQ (charset_list, Qemacs_mule))
8630 {
8631 if (! EQ (coding_type, Qemacs_mule))
8632 error ("Invalid charset-list");
8633 charset_list = Vemacs_mule_charset_list;
8634 }
8635 for (tail = charset_list; CONSP (tail); tail = XCDR (tail))
8636 if (max_charset_id < XFASTINT (XCAR (tail)))
8637 max_charset_id = XFASTINT (XCAR (tail));
8638 }
8639 else
8640 {
8641 charset_list = Fcopy_sequence (charset_list);
8642 for (tail = charset_list; !NILP (tail); tail = Fcdr (tail))
8643 {
8644 struct charset *charset;
8645
8646 val = Fcar (tail);
8647 CHECK_CHARSET_GET_CHARSET (val, charset);
8648 if (EQ (coding_type, Qiso_2022)
8649 ? CHARSET_ISO_FINAL (charset) < 0
8650 : EQ (coding_type, Qemacs_mule)
8651 ? CHARSET_EMACS_MULE_ID (charset) < 0
8652 : 0)
8653 error ("Can't handle charset `%s'",
8654 SDATA (SYMBOL_NAME (CHARSET_NAME (charset))));
8655
8656 XSETCAR (tail, make_number (charset->id));
8657 if (max_charset_id < charset->id)
8658 max_charset_id = charset->id;
8659 }
8660 }
8661 CODING_ATTR_CHARSET_LIST (attrs) = charset_list;
8662
8663 safe_charsets = Fmake_string (make_number (max_charset_id + 1),
8664 make_number (255));
8665 for (tail = charset_list; CONSP (tail); tail = XCDR (tail))
8666 SSET (safe_charsets, XFASTINT (XCAR (tail)), 0);
8667 CODING_ATTR_SAFE_CHARSETS (attrs) = safe_charsets;
8668
8669 CODING_ATTR_ASCII_COMPAT (attrs) = args[coding_arg_ascii_compatible_p];
8670
8671 val = args[coding_arg_decode_translation_table];
8672 if (! CHAR_TABLE_P (val) && ! CONSP (val))
8673 CHECK_SYMBOL (val);
8674 CODING_ATTR_DECODE_TBL (attrs) = val;
8675
8676 val = args[coding_arg_encode_translation_table];
8677 if (! CHAR_TABLE_P (val) && ! CONSP (val))
8678 CHECK_SYMBOL (val);
8679 CODING_ATTR_ENCODE_TBL (attrs) = val;
8680
8681 val = args[coding_arg_post_read_conversion];
8682 CHECK_SYMBOL (val);
8683 CODING_ATTR_POST_READ (attrs) = val;
8684
8685 val = args[coding_arg_pre_write_conversion];
8686 CHECK_SYMBOL (val);
8687 CODING_ATTR_PRE_WRITE (attrs) = val;
8688
8689 val = args[coding_arg_default_char];
8690 if (NILP (val))
8691 CODING_ATTR_DEFAULT_CHAR (attrs) = make_number (' ');
8692 else
8693 {
8694 CHECK_CHARACTER (val);
8695 CODING_ATTR_DEFAULT_CHAR (attrs) = val;
8696 }
8697
8698 val = args[coding_arg_for_unibyte];
8699 CODING_ATTR_FOR_UNIBYTE (attrs) = NILP (val) ? Qnil : Qt;
8700
8701 val = args[coding_arg_plist];
8702 CHECK_LIST (val);
8703 CODING_ATTR_PLIST (attrs) = val;
8704
8705 if (EQ (coding_type, Qcharset))
8706 {
8707 /* Generate a lisp vector of 256 elements. Each element is nil,
8708 integer, or a list of charset IDs.
8709
8710 If Nth element is nil, the byte code N is invalid in this
8711 coding system.
8712
8713 If Nth element is a number NUM, N is the first byte of a
8714 charset whose ID is NUM.
8715
8716 If Nth element is a list of charset IDs, N is the first byte
8717 of one of them. The list is sorted by dimensions of the
8718 charsets. A charset of smaller dimension comes firtst. */
8719 val = Fmake_vector (make_number (256), Qnil);
8720
8721 for (tail = charset_list; CONSP (tail); tail = XCDR (tail))
8722 {
8723 struct charset *charset = CHARSET_FROM_ID (XFASTINT (XCAR (tail)));
8724 int dim = CHARSET_DIMENSION (charset);
8725 int idx = (dim - 1) * 4;
8726
8727 if (CHARSET_ASCII_COMPATIBLE_P (charset))
8728 CODING_ATTR_ASCII_COMPAT (attrs) = Qt;
8729
8730 for (i = charset->code_space[idx];
8731 i <= charset->code_space[idx + 1]; i++)
8732 {
8733 Lisp_Object tmp, tmp2;
8734 int dim2;
8735
8736 tmp = AREF (val, i);
8737 if (NILP (tmp))
8738 tmp = XCAR (tail);
8739 else if (NUMBERP (tmp))
8740 {
8741 dim2 = CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (tmp)));
8742 if (dim < dim2)
8743 tmp = Fcons (XCAR (tail), Fcons (tmp, Qnil));
8744 else
8745 tmp = Fcons (tmp, Fcons (XCAR (tail), Qnil));
8746 }
8747 else
8748 {
8749 for (tmp2 = tmp; CONSP (tmp2); tmp2 = XCDR (tmp2))
8750 {
8751 dim2 = CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (XCAR (tmp2))));
8752 if (dim < dim2)
8753 break;
8754 }
8755 if (NILP (tmp2))
8756 tmp = nconc2 (tmp, Fcons (XCAR (tail), Qnil));
8757 else
8758 {
8759 XSETCDR (tmp2, Fcons (XCAR (tmp2), XCDR (tmp2)));
8760 XSETCAR (tmp2, XCAR (tail));
8761 }
8762 }
8763 ASET (val, i, tmp);
8764 }
8765 }
8766 ASET (attrs, coding_attr_charset_valids, val);
8767 category = coding_category_charset;
8768 }
8769 else if (EQ (coding_type, Qccl))
8770 {
8771 Lisp_Object valids;
8772
8773 if (nargs < coding_arg_ccl_max)
8774 goto short_args;
8775
8776 val = args[coding_arg_ccl_decoder];
8777 CHECK_CCL_PROGRAM (val);
8778 if (VECTORP (val))
8779 val = Fcopy_sequence (val);
8780 ASET (attrs, coding_attr_ccl_decoder, val);
8781
8782 val = args[coding_arg_ccl_encoder];
8783 CHECK_CCL_PROGRAM (val);
8784 if (VECTORP (val))
8785 val = Fcopy_sequence (val);
8786 ASET (attrs, coding_attr_ccl_encoder, val);
8787
8788 val = args[coding_arg_ccl_valids];
8789 valids = Fmake_string (make_number (256), make_number (0));
8790 for (tail = val; !NILP (tail); tail = Fcdr (tail))
8791 {
8792 int from, to;
8793
8794 val = Fcar (tail);
8795 if (INTEGERP (val))
8796 {
8797 from = to = XINT (val);
8798 if (from < 0 || from > 255)
8799 args_out_of_range_3 (val, make_number (0), make_number (255));
8800 }
8801 else
8802 {
8803 CHECK_CONS (val);
8804 CHECK_NATNUM_CAR (val);
8805 CHECK_NATNUM_CDR (val);
8806 from = XINT (XCAR (val));
8807 if (from > 255)
8808 args_out_of_range_3 (XCAR (val),
8809 make_number (0), make_number (255));
8810 to = XINT (XCDR (val));
8811 if (to < from || to > 255)
8812 args_out_of_range_3 (XCDR (val),
8813 XCAR (val), make_number (255));
8814 }
8815 for (i = from; i <= to; i++)
8816 SSET (valids, i, 1);
8817 }
8818 ASET (attrs, coding_attr_ccl_valids, valids);
8819
8820 category = coding_category_ccl;
8821 }
8822 else if (EQ (coding_type, Qutf_16))
8823 {
8824 Lisp_Object bom, endian;
8825
8826 CODING_ATTR_ASCII_COMPAT (attrs) = Qnil;
8827
8828 if (nargs < coding_arg_utf16_max)
8829 goto short_args;
8830
8831 bom = args[coding_arg_utf16_bom];
8832 if (! NILP (bom) && ! EQ (bom, Qt))
8833 {
8834 CHECK_CONS (bom);
8835 val = XCAR (bom);
8836 CHECK_CODING_SYSTEM (val);
8837 val = XCDR (bom);
8838 CHECK_CODING_SYSTEM (val);
8839 }
8840 ASET (attrs, coding_attr_utf_16_bom, bom);
8841
8842 endian = args[coding_arg_utf16_endian];
8843 CHECK_SYMBOL (endian);
8844 if (NILP (endian))
8845 endian = Qbig;
8846 else if (! EQ (endian, Qbig) && ! EQ (endian, Qlittle))
8847 error ("Invalid endian: %s", SDATA (SYMBOL_NAME (endian)));
8848 ASET (attrs, coding_attr_utf_16_endian, endian);
8849
8850 category = (CONSP (bom)
8851 ? coding_category_utf_16_auto
8852 : NILP (bom)
8853 ? (EQ (endian, Qbig)
8854 ? coding_category_utf_16_be_nosig
8855 : coding_category_utf_16_le_nosig)
8856 : (EQ (endian, Qbig)
8857 ? coding_category_utf_16_be
8858 : coding_category_utf_16_le));
8859 }
8860 else if (EQ (coding_type, Qiso_2022))
8861 {
8862 Lisp_Object initial, reg_usage, request, flags;
8863 int i;
8864
8865 if (nargs < coding_arg_iso2022_max)
8866 goto short_args;
8867
8868 initial = Fcopy_sequence (args[coding_arg_iso2022_initial]);
8869 CHECK_VECTOR (initial);
8870 for (i = 0; i < 4; i++)
8871 {
8872 val = Faref (initial, make_number (i));
8873 if (! NILP (val))
8874 {
8875 struct charset *charset;
8876
8877 CHECK_CHARSET_GET_CHARSET (val, charset);
8878 ASET (initial, i, make_number (CHARSET_ID (charset)));
8879 if (i == 0 && CHARSET_ASCII_COMPATIBLE_P (charset))
8880 CODING_ATTR_ASCII_COMPAT (attrs) = Qt;
8881 }
8882 else
8883 ASET (initial, i, make_number (-1));
8884 }
8885
8886 reg_usage = args[coding_arg_iso2022_reg_usage];
8887 CHECK_CONS (reg_usage);
8888 CHECK_NUMBER_CAR (reg_usage);
8889 CHECK_NUMBER_CDR (reg_usage);
8890
8891 request = Fcopy_sequence (args[coding_arg_iso2022_request]);
8892 for (tail = request; ! NILP (tail); tail = Fcdr (tail))
8893 {
8894 int id;
8895 Lisp_Object tmp;
8896
8897 val = Fcar (tail);
8898 CHECK_CONS (val);
8899 tmp = XCAR (val);
8900 CHECK_CHARSET_GET_ID (tmp, id);
8901 CHECK_NATNUM_CDR (val);
8902 if (XINT (XCDR (val)) >= 4)
8903 error ("Invalid graphic register number: %d", XINT (XCDR (val)));
8904 XSETCAR (val, make_number (id));
8905 }
8906
8907 flags = args[coding_arg_iso2022_flags];
8908 CHECK_NATNUM (flags);
8909 i = XINT (flags);
8910 if (EQ (args[coding_arg_charset_list], Qiso_2022))
8911 flags = make_number (i | CODING_ISO_FLAG_FULL_SUPPORT);
8912
8913 ASET (attrs, coding_attr_iso_initial, initial);
8914 ASET (attrs, coding_attr_iso_usage, reg_usage);
8915 ASET (attrs, coding_attr_iso_request, request);
8916 ASET (attrs, coding_attr_iso_flags, flags);
8917 setup_iso_safe_charsets (attrs);
8918
8919 if (i & CODING_ISO_FLAG_SEVEN_BITS)
8920 category = ((i & (CODING_ISO_FLAG_LOCKING_SHIFT
8921 | CODING_ISO_FLAG_SINGLE_SHIFT))
8922 ? coding_category_iso_7_else
8923 : EQ (args[coding_arg_charset_list], Qiso_2022)
8924 ? coding_category_iso_7
8925 : coding_category_iso_7_tight);
8926 else
8927 {
8928 int id = XINT (AREF (initial, 1));
8929
8930 category = (((i & CODING_ISO_FLAG_LOCKING_SHIFT)
8931 || EQ (args[coding_arg_charset_list], Qiso_2022)
8932 || id < 0)
8933 ? coding_category_iso_8_else
8934 : (CHARSET_DIMENSION (CHARSET_FROM_ID (id)) == 1)
8935 ? coding_category_iso_8_1
8936 : coding_category_iso_8_2);
8937 }
8938 if (category != coding_category_iso_8_1
8939 && category != coding_category_iso_8_2)
8940 CODING_ATTR_ASCII_COMPAT (attrs) = Qnil;
8941 }
8942 else if (EQ (coding_type, Qemacs_mule))
8943 {
8944 if (EQ (args[coding_arg_charset_list], Qemacs_mule))
8945 ASET (attrs, coding_attr_emacs_mule_full, Qt);
8946 CODING_ATTR_ASCII_COMPAT (attrs) = Qt;
8947 category = coding_category_emacs_mule;
8948 }
8949 else if (EQ (coding_type, Qshift_jis))
8950 {
8951
8952 struct charset *charset;
8953
8954 if (XINT (Flength (charset_list)) != 3
8955 && XINT (Flength (charset_list)) != 4)
8956 error ("There should be three or four charsets");
8957
8958 charset = CHARSET_FROM_ID (XINT (XCAR (charset_list)));
8959 if (CHARSET_DIMENSION (charset) != 1)
8960 error ("Dimension of charset %s is not one",
8961 SDATA (SYMBOL_NAME (CHARSET_NAME (charset))));
8962 if (CHARSET_ASCII_COMPATIBLE_P (charset))
8963 CODING_ATTR_ASCII_COMPAT (attrs) = Qt;
8964
8965 charset_list = XCDR (charset_list);
8966 charset = CHARSET_FROM_ID (XINT (XCAR (charset_list)));
8967 if (CHARSET_DIMENSION (charset) != 1)
8968 error ("Dimension of charset %s is not one",
8969 SDATA (SYMBOL_NAME (CHARSET_NAME (charset))));
8970
8971 charset_list = XCDR (charset_list);
8972 charset = CHARSET_FROM_ID (XINT (XCAR (charset_list)));
8973 if (CHARSET_DIMENSION (charset) != 2)
8974 error ("Dimension of charset %s is not two",
8975 SDATA (SYMBOL_NAME (CHARSET_NAME (charset))));
8976
8977 charset_list = XCDR (charset_list);
8978 if (! NILP (charset_list))
8979 {
8980 charset = CHARSET_FROM_ID (XINT (XCAR (charset_list)));
8981 if (CHARSET_DIMENSION (charset) != 2)
8982 error ("Dimension of charset %s is not two",
8983 SDATA (SYMBOL_NAME (CHARSET_NAME (charset))));
8984 }
8985
8986 category = coding_category_sjis;
8987 Vsjis_coding_system = name;
8988 }
8989 else if (EQ (coding_type, Qbig5))
8990 {
8991 struct charset *charset;
8992
8993 if (XINT (Flength (charset_list)) != 2)
8994 error ("There should be just two charsets");
8995
8996 charset = CHARSET_FROM_ID (XINT (XCAR (charset_list)));
8997 if (CHARSET_DIMENSION (charset) != 1)
8998 error ("Dimension of charset %s is not one",
8999 SDATA (SYMBOL_NAME (CHARSET_NAME (charset))));
9000 if (CHARSET_ASCII_COMPATIBLE_P (charset))
9001 CODING_ATTR_ASCII_COMPAT (attrs) = Qt;
9002
9003 charset_list = XCDR (charset_list);
9004 charset = CHARSET_FROM_ID (XINT (XCAR (charset_list)));
9005 if (CHARSET_DIMENSION (charset) != 2)
9006 error ("Dimension of charset %s is not two",
9007 SDATA (SYMBOL_NAME (CHARSET_NAME (charset))));
9008
9009 category = coding_category_big5;
9010 Vbig5_coding_system = name;
9011 }
9012 else if (EQ (coding_type, Qraw_text))
9013 {
9014 category = coding_category_raw_text;
9015 CODING_ATTR_ASCII_COMPAT (attrs) = Qt;
9016 }
9017 else if (EQ (coding_type, Qutf_8))
9018 {
9019 category = coding_category_utf_8;
9020 CODING_ATTR_ASCII_COMPAT (attrs) = Qt;
9021 }
9022 else if (EQ (coding_type, Qundecided))
9023 category = coding_category_undecided;
9024 else
9025 error ("Invalid coding system type: %s",
9026 SDATA (SYMBOL_NAME (coding_type)));
9027
9028 CODING_ATTR_CATEGORY (attrs) = make_number (category);
9029 CODING_ATTR_PLIST (attrs)
9030 = Fcons (QCcategory, Fcons (AREF (Vcoding_category_table, category),
9031 CODING_ATTR_PLIST (attrs)));
9032 CODING_ATTR_PLIST (attrs)
9033 = Fcons (QCascii_compatible_p,
9034 Fcons (CODING_ATTR_ASCII_COMPAT (attrs),
9035 CODING_ATTR_PLIST (attrs)));
9036
9037 eol_type = args[coding_arg_eol_type];
9038 if (! NILP (eol_type)
9039 && ! EQ (eol_type, Qunix)
9040 && ! EQ (eol_type, Qdos)
9041 && ! EQ (eol_type, Qmac))
9042 error ("Invalid eol-type");
9043
9044 aliases = Fcons (name, Qnil);
9045
9046 if (NILP (eol_type))
9047 {
9048 eol_type = make_subsidiaries (name);
9049 for (i = 0; i < 3; i++)
9050 {
9051 Lisp_Object this_spec, this_name, this_aliases, this_eol_type;
9052
9053 this_name = AREF (eol_type, i);
9054 this_aliases = Fcons (this_name, Qnil);
9055 this_eol_type = (i == 0 ? Qunix : i == 1 ? Qdos : Qmac);
9056 this_spec = Fmake_vector (make_number (3), attrs);
9057 ASET (this_spec, 1, this_aliases);
9058 ASET (this_spec, 2, this_eol_type);
9059 Fputhash (this_name, this_spec, Vcoding_system_hash_table);
9060 Vcoding_system_list = Fcons (this_name, Vcoding_system_list);
9061 val = Fassoc (Fsymbol_name (this_name), Vcoding_system_alist);
9062 if (NILP (val))
9063 Vcoding_system_alist
9064 = Fcons (Fcons (Fsymbol_name (this_name), Qnil),
9065 Vcoding_system_alist);
9066 }
9067 }
9068
9069 spec_vec = Fmake_vector (make_number (3), attrs);
9070 ASET (spec_vec, 1, aliases);
9071 ASET (spec_vec, 2, eol_type);
9072
9073 Fputhash (name, spec_vec, Vcoding_system_hash_table);
9074 Vcoding_system_list = Fcons (name, Vcoding_system_list);
9075 val = Fassoc (Fsymbol_name (name), Vcoding_system_alist);
9076 if (NILP (val))
9077 Vcoding_system_alist = Fcons (Fcons (Fsymbol_name (name), Qnil),
9078 Vcoding_system_alist);
9079
9080 {
9081 int id = coding_categories[category].id;
9082
9083 if (id < 0 || EQ (name, CODING_ID_NAME (id)))
9084 setup_coding_system (name, &coding_categories[category]);
9085 }
9086
9087 return Qnil;
9088
9089 short_args:
9090 return Fsignal (Qwrong_number_of_arguments,
9091 Fcons (intern ("define-coding-system-internal"),
9092 make_number (nargs)));
9093}
9094
9095
9096DEFUN ("coding-system-put", Fcoding_system_put, Scoding_system_put,
9097 3, 3, 0,
9098 doc: /* Change value in CODING-SYSTEM's property list PROP to VAL. */)
9099 (coding_system, prop, val)
9100 Lisp_Object coding_system, prop, val;
9101{
9102 Lisp_Object spec, attrs;
9103
9104 CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec);
9105 attrs = AREF (spec, 0);
9106 if (EQ (prop, QCmnemonic))
9107 {
9108 if (! STRINGP (val))
9109 CHECK_CHARACTER (val);
9110 CODING_ATTR_MNEMONIC (attrs) = val;
9111 }
9112 else if (EQ (prop, QCdefalut_char))
9113 {
9114 if (NILP (val))
9115 val = make_number (' ');
9116 else
9117 CHECK_CHARACTER (val);
9118 CODING_ATTR_DEFAULT_CHAR (attrs) = val;
9119 }
9120 else if (EQ (prop, QCdecode_translation_table))
9121 {
9122 if (! CHAR_TABLE_P (val) && ! CONSP (val))
9123 CHECK_SYMBOL (val);
9124 CODING_ATTR_DECODE_TBL (attrs) = val;
9125 }
9126 else if (EQ (prop, QCencode_translation_table))
9127 {
9128 if (! CHAR_TABLE_P (val) && ! CONSP (val))
9129 CHECK_SYMBOL (val);
9130 CODING_ATTR_ENCODE_TBL (attrs) = val;
9131 }
9132 else if (EQ (prop, QCpost_read_conversion))
9133 {
9134 CHECK_SYMBOL (val);
9135 CODING_ATTR_POST_READ (attrs) = val;
9136 }
9137 else if (EQ (prop, QCpre_write_conversion))
9138 {
9139 CHECK_SYMBOL (val);
9140 CODING_ATTR_PRE_WRITE (attrs) = val;
9141 }
9142 else if (EQ (prop, QCascii_compatible_p))
9143 {
9144 CODING_ATTR_ASCII_COMPAT (attrs) = val;
9145 }
9146
9147 CODING_ATTR_PLIST (attrs)
9148 = Fplist_put (CODING_ATTR_PLIST (attrs), prop, val);
9149 return val;
9150}
9151
9152
9153DEFUN ("define-coding-system-alias", Fdefine_coding_system_alias,
9154 Sdefine_coding_system_alias, 2, 2, 0,
9155 doc: /* Define ALIAS as an alias for CODING-SYSTEM. */)
9156 (alias, coding_system)
9157 Lisp_Object alias, coding_system;
9158{
9159 Lisp_Object spec, aliases, eol_type, val;
9160
9161 CHECK_SYMBOL (alias);
9162 CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec);
9163 aliases = AREF (spec, 1);
9164 /* ALISES should be a list of length more than zero, and the first
9165 element is a base coding system. Append ALIAS at the tail of the
9166 list. */
9167 while (!NILP (XCDR (aliases)))
9168 aliases = XCDR (aliases);
9169 XSETCDR (aliases, Fcons (alias, Qnil));
9170
9171 eol_type = AREF (spec, 2);
9172 if (VECTORP (eol_type))
9173 {
9174 Lisp_Object subsidiaries;
9175 int i;
9176
9177 subsidiaries = make_subsidiaries (alias);
9178 for (i = 0; i < 3; i++)
9179 Fdefine_coding_system_alias (AREF (subsidiaries, i),
9180 AREF (eol_type, i));
9181 }
9182
9183 Fputhash (alias, spec, Vcoding_system_hash_table);
9184 Vcoding_system_list = Fcons (alias, Vcoding_system_list);
9185 val = Fassoc (Fsymbol_name (alias), Vcoding_system_alist);
9186 if (NILP (val))
9187 Vcoding_system_alist = Fcons (Fcons (Fsymbol_name (alias), Qnil),
9188 Vcoding_system_alist);
9189
9190 return Qnil;
9191}
9192
9193DEFUN ("coding-system-base", Fcoding_system_base, Scoding_system_base,
9194 1, 1, 0,
9195 doc: /* Return the base of CODING-SYSTEM.
9196Any alias or subsidiary coding system is not a base coding system. */)
9197 (coding_system)
9198 Lisp_Object coding_system;
9199{
9200 Lisp_Object spec, attrs;
9201
9202 if (NILP (coding_system))
9203 return (Qno_conversion);
9204 CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec);
9205 attrs = AREF (spec, 0);
9206 return CODING_ATTR_BASE_NAME (attrs);
9207}
9208
9209DEFUN ("coding-system-plist", Fcoding_system_plist, Scoding_system_plist,
9210 1, 1, 0,
9211 doc: "Return the property list of CODING-SYSTEM.")
9212 (coding_system)
9213 Lisp_Object coding_system;
9214{
9215 Lisp_Object spec, attrs;
9216
9217 if (NILP (coding_system))
9218 coding_system = Qno_conversion;
9219 CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec);
9220 attrs = AREF (spec, 0);
9221 return CODING_ATTR_PLIST (attrs);
9222}
9223
9224
9225DEFUN ("coding-system-aliases", Fcoding_system_aliases, Scoding_system_aliases,
9226 1, 1, 0,
9227 doc: /* Return the list of aliases of CODING-SYSTEM. */)
9228 (coding_system)
9229 Lisp_Object coding_system;
9230{
9231 Lisp_Object spec;
9232
9233 if (NILP (coding_system))
9234 coding_system = Qno_conversion;
9235 CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec);
9236 return AREF (spec, 1);
9237}
9238
9239DEFUN ("coding-system-eol-type", Fcoding_system_eol_type,
9240 Scoding_system_eol_type, 1, 1, 0,
9241 doc: /* Return eol-type of CODING-SYSTEM.
9242An eol-type is integer 0, 1, 2, or a vector of coding systems.
9243
9244Integer values 0, 1, and 2 indicate a format of end-of-line; LF, CRLF,
9245and CR respectively.
9246
9247A vector value indicates that a format of end-of-line should be
9248detected automatically. Nth element of the vector is the subsidiary
9249coding system whose eol-type is N. */)
9250 (coding_system)
9251 Lisp_Object coding_system;
9252{
9253 Lisp_Object spec, eol_type;
9254 int n;
9255
9256 if (NILP (coding_system))
9257 coding_system = Qno_conversion;
9258 if (! CODING_SYSTEM_P (coding_system))
9259 return Qnil;
9260 spec = CODING_SYSTEM_SPEC (coding_system);
9261 eol_type = AREF (spec, 2);
9262 if (VECTORP (eol_type))
9263 return Fcopy_sequence (eol_type);
9264 n = EQ (eol_type, Qunix) ? 0 : EQ (eol_type, Qdos) ? 1 : 2;
9265 return make_number (n);
9266}
9267
9268#endif /* emacs */
9269
9270\f
9271/*** 9. Post-amble ***/
9272
9273void
9274init_coding_once ()
9275{
9276 int i;
9277
9278 for (i = 0; i < coding_category_max; i++)
9279 {
9280 coding_categories[i].id = -1;
9281 coding_priorities[i] = i;
9282 }
9283
9284 /* ISO2022 specific initialize routine. */
9285 for (i = 0; i < 0x20; i++)
9286 iso_code_class[i] = ISO_control_0;
9287 for (i = 0x21; i < 0x7F; i++)
9288 iso_code_class[i] = ISO_graphic_plane_0;
9289 for (i = 0x80; i < 0xA0; i++)
9290 iso_code_class[i] = ISO_control_1;
9291 for (i = 0xA1; i < 0xFF; i++)
9292 iso_code_class[i] = ISO_graphic_plane_1;
9293 iso_code_class[0x20] = iso_code_class[0x7F] = ISO_0x20_or_0x7F;
9294 iso_code_class[0xA0] = iso_code_class[0xFF] = ISO_0xA0_or_0xFF;
9295 iso_code_class[ISO_CODE_SO] = ISO_shift_out;
9296 iso_code_class[ISO_CODE_SI] = ISO_shift_in;
9297 iso_code_class[ISO_CODE_SS2_7] = ISO_single_shift_2_7;
9298 iso_code_class[ISO_CODE_ESC] = ISO_escape;
9299 iso_code_class[ISO_CODE_SS2] = ISO_single_shift_2;
9300 iso_code_class[ISO_CODE_SS3] = ISO_single_shift_3;
9301 iso_code_class[ISO_CODE_CSI] = ISO_control_sequence_introducer;
9302
9303 for (i = 0; i < 256; i++)
9304 {
9305 emacs_mule_bytes[i] = 1;
9306 }
9307 emacs_mule_bytes[EMACS_MULE_LEADING_CODE_PRIVATE_11] = 3;
9308 emacs_mule_bytes[EMACS_MULE_LEADING_CODE_PRIVATE_12] = 3;
9309 emacs_mule_bytes[EMACS_MULE_LEADING_CODE_PRIVATE_21] = 4;
9310 emacs_mule_bytes[EMACS_MULE_LEADING_CODE_PRIVATE_22] = 4;
9311}
9312
9313#ifdef emacs
9314
9315void
9316syms_of_coding ()
9317{
9318 staticpro (&Vcoding_system_hash_table);
9319 {
9320 Lisp_Object args[2];
9321 args[0] = QCtest;
9322 args[1] = Qeq;
9323 Vcoding_system_hash_table = Fmake_hash_table (2, args);
9324 }
9325
9326 staticpro (&Vsjis_coding_system);
9327 Vsjis_coding_system = Qnil;
9328
9329 staticpro (&Vbig5_coding_system);
9330 Vbig5_coding_system = Qnil;
9331
9332 staticpro (&Vcode_conversion_reused_workbuf);
9333 Vcode_conversion_reused_workbuf = Qnil;
9334
9335 staticpro (&Vcode_conversion_workbuf_name);
9336 Vcode_conversion_workbuf_name = build_string (" *code-conversion-work*");
9337
9338 reused_workbuf_in_use = 0;
9339
9340 DEFSYM (Qcharset, "charset");
9341 DEFSYM (Qtarget_idx, "target-idx");
9342 DEFSYM (Qcoding_system_history, "coding-system-history");
9343 Fset (Qcoding_system_history, Qnil);
9344
9345 /* Target FILENAME is the first argument. */
9346 Fput (Qinsert_file_contents, Qtarget_idx, make_number (0));
9347 /* Target FILENAME is the third argument. */
9348 Fput (Qwrite_region, Qtarget_idx, make_number (2));
9349
9350 DEFSYM (Qcall_process, "call-process");
9351 /* Target PROGRAM is the first argument. */
9352 Fput (Qcall_process, Qtarget_idx, make_number (0));
9353
9354 DEFSYM (Qcall_process_region, "call-process-region");
9355 /* Target PROGRAM is the third argument. */
9356 Fput (Qcall_process_region, Qtarget_idx, make_number (2));
9357
9358 DEFSYM (Qstart_process, "start-process");
9359 /* Target PROGRAM is the third argument. */
9360 Fput (Qstart_process, Qtarget_idx, make_number (2));
9361
9362 DEFSYM (Qopen_network_stream, "open-network-stream");
9363 /* Target SERVICE is the fourth argument. */
9364 Fput (Qopen_network_stream, Qtarget_idx, make_number (3));
9365
9366 DEFSYM (Qcoding_system, "coding-system");
9367 DEFSYM (Qcoding_aliases, "coding-aliases");
9368
9369 DEFSYM (Qeol_type, "eol-type");
9370 DEFSYM (Qunix, "unix");
9371 DEFSYM (Qdos, "dos");
9372
9373 DEFSYM (Qbuffer_file_coding_system, "buffer-file-coding-system");
9374 DEFSYM (Qpost_read_conversion, "post-read-conversion");
9375 DEFSYM (Qpre_write_conversion, "pre-write-conversion");
9376 DEFSYM (Qdefault_char, "default-char");
9377 DEFSYM (Qundecided, "undecided");
9378 DEFSYM (Qno_conversion, "no-conversion");
9379 DEFSYM (Qraw_text, "raw-text");
9380
9381 DEFSYM (Qiso_2022, "iso-2022");
9382
9383 DEFSYM (Qutf_8, "utf-8");
9384 DEFSYM (Qutf_8_emacs, "utf-8-emacs");
9385
9386 DEFSYM (Qutf_16, "utf-16");
9387 DEFSYM (Qbig, "big");
9388 DEFSYM (Qlittle, "little");
9389
9390 DEFSYM (Qshift_jis, "shift-jis");
9391 DEFSYM (Qbig5, "big5");
9392
9393 DEFSYM (Qcoding_system_p, "coding-system-p");
9394
9395 DEFSYM (Qcoding_system_error, "coding-system-error");
9396 Fput (Qcoding_system_error, Qerror_conditions,
9397 Fcons (Qcoding_system_error, Fcons (Qerror, Qnil)));
9398 Fput (Qcoding_system_error, Qerror_message,
9399 build_string ("Invalid coding system"));
9400
9401 /* Intern this now in case it isn't already done.
9402 Setting this variable twice is harmless.
9403 But don't staticpro it here--that is done in alloc.c. */
9404 Qchar_table_extra_slots = intern ("char-table-extra-slots");
9405
9406 DEFSYM (Qtranslation_table, "translation-table");
9407 Fput (Qtranslation_table, Qchar_table_extra_slots, make_number (2));
9408 DEFSYM (Qtranslation_table_id, "translation-table-id");
9409 DEFSYM (Qtranslation_table_for_decode, "translation-table-for-decode");
9410 DEFSYM (Qtranslation_table_for_encode, "translation-table-for-encode");
9411
9412 DEFSYM (Qvalid_codes, "valid-codes");
9413
9414 DEFSYM (Qemacs_mule, "emacs-mule");
9415
9416 DEFSYM (QCcategory, ":category");
9417 DEFSYM (QCmnemonic, ":mnemonic");
9418 DEFSYM (QCdefalut_char, ":default-char");
9419 DEFSYM (QCdecode_translation_table, ":decode-translation-table");
9420 DEFSYM (QCencode_translation_table, ":encode-translation-table");
9421 DEFSYM (QCpost_read_conversion, ":post-read-conversion");
9422 DEFSYM (QCpre_write_conversion, ":pre-write-conversion");
9423 DEFSYM (QCascii_compatible_p, ":ascii-compatible-p");
9424
9425 Vcoding_category_table
9426 = Fmake_vector (make_number (coding_category_max), Qnil);
9427 staticpro (&Vcoding_category_table);
9428 /* Followings are target of code detection. */
9429 ASET (Vcoding_category_table, coding_category_iso_7,
9430 intern ("coding-category-iso-7"));
9431 ASET (Vcoding_category_table, coding_category_iso_7_tight,
9432 intern ("coding-category-iso-7-tight"));
9433 ASET (Vcoding_category_table, coding_category_iso_8_1,
9434 intern ("coding-category-iso-8-1"));
9435 ASET (Vcoding_category_table, coding_category_iso_8_2,
9436 intern ("coding-category-iso-8-2"));
9437 ASET (Vcoding_category_table, coding_category_iso_7_else,
9438 intern ("coding-category-iso-7-else"));
9439 ASET (Vcoding_category_table, coding_category_iso_8_else,
9440 intern ("coding-category-iso-8-else"));
9441 ASET (Vcoding_category_table, coding_category_utf_8,
9442 intern ("coding-category-utf-8"));
9443 ASET (Vcoding_category_table, coding_category_utf_16_be,
9444 intern ("coding-category-utf-16-be"));
9445 ASET (Vcoding_category_table, coding_category_utf_16_auto,
9446 intern ("coding-category-utf-16-auto"));
9447 ASET (Vcoding_category_table, coding_category_utf_16_le,
9448 intern ("coding-category-utf-16-le"));
9449 ASET (Vcoding_category_table, coding_category_utf_16_be_nosig,
9450 intern ("coding-category-utf-16-be-nosig"));
9451 ASET (Vcoding_category_table, coding_category_utf_16_le_nosig,
9452 intern ("coding-category-utf-16-le-nosig"));
9453 ASET (Vcoding_category_table, coding_category_charset,
9454 intern ("coding-category-charset"));
9455 ASET (Vcoding_category_table, coding_category_sjis,
9456 intern ("coding-category-sjis"));
9457 ASET (Vcoding_category_table, coding_category_big5,
9458 intern ("coding-category-big5"));
9459 ASET (Vcoding_category_table, coding_category_ccl,
9460 intern ("coding-category-ccl"));
9461 ASET (Vcoding_category_table, coding_category_emacs_mule,
9462 intern ("coding-category-emacs-mule"));
9463 /* Followings are NOT target of code detection. */
9464 ASET (Vcoding_category_table, coding_category_raw_text,
9465 intern ("coding-category-raw-text"));
9466 ASET (Vcoding_category_table, coding_category_undecided,
9467 intern ("coding-category-undecided"));
9468
9469 DEFSYM (Qinsufficient_source, "insufficient-source");
9470 DEFSYM (Qinconsistent_eol, "inconsistent-eol");
9471 DEFSYM (Qinvalid_source, "invalid-source");
9472 DEFSYM (Qinterrupted, "interrupted");
9473 DEFSYM (Qinsufficient_memory, "insufficient-memory");
9474 DEFSYM (Qcoding_system_define_form, "coding-system-define-form");
9475
9476 defsubr (&Scoding_system_p);
9477 defsubr (&Sread_coding_system);
9478 defsubr (&Sread_non_nil_coding_system);
9479 defsubr (&Scheck_coding_system);
9480 defsubr (&Sdetect_coding_region);
9481 defsubr (&Sdetect_coding_string);
9482 defsubr (&Sfind_coding_systems_region_internal);
9483 defsubr (&Sunencodable_char_position);
9484 defsubr (&Scheck_coding_systems_region);
9485 defsubr (&Sdecode_coding_region);
9486 defsubr (&Sencode_coding_region);
9487 defsubr (&Sdecode_coding_string);
9488 defsubr (&Sencode_coding_string);
9489 defsubr (&Sdecode_sjis_char);
9490 defsubr (&Sencode_sjis_char);
9491 defsubr (&Sdecode_big5_char);
9492 defsubr (&Sencode_big5_char);
9493 defsubr (&Sset_terminal_coding_system_internal);
9494 defsubr (&Sset_safe_terminal_coding_system_internal);
9495 defsubr (&Sterminal_coding_system);
9496 defsubr (&Sset_keyboard_coding_system_internal);
9497 defsubr (&Skeyboard_coding_system);
9498 defsubr (&Sfind_operation_coding_system);
9499 defsubr (&Sset_coding_system_priority);
9500 defsubr (&Sdefine_coding_system_internal);
9501 defsubr (&Sdefine_coding_system_alias);
9502 defsubr (&Scoding_system_put);
9503 defsubr (&Scoding_system_base);
9504 defsubr (&Scoding_system_plist);
9505 defsubr (&Scoding_system_aliases);
9506 defsubr (&Scoding_system_eol_type);
9507 defsubr (&Scoding_system_priority_list);
9508
9509 DEFVAR_LISP ("coding-system-list", &Vcoding_system_list,
9510 doc: /* List of coding systems.
9511
9512Do not alter the value of this variable manually. This variable should be
9513updated by the functions `define-coding-system' and
9514`define-coding-system-alias'. */);
9515 Vcoding_system_list = Qnil;
9516
9517 DEFVAR_LISP ("coding-system-alist", &Vcoding_system_alist,
9518 doc: /* Alist of coding system names.
9519Each element is one element list of coding system name.
9520This variable is given to `completing-read' as TABLE argument.
9521
9522Do not alter the value of this variable manually. This variable should be
9523updated by the functions `make-coding-system' and
9524`define-coding-system-alias'. */);
9525 Vcoding_system_alist = Qnil;
9526
9527 DEFVAR_LISP ("coding-category-list", &Vcoding_category_list,
9528 doc: /* List of coding-categories (symbols) ordered by priority.
9529
9530On detecting a coding system, Emacs tries code detection algorithms
9531associated with each coding-category one by one in this order. When
9532one algorithm agrees with a byte sequence of source text, the coding
9533system bound to the corresponding coding-category is selected.
9534
9535Don't modify this variable directly, but use `set-coding-priority'. */);
9536 {
9537 int i;
9538
9539 Vcoding_category_list = Qnil;
9540 for (i = coding_category_max - 1; i >= 0; i--)
9541 Vcoding_category_list
9542 = Fcons (XVECTOR (Vcoding_category_table)->contents[i],
9543 Vcoding_category_list);
9544 }
9545
9546 DEFVAR_LISP ("coding-system-for-read", &Vcoding_system_for_read,
9547 doc: /* Specify the coding system for read operations.
9548It is useful to bind this variable with `let', but do not set it globally.
9549If the value is a coding system, it is used for decoding on read operation.
9550If not, an appropriate element is used from one of the coding system alists:
9551There are three such tables, `file-coding-system-alist',
9552`process-coding-system-alist', and `network-coding-system-alist'. */);
9553 Vcoding_system_for_read = Qnil;
9554
9555 DEFVAR_LISP ("coding-system-for-write", &Vcoding_system_for_write,
9556 doc: /* Specify the coding system for write operations.
9557Programs bind this variable with `let', but you should not set it globally.
9558If the value is a coding system, it is used for encoding of output,
9559when writing it to a file and when sending it to a file or subprocess.
9560
9561If this does not specify a coding system, an appropriate element
9562is used from one of the coding system alists:
9563There are three such tables, `file-coding-system-alist',
9564`process-coding-system-alist', and `network-coding-system-alist'.
9565For output to files, if the above procedure does not specify a coding system,
9566the value of `buffer-file-coding-system' is used. */);
9567 Vcoding_system_for_write = Qnil;
9568
9569 DEFVAR_LISP ("last-coding-system-used", &Vlast_coding_system_used,
9570 doc: /*
9571Coding system used in the latest file or process I/O. */);
9572 Vlast_coding_system_used = Qnil;
9573
9574 DEFVAR_LISP ("last-code-conversion-error", &Vlast_code_conversion_error,
9575 doc: /*
9576Error status of the last code conversion.
9577
9578When an error was detected in the last code conversion, this variable
9579is set to one of the following symbols.
9580 `insufficient-source'
9581 `inconsistent-eol'
9582 `invalid-source'
9583 `interrupted'
9584 `insufficient-memory'
9585When no error was detected, the value doesn't change. So, to check
9586the error status of a code conversion by this variable, you must
9587explicitly set this variable to nil before performing code
9588conversion. */);
9589 Vlast_code_conversion_error = Qnil;
9590
9591 DEFVAR_BOOL ("inhibit-eol-conversion", &inhibit_eol_conversion,
9592 doc: /*
9593*Non-nil means always inhibit code conversion of end-of-line format.
9594See info node `Coding Systems' and info node `Text and Binary' concerning
9595such conversion. */);
9596 inhibit_eol_conversion = 0;
9597
9598 DEFVAR_BOOL ("inherit-process-coding-system", &inherit_process_coding_system,
9599 doc: /*
9600Non-nil means process buffer inherits coding system of process output.
9601Bind it to t if the process output is to be treated as if it were a file
9602read from some filesystem. */);
9603 inherit_process_coding_system = 0;
9604
9605 DEFVAR_LISP ("file-coding-system-alist", &Vfile_coding_system_alist,
9606 doc: /*
9607Alist to decide a coding system to use for a file I/O operation.
9608The format is ((PATTERN . VAL) ...),
9609where PATTERN is a regular expression matching a file name,
9610VAL is a coding system, a cons of coding systems, or a function symbol.
9611If VAL is a coding system, it is used for both decoding and encoding
9612the file contents.
9613If VAL is a cons of coding systems, the car part is used for decoding,
9614and the cdr part is used for encoding.
9615If VAL is a function symbol, the function must return a coding system
9616or a cons of coding systems which are used as above. The function gets
9617the arguments with which `find-operation-coding-systems' was called.
9618
9619See also the function `find-operation-coding-system'
9620and the variable `auto-coding-alist'. */);
9621 Vfile_coding_system_alist = Qnil;
9622
9623 DEFVAR_LISP ("process-coding-system-alist", &Vprocess_coding_system_alist,
9624 doc: /*
9625Alist to decide a coding system to use for a process I/O operation.
9626The format is ((PATTERN . VAL) ...),
9627where PATTERN is a regular expression matching a program name,
9628VAL is a coding system, a cons of coding systems, or a function symbol.
9629If VAL is a coding system, it is used for both decoding what received
9630from the program and encoding what sent to the program.
9631If VAL is a cons of coding systems, the car part is used for decoding,
9632and the cdr part is used for encoding.
9633If VAL is a function symbol, the function must return a coding system
9634or a cons of coding systems which are used as above.
9635
9636See also the function `find-operation-coding-system'. */);
9637 Vprocess_coding_system_alist = Qnil;
9638
9639 DEFVAR_LISP ("network-coding-system-alist", &Vnetwork_coding_system_alist,
9640 doc: /*
9641Alist to decide a coding system to use for a network I/O operation.
9642The format is ((PATTERN . VAL) ...),
9643where PATTERN is a regular expression matching a network service name
9644or is a port number to connect to,
9645VAL is a coding system, a cons of coding systems, or a function symbol.
9646If VAL is a coding system, it is used for both decoding what received
9647from the network stream and encoding what sent to the network stream.
9648If VAL is a cons of coding systems, the car part is used for decoding,
9649and the cdr part is used for encoding.
9650If VAL is a function symbol, the function must return a coding system
9651or a cons of coding systems which are used as above.
9652
9653See also the function `find-operation-coding-system'. */);
9654 Vnetwork_coding_system_alist = Qnil;
9655
9656 DEFVAR_LISP ("locale-coding-system", &Vlocale_coding_system,
9657 doc: /* Coding system to use with system messages.
9658Also used for decoding keyboard input on X Window system. */);
9659 Vlocale_coding_system = Qnil;
9660
9661 /* The eol mnemonics are reset in startup.el system-dependently. */
9662 DEFVAR_LISP ("eol-mnemonic-unix", &eol_mnemonic_unix,
9663 doc: /*
9664*String displayed in mode line for UNIX-like (LF) end-of-line format. */);
9665 eol_mnemonic_unix = build_string (":");
9666
9667 DEFVAR_LISP ("eol-mnemonic-dos", &eol_mnemonic_dos,
9668 doc: /*
9669*String displayed in mode line for DOS-like (CRLF) end-of-line format. */);
9670 eol_mnemonic_dos = build_string ("\\");
9671
9672 DEFVAR_LISP ("eol-mnemonic-mac", &eol_mnemonic_mac,
9673 doc: /*
9674*String displayed in mode line for MAC-like (CR) end-of-line format. */);
9675 eol_mnemonic_mac = build_string ("/");
9676
9677 DEFVAR_LISP ("eol-mnemonic-undecided", &eol_mnemonic_undecided,
9678 doc: /*
9679*String displayed in mode line when end-of-line format is not yet determined. */);
9680 eol_mnemonic_undecided = build_string (":");
9681
9682 DEFVAR_LISP ("enable-character-translation", &Venable_character_translation,
9683 doc: /*
9684*Non-nil enables character translation while encoding and decoding. */);
9685 Venable_character_translation = Qt;
9686
9687 DEFVAR_LISP ("standard-translation-table-for-decode",
9688 &Vstandard_translation_table_for_decode,
9689 doc: /* Table for translating characters while decoding. */);
9690 Vstandard_translation_table_for_decode = Qnil;
9691
9692 DEFVAR_LISP ("standard-translation-table-for-encode",
9693 &Vstandard_translation_table_for_encode,
9694 doc: /* Table for translating characters while encoding. */);
9695 Vstandard_translation_table_for_encode = Qnil;
9696
9697 DEFVAR_LISP ("charset-revision-table", &Vcharset_revision_table,
9698 doc: /* Alist of charsets vs revision numbers.
9699While encoding, if a charset (car part of an element) is found,
9700designate it with the escape sequence identifying revision (cdr part
9701of the element). */);
9702 Vcharset_revision_table = Qnil;
9703
9704 DEFVAR_LISP ("default-process-coding-system",
9705 &Vdefault_process_coding_system,
9706 doc: /* Cons of coding systems used for process I/O by default.
9707The car part is used for decoding a process output,
9708the cdr part is used for encoding a text to be sent to a process. */);
9709 Vdefault_process_coding_system = Qnil;
9710
9711 DEFVAR_LISP ("latin-extra-code-table", &Vlatin_extra_code_table,
9712 doc: /*
9713Table of extra Latin codes in the range 128..159 (inclusive).
9714This is a vector of length 256.
9715If Nth element is non-nil, the existence of code N in a file
9716\(or output of subprocess) doesn't prevent it to be detected as
9717a coding system of ISO 2022 variant which has a flag
9718`accept-latin-extra-code' t (e.g. iso-latin-1) on reading a file
9719or reading output of a subprocess.
9720Only 128th through 159th elements has a meaning. */);
9721 Vlatin_extra_code_table = Fmake_vector (make_number (256), Qnil);
9722
9723 DEFVAR_LISP ("select-safe-coding-system-function",
9724 &Vselect_safe_coding_system_function,
9725 doc: /*
9726Function to call to select safe coding system for encoding a text.
9727
9728If set, this function is called to force a user to select a proper
9729coding system which can encode the text in the case that a default
9730coding system used in each operation can't encode the text.
9731
9732The default value is `select-safe-coding-system' (which see). */);
9733 Vselect_safe_coding_system_function = Qnil;
9734
9735 DEFVAR_BOOL ("coding-system-require-warning",
9736 &coding_system_require_warning,
9737 doc: /* Internal use only.
9738If non-nil, on writing a file, `select-safe-coding-system-function' is
9739called even if `coding-system-for-write' is non-nil. The command
9740`universal-coding-system-argument' binds this variable to t temporarily. */);
9741 coding_system_require_warning = 0;
9742
9743
9744 DEFVAR_BOOL ("inhibit-iso-escape-detection",
9745 &inhibit_iso_escape_detection,
9746 doc: /*
9747If non-nil, Emacs ignores ISO2022's escape sequence on code detection.
9748
9749By default, on reading a file, Emacs tries to detect how the text is
9750encoded. This code detection is sensitive to escape sequences. If
9751the sequence is valid as ISO2022, the code is determined as one of
9752the ISO2022 encodings, and the file is decoded by the corresponding
9753coding system (e.g. `iso-2022-7bit').
9754
9755However, there may be a case that you want to read escape sequences in
9756a file as is. In such a case, you can set this variable to non-nil.
9757Then, as the code detection ignores any escape sequences, no file is
9758detected as encoded in some ISO2022 encoding. The result is that all
9759escape sequences become visible in a buffer.
9760
9761The default value is nil, and it is strongly recommended not to change
9762it. That is because many Emacs Lisp source files that contain
9763non-ASCII characters are encoded by the coding system `iso-2022-7bit'
9764in Emacs's distribution, and they won't be decoded correctly on
9765reading if you suppress escape sequence detection.
9766
9767The other way to read escape sequences in a file without decoding is
9768to explicitly specify some coding system that doesn't use ISO2022's
9769escape sequence (e.g `latin-1') on reading by \\[universal-coding-system-argument]. */);
9770 inhibit_iso_escape_detection = 0;
9771
9772 DEFVAR_LISP ("translation-table-for-input", &Vtranslation_table_for_input,
9773 doc: /* Char table for translating self-inserting characters.
9774This is applied to the result of input methods, not their input. See also
9775`keyboard-translate-table'. */);
9776 Vtranslation_table_for_input = Qnil;
9777
9778 {
9779 Lisp_Object args[coding_arg_max];
9780 Lisp_Object plist[16];
9781 int i;
9782
9783 for (i = 0; i < coding_arg_max; i++)
9784 args[i] = Qnil;
9785
9786 plist[0] = intern (":name");
9787 plist[1] = args[coding_arg_name] = Qno_conversion;
9788 plist[2] = intern (":mnemonic");
9789 plist[3] = args[coding_arg_mnemonic] = make_number ('=');
9790 plist[4] = intern (":coding-type");
9791 plist[5] = args[coding_arg_coding_type] = Qraw_text;
9792 plist[6] = intern (":ascii-compatible-p");
9793 plist[7] = args[coding_arg_ascii_compatible_p] = Qt;
9794 plist[8] = intern (":default-char");
9795 plist[9] = args[coding_arg_default_char] = make_number (0);
9796 plist[10] = intern (":for-unibyte");
9797 plist[11] = args[coding_arg_for_unibyte] = Qt;
9798 plist[12] = intern (":docstring");
9799 plist[13] = build_string ("Do no conversion.\n\
9800\n\
9801When you visit a file with this coding, the file is read into a\n\
9802unibyte buffer as is, thus each byte of a file is treated as a\n\
9803character.");
9804 plist[14] = intern (":eol-type");
9805 plist[15] = args[coding_arg_eol_type] = Qunix;
9806 args[coding_arg_plist] = Flist (16, plist);
9807 Fdefine_coding_system_internal (coding_arg_max, args);
9808
9809 plist[1] = args[coding_arg_name] = Qundecided;
9810 plist[3] = args[coding_arg_mnemonic] = make_number ('-');
9811 plist[5] = args[coding_arg_coding_type] = Qundecided;
9812 /* This is already set.
9813 plist[7] = args[coding_arg_ascii_compatible_p] = Qt; */
9814 plist[8] = intern (":charset-list");
9815 plist[9] = args[coding_arg_charset_list] = Fcons (Qascii, Qnil);
9816 plist[11] = args[coding_arg_for_unibyte] = Qnil;
9817 plist[13] = build_string ("No conversion on encoding, automatic conversion on decoding.");
9818 plist[15] = args[coding_arg_eol_type] = Qnil;
9819 args[coding_arg_plist] = Flist (16, plist);
9820 Fdefine_coding_system_internal (coding_arg_max, args);
9821 }
9822
9823 setup_coding_system (Qno_conversion, &keyboard_coding);
9824 setup_coding_system (Qundecided, &terminal_coding);
9825 setup_coding_system (Qno_conversion, &safe_terminal_coding);
9826
9827 {
9828 int i;
9829
9830 for (i = 0; i < coding_category_max; i++)
9831 Fset (AREF (Vcoding_category_table, i), Qno_conversion);
9832 }
9833#if defined (MSDOS) || defined (WINDOWSNT)
9834 system_eol_type = Qdos;
9835#else
9836 system_eol_type = Qunix;
9837#endif
9838 staticpro (&system_eol_type);
9839}
9840
9841char *
9842emacs_strerror (error_number)
9843 int error_number;
9844{
9845 char *str;
9846
9847 synchronize_system_messages_locale ();
9848 str = strerror (error_number);
9849
9850 if (! NILP (Vlocale_coding_system))
9851 {
9852 Lisp_Object dec = code_convert_string_norecord (build_string (str),
9853 Vlocale_coding_system,
9854 0);
9855 str = (char *) SDATA (dec);
9856 }
9857
9858 return str;
9859}
9860
9861#endif /* emacs */
9862
9863/* arch-tag: 3a3a2b01-5ff6-4071-9afe-f5b808d9229d
9864 (do not change this comment) */