ia0 / data-encoding

Efficient and customizable data-encoding functions in Rust
https://data-encoding.rs/
MIT License
177 stars 23 forks source link

Question about Specification #86

Closed NYBACHOK closed 1 year ago

NYBACHOK commented 1 year ago

Hi. I need help with encoding bytes as rfc8949. For example UT with expects value 1903e8 while default HEXLOWER returns 1900000000000003e8. Would appreciate any help.

ia0 commented 1 year ago

Hi, thanks for reaching out!

Could you provide me a bit more information about what you're trying to do exactly? Ideally with code pointers and example data input and output. In particular, which tag number of CBOR are you concerned about? 21, 22, 33, and 34 seem to be the relevant ones.

Thanks!

NYBACHOK commented 1 year ago

Thanks for fast reply!

I want to write tests checks if CBOR/RFC 8949 implementation correct. As I understand bytes in writter is correct and issue when I'm creating hex representation.

#[test]
    fn check_unsigned_int()
    {
        // Examples come from RFC8949, Appendix A
        let var = 
        [
            ( 0_u64, "00" ),
            ( 1, "01" ),
            ( 10, "0a"),
            ( 23, "17"),
            ( 24, "1818"),
            ( 25, "1819"),
            ( 100, "1864" ),
            ( 1000, "1903e8"),
            ( 1000000, "1a000f4240"),
            ( 1000000000000, "1b000000e8d4a51000"),
            ( 18446744073709551615, "1bffffffffffffffff" ),
        ];

        validate_result( var )
    }

     fn validate_result<T : Cbor>(value : impl IntoIterator<Item = (T, &'static str)>)
    {
        for ( i, expected ) in value
        {
            let mut buf = Vec::new();

            i.encode(&mut buf).expect(&format!( "Failed to write buffer for {}", i));

            let hex = data_encoding::HEXLOWER.encode( &buf );

            assert_eq!( &hex, expected );
        }
    }
    assertion `left == right` failed
  left: "1900000000000003e8"
 right: "1903e8"
ia0 commented 1 year ago

Thanks for the code. This looks to me like a problem in the CBOR encoding. You can see it by comparing at the byte level instead of the hexadecimal representation.

-let hex = data_encoding::HEXLOWER.encode( &buf );
-assert_eq!( &hex, expected );
+let expected = data_encoding::HEXLOWER.decode(expected.as_bytes()).unwrap();
+assert_eq!(buf, expected, "{buf:02x?} != {expected:02x?}");
NYBACHOK commented 1 year ago

I forget that I may compare bytes representation. Thanks for that!

NYBACHOK commented 1 year ago

I resolved issue with my code so thanks you again

ia0 commented 1 year ago

Perfect, thanks a lot for the feedback!