123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- (require 'base64)
- (eval-when-compile (require 'cl))
- (require 'mm-util)
- (defconst utf7-direct-encoding-chars " -%'-*,-[]-}"
- "Character ranges which do not need escaping in UTF-7.")
- (defconst utf7-imap-direct-encoding-chars
- (concat utf7-direct-encoding-chars "+\\~")
- "Character ranges which do not need escaping in the IMAP variant of UTF-7.")
- (defconst utf7-utf-16-coding-system
- (cond ((mm-coding-system-p 'utf-16-be-no-signature)
- 'utf-16-be-no-signature)
- ((and (mm-coding-system-p 'utf-16-be)
-
- (= 2 (length (encode-coding-string "a" 'utf-16-be))))
- 'utf-16-be)
- ((mm-coding-system-p 'utf-16-be-nosig)
- 'utf-16-be-nosig))
- "Coding system which encodes big endian UTF-16 without a BOM signature.")
- (defsubst utf7-imap-get-pad-length (len modulus)
- "Return required length of padding for IMAP modified base64 fragment."
- (mod (- len) modulus))
- (defun utf7-encode-internal (&optional for-imap)
- "Encode text in (temporary) buffer as UTF-7.
- Use IMAP modification if FOR-IMAP is non-nil."
- (let ((start (point-min))
- (end (point-max)))
- (narrow-to-region start end)
- (goto-char start)
- (let* ((esc-char (if for-imap ?& ?+))
- (direct-encoding-chars
- (if for-imap utf7-imap-direct-encoding-chars
- utf7-direct-encoding-chars))
- (not-direct-encoding-chars (concat "^" direct-encoding-chars)))
- (while (not (eobp))
- (skip-chars-forward direct-encoding-chars)
- (unless (eobp)
- (insert esc-char)
- (let ((p (point))
- (fc (following-char))
- (run-length
- (skip-chars-forward not-direct-encoding-chars)))
- (if (and (= fc esc-char)
- (= run-length 1))
- (delete-char -1)
- (utf7-fragment-encode p (point) for-imap))
- (insert "-")))))))
- (defun utf7-fragment-encode (start end &optional for-imap)
- "Encode text from START to END in buffer as UTF-7 escape fragment.
- Use IMAP modification if FOR-IMAP is non-nil."
- (save-restriction
- (narrow-to-region start end)
- (funcall (utf7-get-u16char-converter 'to-utf-16))
- (mm-with-unibyte-current-buffer
- (base64-encode-region start (point-max)))
- (goto-char start)
- (let ((pm (point-max)))
- (when for-imap
- (while (search-forward "/" nil t)
- (replace-match ",")))
- (skip-chars-forward "^= \t\n" pm)
- (delete-region (point) pm))))
- (defun utf7-decode-internal (&optional for-imap)
- "Decode UTF-7 text in (temporary) buffer.
- Use IMAP modification if FOR-IMAP is non-nil."
- (let ((start (point-min))
- (end (point-max)))
- (goto-char start)
- (let* ((esc-pattern (concat "^" (char-to-string (if for-imap ?& ?+))))
- (base64-chars (concat "A-Za-z0-9+"
- (char-to-string (if for-imap ?, ?/)))))
- (while (not (eobp))
- (skip-chars-forward esc-pattern)
- (unless (eobp)
- (forward-char)
- (let ((p (point))
- (run-length (skip-chars-forward base64-chars)))
- (when (and (not (eobp)) (= (following-char) ?-))
- (delete-char 1))
- (unless (= run-length 0)
- (save-excursion
- (utf7-fragment-decode p (point) for-imap)
- (goto-char p)
- (delete-char -1)))))))))
- (defun utf7-fragment-decode (start end &optional for-imap)
- "Decode base64 encoded fragment from START to END of UTF-7 text in buffer.
- Use IMAP modification if FOR-IMAP is non-nil."
- (save-restriction
- (narrow-to-region start end)
- (when for-imap
- (goto-char start)
- (while (search-forward "," nil 'move-to-end) (replace-match "/")))
- (let ((pl (utf7-imap-get-pad-length (- end start) 4)))
- (insert (make-string pl ?=))
- (base64-decode-region start (+ end pl)))
- (funcall (utf7-get-u16char-converter 'from-utf-16))))
- (defun utf7-get-u16char-converter (which-way)
- "Return a function to convert between UTF-16 and current character set."
- (if utf7-utf-16-coding-system
- (if (eq which-way 'to-utf-16)
- (lambda ()
- (encode-coding-region (point-min) (point-max)
- utf7-utf-16-coding-system))
- (lambda ()
- (decode-coding-region (point-min) (point-max)
- utf7-utf-16-coding-system)))
-
- (if (eq which-way 'to-utf-16)
- 'utf7-latin1-u16-char-converter
- 'utf7-u16-latin1-char-converter)))
- (defun utf7-latin1-u16-char-converter ()
- "Convert latin 1 (ISO-8859.1) characters to 16 bit Unicode.
- Characters are converted to raw byte pairs in narrowed buffer."
- (mm-encode-coding-region (point-min) (point-max) 'iso-8859-1)
- (mm-disable-multibyte)
- (goto-char (point-min))
- (while (not (eobp))
- (insert 0)
- (forward-char)))
- (defun utf7-u16-latin1-char-converter ()
- "Convert 16 bit Unicode characters to latin 1 (ISO-8859.1).
- Characters are in raw byte pairs in narrowed buffer."
- (goto-char (point-min))
- (while (not (eobp))
- (if (= 0 (following-char))
- (delete-char 1)
- (error "Unable to convert from Unicode"))
- (forward-char))
- (mm-decode-coding-region (point-min) (point-max) 'iso-8859-1)
- (mm-enable-multibyte))
- (defun utf7-encode (string &optional for-imap)
- "Encode UTF-7 STRING. Use IMAP modification if FOR-IMAP is non-nil."
- (if (and (coding-system-p 'utf-7) (coding-system-p 'utf-7-imap))
-
- (encode-coding-string string (if for-imap 'utf-7-imap 'utf-7))
- (mm-with-multibyte-buffer
- (insert string)
- (utf7-encode-internal for-imap)
- (buffer-string))))
- (defun utf7-decode (string &optional for-imap)
- "Decode UTF-7 STRING. Use IMAP modification if FOR-IMAP is non-nil."
- (if (and (coding-system-p 'utf-7) (coding-system-p 'utf-7-imap))
-
- (decode-coding-string string (if for-imap 'utf-7-imap 'utf-7))
- (mm-with-unibyte-buffer
- (insert string)
- (utf7-decode-internal for-imap)
- (mm-enable-multibyte)
- (buffer-string))))
- (provide 'utf7)
|