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