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: encode.lisp,v 1.5 2003/01/14 11:59:44 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 ;;;; Extended by Kevin M. Rosenberg <kevin@rosenberg.net>:
26 ;;;; - numerous speed optimizations
27 ;;;; - conversion to and from integers
28 ;;;; - Renamed functions now that supporting integer conversions
29 ;;;; - URI-compatible encoding using :uri key
31 ;;;; $Id: encode.lisp,v 1.5 2003/01/14 11:59:44 kevin Exp $
33 (in-package #:cl-base64)
36 (defun round-next-multiple (x n)
37 "Round x up to the next highest multiple of n."
40 (let ((remainder (mod x n)))
41 (declare (fixnum remainder))
44 (the fixnum (+ x (the fixnum (- n remainder)))))))
46 (defmacro def-*-to-base64-* (input-type output-type)
47 `(defun ,(intern (concatenate 'string (symbol-name input-type)
48 (symbol-name :-to-base64-)
49 (symbol-name output-type)))
51 ,@(when (eq output-type :stream)
53 &key (uri nil) (columns 0))
54 "Encode a string array to base64. If columns is > 0, designates
55 maximum number of columns in a line and the string will be terminated
57 (declare ,@(case input-type
61 '((type (array (unsigned-byte 8) (*)) input))))
64 (let ((pad (if uri *uri-pad-char* *pad-char*))
65 (encode-table (if uri *uri-encode-table* *encode-table*)))
66 (declare (simple-string encode-table)
68 (let* ((string-length (length input))
69 (complete-group-count (truncate string-length 3))
70 (remainder (nth-value 1 (truncate string-length 3)))
71 (padded-length (* 4 (truncate (+ string-length 2) 3)))
72 ,@(when (eq output-type :string)
73 '((num-lines (if (plusp columns)
74 (truncate (+ padded-length (1- columns)) columns)
76 (num-breaks (if (plusp num-lines)
79 (strlen (+ padded-length num-breaks))
80 (result (make-string strlen))
82 (col (if (plusp columns)
84 (the fixnum (1+ padded-length)))))
85 (declare (fixnum string-length padded-length col
86 ,@(when (eq output-type :string)
88 ,@(when (eq output-type :string)
89 '((simple-string result))))
90 (labels ((output-char (ch)
95 '((write-char #\Newline output)))
97 '((setf (schar result ioutput) #\Newline)
103 '((write-char ch output)))
105 '((setf (schar result ioutput) ch)
107 (output-group (svalue chars)
108 (declare (fixnum svalue chars))
113 (the fixnum (ash svalue -18))))))
118 (the fixnum (ash svalue -12))))))
124 (the fixnum (ash svalue -6))))))
130 (logand #x3f svalue))))
132 (do ((igroup 0 (the fixnum (1+ igroup)))
133 (isource 0 (the fixnum (+ isource 3))))
134 ((= igroup complete-group-count)
144 '(char-code (the character (char input isource))))
146 '(the fixnum (aref input isource))))
152 '(char-code (the character (char input
153 (the fixnum (1+ isource))))))
155 '(the fixnum (aref input (the fixnum
165 '(char-code (the character (char input isource))))
167 '(the fixnum (aref input isource))))
175 (declare (fixnum igroup isource))
184 '(char-code (the character (char input isource))))
186 '(aref input isource))))
193 '(char-code (the character (char input
194 (the fixnum (1+ isource))))))
196 '(aref input (1+ isource)))))
201 '(char-code (the character (char input
202 (the fixnum (+ 2 isource))))))
204 '(aref input (+ 2 isource))))
208 (def-*-to-base64-* :string :string)
209 (def-*-to-base64-* :string :stream)
210 (def-*-to-base64-* :usb8-array :string)
211 (def-*-to-base64-* :usb8-array :stream)
214 (defun integer-to-base64-string (input &key (uri nil) (columns 0))
215 "Encode an integer to base64 format."
216 (declare (integer input)
218 (optimize (speed 3)))
219 (let ((pad (if uri *uri-pad-char* *pad-char*))
220 (encode-table (if uri *uri-encode-table* *encode-table*)))
221 (declare (simple-string encode-table)
223 (let* ((input-bits (integer-length input))
224 (byte-bits (round-next-multiple input-bits 8))
225 (padded-bits (round-next-multiple byte-bits 6))
226 (remainder-padding (mod padded-bits 24))
227 (padding-bits (if (zerop remainder-padding)
229 (- 24 remainder-padding)))
230 (padding-chars (/ padding-bits 6))
231 (padded-length (/ (+ padded-bits padding-bits) 6))
232 (last-line-len (if (plusp columns)
233 (- padded-length (* columns
235 padded-length columns)))
237 (num-lines (if (plusp columns)
238 (truncate (+ padded-length (1- columns)) columns)
240 (num-breaks (if (plusp num-lines)
243 (strlen (+ padded-length num-breaks))
244 (last-char (1- strlen))
245 (str (make-string strlen))
246 (col (if (zerop last-line-len)
249 (declare (fixnum padded-length num-lines col last-char
250 padding-chars last-line-len))
251 (unless (plusp columns)
252 (setq col -1)) ;; set to flag to optimize in loop
254 (dotimes (i padding-chars)
256 (setf (schar str (the fixnum (- last-char i))) pad))
258 (do* ((strpos (- last-char padding-chars) (1- strpos))
259 (int (ash input (/ padding-bits 3))))
262 (declare (fixnum strpos) (integer int))
265 (setf (schar str strpos) #\Newline)
268 (setf (schar str strpos)
269 (schar encode-table (the fixnum (logand int #x3f))))
270 (setq int (ash int -6))
273 (defun integer-to-base64-stream (input stream &key (uri nil) (columns 0))
274 "Encode an integer to base64 format."
275 (declare (integer input)
277 (optimize (speed 3)))
278 (let ((pad (if uri *uri-pad-char* *pad-char*))
279 (encode-table (if uri *uri-encode-table* *encode-table*)))
280 (declare (simple-string encode-table)
282 (let* ((input-bits (integer-length input))
283 (byte-bits (round-next-multiple input-bits 8))
284 (padded-bits (round-next-multiple byte-bits 6))
285 (remainder-padding (mod padded-bits 24))
286 (padding-bits (if (zerop remainder-padding)
288 (- 24 remainder-padding)))
289 (padding-chars (/ padding-bits 6))
290 (padded-length (/ (+ padded-bits padding-bits) 6))
291 (strlen padded-length)
292 (nonpad-chars (- strlen padding-chars))
293 (last-nonpad-char (1- nonpad-chars))
294 (str (make-string strlen)))
295 (declare (fixnum padded-length last-nonpad-char))
296 (do* ((strpos 0 (the fixnum (1+ strpos)))
297 (int (ash input (/ padding-bits 3)) (ash int -6))
298 (6bit-value (the fixnum (logand int #x3f))
299 (the fixnum (logand int #x3f))))
300 ((= strpos nonpad-chars)
302 (declare (fixnum col))
303 (dotimes (i nonpad-chars)
305 (write-char (schar str i) stream)
306 (when (plusp columns)
308 (when (= col columns)
309 (write-char #\Newline stream)
311 (dotimes (ipad padding-chars)
312 (declare (fixnum ipad))
313 (write-char pad stream)
314 (when (plusp columns)
316 (when (= col columns)
317 (write-char #\Newline stream)
320 (declare (fixnum 6bit-value strpos)
322 (setf (schar str (- last-nonpad-char strpos))
323 (schar encode-table 6bit-value))