1 ;;; utf7.el --- UTF-7 encoding/decoding for Emacs
2 ;; Copyright (C) 1999 Free Software Foundation, Inc.
4 ;; Author: Jon K Hellan <hellan@item.ntnu.no>
7 ;; This file is part of GNU Emacs, but the same permissions apply
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to
21 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
25 ;;; UTF-7 - A Mail-Safe Transformation Format of Unicode - RFC 2152
26 ;;; This is a transformation format of Unicode that contains only 7-bit
27 ;;; ASCII octets and is intended to be readable by humans in the limiting
28 ;;; case that the document consists of characters from the US-ASCII
30 ;;; In short, runs of characters outside US-ASCII are encoded as base64
31 ;;; inside delimiters.
32 ;;; A variation of UTF-7 is specified in IMAP 4rev1 (RFC 2060) as the way
33 ;;; to represent characters outside US-ASCII in mailbox names in IMAP.
34 ;;; This library supports both variants, but the IMAP variation was the
35 ;;; reason I wrote it.
36 ;;; The routines convert UTF-7 -> UTF-16 (16 bit encoding of Unicode)
37 ;;; -> current character set, and vice versa.
38 ;;; However, until Emacs supports Unicode, the only Emacs character set
39 ;;; supported here is ISO-8859.1, which can trivially be converted to/from
41 ;;; When decoding results in a character outside the Emacs character set,
42 ;;; an error is thrown. It is up to the application to recover.
44 ;;; Modified 8 December 1999 by Yuuichi Teranishi so that it will work under
45 ;;; Emacs+Mule-UCS and XEmacs.
49 (eval-when-compile (require 'cl))
50 (eval-when-compile (require 'pces))
51 (eval-when-compile (require 'static))
54 ;;(static-if (and (fboundp 'base64-decode-region)
55 ;; (subrp (symbol-function 'base64-decode-region)))
56 ;; (eval-and-compile (fset 'utf7-base64-decode-region 'base64-decode-region))
58 ;; (defun utf7-base64-decode-region (start end)
59 ;; (fset 'utf7-base64-decode-string
60 ;; (symbol-function (mel-find-function 'mime-decode-region "base64")))
61 ;; (utf7-base64-decode-region start end)))
63 ;;(static-if (and (fboundp 'base64-encode-region)
64 ;; (subrp (symbol-function 'base64-encode-region)))
65 ;; (eval-and-compile (fset 'utf7-base64-encode-region 'base64-encode-region))
66 ;; (defun utf7-base64-encode-string (start end)
67 ;; (fset 'utf7-base64-encode-region
68 ;; (symbol-function (mel-find-function 'mime-encode-region "base64")))
69 ;; (utf7-base64-encode-region start end)))
71 ;; On XEmacs which does not support UTF-16 have to use u7tou8 and u8tou7.
72 ;; These programs are included in
73 ;; ftp://ftp.ifcss.org/pub/software/unix/convert/utf7.tar.gz
74 (defvar utf7-utf7-to-utf8-program "u7tou8"
75 "Program to convert utf7 to utf8.")
76 (defvar utf7-utf8-to-utf7-program "u8tou7"
77 "Program to convert utf8 to utf7.")
79 (defvar utf7-direct-encoding-chars " -%'-*,-[]-} \t\n\r"
80 "Characters ranges which do not need escaping in UTF-7")
81 (defvar utf7-imap-direct-encoding-chars
82 (concat utf7-direct-encoding-chars "+\\\\~")
83 "Characters ranges which do not need escaping in the IMAP modified variant of UTF-7")
85 (defsubst utf7-imap-get-pad-length (len modulus)
86 "Return required length of padding for IMAP modified base64 fragment."
87 (mod (- len) modulus))
90 ((and (featurep 'xemacs)
91 (module-installed-p 'xemacs-ucs))
92 (defun utf7-fragment-decode (start end &optional imap)
93 "Decode base64 encoded fragment from START to END of UTF-7 text in buffer.
94 Use IMAP modification if IMAP is non-nil."
97 (narrow-to-region start end)
100 (while (search-forward "," nil 'move-to-end) (replace-match "/")))
101 (goto-char (point-min))
104 (call-process-region (point-min) (point-max)
105 utf7-utf7-to-utf8-program
107 (decode-coding-region (point-min) (point-max) 'utf-8)))
109 (defun utf7-fragment-encode (start end &optional imap)
110 "Decode base64 encoded fragment from START to END of UTF-7 text in buffer.
111 Use IMAP modification if IMAP is non-nil."
112 (require 'xemacs-ucs)
113 (let ((buffer (current-buffer))
117 (insert-buffer-substring buffer start end)
119 (goto-char (point-max))
121 (encode-coding-region (point-min) (point-max) 'utf-8)
123 (call-process-region (point-min) (point-max)
124 utf7-utf8-to-utf7-program
126 (goto-char (point-min))
128 (skip-chars-forward "+")
129 (delete-region (point-min) (point))
131 (while (search-forward "/" nil t)
132 (replace-match ",")))
133 (goto-char (point-max))
134 (delete-backward-char 1)
137 (delete-region start end)
138 (insert encoded-string))))
139 ((module-installed-p 'un-define) ;; Emacs
140 (defun utf7-fragment-decode (start end &optional imap)
141 "Decode base64 encoded fragment from START to END of UTF-7 text in buffer.
142 Use IMAP modification if IMAP is non-nil."
145 (narrow-to-region start end)
146 (goto-char (point-min))
150 (while (search-forward "," nil 'move-to-end) (replace-match "/")))
151 (decode-coding-region (point-min) (point-max) 'utf-7)
154 (defun utf7-fragment-encode (start end &optional imap)
155 "Encode text from START to END in buffer as UTF-7 escape fragment.
156 Use IMAP modification if IMAP is non-nil."
158 (let ((buffer (current-buffer))
162 (insert-buffer-substring buffer start end)
163 (encode-coding-region (point-min)
165 (goto-char (point-min))
167 (skip-chars-forward "+")
168 (delete-region (point-min) (point))
170 (while (search-forward "/" nil t)
171 (replace-match ",")))
172 (skip-chars-forward "^= \t\n" (point-max))
173 (delete-region (point) (point-max))
175 (delete-region start end)
176 (insert encoded-string))))
178 ;; Define as null function.
179 (defun utf7-fragment-decode (start end &optional imap)
180 "Encode text from START to END in buffer as UTF-7 escape fragment.
181 Use IMAP modification if IMAP is non-nil."
184 (defun utf7-fragment-encode (start end &optional imap)
185 "Encode text from START to END in buffer as UTF-7 escape fragment.
186 Use IMAP modification if IMAP is non-nil."
189 (defun utf7-encode-region (start end &optional imap)
190 "Encode text in region as UTF-7.
191 Use IMAP modification if IMAP is non-nil."
194 ;;(set-buffer-multibyte default-enable-multibyte-characters)
195 (narrow-to-region start end)
197 (let ((esc-char (if imap ?& ?+))
198 (direct-encoding-chars
199 (if imap utf7-imap-direct-encoding-chars
200 utf7-direct-encoding-chars)))
202 (skip-chars-forward direct-encoding-chars)
205 (fc (following-char))
207 (skip-chars-forward (concat "^" direct-encoding-chars))))
208 (if (and (= fc esc-char)
209 (= run-length 1)) ; Lone esc-char?
210 (delete-backward-char 1) ; Now there's one too many
211 (utf7-fragment-encode p (point) imap))))))))
213 (defun utf7-decode-region (start end &optional imap)
214 "Decode UTF-7 text in region.
215 Use IMAP modification if IMAP is non-nil."
219 (let* ((esc-pattern (concat "^" (char-to-string (if imap ?& ?+))))
220 (base64-chars (concat "A-Za-z0-9+"
221 (char-to-string (if imap ?, ?/)))))
223 (skip-chars-forward esc-pattern)
227 (run-length (skip-chars-forward base64-chars)))
228 (when (and (not (eobp)) (= (following-char) ?-))
230 (unless (= run-length 0) ; Encoded lone esc-char?
232 (utf7-fragment-decode p (point) imap)
234 (delete-backward-char 1)))))))
235 ;;(set-buffer-multibyte default-enable-multibyte-characters)
238 (defun utf7-encode-string (string &optional imap)
239 "Encode UTF-7 string. Use IMAP modification if IMAP is non-nil."
242 (utf7-encode-region (point-min) (point-max) imap)
245 (defun utf7-decode-string (string &optional imap)
246 "Decode UTF-7 string. Use IMAP modification if IMAP is non-nil."
249 (utf7-decode-region (point-min) (point-max) imap)
252 ;; For compatibility.
253 (defalias 'utf7-decode 'utf7-decode-string)
254 (defalias 'utf7-encode 'utf7-encode-string)
258 ;;; utf7.el ends here