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