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.4 2003/01/14 11:43:10 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.4 2003/01/14 11:43:10 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)
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 (1+ igroup))
133 (isource 0 (+ 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 (1+ isource)))))
154 '(the fixnum (aref input (1+ isource)))))
163 '(char-code (the character (char input isource))))
165 '(the fixnum (aref input isource))))
173 (declare (fixnum igroup isource))
182 '(char-code (the character (char input isource))))
184 '(aref input isource))))
191 '(char-code (the character (char input (1+ isource)))))
193 '(aref input (1+ isource)))))
198 '(char-code (the character (char input (+ 2 isource)))))
200 '(aref input (+ 2 isource))))
204 (def-*-to-base64-* :string :string)
205 (def-*-to-base64-* :string :stream)
206 (def-*-to-base64-* :usb8-array :string)
207 (def-*-to-base64-* :usb8-array :stream)
210 (defun integer-to-base64-string (input &key (uri nil) (columns 0))
211 "Encode an integer to base64 format."
212 (declare (integer input)
214 (optimize (speed 3)))
215 (let ((pad (if uri *uri-pad-char* *pad-char*))
216 (encode-table (if uri *uri-encode-table* *encode-table*)))
217 (declare (simple-string encode-table)
219 (let* ((input-bits (integer-length input))
220 (byte-bits (round-next-multiple input-bits 8))
221 (padded-bits (round-next-multiple byte-bits 6))
222 (remainder-padding (mod padded-bits 24))
223 (padding-bits (if (zerop remainder-padding)
225 (- 24 remainder-padding)))
226 (padding-chars (/ padding-bits 6))
227 (padded-length (/ (+ padded-bits padding-bits) 6))
228 (last-line-len (if (plusp columns)
229 (- padded-length (* columns
231 padded-length columns)))
233 (num-lines (if (plusp columns)
234 (truncate (+ padded-length (1- columns)) columns)
236 (num-breaks (if (plusp num-lines)
239 (strlen (+ padded-length num-breaks))
240 (last-char (1- strlen))
241 (str (make-string strlen))
242 (col (if (zerop last-line-len)
245 (declare (fixnum padded-length num-lines col last-char
246 padding-chars last-line-len))
247 (unless (plusp columns)
248 (setq col -1)) ;; set to flag to optimize in loop
250 (dotimes (i padding-chars)
252 (setf (schar str (the fixnum (- last-char i))) pad))
254 (do* ((strpos (- last-char padding-chars) (1- strpos))
255 (int (ash input (/ padding-bits 3))))
258 (declare (fixnum strpos) (integer int))
261 (setf (schar str strpos) #\Newline)
264 (setf (schar str strpos)
265 (schar encode-table (the fixnum (logand int #x3f))))
266 (setq int (ash int -6))
269 (defun integer-to-base64-stream (input stream &key (uri nil) (columns 0))
270 "Encode an integer to base64 format."
271 (declare (integer input)
273 (optimize (speed 3)))
274 (let ((pad (if uri *uri-pad-char* *pad-char*))
275 (encode-table (if uri *uri-encode-table* *encode-table*)))
276 (declare (simple-string encode-table)
278 (let* ((input-bits (integer-length input))
279 (byte-bits (round-next-multiple input-bits 8))
280 (padded-bits (round-next-multiple byte-bits 6))
281 (remainder-padding (mod padded-bits 24))
282 (padding-bits (if (zerop remainder-padding)
284 (- 24 remainder-padding)))
285 (padding-chars (/ padding-bits 6))
286 (padded-length (/ (+ padded-bits padding-bits) 6))
287 (strlen padded-length)
288 (nonpad-chars (- strlen padding-chars))
289 (last-nonpad-char (1- nonpad-chars))
290 (str (make-string strlen)))
291 (declare (fixnum padded-length last-nonpad-char))
292 (do* ((strpos 0 (1+ strpos))
293 (int (ash input (/ padding-bits 3)) (ash int -6))
294 (6bit-value (logand int #x3f) (logand int #x3f)))
295 ((= strpos nonpad-chars)
297 (declare (fixnum col))
298 (dotimes (i nonpad-chars)
300 (write-char (schar str i) stream)
301 (when (plusp columns)
303 (when (= col columns)
304 (write-char #\Newline stream)
306 (dotimes (ipad padding-chars)
307 (declare (fixnum ipad))
308 (write-char pad stream)
309 (when (plusp columns)
311 (when (= col columns)
312 (write-char #\Newline stream)
315 (declare (fixnum 6bit-value strpos)
317 (setf (schar str (- last-nonpad-char strpos))
318 (schar encode-table 6bit-value))