;;; qp.el --- Quoted-Printable functions
-;; Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
+
+;; Copyright (C) 1998-2015 Free Software Foundation, Inc.
;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
+;; Keywords: mail, extensions
+
;; This file is part of GNU Emacs.
-;; GNU Emacs is free software; you can redistribute it and/or modify
+;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation; either version 2, or (at your option)
-;; any later version.
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
-;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
-;; along with GNU Emacs; see the file COPYING. If not, write to the
-;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-;; Boston, MA 02111-1307, USA.
+;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
+;; Functions for encoding and decoding quoted-printable text as
+;; defined in RFC 2045.
+
;;; Code:
(require 'mm-util)
-
-(defvar quoted-printable-encoding-characters
- (mapcar 'identity "0123456789ABCDEFabcdef"))
-
-(defun quoted-printable-decode-region (from to &optional charset)
- "Decode quoted-printable in the region between FROM and TO.
-If CHARSET is non-nil, decode the region with charset."
- (interactive "r")
+(defvar mm-use-ultra-safe-encoding)
+
+;;;###autoload
+(defun quoted-printable-decode-region (from to &optional coding-system)
+ "Decode quoted-printable in the region between FROM and TO, per RFC 2045.
+If CODING-SYSTEM is non-nil, decode bytes into characters with that
+coding-system.
+
+Interactively, you can supply the CODING-SYSTEM argument
+with \\[universal-coding-system-argument].
+
+The CODING-SYSTEM argument is a historical hangover and is deprecated.
+QP encodes raw bytes and should be decoded into raw bytes. Decoding
+them into characters should be done separately."
+ (interactive
+ ;; Let the user determine the coding system with "C-x RET c".
+ (list (region-beginning) (region-end) coding-system-for-read))
+ (unless (mm-coding-system-p coding-system) ; e.g. `ascii' from Gnus
+ (setq coding-system nil))
(save-excursion
(save-restriction
- (let (start)
+ ;; RFC 2045: ``An "=" followed by two hexadecimal digits, one
+ ;; or both of which are lowercase letters in "abcdef", is
+ ;; formally illegal. A robust implementation might choose to
+ ;; recognize them as the corresponding uppercase letters.''
+ (let ((case-fold-search t))
(narrow-to-region from to)
- (goto-char from)
- (while (not (eobp))
- (cond
- ((eq (char-after) ?=)
- (delete-char 1)
- (unless start
- (setq start (point)))
- (cond
- ;; End of the line.
- ((eq (char-after) ?\n)
- (delete-char 1))
- ;; Encoded character.
- ((and
- (memq (char-after) quoted-printable-encoding-characters)
- (memq (char-after (1+ (point)))
- quoted-printable-encoding-characters))
- (insert
- (string-to-number
- (buffer-substring (point) (+ 2 (point)))
- 16))
- (delete-char 2))
- ;; Quoted equal sign.
- ((eq (char-after) ?=)
- (forward-char 1))
- ;; End of buffer.
- ((eobp))
- ;; Invalid.
- (t
- (message "Malformed MIME quoted-printable message"))))
- ((and charset start (not (eq (mm-charset-after) 'ascii)))
- (mm-decode-coding-region start (point) charset)
- (setq start nil)
- (forward-char 1))
- (t
- (forward-char 1))))
- (if (and charset start)
- (mm-decode-coding-region start (point) charset))))))
-
-(defun quoted-printable-decode-string (string &optional charset)
- "Decode the quoted-printable-encoded STRING and return the results.
-If CHARSET is non-nil, decode the region with charset."
- (with-temp-buffer
+ ;; Do this in case we're called from Gnus, say, in a buffer
+ ;; which already contains non-ASCII characters which would
+ ;; then get doubly-decoded below.
+ (if coding-system
+ (mm-encode-coding-region (point-min) (point-max) coding-system))
+ (goto-char (point-min))
+ (while (and (skip-chars-forward "^=")
+ (not (eobp)))
+ (cond ((eq (char-after (1+ (point))) ?\n)
+ (delete-char 2))
+ ((looking-at "\\(=[0-9A-F][0-9A-F]\\)+")
+ ;; Decode this sequence at once; i.e. by a single
+ ;; deletion and insertion.
+ (let* ((n (/ (- (match-end 0) (point)) 3))
+ (str (make-string n 0)))
+ (dotimes (i n)
+ (let ((n1 (char-after (1+ (point))))
+ (n2 (char-after (+ 2 (point)))))
+ (aset str i
+ (+ (* 16 (- n1 (if (<= n1 ?9) ?0
+ (if (<= n1 ?F) (- ?A 10)
+ (- ?a 10)))))
+ (- n2 (if (<= n2 ?9) ?0
+ (if (<= n2 ?F) (- ?A 10)
+ (- ?a 10)))))))
+ (forward-char 3))
+ (delete-region (match-beginning 0) (match-end 0))
+ (insert str)))
+ (t
+ (message "Malformed quoted-printable text")
+ (forward-char)))))
+ (if coding-system
+ (mm-decode-coding-region (point-min) (point-max) coding-system)))))
+
+(defun quoted-printable-decode-string (string &optional coding-system)
+ "Decode the quoted-printable encoded STRING and return the result.
+If CODING-SYSTEM is non-nil, decode the string with coding-system.
+Use of CODING-SYSTEM is deprecated; this function should deal with
+raw bytes, and coding conversion should be done separately."
+ (mm-with-unibyte-buffer
(insert string)
- (quoted-printable-decode-region (point-min) (point-max) charset)
+ (quoted-printable-decode-region (point-min) (point-max) coding-system)
(buffer-string)))
(defun quoted-printable-encode-region (from to &optional fold class)
- "QP-encode the region between FROM and TO.
+ "Quoted-printable encode the region between FROM and TO per RFC 2045.
-If FOLD fold long lines. If CLASS, translate the characters
-matched by that regexp.
+If FOLD,&nb