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.3 2003/01/13 21:38:01 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.3 2003/01/13 21:38:01 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 (defun string-to-base64 (string &key (uri nil) (columns 0) (stream nil))
47 "Encode a string array to base64. If columns is > 0, designates
48 maximum number of columns in a line and the string will be terminated
50 (declare (string string)
53 (let ((pad (if uri *uri-pad-char* *pad-char*))
54 (encode-table (if uri *uri-encode-table* *encode-table*)))
55 (declare (simple-string encode-table)
57 (let* ((string-length (length string))
58 (complete-group-count (truncate string-length 3))
59 (remainder (nth-value 1 (truncate string-length 3)))
60 (padded-length (* 4 (truncate (+ string-length 2) 3)))
61 (num-lines (if (plusp columns)
62 (truncate (+ padded-length (1- columns)) columns)
64 (num-breaks (if (plusp num-lines)
69 (+ padded-length num-breaks)))
70 (result (make-string strlen))
71 (col (if (plusp columns)
75 (declare (fixnum string-length padded-length col ioutput)
76 (simple-string result))
77 (labels ((output-char (ch)
81 (write-char #\Newline stream)
83 (setf (schar result ioutput) #\Newline)
88 (write-char ch stream)
90 (setf (schar result ioutput) ch)
92 (output-group (svalue chars)
93 (declare (fixnum svalue chars))
98 (the fixnum (ash svalue -18))))))
103 (the fixnum (ash svalue -12))))))
109 (the fixnum (ash svalue -6))))))
115 (logand #x3f svalue))))
117 (do ((igroup 0 (1+ igroup))
118 (isource 0 (+ isource 3)))
119 ((= igroup complete-group-count)
127 (char-code (the character
128 (char string isource))))
132 (char-code (the character
133 (char string (1+ isource)))))
141 (char-code (the character (char string isource))))
145 (declare (fixnum igroup isource))
150 (ash (char-code (the character
151 (char string isource))) 16))
153 (ash (char-code (the character (char string (1+ isource)))) 8))
155 (char-code (the character (char string (+ 2 isource)))))))
158 (defmacro def-*-to-base64-* (input-type output-type)
159 `(defun ,(intern (concatenate 'string (symbol-name input-type)
160 (symbol-name :-to-base64-)
161 (symbol-name output-type)))
163 ,@(when (eq output-type :stream)
165 &key (uri nil) (columns 0))
166 "Encode a string array to base64. If columns is > 0, designates
167 maximum number of columns in a line and the string will be terminated
169 (declare ,@(case input-type
173 '((type (array fixnum (*)) input))))
175 (optimize (speed 3)))
176 (let ((pad (if uri *uri-pad-char* *pad-char*))
177 (encode-table (if uri *uri-encode-table* *encode-table*)))
178 (declare (simple-string encode-table)
180 (let* ((string-length (length input))
181 (complete-group-count (truncate string-length 3))
182 (remainder (nth-value 1 (truncate string-length 3)))
183 (padded-length (* 4 (truncate (+ string-length 2) 3)))
184 ,@(when (eq output-type :string)
185 '((num-lines (if (plusp columns)
186 (truncate (+ padded-length (1- columns)) columns)
188 (num-breaks (if (plusp num-lines)
191 (strlen (+ padded-length num-breaks))
192 (result (make-string strlen))
194 (col (if (plusp columns)
196 (1+ padded-length))))
197 (declare (fixnum string-length padded-length col
198 ,@(when (eq output-type :string)
200 ,@(when (eq output-type :string)
201 '((simple-string result))))
202 (labels ((output-char (ch)
207 '((write-char #\Newline output)))
209 '((setf (schar result ioutput) #\Newline)
215 '((write-char ch output)))
217 '((setf (schar result ioutput) ch)
219 (output-group (svalue chars)
220 (declare (fixnum svalue chars))
225 (the fixnum (ash svalue -18))))))
230 (the fixnum (ash svalue -12))))))
236 (the fixnum (ash svalue -6))))))
242 (logand #x3f svalue))))
244 (do ((igroup 0 (1+ igroup))
245 (isource 0 (+ isource 3)))
246 ((= igroup complete-group-count)
256 '(char-code (the character (char input isource))))
258 '(the fixnum (aref input isource))))
264 '(char-code (the character (char input (1+ isource)))))
266 '(the fixnum (aref input (1+ isource)))))
275 '(char-code (the character (char input isource))))
277 '(the fixnum (aref input isource))))
285 (declare (fixnum igroup isource))
294 '(char-code (the character (char input isource))))
296 '(aref input isource))))
303 '(char-code (the character (char input (1+ isource)))))
305 '(aref input (1+ isource)))))
310 '(char-code (the character (char input (+ 2 isource)))))
312 '(aref input (+ 2 isource))))
316 (def-*-to-base64-* :string :string)
317 (def-*-to-base64-* :string :stream)
318 (def-*-to-base64-* :usb8-array :string)
319 (def-*-to-base64-* :usb8-array :stream)
322 (defun integer-to-base64-string (input &key (uri nil) (columns 0))
323 "Encode an integer to base64 format."
324 (declare (integer input)
326 (optimize (speed 3)))
327 (let ((pad (if uri *uri-pad-char* *pad-char*))
328 (encode-table (if uri *uri-encode-table* *encode-table*)))
329 (declare (simple-string encode-table)
331 (let* ((input-bits (integer-length input))
332 (byte-bits (round-next-multiple input-bits 8))
333 (padded-bits (round-next-multiple byte-bits 6))
334 (remainder-padding (mod padded-bits 24))
335 (padding-bits (if (zerop remainder-padding)
337 (- 24 remainder-padding)))
338 (padding-chars (/ padding-bits 6))
339 (padded-length (/ (+ padded-bits padding-bits) 6))
340 (last-line-len (if (plusp columns)
341 (- padded-length (* columns
343 padded-length columns)))
345 (num-lines (if (plusp columns)
346 (truncate (+ padded-length (1- columns)) columns)
348 (num-breaks (if (plusp num-lines)
351 (strlen (+ padded-length num-breaks))
352 (last-char (1- strlen))
353 (str (make-string strlen))
354 (col (if (zerop last-line-len)
357 (declare (fixnum padded-length num-lines col last-char
358 padding-chars last-line-len))
359 (unless (plusp columns)
360 (setq col -1)) ;; set to flag to optimize in loop
362 (dotimes (i padding-chars)
364 (setf (schar str (the fixnum (- last-char i))) pad))
366 (do* ((strpos (- last-char padding-chars) (1- strpos))
367 (int (ash input (/ padding-bits 3))))
370 (declare (fixnum strpos) (integer int))
373 (setf (schar str strpos) #\Newline)
376 (setf (schar str strpos)
377 (schar encode-table (the fixnum (logand int #x3f))))
378 (setq int (ash int -6))
381 (defun integer-to-base64-stream (input stream &key (uri nil) (columns 0))
382 "Encode an integer to base64 format."
383 (declare (integer input)
385 (optimize (speed 3)))
386 (let ((pad (if uri *uri-pad-char* *pad-char*))
387 (encode-table (if uri *uri-encode-table* *encode-table*)))
388 (declare (simple-string encode-table)
390 (let* ((input-bits (integer-length input))
391 (byte-bits (round-next-multiple input-bits 8))
392 (padded-bits (round-next-multiple byte-bits 6))
393 (remainder-padding (mod padded-bits 24))
394 (padding-bits (if (zerop remainder-padding)
396 (- 24 remainder-padding)))
397 (padding-chars (/ padding-bits 6))
398 (padded-length (/ (+ padded-bits padding-bits) 6))
399 (strlen padded-length)
400 (nonpad-chars (- strlen padding-chars))
401 (last-nonpad-char (1- nonpad-chars))
402 (str (make-string strlen)))
403 (declare (fixnum padded-length last-nonpad-char))
404 (do* ((strpos 0 (1+ strpos))
405 (int (ash input (/ padding-bits 3)) (ash int -6))
406 (6bit-value (logand int #x3f) (logand int #x3f)))
407 ((= strpos nonpad-chars)
409 (declare (fixnum col))
410 (dotimes (i nonpad-chars)
412 (write-char (schar str i) stream)
413 (when (plusp columns)
415 (when (= col columns)
416 (write-char #\Newline stream)
418 (dotimes (ipad padding-chars)
419 (declare (fixnum ipad))
420 (write-char pad stream)
421 (when (plusp columns)
423 (when (= col columns)
424 (write-char #\Newline stream)
427 (declare (fixnum 6bit-value strpos)
429 (setf (schar str (- last-nonpad-char strpos))
430 (schar encode-table 6bit-value))