coding.c 283 KB
Newer Older
Dave Love's avatar
Dave Love committed
1
/* Coding system handler (conversion, detection, etc).
2
   Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
Kenichi Handa's avatar
Kenichi Handa committed
3 4 5
   Copyright (C) 1995, 1997, 1998, 2002, 2003, 2004, 2005
     National Institute of Advanced Industrial Science and Technology (AIST)
     Registration Number H14PRO021
Kenichi Handa's avatar
Kenichi Handa committed
6
   Copyright (C) 2003
Kenichi Handa's avatar
Kenichi Handa committed
7 8
     National Institute of Advanced Industrial Science and Technology (AIST)
     Registration Number H13PRO009
Karl Heuer's avatar
Karl Heuer committed
9

Karl Heuer's avatar
Karl Heuer committed
10 11 12 13 14 15
This file is part of GNU Emacs.

GNU Emacs is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
Karl Heuer's avatar
Karl Heuer committed
16

Karl Heuer's avatar
Karl Heuer committed
17 18 19 20
GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
Karl Heuer's avatar
Karl Heuer committed
21

Karl Heuer's avatar
Karl Heuer committed
22 23
You should have received a copy of the GNU General Public License
along with GNU Emacs; see the file COPYING.  If not, write to
Lute Kamstra's avatar
Lute Kamstra committed
24 25
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.  */
Karl Heuer's avatar
Karl Heuer committed
26 27 28

/*** TABLE OF CONTENTS ***

29
  0. General comments
Karl Heuer's avatar
Karl Heuer committed
30
  1. Preamble
Kenichi Handa's avatar
Kenichi Handa committed
31 32 33 34 35 36 37 38 39 40 41
  2. Emacs' internal format (emacs-utf-8) handlers
  3. UTF-8 handlers
  4. UTF-16 handlers
  5. Charset-base coding systems handlers
  6. emacs-mule (old Emacs' internal format) handlers
  7. ISO2022 handlers
  8. Shift-JIS and BIG5 handlers
  9. CCL handlers
  10. C library functions
  11. Emacs Lisp library functions
  12. Postamble
Karl Heuer's avatar
Karl Heuer committed
42 43 44

*/

Kenichi Handa's avatar
Kenichi Handa committed
45
/*** 0. General comments ***
46 47


Kenichi Handa's avatar
Kenichi Handa committed
48
CODING SYSTEM
Karl Heuer's avatar
Karl Heuer committed
49

Dave Love's avatar
Dave Love committed
50 51
  A coding system is an object for an encoding mechanism that contains
  information about how to convert byte sequences to character
52 53 54 55 56
  sequences and vice versa.  When we say "decode", it means converting
  a byte sequence of a specific coding system into a character
  sequence that is represented by Emacs' internal coding system
  `emacs-utf-8', and when we say "encode", it means converting a
  character sequence of emacs-utf-8 to a byte sequence of a specific
Kenichi Handa's avatar
Kenichi Handa committed
57
  coding system.
Karl Heuer's avatar
Karl Heuer committed
58

59 60
  In Emacs Lisp, a coding system is represented by a Lisp symbol.  In
  C level, a coding system is represented by a vector of attributes
Dave Love's avatar
Dave Love committed
61
  stored in the hash table Vcharset_hash_table.  The conversion from
62 63
  coding system symbol to attributes vector is done by looking up
  Vcharset_hash_table by the symbol.
Karl Heuer's avatar
Karl Heuer committed
64

65
  Coding systems are classified into the following types depending on
Dave Love's avatar
Dave Love committed
66
  the encoding mechanism.  Here's a brief description of the types.
Karl Heuer's avatar
Karl Heuer committed
67

Kenichi Handa's avatar
Kenichi Handa committed
68 69 70 71 72 73 74
  o UTF-8

  o UTF-16

  o Charset-base coding system

  A coding system defined by one or more (coded) character sets.
Dave Love's avatar
Dave Love committed
75
  Decoding and encoding are done by a code converter defined for each
Kenichi Handa's avatar
Kenichi Handa committed
76 77
  character set.

Dave Love's avatar
Dave Love committed
78
  o Old Emacs internal format (emacs-mule)
Kenichi Handa's avatar
Kenichi Handa committed
79

Dave Love's avatar
Dave Love committed
80
  The coding system adopted by old versions of Emacs (20 and 21).
Karl Heuer's avatar
Karl Heuer committed
81

Kenichi Handa's avatar
Kenichi Handa committed
82
  o ISO2022-base coding system
Karl Heuer's avatar
Karl Heuer committed
83 84

  The most famous coding system for multiple character sets.  X's
Kenichi Handa's avatar
Kenichi Handa committed
85 86 87
  Compound Text, various EUCs (Extended Unix Code), and coding systems
  used in the Internet communication such as ISO-2022-JP are all
  variants of ISO2022.
Karl Heuer's avatar
Karl Heuer committed
88

Kenichi Handa's avatar
Kenichi Handa committed
89
  o SJIS (or Shift-JIS or MS-Kanji-Code)
90

Karl Heuer's avatar
Karl Heuer committed
91 92
  A coding system to encode character sets: ASCII, JISX0201, and
  JISX0208.  Widely used for PC's in Japan.  Details are described in
Kenichi Handa's avatar
Kenichi Handa committed
93
  section 8.
Karl Heuer's avatar
Karl Heuer committed
94

Kenichi Handa's avatar
Kenichi Handa committed
95
  o BIG5
Karl Heuer's avatar
Karl Heuer committed
96

Kenichi Handa's avatar
Kenichi Handa committed
97
  A coding system to encode character sets: ASCII and Big5.  Widely
Dave Love's avatar
Dave Love committed
98
  used for Chinese (mainly in Taiwan and Hong Kong).  Details are
Kenichi Handa's avatar
Kenichi Handa committed
99 100 101
  described in section 8.  In this file, when we write "big5" (all
  lowercase), we mean the coding system, and when we write "Big5"
  (capitalized), we mean the character set.
Karl Heuer's avatar
Karl Heuer committed
102

Kenichi Handa's avatar
Kenichi Handa committed
103
  o CCL
104

Dave Love's avatar
Dave Love committed
105
  If a user wants to decode/encode text encoded in a coding system
Kenichi Handa's avatar
Kenichi Handa committed
106 107 108
  not listed above, he can supply a decoder and an encoder for it in
  CCL (Code Conversion Language) programs.  Emacs executes the CCL
  program while decoding/encoding.
109

Kenichi Handa's avatar
Kenichi Handa committed
110
  o Raw-text
Karl Heuer's avatar
Karl Heuer committed
111

Dave Love's avatar
Dave Love committed
112
  A coding system for text containing raw eight-bit data.  Emacs
Dave Love's avatar
Dave Love committed
113
  treats each byte of source text as a character (except for
Kenichi Handa's avatar
Kenichi Handa committed
114
  end-of-line conversion).
Karl Heuer's avatar
Karl Heuer committed
115

Kenichi Handa's avatar
Kenichi Handa committed
116 117 118
  o No-conversion

  Like raw text, but don't do end-of-line conversion.
Karl Heuer's avatar
Karl Heuer committed
119 120


Kenichi Handa's avatar
Kenichi Handa committed
121
END-OF-LINE FORMAT
Karl Heuer's avatar
Karl Heuer committed
122

Dave Love's avatar
Dave Love committed
123
  How text end-of-line is encoded depends on operating system.  For
Kenichi Handa's avatar
Kenichi Handa committed
124
  instance, Unix's format is just one byte of LF (line-feed) code,
Richard M. Stallman's avatar
Richard M. Stallman committed
125
  whereas DOS's format is two-byte sequence of `carriage-return' and
126 127
  `line-feed' codes.  MacOS's format is usually one byte of
  `carriage-return'.
Karl Heuer's avatar
Karl Heuer committed
128

Dave Love's avatar
Dave Love committed
129
  Since text character encoding and end-of-line encoding are
Kenichi Handa's avatar
Kenichi Handa committed
130 131
  independent, any coding system described above can take any format
  of end-of-line (except for no-conversion).
Karl Heuer's avatar
Karl Heuer committed
132

133 134 135 136 137
STRUCT CODING_SYSTEM

  Before using a coding system for code conversion (i.e. decoding and
  encoding), we setup a structure of type `struct coding_system'.
  This structure keeps various information about a specific code
Dave Love's avatar
Dave Love committed
138
  conversion (e.g. the location of source and destination data).
Karl Heuer's avatar
Karl Heuer committed
139 140 141

*/

Kenichi Handa's avatar
Kenichi Handa committed
142 143 144
/* COMMON MACROS */


Karl Heuer's avatar
Karl Heuer committed
145 146
/*** GENERAL NOTES on `detect_coding_XXX ()' functions ***

Kenichi Handa's avatar
Kenichi Handa committed
147
  These functions check if a byte sequence specified as a source in
148 149
  CODING conforms to the format of XXX, and update the members of
  DETECT_INFO.
Kenichi Handa's avatar
Kenichi Handa committed
150

151
  Return 1 if the byte sequence conforms to XXX, otherwise return 0.
Kenichi Handa's avatar
Kenichi Handa committed
152 153 154

  Below is the template of these functions.  */

Karl Heuer's avatar
Karl Heuer committed
155
#if 0
Kenichi Handa's avatar
Kenichi Handa committed
156
static int
157
detect_coding_XXX (coding, detect_info)
Kenichi Handa's avatar
Kenichi Handa committed
158
     struct coding_system *coding;
159
     struct coding_detection_info *detect_info;
Karl Heuer's avatar
Karl Heuer committed
160
{
161 162
  const unsigned char *src = coding->source;
  const unsigned char *src_end = coding->source + coding->src_bytes;
Kenichi Handa's avatar
Kenichi Handa committed
163
  int multibytep = coding->src_multibyte;
164
  int consumed_chars = 0;
Kenichi Handa's avatar
Kenichi Handa committed
165 166 167 168 169 170 171 172
  int found = 0;
  ...;

  while (1)
    {
      /* Get one byte from the source.  If the souce is exausted, jump
	 to no_more_source:.  */
      ONE_MORE_BYTE (c);
173 174 175 176 177

      if (! __C_conforms_to_XXX___ (c))
	break;
      if (! __C_strongly_suggests_XXX__ (c))
	found = CATEGORY_MASK_XXX;
Kenichi Handa's avatar
Kenichi Handa committed
178
    }
179 180
  /* The byte sequence is invalid for XXX.  */
  detect_info->rejected |= CATEGORY_MASK_XXX;
Kenichi Handa's avatar
Kenichi Handa committed
181
  return 0;
182

Kenichi Handa's avatar
Kenichi Handa committed
183
 no_more_source:
184 185
  /* The source exausted successfully.  */
  detect_info->found |= found;
Kenichi Handa's avatar
Kenichi Handa committed
186
  return 1;
Karl Heuer's avatar
Karl Heuer committed
187 188 189 190 191
}
#endif

/*** GENERAL NOTES on `decode_coding_XXX ()' functions ***

Kenichi Handa's avatar
Kenichi Handa committed
192 193 194 195
  These functions decode a byte sequence specified as a source by
  CODING.  The resulting multibyte text goes to a place pointed to by
  CODING->charbuf, the length of which should not exceed
  CODING->charbuf_size;
196

Kenichi Handa's avatar
Kenichi Handa committed
197 198 199 200
  These functions set the information of original and decoded texts in
  CODING->consumed, CODING->consumed_char, and CODING->charbuf_used.
  They also set CODING->result to one of CODING_RESULT_XXX indicating
  how the decoding is finished.
201

Kenichi Handa's avatar
Kenichi Handa committed
202
  Below is the template of these functions.  */
203

Karl Heuer's avatar
Karl Heuer committed
204
#if 0
205
static void
Kenichi Handa's avatar
Kenichi Handa committed
206
decode_coding_XXXX (coding)
Karl Heuer's avatar
Karl Heuer committed
207 208
     struct coding_system *coding;
{
209 210
  const unsigned char *src = coding->source + coding->consumed;
  const unsigned char *src_end = coding->source + coding->src_bytes;
Kenichi Handa's avatar
Kenichi Handa committed
211 212 213
  /* SRC_BASE remembers the start position in source in each loop.
     The loop will be exited when there's not enough source code, or
     when there's no room in CHARBUF for a decoded character.  */
214
  const unsigned char *src_base;
Kenichi Handa's avatar
Kenichi Handa committed
215
  /* A buffer to produce decoded characters.  */
216 217
  int *charbuf = coding->charbuf + coding->charbuf_used;
  int *charbuf_end = coding->charbuf + coding->charbuf_size;
Kenichi Handa's avatar
Kenichi Handa committed
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
  int multibytep = coding->src_multibyte;

  while (1)
    {
      src_base = src;
      if (charbuf < charbuf_end)
	/* No more room to produce a decoded character.  */
	break;
      ONE_MORE_BYTE (c);
      /* Decode it. */
    }

 no_more_source:
  if (src_base < src_end
      && coding->mode & CODING_MODE_LAST_BLOCK)
    /* If the source ends by partial bytes to construct a character,
       treat them as eight-bit raw data.  */
    while (src_base < src_end && charbuf < charbuf_end)
      *charbuf++ = *src_base++;
  /* Remember how many bytes and characters we consumed.  If the
     source is multibyte, the bytes and chars are not identical.  */
  coding->consumed = coding->consumed_char = src_base - coding->source;
  /* Remember how many characters we produced.  */
  coding->charbuf_used = charbuf - coding->charbuf;
Karl Heuer's avatar
Karl Heuer committed
242 243 244 245 246
}
#endif

/*** GENERAL NOTES on `encode_coding_XXX ()' functions ***

Kenichi Handa's avatar
Kenichi Handa committed
247 248
  These functions encode SRC_BYTES length text at SOURCE of Emacs'
  internal multibyte format by CODING.  The resulting byte sequence
249 250
  goes to a place pointed to by DESTINATION, the length of which
  should not exceed DST_BYTES.
251

Kenichi Handa's avatar
Kenichi Handa committed
252 253 254 255
  These functions set the information of original and encoded texts in
  the members produced, produced_char, consumed, and consumed_char of
  the structure *CODING.  They also set the member result to one of
  CODING_RESULT_XXX indicating how the encoding finished.
256

Kenichi Handa's avatar
Kenichi Handa committed
257 258 259
  DST_BYTES zero means that source area and destination area are
  overlapped, which means that we can produce a encoded text until it
  reaches at the head of not-yet-encoded source text.
260

Kenichi Handa's avatar
Kenichi Handa committed
261
  Below is a template of these functions.  */
Karl Heuer's avatar
Karl Heuer committed
262
#if 0
263
static void
Kenichi Handa's avatar
Kenichi Handa committed
264
encode_coding_XXX (coding)
Karl Heuer's avatar
Karl Heuer committed
265 266
     struct coding_system *coding;
{
Kenichi Handa's avatar
Kenichi Handa committed
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
  int multibytep = coding->dst_multibyte;
  int *charbuf = coding->charbuf;
  int *charbuf_end = charbuf->charbuf + coding->charbuf_used;
  unsigned char *dst = coding->destination + coding->produced;
  unsigned char *dst_end = coding->destination + coding->dst_bytes;
  unsigned char *adjusted_dst_end = dst_end - _MAX_BYTES_PRODUCED_IN_LOOP_;
  int produced_chars = 0;

  for (; charbuf < charbuf_end && dst < adjusted_dst_end; charbuf++)
    {
      int c = *charbuf;
      /* Encode C into DST, and increment DST.  */
    }
 label_no_more_destination:
  /* How many chars and bytes we produced.  */
  coding->produced_char += produced_chars;
  coding->produced = dst - coding->destination;
Karl Heuer's avatar
Karl Heuer committed
284 285 286 287 288 289
}
#endif


/*** 1. Preamble ***/

290
#include <config.h>
Karl Heuer's avatar
Karl Heuer committed
291 292 293 294
#include <stdio.h>

#include "lisp.h"
#include "buffer.h"
Kenichi Handa's avatar
Kenichi Handa committed
295
#include "character.h"
Karl Heuer's avatar
Karl Heuer committed
296 297
#include "charset.h"
#include "ccl.h"
Kenichi Handa's avatar
Kenichi Handa committed
298
#include "composite.h"
Karl Heuer's avatar
Karl Heuer committed
299 300 301
#include "coding.h"
#include "window.h"

Kenichi Handa's avatar
Kenichi Handa committed
302
Lisp_Object Vcoding_system_hash_table;
Karl Heuer's avatar
Karl Heuer committed
303

Kenichi Handa's avatar
Kenichi Handa committed
304
Lisp_Object Qcoding_system, Qcoding_aliases, Qeol_type;
305 306
Lisp_Object Qunix, Qdos;
extern Lisp_Object Qmac;	/* frame.c */
Karl Heuer's avatar
Karl Heuer committed
307 308
Lisp_Object Qbuffer_file_coding_system;
Lisp_Object Qpost_read_conversion, Qpre_write_conversion;
Kenichi Handa's avatar
Kenichi Handa committed
309
Lisp_Object Qdefault_char;
310
Lisp_Object Qno_conversion, Qundecided;
Kenichi Handa's avatar
Kenichi Handa committed
311
Lisp_Object Qcharset, Qiso_2022, Qutf_8, Qutf_16, Qshift_jis, Qbig5;
312
Lisp_Object Qbig, Qlittle;
313
Lisp_Object Qcoding_system_history;
Kenichi Handa's avatar
Kenichi Handa committed
314
Lisp_Object Qvalid_codes;
Kenichi Handa's avatar
Kenichi Handa committed
315 316 317
Lisp_Object QCcategory, QCmnemonic, QCdefalut_char;
Lisp_Object QCdecode_translation_table, QCencode_translation_table;
Lisp_Object QCpost_read_conversion, QCpre_write_conversion;
318
Lisp_Object QCascii_compatible_p;
Karl Heuer's avatar
Karl Heuer committed
319 320

extern Lisp_Object Qinsert_file_contents, Qwrite_region;
Kim F. Storm's avatar
Kim F. Storm committed
321
Lisp_Object Qcall_process, Qcall_process_region;
Karl Heuer's avatar
Karl Heuer committed
322 323 324
Lisp_Object Qstart_process, Qopen_network_stream;
Lisp_Object Qtarget_idx;

325 326 327
Lisp_Object Qinsufficient_source, Qinconsistent_eol, Qinvalid_source;
Lisp_Object Qinterrupted, Qinsufficient_memory;

328 329
int coding_system_require_warning;

330 331
Lisp_Object Vselect_safe_coding_system_function;

332 333 334
/* Mnemonic string for each format of end-of-line.  */
Lisp_Object eol_mnemonic_unix, eol_mnemonic_dos, eol_mnemonic_mac;
/* Mnemonic string to indicate format of end-of-line is not yet
Karl Heuer's avatar
Karl Heuer committed
335
   decided.  */
336
Lisp_Object eol_mnemonic_undecided;
Karl Heuer's avatar
Karl Heuer committed
337 338 339

#ifdef emacs

340 341 342
Lisp_Object Vcoding_system_list, Vcoding_system_alist;

Lisp_Object Qcoding_system_p, Qcoding_system_error;
Karl Heuer's avatar
Karl Heuer committed
343

344 345 346
/* Coding system emacs-mule and raw-text are for converting only
   end-of-line format.  */
Lisp_Object Qemacs_mule, Qraw_text;
Kenichi Handa's avatar
Kenichi Handa committed
347
Lisp_Object Qutf_8_emacs;
Dave Love's avatar
Dave Love committed
348

Karl Heuer's avatar
Karl Heuer committed
349 350 351 352 353 354 355 356
/* Coding-systems are handed between Emacs Lisp programs and C internal
   routines by the following three variables.  */
/* Coding-system for reading files and receiving data from process.  */
Lisp_Object Vcoding_system_for_read;
/* Coding-system for writing files and sending data to process.  */
Lisp_Object Vcoding_system_for_write;
/* Coding-system actually used in the latest I/O.  */
Lisp_Object Vlast_coding_system_used;
357 358
/* Set to non-nil when an error is detected while code conversion.  */
Lisp_Object Vlast_code_conversion_error;
359
/* A vector of length 256 which contains information about special
Karl Heuer's avatar
Karl Heuer committed
360
   Latin codes (especially for dealing with Microsoft codes).  */
361
Lisp_Object Vlatin_extra_code_table;
362

363 364 365
/* Flag to inhibit code conversion of end-of-line format.  */
int inhibit_eol_conversion;

366 367 368
/* Flag to inhibit ISO2022 escape sequence detection.  */
int inhibit_iso_escape_detection;

369 370 371
/* Flag to make buffer-file-coding-system inherit from process-coding.  */
int inherit_process_coding_system;

372
/* Coding system to be used to encode text for terminal display.  */
Karl Heuer's avatar
Karl Heuer committed
373 374
struct coding_system terminal_coding;

375 376 377 378 379
/* Coding system to be used to encode text for terminal display when
   terminal coding system is nil.  */
struct coding_system safe_terminal_coding;

/* Coding system of what is sent from terminal keyboard.  */
Karl Heuer's avatar
Karl Heuer committed
380 381
struct coding_system keyboard_coding;

382 383 384
Lisp_Object Vfile_coding_system_alist;
Lisp_Object Vprocess_coding_system_alist;
Lisp_Object Vnetwork_coding_system_alist;
Karl Heuer's avatar
Karl Heuer committed
385

386 387
Lisp_Object Vlocale_coding_system;

Karl Heuer's avatar
Karl Heuer committed
388 389
#endif /* emacs */

390 391
/* Flag to tell if we look up translation table on character code
   conversion.  */
392
Lisp_Object Venable_character_translation;
393 394 395 396
/* Standard translation table to look up on decoding (reading).  */
Lisp_Object Vstandard_translation_table_for_decode;
/* Standard translation table to look up on encoding (writing).  */
Lisp_Object Vstandard_translation_table_for_encode;
397

398 399 400 401
Lisp_Object Qtranslation_table;
Lisp_Object Qtranslation_table_id;
Lisp_Object Qtranslation_table_for_decode;
Lisp_Object Qtranslation_table_for_encode;
Karl Heuer's avatar
Karl Heuer committed
402 403

/* Alist of charsets vs revision number.  */
Kenichi Handa's avatar
Kenichi Handa committed
404
static Lisp_Object Vcharset_revision_table;
Karl Heuer's avatar
Karl Heuer committed
405

406 407 408
/* Default coding systems used for process I/O.  */
Lisp_Object Vdefault_process_coding_system;

409 410 411
/* Char table for translating Quail and self-inserting input.  */
Lisp_Object Vtranslation_table_for_input;

Kenichi Handa's avatar
Kenichi Handa committed
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
/* Two special coding systems.  */
Lisp_Object Vsjis_coding_system;
Lisp_Object Vbig5_coding_system;

/* ISO2022 section */

#define CODING_ISO_INITIAL(coding, reg)			\
  (XINT (AREF (AREF (CODING_ID_ATTRS ((coding)->id),	\
		     coding_attr_iso_initial),		\
	       reg)))


#define CODING_ISO_REQUEST(coding, charset_id)	\
  ((charset_id <= (coding)->max_charset_id	\
    ? (coding)->safe_charsets[charset_id]	\
    : -1))


#define CODING_ISO_FLAGS(coding)	\
  ((coding)->spec.iso_2022.flags)
#define CODING_ISO_DESIGNATION(coding, reg)	\
  ((coding)->spec.iso_2022.current_designation[reg])
#define CODING_ISO_INVOCATION(coding, plane)	\
  ((coding)->spec.iso_2022.current_invocation[plane])
#define CODING_ISO_SINGLE_SHIFTING(coding)	\
  ((coding)->spec.iso_2022.single_shifting)
#define CODING_ISO_BOL(coding)	\
  ((coding)->spec.iso_2022.bol)
#define CODING_ISO_INVOKED_CHARSET(coding, plane)	\
  CODING_ISO_DESIGNATION ((coding), CODING_ISO_INVOCATION ((coding), (plane)))

/* Control characters of ISO2022.  */
			/* code */	/* function */
#define ISO_CODE_LF	0x0A		/* line-feed */
#define ISO_CODE_CR	0x0D		/* carriage-return */
#define ISO_CODE_SO	0x0E		/* shift-out */
#define ISO_CODE_SI	0x0F		/* shift-in */
#define ISO_CODE_SS2_7	0x19		/* single-shift-2 for 7-bit code */
#define ISO_CODE_ESC	0x1B		/* escape */
#define ISO_CODE_SS2	0x8E		/* single-shift-2 */
#define ISO_CODE_SS3	0x8F		/* single-shift-3 */
#define ISO_CODE_CSI	0x9B		/* control-sequence-introducer */

/* All code (1-byte) of ISO2022 is classified into one of the
   followings.  */
enum iso_code_class_type
  {
    ISO_control_0,		/* Control codes in the range
				   0x00..0x1F and 0x7F, except for the
				   following 5 codes.  */
    ISO_shift_out,		/* ISO_CODE_SO (0x0E) */
    ISO_shift_in,		/* ISO_CODE_SI (0x0F) */
    ISO_single_shift_2_7,	/* ISO_CODE_SS2_7 (0x19) */
    ISO_escape,			/* ISO_CODE_SO (0x1B) */
    ISO_control_1,		/* Control codes in the range
				   0x80..0x9F, except for the
				   following 3 codes.  */
    ISO_single_shift_2,		/* ISO_CODE_SS2 (0x8E) */
    ISO_single_shift_3,		/* ISO_CODE_SS3 (0x8F) */
    ISO_control_sequence_introducer, /* ISO_CODE_CSI (0x9B) */
    ISO_0x20_or_0x7F,		/* Codes of the values 0x20 or 0x7F.  */
    ISO_graphic_plane_0,	/* Graphic codes in the range 0x21..0x7E.  */
    ISO_0xA0_or_0xFF,		/* Codes of the values 0xA0 or 0xFF.  */
    ISO_graphic_plane_1		/* Graphic codes in the range 0xA1..0xFE.  */
  };
477

Kenichi Handa's avatar
Kenichi Handa committed
478 479
/** The macros CODING_ISO_FLAG_XXX defines a flag bit of the
    `iso-flags' attribute of an iso2022 coding system.  */
480

Kenichi Handa's avatar
Kenichi Handa committed
481 482 483
/* If set, produce long-form designation sequence (e.g. ESC $ ( A)
   instead of the correct short-form sequence (e.g. ESC $ A).  */
#define CODING_ISO_FLAG_LONG_FORM	0x0001
484

Kenichi Handa's avatar
Kenichi Handa committed
485 486 487
/* If set, reset graphic planes and registers at end-of-line to the
   initial state.  */
#define CODING_ISO_FLAG_RESET_AT_EOL	0x0002
488

Kenichi Handa's avatar
Kenichi Handa committed
489 490 491
/* If set, reset graphic planes and registers before any control
   characters to the initial state.  */
#define CODING_ISO_FLAG_RESET_AT_CNTL	0x0004
492

Kenichi Handa's avatar
Kenichi Handa committed
493 494
/* If set, encode by 7-bit environment.  */
#define CODING_ISO_FLAG_SEVEN_BITS	0x0008
Karl Heuer's avatar
Karl Heuer committed
495

Kenichi Handa's avatar
Kenichi Handa committed
496 497
/* If set, use locking-shift function.  */
#define CODING_ISO_FLAG_LOCKING_SHIFT	0x0010
498

Kenichi Handa's avatar
Kenichi Handa committed
499 500 501
/* If set, use single-shift function.  Overwrite
   CODING_ISO_FLAG_LOCKING_SHIFT.  */
#define CODING_ISO_FLAG_SINGLE_SHIFT	0x0020
502

Kenichi Handa's avatar
Kenichi Handa committed
503 504
/* If set, use designation escape sequence.  */
#define CODING_ISO_FLAG_DESIGNATION	0x0040
505

Kenichi Handa's avatar
Kenichi Handa committed
506 507
/* If set, produce revision number sequence.  */
#define CODING_ISO_FLAG_REVISION	0x0080
508

Kenichi Handa's avatar
Kenichi Handa committed
509 510
/* If set, produce ISO6429's direction specifying sequence.  */
#define CODING_ISO_FLAG_DIRECTION	0x0100
Richard M. Stallman's avatar
Richard M. Stallman committed
511

Kenichi Handa's avatar
Kenichi Handa committed
512 513 514
/* If set, assume designation states are reset at beginning of line on
   output.  */
#define CODING_ISO_FLAG_INIT_AT_BOL	0x0200
Karl Heuer's avatar
Karl Heuer committed
515

Kenichi Handa's avatar
Kenichi Handa committed
516 517 518
/* If set, designation sequence should be placed at beginning of line
   on output.  */
#define CODING_ISO_FLAG_DESIGNATE_AT_BOL 0x0400
519

Kenichi Handa's avatar
Kenichi Handa committed
520 521
/* If set, do not encode unsafe charactes on output.  */
#define CODING_ISO_FLAG_SAFE		0x0800
522

Kenichi Handa's avatar
Kenichi Handa committed
523 524 525
/* If set, extra latin codes (128..159) are accepted as a valid code
   on input.  */
#define CODING_ISO_FLAG_LATIN_EXTRA	0x1000
526

Kenichi Handa's avatar
Kenichi Handa committed
527
#define CODING_ISO_FLAG_COMPOSITION	0x2000
528

Kenichi Handa's avatar
Kenichi Handa committed
529
#define CODING_ISO_FLAG_EUC_TW_SHIFT	0x4000
530

531
#define CODING_ISO_FLAG_USE_ROMAN	0x8000
532

533
#define CODING_ISO_FLAG_USE_OLDJIS	0x10000
534

535
#define CODING_ISO_FLAG_FULL_SUPPORT	0x100000
536

Kenichi Handa's avatar
Kenichi Handa committed
537 538 539
/* A character to be produced on output if encoding of the original
   character is prohibited by CODING_ISO_FLAG_SAFE.  */
#define CODING_INHIBIT_CHARACTER_SUBSTITUTION  '?'
540

Karl Heuer's avatar
Karl Heuer committed
541

Kenichi Handa's avatar
Kenichi Handa committed
542 543 544
/* UTF-16 section */
#define CODING_UTF_16_BOM(coding)	\
  ((coding)->spec.utf_16.bom)
Karl Heuer's avatar
Karl Heuer committed
545

Kenichi Handa's avatar
Kenichi Handa committed
546 547
#define CODING_UTF_16_ENDIAN(coding)	\
  ((coding)->spec.utf_16.endian)
Karl Heuer's avatar
Karl Heuer committed
548

Kenichi Handa's avatar
Kenichi Handa committed
549 550
#define CODING_UTF_16_SURROGATE(coding)	\
  ((coding)->spec.utf_16.surrogate)
Karl Heuer's avatar
Karl Heuer committed
551 552


Kenichi Handa's avatar
Kenichi Handa committed
553 554 555 556 557 558
/* CCL section */
#define CODING_CCL_DECODER(coding)	\
  AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_decoder)
#define CODING_CCL_ENCODER(coding)	\
  AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_encoder)
#define CODING_CCL_VALIDS(coding)					   \
Kenichi Handa's avatar
Kenichi Handa committed
559
  (SDATA (AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_valids)))
Karl Heuer's avatar
Karl Heuer committed
560

Dave Love's avatar
Dave Love committed
561
/* Index for each coding category in `coding_categories' */
Karl Heuer's avatar
Karl Heuer committed
562

Kenichi Handa's avatar
Kenichi Handa committed
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
enum coding_category
  {
    coding_category_iso_7,
    coding_category_iso_7_tight,
    coding_category_iso_8_1,
    coding_category_iso_8_2,
    coding_category_iso_7_else,
    coding_category_iso_8_else,
    coding_category_utf_8,
    coding_category_utf_16_auto,
    coding_category_utf_16_be,
    coding_category_utf_16_le,
    coding_category_utf_16_be_nosig,
    coding_category_utf_16_le_nosig,
    coding_category_charset,
    coding_category_sjis,
    coding_category_big5,
    coding_category_ccl,
    coding_category_emacs_mule,
    /* All above are targets of code detection.  */
    coding_category_raw_text,
    coding_category_undecided,
    coding_category_max
  };

/* Definitions of flag bits used in detect_coding_XXXX.  */
#define CATEGORY_MASK_ISO_7		(1 << coding_category_iso_7)
#define CATEGORY_MASK_ISO_7_TIGHT	(1 << coding_category_iso_7_tight)
#define CATEGORY_MASK_ISO_8_1		(1 << coding_category_iso_8_1)
#define CATEGORY_MASK_ISO_8_2		(1 << coding_category_iso_8_2)
#define CATEGORY_MASK_ISO_7_ELSE	(1 << coding_category_iso_7_else)
#define CATEGORY_MASK_ISO_8_ELSE	(1 << coding_category_iso_8_else)
#define CATEGORY_MASK_UTF_8		(1 << coding_category_utf_8)
596
#define CATEGORY_MASK_UTF_16_AUTO	(1 << coding_category_utf_16_auto)
Kenichi Handa's avatar
Kenichi Handa committed
597 598 599 600 601 602 603 604 605
#define CATEGORY_MASK_UTF_16_BE		(1 << coding_category_utf_16_be)
#define CATEGORY_MASK_UTF_16_LE		(1 << coding_category_utf_16_le)
#define CATEGORY_MASK_UTF_16_BE_NOSIG	(1 << coding_category_utf_16_be_nosig)
#define CATEGORY_MASK_UTF_16_LE_NOSIG	(1 << coding_category_utf_16_le_nosig)
#define CATEGORY_MASK_CHARSET		(1 << coding_category_charset)
#define CATEGORY_MASK_SJIS		(1 << coding_category_sjis)
#define CATEGORY_MASK_BIG5		(1 << coding_category_big5)
#define CATEGORY_MASK_CCL		(1 << coding_category_ccl)
#define CATEGORY_MASK_EMACS_MULE	(1 << coding_category_emacs_mule)
606
#define CATEGORY_MASK_RAW_TEXT		(1 << coding_category_raw_text)
Kenichi Handa's avatar
Kenichi Handa committed
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678

/* This value is returned if detect_coding_mask () find nothing other
   than ASCII characters.  */
#define CATEGORY_MASK_ANY		\
  (CATEGORY_MASK_ISO_7			\
   | CATEGORY_MASK_ISO_7_TIGHT		\
   | CATEGORY_MASK_ISO_8_1		\
   | CATEGORY_MASK_ISO_8_2		\
   | CATEGORY_MASK_ISO_7_ELSE		\
   | CATEGORY_MASK_ISO_8_ELSE		\
   | CATEGORY_MASK_UTF_8		\
   | CATEGORY_MASK_UTF_16_BE		\
   | CATEGORY_MASK_UTF_16_LE		\
   | CATEGORY_MASK_UTF_16_BE_NOSIG	\
   | CATEGORY_MASK_UTF_16_LE_NOSIG	\
   | CATEGORY_MASK_CHARSET		\
   | CATEGORY_MASK_SJIS			\
   | CATEGORY_MASK_BIG5			\
   | CATEGORY_MASK_CCL			\
   | CATEGORY_MASK_EMACS_MULE)


#define CATEGORY_MASK_ISO_7BIT \
  (CATEGORY_MASK_ISO_7 | CATEGORY_MASK_ISO_7_TIGHT)

#define CATEGORY_MASK_ISO_8BIT \
  (CATEGORY_MASK_ISO_8_1 | CATEGORY_MASK_ISO_8_2)

#define CATEGORY_MASK_ISO_ELSE \
  (CATEGORY_MASK_ISO_7_ELSE | CATEGORY_MASK_ISO_8_ELSE)

#define CATEGORY_MASK_ISO_ESCAPE	\
  (CATEGORY_MASK_ISO_7			\
   | CATEGORY_MASK_ISO_7_TIGHT		\
   | CATEGORY_MASK_ISO_7_ELSE		\
   | CATEGORY_MASK_ISO_8_ELSE)

#define CATEGORY_MASK_ISO	\
  (  CATEGORY_MASK_ISO_7BIT	\
     | CATEGORY_MASK_ISO_8BIT	\
     | CATEGORY_MASK_ISO_ELSE)

#define CATEGORY_MASK_UTF_16		\
  (CATEGORY_MASK_UTF_16_BE		\
   | CATEGORY_MASK_UTF_16_LE		\
   | CATEGORY_MASK_UTF_16_BE_NOSIG	\
   | CATEGORY_MASK_UTF_16_LE_NOSIG)


/* List of symbols `coding-category-xxx' ordered by priority.  This
   variable is exposed to Emacs Lisp.  */
static Lisp_Object Vcoding_category_list;

/* Table of coding categories (Lisp symbols).  This variable is for
   internal use oly.  */
static Lisp_Object Vcoding_category_table;

/* Table of coding-categories ordered by priority.  */
static enum coding_category coding_priorities[coding_category_max];

/* Nth element is a coding context for the coding system bound to the
   Nth coding category.  */
static struct coding_system coding_categories[coding_category_max];

/*** Commonly used macros and functions ***/

#ifndef min
#define min(a, b) ((a) < (b) ? (a) : (b))
#endif
#ifndef max
#define max(a, b) ((a) > (b) ? (a) : (b))
#endif
Karl Heuer's avatar
Karl Heuer committed
679

680 681 682 683
#define CODING_GET_INFO(coding, attrs, charset_list)	\
  do {							\
    (attrs) = CODING_ID_ATTRS ((coding)->id);		\
    (charset_list) = CODING_ATTR_CHARSET_LIST (attrs);	\
Kenichi Handa's avatar
Kenichi Handa committed
684
  } while (0)
Karl Heuer's avatar
Karl Heuer committed
685 686


Kenichi Handa's avatar
Kenichi Handa committed
687 688
/* Safely get one byte from the source text pointed by SRC which ends
   at SRC_END, and set C to that byte.  If there are not enough bytes
689 690 691 692 693
   in the source, it jumps to `no_more_source'.  If multibytep is
   nonzero, and a multibyte character is found at SRC, set C to the
   negative value of the character code.  The caller should declare
   and set these variables appropriately in advance:
	src, src_end, multibytep */
694

695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
#define ONE_MORE_BYTE(c)				\
  do {							\
    if (src == src_end)					\
      {							\
	if (src_base < src)				\
	  record_conversion_result			\
	    (coding, CODING_RESULT_INSUFFICIENT_SRC);	\
	goto no_more_source;				\
      }							\
    c = *src++;						\
    if (multibytep && (c & 0x80))			\
      {							\
	if ((c & 0xFE) == 0xC0)				\
	  c = ((c & 1) << 6) | *src++;			\
	else						\
	  {						\
711 712
	    src--;					\
	    c = - string_char (src, &src, NULL);	\
713 714 715 716 717
	    record_conversion_result			\
	      (coding, CODING_RESULT_INVALID_SRC);	\
	  }						\
      }							\
    consumed_chars++;					\
718 719 720
  } while (0)


721 722 723 724 725 726 727 728 729
#define ONE_MORE_BYTE_NO_CHECK(c)			\
  do {							\
    c = *src++;						\
    if (multibytep && (c & 0x80))			\
      {							\
	if ((c & 0xFE) == 0xC0)				\
	  c = ((c & 1) << 6) | *src++;			\
	else						\
	  {						\
730 731
	    src--;					\
	    c = - string_char (src, &src, NULL);	\
732 733 734 735 736
	    record_conversion_result			\
	      (coding, CODING_RESULT_INVALID_SRC);	\
	  }						\
      }							\
    consumed_chars++;					\
737 738 739
  } while (0)


Kenichi Handa's avatar
Kenichi Handa committed
740 741 742 743 744
/* Store a byte C in the place pointed by DST and increment DST to the
   next free point, and increment PRODUCED_CHARS.  The caller should
   assure that C is 0..127, and declare and set the variable `dst'
   appropriately in advance.
*/
745 746


Kenichi Handa's avatar
Kenichi Handa committed
747 748 749 750
#define EMIT_ONE_ASCII_BYTE(c)	\
  do {				\
    produced_chars++;		\
    *dst++ = (c);		\
751
  } while (0)
752 753


Kenichi Handa's avatar
Kenichi Handa committed
754
/* Like EMIT_ONE_ASCII_BYTE byt store two bytes; C1 and C2.  */
755

Kenichi Handa's avatar
Kenichi Handa committed
756 757 758 759 760
#define EMIT_TWO_ASCII_BYTES(c1, c2)	\
  do {					\
    produced_chars += 2;		\
    *dst++ = (c1), *dst++ = (c2);	\
  } while (0)
761 762


Kenichi Handa's avatar
Kenichi Handa committed
763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
/* Store a byte C in the place pointed by DST and increment DST to the
   next free point, and increment PRODUCED_CHARS.  If MULTIBYTEP is
   nonzero, store in an appropriate multibyte from.  The caller should
   declare and set the variables `dst' and `multibytep' appropriately
   in advance.  */

#define EMIT_ONE_BYTE(c)		\
  do {					\
    produced_chars++;			\
    if (multibytep)			\
      {					\
	int ch = (c);			\
	if (ch >= 0x80)			\
	  ch = BYTE8_TO_CHAR (ch);	\
	CHAR_STRING_ADVANCE (ch, dst);	\
      }					\
    else				\
      *dst++ = (c);			\
781 782 783
  } while (0)


Kenichi Handa's avatar
Kenichi Handa committed
784
/* Like EMIT_ONE_BYTE, but emit two bytes; C1 and C2.  */
785

786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
#define EMIT_TWO_BYTES(c1, c2)		\
  do {					\
    produced_chars += 2;		\
    if (multibytep)			\
      {					\
	int ch;				\
					\
	ch = (c1);			\
	if (ch >= 0x80)			\
	  ch = BYTE8_TO_CHAR (ch);	\
	CHAR_STRING_ADVANCE (ch, dst);	\
	ch = (c2);			\
	if (ch >= 0x80)			\
	  ch = BYTE8_TO_CHAR (ch);	\
	CHAR_STRING_ADVANCE (ch, dst);	\
      }					\
    else				\
      {					\
	*dst++ = (c1);			\
	*dst++ = (c2);			\
      }					\
807 808 809
  } while (0)


Kenichi Handa's avatar
Kenichi Handa committed
810 811 812 813 814
#define EMIT_THREE_BYTES(c1, c2, c3)	\
  do {					\
    EMIT_ONE_BYTE (c1);			\
    EMIT_TWO_BYTES (c2, c3);		\
  } while (0)
815 816


Kenichi Handa's avatar
Kenichi Handa committed
817 818 819 820 821
#define EMIT_FOUR_BYTES(c1, c2, c3, c4)		\
  do {						\
    EMIT_TWO_BYTES (c1, c2);			\
    EMIT_TWO_BYTES (c3, c4);			\
  } while (0)
822 823


824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
/* Prototypes for static functions.  */
static void record_conversion_result P_ ((struct coding_system *coding,
					  enum coding_result_code result));
static int detect_coding_utf_8 P_ ((struct coding_system *,
				    struct coding_detection_info *info));
static void decode_coding_utf_8 P_ ((struct coding_system *));
static int encode_coding_utf_8 P_ ((struct coding_system *));

static int detect_coding_utf_16 P_ ((struct coding_system *,
				     struct coding_detection_info *info));
static void decode_coding_utf_16 P_ ((struct coding_system *));
static int encode_coding_utf_16 P_ ((struct coding_system *));

static int detect_coding_iso_2022 P_ ((struct coding_system *,
				       struct coding_detection_info *info));
static void decode_coding_iso_2022 P_ ((struct coding_system *));
static int encode_coding_iso_2022 P_ ((struct coding_system *));

static int detect_coding_emacs_mule P_ ((struct coding_system *,
					 struct coding_detection_info *info));
static void decode_coding_emacs_mule P_ ((struct coding_system *));
static int encode_coding_emacs_mule P_ ((struct coding_system *));

static int detect_coding_sjis P_ ((struct coding_system *,
				   struct coding_detection_info *info));
static void decode_coding_sjis P_ ((struct coding_system *));
static int encode_coding_sjis P_ ((struct coding_system *));

static int detect_coding_big5 P_ ((struct coding_system *,
				   struct coding_detection_info *info));
static void decode_coding_big5 P_ ((struct coding_system *));
static int encode_coding_big5 P_ ((struct coding_system *));

static int detect_coding_ccl P_ ((struct coding_system *,
				  struct coding_detection_info *info));
static void decode_coding_ccl P_ ((struct coding_system *));
static int encode_coding_ccl P_ ((struct coding_system *));

static void decode_coding_raw_text P_ ((struct coding_system *));
static int encode_coding_raw_text P_ ((struct coding_system *));

static void coding_set_source P_ ((struct coding_system *));
static void coding_set_destination P_ ((struct coding_system *));
static void coding_alloc_by_realloc P_ ((struct coding_system *, EMACS_INT));
static void coding_alloc_by_making_gap P_ ((struct coding_system *,
					    EMACS_INT));
static unsigned char *alloc_destination P_ ((struct coding_system *,
					     EMACS_INT, unsigned char *));
static void setup_iso_safe_charsets P_ ((Lisp_Object));
static unsigned char *encode_designation_at_bol P_ ((struct coding_system *,
						     int *, int *,
						     unsigned char *));
static int detect_eol P_ ((const unsigned char *,
			   EMACS_INT, enum coding_category));
static Lisp_Object adjust_coding_eol_type P_ ((struct coding_system *, int));
static void decode_eol P_ ((struct coding_system *));
static Lisp_Object get_translation_table P_ ((Lisp_Object, int, int *));
static Lisp_Object get_translation P_ ((Lisp_Object, int *, int *,
					int, int *, int *));
static int produce_chars P_ ((struct coding_system *, Lisp_Object, int));
static INLINE void produce_composition P_ ((struct coding_system *, int *,
					    EMACS_INT));
static INLINE void produce_charset P_ ((struct coding_system *, int *,
					EMACS_INT));
static void produce_annotation P_ ((struct coding_system *, EMACS_INT));
static int decode_coding P_ ((struct coding_system *));
static INLINE int *handle_composition_annotation P_ ((EMACS_INT, EMACS_INT,
						      struct coding_system *, 
						      int *, EMACS_INT *));
static INLINE int *handle_charset_annotation P_ ((EMACS_INT, EMACS_INT,
						  struct coding_system *,
						  int *, EMACS_INT *));
static void consume_chars P_ ((struct coding_system *, Lisp_Object, int));
static int encode_coding P_ ((struct coding_system *));
static Lisp_Object make_conversion_work_buffer P_ ((int));
static Lisp_Object code_conversion_restore P_ ((Lisp_Object));
static INLINE int char_encodable_p P_ ((int, Lisp_Object));
static Lisp_Object make_subsidiaries P_ ((Lisp_Object));

903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924
static void
record_conversion_result (struct coding_system *coding,
			  enum coding_result_code result)
{
  coding->result = result;
  switch (result)
    {
    case CODING_RESULT_INSUFFICIENT_SRC:
      Vlast_code_conversion_error = Qinsufficient_source;
      break;
    case CODING_RESULT_INCONSISTENT_EOL:
      Vlast_code_conversion_error = Qinconsistent_eol;
      break;
    case CODING_RESULT_INVALID_SRC:
      Vlast_code_conversion_error = Qinvalid_source;
      break;
    case CODING_RESULT_INTERRUPT:
      Vlast_code_conversion_error = Qinterrupted;
      break;
    case CODING_RESULT_INSUFFICIENT_MEM:
      Vlast_code_conversion_error = Qinsufficient_memory;
      break;
925 926
    default:
      Vlast_code_conversion_error = intern ("Unknown error");
927 928 929
    }
}

Kenichi Handa's avatar
Kenichi Handa committed
930 931 932 933 934 935
#define CODING_DECODE_CHAR(coding, src, src_base, src_end, charset, code, c) \
  do {									     \
    charset_map_loaded = 0;						     \
    c = DECODE_CHAR (charset, code);					     \
    if (charset_map_loaded)						     \
      {									     \
Kenichi Handa's avatar
Kenichi Handa committed
936
	const unsigned char *orig = coding->source;			     \
Kenichi Handa's avatar
Kenichi Handa committed
937 938 939 940 941 942 943 944
	EMACS_INT offset;						     \
									     \
	coding_set_source (coding);					     \
	offset = coding->source - orig;					     \
	src += offset;							     \
	src_base += offset;						     \
	src_end += offset;						     \
      }									     \
945 946 947
  } while (0)


Kenichi Handa's avatar
Kenichi Handa committed
948 949 950 951 952 953 954 955 956 957
#define ASSURE_DESTINATION(bytes)				\
  do {								\
    if (dst + (bytes) >= dst_end)				\
      {								\
	int more_bytes = charbuf_end - charbuf + (bytes);	\
								\
	dst = alloc_destination (coding, more_bytes, dst);	\
	dst_end = coding->destination + coding->dst_bytes;	\
      }								\
  } while (0)
958 959 960



Kenichi Handa's avatar
Kenichi Handa committed
961 962
static void
coding_set_source (coding)
963 964
     struct coding_system *coding;
{
Kenichi Handa's avatar
Kenichi Handa committed
965 966
  if (BUFFERP (coding->src_object))
    {
967
      struct buffer *buf = XBUFFER (coding->src_object);
968

Kenichi Handa's avatar
Kenichi Handa committed
969
      if (coding->src_pos < 0)
970
	coding->source = BUF_GAP_END_ADDR (buf) + coding->src_pos_byte;
Kenichi Handa's avatar
Kenichi Handa committed
971
      else
972
	coding->source = BUF_BYTE_ADDRESS (buf, coding->src_pos_byte);
973
    }
Kenichi Handa's avatar
Kenichi Handa committed
974
  else if (STRINGP (coding->src_object))
975
    {
Kenichi Handa's avatar
Kenichi Handa committed
976
      coding->source = SDATA (coding->src_object) + coding->src_pos_byte;
977
    }
Kenichi Handa's avatar
Kenichi Handa committed
978 979 980 981 982
  else
    /* Otherwise, the source is C string and is never relocated
       automatically.  Thus we don't have to update anything.  */
    ;
}
983

Kenichi Handa's avatar
Kenichi Handa committed
984 985 986 987 988
static void
coding_set_destination (coding)
     struct coding_system *coding;
{
  if (BUFFERP (coding->dst_object))
989
    {
Kenichi Handa's avatar
Kenichi Handa committed
990
      if (coding->src_pos < 0)
991
	{
992 993 994 995
	  coding->destination = BEG_ADDR + coding->dst_pos_byte - 1;
	  coding->dst_bytes = (GAP_END_ADDR
			       - (coding->src_bytes - coding->consumed)
			       - coding->destination);
996
	}
Kenichi Handa's avatar
Kenichi Handa committed
997
      else
998 999 1000 1001 1002 1003 1004 1005
	{
	  /* We are sure that coding->dst_pos_byte is before the gap
	     of the buffer. */
	  coding->destination = (BUF_BEG_ADDR (XBUFFER (coding->dst_object))
				 + coding->dst_pos_byte - 1);
	  coding->dst_bytes = (BUF_GAP_END_ADDR (XBUFFER (coding->dst_object))
			       - coding->destination);
	}
Kenichi Handa's avatar
Kenichi Handa committed
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
    }
  else
    /* Otherwise, the destination is C string and is never relocated
       automatically.  Thus we don't have to update anything.  */
    ;
}


static void
coding_alloc_by_realloc (coding, bytes)
     struct coding_system *coding;
     EMACS_INT bytes;
{
  coding->destination = (unsigned char *) xrealloc (coding->destination,
						    coding->dst_bytes + bytes);
  coding->dst_bytes += bytes;
}

static void
coding_alloc_by_making_gap (coding, bytes)
     struct coding_system *coding;
     EMACS_INT bytes;
{
1029 1030
  if (BUFFERP (coding->dst_object)
      && EQ (coding->src_object, coding->dst_object))
Kenichi Handa's avatar
Kenichi Handa committed
1031 1032 1033 1034 1035 1036 1037 1038 1039
    {
      EMACS_INT add = coding->src_bytes - coding->consumed;

      GAP_SIZE -= add; ZV += add; Z += add; ZV_BYTE += add; Z_BYTE += add;
      make_gap (bytes);
      GAP_SIZE += add; ZV -= add; Z -= add; ZV_BYTE -= add; Z_BYTE -= add;
    }
  else
    {
1040 1041 1042
      Lisp_Object this_buffer;

      this_buffer = Fcurrent_buffer ();
Kenichi Handa's avatar
Kenichi Handa committed
1043 1044 1045
      set_buffer_internal (XBUFFER (coding->dst_object));
      make_gap (bytes);
      set_buffer_internal (XBUFFER (this_buffer));
1046
    }
Kenichi Handa's avatar
Kenichi Handa committed
1047
}
Kenichi Handa's avatar
Kenichi Handa committed
1048

Kenichi Handa's avatar
Kenichi Handa committed
1049 1050 1051 1052

static unsigned char *
alloc_destination (coding, nbytes, dst)
     struct coding_system *coding;
1053
     EMACS_INT nbytes;
Kenichi Handa's avatar
Kenichi Handa committed
1054 1055 1056 1057 1058 1059
     unsigned char *dst;
{
  EMACS_INT offset = dst - coding->destination;

  if (BUFFERP (coding->dst_object))
    coding_alloc_by_making_gap (coding, nbytes);
1060
  else
Kenichi Handa's avatar
Kenichi Handa committed
1061
    coding_alloc_by_realloc (coding, nbytes);
1062
  record_conversion_result (coding, CODING_RESULT_SUCCESS);
Kenichi Handa's avatar
Kenichi Handa committed
1063 1064 1065 1066
  coding_set_destination (coding);
  dst = coding->destination + offset;
  return dst;
}
1067

1068 1069 1070 1071
/** Macros for annotations.  */

/* Maximum length of annotation data (sum of annotations for
   composition and charset).  */
1072
#define MAX_ANNOTATION_LENGTH (4 + (MAX_COMPOSITION_COMPONENTS * 2) - 1 + 4)
1073 1074 1075

/* An annotation data is stored in the array coding->charbuf in this
   format:
1076
     [ -LENGTH ANNOTATION_MASK NCHARS ... ]
1077 1078
   LENGTH is the number of elements in the annotation.
   ANNOTATION_MASK is one of CODING_ANNOTATE_XXX_MASK.
1079
   NCHARS is the number of characters in the text annotated.
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092

   The format of the following elements depend on ANNOTATION_MASK.

   In the case of CODING_ANNOTATE_COMPOSITION_MASK, these elements
   follows:
     ... METHOD [ COMPOSITION-COMPONENTS ... ]
   METHOD is one of enum composition_method.
   Optionnal COMPOSITION-COMPONENTS are characters and composition
   rules.

   In the case of CODING_ANNOTATE_CHARSET_MASK, one element CHARSET-ID
   follows.  */

1093
#define ADD_ANNOTATION_DATA(buf, len, mask, nchars)	\
1094 1095 1096
  do {							\
    *(buf)++ = -(len);					\
    *(buf)++ = (mask);					\
1097
    *(buf)++ = (nchars);				\
1098 1099 1100
    coding->annotated = 1;				\
  } while (0);

1101 1102 1103 1104
#define ADD_COMPOSITION_DATA(buf, nchars, method)			    \
  do {									    \
    ADD_ANNOTATION_DATA (buf, 4, CODING_ANNOTATE_COMPOSITION_MASK, nchars); \
    *buf++ = method;							    \
1105 1106 1107
  } while (0)


1108 1109 1110 1111
#define ADD_CHARSET_DATA(buf, nchars, id)				\
  do {									\
    ADD_ANNOTATION_DATA (buf, 4, CODING_ANNOTATE_CHARSET_MASK, nchars);	\
    *buf++ = id;							\
1112 1113
  } while (0)

Kenichi Handa's avatar
Kenichi Handa committed
1114 1115 1116 1117 1118 1119 1120 1121 1122

/*** 2. Emacs' internal format (emacs-utf-8) ***/




/*** 3. UTF-8 ***/

/* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1123 1124
   Check if a text is encoded in UTF-8.  If it is, return 1, else
   return 0.  */
Kenichi Handa's avatar
Kenichi Handa committed
1125 1126 1127 1128 1129 1130 1131 1132 1133

#define UTF_8_1_OCTET_P(c)         ((c) < 0x80)
#define UTF_8_EXTRA_OCTET_P(c)     (((c) & 0xC0) == 0x80)
#define UTF_8_2_OCTET_LEADING_P(c) (((c) & 0xE0) == 0xC0)
#define UTF_8_3_OCTET_LEADING_P(c) (((c) & 0xF0) == 0xE0)
#define UTF_8_4_OCTET_LEADING_P(c) (((c) & 0xF8) == 0xF0)
#define UTF_8_5_OCTET_LEADING_P(c) (((c) & 0xFC) == 0xF8)

static int
1134
detect_coding_utf_8 (coding, detect_info)
Kenichi Handa's avatar
Kenichi Handa committed
1135
     struct coding_system *coding;
1136
     struct coding_detection_info *detect_info;
Kenichi Handa's avatar
Kenichi Handa committed
1137
{
1138
  const unsigned char *src = coding->source, *src_base;
Kenichi Handa's avatar
Kenichi Handa committed
1139
  const unsigned char *src_end = coding->source + coding->src_bytes;
Kenichi Handa's avatar
Kenichi Handa committed
1140 1141 1142 1143
  int multibytep = coding->src_multibyte;
  int consumed_chars = 0;
  int found = 0;

1144
  detect_info->checked |= CATEGORY_MASK_UTF_8;
Kenichi Handa's avatar
Kenichi Handa committed
1145 1146 1147 1148
  /* A coding system of this category is always ASCII compatible.  */
  src += coding->head_ascii;

  while (1)
1149
    {
Kenichi Handa's avatar
Kenichi Handa committed
1150
      int c, c1, c2, c3, c4;
1151

1152
      src_base = src;
Kenichi Handa's avatar
Kenichi Handa committed
1153
      ONE_MORE_BYTE (c);
1154
      if (c < 0 || UTF_8_1_OCTET_P (c))
Kenichi Handa's avatar
Kenichi Handa committed
1155 1156
	continue;
      ONE_MORE_BYTE (c1);
1157
      if (c1 < 0 || ! UTF_8_EXTRA_OCTET_P (c1))
Kenichi Handa's avatar
Kenichi Handa committed
1158 1159
	break;
      if (UTF_8_2_OCTET_LEADING_P (c))
1160
	{
1161
	  found = CATEGORY_MASK_UTF_8;
Kenichi Handa's avatar
Kenichi Handa committed
1162
	  continue;
1163
	}
Kenichi Handa's avatar
Kenichi Handa committed
1164
      ONE_MORE_BYTE (c2);
1165
      if (c2 < 0 || ! UTF_8_EXTRA_OCTET_P (c2))
Kenichi Handa's avatar
Kenichi Handa committed
1166 1167
	break;
      if (UTF_8_3_OCTET_LEADING_P (c))
1168
	{
1169
	  found = CATEGORY_MASK_UTF_8;
Kenichi Handa's avatar
Kenichi Handa committed
1170
	  continue;
1171
	}
Kenichi Handa's avatar
Kenichi Handa committed
1172
      ONE_MORE_BYTE (c3);
1173
      if (c3 < 0 || ! UTF_8_EXTRA_OCTET_P (c3))
Kenichi Handa's avatar
Kenichi Handa committed
1174 1175
	break;
      if (UTF_8_4_OCTET_LEADING_P (c))
1176
	{
1177
	  found = CATEGORY_MASK_UTF_8;
Kenichi Handa's avatar
Kenichi Handa committed
1178 1179 1180
	  continue;
	}
      ONE_MORE_BYTE (c4);
1181
      if (c4 < 0 || ! UTF_8_EXTRA_OCTET_P (c4))
Kenichi Handa's avatar
Kenichi Handa committed
1182 1183 1184
	break;
      if (UTF_8_5_OCTET_LEADING_P (c))
	{
1185
	  found = CATEGORY_MASK_UTF_8;
Kenichi Handa's avatar
Kenichi Handa committed
1186 1187 1188
	  continue;
	}
      break;
1189
    }
1190
  detect_info->rejected |= CATEGORY_MASK_UTF_8;
Kenichi Handa's avatar
Kenichi Handa committed
1191
  return 0;
1192

Kenichi Handa's avatar
Kenichi Handa committed
1193
 no_more_source:
1194
  if (src_base < src && coding->mode & CODING_MODE_LAST_BLOCK)
1195
    {
1196
      detect_info->rejected |= CATEGORY_MASK_UTF_8;
1197
      return 0;
1198
    }
1199 1200
  detect_info->found |= found;
  return 1;
1201 1202
}

Karl Heuer's avatar
Karl Heuer committed
1203

1204
static void
Kenichi Handa's avatar
Kenichi Handa committed
1205
decode_coding_utf_8 (coding)
1206 1207
     struct coding_system *coding;
{
Kenichi Handa's avatar
Kenichi Handa committed
1208 1209 1210
  const unsigned char *src = coding->source + coding->consumed;
  const unsigned char *src_end = coding->source + coding->src_bytes;
  const unsigned char *src_base;
1211 1212
  int *charbuf = coding->charbuf + coding->charbuf_used;
  int *charbuf_end = coding->charbuf + coding->charbuf_size;
Kenichi Handa's avatar
Kenichi Handa committed
1213 1214
  int consumed_chars = 0, consumed_chars_base;
  int multibytep = coding->src_multibyte;
1215
  Lisp_Object attr, charset_list;
Karl Heuer's avatar
Karl Heuer committed
1216

1217
  CODING_GET_INFO (coding, attr, charset_list);
Kenichi Handa's avatar
Kenichi Handa committed
1218 1219

  while (1)
1220
    {
Kenichi Handa's avatar
Kenichi Handa committed
1221
      int c, c1, c2, c3, c4, c5;
Kenichi Handa's avatar
Kenichi Handa committed
1222

Kenichi Handa's avatar
Kenichi Handa committed
1223 1224
      src_base = src;
      consumed_chars_base = consumed_chars;
1225

Kenichi Handa's avatar
Kenichi Handa committed
1226 1227 1228 1229
      if (charbuf >= charbuf_end)
	break;

      ONE_MORE_BYTE (c1);
1230 1231 1232 1233 1234
      if (c1 < 0)
	{
	  c = - c1;
	}
      else if (UTF_8_1_OCTET_P(c1))
Kenichi Handa's avatar
Kenichi Handa committed
1235 1236
	{
	  c = c1;
1237
	}
Kenichi Handa's avatar
Kenichi Handa committed
1238
      else
1239
	{
Kenichi Handa's avatar
Kenichi Handa committed
1240
	  ONE_MORE_BYTE (c2);
1241
	  if (c2 < 0 || ! UTF_8_EXTRA_OCTET_P (c2))
Kenichi Handa's avatar
Kenichi Handa committed
1242 1243
	    goto invalid_code;
	  if (UTF_8_2_OCTET_LEADING_P (c1))
1244
	    {
1245 1246 1247 1248 1249 1250
	      c = ((c1 & 0x1F) << 6) | (c2 & 0x3F);
	      /* Reject overlong sequences here and below.  Encoders
		 producing them are incorrect, they can be misleading,
		 and they mess up read/write invariance.  */
	      if (c < 128)
		goto invalid_code;
1251
	    }
Kenichi Handa's avatar
Kenichi Handa committed
1252
	  else
1253
	    {
Kenichi Handa's avatar
Kenichi Handa committed
1254
	      ONE_MORE_BYTE (c3);
1255
	      if (c3 < 0 || ! UTF_8_EXTRA_OCTET_P (c3))
Kenichi Handa's avatar
Kenichi Handa committed
1256 1257
		goto invalid_code;
	      if (UTF_8_3_OCTET_LEADING_P (c1))
1258 1259 1260
		{
		  c = (((c1 & 0xF) << 12)
		       | ((c2 & 0x3F) << 6) | (c3 & 0x3F));
1261 1262
		  if (c < 0x800
		      || (c >= 0xd800 && c < 0xe000)) /* surrogates (invalid) */
1263 1264
		    goto invalid_code;
		}
Kenichi Handa's avatar
Kenichi Handa committed
1265 1266 1267
	      else
		{
		  ONE_MORE_BYTE (c4);
1268
		  if (c4 < 0 || ! UTF_8_EXTRA_OCTET_P (c4))
Kenichi Handa's avatar
Kenichi Handa committed
1269 1270
		    goto invalid_code;
		  if (UTF_8_4_OCTET_LEADING_P (c1))
1271
		    {
Kenichi Handa's avatar
Kenichi Handa committed
1272 1273
		    c = (((c1 & 0x7) << 18) | ((c2 & 0x3F) << 12)
			 | ((c3 & 0x3F) << 6) | (c4 & 0x3F));
1274 1275 1276
		    if (c < 0x10000)
		      goto invalid_code;
		    }
Kenichi Handa's avatar
Kenichi Handa committed
1277 1278 1279
		  else
		    {
		      ONE_MORE_BYTE (c5);
1280
		      if (c5 < 0 || ! UTF_8_EXTRA_OCTET_P (c5))
Kenichi Handa's avatar
Kenichi Handa committed
1281 1282 1283 1284 1285 1286
			goto invalid_code;
		      if (UTF_8_5_OCTET_LEADING_P (c1))
			{
			  c = (((c1 & 0x3) << 24) | ((c2 & 0x3F) << 18)
			       | ((c3 & 0x3F) << 12) | ((c4 & 0x3F) << 6)
			       | (c5 & 0x3F));
1287
			  if ((c > MAX_CHAR) || (c < 0x200000))
Kenichi Handa's avatar
Kenichi Handa committed
1288 1289 1290 1291 1292 1293
			    goto invalid_code;
			}
		      else
			goto invalid_code;
		    }
		}
1294
	    }
1295
	}
Kenichi Handa's avatar
Kenichi Handa committed
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305

      *charbuf++ = c;
      continue;

    invalid_code:
      src = src_base;
      consumed_chars = consumed_chars_base;
      ONE_MORE_BYTE (c);
      *charbuf++ = ASCII_BYTE_P (c) ? c : BYTE8_TO_CHAR (c);
      coding->errors++;
1306 1307
    }

Kenichi Handa's avatar
Kenichi Handa committed
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323
 no_more_source:
  coding->consumed_char += consumed_chars_base;
  coding->consumed = src_base - coding->source;
  coding->charbuf_used = charbuf - coding->charbuf;
}


static int
encode_coding_utf_8 (coding)
     struct coding_system *coding;
{
  int multibytep = coding->dst_multibyte;
  int *charbuf = coding->charbuf;
  int *charbuf_end = charbuf + coding->charbuf_used;
  unsigned char *dst = coding->destination + coding->produced;
  unsigned char *dst_end = coding->destination + coding->dst_bytes;
1324
  int produced_chars = 0;
Kenichi Handa's avatar