Add arch taglines
[bpt/emacs.git] / lisp / international / utf-16.el
CommitLineData
fc2938d1
DL
1;;; utf-16.el --- UTF-16 encoding/decoding
2
cbcd4dc9 3;; Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
fc2938d1
DL
4
5;; Author: Dave Love <fx@gnu.org>
6;; Keywords: Unicode, UTF-16, i18n
7
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is free software; you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
12;; the Free Software Foundation; either version 2, or (at your option)
13;; any later version.
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
21;; along with GNU Emacs; see the file COPYING. If not, write to the
22;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23;; Boston, MA 02111-1307, USA.
24
25;;; Commentary:
26
27;; Support for UTF-16, which is a two-byte encoding (modulo
cbcd4dc9
DL
28;; surrogates) of Unicode, defined in RFC 2781. It is written either
29;; in little or big endian order and either with or without the
30;; leading BOM (a two-byte signature which identifies their byte sex).
65a0e5fe 31;;
cbcd4dc9 32;; We provide these base coding systems.
65a0e5fe
KH
33;; name endian BOM
34;; ---- ------ ---
35;; mule-utf-16le little no
36;; mule-utf-16be big no
37;; mule-utf-16le-with-signature little yes
38;; mule-utf-16be-with-signature big yes
39;; mule-utf-16 both yes
40;;
fc2938d1
DL
41;; Note that un-decodable sequences aren't (yet?) preserved as raw
42;; bytes, as they are with utf-8, so reading and writing as utf-16 can
43;; corrupt data.
44
45;;; Code:
46
47;; We end up with trivially different -le and -be versions of most
48;; things below, sometimes with commonality abstracted into a let
49;; binding for maintenance convenience.
50
51;; We'd need new charsets distinct from ascii and eight-bit-control to
52;; deal with untranslated sequences, since we can't otherwise
53;; distinguish the bytes, as we can with utf-8.
54
55;; ;; Do a multibyte write for bytes in r3 and r4.
56;; ;; Intended for untranslatable utf-16 sequences.
57;; (define-ccl-program ccl-mule-utf-16-untrans
58;; `(0
59;; (if (r3 < 128)
60;; (r0 = ,(charset-id 'ascii))
61;; (if (r3 < 160)
62;; (r0 = ,(charset-id 'eight-bit-control))
63;; (r0 = ,(charset-id 'eight-bit-graphic))))
64;; (if (r4 < 128)
65;; (r0 = ,(charset-id 'ascii))
66;; (if (r4 < 160)
67;; (r0 = ,(charset-id 'eight-bit-control))
68;; (r0 = ,(charset-id 'eight-bit-graphic))))
69;; (r1 = r4)))
70;; "Do a multibyte write for bytes in r3 and r4.
71;; First swap them if we're big endian, indicated by r5==0.
72;; Intended for untranslatable utf-16 sequences.")
73
74;; Needed in macro expansion, so can't be let-bound. Zapped after use.
75(eval-and-compile
76(defconst utf-16-decode-ucs
2217b8e1 77 ;; We have the unicode in r1. Output is charset ID in r0, code
4fbc4b17 78 ;; point in r1.
278ce936
KH
79 `((lookup-integer utf-subst-table-for-decode r1 r3)
80 (if r7 ; got a translation
81 ((r0 = r1) (r1 = r3))
82 (if (r1 < 128)
83 (r0 = ,(charset-id 'ascii))
84 (if (r1 < 160)
85 (r0 = ,(charset-id 'eight-bit-control))
86 (if (r1 < 256)
87 ((r0 = ,(charset-id 'latin-iso8859-1))
88 (r1 -= 128))
89 (if (r1 < #x2500)
90 ((r0 = ,(charset-id 'mule-unicode-0100-24ff))
91 (r1 -= #x100)
92 (r2 = (((r1 / 96) + 32) << 7))
93 (r1 %= 96)
94 (r1 += (r2 + 32)))
95 (if (r1 < #x3400)
96 ((r0 = ,(charset-id 'mule-unicode-2500-33ff))
97 (r1 -= #x2500)
98 (r2 = (((r1 / 96) + 32) << 7))
99 (r1 %= 96)
100 (r1 += (r2 + 32)))
101 (if (r1 < #xd800) ; 2 untranslated bytes
102 ;; ;; Assume this is rare, so don't worry about the
103 ;; ;; overhead of the call.
104 ;; (call mule-utf-16-untrans)
105 ((r0 = ,(charset-id 'mule-unicode-e000-ffff))
106 (r1 = 15037)) ; U+fffd
107 (if (r1 < #xe000) ; surrogate
108 ;; ((call mule-utf-16-untrans)
109 ;; (write-multibyte-character r0 r1)
110 ;; (read r3 r4)
111 ;; (call mule-utf-16-untrans))
112 ((read r3 r4)
113 (r0 = ,(charset-id 'mule-unicode-e000-ffff))
114 (r1 = 15037))
115 ((r0 = ,(charset-id 'mule-unicode-e000-ffff))
116 (r1 -= #xe000)
117 (r2 = (((r1 / 96) + 32) << 7))
118 (r1 %= 96)
4fbc4b17
KH
119 (r1 += (r2 + 32)))))))))))))
120
65a0e5fe 121(defconst utf-16le-decode-loop
4fbc4b17
KH
122 `(loop
123 (read r3 r4)
124 (r1 = (r4 <8 r3))
125 ,utf-16-decode-ucs
126 (translate-character utf-translation-table-for-decode r0 r1)
127 (write-multibyte-character r0 r1)
128 (repeat)))
129
65a0e5fe 130(defconst utf-16be-decode-loop
4fbc4b17
KH
131 `(loop
132 (read r3 r4)
133 (r1 = (r3 <8 r4))
134 ,@utf-16-decode-ucs
135 (translate-character utf-translation-table-for-decode r0 r1)
136 (write-multibyte-character r0 r1)
137 (repeat)))
138
139)
fc2938d1 140
65a0e5fe 141(define-ccl-program ccl-decode-mule-utf-16le
fc2938d1 142 `(2 ; 2 bytes -> 1 to 4 bytes
65a0e5fe 143 ,utf-16le-decode-loop)
2217b8e1 144 "Decode UTF-16LE (little endian without signature bytes).
fc2938d1 145Basic decoding is done into the charsets ascii, latin-iso8859-1 and
278ce936
KH
146mule-unicode-*. Un-representable Unicode characters are decoded as
147U+fffd. The result is run through the translation-table named
148`utf-translation-table-for-decode'.")
fc2938d1 149
65a0e5fe 150(define-ccl-program ccl-decode-mule-utf-16be
fc2938d1 151 `(2 ; 2 bytes -> 1 to 4 bytes
65a0e5fe 152 ,utf-16be-decode-loop)
2217b8e1 153 "Decode UTF-16BE (big endian without signature bytes).
fc2938d1
DL
154Basic decoding is done into the charsets ascii, latin-iso8859-1 and
155mule-unicode-*. Un-representable Unicode characters are
278ce936
KH
156decoded as U+fffd. The result is run through the translation-table of
157name `utf-translation-table-for-decode'.")
fc2938d1 158
65a0e5fe 159(define-ccl-program ccl-decode-mule-utf-16le-with-signature
4fbc4b17
KH
160 `(2
161 ((read r3 r4)
65a0e5fe
KH
162 ,utf-16le-decode-loop))
163 "Like ccl-decode-utf-16le but skip the first 2-byte BOM.")
4fbc4b17 164
65a0e5fe 165(define-ccl-program ccl-decode-mule-utf-16be-with-signature
4fbc4b17
KH
166 `(2
167 ((read r3 r4)
65a0e5fe
KH
168 ,utf-16be-decode-loop))
169 "Like ccl-decode-utf-16be but skip the first 2-byte BOM.")
4fbc4b17
KH
170
171(define-ccl-program ccl-decode-mule-utf-16
172 `(2
173 ((read r3 r4)
174 (r1 = (r3 <8 r4))
175 (if (r1 == #xFFFE)
176 ;; R1 is a BOM for little endian. We keep this character as
177 ;; is temporarily. It is removed by post-read-conversion
178 ;; function.
179 (,@utf-16-decode-ucs
180 (write-multibyte-character r0 r1)
65a0e5fe 181 ,utf-16le-decode-loop)
4fbc4b17
KH
182 ((if (r1 == #xFEFF)
183 ;; R1 is a BOM for big endian, but we can't keep that
184 ;; character in the output because it can't be
185 ;; distinguished with the normal U+FEFF. So, we keep
186 ;; #xFFFF instead.
187 ((r1 = #xFFFF)
188 ,@utf-16-decode-ucs)
189 ;; R1 a normal Unicode character.
190 (,@utf-16-decode-ucs
191 (translate-character utf-translation-table-for-decode r0 r1)))
192 (write-multibyte-character r0 r1)
65a0e5fe
KH
193 ,utf-16be-decode-loop))))
194 "Like ccl-decode-utf-16be/le but check the first BOM.")
4fbc4b17 195
fc2938d1 196(makunbound 'utf-16-decode-ucs) ; done with it
65a0e5fe
KH
197(makunbound 'utf-16le-decode-loop)
198(makunbound 'utf-16be-decode-loop)
fc2938d1
DL
199
200(eval-and-compile
201(defconst utf-16-decode-to-ucs
202 ;; CCL which, given the result of a multibyte read in r0 and r1,
203 ;; sets r0 to the character's Unicode if the charset is one of the
204 ;; basic utf-8 coding system ones. Otherwise set to U+fffd.
205 `(if (r0 == ,(charset-id 'ascii))
206 (r0 = r1)
207 (if (r0 == ,(charset-id 'latin-iso8859-1))
208 (r0 = (r1 + 128))
209 (if (r0 == ,(charset-id 'eight-bit-control))
210 (r0 = r1)
211 (if (r0 == ,(charset-id 'eight-bit-graphic))
212 (r0 = r1)
213 ((r2 = (r1 & #x7f))
214 (r1 >>= 7)
215 (r3 = ((r1 - 32) * 96))
216 (r3 += (r2 - 32))
217 (if (r0 == ,(charset-id 'mule-unicode-0100-24ff))
218 (r0 = (r3 + #x100))
219 (if (r0 == ,(charset-id 'mule-unicode-2500-33ff))
220 (r0 = (r3 + #x2500))
221 (if (r0 == ,(charset-id 'mule-unicode-e000-ffff))
222 (r0 = (r3 + #xe000))
4fbc4b17
KH
223 (r0 = #xfffd))))))))))
224
65a0e5fe 225(defconst utf-16le-encode-loop
4fbc4b17
KH
226 `(loop
227 (read-multibyte-character r0 r1)
228 (lookup-character utf-subst-table-for-encode r0 r1)
229 (if (r7 == 0)
230 ((translate-character utf-translation-table-for-encode r0 r1)
231 ,utf-16-decode-to-ucs))
232 (write (r0 & 255))
233 (write (r0 >> 8))
234 (repeat)))
235
65a0e5fe 236(defconst utf-16be-encode-loop
4fbc4b17
KH
237 `(loop
238 (read-multibyte-character r0 r1)
239 (lookup-character utf-subst-table-for-encode r0 r1)
240 (if (r7 == 0)
241 ((translate-character utf-translation-table-for-encode r0 r1)
242 ,utf-16-decode-to-ucs))
243 (write (r0 >> 8))
244 (write (r0 & 255))
245 (repeat)))
246)
fc2938d1 247
65a0e5fe
KH
248
249(define-ccl-program ccl-encode-mule-utf-16le
fc2938d1 250 `(1
65a0e5fe 251 ,utf-16le-encode-loop)
2217b8e1 252 "Encode to UTF-16LE (little endian without signature).
fc2938d1
DL
253Characters from the charsets ascii, eight-bit-control,
254eight-bit-graphic, latin-iso8859-1 and mule-unicode-* are encoded
278ce936
KH
255after translation through the translation-table of name
256`utf-translation-table-for-encode'.
fc2938d1
DL
257Others are encoded as U+FFFD.")
258
65a0e5fe 259(define-ccl-program ccl-encode-mule-utf-16be
fc2938d1 260 `(1
65a0e5fe 261 ,utf-16be-encode-loop)
2217b8e1 262 "Encode to UTF-16BE (big endian without signature).
fc2938d1
DL
263Characters from the charsets ascii, eight-bit-control,
264eight-bit-graphic, latin-iso8859-1 and mule-unicode-* are encoded
278ce936
KH
265after translation through the translation-table named
266`utf-translation-table-for-encode'.
fc2938d1
DL
267Others are encoded as U+FFFD.")
268
65a0e5fe 269(define-ccl-program ccl-encode-mule-utf-16le-with-signature
4fbc4b17
KH
270 `(1
271 ((write #xFF)
272 (write #xFE)
65a0e5fe 273 ,utf-16le-encode-loop))
4fbc4b17
KH
274 "Encode to UTF-16 (little endian with signature).
275Characters from the charsets ascii, eight-bit-control,
276eight-bit-graphic, latin-iso8859-1 and mule-unicode-* are encoded
277after translation through the translation-table of name
278`utf-translation-table-for-encode'.
279Others are encoded as U+FFFD.")
280
65a0e5fe 281(define-ccl-program ccl-encode-mule-utf-16be-with-signature
4fbc4b17
KH
282 `(1
283 ((write #xFE)
284 (write #xFF)
65a0e5fe 285 ,utf-16be-encode-loop))
4fbc4b17
KH
286 "Encode to UTF-16 (big endian with signature).
287Characters from the charsets ascii, eight-bit-control,
288eight-bit-graphic, latin-iso8859-1 and mule-unicode-* are encoded
289after translation through the translation-table named
290`utf-translation-table-for-encode'.
291Others are encoded as U+FFFD.")
292
fc2938d1 293(makunbound 'utf-16-decode-to-ucs)
65a0e5fe
KH
294(makunbound 'utf-16le-encode-loop)
295(makunbound 'utf-16be-encode-loop)
4fbc4b17
KH
296
297(defun mule-utf-16-post-read-conversion (length)
298 (when (> length 0)
299 (let ((char (following-char)))
300 (cond ((= char (decode-char 'ucs #xFFFE))
301 (delete-char 1)
302 (setq last-coding-system-used
303 (coding-system-change-text-conversion
304 last-coding-system-used
65a0e5fe 305 'mule-utf-16le-with-signature))
4fbc4b17
KH
306 (setq length (1- length)))
307 ((= char (decode-char 'ucs #xFFFF))
308 (delete-char 1)
309 (setq last-coding-system-used
310 (coding-system-change-text-conversion
311 last-coding-system-used
65a0e5fe 312 'mule-utf-16be-with-signature))
4fbc4b17
KH
313 (setq length (1- length)))
314 (t
65a0e5fe 315 (setq last-coding-system-used 'mule-utf-16be)))))
4fbc4b17 316 length)
fc2938d1 317
fc2938d1
DL
318(let ((doc "
319
278ce936
KH
320It supports Unicode characters of these ranges:
321 U+0000..U+33FF, U+E000..U+FFFF.
322They correspond to these Emacs character sets:
323 ascii, latin-iso8859-1, mule-unicode-0100-24ff,
324 mule-unicode-2500-33ff, mule-unicode-e000-ffff
325
326On decoding (e.g. reading a file), Unicode characters not in the above
327ranges are decoded as U+FFFD, effectively corrupting the data
a1506d29 328if they are re-encoded.
278ce936
KH
329
330On encoding (e.g. writing a file), Emacs characters not belonging to
331any of the character sets listed above are encoded into the byte
332sequence representing U+FFFD (REPLACEMENT CHARACTER)."))
fc2938d1 333 (make-coding-system
65a0e5fe 334 'mule-utf-16le 4
fc2938d1
DL
335 ?u ; Mule-UCS uses ?U, but code-pages uses that for koi8-u.
336 (concat
65a0e5fe 337 "UTF-16LE encoding for Emacs-supported Unicode characters."
fc2938d1
DL
338 doc)
339
65a0e5fe 340 '(ccl-decode-mule-utf-16le . ccl-encode-mule-utf-16le)
fc2938d1
DL
341 '((safe-charsets
342 ascii
343 eight-bit-control
344 latin-iso8859-1
345 mule-unicode-0100-24ff
346 mule-unicode-2500-33ff
347 mule-unicode-e000-ffff)
348 (mime-charset . utf-16le)
cbcd4dc9 349 (mime-text-unsuitable . t)
fc2938d1 350 (valid-codes (0 . 255))
278ce936
KH
351 (dependency unify-8859-on-encoding-mode
352 unify-8859-on-decoding-mode
353 utf-fragment-on-decoding
354 utf-translate-cjk)))
fc2938d1
DL
355
356 (make-coding-system
65a0e5fe 357 'mule-utf-16be 4 ?u
fc2938d1 358 (concat
65a0e5fe 359 "UTF-16BE encoding for Emacs-supported Unicode characters."
fc2938d1
DL
360 doc)
361
65a0e5fe 362 '(ccl-decode-mule-utf-16be . ccl-encode-mule-utf-16be)
fc2938d1
DL
363 '((safe-charsets
364 ascii
365 eight-bit-control
366 latin-iso8859-1
367 mule-unicode-0100-24ff
368 mule-unicode-2500-33ff
369 mule-unicode-e000-ffff)
370 (mime-charset . utf-16be)
4fbc4b17
KH
371 (valid-codes (0 . 255))
372 (dependency unify-8859-on-encoding-mode
373 unify-8859-on-decoding-mode
374 utf-fragment-on-decoding
375 utf-translate-cjk)))
376
377 (make-coding-system
65a0e5fe 378 'mule-utf-16le-with-signature 4 ?u
4fbc4b17
KH
379 (concat
380 "Little endian UTF-16 (with BOM) for Emacs-supported Unicode characters."
381 doc)
382
65a0e5fe
KH
383 '(ccl-decode-mule-utf-16le-with-signature
384 . ccl-encode-mule-utf-16le-with-signature)
4fbc4b17
KH
385 '((safe-charsets
386 ascii
387 eight-bit-control
388 latin-iso8859-1
389 mule-unicode-0100-24ff
390 mule-unicode-2500-33ff
391 mule-unicode-e000-ffff)
392 (coding-category . coding-category-utf-16-le)
393 (mime-charset . utf-16)
cbcd4dc9 394 (mime-text-unsuitable . t)
4fbc4b17
KH
395 (valid-codes (0 . 255))
396 (dependency unify-8859-on-encoding-mode
397 unify-8859-on-decoding-mode
398 utf-fragment-on-decoding
399 utf-translate-cjk)))
400
401 (make-coding-system
65a0e5fe 402 'mule-utf-16be-with-signature 4 ?u
4fbc4b17
KH
403 (concat
404 "Big endian UTF-16 (with BOM) for Emacs-supported Unicode characters."
405 doc)
406
65a0e5fe
KH
407 '(ccl-decode-mule-utf-16be-with-signature
408 . ccl-encode-mule-utf-16be-with-signature)
4fbc4b17
KH
409 '((safe-charsets
410 ascii
411 eight-bit-control
412 latin-iso8859-1
413 mule-unicode-0100-24ff
414 mule-unicode-2500-33ff
415 mule-unicode-e000-ffff)
416 (coding-category . coding-category-utf-16-be)
417 (mime-charset . utf-16)
418 (valid-codes (0 . 255))
419 (dependency unify-8859-on-encoding-mode
420 unify-8859-on-decoding-mode
421 utf-fragment-on-decoding
422 utf-translate-cjk)))
423
424 (make-coding-system
425 'mule-utf-16 4 ?u
426 (concat
427 "UTF-16 (with or without BOM) for Emacs-supported Unicode characters."
428 doc)
429
65a0e5fe 430 '(ccl-decode-mule-utf-16 . ccl-encode-mule-utf-16be-with-signature)
4fbc4b17
KH
431 '((safe-charsets
432 ascii
433 eight-bit-control
434 latin-iso8859-1
435 mule-unicode-0100-24ff
436 mule-unicode-2500-33ff
437 mule-unicode-e000-ffff)
fc2938d1 438 (coding-category . coding-category-utf-16-be)
4fbc4b17 439 (mime-charset . utf-16)
cbcd4dc9 440 (mime-text-unsuitable . t)
fc2938d1 441 (valid-codes (0 . 255))
278ce936
KH
442 (dependency unify-8859-on-encoding-mode
443 unify-8859-on-decoding-mode
444 utf-fragment-on-decoding
4fbc4b17
KH
445 utf-translate-cjk)
446 (post-read-conversion . mule-utf-16-post-read-conversion)))
447)
fc2938d1 448
65a0e5fe
KH
449(define-coding-system-alias 'utf-16le 'mule-utf-16le)
450(define-coding-system-alias 'utf-16be 'mule-utf-16be)
451(define-coding-system-alias 'utf-16le-with-signature
452 'mule-utf-16le-with-signature)
453(define-coding-system-alias 'utf-16be-with-signature
454 'mule-utf-16be-with-signature)
4fbc4b17 455(define-coding-system-alias 'utf-16 'mule-utf-16)
fc2938d1 456
65a0e5fe
KH
457;; For backward compatibility.
458(define-coding-system-alias 'mule-utf-16-le 'mule-utf-16le-with-signature)
459(define-coding-system-alias 'utf-16-le 'mule-utf-16le-with-signature)
460(define-coding-system-alias 'mule-utf-16-be 'mule-utf-16be-with-signature)
461(define-coding-system-alias 'utf-16-be 'mule-utf-16be-with-signature)
462
ab5796a9 463;;; arch-tag: 85455d46-d9c9-466d-a6f3-c3582a7367c4
fc2938d1 464;;; utf-16.el ends here