PySocks icon indicating copy to clipboard operation
PySocks copied to clipboard

Fix length field conversion

Open michaelroland opened this issue 3 years ago • 1 comments
trafficstars

Since urllib3 removed their fork of the library again, I'm reposting this here in the hope that it will eventually be picked up.

michaelroland avatar Aug 18 '22 09:08 michaelroland

The chr() method interprets its argument as Unicode code point. Consequently, chr(x).encode() only works for encoding (unsigned) integers with values between 0x00..0x7F (ASCII subset) into bytes, but not for values between 0x80..0xFF. However, single-byte length fields currently encoded using chr(x).encode() are unsigned 1-byte integers with values between 0..255 (0x00..0xFF).

This PR improves conversion between integers and byte-strings by relying on the pack/unpack methods of the struct standard library to get consistent encodeding and decoding of integer values with well-defined format.

michaelroland avatar Oct 19 '22 13:10 michaelroland