Closed alxu-avpt closed 1 year ago
Correct, but Base64Decoder
is designed for best performance. It's expected that the input stream is a valid base64 string. I understand that A==A==
is not a valid base64 string.
Checking that buffer[^1] == '='
is not enough. The padding can be in the middle of the string. Thus, I have to check the presence of =
character for each call of DecodeFromUtf16
, which is O(n) operation that slows down the decoding of the stream.
Also, the following code is valid as well:
var decoder = new DotNext.Buffers.Text.Base64Decoder();
var owner = decoder.DecodeFromUtf16("AA=");
owner.Dispose();
Assert.True(decoder.NeedMoreData);
owner = decoder.DecodeFromUtf16("=");
owner.Dispose();
Anyway, I'll try to implement this in performant way.
Done, by the cost of extra branching in some methods. I think this is acceptable overhead. Now it's working both for Utf8 and Utf16 encodings.
Thanks!
Fixed in 4.12.2 and published.
Expected behavior: an exception is thrown
Actual behavior: no exception is thrown
The best solution is probably to create member
bool gotPadding
or similar, set it ifbuffer[^1] == '='
, and throw exception if gotPadding is set when starting decoding. Also related, it's possible to save some memory using forbidden states inreservedBuffer
, examples: