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
11void Ebml_Write(EbmlGlobal *glob, const void *buffer_in, unsigned long len) {
12  unsigned char *src = glob->buf;
13  src += glob->offset;
14  memcpy(src, buffer_in, len);
15  glob->offset += len;
16}
17
18static void _Serialize(EbmlGlobal *glob, const unsigned char *p, const unsigned char *q) {
19  while (q != p) {
20    --q;
21    memcpy(&(glob->buf[glob->offset]), q, 1);
22    glob->offset++;
23  }
24}
25
26void Ebml_Serialize(EbmlGlobal *glob, const void *buffer_in, unsigned long len) {
27  // assert(buf);
28
29  const unsigned char *const p = (const unsigned char *)(buffer_in);
30  const unsigned char *const q = p + len;
31
32  _Serialize(glob, p, q);
33}
34
35
36void Ebml_StartSubElement(EbmlGlobal *glob, EbmlLoc *ebmlLoc, unsigned long class_id) {
37  Ebml_WriteID(glob, class_id);
38  ebmlLoc->offset = glob->offset;
39  // todo this is always taking 8 bytes, this may need later optimization
40  unsigned long long unknownLen =  0x01FFFFFFFFFFFFFFLLU;
41  Ebml_Serialize(glob, (void *)&unknownLen, 8); // this is a key that says lenght unknown
42}
43
44void Ebml_EndSubElement(EbmlGlobal *glob, EbmlLoc *ebmlLoc) {
45  unsigned long long size = glob->offset - ebmlLoc->offset - 8;
46  unsigned long long curOffset = glob->offset;
47  glob->offset = ebmlLoc->offset;
48  size |=  0x0100000000000000LLU;
49  Ebml_Serialize(glob, &size, 8);
50  glob->offset = curOffset;
51}
52
53