Skip to content

Commit

Permalink
pass the size we are checking for validity
Browse files Browse the repository at this point in the history
We should not have to force the element to have a size to check if it's valid.
It may end up in a state where it's invalid.

We keep a ValidateSize() method for compatibility (VLC).
  • Loading branch information
robUx4 committed Jan 13, 2024
1 parent 8ef7379 commit c39c0cb
Show file tree
Hide file tree
Showing 10 changed files with 14 additions and 13 deletions.
2 changes: 1 addition & 1 deletion ebml/EbmlBinary.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class EBML_DLL_API EbmlBinary : public EbmlElement {
EbmlBinary& operator=(const EbmlBinary & ElementToClone);
~EbmlBinary() override;

bool ValidateSize() const override {return GetSize() < 0x7FFFFFFF;} // we don't mind about what's inside
bool SizeIsValid(std::uint64_t size) const override {return size < 0x7FFFFFFF;} // we don't mind about what's inside

filepos_t RenderData(IOCallback & output, bool bForceRender, ShouldWrite writeFilter = WriteSkipDefault) override;
filepos_t ReadData(IOCallback & input, ScopeMode ReadFully = SCOPE_ALL_DATA) override;
Expand Down
2 changes: 1 addition & 1 deletion ebml/EbmlDate.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class EBML_DLL_API EbmlDate : public EbmlElementDefaultSameStorage<std::int64_t>
std::int64_t GetEpochDate() const {return EbmlToEpoch(EbmlElementDefaultSameStorage<std::int64_t>::GetValue());}
std::int64_t GetValue() const {return GetEpochDate();}

bool ValidateSize() const override {return GetSize() == 8 || GetSize() == 0;}
bool SizeIsValid(std::uint64_t size) const override {return size == 8 || size == 0;}

/*!
\note no Default date handled
Expand Down
5 changes: 3 additions & 2 deletions ebml/EbmlElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ class DllApi x : public BaseClass { \

#define DECLARE_xxx_BINARY_LENGTH(x,len,DllApi) \
DECLARE_xxx_BASE(x, DllApi, libebml::EbmlBinary) \
bool ValidateSize() const override {return GetSize() == len;}
bool SizeIsValid(std::uint64_t size) const override {return size == len;}

#define DECLARE_xxx_UINTEGER(x,DllApi) \
DECLARE_xxx_BASE_NODEFAULT(x, DllApi, libebml::EbmlUInteger, std::uint64_t)
Expand Down Expand Up @@ -453,7 +453,8 @@ class EBML_DLL_API EbmlElement {
return false;
}

virtual bool ValidateSize() const = 0;
virtual bool SizeIsValid(std::uint64_t) const = 0;
bool ValidateSize() const { return SizeIsValid(GetSize()); }

std::uint64_t GetElementPosition() const {
return ElementPosition;
Expand Down
4 changes: 2 additions & 2 deletions ebml/EbmlFloat.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ class EBML_DLL_API EbmlFloat : public EbmlElementDefaultSameStorage<double> {

EbmlFloat(const EbmlCallbacksDefault<double> &, Precision prec = FLOAT_32);

bool ValidateSize() const override
bool SizeIsValid(std::uint64_t size) const override
{
return (GetSize() == 4 || GetSize() == 8);
return (size == 4 || size == 8);
}

filepos_t RenderData(IOCallback & output, bool bForceRender, ShouldWrite writeFilter = WriteSkipDefault) override;
Expand Down
2 changes: 1 addition & 1 deletion ebml/EbmlMaster.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class EBML_DLL_API EbmlMaster : public EbmlElement {
explicit EbmlMaster(const EbmlCallbacks &, const EbmlSemanticContext & aContext, bool bSizeIsKnown = true);
EbmlMaster(const EbmlMaster & ElementToClone);
EbmlMaster& operator=(const EbmlMaster&) = delete;
bool ValidateSize() const override {return true;}
bool SizeIsValid(std::uint64_t /*size*/) const override {return true;}
/*!
\warning be carefull to clear the memory allocated in the ElementList elsewhere
*/
Expand Down
2 changes: 1 addition & 1 deletion ebml/EbmlSInteger.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class EBML_DLL_API EbmlSInteger : public EbmlElementDefaultSameStorage<std::int6
*/
void SetDefaultSize(std::uint64_t nDefaultSize = DEFAULT_INT_SIZE) override {EbmlElement::SetDefaultSize(nDefaultSize); SetSize_(nDefaultSize);}

bool ValidateSize() const override {return GetSize() <= 8;}
bool SizeIsValid(std::uint64_t size) const override {return size <= 8;}
filepos_t RenderData(IOCallback & output, bool bForceRender, ShouldWrite writeFilter = WriteSkipDefault) override;
filepos_t ReadData(IOCallback & input, ScopeMode ReadFully = SCOPE_ALL_DATA) override;
filepos_t UpdateSize(ShouldWrite writeFilter = WriteSkipDefault, bool bForceRender = false) override;
Expand Down
2 changes: 1 addition & 1 deletion ebml/EbmlString.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class EBML_DLL_API EbmlString : public EbmlElementDefaultStorage<const char *, s
public:
EbmlString(const EbmlCallbacksDefault<const char *> &);

bool ValidateSize() const override {return GetSize() < 0x7FFFFFFF;} // any size is possible
bool SizeIsValid(std::uint64_t size) const override {return size < 0x7FFFFFFF;} // any size is possible
filepos_t RenderData(IOCallback & output, bool bForceRender, ShouldWrite writeFilter = WriteSkipDefault) override;
filepos_t ReadData(IOCallback & input, ScopeMode ReadFully = SCOPE_ALL_DATA) override;
filepos_t UpdateSize(ShouldWrite writeFilter = WriteSkipDefault, bool bForceRender = false) override;
Expand Down
2 changes: 1 addition & 1 deletion ebml/EbmlUInteger.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class EBML_DLL_API EbmlUInteger : public EbmlElementDefaultSameStorage<std::uint
*/
void SetDefaultSize(std::uint64_t nDefaultSize = DEFAULT_UINT_SIZE) override {EbmlElement::SetDefaultSize(nDefaultSize); SetSize_(nDefaultSize);}

bool ValidateSize() const override {return GetSize() <= 8;}
bool SizeIsValid(std::uint64_t size) const override {return size <= 8;}
filepos_t RenderData(IOCallback & output, bool bForceRender, ShouldWrite writeFilter = WriteSkipDefault) override;
filepos_t ReadData(IOCallback & input, ScopeMode ReadFully = SCOPE_ALL_DATA) override;
filepos_t UpdateSize(ShouldWrite writeFilter = WriteSkipDefault, bool bForceRender = false) override;
Expand Down
2 changes: 1 addition & 1 deletion ebml/EbmlUnicodeString.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class EBML_DLL_API EbmlUnicodeString : public EbmlElementDefaultStorage<const wc
public:
EbmlUnicodeString(const EbmlCallbacksDefault<const wchar_t *> &);

bool ValidateSize() const override {return true;} // any size is possible
bool SizeIsValid(std::uint64_t /*size*/) const override {return true;} // any size is possible
filepos_t RenderData(IOCallback & output, bool bForceRender, ShouldWrite writeFilter = WriteSkipDefault) override;
filepos_t ReadData(IOCallback & input, ScopeMode ReadFully = SCOPE_ALL_DATA) override;
filepos_t UpdateSize(ShouldWrite writeFilter = WriteSkipDefault, bool bForceRender = false) override;
Expand Down
4 changes: 2 additions & 2 deletions src/EbmlElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ EbmlElement * EbmlElement::FindNextID(IOCallback & DataStream, const EbmlCallbac
if (Result == nullptr)
return nullptr;

if (!Result->ValidateSize()) {
if (!Result->SizeIsValid(SizeFound)) {
delete Result;
return nullptr;
}
Expand Down Expand Up @@ -340,7 +340,7 @@ EbmlElement * EbmlElement::FindNextElement(IOCallback & DataStream, const EbmlSe
// 0 : child
// 1 : same level
// + : further parent
if (Result->ValidateSize() && (SizeFound == SizeUnknown || UpperLevel > 0 || MaxDataSize == 0 ||
if (Result->SizeIsValid(SizeFound) && (SizeFound == SizeUnknown || UpperLevel > 0 || MaxDataSize == 0 ||
MaxDataSize >= (IdStart + PossibleID_Length + _SizeLength + SizeFound))) {
Result->ElementPosition = ParseStart + IdStart;
Result->SizePosition = Result->ElementPosition + PossibleID_Length;
Expand Down

0 comments on commit c39c0cb

Please sign in to comment.