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