|
@@ -148,3 +148,78 @@ test_base32_roundtrip :: proc(t: ^testing.T) {
|
|
testing.expect(t, bytes.equal(decoded, transmute([]byte)input))
|
|
testing.expect(t, bytes.equal(decoded, transmute([]byte)input))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+@(test)
|
|
|
|
+
|
|
|
|
+test_base32_custom_alphabet :: proc(t: ^testing.T) {
|
|
|
|
+ custom_enc_table := [32]byte{
|
|
|
|
+ '0', '1', '2', '3', '4', '5', '6', '7',
|
|
|
|
+ '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
|
|
|
|
+ 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
|
|
|
|
+ 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ custom_dec_table: [256]u8
|
|
|
|
+ for i := 0; i < len(custom_enc_table); i += 1 {
|
|
|
|
+ custom_dec_table[custom_enc_table[i]] = u8(i)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ custom_dec_table := [256]u8{
|
|
|
|
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x00-0x0f
|
|
|
|
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x10-0x1f
|
|
|
|
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x20-0x2f
|
|
|
|
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, // 0x30-0x3f ('0'-'9')
|
|
|
|
+ 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, // 0x40-0x4f ('A'-'O')
|
|
|
|
+ 25, 26, 27, 28, 29, 30, 31, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x50-0x5f ('P'-'V')
|
|
|
|
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x60-0x6f
|
|
|
|
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x70-0x7f
|
|
|
|
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x80-0x8f
|
|
|
|
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x90-0x9f
|
|
|
|
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xa0-0xaf
|
|
|
|
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xb0-0xbf
|
|
|
|
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xc0-0xcf
|
|
|
|
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xd0-0xdf
|
|
|
|
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xe0-0xef
|
|
|
|
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xf0-0xff
|
|
|
|
+ }
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+ custom_validate :: proc(c: byte) -> bool {
|
|
|
|
+ return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'V') || c == byte(PADDING)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ cases := [?]struct {
|
|
|
|
+ input: string,
|
|
|
|
+ enc_expected: string,
|
|
|
|
+ }{
|
|
|
|
+ {"f", "CO======"},
|
|
|
|
+ {"fo", "CPNG===="},
|
|
|
|
+ {"foo", "CPNMU==="},
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for c in cases {
|
|
|
|
+ // Test encoding
|
|
|
|
+ encoded := encode(transmute([]byte)c.input, custom_enc_table)
|
|
|
|
+ testing.expect(t, encoded == c.enc_expected)
|
|
|
|
+
|
|
|
|
+ // Test decoding
|
|
|
|
+ decoded, err := decode(encoded, custom_dec_table, custom_validate)
|
|
|
|
+ defer if decoded != nil {
|
|
|
|
+ delete(decoded)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ testing.expect_value(t, err, Error.None)
|
|
|
|
+ testing.expect(t, bytes.equal(decoded, transmute([]byte)c.input))
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // Test invalid character detection
|
|
|
|
+ {
|
|
|
|
+ input := "WXY=====" // Contains chars not in our alphabet
|
|
|
|
+ output, err := decode(input, custom_dec_table, custom_validate)
|
|
|
|
+ if output != nil {
|
|
|
|
+ delete(output)
|
|
|
|
+ }
|
|
|
|
+ testing.expect_value(t, err, Error.Invalid_Character)
|
|
|
|
+ }
|
|
|
|
+}
|