tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / hwpfilter / source / hwpfile.cxx
blobc186ad8609a45e9742b4699179b5686ccee875fc
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #include <memory>
21 #include "precompile.h"
23 #include <algorithm>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <errno.h>
29 #include <o3tl/safeint.hxx>
31 #include "hwplib.h"
32 #include "hwpfile.h"
33 #include "hiodev.h"
34 #include "hfont.h"
35 #include "hstyle.h"
36 #include "hbox.h"
37 #include "hpara.h"
38 #include "htags.h"
39 #include "hcode.h"
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;
48 HWPFile::HWPFile()
49 : version(HWP_V30)
50 , compressed(false)
51 , encrypted(false)
52 , linenumber(0)
53 , info_block_len(0)
54 , error_code(HWP_NoError)
55 , readdepth(0)
56 , m_nCurrentPage(1)
57 , m_nMaxSettedPage(0)
58 , currenthyper(0)
60 SetCurrentDoc(this);
63 HWPFile::~HWPFile()
65 oledata.reset();
66 hiodev.reset();
69 int HWPFile::ReadHwpFile(std::unique_ptr<HStream> stream)
71 if (Open(std::move(stream)) != HWP_NoError)
72 return State();
73 InfoRead();
74 FontRead();
75 StyleRead();
76 AddColumnInfo();
77 ParaListRead();
78 TagsRead();
80 return State();
83 int detect_hwp_version(const char *str)
85 if (memcmp(V20SIGNATURE, str, HWPIDLen) == 0)
86 return HWP_V20;
87 else if (memcmp(V21SIGNATURE, str, HWPIDLen) == 0)
88 return HWP_V21;
89 else if (memcmp(V30SIGNATURE, str, HWPIDLen) == 0)
90 return HWP_V30;
91 return 0;
94 // HIODev wrapper
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);
114 return HWP_NoError;
117 int HWPFile::SetState(int errcode)
119 error_code = errcode;
120 return error_code;
123 bool HWPFile::Read1b(unsigned char &out)
125 return hiodev && hiodev->read1b(out);
128 bool HWPFile::Read1b(char &out)
130 unsigned char tmp8;
131 if (!hiodev || !hiodev->read1b(tmp8))
132 return false;
133 out = tmp8;
134 return true;
137 bool HWPFile::Read2b(unsigned short &out)
139 return hiodev && hiodev->read2b(out);
142 bool HWPFile::Read2b(char16_t &out)
144 unsigned short n;
145 auto const ok = Read2b(n);
146 if (ok) {
147 out = n;
149 return ok;
152 bool HWPFile::Read4b(unsigned int &out)
154 return hiodev && hiodev->read4b(out);
157 bool HWPFile::Read4b(int &out)
159 unsigned int tmp32;
160 if (!Read4b(tmp32))
161 return false;
162 out = tmp32;
163 return true;
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)
183 if (hiodev)
184 hiodev->setCompressed(flag);
188 std::unique_ptr<HIODev> HWPFile::SetIODevice(std::unique_ptr<HIODev> new_hiodev)
190 std::swap(hiodev, new_hiodev);
191 return 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()
217 ReadParaList(plist);
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;
237 else{
238 spNode->nch = 0;
239 spNode->reuse_shape = 0;
242 spNode->pshape->pagebreak = spNode->etcflag;
243 if (spNode->nch)
244 AddParaShape(spNode->pshape);
246 if (!aplist.empty())
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;
271 else{
272 spNode->nch = 0;
273 spNode->reuse_shape = 0;
276 spNode->pshape->pagebreak = spNode->etcflag;
277 if (spNode->nch)
278 AddParaShape(spNode->pshape);
280 if (!aplist.empty())
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()
295 while (true)
297 uint tag;
298 if (!Read4b(tag))
299 return;
300 int size;
301 if (!Read4b(size))
302 return;
303 if (size <= 0 && tag > 0){
304 continue;
307 if (tag == FILETAG_END_OF_COMPRESSED ||
308 tag == FILETAG_END_OF_UNCOMPRESSED)
309 return;
310 switch (tag)
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));
319 break;
320 case FILETAG_OLE_OBJECT:
321 oledata.reset( new OlePicture(size) );
322 oledata->Read(*this);
323 break;
324 case FILETAG_HYPERTEXT:
326 const int nRecordLen = 617;
327 if( (size % nRecordLen) != 0 )
328 SkipBlock( size );
329 else
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));
337 else
338 break;
341 break;
343 case 6:
345 ReadBlock(_hwpInfo.back_info.reserved1, 8);
346 if (!Read4b(_hwpInfo.back_info.luminance))
347 return;
348 if (!Read4b(_hwpInfo.back_info.contrast))
349 return;
350 if (!Read1b(_hwpInfo.back_info.effect))
351 return;
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;
356 if (!Read2b(nFlag))
357 return;
358 _hwpInfo.back_info.flag = nFlag >> 8 ;
359 int nRange;
360 if (!Read4b(nRange))
361 return;
362 _hwpInfo.back_info.range = nRange >> 24;
363 ReadBlock(_hwpInfo.back_info.reserved3, 27);
364 if (!Read4b(_hwpInfo.back_info.size))
365 return;
367 if (_hwpInfo.back_info.size < 0)
369 _hwpInfo.back_info.size = 0;
370 return;
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);
387 break;
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;
396 else
397 _hwpInfo.back_info.type = 0;
400 _hwpInfo.back_info.isset = true;
402 break;
404 case FILETAG_PRESENTATION:
405 case FILETAG_PREVIEW_IMAGE:
406 case FILETAG_PREVIEW_TEXT:
407 default:
408 SkipBlock(size);
414 ColumnDef *HWPFile::GetColumnDef(int num)
416 if (o3tl::make_unsigned(num) < columnlist.size())
417 return columnlist[num]->xColdef.get();
418 else
419 return nullptr;
422 /* Index of @return starts from 1 */
423 int HWPFile::GetPageMasterNum(int page)
425 int i = 0;
426 for (auto const& column : columnlist)
428 if( page < column->start_page )
429 return i;
430 ++i;
432 return i;
435 HyperText *HWPFile::GetHyperText()
437 ++currenthyper;
438 if (o3tl::make_unsigned(currenthyper) <= hyperlist.size())
439 return hyperlist[currenthyper-1].get();
440 else
441 return nullptr;
444 EmPicture *HWPFile::GetEmPicture(Picture * pic)
446 char *name = pic->picinfo.picembed.embname;
448 name[0] = 'H';
449 name[1] = 'W';
450 name[2] = 'P';
452 for (auto const& emb : emblist)
453 if (strcmp(name, emb->name) == 0)
454 return emb.get();
455 return nullptr;
458 EmPicture *HWPFile::GetEmPictureByName(char * name)
460 name[0] = 'H';
461 name[1] = 'W';
462 name[2] = 'P';
464 for (auto const& emb : emblist)
465 if (strcmp(name, emb->name) == 0)
466 return emb.get();
467 return nullptr;
470 ParaShape *HWPFile::getParaShape(int index)
472 if (index < 0 || o3tl::make_unsigned(index) >= pslist.size())
473 return nullptr;
474 return pslist[index].get();
477 CharShape *HWPFile::getCharShape(int index)
479 if (index < 0 || o3tl::make_unsigned(index) >= cslist.size())
480 return nullptr;
481 return cslist[index].get();
484 FBoxStyle *HWPFile::getFBoxStyle(int index)
486 if (index < 0 || o3tl::make_unsigned(index) >= fbslist.size())
487 return nullptr;
488 return fbslist[index];
491 DateCode *HWPFile::getDateCode(int index)
493 if (index < 0 || o3tl::make_unsigned(index) >= datecodes.size())
494 return nullptr;
495 return datecodes[index];
498 HeaderFooter *HWPFile::getHeaderFooter(int index)
500 if (index < 0 || o3tl::make_unsigned(index) >= headerfooters.size())
501 return nullptr;
502 return headerfooters[index];
505 ShowPageNum *HWPFile::getPageNumber(int index)
507 if (index < 0 || o3tl::make_unsigned(index) >= pagenumbers.size())
508 return nullptr;
509 return pagenumbers[index];
512 Table *HWPFile::getTable(int index)
514 if (index < 0 || o3tl::make_unsigned(index) >= tables.size())
515 return nullptr;
516 return tables[index].get();
519 void HWPFile::AddParaShape(std::shared_ptr<ParaShape> const & pshape)
521 int nscount = 0;
522 for(int j = 0 ; j < MAXTABS-1 ; j++)
524 if( j > 0 && pshape->tabs[j].position == 0 )
525 break;
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) )
529 nscount = j;
531 else {
532 if( pshape->tabs[j].type || pshape->tabs[j].dot_continue ||
533 (pshape->tabs[j].position != 1000 * (j + 1)) )
534 nscount = j;
537 if( nscount )
539 pshape->tabs[MAXTABS-1].type = sal::static_int_cast<char>(nscount);
540 pshape->index = ++pcount;
541 pslist.push_back(pshape);
542 return;
545 int value = compareParaShape(pshape.get());
546 if (value == 0)
548 pshape->index = ++pcount;
549 pslist.push_back(pshape);
550 return;
552 pshape->index = value;
555 void HWPFile::AddCharShape(std::shared_ptr<CharShape> const & cshape)
557 int value = compareCharShape(cshape.get());
558 if (value == 0)
560 cshape->index = ++ccount;
561 cslist.push_back(cshape);
563 else
564 cshape->index = value;
567 void HWPFile::AddColumnInfo()
569 columnlist.emplace_back(new HWPColumnInfo(m_nCurrentPage));
570 setMaxSettedPage();
573 void HWPFile::SetColumnDef(const std::shared_ptr<ColumnDef>& rColdef)
575 HWPColumnInfo *cinfo = columnlist.back().get();
576 if( cinfo->bIsSet )
577 return;
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;
627 return 0;
630 int HWPFile::compareParaShape(const ParaShape* shape)
632 if (!shape->cshape)
633 return 0;
635 int count = pslist.size();
636 for (int i = 0; i < count; ++i)
638 ParaShape *pshape = pslist[i].get();
639 if (!pshape->cshape)
640 continue;
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;
664 return 0;
667 HWPFile *GetCurrentDoc()
669 return HWPFile::cur_doc;
673 HWPFile *SetCurrentDoc(HWPFile * hwpfp)
675 HWPFile *org = HWPFile::cur_doc;
677 HWPFile::cur_doc = hwpfp;
678 return org;
681 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */