Skip to content

Commit

Permalink
Packer Unpacker: generate length prefix with encoded data
Browse files Browse the repository at this point in the history
  • Loading branch information
Baicheng Yu authored and tzzzoz committed Dec 6, 2024
1 parent 9183e0c commit 83e9046
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion field/packer_unpacker.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (p defaultPacker) Pack(value []byte, spec *Spec) ([]byte, error) {
}

// encode the length
lengthPrefix, err := spec.Pref.EncodeLength(spec.Length, len(value))
lengthPrefix, err := spec.Pref.EncodeLength(spec.Length, len(encodedValue))
if err != nil {
return nil, fmt.Errorf("failed to encode length: %w", err)
}
Expand Down
21 changes: 21 additions & 0 deletions field/packer_unpacker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,24 @@ func TestTrack2Packer(t *testing.T) {
})
}
}

func TestPackerandUnpackerWithVariantDataLength(t *testing.T) {
spec := &field.Spec{
Length: 5,
Description: "Field",
Enc: encoding.EBCDIC1047,
Pref: prefix.EBCDIC1047.L,
}

data := []byte{0xc2, 0xa0, 0x31}
str := field.NewString(spec)
str.SetBytes(data)

packed, err := str.Pack()
require.NoError(t, err)
require.Equal(t, 0xf2, packed[0])

_, err = str.Unpack(packed)
require.NoError(t, err)
require.Equal(t, data, str.Bytes())
}

0 comments on commit 83e9046

Please sign in to comment.