-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer.go
executable file
·164 lines (128 loc) · 3.05 KB
/
buffer.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
/*
Copyright 2023 Milan Suk
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"crypto/sha256"
"encoding/binary"
"errors"
"fmt"
)
type TBuffer struct {
data []byte
size int64
pos int64
}
func NewTBuffer(src []byte) *TBuffer {
var buff TBuffer
if src == nil {
buff.data = make([]byte, 4) //pre alloc
} else {
buff.data = src
buff.size = int64(len(src))
}
return &buff
}
func NewTBufferCopy(src *TBuffer) *TBuffer {
var buff TBuffer
buff.pos = src.pos
buff.size = src.size
buff.data = make([]byte, len(src.data))
copy(buff.data, src.data)
return &buff
}
func (buff *TBuffer) Clear() {
buff.pos = 0
buff.size = 0
}
func TBuffer_sha256(data []byte) ([]byte, error) {
h := sha256.New()
_, err := h.Write(data)
if err != nil {
return nil, fmt.Errorf("TBuffer_sha256() failed: %w", err)
}
return h.Sum(nil), nil
}
func (buff *TBuffer) sha256(start int) ([]byte, error) {
return TBuffer_sha256(buff.data[start:buff.size])
}
func (buff *TBuffer) Resize(bytes int64) {
if bytes > int64(len(buff.data)) {
data := make([]byte, bytes*2)
copy(data, buff.data)
buff.data = data
}
}
func (buff *TBuffer) WriteUint8(value byte) {
buff.Resize(buff.size + 1)
buff.data[buff.size] = value
buff.size++
}
func (buff *TBuffer) WriteSBlob(value []byte) {
len := int64(len(value))
buff.Resize(buff.size + len)
copy(buff.data[buff.size:], value)
buff.size += len
}
func (buff *TBuffer) ReadByte() (byte, error) {
if buff.pos+1 <= buff.size {
value := buff.data[buff.pos]
buff.pos++
return value, nil
}
return 0, errors.New("ReadByte() is is of buffer")
}
func (buff *TBuffer) ReadNumber() (int64, error) {
mask := uint8(buff.data[buff.pos])
buff.pos++
var t [8]byte
for i := 0; i < 8; i++ {
if mask&(1<<i) != 0 {
if buff.pos+1 <= buff.size {
t[i] = buff.data[buff.pos]
buff.pos++
} else {
return 0, errors.New("ReadNumber() is is of buffer")
}
}
}
return int64(binary.LittleEndian.Uint64(t[:])), nil
}
func (buff *TBuffer) ReadSBlob(data []byte, len int64) error {
if buff.pos+len <= buff.size {
copy(data, buff.data[buff.pos:buff.pos+len])
buff.pos += len
return nil
}
return errors.New("ReadSBlob() is is of buffer")
}
func (buff *TBuffer) WriteNumber(value int64) {
var t [8]byte
binary.LittleEndian.PutUint64(t[:], uint64(value))
n := 0
mask := uint8(0)
for i := 0; i < 8; i++ {
if t[i] != 0 {
mask |= 1 << i
n++
}
}
buff.Resize(buff.size + int64(1+n))
buff.data[buff.size] = mask
buff.size += 1
for i := 0; i < 8; i++ {
if t[i] != 0 {
buff.data[buff.size] = t[i]
buff.size += 1
}
}
}