diff options
Diffstat (limited to 'hwpfilter')
-rw-r--r-- | hwpfilter/source/drawing.h | 13 | ||||
-rw-r--r-- | hwpfilter/source/hcode.cxx | 2 | ||||
-rw-r--r-- | hwpfilter/source/htags.cxx | 26 | ||||
-rw-r--r-- | hwpfilter/source/hwpfile.cxx | 7 | ||||
-rw-r--r-- | hwpfilter/source/hwpfile.h | 6 | ||||
-rw-r--r-- | hwpfilter/source/hwpread.cxx | 21 | ||||
-rw-r--r-- | hwpfilter/source/hwpreader.cxx | 91 |
7 files changed, 81 insertions, 85 deletions
diff --git a/hwpfilter/source/drawing.h b/hwpfilter/source/drawing.h index 60bd74f80f80..8d046b0e4a2d 100644 --- a/hwpfilter/source/drawing.h +++ b/hwpfilter/source/drawing.h @@ -316,7 +316,7 @@ static bool LoadCommonHeader(HWPDrawingObject * hdo, unsigned short * link_info) return hmem->skipBlock(size - common_size ) != 0; } -static std::unique_ptr<HWPDrawingObject> LoadDrawingObject(void) +static std::unique_ptr<HWPDrawingObject> LoadDrawingObject(HWPFile& hwpf) { HWPDrawingObject *prev = nullptr; std::unique_ptr<HWPDrawingObject> hdo, head; @@ -355,7 +355,7 @@ static std::unique_ptr<HWPDrawingObject> LoadDrawingObject(void) } if (link_info & HDOFILE_HAS_CHILD) { - hdo->child = LoadDrawingObject(); + hdo->child = LoadDrawingObject(hwpf); if (hdo->child == nullptr) { goto error; @@ -384,6 +384,11 @@ error: { hdo->type = HWPDO_RECT; } + if (hdo->property.pPara) + { + hwpf.move_to_failed(std::unique_ptr<HWPPara>(hdo->property.pPara)); + hdo->property.pPara = nullptr; + } HWPDOFunc(hdo.get(), OBJFUNC_FREE, nullptr, 0); hdo.reset(); @@ -397,7 +402,7 @@ error: } -static bool LoadDrawingObjectBlock(Picture * pic) +static bool LoadDrawingObjectBlock(Picture * pic, HWPFile& hwpf) { int size; if (!hmem->read4b(size)) @@ -423,7 +428,7 @@ static bool LoadDrawingObjectBlock(Picture * pic) !hmem->skipBlock(size - HDOFILE_HEADER_SIZE)) return false; - pic->picinfo.picdraw.hdo = LoadDrawingObject().release(); + pic->picinfo.picdraw.hdo = LoadDrawingObject(hwpf).release(); if (pic->picinfo.picdraw.hdo == nullptr) return false; return true; diff --git a/hwpfilter/source/hcode.cxx b/hwpfilter/source/hcode.cxx index fda36e0953ca..28bc97212fba 100644 --- a/hwpfilter/source/hcode.cxx +++ b/hwpfilter/source/hcode.cxx @@ -1217,6 +1217,8 @@ hchar_string kstr2hstr(uchar const* src) { ret.push_back(src[i] << 8 | src[i+1]); i++; + if (src[i] == '\0') + break; } } return ret; diff --git a/hwpfilter/source/htags.cxx b/hwpfilter/source/htags.cxx index 0c011731c1d4..359c15ae78bc 100644 --- a/hwpfilter/source/htags.cxx +++ b/hwpfilter/source/htags.cxx @@ -19,6 +19,9 @@ #include "precompile.h" +#include <o3tl/char16_t2wchar_t.hxx> +#include <unotools/tempfile.hxx> + #include <string.h> #include "hwplib.h" @@ -108,27 +111,20 @@ void OlePicture::Read(HWPFile & hwpf) delete [] data; return; } - FILE *fp; - char tname[200]; - wchar_t wtname[200]; - tmpnam(tname); - if (nullptr == (fp = fopen(tname, "wb"))) - { - delete [] data; - return; - } - fwrite(data, size, 1, fp); + + utl::TempFile aTempFile; + aTempFile.EnableKillingFile(); + + SvFileStream aOutputStream(aTempFile.GetURL(), StreamMode::WRITE); + aOutputStream.WriteBytes(data, size); delete [] data; - fclose(fp); - MultiByteToWideChar(CP_ACP, 0, tname, -1, wtname, 200); - if( StgOpenStorage(wtname, nullptr, + aOutputStream.Close(); + if( StgOpenStorage(o3tl::toW(aTempFile.GetFileName().getStr()), nullptr, STGM_READWRITE|STGM_SHARE_EXCLUSIVE|STGM_TRANSACTED, nullptr, 0, &pis) != S_OK ) { pis = nullptr; - unlink(tname); return; } - unlink(tname); #else hwpf.SkipBlock(size); #endif diff --git a/hwpfilter/source/hwpfile.cxx b/hwpfilter/source/hwpfile.cxx index a97c39cc8eb6..a1303a8ee256 100644 --- a/hwpfilter/source/hwpfile.cxx +++ b/hwpfilter/source/hwpfile.cxx @@ -241,6 +241,7 @@ void HWPFile::ReadParaList(std::vector < HWPPara* > &aplist) aplist.push_back(spNode.release()); spNode.reset( new HWPPara ); } + move_to_failed(std::move(spNode)); } void HWPFile::ReadParaList(std::vector< std::unique_ptr<HWPPara> > &aplist, unsigned char flag) @@ -274,6 +275,12 @@ void HWPFile::ReadParaList(std::vector< std::unique_ptr<HWPPara> > &aplist, unsi aplist.push_back(std::move(spNode)); spNode.reset( new HWPPara ); } + move_to_failed(std::move(spNode)); +} + +void HWPFile::move_to_failed(std::unique_ptr<HWPPara> xPara) +{ + pfailedlist.push_back(std::move(xPara)); } void HWPFile::TagsRead() diff --git a/hwpfilter/source/hwpfile.h b/hwpfilter/source/hwpfile.h index 768086193523..e20d6b9c48ca 100644 --- a/hwpfilter/source/hwpfile.h +++ b/hwpfilter/source/hwpfile.h @@ -257,6 +257,8 @@ class DLLEXPORT HWPFile } void pop_hpara_type() { element_import_stack.pop_back(); } + void move_to_failed(std::unique_ptr<HWPPara> rPara); + private: int compareCharShape(CharShape const *shape); int compareParaShape(ParaShape const *shape); @@ -284,6 +286,10 @@ class DLLEXPORT HWPFile std::vector<std::unique_ptr<ColumnInfo>> columnlist; // paragraph list std::vector<std::unique_ptr<HWPPara>> plist; + // keep paragraph's that failed to load until + // import is complete to avoid dangling references + // elsewhere + std::vector<std::unique_ptr<HWPPara>> pfailedlist; // floating box list std::vector<FBox*> blist; // embedded picture list(tag data) diff --git a/hwpfilter/source/hwpread.cxx b/hwpfilter/source/hwpread.cxx index 12f29a38925e..f450a98a6e87 100644 --- a/hwpfilter/source/hwpread.cxx +++ b/hwpfilter/source/hwpread.cxx @@ -234,7 +234,6 @@ bool TxtBox::Read(HWPFile & hwpf) hwpf.Read2b(&option, 1); hwpf.Read2b(&ctrl_ch, 1); hwpf.Read2b(style.margin, 12); - hwpf.AddFBoxStyle(&style); hwpf.Read2b(&box_xs, 1); hwpf.Read2b(&box_ys, 1); hwpf.Read2b(&cap_xs, 1); @@ -362,7 +361,10 @@ bool TxtBox::Read(HWPFile & hwpf) else m_pTable = nullptr; - return !hwpf.State(); + bSuccess = !hwpf.State(); + if (bSuccess) + hwpf.AddFBoxStyle(&style); + return bSuccess; } namespace @@ -451,6 +453,7 @@ bool Picture::Read(HWPFile & hwpf) scale[1] = tmp16; hwpf.ReadBlock(picinfo.picun.path, 256); /* Picture File Name: when type is not a Drawing. */ + picinfo.picun.path[255] = 0; // ensure null terminated hwpf.ReadBlock(reserved3, 9); /* Brightness / Contrast / Picture Effect, etc. */ UpdateBBox(this); @@ -490,7 +493,7 @@ bool Picture::Read(HWPFile & hwpf) if (pictype == PICTYPE_DRAW) { auto xGuard(std::make_unique<ChangeMemGuard>(follow.data(), follow_block_size)); - LoadDrawingObjectBlock(this); + LoadDrawingObjectBlock(this, hwpf); style.cell = picinfo.picdraw.hdo; xGuard.reset(); } @@ -509,12 +512,14 @@ bool Picture::Read(HWPFile & hwpf) style.boxtype = 'G'; else style.boxtype = 'D'; - hwpf.AddFBoxStyle(&style); // caption hwpf.ReadParaList(caption); - return !hwpf.State(); + bool bSuccess = !hwpf.State(); + if (bSuccess) + hwpf.AddFBoxStyle(&style); + return bSuccess; } // line(15) @@ -552,7 +557,6 @@ bool Line::Read(HWPFile & hwpf) hwpf.Read2b(&option, 1); hwpf.Read2b(&ctrl_ch, 1); hwpf.Read2b(style.margin, 12); - hwpf.AddFBoxStyle(&style); hwpf.Read2b(&box_xs, 1); hwpf.Read2b(&box_ys, 1); hwpf.Read2b(&cap_xs, 1); @@ -581,7 +585,10 @@ bool Line::Read(HWPFile & hwpf) hwpf.Read2b(&color, 1); style.xpos = width; - return !hwpf.State(); + bool bSuccess = !hwpf.State(); + if (bSuccess) + hwpf.AddFBoxStyle(&style); + return bSuccess; } // hidden(15) diff --git a/hwpfilter/source/hwpreader.cxx b/hwpfilter/source/hwpreader.cxx index 4144d20e0bde..12eb4960f97f 100644 --- a/hwpfilter/source/hwpreader.cxx +++ b/hwpfilter/source/hwpreader.cxx @@ -24,6 +24,7 @@ #include <math.h> #include <osl/diagnose.h> +#include <o3tl/safeint.hxx> #include <tools/stream.hxx> #include "fontmap.hxx" @@ -70,7 +71,6 @@ rendEl("text:span"); \ tstart = false -static hchar *field = nullptr; static char buf[1024]; namespace @@ -96,12 +96,13 @@ struct HwpReaderPrivate bInHeader = false; nPnPos = 0; pPn = nullptr; - + pField = nullptr; } bool bFirstPara; bool bInBody; bool bInHeader; ShowPageNum *pPn; + hchar *pField; int nPnPos; }; @@ -460,7 +461,9 @@ void HwpReader::makeDrawMiscStyle( HWPDrawingObject *hdo ) if( hdo->type == HWPDO_LINE || hdo->type == HWPDO_ARC || hdo->type == HWPDO_FREEFORM || hdo->type == HWPDO_ADVANCED_ARC ) { - if( prop->line_tstyle && !ArrowShape[prop->line_tstyle].bMade ) + if( prop->line_tstyle > 0 && + o3tl::make_unsigned(prop->line_tstyle) < std::size(ArrowShape) && + !ArrowShape[prop->line_tstyle].bMade ) { ArrowShape[prop->line_tstyle].bMade = true; padd("draw:name", sXML_CDATA, @@ -484,7 +487,9 @@ void HwpReader::makeDrawMiscStyle( HWPDrawingObject *hdo ) mxList->clear(); rendEl("draw:marker"); } - if( prop->line_hstyle && !ArrowShape[prop->line_hstyle].bMade) + if (prop->line_hstyle > 0 && + o3tl::make_unsigned(prop->line_hstyle) < std::size(ArrowShape) && + !ArrowShape[prop->line_hstyle].bMade) { ArrowShape[prop->line_hstyle].bMade = true; padd("draw:name", sXML_CDATA, @@ -515,62 +520,28 @@ void HwpReader::makeDrawMiscStyle( HWPDrawingObject *hdo ) if( prop->flag >> 18 & 0x01 ) { padd( "draw:name", sXML_CDATA, ascii(Int2Str(hdo->index, "fillimage%d", buf))); - if( !prop->pictype ) + + EmPicture *emp = nullptr; + if (prop->pictype && strlen(prop->szPatternFile) > 3) + emp = hwpfile.GetEmPictureByName(prop->szPatternFile); + if (!emp) { padd( "xlink:href", sXML_CDATA, reinterpret_cast<sal_Unicode const *>(hconv(kstr2hstr( reinterpret_cast<uchar const *>(urltounix(prop->szPatternFile).c_str())).c_str()))); + padd( "xlink:type", sXML_CDATA, "simple"); + padd( "xlink:show", sXML_CDATA, "embed"); + padd( "xlink:actuate", sXML_CDATA, "onLoad"); } - else - { - EmPicture *emp = nullptr; - if ( strlen( prop->szPatternFile ) > 3) - emp = hwpfile.GetEmPictureByName(prop->szPatternFile); - if( emp ) - { - char filename[128+17+9]; - char dirname[128]; - int fd; -#ifdef _WIN32 - GetTempPathA(sizeof(dirname), dirname); - sprintf(filename, "%s%s",dirname, emp->name); - if( (fd = open( filename , _O_CREAT | _O_WRONLY | _O_BINARY , 0666)) >= 0 ) -#else - strcpy(dirname, "/tmp/"); - sprintf(filename, "%s%s", dirname, emp->name); - if( (fd = open( filename , O_CREAT | O_WRONLY , 0666)) >= 0 ) -#endif - { - size_t nWritten = write(fd, emp->data.get(), emp->size); - OSL_VERIFY(nWritten == emp->size); - close(fd); - } -#ifdef _WIN32 - int j; - for(j = 0 ; j < static_cast<int>(strlen( dirname )) ; j++) - { - if( dirname[j] == '\\' ) buf[j] = '/'; - else buf[j] = dirname[j]; - } - buf[j] = '\0'; - sprintf(filename, "file:///%s%s",buf, emp->name ); -#else - sprintf(filename, "file://%s%s",dirname, emp->name ); -#endif - padd( "xlink:href", sXML_CDATA, ascii(filename)); - } - else - { - padd( "xlink:href", sXML_CDATA, - reinterpret_cast<sal_Unicode const *>(hconv(kstr2hstr( reinterpret_cast<uchar const *>(urltounix(prop->szPatternFile).c_str())).c_str()))); - } - - } - padd( "xlink:type", sXML_CDATA, "simple"); - padd( "xlink:show", sXML_CDATA, "embed"); - padd( "xlink:actuate", sXML_CDATA, "onLoad"); rstartEl( "draw:fill-image", mxList.get()); mxList->clear(); + if (emp) + { + rstartEl("office:binary-data", mxList.get()); + std::shared_ptr<char> pStr(base64_encode_string(emp->data.get(), emp->size), Free<char>()); + rchars(ascii(pStr.get())); + rendEl("office:binary-data"); + } rendEl( "draw:fill-image"); } /* If there is a gradient, when a bitmap file is present, this is the first. */ @@ -2075,7 +2046,8 @@ void HwpReader::makeDrawStyle( HWPDrawingObject * hdo, FBoxStyle * fstyle) hdo->type == HWPDO_FREEFORM || hdo->type == HWPDO_ADVANCED_ARC ) { - if( hdo->property.line_tstyle > 0 ) + if( hdo->property.line_tstyle > 0 && + o3tl::make_unsigned(hdo->property.line_tstyle) < std::size(ArrowShape) ) { padd("draw:marker-start", sXML_CDATA, ascii(ArrowShape[hdo->property.line_tstyle].name) ); @@ -2096,7 +2068,8 @@ void HwpReader::makeDrawStyle( HWPDrawingObject * hdo, FBoxStyle * fstyle) Double2Str( WTMM(hdo->property.line_width * 7)) + "mm"); } - if( hdo->property.line_hstyle > 0 ) + if( hdo->property.line_hstyle > 0 && + o3tl::make_unsigned(hdo->property.line_hstyle) < std::size(ArrowShape) ) { padd("draw:marker-end", sXML_CDATA, ascii(ArrowShape[hdo->property.line_hstyle].name) ); @@ -2959,7 +2932,7 @@ void HwpReader::make_text_p3(HWPPara * para,bool bParaStart) firstspace = 1; if( hbox->type[0] == 4 && hbox->type[1] == 0 ) { - field = hbox->str3.get(); + d->pField = hbox->str3.get(); } else{ makeFieldCode(str, hbox); @@ -2972,7 +2945,7 @@ void HwpReader::make_text_p3(HWPPara * para,bool bParaStart) if( hbox->type[0] == 4 && hbox->type[1] == 0 ) { makeFieldCode(str, hbox); - field = nullptr; + d->pField = nullptr; } infield = false; str.clear(); @@ -3140,8 +3113,8 @@ void HwpReader::makeFieldCode(hchar_string const & rStr, FieldCode const *hbox) if( hbox->type[0] == 4 && hbox->type[1] == 0 ) { padd("text:placeholder-type", sXML_CDATA, "text"); - if( field ) - padd("text:description", sXML_CDATA, reinterpret_cast<sal_Unicode const *>(hconv(field))); + if (d->pField) + padd("text:description", sXML_CDATA, reinterpret_cast<sal_Unicode const *>(hconv(d->pField))); rstartEl( "text:placeholder", mxList.get()); mxList->clear(); rchars( reinterpret_cast<sal_Unicode const *>(rStr.c_str()) ); |