From 7ea123c915d331ba62a7ca2ff33a34fe3932d572 Mon Sep 17 00:00:00 2001 From: blinkthethings Date: Thu, 24 Dec 2020 21:42:14 -0500 Subject: [PATCH] Handle upper or mixed case modhex input --- modhex.go | 49 +++++++++++++++++++++++++++++++------------------ modhex_test.go | 13 ++++++++++++- 2 files changed, 43 insertions(+), 19 deletions(-) diff --git a/modhex.go b/modhex.go index e92e719..92cfdc1 100644 --- a/modhex.go +++ b/modhex.go @@ -4,6 +4,7 @@ package modhex import ( "errors" "fmt" + "strings" ) const modhexTable = "cbdefghijklnrtuv" @@ -24,20 +25,31 @@ func EncodedLen(n int) int { } // Encode encodes src into EncodedLen(len(src)) bytes of dst. -// It returns the number of bytes written to dst, but this is -// always EncodedLen(len(src)). -// Encode implements modhex encoding. +// +// Encode returns the number of bytes written to dst, but this is always +// EncodedLen(len(src)). func Encode(dst, src []byte) int { - i := 0 + // keep track of the number of bytes written to dst + len := 0 + + // convert each byte of src and store in dst for _, v := range src { - dst[i] = modhexTable[v>>4] - dst[i+1] = modhexTable[v&0x0f] - i += 2 + // convert the upper nibble to modhex + // and store it into dst + dst[len] = modhexTable[v>>4] + len++ + + // convert the lower nibble to modhex + // and store it into dst + dst[len] = modhexTable[v&0x0f] + len++ } - return len(src) * 2 + + // return the number of bytes written + return len } -// EncodeToString returns the modhex encoding of src. +// EncodeToString returns the modhex encoding of src as a string. func EncodeToString(src []byte) string { // make a new slice to hold the encoded bytes dst := make([]byte, EncodedLen(len(src))) @@ -58,10 +70,9 @@ func DecodedLen(n int) int { // Decode decodes src into DecodedLen(len(src)) bytes, returning // the actual number of bytes written to dst. // -// Decode expects that src contains only modhex characters and that -// src has an even length. -// If the input is malformed, Decode returns the number -// of bytes decoded before the error. +// Decode expects that src contains only modhex characters and that src has an +// even length. If the input is malformed, Decode returns the number of bytes +// decoded before the error. func Decode(dst, src []byte) (int, error) { i, j := 0, 1 for ; j < len(src); j += 2 { @@ -90,13 +101,15 @@ func Decode(dst, src []byte) (int, error) { return i, nil } -// DecodeString returns the bytes represesented by the modhex string s. +// DecodeString returns the bytes represented by the modhex string s. // -// Decode expects that s contains only modhex characters and that s -// has an even length. -// If the input is malformed, Decode returns the number -// of bytes decoded before the error. +// DecodeString expects that s contains only modhex characters and that s +// has an even length. If the input is malformed, DecodeString returns the +// number of bytes decoded before the error. func DecodeString(s string) ([]byte, error) { + // convert the string to lower case since the map uses lower case + s = strings.ToLower(s) + // convert the input string to an array of bytes src := []byte(s) diff --git a/modhex_test.go b/modhex_test.go index dde1aaa..f3a877a 100644 --- a/modhex_test.go +++ b/modhex_test.go @@ -2,6 +2,7 @@ package modhex import ( "encoding/hex" + "strings" "testing" ) @@ -17,6 +18,16 @@ func TestEncodeHexDecodeHex(t *testing.T) { hex: "2d344e83", }, + { + modhex: "DTEFFUJE", + hex: "2d344e83", + }, + + { + modhex: "dTeFfUjE", + hex: "2d344e83", + }, + { modhex: "hknhfjbrjnlnldnhcujvddbikngjrtgh", hex: "69b6481c8baba2b60e8f22179b58cd56", @@ -32,7 +43,7 @@ func TestEncodeHexDecodeHex(t *testing.T) { src, _ := hex.DecodeString(tc.hex) modhex := EncodeToString(src) - if modhex != tc.modhex { + if modhex != strings.ToLower(modhex) { t.Errorf("Encode Incorrect: Actual: %s; Expected: %s\n", modhex, tc.modhex) }