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.
35 (defvar base64-external-encoder '("mmencode")
36 "*list of base64 encoder program name and its arguments.")
38 (defvar base64-external-decoder '("mmencode" "-u")
39 "*list of base64 decoder program name and its arguments.")
41 (defvar base64-external-decoder-option-to-specify-file '("-o")
42 "*list of options of base64 decoder program to specify file.")
44 (defvar base64-internal-encoding-limit 1000
45 "*limit size to use internal base64 encoder.
46 If size of input to encode is larger than this limit,
47 external encoder is called.")
49 (defvar base64-internal-decoding-limit 1000
50 "*limit size to use internal base64 decoder.
51 If size of input to decode is larger than this limit,
52 external decoder is called.")
55 ;;; @ internal base64 decoder/encoder
56 ;;; based on base64 decoder by Enami Tsugutomo
58 ;;; @@ convert from/to base64 char
61 (defun base64-num-to-char (n)
64 ((< n 52) (+ ?a (- n 26)))
65 ((< n 62) (+ ?0 (- n 52)))
68 (t (error "not a base64 integer %d" n))))
70 (defun base64-char-to-num (c)
71 (cond ((and (<= ?A c) (<= c ?Z)) (- c ?A))
72 ((and (<= ?a c) (<= c ?z)) (+ (- c ?a) 26))
73 ((and (<= ?0 c) (<= c ?9)) (+ (- c ?0) 52))
77 (t (error "not a base64 character %c" c))))
80 ;;; @@ encode/decode one base64 unit
83 (defun base64-encode-1 (pack)
88 (char-to-string (base64-num-to-char (ash a -2)))
92 (base64-num-to-char (logior (ash (logand a 3) 4) (ash b -4))))
96 (base64-num-to-char (logior (ash (logand b 15) 2) (ash c -6))))
97 (char-to-string (base64-num-to-char (logand c 63)))
99 (concat (char-to-string
100 (base64-num-to-char (ash (logand b 15) 2))) "=")
102 (concat (char-to-string
103 (base64-num-to-char (ash (logand a 3) 4))) "==")
106 (defun base64-decode-unit (a b &optional c d)
109 (char-to-string (logior (ash (base64-char-to-num a) 2)
110 (ash (setq b (base64-char-to-num b)) -4)))
111 (if (and c (setq c (base64-char-to-num c)))
112 (concat (char-to-string
113 (logior (ash (logand b 15) 4) (ash c -2)))
114 (if (and d (setq d (base64-char-to-num d)))
115 (char-to-string (logior (ash (logand c 3) 6) d))
117 (error (message (nth 1 err))
121 ;;; @@ base64 encoder/decoder for string
124 (defun base64-encode-string (string)
125 "Encode STRING to base64, and return the result."
126 (let ((len (length string))
133 (function base64-encode-1)
134 (pack-sequence (substring string b e) 3)
141 (let* ((es (mapconcat
142 (function base64-encode-1)
143 (pack-sequence (substring string b) 3)
145 (m (mod (length es) 4))
147 (concat dest es (cond ((= m 3) "=")
152 (defun base64-internal-decode-string (string)
153 (let ((len (length string))
157 (let ((a (aref string i)))
160 (let ((b (aref string i)))
167 (setq dest (concat dest (base64-decode-unit a b) ))
170 (let ((c (aref string i)))
174 (setq dest (concat dest (base64-decode-unit a b)))
177 (setq dest (concat dest (base64-decode-unit a b c)))
180 (let ((d (aref string i)))
185 (base64-decode-unit a b c)
186 (base64-decode-unit a b c d))))
191 ;;; @ base64 encoder/decoder for region
194 (defun base64-internal-encode-region (beg end)
197 (narrow-to-region beg end)
198 (let ((str (buffer-substring beg end)))
199 (delete-region beg end)
200 (insert (base64-encode-string str))
207 (defun base64-internal-decode-region (beg end)
209 (let ((str (buffer-substring beg end)))
210 (delete-region beg end)
212 (insert (base64-internal-decode-string str)))))
214 (defun base64-external-encode-region (beg end)
217 (narrow-to-region beg end)
218 (as-binary-process (apply (function call-process-region)
219 beg end (car base64-external-encoder)
220 t t nil (cdr base64-external-encoder))
223 ;; regularize line break code
224 (goto-char (point-min))
225 (while (re-search-forward "\r$" nil t)
230 (defun base64-external-decode-region (beg end)
232 (as-binary-process (apply (function call-process-region)
233 beg end (car base64-external-decoder)
234 t t nil (cdr base64-external-decoder))
237 (defun base64-external-decode-string (string)
240 (as-binary-process (apply (function call-process-region)
241 (point-min) (point-max)
242 (car base64-external-decoder)
243 t t nil (cdr base64-external-decoder))
248 (defun base64-encode-region (start end)
249 "Encode current region by base64.
250 START and END are buffer positions.
251 This function calls internal base64 encoder if size of region is
252 smaller than `base64-internal-encoding-limit', otherwise it calls
253 external base64 encoder specified by `base64-external-encoder'. In
254 this case, you must install the program (maybe mmencode included in
255 metamail or XEmacs package)."
257 (if (and base64-internal-encoding-limit
258 (> (- end start) base64-internal-encoding-limit))
259 (base64-external-encode-region start end)
260 (base64-internal-encode-region start end)
263 (defun base64-decode-region (start end)
264 "Decode current region by base64.
265 START and END are buffer positions.
266 This function calls internal base64 decoder if size of region is
267 smaller than `base64-internal-decoding-limit', otherwise it calls
268 external base64 decoder specified by `base64-external-decoder'. In
269 this case, you must install the program (maybe mmencode included in
270 metamail or XEmacs package)."
272 (if (and base64-internal-decoding-limit
273 (> (- end start) base64-internal-decoding-limit))
274 (base64-external-decode-region start end)
275 (base64-internal-decode-region start end)
278 (defun base64-decode-string (string)
279 "Decode STRING which is encoded in base64, and return the result.
280 This function calls internal base64 decoder if size of STRING is
281 smaller than `base64-internal-decoding-limit', otherwise it calls
282 external base64 decoder specified by `base64-external-decoder'. In
283 this case, you must install the program (maybe mmencode included in
284 metamail or XEmacs package)."
286 (if (and base64-internal-decoding-limit
287 (> (length string) base64-internal-decoding-limit))
288 (base64-external-decode-string string)
289 (base64-internal-decode-string string)
293 ;;; @ base64 encoder/decoder for file
296 (defun base64-insert-encoded-file (filename)
297 "Encode contents of file FILENAME to base64, and insert the result.
298 It calls external base64 encoder specified by
299 `base64-external-encoder'. So you must install the program (maybe
300 mmencode included in metamail or XEmacs package)."
301 (interactive (list (read-file-name "Insert encoded file: ")))
302 (apply (function call-process) (car base64-external-encoder)
303 filename t nil (cdr base64-external-encoder))
306 (defun base64-write-decoded-region (start end filename)
307 "Decode and write current region encoded by base64 into FILENAME.
308 START and END are buffer positions."
310 (list (region-beginning) (region-end)
311 (read-file-name "Write decoded region to file: ")))
313 (apply (function call-process-region)
314 start end (car base64-external-decoder)
316 (append (cdr base64-external-decoder)
317 base64-external-decoder-option-to-specify-file
325 (defun base64-encoded-length (string)
326 (let ((len (length string)))
328 (if (= (mod len 3) 0) 0 1)
332 (defun pack-sequence (seq size)
333 "Split sequence SEQ into SIZE elements packs,
334 and return list of packs. [mel-b; tl-seq function]"
335 (let ((len (length seq)) (p 0) obj
339 (setq obj (elt seq p))
340 (setq unit (cons obj unit))
344 (setq dest (cons (reverse unit) dest))
351 (setq dest (cons (reverse unit) dest))
362 ;;; mel-b.el ends here.