(GT-K02180): New character.
[chise/xemacs-chise.git-] / src / text-coding.c
index 187d2a9..3808dde 100644 (file)
@@ -94,7 +94,7 @@ Lisp_Object Qpost_read_conversion;
 Lisp_Object Qpre_write_conversion;
 
 #ifdef MULE
-Lisp_Object Qucs4, Qutf8;
+Lisp_Object Qucs4, Qutf16, Qutf8;
 Lisp_Object Qbig5, Qshift_jis;
 Lisp_Object Qcharset_g0, Qcharset_g1, Qcharset_g2, Qcharset_g3;
 Lisp_Object Qforce_g0_on_output, Qforce_g1_on_output;
@@ -105,6 +105,7 @@ Lisp_Object Qescape_quoted;
 Lisp_Object Qshort, Qno_ascii_eol, Qno_ascii_cntl, Qseven, Qlock_shift;
 #endif
 #ifdef UTF2000
+Lisp_Object Qutf_8_mcs;
 Lisp_Object Qdisable_composition;
 Lisp_Object Quse_entity_reference;
 Lisp_Object Qd, Qx, QX;
@@ -187,39 +188,48 @@ struct detection_state;
 
 static void
 text_encode_generic (Lstream *encoding, const Bufbyte *src,
-                    unsigned_char_dynarr *dst, size_t n);
+                    unsigned_char_dynarr *dst, Lstream_data_count n);
 
 static int detect_coding_sjis (struct detection_state *st,
-                              const Extbyte *src, size_t n);
+                              const Extbyte *src, Lstream_data_count n);
 static void decode_coding_sjis (Lstream *decoding, const Extbyte *src,
-                               unsigned_char_dynarr *dst, size_t n);
+                               unsigned_char_dynarr *dst, Lstream_data_count n);
 void char_encode_shift_jis (struct encoding_stream *str, Emchar c,
                            unsigned_char_dynarr *dst, unsigned int *flags);
 void char_finish_shift_jis (struct encoding_stream *str,
                            unsigned_char_dynarr *dst, unsigned int *flags);
 
 static int detect_coding_big5 (struct detection_state *st,
-                              const Extbyte *src, size_t n);
+                              const Extbyte *src, Lstream_data_count n);
 static void decode_coding_big5 (Lstream *decoding, const Extbyte *src,
-                               unsigned_char_dynarr *dst, size_t n);
+                               unsigned_char_dynarr *dst, Lstream_data_count n);
 void char_encode_big5 (struct encoding_stream *str, Emchar c,
                       unsigned_char_dynarr *dst, unsigned int *flags);
 void char_finish_big5 (struct encoding_stream *str,
                       unsigned_char_dynarr *dst, unsigned int *flags);
 
 static int detect_coding_ucs4 (struct detection_state *st,
-                              const Extbyte *src, size_t n);
+                              const Extbyte *src, Lstream_data_count n);
 static void decode_coding_ucs4 (Lstream *decoding, const Extbyte *src,
-                               unsigned_char_dynarr *dst, size_t n);
+                               unsigned_char_dynarr *dst, Lstream_data_count n);
 void char_encode_ucs4 (struct encoding_stream *str, Emchar c,
                       unsigned_char_dynarr *dst, unsigned int *flags);
 void char_finish_ucs4 (struct encoding_stream *str,
                       unsigned_char_dynarr *dst, unsigned int *flags);
 
+static int detect_coding_utf16 (struct detection_state *st,
+                              const Extbyte *src, Lstream_data_count n);
+static void decode_coding_utf16 (Lstream *decoding, const Extbyte *src,
+                               unsigned_char_dynarr *dst, Lstream_data_count n);
+void char_encode_utf16 (struct encoding_stream *str, Emchar c,
+                      unsigned_char_dynarr *dst, unsigned int *flags);
+void char_finish_utf16 (struct encoding_stream *str,
+                      unsigned_char_dynarr *dst, unsigned int *flags);
+
 static int detect_coding_utf8 (struct detection_state *st,
-                              const Extbyte *src, size_t n);
+                              const Extbyte *src, Lstream_data_count n);
 static void decode_coding_utf8 (Lstream *decoding, const Extbyte *src,
-                               unsigned_char_dynarr *dst, size_t n);
+                               unsigned_char_dynarr *dst, Lstream_data_count n);
 void char_encode_utf8 (struct encoding_stream *str, Emchar c,
                       unsigned_char_dynarr *dst, unsigned int *flags);
 void char_finish_utf8 (struct encoding_stream *str,
@@ -229,22 +239,22 @@ static int postprocess_iso2022_mask (int mask);
 static void reset_iso2022 (Lisp_Object coding_system,
                           struct iso2022_decoder *iso);
 static int detect_coding_iso2022 (struct detection_state *st,
-                                 const Extbyte *src, size_t n);
+                                 const Extbyte *src, Lstream_data_count n);
 static void decode_coding_iso2022 (Lstream *decoding, const Extbyte *src,
-                                  unsigned_char_dynarr *dst, size_t n);
+                                  unsigned_char_dynarr *dst, Lstream_data_count n);
 void char_encode_iso2022 (struct encoding_stream *str, Emchar c,
                          unsigned_char_dynarr *dst, unsigned int *flags);
 void char_finish_iso2022 (struct encoding_stream *str,
                          unsigned_char_dynarr *dst, unsigned int *flags);
 #endif /* MULE */
 static void decode_coding_no_conversion (Lstream *decoding, const Extbyte *src,
-                                        unsigned_char_dynarr *dst, size_t n);
+                                        unsigned_char_dynarr *dst, Lstream_data_count n);
 static void encode_coding_no_conversion (Lstream *encoding, const Bufbyte *src,
-                                        unsigned_char_dynarr *dst, size_t n);
+                                        unsigned_char_dynarr *dst, Lstream_data_count n);
 static void mule_decode (Lstream *decoding, const Extbyte *src,
-                        unsigned_char_dynarr *dst, size_t n);
+                        unsigned_char_dynarr *dst, Lstream_data_count n);
 static void mule_encode (Lstream *encoding, const Bufbyte *src,
-                        unsigned_char_dynarr *dst, size_t n);
+                        unsigned_char_dynarr *dst, Lstream_data_count n);
 
 typedef struct codesys_prop codesys_prop;
 struct codesys_prop
@@ -666,7 +676,18 @@ allocate_coding_system (enum coding_system_type type, Lisp_Object name)
        CODING_SYSTEM_ISO2022_INITIAL_CHARSET (codesys, i) = Qnil;
     }
 #ifdef UTF2000
-  if (type == CODESYS_BIG5)
+  if (type == CODESYS_UTF8)
+    {
+      CODING_SYSTEM_ISO2022_INITIAL_CHARSET (codesys, 0)
+       = Vcharset_ucs;
+      CODING_SYSTEM_ISO2022_INITIAL_CHARSET (codesys, 1)
+       = Qnil;
+      CODING_SYSTEM_ISO2022_INITIAL_CHARSET (codesys, 2)
+       = Qnil;
+      CODING_SYSTEM_ISO2022_INITIAL_CHARSET (codesys, 3)
+       = Qnil;
+    }
+  else if (type == CODESYS_BIG5)
     {
       CODING_SYSTEM_ISO2022_INITIAL_CHARSET (codesys, 0)
        = Vcharset_ascii;
@@ -937,6 +958,7 @@ if TYPE is 'ccl:
   else if (EQ (type, Qiso2022))       { ty = CODESYS_ISO2022; }
   else if (EQ (type, Qbig5))          { ty = CODESYS_BIG5; }
   else if (EQ (type, Qucs4))          { ty = CODESYS_UCS4; }
+  else if (EQ (type, Qutf16))         { ty = CODESYS_UTF16; }
   else if (EQ (type, Qutf8))          { ty = CODESYS_UTF8; }
   else if (EQ (type, Qccl))           { ty = CODESYS_CCL; }
 #endif
@@ -1034,6 +1056,16 @@ if TYPE is 'ccl:
              signal_simple_error ("Unrecognized property", key);
          }
 #ifdef UTF2000
+       else if (ty == CODESYS_UTF8)
+         {
+           if      (EQ (key, Qcharset_g0)) FROB_INITIAL_CHARSET (0);
+           else if (EQ (key, Qcharset_g1))
+             CODING_SYSTEM_ISO2022_INITIAL_CHARSET (codesys, 1) = value;
+           else if (EQ (key, Qcharset_g2))
+             CODING_SYSTEM_ISO2022_INITIAL_CHARSET (codesys, 2) = value;
+           else
+             signal_simple_error ("Unrecognized property", key);
+         }
        else if (ty == CODESYS_BIG5)
          {
            if      (EQ (key, Qcharset_g0)) FROB_INITIAL_CHARSET (0);
@@ -1349,6 +1381,7 @@ Return the type of CODING-SYSTEM.
     case CODESYS_ISO2022:      return Qiso2022;
     case CODESYS_BIG5:         return Qbig5;
     case CODESYS_UCS4:         return Qucs4;
+    case CODESYS_UTF16:                return Qutf16;
     case CODESYS_UTF8:         return Qutf8;
     case CODESYS_CCL:          return Qccl;
 #endif
@@ -1667,6 +1700,13 @@ struct detection_state
       int mask;
       int in_byte;
     }
+  utf16;
+
+  struct
+    {
+      int mask;
+      int in_byte;
+    }
   utf8;
 
   struct
@@ -1720,7 +1760,7 @@ mask_has_at_most_one_bit_p (int mask)
 
 static eol_type_t
 detect_eol_type (struct detection_state *st, const Extbyte *src,
-                size_t n)
+                Lstream_data_count n)
 {
   while (n--)
     {
@@ -1763,7 +1803,7 @@ detect_eol_type (struct detection_state *st, const Extbyte *src,
 
 static int
 detect_coding_type (struct detection_state *st, const Extbyte *src,
-                   size_t n, int just_do_eol)
+                   Lstream_data_count n, int just_do_eol)
 {
   if (st->eol_type == EOL_AUTODETECT)
     st->eol_type = detect_eol_type (st, src, n);
@@ -1783,6 +1823,7 @@ detect_coding_type (struct detection_state *st, const Extbyte *src,
              st->shift_jis.mask = ~0;
              st->big5.mask = ~0;
              st->ucs4.mask = ~0;
+             st->utf16.mask = ~0;
              st->utf8.mask = ~0;
              st->iso2022.mask = ~0;
 #endif
@@ -1802,6 +1843,8 @@ detect_coding_type (struct detection_state *st, const Extbyte *src,
     st->big5.mask = detect_coding_big5 (st, src, n);
   if (!mask_has_at_most_one_bit_p (st->utf8.mask))
     st->utf8.mask = detect_coding_utf8 (st, src, n);
+  if (!mask_has_at_most_one_bit_p (st->utf16.mask))
+    st->utf16.mask = detect_coding_utf16 (st, src, n);
   if (!mask_has_at_most_one_bit_p (st->ucs4.mask))
     st->ucs4.mask = detect_coding_ucs4 (st, src, n);
 
@@ -1873,6 +1916,8 @@ coding_system_from_mask (int mask)
    that should be unnecessary with the explicit eol-type argument. */
 
 #define LENGTH(string_constant) (sizeof (string_constant) - 1)
+/* number of leading lines to check for a coding cookie */
+#define LINES_TO_CHECK 2
 
 void
 determine_real_coding_system (Lstream *stream, Lisp_Object *codesys_in_out,
@@ -1894,15 +1939,15 @@ determine_real_coding_system (Lstream *stream, Lisp_Object *codesys_in_out,
       Extbyte buf[4096];
       Lisp_Object coding_system = Qnil;
       Extbyte *p;
-      ssize_t nread = Lstream_read (stream, buf, sizeof (buf));
+      Lstream_data_count nread = Lstream_read (stream, buf, sizeof (buf));
       Extbyte *scan_end;
+      int lines_checked = 0;
 
       /* Look for initial "-*-"; mode line prefix */
       for (p = buf,
             scan_end = buf + nread - LENGTH ("-*-coding:?-*-");
           p <= scan_end
-            && *p != '\n'
-            && *p != '\r';
+            && lines_checked < LINES_TO_CHECK;
           p++)
        if (*p == '-' && *(p+1) == '*' && *(p+2) == '-')
          {
@@ -1911,8 +1956,7 @@ determine_real_coding_system (Lstream *stream, Lisp_Object *codesys_in_out,
            for (p = local_vars_beg,
                   scan_end = buf + nread - LENGTH ("-*-");
                 p <= scan_end
-                  && *p != '\n'
-                  && *p != '\r';
+                  && lines_checked < LINES_TO_CHECK;
                 p++)
              if (*p == '-' && *(p+1) == '*' && *(p+2) == '-')
                {
@@ -1954,8 +1998,24 @@ determine_real_coding_system (Lstream *stream, Lisp_Object *codesys_in_out,
                      }
                  break;
                }
+             /* #### file must use standard EOLs or we miss 2d line */
+             /* #### not to mention this is broken for UTF-16 DOS files */
+             else if (*p == '\n' || *p == '\r')
+               {
+                 lines_checked++;
+                 /* skip past multibyte (DOS) newline */
+                 if (*p == '\r' && *(p+1) == '\n') p++;
+               }
            break;
          }
+       /* #### file must use standard EOLs or we miss 2d line */
+       /* #### not to mention this is broken for UTF-16 DOS files */
+       else if (*p == '\n' || *p == '\r')
+         {
+           lines_checked++;
+           /* skip past multibyte (DOS) newline */
+           if (*p == '\r' && *(p+1) == '\n') p++;
+         }
 
       if (NILP (coding_system))
        do
@@ -2028,7 +2088,7 @@ type.  Optional arg BUFFER defaults to the current buffer.
   while (1)
     {
       Extbyte random_buffer[4096];
-      ssize_t nread = Lstream_read (istr, random_buffer, sizeof (random_buffer));
+      Lstream_data_count nread = Lstream_read (istr, random_buffer, sizeof (random_buffer));
 
       if (!nread)
        break;
@@ -2259,203 +2319,10 @@ struct decoding_stream
   struct detection_state decst;
 };
 
-#ifdef UTF2000
-extern Lisp_Object Qcomposition;
-
-INLINE_HEADER void
-decode_flush_er_chars (struct decoding_stream *str, unsigned_char_dynarr* dst);
-INLINE_HEADER void
-decode_flush_er_chars (struct decoding_stream *str, unsigned_char_dynarr* dst)
-{
-  if ( str->er_counter > 0)
-    {
-      Dynarr_add_many (dst, str->er_buf, str->er_counter);
-      str->er_counter = 0;
-    }
-}
-
-void decode_add_er_char (struct decoding_stream *str, Emchar character,
-                        unsigned_char_dynarr* dst);
-void
-decode_add_er_char (struct decoding_stream *str, Emchar c,
-                   unsigned_char_dynarr* dst)
-{
-  if (str->er_counter == 0)
-    {
-      if (CODING_SYSTEM_USE_ENTITY_REFERENCE (str->codesys)
-         && (c == '&') )
-       {
-         str->er_buf[0] = '&';
-         str->er_counter++;
-       }
-      else
-       DECODE_ADD_UCS_CHAR (c, dst);
-    }
-  else if (c == ';')
-    {
-      Lisp_Object string = make_string (str->er_buf,
-                                       str->er_counter);
-      Lisp_Object rest = Vcoded_charset_entity_reference_alist;
-      Lisp_Object cell;
-      Lisp_Object ret;
-      Lisp_Object pat;
-      Lisp_Object ccs;
-      int base;
-
-      while (!NILP (rest))
-       {                     
-         cell = Fcar (rest);
-         ccs = Fcar (cell);
-         if (NILP (ccs = Ffind_charset (ccs)))
-           continue;
-
-         cell = Fcdr (cell);
-         ret = Fcar (cell);
-         if (STRINGP (ret))
-           pat = ret;
-         else
-           continue;
-
-         cell = Fcdr (cell);
-         cell = Fcdr (cell);
-         ret = Fcar (cell);
-         if (EQ (ret, Qd))
-           {
-             pat = concat3 (build_string ("^&"),
-                            pat, build_string ("\\([0-9]+\\)$"));
-             base = 10;
-           }
-         else if (EQ (ret, Qx))
-           {
-             pat = concat3 (build_string ("^&"),
-                            pat, build_string ("\\([0-9a-f]+\\)$"));
-             base = 16;
-           }
-         else if (EQ (ret, QX))
-           {
-             pat = concat3 (build_string ("^&"),
-                            pat, build_string ("\\([0-9A-F]+\\)$"));
-             base = 16;
-           }
-         else
-           continue;
-
-         if (!NILP (Fstring_match (pat, string, Qnil, Qnil)))
-           {
-             int code
-               = XINT (Fstring_to_number
-                       (Fsubstring (string,
-                                    Fmatch_beginning (make_int (1)),
-                                    Fmatch_end (make_int (1))),
-                        make_int (base)));
-
-             DECODE_ADD_UCS_CHAR (DECODE_CHAR (ccs, code), dst);
-             goto decoded;
-           }
-         rest = Fcdr (rest);
-       }
-      if (!NILP (Fstring_match (build_string ("^&MCS-\\([0-9A-F]+\\)$"),
-                               string, Qnil, Qnil)))
-       {
-         int code
-           = XINT (Fstring_to_number
-                   (Fsubstring (string,
-                                Fmatch_beginning (make_int (1)),
-                                Fmatch_end (make_int (1))),
-                    make_int (16)));
-
-         DECODE_ADD_UCS_CHAR (code, dst);
-       }
-      else
-       {
-         Dynarr_add_many (dst, str->er_buf, str->er_counter);
-         Dynarr_add (dst, ';');
-       }
-    decoded:
-      str->er_counter = 0;
-    }
-  else if ( (str->er_counter >= 16) || (c >= 0x7F) )
-    {
-      Dynarr_add_many (dst, str->er_buf, str->er_counter);
-      str->er_counter = 0;
-      DECODE_ADD_UCS_CHAR (c, dst);
-    }
-  else
-    str->er_buf[str->er_counter++] = c;
-}
-
-INLINE_HEADER void
-COMPOSE_FLUSH_CHARS (struct decoding_stream *str, unsigned_char_dynarr* dst);
-INLINE_HEADER void
-COMPOSE_FLUSH_CHARS (struct decoding_stream *str, unsigned_char_dynarr* dst)
-{
-  unsigned i;
-
-  for (i = 0; i < str->combined_char_count; i++)
-    decode_add_er_char (str, str->combined_chars[i], dst);
-  str->combined_char_count = 0;
-  str->combining_table = Qnil;
-}
-
-void COMPOSE_ADD_CHAR (struct decoding_stream *str, Emchar character,
-                      unsigned_char_dynarr* dst);
-void
-COMPOSE_ADD_CHAR (struct decoding_stream *str,
-                 Emchar character, unsigned_char_dynarr* dst)
-{
-  if (CODING_SYSTEM_DISABLE_COMPOSITION (str->codesys))
-    decode_add_er_char (str, character, dst);
-  else if (!CONSP (str->combining_table))
-    {
-      Lisp_Object ret
-       = Fget_char_attribute (make_char (character), Qcomposition, Qnil);
-
-      if (NILP (ret))
-       decode_add_er_char (str, character, dst);
-      else
-       {
-         str->combined_chars[0] = character;
-         str->combined_char_count = 1;
-         str->combining_table = ret;
-       }
-    }
-  else
-    {
-      Lisp_Object ret = Fcdr (Fassq (make_char (character), str->combining_table));
-
-      if (CHARP (ret))
-       {
-         Emchar char2 = XCHARVAL (ret);
-         ret = Fget_char_attribute (make_char (character), Qcomposition, Qnil);
-         if (NILP (ret))
-           {
-             decode_add_er_char (str, character, dst);
-             str->combined_char_count = 0;
-             str->combining_table = Qnil;
-           }
-         else
-           {
-             str->combined_chars[0] = char2;
-             str->combined_char_count = 1;
-             str->combining_table = ret;
-           }
-       }
-      else
-       {
-         COMPOSE_FLUSH_CHARS (str, dst);
-         decode_add_er_char (str, character, dst);
-       }
-    }
-}
-#else /* not UTF2000 */
-#define COMPOSE_FLUSH_CHARS(str, dst)
-#define COMPOSE_ADD_CHAR(str, ch, dst) DECODE_ADD_UCS_CHAR (ch, dst)
-#endif /* UTF2000 */
-
-static ssize_t decoding_reader (Lstream *stream,
-                               unsigned char *data, size_t size);
-static ssize_t decoding_writer (Lstream *stream,
-                               const unsigned char *data, size_t size);
+static Lstream_data_count decoding_reader (Lstream *stream,
+                               unsigned char *data, Lstream_data_count size);
+static Lstream_data_count decoding_writer (Lstream *stream,
+                               const unsigned char *data, Lstream_data_count size);
 static int decoding_rewinder   (Lstream *stream);
 static int decoding_seekable_p (Lstream *stream);
 static int decoding_flusher    (Lstream *stream);
@@ -2487,12 +2354,12 @@ decoding_marker (Lisp_Object stream)
 /* Read SIZE bytes of data and store it into DATA.  We are a decoding stream
    so we read data from the other end, decode it, and store it into DATA. */
 
-static ssize_t
-decoding_reader (Lstream *stream, unsigned char *data, size_t size)
+static Lstream_data_count
+decoding_reader (Lstream *stream, unsigned char *data, Lstream_data_count size)
 {
   struct decoding_stream *str = DECODING_STREAM_DATA (stream);
   unsigned char *orig_data = data;
-  ssize_t read_size;
+  Lstream_data_count read_size;
   int error_occurred = 0;
 
   /* We need to interface to mule_decode(), which expects to take some
@@ -2508,7 +2375,7 @@ decoding_reader (Lstream *stream, unsigned char *data, size_t size)
         most SIZE bytes, and delete the data from the runoff. */
       if (Dynarr_length (str->runoff) > 0)
        {
-         size_t chunk = min (size, (size_t) Dynarr_length (str->runoff));
+         Lstream_data_count chunk = min (size, (Lstream_data_count) Dynarr_length (str->runoff));
          memcpy (data, Dynarr_atp (str->runoff, 0), chunk);
          Dynarr_delete_many (str->runoff, 0, chunk);
          data += chunk;
@@ -2549,11 +2416,11 @@ decoding_reader (Lstream *stream, unsigned char *data, size_t size)
     return data - orig_data;
 }
 
-static ssize_t
-decoding_writer (Lstream *stream, const unsigned char *data, size_t size)
+static Lstream_data_count
+decoding_writer (Lstream *stream, const unsigned char *data, Lstream_data_count size)
 {
   struct decoding_stream *str = DECODING_STREAM_DATA (stream);
-  ssize_t retval;
+  Lstream_data_count retval;
 
   /* Decode all our data into the runoff, and then attempt to write
      it all out to the other end.  Remove whatever chunk we succeeded
@@ -2590,6 +2457,13 @@ reset_decoding_stream (struct decoding_stream *str)
   str->combined_char_count = 0;
   str->combining_table = Qnil;
 #endif
+  if (CODING_SYSTEM_TYPE (str->codesys) == CODESYS_AUTODETECT
+      || CODING_SYSTEM_EOL_TYPE (str->codesys) == EOL_AUTODETECT)
+    {
+      xzero (str->decst);
+      str->decst.eol_type = EOL_AUTODETECT;
+      str->decst.mask = ~0;
+    }
   str->flags = str->cpos = 0;
 }
 
@@ -2709,7 +2583,7 @@ make_decoding_output_stream (Lstream *stream, Lisp_Object codesys)
 
 static void
 mule_decode (Lstream *decoding, const Extbyte *src,
-            unsigned_char_dynarr *dst, size_t n)
+            unsigned_char_dynarr *dst, Lstream_data_count n)
 {
   struct decoding_stream *str = DECODING_STREAM_DATA (decoding);
 
@@ -2768,6 +2642,9 @@ mule_decode (Lstream *decoding, const Extbyte *src,
     case CODESYS_UCS4:
       decode_coding_ucs4 (decoding, src, dst, n);
       break;
+    case CODESYS_UTF16:
+      decode_coding_utf16 (decoding, src, dst, n);
+      break;
     case CODESYS_UTF8:
       decode_coding_utf8 (decoding, src, dst, n);
       break;
@@ -2831,7 +2708,7 @@ BUFFER defaults to the current buffer if unspecified.
       char tempbuf[1024]; /* some random amount */
       Bufpos newpos, even_newer_pos;
       Bufpos oldpos = lisp_buffer_stream_startpos (istr);
-      ssize_t size_in_bytes = Lstream_read (istr, tempbuf, sizeof (tempbuf));
+      Lstream_data_count size_in_bytes = Lstream_read (istr, tempbuf, sizeof (tempbuf));
 
       if (!size_in_bytes)
        break;
@@ -2921,9 +2798,9 @@ struct encoding_stream
 #endif /* MULE */
 };
 
-static ssize_t encoding_reader (Lstream *stream, unsigned char *data, size_t size);
-static ssize_t encoding_writer (Lstream *stream, const unsigned char *data,
-                               size_t size);
+static Lstream_data_count encoding_reader (Lstream *stream, unsigned char *data, Lstream_data_count size);
+static Lstream_data_count encoding_writer (Lstream *stream, const unsigned char *data,
+                               Lstream_data_count size);
 static int encoding_rewinder   (Lstream *stream);
 static int encoding_seekable_p (Lstream *stream);
 static int encoding_flusher    (Lstream *stream);
@@ -2955,12 +2832,12 @@ encoding_marker (Lisp_Object stream)
 /* Read SIZE bytes of data and store it into DATA.  We are a encoding stream
    so we read data from the other end, encode it, and store it into DATA. */
 
-static ssize_t
-encoding_reader (Lstream *stream, unsigned char *data, size_t size)
+static Lstream_data_count
+encoding_reader (Lstream *stream, unsigned char *data, Lstream_data_count size)
 {
   struct encoding_stream *str = ENCODING_STREAM_DATA (stream);
   unsigned char *orig_data = data;
-  ssize_t read_size;
+  Lstream_data_count read_size;
   int error_occurred = 0;
 
   /* We need to interface to mule_encode(), which expects to take some
@@ -3017,11 +2894,11 @@ encoding_reader (Lstream *stream, unsigned char *data, size_t size)
     return data - orig_data;
 }
 
-static ssize_t
-encoding_writer (Lstream *stream, const unsigned char *data, size_t size)
+static Lstream_data_count
+encoding_writer (Lstream *stream, const unsigned char *data, Lstream_data_count size)
 {
   struct encoding_stream *str = ENCODING_STREAM_DATA (stream);
-  ssize_t retval;
+  Lstream_data_count retval;
 
   /* Encode all our data into the runoff, and then attempt to write
      it all out to the other end.  Remove whatever chunk we succeeded
@@ -3069,6 +2946,10 @@ reset_encoding_stream (struct encoding_stream *str)
       str->encode_char = &char_encode_utf8;
       str->finish = &char_finish_utf8;
       break;
+    case CODESYS_UTF16:
+      str->encode_char = &char_encode_utf16;
+      str->finish = &char_finish_utf16;
+      break;
     case CODESYS_UCS4:
       str->encode_char = &char_encode_ucs4;
       str->finish = &char_finish_ucs4;
@@ -3178,7 +3059,7 @@ make_encoding_output_stream (Lstream *stream, Lisp_Object codesys)
 
 static void
 mule_encode (Lstream *encoding, const Bufbyte *src,
-            unsigned_char_dynarr *dst, size_t n)
+            unsigned_char_dynarr *dst, Lstream_data_count n)
 {
   struct encoding_stream *str = ENCODING_STREAM_DATA (encoding);
 
@@ -3249,7 +3130,7 @@ text.  BUFFER defaults to the current buffer if unspecified.
       char tempbuf[1024]; /* some random amount */
       Bufpos newpos, even_newer_pos;
       Bufpos oldpos = lisp_buffer_stream_startpos (istr);
-      ssize_t size_in_bytes = Lstream_read (istr, tempbuf, sizeof (tempbuf));
+      Lstream_data_count size_in_bytes = Lstream_read (istr, tempbuf, sizeof (tempbuf));
 
       if (!size_in_bytes)
        break;
@@ -3278,7 +3159,7 @@ text.  BUFFER defaults to the current buffer if unspecified.
 \f
 static void
 text_encode_generic (Lstream *encoding, const Bufbyte *src,
-                    unsigned_char_dynarr *dst, size_t n)
+                    unsigned_char_dynarr *dst, Lstream_data_count n)
 {
   unsigned char c;
   unsigned char char_boundary;
@@ -3346,32 +3227,326 @@ text_encode_generic (Lstream *encoding, const Bufbyte *src,
 }
 
 \f
+#ifdef UTF2000
 /************************************************************************/
-/*                          Shift-JIS methods                           */
+/*                          entity reference                            */
 /************************************************************************/
 
-/* Shift-JIS is a coding system encoding three character sets: ASCII, right
-   half of JISX0201-Kana, and JISX0208.  An ASCII character is encoded
-   as is.  A character of JISX0201-Kana (DIMENSION1_CHARS94 character set) is
-   encoded by "position-code + 0x80".  A character of JISX0208
-   (DIMENSION2_CHARS94 character set) is encoded in 2-byte but two
-   position-codes are divided and shifted so that it fit in the range
-   below.
-
-   --- CODE RANGE of Shift-JIS ---
-   (character set)     (range)
-   ASCII               0x00 .. 0x7F
-   JISX0201-Kana       0xA0 .. 0xDF
-   JISX0208 (1st byte) 0x80 .. 0x9F and 0xE0 .. 0xEF
-           (2nd byte)  0x40 .. 0x7E and 0x80 .. 0xFC
-   -------------------------------
-
-*/
-
-/* Is this the first byte of a Shift-JIS two-byte char? */
-
-#define BYTE_SJIS_TWO_BYTE_1_P(c) \
-  (((c) >= 0x81 && (c) <= 0x9F) || ((c) >= 0xE0 && (c) <= 0xEF))
+INLINE_HEADER void
+decode_flush_er_chars (struct decoding_stream *str, unsigned_char_dynarr* dst);
+INLINE_HEADER void
+decode_flush_er_chars (struct decoding_stream *str, unsigned_char_dynarr* dst)
+{
+  if ( str->er_counter > 0)
+    {
+      Dynarr_add_many (dst, str->er_buf, str->er_counter);
+      str->er_counter = 0;
+    }
+}
+
+void decode_add_er_char (struct decoding_stream *str, Emchar character,
+                        unsigned_char_dynarr* dst);
+void
+decode_add_er_char (struct decoding_stream *str, Emchar c,
+                   unsigned_char_dynarr* dst)
+{
+  if (str->er_counter == 0)
+    {
+      if (CODING_SYSTEM_USE_ENTITY_REFERENCE (str->codesys)
+         && (c == '&') )
+       {
+         str->er_buf[0] = '&';
+         str->er_counter++;
+       }
+      else
+       DECODE_ADD_UCS_CHAR (c, dst);
+    }
+  else if (c == ';')
+    {
+      Lisp_Object string = make_string (str->er_buf,
+                                       str->er_counter);
+      Lisp_Object rest = Vcoded_charset_entity_reference_alist;
+      Lisp_Object cell;
+      Lisp_Object ret;
+      Lisp_Object pat;
+      Lisp_Object ccs;
+      Lisp_Object char_type;
+      int base;
+
+      while (!NILP (rest))
+       {                     
+         cell = Fcar (rest);
+         ccs = Fcar (cell);
+         if (CONSP (ccs))
+           {
+             char_type = XCDR (ccs);
+             ccs = XCAR (ccs);
+           }
+         else
+           char_type = Qnil;
+         if (NILP (ccs = Ffind_charset (ccs)))
+           continue;
+
+         cell = Fcdr (cell);
+         ret = Fcar (cell);
+         if (STRINGP (ret))
+           pat = ret;
+         else
+           continue;
+
+         cell = Fcdr (cell);
+         cell = Fcdr (cell);
+         ret = Fcar (cell);
+         if (EQ (ret, Qd))
+           {
+             pat = concat3 (build_string ("^&"),
+                            pat, build_string ("\\([0-9]+\\)$"));
+             base = 10;
+           }
+         else if (EQ (ret, Qx))
+           {
+             pat = concat3 (build_string ("^&"),
+                            pat, build_string ("\\([0-9a-f]+\\)$"));
+             base = 16;
+           }
+         else if (EQ (ret, QX))
+           {
+             pat = concat3 (build_string ("^&"),
+                            pat, build_string ("\\([0-9A-F]+\\)$"));
+             base = 16;
+           }
+         else
+           continue;
+
+         if (!NILP (Fstring_match (pat, string, Qnil, Qnil)))
+           {
+             int code
+               = XINT (Fstring_to_number
+                       (Fsubstring (string,
+                                    Fmatch_beginning (make_int (1)),
+                                    Fmatch_end (make_int (1))),
+                        make_int (base)));
+             Emchar chr
+               = NILP (char_type)
+               ? DECODE_CHAR (ccs, code)
+               : decode_builtin_char (ccs, code);
+
+             DECODE_ADD_UCS_CHAR (chr, dst);
+             goto decoded;
+           }
+         rest = Fcdr (rest);
+       }
+      if (!NILP (Fstring_match (build_string ("^&MCS-\\([0-9A-F]+\\)$"),
+                               string, Qnil, Qnil)))
+       {
+         int code
+           = XINT (Fstring_to_number
+                   (Fsubstring (string,
+                                Fmatch_beginning (make_int (1)),
+                                Fmatch_end (make_int (1))),
+                    make_int (16)));
+
+         DECODE_ADD_UCS_CHAR (code, dst);
+       }
+      else
+       {
+         Dynarr_add_many (dst, str->er_buf, str->er_counter);
+         Dynarr_add (dst, ';');
+       }
+    decoded:
+      str->er_counter = 0;
+    }
+  else if ( (str->er_counter >= 16) || (c >= 0x7F) )
+    {
+      Dynarr_add_many (dst, str->er_buf, str->er_counter);
+      str->er_counter = 0;
+      DECODE_ADD_UCS_CHAR (c, dst);
+    }
+  else
+    str->er_buf[str->er_counter++] = c;
+}
+
+void char_encode_as_entity_reference (Emchar ch, char* buf);
+void
+char_encode_as_entity_reference (Emchar ch, char* buf)
+{
+  Lisp_Object rest = Vcoded_charset_entity_reference_alist;
+  Lisp_Object cell;
+  Lisp_Object ccs;
+  Lisp_Object char_type;
+  int format_columns, idx;
+  char format[18];
+
+  while (!NILP (rest))
+    {
+      cell = Fcar (rest);
+      ccs = Fcar (cell);
+      if (CONSP (ccs))
+       {
+         char_type = XCDR (ccs);
+         ccs = XCAR (ccs);
+       }
+      else
+       char_type = Qnil;
+      if (!NILP (ccs = Ffind_charset (ccs)))
+       {
+         int code_point = charset_code_point (ccs, ch, 0);
+
+         if ( (code_point >= 0)
+              && (NILP (char_type)
+                  || DECODE_CHAR (ccs, code_point) != ch) )
+           {
+             Lisp_Object ret;
+
+             cell = Fcdr (cell);
+             ret = Fcar (cell);
+             if (STRINGP (ret) && ((idx = XSTRING_LENGTH (ret)) <= 6))
+               {
+                 format[0] = '&';
+                 strncpy (&format[1], XSTRING_DATA (ret), idx);
+                 idx++;
+               }
+             else
+               continue;
+
+             cell = Fcdr (cell);
+             ret = Fcar (cell);
+             if (INTP (ret))
+               {
+                 format[idx++] = '%';
+                 format_columns = XINT (ret);
+                 if ( (2 <= format_columns) && (format_columns <= 8) )
+                   {
+                     format [idx++] = '0';
+                     format [idx++] = '0' + format_columns;
+                   }
+               }
+
+             cell = Fcdr (cell);
+             ret = Fcar (cell);
+             if (EQ (ret, Qd))
+               format [idx++] = 'd';
+             else if (EQ (ret, Qx))
+               format [idx++] = 'x';
+             else if (EQ (ret, QX))
+               format [idx++] = 'X';
+             else
+               continue;
+             format [idx++] = ';';
+             format [idx++] = 0;
+
+             sprintf (buf, format, code_point);
+             return;
+           }
+       }
+      rest = Fcdr (rest);
+    }
+  sprintf (buf, "&MCS-%08X;", ch);
+}
+
+\f
+/************************************************************************/
+/*                          character composition                       */
+/************************************************************************/
+extern Lisp_Object Qcomposition;
+
+INLINE_HEADER void
+COMPOSE_FLUSH_CHARS (struct decoding_stream *str, unsigned_char_dynarr* dst);
+INLINE_HEADER void
+COMPOSE_FLUSH_CHARS (struct decoding_stream *str, unsigned_char_dynarr* dst)
+{
+  unsigned i;
+
+  for (i = 0; i < str->combined_char_count; i++)
+    decode_add_er_char (str, str->combined_chars[i], dst);
+  str->combined_char_count = 0;
+  str->combining_table = Qnil;
+}
+
+void COMPOSE_ADD_CHAR (struct decoding_stream *str, Emchar character,
+                      unsigned_char_dynarr* dst);
+void
+COMPOSE_ADD_CHAR (struct decoding_stream *str,
+                 Emchar character, unsigned_char_dynarr* dst)
+{
+  if (CODING_SYSTEM_DISABLE_COMPOSITION (str->codesys))
+    decode_add_er_char (str, character, dst);
+  else if (!CONSP (str->combining_table))
+    {
+      Lisp_Object ret
+       = Fget_char_attribute (make_char (character), Qcomposition, Qnil);
+
+      if (NILP (ret))
+       decode_add_er_char (str, character, dst);
+      else
+       {
+         str->combined_chars[0] = character;
+         str->combined_char_count = 1;
+         str->combining_table = ret;
+       }
+    }
+  else
+    {
+      Lisp_Object ret
+       = Fcdr (Fassq (make_char (character), str->combining_table));
+
+      if (CHARP (ret))
+       {
+         Emchar char2 = XCHARVAL (ret);
+         ret = Fget_char_attribute (make_char (character), Qcomposition,
+                                    Qnil);
+         if (NILP (ret))
+           {
+             decode_add_er_char (str, character, dst);
+             str->combined_char_count = 0;
+             str->combining_table = Qnil;
+           }
+         else
+           {
+             str->combined_chars[0] = char2;
+             str->combined_char_count = 1;
+             str->combining_table = ret;
+           }
+       }
+      else
+       {
+         COMPOSE_FLUSH_CHARS (str, dst);
+         decode_add_er_char (str, character, dst);
+       }
+    }
+}
+#else /* not UTF2000 */
+#define COMPOSE_FLUSH_CHARS(str, dst)
+#define COMPOSE_ADD_CHAR(str, ch, dst) DECODE_ADD_UCS_CHAR (ch, dst)
+#endif /* UTF2000 */
+
+\f
+/************************************************************************/
+/*                          Shift-JIS methods                           */
+/************************************************************************/
+
+/* Shift-JIS is a coding system encoding three character sets: ASCII, right
+   half of JISX0201-Kana, and JISX0208.  An ASCII character is encoded
+   as is.  A character of JISX0201-Kana (DIMENSION1_CHARS94 character set) is
+   encoded by "position-code + 0x80".  A character of JISX0208
+   (DIMENSION2_CHARS94 character set) is encoded in 2-byte but two
+   position-codes are divided and shifted so that it fit in the range
+   below.
+
+   --- CODE RANGE of Shift-JIS ---
+   (character set)     (range)
+   ASCII               0x00 .. 0x7F
+   JISX0201-Kana       0xA0 .. 0xDF
+   JISX0208 (1st byte) 0x80 .. 0x9F and 0xE0 .. 0xEF
+           (2nd byte)  0x40 .. 0x7E and 0x80 .. 0xFC
+   -------------------------------
+
+*/
+
+/* Is this the first byte of a Shift-JIS two-byte char? */
+
+#define BYTE_SJIS_TWO_BYTE_1_P(c) \
+  (((c) >= 0x81 && (c) <= 0x9F) || ((c) >= 0xE0 && (c) <= 0xEF))
 
 /* Is this the second byte of a Shift-JIS two-byte char? */
 
@@ -3382,7 +3557,7 @@ text_encode_generic (Lstream *encoding, const Bufbyte *src,
   ((c) >= 0xA1 && (c) <= 0xDF)
 
 static int
-detect_coding_sjis (struct detection_state *st, const Extbyte *src, size_t n)
+detect_coding_sjis (struct detection_state *st, const Extbyte *src, Lstream_data_count n)
 {
   while (n--)
     {
@@ -3405,7 +3580,7 @@ detect_coding_sjis (struct detection_state *st, const Extbyte *src, size_t n)
 
 static void
 decode_coding_sjis (Lstream *decoding, const Extbyte *src,
-                   unsigned_char_dynarr *dst, size_t n)
+                   unsigned_char_dynarr *dst, Lstream_data_count n)
 {
   struct decoding_stream *str = DECODING_STREAM_DATA (decoding);
   unsigned int flags  = str->flags;
@@ -3492,12 +3667,12 @@ char_encode_shift_jis (struct encoding_stream *str, Emchar ch,
     {
       unsigned int s1, s2;
 #ifdef UTF2000
-      int code_point = charset_code_point (Vcharset_latin_jisx0201, ch);
+      int code_point = charset_code_point (Vcharset_latin_jisx0201, ch, 0);
 
       if (code_point >= 0)
        Dynarr_add (dst, code_point);
       else if ((code_point
-               = charset_code_point (Vcharset_japanese_jisx0208_1990, ch))
+               = charset_code_point (Vcharset_japanese_jisx0208_1990, ch, 0))
               >= 0)
        {
          ENCODE_SJIS ((code_point >> 8) | 0x80,
@@ -3506,11 +3681,11 @@ char_encode_shift_jis (struct encoding_stream *str, Emchar ch,
          Dynarr_add (dst, s2);
        }
       else if ((code_point
-               = charset_code_point (Vcharset_katakana_jisx0201, ch))
+               = charset_code_point (Vcharset_katakana_jisx0201, ch, 0))
               >= 0)
        Dynarr_add (dst, code_point | 0x80);
       else if ((code_point
-               = charset_code_point (Vcharset_japanese_jisx0208, ch))
+               = charset_code_point (Vcharset_japanese_jisx0208, ch, 0))
               >= 0)
        {
          ENCODE_SJIS ((code_point >> 8) | 0x80,
@@ -3518,7 +3693,7 @@ char_encode_shift_jis (struct encoding_stream *str, Emchar ch,
          Dynarr_add (dst, s1);
          Dynarr_add (dst, s2);
        }
-      else if ((code_point = charset_code_point (Vcharset_ascii, ch))
+      else if ((code_point = charset_code_point (Vcharset_ascii, ch, 0))
               >= 0)
        Dynarr_add (dst, code_point);
       else
@@ -3710,7 +3885,7 @@ Return the corresponding character code in SHIFT-JIS as a cons of two bytes.
 } while (0)
 
 static int
-detect_coding_big5 (struct detection_state *st, const Extbyte *src, size_t n)
+detect_coding_big5 (struct detection_state *st, const Extbyte *src, Lstream_data_count n)
 {
   while (n--)
     {
@@ -3743,7 +3918,7 @@ detect_coding_big5 (struct detection_state *st, const Extbyte *src, size_t n)
 
 static void
 decode_coding_big5 (Lstream *decoding, const Extbyte *src,
-                   unsigned_char_dynarr *dst, size_t n)
+                   unsigned_char_dynarr *dst, Lstream_data_count n)
 {
   struct decoding_stream *str = DECODING_STREAM_DATA (decoding);
   unsigned int flags  = str->flags;
@@ -3789,14 +3964,33 @@ decode_coding_big5 (Lstream *decoding, const Extbyte *src,
        {
          DECODE_HANDLE_EOL_TYPE (eol_type, c, flags, dst);
          if (BYTE_BIG5_TWO_BYTE_1_P (c))
-           cpos = c;
+           {
+             decode_flush_er_chars (str, dst);
+             cpos = c;
+           }
+         else if ( c < ' ' )
+           {
+             decode_flush_er_chars (str, dst);
+             DECODE_HANDLE_EOL_TYPE (eol_type, c, flags, dst);
+             DECODE_ADD_BINARY_CHAR (c, dst);
+           }
          else
-           DECODE_ADD_BINARY_CHAR (c, dst);
+           {
+             /* DECODE_ADD_BINARY_CHAR (c, dst); */
+             decode_add_er_char (str, c, dst);
+           }
        }
     label_continue_loop:;
     }
 
-  DECODE_HANDLE_END_OF_CONVERSION (flags, cpos, dst);
+  /* DECODE_HANDLE_END_OF_CONVERSION (flags, cpos, dst); */
+  if (flags & CODING_STATE_END)
+    {
+      decode_flush_er_chars (str, dst);
+      DECODE_OUTPUT_PARTIAL_CHAR (cpos);
+      if (flags & CODING_STATE_CR)
+       Dynarr_add (dst, '\r');
+    }
 
   str->flags = flags;
   str->cpos  = cpos;
@@ -3824,21 +4018,21 @@ char_encode_big5 (struct encoding_stream *str, Emchar ch,
       Lisp_Object ccs
        = CODING_SYSTEM_ISO2022_INITIAL_CHARSET (str->codesys, 1);
 
-      if ((code_point = charset_code_point (Vcharset_ascii, ch)) >= 0)
+      if ((code_point = charset_code_point (Vcharset_ascii, ch, 0)) >= 0)
        Dynarr_add (dst, code_point);
-      else if ((code_point = charset_code_point (ccs, ch)) >= 0)
+      else if ((code_point = charset_code_point (ccs, ch, 0)) >= 0)
        {
          Dynarr_add (dst, code_point >> 8);
          Dynarr_add (dst, code_point & 0xFF);
        }
       else if ((code_point
-               = charset_code_point (Vcharset_chinese_big5, ch)) >= 0)
+               = charset_code_point (Vcharset_chinese_big5, ch, 0)) >= 0)
        {
          Dynarr_add (dst, code_point >> 8);
          Dynarr_add (dst, code_point & 0xFF);
        }
       else if ((code_point
-               = charset_code_point (Vcharset_chinese_big5_1, ch)) >= 0)
+               = charset_code_point (Vcharset_chinese_big5_1, ch, 0)) >= 0)
        {
          unsigned int I
            = ((code_point >> 8) - 33) * (0xFF - 0xA1)
@@ -3851,7 +4045,7 @@ char_encode_big5 (struct encoding_stream *str, Emchar ch,
          Dynarr_add (dst, b2);
        }
       else if ((code_point
-               = charset_code_point (Vcharset_chinese_big5_2, ch)) >= 0)
+               = charset_code_point (Vcharset_chinese_big5_2, ch, 0)) >= 0)
        {
          unsigned int I
            = ((code_point >> 8) - 33) * (0xFF - 0xA1)
@@ -3865,6 +4059,13 @@ char_encode_big5 (struct encoding_stream *str, Emchar ch,
          Dynarr_add (dst, b1);
          Dynarr_add (dst, b2);
        }
+      else if (CODING_SYSTEM_USE_ENTITY_REFERENCE (str->codesys))
+       {
+         char buf[18];
+
+         char_encode_as_entity_reference (ch, buf);
+         Dynarr_add_many (dst, buf, strlen (buf));
+       }
       else
        Dynarr_add (dst, '?');
 #else
@@ -3934,7 +4135,7 @@ Return the corresponding character code in Big5.
 /************************************************************************/
 
 static int
-detect_coding_ucs4 (struct detection_state *st, const Extbyte *src, size_t n)
+detect_coding_ucs4 (struct detection_state *st, const Extbyte *src, Lstream_data_count n)
 {
   while (n--)
     {
@@ -3959,7 +4160,7 @@ detect_coding_ucs4 (struct detection_state *st, const Extbyte *src, size_t n)
 
 static void
 decode_coding_ucs4 (Lstream *decoding, const Extbyte *src,
-                   unsigned_char_dynarr *dst, size_t n)
+                   unsigned_char_dynarr *dst, Lstream_data_count n)
 {
   struct decoding_stream *str = DECODING_STREAM_DATA (decoding);
   unsigned int flags = str->flags;
@@ -4011,11 +4212,128 @@ char_finish_ucs4 (struct encoding_stream *str, unsigned_char_dynarr *dst,
 
 \f
 /************************************************************************/
+/*                           UTF-16 methods                             */
+/************************************************************************/
+
+static int
+detect_coding_utf16 (struct detection_state *st, const Extbyte *src, Lstream_data_count n)
+{
+  return CODING_CATEGORY_UTF16_MASK;
+}
+
+static void
+decode_coding_utf16 (Lstream *decoding, const Extbyte *src,
+                   unsigned_char_dynarr *dst, Lstream_data_count n)
+{
+  struct decoding_stream *str = DECODING_STREAM_DATA (decoding);
+  unsigned int flags = str->flags;
+  unsigned int cpos  = str->cpos;
+  unsigned char counter = str->counter & 3;
+  unsigned char byte_order = str->counter >> 2;
+  eol_type_t eol_type = str->eol_type;
+
+  while (n--)
+    {
+      unsigned char c = *(unsigned char *)src++;
+      if (counter == 0)
+       {
+         cpos = c;
+         counter = 1;
+       }
+      else if (counter == 1)
+       {
+         int code;
+
+         if (byte_order == 0)
+           code = (c << 8) | cpos;
+         else
+           code = (cpos << 8) | c;
+         if (code == 0xFFFE)
+           {
+             code = ((code & 0xFF) << 8) | (code >> 8);
+             if ( byte_order == 0 )
+               byte_order = 1;
+             else
+               byte_order = 0;
+           }
+         if ( (0xD800 <= code) && (code <= 0xDBFF) )
+           {
+             counter = 2;
+             cpos = code;
+           }
+         else
+           {
+             counter = 0;
+             cpos = 0;
+             if (code != 0xFEFF)
+               {
+                 DECODE_HANDLE_EOL_TYPE (eol_type, code, flags, dst);
+                 DECODE_ADD_UCS_CHAR (code, dst);
+               }
+           }
+       }
+      else if (counter == 2)
+       {
+         cpos = (cpos << 8) | c;
+         counter++;
+       }
+      else
+       {
+         int x = cpos >> 8;
+         int y
+           = (byte_order == 0)
+           ? (c << 8) | (cpos & 0xFF)
+           : ((cpos & 0xFF) << 8) | c;
+
+         DECODE_ADD_UCS_CHAR ((x - 0xD800) * 0x400 + (y - 0xDC00)
+                              + 0x10000, dst);
+         counter = 0;
+         cpos = 0;
+       }
+    label_continue_loop:;
+    }
+  if (counter & CODING_STATE_END)
+    DECODE_OUTPUT_PARTIAL_CHAR (cpos);
+
+  str->flags   = flags;
+  str->cpos    = cpos;
+  str->counter = (byte_order << 2) | counter;
+}
+
+void
+char_encode_utf16 (struct encoding_stream *str, Emchar ch,
+                 unsigned_char_dynarr *dst, unsigned int *flags)
+{
+  if (ch <= 0xFFFF)
+    {
+      Dynarr_add (dst, ch);
+      Dynarr_add (dst, ch >> 8);
+    }
+  else
+    {
+      int y = ((ch - 0x10000) / 0x400) + 0xD800;
+      int z = ((ch - 0x10000) % 0x400) + 0xDC00;
+      
+      Dynarr_add (dst, y);
+      Dynarr_add (dst, y >> 8);
+      Dynarr_add (dst, z);
+      Dynarr_add (dst, z >> 8);
+    }
+}
+
+void
+char_finish_utf16 (struct encoding_stream *str, unsigned_char_dynarr *dst,
+                 unsigned int *flags)
+{
+}
+
+\f
+/************************************************************************/
 /*                           UTF-8 methods                              */
 /************************************************************************/
 
 static int
-detect_coding_utf8 (struct detection_state *st, const Extbyte *src, size_t n)
+detect_coding_utf8 (struct detection_state *st, const Extbyte *src, Lstream_data_count n)
 {
   while (n--)
     {
@@ -4139,13 +4457,18 @@ decode_output_utf8_partial_char (unsigned char counter,
 
 static void
 decode_coding_utf8 (Lstream *decoding, const Extbyte *src,
-                   unsigned_char_dynarr *dst, size_t n)
+                   unsigned_char_dynarr *dst, Lstream_data_count n)
 {
   struct decoding_stream *str = DECODING_STREAM_DATA (decoding);
   unsigned int flags   = str->flags;
   unsigned int cpos    = str->cpos;
   eol_type_t eol_type  = str->eol_type;
   unsigned char counter        = str->counter;
+#ifdef UTF2000
+  Lisp_Object ccs
+    = CODING_SYSTEM_ISO2022_INITIAL_CHARSET (DECODING_STREAM_DATA
+                                            (decoding)->codesys, 0);
+#endif
 
   while (n--)
     {
@@ -4197,8 +4520,18 @@ decode_coding_utf8 (Lstream *decoding, const Extbyte *src,
          cpos = ( cpos << 6 ) | ( c & 0x3f );
          if (counter == 1)
            {
-             /* DECODE_ADD_UCS_CHAR (cpos, dst); */
-             COMPOSE_ADD_CHAR (str, cpos, dst);
+             Emchar char_id;
+
+             if (!NILP (ccs))
+               {
+                 char_id = decode_defined_char (ccs, cpos);
+
+                 if (char_id < 0)
+                   char_id = cpos;
+               }
+             else
+               ccs = char_id;
+             COMPOSE_ADD_CHAR (str, char_id, dst);
              cpos = 0;
              counter = 0;
            }
@@ -4252,74 +4585,31 @@ char_encode_utf8 (struct encoding_stream *str, Emchar ch,
     }
   else
     {
-      int code_point = charset_code_point (Vcharset_ucs, ch);
+      Lisp_Object ucs_ccs
+       = CODING_SYSTEM_ISO2022_INITIAL_CHARSET (str->codesys, 0);
+      int code_point = charset_code_point (ucs_ccs, ch, 0);
 
       if ( (code_point < 0) || (code_point > 0x10FFFF) )
        {
-         if (CODING_SYSTEM_USE_ENTITY_REFERENCE (str->codesys))
+         Lisp_Object map
+           = CODING_SYSTEM_ISO2022_INITIAL_CHARSET (str->codesys, 1);
+         Lisp_Object ret;
+
+         if ( !NILP (map)
+              && INTP (ret = Fget_char_attribute (make_char (ch),
+                                                  map, Qnil)) )
+           code_point = XINT (ret);
+         else if ( !NILP (map =
+                          CODING_SYSTEM_ISO2022_INITIAL_CHARSET
+                          (str->codesys, 2))
+                   && INTP (ret = Fget_char_attribute (make_char (ch),
+                                                       map, Qnil)) )
+           code_point = XINT (ret);
+         else if (CODING_SYSTEM_USE_ENTITY_REFERENCE (str->codesys))
            {
-             Lisp_Object rest = Vcoded_charset_entity_reference_alist;
-             Lisp_Object cell;
-             Lisp_Object ret;
-             Lisp_Object ccs;
-             int format_columns, idx;
-             char buf[16], format[16];
+             char buf[18];
 
-             while (!NILP (rest))
-               {
-                 cell = Fcar (rest);
-                 ccs = Fcar (cell);
-                 if (!NILP (ccs = Ffind_charset (ccs)))
-                   {
-                     if ( (code_point
-                           = charset_code_point (ccs, ch)) >= 0 )
-                       {
-                         cell = Fcdr (cell);
-                         ret = Fcar (cell);
-                         if (STRINGP (ret)
-                             && ((idx =XSTRING_LENGTH (ret)) <= 6))
-                           {
-                             strncpy (format, XSTRING_DATA (ret), idx);
-                           }
-                         else
-                           continue;
-
-                         cell = Fcdr (cell);
-                         ret = Fcar (cell);
-                         if (INTP (ret))
-                           {
-                             format [idx++] = '%';
-                             format_columns = XINT (ret);
-                             if ( (2 <= format_columns)
-                                  && (format_columns <= 8) )
-                               {
-                                 format [idx++] = '0';
-                                 format [idx++] = '0' + format_columns;
-                               }
-                           }
-
-                         cell = Fcdr (cell);
-                         ret = Fcar (cell);
-                         if (EQ (ret, Qd))
-                           format [idx++] = 'd';
-                         else if (EQ (ret, Qx))
-                           format [idx++] = 'x';
-                         else if (EQ (ret, QX))
-                           format [idx++] = 'X';
-                         else
-                           continue;
-                         format [idx++] = 0;
-
-                         sprintf (buf, format, code_point);
-                         Dynarr_add (dst, '&');
-                         Dynarr_add_many (dst, buf, strlen (buf));
-                         Dynarr_add (dst, ';');
-                         return;
-                       }
-                   }
-                 rest = Fcdr (rest);
-               }
-             sprintf (buf, "&MCS-%08X;", ch);
+             char_encode_as_entity_reference (ch, buf);
              Dynarr_add_many (dst, buf, strlen (buf));
              return;
            }
@@ -4951,7 +5241,7 @@ parse_iso2022_esc (Lisp_Object codesys, struct iso2022_decoder *iso,
 }
 
 static int
-detect_coding_iso2022 (struct detection_state *st, const Extbyte *src, size_t n)
+detect_coding_iso2022 (struct detection_state *st, const Extbyte *src, Lstream_data_count n)
 {
   int mask;
 
@@ -5142,7 +5432,7 @@ ensure_correct_direction (int direction, Lisp_Coding_System *codesys,
 
 static void
 decode_coding_iso2022 (Lstream *decoding, const Extbyte *src,
-                      unsigned_char_dynarr *dst, size_t n)
+                      unsigned_char_dynarr *dst, Lstream_data_count n)
 {
   struct decoding_stream *str = DECODING_STREAM_DATA (decoding);
   unsigned int flags   = str->flags;
@@ -5643,12 +5933,12 @@ char_encode_iso2022 (struct encoding_stream *str, Emchar ch,
       for (i = 0; i < 4; i++)
        {
          if ((CHARSETP (charset = str->iso2022.charset[i])
-              && ((code_point = charset_code_point (charset, ch)) >= 0))
+              && ((code_point = charset_code_point (charset, ch, 0)) >= 0))
              ||
              (CHARSETP
               (charset
                = CODING_SYSTEM_ISO2022_INITIAL_CHARSET (codesys, i))
-              && ((code_point = charset_code_point (charset, ch)) >= 0)))
+              && ((code_point = charset_code_point (charset, ch, 0)) >= 0)))
            {
              reg = i;
              break;
@@ -5805,7 +6095,7 @@ char_finish_iso2022 (struct encoding_stream *str, unsigned_char_dynarr *dst,
    interpreted as being in any particular decoding. */
 static void
 decode_coding_no_conversion (Lstream *decoding, const Extbyte *src,
-                            unsigned_char_dynarr *dst, size_t n)
+                            unsigned_char_dynarr *dst, Lstream_data_count n)
 {
   struct decoding_stream *str = DECODING_STREAM_DATA (decoding);
   unsigned int flags  = str->flags;
@@ -5829,7 +6119,7 @@ decode_coding_no_conversion (Lstream *decoding, const Extbyte *src,
 
 static void
 encode_coding_no_conversion (Lstream *encoding, const Bufbyte *src,
-                            unsigned_char_dynarr *dst, size_t n)
+                            unsigned_char_dynarr *dst, Lstream_data_count n)
 {
   unsigned char c;
   struct encoding_stream *str = ENCODING_STREAM_DATA (encoding);
@@ -5952,8 +6242,7 @@ syms_of_file_coding (void)
 {
   INIT_LRECORD_IMPLEMENTATION (coding_system);
 
-  deferror (&Qcoding_system_error, "coding-system-error",
-           "Coding-system error", Qio_error);
+  DEFERROR_STANDARD (Qcoding_system_error, Qio_error);
 
   DEFSUBR (Fcoding_system_p);
   DEFSUBR (Ffind_coding_system);
@@ -5998,6 +6287,7 @@ syms_of_file_coding (void)
   defsymbol (&Qshift_jis, "shift-jis");
   defsymbol (&Qucs4, "ucs-4");
   defsymbol (&Qutf8, "utf-8");
+  defsymbol (&Qutf16, "utf-16");
   defsymbol (&Qccl, "ccl");
   defsymbol (&Qiso2022, "iso2022");
 #endif /* MULE */
@@ -6033,6 +6323,7 @@ syms_of_file_coding (void)
   defsymbol (&Qescape_quoted, "escape-quoted");
 #endif /* MULE */
 #ifdef UTF2000
+  defsymbol (&Qutf_8_mcs, "utf-8-mcs");
   defsymbol (&Qdisable_composition, "disable-composition");
   defsymbol (&Quse_entity_reference, "use-entity-reference");
   defsymbol (&Qd, "d");
@@ -6049,6 +6340,8 @@ syms_of_file_coding (void)
             "big5");
   defsymbol (&coding_category_symbol[CODING_CATEGORY_UCS4],
             "ucs-4");
+  defsymbol (&coding_category_symbol[CODING_CATEGORY_UTF16],
+            "utf-16");
   defsymbol (&coding_category_symbol[CODING_CATEGORY_UTF8],
             "utf-8");
   defsymbol (&coding_category_symbol[CODING_CATEGORY_ISO_7],
@@ -6227,9 +6520,10 @@ complex_vars_of_file_coding (void)
 
 #ifdef UTF2000
   Fmake_coding_system
-    (Qutf8, Qutf8,
-     build_string ("Coding-system of ISO/IEC 10646 UTF-8."),
-     list2 (Qmnemonic, build_string ("UTF8")));
+    (Qutf_8_mcs, Qutf8,
+     build_string
+     ("Coding-system of UTF-8 with Multiple Coded-character-Sets extension."),
+     list2 (Qmnemonic, build_string ("MTF8")));
 #endif
 
   Fdefine_coding_system_alias (Qno_conversion, Qraw_text);
@@ -6245,7 +6539,7 @@ complex_vars_of_file_coding (void)
 
 #ifdef UTF2000
   fcd->coding_category_system[CODING_CATEGORY_UTF8]
-   = Fget_coding_system (Qutf8);
+   = Fget_coding_system (Qutf_8_mcs);
 #endif
 
 #if defined(MULE) && !defined(UTF2000)