- (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