2 #include "EbmlBufferWriter.h"
3 #include "EbmlWriter.h"
6 // #include <malloc.h> //_alloca
12 Ebml_Serialize(EbmlGlobal
*glob
, const void *buffer_in
, int buffer_size
, unsigned long len
)
21 for(i
= len
-1; i
>= 0; i
--) {
23 if (buffer_size
== 1) {
24 x
= (char)(*(const int8_t *)buffer_in
>> (i
* 8));
25 } else if (buffer_size
== 2) {
26 x
= (char)(*(const int16_t *)buffer_in
>> (i
* 8));
27 } else if (buffer_size
== 4) {
28 x
= (char)(*(const int32_t *)buffer_in
>> (i
* 8));
29 } else if (buffer_size
== 8) {
30 x
= (char)(*(const int64_t *)buffer_in
>> (i
* 8));
32 Ebml_Write(glob
, &x
, 1);
36 void Ebml_Write(EbmlGlobal
*glob
, const void *buffer_in
, unsigned long len
) {
37 unsigned char *src
= glob
->buf
;
39 memcpy(src
, buffer_in
, len
);
43 static void _Serialize(EbmlGlobal
*glob
, const unsigned char *p
, const unsigned char *q
) {
47 memcpy(&(glob
->buf
[glob
->offset
]), q
, 1);
53 void Ebml_Serialize(EbmlGlobal *glob, const void *buffer_in, unsigned long len) {
56 const unsigned char *const p = (const unsigned char *)(buffer_in);
57 const unsigned char *const q = p + len;
59 _Serialize(glob, p, q);
63 void Ebml_StartSubElement(EbmlGlobal
*glob
, EbmlLoc
*ebmlLoc
, unsigned long class_id
) {
64 unsigned long long unknownLen
= 0x01FFFFFFFFFFFFFFLL
;
65 Ebml_WriteID(glob
, class_id
);
66 ebmlLoc
->offset
= glob
->offset
;
67 // todo this is always taking 8 bytes, this may need later optimization
68 Ebml_Serialize(glob
, (void *)&unknownLen
,sizeof(unknownLen
), 8); // this is a key that says length unknown
71 void Ebml_EndSubElement(EbmlGlobal
*glob
, EbmlLoc
*ebmlLoc
) {
72 unsigned long long size
= glob
->offset
- ebmlLoc
->offset
- 8;
73 unsigned long long curOffset
= glob
->offset
;
74 glob
->offset
= ebmlLoc
->offset
;
75 size
|= 0x0100000000000000LL
;
76 Ebml_Serialize(glob
, &size
,sizeof(size
), 8);
77 glob
->offset
= curOffset
;