Skip to content

Commit

Permalink
Add bit decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
sj14 committed Sep 15, 2019
1 parent c1479c5 commit 1d79255
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
39 changes: 33 additions & 6 deletions decode/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,30 @@ 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
}

var defaultDecoder = Decoder{
proto: true,
bitDec: true,
byteDec: true,
hex: true,
base64: true,
Expand Down Expand Up @@ -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
}
}
Expand All @@ -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
Expand All @@ -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
}
16 changes: 16 additions & 0 deletions decode/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))),
Expand Down

0 comments on commit 1d79255

Please sign in to comment.