From b9c467142f4da51bdea22a3f793cf92e7354ae1e Mon Sep 17 00:00:00 2001 From: Ivan Morozov Date: Thu, 29 Sep 2022 10:02:04 +0300 Subject: [PATCH] small changes --- Common/cfcpp/cfstream.cpp | 6 ++++ Common/cfcpp/cfstream.h | 1 + Common/cfcpp/compoundfile.cpp | 51 ++++++++++++++++------------- Common/cfcpp/compoundfile.h | 1 + Common/cfcpp/svector.h | 3 ++ Common/cfcpp/test/tst_compondfile.h | 5 +-- 6 files changed, 43 insertions(+), 24 deletions(-) diff --git a/Common/cfcpp/cfstream.cpp b/Common/cfcpp/cfstream.cpp index 8b51ce53dc..865e87d6cb 100644 --- a/Common/cfcpp/cfstream.cpp +++ b/Common/cfcpp/cfstream.cpp @@ -33,6 +33,12 @@ void CFStream::Write(const std::vector &data, std::streamsize position, in compoundFile->WriteData(shared_from_this(), data, position, offset, count); } +void CFStream::Write(const char *data, std::streamsize position, int count) +{ + CheckDisposed(); + compoundFile->WriteData(shared_from_this(), data, position, count); +} + void CFStream::Append(const std::vector &data) { CheckDisposed(); diff --git a/Common/cfcpp/cfstream.h b/Common/cfcpp/cfstream.h index f2c35c4697..0a703c9e10 100644 --- a/Common/cfcpp/cfstream.h +++ b/Common/cfcpp/cfstream.h @@ -14,6 +14,7 @@ public: void Append(const std::vector& data); void Write(const std::vector& data, std::streamsize position); void Write(const std::vector& data, std::streamsize position, int offset, int count); + void Write(const char *data, std::streamsize position, int count); int Read(std::vector& buffer, std::streamsize position, int count); int Read(std::vector& buffer, std::streamsize position, int offset, int count); diff --git a/Common/cfcpp/compoundfile.cpp b/Common/cfcpp/compoundfile.cpp index 8365b79b3d..c3ba9a0bd4 100644 --- a/Common/cfcpp/compoundfile.cpp +++ b/Common/cfcpp/compoundfile.cpp @@ -318,7 +318,7 @@ SVector CompoundFile::GetFatSectorChain() while (idx < header->fatSectorsNumber && idx < N_HEADER_FAT_ENTRY) { nextSecID = header->difat[idx]; - auto s = sectors[nextSecID]; + auto& s = sectors[nextSecID]; if (s.get() == nullptr) { @@ -363,9 +363,9 @@ SVector CompoundFile::GetFatSectorChain() EnsureUniqueSectorIndex(nextSecID, processedSectors); - auto s = sectors[nextSecID]; + auto& s = sectors[nextSecID]; - if (s.get() == nullptr) + if (s == nullptr) { s.reset(new Sector(GetSectorSize(), sourceStream)); s->type = SectorType::FAT; @@ -419,7 +419,7 @@ SVector CompoundFile::GetDifatSectorChain() result.push_back(s); - while (validationCount >= 0) + while (true) { int startPos = GetSectorSize() - 4; nextSecID = *reinterpret_cast(s->GetData().data() + startPos); @@ -481,7 +481,7 @@ SVector CompoundFile::GetNormalSectorChain(int secID) throw CFCorruptedFileException("Next Sector ID reference an out of range sector. NextID : " + std::to_string(nextSecID) + " while sector count " + std::to_string(sectors.Count())); - std::shared_ptr s = sectors[nextSecID]; + auto& s = sectors[nextSecID]; if (s == nullptr) { s.reset(new Sector(GetSectorSize(), sourceStream)); @@ -819,7 +819,7 @@ void CompoundFile::FreeMiniChain(SVector §orChain, bool zeroSector) void CompoundFile::FreeMiniChain(SVector §orChain, int nth_sector_to_remove, bool zeroSector) { - std::vector ZEROED_MINI_SECTOR(Sector::MINISECTOR_SIZE); + std::vector ZEROED_MINI_SECTOR(Sector::MINISECTOR_SIZE, 0); SVector miniFAT = GetSectorChain(header->firstMiniFATSectorID, SectorType::Normal); @@ -837,7 +837,7 @@ void CompoundFile::FreeMiniChain(SVector §orChain, int nth_sector_to { for (int i = nth_sector_to_remove; i < (int)sectorChain.size(); i++) { - auto s = sectorChain[i]; + auto& s = sectorChain[i]; if (s->id != -1) { @@ -881,9 +881,6 @@ void CompoundFile::FreeMiniChain(SVector §orChain, int nth_sector_to void CompoundFile::FreeChain(SVector §orChain, int nth_sector_to_remove, bool zeroSector) { - // Dummy zero buffer - std::vector ZEROED_SECTOR; - SVector FAT = GetSectorChain(-1, SectorType::FAT); SList zeroQueue; @@ -894,7 +891,7 @@ void CompoundFile::FreeChain(SVector §orChain, int nth_sector_to_rem { for (int i = nth_sector_to_remove; i < (int)sectorChain.size(); i++) { - auto s = sectorChain[i]; + auto& s = sectorChain[i]; s->ZeroData(); } } @@ -925,7 +922,7 @@ void CompoundFile::FreeChain(SVector §orChain, bool zeroSector) void CompoundFile::AllocateSectorChain(SVector §orChain) { - for (auto& s : *sectorChain) + for (auto& s : *sectorChain) // todo check { if (s->id == -1) { @@ -977,7 +974,7 @@ void CompoundFile::AllocateDIFATSectorChain(SVector &FATsectorChain) header->fatSectorsNumber = FATsectorChain.size(); // Allocate Sectors - for (auto& s : *FATsectorChain) + for (auto& s : *FATsectorChain) // todo check { if (s->id == -1) { @@ -1046,7 +1043,8 @@ void CompoundFile::AllocateDIFATSectorChain(SVector &FATsectorChain) // room for DIFAT chaining at the end of any DIFAT sector (4 bytes) if (i != HEADER_DIFAT_ENTRIES_COUNT && (i - HEADER_DIFAT_ENTRIES_COUNT) % DIFAT_SECTOR_FAT_ENTRIES_COUNT == 0) { - difatStream.write(reinterpret_cast(0L), sizeof(int)); + int zero = 0; + difatStream.write(reinterpret_cast(&zero), sizeof(int)); } difatStream.write(reinterpret_cast(&FATsectorChain[i]->id), sizeof(int)); @@ -1069,7 +1067,7 @@ void CompoundFile::AllocateDIFATSectorChain(SVector &FATsectorChain) // Chain first sector - if (difatStream.BaseSectorChain().size() && difatStream.BaseSectorChain().size() > 0) + if (difatStream.BaseSectorChain() != nullptr && difatStream.BaseSectorChain().size() > 0) { header->firstDIFATSectorID = difatStream.BaseSectorChain()[0]->id; @@ -1086,7 +1084,8 @@ void CompoundFile::AllocateDIFATSectorChain(SVector &FATsectorChain) std::copy_n(src, sizeof(int), dst+offsetDst); } - char* src = const_cast(reinterpret_cast(&Sector::ENDOFCHAIN)); + auto eoc = Sector::ENDOFCHAIN; + char* src = reinterpret_cast(&eoc); char* dst = reinterpret_cast(difatStream.BaseSectorChain()[difatStream.BaseSectorChain().size() - 1]->GetData().data()); int offsetDst = GetSectorSize() - sizeof(int); std::copy_n(src, sizeof(int), dst+offsetDst); @@ -1145,7 +1144,7 @@ void CompoundFile::AllocateMiniSectorChain(SVector §orChain) // We are writing data in a NORMAL Sector chain. for (int i = 0; i < (int)sectorChain.size(); i++) { - std::shared_ptr s = sectorChain[i]; + std::shared_ptr& s = sectorChain[i]; if (s->id == -1) { @@ -1219,7 +1218,7 @@ int CompoundFile::LowSaturation(int i) void CompoundFile::SetSectorChain(SVector sectorChain) { - if (sectorChain.size() == 0) + if (sectorChain != nullptr && sectorChain.size() == 0) return; SectorType _st = sectorChain[0]->type; @@ -1361,7 +1360,7 @@ void CompoundFile::SetStreamLength(std::shared_ptr cfItem, std::streamsi bool transitionToMini = false; bool transitionToNormal = false; - SVector oldChain; + SVector oldChain(nullptr); if (cfItem->dirEntry.lock()->getStartSetc() != Sector::ENDOFCHAIN) { @@ -1752,12 +1751,15 @@ GUID CompoundFile::getGuidForStream(int sid) return g; } -void CompoundFile::WriteData(std::shared_ptr cfItem, const std::vector &buffer, std::streamsize position, int offset, int count) +void CompoundFile::WriteData(std::shared_ptr cfItem, const char* data, std::streamsize position, int count) { + if (data == nullptr) + throw CFInvalidOperation("Parameter [data] cannot be null"); + if (cfItem->dirEntry.expired()) throw CFException("Internal error [cfItem->dirEntry] cannot be null"); - if (buffer.size() == 0) return; + if (count == 0) return; // Get delta size induced by client std::streamsize delta = (position + count) - cfItem->size() < 0 ? 0 : (position + count) - cfItem->size(); @@ -1780,7 +1782,7 @@ void CompoundFile::WriteData(std::shared_ptr cfItem, const std::vector(buffer.data() + offset), count); + sv.write(data, count); if (cfItem->size() < header->minSizeStandardStream) { @@ -1788,6 +1790,11 @@ void CompoundFile::WriteData(std::shared_ptr cfItem, const std::vector cfItem, const std::vector &buffer, std::streamsize position, int offset, int count) +{ + WriteData(cfItem, reinterpret_cast(buffer.data() + offset), position, count); +} + int CompoundFile::GetSectorSize() { return 2 << (header->sectorShift - 1); diff --git a/Common/cfcpp/compoundfile.h b/Common/cfcpp/compoundfile.h index 4281739df7..58ec331bd9 100644 --- a/Common/cfcpp/compoundfile.h +++ b/Common/cfcpp/compoundfile.h @@ -63,6 +63,7 @@ public: void InvalidateDirectoryEntry(int sid); void FreeAssociatedData(int sid); void FreeData(CFStream* stream); + void WriteData(std::shared_ptr cfItem, const char* data, std::streamsize position, int count); void WriteData(std::shared_ptr cfItem, const std::vector& buffer, std::streamsize position, int offset, int count); void WriteData(std::shared_ptr cfItem, std::streamsize position, const std::vector& buffer); void WriteData(std::shared_ptr cfItem, const std::vector& buffer); diff --git a/Common/cfcpp/svector.h b/Common/cfcpp/svector.h index 64749d28a2..0cf646a0dd 100644 --- a/Common/cfcpp/svector.h +++ b/Common/cfcpp/svector.h @@ -21,6 +21,9 @@ public: init(res); } + SVector(const SVectorBasePtr& oth) : SVectorBasePtr(oth) + {} + void init(size_t res = 0) { SVectorBasePtr::reset(new SVectorBase(res)); diff --git a/Common/cfcpp/test/tst_compondfile.h b/Common/cfcpp/test/tst_compondfile.h index 868c513d7e..04b35d35bb 100644 --- a/Common/cfcpp/test/tst_compondfile.h +++ b/Common/cfcpp/test/tst_compondfile.h @@ -75,7 +75,7 @@ TEST_F(CompoundFileTest, test_compoundfile_write) TEST(test_compoundfile, largeFileCopy) { wstring filename = L"../../../data/2.ppt"; - CompoundFile cf(L"../../../data/2.ppt", CFSUpdateMode::ReadOnly); + CompoundFile cf(filename, CFSUpdateMode::ReadOnly); wstring other_filename = L"../../../data/3.ppt"; NSFile::CFileBinary::Remove(other_filename); @@ -261,7 +261,8 @@ TEST(test_compoundfile, largeStream_v3_v4) for (LONG64 i = 0; i < streamLen; i += data.size()) { - EXPECT_NO_THROW(stream1->Write(data, i)); + + EXPECT_NO_THROW(stream1->Write(reinterpret_cast(&i), i, sizeof(i))); // EXPECT_NO_THROW(stream2->Write(data, i)); }