]> gitweb.factorcode.org Git - factor.git/blob - basis/base64/base64.factor
base64: simplify (encode-base64).
[factor.git] / basis / base64 / base64.factor
1 ! Copyright (C) 2008 Doug Coleman, Daniel Ehrenberg.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays combinators fry io io.binary io.encodings.binary
4 io.streams.byte-array kernel literals math namespaces sbufs
5 sequences ;
6 IN: base64
7
8 ERROR: malformed-base64 ;
9
10 <PRIVATE
11
12 <<
13 CONSTANT: alphabet
14     "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
15
16 : alphabet-inverse ( alphabet -- seq )
17     dup supremum 1 + f <array> [
18         '[ swap _ set-nth ] each-index
19     ] keep ;
20 >>
21
22 : ch>base64 ( ch -- ch )
23     alphabet nth ; inline
24
25 : base64>ch ( ch -- ch )
26     $[ alphabet alphabet-inverse 0 CHAR: = pick set-nth ] nth
27     [ malformed-base64 ] unless* ; inline
28
29 : (write-lines) ( column byte-array -- column' )
30     output-stream get dup '[
31         _ stream-write1 1 + dup 76 = [
32             drop B{ CHAR: \r CHAR: \n } _ stream-write 0
33         ] when
34     ] each ; inline
35
36 : write-lines ( column byte-array -- column' )
37     over [ (write-lines) ] [ write ] if ; inline
38
39 : encode3 ( seq -- byte-array )
40     be> { -18 -12 -6 0 } '[
41         shift 0x3f bitand ch>base64
42     ] with B{ } map-as ; inline
43
44 : encode-pad ( seq n -- byte-array )
45     [ 3 0 pad-tail encode3 ] [ 1 + ] bi* head-slice
46     4 CHAR: = pad-tail ; inline
47
48 : (encode-base64) ( stream column -- )
49     3 pick stream-read dup length {
50         { 0 [ 3drop ] }
51         { 3 [ encode3 write-lines (encode-base64) ] }
52         [ encode-pad write-lines (encode-base64) ]
53     } case ;
54
55 PRIVATE>
56
57 : encode-base64 ( -- )
58     input-stream get f (encode-base64) ;
59
60 : encode-base64-lines ( -- )
61     input-stream get 0 (encode-base64) ;
62
63 <PRIVATE
64
65 : read1-ignoring ( ignoring stream -- ch )
66     dup stream-read1 pick dupd member?
67     [ drop read1-ignoring ] [ 2nip ] if ; inline recursive
68
69 : push-ignoring ( accum ch -- accum )
70     dup { f 0 } member-eq? [ drop ] [ suffix! ] if ; inline
71
72 : read-ignoring ( n ignoring stream -- sbuf )
73     [ [ <sbuf> ] keep ] 2dip
74     '[ _ _ read1-ignoring push-ignoring ] times ; inline
75
76 : decode4 ( seq -- )
77     [ 0 [ base64>ch swap 6 shift bitor ] reduce 3 >be ]
78     [ [ CHAR: = = ] count ] bi
79     [ write ] [ head-slice* write ] if-zero ; inline
80
81 : (decode-base64) ( stream -- )
82     4 "\n\r" pick read-ignoring dup length {
83         { 0 [ 2drop ] }
84         { 4 [ decode4 (decode-base64) ] }
85         [ malformed-base64 ]
86     } case ;
87
88 PRIVATE>
89
90 : decode-base64 ( -- )
91     input-stream get (decode-base64) ;
92
93 : >base64 ( seq -- base64 )
94     binary [ binary [ encode-base64 ] with-byte-reader ] with-byte-writer ;
95
96 : base64> ( base64 -- seq )
97     binary [ binary [ decode-base64 ] with-byte-reader ] with-byte-writer ;
98
99 : >base64-lines ( seq -- base64 )
100     binary [ binary [ encode-base64-lines ] with-byte-reader ] with-byte-writer ;