diff --git a/ASCOfficeDocFile/DocDocxConverter/CharacterPropertiesMapping.cpp b/ASCOfficeDocFile/DocDocxConverter/CharacterPropertiesMapping.cpp index 3b8c1217a2..16c55f8f87 100644 --- a/ASCOfficeDocFile/DocDocxConverter/CharacterPropertiesMapping.cpp +++ b/ASCOfficeDocFile/DocDocxConverter/CharacterPropertiesMapping.cpp @@ -256,7 +256,8 @@ namespace DocFileFormat if( nIndex < _doc->FontTable->cData ) { FontFamilyName* ffn = static_cast( _doc->FontTable->operator [] ( nIndex ) ); - this->m_sDefaultFont = ffn->xszFtn; + if (ffn) + this->m_sDefaultFont = ffn->xszFtn; } }break; case sprmCHpsBi : diff --git a/ASCOfficeDocFile/DocDocxConverter/CommentsMapping.h b/ASCOfficeDocFile/DocDocxConverter/CommentsMapping.h index 2dc77ab53b..e8619bb97c 100644 --- a/ASCOfficeDocFile/DocDocxConverter/CommentsMapping.h +++ b/ASCOfficeDocFile/DocDocxConverter/CommentsMapping.h @@ -61,6 +61,7 @@ namespace DocFileFormat while ( ( cp - m_document->FIB->m_RgLw97.ccpText - m_document->FIB->m_RgLw97.ccpFtn - m_document->FIB->m_RgLw97.ccpHdr ) < (*m_document->IndividualCommentsPlex)[index + 1] ) { int fc = m_document->FindFileCharPos(cp); + if (fc < 0) break; ParagraphPropertyExceptions* papx = findValidPapx(fc); TableInfo tai(papx); diff --git a/ASCOfficeDocFile/DocDocxConverter/DocumentMapping.cpp b/ASCOfficeDocFile/DocDocxConverter/DocumentMapping.cpp index 5826f94442..806510e0ab 100755 --- a/ASCOfficeDocFile/DocDocxConverter/DocumentMapping.cpp +++ b/ASCOfficeDocFile/DocDocxConverter/DocumentMapping.cpp @@ -126,6 +126,9 @@ namespace DocFileFormat int cp = initialCp; int fc = m_document->FindFileCharPos(cp); int fcEnd = m_document->FindFileCharPos(cpEnd); + + if (fc < 0 || fcEnd < 0) + return 0; ParagraphPropertyExceptions* papx = findValidPapx(fc); @@ -515,6 +518,7 @@ namespace DocFileFormat std::wstring mergeformat( _T( " MERGEFORMAT" ) ); std::wstring quote ( _T( " QUOTE" ) ); std::wstring chart ( _T( "Chart" ) ); + std::wstring PBrush ( _T( " PBrush" ) ); if ( search( f.begin(), f.end(), form.begin(), form.end() ) != f.end() ) { @@ -1039,6 +1043,7 @@ namespace DocFileFormat TableInfo tai( papx ); int fcRowEnd = findRowEndFc( cp, cp, nestingLevel ); + ParagraphPropertyExceptions* papx_prev = NULL; while ( tai.fInTable ) { @@ -1073,6 +1078,10 @@ namespace DocFileFormat papx = findValidPapx( fcRowEnd ); tai = TableInfo( papx ); fcRowEnd = findRowEndFc( cp, cp, nestingLevel ); + + if (papx_prev && papx_prev == papx ) + break;//file(12).doc + papx_prev = papx; } //build the grid based on the boundaries @@ -1123,8 +1132,12 @@ namespace DocFileFormat //Search the "table trailer paragraph" while ( ( tai.fTtp == false ) && ( tai.fInTable == true ) ) { - while ( m_document->Text->at( cp ) != TextMark::CellOrRowMark ) + while ( true ) { + if (cp >= m_document->Text->size()) + break; + if (m_document->Text->at( cp ) == TextMark::CellOrRowMark) + break; cp++; } diff --git a/ASCOfficeDocFile/DocDocxConverter/EndnotesMapping.h b/ASCOfficeDocFile/DocDocxConverter/EndnotesMapping.h index 9eeae25705..2ec5d48886 100644 --- a/ASCOfficeDocFile/DocDocxConverter/EndnotesMapping.h +++ b/ASCOfficeDocFile/DocDocxConverter/EndnotesMapping.h @@ -45,6 +45,7 @@ namespace DocFileFormat while ( ( cp - m_document->FIB->m_RgLw97.ccpText - m_document->FIB->m_RgLw97.ccpFtn - m_document->FIB->m_RgLw97.ccpHdr - m_document->FIB->m_RgLw97.ccpAtn ) < (*m_document->IndividualEndnotesPlex)[id + 1] ) { int fc = m_document->FindFileCharPos(cp); + if (fc < 0) break; ParagraphPropertyExceptions* papx = findValidPapx( fc ); TableInfo tai( papx ); diff --git a/ASCOfficeDocFile/DocDocxConverter/FooterMapping.cpp b/ASCOfficeDocFile/DocDocxConverter/FooterMapping.cpp index b3db26d699..62438d65ab 100644 --- a/ASCOfficeDocFile/DocDocxConverter/FooterMapping.cpp +++ b/ASCOfficeDocFile/DocDocxConverter/FooterMapping.cpp @@ -37,6 +37,8 @@ namespace DocFileFormat while ( cp < cpMax ) { int fc = m_document->FindFileCharPos(cp); + if (fc < 0) break; + ParagraphPropertyExceptions* papx = findValidPapx( fc ); TableInfo tai( papx ); diff --git a/ASCOfficeDocFile/DocDocxConverter/FootnotesMapping.h b/ASCOfficeDocFile/DocDocxConverter/FootnotesMapping.h index 9eae687b36..3f6386da57 100644 --- a/ASCOfficeDocFile/DocDocxConverter/FootnotesMapping.h +++ b/ASCOfficeDocFile/DocDocxConverter/FootnotesMapping.h @@ -45,6 +45,8 @@ namespace DocFileFormat while ( ( cp - m_document->FIB->m_RgLw97.ccpText ) < (*m_document->IndividualFootnotesPlex)[id + 1] ) { int fc = m_document->FindFileCharPos(cp); + if (fc < 0) break; + ParagraphPropertyExceptions* papx = findValidPapx( fc ); TableInfo tai( papx ); diff --git a/ASCOfficeDocFile/DocDocxConverter/HeaderMapping.cpp b/ASCOfficeDocFile/DocDocxConverter/HeaderMapping.cpp index 10bdec4780..fc4f2c744d 100644 --- a/ASCOfficeDocFile/DocDocxConverter/HeaderMapping.cpp +++ b/ASCOfficeDocFile/DocDocxConverter/HeaderMapping.cpp @@ -37,6 +37,8 @@ namespace DocFileFormat while ( cp < cpMax ) { int fc = m_document->FindFileCharPos(cp); + if (fc < 0) break; + ParagraphPropertyExceptions* papx = findValidPapx( fc ); TableInfo tai( papx ); diff --git a/ASCOfficeDocFile/DocDocxConverter/ListLevel.cpp b/ASCOfficeDocFile/DocDocxConverter/ListLevel.cpp index 973b03f070..ab38684f4f 100644 --- a/ASCOfficeDocFile/DocDocxConverter/ListLevel.cpp +++ b/ASCOfficeDocFile/DocDocxConverter/ListLevel.cpp @@ -63,9 +63,12 @@ namespace DocFileFormat //read the number text short strLen = reader->ReadInt16(); - bytes = reader->ReadBytes( ( strLen * 2 ), true ); - FormatUtils::GetSTLCollectionFromBytes( &(this->xst), bytes, ( strLen * 2 ), ENCODING_UNICODE ); - RELEASEARRAYOBJECTS( bytes ); + if (strLen > 0)//file(14).doc + { + bytes = reader->ReadBytes( ( strLen * 2 ), true ); + FormatUtils::GetSTLCollectionFromBytes( &(this->xst), bytes, ( strLen * 2 ), ENCODING_UNICODE ); + RELEASEARRAYOBJECTS( bytes ); + } long endPos = reader->GetPosition(); reader->Seek( startPos, 0/*STREAM_SEEK_SET*/ ); diff --git a/ASCOfficeDocFile/DocDocxConverter/MainDocumentMapping.cpp b/ASCOfficeDocFile/DocDocxConverter/MainDocumentMapping.cpp index 054ca3308c..6fe38e6655 100644 --- a/ASCOfficeDocFile/DocDocxConverter/MainDocumentMapping.cpp +++ b/ASCOfficeDocFile/DocDocxConverter/MainDocumentMapping.cpp @@ -65,6 +65,7 @@ namespace DocFileFormat while (cp < countText) { fc = m_document->FindFileCharPos(cp); + if (fc < 0) break; papx = findValidPapx(fc); @@ -96,6 +97,8 @@ namespace DocFileFormat int fc = m_document->FindFileCharPos(0); int fcEnd = m_document->FindFileCharPos(countTextRel); + + if (fc < 0 || fcEnd < 0) break; // Read the chars vector* chpxChars = m_document->m_PieceTable->GetEncodingChars (fc, fcEnd, m_document->WordDocumentStream); //FindFileCharPos(iter->second); int fcEnd = m_document->FindFileCharPos(iter->second + 1); + if (fc < 0 || fcEnd < 0 ) break; + list* chpxs = m_document->GetCharacterPropertyExceptions(fc, fcEnd); if ((chpxs != NULL) && (!chpxs->empty())) diff --git a/ASCOfficeDocFile/DocDocxConverter/PictureDescriptor.cpp b/ASCOfficeDocFile/DocDocxConverter/PictureDescriptor.cpp index 149cc8f10c..f0424b2fb6 100644 --- a/ASCOfficeDocFile/DocDocxConverter/PictureDescriptor.cpp +++ b/ASCOfficeDocFile/DocDocxConverter/PictureDescriptor.cpp @@ -123,7 +123,7 @@ namespace DocFileFormat } //Parse the OfficeDrawing Stuff - shapeContainer = static_cast(RecordFactory::ReadRecord(&reader, 0)); + shapeContainer = dynamic_cast(RecordFactory::ReadRecord(&reader, 0)); long pos = reader.GetPosition(); diff --git a/ASCOfficeDocFile/DocDocxConverter/StyleSheetMapping.cpp b/ASCOfficeDocFile/DocDocxConverter/StyleSheetMapping.cpp index 8cfc032294..514043e1d4 100644 --- a/ASCOfficeDocFile/DocDocxConverter/StyleSheetMapping.cpp +++ b/ASCOfficeDocFile/DocDocxConverter/StyleSheetMapping.cpp @@ -43,7 +43,7 @@ namespace DocFileFormat m_pXmlWriter->WriteNodeEnd( _T( "w:docDefaults" ) ); //write the default styles - if ( sheet->Styles->at( 11 ) == NULL ) + if ( (sheet->Styles->size() < 12) || (sheet->Styles->at( 11 ) == NULL )) { //NormalTable writeNormalTableStyle(); @@ -210,16 +210,20 @@ namespace DocFileFormat m_pXmlWriter->WriteNodeBegin( _T( "w:rFonts" ), TRUE ); FontFamilyName* ffnAscii = static_cast( m_document->FontTable->operator [] ( sheet->stshi->rgftcStandardChpStsh[0] ) ); - m_pXmlWriter->WriteAttribute( _T( "w:ascii" ), FormatUtils::XmlEncode(ffnAscii->xszFtn).c_str() ); + if (ffnAscii) + m_pXmlWriter->WriteAttribute( _T( "w:ascii" ), FormatUtils::XmlEncode(ffnAscii->xszFtn).c_str() ); FontFamilyName* ffnAsia = static_cast( m_document->FontTable->operator [] ( sheet->stshi->rgftcStandardChpStsh[1] ) ); - m_pXmlWriter->WriteAttribute( _T( "w:eastAsia" ), FormatUtils::XmlEncode(ffnAsia->xszFtn).c_str() ); + if (ffnAsia) + m_pXmlWriter->WriteAttribute( _T( "w:eastAsia" ), FormatUtils::XmlEncode(ffnAsia->xszFtn).c_str() ); FontFamilyName* ffnAnsi = static_cast( m_document->FontTable->operator [] ( sheet->stshi->rgftcStandardChpStsh[2] ) ); - m_pXmlWriter->WriteAttribute( _T( "w:hAnsi" ), FormatUtils::XmlEncode(ffnAnsi->xszFtn).c_str() ); + if (ffnAnsi) + m_pXmlWriter->WriteAttribute( _T( "w:hAnsi" ), FormatUtils::XmlEncode(ffnAnsi->xszFtn).c_str() ); FontFamilyName* ffnComplex = static_cast( m_document->FontTable->operator [] ( sheet->stshi->rgftcStandardChpStsh[3] ) ); - m_pXmlWriter->WriteAttribute( _T( "w:cs" ), FormatUtils::XmlEncode(ffnComplex->xszFtn).c_str() ); + if (ffnComplex) + m_pXmlWriter->WriteAttribute( _T( "w:cs" ), FormatUtils::XmlEncode(ffnComplex->xszFtn).c_str() ); m_pXmlWriter->WriteNodeEnd( _T( "" ), TRUE, FALSE ); m_pXmlWriter->WriteNodeEnd( _T( "w:rFonts" ) ); diff --git a/ASCOfficeDocFile/DocDocxConverter/TableMapping.cpp b/ASCOfficeDocFile/DocDocxConverter/TableMapping.cpp index 454f0d3dde..adb8d60ab0 100644 --- a/ASCOfficeDocFile/DocDocxConverter/TableMapping.cpp +++ b/ASCOfficeDocFile/DocDocxConverter/TableMapping.cpp @@ -264,6 +264,7 @@ namespace DocFileFormat bool Table::IsCellMarker(int _cp) { int fc = documentMapping->m_document->FindFileCharPos(_cp); + if (fc < 0) return false; ParagraphPropertyExceptions* papx = NULL; @@ -280,6 +281,7 @@ namespace DocFileFormat bool Table::IsRowMarker( int _cp ) { int fc = documentMapping->m_document->FindFileCharPos(_cp); + if (fc < 0) return false; ParagraphPropertyExceptions* papx = NULL; @@ -296,6 +298,7 @@ namespace DocFileFormat bool Table::IsParagraphMarker( int _cp ) { int fc = documentMapping->m_document->FindFileCharPos(_cp); + if (fc < 0) return false; ParagraphPropertyExceptions* papx = NULL; @@ -318,6 +321,7 @@ namespace DocFileFormat SectionPropertyExceptions* sepxBackup = this->documentMapping->_lastValidSepx; int fc = documentMapping->m_document->FindFileCharPos(_cp); + if (fc < 0) return; ParagraphPropertyExceptions* papx = NULL; @@ -331,6 +335,7 @@ namespace DocFileFormat do { fc = documentMapping->m_document->FindFileCharPos(_cp); + if (fc < 0) break; papx = this->documentMapping->findValidPapx( fc ); @@ -345,6 +350,7 @@ namespace DocFileFormat _cp = innerTable.GetCPEnd(); fc = documentMapping->m_document->FindFileCharPos(_cp); + if (fc < 0) break; papx = this->documentMapping->findValidPapx( fc ); diff --git a/ASCOfficeDocFile/DocDocxConverter/TextboxMapping.cpp b/ASCOfficeDocFile/DocDocxConverter/TextboxMapping.cpp index 55ec1582b6..ebeed179c6 100644 --- a/ASCOfficeDocFile/DocDocxConverter/TextboxMapping.cpp +++ b/ASCOfficeDocFile/DocDocxConverter/TextboxMapping.cpp @@ -83,10 +83,16 @@ namespace DocFileFormat if (typeid(*_caller) == typeid(MainDocumentMapping)) { - bkd = static_cast(m_document->TextboxBreakPlex->Elements[m_nTBIndex]); - - cp = txtbxSubdocStart + m_document->TextboxBreakPlex->CharacterPositions[m_nTBIndex]; - cpEnd = txtbxSubdocStart + m_document->TextboxBreakPlex->CharacterPositions[m_nTBIndex + 1]; + if (m_nTBIndex < m_document->TextboxBreakPlex->Elements.size() )//file(21).doc + { + bkd = static_cast(m_document->TextboxBreakPlex->Elements[m_nTBIndex]); + } + + if (m_nTBIndex < m_document->TextboxBreakPlex->CharacterPositions.size() - 1) + { + cp = txtbxSubdocStart + m_document->TextboxBreakPlex->CharacterPositions[m_nTBIndex]; + cpEnd = txtbxSubdocStart + m_document->TextboxBreakPlex->CharacterPositions[m_nTBIndex + 1]; + } } else if ((typeid(*_caller) == typeid(HeaderMapping)) || (typeid(*_caller) == typeid(FooterMapping))) { @@ -101,11 +107,17 @@ namespace DocFileFormat //convert the textbox text _lastValidPapx = (*(m_document->AllPapxFkps->begin()))->grppapx[0]; + ParagraphPropertyExceptions* papx_prev = NULL; while (cp < cpEnd) { int fc = m_document->FindFileCharPos(cp); + if (fc < 0) break; ParagraphPropertyExceptions* papx = findValidPapx( fc ); + if (papx_prev && papx_prev == papx)//file(21).doc + break; + papx_prev = papx; + TableInfo tai( papx ); if ( tai.fInTable ) diff --git a/ASCOfficeDocFile/DocDocxConverter/VirtualStreamReader.h b/ASCOfficeDocFile/DocDocxConverter/VirtualStreamReader.h index c7efe43464..0e45c115b4 100644 --- a/ASCOfficeDocFile/DocDocxConverter/VirtualStreamReader.h +++ b/ASCOfficeDocFile/DocDocxConverter/VirtualStreamReader.h @@ -27,7 +27,7 @@ public: { unsigned short rdUShort = 0; - if ( stream != NULL ) + if (( stream != NULL ) && (position + 2 < stream->size())) { stream->seek( position ); stream->read((unsigned char*)(&rdUShort), sizeof( rdUShort )); @@ -42,7 +42,7 @@ public: { short rdShort = 0; - if ( stream != NULL ) + if (( stream != NULL ) && (position + 2 < stream->size())) { stream->seek( position ); stream->read((unsigned char*)(&rdShort), sizeof( rdShort )); @@ -57,7 +57,7 @@ public: { int rdInt = 0; - if ( stream != NULL ) + if (( stream != NULL ) && (position + 4 < stream->size())) { stream->seek( position ); stream->read( (unsigned char*) &rdInt, sizeof( rdInt ) ); @@ -72,7 +72,7 @@ public: { int rdUInt = 0; - if ( stream != NULL ) + if (( stream != NULL ) && (position + 4 < stream->size())) { stream->seek( position ); stream->read((unsigned char*) &rdUInt, sizeof( rdUInt ) ); @@ -87,7 +87,7 @@ public: { unsigned char rdByte = 0; - if ( this->stream != NULL ) + if (( stream != NULL ) && (position + 1 < stream->size())) { stream->seek( position); stream->read( (unsigned char*)&rdByte, sizeof( rdByte ) ); @@ -105,6 +105,11 @@ public: if ( ( stream != NULL ) && ( count > 0 ) && isResultNeeded ) { + if (position + count > stream->size()) + { + if (position > stream->size()) count = 0; + else count = stream->size() - position; + } rdBytes = new unsigned char[count]; if ( rdBytes != NULL ) diff --git a/ASCOfficeDocFile/DocDocxConverter/WordDocument.cpp b/ASCOfficeDocFile/DocDocxConverter/WordDocument.cpp index cefa13c152..681dbd6dd9 100644 --- a/ASCOfficeDocFile/DocDocxConverter/WordDocument.cpp +++ b/ASCOfficeDocFile/DocDocxConverter/WordDocument.cpp @@ -179,7 +179,10 @@ namespace DocFileFormat } if (FIB->m_FibBase.fEncrypted) + { + Clear(); return AVS_ERROR_DRM; + } // Get the streams if (FIB->m_FibBase.fWhichTblStm) diff --git a/ASCOfficeDocFile/DocDocxConverter/WordDocument.h b/ASCOfficeDocFile/DocDocxConverter/WordDocument.h index 0e4b973c6c..71a2a03d07 100644 --- a/ASCOfficeDocFile/DocDocxConverter/WordDocument.h +++ b/ASCOfficeDocFile/DocDocxConverter/WordDocument.h @@ -72,7 +72,8 @@ namespace DocFileFormat inline int FindFileCharPos(int cp) { - return m_PieceTable->FileCharacterPositions->find(cp)->second; + std::map::iterator it = m_PieceTable->FileCharacterPositions->find(cp); + return (it != m_PieceTable->FileCharacterPositions->end()) ? it->second : -1; } private: diff --git a/ASCOfficeDocFile/DocFormatTest/DocFormatTest.cpp b/ASCOfficeDocFile/DocFormatTest/DocFormatTest.cpp index 828a82a7a3..8180fd4efe 100644 --- a/ASCOfficeDocFile/DocFormatTest/DocFormatTest.cpp +++ b/ASCOfficeDocFile/DocFormatTest/DocFormatTest.cpp @@ -24,14 +24,14 @@ int _tmain(int argc, _TCHAR* argv[]) HRESULT hRes = docFile.LoadFromFile( sSrcDoc, dstTempPath, NULL); - if (hRes != S_OK)return 2; - - COfficeUtils oCOfficeUtils(NULL); - if (S_OK != oCOfficeUtils.CompressFileOrDirectory(dstTempPath.c_str(), sDstDocx, -1)) - return 1; + if (hRes == S_OK) + { + COfficeUtils oCOfficeUtils(NULL); + hRes = oCOfficeUtils.CompressFileOrDirectory(dstTempPath.c_str(), sDstDocx, -1); + } FileSystem::Directory::DeleteDirectory(dstTempPath); - return 0; + return hRes; } diff --git a/ASCOfficeDocFile/Win32/ASCOfficeDocFile2005.vcproj b/ASCOfficeDocFile/Win32/ASCOfficeDocFile2005.vcproj index e1788de4f7..b5321894ee 100644 --- a/ASCOfficeDocFile/Win32/ASCOfficeDocFile2005.vcproj +++ b/ASCOfficeDocFile/Win32/ASCOfficeDocFile2005.vcproj @@ -3,7 +3,7 @@ ProjectType="Visual C++" Version="8,00" Name="ASCOfficeDocFile" - ProjectGUID="{da13d6d4-39b5-49db-b5d7-d1afdbb7c320}" + ProjectGUID="{DA13D6D4-39B5-49DB-B5D7-D1AFDBB7C320}" RootNamespace="ASCOfficeDocFile" Keyword="AtlProj" >