forked from Teamwork/tnef
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
173 lines (146 loc) · 3.76 KB
/
util.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
package tnef
import (
"encoding/binary"
"bytes"
"unicode/utf16"
// "unicode/utf8"
// "strings"
// "fmt"
)
func byteToInt(data []byte) int {
var num int
var n uint
for _, b := range data {
num += (int(b) << n)
n += 8
}
return num
}
/*
func byteToUInt32(data []byte) uint32 {
return binary.LittleEndian.Uint32(data)
}*/
type LittleEndianReader struct {
}
func (c *LittleEndianReader) String(b []byte) (string) {
var v string
buf := bytes.NewReader(b)
binary.Read(buf, binary.LittleEndian, &v)
return v
}
// Int
func (c *LittleEndianReader) Int(b []byte) (int) {
var v int
buf := bytes.NewReader(b)
binary.Read(buf, binary.LittleEndian, &v)
return v
}
// UInt
func (c *LittleEndianReader) Uint(b []byte) (uint) {
var v uint
buf := bytes.NewReader(b)
binary.Read(buf, binary.LittleEndian, &v)
return v
}
// Int = Int32
func (c *LittleEndianReader) Int32(b []byte) (int32) {
var v int32
buf := bytes.NewReader(b)
binary.Read(buf, binary.LittleEndian, &v)
return v
}
// UInt = UInt32
func (c *LittleEndianReader) Uint32(b []byte) (uint32) {
var v uint32
buf := bytes.NewReader(b)
binary.Read(buf, binary.LittleEndian, &v)
return v
}
// int64
func (c *LittleEndianReader) Int64(b []byte) (int64) {
var v int64
buf := bytes.NewReader(b)
binary.Read(buf, binary.LittleEndian, &v)
return v
}
// uint64
func (c *LittleEndianReader) Uint64(b []byte) (uint64) {
var v uint64
buf := bytes.NewReader(b)
binary.Read(buf, binary.LittleEndian, &v)
return v
}
func (c *LittleEndianReader) Int16(b []byte) (int16) {
var v int16
buf := bytes.NewReader(b)
binary.Read(buf, binary.LittleEndian, &v)
return v
}
func (c *LittleEndianReader) Uint16(b []byte) (uint16) {
var v uint16
buf := bytes.NewReader(b)
binary.Read(buf, binary.LittleEndian, &v)
return v
}
func (c *LittleEndianReader) Int8(b []byte) (int8) {
var v int8
buf := bytes.NewReader(b)
binary.Read(buf, binary.LittleEndian, &v)
return v
}
func (c *LittleEndianReader) Uint8(b []byte) (uint8) {
var v uint8
buf := bytes.NewReader(b)
binary.Read(buf, binary.LittleEndian, &v)
return v
}
func (c *LittleEndianReader) Float32(b []byte) (float32) {
var v float32
buf := bytes.NewReader(b)
binary.Read(buf, binary.LittleEndian, &v)
return v
}
func (c *LittleEndianReader) Float64(b []byte) (float64) {
var v float64
buf := bytes.NewReader(b)
binary.Read(buf, binary.LittleEndian, &v)
return v
}
func (c *LittleEndianReader) Boolean(b []byte) (bool) {
var v bool
buf := bytes.NewReader(b)
binary.Read(buf, binary.LittleEndian, &v)
return v
}
// read utf16 little endian
func (c *LittleEndianReader) Utf16(content []byte, maxBytesToRead int) (convertedStringToUnicode string, bytesRead int) {
tmp := []uint16{}
bytesRead = 0
//last2Chars := 0
for {
tmp = append(tmp, binary.LittleEndian.Uint16(content[bytesRead:]))
bytesRead += 2
convertedStringToUnicode = string(utf16.Decode(tmp));
/*
if strings.HasSuffix(convertedStringToUnicode, "\x00") {
last2Chars++
} else {
last2Chars = 0
}
*/
// utf8.RuneCountInString(convertedStringToUnicode) -> number of chars
// len(convertedStringToUnicode) - number of bytes
// fmt.Printf("\r\nRead %v bytes from %v bytes; String Len: %v; Rune Count: %v", bytesRead, len(content), len(convertedStringToUnicode), utf8.RuneCountInString(convertedStringToUnicode))
// fmt.Printf("\r\nString: %s", convertedStringToUnicode)
if (len(content) <= bytesRead || bytesRead >= maxBytesToRead) {
/*
if (last2Chars >= 2) {
convertedStringToUnicode = strings.TrimRight(convertedStringToUnicode, "\x00")
}*/
//fmt.Printf("\r\n Return Read: %v", bytesRead)
break
}
//fmt.Printf("\r\n Read: %v from maxLengthToRead %v len: %v str: %v", bytesRead, maxBytesToRead, len(convertedStringToUnicode), convertedStringToUnicode)
}
return
}