librasn / rasn

A Safe #[no_std] ASN.1 Codec Framework
Other
199 stars 50 forks source link

fix(per): pad non-negative binary integer slice at start #176

Closed 6d7a closed 11 months ago

6d7a commented 11 months ago

The workaround to_vec function in src/per works fine in all cases but when decoding non-negative binary integers. To be read correctly into a BigUint, the bitslice needs to be padded at the start. I added an additional parameter to specify where padding should be inserted when converting the bitslice to Vec<u8>.

If you have a minute, @XAMPPRocky, could you let me know if there is a more elegant way of handling the two cases in to_vec?

Thanks

XAMPPRocky commented 11 months ago

If you have a minute, @XAMPPRocky, could you let me know if there is a more elegant way of handling the two cases in to_vec?

If I'm reading the code right, there's only one spot where it's true and the rest are false, so I would just make a second function the pad_left case. There's no reason AFAICT that it has to be in to_vec.

6d7a commented 11 months ago

Thanks, I was actually thinking more about the repeated for loop. I like the extra parameter in to_vec, because it makes it clear where padding will be added for all cases.

XAMPPRocky commented 11 months ago

I'm still not sure I see the benefit to it being one function rather than two functions? I think two functions is clearer, looking at the callsite it's not clear what the boolean does.