]> gitweb.factorcode.org Git - factor.git/commitdiff
bencode: use linked-assocs to preserve ordering, fix byte-strings.
authorJohn Benediktsson <mrjbq7@gmail.com>
Wed, 8 Jan 2020 19:45:42 +0000 (11:45 -0800)
committerJohn Benediktsson <mrjbq7@gmail.com>
Wed, 8 Jan 2020 19:45:42 +0000 (11:45 -0800)
the byte-string was being "decoded" with replacement characters, messing
up binary data.

extra/bencode/bencode-tests.factor
extra/bencode/bencode.factor

index 1d18c6c205c3fa2b0d3e5c2688f2d3285ea8ab38..2ef67966819cfbb8e139fb7fe950ab14f5b80541 100644 (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
index 2347595ae485ea0953120777874986e35aad187d..7173b93c12bf9a4d104a5eb23e0446854eb02845 100644 (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 ;