1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
6 ;;;; Purpose: cl-base64 encoding routines
7 ;;;; Programmer: Kevin M. Rosenberg
8 ;;;; Date Started: Dec 2002
10 ;;;; $Id: decode.lisp,v 1.2 2003/01/12 22:32:40 kevin Exp $
12 ;;;; This file implements the Base64 transfer encoding algorithm as
13 ;;;; defined in RFC 1521 by Borensten & Freed, September 1993.
14 ;;;; See: http://www.ietf.org/rfc/rfc1521.txt
16 ;;;; Based on initial public domain code by Juri Pakaste <juri@iki.fi>
18 ;;;; Copyright 2002-2003 Kevin M. Rosenberg
19 ;;;; Permission to use with BSD-style license included in the COPYING file
20 ;;;; *************************************************************************
22 (declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0)))
24 (in-package #:cl-base64)
26 (declaim (inline whitespace-p))
27 (defun whitespace-p (c)
28 "Returns T for a whitespace character."
29 (or (char= c #\Newline) (char= c #\Linefeed)
30 (char= c #\Return) (char= c #\Space)
37 (defmacro def-base64-stream-to-* (output-type)
38 `(defun ,(intern (concatenate 'string (symbol-name :base64-stream-to-)
39 (symbol-name output-type)))
41 ,@(when (eq output-type :stream)
43 ,(concatenate 'string "Decode base64 stream to " (string-downcase
44 (symbol-name output-type)))
45 (declare (stream input)
47 (let ((pad (if uri *uri-pad-char* *pad-char*))
48 (decode-table (if uri *uri-decode-table* *decode-table*)))
49 (declare (type decode-table decode-table)
51 (let (,@(case output-type
53 '((result (make-string (* 3 (truncate (length string) 4))))))
55 '((result (make-array (* 3 (truncate (length string) 4))
56 :element-type '(unsigned-byte 8)
60 (declare ,@(case output-type
62 '((simple-string result)))
64 '((type (array fixnum (*)) result))))
68 (char (read-char stream nil #\null)
69 (read-char stream nil #\null)))
75 '(subseq result 0 ridx))))
76 (declare (fixnum bitstore bitcount)
78 (let ((svalue (aref decode-table (the fixnum (char-code char)))))
79 (declare (fixnum svalue))
82 (setf bitstore (logior
83 (the fixnum (ash bitstore 6))
88 (let ((ovalue (the fixnum
92 (the fixnum (- bitcount))))
94 (declare (fixnum ovalue))
97 '(setf (char result ridx) (code-char ovalue)))
99 '(setf (aref result ridx) ovalue))
101 '(write-char (code-char ovalue) stream)))
103 (setf bitstore (the fixnum (logand bitstore #xFF))))))
105 ;; Could add checks to make sure padding is correct
106 ;; Currently, padding is ignored
112 (warn "Bad character ~W in base64 decode" char))
115 ;;(def-base64-stream-to-* :string)
116 ;;(def-base64-stream-to-* :stream)
117 ;;(def-base64-stream-to-* :usb8-array)
119 (defmacro def-base64-string-to-* (output-type)
120 `(defun ,(intern (concatenate 'string (symbol-name :base64-string-to-)
121 (symbol-name output-type)))
122 (input &key (uri nil)
123 ,@(when (eq output-type :stream)
125 ,(concatenate 'string "Decode base64 string to " (string-downcase
126 (symbol-name output-type)))
127 (declare (string input)
128 (optimize (speed 3)))
129 (let ((pad (if uri *uri-pad-char* *pad-char*))
130 (decode-table (if uri *uri-decode-table* *decode-table*)))
131 (declare (type decode-table decode-table)
132 (type character pad))
133 (let (,@(case output-type
135 '((result (make-string (* 3 (truncate (length input) 4))))))
137 '((result (make-array (* 3 (truncate (length input) 4))
138 :element-type '(unsigned-byte 8)
142 (declare ,@(case output-type
144 '((simple-string result)))
146 '((type (array fixnum (*)) result))))
149 for char of-type character across input
150 for svalue of-type fixnum = (aref decode-table
151 (the fixnum (char-code char)))
152 with bitstore of-type fixnum = 0
153 with bitcount of-type fixnum = 0
157 (setf bitstore (logior
158 (the fixnum (ash bitstore 6))
161 (when (>= bitcount 8)
163 (let ((ovalue (the fixnum
167 (the fixnum (- bitcount))))
169 (declare (fixnum ovalue))
172 '(setf (char result ridx) (code-char ovalue)))
174 '(setf (aref result ridx) ovalue))
176 '(write-char (code-char ovalue) stream)))
178 (setf bitstore (the fixnum (logand bitstore #xFF))))))
180 ;; Could add checks to make sure padding is correct
181 ;; Currently, padding is ignored
187 (warn "Bad character ~W in base64 decode" char))
193 '(subseq result 0 ridx)))))))
195 (def-base64-string-to-* :string)
196 (def-base64-string-to-* :stream)
197 (def-base64-string-to-* :usb8-array)
199 ;; input-mode can be :string or :stream
200 ;; input-format can be :character or :usb8
202 (defun base64-string-to-integer (string &key (uri nil))
203 "Decodes a base64 string to an integer"
204 (declare (string string)
205 (optimize (speed 3)))
206 (let ((pad (if uri *uri-pad-char* *pad-char*))
207 (decode-table (if uri *uri-decode-table* *decode-table*)))
208 (declare (type decode-table decode-table)
211 (declare (integer value))
213 for char of-type character across string
214 for svalue of-type fixnum =
215 (aref decode-table (the fixnum (char-code char)))
219 (setq value (+ svalue (ash value 6))))
221 (setq value (ash value -2)))
226 (warn "Bad character ~W in base64 decode" char))))
230 (defun base64-stream-to-integer (stream &key (uri nil))
231 "Decodes a base64 string to an integer"
232 (declare (stream stream)
233 (optimize (speed 3)))
234 (let ((pad (if uri *uri-pad-char* *pad-char*))
235 (decode-table (if uri *uri-decode-table* *decode-table*)))
236 (declare (type decode-table decode-table)
239 (char (read-char stream nil #\null)
240 (read-char stream nil #\null)))
243 (declare (integer value)
245 (let ((svalue (aref decode-table (the fixnum (char-code char)))))
246 (declare (fixnum svalue))
249 (setq value (+ svalue (ash value 6))))
251 (setq value (ash value -2)))
252 ((whitespace-p char) ; ignore whitespace
255 (warn "Bad character ~W in base64 decode" char)))))))