qp.el (quoted-printable-decode-region): Decode multiple bytes at once.
[emacs.git] / lisp / gnus / qp.el
blob87252684a4851f41979ec9f1fc092677b34cfc45
1 ;;; qp.el --- Quoted-Printable functions
3 ;; Copyright (C) 1998-2012 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: mail, extensions
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 3 of the License, or
13 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; Functions for encoding and decoding quoted-printable text as
26 ;; defined in RFC 2045.
28 ;;; Code:
30 (require 'mm-util)
31 (defvar mm-use-ultra-safe-encoding)
33 ;;;###autoload
34 (defun quoted-printable-decode-region (from to &optional coding-system)
35 "Decode quoted-printable in the region between FROM and TO, per RFC 2045.
36 If CODING-SYSTEM is non-nil, decode bytes into characters with that
37 coding-system.
39 Interactively, you can supply the CODING-SYSTEM argument
40 with \\[universal-coding-system-argument].
42 The CODING-SYSTEM argument is a historical hangover and is deprecated.
43 QP encodes raw bytes and should be decoded into raw bytes. Decoding
44 them into characters should be done separately."
45 (interactive
46 ;; Let the user determine the coding system with "C-x RET c".
47 (list (region-beginning) (region-end) coding-system-for-read))
48 (unless (mm-coding-system-p coding-system) ; e.g. `ascii' from Gnus
49 (setq coding-system nil))
50 (save-excursion
51 (save-restriction
52 ;; RFC 2045: ``An "=" followed by two hexadecimal digits, one
53 ;; or both of which are lowercase letters in "abcdef", is
54 ;; formally illegal. A robust implementation might choose to
55 ;; recognize them as the corresponding uppercase letters.''
56 (let ((case-fold-search t)
57 (decode-hex #'(lambda (n1 n2)
58 (+ (* (if (<= n1 ?9) (- n1 ?0) (+ (- n1 ?A) 10)) 16)
59 (if (<= n2 ?9) (- n2 ?0) (+ (- n2 ?A) 10))))))
60 (narrow-to-region from to)
61 ;; Do this in case we're called from Gnus, say, in a buffer
62 ;; which already contains non-ASCII characters which would
63 ;; then get doubly-decoded below.
64 (if coding-system
65 (mm-encode-coding-region (point-min) (point-max) coding-system))
66 (goto-char (point-min))
67 (while (and (skip-chars-forward "^=")
68 (not (eobp)))
69 (cond ((eq (char-after (1+ (point))) ?\n)
70 (delete-char 2))
71 ((looking-at "\\(=[0-9A-F][0-9A-F]\\)+")
72 ;; Decode this sequence at once; i.e. by a single
73 ;; deletion and insertion.
74 (let* ((n (/ (- (match-end 0) (point)) 3))
75 (str (make-string n 0)))
76 (dotimes (i n)
77 (aset str i (funcall decode-hex (char-after (1+ (point)))
78 (char-after (+ 2 (point)))))
79 (forward-char 3))
80 (delete-region (match-beginning 0) (match-end 0))
81 (insert str)))
83 (message "Malformed quoted-printable text")
84 (forward-char)))))
85 (if coding-system
86 (mm-decode-coding-region (point-min) (point-max) coding-system)))))
88 (defun quoted-printable-decode-string (string &optional coding-system)
89 "Decode the quoted-printable encoded STRING and return the result.
90 If CODING-SYSTEM is non-nil, decode the string with coding-system.
91 Use of CODING-SYSTEM is deprecated; this function should deal with
92 raw bytes, and coding conversion should be done separately."
93 (mm-with-unibyte-buffer
94 (insert string)
95 (quoted-printable-decode-region (point-min) (point-max) coding-system)
96 (buffer-string)))
98 (defun quoted-printable-encode-region (from to &optional fold class)
99 "Quoted-printable encode the region between FROM and TO per RFC 2045.
101 If FOLD, fold long lines at 76 characters (as required by the RFC).
102 If CLASS is non-nil, translate the characters not matched by that
103 regexp class, which is in the form expected by `skip-chars-forward'.
104 You should probably avoid non-ASCII characters in this arg.
106 If `mm-use-ultra-safe-encoding' is set, fold lines unconditionally and
107 encode lines starting with \"From\"."
108 (interactive "r")
109 (unless class
110 ;; Avoid using 8bit characters. = is \075.
111 ;; Equivalent to "^\000-\007\013\015-\037\200-\377="
112 (setq class "\010-\012\014\040-\074\076-\177"))
113 (save-excursion
114 (goto-char from)
115 (if (re-search-forward (mm-string-to-multibyte "[^\x0-\x7f\x80-\xff]")
116 to t)
117 (error "Multibyte character in QP encoding region"))
118 (save-restriction
119 (narrow-to-region from to)
120 ;; Encode all the non-ascii and control characters.
121 (goto-char (point-min))
122 (while (and (skip-chars-forward class)
123 (not (eobp)))
124 (insert
125 (prog1
126 ;; To unibyte in case of Emacs 23 (unicode) eight-bit.
127 (format "=%02X" (mm-multibyte-char-to-unibyte (char-after)))
128 (delete-char 1))))
129 ;; Encode white space at the end of lines.
130 (goto-char (point-min))
131 (while (re-search-forward "[ \t]+$" nil t)
132 (goto-char (match-beginning 0))
133 (while (not (eolp))
134 (insert
135 (prog1
136 (format "=%02X" (char-after))
137 (delete-char 1)))))
138 (let ((mm-use-ultra-safe-encoding
139 (and (boundp 'mm-use-ultra-safe-encoding)
140 mm-use-ultra-safe-encoding)))
141 (when (or fold mm-use-ultra-safe-encoding)
142 (let ((tab-width 1)) ; HTAB is one character.
143 (goto-char (point-min))
144 (while (not (eobp))
145 ;; In ultra-safe mode, encode "From " at the beginning
146 ;; of a line.
147 (when mm-use-ultra-safe-encoding
148 (if (looking-at "From ")
149 (replace-match "From=20" nil t)
150 (if (looking-at "-")
151 (replace-match "=2D" nil t))))
152 (end-of-line)
153 ;; Fold long lines.
154 (while (> (current-column) 76) ; tab-width must be 1.
155 (beginning-of-line)
156 (forward-char 75) ; 75 chars plus an "="
157 (search-backward "=" (- (point) 2) t)
158 (insert "=\n")
159 (end-of-line))
160 (forward-line))))))))
162 (defun quoted-printable-encode-string (string)
163 "Encode the STRING as quoted-printable and return the result."
164 (with-temp-buffer
165 (if (mm-multibyte-string-p string)
166 (mm-enable-multibyte)
167 (mm-disable-multibyte))
168 (insert string)
169 (quoted-printable-encode-region (point-min) (point-max))
170 (buffer-string)))
172 (provide 'qp)
174 ;;; qp.el ends here