-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathparser.c
508 lines (395 loc) · 8.92 KB
/
parser.c
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#define WARN_ON(cond) assert(!cond)
#define round_up(x, y) ((x + (y - 1)) & ~(y - 1))
#define __packed __attribute__((packed))
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef int64_t s64;
#define ERR_PTR(err) NULL
#define PTR_ERR(ptr) -EINVAL
#define IS_ERR(ptr) (ptr == NULL)
#define OSSERIALIZE_HDR 0xd3
enum os_otype {
OS_OTYPE_DICTIONARY = 1,
OS_OTYPE_ARRAY = 2,
OS_OTYPE_INT64 = 4,
OS_OTYPE_STRING = 9,
OS_OTYPE_BLOB = 10,
OS_OTYPE_BOOL = 11
};
struct os_tag {
unsigned int size : 24;
enum os_otype type : 5;
unsigned int padding : 2;
bool last : 1;
} __packed;
struct ctx {
void *blob;
u32 pos, len;
};
void *parse_bytes(struct ctx *ctx, size_t count)
{
void *ptr = ctx->blob + ctx->pos;
if (ctx->pos + count > ctx->len)
return ERR_PTR(-EINVAL);
ctx->pos += count;
return ptr;
}
u32 *parse_u32(struct ctx *ctx)
{
return parse_bytes(ctx, sizeof(u32));
}
struct os_tag *parse_tag(struct ctx *ctx)
{
struct os_tag *tag;
/* Align to 32-bits */
ctx->pos = round_up(ctx->pos, 4);
tag = parse_bytes(ctx, sizeof(struct os_tag));
if (IS_ERR(tag))
return tag;
if (tag->padding)
return ERR_PTR(-EINVAL);
return tag;
}
struct os_tag *parse_tag_type(struct ctx *ctx, enum os_otype type)
{
struct os_tag *tag = parse_tag(ctx);
if (IS_ERR(tag))
return tag;
if (tag->type != type)
return ERR_PTR(-EINVAL);
return tag;
}
int skip(struct ctx *handle)
{
struct os_tag *tag = parse_tag(handle);
int ret = 0;
if (IS_ERR(tag))
return PTR_ERR(tag);
switch (tag->type) {
case OS_OTYPE_DICTIONARY:
for (int i = 0; i < tag->size; ++i) {
ret |= skip(handle);
ret |= skip(handle);
}
return ret;
case OS_OTYPE_ARRAY:
for (int i = 0; i < tag->size; ++i)
ret |= skip(handle);
return ret;
case OS_OTYPE_INT64:
handle->pos += 8;
return 0;
case OS_OTYPE_STRING:
case OS_OTYPE_BLOB:
handle->pos += tag->size;
return 0;
case OS_OTYPE_BOOL:
return 0;
default:
return -EINVAL;
}
}
enum os_otype peek_type(struct ctx handle)
{
struct os_tag *tag = parse_tag(&handle);
return tag->type;
}
/* Caller must free */
char *parse_string(struct ctx *handle)
{
struct os_tag *tag = parse_tag_type(handle, OS_OTYPE_STRING);
const char *in;
char *out;
if (IS_ERR(tag))
return (void *) tag;
in = parse_bytes(handle, tag->size);
if (IS_ERR(in))
return (void *) in;
out = malloc(tag->size + 1);
memcpy(out, in, tag->size);
out[tag->size] = '\0';
return out;
}
int parse_int64(struct ctx *handle, s64 *value)
{
void *tag = parse_tag_type(handle, OS_OTYPE_INT64);
s64 *in;
if (IS_ERR(tag))
return PTR_ERR(tag);
in = parse_bytes(handle, sizeof(s64));
if (IS_ERR(in))
return PTR_ERR(in);
memcpy(value, in, sizeof(*value));
return 0;
}
int parse_bool(struct ctx *handle, bool *b)
{
struct os_tag *tag = parse_tag_type(handle, OS_OTYPE_BOOL);
if (IS_ERR(tag))
return PTR_ERR(tag);
*b = !!tag->size;
return 0;
}
struct iterator {
struct ctx *handle;
u32 idx;
u32 len;
};
int iterator_begin(struct ctx *handle, struct iterator *it, bool dictionary)
{
struct os_tag *tag;
enum os_otype type = dictionary ? OS_OTYPE_DICTIONARY : OS_OTYPE_ARRAY;
*it = (struct iterator) {
.handle = handle,
.idx = 0
};
tag = parse_tag_type(it->handle, type);
if (IS_ERR(tag))
return PTR_ERR(tag);
it->len = tag->size;
return 0;
}
#define foreach_in_array(handle, it) \
for (iterator_begin(handle, &it, false); it.idx < it.len; ++it.idx)
#define foreach_in_dict(handle, it) \
for (iterator_begin(handle, &it, true); it.idx < it.len; ++it.idx)
int parse(void *blob, size_t size, struct ctx *ctx)
{
u32 *header;
*ctx = (struct ctx) {
.blob = blob,
.len = size,
.pos = 0,
};
header = parse_u32(ctx);
if (IS_ERR(header))
return PTR_ERR(header);
if (*header != OSSERIALIZE_HDR)
return -EINVAL;
return 0;
}
void print_spaces(int indent) {
int spaces = indent * 4;
while(spaces--) putchar(' ');
}
int print_dict(struct ctx *handle, int indent);
int print_array(struct ctx *handle, int indent);
int print_value(struct ctx *handle, int indent)
{
int ret;
switch (peek_type(*handle)) {
case OS_OTYPE_DICTIONARY:
return print_dict(handle, indent);
case OS_OTYPE_ARRAY:
return print_array(handle, indent);
case OS_OTYPE_STRING:
{
char *val = parse_string(handle);
if (IS_ERR(val))
return PTR_ERR(val);
printf("\"%s\"", val);
free(val);
return 0;
}
case OS_OTYPE_BOOL:
{
bool b;
ret = parse_bool(handle, &b);
if (ret)
return ret;
printf("%s", b ? "true" : "false");
return 0;
}
case OS_OTYPE_INT64:
{
s64 v;
ret = parse_int64(handle, &v);
if (ret)
return ret;
printf("%ld", v);
return 0;
}
case OS_OTYPE_BLOB:
{
skip(handle);
printf("<blob>");
return 0;
}
default:
return -EINVAL;
}
}
int print_dict(struct ctx *handle, int indent)
{
int ret;
struct iterator it;
printf("{\n");
foreach_in_dict(handle, it) {
char *key = parse_string(it.handle);
if (IS_ERR(key))
return PTR_ERR(key);
print_spaces(indent + 1);
printf("\"%s\": ", key);
free(key);
ret = print_value(it.handle, indent + 1);
if (ret)
return ret;
printf(",\n");
}
print_spaces(indent);
printf("}");
return 0;
}
int print_array(struct ctx *handle, int indent)
{
int ret;
struct iterator it;
printf("[\n");
foreach_in_array(handle, it) {
print_spaces(indent + 1);
ret = print_value(it.handle, indent + 1);
if (ret)
return ret;
printf(",\n");
}
print_spaces(indent);
printf("]");
return 0;
}
struct dimension {
s64 total, front_porch, sync_width, back_porch, active;
s64 sync_rate, precise_sync_rate;
};
void print_dimension(struct dimension dim) {
printf(" (%f)", (float) dim.precise_sync_rate / 65536.0);
}
int parse_dimension(struct ctx *handle, struct dimension *dim)
{
struct iterator it;
int ret = 0;
foreach_in_dict(handle, it) {
char *key = parse_string(it.handle);
if (IS_ERR(key))
return PTR_ERR(handle);
if (!strcmp(key, "Active"))
ret = parse_int64(it.handle, &dim->active);
else if (!strcmp(key, "Total"))
ret = parse_int64(it.handle, &dim->total);
else if (!strcmp(key, "FrontPorch"))
ret = parse_int64(it.handle, &dim->front_porch);
else if (!strcmp(key, "BackPorch"))
ret = parse_int64(it.handle, &dim->back_porch);
else if (!strcmp(key, "SyncWidth"))
ret = parse_int64(it.handle, &dim->sync_width);
else if (!strcmp(key, "SyncRate"))
ret = parse_int64(it.handle, &dim->sync_rate);
else if (!strcmp(key, "PreciseSyncRate"))
ret = parse_int64(it.handle, &dim->precise_sync_rate);
else
skip(it.handle);
if (ret)
return ret;
}
return 0;
}
int parse_color_modes(struct ctx *handle, s64 *best_id)
{
struct iterator outer_it;
int ret = 0;
s64 best_score = -1;
*best_id = -1;
foreach_in_array(handle, outer_it) {
struct iterator it;
s64 score = -1, id = -1;
foreach_in_dict(handle, it) {
char *key = parse_string(it.handle);
if (IS_ERR(key))
return PTR_ERR(key);
if (!strcmp(key, "Score"))
ret = parse_int64(it.handle, &score);
else if (!strcmp(key, "ID"))
ret = parse_int64(it.handle, &id);
else
skip(it.handle);
if (ret)
return ret;
}
/* Skip partial entries */
if (score < 0 || id < 0)
continue;
//printf("\t%ld: %ld\n", id, score);
if (score > best_score) {
best_score = score;
*best_id = id;
}
}
return 0;
}
int parse_mode(struct ctx *handle)
{
int ret = 0;
struct iterator it;
struct dimension horiz, vert;
s64 id = -1;
s64 best_color_mode = -1;
bool is_preferred = false;
foreach_in_dict(handle, it) {
char *key = parse_string(it.handle);
if (IS_ERR(key))
return PTR_ERR(key);
if (!strcmp(key, "HorizontalAttributes"))
ret = parse_dimension(it.handle, &horiz);
else if (!strcmp(key, "VerticalAttributes"))
ret = parse_dimension(it.handle, &vert);
else if (!strcmp(key, "ColorModes"))
ret = parse_color_modes(it.handle, &best_color_mode);
else if (!strcmp(key, "IsPreferred"))
ret = parse_bool(it.handle, &is_preferred);
else if (!strcmp(key, "ID"))
ret = parse_int64(it.handle, &id);
else
skip(it.handle);
if (ret)
return ret;
}
printf("%ldx%ld", horiz.active, vert.active);
//print_dimension(horiz);
print_dimension(vert);
//printf("\n");
printf(" ID#%ld, best colour ID#%ld%s\n", id, best_color_mode, is_preferred ? " *" : "");
return 0;
}
int enumerate_modes(struct ctx *handle)
{
struct iterator it;
int ret;
foreach_in_array(handle, it) {
ret = parse_mode(it.handle);
if (ret)
return ret;
}
return 0;
}
int main(int argc, const char **argv) {
//FILE *fp = fopen("test.bin", "rb");
//FILE *fp = fopen("attributes.bin", "rb");
FILE *fp = fopen("timings.bin", "rb");
u8 dump[196616];
fread(dump, 1, sizeof(dump), fp);
fclose(fp);
struct ctx handle;
int ret;
ret = parse(dump, sizeof(dump), &handle);
printf("%u\n", ret);
enumerate_modes(&handle);
//print_value(&handle, 0);
return 0;
}