binhex.el 12 KB

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