yenc.el 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. ;;; yenc.el --- elisp native yenc decoder
  2. ;; Copyright (C) 2002-2012 Free Software Foundation, Inc.
  3. ;; Author: Jesper Harder <harder@ifa.au.dk>
  4. ;; Keywords: yenc news
  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 decoding yenc encoded messages.
  18. ;;
  19. ;; Limitations:
  20. ;;
  21. ;; * Does not handle multipart messages.
  22. ;; * No support for external decoders.
  23. ;; * Doesn't check the crc32 checksum (if present).
  24. ;;; Code:
  25. (eval-when-compile (require 'cl))
  26. (defconst yenc-begin-line
  27. "^=ybegin.*$")
  28. (defconst yenc-decoding-vector
  29. [214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
  30. 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
  31. 248 249 250 251 252 253 254 255 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  32. 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
  33. 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
  34. 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
  35. 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
  36. 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
  37. 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
  38. 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
  39. 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
  40. 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
  41. 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
  42. 208 209 210 211 212 213])
  43. (defun yenc-first-part-p ()
  44. "Say whether the buffer contains the first part of a yEnc file."
  45. (save-excursion
  46. (goto-char (point-min))
  47. (re-search-forward "^=ybegin part=1 " nil t)))
  48. (defun yenc-last-part-p ()
  49. "Say whether the buffer contains the last part of a yEnc file."
  50. (save-excursion
  51. (goto-char (point-min))
  52. (let (total-size end-size)
  53. (when (re-search-forward "^=ybegin.*size=\\([0-9]+\\)" nil t)
  54. (setq total-size (match-string 1)))
  55. (when (re-search-forward "^=ypart.*end=\\([0-9]+\\)" nil t)
  56. (setq end-size (match-string 1)))
  57. (and total-size
  58. end-size
  59. (string= total-size end-size)))))
  60. ;;;###autoload
  61. (defun yenc-decode-region (start end)
  62. "Yenc decode region between START and END using an internal decoder."
  63. (interactive "r")
  64. (let (work-buffer)
  65. (unwind-protect
  66. (save-excursion
  67. (goto-char start)
  68. (when (re-search-forward yenc-begin-line end t)
  69. (let ((first (match-end 0))
  70. (header-alist (yenc-parse-line (match-string 0)))
  71. bytes last footer-alist char)
  72. (when (re-search-forward "^=ypart.*$" end t)
  73. (setq first (match-end 0)))
  74. (when (re-search-forward "^=yend.*$" end t)
  75. (setq last (match-beginning 0))
  76. (setq footer-alist (yenc-parse-line (match-string 0)))
  77. (setq work-buffer (generate-new-buffer " *yenc-work*"))
  78. (unless (featurep 'xemacs)
  79. (with-current-buffer work-buffer (set-buffer-multibyte nil)))
  80. (while (< first last)
  81. (setq char (char-after first))
  82. (cond ((or (eq char ?\r)
  83. (eq char ?\n)))
  84. ((eq char ?=)
  85. (setq char (char-after (incf first)))
  86. (with-current-buffer work-buffer
  87. (insert-char (mod (- char 106) 256) 1)))
  88. (t
  89. (with-current-buffer work-buffer
  90. ;;(insert-char (mod (- char 42) 256) 1)
  91. (insert-char (aref yenc-decoding-vector char) 1))))
  92. (incf first))
  93. (setq bytes (buffer-size work-buffer))
  94. (unless (and (= (cdr (assq 'size header-alist)) bytes)
  95. (= (cdr (assq 'size footer-alist)) bytes))
  96. (message "Warning: Size mismatch while decoding."))
  97. (goto-char start)
  98. (delete-region start end)
  99. (insert-buffer-substring work-buffer))))
  100. (and work-buffer (kill-buffer work-buffer))))))
  101. ;;;###autoload
  102. (defun yenc-extract-filename ()
  103. "Extract file name from an yenc header."
  104. (save-excursion
  105. (when (re-search-forward yenc-begin-line nil t)
  106. (cdr (assoc 'name (yenc-parse-line (match-string 0)))))))
  107. (defun yenc-parse-line (str)
  108. "Extract file name and size from STR."
  109. (let (result name)
  110. (when (string-match "^=y.*size=\\([0-9]+\\)" str)
  111. (push (cons 'size (string-to-number (match-string 1 str))) result))
  112. (when (string-match "^=y.*name=\\(.*\\)$" str)
  113. (setq name (match-string 1 str))
  114. ;; Remove trailing white space
  115. (when (string-match " +$" name)
  116. (setq name (substring name 0 (match-beginning 0))))
  117. (push (cons 'name name) result))
  118. result))
  119. (provide 'yenc)
  120. ;;; yenc.el ends here