diff --git a/field/packer_unpacker.go b/field/packer_unpacker.go index 35408c8..0434cfe 100644 --- a/field/packer_unpacker.go +++ b/field/packer_unpacker.go @@ -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) } @@ -73,7 +73,7 @@ func (p Track2Packer) Pack(value []byte, spec *Spec) ([]byte, error) { // Encode the length to that of the original string, not the potentially // padded length - packedLength, err := spec.Pref.EncodeLength(spec.Length, len(value)) + packedLength, err := spec.Pref.EncodeLength(spec.Length, len(packed)) if err != nil { return nil, fmt.Errorf("failed to encode length: %w", err) } diff --git a/field/packer_unpacker_test.go b/field/packer_unpacker_test.go index 565c8af..762a2b8 100644 --- a/field/packer_unpacker_test.go +++ b/field/packer_unpacker_test.go @@ -184,3 +184,25 @@ 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) + + _, err = str.Unpack(packed) + require.NoError(t, err) + bytes, err := str.Bytes() + require.NoError(t, err) + require.Equal(t, data, bytes) +}