1 ;;; mel-b.el: Base64 encoder/decoder for GNU Emacs
3 ;; Copyright (C) 1992,1995,1996,1997,1998 Free Software Foundation, Inc.
5 ;; Author: ENAMI Tsugutomo <enami@sys.ptg.sony.co.jp>
6 ;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
8 ;; Keywords: MIME, Base64
10 ;; This file is part of MEL (MIME Encoding Library).
12 ;; This program is free software; you can redistribute it and/or
13 ;; modify it under the terms of the GNU General Public License as
14 ;; published by the Free Software Foundation; either version 2, or (at
15 ;; your option) any later version.
17 ;; This program is distributed in the hope that it will be useful, but
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 ;; General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
37 "Base64 encoder/decoder"
40 (defcustom base64-external-encoder '("mmencode")
41 "*list of base64 encoder program name and its arguments."
43 :type '(cons (file :tag "Command")(repeat :tag "Arguments" string)))
45 (defcustom base64-external-decoder '("mmencode" "-u")
46 "*list of base64 decoder program name and its arguments."
48 :type '(cons (file :tag "Command")(repeat :tag "Arguments" string)))
50 (defcustom base64-external-decoder-option-to-specify-file '("-o")
51 "*list of options of base64 decoder program to specify file."
53 :type '(repeat :tag "Arguments" string))
55 (defcustom base64-internal-encoding-limit 1000
56 "*limit size to use internal base64 encoder.
57 If size of input to encode is larger than this limit,
58 external encoder is called."
60 :type '(choice (const :tag "Always use internal encoder" nil)
61 (integer :tag "Size")))
63 (defcustom base64-internal-decoding-limit 70000
64 "*limit size to use internal base64 decoder.
65 If size of input to decode is larger than this limit,
66 external decoder is called."
68 :type '(choice (const :tag "Always use internal decoder" nil)
69 (integer :tag "Size")))
72 ;;; @ internal base64 encoder
73 ;;; based on base64 decoder by Enami Tsugutomo
75 (defconst base64-characters
76 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")
78 (defmacro base64-num-to-char (n)
79 `(aref base64-characters ,n))
81 (defun base64-encode-1 (pack)
86 (char-to-string (base64-num-to-char (ash a -2)))
90 (base64-num-to-char (logior (ash (logand a 3) 4) (ash b -4))))
94 (base64-num-to-char (logior (ash (logand b 15) 2) (ash c -6))))
95 (char-to-string (base64-num-to-char (logand c 63)))
97 (concat (char-to-string
98 (base64-num-to-char (ash (logand b 15) 2))) "=")
100 (concat (char-to-string
101 (base64-num-to-char (ash (logand a 3) 4))) "==")
104 (defun base64-encode-string (string)
105 "Encode STRING to base64, and return the result."
106 (let ((len (length string))
113 (function base64-encode-1)
114 (pack-sequence (substring string b e) 3)
121 (let* ((es (mapconcat
122 (function base64-encode-1)
123 (pack-sequence (substring string b) 3)
125 (m (mod (length es) 4))
127 (concat dest es (cond ((= m 3) "=")
132 (defun base64-internal-encode-region (beg end)
135 (narrow-to-region beg end)
136 (let ((str (buffer-substring beg end)))
137 (delete-region beg end)
138 (insert (base64-encode-string str))
146 ;;; @ internal base64 decoder
149 (defconst base64-numbers
150 `,(let ((len (length base64-characters))
151 (vec (make-vector 123 nil))
154 (aset vec (aref base64-characters i) i)
158 (defmacro base64-char-to-num (c)
159 `(aref base64-numbers ,c))
161 (defsubst base64-internal-decode (string buffer)
162 (let* ((len (length string))
168 (when (prog1 (setq v1 (base64-char-to-num (aref string i)))
170 (setq v2 (base64-char-to-num (aref string i))
172 v3 (base64-char-to-num (aref string i))
174 (aset buffer j (logior (lsh v1 2)(lsh v2 -4)))
177 (let ((v4 (base64-char-to-num (aref string i))))
179 (aset buffer j (logior (lsh (logand v2 15) 4)(lsh v3 -2)))
182 (aset buffer (prog1 j (setq j (1+ j)))
183 (logior (lsh (logand v3 3) 6) v4))
188 (substring buffer 0 j)
191 (defun base64-internal-decode-string (string)
192 (base64-internal-decode string (make-string (length string) 0)))
194 (defsubst base64-decode-string! (string)
195 (base64-internal-decode string string))
197 (defun base64-internal-decode-region (beg end)
199 (let ((str (buffer-substring beg end)))
200 (delete-region beg end)
202 (insert (base64-decode-string! str)))))
205 ;;; @ external encoder/decoder
208 (defun base64-external-encode-region (beg end)
211 (narrow-to-region beg end)
213 (apply (function call-process-region)
214 beg end (car base64-external-encoder)
215 t t nil (cdr base64-external-encoder)))
217 ;; regularize line break code
218 (goto-char (point-min))
219 (while (re-search-forward "\r$" nil t)
223 (defun base64-external-decode-region (beg end)
226 (apply (function call-process-region)
227 beg end (car base64-external-decoder)
228 t t nil (cdr base64-external-decoder)))
231 (defun base64-external-decode-string (string)
235 (apply (function call-process-region)
236 (point-min) (point-max)
237 (car base64-external-decoder)
238 t t nil (cdr base64-external-decoder)))
242 ;;; @ application interfaces
245 (defun base64-encode-region (start end)
246 "Encode current region by base64.
247 START and END are buffer positions.
248 This function calls internal base64 encoder if size of region is
249 smaller than `base64-internal-encoding-limit', otherwise it calls
250 external base64 encoder specified by `base64-external-encoder'. In
251 this case, you must install the program (maybe mmencode included in
252 metamail or XEmacs package)."
254 (if (and base64-internal-encoding-limit
255 (> (- end start) base64-internal-encoding-limit))
256 (base64-external-encode-region start end)
257 (base64-internal-encode-region start end)))
259 (defun base64-decode-region (start end)
260 "Decode current region by base64.
261 START and END are buffer positions.
262 This function calls internal base64 decoder if size of region is
263 smaller than `base64-internal-decoding-limit', otherwise it calls
264 external base64 decoder specified by `base64-external-decoder'. In
265 this case, you must install the program (maybe mmencode included in
266 metamail or XEmacs package)."
268 (if (and base64-internal-decoding-limit
269 (> (- end start) base64-internal-decoding-limit))
270 (base64-external-decode-region start end)
271 (base64-internal-decode-region start end)))
273 (defun base64-decode-string (string)
274 "Decode STRING which is encoded in base64, and return the result.
275 This function calls internal base64 decoder if size of STRING is
276 smaller than `base64-internal-decoding-limit', otherwise it calls
277 external base64 decoder specified by `base64-external-decoder'. In
278 this case, you must install the program (maybe mmencode included in
279 metamail or XEmacs package)."
281 (if (and base64-internal-decoding-limit
282 (> (length string) base64-internal-decoding-limit))
283 (base64-external-decode-string string)
284 (base64-internal-decode-string string)))
287 (mel-define-method-function (mime-encode-string string (nil "base64"))
288 'base64-encode-string)
289 (mel-define-method-function (mime-decode-string string (nil "base64"))
290 'base64-decode-string)
291 (mel-define-method-function (mime-encode-region start end (nil "base64"))
292 'base64-encode-region)
293 (mel-define-method-function (mime-decode-region start end (nil "base64"))
294 'base64-decode-region)
296 (mel-define-method-function (encoded-text-encode-string string (nil "B"))
297 'base64-encode-string)
299 (mel-define-method encoded-text-decode-string (string (nil "B"))
300 (if (and (string-match B-encoded-text-regexp string)
301 (string= string (match-string 0 string)))
302 (base64-decode-string string)
303 (error "Invalid encoded-text %s" string)))
305 (mel-define-method mime-insert-encoded-file (filename (nil "base64"))
306 "Encode contents of file FILENAME to base64, and insert the result.
307 It calls external base64 encoder specified by
308 `base64-external-encoder'. So you must install the program (maybe
309 mmencode included in metamail or XEmacs package)."
310 (interactive (list (read-file-name "Insert encoded file: ")))
311 (if (and base64-internal-encoding-limit
312 (> (nth 7 (file-attributes filename))
313 base64-internal-encoding-limit))
314 (apply (function call-process) (car base64-external-encoder)
315 filename t nil (cdr base64-external-encoder))
317 (base64-encode-string
319 (insert-file-contents-as-binary filename)
325 (mel-define-method mime-write-decoded-region (start end filename
327 "Decode and write current region encoded by base64 into FILENAME.
328 START and END are buffer positions."
330 (list (region-beginning) (region-end)
331 (read-file-name "Write decoded region to file: ")))
332 (if (and base64-internal-decoding-limit
333 (> (- end start) base64-internal-decoding-limit))
335 (apply (function call-process-region)
336 start end (car base64-external-decoder)
338 (append (cdr base64-external-decoder)
339 base64-external-decoder-option-to-specify-file
341 (let ((str (buffer-substring start end)))
343 (insert (base64-internal-decode-string str))
344 (write-region-as-binary (point-min) (point-max) filename)))))
350 (defun pack-sequence (seq size)
351 "Split sequence SEQ into SIZE elements packs,
352 and return list of packs. [mel-b; tl-seq function]"
353 (let ((len (length seq)) (p 0) obj
357 (setq obj (elt seq p))
358 (setq unit (cons obj unit))
362 (setq dest (cons (reverse unit) dest))
369 (setq dest (cons (reverse unit) dest))
380 ;;; mel-b.el ends here.