-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug: base64 decoding adds extra null bytes #40
Comments
I've dug around a bit, and found that it's because the ciphereditor/extensions/essentials/src/binary-to-text.ts Lines 197 to 199 in 534ff1a
The parameters have values […, length=18], 6, 8 . If this was encoding, the transformation would need 13.5 units of 8 bits to encode 18 units of 6 bits. Since this is decoding, the truth is that there is actually only 2 bits of information in the final unit, so we can losslessly store it in 13 bytes.
It isn't fixable for arbitrary transformations, because e.g. encoding one or two 3 bit units into 8 bits is indistinguishable if the second 3 bit is all zero. But if you know that I'll try to make a PR to fix this later. |
…ixes wierkstudio#40) Base64 embeds bytes into 6 bit units. It takes two units to properly encode a single byte. If you then decode this, you can conclude that the 4 additional bits contain no information. (Unless the input violates our assumption that it is a base64 encoding.) You can conclude that if $y = \lceil xs_{input} / s_{output}\rceil$ with x an integer, then $x <= \lfloor ys_{output} / s_{input}\rfloor$. This commit checks every added unit if it is the last, and doesn't add the null byte if it is. It might be more efficient to just use the larger array and slice off the null byte when it exists. It can be impossible to determine a true zero byte from padding if `inputSize < outputSize`, but for binary-to-text that requires an alphabet of more than 256 characters. Signed-off-by: anderium <[email protected]>
Describe the current behavior
When decoding base64 text, there will sometimes be extra padding bytes added to the end of the decoded data.
Here's an example where I'm encoding and decoding the same value, and you can see it added extra null characters to my string:
Describe the expected behavior
base64 encoding should be symetric and not add extra bytes.
Steps to reproduce the bug
Version, Environment, Platform
The text was updated successfully, but these errors were encountered: