annotate lisp/w3/base64.el @ 27:0a3286277d9b

Added tag r19-15b96 for changeset 441bb1e64a06
author cvs
date Mon, 13 Aug 2007 08:51:34 +0200
parents 0293115a14e9
children e04119814345
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
1 ;;; base64.el,v --- Base64 encoding functions
16
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
2 ;; Author: Kyle E. Jones
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
3 ;; Created: 1997/01/23 00:13:17
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
4 ;; Version: 1.4
14
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
5 ;; Keywords: extensions
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
6
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
16
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
8 ;;; Copyright (C) 1997 Kyle E. Jones
14
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
9 ;;;
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
10 ;;; This file is not part of GNU Emacs, but the same permissions apply.
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
11 ;;;
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
12 ;;; GNU Emacs is free software; you can redistribute it and/or modify
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
13 ;;; it under the terms of the GNU General Public License as published by
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
14 ;;; the Free Software Foundation; either version 2, or (at your option)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
15 ;;; any later version.
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
16 ;;;
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
17 ;;; GNU Emacs is distributed in the hope that it will be useful,
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
18 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
19 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
20 ;;; GNU General Public License for more details.
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
21 ;;;
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
22 ;;; You should have received a copy of the GNU General Public License
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
23 ;;; along with GNU Emacs; see the file COPYING. If not, write to the
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
24 ;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
25 ;;; Boston, MA 02111-1307, USA.
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
26 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
27
16
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
28 ;; For non-MULE
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
29 (if (not (fboundp 'char-int))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
30 (fset 'char-int 'identity))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
31
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
32 (defvar base64-alphabet
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
33 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")
14
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
34
16
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
35 (defvar base64-decoder-program nil
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
36 "*Non-nil value should be a string that names a MIME base64 decoder.
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
37 The program should expect to read base64 data on its standard
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
38 input and write the converted data to its standard output.")
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
39
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
40 (defvar base64-decoder-switches nil
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
41 "*List of command line flags passed to the command named by
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
42 base64-decoder-program.")
14
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
43
16
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
44 (defvar base64-encoder-program nil
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
45 "*Non-nil value should be a string that names a MIME base64 encoder.
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
46 The program should expect arbitrary data on its standard
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
47 input and write base64 data to its standard output.")
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
48
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
49 (defvar base64-encoder-switches nil
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
50 "*List of command line flags passed to the command named by
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
51 base64-encoder-program.")
14
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
52
16
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
53 (defconst base64-alphabet-decoding-alist
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
54 '(
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
55 ( ?A . 00) ( ?B . 01) ( ?C . 02) ( ?D . 03) ( ?E . 04) ( ?F . 05)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
56 ( ?G . 06) ( ?H . 07) ( ?I . 08) ( ?J . 09) ( ?K . 10) ( ?L . 11)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
57 ( ?M . 12) ( ?N . 13) ( ?O . 14) ( ?P . 15) ( ?Q . 16) ( ?R . 17)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
58 ( ?S . 18) ( ?T . 19) ( ?U . 20) ( ?V . 21) ( ?W . 22) ( ?X . 23)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
59 ( ?Y . 24) ( ?Z . 25) ( ?a . 26) ( ?b . 27) ( ?c . 28) ( ?d . 29)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
60 ( ?e . 30) ( ?f . 31) ( ?g . 32) ( ?h . 33) ( ?i . 34) ( ?j . 35)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
61 ( ?k . 36) ( ?l . 37) ( ?m . 38) ( ?n . 39) ( ?o . 40) ( ?p . 41)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
62 ( ?q . 42) ( ?r . 43) ( ?s . 44) ( ?t . 45) ( ?u . 46) ( ?v . 47)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
63 ( ?w . 48) ( ?x . 49) ( ?y . 50) ( ?z . 51) ( ?0 . 52) ( ?1 . 53)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
64 ( ?2 . 54) ( ?3 . 55) ( ?4 . 56) ( ?5 . 57) ( ?6 . 58) ( ?7 . 59)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
65 ( ?8 . 60) ( ?9 . 61) ( ?+ . 62) ( ?/ . 63)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
66 ))
14
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
67
16
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
68 (defvar base64-alphabet-decoding-vector
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
69 (let ((v (make-vector 123 nil))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
70 (p base64-alphabet-decoding-alist))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
71 (while p
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
72 (aset v (car (car p)) (cdr (car p)))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
73 (setq p (cdr p)))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
74 v))
14
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
75
16
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
76 (defun base64-run-command-on-region (start end output-buffer command
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
77 &rest arg-list)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
78 (let ((tempfile nil) status errstring)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
79 (unwind-protect
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
80 (progn
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
81 (setq tempfile (make-temp-name "base64"))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
82 (setq status
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
83 (apply 'call-process-region
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
84 start end command nil
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
85 (list output-buffer tempfile)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
86 nil arg-list))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
87 (cond ((equal status 0) t)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
88 ((zerop (save-excursion
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
89 (set-buffer (find-file-noselect tempfile))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
90 (buffer-size)))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
91 t)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
92 (t (save-excursion
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
93 (set-buffer (find-file-noselect tempfile))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
94 (setq errstring (buffer-string))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
95 (kill-buffer nil)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
96 (cons status errstring)))))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
97 (condition-case ()
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
98 (delete-file tempfile)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
99 (error nil)))))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
100
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
101 (defun base64-insert-char (char &optional count ignored buffer)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
102 (condition-case nil
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
103 (progn
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
104 (insert-char char count ignored buffer)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
105 (fset 'vm-insert-char 'insert-char))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
106 (wrong-number-of-arguments
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
107 (fset 'base64-insert-char 'base64-xemacs-insert-char)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
108 (base64-insert-char char count ignored buffer))))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
109
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
110 (defun base64-xemacs-insert-char (char &optional count ignored buffer)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
111 (if (and buffer (eq buffer (current-buffer)))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
112 (insert-char char count)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
113 (save-excursion
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
114 (set-buffer buffer)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
115 (insert-char char count))))
14
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
116
16
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
117 (defun base64-decode-region (start end)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
118 (interactive "r")
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
119 (message "Decoding base64...")
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
120 (let ((work-buffer nil)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
121 (done nil)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
122 (counter 0)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
123 (bits 0)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
124 (lim 0) inputpos
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
125 (non-data-chars (concat "^=" base64-alphabet)))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
126 (unwind-protect
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
127 (save-excursion
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
128 (setq work-buffer (generate-new-buffer " *base64-work*"))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
129 (buffer-disable-undo work-buffer)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
130 (if base64-decoder-program
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
131 (let* ((binary-process-output t) ; any text already has CRLFs
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
132 (status (apply 'command-on-region
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
133 start end work-buffer
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
134 base64-decoder-program
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
135 base64-decoder-switches)))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
136 (if (not (eq status t))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
137 (error "%s" (cdr status))))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
138 (goto-char start)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
139 (skip-chars-forward non-data-chars end)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
140 (while (not done)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
141 (setq inputpos (point))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
142 (cond
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
143 ((> (skip-chars-forward base64-alphabet end) 0)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
144 (setq lim (point))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
145 (while (< inputpos lim)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
146 (setq bits (+ bits
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
147 (aref base64-alphabet-decoding-vector
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
148 (char-int (char-after inputpos)))))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
149 (setq counter (1+ counter)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
150 inputpos (1+ inputpos))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
151 (cond ((= counter 4)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
152 (base64-insert-char (lsh bits -16) 1 nil work-buffer)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
153 (base64-insert-char (logand (lsh bits -8) 255) 1 nil
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
154 work-buffer)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
155 (base64-insert-char (logand bits 255) 1 nil
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
156 work-buffer)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
157 (setq bits 0 counter 0))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
158 (t (setq bits (lsh bits 6)))))))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
159 (cond
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
160 ((= (point) end)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
161 (if (not (zerop counter))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
162 (error "at least %d bits missing at end of base64 encoding"
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
163 (* (- 4 counter) 6)))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
164 (setq done t))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
165 ((= (char-after (point)) ?=)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
166 (setq done t)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
167 (cond ((= counter 1)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
168 (error "at least 2 bits missing at end of base64 encoding"))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
169 ((= counter 2)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
170 (base64-insert-char (lsh bits -10) 1 nil work-buffer))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
171 ((= counter 3)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
172 (base64-insert-char (lsh bits -16) 1 nil work-buffer)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
173 (base64-insert-char (logand (lsh bits -8) 255)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
174 1 nil work-buffer))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
175 ((= counter 0) t)))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
176 (t (skip-chars-forward non-data-chars end)))))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
177 (or (markerp end) (setq end (set-marker (make-marker) end)))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
178 (goto-char start)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
179 (insert-buffer-substring work-buffer)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
180 (delete-region (point) end))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
181 (and work-buffer (kill-buffer work-buffer))))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
182 (message "Decoding base64... done"))
14
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
183
16
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
184 (defun base64-encode-region (start end)
14
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
185 (interactive "r")
16
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
186 (message "Encoding base64...")
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
187 (let ((work-buffer nil)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
188 (counter 0)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
189 (cols 0)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
190 (bits 0)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
191 (alphabet base64-alphabet)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
192 inputpos)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
193 (unwind-protect
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
194 (save-excursion
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
195 (setq work-buffer (generate-new-buffer " *base64-work*"))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
196 (buffer-disable-undo work-buffer)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
197 (if base64-encoder-program
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
198 (let ((status (apply 'base64-run-command-on-region
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
199 start end work-buffer
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
200 base64-encoder-program
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
201 base64-encoder-switches)))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
202 (if (not (eq status t))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
203 (error "%s" (cdr status))))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
204 (setq inputpos start)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
205 (while (< inputpos end)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
206 (setq bits (+ bits (char-int (char-after inputpos))))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
207 (setq counter (1+ counter))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
208 (cond ((= counter 3)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
209 (base64-insert-char (aref alphabet (lsh bits -18)) 1 nil
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
210 work-buffer)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
211 (base64-insert-char
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
212 (aref alphabet (logand (lsh bits -12) 63))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
213 1 nil work-buffer)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
214 (base64-insert-char
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
215 (aref alphabet (logand (lsh bits -6) 63))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
216 1 nil work-buffer)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
217 (base64-insert-char
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
218 (aref alphabet (logand bits 63))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
219 1 nil work-buffer)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
220 (setq cols (+ cols 4))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
221 (cond ((= cols 72)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
222 (base64-insert-char ?\n 1 nil work-buffer)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
223 (setq cols 0)))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
224 (setq bits 0 counter 0))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
225 (t (setq bits (lsh bits 8))))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
226 (setq inputpos (1+ inputpos)))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
227 ;; write out any remaining bits with appropriate padding
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
228 (if (= counter 0)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
229 nil
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
230 (setq bits (lsh bits (- 16 (* 8 counter))))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
231 (base64-insert-char (aref alphabet (lsh bits -18)) 1 nil
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
232 work-buffer)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
233 (base64-insert-char (aref alphabet (logand (lsh bits -12) 63))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
234 1 nil work-buffer)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
235 (if (= counter 1)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
236 (base64-insert-char ?= 2 nil work-buffer)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
237 (base64-insert-char (aref alphabet (logand (lsh bits -6) 63))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
238 1 nil work-buffer)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
239 (base64-insert-char ?= 1 nil work-buffer)))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
240 (if (> cols 0)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
241 (base64-insert-char ?\n 1 nil work-buffer)))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
242 (or (markerp end) (setq end (set-marker (make-marker) end)))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
243 (goto-char start)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
244 (insert-buffer-substring work-buffer)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
245 (delete-region (point) end))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
246 (and work-buffer (kill-buffer work-buffer))))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
247 (message "Encoding base64... done"))
14
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
248
16
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
249 (defun base64-encode (string)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
250 (save-excursion
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
251 (set-buffer (get-buffer-create " *base64-encode*"))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
252 (erase-buffer)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
253 (insert string)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
254 (base64-encode-region (point-min) (point-max))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
255 (skip-chars-backward " \t\r\n")
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
256 (delete-region (point-max) (point))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
257 (prog1
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
258 (buffer-string)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
259 (kill-buffer (current-buffer)))))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
260
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
261 (defun base64-decode (string)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
262 (save-excursion
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
263 (set-buffer (get-buffer-create " *base64-decode*"))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
264 (erase-buffer)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
265 (insert string)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
266 (base64-decode-region (point-min) (point-max))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
267 (goto-char (point-max))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
268 (skip-chars-backward " \t\r\n")
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
269 (delete-region (point-max) (point))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
270 (prog1
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
271 (buffer-string)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
272 (kill-buffer (current-buffer)))))
14
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
273
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
274 (provide 'base64)