PageRenderTime 56ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/lisp/international/utf7.el

https://gitlab.com/wilfred/emacs
Emacs Lisp | 236 lines | 150 code | 27 blank | 59 comment | 7 complexity | b0fc1c739f02b59c5d557ea4e5989c34 MD5 | raw file
  1. ;;; utf7.el --- UTF-7 encoding/decoding for Emacs -*-coding: utf-8;-*-
  2. ;; Copyright (C) 1999-2016 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 utf-8 -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. (let* ((buf (current-buffer))
  105. (base (with-temp-buffer
  106. (set-buffer-multibyte nil)
  107. (insert-buffer-substring buf start end)
  108. (funcall (utf7-get-u16char-converter 'to-utf-16))
  109. (base64-encode-region (point-min) (point-max))
  110. (buffer-string))))
  111. (narrow-to-region start end)
  112. (delete-region (point-min) (point-max))
  113. (insert base))
  114. (goto-char (point-min))
  115. (let ((pm (point-max)))
  116. (when for-imap
  117. (while (search-forward "/" nil t)
  118. (replace-match ",")))
  119. (skip-chars-forward "^= \t\n" pm)
  120. (delete-region (point) pm))))
  121. (defun utf7-decode-internal (&optional for-imap)
  122. "Decode UTF-7 text in (temporary) buffer.
  123. Use IMAP modification if FOR-IMAP is non-nil."
  124. (let ((start (point-min))
  125. (end (point-max)))
  126. (goto-char start)
  127. (let* ((esc-pattern (concat "^" (char-to-string (if for-imap ?& ?+))))
  128. (base64-chars (concat "A-Za-z0-9+"
  129. (char-to-string (if for-imap ?, ?/)))))
  130. (while (not (eobp))
  131. (skip-chars-forward esc-pattern)
  132. (unless (eobp)
  133. (forward-char)
  134. (let ((p (point))
  135. (run-length (skip-chars-forward base64-chars)))
  136. (when (and (not (eobp)) (= (following-char) ?-))
  137. (delete-char 1))
  138. (unless (= run-length 0) ; Encoded lone esc-char?
  139. (save-excursion
  140. (utf7-fragment-decode p (point) for-imap)
  141. (goto-char p)
  142. (delete-char -1)))))))))
  143. (defun utf7-fragment-decode (start end &optional for-imap)
  144. "Decode base64 encoded fragment from START to END of UTF-7 text in buffer.
  145. Use IMAP modification if FOR-IMAP is non-nil."
  146. (save-restriction
  147. (narrow-to-region start end)
  148. (when for-imap
  149. (goto-char start)
  150. (while (search-forward "," nil 'move-to-end) (replace-match "/")))
  151. (let ((pl (utf7-imap-get-pad-length (- end start) 4)))
  152. (insert (make-string pl ?=))
  153. (base64-decode-region start (+ end pl)))
  154. (funcall (utf7-get-u16char-converter 'from-utf-16))))
  155. (defun utf7-get-u16char-converter (which-way)
  156. "Return a function to convert between UTF-16 and current character set."
  157. (if utf7-utf-16-coding-system
  158. (if (eq which-way 'to-utf-16)
  159. (lambda ()
  160. (encode-coding-region (point-min) (point-max)
  161. utf7-utf-16-coding-system))
  162. (lambda ()
  163. (decode-coding-region (point-min) (point-max)
  164. utf7-utf-16-coding-system)))
  165. ;; Add test to check if we are really Latin-1.
  166. (if (eq which-way 'to-utf-16)
  167. 'utf7-latin1-u16-char-converter
  168. 'utf7-u16-latin1-char-converter)))
  169. (defun utf7-latin1-u16-char-converter ()
  170. "Convert latin 1 (ISO-8859.1) characters to 16 bit Unicode.
  171. Characters are converted to raw byte pairs in narrowed buffer."
  172. (encode-coding-region (point-min) (point-max) 'iso-8859-1)
  173. (goto-char (point-min))
  174. (while (not (eobp))
  175. (insert 0)
  176. (forward-char)))
  177. (defun utf7-u16-latin1-char-converter ()
  178. "Convert 16 bit Unicode characters to latin 1 (ISO-8859.1).
  179. Characters are in raw byte pairs in narrowed buffer."
  180. (goto-char (point-min))
  181. (while (not (eobp))
  182. (if (= 0 (following-char))
  183. (delete-char 1)
  184. (error "Unable to convert from Unicode"))
  185. (forward-char))
  186. (decode-coding-region (point-min) (point-max) 'iso-8859-1)
  187. (mm-enable-multibyte))
  188. ;;;###autoload
  189. (defun utf7-encode (string &optional for-imap)
  190. "Encode UTF-7 STRING. Use IMAP modification if FOR-IMAP is non-nil."
  191. (if (and (coding-system-p 'utf-7) (coding-system-p 'utf-7-imap))
  192. ;; Emacs 23 with proper support for IMAP
  193. (encode-coding-string string (if for-imap 'utf-7-imap 'utf-7))
  194. (mm-with-multibyte-buffer
  195. (insert string)
  196. (utf7-encode-internal for-imap)
  197. (buffer-string))))
  198. (defun utf7-decode (string &optional for-imap)
  199. "Decode UTF-7 STRING. Use IMAP modification if FOR-IMAP is non-nil."
  200. (if (and (coding-system-p 'utf-7) (coding-system-p 'utf-7-imap))
  201. ;; Emacs 23 with proper support for IMAP
  202. (decode-coding-string string (if for-imap 'utf-7-imap 'utf-7))
  203. (mm-with-unibyte-buffer
  204. (insert string)
  205. (utf7-decode-internal for-imap)
  206. (mm-enable-multibyte)
  207. (buffer-string))))
  208. (provide 'utf7)
  209. ;;; utf7.el ends here