forked from axigenmessaging/tnefdecoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlittle-endian.go
107 lines (87 loc) · 1.97 KB
/
little-endian.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
/**
* little endian decoder/reader
*/
package tnefdecoder
import (
"bytes"
"encoding/binary"
"unicode/utf16"
)
/**
* little endian reader
*/
type LittleEndianDecoder struct {
}
// read an int
func (r *LittleEndianDecoder) Int(data []byte) int {
var num int
var n uint
for _, b := range data {
num += (int(b) << n)
n += 8
}
return num
}
func (c *LittleEndianDecoder) String(b []byte) (string) {
var v string
buf := bytes.NewReader(b)
binary.Read(buf, binary.LittleEndian, &v)
return v
}
// Int = Int32
func (c *LittleEndianDecoder) Int32(b []byte) (int32) {
var v int32
buf := bytes.NewReader(b)
binary.Read(buf, binary.LittleEndian, &v)
return v
}
// UInt = UInt32
func (c *LittleEndianDecoder) Uint32(b []byte) (uint32) {
return binary.LittleEndian.Uint32(b)
}
// int64
func (c *LittleEndianDecoder) Int64(b []byte) (int64) {
return int64(c.Int(b))
}
// uint64
func (c *LittleEndianDecoder) Uint64(b []byte) (uint64) {
return binary.LittleEndian.Uint64(b)
}
func (c *LittleEndianDecoder) Int16(b []byte) (int16) {
return int16(c.Int(b))
}
func (c *LittleEndianDecoder) Uint16(b []byte) (uint16) {
return binary.LittleEndian.Uint16(b)
}
func (c *LittleEndianDecoder) Float32(b []byte) (float32) {
var v float32
buf := bytes.NewReader(b)
binary.Read(buf, binary.LittleEndian, &v)
return v
}
func (c *LittleEndianDecoder) Float64(b []byte) (float64) {
var v float64
buf := bytes.NewReader(b)
binary.Read(buf, binary.LittleEndian, &v)
return v
}
func (c *LittleEndianDecoder) 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 *LittleEndianDecoder) Utf16(content []byte) (convertedStringToUnicode string) {
tmp := []uint16{}
bytesRead := 0
for {
tmp = append(tmp, binary.LittleEndian.Uint16(content[bytesRead:]))
bytesRead += 2
convertedStringToUnicode = string(utf16.Decode(tmp));
if (len(content) <= bytesRead) {
break
}
}
return
}