1 ;;; utf7.el --- UTF-7 encoding/decoding for Emacs -*-coding: iso-8859-1;-*-
3 ;; Copyright (C) 1999, 2000, 2002, 2003, 2004,
4 ;; 2005, 2006 Free Software Foundation, Inc.
6 ;; Author: Jon K Hellan <hellan@acm.org>
7 ;; Maintainer: bugs@gnus.org
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 2, or (at your option)
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; see the file COPYING. If not, write to
24 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
29 ;; UTF-7 - A Mail-Safe Transformation Format of Unicode - RFC 2152
30 ;; This is a transformation format of Unicode that contains only 7-bit
31 ;; ASCII octets and is intended to be readable by humans in the limiting
32 ;; case that the document consists of characters from the US-ASCII
34 ;; In short, runs of characters outside US-ASCII are encoded as base64
36 ;; A variation of UTF-7 is specified in IMAP 4rev1 (RFC 2060) as the way
37 ;; to represent characters outside US-ASCII in mailbox names in IMAP.
38 ;; This library supports both variants, but the IMAP variation was the
40 ;; The routines convert UTF-7 -> UTF-16 (16 bit encoding of Unicode)
41 ;; -> current character set, and vice versa.
42 ;; However, until Emacs supports Unicode, the only Emacs character set
43 ;; supported here is ISO-8859.1, which can trivially be converted to/from
45 ;; When decoding results in a character outside the Emacs character set,
46 ;; an error is thrown. It is up to the application to recover.
48 ;; UTF-7 should be done by providing a coding system. Mule-UCS does
49 ;; already, but I don't know if it does the IMAP version and it's not
50 ;; clear whether that should really be a coding system. The UTF-16
51 ;; part of the conversion can be done with coding systems available
52 ;; with Mule-UCS or some versions of Emacs. Unfortunately these were
53 ;; done wrongly (regarding handling of byte-order marks and how the
54 ;; variants were named), so we don't have a consistent name for the
55 ;; necessary coding system. The code below doesn't seem to DTRT
58 ;; (utf7-encode "a+£")
61 ;; $ echo "a+£"|iconv -f iso-8859-1 -t utf-7
70 (eval-when-compile (require 'cl))
73 (defconst utf7-direct-encoding-chars " -%'-*,-[]-}"
74 "Character ranges which do not need escaping in UTF-7.")
76 (defconst utf7-imap-direct-encoding-chars
77 (concat utf7-direct-encoding-chars "+\\~")
78 "Character ranges which do not need escaping in the IMAP variant of UTF-7.")
80 (defconst utf7-utf-16-coding-system
81 (cond ((mm-coding-system-p 'utf-16-be-no-signature) ; Mule-UCS
82 'utf-16-be-no-signature)
83 ((and (mm-coding-system-p 'utf-16-be) ; Emacs 21.3, Emacs 22
84 ;; Avoid versions with BOM.
85 (= 2 (length (encode-coding-string "a" 'utf-16-be))))
87 ((mm-coding-system-p 'utf-16-be-nosig) ; ?
89 "Coding system which encodes big endian UTF-16 without a BOM signature.")
91 (defsubst utf7-imap-get-pad-length (len modulus)
92 "Return required length of padding for IMAP modified base64 fragment."
93 (mod (- len) modulus))
95 (defun utf7-encode-internal (&optional for-imap)
96 "Encode text in (temporary) buffer as UTF-7.
97 Use IMAP modification if FOR-IMAP is non-nil."
98 (let ((start (point-min))
100 (narrow-to-region start end)
102 (let* ((esc-char (if for-imap ?& ?+))
103 (direct-encoding-chars
104 (if for-imap utf7-imap-direct-encoding-chars
105 utf7-direct-encoding-chars))
106 (not-direct-encoding-chars (concat "^" direct-encoding-chars)))
108 (skip-chars-forward direct-encoding-chars)
112 (fc (following-char))
114 (skip-chars-forward not-direct-encoding-chars)))
115 (if (and (= fc esc-char)
116 (= run-length 1)) ; Lone esc-char?
117 (delete-backward-char 1) ; Now there's one too many