1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
|
;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
;;;; *************************************************************************
;;;; FILE IDENTIFICATION
;;;;
;;;; Name: encode.lisp
;;;; Purpose: cl-base64 encoding routines
;;;; Programmer: Kevin M. Rosenberg
;;;; Date Started: Dec 2002
;;;;
;;;; $Id$
;;;;
;;;; This file implements the Base64 transfer encoding algorithm as
;;;; defined in RFC 1521 by Borensten & Freed, September 1993.
;;;; See: http://www.ietf.org/rfc/rfc1521.txt
;;;;
;;;; Based on initial public domain code by Juri Pakaste <juri@iki.fi>
;;;;
;;;; Copyright 2002-2003 Kevin M. Rosenberg
;;;; Permission to use with BSD-style license included in the COPYING file
;;;; *************************************************************************
(in-package #:cl-base64)
(define-condition base64-error (error)
((input
:initarg :input
:reader base64-error-input)
(position
:initarg :position
:reader base64-error-position
:type unsigned-byte)))
(define-condition bad-base64-character (base64-error)
((code :initarg :code :reader bad-base64-character-code))
(:report (lambda (condition stream)
(format stream "Bad character ~S at index ~D of ~S"
(code-char (bad-base64-character-code condition))
(base64-error-position condition)
(base64-error-input condition)))))
(define-condition incomplete-base64-data (base64-error)
()
(:report (lambda (condition stream)
(format stream "Unexpected end of Base64 data at index ~D of ~S"
(base64-error-position condition)
(base64-error-input condition)))))
(deftype array-index (&optional (length array-dimension-limit))
`(integer 0 (,length)))
(deftype array-length (&optional (length array-dimension-limit))
`(integer 0 ,length))
(deftype character-code ()
`(integer 0 (,char-code-limit)))
(defmacro etypecase/unroll ((var &rest types) &body body)
#+sbcl `(etypecase ,var
,@(loop for type in types
collect `(,type ,@body)))
#-sbcl `(locally
(declare (type (or ,@types) ,var))
,@body))
(defmacro let/typed ((&rest vars) &body body)
`(let ,(loop for (var value) in vars
collect (list var value))
(declare ,@(loop for (var nil type) in vars
when type
collect (list 'type type var)))
,@body))
(defmacro define-base64-decoder (hose sink)
`(defun ,(intern (format nil "~A-~A-~A-~A" '#:base64 hose '#:to sink))
(input &key (table +decode-table+)
(uri nil)
,@(when (eq sink :stream) `(stream))
(whitespace :ignore))
,(format nil "~
Decode Base64 ~(~A~) to ~(~A~).
TABLE is the decode table to use. Two decode tables are provided:
+DECODE-TABLE+ (used by default) and +URI-DECODE-TABLE+. See
MAKE-DECODE-TABLE.
For backwards-compatibility the URI parameter is supported. If it is
true, then +URI-DECODE-TABLE+ is used, and the value for TABLE
parameter is ignored.
WHITESPACE can be one of:
:ignore - Whitespace characters are ignored (default).
:signal - Signal a BAD-BASE64-CHARACTER condition using SIGNAL.
:error - Signal a BAD-BASE64-CHARACTER condition using ERROR."
hose sink)
(declare (optimize (speed 3) (safety 1))
(type decode-table table)
(type ,(ecase hose
(:stream 'stream)
(:string 'string))
input))
(let/typed ((decode-table (if uri +uri-decode-table+ table)
decode-table)
,@(ecase sink
(:stream)
(:usb8-array
(ecase hose
(:stream
`((result (make-array 1024
:element-type '(unsigned-byte 8)
:adjustable t
:fill-pointer 0)
(array (unsigned-byte 8) (*)))))
(:string
`((result (make-array (* 3 (ceiling (length input) 4))
:element-type '(unsigned-byte 8))
(simple-array (unsigned-byte 8) (*)))
(rpos 0 array-index)))))
(:string
(case hose
(:stream
`((result (make-array 1024
:element-type 'character
:adjustable t
:fill-pointer 0)
(array character (*)))))
(:string
`((result (make-array (* 3 (ceiling (length input) 4))
:element-type 'character)
(simple-array character (*)))
(rpos 0 array-index)))))
(:integer
`((result 0 unsigned-byte)))))
(flet ((bad-char (pos code &optional (action :error))
(let ((args (list 'bad-base64-character
:input input
:position pos
:code code)))
(ecase action
(:error
(apply #'error args))
(:cerror
(apply #'cerror "Ignore the error and continue." args))
(:signal
(apply #'signal args)))))
(incomplete-input (pos)
(error 'incomplete-base64-data :input input :position pos)))
,(let ((body
`(let/typed ((ipos 0 array-index)
(bitstore 0 (unsigned-byte 24))
(bitcount 0 (integer 0 14))
(svalue -1 (signed-byte 8))
(padchar 0 (integer 0 3))
(code 0 fixnum))
(loop
,@(ecase hose
(:string
`((if (< ipos length)
(setq code (char-code (aref input ipos)))
(return))))
(:stream
`((let ((char (read-char input nil nil)))
(if char
(setq code (char-code char))
(return))))))
(cond
((or (< 127 code)
(= -1 (setq svalue (aref decode-table code))))
(bad-char ipos code))
((= -2 svalue)
(cond ((<= (incf padchar) 2)
(unless (<= 2 bitcount)
(bad-char ipos code))
(decf bitcount 2))
(t
(bad-char ipos code))))
((= -3 svalue)
(ecase whitespace
(:ignore
;; Do nothing.
)
(:error
(bad-char ipos code :error))
(:signal
(bad-char ipos code :signal))))
((not (zerop padchar))
(bad-char ipos code))
(t
(setf bitstore (logior (the (unsigned-byte 24)
(ash bitstore 6))
svalue))
(incf bitcount 6)
(when (>= bitcount 8)
(decf bitcount 8)
(let ((byte (logand (the (unsigned-byte 24)
(ash bitstore (- bitcount)))
#xFF)))
(declare (type (unsigned-byte 8) byte))
,@(ecase sink
(:usb8-array
(ecase hose
(:string
`((setf (aref result rpos) byte)
(incf rpos)))
(:stream
`((vector-push-extend byte result)))))
(:string
(ecase hose
(:string
`((setf (schar result rpos)
(code-char byte))
(incf rpos)))
(:stream
`((vector-push-extend (code-char byte)
result)))))
(:integer
`((setq result
(logior (ash result 8) byte))))
(:stream
'((write-char (code-char byte) stream)))))
(setf bitstore (logand bitstore #xFF)))))
(incf ipos))
(unless (zerop bitcount)
(incomplete-input ipos))
,(ecase sink
((:string :usb8-array)
(ecase hose
(:string
`(if (= rpos (length result))
result
(subseq result 0 rpos)))
(:stream
`(copy-seq result))))
(:integer
'result)
(:stream
'stream)))))
(ecase hose
(:string
`(let ((length (length input)))
(declare (type array-length length))
(etypecase/unroll (input simple-base-string
simple-string
string)
,body)))
(:stream
body)))))))
(define-base64-decoder :string :usb8-array)
(define-base64-decoder :string :string)
(define-base64-decoder :string :integer)
(define-base64-decoder :string :stream)
(define-base64-decoder :stream :usb8-array)
(define-base64-decoder :stream :string)
(define-base64-decoder :stream :integer)
(define-base64-decoder :stream :stream)
;; input-mode can be :string or :stream
;; input-format can be :character or :usb8
|