qp.el 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. ;;; qp.el --- Quoted-Printable functions
  2. ;; Copyright (C) 1998-2012 Free Software Foundation, Inc.
  3. ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
  4. ;; Keywords: mail, extensions
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; Functions for encoding and decoding quoted-printable text as
  18. ;; defined in RFC 2045.
  19. ;;; Code:
  20. (require 'mm-util)
  21. (defvar mm-use-ultra-safe-encoding)
  22. ;;;###autoload
  23. (defun quoted-printable-decode-region (from to &optional coding-system)
  24. "Decode quoted-printable in the region between FROM and TO, per RFC 2045.
  25. If CODING-SYSTEM is non-nil, decode bytes into characters with that
  26. coding-system.
  27. Interactively, you can supply the CODING-SYSTEM argument
  28. with \\[universal-coding-system-argument].
  29. The CODING-SYSTEM argument is a historical hangover and is deprecated.
  30. QP encodes raw bytes and should be decoded into raw bytes. Decoding
  31. them into characters should be done separately."
  32. (interactive
  33. ;; Let the user determine the coding system with "C-x RET c".
  34. (list (region-beginning) (region-end) coding-system-for-read))
  35. (unless (mm-coding-system-p coding-system) ; e.g. `ascii' from Gnus
  36. (setq coding-system nil))
  37. (save-excursion
  38. (save-restriction
  39. ;; RFC 2045: ``An "=" followed by two hexadecimal digits, one
  40. ;; or both of which are lowercase letters in "abcdef", is
  41. ;; formally illegal. A robust implementation might choose to
  42. ;; recognize them as the corresponding uppercase letters.''
  43. (let ((case-fold-search t))
  44. (narrow-to-region from to)
  45. ;; Do this in case we're called from Gnus, say, in a buffer
  46. ;; which already contains non-ASCII characters which would
  47. ;; then get doubly-decoded below.
  48. (if coding-system
  49. (mm-encode-coding-region (point-min) (point-max) coding-system))
  50. (goto-char (point-min))
  51. (while (and (skip-chars-forward "^=")
  52. (not (eobp)))
  53. (cond ((eq (char-after (1+ (point))) ?\n)
  54. (delete-char 2))
  55. ((looking-at "=[0-9A-F][0-9A-F]")
  56. (let ((byte (string-to-number (buffer-substring (1+ (point))
  57. (+ 3 (point)))
  58. 16)))
  59. (mm-insert-byte byte 1)
  60. (delete-char 3)))
  61. (t
  62. (message "Malformed quoted-printable text")
  63. (forward-char)))))
  64. (if coding-system
  65. (mm-decode-coding-region (point-min) (point-max) coding-system)))))
  66. (defun quoted-printable-decode-string (string &optional coding-system)
  67. "Decode the quoted-printable encoded STRING and return the result.
  68. If CODING-SYSTEM is non-nil, decode the string with coding-system.
  69. Use of CODING-SYSTEM is deprecated; this function should deal with
  70. raw bytes, and coding conversion should be done separately."
  71. (mm-with-unibyte-buffer
  72. (insert string)
  73. (quoted-printable-decode-region (point-min) (point-max) coding-system)
  74. (buffer-string)))
  75. (defun quoted-printable-encode-region (from to &optional fold class)
  76. "Quoted-printable encode the region between FROM and TO per RFC 2045.
  77. If FOLD, fold long lines at 76 characters (as required by the RFC).
  78. If CLASS is non-nil, translate the characters not matched by that
  79. regexp class, which is in the form expected by `skip-chars-forward'.
  80. You should probably avoid non-ASCII characters in this arg.
  81. If `mm-use-ultra-safe-encoding' is set, fold lines unconditionally and
  82. encode lines starting with \"From\"."
  83. (interactive "r")
  84. (unless class
  85. ;; Avoid using 8bit characters. = is \075.
  86. ;; Equivalent to "^\000-\007\013\015-\037\200-\377="
  87. (setq class "\010-\012\014\040-\074\076-\177"))
  88. (save-excursion
  89. (goto-char from)
  90. (if (re-search-forward (mm-string-to-multibyte "[^\x0-\x7f\x80-\xff]")
  91. to t)
  92. (error "Multibyte character in QP encoding region"))
  93. (save-restriction
  94. (narrow-to-region from to)
  95. ;; Encode all the non-ascii and control characters.
  96. (goto-char (point-min))
  97. (while (and (skip-chars-forward class)
  98. (not (eobp)))
  99. (insert
  100. (prog1
  101. ;; To unibyte in case of Emacs 23 (unicode) eight-bit.
  102. (format "=%02X" (mm-multibyte-char-to-unibyte (char-after)))
  103. (delete-char 1))))
  104. ;; Encode white space at the end of lines.
  105. (goto-char (point-min))
  106. (while (re-search-forward "[ \t]+$" nil t)
  107. (goto-char (match-beginning 0))
  108. (while (not (eolp))
  109. (insert
  110. (prog1
  111. (format "=%02X" (char-after))
  112. (delete-char 1)))))
  113. (let ((mm-use-ultra-safe-encoding
  114. (and (boundp 'mm-use-ultra-safe-encoding)
  115. mm-use-ultra-safe-encoding)))
  116. (when (or fold mm-use-ultra-safe-encoding)
  117. (let ((tab-width 1)) ; HTAB is one character.
  118. (goto-char (point-min))
  119. (while (not (eobp))
  120. ;; In ultra-safe mode, encode "From " at the beginning
  121. ;; of a line.
  122. (when mm-use-ultra-safe-encoding
  123. (if (looking-at "From ")
  124. (replace-match "From=20" nil t)
  125. (if (looking-at "-")
  126. (replace-match "=2D" nil t))))
  127. (end-of-line)
  128. ;; Fold long lines.
  129. (while (> (current-column) 76) ; tab-width must be 1.
  130. (beginning-of-line)
  131. (forward-char 75) ; 75 chars plus an "="
  132. (search-backward "=" (- (point) 2) t)
  133. (insert "=\n")
  134. (end-of-line))
  135. (forward-line))))))))
  136. (defun quoted-printable-encode-string (string)
  137. "Encode the STRING as quoted-printable and return the result."
  138. (with-temp-buffer
  139. (if (mm-multibyte-string-p string)
  140. (mm-enable-multibyte)
  141. (mm-disable-multibyte))
  142. (insert string)
  143. (quoted-printable-encode-region (point-min) (point-max))
  144. (buffer-string)))
  145. (provide 'qp)
  146. ;;; qp.el ends here