forked from moov-io/iso8583
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathl8var.go
102 lines (91 loc) · 2.28 KB
/
l8var.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright 2020 The Moov Authors
// Use of this source code is governed by an Apache License
// license that can be found in the LICENSE file.
package iso8583
import (
"errors"
"fmt"
"strconv"
)
// L8var contains bytes in non-fixed length field, first 8 symbols of field contains length
type L8var struct {
Value []byte
}
// NewL8var create new L8var field
func NewL8var(val []byte) *L8var {
return &L8var{val}
}
// IsEmpty check Lllvar field for empty value
func (l *L8var) IsEmpty() bool {
return len(l.Value) == 0
}
// Bytes encode Lllvar field to bytes
func (l *L8var) Bytes(encoder, lenEncoder, length int) ([]byte, error) {
val, err := UTF8ToWindows1252(l.Value)
if err != nil {
return nil, err
}
if length != -1 && len(val) > length {
return nil, fmt.Errorf(ErrValueTooLong, "L8var", length, len(val))
}
if encoder != ASCII {
return nil, errors.New(ErrInvalidEncoder)
}
lenStr := fmt.Sprintf("%08d", len(val))
contentLen := []byte(lenStr)
var lenVal []byte
switch lenEncoder {
case ASCII:
lenVal = contentLen
if len(lenVal) > 8 {
return nil, errors.New(ErrInvalidLengthHead)
}
case rBCD:
fallthrough
case BCD:
lenVal = rbcd(contentLen)
if len(lenVal) > 7 || len(contentLen) > 8 {
return nil, errors.New(ErrInvalidLengthHead)
}
default:
return nil, errors.New(ErrInvalidLengthEncoder)
}
return append(lenVal, val...), nil
}
// Load decode Lllvar field from bytes
func (l *L8var) Load(raw []byte, encoder, lenEncoder, length int) (read int, err error) {
raw, err = UTF8ToWindows1252(raw)
if err != nil {
return 0, err
}
// parse length head:
var contentLen int
switch lenEncoder {
case ASCII:
read = 8
contentLen, err = strconv.Atoi(string(raw[:read]))
if err != nil {
return 0, errors.New(ErrParseLengthFailed + ": " + string(raw[:8]))
}
case rBCD:
fallthrough
case BCD:
read = 7
contentLen, err = strconv.Atoi(string(bcdr2Ascii(raw[:read], 8)))
if err != nil {
return 0, errors.New(ErrParseLengthFailed + ": " + string(raw[:8]))
}
default:
return 0, errors.New(ErrInvalidLengthEncoder)
}
if len(raw) < (read + contentLen) {
return 0, errors.New(ErrBadRaw)
}
// parse body:
l.Value = raw[read : read+contentLen]
read += contentLen
if encoder != ASCII {
return 0, errors.New(ErrInvalidEncoder)
}
return read, nil
}