1 ;;; mel-b-el.el --- Base64 encoder/decoder.
3 ;; Copyright (C) 1992,95,96,97,98,99,2001 Free Software Foundation, Inc.
5 ;; Author: ENAMI Tsugutomo <enami@sys.ptg.sony.co.jp>
6 ;; MORIOKA Tomohiko <tomo@m17n.org>
8 ;; Keywords: MIME, Base64
10 ;; This file is part of FLIM (Faithful Library about Internet Message).
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 this program; 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.
31 ;; XXX: the macro `as-binary-process' should be provided when compiling.
39 "Base64 encoder/decoder"
42 (defcustom base64-external-encoder '("mmencode")
43 "*list of base64 encoder program name and its arguments."
45 :type '(cons (file :tag "Command")(repeat :tag "Arguments" string)))
47 (defcustom base64-external-decoder '("mmencode" "-u")
48 "*list of base64 decoder program name and its arguments."
50 :type '(cons (file :tag "Command")(repeat :tag "Arguments" string)))
52 (defcustom base64-external-decoder-option-to-specify-file '("-o")
53 "*list of options of base64 decoder program to specify file."
55 :type '(repeat :tag "Arguments" string))
57 (defcustom base64-internal-encoding-limit 1000
58 "*limit size to use internal base64 encoder.
59 If size of input to encode is larger than this limit,
60 external encoder is called."
62 :type '(choice (const :tag "Always use internal encoder" nil)
63 (integer :tag "Size")))
65 (defcustom base64-internal-decoding-limit (if (and (featurep 'xemacs)
69 "*limit size to use internal base64 decoder.
70 If size of input to decode is larger than this limit,
71 external decoder is called."
73 :type '(choice (const :tag "Always use internal decoder" nil)
74 (integer :tag "Size")))
77 ;;; @ utility function
80 (defun pack-sequence (seq size)
81 "Split sequence SEQ into SIZE elements packs, and return list of packs.
82 \[mel-b-el; tl-seq function]"
83 (let ((len (length seq))
87 (setq unit (cons (elt seq p) unit))
89 (when (zerop (mod p size))
90 (setq dest (cons (nreverse unit) dest))
93 (nreverse (cons (nreverse unit) dest))
97 ;;; @ internal base64 encoder
98 ;;; based on base64 decoder by Enami Tsugutomo
101 (defconst base64-characters
102 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")
105 (defmacro base64-num-to-char (n)
106 `(aref base64-characters ,n))
108 (defun base64-encode-1 (pack)
109 (let ((buf (make-string 4 ?=)))
110 (aset buf 0 (base64-num-to-char (ash (car pack) -2)))
113 (aset buf 1 (base64-num-to-char
114 (logior (ash (logand (car pack) 3) 4)
115 (ash (nth 1 pack) -4))))
118 (aset buf 2 (base64-num-to-char
119 (logior (ash (logand (nth 1 pack) 15) 2)
120 (ash (nth 2 pack) -6))))
121 (aset buf 3 (base64-num-to-char
122 (logand (nth 2 pack) 63))))
123 (aset buf 2 (base64-num-to-char
124 (ash (logand (nth 1 pack) 15) 2)))))
125 (aset buf 1 (base64-num-to-char
126 (ash (logand (car pack) 3) 4))))
129 (defun-maybe base64-encode-string (string &optional no-line-break)
130 "Base64-encode STRING and return the result.
131 Optional second argument NO-LINE-BREAK means do not break long lines
133 (let* ((len (length string))
140 (function base64-encode-1)
141 (pack-sequence (substring string b e) 3)
143 (if (not no-line-break) "\n")))
148 (function base64-encode-1)
149 (pack-sequence (substring string b) 3)
152 (defun base64-internal-encode-region (beg end &optional no-line-break)
155 (narrow-to-region beg end)
158 (base64-encode-string (buffer-substring beg end) no-line-break)
159 (delete-region beg end))))))
162 ;;; @ internal base64 decoder
165 (defconst base64-numbers
167 (let ((len (length base64-characters))
168 (vec (make-vector 123 nil))
171 (aset vec (aref base64-characters i) i)
175 (defmacro base64-char-to-num (c)
176 `(aref base64-numbers ,c))
178 (defsubst base64-internal-decode (string buffer)
179 (let* ((len (length string))
184 (when (prog1 (setq v1 (base64-char-to-num (aref string i)))
186 (setq v2 (base64-char-to-num (aref string i))
188 v3 (base64-char-to-num (aref string i))
190 (aset buffer j (logior (lsh v1 2)(lsh v2 -4)))
193 (let ((v4 (base64-char-to-num (aref string i))))
195 (aset buffer j (logior (lsh (logand v2 15) 4)(lsh v3 -2)))
198 (aset buffer (prog1 j (setq j (1+ j)))
199 (logior (lsh (logand v3 3) 6) v4))
202 (substring buffer 0 j)))
204 (defun base64-internal-decode-string (string)
205 (base64-internal-decode string (make-string (length string) 0)))
207 ;; (defsubst base64-decode-string! (string)
208 ;; (setq string (string-as-unibyte string))
209 ;; (base64-internal-decode string string))
211 (defun base64-internal-decode-region (beg end)
213 (let ((str (string-as-unibyte (buffer-substring beg end))))
216 (base64-internal-decode str str)
217 (delete-region beg end))))))
219 ;; (defun base64-internal-decode-region2 (beg end)
221 ;; (let ((str (buffer-substring beg end)))
222 ;; (delete-region beg end)
224 ;; (insert (base64-decode-string! str)))))
226 ;; (defun base64-internal-decode-region3 (beg end)
228 ;; (let ((str (buffer-substring beg end)))
229 ;; (delete-region beg end)
231 ;; (insert (base64-internal-decode-string str)))))
234 ;;; @ external encoder/decoder
237 (defun base64-external-encode-region (beg end &optional no-line-break)
240 (narrow-to-region beg end)
242 (apply (function call-process-region)
243 beg end (car base64-external-encoder)
245 (cdr base64-external-encoder)))
247 ;; regularize line break code
248 (goto-char (point-min))
249 (while (re-search-forward "\r$" nil t)
253 (goto-char (point-min))
254 (while (search-forward "\n" nil t)
255 (replace-match "")))))))
257 (defun base64-external-decode-region (beg end)
260 (apply (function call-process-region)
261 beg end (car base64-external-decoder)
263 (cdr base64-external-decoder)))))
265 (defun base64-external-decode-string (string)
269 (apply (function call-process-region)
270 (point-min)(point-max) (car base64-external-decoder)
272 (cdr base64-external-decoder)))
276 ;;; @ application interfaces
279 (defun-maybe base64-encode-region (start end &optional no-line-break)
280 "Base64-encode the region between START and END.
281 Return the length of the encoded text.
282 Optional third argument NO-LINE-BREAK means do not break long lines
284 This function calls internal base64 encoder if size of region is
285 smaller than `base64-internal-encoding-limit', otherwise it calls
286 external base64 encoder specified by `base64-external-encoder'. In
287 this case, you must install the program (maybe mmencode included in
288 metamail or XEmacs package)."
290 (if (and base64-internal-encoding-limit
291 (> (- end start) base64-internal-encoding-limit))
292 (base64-external-encode-region start end no-line-break)
293 (base64-internal-encode-region start end no-line-break)))
295 (defun-maybe base64-decode-region (start end)
296 "Decode current region by base64.
297 START and END are buffer positions.
298 This function calls internal base64 decoder if size of region is
299 smaller than `base64-internal-decoding-limit', otherwise it calls
300 external base64 decoder specified by `base64-external-decoder'. In
301 this case, you must install the program (maybe mmencode included in
302 metamail or XEmacs package)."
304 (if (and base64-internal-decoding-limit
305 (> (- end start) base64-internal-decoding-limit))
306 (base64-external-decode-region start end)
307 (base64-internal-decode-region start end)))
309 (defun-maybe base64-decode-string (string)
310 "Decode STRING which is encoded in base64, and return the result.
311 This function calls internal base64 decoder if size of STRING is
312 smaller than `base64-internal-decoding-limit', otherwise it calls
313 external base64 decoder specified by `base64-external-decoder'. In
314 this case, you must install the program (maybe mmencode included in
315 metamail or XEmacs package)."
316 (if (and base64-internal-decoding-limit
317 (> (length string) base64-internal-decoding-limit))
318 (base64-external-decode-string string)
319 (base64-internal-decode-string string)))
322 (mel-define-method-function (mime-encode-string string (nil "base64"))
323 'base64-encode-string)
324 (mel-define-method-function (mime-decode-string string (nil "base64"))
325 'base64-decode-string)
326 (mel-define-method-function (mime-encode-region start end (nil "base64"))
327 'base64-encode-region)
328 (mel-define-method-function (mime-decode-region start end (nil "base64"))
329 'base64-decode-region)
331 (mel-define-method-function (encoded-text-encode-string string (nil "B"))
332 'base64-encode-string)
334 (mel-define-method encoded-text-decode-string (string (nil "B"))
335 (if (string-match (eval-when-compile
336 (concat "\\`" B-encoded-text-regexp "\\'"))
338 (base64-decode-string string)
339 (error "Invalid encoded-text %s" string)))
341 (defun base64-insert-encoded-file (filename)
342 "Encode contents of file FILENAME to base64, and insert the result.
343 It calls external base64 encoder specified by
344 `base64-external-encoder'. So you must install the program (maybe
345 mmencode included in metamail or XEmacs package)."
346 (interactive "*fInsert encoded file: ")
347 (if (and base64-internal-encoding-limit
348 (> (nth 7 (file-attributes filename))
349 base64-internal-encoding-limit))
350 (apply (function call-process)
351 (car base64-external-encoder)
353 (cdr base64-external-encoder))
355 (base64-encode-string
357 (set-buffer-multibyte nil)
358 (insert-file-contents-as-binary filename)
360 (or (bolp) (insert ?\n))))
362 (mel-define-method-function (mime-insert-encoded-file filename (nil "base64"))
363 'base64-insert-encoded-file)
365 (defun base64-write-decoded-region (start end filename)
366 "Decode and write current region encoded by base64 into FILENAME.
367 START and END are buffer positions."
368 (interactive "*r\nFWrite decoded region to file: ")
369 (if (and base64-internal-decoding-limit
370 (> (- end start) base64-internal-decoding-limit))
372 (apply (function call-process-region)
373 start end (car base64-external-decoder)
375 (append (cdr base64-external-decoder)
376 base64-external-decoder-option-to-specify-file
378 (let ((str (buffer-substring start end)))
380 (insert (base64-internal-decode-string str))
381 (write-region-as-binary (point-min) (point-max) filename)))))
383 (mel-define-method-function
384 (mime-write-decoded-region start end filename (nil "base64"))
385 'base64-write-decoded-region)
393 ;;; mel-b-el.el ends here.