forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 2
/
nestegg.c
1947 lines (1590 loc) · 41.8 KB
/
nestegg.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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright © 2010 Mozilla Foundation
*
* This program is made available under an ISC-style license. See the
* accompanying file LICENSE for details.
*/
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "halloc.h"
#include "nestegg/nestegg.h"
/* EBML Elements */
#define ID_EBML 0x1a45dfa3
#define ID_EBML_VERSION 0x4286
#define ID_EBML_READ_VERSION 0x42f7
#define ID_EBML_MAX_ID_LENGTH 0x42f2
#define ID_EBML_MAX_SIZE_LENGTH 0x42f3
#define ID_DOCTYPE 0x4282
#define ID_DOCTYPE_VERSION 0x4287
#define ID_DOCTYPE_READ_VERSION 0x4285
/* Global Elements */
#define ID_VOID 0xec
#define ID_CRC32 0xbf
/* WebMedia Elements */
#define ID_SEGMENT 0x18538067
/* Seek Head Elements */
#define ID_SEEK_HEAD 0x114d9b74
#define ID_SEEK 0x4dbb
#define ID_SEEK_ID 0x53ab
#define ID_SEEK_POSITION 0x53ac
/* Info Elements */
#define ID_INFO 0x1549a966
#define ID_TIMECODE_SCALE 0x2ad7b1
#define ID_DURATION 0x4489
/* Cluster Elements */
#define ID_CLUSTER 0x1f43b675
#define ID_TIMECODE 0xe7
#define ID_BLOCK_GROUP 0xa0
#define ID_SIMPLE_BLOCK 0xa3
/* BlockGroup Elements */
#define ID_BLOCK 0xa1
#define ID_BLOCK_DURATION 0x9b
#define ID_REFERENCE_BLOCK 0xfb
/* Tracks Elements */
#define ID_TRACKS 0x1654ae6b
#define ID_TRACK_ENTRY 0xae
#define ID_TRACK_NUMBER 0xd7
#define ID_TRACK_UID 0x73c5
#define ID_TRACK_TYPE 0x83
#define ID_FLAG_ENABLED 0xb9
#define ID_FLAG_DEFAULT 0x88
#define ID_FLAG_LACING 0x9c
#define ID_TRACK_TIMECODE_SCALE 0x23314f
#define ID_LANGUAGE 0x22b59c
#define ID_CODEC_ID 0x86
#define ID_CODEC_PRIVATE 0x63a2
/* Video Elements */
#define ID_VIDEO 0xe0
#define ID_STEREO_MODE 0x53b8
#define ID_PIXEL_WIDTH 0xb0
#define ID_PIXEL_HEIGHT 0xba
#define ID_PIXEL_CROP_BOTTOM 0x54aa
#define ID_PIXEL_CROP_TOP 0x54bb
#define ID_PIXEL_CROP_LEFT 0x54cc
#define ID_PIXEL_CROP_RIGHT 0x54dd
#define ID_DISPLAY_WIDTH 0x54b0
#define ID_DISPLAY_HEIGHT 0x54ba
/* Audio Elements */
#define ID_AUDIO 0xe1
#define ID_SAMPLING_FREQUENCY 0xb5
#define ID_CHANNELS 0x9f
#define ID_BIT_DEPTH 0x6264
/* Cues Elements */
#define ID_CUES 0x1c53bb6b
#define ID_CUE_POINT 0xbb
#define ID_CUE_TIME 0xb3
#define ID_CUE_TRACK_POSITIONS 0xb7
#define ID_CUE_TRACK 0xf7
#define ID_CUE_CLUSTER_POSITION 0xf1
#define ID_CUE_BLOCK_NUMBER 0x5378
/* EBML Types */
enum ebml_type_enum {
TYPE_UNKNOWN,
TYPE_MASTER,
TYPE_UINT,
TYPE_FLOAT,
TYPE_INT,
TYPE_STRING,
TYPE_BINARY
};
#define LIMIT_STRING (1 << 20)
#define LIMIT_BINARY (1 << 24)
#define LIMIT_BLOCK (1 << 30)
#define LIMIT_FRAME (1 << 28)
/* Field Flags */
#define DESC_FLAG_NONE 0
#define DESC_FLAG_MULTI (1 << 0)
#define DESC_FLAG_SUSPEND (1 << 1)
#define DESC_FLAG_OFFSET (1 << 2)
/* Block Header Flags */
#define BLOCK_FLAGS_LACING 6
/* Lacing Constants */
#define LACING_NONE 0
#define LACING_XIPH 1
#define LACING_FIXED 2
#define LACING_EBML 3
/* Track Types */
#define TRACK_TYPE_VIDEO 1
#define TRACK_TYPE_AUDIO 2
/* Track IDs */
#define TRACK_ID_VP8 "V_VP8"
#define TRACK_ID_VORBIS "A_VORBIS"
enum vint_mask {
MASK_NONE,
MASK_FIRST_BIT
};
struct ebml_binary {
unsigned char * data;
size_t length;
};
struct ebml_list_node {
struct ebml_list_node * next;
uint64_t id;
void * data;
};
struct ebml_list {
struct ebml_list_node * head;
struct ebml_list_node * tail;
};
struct ebml_type {
union ebml_value {
uint64_t u;
double f;
int64_t i;
char * s;
struct ebml_binary b;
} v;
enum ebml_type_enum type;
int read;
};
/* EBML Definitions */
struct ebml {
struct ebml_type ebml_version;
struct ebml_type ebml_read_version;
struct ebml_type ebml_max_id_length;
struct ebml_type ebml_max_size_length;
struct ebml_type doctype;
struct ebml_type doctype_version;
struct ebml_type doctype_read_version;
};
/* Matroksa Definitions */
struct seek {
struct ebml_type id;
struct ebml_type position;
};
struct seek_head {
struct ebml_list seek;
};
struct info {
struct ebml_type timecode_scale;
struct ebml_type duration;
};
struct block_group {
struct ebml_type duration;
struct ebml_type reference_block;
};
struct cluster {
struct ebml_type timecode;
struct ebml_list block_group;
};
struct video {
struct ebml_type stereo_mode;
struct ebml_type pixel_width;
struct ebml_type pixel_height;
struct ebml_type pixel_crop_bottom;
struct ebml_type pixel_crop_top;
struct ebml_type pixel_crop_left;
struct ebml_type pixel_crop_right;
struct ebml_type display_width;
struct ebml_type display_height;
};
struct audio {
struct ebml_type sampling_frequency;
struct ebml_type channels;
struct ebml_type bit_depth;
};
struct track_entry {
struct ebml_type number;
struct ebml_type uid;
struct ebml_type type;
struct ebml_type flag_enabled;
struct ebml_type flag_default;
struct ebml_type flag_lacing;
struct ebml_type track_timecode_scale;
struct ebml_type language;
struct ebml_type codec_id;
struct ebml_type codec_private;
struct video video;
struct audio audio;
};
struct tracks {
struct ebml_list track_entry;
};
struct cue_track_positions {
struct ebml_type track;
struct ebml_type cluster_position;
struct ebml_type block_number;
};
struct cue_point {
struct ebml_type time;
struct ebml_list cue_track_positions;
};
struct cues {
struct ebml_list cue_point;
};
struct segment {
struct ebml_list seek_head;
struct info info;
struct ebml_list cluster;
struct tracks tracks;
struct cues cues;
};
/* Misc. */
struct pool_ctx {
char dummy;
};
struct list_node {
struct list_node * previous;
struct ebml_element_desc * node;
unsigned char * data;
};
struct saved_state {
int64_t stream_offset;
struct list_node * ancestor;
uint64_t last_id;
uint64_t last_size;
};
struct frame {
unsigned char * data;
size_t length;
struct frame * next;
};
/* Public (opaque) Structures */
struct nestegg {
nestegg_io * io;
nestegg_log log;
struct pool_ctx * alloc_pool;
uint64_t last_id;
uint64_t last_size;
struct list_node * ancestor;
struct ebml ebml;
struct segment segment;
int64_t segment_offset;
unsigned int track_count;
};
struct nestegg_packet {
uint64_t track;
uint64_t timecode;
struct frame * frame;
};
/* Element Descriptor */
struct ebml_element_desc {
char const * name;
uint64_t id;
enum ebml_type_enum type;
size_t offset;
unsigned int flags;
struct ebml_element_desc * children;
size_t size;
size_t data_offset;
};
#define E_FIELD(ID, TYPE, STRUCT, FIELD) \
{ #ID, ID, TYPE, offsetof(STRUCT, FIELD), DESC_FLAG_NONE, NULL, 0, 0 }
#define E_MASTER(ID, TYPE, STRUCT, FIELD) \
{ #ID, ID, TYPE, offsetof(STRUCT, FIELD), DESC_FLAG_MULTI, ne_ ## FIELD ## _elements, \
sizeof(struct FIELD), 0 }
#define E_SINGLE_MASTER_O(ID, TYPE, STRUCT, FIELD) \
{ #ID, ID, TYPE, offsetof(STRUCT, FIELD), DESC_FLAG_OFFSET, ne_ ## FIELD ## _elements, 0, \
offsetof(STRUCT, FIELD ## _offset) }
#define E_SINGLE_MASTER(ID, TYPE, STRUCT, FIELD) \
{ #ID, ID, TYPE, offsetof(STRUCT, FIELD), DESC_FLAG_NONE, ne_ ## FIELD ## _elements, 0, 0 }
#define E_SUSPEND(ID, TYPE) \
{ #ID, ID, TYPE, 0, DESC_FLAG_SUSPEND, NULL, 0, 0 }
#define E_LAST \
{ NULL, 0, 0, 0, DESC_FLAG_NONE, NULL, 0, 0 }
/* EBML Element Lists */
static struct ebml_element_desc ne_ebml_elements[] = {
E_FIELD(ID_EBML_VERSION, TYPE_UINT, struct ebml, ebml_version),
E_FIELD(ID_EBML_READ_VERSION, TYPE_UINT, struct ebml, ebml_read_version),
E_FIELD(ID_EBML_MAX_ID_LENGTH, TYPE_UINT, struct ebml, ebml_max_id_length),
E_FIELD(ID_EBML_MAX_SIZE_LENGTH, TYPE_UINT, struct ebml, ebml_max_size_length),
E_FIELD(ID_DOCTYPE, TYPE_STRING, struct ebml, doctype),
E_FIELD(ID_DOCTYPE_VERSION, TYPE_UINT, struct ebml, doctype_version),
E_FIELD(ID_DOCTYPE_READ_VERSION, TYPE_UINT, struct ebml, doctype_read_version),
E_LAST
};
/* WebMedia Element Lists */
static struct ebml_element_desc ne_seek_elements[] = {
E_FIELD(ID_SEEK_ID, TYPE_BINARY, struct seek, id),
E_FIELD(ID_SEEK_POSITION, TYPE_UINT, struct seek, position),
E_LAST
};
static struct ebml_element_desc ne_seek_head_elements[] = {
E_MASTER(ID_SEEK, TYPE_MASTER, struct seek_head, seek),
E_LAST
};
static struct ebml_element_desc ne_info_elements[] = {
E_FIELD(ID_TIMECODE_SCALE, TYPE_UINT, struct info, timecode_scale),
E_FIELD(ID_DURATION, TYPE_FLOAT, struct info, duration),
E_LAST
};
static struct ebml_element_desc ne_block_group_elements[] = {
E_SUSPEND(ID_BLOCK, TYPE_BINARY),
E_FIELD(ID_BLOCK_DURATION, TYPE_UINT, struct block_group, duration),
E_FIELD(ID_REFERENCE_BLOCK, TYPE_INT, struct block_group, reference_block),
E_LAST
};
static struct ebml_element_desc ne_cluster_elements[] = {
E_FIELD(ID_TIMECODE, TYPE_UINT, struct cluster, timecode),
E_MASTER(ID_BLOCK_GROUP, TYPE_MASTER, struct cluster, block_group),
E_SUSPEND(ID_SIMPLE_BLOCK, TYPE_BINARY),
E_LAST
};
static struct ebml_element_desc ne_video_elements[] = {
E_FIELD(ID_STEREO_MODE, TYPE_UINT, struct video, stereo_mode),
E_FIELD(ID_PIXEL_WIDTH, TYPE_UINT, struct video, pixel_width),
E_FIELD(ID_PIXEL_HEIGHT, TYPE_UINT, struct video, pixel_height),
E_FIELD(ID_PIXEL_CROP_BOTTOM, TYPE_UINT, struct video, pixel_crop_bottom),
E_FIELD(ID_PIXEL_CROP_TOP, TYPE_UINT, struct video, pixel_crop_top),
E_FIELD(ID_PIXEL_CROP_LEFT, TYPE_UINT, struct video, pixel_crop_left),
E_FIELD(ID_PIXEL_CROP_RIGHT, TYPE_UINT, struct video, pixel_crop_right),
E_FIELD(ID_DISPLAY_WIDTH, TYPE_UINT, struct video, display_width),
E_FIELD(ID_DISPLAY_HEIGHT, TYPE_UINT, struct video, display_height),
E_LAST
};
static struct ebml_element_desc ne_audio_elements[] = {
E_FIELD(ID_SAMPLING_FREQUENCY, TYPE_FLOAT, struct audio, sampling_frequency),
E_FIELD(ID_CHANNELS, TYPE_UINT, struct audio, channels),
E_FIELD(ID_BIT_DEPTH, TYPE_UINT, struct audio, bit_depth),
E_LAST
};
static struct ebml_element_desc ne_track_entry_elements[] = {
E_FIELD(ID_TRACK_NUMBER, TYPE_UINT, struct track_entry, number),
E_FIELD(ID_TRACK_UID, TYPE_UINT, struct track_entry, uid),
E_FIELD(ID_TRACK_TYPE, TYPE_UINT, struct track_entry, type),
E_FIELD(ID_FLAG_ENABLED, TYPE_UINT, struct track_entry, flag_enabled),
E_FIELD(ID_FLAG_DEFAULT, TYPE_UINT, struct track_entry, flag_default),
E_FIELD(ID_FLAG_LACING, TYPE_UINT, struct track_entry, flag_lacing),
E_FIELD(ID_TRACK_TIMECODE_SCALE, TYPE_FLOAT, struct track_entry, track_timecode_scale),
E_FIELD(ID_LANGUAGE, TYPE_STRING, struct track_entry, language),
E_FIELD(ID_CODEC_ID, TYPE_STRING, struct track_entry, codec_id),
E_FIELD(ID_CODEC_PRIVATE, TYPE_BINARY, struct track_entry, codec_private),
E_SINGLE_MASTER(ID_VIDEO, TYPE_MASTER, struct track_entry, video),
E_SINGLE_MASTER(ID_AUDIO, TYPE_MASTER, struct track_entry, audio),
E_LAST
};
static struct ebml_element_desc ne_tracks_elements[] = {
E_MASTER(ID_TRACK_ENTRY, TYPE_MASTER, struct tracks, track_entry),
E_LAST
};
static struct ebml_element_desc ne_cue_track_positions_elements[] = {
E_FIELD(ID_CUE_TRACK, TYPE_UINT, struct cue_track_positions, track),
E_FIELD(ID_CUE_CLUSTER_POSITION, TYPE_UINT, struct cue_track_positions, cluster_position),
E_FIELD(ID_CUE_BLOCK_NUMBER, TYPE_UINT, struct cue_track_positions, block_number),
E_LAST
};
static struct ebml_element_desc ne_cue_point_elements[] = {
E_FIELD(ID_CUE_TIME, TYPE_UINT, struct cue_point, time),
E_MASTER(ID_CUE_TRACK_POSITIONS, TYPE_MASTER, struct cue_point, cue_track_positions),
E_LAST
};
static struct ebml_element_desc ne_cues_elements[] = {
E_MASTER(ID_CUE_POINT, TYPE_MASTER, struct cues, cue_point),
E_LAST
};
static struct ebml_element_desc ne_segment_elements[] = {
E_MASTER(ID_SEEK_HEAD, TYPE_MASTER, struct segment, seek_head),
E_SINGLE_MASTER(ID_INFO, TYPE_MASTER, struct segment, info),
E_MASTER(ID_CLUSTER, TYPE_MASTER, struct segment, cluster),
E_SINGLE_MASTER(ID_TRACKS, TYPE_MASTER, struct segment, tracks),
E_SINGLE_MASTER(ID_CUES, TYPE_MASTER, struct segment, cues),
E_LAST
};
static struct ebml_element_desc ne_top_level_elements[] = {
E_SINGLE_MASTER(ID_EBML, TYPE_MASTER, nestegg, ebml),
E_SINGLE_MASTER_O(ID_SEGMENT, TYPE_MASTER, nestegg, segment),
E_LAST
};
#undef E_FIELD
#undef E_MASTER
#undef E_SINGLE_MASTER_O
#undef E_SINGLE_MASTER
#undef E_SUSPEND
#undef E_LAST
static struct pool_ctx *
ne_pool_init(void)
{
struct pool_ctx * pool;
pool = h_malloc(sizeof(*pool));
if (!pool)
abort();
return pool;
}
static void
ne_pool_destroy(struct pool_ctx * pool)
{
h_free(pool);
}
static void *
ne_pool_alloc(size_t size, struct pool_ctx * pool)
{
void * p;
p = h_malloc(size);
if (!p)
abort();
hattach(p, pool);
memset(p, 0, size);
return p;
}
static void *
ne_alloc(size_t size)
{
void * p;
p = calloc(1, size);
if (!p)
abort();
return p;
}
static int
ne_io_read(nestegg_io * io, void * buffer, size_t length)
{
return io->read(buffer, length, io->userdata);
}
static int
ne_io_seek(nestegg_io * io, int64_t offset, int whence)
{
return io->seek(offset, whence, io->userdata);
}
static int
ne_io_read_skip(nestegg_io * io, size_t length)
{
size_t get;
unsigned char buf[8192];
int r = 1;
while (length > 0) {
get = length < sizeof(buf) ? length : sizeof(buf);
r = ne_io_read(io, buf, get);
if (r != 1)
break;
length -= get;
}
return r;
}
static int64_t
ne_io_tell(nestegg_io * io)
{
return io->tell(io->userdata);
}
static int
ne_bare_read_vint(nestegg_io * io, uint64_t * value, uint64_t * length, enum vint_mask maskflag)
{
int r;
unsigned char b;
size_t maxlen = 8;
unsigned int count = 1, mask = 1 << 7;
r = ne_io_read(io, &b, 1);
if (r != 1)
return r;
while (count < maxlen) {
if ((b & mask) != 0)
break;
mask >>= 1;
count += 1;
}
if (length)
*length = count;
*value = b;
if (maskflag == MASK_FIRST_BIT)
*value = b & ~mask;
while (--count) {
r = ne_io_read(io, &b, 1);
if (r != 1)
return r;
*value <<= 8;
*value |= b;
}
return 1;
}
static int
ne_read_id(nestegg_io * io, uint64_t * value, uint64_t * length)
{
return ne_bare_read_vint(io, value, length, MASK_NONE);
}
static int
ne_read_vint(nestegg_io * io, uint64_t * value, uint64_t * length)
{
return ne_bare_read_vint(io, value, length, MASK_FIRST_BIT);
}
static int
ne_read_svint(nestegg_io * io, int64_t * value, uint64_t * length)
{
int r;
uint64_t uvalue;
uint64_t ulength;
int64_t svint_subtr[] = {
0x3f, 0x1fff,
0xfffff, 0x7ffffff,
0x3ffffffffLL, 0x1ffffffffffLL,
0xffffffffffffLL, 0x7fffffffffffffLL
};
r = ne_bare_read_vint(io, &uvalue, &ulength, MASK_FIRST_BIT);
if (r != 1)
return r;
*value = uvalue - svint_subtr[ulength - 1];
if (length)
*length = ulength;
return r;
}
static int
ne_read_uint(nestegg_io * io, uint64_t * val, uint64_t length)
{
unsigned char b;
int r;
if (length == 0 || length > 8)
return -1;
r = ne_io_read(io, &b, 1);
if (r != 1)
return r;
*val = b;
while (--length) {
r = ne_io_read(io, &b, 1);
if (r != 1)
return r;
*val <<= 8;
*val |= b;
}
return 1;
}
static int
ne_read_int(nestegg_io * io, int64_t * val, uint64_t length)
{
int r;
uint64_t uval, base;
r = ne_read_uint(io, &uval, length);
if (r != 1)
return r;
if (length < sizeof(int64_t)) {
base = 1;
base <<= length * 8 - 1;
if (uval >= base) {
base = 1;
base <<= length * 8;
} else {
base = 0;
}
*val = uval - base;
} else {
*val = (int64_t) uval;
}
return 1;
}
static int
ne_read_float(nestegg_io * io, double * val, uint64_t length)
{
union {
uint64_t u;
float f;
double d;
} value;
int r;
/* length == 10 not implemented */
if (length != 4 && length != 8)
return -1;
r = ne_read_uint(io, &value.u, length);
if (r != 1)
return r;
if (length == 4)
*val = value.f;
else
*val = value.d;
return 1;
}
static int
ne_read_string(nestegg * ctx, char ** val, uint64_t length)
{
char * str;
int r;
if (length == 0 || length > LIMIT_STRING)
return -1;
str = ne_pool_alloc(length + 1, ctx->alloc_pool);
r = ne_io_read(ctx->io, (unsigned char *) str, length);
if (r != 1)
return r;
str[length] = '\0';
*val = str;
return 1;
}
static int
ne_read_binary(nestegg * ctx, struct ebml_binary * val, uint64_t length)
{
if (length == 0 || length > LIMIT_BINARY)
return -1;
val->data = ne_pool_alloc(length, ctx->alloc_pool);
val->length = length;
return ne_io_read(ctx->io, val->data, length);
}
static int
ne_get_uint(struct ebml_type type, uint64_t * value)
{
if (!type.read)
return -1;
assert(type.type == TYPE_UINT);
*value = type.v.u;
return 0;
}
static int
ne_get_float(struct ebml_type type, double * value)
{
if (!type.read)
return -1;
assert(type.type == TYPE_FLOAT);
*value = type.v.f;
return 0;
}
static int
ne_get_string(struct ebml_type type, char ** value)
{
if (!type.read)
return -1;
assert(type.type == TYPE_STRING);
*value = type.v.s;
return 0;
}
static int
ne_get_binary(struct ebml_type type, struct ebml_binary * value)
{
if (!type.read)
return -1;
assert(type.type == TYPE_BINARY);
*value = type.v.b;
return 0;
}
static int
ne_is_ancestor_element(uint64_t id, struct list_node * ancestor)
{
struct ebml_element_desc * element;
for (; ancestor; ancestor = ancestor->previous)
for (element = ancestor->node; element->id; ++element)
if (element->id == id)
return 1;
return 0;
}
static struct ebml_element_desc *
ne_find_element(uint64_t id, struct ebml_element_desc * elements)
{
struct ebml_element_desc * element;
for (element = elements; element->id; ++element)
if (element->id == id)
return element;
return NULL;
}
static void
ne_ctx_push(nestegg * ctx, struct ebml_element_desc * ancestor, void * data)
{
struct list_node * item;
item = ne_alloc(sizeof(*item));
item->previous = ctx->ancestor;
item->node = ancestor;
item->data = data;
ctx->ancestor = item;
}
static void
ne_ctx_pop(nestegg * ctx)
{
struct list_node * item;
item = ctx->ancestor;
ctx->ancestor = item->previous;
free(item);
}
static int
ne_ctx_save(nestegg * ctx, struct saved_state * s)
{
s->stream_offset = ne_io_tell(ctx->io);
if (s->stream_offset < 0)
return -1;
s->ancestor = ctx->ancestor;
s->last_id = ctx->last_id;
s->last_size = ctx->last_size;
return 0;
}
static int
ne_ctx_restore(nestegg * ctx, struct saved_state * s)
{
int r;
r = ne_io_seek(ctx->io, s->stream_offset, NESTEGG_SEEK_SET);
if (r != 0)
return -1;
ctx->ancestor = s->ancestor;
ctx->last_id = s->last_id;
ctx->last_size = s->last_size;
return 0;
}
static int
ne_peek_element(nestegg * ctx, uint64_t * id, uint64_t * size)
{
int r;
if (ctx->last_id && ctx->last_size) {
if (id)
*id = ctx->last_id;
if (size)
*size = ctx->last_size;
return 1;
}
r = ne_read_id(ctx->io, &ctx->last_id, NULL);
if (r != 1)
return r;
r = ne_read_vint(ctx->io, &ctx->last_size, NULL);
if (r != 1)
return r;
if (id)
*id = ctx->last_id;
if (size)
*size = ctx->last_size;
return 1;
}
static int
ne_read_element(nestegg * ctx, uint64_t * id, uint64_t * size)
{
int r;
r = ne_peek_element(ctx, id, size);
if (r != 1)
return r;
ctx->last_id = 0;
ctx->last_size = 0;
return 1;
}
static void
ne_read_master(nestegg * ctx, struct ebml_element_desc * desc)
{
struct ebml_list * list;
struct ebml_list_node * node, * oldtail;
assert(desc->type == TYPE_MASTER && desc->flags & DESC_FLAG_MULTI);
ctx->log(ctx, NESTEGG_LOG_DEBUG, "multi master element %llx (%s)",
desc->id, desc->name);
list = (struct ebml_list *) (ctx->ancestor->data + desc->offset);
node = ne_pool_alloc(sizeof(*node), ctx->alloc_pool);
node->id = desc->id;
node->data = ne_pool_alloc(desc->size, ctx->alloc_pool);
oldtail = list->tail;
if (oldtail)
oldtail->next = node;
list->tail = node;
if (!list->head)
list->head = node;
ctx->log(ctx, NESTEGG_LOG_DEBUG, " -> using data %p", node->data);
ne_ctx_push(ctx, desc->children, node->data);
}
static void
ne_read_single_master(nestegg * ctx, struct ebml_element_desc * desc)
{
assert(desc->type == TYPE_MASTER && !(desc->flags & DESC_FLAG_MULTI));
ctx->log(ctx, NESTEGG_LOG_DEBUG, "single master element %llx (%s)",
desc->id, desc->name);
ctx->log(ctx, NESTEGG_LOG_DEBUG, " -> using data %p (%u)",
ctx->ancestor->data + desc->offset, desc->offset);
ne_ctx_push(ctx, desc->children, ctx->ancestor->data + desc->offset);
}
static int
ne_read_simple(nestegg * ctx, struct ebml_element_desc * desc, size_t length)
{
struct ebml_type * storage;
int r;
storage = (struct ebml_type *) (ctx->ancestor->data + desc->offset);
if (storage->read) {
ctx->log(ctx, NESTEGG_LOG_DEBUG, "element %llx (%s) already read, skipping",
desc->id, desc->name);
return 0;
}
storage->type = desc->type;
ctx->log(ctx, NESTEGG_LOG_DEBUG, "element %llx (%s) -> %p (%u)",
desc->id, desc->name, storage, desc->offset);
r = -1;
switch (desc->type) {
case TYPE_UINT:
r = ne_read_uint(ctx->io, &storage->v.u, length);
break;
case TYPE_FLOAT:
r = ne_read_float(ctx->io, &storage->v.f, length);
break;
case TYPE_INT:
r = ne_read_int(ctx->io, &storage->v.i, length);
break;
case TYPE_STRING:
r = ne_read_string(ctx, &storage->v.s, length);
break;
case TYPE_BINARY:
r = ne_read_binary(ctx, &storage->v.b, length);
break;
case TYPE_MASTER:
case TYPE_UNKNOWN:
assert(0);
break;
}
if (r == 1)
storage->read = 1;
return r;
}
static int
ne_parse(nestegg * ctx, struct ebml_element_desc * top_level)
{
int r;
int64_t * data_offset;
uint64_t id, size;
struct ebml_element_desc * element;
/* loop until we need to return:
- hit suspend point
- parse complete
- error occurred */
/* loop over elements at current level reading them if sublevel found,
push ctx onto stack and continue if sublevel ended, pop ctx off stack
and continue */
if (!ctx->ancestor)
return -1;
for (;;) {
r = ne_peek_element(ctx, &id, &size);
if (r != 1)
break;
element = ne_find_element(id, ctx->ancestor->node);
if (element) {
if (element->flags & DESC_FLAG_SUSPEND) {
assert(element->type == TYPE_BINARY);
ctx->log(ctx, NESTEGG_LOG_DEBUG, "suspend parse at %llx", id);
r = 1;
break;
}
r = ne_read_element(ctx, &id, &size);
if (r != 1)