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
174 (goto-char (point-min))
175 (skip-chars-forward "^:")
176 (when (looking-at ": ")
180 ;; We found something that may perhaps be encoded.
182 alist rfc2047-header-encoding-alist)
183 (while (setq elem (pop alist))
184 (when (or (and (stringp (car elem))
185 (looking-at (car elem)))
189 (goto-char (point-min))
190 (re-search-forward "^[^:]+: *" nil t)
192 ((eq method 'address-mime)
193 (rfc2047-encode-region (point) (point-max)))
195 (let ((rfc2047-encoding-type 'mime))
196 (rfc2047-encode-region (point) (point-max))))
197 ((eq method 'default)
198 (if (and (featurep 'mule)
199 (if (boundp 'default-enable-multibyte-characters)
200 default-enable-multibyte-characters)
202 (mm-encode-coding-region (point) (point-max)
203 mail-parse-charset)))
204 ;; We get this when CC'ing messsages to newsgroups with
205 ;; 8-bit names. The group name mail copy just got
206 ;; unconditionally encoded. Previously, it would ask
207 ;; whether to encode, which was quite confusing for the
208 ;; user. If the new behaviour is wrong, tell me. I have
209 ;; left the old code commented out below.
210 ;; -- Per Abrahamsen <abraham@dina.kvl.dk> Date: 2001-10-07.
211 ;; Modified by Dave Love, with the commented-out code changed
212 ;; in accordance with changes elsewhere.
214 (rfc2047-encode-region (point) (point-max)))
216 ;;; (if (or (message-options-get
217 ;;; 'rfc2047-encode-message-header-encode-any)
218 ;;; (message-options-set
219 ;;; 'rfc2047-encode-message-header-encode-any
221 ;;; "Some texts are not encoded. Encode anyway?")))
222 ;;; (rfc2047-encode-region (point-min) (point-max))
223 ;;; (error "Cannot send unencoded text")))
224 ((mm-coding-system-p method)
225 (if (and (featurep 'mule)
226 (if (boundp 'default-enable-multibyte-characters)
227 default-enable-multibyte-characters))
228 (mm-encode-coding-region (point) (point-max) method)))
231 (goto-char (point-max)))))))
233 ;; Fixme: This, and the require below may not be the Right Thing, but
234 ;; should be safe just before release. -- fx 2001-02-08
235 (eval-when-compile (defvar message-posting-charset))
237 (defun rfc2047-encodable-p ()
238 "Return non-nil if any characters in current buffer need encoding in headers.
239 The buffer may be narrowed."
240 (require 'message) ; for message-posting-charset
242 (mm-find-mime-charset-region (point-min) (point-max))))
243 (goto-char (point-min))
244 (or (and rfc2047-encode-encoded-words
246 (search-forward "=?" nil t)
247 (goto-char (point-min))))
249 (not (equal charsets (list (car message-posting-charset))))))))
251 ;; Use this syntax table when parsing into regions that may need
252 ;; encoding. Double quotes are string delimiters, backslash is
253 ;; character quoting, and all other RFC 2822 special characters are
254 ;; treated as punctuation so we can use forward-sexp/forward-word to
255 ;; skip to the end of regions appropriately. Nb. ietf-drums does
256 ;; things differently.
257 (defconst rfc2047-syntax-table
258 ;; (make-char-table 'syntax-table '(2)) only works in Emacs.
259 (let ((table (make-syntax-table)))
260 ;; The following is done to work for setting all elements of the table
261 ;; in Emacs 21 and 22 and XEmacs; it appears to be the cleanest way.
262 ;; Play safe and don't assume the form of the word syntax entry --
264 (if (fboundp 'set-char-table-range) ; Emacs
265 (funcall (intern "set-char-table-range")
266 table t (aref (standard-syntax-table) ?a))
267 (if (fboundp 'put-char-table)
268 (if (fboundp 'get-char-table) ; warning avoidance
269 (put-char-table t (get-char-table ?a (standard-syntax-table))
271 (modify-syntax-entry ?\\ "\\" 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)
285 (defun rfc2047-encode-region (b e)
286 "Encode words in region B to E that need encoding.
287 By default, the region is treated as containing RFC2822 addresses.
288 Dynamically bind `rfc2047-encoding-type' to change that."
290 (narrow-to-region b e)
291 (let ((encodable-regexp (if rfc2047-encode-encoded-words
292 "[^\000-\177]+\\|=\\?"
294 start ; start of current token
296 ;; Whether there's an encoded word before the current token,
297 ;; either immediately or separated by space.
299 (if (eq 'mime rfc2047-encoding-type)
300 ;; Simple case. Continuous words in which all those contain
301 ;; non-ASCII characters are encoded collectively. Encoding
302 ;; ASCII words, including `Re:' used in Subject headers, is
303 ;; avoided for interoperability with non-MIME clients and
304 ;; for making it easy to find keywords.
306 (goto-char (point-min))
307 (while (progn (skip-chars-forward " \t\n")
310 (while (and (looking-at "[ \t\n]*\\([^ \t\n]+\\)")
312 (setq end (match-end 0))
313 (re-search-forward encodable-regexp end t)))
315 (if (> (point) start)
316 (rfc2047-encode start (point))
318 ;; `address-mime' case -- take care of quoted words, comments.
319 (with-syntax-table rfc2047-syntax-table
320 (goto-char (point-min))
321 (condition-case nil ; in case of unbalanced quotes
322 ;; Look for rfc2822-style: sequences of atoms, quoted
323 ;; strings, specials, whitespace. (Specials mustn't be
327 (skip-chars-forward " \t\n")
330 ((not (char-after))) ; eob
332 ((eq ?\" (char-syntax (char-after)))
336 ;; Does it need encoding?
338 (if (re-search-forward encodable-regexp end 'move)
339 ;; It needs encoding. Strip the quotes first,
340 ;; since encoded words can't occur in quotes.
343 (delete-backward-char 1)
347 ;; There was a preceding quoted word. We need
348 ;; to include any separating whitespace in this
349 ;; word to avoid it getting lost.
350 (skip-chars-backward " \t")
351 ;; A space is needed between the encoded words.
355 ;; Adjust the end position for the deleted quotes.
356 (rfc2047-encode start (- end 2))
357 (setq last-encoded t)) ; record that it was encoded
358 (setq last-encoded nil)))
359 ((eq ?. (char-syntax (char-after)))
360 ;; Skip other delimiters, but record that they've
361 ;; potentially separated quoted words.
363 (setq last-encoded nil))
364 (t ; normal token/whitespace sequence
367 (skip-chars-backward " \t\n")
368 (eq (char-before) ?\()
370 ;; Look for the end of parentheses.
371 (let ((string (buffer-substring (point)
373 (default-major-mode 'fundamental-mode))
374 ;; Use `standard-syntax-table'.
377 (goto-char (point-min))
381 (setq end (+ start (point) -3)))
383 ;; Encode text as an unstructured field.
384 (let ((rfc2047-encoding-type 'mime))
385 (rfc2047-encode-region start end)
387 ;; Skip one ASCII word, or encode continuous words
388 ;; in which all those contain non-ASCII characters.
391 (when (looking-at "[\000-\177]+")
394 (if (re-search-forward "[ \t\n]\\|\\Sw" end 'move)
396 (setq end (match-beginning 0))
397 (if rfc2047-encode-encoded-words
400 (when (search-forward "=?" end 'move)
401 (goto-char (match-beginning 0))
407 (when (looking-at encodable-regexp)
408 (goto-char (match-end 0))
409 (while (and (looking-at "[ \t\n]+\\([^ \t\n]+\\)")
410 (setq end (match-end 0))
411 (string-match encodable-regexp
414 (when (looking-at "[^ \t\n]+")
415 (setq end (match-end 0))
416 (if (re-search-forward "\\Sw+" end t)
417 ;; There are special characters better
418 ;; to be encoded so that MTAs may parse
420 (cond ((= end (point)))
421 ((looking-at encodable-regexp)
424 (goto-char (1- (match-end 0)))
425 (unless (= (point) (match-beginning 0))
428 (skip-chars-forward " \t\n")
429 (if (and (looking-at "[^ \t\n]+")
430 (string-match encodable-regexp
433 (goto-char end)))))))
434 (skip-chars-backward " \t\n")
437 (if (re-search-forward encodable-regexp end 'move)
439 (rfc2047-encode start end)
440 (setq last-encoded t))
441 (setq last-encoded nil))))))
443 (error "Invalid data for rfc2047 encoding: %s"
444 (buffer-substring b e)))))))
445 (rfc2047-fold-region b (point))))
447 (defun rfc2047-encode-string (string)
448 "Encode words in STRING.
449 By default, the string is treated as containing addresses (see
450 `rfc2047-encoding-type')."
453 (rfc2047-encode-region (point-min) (point-max))
456 (defun rfc2047-encode-1 (column string cs encoder start space &optional eword)
457 "Subroutine used by `rfc2047-encode'."
458 (cond ((string-equal string "")
462 (string-match "\n[ \t]+\\'" eword))
463 ;; Reomove a superfluous empty line.
464 (setq eword (substring eword 0 (match-beginning 0))))
465 (rfc2047-encode-1 (length space) string cs encoder start " "
466 (concat eword "\n" space)))
469 (limit (1- (length string)))
474 (setq next (concat start
477 (mm-encode-coding-string
478 (substring string 0 (1+ index))
480 (substring string 0 (1+ index))))
482 (if (<= (+ column (length next)) 76)
487 (setq eword (concat eword next))
490 (when (string-match "\n[ \t]+\\'" eword)
491 ;; Reomove a superfluous empty line.
492 (setq eword (substring eword 0 (match-beginning 0))))
493 (rfc2047-encode-1 (length space) (substring string index)
495 (concat eword "\n" space)))))))
497 (defun rfc2047-encode (b e)
498 "Encode the word(s) in the region B to E.
499 By default, the region is treated as containing addresses (see
500 `rfc2047-encoding-type')."
501 (let ((mime-charset (or (mm-find-mime-charset-region b e) (list 'us-ascii)))
502 cs encoding space eword)
503 (cond ((> (length mime-charset) 1)
504 (error "Can't rfc2047-encode `%s'"
505 (buffer-substring-no-properties b e)))
506 ((= (length mime-charset) 1)
507 (setq mime-charset (car mime-charset)
508 cs (mm-charset-to-coding-system mime-charset))
509 (unless (and (mm-multibyte-p)
510 (mm-coding-system-p cs))
513 (narrow-to-region b e)
515 (or (cdr (assq mime-charset
516 rfc2047-charset-encoding-alist))
517 ;; For the charsets that don't have a preferred
518 ;; encoding, choose the one that's shorter.
519 (if (eq (rfc2047-qp-or-base64) 'base64)
524 (unless (= 0 (skip-chars-backward " \t"))
525 (setq space (buffer-substring-no-properties (point) b)))
526 (setq eword (rfc2047-encode-1
528 (mm-replace-in-string
529 (buffer-substring-no-properties b e)
530 "\n\\([ \t]?\\)" "\\1")
532 (or (cdr (assq encoding
533 rfc2047-encode-function-alist))
535 (concat "=?" (downcase (symbol-name mime-charset))
536 "?" (upcase (symbol-name encoding)) "?")
538 (delete-region (if (eq (aref eword 0) ?\n)
544 (looking-at "[ \t\n)]"))
549 (defun rfc2047-fold-field ()
550 "Fold the current header field."
553 (rfc2047-narrow-to-field)
554 (rfc2047-fold-region (point-min) (point-max)))))
556 (defun rfc2047-fold-region (b e)
557 "Fold long lines in region B to E."
559 (narrow-to-region b e)
560 (goto-char (point-min))
564 (bol (save-restriction
568 (when (and (or break qword-break)
569 (> (- (point) bol) 76))
570 (goto-char (or break qword-break))
573 (skip-chars-backward " \t")
574 (if (looking-at "[ \t]")
577 (setq bol (1- (point)))
578 ;; Don't break before the first non-LWSP characters.
579 (skip-chars-forward " \t")
583 ((eq (char-after) ?\n)
588 (skip-chars-forward " \t")
589 (unless (or (eobp) (eq (char-after) ?\n))
591 ((eq (char-after) ?\r)
593 ((memq (char-after) '(? ?\t))
594 (skip-chars-forward " \t")
595 (unless first ;; Don't break just after the header name.
596 (setq break (point))))
598 (if (not (looking-at "=\\?[^=]"))
599 (if (eq (char-after) ?=)
601 (skip-chars-forward "^ \t\n\r="))
602 ;; Don't break at the start of the field.
603 (unless (= (point) b)
604 (setq qword-break (point)))
605 (skip-chars-forward "^ \t\n\r")))
607 (skip-chars-forward "^ \t\n\r")))
609 (when (and (or break qword-break)
610 (> (- (point) bol) 76))
611 (goto-char (or break qword-break))
614 (if (looking-at "[ \t]")
617 (setq bol (1- (point)))
618 ;; Don't break before the first non-LWSP characters.
619 (skip-chars-forward " \t")
621 (forward-char 1))))))
623 (defun rfc2047-unfold-field ()
624 "Fold the current line."
627 (rfc2047-narrow-to-field)
628 (rfc2047-unfold-region (point-min) (point-max)))))
630 (defun rfc2047-unfold-region (b e)
631 "Unfold lines in region B to E."
633 (narrow-to-region b e)
634 (goto-char (point-min))
635 (let ((bol (save-restriction
638 (eol (point-at-eol)))
641 (if (and (looking-at "[ \t]")
642 (< (- (point-at-eol) bol) 76))
643 (delete-region eol (progn
645 (skip-chars-forward "\r\n")
647 (setq bol (point-at-bol)))
648 (setq eol (point-at-eol))
651 (defun rfc2047-b-encode-string (string)
652 "Base64-encode the header contained in STRING."
653 (base64-encode-string string t))
655 (defun rfc2047-q-encode-string (string)
656 "Quoted-printable-encode the header in STRING."
657 (mm-with-unibyte-buffer
659 (quoted-printable-encode-region
660 (point-min) (point-max) nil
661 ;; = (\075), _ (\137), ? (\077) are used in the encoded word.
662 ;; Avoid using 8bit characters.
663 ;; This list excludes `especials' (see the RFC2047 syntax),
664 ;; meaning that some characters in non-structured fields will
665 ;; get encoded when they con't need to be. The following is
666 ;; what it used to be.
667 ;;; ;; Equivalent to "^\000-\007\011\013\015-\037\200-\377=_?"
668 ;;; "\010\012\014\040-\074\076\100-\136\140-\177")
669 "-\b\n\f !#-'*+0-9A-Z\\^`-~\d")
670 (subst-char-in-region (point-min) (point-max) ? ?_)
674 ;;; Functions for decoding RFC2047 messages
678 (defconst rfc2047-encoded-word-regexp
679 "=\\?\\([^][\000-\040()<>@,\;:*\\\"/?.=]+\\)\\(?:\\*[^?]+\\)?\
680 \\?\\(B\\|Q\\)\\?\\([!->@-~ ]*\\)\\?="))
682 ;; Fixme: This should decode in place, not cons intermediate strings.
683 ;; Also check whether it needs to worry about delimiting fields like
686 ;; In fact it's reported that (invalid) encoding of mailboxes in
687 ;; addr-specs is in use, so delimiting fields might help. Probably
688 ;; not decoding a word which isn't properly delimited is good enough
689 ;; and worthwhile (is it more correct or not?), e.g. something like
690 ;; `=?iso-8859-1?q?foo?=@'.
692 (defun rfc2047-decode-region (start end)
693 "Decode MIME-encoded words in region between START and END."
695 (let ((case-fold-search t)
699 (narrow-to-region start end)
700 (goto-char (point-min))
701 ;; Remove whitespace between encoded words.
702 (while (re-search-forward
704 (concat "\\(" rfc2047-encoded-word-regexp "\\)"
706 "\\(" rfc2047-encoded-word-regexp "\\)"))
708 (delete-region (goto-char (match-end 1)) (match-beginning 6)))
709 ;; Decode the encoded words.
710 (setq b (goto-char (point-min)))
711 (while (re-search-forward rfc2047-encoded-word-regexp nil t)
712 (setq e (match-beginning 0))
713 (insert (rfc2047-parse-and-decode
716 (delete-region (match-beginning 0) (match-end 0)))))
717 ;; Remove newlines between decoded words, though such things
718 ;; essentially must not be there.
720 (narrow-to-region e (point))
722 (while (re-search-forward "[\n\r]+" nil t)
724 (goto-char (point-max)))
725 (when (and (mm-multibyte-p)
727 (not (eq mail-parse-charset 'us-ascii))
728 (not (eq mail-parse-charset 'gnus-decoded)))
729 (mm-decode-coding-region b e mail-parse-charset))
731 (when (and (mm-multibyte-p)
733 (not (eq mail-parse-charset 'us-ascii))
734 (not (eq mail-parse-charset 'gnus-decoded)))
735 (mm-decode-coding-region b (point-max) mail-parse-charset))))))
737 (defun rfc2047-decode-string (string)
738 "Decode the quoted-printable-encoded STRING and return the results."
739 (let ((m (mm-multibyte-p)))
740 (if (string-match "=\\?" string)
742 ;; Fixme: This logic is wrong, but seems to be required by
743 ;; Gnus summary buffer generation. The value of `m' depends
744 ;; on the current buffer, not global multibyteness or that
745 ;; of the string. Also the string returned should always be
746 ;; multibyte in a multibyte session, i.e. the buffer should
747 ;; be multibyte before `buffer-string' is called.
749 (mm-enable-multibyte))
752 (rfc2047-decode-region (point-min) (point-max)))
754 ;; Fixme: As above, `m' here is inappropriate.
757 (not (eq mail-parse-charset 'us-ascii))
758 (not (eq mail-parse-charset 'gnus-decoded)))
759 ;; `decode-coding-string' in Emacs offers a third optional
760 ;; arg NOCOPY to avoid consing a new string if the decoding
761 ;; is "trivial". Unfortunately it currently doesn't
762 ;; consider anything else than a `nil' coding system
764 ;; `rfc2047-decode-string' is called multiple times for each
765 ;; article during summary buffer generation, and we really
766 ;; want to avoid unnecessary consing. So we bypass
767 ;; `decode-coding-string' if the string is purely ASCII.
768 (if (and (fboundp 'detect-coding-string)
769 ;; string is purely ASCII
770 (eq (detect-coding-string string t) 'undecided))
772 (mm-decode-coding-string string mail-parse-charset))
773 (mm-string-as-multibyte string)))))
775 (defun rfc2047-parse-and-decode (word)
776 "Decode WORD and return it if it is an encoded word.
777 Return WORD if it is not not an encoded word or if the charset isn't
779 (if (not (string-match rfc2047-encoded-word-regexp word))
784 (match-string 1 word)
785 (string-to-char (match-string 2 word))
786 (match-string 3 word))
788 word))) ; un-decodable
790 (defun rfc2047-pad-base64 (string)
791 "Pad STRING to quartets."
792 ;; Be more liberal to accept buggy base64 strings. If
793 ;; base64-decode-string accepts buggy strings, this function could
794 ;; be aliased to identity.
795 (if (= 0 (mod (length string) 4))
797 (when (string-match "=+$" string)
798 (setq string (substring string 0 (match-beginning 0))))
799 (case (mod (length string) 4)
801 (1 string) ;; Error, don't pad it.
802 (2 (concat string "=="))
803 (3 (concat string "=")))))
805 (defun rfc2047-decode (charset encoding string)
806 "Decode STRING from the given MIME CHARSET in the given ENCODING.
807 Valid ENCODINGs are the characters \"B\" and \"Q\".
808 If your Emacs implementation can't decode CHARSET, return nil."
809 (if (stringp charset)
810 (setq charset (intern (downcase charset))))
811 (if (or (not charset)
812 (eq 'gnus-all mail-parse-ignored-charsets)
813 (memq 'gnus-all mail-parse-ignored-charsets)
814 (memq charset mail-parse-ignored-charsets))
815 (setq charset mail-parse-charset))
816 (let ((cs (mm-charset-to-coding-system charset)))
817 (if (and (not cs) charset
818 (listp mail-parse-ignored-charsets)
819 (memq 'gnus-unknown mail-parse-ignored-charsets))
820 (setq cs (mm-charset-to-coding-system mail-parse-charset)))
823 (setq cs (or mail-parse-charset 'raw-text)))
824 (mm-decode-coding-string
826 ((char-equal ?B encoding)
827 (base64-decode-string
828 (rfc2047-pad-base64 string)))
829 ((char-equal ?Q encoding)
830 (quoted-printable-decode-string
831 (mm-subst-char-in-string ?_ ? string t)))
832 (t (error "Invalid encoding: %c" encoding)))
837 ;;; rfc2047.el ends here