-
Notifications
You must be signed in to change notification settings - Fork 1
/
read_frame_header.zig
369 lines (342 loc) · 14.7 KB
/
read_frame_header.zig
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
const std = @import("std");
const testing = std.testing;
const mem = std.mem;
pub const FrameType = enum(u1) {
General,
Skippable,
};
// returns the frame type, will always read 4 bytes if available
fn handleMagic(data: []const u8) !FrameType {
if (data.len < 4) {
return error.NotEnoughData;
}
const magic = mem.readIntSliceLittle(u32, data);
const general_magic: u32 = 0x184D2204;
if (magic == general_magic)
return FrameType.General;
const skippable_magic: u32 = 0x184D2A50; // up to 0x184D2A5F
if (magic & 0xFFFFFFF0 == skippable_magic)
return FrameType.Skippable;
return error.InvalidMagic;
}
pub const FrameFlags = struct {
Version: u2,
BlockIndependence: u1,
BlockChecksum: u1,
ContentSize: u1,
ContentChecksum: u1,
_reserved: u1,
DictionaryId: u1,
};
pub const BlockMaximumSize = enum(u3) {
_64KB = 4,
_256KB = 5,
_1MB = 6,
_4MB = 7,
};
pub const BlockData = struct {
_reserved: u1,
BlockMaximumSize: BlockMaximumSize,
_reserved2: u4,
};
pub const FrameDescriptor = struct {
flags: FrameFlags,
block_data: BlockData,
content_size: ?u64,
dictionary_id: ?u32,
header_checksum: u8,
};
pub const SkippableFrameDescriptor = struct {
size: u32,
};
pub const FrameHeader = union(FrameType) {
General: FrameDescriptor,
Skippable: SkippableFrameDescriptor,
};
fn readFrameFlags(data: u8) !FrameFlags {
var flags: FrameFlags = undefined;
flags.Version = @intCast(u2, (data & 0xC0) >> 6);
if (flags.Version != 1)
return error.UnsupportedVersion;
flags.BlockIndependence = @intCast(u1, (data & 0x20) >> 5);
flags.BlockChecksum = @intCast(u1, (data & 0x10) >> 4);
flags.ContentSize = @intCast(u1, (data & 0x08) >> 3);
flags.ContentChecksum = @intCast(u1, (data & 0x04) >> 2);
flags._reserved = @intCast(u1, (data & 0x02) >> 1);
if (flags._reserved != 0)
return error.UnableToDecode;
flags.DictionaryId = @intCast(u1, (data & 0x01) >> 0);
return flags;
}
fn readBlockData(data: u8) !BlockData {
var block: BlockData = undefined;
block._reserved = @intCast(u1, (data & 0x80) >> 7);
block._reserved2 = @intCast(u4, data & 0x0F);
if (block._reserved != 0 or block._reserved2 != 0)
return error.UnableToDecode;
block.BlockMaximumSize = @bitCast(BlockMaximumSize, @intCast(u3, (data & 0x70) >> 4));
if (block.BlockMaximumSize != BlockMaximumSize._256KB and
block.BlockMaximumSize != BlockMaximumSize._64KB and
block.BlockMaximumSize != BlockMaximumSize._1MB and
block.BlockMaximumSize != BlockMaximumSize._4MB)
return error.InvalidBlockSize;
return block;
}
// returns how much data is has read from the input
fn readFrameDescriptor(data: []const u8, frame_descriptor: *FrameDescriptor) !usize {
if (data.len < 3){
return error.NotEnoughData;
}
frame_descriptor.flags = try readFrameFlags(data[0]);
frame_descriptor.block_data = try readBlockData(data[1]);
var read = usize(2);
if (frame_descriptor.flags.ContentSize == 1) {
if (data.len < read + 9)
return error.NotEnoughData;
frame_descriptor.content_size = mem.readIntSliceLittle(u64, data[read..read + 8]);
read += 8;
}
if (frame_descriptor.flags.DictionaryId == 1) {
if (data.len < read + 5)
return error.NotEnoughData;
frame_descriptor.dictionary_id = mem.readIntSliceLittle(u32, data[read..read + 4]);
read += 4;
}
frame_descriptor.header_checksum = data[read];
read += 1;
return read;
}
// returns how many data is has read from the input
fn readSkippableFrameDescriptor(data: []const u8, frame_descriptor: *SkippableFrameDescriptor) !usize {
if (data.len < 4)
return error.NotEnoughData;
frame_descriptor.size = mem.readIntSliceLittle(u32, data[0..4]);
return 4;
}
// returns how many data is has read from the input
pub fn readFrameHeader(data: []const u8, frame_header: *FrameHeader) !usize {
const read = switch(try handleMagic(data)) {
FrameType.General => blk: {
frame_header.* = FrameHeader{ .General = undefined };
break: blk try readFrameDescriptor(data[4..], &frame_header.General);
},
FrameType.Skippable => blk: {
frame_header.* = FrameHeader{ .Skippable = undefined };
break: blk try readSkippableFrameDescriptor(data[4..], &frame_header.Skippable);
},
};
return read + 4;
}
pub fn readContentChecksum(data: []const u8) !u32 {
if (data.len < 4)
return error.NotEnoughData;
return mem.readIntSliceLittle(u32, data[0..4]);
}
test "magic" {
const dataShort = [3]u8{1,2,3};
testing.expectError(error.NotEnoughData, handleMagic(dataShort));
const dataInvalid = [4]u8{1,2,3,4};
testing.expectError(error.InvalidMagic, handleMagic(dataInvalid));
const dataCorrect = [4]u8{0x04, 0x22, 0x4D, 0x18};
testing.expectEqual(FrameType.General, try handleMagic(dataCorrect));
const dataCorrectAndMore = [8]u8{0x04, 0x22, 0x4D, 0x18, 1, 2, 3, 4};
testing.expectEqual(FrameType.General, try handleMagic(dataCorrectAndMore[0..4]));
inline for ([]u8{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}) |i| {
const dataSkippable = [4]u8{0x50 + i, 0x2A, 0x4D, 0x18};
testing.expectEqual(FrameType.Skippable, try handleMagic(dataSkippable[0..4]));
}
}
test "frame descriptor errors" {
const dataShort: [2]u8 = undefined;
var frame_descriptor: FrameDescriptor = undefined;
testing.expectError(error.NotEnoughData, readFrameDescriptor(dataShort, &frame_descriptor));
}
test "frame version" {
{
var frame_descriptor: FrameDescriptor = undefined;
const dataInvalidVersion = [3]u8{0x00, 0x00, 0x00};
testing.expectError(error.UnsupportedVersion, readFrameDescriptor(dataInvalidVersion, &frame_descriptor));
}
{
var frame_descriptor: FrameDescriptor = undefined;
const dataValidVersion = [3]u8{0x40, 0x40, 0x00};
_ = try readFrameDescriptor(dataValidVersion, &frame_descriptor);
testing.expectEqual(u2(0x01), frame_descriptor.flags.Version);
}
}
test "frame flags" {
{
var frame_descriptor: FrameDescriptor = undefined;
const data = [3]u8{0x42, 0x40, 0x00};
testing.expectError(error.UnableToDecode, readFrameDescriptor(data, &frame_descriptor));
}
{
var frame_descriptor: FrameDescriptor = undefined;
const data = [3]u8{0x40, 0x40, 0x00};
_ = try readFrameDescriptor(data, &frame_descriptor);
testing.expectEqual(u1(0), frame_descriptor.flags.BlockIndependence);
testing.expectEqual(u1(0), frame_descriptor.flags.BlockChecksum);
testing.expectEqual(u1(0), frame_descriptor.flags.ContentSize);
testing.expectEqual(u1(0), frame_descriptor.flags.ContentChecksum);
testing.expectEqual(u1(0), frame_descriptor.flags._reserved);
testing.expectEqual(u1(0), frame_descriptor.flags.DictionaryId);
}
{
var frame_descriptor: FrameDescriptor = undefined;
const data = [3]u8{0x60, 0x40, 0x00};
_ = try readFrameDescriptor(data, &frame_descriptor);
testing.expectEqual(u1(1), frame_descriptor.flags.BlockIndependence);
testing.expectEqual(u1(0), frame_descriptor.flags.BlockChecksum);
testing.expectEqual(u1(0), frame_descriptor.flags.ContentSize);
testing.expectEqual(u1(0), frame_descriptor.flags.ContentChecksum);
testing.expectEqual(u1(0), frame_descriptor.flags._reserved);
testing.expectEqual(u1(0), frame_descriptor.flags.DictionaryId);
}
{
var frame_descriptor: FrameDescriptor = undefined;
const data = [3]u8{0x50, 0x40, 0x00};
_ = try readFrameDescriptor(data, &frame_descriptor);
testing.expectEqual(u1(0), frame_descriptor.flags.BlockIndependence);
testing.expectEqual(u1(1), frame_descriptor.flags.BlockChecksum);
testing.expectEqual(u1(0), frame_descriptor.flags.ContentSize);
testing.expectEqual(u1(0), frame_descriptor.flags.ContentChecksum);
testing.expectEqual(u1(0), frame_descriptor.flags._reserved);
testing.expectEqual(u1(0), frame_descriptor.flags.DictionaryId);
}
{
var frame_descriptor: FrameDescriptor = undefined;
const data = [11]u8{0x48, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
_ = try readFrameDescriptor(data, &frame_descriptor);
testing.expectEqual(u1(0), frame_descriptor.flags.BlockIndependence);
testing.expectEqual(u1(0), frame_descriptor.flags.BlockChecksum);
testing.expectEqual(u1(1), frame_descriptor.flags.ContentSize);
testing.expectEqual(u1(0), frame_descriptor.flags.ContentChecksum);
testing.expectEqual(u1(0), frame_descriptor.flags._reserved);
testing.expectEqual(u1(0), frame_descriptor.flags.DictionaryId);
}
{
var frame_descriptor: FrameDescriptor = undefined;
const data = [3]u8{0x44, 0x40, 0x00};
_ = try readFrameDescriptor(data, &frame_descriptor);
testing.expectEqual(u1(0), frame_descriptor.flags.BlockIndependence);
testing.expectEqual(u1(0), frame_descriptor.flags.BlockChecksum);
testing.expectEqual(u1(0), frame_descriptor.flags.ContentSize);
testing.expectEqual(u1(1), frame_descriptor.flags.ContentChecksum);
testing.expectEqual(u1(0), frame_descriptor.flags.DictionaryId);
}
{
var frame_descriptor: FrameDescriptor = undefined;
const data = [7]u8{0x41, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00};
_ = try readFrameDescriptor(data, &frame_descriptor);
testing.expectEqual(u1(0), frame_descriptor.flags.BlockIndependence);
testing.expectEqual(u1(0), frame_descriptor.flags.BlockChecksum);
testing.expectEqual(u1(0), frame_descriptor.flags.ContentSize);
testing.expectEqual(u1(0), frame_descriptor.flags.ContentChecksum);
testing.expectEqual(u1(0), frame_descriptor.flags._reserved);
testing.expectEqual(u1(1), frame_descriptor.flags.DictionaryId);
}
}
test "block data" {
{
var frame_descriptor: FrameDescriptor = undefined;
const data = [3]u8{0x40, 0x00, 0x00};
testing.expectError(error.InvalidBlockSize, readFrameDescriptor(data, &frame_descriptor));
}
{
var frame_descriptor: FrameDescriptor = undefined;
const data = [3]u8{0x40, 0x80, 0x00};
testing.expectError(error.UnableToDecode, readFrameDescriptor(data, &frame_descriptor));
}
{
var frame_descriptor: FrameDescriptor = undefined;
for ([]u2{0,1,2,3}) |i| {
const data = [3]u8{0x40, u8(1) << i, 0x00};
testing.expectError(error.UnableToDecode, readFrameDescriptor(data, &frame_descriptor));
}
}
inline for (@typeInfo(BlockMaximumSize).Enum.fields) |field| {
var frame_descriptor: FrameDescriptor = undefined;
const data = [3]u8{0x40, u8(field.value) << 4, 0x00};
_ = try readFrameDescriptor(data, &frame_descriptor);
testing.expectEqual(@intToEnum(BlockMaximumSize, field.value), frame_descriptor.block_data.BlockMaximumSize);
}
}
test "Content Size" {
var frame_descriptor: FrameDescriptor = undefined;
const data = [11]u8{0x48, 0x40, 0xFE, 0xDE, 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12, 0x00};
_ = try readFrameDescriptor(data, &frame_descriptor);
testing.expectEqual(u64(0x123456789ABCDEFE), frame_descriptor.content_size.?);
}
test "Dictionary Id" {
var frame_descriptor: FrameDescriptor = undefined;
const data = [7]u8{0x41, 0x40, 0xFE, 0xDE, 0xBC, 0x9A, 0x00};
_ = try readFrameDescriptor(data, &frame_descriptor);
testing.expectEqual(u32(0x9ABCDEFE), frame_descriptor.dictionary_id.?);
}
test "Header Checksum" {
var frame_descriptor: FrameDescriptor = undefined;
const data = [3]u8{0x40, 0x40, 0xFE};
_ = try readFrameDescriptor(data, &frame_descriptor);
testing.expectEqual(u8(0xFE), frame_descriptor.header_checksum);
}
test "Content Size, Dictionary Id and Header Checksum" {
var frame_descriptor: FrameDescriptor = undefined;
const data = [15]u8{0x49, 0x40, 0xFE, 0xDE, 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12, 0x21, 0x43, 0x65, 0x87, 0xF1};
_ = try readFrameDescriptor(data, &frame_descriptor);
testing.expectEqual(u64(0x123456789ABCDEFE), frame_descriptor.content_size.?);
testing.expectEqual(u32(0x87654321), frame_descriptor.dictionary_id.?);
testing.expectEqual(u8(0xF1), frame_descriptor.header_checksum);
}
test "Bytes read" {
{
var frame_descriptor: FrameDescriptor = undefined;
const data = [3]u8{0x40, 0x40, 0xFE};
testing.expectEqual(usize(3), try readFrameDescriptor(data, &frame_descriptor));
}
{
var frame_descriptor: FrameDescriptor = undefined;
const data = [3]u8{0x40, 0x40, 0xFE} ++ []u8{0x00} ** 100;
testing.expectEqual(usize(3), try readFrameDescriptor(data, &frame_descriptor));
}
{
var frame_descriptor: FrameDescriptor = undefined;
const data = [2]u8{0x48, 0x40} ++ []u8{0x00} ** 100;
testing.expectEqual(usize(11), try readFrameDescriptor(data, &frame_descriptor));
}
{
var frame_descriptor: FrameDescriptor = undefined;
const data = [2]u8{0x41, 0x40} ++ []u8{0x00} ** 100;
testing.expectEqual(usize(7), try readFrameDescriptor(data, &frame_descriptor));
}
{
var frame_descriptor: FrameDescriptor = undefined;
const data = [2]u8{0x49, 0x40} ++ []u8{0x00} ** 100;
testing.expectEqual(usize(15), try readFrameDescriptor(data, &frame_descriptor));
}
}
test "Skippable frame" {
const data = [4]u8{0x01, 0x02, 0x03, 0x04};
var frame_descriptor: SkippableFrameDescriptor = undefined;
_ = try readSkippableFrameDescriptor(data, &frame_descriptor);
testing.expectEqual(u32(0x04030201), frame_descriptor.size);
const dataShort: [3]u8 = undefined;
testing.expectError(error.NotEnoughData, readSkippableFrameDescriptor(dataShort, &frame_descriptor));
}
test "Read Frame Header" {
{
const skippableData = [4]u8{0x50, 0x2A, 0x4D, 0x18} ++ [4]u8{0x01, 0x02, 0x03, 0x04};
var frame_header: FrameHeader = undefined;
const read = try readFrameHeader(skippableData, &frame_header);
testing.expectEqual(usize(8), read);
testing.expectEqual(FrameType.Skippable, FrameType(frame_header));
testing.expectEqual(u32(0x04030201), frame_header.Skippable.size);
}
{
var frame_header: FrameHeader = undefined;
const data = [4]u8{0x04, 0x22, 0x4D, 0x18} ++ [3]u8{0x40, 0x40, 0xFE};
const read = try readFrameHeader(data, &frame_header);
testing.expectEqual(usize(7), read);
testing.expectEqual(FrameType.General, FrameType(frame_header));
testing.expectEqual(u8(0xFE), frame_header.General.header_checksum);
}
}