Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: generate length prefix with encoded data #340

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions 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 Expand Up @@ -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)
}
Expand Down
22 changes: 22 additions & 0 deletions field/packer_unpacker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}