-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathfixedslicewriter.go
250 lines (224 loc) · 5.49 KB
/
fixedslicewriter.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package bits
import "encoding/binary"
// FixedSliceWriter - write numbers to a fixed []byte slice
type FixedSliceWriter struct {
accError error
buf []byte
off int
n int // current number of bits
v uint // current accumulated value for bits
}
// NewFixedSliceWriter - create writer around slice.
// The slice will not grow, but stay the same size.
// If too much data is written, there will be
// an accumuluated error. Can be retrieved with AccError()
func NewFixedSliceWriterFromSlice(data []byte) *FixedSliceWriter {
return &FixedSliceWriter{
buf: data,
off: 0,
n: 0,
v: 0,
accError: nil,
}
}
// NewSliceWriter - create slice writer with fixed size.
func NewFixedSliceWriter(size int) *FixedSliceWriter {
return &FixedSliceWriter{
buf: make([]byte, size),
off: 0,
n: 0,
v: 0,
accError: nil,
}
}
// Len - length of FixedSliceWriter buffer written. Same as Offset()
func (sw *FixedSliceWriter) Len() int {
return sw.off
}
// Capacity - max length of FixedSliceWriter buffer
func (sw *FixedSliceWriter) Capacity() int {
return len(sw.buf)
}
// Offset - offset for writing in FixedSliceWriter buffer
func (sw *FixedSliceWriter) Offset() int {
return sw.off
}
// Bytes - return buf up to what's written
func (sw *FixedSliceWriter) Bytes() []byte {
return sw.buf[:sw.off]
}
// AccError - return accumulated erro
func (sw *FixedSliceWriter) AccError() error {
return sw.accError
}
// WriteUint8 - write byte to slice
func (sw *FixedSliceWriter) WriteUint8(n byte) {
if sw.off+1 > len(sw.buf) {
sw.accError = ErrSliceWrite
return
}
sw.buf[sw.off] = n
sw.off++
}
// WriteUint16 - write uint16 to slice
func (sw *FixedSliceWriter) WriteUint16(n uint16) {
if sw.off+2 > len(sw.buf) {
sw.accError = ErrSliceWrite
return
}
binary.BigEndian.PutUint16(sw.buf[sw.off:], n)
sw.off += 2
}
// WriteInt16 - write int16 to slice
func (sw *FixedSliceWriter) WriteInt16(n int16) {
if sw.off+2 > len(sw.buf) {
sw.accError = ErrSliceWrite
return
}
binary.BigEndian.PutUint16(sw.buf[sw.off:], uint16(n))
sw.off += 2
}
// WriteUint24 - write uint24 to slice
func (sw *FixedSliceWriter) WriteUint24(n uint32) {
if sw.off+3 > len(sw.buf) {
sw.accError = ErrSliceWrite
return
}
sw.WriteUint8(byte(n >> 16))
sw.WriteUint16(uint16(n & 0xffff))
}
// WriteUint32 - write uint32 to slice
func (sw *FixedSliceWriter) WriteUint32(n uint32) {
if sw.off+4 > len(sw.buf) {
sw.accError = ErrSliceWrite
return
}
binary.BigEndian.PutUint32(sw.buf[sw.off:], n)
sw.off += 4
}
// WriteInt32 - write int32 to slice
func (sw *FixedSliceWriter) WriteInt32(n int32) {
if sw.off+4 > len(sw.buf) {
sw.accError = ErrSliceWrite
return
}
binary.BigEndian.PutUint32(sw.buf[sw.off:], uint32(n))
sw.off += 4
}
// WriteUint48 - write uint48
func (sw *FixedSliceWriter) WriteUint48(u uint64) {
if sw.off+6 > len(sw.buf) {
sw.accError = ErrSliceWrite
return
}
msb := uint16(u >> 32)
binary.BigEndian.PutUint16(sw.buf[sw.off:], msb)
sw.off += 2
lsb := uint32(u & 0xffffffff)
binary.BigEndian.PutUint32(sw.buf[sw.off:], lsb)
sw.off += 4
}
// WriteUint64 - write uint64 to slice
func (sw *FixedSliceWriter) WriteUint64(n uint64) {
if sw.off+8 > len(sw.buf) {
sw.accError = ErrSliceWrite
return
}
binary.BigEndian.PutUint64(sw.buf[sw.off:], n)
sw.off += 8
}
// WriteInt64 - write int64 to slice
func (sw *FixedSliceWriter) WriteInt64(n int64) {
if sw.off+8 > len(sw.buf) {
sw.accError = ErrSliceWrite
return
}
binary.BigEndian.PutUint64(sw.buf[sw.off:], uint64(n))
sw.off += 8
}
// WriteString - write string to slice with or without zero end
func (sw *FixedSliceWriter) WriteString(s string, addZeroEnd bool) {
nrNew := len(s)
if addZeroEnd {
nrNew++
}
if sw.off+nrNew > len(sw.buf) {
sw.accError = ErrSliceWrite
return
}
copy(sw.buf[sw.off:sw.off+len(s)], s)
sw.off += len(s)
if addZeroEnd {
sw.buf[sw.off] = 0
sw.off++
}
}
// WriteZeroBytes - write n byte of zeroes
func (sw *FixedSliceWriter) WriteZeroBytes(n int) {
if sw.off+n > len(sw.buf) {
sw.accError = ErrSliceWrite
return
}
for i := 0; i < n; i++ {
sw.buf[sw.off] = 0
sw.off++
}
}
// WriteBytes - write []byte
func (sw *FixedSliceWriter) WriteBytes(byteSlice []byte) {
if sw.off+len(byteSlice) > len(sw.buf) {
sw.accError = ErrSliceWrite
return
}
copy(sw.buf[sw.off:sw.off+len(byteSlice)], byteSlice)
sw.off += len(byteSlice)
}
// WriteUnityMatrix - write a unity matrix for mvhd or tkhd
func (sw *FixedSliceWriter) WriteUnityMatrix() {
if sw.off+36 > len(sw.buf) {
sw.accError = ErrSliceWrite
return
}
sw.WriteUint32(0x00010000) // = 1 fixed 16.16
sw.WriteUint32(0)
sw.WriteUint32(0)
sw.WriteUint32(0)
sw.WriteUint32(0x00010000) // = 1 fixed 16.16
sw.WriteUint32(0)
sw.WriteUint32(0)
sw.WriteUint32(0)
sw.WriteUint32(0x40000000) // = 1 fixed 2.30
}
func (sw *FixedSliceWriter) WriteBits(bits uint, n int) {
if sw.accError != nil {
return
}
sw.v <<= uint(n)
sw.v |= bits & Mask(n)
sw.n += n
for sw.n >= 8 {
b := byte((sw.v >> (uint(sw.n) - 8)) & Mask(8))
sw.WriteUint8(b)
sw.n -= 8
}
sw.v &= Mask(8)
}
// WriteFlag writes a flag as 1 bit.
func (sw *FixedSliceWriter) WriteFlag(f bool) {
bit := uint(0)
if f {
bit = 1
}
sw.WriteBits(bit, 1)
}
// FlushBits - write remaining bits to the underlying .Writer.
// bits will be left-shifted and zeros appended to fill up a byte.
func (sw *FixedSliceWriter) FlushBits() {
if sw.accError != nil {
return
}
if sw.n != 0 {
b := byte((sw.v << (8 - uint(sw.n))) & Mask(8))
sw.WriteUint8(b)
}
}