|
|
@ -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) |
|
|
|
|
|
|
|