2013-04-04 13:39:43 -04:00
|
|
|
! Copyright (C) 2013 John Benediktsson.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2019-04-05 16:03:19 -04:00
|
|
|
USING: base64.private byte-arrays combinators io io.binary
|
2019-05-19 19:23:35 -04:00
|
|
|
io.encodings.binary io.streams.byte-array kernel kernel.private
|
|
|
|
literals math namespaces sequences ;
|
2013-04-04 13:39:43 -04:00
|
|
|
IN: base85
|
|
|
|
|
|
|
|
ERROR: malformed-base85 ;
|
|
|
|
|
|
|
|
<PRIVATE
|
|
|
|
|
2015-07-15 23:04:19 -04:00
|
|
|
<<
|
2019-04-05 16:03:19 -04:00
|
|
|
CONSTANT: alphabet $[
|
2019-01-24 22:21:00 -05:00
|
|
|
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~"
|
2019-04-05 16:03:19 -04:00
|
|
|
>byte-array
|
|
|
|
]
|
2015-07-15 23:04:19 -04:00
|
|
|
>>
|
2019-04-05 16:03:19 -04:00
|
|
|
|
2015-07-15 21:20:35 -04:00
|
|
|
: ch>base85 ( ch -- ch )
|
|
|
|
alphabet nth ; inline
|
2013-04-04 13:39:43 -04:00
|
|
|
|
|
|
|
: base85>ch ( ch -- ch )
|
2015-07-15 21:20:35 -04:00
|
|
|
$[ alphabet alphabet-inverse ] nth
|
2019-05-19 19:23:35 -04:00
|
|
|
[ malformed-base85 ] unless* { fixnum } declare ; inline
|
2013-04-04 13:39:43 -04:00
|
|
|
|
2015-07-16 01:34:15 -04:00
|
|
|
: encode4 ( seq -- seq' )
|
|
|
|
be> 5 [ 85 /mod ch>base85 ] B{ } replicate-as reverse! nip ; inline
|
|
|
|
|
2015-07-16 10:31:09 -04:00
|
|
|
: (encode-base85) ( stream column -- )
|
2015-07-16 01:34:15 -04:00
|
|
|
4 pick stream-read dup length {
|
2015-07-16 10:31:09 -04:00
|
|
|
{ 0 [ 3drop ] }
|
2015-07-16 01:34:15 -04:00
|
|
|
{ 4 [ encode4 write-lines (encode-base85) ] }
|
2019-01-26 00:14:11 -05:00
|
|
|
[
|
|
|
|
drop
|
|
|
|
[ 4 0 pad-tail encode4 ]
|
2019-01-26 10:14:09 -05:00
|
|
|
[ length 4 swap - head-slice* write-lines ] bi
|
2019-01-26 00:14:11 -05:00
|
|
|
(encode-base85)
|
|
|
|
]
|
2013-04-04 13:39:43 -04:00
|
|
|
} case ;
|
|
|
|
|
|
|
|
PRIVATE>
|
|
|
|
|
|
|
|
: encode-base85 ( -- )
|
2015-07-16 10:31:09 -04:00
|
|
|
input-stream get f (encode-base85) ;
|
2013-04-04 13:39:43 -04:00
|
|
|
|
|
|
|
: encode-base85-lines ( -- )
|
2015-07-16 10:31:09 -04:00
|
|
|
input-stream get 0 (encode-base85) ;
|
2013-04-04 13:39:43 -04:00
|
|
|
|
|
|
|
<PRIVATE
|
|
|
|
|
2019-01-26 00:14:11 -05:00
|
|
|
: decode5 ( seq -- seq' )
|
|
|
|
0 [ [ 85 * ] [ base85>ch ] bi* + ] reduce 4 >be ; inline
|
2013-04-04 13:39:43 -04:00
|
|
|
|
|
|
|
: (decode-base85) ( stream -- )
|
|
|
|
5 "\n\r" pick read-ignoring dup length {
|
|
|
|
{ 0 [ 2drop ] }
|
2019-01-26 00:14:11 -05:00
|
|
|
{ 5 [ decode5 write (decode-base85) ] }
|
|
|
|
[
|
|
|
|
drop
|
|
|
|
[ 5 CHAR: ~ pad-tail decode5 ]
|
2019-01-26 10:14:09 -05:00
|
|
|
[ length 5 swap - head-slice* write ] bi
|
2019-01-26 00:14:11 -05:00
|
|
|
(decode-base85)
|
|
|
|
]
|
2013-04-04 13:39:43 -04:00
|
|
|
} case ;
|
|
|
|
|
|
|
|
PRIVATE>
|
|
|
|
|
|
|
|
: decode-base85 ( -- )
|
|
|
|
input-stream get (decode-base85) ;
|
|
|
|
|
|
|
|
: >base85 ( seq -- base85 )
|
|
|
|
binary [ binary [ encode-base85 ] with-byte-reader ] with-byte-writer ;
|
|
|
|
|
|
|
|
: base85> ( base85 -- seq )
|
|
|
|
binary [ binary [ decode-base85 ] with-byte-reader ] with-byte-writer ;
|
|
|
|
|
|
|
|
: >base85-lines ( seq -- base85 )
|
|
|
|
binary [ binary [ encode-base85-lines ] with-byte-reader ] with-byte-writer ;
|