struct DecoderWithIgnore [src]
A decoder that ignores certain characters.
The decoder will skip any characters that are in the ignore list.
Fields
ignored_chars: StaticBitSet(256) = undefinedThe characters to ignore.
Members
- decode (Function)
- decodedLenForSlice (Function)
- decodedLenUpperBound (Function)
Source
pub const DecoderWithIgnore = struct {
/// The characters to ignore.
ignored_chars: StaticBitSet(256) = undefined,
/// Decodes a hexadecimal string into a binary buffer.
/// The output buffer must be half the size of the input buffer.
pub fn decode(
self: DecoderWithIgnore,
bin: []u8,
encoded: []const u8,
) error{ NoSpaceLeft, InvalidCharacter, InvalidPadding }![]const u8 {
return decodeAny(bin, encoded, self.ignored_chars);
}
/// Returns the decoded length of a hexadecimal string, ignoring any characters in the ignore list.
/// This operation does not run in constant time, but it aims to avoid leaking information about the underlying hexadecimal string.
pub fn decodedLenForSlice(decoder: DecoderWithIgnore, encoded: []const u8) !usize {
var hex_len = encoded.len;
for (encoded) |c| {
if (decoder.ignored_chars.isSet(c)) hex_len -= 1;
}
if (hex_len % 2 != 0) {
return error.InvalidPadding;
}
return hex_len / 2;
}
/// Returns the maximum possible decoded size for a given input length after skipping ignored characters.
pub fn decodedLenUpperBound(hex_len: usize) usize {
return hex_len / 2;
}
}