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