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