Line data Source code
1 : // #include <strmif.h>
2 : #include "EbmlBufferWriter.h"
3 : #include "EbmlWriter.h"
4 : // #include <cassert>
5 : // #include <limits>
6 : // #include <malloc.h> //_alloca
7 : #include <stdlib.h>
8 : #include <wchar.h>
9 : #include <string.h>
10 :
11 : void
12 0 : Ebml_Serialize(EbmlGlobal *glob, const void *buffer_in, int buffer_size, unsigned long len)
13 : {
14 : /* buffer_size:
15 : * 1 - int8_t;
16 : * 2 - int16_t;
17 : * 4 - int32_t;
18 : * 8 - int64_t;
19 : */
20 : long i;
21 0 : for(i = len-1; i >= 0; i--) {
22 : unsigned char x;
23 0 : if (buffer_size == 1) {
24 0 : x = (char)(*(const int8_t *)buffer_in >> (i * 8));
25 0 : } else if (buffer_size == 2) {
26 0 : x = (char)(*(const int16_t *)buffer_in >> (i * 8));
27 0 : } else if (buffer_size == 4) {
28 0 : x = (char)(*(const int32_t *)buffer_in >> (i * 8));
29 0 : } else if (buffer_size == 8) {
30 0 : x = (char)(*(const int64_t *)buffer_in >> (i * 8));
31 : }
32 0 : Ebml_Write(glob, &x, 1);
33 : }
34 0 : }
35 :
36 0 : void Ebml_Write(EbmlGlobal *glob, const void *buffer_in, unsigned long len) {
37 0 : unsigned char *src = glob->buf;
38 0 : src += glob->offset;
39 0 : memcpy(src, buffer_in, len);
40 0 : glob->offset += len;
41 0 : }
42 :
43 0 : static void _Serialize(EbmlGlobal *glob, const unsigned char *p, const unsigned char *q) {
44 0 : while (q != p) {
45 0 : --q;
46 :
47 0 : memcpy(&(glob->buf[glob->offset]), q, 1);
48 0 : glob->offset++;
49 : }
50 0 : }
51 :
52 : /*
53 : void Ebml_Serialize(EbmlGlobal *glob, const void *buffer_in, unsigned long len) {
54 : // assert(buf);
55 :
56 : const unsigned char *const p = (const unsigned char *)(buffer_in);
57 : const unsigned char *const q = p + len;
58 :
59 : _Serialize(glob, p, q);
60 : }
61 : */
62 :
63 0 : void Ebml_StartSubElement(EbmlGlobal *glob, EbmlLoc *ebmlLoc, unsigned long class_id) {
64 0 : unsigned long long unknownLen = 0x01FFFFFFFFFFFFFFLL;
65 0 : Ebml_WriteID(glob, class_id);
66 0 : ebmlLoc->offset = glob->offset;
67 : // todo this is always taking 8 bytes, this may need later optimization
68 0 : Ebml_Serialize(glob, (void *)&unknownLen,sizeof(unknownLen), 8); // this is a key that says length unknown
69 0 : }
70 :
71 0 : void Ebml_EndSubElement(EbmlGlobal *glob, EbmlLoc *ebmlLoc) {
72 0 : unsigned long long size = glob->offset - ebmlLoc->offset - 8;
73 0 : unsigned long long curOffset = glob->offset;
74 0 : glob->offset = ebmlLoc->offset;
75 0 : size |= 0x0100000000000000LL;
76 0 : Ebml_Serialize(glob, &size,sizeof(size), 8);
77 0 : glob->offset = curOffset;
78 0 : }
79 :
|