mirror of
https://github.com/ONLYOFFICE/core.git
synced 2026-04-07 13:55:33 +08:00
small changes
This commit is contained in:
@ -33,6 +33,12 @@ void CFStream::Write(const std::vector<BYTE> &data, std::streamsize position, in
|
|||||||
compoundFile->WriteData(shared_from_this(), data, position, offset, count);
|
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<BYTE> &data)
|
void CFStream::Append(const std::vector<BYTE> &data)
|
||||||
{
|
{
|
||||||
CheckDisposed();
|
CheckDisposed();
|
||||||
|
|||||||
@ -14,6 +14,7 @@ public:
|
|||||||
void Append(const std::vector<BYTE>& data);
|
void Append(const std::vector<BYTE>& data);
|
||||||
void Write(const std::vector<BYTE>& data, std::streamsize position);
|
void Write(const std::vector<BYTE>& data, std::streamsize position);
|
||||||
void Write(const std::vector<BYTE>& data, std::streamsize position, int offset, int count);
|
void Write(const std::vector<BYTE>& data, std::streamsize position, int offset, int count);
|
||||||
|
void Write(const char *data, std::streamsize position, int count);
|
||||||
|
|
||||||
int Read(std::vector<BYTE>& buffer, std::streamsize position, int count);
|
int Read(std::vector<BYTE>& buffer, std::streamsize position, int count);
|
||||||
int Read(std::vector<BYTE>& buffer, std::streamsize position, int offset, int count);
|
int Read(std::vector<BYTE>& buffer, std::streamsize position, int offset, int count);
|
||||||
|
|||||||
@ -318,7 +318,7 @@ SVector<Sector> CompoundFile::GetFatSectorChain()
|
|||||||
while (idx < header->fatSectorsNumber && idx < N_HEADER_FAT_ENTRY)
|
while (idx < header->fatSectorsNumber && idx < N_HEADER_FAT_ENTRY)
|
||||||
{
|
{
|
||||||
nextSecID = header->difat[idx];
|
nextSecID = header->difat[idx];
|
||||||
auto s = sectors[nextSecID];
|
auto& s = sectors[nextSecID];
|
||||||
|
|
||||||
if (s.get() == nullptr)
|
if (s.get() == nullptr)
|
||||||
{
|
{
|
||||||
@ -363,9 +363,9 @@ SVector<Sector> CompoundFile::GetFatSectorChain()
|
|||||||
|
|
||||||
EnsureUniqueSectorIndex(nextSecID, processedSectors);
|
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.reset(new Sector(GetSectorSize(), sourceStream));
|
||||||
s->type = SectorType::FAT;
|
s->type = SectorType::FAT;
|
||||||
@ -419,7 +419,7 @@ SVector<Sector> CompoundFile::GetDifatSectorChain()
|
|||||||
|
|
||||||
result.push_back(s);
|
result.push_back(s);
|
||||||
|
|
||||||
while (validationCount >= 0)
|
while (true)
|
||||||
{
|
{
|
||||||
int startPos = GetSectorSize() - 4;
|
int startPos = GetSectorSize() - 4;
|
||||||
nextSecID = *reinterpret_cast<int*>(s->GetData().data() + startPos);
|
nextSecID = *reinterpret_cast<int*>(s->GetData().data() + startPos);
|
||||||
@ -481,7 +481,7 @@ SVector<Sector> CompoundFile::GetNormalSectorChain(int secID)
|
|||||||
throw CFCorruptedFileException("Next Sector ID reference an out of range sector. NextID : " + std::to_string(nextSecID) +
|
throw CFCorruptedFileException("Next Sector ID reference an out of range sector. NextID : " + std::to_string(nextSecID) +
|
||||||
" while sector count " + std::to_string(sectors.Count()));
|
" while sector count " + std::to_string(sectors.Count()));
|
||||||
|
|
||||||
std::shared_ptr<Sector> s = sectors[nextSecID];
|
auto& s = sectors[nextSecID];
|
||||||
if (s == nullptr)
|
if (s == nullptr)
|
||||||
{
|
{
|
||||||
s.reset(new Sector(GetSectorSize(), sourceStream));
|
s.reset(new Sector(GetSectorSize(), sourceStream));
|
||||||
@ -819,7 +819,7 @@ void CompoundFile::FreeMiniChain(SVector<Sector> §orChain, bool zeroSector)
|
|||||||
|
|
||||||
void CompoundFile::FreeMiniChain(SVector<Sector> §orChain, int nth_sector_to_remove, bool zeroSector)
|
void CompoundFile::FreeMiniChain(SVector<Sector> §orChain, int nth_sector_to_remove, bool zeroSector)
|
||||||
{
|
{
|
||||||
std::vector<char> ZEROED_MINI_SECTOR(Sector::MINISECTOR_SIZE);
|
std::vector<char> ZEROED_MINI_SECTOR(Sector::MINISECTOR_SIZE, 0);
|
||||||
|
|
||||||
SVector<Sector> miniFAT
|
SVector<Sector> miniFAT
|
||||||
= GetSectorChain(header->firstMiniFATSectorID, SectorType::Normal);
|
= GetSectorChain(header->firstMiniFATSectorID, SectorType::Normal);
|
||||||
@ -837,7 +837,7 @@ void CompoundFile::FreeMiniChain(SVector<Sector> §orChain, int nth_sector_to
|
|||||||
{
|
{
|
||||||
for (int i = nth_sector_to_remove; i < (int)sectorChain.size(); i++)
|
for (int i = nth_sector_to_remove; i < (int)sectorChain.size(); i++)
|
||||||
{
|
{
|
||||||
auto s = sectorChain[i];
|
auto& s = sectorChain[i];
|
||||||
|
|
||||||
if (s->id != -1)
|
if (s->id != -1)
|
||||||
{
|
{
|
||||||
@ -881,9 +881,6 @@ void CompoundFile::FreeMiniChain(SVector<Sector> §orChain, int nth_sector_to
|
|||||||
|
|
||||||
void CompoundFile::FreeChain(SVector<Sector> §orChain, int nth_sector_to_remove, bool zeroSector)
|
void CompoundFile::FreeChain(SVector<Sector> §orChain, int nth_sector_to_remove, bool zeroSector)
|
||||||
{
|
{
|
||||||
// Dummy zero buffer
|
|
||||||
std::vector<char> ZEROED_SECTOR;
|
|
||||||
|
|
||||||
SVector<Sector> FAT = GetSectorChain(-1, SectorType::FAT);
|
SVector<Sector> FAT = GetSectorChain(-1, SectorType::FAT);
|
||||||
|
|
||||||
SList<Sector> zeroQueue;
|
SList<Sector> zeroQueue;
|
||||||
@ -894,7 +891,7 @@ void CompoundFile::FreeChain(SVector<Sector> §orChain, int nth_sector_to_rem
|
|||||||
{
|
{
|
||||||
for (int i = nth_sector_to_remove; i < (int)sectorChain.size(); i++)
|
for (int i = nth_sector_to_remove; i < (int)sectorChain.size(); i++)
|
||||||
{
|
{
|
||||||
auto s = sectorChain[i];
|
auto& s = sectorChain[i];
|
||||||
s->ZeroData();
|
s->ZeroData();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -925,7 +922,7 @@ void CompoundFile::FreeChain(SVector<Sector> §orChain, bool zeroSector)
|
|||||||
|
|
||||||
void CompoundFile::AllocateSectorChain(SVector<Sector> §orChain)
|
void CompoundFile::AllocateSectorChain(SVector<Sector> §orChain)
|
||||||
{
|
{
|
||||||
for (auto& s : *sectorChain)
|
for (auto& s : *sectorChain) // todo check
|
||||||
{
|
{
|
||||||
if (s->id == -1)
|
if (s->id == -1)
|
||||||
{
|
{
|
||||||
@ -977,7 +974,7 @@ void CompoundFile::AllocateDIFATSectorChain(SVector<Sector> &FATsectorChain)
|
|||||||
header->fatSectorsNumber = FATsectorChain.size();
|
header->fatSectorsNumber = FATsectorChain.size();
|
||||||
|
|
||||||
// Allocate Sectors
|
// Allocate Sectors
|
||||||
for (auto& s : *FATsectorChain)
|
for (auto& s : *FATsectorChain) // todo check
|
||||||
{
|
{
|
||||||
if (s->id == -1)
|
if (s->id == -1)
|
||||||
{
|
{
|
||||||
@ -1046,7 +1043,8 @@ void CompoundFile::AllocateDIFATSectorChain(SVector<Sector> &FATsectorChain)
|
|||||||
// room for DIFAT chaining at the end of any DIFAT sector (4 bytes)
|
// 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)
|
if (i != HEADER_DIFAT_ENTRIES_COUNT && (i - HEADER_DIFAT_ENTRIES_COUNT) % DIFAT_SECTOR_FAT_ENTRIES_COUNT == 0)
|
||||||
{
|
{
|
||||||
difatStream.write(reinterpret_cast<const char*>(0L), sizeof(int));
|
int zero = 0;
|
||||||
|
difatStream.write(reinterpret_cast<const char*>(&zero), sizeof(int));
|
||||||
}
|
}
|
||||||
|
|
||||||
difatStream.write(reinterpret_cast<const char*>(&FATsectorChain[i]->id), sizeof(int));
|
difatStream.write(reinterpret_cast<const char*>(&FATsectorChain[i]->id), sizeof(int));
|
||||||
@ -1069,7 +1067,7 @@ void CompoundFile::AllocateDIFATSectorChain(SVector<Sector> &FATsectorChain)
|
|||||||
|
|
||||||
|
|
||||||
// Chain first sector
|
// 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;
|
header->firstDIFATSectorID = difatStream.BaseSectorChain()[0]->id;
|
||||||
|
|
||||||
@ -1086,7 +1084,8 @@ void CompoundFile::AllocateDIFATSectorChain(SVector<Sector> &FATsectorChain)
|
|||||||
std::copy_n(src, sizeof(int), dst+offsetDst);
|
std::copy_n(src, sizeof(int), dst+offsetDst);
|
||||||
}
|
}
|
||||||
|
|
||||||
char* src = const_cast<char*>(reinterpret_cast<const char*>(&Sector::ENDOFCHAIN));
|
auto eoc = Sector::ENDOFCHAIN;
|
||||||
|
char* src = reinterpret_cast<char*>(&eoc);
|
||||||
char* dst = reinterpret_cast<char *>(difatStream.BaseSectorChain()[difatStream.BaseSectorChain().size() - 1]->GetData().data());
|
char* dst = reinterpret_cast<char *>(difatStream.BaseSectorChain()[difatStream.BaseSectorChain().size() - 1]->GetData().data());
|
||||||
int offsetDst = GetSectorSize() - sizeof(int);
|
int offsetDst = GetSectorSize() - sizeof(int);
|
||||||
std::copy_n(src, sizeof(int), dst+offsetDst);
|
std::copy_n(src, sizeof(int), dst+offsetDst);
|
||||||
@ -1145,7 +1144,7 @@ void CompoundFile::AllocateMiniSectorChain(SVector<Sector> §orChain)
|
|||||||
// We are writing data in a NORMAL Sector chain.
|
// We are writing data in a NORMAL Sector chain.
|
||||||
for (int i = 0; i < (int)sectorChain.size(); i++)
|
for (int i = 0; i < (int)sectorChain.size(); i++)
|
||||||
{
|
{
|
||||||
std::shared_ptr<Sector> s = sectorChain[i];
|
std::shared_ptr<Sector>& s = sectorChain[i];
|
||||||
|
|
||||||
if (s->id == -1)
|
if (s->id == -1)
|
||||||
{
|
{
|
||||||
@ -1219,7 +1218,7 @@ int CompoundFile::LowSaturation(int i)
|
|||||||
|
|
||||||
void CompoundFile::SetSectorChain(SVector<Sector> sectorChain)
|
void CompoundFile::SetSectorChain(SVector<Sector> sectorChain)
|
||||||
{
|
{
|
||||||
if (sectorChain.size() == 0)
|
if (sectorChain != nullptr && sectorChain.size() == 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
SectorType _st = sectorChain[0]->type;
|
SectorType _st = sectorChain[0]->type;
|
||||||
@ -1361,7 +1360,7 @@ void CompoundFile::SetStreamLength(std::shared_ptr<CFItem> cfItem, std::streamsi
|
|||||||
|
|
||||||
bool transitionToMini = false;
|
bool transitionToMini = false;
|
||||||
bool transitionToNormal = false;
|
bool transitionToNormal = false;
|
||||||
SVector<Sector> oldChain;
|
SVector<Sector> oldChain(nullptr);
|
||||||
|
|
||||||
if (cfItem->dirEntry.lock()->getStartSetc() != Sector::ENDOFCHAIN)
|
if (cfItem->dirEntry.lock()->getStartSetc() != Sector::ENDOFCHAIN)
|
||||||
{
|
{
|
||||||
@ -1752,12 +1751,15 @@ GUID CompoundFile::getGuidForStream(int sid)
|
|||||||
return g;
|
return g;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CompoundFile::WriteData(std::shared_ptr<CFItem> cfItem, const std::vector<BYTE> &buffer, std::streamsize position, int offset, int count)
|
void CompoundFile::WriteData(std::shared_ptr<CFItem> cfItem, const char* data, std::streamsize position, int count)
|
||||||
{
|
{
|
||||||
|
if (data == nullptr)
|
||||||
|
throw CFInvalidOperation("Parameter [data] cannot be null");
|
||||||
|
|
||||||
if (cfItem->dirEntry.expired())
|
if (cfItem->dirEntry.expired())
|
||||||
throw CFException("Internal error [cfItem->dirEntry] cannot be null");
|
throw CFException("Internal error [cfItem->dirEntry] cannot be null");
|
||||||
|
|
||||||
if (buffer.size() == 0) return;
|
if (count == 0) return;
|
||||||
|
|
||||||
// Get delta size induced by client
|
// Get delta size induced by client
|
||||||
std::streamsize delta = (position + count) - cfItem->size() < 0 ? 0 : (position + count) - cfItem->size();
|
std::streamsize delta = (position + count) - cfItem->size() < 0 ? 0 : (position + count) - cfItem->size();
|
||||||
@ -1780,7 +1782,7 @@ void CompoundFile::WriteData(std::shared_ptr<CFItem> cfItem, const std::vector<B
|
|||||||
StreamView sv(sectorChain, _sectorSize, newLength, zeroQueue, sourceStream);
|
StreamView sv(sectorChain, _sectorSize, newLength, zeroQueue, sourceStream);
|
||||||
|
|
||||||
sv.seek(position, std::ios::beg);
|
sv.seek(position, std::ios::beg);
|
||||||
sv.write(reinterpret_cast<const char*>(buffer.data() + offset), count);
|
sv.write(data, count);
|
||||||
|
|
||||||
if (cfItem->size() < header->minSizeStandardStream)
|
if (cfItem->size() < header->minSizeStandardStream)
|
||||||
{
|
{
|
||||||
@ -1788,6 +1790,11 @@ void CompoundFile::WriteData(std::shared_ptr<CFItem> cfItem, const std::vector<B
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CompoundFile::WriteData(std::shared_ptr<CFItem> cfItem, const std::vector<BYTE> &buffer, std::streamsize position, int offset, int count)
|
||||||
|
{
|
||||||
|
WriteData(cfItem, reinterpret_cast<const char*>(buffer.data() + offset), position, count);
|
||||||
|
}
|
||||||
|
|
||||||
int CompoundFile::GetSectorSize()
|
int CompoundFile::GetSectorSize()
|
||||||
{
|
{
|
||||||
return 2 << (header->sectorShift - 1);
|
return 2 << (header->sectorShift - 1);
|
||||||
|
|||||||
@ -63,6 +63,7 @@ public:
|
|||||||
void InvalidateDirectoryEntry(int sid);
|
void InvalidateDirectoryEntry(int sid);
|
||||||
void FreeAssociatedData(int sid);
|
void FreeAssociatedData(int sid);
|
||||||
void FreeData(CFStream* stream);
|
void FreeData(CFStream* stream);
|
||||||
|
void WriteData(std::shared_ptr<CFItem> cfItem, const char* data, std::streamsize position, int count);
|
||||||
void WriteData(std::shared_ptr<CFItem> cfItem, const std::vector<BYTE>& buffer, std::streamsize position, int offset, int count);
|
void WriteData(std::shared_ptr<CFItem> cfItem, const std::vector<BYTE>& buffer, std::streamsize position, int offset, int count);
|
||||||
void WriteData(std::shared_ptr<CFItem> cfItem, std::streamsize position, const std::vector<BYTE>& buffer);
|
void WriteData(std::shared_ptr<CFItem> cfItem, std::streamsize position, const std::vector<BYTE>& buffer);
|
||||||
void WriteData(std::shared_ptr<CFItem> cfItem, const std::vector<BYTE>& buffer);
|
void WriteData(std::shared_ptr<CFItem> cfItem, const std::vector<BYTE>& buffer);
|
||||||
|
|||||||
@ -21,6 +21,9 @@ public:
|
|||||||
init(res);
|
init(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SVector(const SVectorBasePtr<T>& oth) : SVectorBasePtr<T>(oth)
|
||||||
|
{}
|
||||||
|
|
||||||
void init(size_t res = 0)
|
void init(size_t res = 0)
|
||||||
{
|
{
|
||||||
SVectorBasePtr<T>::reset(new SVectorBase<T>(res));
|
SVectorBasePtr<T>::reset(new SVectorBase<T>(res));
|
||||||
|
|||||||
@ -75,7 +75,7 @@ TEST_F(CompoundFileTest, test_compoundfile_write)
|
|||||||
TEST(test_compoundfile, largeFileCopy)
|
TEST(test_compoundfile, largeFileCopy)
|
||||||
{
|
{
|
||||||
wstring filename = L"../../../data/2.ppt";
|
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";
|
wstring other_filename = L"../../../data/3.ppt";
|
||||||
NSFile::CFileBinary::Remove(other_filename);
|
NSFile::CFileBinary::Remove(other_filename);
|
||||||
@ -261,7 +261,8 @@ TEST(test_compoundfile, largeStream_v3_v4)
|
|||||||
|
|
||||||
for (LONG64 i = 0; i < streamLen; i += data.size())
|
for (LONG64 i = 0; i < streamLen; i += data.size())
|
||||||
{
|
{
|
||||||
EXPECT_NO_THROW(stream1->Write(data, i));
|
|
||||||
|
EXPECT_NO_THROW(stream1->Write(reinterpret_cast<char*>(&i), i, sizeof(i)));
|
||||||
// EXPECT_NO_THROW(stream2->Write(data, i));
|
// EXPECT_NO_THROW(stream2->Write(data, i));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user