-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathEbmlMaster.cpp
More file actions
479 lines (406 loc) · 12.9 KB
/
EbmlMaster.cpp
File metadata and controls
479 lines (406 loc) · 12.9 KB
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
// Copyright © 2002-2010 Steve Lhomme.
// SPDX-License-Identifier: LGPL-2.1-or-later
/*!
\file
\author Steve Lhomme <robux4 @ users.sf.net>
*/
#include "ebml/EbmlMaster.h"
#include "ebml/EbmlStream.h"
#include "ebml/MemIOCallback.h"
#include <cassert>
#include <algorithm>
#include <sstream>
namespace libebml {
EbmlMaster::EbmlMaster(const EbmlCallbacksMaster & classInfo, bool bSizeIsknown)
:EbmlElement(classInfo, 0)
{
SetSizeInfinite(!bSizeIsknown);
SetValueIsSet();
ProcessMandatory();
}
EbmlMaster::EbmlMaster(const EbmlMaster & ElementToClone)
:EbmlElement(ElementToClone)
,bChecksumUsed(ElementToClone.bChecksumUsed)
,Checksum(ElementToClone.Checksum)
{
SetSizeInfinite(!IsFiniteSize());
ElementList.reserve(ElementToClone.ListSize());
// add a clone of the list
for (const auto& e : ElementToClone.ElementList)
ElementList.push_back(e->Clone());
}
EbmlMaster::~EbmlMaster()
{
for (auto Element : ElementList) {
delete Element;
}
}
const EbmlSemantic & EbmlSemanticContextMaster::GetSemantic(std::size_t i) const
{
assert(i<GetSize());
if (i<GetSize())
return MyTable[i];
std::stringstream ss;
ss << "EbmlSemanticContext::GetSemantic: programming error: index i outside of table size (" << i << " >= " << GetSize() << ")";
throw std::logic_error(ss.str());
}
/*!
\todo handle exception on errors
\todo write all the Mandatory elements in the Context, otherwise assert
*/
filepos_t EbmlMaster::RenderData(IOCallback & output, bool bForceRender, const ShouldWrite & writeFilter)
{
filepos_t Result = 0;
if (!bForceRender) {
assert(CheckMandatory());
}
if (!bChecksumUsed) { // old school
for (auto Element : ElementList) {
if (!Element->CanWrite(writeFilter))
continue;
Result += Element->Render(output, writeFilter, false ,bForceRender);
}
} else { // new school
MemIOCallback TmpBuf(GetSize() - 6);
for (auto Element : ElementList) {
if (!Element->CanWrite(writeFilter))
continue;
Element->Render(TmpBuf, writeFilter, false ,bForceRender);
}
std::uint64_t memSize = TmpBuf.GetDataBufferSize();
const binary *memStart = TmpBuf.GetDataBuffer();
while (memSize != 0) {
const auto fillSize = static_cast<std::uint32_t>(std::min<std::uint64_t>(std::numeric_limits<std::uint32_t>::max(), memSize));
Checksum.FillCRC32(memStart, fillSize);
memStart += fillSize;
memSize -= fillSize;
}
Result += Checksum.Render(output, writeFilter, false ,bForceRender);
output.writeFully(TmpBuf.GetDataBuffer(), TmpBuf.GetDataBufferSize());
Result += TmpBuf.GetDataBufferSize();
}
return Result;
}
/*!
\todo We might be able to forbid elements that don't exist in the context
*/
bool EbmlMaster::PushElement(EbmlElement & element)
{
try {
ElementList.push_back(&element);
return true;
} catch(...) {
}
return false;
}
filepos_t EbmlMaster::UpdateSize(const ShouldWrite & writeFilter, bool bForceRender)
{
if (!CanWrite(writeFilter))
return 0;
SetSize_(0);
if (!IsFiniteSize())
return std::numeric_limits<std::uint64_t>::max();
if (!bForceRender) {
assert(CheckMandatory());
}
for (auto Element : ElementList) {
if (!Element->CanWrite(writeFilter))
continue;
Element->UpdateSize(writeFilter, bForceRender);
const std::uint64_t SizeToAdd = Element->ElementSize(writeFilter);
#if !defined(NDEBUG)
if (static_cast<std::int64_t>(SizeToAdd) == (0-1))
return std::numeric_limits<std::uint64_t>::max();
#endif // !NDEBUG
SetSize_(GetSize() + SizeToAdd);
}
if (bChecksumUsed) {
SetSize_(GetSize() + Checksum.ElementSize());
}
return GetSize();
}
filepos_t EbmlMaster::WriteHead(IOCallback & output, unsigned int nSizeLength, const ShouldWrite& writeFilter)
{
SetSizeLength(nSizeLength);
return RenderHead(output, false, writeFilter);
}
filepos_t EbmlMaster::ReadData(IOCallback & input, ScopeMode scope)
{
int upper = 0;
EbmlStream aStream(input);
EbmlElement *Parent = nullptr;
Read(aStream, Context(), upper, Parent, false, scope);
assert(Parent == nullptr);
assert(upper == 0);
input.setFilePointer(GetEndPosition(), seek_beginning);
return GetSize();
}
/*!
\note Hopefully no global element is mandatory
\todo should be called for ALL EbmlMaster element on construction
*/
bool EbmlMaster::ProcessMandatory()
{
const auto & MasterContext = ContextMaster();
for (unsigned int EltIdx = 0; EltIdx < EBML_CTX_SIZE(MasterContext); EltIdx++) {
if (EBML_CTX_IDX(MasterContext,EltIdx).IsMandatory() && EBML_CTX_IDX(MasterContext,EltIdx).IsUnique()) {
// assert(EBML_CTX_IDX(MasterContext,EltIdx).Create != NULL);
if (PushElement(EBML_SEM_CREATE(EBML_CTX_IDX(MasterContext,EltIdx))))
continue;
while (EltIdx-- != 0)
{
if (EBML_CTX_IDX(MasterContext,EltIdx).IsMandatory() && EBML_CTX_IDX(MasterContext,EltIdx).IsUnique())
{
// element that have been pushed should be removed
auto *todelete = ElementList.back();
ElementList.pop_back();
delete todelete;
}
}
return false;
}
}
return true;
}
bool EbmlMaster::CheckMandatory() const
{
const auto & MasterContext = ContextMaster();
for (unsigned int EltIdx = 0; EltIdx < EBML_CTX_SIZE(MasterContext); EltIdx++) {
if (EBML_CTX_IDX(MasterContext,EltIdx).IsMandatory()) {
const auto & semcb = EBML_CTX_IDX_INFO(MasterContext,EltIdx);
if (FindFirstElt(semcb) == nullptr) {
const bool hasDefaultValue = semcb.HasDefault();
#if !defined(NDEBUG)
// you are missing this Mandatory element
// const char * MissingName = semcb.GetName();
#endif // !NDEBUG
if (!hasDefaultValue)
return false;
}
}
}
return true;
}
EbmlElement *EbmlMaster::FindFirstElt(const EbmlCallbacks & Callbacks) const
{
auto it = std::find_if(ElementList.begin(), ElementList.end(), [&](const EbmlElement *Element)
{ return EbmlId(*Element) == EBML_INFO_ID(Callbacks); });
return it != ElementList.end() ? *it : nullptr;
}
EbmlElement *EbmlMaster::FindFirstElt(const EbmlCallbacks & Callbacks, bool bCreateIfNull)
{
auto e = FindFirstElt(Callbacks);
if (e)
return e;
if (bCreateIfNull) {
// add the element
return AddNewElt(Callbacks);
}
return nullptr;
}
/*!
\todo only return elements that are from the same type !
\todo the element might be the unique in the context !
*/
EbmlElement *EbmlMaster::FindNextElt(const EbmlElement & PastElt, bool bCreateIfNull)
{
EbmlElement *e = FindNextElt(PastElt);
if (e)
return e;
if (bCreateIfNull) {
// add the element
return AddNewElt(PastElt.ElementSpec());
}
return nullptr;
}
EbmlElement *EbmlMaster::FindNextElt(const EbmlElement & PastElt) const
{
auto it = std::find(ElementList.begin(), ElementList.end(), &PastElt);
if (it != ElementList.end()) {
it = std::find_if(it + 1, ElementList.end(), [&](auto &&element) {
return EbmlId(PastElt) == EbmlId(*element);
});
if (it != ElementList.end())
return *it;
}
return nullptr;
}
EbmlElement *EbmlMaster::AddNewElt(const EbmlCallbacks & Callbacks)
{
// add the element
EbmlElement *NewElt = &EBML_INFO_CREATE(Callbacks);
if (NewElt == nullptr)
return nullptr;
if (!PushElement(*NewElt)) {
delete NewElt;
NewElt = nullptr;
}
return NewElt;
}
void EbmlMaster::Sort()
{
std::sort(ElementList.begin(), ElementList.end(), EbmlElement::CompareElements);
}
/*!
\brief Method to help reading a Master element and all subsequent children quickly
\todo add an option to discard even unknown elements
\todo handle when a mandatory element is not found
*/
void EbmlMaster::Read(EbmlStream & inDataStream, const EbmlSemanticContext & sContext, int & UpperEltFound, EbmlElement * & FoundElt, bool AllowDummyElt, ScopeMode ReadFully)
{
if (ReadFully == SCOPE_NO_DATA)
return;
EbmlElement * ElementLevelA;
// remove all existing elements, including the mandatory ones...
for (auto Element : ElementList) {
delete Element;
}
ElementList.clear();
std::uint64_t MaxSizeToRead;
if (IsFiniteSize())
MaxSizeToRead = GetSize();
else
MaxSizeToRead = 0x7FFFFFFF;
// read blocks and discard the ones we don't care about
if (MaxSizeToRead > 0)
{
inDataStream.I_O().setFilePointer(GetSizePosition() + GetSizeLength(), seek_beginning);
ElementLevelA = inDataStream.FindNextElement(sContext, UpperEltFound, MaxSizeToRead, AllowDummyElt);
while (ElementLevelA != nullptr && UpperEltFound <= 0 && MaxSizeToRead > 0) {
if (IsFiniteSize() && ElementLevelA->IsFiniteSize())
MaxSizeToRead = GetEndPosition() - ElementLevelA->GetEndPosition(); // even if it's the default value
if (!AllowDummyElt && ElementLevelA->IsDummy()) {
if (ElementLevelA->IsFiniteSize()) {
ElementLevelA->SkipData(inDataStream, sContext);
delete ElementLevelA; // forget this unknown element
} else {
delete ElementLevelA; // forget this unknown element
break;
}
} else {
try {
ElementLevelA->Read(inDataStream, EBML_CONTEXT(ElementLevelA), UpperEltFound, FoundElt, AllowDummyElt, ReadFully);
} catch (...) {
if (ElementLevelA == FoundElt) {
UpperEltFound = 0;
FoundElt = nullptr;
}
delete ElementLevelA;
throw;
}
// Discard elements that couldn't be read properly if
// SCOPE_ALL_DATA has been requested. This can happen
// e.g. if block data is defective.
bool DeleteElement = true;
if (ElementLevelA->ValueIsSet() || (ReadFully != SCOPE_ALL_DATA)) {
ElementList.push_back(ElementLevelA);
DeleteElement = false;
}
// just in case
if (ElementLevelA->IsFiniteSize()) {
ElementLevelA->SkipData(inDataStream, EBML_CONTEXT(ElementLevelA));
if (DeleteElement)
delete ElementLevelA;
} else {
if (DeleteElement)
delete ElementLevelA;
if (UpperEltFound) {
--UpperEltFound;
if (UpperEltFound > 0 || MaxSizeToRead <= 0)
goto processCrc;
ElementLevelA = FoundElt;
}
break;
}
}
if (UpperEltFound > 0) {
UpperEltFound--;
if (UpperEltFound > 0)
goto processCrc;
ElementLevelA = FoundElt;
if (IsFiniteSize() && ElementLevelA->IsFiniteSize()) {
MaxSizeToRead = GetEndPosition() - ElementLevelA->GetEndPosition(); // even if it's the default value
}
continue;
}
if (UpperEltFound < 0) {
UpperEltFound++;
if (UpperEltFound < 0)
goto processCrc;
}
if (MaxSizeToRead <= 0)
goto processCrc;// this level is finished
ElementLevelA = inDataStream.FindNextElement(sContext, UpperEltFound, MaxSizeToRead, AllowDummyElt);
}
if (UpperEltFound > 0) {
FoundElt = ElementLevelA;
}
}
processCrc:
auto CrcItr =
std::find_if(ElementList.begin(), ElementList.end(), [](auto &&element) {
return EbmlId(*element) == EBML_ID(EbmlCrc32);
});
if (CrcItr != ElementList.end()) {
bChecksumUsed = true;
// remove the element
Checksum = *(static_cast<EbmlCrc32 *>(*CrcItr));
}
if (bChecksumUsed)
{
delete *CrcItr;
Remove(CrcItr);
}
SetValueIsSet();
}
void EbmlMaster::Remove(std::size_t Index)
{
if (Index < ElementList.size()) {
ElementList.erase(ElementList.begin() + Index);
}
}
void EbmlMaster::Remove(EBML_MASTER_ITERATOR & Itr)
{
ElementList.erase(Itr);
}
void EbmlMaster::Remove(EBML_MASTER_RITERATOR & Itr)
{
ElementList.erase(Itr.base());
}
bool EbmlMaster::VerifyChecksum() const
{
if (!bChecksumUsed)
return true;
EbmlCrc32 aChecksum;
/// \todo remove the Checksum if it's in the list
/// \todo find another way when not all default values are saved or (unknown from the reader !!!)
MemIOCallback TmpBuf(GetSize() - 6);
for (auto Element : ElementList) {
Element->Render(TmpBuf, WriteAll, false, true);
}
std::uint64_t memSize = TmpBuf.GetDataBufferSize();
const binary *memStart = TmpBuf.GetDataBuffer();
while (memSize != 0) {
const auto fillSize = static_cast<std::uint32_t>(std::min<std::uint64_t>(std::numeric_limits<std::uint32_t>::max(), memSize));
aChecksum.FillCRC32(memStart, fillSize);
memStart += fillSize;
memSize -= fillSize;
}
return (aChecksum.GetCrc32() == Checksum.GetCrc32());
}
bool EbmlMaster::InsertElement(EbmlElement & element, std::size_t position)
{
if ((ElementList.empty()) && position)
return false;
ElementList.insert(ElementList.begin() + position, &element);
return true;
}
bool EbmlMaster::InsertElement(EbmlElement & element, const EbmlElement & before)
{
auto Itr = std::find(ElementList.begin(), ElementList.end(), &before);
if (Itr == ElementList.end())
return false;
ElementList.insert(Itr, &element);
return true;
}
} // namespace libebml