1 ;;; utf-7.el --- utf-7 coding system
3 ;; Copyright (C) 2003 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., 59 Temple Place - Suite 330,
21 ;; Boston, MA 02111-1307, 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.
41 "UTF-7 encoding of Unicode (RFC 2152)"
43 `((safe-chars .
,(coding-system-get 'utf-16be
'safe-chars
))
44 (mime-charset . utf-7
)
45 (pre-write-conversion . utf-7-pre-write-conversion
)
46 (post-read-conversion . utf-7-post-read-conversion
)))
48 ;; (make-coding-system
50 ;; "UTF-7 encoding of Unicode, IMAP version (RFC 2060)"
52 ;; `((safe-chars . ,(coding-system-get 'utf-16be 'safe-chars))
53 ;; (pre-write-conversion . utf-7-imap-pre-write-conversion)
54 ;; (post-read-conversion . utf-7-imap-post-read-conversion)))
56 (defun utf-7-decode (len imap
)
57 "Decode LEN bytes of UTF-7 at point.
58 IMAP non-nil means use the IMAP version."
61 (narrow-to-region (point) (+ (point) len
))
62 (let ((not-esc (if imap
"^&" "^+"))
63 (skip-chars (if imap
"A-Za-z0-9+," "A-Za-z0-9+/")))
65 (skip-chars-forward not-esc
)
69 (run-length (skip-chars-forward skip-chars
)))
70 (if (eq ?-
(char-after))
72 (unless (= run-length
0) ; encoded lone esc-char
73 (let ((pl (mod (- run-length
) 4)))
76 (subst-char-in-region p
(point) ?
, ?
/))
77 (base64-decode-region p
(point)))
78 (decode-coding-region p
(point) 'utf-16be
)
81 (delete-backward-char 1)))))))
82 (- (point-max) (point-min)))))
84 (defun utf-7-post-read-conversion (len)
85 (utf-7-decode len nil
))
87 ;; (defun utf-7-imap-post-read-conversion (len)
88 ;; (utf-7-decode len t))
90 (defun utf-7-encode (from to imap
)
91 "Encode bytes between FROM and TO to UTF-7.
92 ESC and SKIP-CHARS are adjusted for the normal and IMAP versions."
93 (let* ((old-buf (current-buffer))
95 ;; These are characters which can be encoded asis.
97 "\t\n\r\x20-\x25\x27-\x7e" ; rfc2060
98 ;; This includes the rfc2152 optional set.
99 ;; Perhaps it shouldn't (like iconv).
101 (not-skip-chars (format "^%s%c" skip-chars esc
)))
102 (set-buffer (generate-new-buffer " *temp*"))
105 (insert-buffer-substring old-buf from to
))
106 (goto-char (point-min))
108 (skip-chars-forward skip-chars
)
109 (if (eq ?
+ (char-after))
110 (progn (forward-char)
115 (skip-chars-forward not-skip-chars
)
117 ;; encode-coding-region doesn't preserve point
118 (narrow-to-region p
(point))
119 (encode-coding-region p
(point-max) 'utf-16be
)
120 (base64-encode-region p
(point-max))
122 (subst-char-in-region p
(point-max) ?
/ ?
,))
124 ;; As I read the RFC, this isn't correct, but it's
125 ;; consistent with iconv, at least regarding `='.
126 (skip-chars-forward "^= \t\n")
127 (delete-region (point) (point-max))))
132 (defun utf-7-pre-write-conversion (from to
)
133 (utf-7-encode from to nil
))
135 ;; (defun utf-7-imap-pre-write-conversion (from to)
136 ;; (utf-7-encode from to t))
139 ;;; utf-7.el ends here