1 ;;; utf-7.el --- utf-7 coding system
3 ;; Copyright (C) 2003-2015 Free Software Foundation, Inc.
5 ;; Author: Dave Love <fx@gnu.org>
6 ;; Keywords: i18n, mail
8 ;; This file is part of GNU Emacs.
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 3 of the License, or
13 ;; (at your option) any later version.
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.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; Defines a coding system for UTF-7, defined in RFC 2152. Non-ASCII
26 ;; segments are encoded as base64-encoded big endian UTF-16. Also
27 ;; defines a variation required for IMAP (RFC 2060).
29 ;; The encoding and decoding was originally taken from Jon K Hellan's
30 ;; implementation in Gnus, but has been substantially re-done.
32 ;; This probably needs more attention. In particular, it's not
33 ;; completely consistent with iconv's behavior. It's arguable
34 ;; whether the IMAP version should be a coding system since it's
35 ;; apparently only used for IMAP mailbox names, so it's commented out.
39 (defun utf-7-decode (len imap
)
40 "Decode LEN bytes of UTF-7 at point.
41 IMAP non-nil means use the IMAP version."
44 (narrow-to-region (point) (+ (point) len
))
45 (let ((not-esc (if imap
"^&" "^+"))
46 (skip-chars (if imap
"A-Za-z0-9+," "A-Za-z0-9+/")))
48 (skip-chars-forward not-esc
)
52 (run-length (skip-chars-forward skip-chars
)))
53 (if (eq ?-
(char-after))
55 (unless (= run-length
0) ; encoded lone esc-char
56 (let ((pl (mod (- run-length
) 4)))
59 (subst-char-in-region p
(point) ?
, ?
/))
60 (base64-decode-region p
(point)))
61 (decode-coding-region p
(point) 'utf-16be
)
64 (delete-char -
1)))))))
65 (- (point-max) (point-min)))))
68 (defun utf-7-post-read-conversion (len)
69 (utf-7-decode len nil
))
72 (defun utf-7-imap-post-read-conversion (len)
75 (defun utf-7-encode (from to imap
)
76 "Encode bytes between FROM and TO to UTF-7.
77 ESC and SKIP-CHARS are adjusted for the normal and IMAP versions."
78 (let* ((old-buf (current-buffer))
80 ;; These are characters which can be encoded asis.
82 "\t\n\r\x20-\x25\x27-\x7e" ; rfc2060
83 ;; This includes the rfc2152 optional set.
84 ;; Perhaps it shouldn't (like iconv).
86 (not-skip-chars (format "^%s%c" skip-chars esc
)))
87 (set-buffer (generate-new-buffer " *temp*"))
90 (insert-buffer-substring old-buf from to
))
91 (goto-char (point-min))
93 (skip-chars-forward skip-chars
)
94 (if (eq esc
(char-after))
100 (skip-chars-forward not-skip-chars
)
102 ;; encode-coding-region doesn't preserve point
103 (narrow-to-region p
(point))
104 (encode-coding-region p
(point-max) 'utf-16be
)
105 (base64-encode-region p
(point-max))
107 (subst-char-in-region p
(point-max) ?
/ ?
,))
109 ;; As I read the RFC, this isn't correct, but it's
110 ;; consistent with iconv, at least regarding `='.
111 (skip-chars-forward "^= \t\n")
112 (delete-region (point) (point-max))))
113 ;; RFC2060 stipulates that all names MUST end in US-ASCII (i.e.
114 ;; a name that ends with a Unicode octet MUST end with a "-").
115 (if (or imap
(not (eobp)))
120 (defun utf-7-pre-write-conversion (from to
)
121 (utf-7-encode from to nil
))
124 (defun utf-7-imap-pre-write-conversion (from to
)
125 (utf-7-encode from to t
))
129 ;;; utf-7.el ends here