126 |
;;; Functions for encoding RFC2047 messages |
;;; Functions for encoding RFC2047 messages |
127 |
;;; |
;;; |
128 |
|
|
129 |
|
(defun rfc2047-qp-or-base64 () |
130 |
|
"Return the type with which to encode the buffer. |
131 |
|
This is either `base64' or `quoted-printable'." |
132 |
|
(save-excursion |
133 |
|
(let ((limit (min (point-max) (+ 2000 (point-min)))) |
134 |
|
(n8bit 0)) |
135 |
|
(goto-char (point-min)) |
136 |
|
(skip-chars-forward "\x20-\x7f\r\n\t" limit) |
137 |
|
(while (< (point) limit) |
138 |
|
(incf n8bit) |
139 |
|
(forward-char 1) |
140 |
|
(skip-chars-forward "\x20-\x7f\r\n\t" limit)) |
141 |
|
(if (or (< (* 6 n8bit) (- limit (point-min))) |
142 |
|
;; Don't base64, say, a short line with a single |
143 |
|
;; non-ASCII char when splitting parts by charset. |
144 |
|
(= n8bit 1)) |
145 |
|
'quoted-printable |
146 |
|
'base64)))) |
147 |
|
|
148 |
(defun rfc2047-narrow-to-field () |
(defun rfc2047-narrow-to-field () |
149 |
"Narrow the buffer to the header on the current line." |
"Narrow the buffer to the header on the current line." |
150 |
(beginning-of-line) |
(beginning-of-line) |
430 |
;; encoding, choose the one that's shorter. |
;; encoding, choose the one that's shorter. |
431 |
(save-restriction |
(save-restriction |
432 |
(narrow-to-region b e) |
(narrow-to-region b e) |
433 |
(if (eq (mm-qp-or-base64) 'base64) |
(if (eq (rfc2047-qp-or-base64) 'base64) |
434 |
'B |
'B |
435 |
'Q)))) |
'Q)))) |
436 |
(start (concat |
(start (concat |
739 |
;; Be more liberal to accept buggy base64 strings. If |
;; Be more liberal to accept buggy base64 strings. If |
740 |
;; base64-decode-string accepts buggy strings, this function could |
;; base64-decode-string accepts buggy strings, this function could |
741 |
;; be aliased to identity. |
;; be aliased to identity. |
742 |
(case (mod (length string) 4) |
(if (= 0 (mod (length string) 4)) |
743 |
(0 string) |
string |
744 |
(1 string) ;; Error, don't pad it. |
(when (string-match "=+$" string) |
745 |
(2 (concat string "==")) |
(setq string (substring string 0 (match-beginning 0)))) |
746 |
(3 (concat string "=")))) |
(case (mod (length string) 4) |
747 |
|
(0 string) |
748 |
|
(1 string) ;; Error, don't pad it. |
749 |
|
(2 (concat string "==")) |
750 |
|
(3 (concat string "="))))) |
751 |
|
|
752 |
(defun rfc2047-decode (charset encoding string) |
(defun rfc2047-decode (charset encoding string) |
753 |
"Decode STRING from the given MIME CHARSET in the given ENCODING. |
"Decode STRING from the given MIME CHARSET in the given ENCODING. |