5b74b5a30ae195d77b156f0e13feea8f914f8ed6
[gnus] / lisp / uudecode.el
1 ;;; uudecode.el -- elisp native uudecode
2
3 ;; Copyright (c) 1998, 1999, 2000, 2001, 2003, 2004
4 ;;        Free Software Foundation, Inc.
5
6 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
7 ;; Keywords: uudecode news
8
9 ;; This file is part of GNU Emacs.
10
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 2, or (at your option)
14 ;; any later version.
15
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.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (eval-when-compile (require 'cl))
31
32 (eval-and-compile
33   (defalias 'uudecode-char-int
34     (if (fboundp 'char-int)
35         'char-int
36       'identity)))
37
38 (defcustom uudecode-decoder-program "uudecode"
39   "*Non-nil value should be a string that names a uu decoder.
40 The program should expect to read uu data on its standard
41 input and write the converted data to its standard output."
42   :type 'string
43   :group 'gnus-extract)
44
45 (defcustom uudecode-decoder-switches nil
46   "*List of command line flags passed to `uudecode-decoder-program'."
47   :group 'gnus-extract
48   :type '(repeat string))
49
50 (defcustom uudecode-use-external
51   (executable-find uudecode-decoder-program)
52   "*Use external uudecode program."
53   :group 'gnus-extract
54   :type 'boolean)
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 using external program.
75 If FILE-NAME is non-nil, save the result to FILE-NAME.  The program
76 used is specified by `uudecode-decoder-program'."
77   (interactive "r\nP")
78   (let ((cbuf (current-buffer)) tempfile firstline 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                            (if (fboundp 'make-temp-file)
93                                (let ((temporary-file-directory
94                                       uudecode-temporary-file-directory))
95                                  (make-temp-file "uu"))
96                              (expand-file-name
97                               (make-temp-name "uu")
98                               uudecode-temporary-file-directory))))
99         (let ((cdir default-directory)
100               default-process-coding-system)
101           (unwind-protect
102               (with-temp-buffer
103                 (insert "begin 600 " (file-name-nondirectory tempfile) "\n")
104                 (insert-buffer-substring cbuf firstline end)
105                 (cd (file-name-directory tempfile))
106                 (apply 'call-process-region
107                        (point-min)
108                        (point-max)
109                        uudecode-decoder-program
110                        nil
111                        nil
112                        nil
113                        uudecode-decoder-switches))
114             (cd cdir) (set-buffer cbuf)))
115         (if (file-exists-p tempfile)
116             (unless file-name
117               (goto-char start)
118               (delete-region start end)
119               (let (format-alist)
120                 (insert-file-contents-literally tempfile)))
121           (message "Can not uudecode")))
122       (ignore-errors (or file-name (delete-file tempfile))))))
123
124 ;;;###autoload
125 (defun uudecode-decode-region-internal (start end &optional file-name)
126   "Uudecode region between START and END without using an external program.
127 If FILE-NAME is non-nil, save the result to FILE-NAME."
128   (interactive "r\nP")
129   (let ((done nil)
130         (counter 0)
131         (remain 0)
132         (bits 0)
133         (lim 0) inputpos result
134         (non-data-chars (concat "^" uudecode-alphabet)))
135     (save-excursion
136       (goto-char start)
137       (when (re-search-forward uudecode-begin-line nil t)
138         (cond ((null file-name))
139               ((stringp file-name))
140               (t
141                (setq file-name (expand-file-name
142                                 (read-file-name "File to Name:"
143                                                 nil nil nil
144                                                 (match-string 1))))))
145         (forward-line 1)
146         (skip-chars-forward non-data-chars end)
147         (while (not done)
148           (setq inputpos (point))
149           (setq remain 0 bits 0 counter 0)
150           (cond
151            ((> (skip-chars-forward uudecode-alphabet end) 0)
152             (setq lim (point))
153             (setq remain
154                   (logand (- (uudecode-char-int (char-after inputpos)) 32)
155                           63))
156             (setq inputpos (1+ inputpos))
157             (if (= remain 0) (setq done t))
158             (while (and (< inputpos lim) (> remain 0))
159               (setq bits (+ bits
160                             (logand
161                              (-
162                               (uudecode-char-int (char-after inputpos)) 32)
163                              63)))
164               (if (/= counter 0) (setq remain (1- remain)))
165               (setq counter (1+ counter)
166                     inputpos (1+ inputpos))
167               (cond ((= counter 4)
168                      (setq result (cons
169                                    (concat
170                                     (char-to-string (lsh bits -16))
171                                     (char-to-string (logand (lsh bits -8) 255))
172                                     (char-to-string (logand bits 255)))
173                                    result))
174                      (setq bits 0 counter 0))
175                     (t (setq bits (lsh bits 6)))))))
176           (cond
177            (done)
178            ((> 0 remain)
179             (error "uucode line ends unexpectly")
180             (setq done t))
181            ((and (= (point) end) (not done))
182             ;;(error "uucode ends unexpectly")
183             (setq done t))
184            ((= counter 3)
185             (setq result (cons
186                           (concat
187                            (char-to-string (logand (lsh bits -16) 255))
188                            (char-to-string (logand (lsh bits -8) 255)))
189                           result)))
190            ((= counter 2)
191             (setq result (cons
192                           (char-to-string (logand (lsh bits -10) 255))
193                           result))))
194           (skip-chars-forward non-data-chars end))
195         (if file-name
196             (let (default-enable-multibyte-characters)
197               (with-temp-file file-name
198                 (insert (apply 'concat (nreverse result)))))
199           (or (markerp end) (setq end (set-marker (make-marker) end)))
200           (goto-char start)
201           (insert (apply 'concat (nreverse result)))
202           (delete-region (point) end))))))
203
204 ;;;###autoload
205 (defun uudecode-decode-region (start end &optional file-name)
206   "Uudecode region between START and END.
207 If FILE-NAME is non-nil, save the result to FILE-NAME."
208   (if uudecode-use-external
209       (uudecode-decode-region-external start end file-name)
210     (uudecode-decode-region-internal start end file-name)))
211
212 (provide 'uudecode)
213
214 ;;; uudecode.el ends here