Commit e299e59d authored by ElenaSubbotina's avatar ElenaSubbotina

fix write compaund files

parent 1e0f4d6f
......@@ -115,7 +115,8 @@ void CompoundFile::copy_stream(std::string streamNameOpen, std::string streamNam
POLE::Stream *streamNew = new POLE::Stream(storageOut, streamNameCreate, true, size_stream);
if (!streamNew) return;
unsigned char* data_stream = new unsigned char[size_stream];
unsigned char* data_stream = new unsigned char[size_stream + 64];
memset(data_stream, 0, size_stream + 64);
if (data_stream)
{
stream->read(data_stream, size_stream);
......@@ -140,16 +141,15 @@ void CompoundFile::copy( int indent, std::string path, POLE::Storage * storageOu
for( std::list<std::string>::iterator it = entries.begin(); it != entries.end(); it++ )
{
std::string name = *it;
std::string fullname = path + name;
std::string fullname = path + *it;
if( storage_->isDirectory( fullname ) )
if ((it->at(0) >= 32) && (storage_->isDirectory( fullname ) ))
{
entries_dir.push_back(name);
entries_dir.push_back(*it);
}
else
{
entries_files.push_front(name);
entries_files.push_front(*it);
}
}
for( std::list<std::string>::iterator it = entries_dir.begin(); it != entries_dir.end(); it++ )
......@@ -158,8 +158,8 @@ void CompoundFile::copy( int indent, std::string path, POLE::Storage * storageOu
copy( indent + 1, fullname + "/", storageOut, bWithRoot, bSortFiles );
}
entries_files.sort();
//if (bSortFiles)
entries_files.sort();
for( std::list<std::string>::iterator it = entries_files.begin(); it != entries_files.end(); it++ )
{
......
......@@ -31,7 +31,7 @@
*/
#include "SheetExt.h"
#include <Logic/Biff_structures/FrtHeader.h>
#include "../Biff_structures/FrtHeader.h"
namespace XLS
{
......@@ -70,31 +70,5 @@ void SheetExt::readFields(CFRecord& record)
}
}
int SheetExt::serialize(std::wostream & stream)
{
if (!sheetExtOptional.bEnabled) return 0;
CP_XML_WRITER(stream)
{
CP_XML_NODE(L"sheetPr")
{
if (!sheetExtOptional.fCondFmtCalc)
CP_XML_ATTR(L"enableFormatConditionsCalculation", false);
if (!sheetExtOptional.fNotPublished)
CP_XML_ATTR(L"published" ,false);
if (sheetExtOptional.color.xclrType.type == XColorType::XCLRRGB)
{
CP_XML_NODE(L"tabColor")
{
CP_XML_ATTR(L"rgb", sheetExtOptional.color.rgb.strARGB);
}
}
}
}
return 0;
}
} // namespace XLS
......@@ -32,13 +32,11 @@
#pragma once
#include "BiffRecord.h"
#include <Logic/Biff_structures/SheetExtOptional.h>
#include "../Biff_structures/SheetExtOptional.h"
namespace XLS
{
// Logical representation of SheetExt record in BIFF8
class SheetExt: public BiffRecord
{
BIFF_RECORD_DEFINE_TYPE_INFO(SheetExt)
......@@ -48,13 +46,11 @@ public:
~SheetExt();
BaseObjectPtr clone();
void readFields(CFRecord& record);
static const ElementType type = typeSheetExt;
static const ElementType type = typeSheetExt;
int serialize(std::wostream & stream);
//-----------------------------
_UINT32 cb;
unsigned char icvPlain;
......
......@@ -32,8 +32,7 @@
#pragma once
#include "BiffStructure.h"
#include <Logic/Biff_structures/CFColor.h>
//#include <Logic/Biff_structures/BiffString.h>
#include "CFColor.h"
namespace XLS
{
......
......@@ -150,7 +150,14 @@ const bool ChartSheetSubstream::loadContent(BinProcessor& proc)
switch(type)
{
case rt_WriteProtect: proc.optional<WriteProtect>(); break;
case rt_SheetExt: proc.optional<SheetExt>(); break;
case rt_SheetExt:
{
if (proc.optional<SheetExt>())
{
m_SheetExt = elements_.back();
elements_.pop_back();
}
}break;
case rt_WebPub: proc.optional<WebPub>(); break;
case rt_HFPicture: proc.repeated<HFPicture>(0, 0); break;
......
......@@ -82,6 +82,8 @@ public:
BaseObjectPtr m_PivotChartBits;
BaseObjectPtr m_SBaseRef;
BaseObjectPtr m_CodeName;
BaseObjectPtr m_SheetExt;
private:
void recalc(CHARTFORMATS* charts);
......
......@@ -144,9 +144,17 @@ const bool MacroSheetSubstream::loadContent(BinProcessor& proc)
proc.repeated<SORT>(0, 2);
proc.optional<DxGCol>();
proc.optional<PHONETICINFO>();
proc.optional<CodeName>();
if (proc.optional<CodeName>())
{
m_CodeName = elements_.back();
elements_.pop_back();
}
proc.repeated<CellWatch>(0, 0);
proc.optional<SheetExt>();
if (proc.optional<SheetExt>())
{
m_SheetExt = elements_.back();
elements_.pop_back();
}
proc.repeated<FEAT>(0, 0);
proc.repeated<RECORD12>(0, 0);
proc.mandatory<EOF_T>();
......
......@@ -54,6 +54,9 @@ public:
BaseObjectPtr m_GLOBALS;
BaseObjectPtr m_OBJECTS;
std::vector<BaseObjectPtr> m_arCUSTOMVIEW;
BaseObjectPtr m_SheetExt;
BaseObjectPtr m_CodeName;
};
......
......@@ -503,7 +503,37 @@ void WorksheetSubstream::LoadHFPicture()
}
}
int WorksheetSubstream::serialize_format(std::wostream & strm)
{
SheetExt *sheet_ext = dynamic_cast<SheetExt*>(m_SheetExt.get());
CodeName *code_name = dynamic_cast<CodeName*>(m_CodeName.get());
CP_XML_WRITER(strm)
{
CP_XML_NODE(L"sheetPr")
{
if (code_name)
{
CP_XML_ATTR(L"codeName", code_name->value);
}
if ((sheet_ext) && (sheet_ext->sheetExtOptional.bEnabled))
{
if (!sheet_ext->sheetExtOptional.fCondFmtCalc)
CP_XML_ATTR(L"enableFormatConditionsCalculation", false);
if (!sheet_ext->sheetExtOptional.fNotPublished)
CP_XML_ATTR(L"published" ,false);
if (sheet_ext->sheetExtOptional.color.xclrType.type == XColorType::XCLRRGB)
{
CP_XML_NODE(L"tabColor")
{
CP_XML_ATTR(L"rgb", sheet_ext->sheetExtOptional.color.rgb.strARGB);
}
}
}
}
}
return 0;
}
} // namespace XLS
......@@ -50,6 +50,7 @@ public:
BaseObjectPtr clone();
virtual const bool loadContent (BinProcessor& proc);
int serialize_format(std::wostream & _stream);
static const ElementType type = typeWorksheetSubstream;
......
......@@ -431,11 +431,9 @@ void XlsConverter::convert (XLS::WorksheetSubstream* sheet)
{
sheet->m_Dimensions->serialize(xlsx_context->current_sheet().dimension());
}
if (sheet->m_SheetExt)
{
sheet->m_SheetExt->serialize(xlsx_context->current_sheet().sheetProperties());
// todooo + GLOBALS->WsBool
}
sheet->serialize_format(xlsx_context->current_sheet().sheetProperties());
if (sheet->m_GLOBALS)
{
XLS::GLOBALS * globals = dynamic_cast<XLS::GLOBALS *>(sheet->m_GLOBALS.get());
......
......@@ -399,13 +399,13 @@ void Header::save( unsigned char* buffer )
{
memset( buffer, 0, 0x4c );
memcpy( buffer, pole_magic, 8 ); // ole signature
writeU32( buffer + 8, 0 ); // unknown
writeU32( buffer + 12, 0 ); // unknown
writeU32( buffer + 16, 0 ); // unknown
writeU16( buffer + 24, 0x003e ); // revision ?
writeU16( buffer + 26, 3 ); // version ?
writeU16( buffer + 28, 0xfffe ); // unknown
writeU16( buffer + 0x1e, (uint32) b_shift );
writeU32( buffer + 8, 0 ); // reserved must be zero
writeU32( buffer + 12, 0 ); // reserved must be zero
writeU32( buffer + 16, 0 ); // reserved must be zero
writeU16( buffer + 24, 0x003e ); // minor version of the format: 33
writeU16( buffer + 26, 3 ); // major version (512 clasters)
writeU16( buffer + 28, 0xfffe ); // indicates Intel byte-ordering
writeU16( buffer + 0x1e, (uint32) b_shift ); //size of sectors in power-of-two
writeU16( buffer + 0x20, (uint32) s_shift );
writeU32( buffer + 0x2c, (uint32) num_bat );
writeU32( buffer + 0x30, (uint32) dirent_start );
......@@ -415,8 +415,8 @@ void Header::save( unsigned char* buffer )
writeU32( buffer + 0x44, (uint32) mbat_start );
writeU32( buffer + 0x48, (uint32) num_mbat );
for( unsigned int i=0; i<109; i++ )
writeU32( buffer + 0x4C+i*4, (uint32) bb_blocks[i] );
for( unsigned int i = 0; i < 109; i++ )
writeU32( buffer + 0x4C + i * 4, (uint32) bb_blocks[i] );
dirty = false;
}
......@@ -711,7 +711,7 @@ int64 DirTree::parent( uint64 index )
for( uint64 j=0; j<entryCount(); j++ )
{
std::vector<uint64> chi = children( j );
for( unsigned i=0; i<chi.size();i++ )
for( size_t i=0; i<chi.size();i++ )
if( chi[i] == index )
return j;
}
......@@ -748,7 +748,7 @@ std::string DirTree::fullName( uint64 index )
// if create is true, a new entry is returned
DirEntry* DirTree::entry( const std::string& name, bool create, int64 bigBlockSize, StorageIO *const io, int64 streamSize)
{
if( !name.length() ) return (DirEntry*)0;
if( name.empty() ) return (DirEntry*)0;
// quick check for "/" (that's root)
if( name == "/" ) return entry( 0 );
......@@ -778,23 +778,21 @@ DirEntry* DirTree::entry( const std::string& name, bool create, int64 bigBlockSi
{
// find among the children of index
levelsLeft--;
uint64 child = 0;
uint64 child = 0;
/*
// dima: this block is really inefficient
std::vector<unsigned> chi = children( index );
for( unsigned i = 0; i < chi.size(); i++ )
{
DirEntry* ce = entry( chi[i] );
if( ce )
if( ce->valid && ( ce->name.length()>1 ) )
if( ce->name == *it ) {
child = chi[i];
break;
}
}
*/
//std::vector<uint64> chi = children( index );
//for( size_t i = 0; i < chi.size(); i++ )
//{
// DirEntry* ce = entry( chi[i] );
// if( ce )
// if( ce->valid && ( ce->name !="/" ) )
// if( ce->name == *it ) {
// child = chi[i];
// break;
// }
//}
// dima: performance optimisation of the previous
uint64 closest = End;
child = find_child( index, *it, closest );
......@@ -804,14 +802,14 @@ DirEntry* DirTree::entry( const std::string& name, bool create, int64 bigBlockSi
{
index = child;
}
else
else if( !create || !io->writeable)
{
std::vector<uint64> chi = children( index );
for( unsigned i = 0; i < chi.size(); i++ )
for( size_t i = 0; i < chi.size(); i++ )
{
DirEntry* ce = entry( chi[i] );
if( ce )
if( ce->valid && ( ce->name.length()>1 ) )
if( ce->valid && ( ce->name !="/" )/*( ce->name.length()>1 )*/ )
if( ce->name == *it )
{
child = chi[i];
......@@ -1502,13 +1500,28 @@ void StorageIO::flush()
void StorageIO::close()
{
if( !opened ) return;
if (writeable)
{
file.seekg(0, std::ios::end );
filesize = static_cast<uint64>(file.tellg());
if (filesize % 512 != 0)
{
char padding[512];
memset(padding, 0, 512);
file.write(padding, (filesize / 512 + 1 ) * 512 - filesize);
fileCheck(file);
}
}
file.close();
opened = false;
std::list<Stream*>::iterator it;
for( it = streams.begin(); it != streams.end(); ++it )
delete *it;
streams.clear();
}
......@@ -1595,8 +1608,7 @@ bool StorageIO::deleteLeaf(DirEntry *entry, const std::string& fullName)
return true;
}
uint64 StorageIO::loadBigBlocks( std::vector<uint64> blocks,
unsigned char* data, uint64 maxlen )
uint64 StorageIO::loadBigBlocks( std::vector<uint64> blocks, unsigned char* data, uint64 maxlen )
{
// sentinel
if( !data ) return 0;
......@@ -1624,8 +1636,7 @@ uint64 StorageIO::loadBigBlocks( std::vector<uint64> blocks,
return bytes;
}
uint64 StorageIO::loadBigBlock( uint64 block,
unsigned char* data, uint64 maxlen )
uint64 StorageIO::loadBigBlock( uint64 block, unsigned char* data, uint64 maxlen )
{
// sentinel
if( !data ) return 0;
......@@ -1651,7 +1662,7 @@ uint64 StorageIO::saveBigBlocks( std::vector<uint64> blocks, uint64 offset, unsi
// write block one by one, seems fast enough
uint64 bytes = 0;
for( unsigned int i=0; (i < blocks.size() ) & ( bytes<len ); i++ )
for( size_t i=0; (i < blocks.size() ) & ( bytes<len ); i++ )
{
uint64 block = blocks[i];
uint64 pos = (bbat->blockSize * ( block+1 ) ) + offset;
......@@ -1686,8 +1697,7 @@ uint64 StorageIO::saveBigBlock( uint64 block, uint64 offset, unsigned char* data
}
// return number of bytes which has been read
uint64 StorageIO::loadSmallBlocks( std::vector<uint64> blocks,
unsigned char* data, uint64 maxlen )
uint64 StorageIO::loadSmallBlocks( std::vector<uint64> blocks, unsigned char* data, uint64 maxlen )
{
// sentinel
if( !data ) return 0;
......@@ -1725,8 +1735,7 @@ uint64 StorageIO::loadSmallBlocks( std::vector<uint64> blocks,
return bytes;
}
uint64 StorageIO::loadSmallBlock( uint64 block,
unsigned char* data, uint64 maxlen )
uint64 StorageIO::loadSmallBlock( uint64 block, unsigned char* data, uint64 maxlen )
{
// sentinel
if( !data ) return 0;
......
......@@ -189,17 +189,17 @@ bool AddFile2Storage(POLE::Storage* storage, std::string inFile, std::string nam
return true;
}
void AddDir2Storage(POLE::Storage* storage, std::wstring inDir, std::wstring name, int &nFolders, int &nFiles)
void AddDir2Storage(POLE::Storage* storage, std::string inDir, std::string name, int &nFolders, int &nFiles)
{
WIN32_FIND_DATA ffd;
WIN32_FIND_DATAA ffd;
HANDLE h;
std::wstring inDirLcl = inDir + "*";
std::wstring outNameLcl = name;
std::string inDirLcl = inDir + "*";
std::string outNameLcl = name;
const wchar_t *cInDir = inDirLcl.c_str();
h = FindFirstFile(cInDir, &ffd);
const char *cInDir = inDirLcl.c_str();
h = FindFirstFileA(cInDir, &ffd);
if (h == INVALID_HANDLE_VALUE)
return;
nFolders++;
......@@ -249,7 +249,7 @@ void AddDir2Storage(POLE::Storage* storage, std::wstring inDir, std::wstring nam
nFiles++;
}
}
while (FindNextFile(h, &ffd) != 0);
while (FindNextFileA(h, &ffd) != 0);
}
......@@ -264,7 +264,10 @@ void cmdOpen(std::list<std::string> &entries)
storage->close();
std::string ssName = entries.front();
entries.pop_front();
storage = new POLE::Storage(ssName.c_str());
std::wstring sName(ssName.begin(), ssName.end());
storage = new POLE::Storage(sName.c_str());
bool bWrite = false;
bool bCreate = false;
if (entries.size() > 0)
......@@ -313,7 +316,7 @@ void cmdStats(std::list<std::string> &entries)
{
if (storage)
{
unsigned int nDirs, nUUDirs, nBBs, nUUBBs, nSBs, nUUSBs;
unsigned __int64 nDirs, nUUDirs, nBBs, nUUBBs, nSBs, nUUSBs;
storage->GetStats(&nDirs, &nUUDirs, &nBBs, &nUUBBs, &nSBs, &nUUSBs);
printf("%d Directory Entries, %d unused.\n", nDirs, nUUDirs);
printf("%d Big Blocks, %d unused.\n", nBBs, nUUBBs);
......@@ -334,7 +337,7 @@ void cmdVisit(std::list<std::string> &entries)
}
}
void cmdExtract(std::list<std::wstring> &entries)
void cmdExtract(std::list<std::string> &entries)
{
if (!storage)
std::cout << "No storage is opened." << std::endl;
......@@ -342,9 +345,9 @@ void cmdExtract(std::list<std::wstring> &entries)
std::cout << "You must specify a path in the open storage to be extracted, and a destination file or folder." << std::endl;
else
{
std::wstring ssPath = entries.front();
std::string ssPath = entries.front();
entries.pop_front();
std::wstring filePath = entries.front();
std::string filePath = entries.front();
if (storage->isDirectory(ssPath))
{
if (filePath[filePath.size()-1] != '/')
......@@ -366,7 +369,7 @@ void cmdExtract(std::list<std::wstring> &entries)
}
}
void cmdAdd(std::list<std::wstring> &entries)
void cmdAdd(std::list<std::string> &entries)
{
if (!storage)
std::cout << "No storage is opened." << std::endl;
......@@ -378,9 +381,9 @@ void cmdAdd(std::list<std::wstring> &entries)
std::cout << "The specified storage node already exists - to save it again, first delete it." << std::endl;
else
{
std::wstring ssPath = entries.front();
std::string ssPath = entries.front();
entries.pop_front();
std::wstring filePath = entries.front();
std::string filePath = entries.front();
if (DirectoryExists(filePath.c_str()))
{
if (filePath[filePath.size()-1] != '/')
......@@ -481,67 +484,77 @@ std::list<std::string> clineParse(std::string inCmd)
return outWords;
}
//int main(int argc, char* argv[])
//{
// if( argc > 1 )
// {
// std::cout << "Usage:" << std::endl;
// std::cout << argv[0] << " This takes no arguments - type help in the console window for information. " << std::endl;
// return 0;
// }
//
// std::string command;
// while (true)
// {
// std::getline(std::cin, command);
// std::list<std::string> entries = clineParse(command);
// if (entries.size() == 0)
// continue;
// std::string cmdWord = entries.front();
// entries.pop_front();
// if (cmdWord.compare("open") == 0)
// cmdOpen(entries);
// else if (cmdWord.compare("visit") == 0)
// cmdVisit(entries);
// else if (cmdWord.compare("extract") == 0)
// cmdExtract(entries);
// else if (cmdWord.compare("add") == 0)
// cmdAdd(entries);
// else if (cmdWord.compare("delete") == 0)
// cmdDelete(entries);
// else if (cmdWord.compare("stats") == 0)
// cmdStats(entries);
// else if (cmdWord.compare("close") == 0)
// cmdClose(entries);
// else if (cmdWord.compare("quit") == 0)
// {
// if (cmdQuit(entries))
// break;
// }
// else
// cmdHelp(entries);
// }
//#if 0
// std::list<std::string>::iterator it;
// for( it = entries.begin(); it != entries.end(); ++it )
// std::cout << *it << std::endl;
// open filename [modifier] (modifier can be r, w, c)
//visit [sspath]
//extract sspath fpath
//add sspath fpath
//delete sspath
//close
//
// std::string indir = argv[1];
// char* outfile = argv[2];
// if (indir[indir.length()-1] != '\\')
// indir += "\\";
// POLE::Storage* storage = new POLE::Storage(outfile);
// storage->open(true, true);
// AddDir2Storage(storage, indir, "/");
// storage->close();
// storage->open();
// visit(0, storage, "/");
// storage->close();
//#endif
//}
\ No newline at end of file
int main(int argc, char* argv[])
{
//if( argc > 1 )
//{
// std::cout << "Usage:" << std::endl;
// std::cout << argv[0] << " This takes no arguments - type help in the console window for information. " << std::endl;
// return 0;
//}
std::list<std::string> entries;
entries.push_back("open");
entries.push_back(argv[2]);
entries.push_back("cw");
entries.push_back("add");
entries.push_back("/");
entries.push_back(argv[1]);
entries.push_back("close");
std::string command;
while (true)
{
//std::getline(std::cin, command);
//std::list<std::string> entries = clineParse(command);
if (entries.size() == 0)
continue;
std::string cmdWord = entries.front();
entries.pop_front();
if (cmdWord.compare("open") == 0)
cmdOpen(entries);
else if (cmdWord.compare("visit") == 0)
cmdVisit(entries);
else if (cmdWord.compare("extract") == 0)
cmdExtract(entries);
else if (cmdWord.compare("add") == 0)
cmdAdd(entries);
else if (cmdWord.compare("delete") == 0)
cmdDelete(entries);
else if (cmdWord.compare("stats") == 0)
cmdStats(entries);
else if (cmdWord.compare("close") == 0)
cmdClose(entries);
else if (cmdWord.compare("quit") == 0)
{
if (cmdQuit(entries))
break;
}
else
cmdHelp(entries);
}
#if 0
std::list<std::string>::iterator it;
for( it = entries.begin(); it != entries.end(); ++it )
std::cout << *it << std::endl;
open filename [modifier] (modifier can be r, w, c)
visit [sspath]
extract sspath fpath
add sspath fpath
delete sspath
close
std::string indir = argv[1];
char* outfile = argv[2];
if (indir[indir.length()-1] != '\\')
indir += "\\";
POLE::Storage* storage = new POLE::Storage(outfile);
storage->open(true, true);
AddDir2Storage(storage, indir, "/");
storage->close();
storage->open();
visit(0, storage, "/");
storage->close();
#endif
}
\ No newline at end of file
......@@ -5,22 +5,27 @@
int _tmain(int argc, _TCHAR* argv[])
{
//std::wstring srcFileName = L"D:\\test\\_crypted\\test-password-2016.docx";
std::wstring srcFileName = L"D:\\test\\_crypted\\test-111.docx";
std::wstring dstFileName = srcFileName + L"-mycrypt.docx";
std::wstring dstFileName2 = dstFileName + L".oox";
//std::wstring dstFileName = srcFileName + L".oox";
//std::wstring dstFileName2 = dstFileName + L"-mycrypt.docx";
std::wstring srcFileName = L"D:\\test\\_crypted\\test.docx";
std::wstring dstFileName2 = srcFileName + L"-mycrypt.docx";
std::wstring password = L"password132eqdqdwewedwdwwskskms09elzwewedskjsdnkjsdnjksjsnkcsdncskjdnss";
//std::wstring srcFileName = L"D:\\test\\_crypted\\test.docx";
//std::wstring dstFileName3 = srcFileName + L"-mycrypt1.docx";
std::wstring password = L"574446f1-6aa0-860a-0296-787a87a214bb";
ECMACryptFile crypt_file;
bool result, bDataIntegrity;
//result = crypt_file.DecryptOfficeFile(srcFileName, dstFileName, password, bDataIntegrity);
//result = crypt_file.EncryptOfficeFile(dstFileName, dstFileName2, password);
result = crypt_file.EncryptOfficeFile(srcFileName, dstFileName2, password);
result = crypt_file.EncryptOfficeFile(srcFileName, dstFileName, password);
result = crypt_file.DecryptOfficeFile(dstFileName, dstFileName2, password, bDataIntegrity);
return 0;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment