| 1 |
;;; |
| 2 |
;;; base64.scm - base64 encoding/decoding routine |
| 3 |
;;; |
| 4 |
;;; Copyright (c) 2000-2024 Shiro Kawai <[email protected]> |
| 5 |
;;; |
| 6 |
;;; Redistribution and use in source and binary forms, with or without |
| 7 |
;;; modification, are permitted provided that the following conditions |
| 8 |
;;; are met: |
| 9 |
;;; |
| 10 |
;;; 1. Redistributions of source code must retain the above copyright |
| 11 |
;;; notice, this list of conditions and the following disclaimer. |
| 12 |
;;; |
| 13 |
;;; 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
;;; notice, this list of conditions and the following disclaimer in the |
| 15 |
;;; documentation and/or other materials provided with the distribution. |
| 16 |
;;; |
| 17 |
;;; 3. Neither the name of the authors nor the names of its contributors |
| 18 |
;;; may be used to endorse or promote products derived from this |
| 19 |
;;; software without specific prior written permission. |
| 20 |
;;; |
| 21 |
;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 27 |
;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 |
;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 |
;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 |
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 |
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
;;; |
| 33 |
|
| 34 |
;; Implements Base64 encoding/decoding routine |
| 35 |
;; RFC4648 <https://datatracker.ietf.org/doc/html/rfc4648> |
| 36 |
;; We also provide Base32 and Base16 as defined in RFC4648. |
| 37 |
|
| 38 |
(define-module rfc.base64 |
| 39 |
(use gauche.sequence) |
| 40 |
(use gauche.threads) |
| 41 |
(use srfi.42) |
| 42 |
(export base64-encode base64-encode-message |
| 43 |
base64-decode base64-decode-string-to |
| 44 |
|
| 45 |
base32-encode base32-encode-message |
| 46 |
base32-decode base32-decode-string-to |
| 47 |
base32hex-encode base32hex-encode-message |
| 48 |
base32hex-decode base32hex-decode-string-to |
| 49 |
|
| 50 |
base16-encode base16-encode-message |
| 51 |
base16-decode base16-decode-string-to |
| 52 |
|
| 53 |
;; deprecated, kept for the backward compatibility |
| 54 |
base64-encode-string base64-encode-bytevector |
| 55 |
base64-decode-string base64-decode-bytevector |
| 56 |
)) |
| 57 |
(select-module rfc.base64) |
| 58 |
|
| 59 |
(autoload gauche.vport open-input-uvector open-output-uvector get-output-uvector) |
| 60 |
|
| 61 |
;; Common routine to derive port->port filter to seq->seq |
| 62 |
(define (make-string->bytevector string thunk) |
| 63 |
(let1 out (open-output-uvector) |
| 64 |
(with-input-from-string string |
| 65 |
(cut with-output-to-port out thunk)) |
| 66 |
(get-output-uvector out))) |
| 67 |
(define (make-bytevector->string bv thunk) |
| 68 |
(assume-type bv <u8vector>) |
| 69 |
(with-output-to-string |
| 70 |
(^[] (with-input-from-port (open-input-uvector bv) thunk)))) |
| 71 |
(define (make-decode-string-to target string thunk) |
| 72 |
(cond |
| 73 |
[(eqv? target <string>) (with-string-io string thunk)] |
| 74 |
[(eqv? target <u8vector>) (make-string->bytevector string thunk)] |
| 75 |
[else (error "invalid target:" target)])) |
| 76 |
|
| 77 |
|
| 78 |
;;; |
| 79 |
;;; Base 64 |
| 80 |
;;; |
| 81 |
|
| 82 |
;; Mapping tables |
| 83 |
;; Decode table is a 96-element vector, maps charcode-32 to integer 0-63. |
| 84 |
;; Encode table is a 65-element vector, maps 0-63 and padding to a character. |
| 85 |
|
| 86 |
(define *standard-decode-table* |
| 87 |
;; ! " # $ % & ' ( ) * + , - . / |
| 88 |
#(#f #f #f #f #f #f #f #f #f #f #f 62 #f #f #f 63 |
| 89 |
;;0 1 2 3 4 5 6 7 8 9 : ; < = > ? |
| 90 |
52 53 54 55 56 57 58 59 60 61 #f #f #f #f #f #f |
| 91 |
;;@ A B C D E F G H I J K L M N O |
| 92 |
#f 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
| 93 |
;;P Q R S T U V W X Y Z [ \ ] ^ _ |
| 94 |
15 16 17 18 19 20 21 22 23 24 25 #f #f #f #f #f |
| 95 |
;;` a b c d e f g h i j k l m n o |
| 96 |
#f 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
| 97 |
;;p q r s t u v w x y z { | } ~ |
| 98 |
41 42 43 44 45 46 47 48 49 50 51 #f #f #f #f #f |
| 99 |
)) |
| 100 |
|
| 101 |
(define *standard-encode-table* |
| 102 |
;;0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
| 103 |
#(#\A #\B #\C #\D #\E #\F #\G #\H #\I #\J #\K #\L #\M #\N #\O #\P |
| 104 |
;;16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
| 105 |
#\Q #\R #\S #\T #\U #\V #\W #\X #\Y #\Z #\a #\b #\c #\d #\e #\f |
| 106 |
;;32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
| 107 |
#\g #\h #\i #\j #\k #\l #\m #\n #\o #\p #\q #\r #\s #\t #\u #\v |
| 108 |
;;48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
| 109 |
#\w #\x #\y #\z #\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9 #\+ #\/ |
| 110 |
;;pad |
| 111 |
#\= |
| 112 |
)) |
| 113 |
|
| 114 |
(define *url-safe-decode-table* |
| 115 |
;; ! " # $ % & ' ( ) * + , - . / |
| 116 |
#(#f #f #f #f #f #f #f #f #f #f #f #f #f 62 #f #f |
| 117 |
;;0 1 2 3 4 5 6 7 8 9 : ; < = > ? |
| 118 |
52 53 54 55 56 57 58 59 60 61 #f #f #f #f #f #f |
| 119 |
;;@ A B C D E F G H I J K L M N O |
| 120 |
#f 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
| 121 |
;;P Q R S T U V W X Y Z [ \ ] ^ _ |
| 122 |
15 16 17 18 19 20 21 22 23 24 25 #f #f #f #f 63 |
| 123 |
;;` a b c d e f g h i j k l m n o |
| 124 |
#f 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
| 125 |
;;p q r s t u v w x y z { | } ~ |
| 126 |
41 42 43 44 45 46 47 48 49 50 51 #f #f #f #f #f |
| 127 |
)) |
| 128 |
|
| 129 |
(define *url-safe-encode-table* |
| 130 |
;;0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
| 131 |
#(#\A #\B #\C #\D #\E #\F #\G #\H #\I #\J #\K #\L #\M #\N #\O #\P |
| 132 |
;;16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
| 133 |
#\Q #\R #\S #\T #\U #\V #\W #\X #\Y #\Z #\a #\b #\c #\d #\e #\f |
| 134 |
;;32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
| 135 |
#\g #\h #\i #\j #\k #\l #\m #\n #\o #\p #\q #\r #\s #\t #\u #\v |
| 136 |
;;48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
| 137 |
#\w #\x #\y #\z #\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9 #\- #\_ |
| 138 |
;;pad |
| 139 |
#\= |
| 140 |
)) |
| 141 |
|
| 142 |
(define (%standard-digits? digit0 digit1) |
| 143 |
(and (eqv? digit0 #\+) (eqv? digit1 #\/))) |
| 144 |
|
| 145 |
(define (%url-safe-digits? digit0 digit1) |
| 146 |
(and (eqv? digit0 #\-) (eqv? digit1 #\_))) |
| 147 |
|
| 148 |
(define *nonstd-decode-tables* |
| 149 |
(make-hash-table 'equal?)) ; cons -> vector |
| 150 |
|
| 151 |
(define (%valid-digit? digit) |
| 152 |
(and (char-set:ascii digit) |
| 153 |
(not (#[A-Za-z0-9=] digit)))) |
| 154 |
|
| 155 |
(define (%digits->decode-table digits) |
| 156 |
(unless (and (or (string? digits) (vector? digits)) |
| 157 |
(eqv? (size-of digits) 2)) |
| 158 |
(error "Digits must be a string or vector of length 2, but got:" digits)) |
| 159 |
(let ([digit0 (~ digits 0)] |
| 160 |
[digit1 (~ digits 1)]) |
| 161 |
(assume (%valid-digit? digit0) "Invalid extra digit for base64:" digit0) |
| 162 |
(assume (%valid-digit? digit1) "Invalid extra digit for base64:" digit1) |
| 163 |
(cond [(%standard-digits? digit0 digit1) *standard-decode-table*] |
| 164 |
[(%url-safe-digits? digit0 digit1) *url-safe-decode-table*] |
| 165 |
[(hash-table-get *nonstd-decode-tables* (cons digit0 digit1) #f)] |
| 166 |
[else |
| 167 |
(rlet1 v (vector-copy *standard-decode-table*) |
| 168 |
(vector-set! v (- (char->integer digit0) 32) 62) |
| 169 |
(vector-set! v (- (char->integer digit1) 32) 63) |
| 170 |
(hash-table-put! *nonstd-decode-tables* (cons digit0 digit1) v))]))) |
| 171 |
|
| 172 |
(define *nonstd-encode-tables* |
| 173 |
(make-hash-table 'equal?)) ; digits -> vector |
| 174 |
|
| 175 |
(define (%digits->encode-table digits) |
| 176 |
(unless (and (or (string? digits) (vector? digits)) |
| 177 |
(eqv? (size-of digits) 2)) |
| 178 |
(error "Digits must be a string or vector of length 2, but got:" digits)) |
| 179 |
(let ([digit0 (~ digits 0)] |
| 180 |
[digit1 (~ digits 1)]) |
| 181 |
(assume (%valid-digit? digit0) "Invalid extra digit for base64:" digit0) |
| 182 |
(assume (%valid-digit? digit1) "Invalid extra digit for base64:" digit1) |
| 183 |
(cond [(%standard-digits? digit0 digit1) *standard-encode-table*] |
| 184 |
[(%url-safe-digits? digit0 digit1) *url-safe-encode-table*] |
| 185 |
[(hash-table-get *nonstd-encode-tables* (cons digit0 digit1) #f)] |
| 186 |
[else |
| 187 |
(rlet1 v (vector-copy *standard-encode-table*) |
| 188 |
(vector-set! v 62 digit0) |
| 189 |
(vector-set! v 63 digit1) |
| 190 |
(hash-table-put! *nonstd-encode-tables* (cons digit0 digit1) v))]))) |
| 191 |
|
| 192 |
(define (base64-decode :key (url-safe #f) (digits #f) (strict #f)) |
| 193 |
(define table (cond [url-safe *url-safe-decode-table*] |
| 194 |
[digits (%digits->decode-table digits)] |
| 195 |
[else *standard-decode-table*])) |
| 196 |
(define (invalid c) (error "Invalid base64 input character" c)) |
| 197 |
(define (premature) (when strict (error "Premature end of base64 input"))) |
| 198 |
(letrec-syntax ([lookup (syntax-rules () |
| 199 |
[(_ c) |
| 200 |
(let1 i (char->integer c) |
| 201 |
(and (< 32 i 128) |
| 202 |
(vector-ref table (- i 32))))])] |
| 203 |
[state (syntax-rules () |
| 204 |
[(_ c end-expr handler skipper) |
| 205 |
(cond [(eof-object? c) end-expr] |
| 206 |
[(eqv? c #\=)] |
| 207 |
[(lookup c) => handler] |
| 208 |
[(and strict (not (char-whitespace? c))) |
| 209 |
(invalid c)] |
| 210 |
[else skipper])])]) |
| 211 |
;; |00000011|11112222|22333333| |
| 212 |
(define (d0 c) |
| 213 |
(state c #f (cut d1 (read-char) <>) (d0 (read-char)))) |
| 214 |
(define (d1 c c0) |
| 215 |
(state c (premature) |
| 216 |
(^[c1] |
| 217 |
(write-byte (+ (ash c0 2) (ash c1 -4))) |
| 218 |
(d2 (read-char) (logand c1 #x0f))) |
| 219 |
(d1 (read-char) c0))) |
| 220 |
(define (d2 c c1) |
| 221 |
(state c (premature) |
| 222 |
(^[c2] |
| 223 |
(write-byte (+ (ash c1 4) (ash c2 -2))) |
| 224 |
(d3 (read-char) (logand c2 #x03))) |
| 225 |
(d2 (read-char) c1))) |
| 226 |
(define (d3 c c2) |
| 227 |
(state c (premature) |
| 228 |
(^[c3] |
| 229 |
(write-byte (+ (ash c2 6) c3)) |
| 230 |
(d0 (read-char))) |
| 231 |
(d3 (read-char) c2))) |
| 232 |
|
| 233 |
(d0 (read-char)))) |
| 234 |
|
| 235 |
(define (base64-decode-string-to target string . opts) |
| 236 |
(make-decode-string-to target string (cut apply base64-decode opts))) |
| 237 |
|
| 238 |
;; DEPRECATED |
| 239 |
(define (base64-decode-string string . opts) |
| 240 |
(with-string-io string (cut apply base64-decode opts))) |
| 241 |
(define (base64-decode-bytevector string . opts) |
| 242 |
(make-string->bytevector string (cut apply base64-decode opts))) |
| 243 |
|
| 244 |
(define (base64-encode :key (line-width 76) (url-safe #f) (digits #f) |
| 245 |
(omit-padding #f)) |
| 246 |
(define table (cond [url-safe *url-safe-encode-table*] |
| 247 |
[digits (%digits->encode-table digits)] |
| 248 |
[else *standard-encode-table*])) |
| 249 |
(define maxcol (and line-width (> line-width 0) (- line-width 1))) |
| 250 |
|
| 251 |
(letrec-syntax ([emit* |
| 252 |
(syntax-rules () |
| 253 |
[(_ col) col] |
| 254 |
[(_ col idx idx2 ...) |
| 255 |
(begin |
| 256 |
(write-char (vector-ref table idx)) |
| 257 |
(let1 col2 (cond [(eqv? col maxcol) (newline) 0] |
| 258 |
[else (+ col 1)]) |
| 259 |
(emit* col2 idx2 ...)))])]) |
| 260 |
;; b0 |
| 261 |
;; |AAAAAA--| |
| 262 |
(define (e0 b0 col) |
| 263 |
(if (eof-object? b0) |
| 264 |
col |
| 265 |
(e1 (logand b0 #x03) (read-byte) (emit* col (ash b0 -2))))) |
| 266 |
|
| 267 |
;; b0 b1 |
| 268 |
;; |------BB|BBBB----| |
| 269 |
(define (e1 b0 b1 col) |
| 270 |
(if (eof-object? b1) |
| 271 |
(let1 col (emit* col (ash b0 4)) |
| 272 |
(unless omit-padding |
| 273 |
(emit* col 64 64))) |
| 274 |
(e2 (logand b1 #x0f) (read-byte) |
| 275 |
(emit* col (+ (ash b0 4) (ash b1 -4)))))) |
| 276 |
|
| 277 |
;; b0 b1 b2 |
| 278 |
;; |--------|----CCCC|CCDDDDDD| |
| 279 |
(define (e2 b1 b2 col) |
| 280 |
(if (eof-object? b2) |
| 281 |
(let1 col (emit* col (ash b1 2)) |
| 282 |
(unless omit-padding |
| 283 |
(emit* col 64))) |
| 284 |
(e0 (read-byte) |
| 285 |
(emit* col (+ (ash b1 2) (ash b2 -6)) (logand b2 #x3f))))) |
| 286 |
|
| 287 |
(e0 (read-byte) 0))) |
| 288 |
|
| 289 |
(define (base64-encode-message msg . opts) |
| 290 |
(etypecase msg |
| 291 |
[<string> (with-string-io msg (cut apply base64-encode opts))] |
| 292 |
[<u8vector> (make-bytevector->string msg (cut apply base64-encode opts))])) |
| 293 |
|
| 294 |
;; DEPRECATED |
| 295 |
(define (base64-encode-string string . opts) |
| 296 |
(with-string-io string (cut apply base64-encode opts))) |
| 297 |
(define (base64-encode-bytevector vec . opts) |
| 298 |
(make-bytevector->string vec (cut apply base64-encode opts))) |
| 299 |
|
| 300 |
|
| 301 |
;;; |
| 302 |
;;; Base32 |
| 303 |
;;; |
| 304 |
|
| 305 |
(define *base32-decode-table* |
| 306 |
;; ! " # $ % & ' ( ) * + , - . / |
| 307 |
#(#f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f |
| 308 |
;;0 1 2 3 4 5 6 7 8 9 : ; < = > ? |
| 309 |
#f #f 26 27 28 29 30 31 #f #f #f #f #f #f #f #f |
| 310 |
;;@ A B C D E F G H I J K L M N O |
| 311 |
#f 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
| 312 |
;;P Q R S T U V W X Y Z [ \ ] ^ _ |
| 313 |
15 16 17 18 19 20 21 22 23 24 25 #f #f #f #f #f |
| 314 |
;;` a b c d e f g h i j k l m n o |
| 315 |
#f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f |
| 316 |
;;p q r s t u v w x y z { | } ~ |
| 317 |
#f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f |
| 318 |
)) |
| 319 |
|
| 320 |
(define *base32-encode-table* |
| 321 |
;;0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
| 322 |
#(#\A #\B #\C #\D #\E #\F #\G #\H #\I #\J #\K #\L #\M #\N #\O #\P |
| 323 |
;;16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
| 324 |
#\Q #\R #\S #\T #\U #\V #\W #\X #\Y #\Z #\2 #\3 #\4 #\5 #\6 #\7 |
| 325 |
;;pad |
| 326 |
#\= |
| 327 |
)) |
| 328 |
|
| 329 |
(define *base32hex-decode-table* |
| 330 |
;; ! " # $ % & ' ( ) * + , - . / |
| 331 |
#(#f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f |
| 332 |
;;0 1 2 3 4 5 6 7 8 9 : ; < = > ? |
| 333 |
0 1 2 3 4 5 6 7 8 9 #f #f #f #f #f #f |
| 334 |
;;@ A B C D E F G H I J K L M N O |
| 335 |
#f 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
| 336 |
;;P Q R S T U V W X Y Z [ \ ] ^ _ |
| 337 |
25 26 27 28 29 30 31 #f #f #f #f #f #f #f #f #f |
| 338 |
;;` a b c d e f g h i j k l m n o |
| 339 |
#f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f |
| 340 |
;;p q r s t u v w x y z { | } ~ |
| 341 |
#f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f |
| 342 |
)) |
| 343 |
|
| 344 |
(define *base32hex-encode-table* |
| 345 |
;;0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
| 346 |
#(#\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9 #\A #\B #\C #\D #\E #\F |
| 347 |
;;16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
| 348 |
#\G #\H #\I #\J #\K #\L #\M #\N #\O #\P #\Q #\R #\S #\T #\U #\V |
| 349 |
;;pad |
| 350 |
#\= |
| 351 |
)) |
| 352 |
|
| 353 |
(define (%base32-decode table strict) |
| 354 |
(define (invalid c) (error "Invalid base32 input character" c)) |
| 355 |
(define (premature) (when strict (error "Premature end of base32 input"))) |
| 356 |
(letrec-syntax ([lookup (syntax-rules () |
| 357 |
[(_ c) |
| 358 |
(let1 i (char->integer c) |
| 359 |
(and (< 32 i 128) |
| 360 |
(vector-ref table (- i 32))))])] |
| 361 |
[state (syntax-rules () |
| 362 |
[(_ c end-expr handler skipper) |
| 363 |
(cond [(eof-object? c) end-expr] |
| 364 |
[(eqv? c #\=)] |
| 365 |
[(lookup c) => handler] |
| 366 |
[(and strict (not (char-whitespace? c))) |
| 367 |
(invalid c)] |
| 368 |
[else skipper])])]) |
| 369 |
;; |00000111|11222223|33334444|45555566|66677777| |
| 370 |
(define (d0 c) |
| 371 |
(state c #f (cut d1 (read-char) <>) (d0 (read-char)))) |
| 372 |
(define (d1 c c0) |
| 373 |
(state c (premature) |
| 374 |
(^[c1] |
| 375 |
(write-byte (+ (ash c0 3) (ash c1 -2))) |
| 376 |
(d2 (read-char) (logand c1 #x03))) |
| 377 |
(d1 (read-char) c0))) |
| 378 |
(define (d2 c c1) |
| 379 |
(state c (premature) |
| 380 |
(^[c2] (d3 (read-char) c1 c2)) |
| 381 |
(d2 (read-char) c1))) |
| 382 |
(define (d3 c c1 c2) |
| 383 |
(state c (premature) |
| 384 |
(^[c3] |
| 385 |
(write-byte (+ (ash c1 6) (ash c2 1) (ash c3 -4))) |
| 386 |
(d4 (read-char) (logand c3 #x0f))) |
| 387 |
(d3 (read-char) c1 c2))) |
| 388 |
(define (d4 c c3) |
| 389 |
(state c (premature) |
| 390 |
(^[c4] |
| 391 |
(write-byte (+ (ash c3 4) (ash c4 -1))) |
| 392 |
(d5 (read-char) (logand c4 #x01))) |
| 393 |
(d4 (read-char) c3))) |
| 394 |
(define (d5 c c4) |
| 395 |
(state c (premature) |
| 396 |
(^[c5] (d6 (read-char) c4 c5)) |
| 397 |
(d5 (read-char) c4))) |
| 398 |
(define (d6 c c4 c5) |
| 399 |
(state c (premature) |
| 400 |
(^[c6] |
| 401 |
(write-byte (+ (ash c4 7) (ash c5 2) (ash c6 -3))) |
| 402 |
(d7 (read-char) (logand c6 #x07))) |
| 403 |
(d6 (read-char) c4 c5))) |
| 404 |
(define (d7 c c6) |
| 405 |
(state c (premature) |
| 406 |
(^[c7] |
| 407 |
(write-byte (+ (ash c6 5) c7)) |
| 408 |
(d0 (read-char))) |
| 409 |
(d7 c c6))) |
| 410 |
|
| 411 |
(d0 (read-char)))) |
| 412 |
|
| 413 |
(define (base32-decode :key (strict #f)) |
| 414 |
(%base32-decode *base32-decode-table* strict)) |
| 415 |
(define (base32hex-decode :key (strict #f)) |
| 416 |
(%base32-decode *base32hex-decode-table* strict)) |
| 417 |
|
| 418 |
(define (base32-decode-string-to target string :key (strict #f)) |
| 419 |
(make-decode-string-to target string (cut base32-decode :strict strict))) |
| 420 |
(define (base32hex-decode-string-to target string :key (strict #f)) |
| 421 |
(make-decode-string-to target string (cut base32hex-decode :strict strict))) |
| 422 |
|
| 423 |
(define (%base32-encode table omit-padding line-width) |
| 424 |
(define maxcol (and line-width (> line-width 0) (- line-width 1))) |
| 425 |
|
| 426 |
(letrec-syntax ([emit* |
| 427 |
(syntax-rules () |
| 428 |
[(_ col) col] |
| 429 |
[(_ col idx idx2 ...) |
| 430 |
(begin |
| 431 |
(write-char (vector-ref table idx)) |
| 432 |
(let1 col2 (cond [(eqv? col maxcol) (newline) 0] |
| 433 |
[else (+ col 1)]) |
| 434 |
(emit* col2 idx2 ...)))])]) |
| 435 |
|
| 436 |
;; b0 |
| 437 |
;; |AAAAA---| |
| 438 |
(define (e0 b0 col) |
| 439 |
(if (eof-object? b0) |
| 440 |
col |
| 441 |
(e1 (logand b0 #x07) (read-byte) (emit* col (ash b0 -3))))) |
| 442 |
|
| 443 |
;; b0 b1 |
| 444 |
;; |-----BBB|BBCCCCC-| |
| 445 |
(define (e1 b0 b1 col) |
| 446 |
(if (eof-object? b1) |
| 447 |
(rlet1 col (emit* col (ash b0 2)) |
| 448 |
(unless omit-padding |
| 449 |
(emit* col 32 32 32 32 32 32))) |
| 450 |
(e2 (logand b1 #x01) (read-byte) |
| 451 |
(emit* col |
| 452 |
(+ (ash b0 2) (ash b1 -6)) |
| 453 |
(logand (ash b1 -1) #x1f))))) |
| 454 |
|
| 455 |
;; b0 b1 b2 |
| 456 |
;; |--------|-------D|DDDD----| |
| 457 |
(define (e2 b1 b2 col) |
| 458 |
(if (eof-object? b2) |
| 459 |
(rlet1 col (emit* col (ash b1 4)) |
| 460 |
(unless omit-padding |
| 461 |
(emit* col 32 32 32 32))) |
| 462 |
(e3 (logand b2 #x0f) (read-byte) |
| 463 |
(emit* col (+ (ash b1 4) (ash b2 -4)))))) |
| 464 |
|
| 465 |
;; b0 b1 b2 b3 |
| 466 |
;; |--------|--------|----EEEE|EFFFFF--| |
| 467 |
(define (e3 b2 b3 col) |
| 468 |
(if (eof-object? b3) |
| 469 |
(rlet1 col (emit* col (ash b2 1)) |
| 470 |
(unless omit-padding |
| 471 |
(emit* col 32 32 32))) |
| 472 |
(e4 (logand b3 #x03) (read-byte) |
| 473 |
(emit* col (+ (ash b2 1) (ash b3 -7)) |
| 474 |
(logand (ash b3 -2) #x1f))))) |
| 475 |
|
| 476 |
;; b0 b1 b2 b3 b4 |
| 477 |
;; |--------|--------|--------|------GG|GGGHHHHH| |
| 478 |
(define (e4 b3 b4 col) |
| 479 |
(if (eof-object? b4) |
| 480 |
(rlet1 col (emit* col (ash b3 3)) |
| 481 |
(unless omit-padding |
| 482 |
(emit* col 32))) |
| 483 |
(e0 (read-byte) |
| 484 |
(emit* col (+ (ash b3 3) (ash b4 -5)) (logand b4 #x1f))))) |
| 485 |
|
| 486 |
(e0 (read-byte) 0))) |
| 487 |
|
| 488 |
(define (base32-encode :key (omit-padding #f) (line-width 76)) |
| 489 |
(%base32-encode *base32-encode-table* omit-padding line-width)) |
| 490 |
(define (base32hex-encode :key (omit-padding #f) (line-width 76)) |
| 491 |
(%base32-encode *base32hex-encode-table* omit-padding line-width)) |
| 492 |
|
| 493 |
(define (base32-encode-message msg . args) |
| 494 |
(etypecase msg |
| 495 |
[<string> (with-string-io msg (cut apply base32-encode args))] |
| 496 |
[<u8vector> (make-bytevector->string msg (cut apply base32-encode args))])) |
| 497 |
(define (base32hex-encode-message msg . args) |
| 498 |
(etypecase msg |
| 499 |
[<string> (with-string-io msg (cut apply base32hex-encode args))] |
| 500 |
[<u8vector> (make-bytevector->string msg (cut apply base32hex-encode args))])) |
| 501 |
|
| 502 |
;;; |
| 503 |
;;; Base16 |
| 504 |
;;; |
| 505 |
|
| 506 |
;; The RFC only uses uppercase alphabets. For the convenience, |
| 507 |
;; the decoder accept lowercase letters in non-strict mode. The encoder |
| 508 |
;; generates lowercase letetrs when :lowercase is true. |
| 509 |
|
| 510 |
(define *base16-decode-table* |
| 511 |
;; ! " # $ % & ' ( ) * + , - . / |
| 512 |
#(#f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f |
| 513 |
;;0 1 2 3 4 5 6 7 8 9 : ; < = > ? |
| 514 |
0 1 2 3 4 5 6 7 8 9 #f #f #f #f #f #f |
| 515 |
;;@ A B C D E F G H I J K L M N O |
| 516 |
#f 10 11 12 13 14 15 #f #f #f #f #f #f #f #f #f |
| 517 |
;;P Q R S T U V W X Y Z [ \ ] ^ _ |
| 518 |
#f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f |
| 519 |
;;` a b c d e f g h i j k l m n o |
| 520 |
#f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f |
| 521 |
;;p q r s t u v w x y z { | } ~ |
| 522 |
#f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f |
| 523 |
)) |
| 524 |
(define *base16lax-decode-table* |
| 525 |
;; ! " # $ % & ' ( ) * + , - . / |
| 526 |
#(#f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f |
| 527 |
;;0 1 2 3 4 5 6 7 8 9 : ; < = > ? |
| 528 |
0 1 2 3 4 5 6 7 8 9 #f #f #f #f #f #f |
| 529 |
;;@ A B C D E F G H I J K L M N O |
| 530 |
#f 10 11 12 13 14 15 #f #f #f #f #f #f #f #f #f |
| 531 |
;;P Q R S T U V W X Y Z [ \ ] ^ _ |
| 532 |
#f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f |
| 533 |
;;` a b c d e f g h i j k l m n o |
| 534 |
#f 10 11 12 13 14 15 #f #f #f #f #f #f #f #f #f |
| 535 |
;;p q r s t u v w x y z { | } ~ |
| 536 |
#f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f |
| 537 |
)) |
| 538 |
|
| 539 |
(define *base16-encode-table* |
| 540 |
;;0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
| 541 |
#(#\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9 #\A #\B #\C #\D #\E #\F)) |
| 542 |
(define *base16lc-encode-table* |
| 543 |
;;0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
| 544 |
#(#\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9 #\a #\b #\c #\d #\e #\f)) |
| 545 |
|
| 546 |
|
| 547 |
(define (base16-decode :key (strict #f)) |
| 548 |
(define (invalid c) (error "Invalid base16 input character" c)) |
| 549 |
(define (premature) (when strict (error "Premature end of base16 input"))) |
| 550 |
(define table (if strict *base16-decode-table* *base16lax-decode-table*)) |
| 551 |
(letrec-syntax ([lookup (syntax-rules () |
| 552 |
[(_ c) |
| 553 |
(let1 i (char->integer c) |
| 554 |
(and (< 32 i 128) |
| 555 |
(vector-ref table (- i 32))))])] |
| 556 |
[state (syntax-rules () |
| 557 |
[(_ c end-expr handler skipper) |
| 558 |
(cond [(eof-object? c) end-expr] |
| 559 |
[(lookup c) => handler] |
| 560 |
[(and strict (not (char-whitespace? c))) |
| 561 |
(invalid c)] |
| 562 |
[else skipper])])]) |
| 563 |
(define (d0 c) |
| 564 |
(state c #f (cut d1 (read-char) <>) (d0 (read-char)))) |
| 565 |
(define (d1 c b0) |
| 566 |
(state c (premature) |
| 567 |
(^[b1] (write-byte (+ (ash b0 4) b1)) (d0 (read-char))) |
| 568 |
(d1 (read-char) b0))) |
| 569 |
(d0 (read-char)))) |
| 570 |
|
| 571 |
(define (base16-decode-string-to target string :key (strict #f)) |
| 572 |
(make-decode-string-to target string (cut base16-decode :strict strict))) |
| 573 |
|
| 574 |
(define (base16-encode :key (lowercase #f)) |
| 575 |
(define table (if lowercase *base16lc-encode-table* *base16-encode-table*)) |
| 576 |
(generator-for-each |
| 577 |
(^b (write-char (vector-ref table (ash b -4))) |
| 578 |
(write-char (vector-ref table (logand b #x0f)))) |
| 579 |
read-byte)) |
| 580 |
|
| 581 |
(define (base16-encode-message msg :key (lowercase #f)) |
| 582 |
(etypecase msg |
| 583 |
[<string> (with-string-io msg (cut base16-encode :lowercase lowercase))] |
| 584 |
[<u8vector> (make-bytevector->string msg (cut base16-encode :lowercase lowercase))])) |