mirror of
https://github.com/ONLYOFFICE/core.git
synced 2026-02-10 18:05:41 +08:00
fixed problems with memory
This commit is contained in:
@ -88,35 +88,33 @@ public:
|
||||
virtual bool read (const std::wstring& path, BYTE*& data, DWORD& length)
|
||||
{
|
||||
std::string sPath = normalPath(path);
|
||||
std::pair<DWORD, BYTE*> oRes = m_zlib->getFile(sPath);
|
||||
if (oRes.first)
|
||||
{
|
||||
length = oRes.first;
|
||||
data = oRes.second;
|
||||
m_zlib->getFile(sPath, data, length);
|
||||
if (length)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
virtual void write (const std::wstring& path, BYTE* data, DWORD length)
|
||||
{
|
||||
std::string sPath = normalPath(path);
|
||||
m_zlib->addFile(sPath, data, length);
|
||||
BYTE* copyData = new BYTE[length];
|
||||
memcpy(copyData, data, length);
|
||||
m_zlib->addFile(sPath, copyData, length);
|
||||
}
|
||||
virtual void move (const std::wstring& sSrc, const std::wstring& sDst)
|
||||
{
|
||||
std::string sSrcPath = normalPath(sSrc);
|
||||
std::pair<DWORD, BYTE*> oFile = m_zlib->getFile(sSrcPath);
|
||||
BYTE* data; DWORD length;
|
||||
m_zlib->getFile(sSrcPath, data, length);
|
||||
BYTE* copyData = new BYTE[length];
|
||||
memcpy(copyData, data, length);
|
||||
m_zlib->removeFile(sSrcPath);
|
||||
m_zlib->addFile(normalPath(sDst), oFile.second, oFile.first);
|
||||
m_zlib->addFile(normalPath(sDst), copyData, length);
|
||||
}
|
||||
virtual bool exists(const std::wstring& path)
|
||||
{
|
||||
std::string sPath = normalPath(path);
|
||||
std::vector<std::string> sPaths = m_zlib->getPaths();
|
||||
for (std::string& i : sPaths)
|
||||
if (i == sPath)
|
||||
return true;
|
||||
return false;
|
||||
return std::find(sPaths.begin(), sPaths.end(), sPath) != sPaths.end();
|
||||
}
|
||||
virtual void remove(const std::wstring& path)
|
||||
{
|
||||
@ -128,18 +126,13 @@ public:
|
||||
}
|
||||
virtual void writeZipFolder(BYTE*& data, DWORD& length)
|
||||
{
|
||||
std::pair<DWORD, BYTE*> oRes = m_zlib->save();
|
||||
length = oRes.first;
|
||||
data = new BYTE[length];
|
||||
memcpy(data, oRes.second, length);
|
||||
m_zlib->save(data, length);
|
||||
m_zlib->close();
|
||||
}
|
||||
virtual void writeXml(const std::wstring& path, const std::wstring& xml)
|
||||
{
|
||||
BYTE* pData = NULL;
|
||||
LONG nLen = 0;
|
||||
NSFile::CUtf8Converter::GetUtf8StringFromUnicode(xml.c_str(), xml.length(), pData, nLen);
|
||||
write(path, pData, nLen);
|
||||
std::string sXmlUtf8 = U_TO_UTF8(xml);
|
||||
write(path, (BYTE*)sXmlUtf8.c_str(), (DWORD)sXmlUtf8.length());
|
||||
}
|
||||
virtual std::vector<std::wstring> getFiles(const std::wstring& path, bool bIsRecursion)
|
||||
{
|
||||
|
||||
@ -183,6 +183,7 @@ public:
|
||||
long nLength = dwLen;
|
||||
oRels.CheckOriginSigs(pDataNew, nLength);
|
||||
m_pZipFolder->write(m_sFolder + file, pDataNew, nLength);
|
||||
RELEASEARRAYOBJECTS(pDataNew);
|
||||
|
||||
// удалим все лишнее
|
||||
std::vector<COOXMLRelationship>::iterator i = oRels.rels.begin();
|
||||
|
||||
@ -188,23 +188,27 @@ unsigned char* Zlib_GetFile (CZipBuffer* p, const char* path)
|
||||
{
|
||||
if (!p)
|
||||
return NULL;
|
||||
std::pair<unsigned long, unsigned char*> oRes = p->getFile(path);
|
||||
if (oRes.first == 0)
|
||||
unsigned char* data;
|
||||
unsigned long length;
|
||||
p->getFile(path, data, length);
|
||||
if (length == 0)
|
||||
return NULL;
|
||||
|
||||
CData* oData = new CData();
|
||||
oData->WriteString(oRes.second, oRes.first);
|
||||
oData->WriteString(data, length);
|
||||
return oData->GetBuffer();
|
||||
}
|
||||
unsigned char* Zlib_Save (CZipBuffer* p)
|
||||
{
|
||||
if (!p)
|
||||
return NULL;
|
||||
std::pair<unsigned long, unsigned char*> oRes = p->save();
|
||||
if (oRes.first == 0)
|
||||
unsigned char* data;
|
||||
unsigned long length;
|
||||
p->save(data, length);
|
||||
if (length == 0)
|
||||
return NULL;
|
||||
|
||||
CData* oData = new CData();
|
||||
oData->WriteString(oRes.second, oRes.first);
|
||||
oData->WriteString(data, length);
|
||||
return oData->GetBuffer();
|
||||
}
|
||||
|
||||
@ -16,10 +16,6 @@ zipFile zipOpenHelp(BUFFER_IO* buffer)
|
||||
fill_buffer_filefunc(&ffunc, buffer);
|
||||
return zipOpen2(NULL, APPEND_STATUS_CREATE, NULL, &ffunc);
|
||||
}
|
||||
unsigned int GetLength(BYTE* x)
|
||||
{
|
||||
return x[0] | x[1] << 8 | x[2] << 16 | x[3] << 24;
|
||||
}
|
||||
|
||||
// begin from (ZipUtilsCP.cpp)
|
||||
bool current_file_is_find(unzFile uf, const char* filename)
|
||||
@ -108,10 +104,12 @@ void CZipBuffer::open(BYTE* buffer, DWORD size)
|
||||
if (file_info.uncompressed_size != 0)
|
||||
m_arrFiles.push_back(CFile(get_filename_from_unzfile(uf), NULL, 0));
|
||||
} while (UNZ_OK == unzGoToNextFile(uf));
|
||||
RELEASEOBJECT(buf);
|
||||
}
|
||||
void CZipBuffer::close()
|
||||
{
|
||||
RELEASEARRAYOBJECTS(m_zipFile);
|
||||
for (CFile& oFile : m_arrFiles)
|
||||
RELEASEARRAYOBJECTS(oFile.m_pData);
|
||||
m_arrFiles.clear();
|
||||
}
|
||||
|
||||
@ -122,69 +120,78 @@ std::vector<std::string> CZipBuffer::getPaths()
|
||||
oRes.push_back(oFile.m_sPath);
|
||||
return oRes;
|
||||
}
|
||||
std::pair<DWORD, BYTE*> CZipBuffer::save()
|
||||
void CZipBuffer::save(BYTE*& data, DWORD& length)
|
||||
{
|
||||
BUFFER_IO* buf = new BUFFER_IO;
|
||||
buf->bGrow = 1;
|
||||
buf->nCurrentPos = 0;
|
||||
zipFile zip_file_handle = zipOpenHelp(buf);
|
||||
|
||||
for (CFile& oFile : m_arrFiles)
|
||||
if (!oFile.m_nLength)
|
||||
getFile(oFile.m_sPath, oFile.m_pData, oFile.m_nLength);
|
||||
|
||||
for (CFile& oFile : m_arrFiles)
|
||||
{
|
||||
getFile(oFile.m_sPath);
|
||||
if (ZIP_OK != zipOpenNewFileInZip( zip_file_handle, oFile.m_sPath.c_str(), NULL, NULL, 0, NULL, 0, NULL, Z_DEFLATED, -1 ) ||
|
||||
ZIP_OK != zipWriteInFileInZip(zip_file_handle, oFile.m_pData, oFile.m_nLength) ||
|
||||
ZIP_OK != zipCloseFileInZip(zip_file_handle))
|
||||
{
|
||||
zipClose(zip_file_handle, NULL);
|
||||
return make_pair(m_sizeZip, m_zipFile);
|
||||
data = NULL;
|
||||
length = 0;
|
||||
return;
|
||||
}
|
||||
}
|
||||
zipClose(zip_file_handle, NULL);
|
||||
|
||||
m_zipFile = buf->buffer;
|
||||
m_sizeZip = buf->nSize;
|
||||
return make_pair(m_sizeZip, m_zipFile);
|
||||
data = m_zipFile = buf->buffer;
|
||||
length = m_sizeZip = buf->nCurrentPos;
|
||||
RELEASEOBJECT(buf);
|
||||
}
|
||||
std::pair<DWORD, BYTE*> CZipBuffer::getFile(const std::string& sPath)
|
||||
void CZipBuffer::getFile(const std::string& sPath, BYTE*& data, DWORD& length)
|
||||
{
|
||||
BYTE* file = NULL;
|
||||
DWORD nFileSize = 0;
|
||||
std::vector<CFile>::iterator it = std::find_if(m_arrFiles.begin(), m_arrFiles.end(), [sPath] (const CFile& oFile) { return oFile.m_sPath == sPath; });
|
||||
std::vector<CFile>::iterator it =
|
||||
std::find_if(m_arrFiles.begin(), m_arrFiles.end(), [&sPath] (const CFile& oFile) { return oFile.m_sPath == sPath; });
|
||||
if (it == m_arrFiles.end())
|
||||
return make_pair(nFileSize, file);
|
||||
{
|
||||
data = NULL;
|
||||
length = 0;
|
||||
return;
|
||||
}
|
||||
if (it->m_nLength)
|
||||
return make_pair(it->m_nLength, it->m_pData);
|
||||
{
|
||||
data = it->m_pData;
|
||||
length = it->m_nLength;
|
||||
return;
|
||||
}
|
||||
|
||||
BUFFER_IO* buf = new BUFFER_IO;
|
||||
buf->buffer = m_zipFile;
|
||||
buf->nSize = m_sizeZip;
|
||||
unzFile uf = unzOpenHelp(buf);
|
||||
|
||||
file = new BYTE;
|
||||
get_file_in_archive(uf, sPath.c_str(), &file, nFileSize);
|
||||
data = new BYTE;
|
||||
length = 0;
|
||||
get_file_in_archive(uf, sPath.c_str(), &data, length);
|
||||
unzClose(uf);
|
||||
|
||||
it->m_nLength = nFileSize;
|
||||
it->m_pData = file;
|
||||
return make_pair(nFileSize, file);
|
||||
it->m_nLength = length;
|
||||
it->m_pData = data;
|
||||
RELEASEOBJECT(buf);
|
||||
}
|
||||
void CZipBuffer::addFile (const std::string& sPath, BYTE* data, DWORD length)
|
||||
{
|
||||
bool bExists = false;
|
||||
for (CFile& oFile : m_arrFiles)
|
||||
{
|
||||
if (oFile.m_sPath == sPath)
|
||||
{
|
||||
RELEASEARRAYOBJECTS(oFile.m_pData);
|
||||
oFile.m_pData = data;
|
||||
oFile.m_nLength = length;
|
||||
bExists = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!bExists)
|
||||
std::vector<CFile>::iterator it =
|
||||
std::find_if(m_arrFiles.begin(), m_arrFiles.end(), [&sPath] (const CFile& oFile) { return oFile.m_sPath == sPath; });
|
||||
if (it == m_arrFiles.end())
|
||||
m_arrFiles.push_back(CFile(sPath, data, length));
|
||||
else
|
||||
{
|
||||
RELEASEARRAYOBJECTS(it->m_pData);
|
||||
it->m_pData = data;
|
||||
it->m_nLength = length;
|
||||
}
|
||||
}
|
||||
bool CZipBuffer::removeFile(const std::string& sPath)
|
||||
{
|
||||
|
||||
@ -40,8 +40,8 @@ public:
|
||||
void close();
|
||||
|
||||
std::vector<std::string> getPaths();
|
||||
std::pair<DWORD, BYTE*> save();
|
||||
std::pair<DWORD, BYTE*> getFile(const std::string& sPath);
|
||||
void save(BYTE*& data, DWORD& length);
|
||||
void getFile(const std::string& sPath, BYTE*& data, DWORD& length);
|
||||
void addFile (const std::string& sPath, BYTE* data, DWORD length);
|
||||
bool removeFile(const std::string& sPath);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user