1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include "precompile.h"
29 #include <o3tl/safeint.hxx>
40 #include "hstream.hxx"
43 HWPFile
*HWPFile::cur_doc
= nullptr;
44 static int ccount
= 0;
45 static int pcount
= 0;
46 static int datecodecount
= 0;
54 , error_code(HWP_NoError
)
69 int HWPFile::ReadHwpFile(std::unique_ptr
<HStream
> stream
)
71 if (Open(std::move(stream
)) != HWP_NoError
)
83 int detect_hwp_version(const char *str
)
85 if (memcmp(V20SIGNATURE
, str
, HWPIDLen
) == 0)
87 else if (memcmp(V21SIGNATURE
, str
, HWPIDLen
) == 0)
89 else if (memcmp(V30SIGNATURE
, str
, HWPIDLen
) == 0)
96 int HWPFile::Open(std::unique_ptr
<HStream
> stream
)
98 std::unique_ptr
<HStreamIODev
> hstreamio(new HStreamIODev(std::move(stream
)));
100 if (!hstreamio
->open())
102 return SetState(HWP_EMPTY_FILE
);
105 SetIODevice(std::move(hstreamio
));
107 char idstr
[HWPIDLen
];
109 if (ReadBlock(idstr
, HWPIDLen
) < HWPIDLen
)
110 return SetState(HWP_UNSUPPORTED_VERSION
);
111 version
= detect_hwp_version(idstr
);
112 if (HWP_V30
!= version
)
113 return SetState(HWP_UNSUPPORTED_VERSION
);
117 int HWPFile::SetState(int errcode
)
119 error_code
= errcode
;
123 bool HWPFile::Read1b(unsigned char &out
)
125 return hiodev
&& hiodev
->read1b(out
);
128 bool HWPFile::Read1b(char &out
)
131 if (!hiodev
|| !hiodev
->read1b(tmp8
))
137 bool HWPFile::Read2b(unsigned short &out
)
139 return hiodev
&& hiodev
->read2b(out
);
142 bool HWPFile::Read2b(char16_t
&out
)
145 auto const ok
= Read2b(n
);
152 bool HWPFile::Read4b(unsigned int &out
)
154 return hiodev
&& hiodev
->read4b(out
);
157 bool HWPFile::Read4b(int &out
)
166 size_t HWPFile::Read2b(void *ptr
, size_t nmemb
)
168 return hiodev
? hiodev
->read2b(ptr
, nmemb
) : 0;
171 size_t HWPFile::ReadBlock(void *ptr
, size_t size
)
173 return hiodev
? hiodev
->readBlock(ptr
, size
) : 0;
176 size_t HWPFile::SkipBlock(size_t size
)
178 return hiodev
? hiodev
->skipBlock(size
) : 0;
181 void HWPFile::SetCompressed(bool flag
)
184 hiodev
->setCompressed(flag
);
188 std::unique_ptr
<HIODev
> HWPFile::SetIODevice(std::unique_ptr
<HIODev
> new_hiodev
)
190 std::swap(hiodev
, new_hiodev
);
195 // end of HIODev wrapper
197 void HWPFile::InfoRead()
199 _hwpInfo
.Read(*this);
203 void HWPFile::FontRead()
205 _hwpFont
.Read(*this);
209 void HWPFile::StyleRead()
211 _hwpStyle
.Read(*this);
215 void HWPFile::ParaListRead()
220 void HWPFile::ReadParaList(std::vector
< HWPPara
* > &aplist
)
222 std::unique_ptr
<HWPPara
> spNode( new HWPPara
);
223 unsigned char tmp_etcflag
;
224 unsigned char prev_etcflag
= 0;
225 while (spNode
->Read(*this, 0))
227 if( !(spNode
->etcflag
& 0x04) ){
228 tmp_etcflag
= spNode
->etcflag
;
229 spNode
->etcflag
= prev_etcflag
;
230 prev_etcflag
= tmp_etcflag
;
232 if (spNode
->nch
&& spNode
->reuse_shape
)
234 if (!aplist
.empty()){
235 spNode
->pshape
= aplist
.back()->pshape
;
239 spNode
->reuse_shape
= 0;
242 spNode
->pshape
->pagebreak
= spNode
->etcflag
;
244 AddParaShape(spNode
->pshape
);
247 aplist
.back()->SetNext(spNode
.get());
248 aplist
.push_back(spNode
.release());
249 spNode
.reset( new HWPPara
);
251 move_to_failed(std::move(spNode
));
254 void HWPFile::ReadParaList(std::vector
< std::unique_ptr
<HWPPara
> > &aplist
, unsigned char flag
)
256 std::unique_ptr
<HWPPara
> spNode( new HWPPara
);
257 unsigned char tmp_etcflag
;
258 unsigned char prev_etcflag
= 0;
259 while (spNode
->Read(*this, flag
))
261 if( !(spNode
->etcflag
& 0x04) ){
262 tmp_etcflag
= spNode
->etcflag
;
263 spNode
->etcflag
= prev_etcflag
;
264 prev_etcflag
= tmp_etcflag
;
266 if (spNode
->nch
&& spNode
->reuse_shape
)
268 if (!aplist
.empty()){
269 spNode
->pshape
= aplist
.back()->pshape
;
273 spNode
->reuse_shape
= 0;
276 spNode
->pshape
->pagebreak
= spNode
->etcflag
;
278 AddParaShape(spNode
->pshape
);
281 aplist
.back()->SetNext(spNode
.get());
282 aplist
.push_back(std::move(spNode
));
283 spNode
.reset( new HWPPara
);
285 move_to_failed(std::move(spNode
));
288 void HWPFile::move_to_failed(std::unique_ptr
<HWPPara
> xPara
)
290 pfailedlist
.push_back(std::move(xPara
));
293 void HWPFile::TagsRead()
303 if (size
<= 0 && tag
> 0){
307 if (tag
== FILETAG_END_OF_COMPRESSED
||
308 tag
== FILETAG_END_OF_UNCOMPRESSED
)
312 case FILETAG_EMBEDDED_PICTURE
:
314 std::unique_ptr
<EmPicture
> emb(new EmPicture(size
));
316 if (emb
->Read(*this))
317 emblist
.push_back(std::move(emb
));
320 case FILETAG_OLE_OBJECT
:
321 oledata
.reset( new OlePicture(size
) );
322 oledata
->Read(*this);
324 case FILETAG_HYPERTEXT
:
326 const int nRecordLen
= 617;
327 if( (size
% nRecordLen
) != 0 )
331 const int nRecords
= size
/ nRecordLen
;
332 for (int i
= 0 ; i
< nRecords
; ++i
)
334 std::unique_ptr
<HyperText
> hypert(new HyperText
);
335 if (hypert
->Read(*this))
336 hyperlist
.push_back(std::move(hypert
));
345 ReadBlock(_hwpInfo
.back_info
.reserved1
, 8);
346 if (!Read4b(_hwpInfo
.back_info
.luminance
))
348 if (!Read4b(_hwpInfo
.back_info
.contrast
))
350 if (!Read1b(_hwpInfo
.back_info
.effect
))
352 ReadBlock(_hwpInfo
.back_info
.reserved2
, 7);
353 ReadBlock(_hwpInfo
.back_info
.filename
, 260);
354 ReadBlock(_hwpInfo
.back_info
.color
, 3);
355 unsigned short nFlag
;
358 _hwpInfo
.back_info
.flag
= nFlag
>> 8 ;
362 _hwpInfo
.back_info
.range
= nRange
>> 24;
363 ReadBlock(_hwpInfo
.back_info
.reserved3
, 27);
364 if (!Read4b(_hwpInfo
.back_info
.size
))
367 if (_hwpInfo
.back_info
.size
< 0)
369 _hwpInfo
.back_info
.size
= 0;
373 _hwpInfo
.back_info
.data
.clear();
375 //read potentially compressed data in blocks as it's more
376 //likely large values are simply broken and we'll run out
377 //of data before we need to realloc
378 for (int i
= 0; i
< _hwpInfo
.back_info
.size
; i
+= SAL_MAX_UINT16
)
380 int nOldSize
= _hwpInfo
.back_info
.data
.size();
381 size_t nBlock
= std::min
<int>(SAL_MAX_UINT16
, _hwpInfo
.back_info
.size
- nOldSize
);
382 _hwpInfo
.back_info
.data
.resize(nOldSize
+ nBlock
);
383 size_t nReadBlock
= ReadBlock(_hwpInfo
.back_info
.data
.data() + nOldSize
, nBlock
);
384 if (nBlock
!= nReadBlock
)
386 _hwpInfo
.back_info
.data
.resize(nOldSize
+ nReadBlock
);
390 _hwpInfo
.back_info
.size
= _hwpInfo
.back_info
.data
.size();
392 if( _hwpInfo
.back_info
.size
> 0 )
393 _hwpInfo
.back_info
.type
= 2;
394 else if( _hwpInfo
.back_info
.filename
[0] )
395 _hwpInfo
.back_info
.type
= 1;
397 _hwpInfo
.back_info
.type
= 0;
400 _hwpInfo
.back_info
.isset
= true;
404 case FILETAG_PRESENTATION
:
405 case FILETAG_PREVIEW_IMAGE
:
406 case FILETAG_PREVIEW_TEXT
:
414 ColumnDef
*HWPFile::GetColumnDef(int num
)
416 if (o3tl::make_unsigned(num
) < columnlist
.size())
417 return columnlist
[num
]->xColdef
.get();
422 /* Index of @return starts from 1 */
423 int HWPFile::GetPageMasterNum(int page
)
426 for (auto const& column
: columnlist
)
428 if( page
< column
->start_page
)
435 HyperText
*HWPFile::GetHyperText()
438 if (o3tl::make_unsigned(currenthyper
) <= hyperlist
.size())
439 return hyperlist
[currenthyper
-1].get();
444 EmPicture
*HWPFile::GetEmPicture(Picture
* pic
)
446 char *name
= pic
->picinfo
.picembed
.embname
;
452 for (auto const& emb
: emblist
)
453 if (strcmp(name
, emb
->name
) == 0)
458 EmPicture
*HWPFile::GetEmPictureByName(char * name
)
464 for (auto const& emb
: emblist
)
465 if (strcmp(name
, emb
->name
) == 0)
470 ParaShape
*HWPFile::getParaShape(int index
)
472 if (index
< 0 || o3tl::make_unsigned(index
) >= pslist
.size())
474 return pslist
[index
].get();
477 CharShape
*HWPFile::getCharShape(int index
)
479 if (index
< 0 || o3tl::make_unsigned(index
) >= cslist
.size())
481 return cslist
[index
].get();
484 FBoxStyle
*HWPFile::getFBoxStyle(int index
)
486 if (index
< 0 || o3tl::make_unsigned(index
) >= fbslist
.size())
488 return fbslist
[index
];
491 DateCode
*HWPFile::getDateCode(int index
)
493 if (index
< 0 || o3tl::make_unsigned(index
) >= datecodes
.size())
495 return datecodes
[index
];
498 HeaderFooter
*HWPFile::getHeaderFooter(int index
)
500 if (index
< 0 || o3tl::make_unsigned(index
) >= headerfooters
.size())
502 return headerfooters
[index
];
505 ShowPageNum
*HWPFile::getPageNumber(int index
)
507 if (index
< 0 || o3tl::make_unsigned(index
) >= pagenumbers
.size())
509 return pagenumbers
[index
];
512 Table
*HWPFile::getTable(int index
)
514 if (index
< 0 || o3tl::make_unsigned(index
) >= tables
.size())
516 return tables
[index
].get();
519 void HWPFile::AddParaShape(std::shared_ptr
<ParaShape
> const & pshape
)
522 for(int j
= 0 ; j
< MAXTABS
-1 ; j
++)
524 if( j
> 0 && pshape
->tabs
[j
].position
== 0 )
526 if( pshape
->tabs
[0].position
== 0 ){
527 if( pshape
->tabs
[j
].type
|| pshape
->tabs
[j
].dot_continue
||
528 (pshape
->tabs
[j
].position
!= 1000 *j
) )
532 if( pshape
->tabs
[j
].type
|| pshape
->tabs
[j
].dot_continue
||
533 (pshape
->tabs
[j
].position
!= 1000 * (j
+ 1)) )
539 pshape
->tabs
[MAXTABS
-1].type
= sal::static_int_cast
<char>(nscount
);
540 pshape
->index
= ++pcount
;
541 pslist
.push_back(pshape
);
545 int value
= compareParaShape(pshape
.get());
548 pshape
->index
= ++pcount
;
549 pslist
.push_back(pshape
);
552 pshape
->index
= value
;
555 void HWPFile::AddCharShape(std::shared_ptr
<CharShape
> const & cshape
)
557 int value
= compareCharShape(cshape
.get());
560 cshape
->index
= ++ccount
;
561 cslist
.push_back(cshape
);
564 cshape
->index
= value
;
567 void HWPFile::AddColumnInfo()
569 columnlist
.emplace_back(new HWPColumnInfo(m_nCurrentPage
));
573 void HWPFile::SetColumnDef(const std::shared_ptr
<ColumnDef
>& rColdef
)
575 HWPColumnInfo
*cinfo
= columnlist
.back().get();
578 cinfo
->xColdef
= rColdef
;
579 cinfo
->bIsSet
= true;
582 void HWPFile::AddDateFormat(DateCode
* hbox
)
584 hbox
->key
= sal::static_int_cast
<char>(++datecodecount
);
585 datecodes
.push_back(hbox
);
588 void HWPFile::AddPageNumber(ShowPageNum
* hbox
)
590 pagenumbers
.push_back(hbox
);
593 void HWPFile::AddHeaderFooter(HeaderFooter
* hbox
)
595 headerfooters
.push_back(hbox
);
598 void HWPFile::AddTable(std::unique_ptr
<Table
> hbox
)
600 tables
.push_back(std::move(hbox
));
603 void HWPFile::AddFBoxStyle(FBoxStyle
* fbstyle
)
605 fbslist
.push_back(fbstyle
);
608 int HWPFile::compareCharShape(CharShape
const *shape
)
610 int count
= cslist
.size();
611 for(int i
= 0; i
< count
; i
++)
613 CharShape
*cshape
= getCharShape(i
);
615 if( shape
->size
== cshape
->size
&&
616 shape
->font
== cshape
->font
&&
617 shape
->ratio
== cshape
->ratio
&&
618 shape
->space
== cshape
->space
&&
619 shape
->color
[1] == cshape
->color
[1] &&
620 shape
->color
[0] == cshape
->color
[0] &&
621 shape
->shade
== cshape
->shade
&&
622 shape
->attr
== cshape
->attr
)
624 return cshape
->index
;
630 int HWPFile::compareParaShape(const ParaShape
* shape
)
635 int count
= pslist
.size();
636 for (int i
= 0; i
< count
; ++i
)
638 ParaShape
*pshape
= pslist
[i
].get();
641 if (shape
->left_margin
== pshape
->left_margin
&&
642 shape
->right_margin
== pshape
->right_margin
&&
643 shape
->pspacing_prev
== pshape
->pspacing_prev
&&
644 shape
->pspacing_next
== pshape
->pspacing_next
&&
645 shape
->indent
== pshape
->indent
&&
646 shape
->lspacing
== pshape
->lspacing
&&
647 shape
->arrange_type
== pshape
->arrange_type
&&
648 shape
->outline
== pshape
->outline
&&
649 shape
->pagebreak
== pshape
->pagebreak
)
651 if (shape
->cshape
->size
== pshape
->cshape
->size
&&
652 shape
->cshape
->font
== pshape
->cshape
->font
&&
653 shape
->cshape
->ratio
== pshape
->cshape
->ratio
&&
654 shape
->cshape
->space
== pshape
->cshape
->space
&&
655 shape
->cshape
->color
[1] == pshape
->cshape
->color
[1] &&
656 shape
->cshape
->color
[0] == pshape
->cshape
->color
[0] &&
657 shape
->cshape
->shade
== pshape
->cshape
->shade
&&
658 shape
->cshape
->attr
== pshape
->cshape
->attr
)
660 return pshape
->index
;
667 HWPFile
*GetCurrentDoc()
669 return HWPFile::cur_doc
;
673 HWPFile
*SetCurrentDoc(HWPFile
* hwpfp
)
675 HWPFile
*org
= HWPFile::cur_doc
;
677 HWPFile::cur_doc
= hwpfp
;
681 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */