-
Notifications
You must be signed in to change notification settings - Fork 51
/
field.go
581 lines (529 loc) · 14.6 KB
/
field.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
package iso8583
import (
"errors"
"fmt"
"strconv"
"strings"
)
const (
// ASCII is ASCII encoding
ASCII = iota
// BCD is "left-aligned" BCD
BCD
// rBCD is "right-aligned" BCD with odd length (for ex. "643" as [6 67] == "0643"), only for Numeric, Llnumeric and Lllnumeric fields
rBCD
)
const (
ERR_INVALID_ENCODER string = "invalid encoder"
ERR_INVALID_LENGTH_ENCODER string = "invalid length encoder"
ERR_INVALID_LENGTH_HEAD string = "invalid length head"
ERR_MISSING_LENGTH string = "missing length"
ERR_VALUE_TOO_LONG string = "length of value is longer than definition; type=%s, def_len=%d, len=%d"
ERR_BAD_RAW string = "bad raw data"
ERR_PARSE_LENGTH_FAILED string = "parse length head failed"
)
// Iso8583Type interface for ISO 8583 fields
type Iso8583Type interface {
// Byte representation of current field.
Bytes(encoder, lenEncoder, length int) ([]byte, error)
// Load unmarshal byte value into Iso8583Type according to the
// specific arguments. It returns the number of bytes actually read.
Load(raw []byte, encoder, lenEncoder, length int) (int, error)
// IsEmpty check is field empty
IsEmpty() bool
}
// A Numeric contains numeric value only in fix length. It holds numeric
// value as a string. Supportted encoder are ascii, bcd and rbcd. Length is
// required for marshalling and unmarshalling.
type Numeric struct {
Value string
}
// NewNumeric create new Numeric field
func NewNumeric(val string) *Numeric {
return &Numeric{val}
}
// IsEmpty check Numeric field for empty value
func (n *Numeric) IsEmpty() bool {
return len(n.Value) == 0
}
// Bytes encode Numeric field to bytes
func (n *Numeric) Bytes(encoder, lenEncoder, length int) ([]byte, error) {
val := []byte(n.Value)
if length == -1 {
return nil, errors.New(ERR_MISSING_LENGTH)
}
// if encoder == rBCD then length can be, for example, 3,
// but value can be, for example, "0631" (after decode from rBCD, because BCD use 1 byte for 2 digits),
// and we can encode it only if first digit == 0
if (encoder == rBCD) &&
len(val) == (length+1) &&
(string(val[0:1]) == "0") {
// Cut value to length
val = val[1:len(val)]
}
if len(val) > length {
return nil, errors.New(fmt.Sprintf(ERR_VALUE_TOO_LONG, "Numeric", length, len(val)))
}
if len(val) < length {
val = append([]byte(strings.Repeat("0", length-len(val))), val...)
}
switch encoder {
case BCD:
return lbcd(val), nil
case rBCD:
return rbcd(val), nil
case ASCII:
return val, nil
default:
return nil, errors.New(ERR_INVALID_ENCODER)
}
}
// Load decode Numeric field from bytes
func (n *Numeric) Load(raw []byte, encoder, lenEncoder, length int) (int, error) {
if length == -1 {
return 0, errors.New(ERR_MISSING_LENGTH)
}
switch encoder {
case BCD:
l := (length + 1) / 2
if len(raw) < l {
return 0, errors.New(ERR_BAD_RAW)
}
n.Value = string(bcdl2Ascii(raw[:l], length))
return l, nil
case rBCD:
l := (length + 1) / 2
if len(raw) < l {
return 0, errors.New(ERR_BAD_RAW)
}
n.Value = string(bcdr2Ascii(raw[0:l], length))
return l, nil
case ASCII:
if len(raw) < length {
return 0, errors.New(ERR_BAD_RAW)
}
n.Value = string(raw[:length])
return length, nil
default:
return 0, errors.New(ERR_INVALID_ENCODER)
}
}
// An Alphanumeric contains alphanumeric value in fix length. The only
// supportted encoder is ascii. Length is required for marshalling and
// unmarshalling.
type Alphanumeric struct {
Value string
}
// NewAlphanumeric create new Alphanumeric field
func NewAlphanumeric(val string) *Alphanumeric {
return &Alphanumeric{Value: val}
}
// IsEmpty check Alphanumeric field for empty value
func (a *Alphanumeric) IsEmpty() bool {
return len(a.Value) == 0
}
// Bytes encode Alphanumeric field to bytes
func (a *Alphanumeric) Bytes(encoder, lenEncoder, length int) ([]byte, error) {
val := []byte(a.Value)
if length == -1 {
return nil, errors.New(ERR_MISSING_LENGTH)
}
if len(val) > length {
return nil, errors.New(fmt.Sprintf(ERR_VALUE_TOO_LONG, "Alphanumeric", length, len(val)))
}
if len(val) < length {
val = append([]byte(strings.Repeat(" ", length-len(val))), val...)
}
return val, nil
}
// Load decode Alphanumeric field from bytes
func (a *Alphanumeric) Load(raw []byte, encoder, lenEncoder, length int) (int, error) {
if length == -1 {
return 0, errors.New(ERR_MISSING_LENGTH)
}
if len(raw) < length {
return 0, errors.New(ERR_BAD_RAW)
}
a.Value = string(raw[:length])
return length, nil
}
// Binary contains binary value
type Binary struct {
Value []byte
FixLen int
}
// NewBinary create new Binary field
func NewBinary(d []byte) *Binary {
return &Binary{d, -1}
}
// IsEmpty check Binary field for empty value
func (b *Binary) IsEmpty() bool {
return len(b.Value) == 0
}
// Bytes encode Binary field to bytes
func (b *Binary) Bytes(encoder, lenEncoder, l int) ([]byte, error) {
length := l
if b.FixLen != -1 {
length = b.FixLen
}
if length == -1 {
return nil, errors.New(ERR_MISSING_LENGTH)
}
if len(b.Value) > length {
return nil, errors.New(fmt.Sprintf(ERR_VALUE_TOO_LONG, "Binary", length, len(b.Value)))
}
if len(b.Value) < length {
return append(b.Value, make([]byte, length-len(b.Value))...), nil
}
return b.Value, nil
}
// Load decode Binary field from bytes
func (b *Binary) Load(raw []byte, encoder, lenEncoder, length int) (int, error) {
if length == -1 {
return 0, errors.New(ERR_MISSING_LENGTH)
}
if len(raw) < length {
return 0, errors.New(ERR_BAD_RAW)
}
b.Value = raw[:length]
b.FixLen = length
return length, nil
}
// Llvar contains bytes in non-fixed length field, first 2 symbols of field contains length
type Llvar struct {
Value []byte
}
// NewLlvar create new Llvar field
func NewLlvar(val []byte) *Llvar {
return &Llvar{val}
}
// IsEmpty check Llvar field for empty value
func (l *Llvar) IsEmpty() bool {
return len(l.Value) == 0
}
// Bytes encode Llvar field to bytes
func (l *Llvar) Bytes(encoder, lenEncoder, length int) ([]byte, error) {
if length != -1 && len(l.Value) > length {
return nil, errors.New(fmt.Sprintf(ERR_VALUE_TOO_LONG, "Llvar", length, len(l.Value)))
}
if encoder != ASCII {
return nil, errors.New(ERR_INVALID_ENCODER)
}
lenStr := fmt.Sprintf("%02d", len(l.Value))
contentLen := []byte(lenStr)
var lenVal []byte
switch lenEncoder {
case ASCII:
lenVal = contentLen
if len(lenVal) > 2 {
return nil, errors.New(ERR_INVALID_LENGTH_HEAD)
}
case rBCD:
fallthrough
case BCD:
lenVal = rbcd(contentLen)
if len(lenVal) > 1 {
return nil, errors.New(ERR_INVALID_LENGTH_HEAD)
}
default:
return nil, errors.New(ERR_INVALID_LENGTH_ENCODER)
}
return append(lenVal, l.Value...), nil
}
// Load decode Llvar field from bytes
func (l *Llvar) Load(raw []byte, encoder, lenEncoder, length int) (read int, err error) {
// parse length head:
var contentLen int
switch lenEncoder {
case ASCII:
read = 2
contentLen, err = strconv.Atoi(string(raw[:read]))
if err != nil {
return 0, errors.New(ERR_PARSE_LENGTH_FAILED + ": " + string(raw[:2]))
}
case rBCD:
fallthrough
case BCD:
read = 1
contentLen, err = strconv.Atoi(string(bcdr2Ascii(raw[:read], 2)))
if err != nil {
return 0, errors.New(ERR_PARSE_LENGTH_FAILED + ": " + string(raw[0]))
}
default:
return 0, errors.New(ERR_INVALID_LENGTH_ENCODER)
}
if len(raw) < (read + contentLen) {
return 0, errors.New(ERR_BAD_RAW)
}
// parse body:
l.Value = raw[read : read+contentLen]
read += contentLen
if encoder != ASCII {
return 0, errors.New(ERR_INVALID_ENCODER)
}
return read, nil
}
// A Llnumeric contains numeric value only in non-fix length, contains length in first 2 symbols. It holds numeric
// value as a string. Supportted encoder are ascii, bcd and rbcd. Length is
// required for marshalling and unmarshalling.
type Llnumeric struct {
Value string
}
// NewLlnumeric create new Llnumeric field
func NewLlnumeric(val string) *Llnumeric {
return &Llnumeric{val}
}
// IsEmpty check Llnumeric field for empty value
func (l *Llnumeric) IsEmpty() bool {
return len(l.Value) == 0
}
// Bytes encode Llnumeric field to bytes
func (l *Llnumeric) Bytes(encoder, lenEncoder, length int) ([]byte, error) {
raw := []byte(l.Value)
if length != -1 && len(raw) > length {
return nil, errors.New(fmt.Sprintf(ERR_VALUE_TOO_LONG, "Llnumeric", length, len(raw)))
}
val := raw
switch encoder {
case ASCII:
case BCD:
val = lbcd(raw)
case rBCD:
val = rbcd(raw)
default:
return nil, errors.New(ERR_INVALID_ENCODER)
}
lenStr := fmt.Sprintf("%02d", len(raw)) // length of digital characters
contentLen := []byte(lenStr)
var lenVal []byte
switch lenEncoder {
case ASCII:
lenVal = contentLen
if len(lenVal) > 2 {
return nil, errors.New(ERR_INVALID_LENGTH_HEAD)
}
case rBCD:
fallthrough
case BCD:
lenVal = rbcd(contentLen)
if len(lenVal) > 1 || len(contentLen) > 3 {
return nil, errors.New(ERR_INVALID_LENGTH_HEAD)
}
default:
return nil, errors.New(ERR_INVALID_LENGTH_ENCODER)
}
return append(lenVal, val...), nil
}
// Load decode Llnumeric field from bytes
func (l *Llnumeric) Load(raw []byte, encoder, lenEncoder, length int) (read int, err error) {
// parse length head:
var contentLen int
switch lenEncoder {
case ASCII:
read = 2
contentLen, err = strconv.Atoi(string(raw[:read]))
if err != nil {
return 0, errors.New(ERR_PARSE_LENGTH_FAILED + ": " + string(raw[:2]))
}
case rBCD:
fallthrough
case BCD:
read = 1
contentLen, err = strconv.Atoi(string(bcdr2Ascii(raw[:read], 2)))
if err != nil {
return 0, errors.New(ERR_PARSE_LENGTH_FAILED + ": " + string(raw[0]))
}
default:
return 0, errors.New(ERR_INVALID_LENGTH_ENCODER)
}
// parse body:
switch encoder {
case ASCII:
if len(raw) < (read + contentLen) {
return 0, errors.New(ERR_BAD_RAW)
}
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(ERR_BAD_RAW)
}
l.Value = string(bcdl2Ascii(raw[read:read+bcdLen], contentLen))
read += bcdLen
default:
return 0, errors.New(ERR_INVALID_ENCODER)
}
return read, nil
}
// Lllvar contains bytes in non-fixed length field, first 3 symbols of field contains length
type Lllvar struct {
Value []byte
}
// NewLllvar create new Lllvar field
func NewLllvar(val []byte) *Lllvar {
return &Lllvar{val}
}
// IsEmpty check Lllvar field for empty value
func (l *Lllvar) IsEmpty() bool {
return len(l.Value) == 0
}
// Bytes encode Lllvar field to bytes
func (l *Lllvar) Bytes(encoder, lenEncoder, length int) ([]byte, error) {
if length != -1 && len(l.Value) > length {
return nil, errors.New(fmt.Sprintf(ERR_VALUE_TOO_LONG, "Lllvar", length, len(l.Value)))
}
if encoder != ASCII {
return nil, errors.New(ERR_INVALID_ENCODER)
}
lenStr := fmt.Sprintf("%03d", len(l.Value))
contentLen := []byte(lenStr)
var lenVal []byte
switch lenEncoder {
case ASCII:
lenVal = contentLen
if len(lenVal) > 3 {
return nil, errors.New(ERR_INVALID_LENGTH_HEAD)
}
case rBCD:
fallthrough
case BCD:
lenVal = rbcd(contentLen)
if len(lenVal) > 2 || len(contentLen) > 3 {
return nil, errors.New(ERR_INVALID_LENGTH_HEAD)
}
default:
return nil, errors.New(ERR_INVALID_LENGTH_ENCODER)
}
return append(lenVal, l.Value...), nil
}
// Load decode Lllvar field from bytes
func (l *Lllvar) 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, errors.New(ERR_PARSE_LENGTH_FAILED + ": " + string(raw[:3]))
}
case rBCD:
fallthrough
case BCD:
read = 2
contentLen, err = strconv.Atoi(string(bcdr2Ascii(raw[:read], 3)))
if err != nil {
return 0, errors.New(ERR_PARSE_LENGTH_FAILED + ": " + string(raw[:2]))
}
default:
return 0, errors.New(ERR_INVALID_LENGTH_ENCODER)
}
if len(raw) < (read + contentLen) {
return 0, errors.New(ERR_BAD_RAW)
}
// parse body:
l.Value = raw[read : read+contentLen]
read += contentLen
if encoder != ASCII {
return 0, errors.New(ERR_INVALID_ENCODER)
}
return read, nil
}
// 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 marshalling and unmarshalling.
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, errors.New(fmt.Sprintf(ERR_VALUE_TOO_LONG, "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(ERR_INVALID_ENCODER)
}
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(ERR_INVALID_LENGTH_HEAD)
}
case rBCD:
fallthrough
case BCD:
lenVal = rbcd(contentLen)
if len(lenVal) > 2 || len(contentLen) > 3 {
return nil, errors.New(ERR_INVALID_LENGTH_HEAD)
}
default:
return nil, errors.New(ERR_INVALID_LENGTH_ENCODER)
}
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, errors.New(ERR_PARSE_LENGTH_FAILED + ": " + string(raw[:3]))
}
case rBCD:
fallthrough
case BCD:
read = 2
contentLen, err = strconv.Atoi(string(bcdr2Ascii(raw[:read], 2)))
if err != nil {
return 0, errors.New(ERR_PARSE_LENGTH_FAILED + ": " + string(raw[:2]))
}
default:
return 0, errors.New(ERR_INVALID_LENGTH_ENCODER)
}
// parse body:
switch encoder {
case ASCII:
if len(raw) < (read + contentLen) {
return 0, errors.New(ERR_BAD_RAW)
}
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(ERR_BAD_RAW)
}
l.Value = string(bcdl2Ascii(raw[read:read+bcdLen], contentLen))
read += bcdLen
default:
return 0, errors.New(ERR_INVALID_ENCODER)
}
return read, nil
}