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

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

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

Karl Heuer's avatar
Karl Heuer committed
18 19 20 21
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
22

Karl Heuer's avatar
Karl Heuer committed
23
You should have received a copy of the GNU General Public License
24
along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
Karl Heuer's avatar
Karl Heuer committed
25 26 27

/*** TABLE OF CONTENTS ***

28
  0. General comments
Karl Heuer's avatar
Karl Heuer committed
29
  1. Preamble
Kenichi Handa's avatar
Kenichi Handa committed
30 31 32 33 34 35 36 37 38 39 40
  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
41 42 43

*/

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


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

Dave Love's avatar
Dave Love committed
49 50
  A coding system is an object for an encoding mechanism that contains
  information about how to convert byte sequences to character
51 52 53 54 55
  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
56
  coding system.
Karl Heuer's avatar
Karl Heuer committed
57

58 59
  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
60
  stored in the hash table Vcharset_hash_table.  The conversion from
61 62
  coding system symbol to attributes vector is done by looking up
  Vcharset_hash_table by the symbol.
Karl Heuer's avatar
Karl Heuer committed
63

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

Kenichi Handa's avatar
Kenichi Handa committed
67 68 69 70 71 72 73
  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
74
  Decoding and encoding are done by a code converter defined for each
Kenichi Handa's avatar
Kenichi Handa committed
75 76
  character set.

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

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

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

  The most famous coding system for multiple character sets.  X's
Kenichi Handa's avatar
Kenichi Handa committed
84 85 86
  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
87

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

Karl Heuer's avatar
Karl Heuer committed
90 91
  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
92
  section 8.
Karl Heuer's avatar
Karl Heuer committed
93

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

Kenichi Handa's avatar
Kenichi Handa committed
96
  A coding system to encode character sets: ASCII and Big5.  Widely
Dave Love's avatar
Dave Love committed
97
  used for Chinese (mainly in Taiwan and Hong Kong).  Details are
Kenichi Handa's avatar
Kenichi Handa committed
98 99 100
  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
101

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

Dave Love's avatar
Dave Love committed
104
  If a user wants to decode/encode text encoded in a coding system
Kenichi Handa's avatar
Kenichi Handa committed
105 106 107
  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.
108

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

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

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

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


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

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

Dave Love's avatar
Dave Love committed
128
  Since text character encoding and end-of-line encoding are
Kenichi Handa's avatar
Kenichi Handa committed
129 130
  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
131

132 133 134 135 136
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
137
  conversion (e.g. the location of source and destination data).
Karl Heuer's avatar
Karl Heuer committed
138 139 140

*/

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


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

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

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

  Below is the template of these functions.  */

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

  while (1)
    {
Kenichi Handa's avatar
Kenichi Handa committed
168
      /* Get one byte from the source.  If the source is exhausted, jump
Kenichi Handa's avatar
Kenichi Handa committed
169 170
	 to no_more_source:.  */
      ONE_MORE_BYTE (c);
171 172 173 174 175

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

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

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

Kenichi Handa's avatar
Kenichi Handa committed
190 191 192 193
  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;
194

Kenichi Handa's avatar
Kenichi Handa committed
195 196 197 198
  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.
199

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

Karl Heuer's avatar
Karl Heuer committed
202
#if 0
203
static void
204
decode_coding_XXXX (struct coding_system *coding)
Karl Heuer's avatar
Karl Heuer committed
205
{
206 207
  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
208 209 210
  /* 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.  */
211
  const unsigned char *src_base;
Kenichi Handa's avatar
Kenichi Handa committed
212
  /* A buffer to produce decoded characters.  */
213 214
  int *charbuf = coding->charbuf + coding->charbuf_used;
  int *charbuf_end = coding->charbuf + coding->charbuf_size;
Kenichi Handa's avatar
Kenichi Handa committed
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
  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
239 240 241 242 243
}
#endif

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

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

Kenichi Handa's avatar
Kenichi Handa committed
249 250 251 252
  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.
253

Kenichi Handa's avatar
Kenichi Handa committed
254 255 256
  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.
257

Kenichi Handa's avatar
Kenichi Handa committed
258
  Below is a template of these functions.  */
Karl Heuer's avatar
Karl Heuer committed
259
#if 0
260
static void
261
encode_coding_XXX (struct coding_system *coding)
Karl Heuer's avatar
Karl Heuer committed
262
{
Kenichi Handa's avatar
Kenichi Handa committed
263 264 265 266 267 268
  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_;
269
  EMACS_INT produced_chars = 0;
Kenichi Handa's avatar
Kenichi Handa committed
270 271 272 273 274 275 276 277 278 279

  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
280 281 282 283 284 285
}
#endif


/*** 1. Preamble ***/

286
#include <config.h>
Karl Heuer's avatar
Karl Heuer committed
287
#include <stdio.h>
288
#include <setjmp.h>
Karl Heuer's avatar
Karl Heuer committed
289 290 291

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

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

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

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
Lisp_Object Qstart_process, Qopen_network_stream;
323
static Lisp_Object Qtarget_idx;
Karl Heuer's avatar
Karl Heuer committed
324

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

328 329 330 331
/* If a symbol has this property, evaluate the value to define the
   symbol as a coding system.  */
static Lisp_Object Qcoding_system_define_form;

332 333 334 335 336 337
/* Format of end-of-line decided by system.  This is Qunix on
   Unix and Mac, Qdos on DOS/Windows.
   This has an effect only for external encoding (i.e. for output to
   file and process), not for in-buffer or Lisp string encoding.  */
static Lisp_Object system_eol_type;

Karl Heuer's avatar
Karl Heuer committed
338 339
#ifdef emacs

340
Lisp_Object Qcoding_system_p, Qcoding_system_error;
Karl Heuer's avatar
Karl Heuer committed
341

342 343 344
/* 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
345
Lisp_Object Qutf_8_emacs;
Dave Love's avatar
Dave Love committed
346

Karl Heuer's avatar
Karl Heuer committed
347 348
/* Coding-systems are handed between Emacs Lisp programs and C internal
   routines by the following three variables.  */
349 350 351 352
/* Coding system to be used to encode text for terminal display when
   terminal coding system is nil.  */
struct coding_system safe_terminal_coding;

Karl Heuer's avatar
Karl Heuer committed
353 354
#endif /* emacs */

355 356
Lisp_Object Qtranslation_table;
Lisp_Object Qtranslation_table_id;
357 358
static Lisp_Object Qtranslation_table_for_decode;
static Lisp_Object Qtranslation_table_for_encode;
Karl Heuer's avatar
Karl Heuer committed
359

Kenichi Handa's avatar
Kenichi Handa committed
360
/* Two special coding systems.  */
361 362
static Lisp_Object Vsjis_coding_system;
static Lisp_Object Vbig5_coding_system;
Kenichi Handa's avatar
Kenichi Handa committed
363 364 365 366 367 368 369 370 371

/* ISO2022 section */

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


372 373 374 375 376
#define CODING_ISO_REQUEST(coding, charset_id)		\
  (((charset_id) <= (coding)->max_charset_id		\
    ? ((coding)->safe_charsets[charset_id] != 255	\
       ? (coding)->safe_charsets[charset_id]		\
       : -1)						\
Kenichi Handa's avatar
Kenichi Handa committed
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
    : -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)))
392 393 394 395 396 397
#define CODING_ISO_CMP_STATUS(coding)	\
  (&(coding)->spec.iso_2022.cmp_status)
#define CODING_ISO_EXTSEGMENT_LEN(coding)	\
  ((coding)->spec.iso_2022.ctext_extended_segment_len)
#define CODING_ISO_EMBEDDED_UTF_8(coding)	\
  ((coding)->spec.iso_2022.embedded_utf_8)
Kenichi Handa's avatar
Kenichi Handa committed
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430

/* Control characters of ISO2022.  */
			/* code */	/* function */
#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.  */
  };
431

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

Kenichi Handa's avatar
Kenichi Handa committed
435 436 437
/* 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
438

Kenichi Handa's avatar
Kenichi Handa committed
439 440 441
/* If set, reset graphic planes and registers at end-of-line to the
   initial state.  */
#define CODING_ISO_FLAG_RESET_AT_EOL	0x0002
442

Kenichi Handa's avatar
Kenichi Handa committed
443 444 445
/* If set, reset graphic planes and registers before any control
   characters to the initial state.  */
#define CODING_ISO_FLAG_RESET_AT_CNTL	0x0004
446

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

Kenichi Handa's avatar
Kenichi Handa committed
450 451
/* If set, use locking-shift function.  */
#define CODING_ISO_FLAG_LOCKING_SHIFT	0x0010
452

Kenichi Handa's avatar
Kenichi Handa committed
453 454 455
/* If set, use single-shift function.  Overwrite
   CODING_ISO_FLAG_LOCKING_SHIFT.  */
#define CODING_ISO_FLAG_SINGLE_SHIFT	0x0020
456

Kenichi Handa's avatar
Kenichi Handa committed
457 458
/* If set, use designation escape sequence.  */
#define CODING_ISO_FLAG_DESIGNATION	0x0040
459

Kenichi Handa's avatar
Kenichi Handa committed
460 461
/* If set, produce revision number sequence.  */
#define CODING_ISO_FLAG_REVISION	0x0080
462

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

Kenichi Handa's avatar
Kenichi Handa committed
466 467 468
/* 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
469

Kenichi Handa's avatar
Kenichi Handa committed
470 471 472
/* If set, designation sequence should be placed at beginning of line
   on output.  */
#define CODING_ISO_FLAG_DESIGNATE_AT_BOL 0x0400
473

Kenichi Handa's avatar
Kenichi Handa committed
474
/* If set, do not encode unsafe characters on output.  */
Kenichi Handa's avatar
Kenichi Handa committed
475
#define CODING_ISO_FLAG_SAFE		0x0800
476

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

Kenichi Handa's avatar
Kenichi Handa committed
481
#define CODING_ISO_FLAG_COMPOSITION	0x2000
482

483
/* #define CODING_ISO_FLAG_EUC_TW_SHIFT	0x4000 */
484

485
#define CODING_ISO_FLAG_USE_ROMAN	0x8000
486

487
#define CODING_ISO_FLAG_USE_OLDJIS	0x10000
488

489
#define CODING_ISO_FLAG_FULL_SUPPORT	0x100000
490

Kenichi Handa's avatar
Kenichi Handa committed
491 492 493
/* 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  '?'
494

Kenichi Handa's avatar
Kenichi Handa committed
495 496 497
/* UTF-8 section */
#define CODING_UTF_8_BOM(coding)	\
  ((coding)->spec.utf_8_bom)
Karl Heuer's avatar
Karl Heuer committed
498

Kenichi Handa's avatar
Kenichi Handa committed
499 500 501
/* UTF-16 section */
#define CODING_UTF_16_BOM(coding)	\
  ((coding)->spec.utf_16.bom)
Karl Heuer's avatar
Karl Heuer committed
502

Kenichi Handa's avatar
Kenichi Handa committed
503 504
#define CODING_UTF_16_ENDIAN(coding)	\
  ((coding)->spec.utf_16.endian)
Karl Heuer's avatar
Karl Heuer committed
505

Kenichi Handa's avatar
Kenichi Handa committed
506 507
#define CODING_UTF_16_SURROGATE(coding)	\
  ((coding)->spec.utf_16.surrogate)
Karl Heuer's avatar
Karl Heuer committed
508 509


Kenichi Handa's avatar
Kenichi Handa committed
510 511 512 513 514 515
/* 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
516
  (SDATA (AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_valids)))
Karl Heuer's avatar
Karl Heuer committed
517

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

Kenichi Handa's avatar
Kenichi Handa committed
520 521 522 523 524 525 526 527
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,
Kenichi Handa's avatar
Kenichi Handa committed
528 529 530
    coding_category_utf_8_auto,
    coding_category_utf_8_nosig,
    coding_category_utf_8_sig,
Kenichi Handa's avatar
Kenichi Handa committed
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
    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)
Kenichi Handa's avatar
Kenichi Handa committed
554 555 556
#define CATEGORY_MASK_UTF_8_AUTO	(1 << coding_category_utf_8_auto)
#define CATEGORY_MASK_UTF_8_NOSIG	(1 << coding_category_utf_8_nosig)
#define CATEGORY_MASK_UTF_8_SIG		(1 << coding_category_utf_8_sig)
557
#define CATEGORY_MASK_UTF_16_AUTO	(1 << coding_category_utf_16_auto)
Kenichi Handa's avatar
Kenichi Handa committed
558 559 560 561 562 563 564 565 566
#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)
567
#define CATEGORY_MASK_RAW_TEXT		(1 << coding_category_raw_text)
Kenichi Handa's avatar
Kenichi Handa committed
568 569 570 571 572 573 574 575 576 577

/* 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		\
Kenichi Handa's avatar
Kenichi Handa committed
578 579 580
   | CATEGORY_MASK_UTF_8_AUTO		\
   | CATEGORY_MASK_UTF_8_NOSIG		\
   | CATEGORY_MASK_UTF_8_SIG		\
581
   | CATEGORY_MASK_UTF_16_AUTO		\
Kenichi Handa's avatar
Kenichi Handa committed
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
   | 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		\
614 615
  (CATEGORY_MASK_UTF_16_AUTO		\
   | CATEGORY_MASK_UTF_16_BE		\
Kenichi Handa's avatar
Kenichi Handa committed
616 617 618 619
   | CATEGORY_MASK_UTF_16_LE		\
   | CATEGORY_MASK_UTF_16_BE_NOSIG	\
   | CATEGORY_MASK_UTF_16_LE_NOSIG)

Kenichi Handa's avatar
Kenichi Handa committed
620 621 622 623
#define CATEGORY_MASK_UTF_8	\
  (CATEGORY_MASK_UTF_8_AUTO	\
   | CATEGORY_MASK_UTF_8_NOSIG	\
   | CATEGORY_MASK_UTF_8_SIG)
Kenichi Handa's avatar
Kenichi Handa committed
624 625

/* Table of coding categories (Lisp symbols).  This variable is for
Kenichi Handa's avatar
Kenichi Handa committed
626
   internal use only.  */
Kenichi Handa's avatar
Kenichi Handa committed
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
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
644

645 646 647 648
#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
649
  } while (0)
Karl Heuer's avatar
Karl Heuer committed
650 651


Kenichi Handa's avatar
Kenichi Handa committed
652 653
/* 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
654 655 656 657 658
   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 */
659

660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
#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						\
	  {						\
676 677
	    src--;					\
	    c = - string_char (src, &src, NULL);	\
678 679 680 681 682
	    record_conversion_result			\
	      (coding, CODING_RESULT_INVALID_SRC);	\
	  }						\
      }							\
    consumed_chars++;					\
683 684
  } while (0)

Kenichi Handa's avatar
Kenichi Handa committed
685
/* Safely get two bytes from the source text pointed by SRC which ends
Kenichi Handa's avatar
Kenichi Handa committed
686 687 688 689 690 691
   at SRC_END, and set C1 and C2 to those bytes while skipping the
   heading multibyte characters.  If there are not enough bytes in the
   source, it jumps to `no_more_source'.  If multibytep is nonzero and
   a multibyte character is found for C2, set C2 to the negative value
   of the character code.  The caller should declare and set these
   variables appropriately in advance:
Kenichi Handa's avatar
Kenichi Handa committed
692 693 694
	src, src_end, multibytep
   It is intended that this macro is used in detect_coding_utf_16.  */

Kenichi Handa's avatar
Kenichi Handa committed
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
#define TWO_MORE_BYTES(c1, c2)				\
  do {							\
    do {						\
      if (src == src_end)				\
	goto no_more_source;				\
      c1 = *src++;					\
      if (multibytep && (c1 & 0x80))			\
	{						\
	  if ((c1 & 0xFE) == 0xC0)			\
	    c1 = ((c1 & 1) << 6) | *src++;		\
	  else						\
	    {						\
	      src += BYTES_BY_CHAR_HEAD (c1) - 1;	\
	      c1 = -1;					\
	    }						\
	}						\
    } while (c1 < 0);					\
    if (src == src_end)					\
      goto no_more_source;				\
    c2 = *src++;					\
    if (multibytep && (c2 & 0x80))			\
      {							\
	if ((c2 & 0xFE) == 0xC0)			\
	  c2 = ((c2 & 1) << 6) | *src++;		\
	else						\
	  c2 = -1;					\
      }							\
Kenichi Handa's avatar
Kenichi Handa committed
722 723
  } while (0)

724

Kenichi Handa's avatar
Kenichi Handa committed
725 726 727 728 729
/* 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.
*/
730 731


Kenichi Handa's avatar
Kenichi Handa committed
732 733 734 735
#define EMIT_ONE_ASCII_BYTE(c)	\
  do {				\
    produced_chars++;		\
    *dst++ = (c);		\
736
  } while (0)
737 738


Kenichi Handa's avatar
Kenichi Handa committed
739
/* Like EMIT_ONE_ASCII_BYTE but store two bytes; C1 and C2.  */
740

Kenichi Handa's avatar
Kenichi Handa committed
741 742 743 744 745
#define EMIT_TWO_ASCII_BYTES(c1, c2)	\
  do {					\
    produced_chars += 2;		\
    *dst++ = (c1), *dst++ = (c2);	\
  } while (0)
746 747


Kenichi Handa's avatar
Kenichi Handa committed
748 749 750 751 752 753 754 755 756 757 758
/* 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)			\
      {					\
759
	unsigned ch = (c);		\
Kenichi Handa's avatar
Kenichi Handa committed
760 761 762 763 764 765
	if (ch >= 0x80)			\
	  ch = BYTE8_TO_CHAR (ch);	\
	CHAR_STRING_ADVANCE (ch, dst);	\
      }					\
    else				\
      *dst++ = (c);			\
766 767 768
  } while (0)


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

771 772 773 774 775
#define EMIT_TWO_BYTES(c1, c2)		\
  do {					\
    produced_chars += 2;		\
    if (multibytep)			\
      {					\
776
	unsigned ch;			\
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
					\
	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);			\
      }					\
792 793 794
  } while (0)


Kenichi Handa's avatar
Kenichi Handa committed
795 796 797 798 799
#define EMIT_THREE_BYTES(c1, c2, c3)	\
  do {					\
    EMIT_ONE_BYTE (c1);			\
    EMIT_TWO_BYTES (c2, c3);		\
  } while (0)
800 801


Kenichi Handa's avatar
Kenichi Handa committed
802 803 804 805 806
#define EMIT_FOUR_BYTES(c1, c2, c3, c4)		\
  do {						\
    EMIT_TWO_BYTES (c1, c2);			\
    EMIT_TWO_BYTES (c3, c4);			\
  } while (0)
807 808


809
/* Prototypes for static functions.  */
810 811 812 813 814 815 816 817 818 819 820 821 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
static void record_conversion_result (struct coding_system *coding,
                                      enum coding_result_code result);
static int detect_coding_utf_8 (struct coding_system *,
                                struct coding_detection_info *info);
static void decode_coding_utf_8 (struct coding_system *);
static int encode_coding_utf_8 (struct coding_system *);

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

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

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

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

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

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

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

static void coding_set_source (struct coding_system *);
static void coding_set_destination (struct coding_system *);
static void coding_alloc_by_realloc (struct coding_system *, EMACS_INT);
static void coding_alloc_by_making_gap (struct coding_system *,
                                        EMACS_INT, EMACS_INT);
static unsigned char *alloc_destination (struct coding_system *,
                                         EMACS_INT, unsigned char *);
static void setup_iso_safe_charsets (Lisp_Object);
static unsigned char *encode_designation_at_bol (struct coding_system *,
859
                                                 int *, unsigned char *);
860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882
static int detect_eol (const unsigned char *,
                       EMACS_INT, enum coding_category);
static Lisp_Object adjust_coding_eol_type (struct coding_system *, int);
static void decode_eol (struct coding_system *);
static Lisp_Object get_translation_table (Lisp_Object, int, int *);
static Lisp_Object get_translation (Lisp_Object, int *, int *);
static int produce_chars (struct coding_system *, Lisp_Object, int);
static INLINE void produce_charset (struct coding_system *, int *,
                                    EMACS_INT);
static void produce_annotation (struct coding_system *, EMACS_INT);
static int decode_coding (struct coding_system *);
static INLINE int *handle_composition_annotation (EMACS_INT, EMACS_INT,
                                                  struct coding_system *,
                                                  int *, EMACS_INT *);
static INLINE int *handle_charset_annotation (EMACS_INT, EMACS_INT,
                                              struct coding_system *,
                                              int *, EMACS_INT *);
static void consume_chars (struct coding_system *, Lisp_Object, int);
static int encode_coding (struct coding_system *);
static Lisp_Object make_conversion_work_buffer (int);
static Lisp_Object code_conversion_restore (Lisp_Object);
static INLINE int char_encodable_p (int, Lisp_Object);
static Lisp_Object make_subsidiaries (Lisp_Object);
883

884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905
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;
906 907 908 909 910
    case CODING_RESULT_INSUFFICIENT_DST:
      /* Don't record this error in Vlast_code_conversion_error
	 because it happens just temporarily and is resolved when the
	 whole conversion is finished.  */
      break;
911 912
    case CODING_RESULT_SUCCESS:
      break;
913 914
    default:
      Vlast_code_conversion_error = intern ("Unknown error");
915 916 917
    }
}

918 919 920 921
/* This wrapper macro is used to preserve validity of pointers into
   buffer text across calls to decode_char, which could cause
   relocation of buffers if it loads a charset map, because loading a
   charset map allocates large structures.  */
Kenichi Handa's avatar
Kenichi Handa committed
922 923 924 925 926 927
#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
928
	const unsigned char *orig = coding->source;			     \
Kenichi Handa's avatar
Kenichi Handa committed
929 930 931 932 933 934 935 936
	EMACS_INT offset;						     \
									     \
	coding_set_source (coding);					     \
	offset = coding->source - orig;					     \
	src += offset;							     \
	src_base += offset;						     \
	src_end += offset;						     \
      }									     \
937 938 939
  } while (0)


940 941 942 943 944
/* If there are at least BYTES length of room at dst, allocate memory
   for coding->destination and update dst and dst_end.  We don't have
   to take care of coding->source which will be relocated.  It is
   handled by calling coding_set_source in encode_coding.  */

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


Kenichi Handa's avatar
Kenichi Handa committed
957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
/* Store multibyte form of the character C in P, and advance P to the
   end of the multibyte form.  This is like CHAR_STRING_ADVANCE but it
   never calls MAYBE_UNIFY_CHAR.  */

#define CHAR_STRING_ADVANCE_NO_UNIFY(c, p)	\
  do {						\
    if ((c) <= MAX_1_BYTE_CHAR)			\
      *(p)++ = (c);				\
    else if ((c) <= MAX_2_BYTE_CHAR)		\
      *(p)++ = (0xC0 | ((c) >> 6)),		\
	*(p)++ = (0x80 | ((c) & 0x3F));		\
    else if ((c) <= MAX_3_BYTE_CHAR)		\
      *(p)++ = (0xE0 | ((c) >> 12)),		\
	*(p)++ = (0x80 | (((c) >> 6) & 0x3F)),	\
	*(p)++ = (0x80 | ((c) & 0x3F));		\
    else if ((c) <= MAX_4_BYTE_CHAR)		\
      *(p)++ = (0xF0 | (c >> 18)),		\
	*(p)++ = (0x80 | ((c >> 12) & 0x3F)),	\
	*(p)++ = (0x80 | ((c >> 6) & 0x3F)),	\
	*(p)++ = (0x80 | (c & 0x3F));		\
    else if ((c) <= MAX_5_BYTE_CHAR)		\
      *(p)++ = 0xF8,				\
	*(p)++ = (0x80 | ((c >> 18) & 0x0F)),	\
	*(p)++ = (0x80 | ((c >> 12) & 0x3F)),	\
	*(p)++ = (0x80 | ((c >> 6) & 0x3F)),	\
	*(p)++ = (0x80 | (c & 0x3F));		\
    else					\
      (p) += BYTE8_STRING ((c) - 0x3FFF80, p);	\
  } while (0)


/* Return the character code of character whose multibyte form is at
   P, and advance P to the end of the multibyte form.  This is like
   STRING_CHAR_ADVANCE, but it never calls MAYBE_UNIFY_CHAR.  */

#define STRING_CHAR_ADVANCE_NO_UNIFY(p)				\
  (!((p)[0] & 0x80)						\
   ? *(p)++							\
   : ! ((p)[0] & 0x20)						\
   ? ((p) += 2,							\
      ((((p)[-2] & 0x1F) << 6)					\
       | ((p)[-1] & 0x3F)					\
       | ((unsigned char) ((p)[-2]) < 0xC2 ? 0x3FFF80 : 0)))	\
   : ! ((p)[0] & 0x10)						\
   ? ((p) += 3,							\
      ((((p)[-3] & 0x0F) << 12)					\
       | (((p)[-2] & 0x3F) << 6)				\
       | ((p)[-1] & 0x3F)))					\
   : ! ((p)[0] & 0x08)						\
   ? ((p) += 4,							\
      ((((p)[-4] & 0xF) << 18)					\
       | (((p)[-3] & 0x3F) << 12)				\
       | (((p)[-2] & 0x3F) << 6)				\
       | ((p)[-1] & 0x3F)))					\
   : ((p) += 5,							\
      ((((p)[-4] & 0x3F) << 18)					\
       | (((p)[-3] & 0x3F) << 12)				\
       | (((p)[-2] & 0x3F) << 6)				\
       | ((p)[-1] & 0x3F))))

1017

Kenichi Handa's avatar
Kenichi Handa committed
1018
static void
1019
coding_set_source (struct coding_system *coding)
1020
{
Kenichi Handa's avatar
Kenichi Handa committed
1021 1022
  if (BUFFERP (coding->src_object))
    {
1023
      struct buffer *buf = XBUFFER (coding->src_object);
1024

Kenichi Handa's avatar
Kenichi Handa committed
1025
      if (coding->src_pos < 0)
1026
	coding->source = BUF_GAP_END_ADDR (buf) + coding->src_pos_byte;
Kenichi Handa's avatar
Kenichi Handa committed
1027
      else
1028
	coding->source = BUF_BYTE_ADDRESS (buf, coding->src_pos_byte);
1029
    }
Kenichi Handa's avatar
Kenichi Handa committed
1030
  else if (STRINGP (coding->src_object))
1031
    {
Kenichi Handa's avatar
Kenichi Handa committed
1032
      coding->source = SDATA (coding->src_object) + coding->src_pos_byte;
1033
    }
Kenichi Handa's avatar
Kenichi Handa committed
1034
  else
1035 1036 1037 1038
    {
      /* Otherwise, the source is C string and is never relocated
	 automatically.  Thus we don't have to update anything.  */
    }
Kenichi Handa's avatar
Kenichi Handa committed
1039
}
1040

Kenichi Handa's avatar
Kenichi Handa committed
1041
static void
1042
coding_set_destination (struct coding_system *coding)
Kenichi Handa's avatar
Kenichi Handa committed
1043 1044
{
  if (BUFFERP (coding->dst_object))
1045
    {
Kenichi Handa's avatar
Kenichi Handa committed
1046
      if (coding->src_pos < 0)
1047
	{
1048
	  coding->destination = BEG_ADDR + coding->dst_pos_byte - BEG_BYTE;
1049 1050 1051
	  coding->dst_bytes = (GAP_END_ADDR
			       - (coding->src_bytes - coding->consumed)
			       - coding->destination);
1052
	}
Kenichi Handa's avatar
Kenichi Handa committed
1053
      else
1054 1055 1056 1057
	{
	  /* We are sure that coding->dst_pos_byte is before the gap
	     of the buffer. */
	  coding->destination = (BUF_BEG_ADDR (XBUFFER (coding->dst_object))
1058
				 + coding->dst_pos_byte - BEG_BYTE);
1059 1060 1061
	  coding->dst_bytes = (BUF_GAP_END_ADDR (XBUFFER (coding->dst_object))
			       - coding->destination);
	}
Kenichi Handa's avatar
Kenichi Handa committed
1062 1063
    }
  else
1064 1065 1066 1067
    {
      /* Otherwise, the destination is C string and is never relocated
	 automatically.  Thus we don't have to update anything.  */
    }
Kenichi Handa's avatar
Kenichi Handa committed
1068 1069 1070 1071
}


static void
1072
coding_alloc_by_realloc (struct coding_system *coding, EMACS_INT bytes)
Kenichi Handa's avatar
Kenichi Handa committed
1073
{
1074 1075
  if (coding->dst_bytes >= MOST_POSITIVE_FIXNUM - bytes)
    error ("Maximum size of buffer or string exceeded");
Kenichi Handa's avatar
Kenichi Handa committed
1076 1077 1078 1079 1080 1081
  coding->destination = (unsigned char *) xrealloc (coding->destination,
						    coding->dst_bytes + bytes);
  coding->dst_bytes += bytes;
}

static void
1082 1083
coding_alloc_by_making_gap (struct coding_system *coding,
			    EMACS_INT gap_head_used, EMACS_INT bytes)
Kenichi Handa's avatar
Kenichi Handa committed
1084
{
Kenichi Handa's avatar
Kenichi Handa committed
1085
  if (EQ (coding->src_object, coding->dst_object))
Kenichi Handa's avatar
Kenichi Handa committed
1086
    {
Kenichi Handa's avatar
Kenichi Handa committed
1087 1088 1089 1090 1091 1092 1093 1094
      /* The gap may contain the produced data at the head and not-yet
	 consumed data at the tail.  To preserve those data, we at
	 first make the gap size to zero, then increase the gap
	 size.  */
      EMACS_INT add = GAP_SIZE;

      GPT += gap_head_used, GPT_BYTE += gap_head_used;
      GAP_SIZE = 0; ZV += add; Z += add; ZV_BYTE += add; Z_BYTE += add;
Kenichi Handa's avatar
Kenichi Handa committed
1095 1096
      make_gap (bytes);
      GAP_SIZE += add; ZV -= add; Z -= add; ZV_BYTE -= add; Z_BYTE -= add;
Kenichi Handa's avatar
Kenichi Handa committed
1097
      GPT -= gap_head_used, GPT_BYTE -= gap_head_used;
Kenichi Handa's avatar
Kenichi Handa committed
1098
    }
1099
  else
Kenichi Handa's avatar
Kenichi Handa committed
1100
    {
1101 1102 1103
      Lisp_Object this_buffer;

      this_buffer = Fcurrent_buffer ();
Kenichi Handa's avatar
Kenichi Handa committed
1104 1105 1106
      set_buffer_internal (XBUFFER (coding->dst_object));
      make_gap (bytes);
      set_buffer_internal (XBUFFER (this_buffer));
1107
    }
Kenichi Handa's avatar
Kenichi Handa committed
1108
}
Kenichi Handa's avatar
Kenichi Handa committed
1109

Kenichi Handa's avatar
Kenichi Handa committed
1110 1111

static unsigned char *
1112 1113
alloc_destination (struct coding_system *coding, EMACS_INT nbytes,
		   unsigned char *dst)
Kenichi Handa's avatar
Kenichi Handa committed
1114 1115 1116 1117
{
  EMACS_INT offset = dst - coding->destination;

  if (BUFFERP (coding->dst_object))
Kenichi Handa's avatar
Kenichi Handa committed
1118 1119 1120 1121 1122
    {
      struct buffer *buf = XBUFFER (coding->dst_object);

      coding_alloc_by_making_gap (coding, dst - BUF_GPT_ADDR (buf), nbytes);
    }
1123
  else
Kenichi Handa's avatar
Kenichi Handa committed
1124 1125 1126 1127 1128
    coding_alloc_by_realloc (coding, nbytes);
  coding_set_destination (coding);
  dst = coding->destination + offset;
  return dst;
}
1129

1130 1131 1132 1133
/** Macros for annotations.  */

/* An annotation data is stored in the array coding->charbuf in this
   format:
1134
     [ -LENGTH ANNOTATION_MASK NCHARS ... ]
1135 1136
   LENGTH is the number of elements in the annotation.
   ANNOTATION_MASK is one of CODING_ANNOTATE_XXX_MASK.
1137
   NCHARS is the number of characters in the text annotated.
1138 1139 1140 1141 1142

   The format of the following elements depend on ANNOTATION_MASK.

   In the case of CODING_ANNOTATE_COMPOSITION_MASK, these elements
   follows:
1143 1144 1145 1146 1147 1148
     ... NBYTES METHOD [ COMPOSITION-COMPONENTS ... ]

   NBYTES is the number of bytes specified in the header part of
   old-style emacs-mule encoding, or 0 for the other kind of
   composition.

1149
   METHOD is one of enum composition_method.
1150

Kenichi Handa's avatar
Kenichi Handa committed
1151
   Optional COMPOSITION-COMPONENTS are characters and composition
1152 1153 1154
   rules.

   In the case of CODING_ANNOTATE_CHARSET_MASK, one element CHARSET-ID
1155 1156 1157 1158 1159 1160 1161 1162
   follows.

   If ANNOTATION_MASK is 0, this annotation is just a space holder to
   recover from an invalid annotation, and should be skipped by
   produce_annotation.  */

/* Maximum length of the header of annotation data.  */
#define MAX_ANNOTATION_LENGTH 5
1163

1164
#define ADD_ANNOTATION_DATA(buf, len, mask, nchars)	\
1165 1166 1167
  do {							\
    *(buf)++ = -(len);					\
    *(buf)++ = (mask);					\
1168
    *(buf)++ = (nchars);				\
1169 1170 1171
    coding->annotated = 1;				\
  } while (0);

1172
#define ADD_COMPOSITION_DATA(buf, nchars, nbytes, method)		    \
1173
  do {									    \
1174 1175
    ADD_ANNOTATION_DATA (buf, 5, CODING_ANNOTATE_COMPOSITION_MASK, nchars); \
    *buf++ = nbytes;							    \
1176
    *buf++ = method;							    \
1177 1178 1179
  } while (0)


1180 1181 1182 1183
#define ADD_CHARSET_DATA(buf, nchars, id)				\
  do {									\
    ADD_ANNOTATION_DATA (buf, 4, CODING_ANNOTATE_CHARSET_MASK, nchars);	\
    *buf++ = id;							\
1184 1185
  } while (0)

Kenichi Handa's avatar
Kenichi Handa committed
1186 1187 1188 1189 1190 1191 1192 1193 1194

/*** 2. Emacs' internal format (emacs-utf-8) ***/



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

/* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1195 1196
   Check if a text is encoded in UTF-8.  If it is, return 1, else
   return 0.  */
Kenichi Handa's avatar
Kenichi Handa committed
1197 1198 1199 1200 1201 1202 1203 1204

#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)

Kenichi Handa's avatar
Kenichi Handa committed
1205 1206 1207 1208
#define UTF_8_BOM_1 0xEF
#define UTF_8_BOM_2 0xBB
#define UTF_8_BOM_3 0xBF

Kenichi Handa's avatar
Kenichi Handa committed
1209
static int
1210 1211
detect_coding_utf_8 (struct coding_system *coding,
		     struct coding_detection_info *detect_info)
Kenichi Handa's avatar
Kenichi Handa committed
1212
{
1213
  const unsigned char *src = coding->source, *src_base;
Kenichi Handa's avatar
Kenichi Handa committed
1214
  const unsigned char *src_end = coding->source + coding->src_bytes;
Kenichi Handa's avatar
Kenichi Handa committed
1215
  int multibytep = coding->src_multibyte;
1216
  EMACS_INT consumed_chars = 0;
Kenichi Handa's avatar
Kenichi Handa committed
1217
  int bom_found = 0;
Kenichi Handa's avatar
Kenichi Handa committed
1218 1219
  int found = 0;

1220
  detect_info->checked |= CATEGORY_MASK_UTF_8;
Kenichi Handa's avatar
Kenichi Handa committed
1221 1222 1223 1224
  /* A coding system of this category is always ASCII compatible.  */
  src += coding->head_ascii;

  while (1)
1225
    {
Kenichi Handa's avatar
Kenichi Handa committed
1226
      int c, c1, c2, c3, c4;
1227

1228
      src_base = src;
Kenichi Handa's avatar
Kenichi Handa committed
1229
      ONE_MORE_BYTE (c);
1230
      if (c < 0 || UTF_8_1_OCTET_P (c))
Kenichi Handa's avatar
Kenichi Handa committed
1231 1232
	continue;
      ONE_MORE_BYTE (c1);
1233
      if (c1 < 0 || ! UTF_8_EXTRA_OCTET_P (c1))
Kenichi Handa's avatar
Kenichi Handa committed
1234 1235
	break;
      if (UTF_8_2_OCTET_LEADING_P (c))
1236
	{
Kenichi Handa's avatar
Kenichi Handa committed
1237
	  found = 1;
Kenichi Handa's avatar
Kenichi Handa committed
1238
	  continue;
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
	break;
      if (UTF_8_3_OCTET_LEADING_P (c))
1244
	{
Kenichi Handa's avatar
Kenichi Handa committed
1245 1246 1247 1248
	  found = 1;
	  if (src_base == coding->source
	      && c == UTF_8_BOM_1 && c1 == UTF_8_BOM_2 && c2 == UTF_8_BOM_3)
	    bom_found = 1;
Kenichi Handa's avatar
Kenichi Handa committed
1249
	  continue;
1250
	}
Kenichi Handa's avatar
Kenichi Handa committed
1251
      ONE_MORE_BYTE (c3);
1252
      if (c3 < 0 || ! UTF_8_EXTRA_OCTET_P (c3))
Kenichi Handa's avatar
Kenichi Handa committed
1253 1254
	break;
      if (UTF_8_4_OCTET_LEADING_P (c))
1255
	{
Kenichi Handa's avatar
Kenichi Handa committed
1256
	  found = 1;
Kenichi Handa's avatar
Kenichi Handa committed
1257 1258 1259
	  continue;
	}
      ONE_MORE_BYTE (c4);
1260
      if (c4 < 0 || ! UTF_8_EXTRA_OCTET_P (c4))
Kenichi Handa's avatar
Kenichi Handa committed
1261 1262 1263
	break;
      if (UTF_8_5_OCTET_LEADING_P (c))
	{
Kenichi Handa's avatar
Kenichi Handa committed
1264
	  found = 1;
Kenichi Handa's avatar
Kenichi Handa committed
1265 1266 1267
	  continue;
	}
      break;
1268
    }
1269
  detect_info->rejected |= CATEGORY_MASK_UTF_8;
Kenichi Handa's avatar
Kenichi Handa committed
1270
  return 0;
1271

Kenichi Handa's avatar
Kenichi Handa committed
1272
 no_more_source:
1273
  if (src_base < src && coding->mode & CODING_MODE_LAST_BLOCK)
1274
    {
1275
      detect_info->rejected |= CATEGORY_MASK_UTF_8;
1276
      return 0;
1277
    }
Kenichi Handa's avatar
Kenichi Handa committed
1278 1279 1280 1281 1282 1283 1284 1285
  if (bom_found)
    {
      /* The first character 0xFFFE doesn't necessarily mean a BOM.  */
      detect_info->found |= CATEGORY_MASK_UTF_8_SIG | CATEGORY_MASK_UTF_8_NOSIG;
    }
  else
    {
      detect_info->rejected |= CATEGORY_MASK_UTF_8_SIG;
1286 1287
      if (found)
	detect_info->found |= CATEGORY_MASK_UTF_8_NOSIG;
Kenichi Handa's avatar
Kenichi Handa committed
1288
    }
1289
  return 1;
1290 1291
}

Karl Heuer's avatar
Karl Heuer committed
1292

1293
static void
1294
decode_coding_utf_8 (struct coding_system *coding)
1295
{
Kenichi Handa's avatar
Kenichi Handa committed
1296 1297 1298
  const unsigned char *src = coding->source + coding->consumed;
  const unsigned char *src_end = coding->source + coding->src_bytes;
  const unsigned char *src_base;
1299 1300
  int *charbuf = coding->charbuf + coding->charbuf_used;
  int *charbuf_end = coding->charbuf + coding->charbuf_size;
1301
  EMACS_INT consumed_chars = 0, consumed_chars_base = 0;
Kenichi Handa's avatar
Kenichi Handa committed
1302
  int multibytep = coding->src_multibyte;
Kenichi Handa's avatar
Kenichi Handa committed
1303
  enum utf_bom_type bom = CODING_UTF_8_BOM (coding);
1304
  int eol_dos =
1305
    !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos);
1306
  int byte_after_cr = -1;
Karl Heuer's avatar
Karl Heuer committed
1307

Kenichi Handa's avatar
Kenichi Handa committed
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317
  if (bom != utf_without_bom)
    {
      int c1, c2, c3;

      src_base = src;
      ONE_MORE_BYTE (c1);
      if (! UTF_8_3_OCTET_LEADING_P (c1))
	src = src_base;
      else
	{
1318
	  ONE_MORE_BYTE (c2);
Kenichi Handa's avatar
Kenichi Handa committed
1319 1320 1321 1322
	  if (! UTF_8_EXTRA_OCTET_P (c2))
	    src = src_base;
	  else
	    {
1323
	      ONE_MORE_BYTE (c3);
Kenichi Handa's avatar
Kenichi Handa committed
1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338
	      if (! UTF_8_EXTRA_OCTET_P (c3))
		src = src_base;
	      else
		{
		  if ((c1 != UTF_8_BOM_1)
		      || (c2 != UTF_8_BOM_2) || (c3 != UTF_8_BOM_3))
		    src = src_base;
		  else
		    CODING_UTF_8_BOM (coding) = utf_without_bom;
		}
	    }
	}
    }
  CODING_UTF_8_BOM (coding) = utf_without_bom;

Kenichi Handa's avatar
Kenichi Handa committed
1339
  while (1)
1340
    {
Kenichi Handa's avatar
Kenichi Handa committed
1341
      int c, c1, c2, c3, c4, c5;
Kenichi Handa's avatar
Kenichi Handa committed
1342

Kenichi Handa's avatar
Kenichi Handa committed
1343 1344
      src_base = src;
      consumed_chars_base = consumed_chars;
1345

Kenichi Handa's avatar
Kenichi Handa committed
1346
      if (charbuf >= charbuf_end)
1347 1348 1349 1350 1351
	{
	  if (byte_after_cr >= 0)
	    src_base--;
	  break;
	}
Kenichi Handa's avatar
Kenichi Handa committed
1352

1353 1354 1355 1356
      if (byte_after_cr >= 0)
	c1 = byte_after_cr, byte_after_cr = -1;
      else
	ONE_MORE_BYTE (c1);
1357 1358 1359 1360
      if (c1 < 0)
	{
	  c = - c1;
	}
1361
      else if (UTF_8_1_OCTET_P (c1))
Kenichi Handa's avatar
Kenichi Handa committed
1362
	{
1363
	  if (eol_dos && c1 == '\r')
1364
	    ONE_MORE_BYTE (byte_after_cr);
Kenichi Handa's avatar
Kenichi Handa committed
1365
	  c = c1;
1366