31717
|
1 ;;; uudecode.el -- elisp native uudecode
|
|
2
|
|
3 ;; Copyright (c) 1998, 1999, 2000 Free Software Foundation, Inc.
|
|
4
|
|
5 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
|
|
6 ;; Keywords: uudecode news
|
|
7
|
|
8 ;; This file is a 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 ;; Lots of codes are stolen from mm-decode.el, gnus-uu.el and
|
|
28 ;; base64.el
|
|
29
|
33263
|
30 ;; This looks as though it could be made rather more efficient.
|
|
31 ;; Encoding could use a lookup table and decoding should presumably
|
|
32 ;; use a vector or list buffer for partial results rather than
|
|
33 ;; with-current-buffer. -- fx
|
|
34
|
31717
|
35 ;;; Code:
|
|
36
|
33264
|
37 (eval-when-compile (require 'cl))
|
|
38
|
33263
|
39 (defalias 'uudecode-char-int
|
|
40 (if (fboundp 'char-int)
|
|
41 'char-int
|
|
42 'identity))
|
31717
|
43
|
|
44 (defcustom uudecode-decoder-program "uudecode"
|
|
45 "*Non-nil value should be a string that names a uu decoder.
|
|
46 The program should expect to read uu data on its standard
|
|
47 input and write the converted data to its standard output."
|
|
48 :type 'string
|
|
49 :group 'gnus-extract)
|
|
50
|
|
51 (defcustom uudecode-decoder-switches nil
|
|
52 "*List of command line flags passed to `uudecode-decoder-program'."
|
|
53 :group 'gnus-extract
|
|
54 :type '(repeat string))
|
|
55
|
|
56 (defconst uudecode-alphabet "\040-\140")
|
|
57
|
|
58 (defconst uudecode-begin-line "^begin[ \t]+[0-7][0-7][0-7][ \t]+\\(.*\\)$")
|
|
59 (defconst uudecode-end-line "^end[ \t]*$")
|
|
60
|
|
61 (defconst uudecode-body-line
|
|
62 (let ((i 61) (str "^M"))
|
|
63 (while (> (setq i (1- i)) 0)
|
|
64 (setq str (concat str "[^a-z]")))
|
|
65 (concat str ".?$")))
|
|
66
|
|
67 (defvar uudecode-temporary-file-directory
|
|
68 (cond ((fboundp 'temp-directory) (temp-directory))
|
|
69 ((boundp 'temporary-file-directory) temporary-file-directory)
|
|
70 ("/tmp")))
|
|
71
|
|
72 ;;;###autoload
|
|
73 (defun uudecode-decode-region-external (start end &optional file-name)
|
|
74 "Uudecode region between START and END with external decoder.
|
|
75
|
|
76 If FILE-NAME is non-nil, save the result to FILE-NAME."
|
|
77 (interactive "r\nP")
|
|
78 (let ((cbuf (current-buffer)) tempfile firstline work-buffer status)
|
|
79 (save-excursion
|
|
80 (goto-char start)
|
|
81 (when (re-search-forward uudecode-begin-line nil t)
|
|
82 (forward-line 1)
|
|
83 (setq firstline (point))
|
|
84 (cond ((null file-name))
|
|
85 ((stringp file-name))
|
|
86 (t
|
|
87 (setq file-name (read-file-name "File to Name:"
|
|
88 nil nil nil
|
|
89 (match-string 1)))))
|
|
90 (setq tempfile (if file-name
|
|
91 (expand-file-name file-name)
|
|
92 (make-temp-name
|
|
93 ;; /tmp/uu...
|
|
94 (expand-file-name
|
|
95 "uu" uudecode-temporary-file-directory))))
|
|
96 (let ((cdir default-directory) default-process-coding-system)
|
|
97 (unwind-protect
|
|
98 (progn
|
|
99 (set-buffer (setq work-buffer
|
|
100 (generate-new-buffer " *uudecode-work*")))
|
|
101 (buffer-disable-undo work-buffer)
|
|
102 (insert "begin 600 " (file-name-nondirectory tempfile) "\n")
|
|
103 (insert-buffer-substring cbuf firstline end)
|
|
104 (cd (file-name-directory tempfile))
|
|
105 (apply 'call-process-region
|
|
106 (point-min)
|
|
107 (point-max)
|
|
108 uudecode-decoder-program
|
|
109 nil
|
|
110 nil
|
|
111 nil
|
|
112 uudecode-decoder-switches))
|
|
113 (cd cdir) (set-buffer cbuf)))
|
|
114 (if (file-exists-p tempfile)
|
|
115 (unless file-name
|
|
116 (goto-char start)
|
|
117 (delete-region start end)
|
|
118 (let (format-alist)
|
|
119 (insert-file-contents-literally tempfile)))
|
|
120 (message "Can not uudecode")))
|
|
121 (and work-buffer (kill-buffer work-buffer))
|
|
122 (ignore-errors (or file-name (delete-file tempfile))))))
|
|
123
|
33263
|
124 (if (featurep 'xemacs)
|
31717
|
125 (defalias 'uudecode-insert-char 'insert-char)
|
|
126 (defun uudecode-insert-char (char &optional count ignored buffer)
|
|
127 (if (or (null buffer) (eq buffer (current-buffer)))
|
|
128 (insert-char char count)
|
|
129 (with-current-buffer buffer
|
|
130 (insert-char char count)))))
|
|
131
|
|
132 ;;;###autoload
|
|
133
|
|
134 (defun uudecode-decode-region (start end &optional file-name)
|
|
135 "Uudecode region between START and END.
|
|
136 If FILE-NAME is non-nil, save the result to FILE-NAME."
|
|
137 (interactive "r\nP")
|
|
138 (let ((work-buffer nil)
|
|
139 (done nil)
|
|
140 (counter 0)
|
|
141 (remain 0)
|
|
142 (bits 0)
|
|
143 (lim 0) inputpos
|
|
144 (non-data-chars (concat "^" uudecode-alphabet)))
|
|
145 (unwind-protect
|
|
146 (save-excursion
|
|
147 (goto-char start)
|
|
148 (when (re-search-forward uudecode-begin-line nil t)
|
|
149 (cond ((null file-name))
|
|
150 ((stringp file-name))
|
|
151 (t
|
|
152 (setq file-name (expand-file-name
|
|
153 (read-file-name "File to Name:"
|
|
154 nil nil nil
|
|
155 (match-string 1))))))
|
|
156 (setq work-buffer (generate-new-buffer " *uudecode-work*"))
|
|
157 (forward-line 1)
|
|
158 (skip-chars-forward non-data-chars end)
|
|
159 (while (not done)
|
|
160 (setq inputpos (point))
|
|
161 (setq remain 0 bits 0 counter 0)
|
|
162 (cond
|
|
163 ((> (skip-chars-forward uudecode-alphabet end) 0)
|
|
164 (setq lim (point))
|
|
165 (setq remain
|
33263
|
166 (logand (- (uudecode-char-int (char-after inputpos)) 32)
|
|
167 63))
|
31717
|
168 (setq inputpos (1+ inputpos))
|
|
169 (if (= remain 0) (setq done t))
|
|
170 (while (and (< inputpos lim) (> remain 0))
|
|
171 (setq bits (+ bits
|
|
172 (logand
|
|
173 (-
|
33263
|
174 (uudecode-char-int (char-after inputpos)) 32)
|
|
175 63)))
|
31717
|
176 (if (/= counter 0) (setq remain (1- remain)))
|
|
177 (setq counter (1+ counter)
|
|
178 inputpos (1+ inputpos))
|
|
179 (cond ((= counter 4)
|
|
180 (uudecode-insert-char
|
|
181 (lsh bits -16) 1 nil work-buffer)
|
|
182 (uudecode-insert-char
|
|
183 (logand (lsh bits -8) 255) 1 nil work-buffer)
|
|
184 (uudecode-insert-char (logand bits 255) 1 nil
|
|
185 work-buffer)
|
|
186 (setq bits 0 counter 0))
|
|
187 (t (setq bits (lsh bits 6)))))))
|
|
188 (cond
|
|
189 (done)
|
|
190 ((> 0 remain)
|
|
191 (error "uucode line ends unexpectly")
|
|
192 (setq done t))
|
|
193 ((and (= (point) end) (not done))
|
|
194 ;;(error "uucode ends unexpectly")
|
|
195 (setq done t))
|
|
196 ((= counter 3)
|
|
197 (uudecode-insert-char (logand (lsh bits -16) 255) 1 nil
|
|
198 work-buffer)
|
|
199 (uudecode-insert-char (logand (lsh bits -8) 255) 1 nil
|
|
200 work-buffer))
|
|
201 ((= counter 2)
|
|
202 (uudecode-insert-char (logand (lsh bits -10) 255) 1 nil
|
|
203 work-buffer)))
|
|
204 (skip-chars-forward non-data-chars end))
|
|
205 (if file-name
|
|
206 (save-excursion
|
|
207 (set-buffer work-buffer)
|
|
208 (write-file file-name))
|
|
209 (or (markerp end) (setq end (set-marker (make-marker) end)))
|
|
210 (goto-char start)
|
|
211 (insert-buffer-substring work-buffer)
|
|
212 (delete-region (point) end))))
|
|
213 (and work-buffer (kill-buffer work-buffer)))))
|
|
214
|
|
215 (provide 'uudecode)
|
|
216
|
|
217 ;;; uudecode.el ends here
|