1 ;;; utf-7.el --- utf-7 coding system
3 ;; Copyright (C) 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
5 ;; Author: Dave Love <fx@gnu.org>
6 ;; Keywords: i18n, mail
8 ;; This file is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; This file is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING. If not, write to
20 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 ;; Boston, MA 02110-1301, USA.
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 behaviour. 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 ;; (define-coding-system 'utf-7-imap
40 ;; "UTF-7 encoding of Unicode, IMAP version (RFC 2060)"
41 ;; :coding-type 'utf-8
43 ;; :charset-list '(unicode)
44 ;; :pre-write-conversion 'utf-7-imap-pre-write-conversion
45 ;; :post-read-conversion 'utf-7-imap-post-read-conversion)
47 (defun utf-7-decode (len imap
)
48 "Decode LEN bytes of UTF-7 at point.
49 IMAP non-nil means use the IMAP version."
52 (narrow-to-region (point) (+ (point) len
))
53 (let ((not-esc (if imap
"^&" "^+"))
54 (skip-chars (if imap
"A-Za-z0-9+," "A-Za-z0-9+/")))
56 (skip-chars-forward not-esc
)
60 (run-length (skip-chars-forward skip-chars
)))
61 (if (eq ?-
(char-after))
63 (unless (= run-length
0) ; encoded lone esc-char
64 (let ((pl (mod (- run-length
) 4)))
67 (subst-char-in-region p
(point) ?
, ?
/))
68 (base64-decode-region p
(point)))
69 (decode-coding-region p
(point) 'utf-16be
)
72 (delete-backward-char 1)))))))
73 (- (point-max) (point-min)))))
76 (defun utf-7-post-read-conversion (len)
77 (utf-7-decode len nil
))
79 ;; (defun utf-7-imap-post-read-conversion (len)
80 ;; (utf-7-decode len t))
82 (defun utf-7-encode (from to imap
)
83 "Encode bytes between FROM and TO to UTF-7.
84 ESC and SKIP-CHARS are adjusted for the normal and IMAP versions."
85 (let* ((old-buf (current-buffer))
87 ;; These are characters which can be encoded asis.
89 "\t\n\r\x20-\x25\x27-\x7e" ; rfc2060
90 ;; This includes the rfc2152 optional set.
91 ;; Perhaps it shouldn't (like iconv).
93 (not-skip-chars (format "^%s%c" skip-chars esc
)))
94 (set-buffer (generate-new-buffer " *temp*"))
97 (insert-buffer-substring old-buf from to
))
98 (goto-char (point-min))
100 (skip-chars-forward skip-chars
)
101 (if (eq ?
+ (char-after))
102 (progn (forward-char)
107 (skip-chars-forward not-skip-chars
)
109 ;; encode-coding-region doesn't preserve point
110 (narrow-to-region p
(point))
111 (encode-coding-region p
(point-max) 'utf-16be
)
112 (base64-encode-region p
(point-max))
114 (subst-char-in-region p
(point-max) ?
/ ?
,))
116 ;; As I read the RFC, this isn't correct, but it's
117 ;; consistent with iconv, at least regarding `='.
118 (skip-chars-forward "^= \t\n")
119 (delete-region (point) (point-max))))
125 (defun utf-7-pre-write-conversion (from to
)
126 (utf-7-encode from to nil
))
128 ;; (defun utf-7-imap-pre-write-conversion (from to)
129 ;; (utf-7-encode from to t))
133 ;;; arch-tag: 975ee403-90a4-4286-97d2-4ed1323f4ef9
134 ;;; utf-7.el ends here