-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenc.go
executable file
·77 lines (59 loc) · 1.76 KB
/
enc.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
package uu
const (
//MaxBytesPerLine is maximum bytes allowed in per line
MaxBytesPerLine = 45
//UUCharStart is integer representation of first allowed Uuencode char
//Equvalent to 'space' in ASCII table
UUCharStart = 0x20
//UUCharEnd is the integer representation of last allowed Uuencode char
//Equivalent to '`' in ASCII table
UUCharEnd = 0x60
//UUCharPseudoZero represents pseudo zero
UUCharPseudoZero = 0x60
//PCharNewline is integer representation of newline character
PCharNewline = 0x0A
//PCharCR is integer representation of carriage return character
PCharCR = 0x0D
)
//ToUUPack converts 3 bytes into UUPack
func ToUUPack(aBytes [3]byte) [4]byte {
lRet := [4]byte{}
lInt32 := uint32(0)
for cIdx := 0; cIdx < len(aBytes); cIdx++ {
lInt32 <<= 8
lInt32 |= uint32(aBytes[cIdx])
}
for cIdx := 0; cIdx < len(lRet); cIdx++ {
lRet[len(lRet)-1-cIdx] = UUCharStart + byte(lInt32&0x3F)
lInt32 >>= 6
}
return lRet
}
//EncodeLine encodes the given bytes into Uuencode line
func EncodeLine(aBytes []byte) []byte {
if len(aBytes) > MaxBytesPerLine {
return nil
}
if len(aBytes) == 0 {
return []byte{UUCharPseudoZero, PCharCR, PCharNewline}
}
var lRet []byte
lRet = append(lRet, byte(UUCharStart+len(aBytes)))
for cIdx := 0; cIdx < (len(aBytes) / 3); cIdx++ {
bIn := [3]byte{}
copy(bIn[:], aBytes[cIdx*3:((cIdx+1)*3)])
bOut := ToUUPack(bIn)
lRet = append(lRet, bOut[0], bOut[1], bOut[2], bOut[3])
}
{
bLeft := len(aBytes) % 3
if bLeft > 0 {
bIn := [3]byte{}
copy(bIn[0:bLeft], aBytes[len(aBytes)-bLeft:])
bOut := ToUUPack(bIn)
lRet = append(lRet, bOut[0], bOut[1], bOut[2], bOut[3])
}
}
lRet = append(lRet, PCharCR, PCharNewline)
return lRet
}