summaryrefslogtreecommitdiff
path: root/base64.scm
blob: 5720f18da0dbbe53c51cdf65c633cc51ce442746 (plain)
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
;; -*- mode: scheme; coding: utf-8 -*-
;; Copyright © 2009, 2010, 2012, 2013 Göran Weinholt <goran@weinholt.se>

;; Permission is hereby granted, free of charge, to any person obtaining a
;; copy of this software and associated documentation files (the "Software"),
;; to deal in the Software without restriction, including without limitation
;; the rights to use, copy, modify, merge, publish, distribute, sublicense,
;; and/or sell copies of the Software, and to permit persons to whom the
;; Software is furnished to do so, subject to the following conditions:

;; The above copyright notice and this permission notice shall be included in
;; all copies or substantial portions of the Software.

;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
;; THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
;; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
;; DEALINGS IN THE SOFTWARE.
#!r6rs

;; RFC 4648 Base-N Encodings

(library (base64)
  (export base64-encode
          base64-decode
          base64-alphabet
          base64url-alphabet
          get-delimited-base64
          put-delimited-base64)
  (import (rnrs)
          (only (srfi :13 strings)
                string-index
                string-prefix? string-suffix?
                string-concatenate string-trim-both))

  (define base64-alphabet
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")

  (define base64url-alphabet
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_")

  (define base64-encode
    (case-lambda
      ;; Simple interface. Returns a string containing the canonical
      ;; base64 representation of the given bytevector.
      ((bv)
       (base64-encode bv 0 (bytevector-length bv) #f #f base64-alphabet #f))
      ((bv start)
       (base64-encode bv start (bytevector-length bv) #f #f base64-alphabet #f))
      ((bv start end)
       (base64-encode bv start end #f #f base64-alphabet #f))
      ((bv start end line-length)
       (base64-encode bv start end line-length #f base64-alphabet #f))
      ((bv start end line-length no-padding)
       (base64-encode bv start end line-length no-padding base64-alphabet #f))
      ((bv start end line-length no-padding alphabet)
       (base64-encode bv start end line-length no-padding alphabet #f))
      ;; Base64 encodes the bytes [start,end[ in the given bytevector.
      ;; Lines are limited to line-length characters (unless #f),
      ;; which must be a multiple of four. To omit the padding
      ;; characters (#\=) set no-padding to a true value. If port is
      ;; #f, returns a string.
      ((bv start end line-length no-padding alphabet port)
       (assert (or (not line-length) (zero? (mod line-length 4))))
       (let-values (((p extract) (if port
                                     (values port (lambda () (values)))
                                     (open-string-output-port))))
         (letrec ((put (if line-length
                           (let ((chars 0))
                             (lambda (p c)
                               (when (fx=? chars line-length)
                                 (set! chars 0)
                                 (put-char p #\linefeed))
                               (set! chars (fx+ chars 1))
                               (put-char p c)))
                           put-char)))
           (let lp ((i start))
             (cond ((= i end))
                   ((<= (+ i 3) end)
                    (let ((x (bytevector-uint-ref bv i (endianness big) 3)))
                      (put p (string-ref alphabet (fxbit-field x 18 24)))
                      (put p (string-ref alphabet (fxbit-field x 12 18)))
                      (put p (string-ref alphabet (fxbit-field x 6 12)))
                      (put p (string-ref alphabet (fxbit-field x 0 6)))
                      (lp (+ i 3))))
                   ((<= (+ i 2) end)
                    (let ((x (fxarithmetic-shift-left (bytevector-u16-ref bv i (endianness big)) 8)))
                      (put p (string-ref alphabet (fxbit-field x 18 24)))
                      (put p (string-ref alphabet (fxbit-field x 12 18)))
                      (put p (string-ref alphabet (fxbit-field x 6 12)))
                      (unless no-padding
                        (put p #\=))))
                   (else
                    (let ((x (fxarithmetic-shift-left (bytevector-u8-ref bv i) 16)))
                      (put p (string-ref alphabet (fxbit-field x 18 24)))
                      (put p (string-ref alphabet (fxbit-field x 12 18)))
                      (unless no-padding
                        (put p #\=)
                        (put p #\=)))))))
         (extract)))))

  ;; Create a lookup table for the alphabet and remember the latest table.
  (define get-decode-table
    (let ((ascii-table #f)
          (extra-table '())     ;in the unlikely case of unicode chars
          (table-alphabet #f))
      (lambda (alphabet)
        (unless (eq? alphabet table-alphabet)
          ;; Rebuild the table.
          (do ((ascii (make-vector 128 #f))
               (extra '())
               (i 0 (+ i 1)))
              ((= i (string-length alphabet))
               (set! ascii-table ascii)
               (set! extra-table extra))
            (let ((c (char->integer (string-ref alphabet i))))
              (if (fx<=? c 127)
                  (vector-set! ascii c i)
                  (set! extra (cons (cons c i) extra)))))
          (set! table-alphabet alphabet))
        (values ascii-table extra-table))))

  ;; Decodes a correctly padded base64 string, optionally ignoring
  ;; non-alphabet characters.
  (define base64-decode
    (case-lambda
      ((str)
       (base64-decode str base64-alphabet #f))
      ((str alphabet)
       (base64-decode str alphabet #f))
      ((str alphabet port)
       (base64-decode str alphabet port #t))
      ((str alphabet port strict?)
       (define (pad? c) (eqv? c (char->integer #\=)))
       (let-values (((p extract) (if port
                                     (values port (lambda () (values)))
                                     (open-bytevector-output-port)))
                    ((ascii extra) (get-decode-table alphabet)))
         (define-syntax lookup
           (syntax-rules ()
             ((_ c) (or (and (fx<=? c 127) (vector-ref ascii c))
                        (cond ((assv c extra) => cdr)
                              (else #f))))))
         (let* ((len (if strict?
                         (string-length str)
                         (let lp ((i (fx- (string-length str) 1)))
                           ;; Skip trailing invalid chars.
                           (cond ((fxzero? i) 0)
                                 ((let ((c (char->integer (string-ref str i))))
                                    (or (lookup c) (pad? c)))
                                  (fx+ i 1))
                                 (else (lp (fx- i 1))))))))
           (let lp ((i 0))
             (cond
               ((fx=? i len)
                (extract))
               ((fx<=? i (fx- len 4))
                (let lp* ((c1 (char->integer (string-ref str i)))
                          (c2 (char->integer (string-ref str (fx+ i 1))))
                          (c3 (char->integer (string-ref str (fx+ i 2))))
                          (c4 (char->integer (string-ref str (fx+ i 3))))
                          (i i))
                  (let ((i1 (lookup c1)) (i2 (lookup c2))
                        (i3 (lookup c3)) (i4 (lookup c4)))
                    (cond
                      ((and i1 i2 i3 i4)
                       ;; All characters present and accounted for.
                       ;; The most common case.
                       (let ((x (fxior (fxarithmetic-shift-left i1 18)
                                       (fxarithmetic-shift-left i2 12)
                                       (fxarithmetic-shift-left i3 6)
                                       i4)))
                         (put-u8 p (fxbit-field x 16 24))
                         (put-u8 p (fxbit-field x 8 16))
                         (put-u8 p (fxbit-field x 0 8))
                         (lp (fx+ i 4))))
                      ((and i1 i2 i3 (pad? c4) (= i (- len 4)))
                       ;; One padding character at the end of the input.
                       (let ((x (fxior (fxarithmetic-shift-left i1 18)
                                       (fxarithmetic-shift-left i2 12)
                                       (fxarithmetic-shift-left i3 6))))
                         (put-u8 p (fxbit-field x 16 24))
                         (put-u8 p (fxbit-field x 8 16))
                         (lp (fx+ i 4))))
                      ((and i1 i2 (pad? c3) (pad? c4) (= i (- len 4)))
                       ;; Two padding characters.
                       (let ((x (fxior (fxarithmetic-shift-left i1 18)
                                       (fxarithmetic-shift-left i2 12))))
                         (put-u8 p (fxbit-field x 16 24))
                         (lp (fx+ i 4))))
                      ((not strict?)
                       ;; Non-alphabet characters.
                       (let lp ((i i) (c* '()) (n 4))
                         (cond ((fxzero? n)
                                ;; Found four valid characters.
                                (lp* (cadddr c*) (caddr c*) (cadr c*) (car c*)
                                     (fx- i 4)))
                               ((fx=? i len)
                                (error 'base64-decode
                                       "Invalid input in non-strict mode."
                                       i c*))
                               (else
                                ;; Gather alphabetic (or valid
                                ;; padding) characters.
                                (let ((c (char->integer (string-ref str i))))
                                  (cond ((or (lookup c)
                                             (and (pad? c)
                                                  (fx<=? n 2)
                                                  (fx=? i (fx- len n))))
                                         (lp (fx+ i 1) (cons c c*) (fx- n 1)))
                                        (else
                                         (lp (fx+ i 1) c* n))))))))
                      (else
                       (error 'base64-decode
                              "Invalid input in strict mode."
                              c1 c2 c3 c4))))))
               (else
                (error 'base64-decode
                       "The input is too short, it may be missing padding."
                       i)))))))))

  (define (get-line-comp f port)
    (if (port-eof? port)
        (eof-object)
        (f (get-line port))))

  ;; Reads the common -----BEGIN/END type----- delimited format from
  ;; the given port. Returns two values: a string with the type and a
  ;; bytevector containing the base64 decoded data. The second value
  ;; is the eof object if there is an eof before the BEGIN delimiter.
  (define get-delimited-base64
    (case-lambda
      ((port)
       (get-delimited-base64 port #t))
      ((port strict)
       (define (get-first-data-line port)
         ;; Some MIME data has header fields in the same format as mail
         ;; or http. These are ignored.
         (let ((line (get-line-comp string-trim-both port)))
           (cond ((eof-object? line) line)
                 ((string-index line #\:)
                  (let lp ()               ;read until empty line
                    (let ((line (get-line-comp string-trim-both port)))
                      (if (string=? line "")
                          (get-line-comp string-trim-both port)
                          (lp)))))
                 (else line))))
       (let ((line (get-line-comp string-trim-both port)))
         (cond ((eof-object? line)
                (values "" (eof-object)))
               ((string=? line "")
                (get-delimited-base64 port))
               ((and (string-prefix? "-----BEGIN " line)
                     (string-suffix? "-----" line))
                (let* ((type (substring line 11 (- (string-length line) 5)))
                       (endline (string-append "-----END " type "-----")))
                  (let-values (((outp extract) (open-bytevector-output-port)))
                    (let lp ((line (get-first-data-line port)))
                      (cond ((eof-object? line)
                             (error 'get-delimited-base64
                                    "unexpected end of file"))
                            ((string-prefix? "-" line)
                             (unless (string=? line endline)
                               (error 'get-delimited-base64
                                      "bad end delimiter" type line))
                             (values type (extract)))
                            (else
                             (unless (and (= (string-length line) 5)
                                          (string-prefix? "=" line)) ;Skip Radix-64 checksum
                               (base64-decode line base64-alphabet outp))
                             (lp (get-line-comp string-trim-both port))))))))
               (else ;skip garbage (like in openssl x509 -in foo -text output).
                (get-delimited-base64 port)))))))

  (define put-delimited-base64
    (case-lambda
      ((port type bv line-length)
       (display (string-append "-----BEGIN " type "-----\n") port)
       (base64-encode bv 0 (bytevector-length bv)
                      line-length #f base64-alphabet port)
       (display (string-append "\n-----END " type "-----\n") port))
      ((port type bv)
       (put-delimited-base64 port type bv 76)))))