diff --git a/basis/serialize/serialize-tests.factor b/basis/serialize/serialize-tests.factor index 0dfa1ee051..937f7c5e02 100644 --- a/basis/serialize/serialize-tests.factor +++ b/basis/serialize/serialize-tests.factor @@ -5,7 +5,7 @@ USING: tools.test kernel serialize io io.streams.byte-array alien arrays byte-arrays bit-arrays specialized-arrays sequences math prettyprint parser classes math.constants io.encodings.binary random assocs serialize.private alien.c-types -combinators.short-circuit ; +combinators.short-circuit literals ; SPECIALIZED-ARRAY: double IN: serialize.tests @@ -107,3 +107,25 @@ CONSTANT: objects bytes>object dup keys first eq? ] unit-test + +! Changed the serialization of numbers in [2^1008;2^1024[ +! check backwards compatibility +${ 1008 2^ } [ B{ + 255 1 127 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 +} binary [ deserialize-cell ] with-byte-reader ] unit-test + +${ 1024 2^ 1 - } [ B{ + 255 1 128 255 255 255 255 255 255 255 255 255 255 255 255 + 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 + 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 + 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 + 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 + 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 + 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 + 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 + 255 255 255 255 255 255 255 255 255 255 255 +} binary [ deserialize-cell ] with-byte-reader ] unit-test diff --git a/basis/serialize/serialize.factor b/basis/serialize/serialize.factor index 2a79beabf2..cdd58e3c99 100644 --- a/basis/serialize/serialize.factor +++ b/basis/serialize/serialize.factor @@ -29,11 +29,11 @@ SYMBOL: serialized #! Return the id of an already serialized object serialized get at ; -! Numbers are serialized as follows: +! Positive numbers are serialized as follows: ! 0 => B{ 0 } -! 1<=x<=126 => B{ x | 0x80 } -! x>127 => B{ length(x) x[0] x[1] ... } -! x>2^1024 => B{ 0xff length(x) x[0] x[1] ... } +! 1<=x<127 => B{ x | 0x80 } +! 127<=x<2^1024 => B{ length(x) x[0] x[1] ... }; 1 B{ 0xff } + serialize(length(x)) + B{ x[0] x[1] ... } ! The last case is needed because a very large number would ! otherwise be confused with a small number. : serialize-cell ( n -- ) @@ -42,7 +42,7 @@ SYMBOL: serialized 0x80 bitor write1 ] [ dup log2 8 /i 1 + - dup 0x7f >= [ + dup 0x81 >= [ 0xff write1 dup serialize-cell ] [ @@ -55,7 +55,7 @@ SYMBOL: serialized : deserialize-cell ( -- n ) read1 { { [ dup 0xff = ] [ drop deserialize-cell read be> ] } - { [ dup 0x80 >= ] [ 0x80 bitxor ] } + { [ dup 0x81 >= ] [ 0x80 bitxor ] } [ read be> ] } cond ;