Skip to content

Commit

Permalink
fix errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mfdeveloper508 committed Oct 17, 2023
1 parent 7429700 commit 0af6905
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 29 deletions.
118 changes: 94 additions & 24 deletions imageViewData.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,71 +158,141 @@ func (ivData *ImageViewData) setRecordType() {
}

// ParseAndDecode takes the input record string, decodes all except image and parses the ImageViewData values
func (ivData *ImageViewData) ParseAndDecode(record string, decode DecodeLineFn) {
func (ivData *ImageViewData) ParseAndDecode(record string, decode DecodeLineFn) error {
// record contains binary data that may not map to UTF-8 charmap
recordLength := len(record)
if recordLength < 105 {
return // line too short
return fmt.Errorf("parse record is short, got %d, expected length is 105", recordLength) // line too short
}
// Character position 1-2, Always "52"
ivData.setRecordType()
// 03-11
ivData.EceInstitutionRoutingNumber = ivData.parseStringField(decode(record[2:11]))
lineOut, err := decode(record[2:11])
if err != nil {
return err
}
ivData.EceInstitutionRoutingNumber = ivData.parseStringField(lineOut)
// 12-19
ivData.BundleBusinessDate = ivData.parseYYYYMMDDDate(decode(record[11:19]))
lineOut, err = decode(record[11:19])
if err != nil {
return err
}
ivData.BundleBusinessDate = ivData.parseYYYYMMDDDate(lineOut)
// 20-21
ivData.CycleNumber = ivData.parseStringField(decode(record[19:21]))
lineOut, err = decode(record[19:21])
if err != nil {
return err
}
ivData.CycleNumber = ivData.parseStringField(lineOut)
// 22-36
ivData.EceInstitutionItemSequenceNumber = ivData.parseStringField(decode(record[21:36]))
lineOut, err = decode(record[21:36])
if err != nil {
return err
}
ivData.EceInstitutionItemSequenceNumber = ivData.parseStringField(lineOut)
// 37-52
ivData.SecurityOriginatorName = ivData.parseStringField(decode(record[36:52]))
lineOut, err = decode(record[36:52])
if err != nil {
return err
}
ivData.SecurityOriginatorName = ivData.parseStringField(lineOut)
// 53-68
ivData.SecurityAuthenticatorName = ivData.parseStringField(decode(record[52:68]))
lineOut, err = decode(record[52:68])
if err != nil {
return err
}
ivData.SecurityAuthenticatorName = ivData.parseStringField(lineOut)
// 69-84
ivData.SecurityKeyName = ivData.parseStringField(decode(record[68:84]))
lineOut, err = decode(record[68:84])
if err != nil {
return err
}
ivData.SecurityKeyName = ivData.parseStringField(lineOut)
// 85-85
ivData.ClippingOrigin = ivData.parseNumField(decode(record[84:85]))
lineOut, err = decode(record[84:85])
if err != nil {
return err
}
ivData.ClippingOrigin = ivData.parseNumField(lineOut)
// 86-89
ivData.ClippingCoordinateH1 = ivData.parseStringField(decode(record[85:89]))
lineOut, err = decode(record[85:89])
if err != nil {
return err
}
ivData.ClippingCoordinateH1 = ivData.parseStringField(lineOut)
// 90-93
ivData.ClippingCoordinateH2 = ivData.parseStringField(decode(record[89:93]))
lineOut, err = decode(record[89:93])
if err != nil {
return err
}
ivData.ClippingCoordinateH2 = ivData.parseStringField(lineOut)
// 94-97
ivData.ClippingCoordinateV1 = ivData.parseStringField(decode(record[93:97]))
lineOut, err = decode(record[93:97])
if err != nil {
return err
}
ivData.ClippingCoordinateV1 = ivData.parseStringField(lineOut)
// 98-101
ivData.ClippingCoordinateV2 = ivData.parseStringField(decode(record[97:101]))
lineOut, err = decode(record[97:101])
if err != nil {
return err
}
ivData.ClippingCoordinateV2 = ivData.parseStringField(lineOut)
// 102-105
ivData.LengthImageReferenceKey = ivData.parseStringField(decode(record[101:105]))
lineOut, err = decode(record[101:105])
if err != nil {
return err
}
ivData.LengthImageReferenceKey = ivData.parseStringField(lineOut)

lirk := ivData.parseNumField(ivData.LengthImageReferenceKey)
if lirk < 0 || recordLength < 110+lirk {
return // line too short
return nil // line too short
}

// 106 - (105+X)
ivData.ImageReferenceKey = ivData.parseStringField(decode(record[105 : 105+lirk]))
lineOut, err = decode(record[105 : 105+lirk])
if err != nil {
return err
}
ivData.ImageReferenceKey = ivData.parseStringField(lineOut)
// (106 + lirk) – (110 + lirk)
ivData.LengthDigitalSignature = ivData.parseStringField(decode(record[105+lirk : 110+lirk]))
lineOut, err = decode(record[105+lirk : 110+lirk])
if err != nil {
return err
}
ivData.LengthDigitalSignature = ivData.parseStringField(lineOut)

lds := ivData.parseNumField(ivData.LengthDigitalSignature)
if lds < 0 || recordLength < 117+lirk+lds {
return // line too short
return nil // line too short
}
// (111 + lirk) – (110 + lirk + lds)
ivData.DigitalSignature = ivData.stringToBytesField(decode(record[110+lirk : 110+lirk+lds]))
lineOut, err = decode(record[110+lirk : 110+lirk+lds])
if err != nil {
return err
}
ivData.DigitalSignature = ivData.stringToBytesField(lineOut)
// (111 + lirk + lds) – (117 + lirk + lds)
ivData.LengthImageData = ivData.parseStringField(decode(record[110+lirk+lds : 117+lirk+lds]))
lineOut, err = decode(record[110+lirk+lds : 117+lirk+lds])
if err != nil {
return err
}
ivData.LengthImageData = ivData.parseStringField(lineOut)

lid := ivData.parseNumField(ivData.LengthImageData)
if lid < 0 || recordLength < 117+lirk+lds+lid {
return // line too short
return nil // line too short
}
// (118 + lirk + lds) – (117+lirk + lds + lid)
ivData.ImageData = ivData.stringToBytesField(record[117+lirk+lds : 117+lirk+lds+lid])

return nil
}

// Parse takes the input record string and parses the ImageViewData values
func (ivData *ImageViewData) Parse(record string) {
ivData.ParseAndDecode(record, Passthrough)
func (ivData *ImageViewData) Parse(record string) error {
return ivData.ParseAndDecode(record, Passthrough)
}

func (ivData *ImageViewData) UnmarshalJSON(data []byte) error {
Expand Down
14 changes: 9 additions & 5 deletions reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ func (r *Reader) ImageViewData() error {
}

ivData := NewImageViewData()
ivData.ParseAndDecode(r.line, r.decodeLine)
ivData.Parse(lineOut)
if err := ivData.Validate(); err != nil {
return r.error(err)
}
Expand All @@ -686,7 +686,7 @@ func (r *Reader) ImageViewData() error {
}

ivData := NewImageViewData()
ivData.ParseAndDecode(r.line, r.decodeLine)
ivData.Parse(lineOut)
if err := ivData.Validate(); err != nil {
return r.error(err)
}
Expand Down Expand Up @@ -797,7 +797,13 @@ func (r *Reader) parseBundleControl() error {
if r.currentCashLetter.currentBundle == nil || r.currentCashLetter.currentBundle.BundleControl == nil {
return r.error(&FileError{Msg: msgFileBundleControl})
}
r.currentCashLetter.currentBundle.GetControl().Parse(r.decodeLine(r.line))

lineOut, err := r.decodeLine(r.line)
if err != nil {
return err
}

r.currentCashLetter.currentBundle.GetControl().Parse(lineOut)
if err := r.currentCashLetter.currentBundle.GetControl().Validate(); err != nil {
return r.error(err)
}
Expand Down Expand Up @@ -852,12 +858,10 @@ func (r *Reader) parseFileControl() error {
// Can be only one file control per file
return r.error(&FileError{Msg: msgFileControl})
}

lineOut, err := r.decodeLine(r.line)
if err != nil {
return err
}

r.File.Control.Parse(lineOut)
// Ensure valid FileControl
if err := r.File.Control.Validate(); err != nil {
Expand Down
7 changes: 7 additions & 0 deletions reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -861,3 +861,10 @@ func TestICLFile_LargeCheckImage(t *testing.T) {
_, err = r.Read()
require.NoError(t, err)
}

func Test_DecodeEBCDIC(t *testing.T) {
testData := []byte{0xF0, 0xF1, 0xF2}
decoded, err := DecodeEBCDIC(string(testData))
require.NoError(t, err)
require.Equal(t, "012", decoded)
}

0 comments on commit 0af6905

Please sign in to comment.