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