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