1 ;;;; This file implements the Base64 transfer encoding algorithm as
2 ;;;; defined in RFC 1521 by Borensten & Freed, September 1993.
3 ;;;; See: http://www.ietf.org/rfc/rfc1521.txt
5 ;;;; Based on initial public domain code by Juri Pakaste <juri@iki.fi>
7 ;;;; Extended by Kevin M. Rosenberg <kevin@rosenberg.net>:
9 ;;;; - numerous speed optimizations
10 ;;;; - conversion to and from integers
11 ;;;; - Renamed functions now that supporting integer conversions
12 ;;;; - URI-compatible encoding using :uri key
14 ;;;; Copyright 2002-2003 Kevin M. Rosenberg
15 ;;;; Permission to use with BSD-style license included in the COPYING file
17 ;;;; $Id: src.lisp,v 1.6 2003/01/04 13:43:27 kevin Exp $
21 (:export #:base64-to-string #:base64-to-integer
22 #:string-to-base64 #:integer-to-base64))
27 (eval-when (:compile-toplevel :load-toplevel :execute)
28 (defvar *encode-table*
29 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")
30 (declaim (type simple-string *encode-table*))
32 (defvar *uri-encode-table*
33 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_")
34 (declaim (type simple-string *uri-encode-table*))
36 (deftype decode-table () '(simple-array fixnum (256)))
38 (defvar *decode-table*
39 (let ((da (make-array 256 :adjustable nil :fill-pointer nil
41 :initial-element -1)))
42 (loop for char of-type character across *encode-table*
43 for index of-type fixnum from 0 below 64
44 do (setf (aref da (the fixnum (char-code char))) index))
47 (defvar *uri-decode-table*
48 (let ((da (make-array 256 :adjustable nil :fill-pointer nil
50 :initial-element -1)))
52 for char of-type character across *uri-encode-table*
53 for index of-type fixnum from 0 below 64
54 do (setf (aref da (the fixnum (char-code char))) index))
57 (declaim (type decode-table *decode-table* *uri-decode-table*))
59 (defvar *pad-char* #\=)
60 (defvar *uri-pad-char* #\.)
61 (declaim (type character *pad-char* *uri-pad-char*))
67 (defun round-next-multiple (x n)
68 "Round x up to the next highest multiple of n."
71 (let ((remainder (mod x n)))
72 (declare (fixnum remainder))
75 (the fixnum (+ x (the fixnum (- n remainder)))))))
77 (declaim (inline whitespace-p))
78 (defun whitespace-p (c)
79 "Returns T for a whitespace character."
80 (or (char= c #\Newline) (char= c #\Linefeed)
81 (char= c #\Return) (char= c #\Space)
87 (defun string-to-base64 (string &key (uri nil) (columns 0) (stream nil))
88 "Encode a string array to base64. If columns is > 0, designates
89 maximum number of columns in a line and the string will be terminated
91 (declare (string string)
94 (let ((pad (if uri *uri-pad-char* *pad-char*))
95 (encode-table (if uri *uri-encode-table* *encode-table*)))
96 (declare (simple-string encode-table)
98 (let* ((string-length (length string))
99 (complete-group-count (truncate string-length 3))
100 (remainder (nth-value 1 (truncate string-length 3)))
101 (padded-length (* 4 (truncate (+ string-length 2) 3)))
102 (num-lines (if (plusp columns)
103 (truncate (+ padded-length (1- columns)) columns)
105 (num-breaks (if (plusp num-lines)
110 (+ padded-length num-breaks)))
111 (result (make-string strlen))
112 (col (if (plusp columns)
116 (declare (fixnum string-length padded-length col ioutput)
117 (simple-string result))
118 (labels ((output-char (ch)
119 (when (= col columns)
121 (write-char #\Newline stream)
123 (setf (schar result ioutput) #\Newline)
128 (write-char ch stream)
130 (setf (schar result ioutput) ch)
132 (output-group (svalue chars)
133 (declare (fixnum svalue chars))
138 (the fixnum (ash svalue -18))))))
143 (the fixnum (ash svalue -12))))))
149 (the fixnum (ash svalue -6))))))
155 (logand #x3f svalue))))
157 (do ((igroup 0 (1+ igroup))
158 (isource 0 (+ isource 3)))
159 ((= igroup complete-group-count)
166 (ash (char-code (the character
167 (char string isource))) 16))
169 (ash (char-code (the character
170 (char string (1+ isource)))) 8))))
175 (ash (char-code (the character (char string isource))) 16))
178 (declare (fixnum igroup isource))
183 (ash (char-code (the character
184 (char string isource))) 16))
186 (ash (char-code (the character (char string (1+ isource)))) 8))
188 (char-code (the character (char string (+ 2 isource)))))))
191 (defun integer-to-base64 (input &key (uri nil) (columns 0) (stream nil))
193 (integer-to-base64-stream input stream :uri uri :columns columns)
194 (integer-to-base64-string input :uri uri :columns columns)))
196 (defun integer-to-base64-string (input &key (uri nil) (columns 0))
197 "Encode an integer to base64 format."
198 (declare (integer input)
200 (optimize (speed 3)))
201 (let ((pad (if uri *uri-pad-char* *pad-char*))
202 (encode-table (if uri *uri-encode-table* *encode-table*)))
203 (declare (simple-string encode-table)
205 (let* ((input-bits (integer-length input))
206 (byte-bits (round-next-multiple input-bits 8))
207 (padded-bits (round-next-multiple byte-bits 6))
208 (remainder-padding (mod padded-bits 24))
209 (padding-bits (if (zerop remainder-padding)
211 (- 24 remainder-padding)))
212 (padding-chars (/ padding-bits 6))
213 (padded-length (/ (+ padded-bits padding-bits) 6))
214 (last-line-len (if (plusp columns)
215 (- padded-length (* columns
217 padded-length columns)))
219 (num-lines (if (plusp columns)
220 (truncate (+ padded-length (1- columns)) columns)
222 (num-breaks (if (plusp num-lines)
225 (strlen (+ padded-length num-breaks))
226 (last-char (1- strlen))
227 (str (make-string strlen))
228 (col (if (zerop last-line-len)
230 (1- last-line-len))))
231 (declare (fixnum padded-length num-lines col last-char
232 padding-chars last-line-len))
233 (unless (plusp columns)
234 (setq col -1)) ;; set to flag to optimize in loop
236 (dotimes (i padding-chars)
238 (setf (schar str (the fixnum (- last-char i))) pad))
240 (do* ((strpos (- last-char padding-chars) (1- strpos))
241 (int (ash input (/ padding-bits 3))))
244 (declare (fixnum strpos) (integer int))
247 (setf (schar str strpos) #\Newline)
250 (setf (schar str strpos)
251 (schar encode-table (the fixnum (logand int #x3f))))
252 (setq int (ash int -6))
255 (defun integer-to-base64-stream (input stream &key (uri nil) (columns 0))
256 "Encode an integer to base64 format."
257 (declare (integer input)
259 (optimize (speed 3)))
260 (let ((pad (if uri *uri-pad-char* *pad-char*))
261 (encode-table (if uri *uri-encode-table* *encode-table*)))
262 (declare (simple-string encode-table)
264 (let* ((input-bits (integer-length input))
265 (byte-bits (round-next-multiple input-bits 8))
266 (padded-bits (round-next-multiple byte-bits 6))
267 (remainder-padding (mod padded-bits 24))
268 (padding-bits (if (zerop remainder-padding)
270 (- 24 remainder-padding)))
271 (padding-chars (/ padding-bits 6))
272 (padded-length (/ (+ padded-bits padding-bits) 6))
273 (strlen padded-length)
274 (nonpad-chars (- strlen padding-chars))
275 (last-nonpad-char (1- nonpad-chars))
276 (str (make-string strlen)))
277 (declare (fixnum padded-length last-nonpad-char))
278 (do* ((strpos 0 (1+ strpos))
279 (int (ash input (/ padding-bits 3)) (ash int -6))
280 (6bit-value (logand int #x3f) (logand int #x3f)))
281 ((= strpos nonpad-chars)
283 (declare (fixnum col))
284 (dotimes (i nonpad-chars)
286 (write-char (schar str i) stream)
287 (when (plusp columns)
289 (when (= col columns)
290 (write-char #\Newline stream)
292 (dotimes (ipad padding-chars)
293 (declare (fixnum ipad))
294 (write-char pad stream)
295 (when (plusp columns)
297 (when (= col columns)
298 (write-char #\Newline stream)
301 (declare (fixnum 6bit-value strpos)
303 (setf (schar str (- last-nonpad-char strpos))
304 (schar encode-table 6bit-value))
309 (defun base64-to-string (string &key (uri nil))
310 "Decode a base64 string to a string array."
311 (declare (string string)
312 (optimize (speed 3)))
313 (let ((pad (if uri *uri-pad-char* *pad-char*))
314 (decode-table (if uri *uri-decode-table* *decode-table*)))
315 (declare (type decode-table decode-table)
317 (let ((result (make-string (* 3 (truncate (length string) 4))))
319 (declare (simple-string result)
322 for char of-type character across string
323 for svalue of-type fixnum = (aref decode-table (the fixnum (char-code char)))
324 with bitstore of-type fixnum = 0
325 with bitcount of-type fixnum = 0
329 (setf bitstore (logior
330 (the fixnum (ash bitstore 6))
333 (when (>= bitcount 8)
335 (setf (char result ridx)
336 (code-char (the fixnum
340 (the fixnum (- bitcount))))
343 (setf bitstore (the fixnum (logand bitstore #xFF)))))
345 ;; Could add checks to make sure padding is correct
346 ;; Currently, padding is ignored
352 (warn "Bad character ~W in base64 decode" char))
354 (subseq result 0 ridx))))
357 (defun base64-to-integer (string &key (uri nil))
358 "Decodes a base64 string to an integer"
359 (declare (string string)
360 (optimize (speed 3)))
361 (let ((pad (if uri *uri-pad-char* *pad-char*))
362 (decode-table (if uri *uri-decode-table* *decode-table*)))
363 (declare (type decode-table decode-table)
366 (declare (integer value))
368 for char of-type character across string
369 for svalue of-type fixnum =
370 (aref decode-table (the fixnum (char-code char)))
374 (setq value (+ svalue (ash value 6))))
376 (setq value (ash value -2)))
381 (warn "Bad character ~W in base64 decode" char))))