forked from moov-io/iso8583
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlllnumeric.go
115 lines (104 loc) · 2.73 KB
/
lllnumeric.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
103
104
105
106
107
108
109
110
111
112
113
114
115
// 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"
)
// A Lllnumeric contains numeric value only in non-fix length, contains length in first 3 symbols. It holds numeric
// value as a string. Supportted encoder are ascii, bcd and rbcd. Length is
// required for marshaling and unmarshaling.
type Lllnumeric struct {
Value string
}
// NewLllnumeric create new Lllnumeric field
func NewLllnumeric(val string) *Lllnumeric {
return &Lllnumeric{val}
}
// IsEmpty check Lllnumeric field for empty value
func (l *Lllnumeric) IsEmpty() bool {
return len(l.Value) == 0
}
// Bytes encode Lllnumeric field to bytes
func (l *Lllnumeric) Bytes(encoder, lenEncoder, length int) ([]byte, error) {
raw := []byte(l.Value)
if length != -1 && len(raw) > length {
return nil, fmt.Errorf(ErrValueTooLong, "Lllnumeric", length, len(raw))
}
val := raw
switch encoder {
case ASCII:
case BCD:
val = lbcd(raw)
case rBCD:
val = rbcd(raw)
default:
return nil, errors.New(ErrInvalidEncoder)
}
lenStr := fmt.Sprintf("%03d", len(raw)) // length of digital characters
contentLen := []byte(lenStr)
var lenVal []byte
switch lenEncoder {
case ASCII:
lenVal = contentLen
if len(lenVal) > 3 {
return nil, errors.New(ErrInvalidLengthHead)
}
case rBCD:
fallthrough
case BCD:
lenVal = rbcd(contentLen)
if len(lenVal) > 2 || len(contentLen) > 3 {
return nil, errors.New(ErrInvalidLengthHead)
}
default:
return nil, errors.New(ErrInvalidLengthEncoder)
}
return append(lenVal, val...), nil
}
// Load decode Lllnumeric field from bytes
func (l *Lllnumeric) Load(raw []byte, encoder, lenEncoder, length int) (read int, err error) {
// parse length head:
var contentLen int
switch lenEncoder {
case ASCII:
read = 3
contentLen, err = strconv.Atoi(string(raw[:read]))
if err != nil {
return 0, fmt.Errorf("%s: %s", ErrParseLengthFailed, string(raw[:3]))
}
case rBCD:
fallthrough
case BCD:
read = 2
contentLen, err = strconv.Atoi(string(bcdr2Ascii(raw[:read], 2)))
if err != nil {
return 0, fmt.Errorf("%s: %s", ErrParseLengthFailed, string(raw[:2]))
}
default:
return 0, errors.New(ErrInvalidLengthEncoder)
}
// parse body:
switch encoder {
case ASCII:
if len(raw) < (read + contentLen) {
return 0, errors.New(ErrBadRaw)
}
l.Value = string(raw[read : read+contentLen])
read += contentLen
case rBCD:
fallthrough
case BCD:
bcdLen := (contentLen + 1) / 2
if len(raw) < (read + bcdLen) {
return 0, errors.New(ErrBadRaw)
}
l.Value = string(bcdl2Ascii(raw[read:read+bcdLen], contentLen))
read += bcdLen
default:
return 0, errors.New(ErrInvalidEncoder)
}
return read, nil
}