From 1d7925533c5684a4b94e52adfc9d379d506a8c5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20J=C3=BCrgensmeyer?= Date: Sun, 15 Sep 2019 09:21:08 +0200 Subject: [PATCH] Add bit decoding --- decode/decode.go | 39 +++++++++++++++++++++++++++++++++------ decode/decode_test.go | 16 ++++++++++++++++ 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/decode/decode.go b/decode/decode.go index 781536d..7b1f44d 100644 --- a/decode/decode.go +++ b/decode/decode.go @@ -16,19 +16,22 @@ type Encoding string const ( // None decoding. None = "none" - // Proto decoding. - Proto = "proto" - // Hex decoding. - Hex = "hex" + // Bit decoding. + Bit = "bit" // Byte decoding. Byte = "byte" + // Hex decoding. + Hex = "hex" // Base64 decoding. Base64 = "base64" + // Proto decoding. + Proto = "proto" ) // Decoder implementation. type Decoder struct { proto bool + bitDec bool byteDec bool hex bool base64 bool @@ -36,6 +39,7 @@ type Decoder struct { var defaultDecoder = Decoder{ proto: true, + bitDec: true, byteDec: true, hex: true, base64: true, @@ -124,11 +128,19 @@ func (d *Decoder) Decode(input []byte) ([]byte, Encoding) { } } + if d.bitDec { + byteIn := strings.Trim(string(input), "[]") // [32 87 111 114 108 100] -> 32 87 111 114 108 100 + + if b, err := Base2AsBytes(byteIn); err == nil { + return b, Bit + } + } + // byte before hex, hex might contains letters, which are not valid in byte dec if d.byteDec { byteIn := strings.Trim(string(input), "[]") // [32 87 111 114 108 100] -> 32 87 111 114 108 100 - if b, err := AsBytes(byteIn); err == nil { + if b, err := Base10AsBytes(byteIn); err == nil { return b, Byte } } @@ -155,7 +167,7 @@ func (d *Decoder) Decode(input []byte) ([]byte, Encoding) { return input, None } -func AsBytes(input string) ([]byte, error) { +func Base10AsBytes(input string) ([]byte, error) { input = strings.TrimSpace(input) splitted := strings.Split(input, " ") var result []byte @@ -169,3 +181,18 @@ func AsBytes(input string) ([]byte, error) { } return result, nil } + +func Base2AsBytes(input string) ([]byte, error) { + input = strings.TrimSpace(input) + splitted := strings.Split(input, " ") + var result []byte + + for _, i := range splitted { + byteAsInt, err := strconv.ParseInt(i, 2, 0) + if err != nil { + return nil, err + } + result = append(result, byte(byteAsInt)) + } + return result, nil +} diff --git a/decode/decode_test.go b/decode/decode_test.go index acc44b1..7cc1fc3 100644 --- a/decode/decode_test.go +++ b/decode/decode_test.go @@ -27,6 +27,22 @@ func TestDecode(t *testing.T) { output: []byte("no encoding"), }, }, + { + description: "bit", + given: []byte("01010100 01101000 01100101 00100000 01110001 01110101 01101001 01100011 01101011 00100000 01100010 01110010 01101111 01110111 01101110 00100000 11110000 10011111 10100110 10001010 00100000 01101010 01110101 01101101 01110000 01110011 00100000 01101111 01110110 01100101 01110010 00100000 00110001 00110011 00100000 01101100 01100001 01111010 01111001 00100000 11110000 10011111 10010000 10110110 00101110"), + expect: expect{ + encryption: Bit, + output: []byte("The quick brown 🦊 jumps over 13 lazy 🐶."), + }, + }, + { + description: "bytes", + given: []byte("84 104 101 32 113 117 105 99 107 32 98 114 111 119 110 32 240 159 166 138 32 106 117 109 112 115 32 111 118 101 114 32 49 51 32 108 97 122 121 32 240 159 144 182 46"), + expect: expect{ + encryption: Byte, + output: []byte("The quick brown 🦊 jumps over 13 lazy 🐶."), + }, + }, { description: "base64", given: []byte(base64.StdEncoding.EncodeToString([]byte("This is a base64 test"))),