bencode: use linked-assocs to preserve ordering, fix byte-strings.

the byte-string was being "decoded" with replacement characters, messing
up binary data.
master
John Benediktsson 2020-01-08 11:45:42 -08:00
parent d7c0dfcb2b
commit 73b01704a2
2 changed files with 11 additions and 11 deletions

View File

@ -1,4 +1,4 @@
USING: bencode tools.test ;
USING: bencode linked-assocs tools.test ;
{ "i42e" } [ 42 >bencode ] unit-test
{ "i0e" } [ 0 >bencode ] unit-test
@ -8,6 +8,6 @@ USING: bencode tools.test ;
{ { "spam" 42 } } [ "l4:spami42ee" bencode> ] unit-test
{ H{ { "bar" "spam" } { "foo" 42 } } } [
{ LH{ { "bar" "spam" } { "foo" 42 } } } [
"d3:bar4:spam3:fooi42ee" bencode>
] unit-test

View File

@ -1,6 +1,6 @@
USING: arrays assocs combinators hashtables io
io.encodings.ascii io.encodings.string io.streams.string kernel
math math.parser sequences strings ;
USING: arrays assocs combinators io io.encodings.ascii
io.encodings.string io.streams.string kernel linked-assocs math
math.parser sequences strings ;
IN: bencode
GENERIC: >bencode ( obj -- bencode )
@ -18,10 +18,10 @@ M: assoc >bencode
[ [ >bencode ] bi@ append ] { } assoc>map concat
"d" "e" surround ;
<PRIVATE
DEFER: read-bencode
<PRIVATE
: read-integer ( -- obj )
"e" read-until CHAR: e assert= string>number ;
@ -31,11 +31,13 @@ DEFER: read-bencode
: read-dictionary ( -- obj )
[
read-bencode [ read-bencode 2array ] [ f ] if* dup
] [ ] produce nip >hashtable ;
] [ ] produce nip >linked-hash ;
: read-string ( prefix -- obj )
":" read-until CHAR: : assert= swap prefix
string>number read ascii decode ;
string>number read "" like ;
PRIVATE>
: read-bencode ( -- obj )
read1 {
@ -46,7 +48,5 @@ DEFER: read-bencode
[ read-string ]
} case ;
PRIVATE>
: bencode> ( bencode -- obj )
[ read-bencode ] with-string-reader ;