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