annotate lisp/url/base64.el @ 7:c153ca296910

Added tag r19-15b4 for changeset 27bc7f280385
author cvs
date Mon, 13 Aug 2007 08:47:16 +0200
parents 376386a54a3c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1 ;;; base64.el,v --- Base64 encoding functions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2 ;; Author: wmperry
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3 ;; Created: 1996/04/22 15:08:08
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4 ;; Version: 1.7
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5 ;; Keywords: extensions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7 ;;; LCD Archive Entry:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8 ;;; base64.el|William M. Perry|wmperry@spry.com|
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9 ;;; Package for encoding/decoding base64 data (MIME)|
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10 ;;; 1996/04/22 15:08:08|1.7|Location Undetermined
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
11 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
12
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14 ;;; Base 64 encoding functions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15 ;;; This code was converted to lisp code by me from the C code in
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
16 ;;; ftp://cs.utk.edu/pub/MIME/b64encode.c
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
17 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
18
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
19 (defvar base64-code-string
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
20 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
21 "Character set used for base64 decoding")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
22
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23 (defvar base64-decode-vector
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24 (let ((vec (make-vector 256 nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25 (i 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26 (case-fold-search nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27 (while (< i 256)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28 (aset vec i (string-match (regexp-quote (char-to-string i))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29 base64-code-string))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30 (setq i (1+ i)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31 vec))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33 (defvar base64-max-line-length 64)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 ;(defun b0 (x) (aref base64-code-string (logand (lsh x -18) 63)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36 ;(defun b1 (x) (aref base64-code-string (logand (lsh x -12) 63)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37 ;(defun b2 (x) (aref base64-code-string (logand (lsh x -6) 63)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 ;(defun b3 (x) (aref base64-code-string (logand x 63)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 (defmacro b0 (x) (` (aref base64-code-string (logand (lsh (, x) -18) 63))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 (defmacro b1 (x) (` (aref base64-code-string (logand (lsh (, x) -12) 63))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 (defmacro b2 (x) (` (aref base64-code-string (logand (lsh (, x) -6) 63))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 (defmacro b3 (x) (` (aref base64-code-string (logand (, x) 63))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 (defun base64-encode (str)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 "Do base64 encoding on string STR and return the encoded string.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47 This code was converted to lisp code by me from the C code in
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 ftp://cs.utk.edu/pub/MIME/b64encode.c. Returns a string that is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 broken into `base64-max-line-length' byte lines."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 (or str (setq str (buffer-string)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 (let ((x (base64-encode-internal str))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 (y ""))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 (while (> (length x) base64-max-line-length)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54 (setq y (concat y (substring x 0 base64-max-line-length) "\n")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 x (substring x base64-max-line-length nil)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 (setq y (concat y x))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 y))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 (defun base64-encode-internal (str)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 "Do base64 encoding on string STR and return the encoded string.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61 This code was converted to lisp code by me from the C code in
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 ftp://cs.utk.edu/pub/MIME/b64encode.c. Returns the entire string,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 not broken up into `base64-max-line-length' byte lines."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 (let (
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 (word 0) ; The word to translate
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 w1 w2 w3
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 (cond
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 ((> (length str) 3)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 (concat
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71 (base64-encode-internal (substring str 0 3))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 (base64-encode-internal (substring str 3 nil))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73 ((= (length str) 3)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 (setq w1 (aref str 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 w2 (aref str 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 w3 (aref str 2)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 word (logior
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 (lsh (logand w1 255) 16)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 (lsh (logand w2 255) 8)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 (logand w3 255)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 (format "%c%c%c%c" (b0 word) (b1 word) (b2 word) (b3 word)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 ((= (length str) 2)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 (setq w1 (aref str 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84 w2 (aref str 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 word (logior
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 (lsh (logand w1 255) 16)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 (lsh (logand w2 255) 8)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 (format "%c%c%c=" (b0 word) (b1 word) (b2 word)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 ((= (length str) 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91 (setq w1 (aref str 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92 word (logior
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 (lsh (logand w1 255) 16)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 (format "%c%c==" (b0 word) (b1 word)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 (t ""))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 ;;; Base64 decoding functions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 ;;; Most of the decoding code is courtesy Francesco Potorti`
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 ;;; <F.Potorti@cnuce.cnr.it>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 ;;; this is much faster than my original code - thanks!
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 (defun base64-decode-region (beg end)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 (interactive "r")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 (barf-if-buffer-read-only)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 (let
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 ((exchange (= (point) beg))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 (endchars 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 (list) (code))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 (goto-char beg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112 (while (< (point) end)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 (setq list (mapcar
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 (function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 (lambda (c)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 (cond
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 ((aref base64-decode-vector c))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 ((char-equal c ?=)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119 (setq endchars (1+ endchars))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 (nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122 (error
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123 "Character %c does not match Mime base64 coding" c)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 (buffer-substring (point) (+ (point) 4))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 (setq code (+ (nth 3 list) (lsh (nth 2 list) 6)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 (lsh (nth 1 list) 12) (lsh (car list) 18)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 (delete-char 4)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 (cond
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 ((zerop endchars)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 (insert (% (lsh code -16) 256) (% (lsh code -8) 256) (% code 256)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 ((= endchars 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 (insert (% (lsh code -16) 256) (% (lsh code -8) 256))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133 (setq end (point)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 ((= endchars 2)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 (insert (% (lsh code -16) 256))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 (setq end (point))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 (if (char-equal (following-char) ?\n)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 (progn (delete-char 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139 (setq end (- end 2)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 (setq end (1- end))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 ))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 ; (if exchange
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143 ; (exchange-point-and-mark))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 (defun base64-decode (st &optional nd)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 "Do base64 decoding on string STR and return the original string.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147 If given buffer positions, destructively decodes that area of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 current buffer."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149 (let ((replace-p nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150 (retval nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151 (if (stringp st)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152 nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153 (setq st (prog1
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154 (buffer-substring st (or nd (point-max)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155 (delete-region st (or nd (point-max))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156 replace-p t))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157 (setq retval
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159 (set-buffer (get-buffer-create " *b64decode*"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160 (erase-buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161 (insert st)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162 (goto-char (point-min))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163 (while (re-search-forward "\r*\n" nil t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164 (replace-match ""))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165 (goto-char (point-min))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166 (base64-decode-region (point-min) (point-max))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167 (buffer-string)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168 (if replace-p (insert retval))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169 retval))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171 (provide 'base64)