1 ;;; base64.el,v --- Base64 encoding functions
2 ;; Author: Kyle E. Jones
3 ;; Created: 1997/03/12 14:37:09
5 ;; Keywords: extensions
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
8 ;;; Copyright (C) 1997 Kyle E. Jones
10 ;;; This file is not part of GNU Emacs, but the same permissions apply.
12 ;;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;;; it under the terms of the GNU General Public License as published by
14 ;;; the Free Software Foundation; either version 2, or (at your option)
15 ;;; any later version.
17 ;;; GNU Emacs is distributed in the hope that it will be useful,
18 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;;; GNU 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.
26 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
31 (if (not (fboundp 'char-int))
32 (fset 'char-int 'identity))
34 (defvar base64-alphabet
35 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")
37 (defvar base64-decoder-program nil
38 "*Non-nil value should be a string that names a MIME base64 decoder.
39 The program should expect to read base64 data on its standard
40 input and write the converted data to its standard output.")
42 (defvar base64-decoder-switches nil
43 "*List of command line flags passed to the command named by
44 base64-decoder-program.")
46 (defvar base64-encoder-program nil
47 "*Non-nil value should be a string that names a MIME base64 encoder.
48 The program should expect arbitrary data on its standard
49 input and write base64 data to its standard output.")
51 (defvar base64-encoder-switches nil
52 "*List of command line flags passed to the command named by
53 base64-encoder-program.")
55 (defconst base64-alphabet-decoding-alist
57 ( ?A . 00) ( ?B . 01) ( ?C . 02) ( ?D . 03) ( ?E . 04) ( ?F . 05)
58 ( ?G . 06) ( ?H . 07) ( ?I . 08) ( ?J . 09) ( ?K . 10) ( ?L . 11)
59 ( ?M . 12) ( ?N . 13) ( ?O . 14) ( ?P . 15) ( ?Q . 16) ( ?R . 17)
60 ( ?S . 18) ( ?T . 19) ( ?U . 20) ( ?V . 21) ( ?W . 22) ( ?X . 23)
61 ( ?Y . 24) ( ?Z . 25) ( ?a . 26) ( ?b . 27) ( ?c . 28) ( ?d . 29)
62 ( ?e . 30) ( ?f . 31) ( ?g . 32) ( ?h . 33) ( ?i . 34) ( ?j . 35)
63 ( ?k . 36) ( ?l . 37) ( ?m . 38) ( ?n . 39) ( ?o . 40) ( ?p . 41)
64 ( ?q . 42) ( ?r . 43) ( ?s . 44) ( ?t . 45) ( ?u . 46) ( ?v . 47)
65 ( ?w . 48) ( ?x . 49) ( ?y . 50) ( ?z . 51) ( ?0 . 52) ( ?1 . 53)
66 ( ?2 . 54) ( ?3 . 55) ( ?4 . 56) ( ?5 . 57) ( ?6 . 58) ( ?7 . 59)
67 ( ?8 . 60) ( ?9 . 61) ( ?+ . 62) ( ?/ . 63)
70 (defvar base64-alphabet-decoding-vector
71 (let ((v (make-vector 123 nil))
72 (p base64-alphabet-decoding-alist))
74 (aset v (car (car p)) (cdr (car p)))
78 (defun base64-run-command-on-region (start end output-buffer command
80 (let ((tempfile nil) status errstring default-process-coding-system)
83 (setq tempfile (make-temp-name "base64"))
85 (apply 'call-process-region
87 (list output-buffer tempfile)
89 (cond ((equal status 0) t)
90 ((zerop (save-excursion
91 (set-buffer (find-file-noselect tempfile))
95 (set-buffer (find-file-noselect tempfile))
96 (setq errstring (buffer-string))
98 (cons status errstring)))))
100 (delete-file tempfile)
103 (if (string-match "XEmacs" emacs-version)
104 (defalias 'base64-insert-char 'insert-char)
105 (defun base64-insert-char (char &optional count ignored buffer)
106 (if (or (null buffer) (eq buffer (current-buffer)))
107 (insert-char char count)
108 (with-current-buffer buffer
109 (insert-char char count)))))
111 (defun-maybe base64-decode-region (start end)
113 ;;(message "Decoding base64...")
114 (let ((work-buffer nil)
119 (non-data-chars (concat "^=" base64-alphabet)))
122 (setq work-buffer (generate-new-buffer " *base64-work*"))
123 (buffer-disable-undo work-buffer)
124 (if base64-decoder-program
125 (let* ((binary-process-output t) ; any text already has CRLFs
126 (status (apply 'base64-run-command-on-region
127 start end work-buffer
128 base64-decoder-program
129 base64-decoder-switches)))
130 (if (not (eq status t))
131 (error "%s" (cdr status))))
133 (skip-chars-forward non-data-chars end)
135 (setq inputpos (point))
137 ((> (skip-chars-forward base64-alphabet end) 0)
139 (while (< inputpos lim)
141 (aref base64-alphabet-decoding-vector
142 (char-int (char-after inputpos)))))
143 (setq counter (1+ counter)
144 inputpos (1+ inputpos))
146 (base64-insert-char (lsh bits -16) 1 nil work-buffer)
147 (base64-insert-char (logand (lsh bits -8) 255) 1 nil
149 (base64-insert-char (logand bits 255) 1 nil
151 (setq bits 0 counter 0))
152 (t (setq bits (lsh bits 6)))))))
155 (if (not (zerop counter))
156 (error "at least %d bits missing at end of base64 encoding"
157 (* (- 4 counter) 6)))
159 ((eq (char-after (point)) ?=)
162 (error "at least 2 bits missing at end of base64 encoding"))
164 (base64-insert-char (lsh bits -10) 1 nil work-buffer))
166 (base64-insert-char (lsh bits -16) 1 nil work-buffer)
167 (base64-insert-char (logand (lsh bits -8) 255)
170 (t (skip-chars-forward non-data-chars end)))))
171 (or (markerp end) (setq end (set-marker (make-marker) end)))
173 (insert-buffer-substring work-buffer)
174 (delete-region (point) end))
175 (and work-buffer (kill-buffer work-buffer))))
176 ;;(message "Decoding base64... done")
179 (defun-maybe base64-encode-region (start end &optional no-line-break)
181 (message "Encoding base64...")
182 (let ((work-buffer nil)
186 (alphabet base64-alphabet)
190 (setq work-buffer (generate-new-buffer " *base64-work*"))
191 (buffer-disable-undo work-buffer)
192 (if base64-encoder-program
193 (let ((status (apply 'base64-run-command-on-region
194 start end work-buffer
195 base64-encoder-program
196 base64-encoder-switches)))
197 (if (not (eq status t))
198 (error "%s" (cdr status))))
199 (setq inputpos start)
200 (while (< inputpos end)
201 (setq bits (+ bits (char-int (char-after inputpos))))
202 (setq counter (1+ counter))
204 (base64-insert-char (aref alphabet (lsh bits -18)) 1 nil
207 (aref alphabet (logand (lsh bits -12) 63))
210 (aref alphabet (logand (lsh bits -6) 63))
213 (aref alphabet (logand bits 63))
215 (setq cols (+ cols 4))
216 (cond ((and (= cols 72)
218 (base64-insert-char ?\n 1 nil work-buffer)
220 (setq bits 0 counter 0))
221 (t (setq bits (lsh bits 8))))
222 (setq inputpos (1+ inputpos)))
223 ;; write out any remaining bits with appropriate padding
226 (setq bits (lsh bits (- 16 (* 8 counter))))
227 (base64-insert-char (aref alphabet (lsh bits -18)) 1 nil
229 (base64-insert-char (aref alphabet (logand (lsh bits -12) 63))
232 (base64-insert-char ?= 2 nil work-buffer)
233 (base64-insert-char (aref alphabet (logand (lsh bits -6) 63))
235 (base64-insert-char ?= 1 nil work-buffer)))
238 (base64-insert-char ?\n 1 nil work-buffer)))
239 (or (markerp end) (setq end (set-marker (make-marker) end)))
241 (insert-buffer-substring work-buffer)
242 (delete-region (point) end))
243 (and work-buffer (kill-buffer work-buffer))))
244 (message "Encoding base64... done"))
246 (defun base64-encode (string)
248 (set-buffer (get-buffer-create " *base64-encode*"))
251 (base64-encode-region (point-min) (point-max))
252 (skip-chars-backward " \t\r\n")
253 (delete-region (point-max) (point))
256 (kill-buffer (current-buffer)))))
258 (defun base64-decode (string)
260 (set-buffer (get-buffer-create " *base64-decode*"))
263 (base64-decode-region (point-min) (point-max))
264 (goto-char (point-max))
265 (skip-chars-backward " \t\r\n")
266 (delete-region (point-max) (point))
269 (kill-buffer (current-buffer)))))
271 (fset 'base64-decode-string 'base64-decode)