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