1 ;;; rfc2047.el --- functions for encoding and decoding rfc2047 messages
3 ;; Copyright (C) 1998, 1999, 2000, 2002, 2003, 2004
4 ;; Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; RFC 2047 is "MIME (Multipurpose Internet Mail Extensions) Part
28 ;; Three: Message Header Extensions for Non-ASCII Text".
34 (defvar message-posting-charset))
38 ;; Fixme: Avoid this (used for mail-parse-charset) mm dependence on gnus.
41 (autoload 'mm-body-7-or-8 "mm-bodies")
43 (defvar rfc2047-header-encoding-alist
44 '(("Newsgroups" . nil)
47 ("\\(Resent-\\)?\\(From\\|Cc\\|To\\|Bcc\\|Reply-To\\|Sender\
48 \\|Mail-Followup-To\\|Mail-Copies-To\\|Approved\\)" . address-mime)
50 "*Header/encoding method alist.
51 The list is traversed sequentially. The keys can either be
56 1) nil, in which case no encoding is done;
57 2) `mime', in which case the header will be encoded according to RFC2047;
58 3) `address-mime', like `mime', but takes account of the rules for address
59 fields (where quoted strings and comments must be treated separately);
60 4) a charset, in which case it will be encoded as that charset;
61 5) `default', in which case the field will be encoded as the rest
64 (defvar rfc2047-charset-encoding-alist
88 "Alist of MIME charsets to RFC2047 encodings.
89 Valid encodings are nil, `Q' and `B'. These indicate binary (no) encoding,
90 quoted-printable and base64 respectively.")
92 (defvar rfc2047-encode-function-alist
93 '((Q . rfc2047-q-encode-string)
94 (B . rfc2047-b-encode-string)
96 "Alist of RFC2047 encodings to encoding functions.")
98 (defvar rfc2047-encode-encoded-words t
99 "Whether encoded words should be encoded again.")
102 ;;; Functions for encoding RFC2047 messages
105 (defun rfc2047-qp-or-base64 ()
106 "Return the type with which to encode the buffer.
107 This is either `base64' or `quoted-printable'."
109 (let ((limit (min (point-max) (+ 2000 (point-min))))
111 (goto-char (point-min))
112 (skip-chars-forward "\x20-\x7f\r\n\t" limit)
113 (while (< (point) limit)
116 (skip-chars-forward "\x20-\x7f\r\n\t" limit))
117 (if (or (< (* 6 n8bit) (- limit (point-min)))
118 ;; Don't base64, say, a short line with a single
119 ;; non-ASCII char when splitting parts by charset.
124 (defun rfc2047-narrow-to-field ()
125 "Narrow the buffer to the header on the current line."
131 (if (re-search-forward "^[^ \n\t]" nil t)
134 (goto-char (point-min)))
136 (defun rfc2047-field-value ()
137 "Return the value of the field at point."
140 (rfc2047-narrow-to-field)
141 (re-search-forward ":[ \t\n]*" nil t)
142 (buffer-substring-no-properties (point) (point-max)))))
144 (defvar rfc2047-encoding-type 'address-mime
145 "The type of encoding done by `rfc2047-encode-region'.
146 This should be dynamically bound around calls to
147 `rfc2047-encode-region' to either `mime' or `address-mime'. See
148 `rfc2047-header-encoding-alist', for definitions.")
150 (defun rfc2047-encode-message-header ()
151 "Encode the message header according to `rfc2047-header-encoding-alist'.
152 Should be called narrowed to the head of the message."
155 (goto-char (point-min))
156 (let (alist elem method)
159 (rfc2047-narrow-to-field)
160 (if (not (rfc2047-encodable-p))
162 (if (and (eq (mm-body-7-or-8) '8bit)
165 (car message-posting-charset)))
166 ;; 8 bit must be decoded.
167 (mm-encode-coding-region
168 (point-min) (point-max)
169 (mm-charset-to-coding-system
170 (car message-posting-charset))))
171 ;; No encoding necessary, but folding is nice
175 (goto-char (point-min))
176 (skip-chars-forward "^:")
177 (when (looking-at ": ")
181 ;; We found something that may perhaps be encoded.
183 alist rfc2047-header-encoding-alist)
184 (while (setq elem (pop alist))
185 (when (or (and (stringp (car elem))
186 (looking-at (car elem)))
190 (goto-char (point-min))
191 (re-search-forward "^[^:]+: *" nil t)
193 ((eq method 'address-mime)
194 (rfc2047-encode-region (point) (point-max)))
196 (let ((rfc2047-encoding-type 'mime))
197 (rfc2047-encode-region (point) (point-max))))
198 ((eq method 'default)
199 (if (and (featurep 'mule)
200 (if (boundp 'default-enable-multibyte-characters)
201 default-enable-multibyte-characters)
203 (mm-encode-coding-region (point) (point-max)
204 mail-parse-charset)))
205 ;; We get this when CC'ing messsages to newsgroups with
206 ;; 8-bit names. The group name mail copy just got
207 ;; unconditionally encoded. Previously, it would ask
208 ;; whether to encode, which was quite confusing for the
209 ;; user. If the new behaviour is wrong, tell me. I have
210 ;; left the old code commented out below.
211 ;; -- Per Abrahamsen <abraham@dina.kvl.dk> Date: 2001-10-07.
212 ;; Modified by Dave Love, with the commented-out code changed
213 ;; in accordance with changes elsewhere.
215 (rfc2047-encode-region (point) (point-max)))
217 ;;; (if (or (message-options-get
218 ;;; 'rfc2047-encode-message-header-encode-any)
219 ;;; (message-options-set
220 ;;; 'rfc2047-encode-message-header-encode-any
222 ;;; "Some texts are not encoded. Encode anyway?")))
223 ;;; (rfc2047-encode-region (point-min) (point-max))
224 ;;; (error "Cannot send unencoded text")))
225 ((mm-coding-system-p method)
226 (if (and (featurep 'mule)
227 (if (boundp 'default-enable-multibyte-characters)
228 default-enable-multibyte-characters))
229 (mm-encode-coding-region (point) (point-max) method)))
232 (goto-char (point-max)))))))
234 ;; Fixme: This, and the require below may not be the Right Thing, but
235 ;; should be safe just before release. -- fx 2001-02-08
236 (eval-when-compile (defvar message-posting-charset))
238 (defun rfc2047-encodable-p ()
239 "Return non-nil if any characters in current buffer need encoding in headers.
240 The buffer may be narrowed."
241 (require 'message) ; for message-posting-charset
243 (mm-find-mime-charset-region (point-min) (point-max))))
244 (goto-char (point-min))
245 (or (and rfc2047-encode-encoded-words
247 (search-forward "=?" nil t)
248 (goto-char (point-min))))
250 (not (equal charsets (list (car message-posting-charset))))))))
252 ;; Use this syntax table when parsing into regions that may need
253 ;; encoding. Double quotes are string delimiters, backslash is
254 ;; character quoting, and all other RFC 2822 special characters are
255 ;; treated as punctuation so we can use forward-sexp/forward-word to
256 ;; skip to the end of regions appropriately. Nb. ietf-drums does
257 ;; things differently.
258 (defconst rfc2047-syntax-table
259 ;; (make-char-table 'syntax-table '(2)) only works in Emacs.
260 (let ((table (make-syntax-table)))
261 ;; The following is done to work for setting all elements of the table
262 ;; in Emacs 21 and 22 and XEmacs; it appears to be the cleanest way.
263 ;; Play safe and don't assume the form of the word syntax entry --
265 (if (fboundp 'set-char-table-range) ; Emacs
266 (funcall (intern "set-char-table-range")
267 table t (aref (standard-syntax-table) ?a))
268 (if (fboundp 'put-char-table)
269 (if (fboundp 'get-char-table) ; warning avoidance
270 (put-char-table t (get-char-table ?a (standard-syntax-table))
272 (modify-syntax-entry ?\\ "\\" table)
273 (modify-syntax-entry ?\" "\"" table)
274 (modify-syntax-entry ?\( "." table)
275 (modify-syntax-entry ?\) "." table)
276 (modify-syntax-entry ?\< "." table)
277 (modify-syntax-entry ?\> "." table)
278 (modify-syntax-entry ?\[ "." table)
279 (modify-syntax-entry ?\] "." table)
280 (modify-syntax-entry ?: "." table)
281 (modify-syntax-entry ?\; "." table)
282 (modify-syntax-entry ?, "." table)
283 (modify-syntax-entry ?@ "." table)
286 (defun rfc2047-encode-region (b e)
287 "Encode words in region B to E that need encoding.
288 By default, the region is treated as containing RFC2822 addresses.
289 Dynamically bind `rfc2047-encoding-type' to change that."
291 (narrow-to-region b e)
292 (let ((encodable-regexp (if rfc2047-encode-encoded-words
293 "[^\000-\177]+\\|=\\?"
295 start ; start of current token
297 ;; Whether there's an encoded word before the current token,
298 ;; either immediately or separated by space.
300 (if (eq 'mime rfc2047-encoding-type)
301 ;; Simple case. Continuous words in which all those contain
302 ;; non-ASCII characters are encoded collectively. Encoding
303 ;; ASCII words, including `Re:' used in Subject headers, is
304 ;; avoided for interoperability with non-MIME clients and
305 ;; for making it easy to find keywords.
307 (goto-char (point-min))
308 (while (progn (skip-chars-forward " \t\n")
311 (while (and (looking-at "[ \t\n]*\\([^ \t\n]+\\)")
313 (setq end (match-end 0))
314 (re-search-forward encodable-regexp end t)))
316 (if (> (point) start)
317 (rfc2047-encode start (point))
319 ;; `address-mime' case -- take care of quoted words, comments.
320 (with-syntax-table rfc2047-syntax-table
321 (goto-char (point-min))
322 (condition-case nil ; in case of unbalanced quotes
323 ;; Look for rfc2822-style: sequences of atoms, quoted
324 ;; strings, specials, whitespace. (Specials mustn't be
328 (skip-chars-forward " \t\n")
331 ((not (char-after))) ; eob
333 ((eq ?\" (char-syntax (char-after)))
337 ;; Does it need encoding?
339 (if (re-search-forward encodable-regexp end 'move)
340 ;; It needs encoding. Strip the quotes first,
341 ;; since encoded words can't occur in quotes.
344 (delete-backward-char 1)
348 ;; There was a preceding quoted word. We need
349 ;; to include any separating whitespace in this
350 ;; word to avoid it getting lost.
351 (skip-chars-backward " \t")
352 ;; A space is needed between the encoded words.
356 ;; Adjust the end position for the deleted quotes.
357 (rfc2047-encode start (- end 2))
358 (setq last-encoded t)) ; record that it was encoded
359 (setq last-encoded nil)))
360 ((eq ?. (char-syntax (char-after)))
361 ;; Skip other delimiters, but record that they've
362 ;; potentially separated quoted words.
364 (setq last-encoded nil))
365 (t ; normal token/whitespace sequence
368 (skip-chars-backward " \t\n")
369 (eq (char-before) ?\()
371 ;; Look for the end of parentheses.
372 (let ((string (buffer-substring (point)
374 (default-major-mode 'fundamental-mode))
375 ;; Use `standard-syntax-table'.
378 (goto-char (point-min))
382 (setq end (+ start (point) -3)))
384 ;; Encode text as an unstructured field.
385 (let ((rfc2047-encoding-type 'mime))
386 (rfc2047-encode-region start end)
388 ;; Skip one ASCII word, or encode continuous words
389 ;; in which all those contain non-ASCII characters.
392 (when (looking-at "[\000-\177]+")
395 (if (re-search-forward "[ \t\n]\\|\\Sw" end 'move)
397 (setq end (match-beginning 0))
398 (if rfc2047-encode-encoded-words
401 (when (search-forward "=?" end 'move)
402 (goto-char (match-beginning 0))
408 (when (looking-at encodable-regexp)
409 (goto-char (match-end 0))
410 (while (and (looking-at "[ \t\n]+\\([^ \t\n]+\\)")
411 (setq end (match-end 0))
412 (string-match encodable-regexp
415 (when (looking-at "[^ \t\n]+")
416 (setq end (match-end 0))
417 (if (re-search-forward "\\Sw+" end t)
418 ;; There are special characters better
419 ;; to be encoded so that MTAs may parse
421 (cond ((= end (point)))
422 ((looking-at encodable-regexp)
425 (goto-char (1- (match-end 0)))
426 (unless (= (point) (match-beginning 0))
429 (skip-chars-forward " \t\n")
430 (if (and (looking-at "[^ \t\n]+")
431 (string-match encodable-regexp
434 (goto-char end)))))))
435 (skip-chars-backward " \t\n")
438 (if (re-search-forward encodable-regexp end 'move)
440 (rfc2047-encode start end)
441 (setq last-encoded t))
442 (setq last-encoded nil))))))
444 (error "Invalid data for rfc2047 encoding: %s"
445 (buffer-substring b e)))))))
446 (rfc2047-fold-region b (point))))
448 (defun rfc2047-encode-string (string)
449 "Encode words in STRING.
450 By default, the string is treated as containing addresses (see
451 `rfc2047-encoding-type')."
454 (rfc2047-encode-region (point-min) (point-max))
457 (defun rfc2047-encode-1 (column string cs encoder start space &optional eword)
458 "Subroutine used by `rfc2047-encode'."
459 (cond ((string-equal string "")
463 (string-match "\n[ \t]+\\'" eword))
464 ;; Reomove a superfluous empty line.
465 (setq eword (substring eword 0 (match-beginning 0))))
466 (rfc2047-encode-1 (length space) string cs encoder start " "
467 (concat eword "\n" space)))
470 (limit (1- (length string)))
475 (setq next (concat start
478 (mm-encode-coding-string
479 (substring string 0 (1+ index))
481 (substring string 0 (1+ index))))
483 (if (<= (+ column (length next)) 76)
488 (setq eword (concat eword next))
491 (when (string-match "\n[ \t]+\\'" eword)
492 ;; Reomove a superfluous empty line.
493 (setq eword (substring eword 0 (match-beginning 0))))
494 (rfc2047-encode-1 (length space) (substring string index)
496 (concat eword "\n" space)))))))
498 (defun rfc2047-encode (b e)
499 "Encode the word(s) in the region B to E.
500 By default, the region is treated as containing addresses (see
501 `rfc2047-encoding-type')."
502 (let ((mime-charset (or (mm-find-mime-charset-region b e) (list 'us-ascii)))
503 cs encoding space eword)
504 (cond ((> (length mime-charset) 1)
505 (error "Can't rfc2047-encode `%s'"
506 (buffer-substring-no-properties b e)))
507 ((= (length mime-charset) 1)
508 (setq mime-charset (car mime-charset)
509 cs (mm-charset-to-coding-system mime-charset))
510 (unless (and (mm-multibyte-p)
511 (mm-coding-system-p cs))
514 (narrow-to-region b e)
516 (or (cdr (assq mime-charset
517 rfc2047-charset-encoding-alist))
518 ;; For the charsets that don't have a preferred
519 ;; encoding, choose the one that's shorter.
520 (if (eq (rfc2047-qp-or-base64) 'base64)
525 (unless (= 0 (skip-chars-backward " \t"))
526 (setq space (buffer-substring-no-properties (point) b)))
527 (setq eword (rfc2047-encode-1
529 (mm-replace-in-string
530 (buffer-substring-no-properties b e)
531 "\n\\([ \t]?\\)" "\\1")
533 (or (cdr (assq encoding
534 rfc2047-encode-function-alist))
536 (concat "=?" (downcase (symbol-name mime-charset))
537 "?" (upcase (symbol-name encoding)) "?")
539 (delete-region (if (eq (aref eword 0) ?\n)
545 (looking-at "[ \t\n)]"))
550 (defun rfc2047-fold-field ()
551 "Fold the current header field."
554 (rfc2047-narrow-to-field)
555 (rfc2047-fold-region (point-min) (point-max)))))
557 (defun rfc2047-fold-region (b e)
558 "Fold long lines in region B to E."
560 (narrow-to-region b e)
561 (goto-char (point-min))
565 (bol (save-restriction
569 (when (and (or break qword-break)
570 (> (- (point) bol) 76))
571 (goto-char (or break qword-break))
574 (skip-chars-backward " \t")
575 (if (looking-at "[ \t]")
578 (setq bol (1- (point)))
579 ;; Don't break before the first non-LWSP characters.
580 (skip-chars-forward " \t")
584 ((eq (char-after) ?\n)
589 (skip-chars-forward " \t")
590 (unless (or (eobp) (eq (char-after) ?\n))
592 ((eq (char-after) ?\r)
594 ((memq (char-after) '(? ?\t))
595 (skip-chars-forward " \t")
596 (unless first ;; Don't break just after the header name.
597 (setq break (point))))
599 (if (not (looking-at "=\\?[^=]"))
600 (if (eq (char-after) ?=)
602 (skip-chars-forward "^ \t\n\r="))
603 ;; Don't break at the start of the field.
604 (unless (= (point) b)
605 (setq qword-break (point)))
606 (skip-chars-forward "^ \t\n\r")))
608 (skip-chars-forward "^ \t\n\r")))
610 (when (and (or break qword-break)
611 (> (- (point) bol) 76))
612 (goto-char (or break qword-break))
615 (if (looking-at "[ \t]")
618 (setq bol (1- (point)))
619 ;; Don't break before the first non-LWSP characters.
620 (skip-chars-forward " \t")
622 (forward-char 1))))))
624 (defun rfc2047-unfold-field ()
625 "Fold the current line."
628 (rfc2047-narrow-to-field)
629 (rfc2047-unfold-region (point-min) (point-max)))))
631 (defun rfc2047-unfold-region (b e)
632 "Unfold lines in region B to E."
634 (narrow-to-region b e)
635 (goto-char (point-min))
636 (let ((bol (save-restriction
639 (eol (point-at-eol)))
642 (if (and (looking-at "[ \t]")
643 (< (- (point-at-eol) bol) 76))
644 (delete-region eol (progn
646 (skip-chars-forward "\r\n")
648 (setq bol (point-at-bol)))
649 (setq eol (point-at-eol))
652 (defun rfc2047-b-encode-string (string)
653 "Base64-encode the header contained in STRING."
654 (base64-encode-string string t))
656 (defun rfc2047-q-encode-string (string)
657 "Quoted-printable-encode the header in STRING."
658 (mm-with-unibyte-buffer
660 (quoted-printable-encode-region
661 (point-min) (point-max) nil
662 ;; = (\075), _ (\137), ? (\077) are used in the encoded word.
663 ;; Avoid using 8bit characters.
664 ;; This list excludes `especials' (see the RFC2047 syntax),
665 ;; meaning that some characters in non-structured fields will
666 ;; get encoded when they con't need to be. The following is
667 ;; what it used to be.
668 ;;; ;; Equivalent to "^\000-\007\011\013\015-\037\200-\377=_?"
669 ;;; "\010\012\014\040-\074\076\100-\136\140-\177")
670 "-\b\n\f !#-'*+0-9A-Z\\^`-~\d")
671 (subst-char-in-region (point-min) (point-max) ? ?_)
675 ;;; Functions for decoding RFC2047 messages
679 (defconst rfc2047-encoded-word-regexp
680 "=\\?\\([^][\000-\040()<>@,\;:*\\\"/?.=]+\\)\\(?:\\*[^?]+\\)?\
681 \\?\\(B\\|Q\\)\\?\\([!->@-~ ]*\\)\\?="))
683 ;; Fixme: This should decode in place, not cons intermediate strings.
684 ;; Also check whether it needs to worry about delimiting fields like
687 ;; In fact it's reported that (invalid) encoding of mailboxes in
688 ;; addr-specs is in use, so delimiting fields might help. Probably
689 ;; not decoding a word which isn't properly delimited is good enough
690 ;; and worthwhile (is it more correct or not?), e.g. something like
691 ;; `=?iso-8859-1?q?foo?=@'.
693 (defun rfc2047-decode-region (start end)
694 "Decode MIME-encoded words in region between START and END."
696 (let ((case-fold-search t)
700 (narrow-to-region start end)
701 (goto-char (point-min))
702 ;; Remove whitespace between encoded words.
703 (while (re-search-forward
705 (concat "\\(" rfc2047-encoded-word-regexp "\\)"
707 "\\(" rfc2047-encoded-word-regexp "\\)"))
709 (delete-region (goto-char (match-end 1)) (match-beginning 6)))
710 ;; Decode the encoded words.
711 (setq b (goto-char (point-min)))
712 (while (re-search-forward rfc2047-encoded-word-regexp nil t)
713 (setq e (match-beginning 0))
714 (insert (rfc2047-parse-and-decode
717 (delete-region (match-beginning 0) (match-end 0)))))
718 ;; Remove newlines between decoded words, though such things
719 ;; essentially must not be there.
721 (narrow-to-region e (point))
723 (while (re-search-forward "[\n\r]+" nil t)
725 (goto-char (point-max)))
726 (when (and (mm-multibyte-p)
728 (not (eq mail-parse-charset 'us-ascii))
729 (not (eq mail-parse-charset 'gnus-decoded)))
730 (mm-decode-coding-region b e mail-parse-charset))
732 (when (and (mm-multibyte-p)
734 (not (eq mail-parse-charset 'us-ascii))
735 (not (eq mail-parse-charset 'gnus-decoded)))
736 (mm-decode-coding-region b (point-max) mail-parse-charset))))))
738 (defun rfc2047-decode-string (string)
739 "Decode the quoted-printable-encoded STRING and return the results."
740 (let ((m (mm-multibyte-p)))
741 (if (string-match "=\\?" string)
743 ;; Fixme: This logic is wrong, but seems to be required by
744 ;; Gnus summary buffer generation. The value of `m' depends
745 ;; on the current buffer, not global multibyteness or that
746 ;; of the string. Also the string returned should always be
747 ;; multibyte in a multibyte session, i.e. the buffer should
748 ;; be multibyte before `buffer-string' is called.
750 (mm-enable-multibyte))
753 (rfc2047-decode-region (point-min) (point-max)))
755 ;; Fixme: As above, `m' here is inappropriate.
758 (not (eq mail-parse-charset 'us-ascii))
759 (not (eq mail-parse-charset 'gnus-decoded)))
760 ;; `decode-coding-string' in Emacs offers a third optional
761 ;; arg NOCOPY to avoid consing a new string if the decoding
762 ;; is "trivial". Unfortunately it currently doesn't
763 ;; consider anything else than a `nil' coding system
765 ;; `rfc2047-decode-string' is called multiple times for each
766 ;; article during summary buffer generation, and we really
767 ;; want to avoid unnecessary consing. So we bypass
768 ;; `decode-coding-string' if the string is purely ASCII.
769 (if (and (fboundp 'detect-coding-string)
770 ;; string is purely ASCII
771 (eq (detect-coding-string string t) 'undecided))
773 (mm-decode-coding-string string mail-parse-charset))
774 (mm-string-as-multibyte string)))))
776 (defun rfc2047-parse-and-decode (word)
777 "Decode WORD and return it if it is an encoded word.
778 Return WORD if it is not not an encoded word or if the charset isn't
780 (if (not (string-match rfc2047-encoded-word-regexp word))
785 (match-string 1 word)
786 (string-to-char (match-string 2 word))
787 (match-string 3 word))
789 word))) ; un-decodable
791 (defun rfc2047-pad-base64 (string)
792 "Pad STRING to quartets."
793 ;; Be more liberal to accept buggy base64 strings. If
794 ;; base64-decode-string accepts buggy strings, this function could
795 ;; be aliased to identity.
796 (if (= 0 (mod (length string) 4))
798 (when (string-match "=+$" string)
799 (setq string (substring string 0 (match-beginning 0))))
800 (case (mod (length string) 4)
802 (1 string) ;; Error, don't pad it.
803 (2 (concat string "=="))
804 (3 (concat string "=")))))
806 (defun rfc2047-decode (charset encoding string)
807 "Decode STRING from the given MIME CHARSET in the given ENCODING.
808 Valid ENCODINGs are the characters \"B\" and \"Q\".
809 If your Emacs implementation can't decode CHARSET, return nil."
810 (if (stringp charset)
811 (setq charset (intern (downcase charset))))
812 (if (or (not charset)
813 (eq 'gnus-all mail-parse-ignored-charsets)
814 (memq 'gnus-all mail-parse-ignored-charsets)
815 (memq charset mail-parse-ignored-charsets))
816 (setq charset mail-parse-charset))
817 (let ((cs (mm-charset-to-coding-system charset)))
818 (if (and (not cs) charset
819 (listp mail-parse-ignored-charsets)
820 (memq 'gnus-unknown mail-parse-ignored-charsets))
821 (setq cs (mm-charset-to-coding-system mail-parse-charset)))
824 (setq cs (or mail-parse-charset 'raw-text)))
825 (mm-decode-coding-string
827 ((char-equal ?B encoding)
828 (base64-decode-string
829 (rfc2047-pad-base64 string)))
830 ((char-equal ?Q encoding)
831 (quoted-printable-decode-string
832 (mm-subst-char-in-string ?_ ? string t)))
833 (t (error "Invalid encoding: %c" encoding)))
838 ;;; arch-tag: a07fe3d4-22b5-4c4a-bd89-b1f82d5d36f6
839 ;;; rfc2047.el ends here