-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase64.sc
318 lines (284 loc) · 11.7 KB
/
base64.sc
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
;;
;; Copyright (c) 2009 Takeshi Abe. All rights reserved.
;;
;; Redistribution and use in source and binary forms, with or without
;; modification, are permitted provided that the following conditions
;; are met:
;;
;; 1. Redistributions of source code must retain the above copyright
;; notice, this list of conditions and the following disclaimer.
;;
;; 2. Redistributions in binary form must reproduce the above copyright
;; notice, this list of conditions and the following disclaimer in the
;; documentation and/or other materials provided with the distribution.
;;
;; 3. Neither the name of the authors nor the names of its contributors
;; may be used to endorse or promote products derived from this
;; software without specific prior written permission.
;;
;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
(library (base64 base64)
(export
encode
encode-url
encode-utf8
encode-utf8-url
encode-bytevector
decode
decode-url
decode-utf8
decode-utf8-url
decode-string
len-verif
&invalid-encoding
invalid-encoding?
invalid-encoding-position
&unknown-alphabet
unknown-alphabet?
unknown-alphabet-char)
(import (scheme))
(define *table*
'#(#\A #\B #\C #\D #\E #\F #\G #\H
#\I #\J #\K #\L #\M #\N #\O #\P
#\Q #\R #\S #\T #\U #\V #\W #\X
#\Y #\Z #\a #\b #\c #\d #\e #\f
#\g #\h #\i #\j #\k #\l #\m #\n
#\o #\p #\q #\r #\s #\t #\u #\v
#\w #\x #\y #\z #\0 #\1 #\2 #\3
#\4 #\5 #\6 #\7 #\8 #\9 #\+ #\/))
(define *table-url*
'#(#\A #\B #\C #\D #\E #\F #\G #\H
#\I #\J #\K #\L #\M #\N #\O #\P
#\Q #\R #\S #\T #\U #\V #\W #\X
#\Y #\Z #\a #\b #\c #\d #\e #\f
#\g #\h #\i #\j #\k #\l #\m #\n
#\o #\p #\q #\r #\s #\t #\u #\v
#\w #\x #\y #\z #\0 #\1 #\2 #\3
#\4 #\5 #\6 #\7 #\8 #\9 #\- #\_))
(define encode
(lambda (iport oport)
(define put-alphabet
(lambda (i)
(put-char oport (vector-ref *table* i))))
(let loop ((b0 (get-u8 iport)) (n 0))
(cond
((eof-object? b0) n)
(else
(put-alphabet (fxarithmetic-shift-right b0 2))
(let
((p0 (fxarithmetic-shift-left (fxbit-field b0 0 2) 4))
(b1 (get-u8 iport)))
(cond
((eof-object? b1)
(put-alphabet p0)
(put-string oport "==")
(+ n 4))
(else
(put-alphabet (fxior p0 (fxarithmetic-shift-right b1 4)))
(let
((p1 (fxarithmetic-shift-left (fxbit-field b1 0 4) 2))
(b2 (get-u8 iport)))
(cond
((eof-object? b2)
(put-alphabet p1)
(put-char oport #\=)
(+ n 4))
(else
(put-alphabet (fxior p1 (fxarithmetic-shift-right b2 6)))
(put-alphabet (fxbit-field b2 0 6))
(loop (get-u8 iport) (+ n 4)))))))))))))
(define encode-url
(lambda (iport oport)
(define put-alphabet
(lambda (i)
(put-char oport (vector-ref *table-url* i))))
(let loop ((b0 (get-u8 iport)) (n 0))
(cond
((eof-object? b0) n)
(else
(put-alphabet (fxarithmetic-shift-right b0 2))
(let
((p0 (fxarithmetic-shift-left (fxbit-field b0 0 2) 4))
(b1 (get-u8 iport)))
(cond
((eof-object? b1)
(put-alphabet p0)
(+ n 4))
(else
(put-alphabet (fxior p0 (fxarithmetic-shift-right b1 4)))
(let
((p1 (fxarithmetic-shift-left (fxbit-field b1 0 4) 2))
(b2 (get-u8 iport)))
(cond
((eof-object? b2)
(put-alphabet p1)
(+ n 4))
(else
(put-alphabet (fxior p1 (fxarithmetic-shift-right b2 6)))
(put-alphabet (fxbit-field b2 0 6))
(loop (get-u8 iport) (+ n 4)))))))))))))
(define (encode-bytevector bv f)
(call-with-port (open-bytevector-input-port bv)
(lambda (iport)
(call-with-string-output-port
(lambda (oport)
(f iport oport))))))
(define encode-utf8
(lambda (str)
(encode-bytevector (string->bytevector str (make-transcoder (utf-8-codec))) encode)))
(define encode-utf8-url
(lambda (str)
(encode-bytevector (string->bytevector str (make-transcoder (utf-8-codec))) encode-url)))
(define-condition-type &invalid-encoding &condition
make-invalid-encoding invalid-encoding?
(position invalid-encoding-position))
(define-condition-type &unknown-alphabet &condition
make-unknown-alphabet unknown-alphabet?
(char unknown-alphabet-char))
(define decode
(lambda (iport oport)
(define c->i
(lambda (c)
(let ((n (char->integer c)))
(cond
((<= 65 n 90) (- n 65))
((<= 97 n 122) (- n 71)) ; (+ (- n 97) 26)
((<= 48 n 57) (+ n 4)) ; (+ (- n 48) 52)
((char=? #\+ c) 62)
((char=? #\/ c) 63)
(else (raise (make-unknown-alphabet c)))))))
(define-syntax put-bytes
(syntax-rules ()
((_ c0 c1) (let*
((i0 (c->i c0))
(i1 (c->i c1))
(b0 (fxior (fxarithmetic-shift-left i0 2)(fxbit-field i1 4 6))))
(put-u8 oport b0)
i1))
((_ c0 c1 c2) (let*
((i1 (put-bytes c0 c1))
(i2 (c->i c2))
(b1 (fxior (fxarithmetic-shift-left (fxbit-field i1 0 4) 4)
(fxbit-field i2 2 6))))
(put-u8 oport b1)
i2))
((_ c0 c1 c2 c3) (let*
((i2 (put-bytes c0 c1 c2))
(b2 (fxior (fxarithmetic-shift-left (fxbit-field i2 0 2) 6) (c->i c3))))
(put-u8 oport b2)))))
(let loop ((c0 (get-char iport)) (n 0))
(if (eof-object? c0)
n
(let
((c1 (get-char iport)))
(if (eof-object? c1)
(raise (make-invalid-encoding (+ n 1))))
(let ((c2 (get-char iport)))
(if (eof-object? c2)
(raise (make-invalid-encoding (+ n 2))))
(let ((c3 (get-char iport)))
(cond
((eof-object? c3)
(raise (make-invalid-encoding (+ n 3))))
((char=? #\= c2)
(cond
((char=? #\= c3)
(put-bytes c0 c1)
(+ n 1))
(else
(raise (make-invalid-encoding (+ n 3))))))
((char=? #\= c3)
(put-bytes c0 c1 c2)
(+ n 2))
(else
(put-bytes c0 c1 c2 c3)
(loop (get-char iport) (+ n 3)))))))))))
(define decode-url
(lambda (iport oport)
(define c->i
(lambda (c)
(let ((n (char->integer c)))
(cond
((<= 65 n 90) (- n 65))
((<= 97 n 122) (- n 71)) ; (+ (- n 97) 26)
((<= 48 n 57) (+ n 4)) ; (+ (- n 48) 52)
((char=? #\- c) 62)
((char=? #\_ c) 63)
(else (raise (make-unknown-alphabet c)))))))
(define-syntax put-bytes
(syntax-rules ()
((_ c0 c1) (let*
((i0 (c->i c0))
(i1 (c->i c1))
(b0 (fxior (fxarithmetic-shift-left i0 2)(fxbit-field i1 4 6))))
(put-u8 oport b0)
i1))
((_ c0 c1 c2) (let*
((i1 (put-bytes c0 c1))
(i2 (c->i c2))
(b1 (fxior (fxarithmetic-shift-left (fxbit-field i1 0 4) 4)
(fxbit-field i2 2 6))))
(put-u8 oport b1)
i2))
((_ c0 c1 c2 c3) (let*
((i2 (put-bytes c0 c1 c2))
(b2 (fxior (fxarithmetic-shift-left (fxbit-field i2 0 2) 6) (c->i c3))))
(put-u8 oport b2)))))
(let loop ((c0 (get-char iport)) (n 0))
(if (eof-object? c0)
n
(let
((c1 (get-char iport)))
(if (eof-object? c1)
(raise (make-invalid-encoding (+ n 1))))
(let ((c2 (get-char iport)))
(if (eof-object? c2)
(raise (make-invalid-encoding (+ n 2))))
(let ((c3 (get-char iport)))
(cond
((eof-object? c3)
(raise (make-invalid-encoding (+ n 3))))
((char=? #\= c2)
(cond
((char=? #\= c3)
(put-bytes c0 c1)
(+ n 1))
(else
(raise (make-invalid-encoding (+ n 3))))))
((char=? #\= c3)
(put-bytes c0 c1 c2)
(+ n 2))
(else
(put-bytes c0 c1 c2 c3)
(loop (get-char iport) (+ n 3)))))))))))
(define (decode-string str f)
(assert (string? str))
(call-with-port (open-string-input-port str)
(lambda (iport)
(call-with-bytevector-output-port
(lambda (oport)
(f iport oport))))))
(define len-verif
(lambda (str)
(let ((x (mod (string-length str) 4)))
(cond
((= x 3) (string-append str "="))
((= x 2) (string-append str "=="))
(else str)))))
(define decode-utf8
(lambda (str)
(bytevector->string (decode-string str decode) (make-transcoder (utf-8-codec)))))
(define decode-utf8-url
(lambda (str)
(bytevector->string (decode-string (len-verif str) decode-url) (make-transcoder (utf-8-codec)))))
)