1 ;;; mel-b.el: Base64 encoder/decoder for GNU Emacs
3 ;; Copyright (C) 1992,1995,1996,1997 Free Software Foundation, Inc.
5 ;; Author: ENAMI Tsugutomo <enami@sys.ptg.sony.co.jp>
6 ;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;; Maintainer: MORIOKA Tomohiko <morioka@jaist.ac.jp>
9 ;; Version: $Id: mel-b.el,v 6.3 1997/04/30 17:17:42 morioka Exp $
10 ;; Keywords: MIME, Base64
12 ;; This file is part of MEL (MIME Encoding Library).
14 ;; This program is free software; you can redistribute it and/or
15 ;; modify it under the terms of the GNU General Public License as
16 ;; published by the Free Software Foundation; either version 2, or (at
17 ;; your option) any later version.
19 ;; This program is distributed in the hope that it will be useful, but
20 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 ;; General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 ;; Boston, MA 02111-1307, USA.
37 (defvar base64-external-encoder '("mmencode")
38 "*list of base64 encoder program name and its arguments.")
40 (defvar base64-external-decoder '("mmencode" "-u")
41 "*list of base64 decoder program name and its arguments.")
43 (defvar base64-internal-encoding-limit 1000
44 "*limit size to use internal base64 encoder.
45 If size of input to encode is larger than this limit,
46 external encoder is called.")
48 (defvar base64-internal-decoding-limit 1000
49 "*limit size to use internal base64 decoder.
50 If size of input to decode is larger than this limit,
51 external decoder is called.")
54 ;;; @ internal base64 decoder/encoder
55 ;;; based on base64 decoder by Enami Tsugutomo
57 ;;; @@ convert from/to base64 char
60 (defun base64-num-to-char (n)
63 ((< n 52) (+ ?a (- n 26)))
64 ((< n 62) (+ ?0 (- n 52)))
67 (t (error "not a base64 integer %d" n))))
69 (defun base64-char-to-num (c)
70 (cond ((and (<= ?A c) (<= c ?Z)) (- c ?A))
71 ((and (<= ?a c) (<= c ?z)) (+ (- c ?a) 26))
72 ((and (<= ?0 c) (<= c ?9)) (+ (- c ?0) 52))
76 (t (error "not a base64 character %c" c))))
79 ;;; @@ encode/decode one base64 unit
82 (defun base64-encode-1 (pack)
87 (char-to-string (base64-num-to-char (ash a -2)))
91 (base64-num-to-char (logior (ash (logand a 3) 4) (ash b -4))))
95 (base64-num-to-char (logior (ash (logand b 15) 2) (ash c -6))))
96 (char-to-string (base64-num-to-char (logand c 63)))
98 (concat (char-to-string
99 (base64-num-to-char (ash (logand b 15) 2))) "=")
101 (concat (char-to-string
102 (base64-num-to-char (ash (logand a 3) 4))) "==")
105 (defun base64-decode-1 (pack)
106 (let ((a (base64-char-to-num (car pack)))
107 (b (base64-char-to-num (nth 1 pack)))
110 (concat (char-to-string (logior (ash a 2) (ash 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))
119 ;;; @@ base64 encoder/decoder for string
122 (defun base64-encode-string (string)
123 "Encode STRING to base64, and return the result."
124 (let ((len (length string))
131 (function base64-encode-1)
132 (pack-sequence (substring string b e) 3)
139 (let* ((es (mapconcat
140 (function base64-encode-1)
141 (pack-sequence (substring string b) 3)
143 (m (mod (length es) 4))
145 (concat dest es (cond ((= m 3) "=")
150 (defun base64-decode-string (string)
151 "Decode STRING which is encoded in base64, and return the result."
152 (mapconcat (function base64-decode-1)
153 (pack-sequence string 4)
157 ;;; @ base64 encoder/decoder for region
160 (defun base64-internal-encode-region (beg end)
163 (narrow-to-region beg end)
164 (let ((str (buffer-substring beg end)))
165 (delete-region beg end)
166 (insert (base64-encode-string str))
173 (defun base64-internal-decode-region (beg end)
176 (narrow-to-region beg end)
177 (goto-char (point-min))
178 (while (looking-at ".*\n")
181 (base64-decode-string
182 (buffer-substring (match-beginning 0) (1- (match-end 0))))
186 (message (nth 1 err))
187 (replace-match "")))))
188 (if (looking-at ".*$")
191 (base64-decode-string
192 (buffer-substring (match-beginning 0) (match-end 0)))
196 (message (nth 1 err))
201 (defun base64-external-encode-region (beg end)
204 (narrow-to-region beg end)
205 (as-binary-process (apply (function call-process-region)
206 beg end (car base64-external-encoder)
207 t t nil (cdr base64-external-encoder))
210 ;; regularize line break code
211 (goto-char (point-min))
212 (while (re-search-forward "\r$" nil t)
217 (defun base64-external-decode-region (beg end)
219 (as-binary-process (apply (function call-process-region)
220 beg end (car base64-external-decoder)
221 t t nil (cdr base64-external-decoder))
224 (defun base64-encode-region (start end)
225 "Encode current region by base64.
226 START and END are buffer positions.
227 This function calls internal base64 encoder if size of region is
228 smaller than `base64-internal-encoding-limit', otherwise it calls
229 external base64 encoder specified by `base64-external-encoder'. In
230 this case, you must install the program (maybe mmencode included in
231 metamail or XEmacs package)."
233 (if (and base64-internal-encoding-limit
234 (> (- end start) base64-internal-encoding-limit))
235 (base64-external-encode-region start end)
236 (base64-internal-encode-region start end)
239 (defun base64-decode-region (start end)
240 "Decode current region by base64.
241 START and END are buffer positions.
242 This function calls internal base64 decoder if size of region is
243 smaller than `base64-internal-decoding-limit', otherwise it calls
244 external base64 decoder specified by `base64-external-decoder'. In
245 this case, you must install the program (maybe mmencode included in
246 metamail or XEmacs package)."
248 (if (and base64-internal-decoding-limit
249 (> (- end start) base64-internal-decoding-limit))
250 (base64-external-decode-region start end)
251 (base64-internal-decode-region start end)
255 ;;; @ base64 encoder/decoder for file
258 (defun base64-insert-encoded-file (filename)
259 "Encode contents of file FILENAME to base64, and insert the result.
260 It calls external base64 encoder specified by
261 `base64-external-encoder'. So you must install the program (maybe
262 mmencode included in metamail or XEmacs package)."
263 (interactive (list (read-file-name "Insert encoded file: ")))
264 (apply (function call-process) (car base64-external-encoder)
265 filename t nil (cdr base64-external-encoder))
272 (defun base64-encoded-length (string)
273 (let ((len (length string)))
275 (if (= (mod len 3) 0) 0 1)
279 (defun pack-sequence (seq size)
280 "Split sequence SEQ into SIZE elements packs,
281 and return list of packs. [mel-b; tl-seq function]"
282 (let ((len (length seq)) (p 0) obj
286 (setq obj (elt seq p))
287 (setq unit (cons obj unit))
291 (setq dest (cons (reverse unit) dest))
298 (setq dest (cons (reverse unit) dest))
309 ;;; mel-b.el ends here.