-
Notifications
You must be signed in to change notification settings - Fork 1
/
dec.go
executable file
·87 lines (65 loc) · 1.77 KB
/
dec.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
package uu
import (
"github.com/tejainece/hexutils"
"math"
)
const (
//MaxBytesPerEncLine is maximum number of bytes allowed in an UULine
MaxBytesPerEncLine = 61
)
//DecodeUUByte decodes given UUByte into normal byte
func DecodeUUByte(aData byte) (byte, bool) {
if aData < UUCharStart || aData > UUCharEnd {
return 0, false
}
lRet := aData - UUCharStart
if lRet == 0x40 {
lRet = 0
}
return lRet, true
}
//DecodePack decodes given UUPack into byte array
func DecodePack(aBytes [4]byte) ([3]byte, bool) {
lRet := [3]byte{}
lInt32 := uint32(0)
for cIdx := 0; cIdx < len(aBytes); cIdx++ {
bByte, bErr := DecodeUUByte(aBytes[cIdx])
if !bErr {
return lRet, false
}
lInt32 <<= 6
lInt32 |= uint32(bByte)
}
lRet[2] = hexutils.GetByte0FromUInt32(lInt32)
lRet[1] = hexutils.GetByte1FromUInt32(lInt32)
lRet[0] = hexutils.GetByte2FromUInt32(lInt32)
return lRet, true
}
//DecodeLine decodes given UULine and returns data
func DecodeLine(aBytes []byte) ([]byte, error) {
if len(aBytes) > MaxBytesPerEncLine {
return nil, ErrInvalidLineLen
}
lLen, lSts := DecodeUUByte(aBytes[0])
if !lSts {
return nil, ErrInvalidData
}
lEncLen := int(math.Ceil(float64(lLen)/3)) * 4
//+3 because of length, termination CR, termination LF
lExpLineLen := lEncLen + 3
if len(aBytes) != lExpLineLen {
return nil, ErrInvalidDataLen
}
lRet := make([]byte, 0, lEncLen)
lBytes := aBytes[1 : len(aBytes)-2]
for cIdx := 0; cIdx < lEncLen; cIdx += 4 {
bTempBuf := [4]byte{}
copy(bTempBuf[:], lBytes[cIdx:cIdx+4])
bBytes, bErr := DecodePack(bTempBuf)
if !bErr {
return nil, ErrInvalidData
}
lRet = append(lRet, bBytes[0], bBytes[1], bBytes[2])
}
return lRet[:lLen], nil
}