encoding/base32: Convert files to UTF-8 with Unix line endings

This commit is contained in:
Zoltán Kéri
2024-12-30 15:31:57 +01:00
parent 5ce6990077
commit 3d25128520
+224 -223
View File
@@ -1,223 +1,224 @@
package encoding_base32 package encoding_base32
// @note(zh): Encoding utility for Base32 // @note(zh): Encoding utility for Base32
// A secondary param can be used to supply a custom alphabet to // A secondary param can be used to supply a custom alphabet to
// @link(encode) and a matching decoding table to @link(decode). // @link(encode) and a matching decoding table to @link(decode).
// If none is supplied it just uses the standard Base32 alphabet. // If none is supplied it just uses the standard Base32 alphabet.
// In case your specific version does not use padding, you may // In case your specific version does not use padding, you may
// truncate it from the encoded output. // truncate it from the encoded output.
// Error represents errors that can occur during base32 decoding operations. // Error represents errors that can occur during base32 decoding operations.
// See RFC 4648 sections 3.2, 4 and 6. // See RFC 4648 sections 3.2, 4 and 6.
Error :: enum { Error :: enum {
None, None,
Invalid_Character, // Input contains characters outside the specified alphabet Invalid_Character, // Input contains characters outside the specified alphabet
Invalid_Length, // Input length is not valid for base32 (must be a multiple of 8 with proper padding) Invalid_Length, // Input length is not valid for base32 (must be a multiple of 8 with proper padding)
Malformed_Input, // Input has improper structure (wrong padding position or incomplete groups) Malformed_Input, // Input has improper structure (wrong padding position or incomplete groups)
} }
Validate_Proc :: #type proc(c: byte) -> bool Validate_Proc :: #type proc(c: byte) -> bool
@private @private
_validate_default :: proc(c: byte) -> bool { _validate_default :: proc(c: byte) -> bool {
return (c >= 'A' && c <= 'Z') || (c >= '2' && c <= '7') return (c >= 'A' && c <= 'Z') || (c >= '2' && c <= '7')
} }
ENC_TABLE := [32]byte { ENC_TABLE := [32]byte {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', '2', '3', '4', '5', '6', '7', 'Y', 'Z', '2', '3', '4', '5', '6', '7',
} }
PADDING :: '=' PADDING :: '='
DEC_TABLE := [256]u8 { DEC_TABLE := [256]u8 {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 26, 27, 28, 29, 30, 31, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 26, 27, 28, 29, 30, 31, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0, 0, 0, 0, 0, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0, 0, 0, 0, 0,
0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0, 0, 0, 0, 0, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
} }
encode :: proc(data: []byte, ENC_TBL := ENC_TABLE, allocator := context.allocator) -> string { encode :: proc(data: []byte, ENC_TBL := ENC_TABLE, allocator := context.allocator) -> string {
out_length := (len(data) + 4) / 5 * 8 out_length := (len(data) + 4) / 5 * 8
out := make([]byte, out_length, allocator) out := make([]byte, out_length, allocator)
_encode(out, data, ENC_TBL) _encode(out, data, ENC_TBL)
return string(out[:]) return string(out[:])
} }
@private @private
_encode :: proc(out, data: []byte, ENC_TBL := ENC_TABLE, allocator := context.allocator) { _encode :: proc(out, data: []byte, ENC_TBL := ENC_TABLE, allocator := context.allocator) {
out := out out := out
data := data data := data
for len(data) > 0 { for len(data) > 0 {
carry: byte carry: byte
switch len(data) { switch len(data) {
case: case:
out[7] = ENC_TBL[data[4] & 0x1f] out[7] = ENC_TBL[data[4] & 0x1f]
carry = data[4] >> 5 carry = data[4] >> 5
fallthrough fallthrough
case 4: case 4:
out[6] = ENC_TBL[carry | (data[3] << 3) & 0x1f] out[6] = ENC_TBL[carry | (data[3] << 3) & 0x1f]
out[5] = ENC_TBL[(data[3] >> 2) & 0x1f] out[5] = ENC_TBL[(data[3] >> 2) & 0x1f]
carry = data[3] >> 7 carry = data[3] >> 7
fallthrough fallthrough
case 3: case 3:
out[4] = ENC_TBL[carry | (data[2] << 1) & 0x1f] out[4] = ENC_TBL[carry | (data[2] << 1) & 0x1f]
carry = (data[2] >> 4) & 0x1f carry = (data[2] >> 4) & 0x1f
fallthrough fallthrough
case 2: case 2:
out[3] = ENC_TBL[carry | (data[1] << 4) & 0x1f] out[3] = ENC_TBL[carry | (data[1] << 4) & 0x1f]
out[2] = ENC_TBL[(data[1] >> 1) & 0x1f] out[2] = ENC_TBL[(data[1] >> 1) & 0x1f]
carry = (data[1] >> 6) & 0x1f carry = (data[1] >> 6) & 0x1f
fallthrough fallthrough
case 1: case 1:
out[1] = ENC_TBL[carry | (data[0] << 2) & 0x1f] out[1] = ENC_TBL[carry | (data[0] << 2) & 0x1f]
out[0] = ENC_TBL[data[0] >> 3] out[0] = ENC_TBL[data[0] >> 3]
} }
if len(data) < 5 { if len(data) < 5 {
out[7] = byte(PADDING) out[7] = byte(PADDING)
if len(data) < 4 { if len(data) < 4 {
out[6] = byte(PADDING) out[6] = byte(PADDING)
out[5] = byte(PADDING) out[5] = byte(PADDING)
if len(data) < 3 { if len(data) < 3 {
out[4] = byte(PADDING) out[4] = byte(PADDING)
if len(data) < 2 { if len(data) < 2 {
out[3] = byte(PADDING) out[3] = byte(PADDING)
out[2] = byte(PADDING) out[2] = byte(PADDING)
} }
} }
} }
break break
} }
data = data[5:] data = data[5:]
out = out[8:] out = out[8:]
} }
} }
@(optimization_mode="favor_size") @(optimization_mode="favor_size")
decode :: proc( decode :: proc(
data: string, data: string,
DEC_TBL := DEC_TABLE, DEC_TBL := DEC_TABLE,
validate: Validate_Proc = _validate_default, validate: Validate_Proc = _validate_default,
allocator := context.allocator) -> (out: []byte, err: Error) { allocator := context.allocator) -> (out: []byte, err: Error)
if len(data) == 0 { {
return nil, .None if len(data) == 0 {
} return nil, .None
}
// Check minimum length requirement first
if len(data) < 2 { // Check minimum length requirement first
return nil, .Invalid_Length if len(data) < 2 {
} return nil, .Invalid_Length
}
// Validate characters using provided validation function
for i := 0; i < len(data); i += 1 { // Validate characters using provided validation function
c := data[i] for i := 0; i < len(data); i += 1 {
if c == byte(PADDING) { c := data[i]
break if c == byte(PADDING) {
} break
if !validate(c) { }
return nil, .Invalid_Character if !validate(c) {
} return nil, .Invalid_Character
} }
}
// Validate padding and length
data_len := len(data) // Validate padding and length
padding_count := 0 data_len := len(data)
for i := data_len - 1; i >= 0; i -= 1 { padding_count := 0
if data[i] != byte(PADDING) { for i := data_len - 1; i >= 0; i -= 1 {
break if data[i] != byte(PADDING) {
} break
padding_count += 1 }
} padding_count += 1
}
// Check for proper padding and length combinations
if padding_count > 0 { // Check for proper padding and length combinations
// Verify no padding in the middle if padding_count > 0 {
for i := 0; i < data_len - padding_count; i += 1 { // Verify no padding in the middle
if data[i] == byte(PADDING) { for i := 0; i < data_len - padding_count; i += 1 {
return nil, .Malformed_Input if data[i] == byte(PADDING) {
} return nil, .Malformed_Input
} }
}
content_len := data_len - padding_count
mod8 := content_len % 8 content_len := data_len - padding_count
required_padding: int mod8 := content_len % 8
switch mod8 { required_padding: int
case 2: required_padding = 6 // 2 chars need 6 padding chars switch mod8 {
case 4: required_padding = 4 // 4 chars need 4 padding chars case 2: required_padding = 6 // 2 chars need 6 padding chars
case 5: required_padding = 3 // 5 chars need 3 padding chars case 4: required_padding = 4 // 4 chars need 4 padding chars
case 7: required_padding = 1 // 7 chars need 1 padding char case 5: required_padding = 3 // 5 chars need 3 padding chars
case: required_padding = 0 case 7: required_padding = 1 // 7 chars need 1 padding char
} case: required_padding = 0
}
if required_padding > 0 {
if padding_count != required_padding { if required_padding > 0 {
return nil, .Malformed_Input if padding_count != required_padding {
} return nil, .Malformed_Input
} else if mod8 != 0 { }
return nil, .Malformed_Input } else if mod8 != 0 {
} return nil, .Malformed_Input
} else { }
// No padding - must be multiple of 8 } else {
if data_len % 8 != 0 { // No padding - must be multiple of 8
return nil, .Malformed_Input if data_len % 8 != 0 {
} return nil, .Malformed_Input
} }
}
// Calculate decoded length: 5 bytes for every 8 input chars
input_chars := data_len - padding_count // Calculate decoded length: 5 bytes for every 8 input chars
out_len := input_chars * 5 / 8 input_chars := data_len - padding_count
out = make([]byte, out_len, allocator) out_len := input_chars * 5 / 8
defer if err != .None { out = make([]byte, out_len, allocator)
delete(out) defer if err != .None {
} delete(out)
}
// Process input in 8-byte blocks
outi := 0 // Process input in 8-byte blocks
for i := 0; i < input_chars; i += 8 { outi := 0
buf: [8]byte for i := 0; i < input_chars; i += 8 {
block_size := min(8, input_chars - i) buf: [8]byte
block_size := min(8, input_chars - i)
// Decode block
for j := 0; j < block_size; j += 1 { // Decode block
buf[j] = DEC_TBL[data[i + j]] for j := 0; j < block_size; j += 1 {
} buf[j] = DEC_TBL[data[i + j]]
}
// Convert to output bytes based on block size
bytes_to_write := block_size * 5 / 8 // Convert to output bytes based on block size
switch block_size { bytes_to_write := block_size * 5 / 8
case 8: switch block_size {
out[outi + 4] = (buf[6] << 5) | buf[7] case 8:
fallthrough out[outi + 4] = (buf[6] << 5) | buf[7]
case 7: fallthrough
out[outi + 3] = (buf[4] << 7) | (buf[5] << 2) | (buf[6] >> 3) case 7:
fallthrough out[outi + 3] = (buf[4] << 7) | (buf[5] << 2) | (buf[6] >> 3)
case 5: fallthrough
out[outi + 2] = (buf[3] << 4) | (buf[4] >> 1) case 5:
fallthrough out[outi + 2] = (buf[3] << 4) | (buf[4] >> 1)
case 4: fallthrough
out[outi + 1] = (buf[1] << 6) | (buf[2] << 1) | (buf[3] >> 4) case 4:
fallthrough out[outi + 1] = (buf[1] << 6) | (buf[2] << 1) | (buf[3] >> 4)
case 2: fallthrough
out[outi] = (buf[0] << 3) | (buf[1] >> 2) case 2:
} out[outi] = (buf[0] << 3) | (buf[1] >> 2)
outi += bytes_to_write }
} outi += bytes_to_write
}
return
} return
}