An electric test is now passing
[emacs.git] / lisp / international / utf7.el
blob73ee5ad27c368116789106fdddafc377b72c4d5c
1 ;;; utf7.el --- UTF-7 encoding/decoding for Emacs -*- lexical-binding:t -*-
3 ;; Copyright (C) 1999-2019 Free Software Foundation, Inc.
5 ;; Author: Jon K Hellan <hellan@acm.org>
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; Keywords: mail
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; UTF-7 - A Mail-Safe Transformation Format of Unicode - RFC 2152
27 ;; This is a transformation format of Unicode that contains only 7-bit
28 ;; ASCII octets and is intended to be readable by humans in the limiting
29 ;; case that the document consists of characters from the US-ASCII
30 ;; repertoire.
31 ;; In short, runs of characters outside US-ASCII are encoded as base64
32 ;; inside delimiters.
33 ;; A variation of UTF-7 is specified in IMAP 4rev1 (RFC 2060) as the way
34 ;; to represent characters outside US-ASCII in mailbox names in IMAP.
35 ;; This library supports both variants, but the IMAP variation was the
36 ;; reason I wrote it.
37 ;; The routines convert UTF-7 -> UTF-16 (16 bit encoding of Unicode)
38 ;; -> current character set, and vice versa.
39 ;; However, until Emacs supports Unicode, the only Emacs character set
40 ;; supported here is ISO-8859.1, which can trivially be converted to/from
41 ;; Unicode.
42 ;; When decoding results in a character outside the Emacs character set,
43 ;; an error is thrown. It is up to the application to recover.
45 ;; UTF-7 should be done by providing a coding system. Mule-UCS does
46 ;; already, but I don't know if it does the IMAP version and it's not
47 ;; clear whether that should really be a coding system. The UTF-16
48 ;; part of the conversion can be done with coding systems available
49 ;; with Mule-UCS or some versions of Emacs. Unfortunately these were
50 ;; done wrongly (regarding handling of byte-order marks and how the
51 ;; variants were named), so we don't have a consistent name for the
52 ;; necessary coding system. The code below doesn't seem to DTRT
53 ;; generally. E.g.:
55 ;; (utf7-encode "a+£")
56 ;; => "a+ACsAow-"
58 ;; $ echo "a+£"|iconv -f utf-8 -t utf-7
59 ;; a+-+AKM
61 ;; -- fx
64 ;;; Code:
66 (require 'base64)
67 (require 'mm-util)
69 (defconst utf7-direct-encoding-chars " -%'-*,-[]-}"
70 "Character ranges which do not need escaping in UTF-7.")
72 (defconst utf7-imap-direct-encoding-chars
73 (concat utf7-direct-encoding-chars "+\\~")
74 "Character ranges which do not need escaping in the IMAP variant of UTF-7.")
76 (defconst utf7-utf-16-coding-system
77 (cond ((mm-coding-system-p 'utf-16-be-no-signature) ; Mule-UCS
78 'utf-16-be-no-signature)
79 ((and (mm-coding-system-p 'utf-16-be) ; Emacs
80 ;; Avoid versions with BOM.
81 (= 2 (length (encode-coding-string "a" 'utf-16-be))))
82 'utf-16-be)
83 ((mm-coding-system-p 'utf-16-be-nosig) ; ?
84 'utf-16-be-nosig))
85 "Coding system which encodes big endian UTF-16 without a BOM signature.")
87 (defsubst utf7-imap-get-pad-length (len modulus)
88 "Return required length of padding for IMAP modified base64 fragment."
89 (mod (- len) modulus))
91 (defun utf7-encode-internal (&optional for-imap)
92 "Encode text in (temporary) buffer as UTF-7.
93 Use IMAP modification if FOR-IMAP is non-nil."
94 (let ((start (point-min))
95 (end (point-max)))
96 (narrow-to-region start end)
97 (goto-char start)
98 (let* ((esc-char (if for-imap ?& ?+))
99 (direct-encoding-chars
100 (if for-imap utf7-imap-direct-encoding-chars
101 utf7-direct-encoding-chars))
102 (not-direct-encoding-chars (concat "^" direct-encoding-chars)))
103 (while (not (eobp))
104 (skip-chars-forward direct-encoding-chars)
105 (unless (eobp)
106 (insert esc-char)
107 (let ((p (point))
108 (fc (following-char))
109 (run-length
110 (skip-chars-forward not-direct-encoding-chars)))
111 (if (and (= fc esc-char)
112 (= run-length 1)) ; Lone esc-char?
113 (delete-char -1) ; Now there's one too many
114 (utf7-fragment-encode p (point) for-imap))
115 (insert "-")))))))
117 (defun utf7-fragment-encode (start end &optional for-imap)
118 "Encode text from START to END in buffer as UTF-7 escape fragment.
119 Use IMAP modification if FOR-IMAP is non-nil."
120 (save-restriction
121 (let* ((buf (current-buffer))
122 (base (with-temp-buffer
123 (set-buffer-multibyte nil)
124 (insert-buffer-substring buf start end)
125 (funcall (utf7-get-u16char-converter 'to-utf-16))
126 (base64-encode-region (point-min) (point-max))
127 (buffer-string))))
128 (narrow-to-region start end)
129 (delete-region (point-min) (point-max))
130 (insert base))
131 (goto-char (point-min))
132 (let ((pm (point-max)))
133 (when for-imap
134 (while (search-forward "/" nil t)
135 (replace-match ",")))
136 (skip-chars-forward "^= \t\n" pm)
137 (delete-region (point) pm))))
139 (defun utf7-decode-internal (&optional for-imap)
140 "Decode UTF-7 text in (temporary) buffer.
141 Use IMAP modification if FOR-IMAP is non-nil."
142 (let ((start (point-min)))
143 (goto-char start)
144 (let* ((esc-pattern (concat "^" (char-to-string (if for-imap ?& ?+))))
145 (base64-chars (concat "A-Za-z0-9+"
146 (char-to-string (if for-imap ?, ?/)))))
147 (while (not (eobp))
148 (skip-chars-forward esc-pattern)
149 (unless (eobp)
150 (forward-char)
151 (let ((p (point))
152 (run-length (skip-chars-forward base64-chars)))
153 (when (and (not (eobp)) (= (following-char) ?-))
154 (delete-char 1))
155 (unless (= run-length 0) ; Encoded lone esc-char?
156 (save-excursion
157 (utf7-fragment-decode p (point) for-imap)
158 (goto-char p)
159 (delete-char -1)))))))))
161 (defun utf7-fragment-decode (start end &optional for-imap)
162 "Decode base64 encoded fragment from START to END of UTF-7 text in buffer.
163 Use IMAP modification if FOR-IMAP is non-nil."
164 (save-restriction
165 (narrow-to-region start end)
166 (when for-imap
167 (goto-char start)
168 (while (search-forward "," nil 'move-to-end) (replace-match "/")))
169 (let ((pl (utf7-imap-get-pad-length (- end start) 4)))
170 (insert (make-string pl ?=))
171 (base64-decode-region start (+ end pl)))
172 (funcall (utf7-get-u16char-converter 'from-utf-16))))
174 (defun utf7-get-u16char-converter (which-way)
175 "Return a function to convert between UTF-16 and current character set."
176 (if utf7-utf-16-coding-system
177 (if (eq which-way 'to-utf-16)
178 (lambda ()
179 (encode-coding-region (point-min) (point-max)
180 utf7-utf-16-coding-system))
181 (lambda ()
182 (decode-coding-region (point-min) (point-max)
183 utf7-utf-16-coding-system)))
184 ;; Add test to check if we are really Latin-1.
185 (if (eq which-way 'to-utf-16)
186 'utf7-latin1-u16-char-converter
187 'utf7-u16-latin1-char-converter)))
189 (defun utf7-latin1-u16-char-converter ()
190 "Convert latin 1 (ISO-8859.1) characters to 16 bit Unicode.
191 Characters are converted to raw byte pairs in narrowed buffer."
192 (encode-coding-region (point-min) (point-max) 'iso-8859-1)
193 (goto-char (point-min))
194 (while (not (eobp))
195 (insert 0)
196 (forward-char)))
198 (defun utf7-u16-latin1-char-converter ()
199 "Convert 16 bit Unicode characters to latin 1 (ISO-8859.1).
200 Characters are in raw byte pairs in narrowed buffer."
201 (goto-char (point-min))
202 (while (not (eobp))
203 (if (= 0 (following-char))
204 (delete-char 1)
205 (error "Unable to convert from Unicode"))
206 (forward-char))
207 (decode-coding-region (point-min) (point-max) 'iso-8859-1)
208 (mm-enable-multibyte))
210 ;;;###autoload
211 (defun utf7-encode (string &optional for-imap)
212 "Encode UTF-7 STRING. Use IMAP modification if FOR-IMAP is non-nil."
213 (if (and (coding-system-p 'utf-7) (coding-system-p 'utf-7-imap))
214 ;; Emacs 23 with proper support for IMAP
215 (encode-coding-string string (if for-imap 'utf-7-imap 'utf-7))
216 (mm-with-multibyte-buffer
217 (insert string)
218 (utf7-encode-internal for-imap)
219 (buffer-string))))
221 (defun utf7-decode (string &optional for-imap)
222 "Decode UTF-7 STRING. Use IMAP modification if FOR-IMAP is non-nil."
223 (if (and (coding-system-p 'utf-7) (coding-system-p 'utf-7-imap))
224 ;; Emacs 23 with proper support for IMAP
225 (decode-coding-string string (if for-imap 'utf-7-imap 'utf-7))
226 (mm-with-unibyte-buffer
227 (insert string)
228 (utf7-decode-internal for-imap)
229 (mm-enable-multibyte)
230 (buffer-string))))
232 (provide 'utf7)
234 ;;; utf7.el ends here