1 ! Copyright (C) 2013 John Benediktsson
2 ! See http://factorcode.org/license.txt for BSD license
4 USING: combinators.short-circuit io io.streams.string kernel
5 locals make math math.bitwise namespaces sequences ;
11 ERROR: bad-length seq ;
13 : check-length ( seq -- seq )
14 dup length 45 > [ throw-bad-length ] when ; inline
16 :: binary>ascii ( seq -- seq' )
20 dup length CHAR: \s + ,
22 [ dup empty? bits zero? and ] [
28 unclip-slice char bitor char!
33 [ char swap neg shift 0x3f bitand CHAR: \s + , ]
40 ERROR: illegal-character ch ;
42 : check-illegal-character ( ch -- ch )
43 dup { [ CHAR: \s < ] [ CHAR: \s 64 + > ] } 1||
44 [ throw-illegal-character ] when ;
46 :: ascii>binary ( seq -- seq' )
50 seq unclip-slice dup CHAR: \s =
51 [ drop 0 ] [ CHAR: \s - ] if :> len!
54 [ dup empty? not len 0 > and ] [
55 dup empty? [ 0 ] [ unclip-slice ] if
56 dup "\r\n\0" member? [
59 check-illegal-character
63 char 6 shift bitor char!
68 [ char swap neg shift 0xff bitand , ]
69 [ on-bits char bitand char! ]
81 input-stream get [ binary>ascii print ] 45 (each-stream-block)
84 : string>uu ( seq -- seq' )
85 [ [ uu-encode ] with-string-writer ] with-string-reader ;
88 [ [ "begin" head? ] [ not ] bi or ] [ readln ] do until
90 dup [ "end" head? ] [ not ] bi or
91 [ drop t ] [ ascii>binary write f ] if
92 ] [ readln ] do until ;
94 : uu>string ( seq -- seq )
95 [ [ uu-decode ] with-string-writer ] with-string-reader ;