Skip to content

Commit

Permalink
add comments to ASCIIHexToBytes to clarify its usage (#329)
Browse files Browse the repository at this point in the history
  • Loading branch information
alovak authored Oct 4, 2024
1 parent b29f3de commit a62ddd3
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 126 deletions.
19 changes: 15 additions & 4 deletions encoding/hex.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ import (

// HEX to ASCII encoder
var (
_ Encoder = (*hexToASCIIEncoder)(nil)
BytesToASCIIHex = &hexToASCIIEncoder{}
_ Encoder = (*hexToASCIIEncoder)(nil)
// BytesToASCIIHex is an encoder that converts bytes into their ASCII
// representation. On success, the ASCII representation bytes are returned
// Don't use this encoder with String, Numeric or Binary fields as packing and
// unpacking in these fields uses length of value/bytes, so only Pack will be
// able to write the value correctly.
BytesToASCIIHex = &hexToASCIIEncoder{}
)

type hexToASCIIEncoder struct{}
Expand Down Expand Up @@ -56,8 +61,14 @@ func (e hexToASCIIEncoder) Decode(data []byte, length int) ([]byte, int, error)

// ASCII To HEX encoder
var (
_ Encoder = (*asciiToHexEncoder)(nil)
ASCIIHexToBytes = &asciiToHexEncoder{}
_ Encoder = (*asciiToHexEncoder)(nil)
// ASCIIHexToBytes is an encoder that converts ASCII Hex-digits into a byte slice
// This encoder is used in TagSpec, BerTLVTag and similar.
// It shouldn't be used with String, Numeric or Binary fields as packing and unpacking
// in these fields uses length of value/bytes, so only Unpack will be able to read
// the value correctly.
// If you are looking for a way to work with HEX strings, use Hex field instead.
ASCIIHexToBytes = &asciiToHexEncoder{}
)

type asciiToHexEncoder struct{}
Expand Down
95 changes: 0 additions & 95 deletions examples/icc_test.go

This file was deleted.

33 changes: 7 additions & 26 deletions field/hex.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var (
// field. It's convenient to use when you need to work with hex strings, but
// don't want to deal with converting them to bytes manually.
// If provided value is not a valid hex string, it will return an error during
// packing.
// packing. For the Hex field, the Binary encoding shoud be used in the Spec.
type Hex struct {
value string
spec *Spec
Expand Down Expand Up @@ -85,43 +85,24 @@ func (f *Hex) Pack() ([]byte, error) {
return nil, utils.NewSafeErrorf(err, "converting hex field into bytes")
}

if f.spec.Pad != nil {
data = f.spec.Pad.Pad(data, f.spec.Length)
}

packed, err := f.spec.Enc.Encode(data)
if err != nil {
return nil, fmt.Errorf("failed to encode content: %w", err)
}
packer := f.spec.getPacker()

packedLength, err := f.spec.Pref.EncodeLength(f.spec.Length, len(data))
if err != nil {
return nil, fmt.Errorf("failed to encode length: %w", err)
}

return append(packedLength, packed...), nil
return packer.Pack(data, f.spec)
}

func (f *Hex) Unpack(data []byte) (int, error) {
dataLen, prefBytes, err := f.spec.Pref.DecodeLength(f.spec.Length, data)
if err != nil {
return 0, fmt.Errorf("failed to decode length: %w", err)
}
unpacker := f.spec.getUnpacker()

raw, read, err := f.spec.Enc.Decode(data[prefBytes:], dataLen)
raw, bytesRead, err := unpacker.Unpack(data, f.spec)
if err != nil {
return 0, fmt.Errorf("failed to decode content: %w", err)
}

if f.spec.Pad != nil {
raw = f.spec.Pad.Unpad(raw)
return 0, err
}

if err := f.SetBytes(raw); err != nil {
return 0, fmt.Errorf("failed to set bytes: %w", err)
}

return read + prefBytes, nil
return bytesRead, nil
}

// Deprecated. Use Marshal instead
Expand Down
25 changes: 25 additions & 0 deletions field/hex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,31 @@ func TestHexField(t *testing.T) {
require.Equal(t, "AABBCCDDEE", f.Value())
})

t.Run("packing and unpacking with variable length", func(t *testing.T) {
spec := &Spec{
Length: 5, // 5 bytes, 10 hex chars
Description: "Field",
Enc: encoding.Binary,
Pref: prefix.Binary.LL,
}

f := NewHexValue("AABBCCDDEE")
f.SetSpec(spec)

packed, err := f.Pack()

require.NoError(t, err)
require.Equal(t, []byte{0x00, 0x05, 0xaa, 0xbb, 0xcc, 0xdd, 0xee}, packed)

f = NewHex(spec)
read, err := f.Unpack(packed)

require.NoError(t, err)
require.Equal(t, 7, read)
require.Equal(t, "AABBCCDDEE", f.Value())

})

t.Run("marshaling", func(t *testing.T) {
f := NewHexValue("AABBCCDDEE")
f2 := &Hex{}
Expand Down
4 changes: 3 additions & 1 deletion field/packer_unpacker.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package field

import "fmt"
import (
"fmt"
)

type defaultPacker struct{}

Expand Down

0 comments on commit a62ddd3

Please sign in to comment.