PageRenderTime 62ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 1ms

/lisp/mail/binhex.el

https://github.com/kamitchell/emacs
Emacs Lisp | 331 lines | 275 code | 32 blank | 24 comment | 0 complexity | 925ea8274019252048704b6e9e44adf1 MD5 | raw file
  1. ;;; binhex.el --- elisp native binhex decode
  2. ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
  3. ;; 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
  4. ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
  5. ;; Keywords: binhex news
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;;; Code:
  19. (eval-when-compile (require 'cl))
  20. (eval-and-compile
  21. (defalias 'binhex-char-int
  22. (if (fboundp 'char-int)
  23. 'char-int
  24. 'identity)))
  25. (defgroup binhex nil
  26. "Decoding of BinHex (binary-to-hexadecimal) data."
  27. :group 'mail
  28. :group 'news)
  29. (defcustom binhex-decoder-program "hexbin"
  30. "*Non-nil value should be a string that names a binhex decoder.
  31. The program should expect to read binhex data on its standard
  32. input and write the converted data to its standard output."
  33. :type 'string
  34. :group 'binhex)
  35. (defcustom binhex-decoder-switches '("-d")
  36. "*List of command line flags passed to the command `binhex-decoder-program'."
  37. :group 'binhex
  38. :type '(repeat string))
  39. (defcustom binhex-use-external
  40. (executable-find binhex-decoder-program)
  41. "*Use external binhex program."
  42. :version "22.1"
  43. :group 'binhex
  44. :type 'boolean)
  45. (defconst binhex-alphabet-decoding-alist
  46. '(( ?\! . 0) ( ?\" . 1) ( ?\# . 2) ( ?\$ . 3) ( ?\% . 4) ( ?\& . 5)
  47. ( ?\' . 6) ( ?\( . 7) ( ?\) . 8) ( ?\* . 9) ( ?\+ . 10) ( ?\, . 11)
  48. ( ?\- . 12) ( ?0 . 13) ( ?1 . 14) ( ?2 . 15) ( ?3 . 16) ( ?4 . 17)
  49. ( ?5 . 18) ( ?6 . 19) ( ?8 . 20) ( ?9 . 21) ( ?@ . 22) ( ?A . 23)
  50. ( ?B . 24) ( ?C . 25) ( ?D . 26) ( ?E . 27) ( ?F . 28) ( ?G . 29)
  51. ( ?H . 30) ( ?I . 31) ( ?J . 32) ( ?K . 33) ( ?L . 34) ( ?M . 35)
  52. ( ?N . 36) ( ?P . 37) ( ?Q . 38) ( ?R . 39) ( ?S . 40) ( ?T . 41)
  53. ( ?U . 42) ( ?V . 43) ( ?X . 44) ( ?Y . 45) ( ?Z . 46) ( ?\[ . 47)
  54. ( ?\` . 48) ( ?a . 49) ( ?b . 50) ( ?c . 51) ( ?d . 52) ( ?e . 53)
  55. ( ?f . 54) ( ?h . 55) ( ?i . 56) ( ?j . 57) ( ?k . 58) ( ?l . 59)
  56. ( ?m . 60) ( ?p . 61) ( ?q . 62) ( ?r . 63)))
  57. (defun binhex-char-map (char)
  58. (cdr (assq char binhex-alphabet-decoding-alist)))
  59. ;;;###autoload
  60. (defconst binhex-begin-line
  61. "^:...............................................................$")
  62. (defconst binhex-body-line
  63. "^[^:]...............................................................$")
  64. (defconst binhex-end-line ":$")
  65. (defvar binhex-temporary-file-directory
  66. (cond ((fboundp 'temp-directory) (temp-directory))
  67. ((boundp 'temporary-file-directory) temporary-file-directory)
  68. ("/tmp/")))
  69. (eval-and-compile
  70. (defalias 'binhex-insert-char
  71. (if (featurep 'xemacs)
  72. 'insert-char
  73. (lambda (char &optional count ignored buffer)
  74. "Insert COUNT copies of CHARACTER into BUFFER."
  75. (if (or (null buffer) (eq buffer (current-buffer)))
  76. (insert-char char count)
  77. (with-current-buffer buffer
  78. (insert-char char count)))))))
  79. (defvar binhex-crc-table
  80. [0 4129 8258 12387 16516 20645 24774 28903
  81. 33032 37161 41290 45419 49548 53677 57806 61935
  82. 4657 528 12915 8786 21173 17044 29431 25302
  83. 37689 33560 45947 41818 54205 50076 62463 58334
  84. 9314 13379 1056 5121 25830 29895 17572 21637
  85. 42346 46411 34088 38153 58862 62927 50604 54669
  86. 13907 9842 5649 1584 30423 26358 22165 18100
  87. 46939 42874 38681 34616 63455 59390 55197 51132
  88. 18628 22757 26758 30887 2112 6241 10242 14371
  89. 51660 55789 59790 63919 35144 39273 43274 47403
  90. 23285 19156 31415 27286 6769 2640 14899 10770
  91. 56317 52188 64447 60318 39801 35672 47931 43802
  92. 27814 31879 19684 23749 11298 15363 3168 7233
  93. 60846 64911 52716 56781 44330 48395 36200 40265
  94. 32407 28342 24277 20212 15891 11826 7761 3696
  95. 65439 61374 57309 53244 48923 44858 40793 36728
  96. 37256 33193 45514 41451 53516 49453 61774 57711
  97. 4224 161 12482 8419 20484 16421 28742 24679
  98. 33721 37784 41979 46042 49981 54044 58239 62302
  99. 689 4752 8947 13010 16949 21012 25207 29270
  100. 46570 42443 38312 34185 62830 58703 54572 50445
  101. 13538 9411 5280 1153 29798 25671 21540 17413
  102. 42971 47098 34713 38840 59231 63358 50973 55100
  103. 9939 14066 1681 5808 26199 30326 17941 22068
  104. 55628 51565 63758 59695 39368 35305 47498 43435
  105. 22596 18533 30726 26663 6336 2273 14466 10403
  106. 52093 56156 60223 64286 35833 39896 43963 48026
  107. 19061 23124 27191 31254 2801 6864 10931 14994
  108. 64814 60687 56684 52557 48554 44427 40424 36297
  109. 31782 27655 23652 19525 15522 11395 7392 3265
  110. 61215 65342 53085 57212 44955 49082 36825 40952
  111. 28183 32310 20053 24180 11923 16050 3793 7920])
  112. (defun binhex-update-crc (crc char &optional count)
  113. (if (null count) (setq count 1))
  114. (while (> count 0)
  115. (setq crc (logxor (logand (lsh crc 8) 65280)
  116. (aref binhex-crc-table
  117. (logxor (logand (lsh crc -8) 255)
  118. char)))
  119. count (1- count)))
  120. crc)
  121. (defun binhex-verify-crc (buffer start end)
  122. (with-current-buffer buffer
  123. (let ((pos start) (crc 0) (last (- end 2)))
  124. (while (< pos last)
  125. (setq crc (binhex-update-crc crc (char-after pos))
  126. pos (1+ pos)))
  127. (if (= crc (binhex-string-big-endian (buffer-substring last end)))
  128. nil
  129. (error "CRC error")))))
  130. (defun binhex-string-big-endian (string)
  131. (let ((ret 0) (i 0) (len (length string)))
  132. (while (< i len)
  133. (setq ret (+ (lsh ret 8) (binhex-char-int (aref string i)))
  134. i (1+ i)))
  135. ret))
  136. (defun binhex-string-little-endian (string)
  137. (let ((ret 0) (i 0) (shift 0) (len (length string)))
  138. (while (< i len)
  139. (setq ret (+ ret (lsh (binhex-char-int (aref string i)) shift))
  140. i (1+ i)
  141. shift (+ shift 8)))
  142. ret))
  143. (defun binhex-header (buffer)
  144. (with-current-buffer buffer
  145. (let ((pos (point-min)) len)
  146. (vector
  147. (prog1
  148. (setq len (binhex-char-int (char-after pos)))
  149. (setq pos (1+ pos)))
  150. (buffer-substring pos (setq pos (+ pos len)))
  151. (prog1
  152. (setq len (binhex-char-int (char-after pos)))
  153. (setq pos (1+ pos)))
  154. (buffer-substring pos (setq pos (+ pos 4)))
  155. (buffer-substring pos (setq pos (+ pos 4)))
  156. (binhex-string-big-endian
  157. (buffer-substring pos (setq pos (+ pos 2))))
  158. (binhex-string-big-endian
  159. (buffer-substring pos (setq pos (+ pos 4))))
  160. (binhex-string-big-endian
  161. (buffer-substring pos (setq pos (+ pos 4))))))))
  162. (defvar binhex-last-char)
  163. (defvar binhex-repeat)
  164. (defun binhex-push-char (char &optional count ignored buffer)
  165. (cond
  166. (binhex-repeat
  167. (if (eq char 0)
  168. (binhex-insert-char (setq binhex-last-char 144) 1
  169. ignored buffer)
  170. (binhex-insert-char binhex-last-char (- char 1)
  171. ignored buffer)
  172. (setq binhex-last-char nil))
  173. (setq binhex-repeat nil))
  174. ((= char 144)
  175. (setq binhex-repeat t))
  176. (t
  177. (binhex-insert-char (setq binhex-last-char char) 1 ignored buffer))))
  178. ;;;###autoload
  179. (defun binhex-decode-region-internal (start end &optional header-only)
  180. "Binhex decode region between START and END without using an external program.
  181. If HEADER-ONLY is non-nil only decode header and return filename."
  182. (interactive "r")
  183. (let ((work-buffer nil)
  184. (counter 0)
  185. (bits 0) (tmp t)
  186. (lim 0) inputpos
  187. (non-data-chars " \t\n\r:")
  188. file-name-length data-fork-start
  189. header
  190. binhex-last-char binhex-repeat)
  191. (unwind-protect
  192. (save-excursion
  193. (goto-char start)
  194. (when (re-search-forward binhex-begin-line end t)
  195. (setq work-buffer (generate-new-buffer " *binhex-work*"))
  196. (with-current-buffer work-buffer (set-buffer-multibyte nil))
  197. (beginning-of-line)
  198. (setq bits 0 counter 0)
  199. (while tmp
  200. (skip-chars-forward non-data-chars end)
  201. (setq inputpos (point))
  202. (end-of-line)
  203. (setq lim (point))
  204. (while (and (< inputpos lim)
  205. (setq tmp (binhex-char-map (char-after inputpos))))
  206. (setq bits (+ bits tmp)
  207. counter (1+ counter)
  208. inputpos (1+ inputpos))
  209. (cond ((= counter 4)
  210. (binhex-push-char (lsh bits -16) 1 nil work-buffer)
  211. (binhex-push-char (logand (lsh bits -8) 255) 1 nil
  212. work-buffer)
  213. (binhex-push-char (logand bits 255) 1 nil
  214. work-buffer)
  215. (setq bits 0 counter 0))
  216. (t (setq bits (lsh bits 6)))))
  217. (if (null file-name-length)
  218. (with-current-buffer work-buffer
  219. (setq file-name-length (char-after (point-min))
  220. data-fork-start (+ (point-min)
  221. file-name-length 22))))
  222. (when (and (null header)
  223. (with-current-buffer work-buffer
  224. (>= (buffer-size) data-fork-start)))
  225. (binhex-verify-crc work-buffer
  226. (point-min) data-fork-start)
  227. (setq header (binhex-header work-buffer))
  228. (when header-only (setq tmp nil counter 0)))
  229. (setq tmp (and tmp (not (eq inputpos end)))))
  230. (cond
  231. ((= counter 3)
  232. (binhex-push-char (logand (lsh bits -16) 255) 1 nil
  233. work-buffer)
  234. (binhex-push-char (logand (lsh bits -8) 255) 1 nil
  235. work-buffer))
  236. ((= counter 2)
  237. (binhex-push-char (logand (lsh bits -10) 255) 1 nil
  238. work-buffer))))
  239. (if header-only nil
  240. (binhex-verify-crc work-buffer
  241. data-fork-start
  242. (+ data-fork-start (aref header 6) 2))
  243. (or (markerp end) (setq end (set-marker (make-marker) end)))
  244. (goto-char start)
  245. (insert-buffer-substring work-buffer
  246. data-fork-start (+ data-fork-start
  247. (aref header 6)))
  248. (delete-region (point) end)))
  249. (and work-buffer (kill-buffer work-buffer)))
  250. (if header (aref header 1))))
  251. ;;;###autoload
  252. (defun binhex-decode-region-external (start end)
  253. "Binhex decode region between START and END using external decoder."
  254. (interactive "r")
  255. (let ((cbuf (current-buffer)) firstline work-buffer status
  256. (file-name (expand-file-name
  257. (concat (binhex-decode-region-internal start end t)
  258. ".data")
  259. binhex-temporary-file-directory)))
  260. (save-excursion
  261. (goto-char start)
  262. (when (re-search-forward binhex-begin-line nil t)
  263. (let ((cdir default-directory) default-process-coding-system)
  264. (unwind-protect
  265. (progn
  266. (set-buffer (setq work-buffer
  267. (generate-new-buffer " *binhex-work*")))
  268. (buffer-disable-undo work-buffer)
  269. (insert-buffer-substring cbuf firstline end)
  270. (cd binhex-temporary-file-directory)
  271. (apply 'call-process-region
  272. (point-min)
  273. (point-max)
  274. binhex-decoder-program
  275. nil
  276. nil
  277. nil
  278. binhex-decoder-switches))
  279. (cd cdir) (set-buffer cbuf)))
  280. (if (and file-name (file-exists-p file-name))
  281. (progn
  282. (goto-char start)
  283. (delete-region start end)
  284. (let (format-alist)
  285. (insert-file-contents-literally file-name)))
  286. (error "Can not binhex")))
  287. (and work-buffer (kill-buffer work-buffer))
  288. (ignore-errors
  289. (if file-name (delete-file file-name))))))
  290. ;;;###autoload
  291. (defun binhex-decode-region (start end)
  292. "Binhex decode region between START and END."
  293. (interactive "r")
  294. (if binhex-use-external
  295. (binhex-decode-region-external start end)
  296. (binhex-decode-region-internal start end)))
  297. (provide 'binhex)
  298. ;; arch-tag: 8476badd-1e76-4f1d-a640-f9a38c72eed8
  299. ;;; binhex.el ends here