1 // Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3 // Use of this source code is governed by a BSD-style license
4 // that can be found in the LICENSE file in the root of the source
5 // tree. An additional intellectual property rights grant can be found
6 // in the file PATENTS. All contributing project authors may
7 // be found in the AUTHORS file in the root of the source tree.
10 #include "EbmlWriter.h"
16 #define LITERALU64(n) n
18 #define LITERALU64(n) n##LLU
21 void Ebml_WriteLen(EbmlGlobal *glob, long long val)
23 //TODO check and make sure we are not > than 0x0100000000000000LLU
24 unsigned char size = 8; //size in bytes to output
25 unsigned long long minVal = LITERALU64(0x00000000000000ff); //mask to compare for byte size
27 for (size = 1; size < 8; size ++)
32 minVal = (minVal << 7);
35 val |= (LITERALU64(0x000000000000080) << ((size - 1) * 7));
37 Ebml_Serialize(glob, (void *) &val, sizeof(val), size);
40 void Ebml_WriteString(EbmlGlobal *glob, const char *str)
42 const size_t size_ = strlen(str);
43 const unsigned long long size = size_;
44 Ebml_WriteLen(glob, size);
45 //TODO: it's not clear from the spec whether the nul terminator
46 //should be serialized too. For now we omit the null terminator.
47 Ebml_Write(glob, str, size);
50 void Ebml_WriteUTF8(EbmlGlobal *glob, const wchar_t *wstr)
52 const size_t strlen = wcslen(wstr);
54 //TODO: it's not clear from the spec whether the nul terminator
55 //should be serialized too. For now we include it.
56 const unsigned long long size = strlen;
58 Ebml_WriteLen(glob, size);
59 Ebml_Write(glob, wstr, size);
62 void Ebml_WriteID(EbmlGlobal *glob, unsigned long class_id)
66 if (class_id >= 0x01000000)
68 else if (class_id >= 0x00010000)
70 else if (class_id >= 0x00000100)
75 Ebml_Serialize(glob, (void *)&class_id, sizeof(class_id), len);
78 void Ebml_SerializeUnsigned64(EbmlGlobal *glob, unsigned long class_id, uint64_t ui)
80 unsigned char sizeSerialized = 8 | 0x80;
81 Ebml_WriteID(glob, class_id);
82 Ebml_Serialize(glob, &sizeSerialized, sizeof(sizeSerialized), 1);
83 Ebml_Serialize(glob, &ui, sizeof(ui), 8);
86 void Ebml_SerializeUnsigned(EbmlGlobal *glob, unsigned long class_id, unsigned long ui)
88 unsigned char size = 8; //size in bytes to output
89 unsigned char sizeSerialized = 0;
92 Ebml_WriteID(glob, class_id);
93 minVal = 0x7fLU; //mask to compare for byte size
95 for (size = 1; size < 4; size ++)
105 sizeSerialized = 0x80 | size;
106 Ebml_Serialize(glob, &sizeSerialized, sizeof(sizeSerialized), 1);
107 Ebml_Serialize(glob, &ui, sizeof(ui), size);
109 //TODO: perhaps this is a poor name for this id serializer helper function
110 void Ebml_SerializeBinary(EbmlGlobal *glob, unsigned long class_id, unsigned long bin)
113 for (size=4; size > 1; size--)
115 if (bin & 0x000000ff << ((size-1) * 8))
118 Ebml_WriteID(glob, class_id);
119 Ebml_WriteLen(glob, size);
120 Ebml_WriteID(glob, bin);
123 void Ebml_SerializeFloat(EbmlGlobal *glob, unsigned long class_id, double d)
125 unsigned char len = 0x88;
127 Ebml_WriteID(glob, class_id);
128 Ebml_Serialize(glob, &len, sizeof(len), 1);
129 Ebml_Serialize(glob, &d, sizeof(d), 8);
132 void Ebml_WriteSigned16(EbmlGlobal *glob, short val)
134 signed long out = ((val & 0x003FFFFF) | 0x00200000) << 8;
135 Ebml_Serialize(glob, &out, sizeof(out), 3);
138 void Ebml_SerializeString(EbmlGlobal *glob, unsigned long class_id, const char *s)
140 Ebml_WriteID(glob, class_id);
141 Ebml_WriteString(glob, s);
144 void Ebml_SerializeUTF8(EbmlGlobal *glob, unsigned long class_id, wchar_t *s)
146 Ebml_WriteID(glob, class_id);
147 Ebml_WriteUTF8(glob, s);
150 void Ebml_SerializeData(EbmlGlobal *glob, unsigned long class_id, unsigned char *data, unsigned long data_length)
152 Ebml_WriteID(glob, class_id);
153 Ebml_WriteLen(glob, data_length);
154 Ebml_Write(glob, data, data_length);
157 void Ebml_WriteVoid(EbmlGlobal *glob, unsigned long vSize)
159 unsigned char tmp = 0;
162 Ebml_WriteID(glob, 0xEC);
163 Ebml_WriteLen(glob, vSize);
165 for (i = 0; i < vSize; i++)
167 Ebml_Write(glob, &tmp, 1);
171 //TODO Serialize Date