diff --git a/CMakeLists.txt b/CMakeLists.txt index cb825519..49de3478 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -148,6 +148,10 @@ if (BUILD_TESTING) target_link_libraries(test_macros PUBLIC ebml) add_test(test_macros test_macros) + add_executable(test_crc test/test_crc.cxx) + target_link_libraries(test_crc PUBLIC ebml) + add_test(test_crc test_crc) + endif(BUILD_TESTING) diff --git a/src/EbmlMaster.cpp b/src/EbmlMaster.cpp index 91eaaff3..14084644 100644 --- a/src/EbmlMaster.cpp +++ b/src/EbmlMaster.cpp @@ -130,12 +130,16 @@ filepos_t EbmlMaster::WriteHead(IOCallback & output, int nSizeLength, ShouldWrit return RenderHead(output, false, writeFilter); } -/*! - \todo this code is very suspicious ! -*/ -filepos_t EbmlMaster::ReadData(IOCallback & input, ScopeMode /* ReadFully */) +filepos_t EbmlMaster::ReadData(IOCallback & input, ScopeMode scope) { - input.setFilePointer(GetSize(), seek_current); + 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(); } diff --git a/test/test_crc.cxx b/test/test_crc.cxx new file mode 100644 index 00000000..a42756f7 --- /dev/null +++ b/test/test_crc.cxx @@ -0,0 +1,61 @@ +// Copyright © 2024 Steve Lhomme. +// SPDX-License-Identifier: ISC + +#include +#include +#include +#include +#include + +int main(int /*argc*/, char** /*argv*/) +{ + ///// Writing test + libebml::MemIOCallback Ebml_file; + libebml::EbmlHead TestHead; + + TestHead.EnableChecksum(); + + libebml::EDocTypeVersion & MyDocTypeVer = libebml::GetChild(TestHead); + MyDocTypeVer.SetValue(1); + + libebml::GetChild(TestHead).SetValue(7); + + auto length = TestHead.Render(Ebml_file, libebml::EbmlElement::WriteAll); + if (length != 46) + return 1; + + ///// Reading test + Ebml_file.setFilePointer(0); + libebml::EbmlStream aStream(Ebml_file); + libebml::EbmlElement * ElementLevel0; + + // find the EBML head in the file + ElementLevel0 = aStream.FindNextID(EBML_INFO(libebml::EbmlHead), 0xFFFFFFFFL); + if (ElementLevel0 == nullptr) + return 1; + + if (ElementLevel0->GetClassId() != libebml::EbmlHead::ClassId()) + return 1; + + libebml::EbmlHead &ReadHead = static_cast(*ElementLevel0); + + ReadHead.ReadData(aStream, libebml::SCOPE_ALL_DATA); + + if (!ReadHead.VerifyChecksum()) + return 1; + + libebml::EDocType & ReadDocType = libebml::GetChild(ReadHead); + const std::string & DocTypeStr = static_cast(ReadDocType); + if (DocTypeStr != "matroska") + return 1; + + auto & ReadReadVersion = libebml::GetChild(ReadHead); + if (static_cast(ReadReadVersion) != 1) + return 1; + + auto & ReadMaxLength = libebml::GetChild(ReadHead); + if (static_cast(ReadMaxLength) != 7) + return 1; + + return 0; +} diff --git a/test/test_header.cxx b/test/test_header.cxx index c8aa33d1..f52eb7b2 100644 --- a/test/test_header.cxx +++ b/test/test_header.cxx @@ -49,6 +49,9 @@ int main(int /*argc*/, char** /*argv*/) if (upper != 0) return 1; + if (!ReadHead.VerifyChecksum()) + return 1; + libebml::EDocType & ReadDocType = libebml::GetChild(ReadHead); const std::string & DocTypeStr = static_cast(ReadDocType); if (DocTypeStr != "webm")