1 ;;; uudecode.el -- elisp native uudecode
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
7 ;; Keywords: uudecode news
9 ;; This file is part of GNU Emacs.
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 of the License, or
14 ;; (at your option) any later version.
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.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 (eval-when-compile (require 'cl
))
31 (defalias 'uudecode-char-int
32 (if (fboundp 'char-int
)
36 (defgroup uudecode nil
37 "Decoding of uuencoded data."
41 (defcustom uudecode-decoder-program
"uudecode"
42 "*Non-nil value should be a string that names a uu decoder.
43 The program should expect to read uu data on its standard
44 input and write the converted data to its standard output."
48 (defcustom uudecode-decoder-switches nil
49 "*List of command line flags passed to `uudecode-decoder-program'."
51 :type
'(repeat string
))
53 (defcustom uudecode-use-external
54 (executable-find uudecode-decoder-program
)
55 "*Use external uudecode program."
60 (defconst uudecode-alphabet
"\040-\140")
62 (defconst uudecode-begin-line
"^begin[ \t]+[0-7][0-7][0-7][ \t]+\\(.*\\)$")
63 (defconst uudecode-end-line
"^end[ \t]*$")
65 (defconst uudecode-body-line
66 (let ((i 61) (str "^M"))
67 (while (> (setq i
(1- i
)) 0)
68 (setq str
(concat str
"[^a-z]")))
71 (defvar uudecode-temporary-file-directory
72 (cond ((fboundp 'temp-directory
) (temp-directory))
73 ((boundp 'temporary-file-directory
) temporary-file-directory
)
77 (defun uudecode-decode-region-external (start end
&optional file-name
)
78 "Uudecode region between START and END using external program.
79 If FILE-NAME is non-nil, save the result to FILE-NAME. The program
80 used is specified by `uudecode-decoder-program'."
82 (let ((cbuf (current-buffer)) tempfile firstline status
)
85 (when (re-search-forward uudecode-begin-line nil t
)
87 (setq firstline
(point))
88 (cond ((null file-name
))
91 (setq file-name
(read-file-name "File to Name:"
94 (setq tempfile
(if file-name
95 (expand-file-name file-name
)
96 (if (fboundp 'make-temp-file
)
97 (let ((temporary-file-directory
98 uudecode-temporary-file-directory
))
99 (make-temp-file "uu"))
101 (make-temp-name "uu")
102 uudecode-temporary-file-directory
))))
103 (let ((cdir default-directory
)
104 (default-process-coding-system
105 (if (featurep 'xemacs
)
106 ;; In XEmacs, `nil' is not a valid coding system.
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
117 uudecode-decoder-program
121 uudecode-decoder-switches
))
122 (cd cdir
) (set-buffer cbuf
)))
123 (if (file-exists-p tempfile
)
126 (delete-region start end
)
128 (insert-file-contents-literally tempfile
)))
129 (message "Can not uudecode")))
130 (ignore-errors (or file-name
(delete-file tempfile
))))))
133 (defalias 'uudecode-string-to-multibyte
137 ((fboundp 'string-to-multibyte
)
138 'string-to-multibyte
)
141 "Return a multibyte string with the same individual chars as string."
143 (lambda (ch) (string-as-multibyte (char-to-string ch
)))
147 (defun uudecode-decode-region-internal (start end
&optional file-name
)
148 "Uudecode region between START and END without using an external program.
149 If FILE-NAME is non-nil, save the result to FILE-NAME."
155 (lim 0) inputpos result
156 (non-data-chars (concat "^" uudecode-alphabet
)))
159 (when (re-search-forward uudecode-begin-line nil t
)
160 (cond ((null file-name
))
161 ((stringp file-name
))
163 (setq file-name
(expand-file-name
164 (read-file-name "File to Name:"
166 (match-string 1))))))
168 (skip-chars-forward non-data-chars end
)
170 (setq inputpos
(point))
171 (setq remain
0 bits
0 counter
0)
173 ((> (skip-chars-forward uudecode-alphabet end
) 0)
176 (logand (- (uudecode-char-int (char-after inputpos
)) 32)
178 (setq inputpos
(1+ inputpos
))
179 (if (= remain
0) (setq done t
))
180 (while (and (< inputpos lim
) (> remain
0))
184 (uudecode-char-int (char-after inputpos
)) 32)
186 (if (/= counter
0) (setq remain
(1- remain
)))
187 (setq counter
(1+ counter
)
188 inputpos
(1+ inputpos
))
192 (char-to-string (lsh bits -
16))
193 (char-to-string (logand (lsh bits -
8) 255))
194 (char-to-string (logand bits
255)))
196 (setq bits
0 counter
0))
197 (t (setq bits
(lsh bits
6)))))))
201 (error "uucode line ends unexpectly")
203 ((and (= (point) end
) (not done
))
204 ;;(error "uucode ends unexpectly")
209 (char-to-string (logand (lsh bits -
16) 255))
210 (char-to-string (logand (lsh bits -
8) 255)))
214 (char-to-string (logand (lsh bits -
10) 255))
216 (skip-chars-forward non-data-chars end
))
218 (with-temp-file file-name
219 (set-buffer-multibyte nil
)
220 (insert (apply 'concat
(nreverse result
))))
221 (or (markerp end
) (setq end
(set-marker (make-marker) end
)))
223 (if enable-multibyte-characters
224 (dolist (x (nreverse result
))
225 (insert (uudecode-string-to-multibyte x
)))
226 (insert (apply 'concat
(nreverse result
))))
227 (delete-region (point) end
))))))
230 (defun uudecode-decode-region (start end
&optional file-name
)
231 "Uudecode region between START and END.
232 If FILE-NAME is non-nil, save the result to FILE-NAME."
233 (if uudecode-use-external
234 (uudecode-decode-region-external start end file-name
)
235 (uudecode-decode-region-internal start end file-name
)))
239 ;; arch-tag: e1f09ed5-62b4-4677-9f13-4e81c4fe8ce3
240 ;;; uudecode.el ends here