From 1615359503505ca22dc3070d88325b0d47255019 Mon Sep 17 00:00:00 2001 From: Steve Lhomme Date: Sat, 13 Jan 2024 17:06:50 +0100 Subject: [PATCH 1/3] implement EbmlMaster::ReadData It's easier to use than Read(). --- src/EbmlMaster.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) 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(); } From cca5c8ca6f12368f585b5fdb10477db5be8f7e91 Mon Sep 17 00:00:00 2001 From: Steve Lhomme Date: Sat, 13 Jan 2024 17:09:56 +0100 Subject: [PATCH 2/3] add a test writing+verifying the checksum --- CMakeLists.txt | 4 ++++ test/test_crc.cxx | 61 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 test/test_crc.cxx 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/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; +} From afc410dfde06d53629d4aa1f736d9f8ebb616ff3 Mon Sep 17 00:00:00 2001 From: Steve Lhomme Date: Sat, 13 Jan 2024 17:10:17 +0100 Subject: [PATCH 3/3] check the checksum API works when there is no checksum written --- test/test_header.cxx | 3 +++ 1 file changed, 3 insertions(+) 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")