coding.c 238 KB
Newer Older
Karl Heuer's avatar
Karl Heuer committed
1
/* Coding system handler (conversion, detection, and etc).
Richard M. Stallman's avatar
Richard M. Stallman committed
2
   Copyright (C) 1995, 1997, 1998 Electrotechnical Laboratory, JAPAN.
3
   Licensed to the Free Software Foundation.
4
   Copyright (C) 2001 Free Software Foundation, Inc.
Kenichi Handa's avatar
Kenichi Handa committed
5 6 7
   Copyright (C) 2001, 2002
     National Institute of Advanced Industrial Science and Technology (AIST)
     Registration Number H13PRO009
Karl Heuer's avatar
Karl Heuer committed
8

Karl Heuer's avatar
Karl Heuer committed
9 10 11 12 13 14
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
15

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

Karl Heuer's avatar
Karl Heuer committed
21 22 23 24
You should have received a copy of the GNU General Public License
along with GNU Emacs; see the file COPYING.  If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.  */
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

Kenichi Handa's avatar
Kenichi Handa committed
49 50 51 52 53 54
  Coding system is an encoding mechanism of one or more character
  sets.  Here's a list of coding system types supported by Emacs.
  When we say "decode", it means converting a text encoded by some
  coding system into Emacs' internal format (emacs-utf-8), and when we
  say "encode", it means converting a text of emacs-utf-8 to some
  other coding system.
Karl Heuer's avatar
Karl Heuer committed
55

Kenichi Handa's avatar
Kenichi Handa committed
56 57 58
  Emacs represents a coding system by a Lisp symbol.  Each symbol is a
  key to the hash table Vcharset_hash_table.  This hash table
  associates the symbol to the corresponding detailed specifications.
Karl Heuer's avatar
Karl Heuer committed
59

Kenichi Handa's avatar
Kenichi Handa committed
60 61 62 63
  Before using a coding system for decoding and encoding, we setup a
  structure of type `struct coding_system'.  This structure keeps
  various information about a specific code conversion (e.g.  the
  location of source and destination data).
Karl Heuer's avatar
Karl Heuer committed
64

Kenichi Handa's avatar
Kenichi Handa committed
65 66 67
  Coding systems are classified into the following types by how to
  represent a character in a byte sequence.  Here's a brief descrition
  about type.
Karl Heuer's avatar
Karl Heuer committed
68

Kenichi Handa's avatar
Kenichi Handa committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
  o Emacs' internal format (emacs-utf-8)

  The extended UTF-8 which allows eight-bit raw bytes mixed with
  character codes.  Emacs holds characters in buffers and strings by
  this format.

  o UTF-8

  o UTF-16

  o Charset-base coding system

  A coding system defined by one or more (coded) character sets.
  Decoding and encoding are done by code converter defined for each
  character set.

  o Old Emacs' internal format (emacs-mule)

  The coding system adopted by an old versions of Emacs (20 and 21).
Karl Heuer's avatar
Karl Heuer committed
88

Kenichi Handa's avatar
Kenichi Handa committed
89
  o ISO2022-base coding system
90

Kenichi Handa's avatar
Kenichi Handa committed
91 92 93 94 95 96 97
  The most famous coding system for multiple character sets.  X's
  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.

  o SJIS (or Shift-JIS or MS-Kanji-Code)
   
Karl Heuer's avatar
Karl Heuer committed
98 99
  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
100
  section 8.
Karl Heuer's avatar
Karl Heuer committed
101

Kenichi Handa's avatar
Kenichi Handa committed
102
  o BIG5
Karl Heuer's avatar
Karl Heuer committed
103

Kenichi Handa's avatar
Kenichi Handa committed
104 105 106 107 108
  A coding system to encode character sets: ASCII and Big5.  Widely
  used by Chinese (mainly in Taiwan and Hong Kong).  Details are
  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
109

Kenichi Handa's avatar
Kenichi Handa committed
110
  o CCL
111

Kenichi Handa's avatar
Kenichi Handa committed
112 113 114 115
  If a user wants to decode/encode a text encoded in a coding system
  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.
116

Kenichi Handa's avatar
Kenichi Handa committed
117
  o Raw-text
Karl Heuer's avatar
Karl Heuer committed
118

Kenichi Handa's avatar
Kenichi Handa committed
119 120 121
  A coding system for a text containing raw eight-bit data.  Emacs
  treat each byte of source text as a character (except for
  end-of-line conversion).
Karl Heuer's avatar
Karl Heuer committed
122

Kenichi Handa's avatar
Kenichi Handa committed
123 124 125
  o No-conversion

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


Kenichi Handa's avatar
Kenichi Handa committed
128
END-OF-LINE FORMAT
Karl Heuer's avatar
Karl Heuer committed
129

Kenichi Handa's avatar
Kenichi Handa committed
130 131
  How end-of-line of a text is encoded depends on a system.  For
  instance, Unix's format is just one byte of LF (line-feed) code,
Richard M. Stallman's avatar
Richard M. Stallman committed
132
  whereas DOS's format is two-byte sequence of `carriage-return' and
133 134
  `line-feed' codes.  MacOS's format is usually one byte of
  `carriage-return'.
Karl Heuer's avatar
Karl Heuer committed
135

Kenichi Handa's avatar
Kenichi Handa committed
136 137 138
  Since text characters encoding and end-of-line encoding are
  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
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 148 149 150 151 152 153 154 155 156 157
  These functions check if a byte sequence specified as a source in
  CODING conforms to the format of XXX.  Return 1 if the data contains
  a byte sequence which can be decoded into non-ASCII characters by
  the coding system.  Otherwize (i.e. the data contains only ASCII
  characters or invalid sequence) return 0.

  It also resets some bits of an integer pointed by MASK.  The macros
  CATEGORY_MASK_XXX specifies each bit of this integer.

  Below is the template of these functions.  */

Karl Heuer's avatar
Karl Heuer committed
158
#if 0
Kenichi Handa's avatar
Kenichi Handa committed
159 160 161 162
static int
detect_coding_XXX (coding, mask)
     struct coding_system *coding;
     int *mask;
Karl Heuer's avatar
Karl Heuer committed
163
{
Kenichi Handa's avatar
Kenichi Handa committed
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
  unsigned char *src = coding->source;
  unsigned char *src_end = coding->source + coding->src_bytes;
  int multibytep = coding->src_multibyte;
  int c;
  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);
      /* Check if it conforms to XXX.  If not, break the loop.  */
    }
  /* As the data is invalid for XXX, reset a proper bits.  */
  *mask &= ~CODING_CATEGORY_XXX;
  return 0;
 no_more_source:
  /* The source exausted.  */
  if (!found)
    /* ASCII characters only. */
    return 0;
  /* Some data should be decoded into non-ASCII characters.  */
  *mask &= CODING_CATEGORY_XXX;
  return 1;
Karl Heuer's avatar
Karl Heuer committed
189 190 191 192 193
}
#endif

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

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

Kenichi Handa's avatar
Kenichi Handa committed
199 200 201 202
  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.
203

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

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

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

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

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

Kenichi Handa's avatar
Kenichi Handa committed
259 260 261
  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.
262

Kenichi Handa's avatar
Kenichi Handa committed
263
  Below is a template of these functions.  */
Karl Heuer's avatar
Karl Heuer committed
264
#if 0
265
static void
Kenichi Handa's avatar
Kenichi Handa committed
266
encode_coding_XXX (coding)
Karl Heuer's avatar
Karl Heuer committed
267 268
     struct coding_system *coding;
{
Kenichi Handa's avatar
Kenichi Handa committed
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
  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
286 287 288 289 290 291
}
#endif


/*** 1. Preamble ***/

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

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

Kenichi Handa's avatar
Kenichi Handa committed
304
Lisp_Object Vcoding_system_hash_table;
Karl Heuer's avatar
Karl Heuer committed
305

Kenichi Handa's avatar
Kenichi Handa committed
306 307
Lisp_Object Qcoding_system, Qcoding_aliases, Qeol_type;
Lisp_Object Qunix, Qdos, Qmac;
Karl Heuer's avatar
Karl Heuer committed
308 309
Lisp_Object Qbuffer_file_coding_system;
Lisp_Object Qpost_read_conversion, Qpre_write_conversion;
Kenichi Handa's avatar
Kenichi Handa committed
310
Lisp_Object Qdefault_char;
311
Lisp_Object Qno_conversion, Qundecided;
Kenichi Handa's avatar
Kenichi Handa committed
312 313 314
Lisp_Object Qcharset, Qiso_2022, Qutf_8, Qutf_16, Qshift_jis, Qbig5;
Lisp_Object Qutf_16_be_nosig, Qutf_16_be, Qutf_16_le_nosig, Qutf_16_le;
Lisp_Object Qsignature, Qendian, Qbig, Qlittle;
315
Lisp_Object Qcoding_system_history;
Kenichi Handa's avatar
Kenichi Handa committed
316
Lisp_Object Qvalid_codes;
Karl Heuer's avatar
Karl Heuer committed
317 318 319 320 321 322

extern Lisp_Object Qinsert_file_contents, Qwrite_region;
Lisp_Object Qcall_process, Qcall_process_region, Qprocess_argument;
Lisp_Object Qstart_process, Qopen_network_stream;
Lisp_Object Qtarget_idx;

323 324
Lisp_Object Vselect_safe_coding_system_function;

325 326 327
/* 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
328
   decided.  */
329
Lisp_Object eol_mnemonic_undecided;
Karl Heuer's avatar
Karl Heuer committed
330 331 332

#ifdef emacs

333 334 335
Lisp_Object Vcoding_system_list, Vcoding_system_alist;

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

337 338 339
/* Coding system emacs-mule and raw-text are for converting only
   end-of-line format.  */
Lisp_Object Qemacs_mule, Qraw_text;
340

Karl Heuer's avatar
Karl Heuer committed
341 342 343 344 345 346 347 348 349
/* 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;

350
/* A vector of length 256 which contains information about special
Karl Heuer's avatar
Karl Heuer committed
351
   Latin codes (especially for dealing with Microsoft codes).  */
352
Lisp_Object Vlatin_extra_code_table;
353

354 355 356
/* Flag to inhibit code conversion of end-of-line format.  */
int inhibit_eol_conversion;

357 358 359
/* Flag to inhibit ISO2022 escape sequence detection.  */
int inhibit_iso_escape_detection;

360 361 362
/* Flag to make buffer-file-coding-system inherit from process-coding.  */
int inherit_process_coding_system;

363
/* Coding system to be used to encode text for terminal display.  */
Karl Heuer's avatar
Karl Heuer committed
364 365
struct coding_system terminal_coding;

366 367 368 369 370
/* 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
371 372
struct coding_system keyboard_coding;

373 374 375
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
376

377 378
Lisp_Object Vlocale_coding_system;

Karl Heuer's avatar
Karl Heuer committed
379 380
#endif /* emacs */

381 382
/* Flag to tell if we look up translation table on character code
   conversion.  */
383
Lisp_Object Venable_character_translation;
384 385 386 387
/* 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;
388

389 390 391 392
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
393 394

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

397 398 399
/* Default coding systems used for process I/O.  */
Lisp_Object Vdefault_process_coding_system;

400 401 402 403 404 405
/* Global flag to tell that we can't call post-read-conversion and
   pre-write-conversion functions.  Usually the value is zero, but it
   is set to 1 temporarily while such functions are running.  This is
   to avoid infinite recursive call.  */
static int inhibit_pre_post_conversion;

406 407 408 409
/* Char-table containing safe coding systems of each character.  */
Lisp_Object Vchar_coding_system_table;
Lisp_Object Qchar_coding_system;

Kenichi Handa's avatar
Kenichi Handa committed
410 411 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 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
/* Two special coding systems.  */
Lisp_Object Vsjis_coding_system;
Lisp_Object Vbig5_coding_system;


static int detect_coding_utf_8 P_ ((struct coding_system *, int *));
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 *, int *));
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 *, int *));
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 *, int *));
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 *, int *));
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 *, int *));
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 *, int *));
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 *));


/* 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_carriage_return,	/* ISO_CODE_CR (0x0D) */
    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.  */
  };
509

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

Kenichi Handa's avatar
Kenichi Handa committed
513 514 515
/* 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
516

Kenichi Handa's avatar
Kenichi Handa committed
517 518 519
/* If set, reset graphic planes and registers at end-of-line to the
   initial state.  */
#define CODING_ISO_FLAG_RESET_AT_EOL	0x0002
520

Kenichi Handa's avatar
Kenichi Handa committed
521 522 523
/* If set, reset graphic planes and registers before any control
   characters to the initial state.  */
#define CODING_ISO_FLAG_RESET_AT_CNTL	0x0004
Karl Heuer's avatar
Karl Heuer committed
524

Kenichi Handa's avatar
Kenichi Handa committed
525 526
/* If set, encode by 7-bit environment.  */
#define CODING_ISO_FLAG_SEVEN_BITS	0x0008
527

Kenichi Handa's avatar
Kenichi Handa committed
528 529
/* If set, use locking-shift function.  */
#define CODING_ISO_FLAG_LOCKING_SHIFT	0x0010
530

Kenichi Handa's avatar
Kenichi Handa committed
531 532 533
/* If set, use single-shift function.  Overwrite
   CODING_ISO_FLAG_LOCKING_SHIFT.  */
#define CODING_ISO_FLAG_SINGLE_SHIFT	0x0020
534

Kenichi Handa's avatar
Kenichi Handa committed
535 536
/* If set, use designation escape sequence.  */
#define CODING_ISO_FLAG_DESIGNATION	0x0040
537

Kenichi Handa's avatar
Kenichi Handa committed
538 539
/* If set, produce revision number sequence.  */
#define CODING_ISO_FLAG_REVISION	0x0080
Richard M. Stallman's avatar
Richard M. Stallman committed
540

Kenichi Handa's avatar
Kenichi Handa committed
541 542
/* If set, produce ISO6429's direction specifying sequence.  */
#define CODING_ISO_FLAG_DIRECTION	0x0100
Karl Heuer's avatar
Karl Heuer committed
543

Kenichi Handa's avatar
Kenichi Handa committed
544 545 546
/* If set, assume designation states are reset at beginning of line on
   output.  */
#define CODING_ISO_FLAG_INIT_AT_BOL	0x0200
547

Kenichi Handa's avatar
Kenichi Handa committed
548 549 550
/* If set, designation sequence should be placed at beginning of line
   on output.  */
#define CODING_ISO_FLAG_DESIGNATE_AT_BOL 0x0400
551

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

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

Kenichi Handa's avatar
Kenichi Handa committed
559
#define CODING_ISO_FLAG_COMPOSITION	0x2000
560

Kenichi Handa's avatar
Kenichi Handa committed
561
#define CODING_ISO_FLAG_EUC_TW_SHIFT	0x4000
562

Kenichi Handa's avatar
Kenichi Handa committed
563
#define CODING_ISO_FLAG_FULL_SUPPORT	0x8000
564

Kenichi Handa's avatar
Kenichi Handa committed
565 566 567
/* 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  '?'
568 569


Kenichi Handa's avatar
Kenichi Handa committed
570 571 572
/* UTF-16 section */
#define CODING_UTF_16_BOM(coding)	\
  ((coding)->spec.utf_16.bom)
Karl Heuer's avatar
Karl Heuer committed
573

Kenichi Handa's avatar
Kenichi Handa committed
574 575
#define CODING_UTF_16_ENDIAN(coding)	\
  ((coding)->spec.utf_16.endian)
Karl Heuer's avatar
Karl Heuer committed
576

Kenichi Handa's avatar
Kenichi Handa committed
577 578
#define CODING_UTF_16_SURROGATE(coding)	\
  ((coding)->spec.utf_16.surrogate)
Karl Heuer's avatar
Karl Heuer committed
579 580


Kenichi Handa's avatar
Kenichi Handa committed
581 582 583 584 585 586 587 588
/* 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)					   \
  (XSTRING (AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_valids)) \
   ->data)
Karl Heuer's avatar
Karl Heuer committed
589

Kenichi Handa's avatar
Kenichi Handa committed
590
/* Index for each coding category in `coding_category_table' */
Karl Heuer's avatar
Karl Heuer committed
591

Kenichi Handa's avatar
Kenichi Handa committed
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 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 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 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 722 723 724 725
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)
#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)

/* 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];

static int detected_mask[coding_category_raw_text] =
  { CATEGORY_MASK_ISO,
    CATEGORY_MASK_ISO,
    CATEGORY_MASK_ISO,
    CATEGORY_MASK_ISO,
    CATEGORY_MASK_ISO,
    CATEGORY_MASK_ISO,
    CATEGORY_MASK_UTF_8,
    CATEGORY_MASK_UTF_16,
    CATEGORY_MASK_UTF_16,
    CATEGORY_MASK_UTF_16,
    CATEGORY_MASK_UTF_16,
    CATEGORY_MASK_UTF_16,
    CATEGORY_MASK_CHARSET,
    CATEGORY_MASK_SJIS,
    CATEGORY_MASK_BIG5,
    CATEGORY_MASK_CCL,
    CATEGORY_MASK_EMACS_MULE
  };

/*** 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
726

Kenichi Handa's avatar
Kenichi Handa committed
727 728 729 730 731 732 733 734
#define CODING_GET_INFO(coding, attrs, eol_type, charset_list)	\
  do {								\
    attrs = CODING_ID_ATTRS (coding->id);			\
    eol_type = CODING_ID_EOL_TYPE (coding->id);			\
    if (VECTORP (eol_type))					\
      eol_type = Qunix;						\
    charset_list = CODING_ATTR_CHARSET_LIST (attrs);		\
  } while (0)
Karl Heuer's avatar
Karl Heuer committed
735 736


Kenichi Handa's avatar
Kenichi Handa committed
737 738 739 740 741 742
/* 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
   in the source, it jumps to `no_more_source'.  The caller
   should declare and set these variables appropriately in advance:
	src, src_end, multibytep
*/
743

Kenichi Handa's avatar
Kenichi Handa committed
744
#define ONE_MORE_BYTE(c)					\
745
  do {								\
Kenichi Handa's avatar
Kenichi Handa committed
746 747 748 749 750 751 752 753 754 755 756 757 758 759
    if (src == src_end)						\
      {								\
	if (src_base < src)					\
	  coding->result = CODING_RESULT_INSUFFICIENT_SRC;	\
	goto no_more_source;					\
      }								\
    c = *src++;							\
    if (multibytep && (c & 0x80))				\
      {								\
	if ((c & 0xFE) != 0xC0)					\
	  error ("Undecodable char found");			\
	c = ((c & 1) << 6) | *src++;				\
      }								\
    consumed_chars++;						\
760 761 762
  } while (0)


Kenichi Handa's avatar
Kenichi Handa committed
763 764 765 766 767 768 769 770 771
#define ONE_MORE_BYTE_NO_CHECK(c)		\
  do {						\
    c = *src++;					\
    if (multibytep && (c & 0x80))		\
      {						\
	if ((c & 0xFE) != 0xC0)			\
	  error ("Undecodable char found");	\
	c = ((c & 1) << 6) | *src++;		\
      }						\
772 773 774
  } while (0)


Kenichi Handa's avatar
Kenichi Handa committed
775 776 777 778 779
/* 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.
*/
780 781


Kenichi Handa's avatar
Kenichi Handa committed
782 783 784 785 786
#define EMIT_ONE_ASCII_BYTE(c)	\
  do {				\
    produced_chars++;		\
    *dst++ = (c);		\
  } while (0)
787 788


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

Kenichi Handa's avatar
Kenichi Handa committed
791 792 793 794 795
#define EMIT_TWO_ASCII_BYTES(c1, c2)	\
  do {					\
    produced_chars += 2;		\
    *dst++ = (c1), *dst++ = (c2);	\
  } while (0)
796

Kenichi Handa's avatar
Kenichi Handa committed
797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815

/* 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);			\
816 817 818
  } while (0)


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

Kenichi Handa's avatar
Kenichi Handa committed
821 822 823 824 825 826 827 828 829 830 831 832 833
#define EMIT_TWO_BYTES(c1, c2)			\
  do {						\
    produced_chars += 2;			\
    if (multibytep)				\
      {						\
	CHAR_STRING_ADVANCE ((int) (c1), dst);	\
	CHAR_STRING_ADVANCE ((int) (c2), dst);	\
      }						\
    else					\
      {						\
	*dst++ = (c1);				\
	*dst++ = (c2);				\
      }						\
834 835 836
  } while (0)


Kenichi Handa's avatar
Kenichi Handa committed
837 838 839 840 841
#define EMIT_THREE_BYTES(c1, c2, c3)	\
  do {					\
    EMIT_ONE_BYTE (c1);			\
    EMIT_TWO_BYTES (c2, c3);		\
  } while (0)
842 843


Kenichi Handa's avatar
Kenichi Handa committed
844 845 846 847 848
#define EMIT_FOUR_BYTES(c1, c2, c3, c4)		\
  do {						\
    EMIT_TWO_BYTES (c1, c2);			\
    EMIT_TWO_BYTES (c3, c4);			\
  } while (0)
849 850


Kenichi Handa's avatar
Kenichi Handa committed
851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866
#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)						     \
      {									     \
	unsigned char *orig = coding->source;				     \
	EMACS_INT offset;						     \
									     \
	coding_set_source (coding);					     \
	offset = coding->source - orig;					     \
	src += offset;							     \
	src_base += offset;						     \
	src_end += offset;						     \
      }									     \
  } while (0)
867 868


Kenichi Handa's avatar
Kenichi Handa committed
869 870 871 872 873 874 875 876 877 878
#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)
879

Kenichi Handa's avatar
Kenichi Handa committed
880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895


static void
coding_set_source (coding)
     struct coding_system *coding;
{
  if (BUFFERP (coding->src_object))
    {
      if (coding->src_pos < 0)
	coding->source = GAP_END_ADDR + coding->src_pos_byte;
      else
	{
	  if (coding->src_pos < GPT
	      && coding->src_pos + coding->src_chars >= GPT)
	    move_gap_both (coding->src_pos, coding->src_pos_byte);
	  coding->source = BYTE_POS_ADDR (coding->src_pos_byte);
896 897
	}
    }
Kenichi Handa's avatar
Kenichi Handa committed
898
  else if (STRINGP (coding->src_object))
899
    {
Kenichi Handa's avatar
Kenichi Handa committed
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 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
      coding->source = (XSTRING (coding->src_object)->data
			+ coding->src_pos_byte);
    }
  else
    /* Otherwise, the source is C string and is never relocated
       automatically.  Thus we don't have to update anything.  */
    ;
}

static void
coding_set_destination (coding)
     struct coding_system *coding;
{
  if (BUFFERP (coding->dst_object))
    {
      /* 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);
      if (coding->src_pos < 0)
	/* The source and destination is in the same buffer.  */
	coding->dst_bytes = (GAP_END_ADDR
			     - (coding->src_bytes - coding->consumed)
			     - coding->destination);
      else
	coding->dst_bytes = (BUF_GAP_END_ADDR (XBUFFER (coding->dst_object))
			     - coding->destination);
    }
  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;
{
  Lisp_Object this_buffer;

  this_buffer = Fcurrent_buffer ();
  if (EQ (this_buffer, coding->dst_object))
    {
      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
    {
      set_buffer_internal (XBUFFER (coding->dst_object));
      make_gap (bytes);
      set_buffer_internal (XBUFFER (this_buffer));
    }
}
     

static unsigned char *
alloc_destination (coding, nbytes, dst)
     struct coding_system *coding;
     int nbytes;
     unsigned char *dst;
{
  EMACS_INT offset = dst - coding->destination;

  if (BUFFERP (coding->dst_object))
    coding_alloc_by_making_gap (coding, nbytes);
  else
    coding_alloc_by_realloc (coding, nbytes);
  coding->result = CODING_RESULT_SUCCESS;
  coding_set_destination (coding);
  dst = coding->destination + offset;
  return dst;
}
987

Kenichi Handa's avatar
Kenichi Handa committed
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 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047

/*** 2. Emacs' internal format (emacs-utf-8) ***/



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

/* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
   Check if a text is encoded in UTF-8.  If it is, return
   CATEGORY_MASK_UTF_8, else return 0.  */

#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
detect_coding_utf_8 (coding, mask)
     struct coding_system *coding;
     int *mask;
{
  unsigned char *src = coding->source, *src_base = src;
  unsigned char *src_end = coding->source + coding->src_bytes;
  int multibytep = coding->src_multibyte;
  int consumed_chars = 0;
  int found = 0;

  /* A coding system of this category is always ASCII compatible.  */
  src += coding->head_ascii;

  while (1)
    {
      int c, c1, c2, c3, c4;

      ONE_MORE_BYTE (c);
      if (UTF_8_1_OCTET_P (c))
	continue;
      ONE_MORE_BYTE (c1);
      if (! UTF_8_EXTRA_OCTET_P (c1))
	break;
      if (UTF_8_2_OCTET_LEADING_P (c))
	{
	  found++;
	  continue;
	}
      ONE_MORE_BYTE (c2);
      if (! UTF_8_EXTRA_OCTET_P (c2))
	break;
      if (UTF_8_3_OCTET_LEADING_P (c))
	{
	  found++;
	  continue;
	}
      ONE_MORE_BYTE (c3);
      if (! UTF_8_EXTRA_OCTET_P (c3))
	break;
      if (UTF_8_4_OCTET_LEADING_P (c))
1048
	{
Kenichi Handa's avatar
Kenichi Handa committed
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
	  found++;
	  continue;
	}
      ONE_MORE_BYTE (c4);
      if (! UTF_8_EXTRA_OCTET_P (c4))
	break;
      if (UTF_8_5_OCTET_LEADING_P (c))
	{
	  found++;
	  continue;
	}
      break;
    }
  *mask &= ~CATEGORY_MASK_UTF_8;
  return 0;

 no_more_source:
  if (! found)
    return 0;
  *mask &= CATEGORY_MASK_UTF_8;
  return 1;
}


static void
decode_coding_utf_8 (coding)
     struct coding_system *coding;
{
  unsigned char *src = coding->source + coding->consumed;
  unsigned char *src_end = coding->source + coding->src_bytes;
  unsigned char *src_base;
  int *charbuf = coding->charbuf;
  int *charbuf_end = charbuf + coding->charbuf_size;
  int consumed_chars = 0, consumed_chars_base;
  int multibytep = coding->src_multibyte;
  Lisp_Object attr, eol_type, charset_list;

  CODING_GET_INFO (coding, attr, eol_type, charset_list);

  while (1)
    {
      int c, c1, c2, c3, c4, c5;

      src_base = src;
      consumed_chars_base = consumed_chars;

      if (charbuf >= charbuf_end)
	break;

      ONE_MORE_BYTE (c1);
      if (UTF_8_1_OCTET_P(c1))
	{
	  c = c1;
	  if (c == '\r')
1103
	    {
Kenichi Handa's avatar
Kenichi Handa committed
1104 1105 1106 1107 1108 1109 1110 1111 1112
	      if (EQ (eol_type, Qdos))
		{
		  if (src == src_end)
		    goto no_more_source;
		  if (*src == '\n')
		    ONE_MORE_BYTE (c);
		}
	      else if (EQ (eol_type, Qmac))
		c = '\n';
1113 1114
	    }
	}
Kenichi Handa's avatar
Kenichi Handa committed
1115
      else
1116
	{
Kenichi Handa's avatar
Kenichi Handa committed
1117 1118 1119 1120 1121 1122
	  ONE_MORE_BYTE (c2);
	  if (! UTF_8_EXTRA_OCTET_P (c2))
	    goto invalid_code;
	  if (UTF_8_2_OCTET_LEADING_P (c1))
	    c = ((c1 & 0x1F) << 6) | (c2 & 0x3F);
	  else
1123
	    {
Kenichi Handa's avatar
Kenichi Handa committed
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
	      ONE_MORE_BYTE (c3);
	      if (! UTF_8_EXTRA_OCTET_P (c3))
		goto invalid_code;
	      if (UTF_8_3_OCTET_LEADING_P (c1))
		c = (((c1 & 0xF) << 12)
		     | ((c2 & 0x3F) << 6) | (c3 & 0x3F));
	      else
		{
		  ONE_MORE_BYTE (c4);
		  if (! UTF_8_EXTRA_OCTET_P (c4))
		    goto invalid_code;
		  if (UTF_8_4_OCTET_LEADING_P (c1))
		    c = (((c1 & 0x7) << 18) | ((c2 & 0x3F) << 12)
			 | ((c3 & 0x3F) << 6) | (c4 & 0x3F));
		  else
		    {
		      ONE_MORE_BYTE (c5);
		      if (! UTF_8_EXTRA_OCTET_P (c5))
			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));
			  if (c > MAX_CHAR)
			    goto invalid_code;
			}
		      else
			goto invalid_code;
		    }
		}
1155 1156
	    }
	}
Kenichi Handa's avatar
Kenichi Handa committed
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166

      *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++;
1167 1168
    }

Kenichi Handa's avatar
Kenichi Handa committed
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
 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;
  int produced_chars;
  int c;

  if (multibytep)
1189
    {
Kenichi Handa's avatar
Kenichi Handa committed
1190 1191 1192
      int safe_room = MAX_MULTIBYTE_LENGTH * 2;

      while (charbuf < charbuf_end)
1193
	{
Kenichi Handa's avatar
Kenichi Handa committed
1194 1195 1196 1197 1198 1199 1200
	  unsigned char str[MAX_MULTIBYTE_LENGTH], *p, *pend = str;
	  
	  ASSURE_DESTINATION (safe_room);
	  c = *charbuf++;
	  CHAR_STRING_ADVANCE (c, pend);
	  for (p = str; p < pend; p++)
	    EMIT_ONE_BYTE (*p);
1201 1202
	}
    }
Kenichi Handa's avatar
Kenichi Handa committed
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
  else
    {
      int safe_room = MAX_MULTIBYTE_LENGTH;

      while (charbuf < charbuf_end)
	{
	  ASSURE_DESTINATION (safe_room);
	  c = *charbuf++;
	  dst += CHAR_STRING (c, dst);
	  produced_chars++;
	}
    }
  coding->result = CODING_RESULT_SUCCESS;
  coding->produced_char += produced_chars;
  coding->produced = dst - coding->destination;
  return 0;
1219 1220
}

Karl Heuer's avatar
Karl Heuer committed
1221

Kenichi Handa's avatar
Kenichi Handa committed
1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
/* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
   Check if a text is encoded in UTF-16 Big Endian (endian == 1) or
   Little Endian (otherwise).  If it is, return
   CATEGORY_MASK_UTF_16_BE or CATEGORY_MASK_UTF_16_LE,
   else return 0.  */

#define UTF_16_HIGH_SURROGATE_P(val) \
  (((val) & 0xFC00) == 0xD800)

#define UTF_16_LOW_SURROGATE_P(val) \
  (((val) & 0xFC00) == 0xDC00)

#define UTF_16_INVALID_P(val)	\
  (((val) == 0xFFFE)		\
   || ((val) == 0xFFFF)		\
   || UTF_16_LOW_SURROGATE_P (val))


static int
detect_coding_utf_16 (coding, mask)
1242
     struct coding_system *coding;
Kenichi Handa's avatar
Kenichi Handa committed
1243
     int *mask;
1244
{
Kenichi Handa's avatar
Kenichi Handa committed
1245 1246 1247 1248 1249 1250 1251 1252
  unsigned char *src = coding->source, *src_base = src;
  unsigned char *src_end = coding->source + coding->src_bytes;
  int multibytep = coding->src_multibyte;
  int consumed_chars = 0;
  int c1, c2;

  ONE_MORE_BYTE (c1);
  ONE_MORE_BYTE (c2);
Karl Heuer's avatar
Karl Heuer committed
1253

Kenichi Handa's avatar
Kenichi Handa committed
1254
  if ((c1 == 0xFF) && (c2 == 0xFE))
1255
    {
Kenichi Handa's avatar
Kenichi Handa committed
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
      *mask &= CATEGORY_MASK_UTF_16_LE;
      return 1;
    }
  else if ((c1 == 0xFE) && (c2 == 0xFF))
    {
      *mask &= CATEGORY_MASK_UTF_16_BE;
      return 1;
    }
 no_more_source:
  return 0;
}
Kenichi Handa's avatar
Kenichi Handa committed
1267

Kenichi Handa's avatar
Kenichi Handa committed
1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288
static void
decode_coding_utf_16 (coding)
     struct coding_system *coding;
{
  unsigned char *src = coding->source + coding->consumed;
  unsigned char *src_end = coding->source + coding->src_bytes;
  unsigned char *src_base, *surrogate_high_base;
  int *charbuf = coding->charbuf;
  int *charbuf_end = charbuf + coding->charbuf_size;
  int consumed_chars = 0, consumed_chars_base;
  int multibytep = coding->src_multibyte;
  enum utf_16_bom_type bom = CODING_UTF_16_BOM (coding);
  enum utf_16_endian_type endian = CODING_UTF_16_ENDIAN (coding);
  int surrogate = CODING_UTF_16_SURROGATE (coding);
  Lisp_Object attr, eol_type, charset_list;

  CODING_GET_INFO (coding, attr, eol_type, charset_list);

  if (bom != utf_16_without_bom)
    {
      int c, c1, c2;
1289

Kenichi Handa's avatar
Kenichi Handa committed
1290 1291 1292 1293 1294 1295 1296 1297
      src_base = src;
      ONE_MORE_BYTE (c1);
      ONE_MORE_BYTE (c2);
      c = (c1 << 16) | c2;
      if (bom == utf_16_with_bom)
	{
	  if (endian == utf_16_big_endian
	      ? c != 0xFFFE : c != 0xFEFF)
1298
	    {
Kenichi Handa's avatar
Kenichi Handa committed
1299 1300 1301 1302
	      /* We are sure that there's enouph room at CHARBUF.  */
	      *charbuf++ = c1;
	      *charbuf++ = c2;
	      coding->errors++;
1303 1304
	    }
	}
Kenichi Handa's avatar
Kenichi Handa committed
1305
      else
1306
	{
Kenichi Handa's avatar
Kenichi Handa committed
1307 1308 1309 1310 1311 1312 1313
	  if (c == 0xFFFE)
	    CODING_UTF_16_ENDIAN (coding)
	      = endian = utf_16_big_endian;
	  else if (c == 0xFEFF)
	    CODING_UTF_16_ENDIAN (coding)
	      = endian = utf_16_little_endian;
	  else
1314
	    {
Kenichi Handa's avatar
Kenichi Handa committed
1315 1316 1317
	      CODING_UTF_16_ENDIAN (coding)
		= endian = utf_16_big_endian;
	      src = src_base;
1318 1319
	    }
	}
Kenichi Handa's avatar
Kenichi Handa committed
1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337
      CODING_UTF_16_BOM (coding) = utf_16_with_bom;
    }

  while (1)
    {
      int c, c1, c2;

      src_base = src;
      consumed_chars_base = consumed_chars;

      if (charbuf + 2 >= charbuf_end)
	break;

      ONE_MORE_BYTE (c1);
      ONE_MORE_BYTE (c2);
      c = (endian == utf_16_big_endian
	   ? ((c1 << 16) | c2) : ((c2 << 16) | c1));
      if (surrogate)
1338
	{
Kenichi Handa's avatar
Kenichi Handa committed
1339
	  if (! UTF_16_LOW_SURROGATE_P (c))
1340
	    {
Kenichi Handa's avatar
Kenichi Handa committed
1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351
	      if (endian == utf_16_big_endian)
		c1 = surrogate >> 8, c2 = surrogate & 0xFF;
	      else
		c1 = surrogate & 0xFF, c2 = surrogate >> 8;
	      *charbuf++ = c1;
	      *charbuf++ = c2;
	      coding->errors++;
	      if (UTF_16_HIGH_SURROGATE_P (c))
		CODING_UTF_16_SURROGATE (coding) = surrogate = c;
	      else
		*charbuf++ = c;
1352
	    }
Kenichi Handa's avatar
Kenichi Handa committed
1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545
	  else
	    {
	      c = ((surrogate - 0xD800) << 10) | (c - 0xDC00);
	      CODING_UTF_16_SURROGATE (coding) = surrogate = 0;
	      *charbuf++ = c;
	    }
	}
      else
	{
	  if (UTF_16_HIGH_SURROGATE_P (c))
	    CODING_UTF_16_SURROGATE (coding) = surrogate = c;
	  else
	    *charbuf++ = c;
	}	  
    }

 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_16 (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;
  int safe_room = 8;
  enum utf_16_bom_type bom = CODING_UTF_16_BOM (coding);
  int big_endian = CODING_UTF_16_ENDIAN (coding) == utf_16_big_endian;
  int produced_chars = 0;
  Lisp_Object attrs, eol_type, charset_list;
  int c;

  CODING_GET_INFO (coding, attrs, eol_type, charset_list);

  if (bom == utf_16_with_bom)
    {
      ASSURE_DESTINATION (safe_room);
      if (big_endian)
	EMIT_TWO_BYTES (0xFF, 0xFE);
      else
	EMIT_TWO_BYTES (0xFE, 0xFF);
      CODING_UTF_16_BOM (coding) = utf_16_without_bom;
    }

  while (charbuf < charbuf_end)
    {
      ASSURE_DESTINATION (safe_room);
      c = *charbuf++;
      if (c >= 0x110000)
	c = 0xFFFF;

      if (c < 0x10000)
	{
	  if (big_endian)
	    EMIT_TWO_BYTES (c >> 8, c & 0xFF);
	  else
	    EMIT_TWO_BYTES (c & 0xFF, c >> 8);
	}
      else
	{
	  int c1, c2;

	  c -= 0x10000;
	  c1 = (c >> 10) + 0xD800;
	  c2 = (c & 0x3FF) + 0xDC00;
	  if (big_endian)
	    EMIT_FOUR_BYTES (c1 >> 8, c1 & 0xFF, c2 >> 8, c2 & 0xFF);
	  else
	    EMIT_FOUR_BYTES (c1 & 0xFF, c1 >> 8, c2 & 0xFF, c2 >> 8);
	}
    }
  coding->result = CODING_RESULT_SUCCESS;
  coding->produced = dst - coding->destination;
  coding->produced_char += produced_chars;
  return 0;
}


/*** 6. Old Emacs' internal format (emacs-mule) ***/

/* Emacs' internal format for representation of multiple character
   sets is a kind of multi-byte encoding, i.e. characters are
   represented by variable-length sequences of one-byte codes.

   ASCII characters and control characters (e.g. `tab', `newline') are
   represented by one-byte sequences which are their ASCII codes, in
   the range 0x00 through 0x7F.

   8-bit characters of the range 0x80..0x9F are represented by
   two-byte sequences of LEADING_CODE_8_BIT_CONTROL and (their 8-bit
   code + 0x20).

   8-bit characters of the range 0xA0..0xFF are represented by
   one-byte sequences which are their 8-bit code.

   The other characters are represented by a sequence of `base
   leading-code', optional `extended leading-code', and one or two
   `position-code's.  The length of the sequence is determined by the
   base leading-code.  Leading-code takes the range 0x81 through 0x9D,
   whereas extended leading-code and position-code take the range 0xA0
   through 0xFF.  See `charset.h' for more details about leading-code
   and position-code.

   --- CODE RANGE of Emacs' internal format ---
   character set	range
   -------------	-----
   ascii		0x00..0x7F
   eight-bit-control	LEADING_CODE_8_BIT_CONTROL + 0xA0..0xBF
   eight-bit-graphic	0xA0..0xBF
   ELSE			0x81..0x9D + [0xA0..0xFF]+
   ---------------------------------------------

   As this is the internal character representation, the format is
   usually not used externally (i.e. in a file or in a data sent to a
   process).  But, it is possible to have a text externally in this
   format (i.e. by encoding by the coding system `emacs-mule').

   In that case, a sequence of one-byte codes has a slightly different
   form.

   At first, all characters in eight-bit-control are represented by
   one-byte sequences which are their 8-bit code.

   Next, character composition data are represented by the byte
   sequence of the form: 0x80 METHOD BYTES CHARS COMPONENT ...,
   where,
	METHOD is 0xF0 plus one of composition method (enum
	composition_method),

	BYTES is 0xA0 plus a byte length of this composition data,

	CHARS is 0x20 plus a number of characters composed by this
	data,

	COMPONENTs are characters of multibye form or composition
	rules encoded by two-byte of ASCII codes.

   In addition, for backward compatibility, the following formats are
   also recognized as composition data on decoding.

   0x80 MSEQ ...
   0x80 0xFF MSEQ RULE MSEQ RULE ... MSEQ

   Here,
	MSEQ is a multibyte form but in these special format:
	  ASCII: 0xA0 ASCII_CODE+0x80,
	  other: LEADING_CODE+0x20 FOLLOWING-BYTE ...,
	RULE is a one byte code of the range 0xA0..0xF0 that
	represents a composition rule.
  */

char emacs_mule_bytes[256];

/* Leading-code followed by extended leading-code.  */
#define LEADING_CODE_PRIVATE_11	0x9A /* for private DIMENSION1 of 1-column */
#define LEADING_CODE_PRIVATE_12	0x9B /* for private DIMENSION1 of 2-column */
#define LEADING_CODE_PRIVATE_21	0x9C /* for private DIMENSION2 of 1-column */
#define LEADING_CODE_PRIVATE_22	0x9D /* for private DIMENSION2 of 2-column */


int
emacs_mule_char (coding, composition, nbytes, nchars)
     struct coding_system *coding;
     int composition;
     int *nbytes, *nchars;
{
  unsigned char *src = coding->source + coding->consumed;
  unsigned char *src_end = coding->source + coding->src_bytes;
  int multibytep = coding->src_multibyte;
  unsigned char *src_base = src;
  struct charset *charset;
  unsigned code;
  int c;
  int consumed_chars = 0;

  ONE_MORE_BYTE (c);
  if (composition)
    {
      c -= 0x20;
      if (c == 0x80)
	{
	  ONE_MORE_BYTE (c);
	  if (c < 0xA0)
	    goto invalid_code;
	  *nbytes = src - src_base;
	  *nchars = consumed_chars;
	  return (c - 0x80);
1546
	}
Kenichi Handa's avatar
Kenichi Handa committed
1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560
    }

  switch (emacs_mule_bytes[c])
    {
    case 2:
      if (! (charset = emacs_mule_charset[c]))
	goto invalid_code;
      ONE_MORE_BYTE (c);
      code = c & 0x7F;
      break;

    case 3:
      if (c == LEADING_CODE_PRIVATE_11
	  || c == LEADING_CODE_PRIVATE_12)
1561
	{
Kenichi Handa's avatar
Kenichi Handa committed
1562 1563 1564 1565 1566
	  ONE_MORE_BYTE (c);
	  if (! (charset = emacs_mule_charset[c]))
	    goto invalid_code;
	  ONE_MORE_BYTE (c);
	  code = c & 0x7F;
1567 1568 1569
	}
      else
	{
Kenichi Handa's avatar
Kenichi Handa committed
1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655
	  if (! (charset = emacs_mule_charset[c]))
	    goto invalid_code;
	  ONE_MORE_BYTE (c);
	  code = (c & 0x7F) << 7;
	  ONE_MORE_BYTE (c);
	  code |= c & 0x7F;
	}
      break;

    case 4:
      if (! (charset = emacs_mule_charset[c]))
	goto invalid_code;
      ONE_MORE_BYTE (c);
      code = (c & 0x7F) << 7;
      ONE_MORE_BYTE (c);
      code |= c & 0x7F;
      break;

    case 1:
      code = c;
      charset = CHARSET_FROM_ID (ASCII_BYTE_P (code) ? charset_ascii
				 : code < 0xA0 ? charset_8_bit_control
				 : charset_8_bit_graphic);
      break;

    default:
      abort ();
    }
  c = DECODE_CHAR (charset, code);
  if (c < 0)
    goto invalid_code;
  *nbytes = src - src_base;
  *nchars = consumed_chars;
  return c;

 no_more_source:
  return -2;

 invalid_code:
  return -1;
}


/* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
   Check if a text is encoded in `emacs-mule'.  */

static int
detect_coding_emacs_mule (coding, mask)
     struct coding_system *coding;
     int *mask;
{
  unsigned char *src = coding->source, *src_base = src;
  unsigned char *src_end = coding->source + coding->src_bytes;
  int multibytep = coding->src_multibyte;
  int consumed_chars = 0;
  int c;
  int found = 0;

  /* A coding system of this category is always ASCII compatible.  */
  src += coding->head_ascii;

  while (1)
    {
      ONE_MORE_BYTE (c);

      if (c == 0x80)
	{
	  /* Perhaps the start of composite character.  We simple skip
	     it because analyzing it is too heavy for detecting.  But,
	     at least, we check that the composite character
	     constitues of more than 4 bytes.  */
	  unsigned char *src_base;

	repeat:
	  src_base = src;
	  do
	    {
	      ONE_MORE_BYTE (c);
	    }
	  while (c >= 0xA0);

	  if (src - src_base <= 4)
	    break;
	  found = 1;
	  if (c == 0x80)
	    goto repeat;
1656
	}
Kenichi Handa's avatar
Kenichi Handa committed
1657 1658

      if (c < 0x80)
1659
	{
Kenichi Handa's avatar
Kenichi Handa committed
1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675
	  if (c < 0x20
	      && (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO))
	    break;
	}
      else
	{
	  unsigned char *src_base = src - 1;

	  do
	    {
	      ONE_MORE_BYTE (c);
	    }
	  while (c >= 0xA0);
	  if (src - src_base != emacs_mule_bytes[*src_base])
	    break;
	  found = 1;
Karl Heuer's avatar
Karl Heuer committed
1676 1677
	}
    }
Kenichi Handa's avatar
Kenichi Handa committed
1678 1679 1680 1681 1682 1683 1684 1685
  *mask &= ~CATEGORY_MASK_EMACS_MULE;
  return 0;

 no_more_source:
  if (!found)
    return 0;
  *mask &= CATEGORY_MASK_EMACS_MULE;
  return 1;
Karl Heuer's avatar
Karl Heuer committed
1686 1687
}

1688

Kenichi Handa's avatar
Kenichi Handa committed
1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747
/* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions".  */

/* Decode a character represented as a component of composition
   sequence of Emacs 20/21 style at SRC.  Set C to that character and
   update SRC to the head of next character (or an encoded composition
   rule).  If SRC doesn't points a composition component, set C to -1.
   If SRC points an invalid byte sequence, global exit by a return
   value 0.  */

#define DECODE_EMACS_MULE_COMPOSITION_CHAR(buf)			\
  if (1)							\
    {								\
      int c;							\
      int nbytes, nchars;					\
								\
      if (src == src_end)					\
	break;							\
      c = emacs_mule_char (coding, 1, &nbytes, &nchars);	\
      if (c < 0)						\
	{							\
	  if (c == -2)						\
	    break;						\
	  goto invalid_code;					\
	}							\
      *buf++ = c;						\
      src += nbytes;						\
      consumed_chars += nchars;					\
    }								\
  else


/* Decode a composition rule represented as a component of composition
   sequence of Emacs 20 style at SRC.  Set C to the rule.  If SRC
   points an invalid byte sequence, set C to -1.  */

#define DECODE_EMACS_MULE_COMPOSITION_RULE(buf)		\
  do {							\
    int c, gref, nref;					\
							\
    if (src < src_end)					\
      goto invalid_code;				\
    ONE_MORE_BYTE_NO_CHECK (c);				\
    c -= 0xA0;						\
    if (c < 0 || c >= 81)				\
      goto invalid_code;				\
							\
    gref = c / 9, nref = c % 9;				\
    *buf++ = COMPOSITION_ENCODE_RULE (gref, nref);	\
  } while (0)


#define ADD_COMPOSITION_DATA(buf, method, nchars)	\
  do {							\
    *buf++ = -5;					\
    *buf++ = coding->produced_char + char_offset;	\
    *buf++ = CODING_ANNOTATE_COMPOSITION_MASK;		\
    *buf++ = method;					\
    *buf++ = nchars;					\
  } while (0)
1748

Kenichi Handa's avatar
Kenichi Handa committed
1749 1750

#define DECODE_EMACS_MULE_21_COMPOSITION(c)				\
1751
  do {									\
Kenichi Handa's avatar
Kenichi Handa committed
1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768
    /* Emacs 21 style format.  The first three bytes at SRC are		\
       (METHOD - 0xF0), (BYTES - 0xA0), (CHARS - 0xA0), where BYTES is	\
       the byte length of this composition information, CHARS is the	\
       number of characters composed by this composition.  */		\
    enum composition_method method = c - 0xF0;				\
    int consumed_chars_limit;						\
    int nbytes, nchars;							\
									\
    ONE_MORE_BYTE (c);							\
    nbytes = c - 0xA0;							\
    if (nbytes < 3)							\
      goto invalid_code;						\
    ONE_MORE_BYTE (c);							\
    nchars = c - 0xA0;							\
    ADD_COMPOSITION_DATA (charbuf, method, nchars);			\
    consumed_chars_limit = consumed_chars_base + nbytes;		\
    if (method != COMPOSITION_RELATIVE)					\
1769
      {									\
Kenichi Handa's avatar
Kenichi Handa committed
1770 1771
	int i = 0;							\
	while (consumed_chars < consumed_chars_limit)			\
1772
	  {								\
Kenichi Handa's avatar
Kenichi Handa committed
1773 1774 1775 1776
	    if (i % 2 && method != COMPOSITION_WITH_ALTCHARS)		\
	      DECODE_EMACS_MULE_COMPOSITION_RULE (charbuf);		\
	    else							\
	      DECODE_EMACS_MULE_COMPOSITION_CHAR (charbuf);		\
1777
	  }								\
Kenichi Handa's avatar
Kenichi Handa committed
1778 1779
	if (consumed_chars < consumed_chars_limit)			\
	  goto invalid_code;						\
1780 1781
      }									\
  } while (0)
1782