-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutil.go
133 lines (114 loc) · 2.59 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
// Copyright 2022 Alim Zanibekov
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.
package teltonika
import (
"encoding/binary"
"io"
)
func genCrc16IBMLookupTable() []uint16 {
table := make([]uint16, 256)
i := 128
crc := uint16(1)
for i > 0 {
if (crc & 0x0001) == 1 {
crc = (crc >> 1) ^ 0xA001
} else {
crc = crc >> 1
}
for j := 0; j < 255; j += i * 2 {
table[i+j] = crc ^ table[j]
}
i = i >> 1
}
return table
}
var crc16IBMLookupTable = genCrc16IBMLookupTable()
func Crc16IBM(data []byte) uint16 {
crc := uint16(0)
size := len(data)
for i := 0; i < size; i++ {
lookupIndex := (crc ^ uint16(data[i])) & 0xff
crc = (crc >> 8) ^ crc16IBMLookupTable[lookupIndex]
}
return crc
}
type byteReader struct {
pos int
size int
input []byte
allocOnRead bool
}
func newByteReader(input []byte, allocOnRead bool) *byteReader {
return &byteReader{input: input, size: len(input), allocOnRead: allocOnRead}
}
func (r *byteReader) ReadBytes(n int) ([]byte, error) {
if r.pos+n > r.size {
return nil, io.EOF
}
r.pos += n
if r.allocOnRead {
buf := make([]byte, n)
copy(buf, r.input[r.pos-n:r.pos])
return buf, nil
}
return r.input[r.pos-n : r.pos], nil
}
func (r *byteReader) ReadUInt8BE() (uint8, error) {
if r.pos+1 > r.size {
return 0, io.EOF
}
r.pos += 1
return r.input[r.pos-1], nil
}
func (r *byteReader) ReadInt8BE() (int8, error) {
if r.pos+1 > r.size {
return 0, io.EOF
}
r.pos += 1
return int8(r.input[r.pos-1]), nil
}
func (r *byteReader) ReadUInt16BE() (uint16, error) {
if r.pos+2 > r.size {
return 0, io.EOF
}
r.pos += 2
return binary.BigEndian.Uint16(r.input[r.pos-2:]), nil
}
func (r *byteReader) ReadInt16BE() (int16, error) {
if r.pos+2 > r.size {
return 0, io.EOF
}
r.pos += 2
return int16(binary.BigEndian.Uint16(r.input[r.pos-2:])), nil
}
func (r *byteReader) ReadUInt32BE() (uint32, error) {
if r.pos+4 > r.size {
return 0, io.EOF
}
r.pos += 4
return binary.BigEndian.Uint32(r.input[r.pos-4:]), nil
}
func (r *byteReader) ReadInt32BE() (int32, error) {
if r.pos+4 > r.size {
return 0, io.EOF
}
r.pos += 4
return int32(binary.BigEndian.Uint32(r.input[r.pos-4:])), nil
}
func (r *byteReader) ReadUInt64BE() (uint64, error) {
if r.pos+8 > r.size {
return 0, io.EOF
}
r.pos += 8
return binary.BigEndian.Uint64(r.input[r.pos-8:]), nil
}
func (r *byteReader) ReadInt64BE() (int64, error) {
if r.pos+8 > r.size {
return 0, io.EOF
}
r.pos += 8
return int64(binary.BigEndian.Uint64(r.input[r.pos-8:])), nil
}