Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / hwpfilter / source / hwpfile.cxx
blob1631c7a7a4ca515a59ebeac7d22acbad2b13cb20
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include "precompile.h"
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <errno.h>
35 #include "hwplib.h"
36 #include "hwpfile.h"
37 #include "hiodev.h"
38 #include "hfont.h"
39 #include "hstyle.h"
40 #include "hbox.h"
41 #include "hpara.h"
42 #include "htags.h"
43 #include "hcode.h"
44 #include "hstream.h"
46 #include <osl/diagnose.h>
48 #define HWPHeadLen 128
49 #define HWPSummaryLen 1008
51 #define FILESTG_SIGNATURE 0xF8995567
52 #define FILESTG_SIGNATURE_NORMAL 0xF8995568
54 HWPFile *HWPFile::cur_doc = 0;
55 static int ccount = 0;
56 static int pcount = 0;
57 static int datecodecount = 0;
59 HWPFile::HWPFile()
60 : version(HWP_V30)
61 , compressed(false)
62 , encrypted(false)
63 , linenumber(0)
64 , info_block_len(0)
65 , error_code(HWP_NoError)
66 , oledata(0)
67 , m_nCurrentPage(1)
68 , m_nMaxSettedPage(0)
69 , hiodev(0)
70 , currenthyper(0)
72 SetCurrentDoc(this);
75 HWPFile::~HWPFile()
77 delete oledata;
78 delete hiodev;
80 std::list < ColumnInfo* >::iterator it_column = columnlist.begin();
81 for (; it_column != columnlist.end(); ++it_column)
82 delete *it_column;
84 std::list < HWPPara* >::iterator it = plist.begin();
85 for (; it != plist.end(); ++it)
86 delete *it;
88 std::list < Table* >::iterator tbl = tables.begin();
89 for (; tbl != tables.end(); ++tbl)
90 delete *tbl;
92 std::list < HyperText* >::iterator hyp = hyperlist.begin();
93 for (; hyp != hyperlist.end(); ++hyp)
95 delete *hyp;
99 int HWPFile::ReadHwpFile(HStream & stream)
101 if (Open(stream) != HWP_NoError)
102 return State();
103 InfoRead();
104 FontRead();
105 StyleRead();
106 AddColumnInfo();
107 ParaListRead();
108 TagsRead();
110 return State();
113 int detect_hwp_version(const char *str)
115 if (memcmp(V20SIGNATURE, str, HWPIDLen) == 0)
116 return HWP_V20;
117 else if (memcmp(V21SIGNATURE, str, HWPIDLen) == 0)
118 return HWP_V21;
119 else if (memcmp(V30SIGNATURE, str, HWPIDLen) == 0)
120 return HWP_V30;
121 return 0;
124 // HIODev wrapper
126 int HWPFile::Open(HStream & stream)
128 HStreamIODev *hstreamio = new HStreamIODev(stream);
130 if (!hstreamio->open())
132 delete hstreamio;
134 return SetState(HWP_EMPTY_FILE);
137 HIODev *pPrev = SetIODevice(hstreamio);
138 delete pPrev;
140 char idstr[HWPIDLen];
142 if (ReadBlock(idstr, HWPIDLen) <= 0
143 || HWP_V30 != (version = detect_hwp_version(idstr)))
145 return SetState(HWP_UNSUPPORTED_VERSION);
147 return HWP_NoError;
151 int HWPFile::State(void) const
153 return error_code;
157 int HWPFile::SetState(int errcode)
159 error_code = errcode;
160 return error_code;
164 int HWPFile::Read1b(void)
166 return hiodev ? hiodev->read1b() : -1;
170 int HWPFile::Read2b(void)
172 return hiodev ? hiodev->read2b() : -1;
176 long HWPFile::Read4b(void)
178 return hiodev ? hiodev->read4b() : -1;
182 int HWPFile::Read1b(void *ptr, size_t nmemb)
184 return hiodev ? hiodev->read1b(ptr, nmemb) : 0;
188 int HWPFile::Read2b(void *ptr, size_t nmemb)
190 return hiodev ? hiodev->read2b(ptr, nmemb) : 0;
194 int HWPFile::Read4b(void *ptr, size_t nmemb)
196 return hiodev ? hiodev->read4b(ptr, nmemb) : 0;
200 size_t HWPFile::ReadBlock(void *ptr, size_t size)
202 return hiodev ? hiodev->readBlock(ptr, size) : 0;
206 size_t HWPFile::SkipBlock(size_t size)
208 return hiodev ? hiodev->skipBlock(size) : 0;
212 bool HWPFile::SetCompressed(bool flag)
214 return hiodev ? hiodev->setCompressed(flag) : false;
218 HIODev *HWPFile::SetIODevice(HIODev * new_hiodev)
220 HIODev *old_hiodev = hiodev;
222 hiodev = new_hiodev;
224 return old_hiodev;
228 // end of HIODev wrapper
230 bool HWPFile::InfoRead(void)
232 return _hwpInfo.Read(*this);
236 bool HWPFile::FontRead(void)
238 return _hwpFont.Read(*this);
242 bool HWPFile::StyleRead(void)
244 return _hwpStyle.Read(*this);
248 bool HWPFile::ParaListRead(void)
250 return ReadParaList(plist);
253 bool HWPFile::ReadParaList(std::list < HWPPara* > &aplist, unsigned char flag)
255 HWPPara *spNode = new HWPPara;
256 unsigned char tmp_etcflag;
257 unsigned char prev_etcflag = 0;
258 while (spNode->Read(*this, flag))
260 if( !(spNode->etcflag & 0x04) ){
261 tmp_etcflag = spNode->etcflag;
262 spNode->etcflag = prev_etcflag;
263 prev_etcflag = tmp_etcflag;
265 if (spNode->nch && spNode->reuse_shape)
267 if (aplist.size()){
268 spNode->pshape = aplist.back()->pshape;
270 else{
271 spNode->nch = 0;
272 spNode->reuse_shape = 0;
275 spNode->pshape.pagebreak = spNode->etcflag;
276 if( spNode->nch )
277 AddParaShape( &spNode->pshape );
279 if (aplist.size())
280 aplist.back()->SetNext(spNode);
281 aplist.push_back(spNode);
282 spNode = new HWPPara;
284 delete spNode;
286 return true;
290 bool HWPFile::TagsRead(void)
292 ulong tag;
293 long size;
295 while (1)
297 tag = Read4b();
298 size = Read4b();
299 if (size <= 0 && tag > 0){
300 continue;
303 if (tag == FILETAG_END_OF_COMPRESSED ||
304 tag == FILETAG_END_OF_UNCOMPRESSED)
305 return true;
306 switch (tag)
308 case FILETAG_EMBEDDED_PICTURE:
310 EmPicture *emb = new EmPicture(size);
312 if (true == emb->Read(*this))
313 emblist.push_back(emb);
314 else
315 delete emb;
317 break;
318 case FILETAG_OLE_OBJECT:
319 if (oledata)
320 delete oledata;
321 oledata = new OlePicture(size);
322 oledata->Read(*this);
323 break;
324 case FILETAG_HYPERTEXT:
326 if( (size % 617) != 0 )
327 SkipBlock( size );
328 else
329 for( int i = 0 ; i < size/617 ; i++)
331 HyperText *hypert = new HyperText;
332 hypert->Read(*this);
333 hyperlist.push_back(hypert);
335 break;
337 case 6:
339 ReadBlock(_hwpInfo.back_info.reserved1, 8);
340 _hwpInfo.back_info.luminance = Read4b();
341 _hwpInfo.back_info.contrast = Read4b();
342 _hwpInfo.back_info.effect = sal::static_int_cast<char>(Read1b());
343 ReadBlock(_hwpInfo.back_info.reserved2, 7);
344 ReadBlock(_hwpInfo.back_info.filename, 260);
345 ReadBlock(_hwpInfo.back_info.color, 3);
346 unsigned short nFlag = sal::static_int_cast<unsigned short>(Read2b());
347 _hwpInfo.back_info.flag = nFlag >> 8 ;
348 int nRange = Read4b();
349 _hwpInfo.back_info.range = nRange >> 24;
350 ReadBlock(_hwpInfo.back_info.reserved3, 27);
351 _hwpInfo.back_info.size = Read4b();
353 _hwpInfo.back_info.data = new char[(unsigned int)_hwpInfo.back_info.size];
354 ReadBlock(_hwpInfo.back_info.data, _hwpInfo.back_info.size);
356 if( _hwpInfo.back_info.size > 0 )
357 _hwpInfo.back_info.type = 2;
358 else if( _hwpInfo.back_info.filename[0] )
359 _hwpInfo.back_info.type = 1;
360 else
361 _hwpInfo.back_info.type = 0;
364 _hwpInfo.back_info.isset = true;
366 break;
368 case FILETAG_PRESENTATION:
369 case FILETAG_PREVIEW_IMAGE:
370 case FILETAG_PREVIEW_TEXT:
371 default:
372 SkipBlock(size);
378 ColumnDef *HWPFile::GetColumnDef(int num)
380 std::list<ColumnInfo*>::iterator it = columnlist.begin();
382 for(int i = 0; it != columnlist.end() ; ++it, i++){
383 if( i == num )
384 break;
387 if( it != columnlist.end() )
388 return (*it)->coldef;
389 else
390 return 0;
392 /* @return À妽º´Â 1ºÎÅÍ ½ÃÀÛÇÑ´Ù. */
393 int HWPFile::GetPageMasterNum(int page)
395 std::list<ColumnInfo*>::iterator it = columnlist.begin();
396 ColumnInfo *now = 0;
397 int i;
399 for( i = 1 ; it != columnlist.end() ; ++it, i++){
400 now = *it;
401 if( page < now->start_page )
402 return i-1;
404 return i-1;
407 HyperText *HWPFile::GetHyperText()
409 std::list<HyperText*>::iterator it = hyperlist.begin();
411 for( int i = 0; it != hyperlist.end(); ++it, i++ ){
412 if( i == currenthyper )
413 break;
416 currenthyper++;
417 return *it;
420 EmPicture *HWPFile::GetEmPicture(Picture * pic)
422 char *name = pic->picinfo.picembed.embname;
424 name[0] = 'H';
425 name[1] = 'W';
426 name[2] = 'P';
428 std::list < EmPicture* >::iterator it = emblist.begin();
429 for (; it != emblist.end(); ++it)
430 if (strcmp(name, (*it)->name) == 0)
431 return *it;
432 return 0;
435 EmPicture *HWPFile::GetEmPictureByName(char * name)
437 name[0] = 'H';
438 name[1] = 'W';
439 name[2] = 'P';
441 std::list < EmPicture* >::iterator it = emblist.begin();
442 for (; it != emblist.end(); ++it)
443 if (strcmp(name, (*it)->name) == 0)
444 return *it;
445 return 0;
449 void HWPFile::AddBox(FBox * box)
451 // LATER if we don't use box->next(),
452 // AddBox() and GetBoxHead() are useless;
453 if (blist.size())
455 box->prev = blist.back();
456 box->prev->next = box;
458 else
459 box->prev = 0;
460 blist.push_back(box);
464 ParaShape *HWPFile::getParaShape(int index)
466 std::list<ParaShape*>::iterator it = pslist.begin();
468 for( int i = 0; it != pslist.end(); ++it, i++ ){
469 if( i == index )
470 break;
473 return *it;
477 CharShape *HWPFile::getCharShape(int index)
479 std::list<CharShape*>::iterator it = cslist.begin();
481 for( int i = 0; it != cslist.end(); ++it, i++ ){
482 if( i == index )
483 break;
486 return *it;
490 FBoxStyle *HWPFile::getFBoxStyle(int index)
492 std::list<FBoxStyle*>::iterator it = fbslist.begin();
494 for( int i = 0; it != fbslist.end(); ++it, i++ ){
495 if( i == index )
496 break;
499 return *it;
502 DateCode *HWPFile::getDateCode(int index)
504 std::list<DateCode*>::iterator it = datecodes.begin();
506 for( int i = 0; it != datecodes.end(); ++it, i++ ){
507 if( i == index )
508 break;
511 return *it;
514 HeaderFooter *HWPFile::getHeaderFooter(int index)
516 std::list<HeaderFooter*>::iterator it = headerfooters.begin();
518 for( int i = 0; it != headerfooters.end(); ++it, i++ ){
519 if( i == index )
520 break;
523 return *it;
526 ShowPageNum *HWPFile::getPageNumber(int index)
528 std::list<ShowPageNum*>::iterator it = pagenumbers.begin();
530 for( int i = 0; it != pagenumbers.end(); ++it, i++ ){
531 if( i == index )
532 break;
535 return *it;
539 Table *HWPFile::getTable(int index)
541 std::list<Table*>::iterator it = tables.begin();
543 for( int i = 0; it != tables.end(); ++it, i++ ){
544 if( i == index )
545 break;
548 return *it;
551 void HWPFile::AddParaShape(ParaShape * pshape)
553 int nscount = 0;
554 for(int j = 0 ; j < MAXTABS-1 ; j++)
556 if( j > 0 && pshape->tabs[j].position == 0 )
557 break;
558 if( pshape->tabs[0].position == 0 ){
559 if( pshape->tabs[j].type || pshape->tabs[j].dot_continue ||
560 (pshape->tabs[j].position != 1000 *j) )
561 nscount = j;
563 else{
564 if( pshape->tabs[j].type || pshape->tabs[j].dot_continue ||
565 (pshape->tabs[j].position != 1000 * (j + 1)) )
566 nscount = j;
569 if( nscount )
570 pshape->tabs[MAXTABS-1].type = sal::static_int_cast<char>(nscount);
571 int value = compareParaShape(pshape);
572 if( value == 0 || nscount )
574 pshape->index = ++pcount;
575 pslist.push_back(pshape);
577 else
578 pshape->index = value;
582 void HWPFile::AddCharShape(CharShape * cshape)
584 int value = compareCharShape(cshape);
585 if( value == 0 )
587 cshape->index = ++ccount;
588 cslist.push_back(cshape);
590 else
591 cshape->index = value;
594 void HWPFile::AddColumnInfo()
596 ColumnInfo *cinfo = new ColumnInfo(m_nCurrentPage);
597 columnlist.push_back(cinfo);
598 setMaxSettedPage();
601 void HWPFile::SetColumnDef(ColumnDef *coldef)
603 ColumnInfo *cinfo = columnlist.back();
604 if( cinfo->bIsSet )
605 return;
606 cinfo->coldef = coldef;
607 cinfo->bIsSet = true;
610 void HWPFile::AddDateFormat(DateCode * hbox)
612 hbox->key = sal::static_int_cast<char>(++datecodecount);
613 datecodes.push_back(hbox);
616 void HWPFile::AddPageNumber(ShowPageNum * hbox)
618 pagenumbers.push_back(hbox);
621 void HWPFile::AddHeaderFooter(HeaderFooter * hbox)
623 headerfooters.push_back(hbox);
626 void HWPFile::AddTable(Table * hbox)
628 tables.push_back(hbox);
631 void HWPFile::AddFBoxStyle(FBoxStyle * fbstyle)
633 fbslist.push_back(fbstyle);
636 int HWPFile::compareCharShape(CharShape *shape)
638 int count = cslist.size();
639 if( count > 0 )
641 CharShape *cshape=0;
642 for(int i = 0; i< count; i++)
644 cshape = getCharShape(i);
646 if( shape->size == cshape->size &&
647 shape->font[0] == cshape->font[0] &&
648 shape->ratio[0] == cshape->ratio[0] &&
649 shape->space[0] == cshape->space[0] &&
650 shape->color[1] == cshape->color[1] &&
651 shape->color[0] == cshape->color[0] &&
652 shape->shade == cshape->shade &&
653 shape->attr == cshape->attr )
655 return cshape->index;
659 return 0;
663 int HWPFile::compareParaShape(ParaShape *shape)
665 int count = pslist.size();
666 if( count > 0 )
668 ParaShape *pshape=0;
669 for(int i = 0; i< count; i++)
671 pshape = getParaShape(i);
672 if( shape->left_margin == pshape->left_margin &&
673 shape->right_margin == pshape->right_margin &&
674 shape->pspacing_prev == pshape->pspacing_prev &&
675 shape->pspacing_next == pshape->pspacing_next &&
676 shape->indent == pshape->indent &&
677 shape->lspacing == pshape->lspacing &&
678 shape->arrange_type == pshape->arrange_type &&
679 shape->outline == pshape->outline &&
680 shape->pagebreak == pshape->pagebreak)
682 if( shape->cshape->size == pshape->cshape->size &&
683 shape->cshape->font[0] == pshape->cshape->font[0] &&
684 shape->cshape->ratio[0] == pshape->cshape->ratio[0] &&
685 shape->cshape->space[0] == pshape->cshape->space[0] &&
686 shape->cshape->color[1] == pshape->cshape->color[1] &&
687 shape->cshape->color[0] == pshape->cshape->color[0] &&
688 shape->cshape->shade == pshape->cshape->shade &&
689 shape->cshape->attr == pshape->cshape->attr )
691 return pshape->index;
696 return 0;
700 HWPFile *GetCurrentDoc(void)
702 return HWPFile::cur_doc;
706 HWPFile *SetCurrentDoc(HWPFile * hwpfp)
708 HWPFile *org = HWPFile::cur_doc;
710 HWPFile::cur_doc = hwpfp;
711 return org;
714 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */