utf7.el 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. ;;; utf7.el --- UTF-7 encoding/decoding for Emacs -*-coding: iso-8859-1;-*-
  2. ;; Copyright (C) 1999-2012 Free Software Foundation, Inc.
  3. ;; Author: Jon K Hellan <hellan@acm.org>
  4. ;; Maintainer: bugs@gnus.org
  5. ;; Keywords: mail
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; UTF-7 - A Mail-Safe Transformation Format of Unicode - RFC 2152
  19. ;; This is a transformation format of Unicode that contains only 7-bit
  20. ;; ASCII octets and is intended to be readable by humans in the limiting
  21. ;; case that the document consists of characters from the US-ASCII
  22. ;; repertoire.
  23. ;; In short, runs of characters outside US-ASCII are encoded as base64
  24. ;; inside delimiters.
  25. ;; A variation of UTF-7 is specified in IMAP 4rev1 (RFC 2060) as the way
  26. ;; to represent characters outside US-ASCII in mailbox names in IMAP.
  27. ;; This library supports both variants, but the IMAP variation was the
  28. ;; reason I wrote it.
  29. ;; The routines convert UTF-7 -> UTF-16 (16 bit encoding of Unicode)
  30. ;; -> current character set, and vice versa.
  31. ;; However, until Emacs supports Unicode, the only Emacs character set
  32. ;; supported here is ISO-8859.1, which can trivially be converted to/from
  33. ;; Unicode.
  34. ;; When decoding results in a character outside the Emacs character set,
  35. ;; an error is thrown. It is up to the application to recover.
  36. ;; UTF-7 should be done by providing a coding system. Mule-UCS does
  37. ;; already, but I don't know if it does the IMAP version and it's not
  38. ;; clear whether that should really be a coding system. The UTF-16
  39. ;; part of the conversion can be done with coding systems available
  40. ;; with Mule-UCS or some versions of Emacs. Unfortunately these were
  41. ;; done wrongly (regarding handling of byte-order marks and how the
  42. ;; variants were named), so we don't have a consistent name for the
  43. ;; necessary coding system. The code below doesn't seem to DTRT
  44. ;; generally. E.g.:
  45. ;;
  46. ;; (utf7-encode "a+£")
  47. ;; => "a+ACsAow-"
  48. ;;
  49. ;; $ echo "a+£"|iconv -f iso-8859-1 -t utf-7
  50. ;; a+-+AKM
  51. ;;
  52. ;; -- fx
  53. ;;; Code:
  54. (require 'base64)
  55. (eval-when-compile (require 'cl))
  56. (require 'mm-util)
  57. (defconst utf7-direct-encoding-chars " -%'-*,-[]-}"
  58. "Character ranges which do not need escaping in UTF-7.")
  59. (defconst utf7-imap-direct-encoding-chars
  60. (concat utf7-direct-encoding-chars "+\\~")
  61. "Character ranges which do not need escaping in the IMAP variant of UTF-7.")
  62. (defconst utf7-utf-16-coding-system
  63. (cond ((mm-coding-system-p 'utf-16-be-no-signature) ; Mule-UCS
  64. 'utf-16-be-no-signature)
  65. ((and (mm-coding-system-p 'utf-16-be) ; Emacs
  66. ;; Avoid versions with BOM.
  67. (= 2 (length (encode-coding-string "a" 'utf-16-be))))
  68. 'utf-16-be)
  69. ((mm-coding-system-p 'utf-16-be-nosig) ; ?
  70. 'utf-16-be-nosig))
  71. "Coding system which encodes big endian UTF-16 without a BOM signature.")
  72. (defsubst utf7-imap-get-pad-length (len modulus)
  73. "Return required length of padding for IMAP modified base64 fragment."
  74. (mod (- len) modulus))
  75. (defun utf7-encode-internal (&optional for-imap)
  76. "Encode text in (temporary) buffer as UTF-7.
  77. Use IMAP modification if FOR-IMAP is non-nil."
  78. (let ((start (point-min))
  79. (end (point-max)))
  80. (narrow-to-region start end)
  81. (goto-char start)
  82. (let* ((esc-char (if for-imap ?& ?+))
  83. (direct-encoding-chars
  84. (if for-imap utf7-imap-direct-encoding-chars
  85. utf7-direct-encoding-chars))
  86. (not-direct-encoding-chars (concat "^" direct-encoding-chars)))
  87. (while (not (eobp))
  88. (skip-chars-forward direct-encoding-chars)
  89. (unless (eobp)
  90. (insert esc-char)
  91. (let ((p (point))
  92. (fc (following-char))
  93. (run-length
  94. (skip-chars-forward not-direct-encoding-chars)))
  95. (if (and (= fc esc-char)
  96. (= run-length 1)) ; Lone esc-char?
  97. (delete-char -1) ; Now there's one too many
  98. (utf7-fragment-encode p (point) for-imap))
  99. (insert "-")))))))
  100. (defun utf7-fragment-encode (start end &optional for-imap)
  101. "Encode text from START to END in buffer as UTF-7 escape fragment.
  102. Use IMAP modification if FOR-IMAP is non-nil."
  103. (save-restriction
  104. (narrow-to-region start end)
  105. (funcall (utf7-get-u16char-converter 'to-utf-16))
  106. (mm-with-unibyte-current-buffer
  107. (base64-encode-region start (point-max)))
  108. (goto-char start)
  109. (let ((pm (point-max)))
  110. (when for-imap
  111. (while (search-forward "/" nil t)
  112. (replace-match ",")))
  113. (skip-chars-forward "^= \t\n" pm)
  114. (delete-region (point) pm))))
  115. (defun utf7-decode-internal (&optional for-imap)
  116. "Decode UTF-7 text in (temporary) buffer.
  117. Use IMAP modification if FOR-IMAP is non-nil."
  118. (let ((start (point-min))
  119. (end (point-max)))
  120. (goto-char start)
  121. (let* ((esc-pattern (concat "^" (char-to-string (if for-imap ?& ?+))))
  122. (base64-chars (concat "A-Za-z0-9+"
  123. (char-to-string (if for-imap ?, ?/)))))
  124. (while (not (eobp))
  125. (skip-chars-forward esc-pattern)
  126. (unless (eobp)
  127. (forward-char)
  128. (let ((p (point))
  129. (run-length (skip-chars-forward base64-chars)))
  130. (when (and (not (eobp)) (= (following-char) ?-))
  131. (delete-char 1))
  132. (unless (= run-length 0) ; Encoded lone esc-char?
  133. (save-excursion
  134. (utf7-fragment-decode p (point) for-imap)
  135. (goto-char p)
  136. (delete-char -1)))))))))
  137. (defun utf7-fragment-decode (start end &optional for-imap)
  138. "Decode base64 encoded fragment from START to END of UTF-7 text in buffer.
  139. Use IMAP modification if FOR-IMAP is non-nil."
  140. (save-restriction
  141. (narrow-to-region start end)
  142. (when for-imap
  143. (goto-char start)
  144. (while (search-forward "," nil 'move-to-end) (replace-match "/")))
  145. (let ((pl (utf7-imap-get-pad-length (- end start) 4)))
  146. (insert (make-string pl ?=))
  147. (base64-decode-region start (+ end pl)))
  148. (funcall (utf7-get-u16char-converter 'from-utf-16))))
  149. (defun utf7-get-u16char-converter (which-way)
  150. "Return a function to convert between UTF-16 and current character set."
  151. (if utf7-utf-16-coding-system
  152. (if (eq which-way 'to-utf-16)
  153. (lambda ()
  154. (encode-coding-region (point-min) (point-max)
  155. utf7-utf-16-coding-system))
  156. (lambda ()
  157. (decode-coding-region (point-min) (point-max)
  158. utf7-utf-16-coding-system)))
  159. ;; Add test to check if we are really Latin-1.
  160. (if (eq which-way 'to-utf-16)
  161. 'utf7-latin1-u16-char-converter
  162. 'utf7-u16-latin1-char-converter)))
  163. (defun utf7-latin1-u16-char-converter ()
  164. "Convert latin 1 (ISO-8859.1) characters to 16 bit Unicode.
  165. Characters are converted to raw byte pairs in narrowed buffer."
  166. (mm-encode-coding-region (point-min) (point-max) 'iso-8859-1)
  167. (mm-disable-multibyte)
  168. (goto-char (point-min))
  169. (while (not (eobp))
  170. (insert 0)
  171. (forward-char)))
  172. (defun utf7-u16-latin1-char-converter ()
  173. "Convert 16 bit Unicode characters to latin 1 (ISO-8859.1).
  174. Characters are in raw byte pairs in narrowed buffer."
  175. (goto-char (point-min))
  176. (while (not (eobp))
  177. (if (= 0 (following-char))
  178. (delete-char 1)
  179. (error "Unable to convert from Unicode"))
  180. (forward-char))
  181. (mm-decode-coding-region (point-min) (point-max) 'iso-8859-1)
  182. (mm-enable-multibyte))
  183. ;;;###autoload
  184. (defun utf7-encode (string &optional for-imap)
  185. "Encode UTF-7 STRING. Use IMAP modification if FOR-IMAP is non-nil."
  186. (if (and (coding-system-p 'utf-7) (coding-system-p 'utf-7-imap))
  187. ;; Emacs 23 with proper support for IMAP
  188. (encode-coding-string string (if for-imap 'utf-7-imap 'utf-7))
  189. (mm-with-multibyte-buffer
  190. (insert string)
  191. (utf7-encode-internal for-imap)
  192. (buffer-string))))
  193. (defun utf7-decode (string &optional for-imap)
  194. "Decode UTF-7 STRING. Use IMAP modification if FOR-IMAP is non-nil."
  195. (if (and (coding-system-p 'utf-7) (coding-system-p 'utf-7-imap))
  196. ;; Emacs 23 with proper support for IMAP
  197. (decode-coding-string string (if for-imap 'utf-7-imap 'utf-7))
  198. (mm-with-unibyte-buffer
  199. (insert string)
  200. (utf7-decode-internal for-imap)
  201. (mm-enable-multibyte)
  202. (buffer-string))))
  203. (provide 'utf7)
  204. ;;; utf7.el ends here