Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "precompile.h"
21 :
22 : #include <stdio.h>
23 : #include <stdlib.h>
24 : #include <string.h>
25 : #include <errno.h>
26 : #include "hwplib.h"
27 : #include "hwpfile.h"
28 : #include "hiodev.h"
29 : #include "hfont.h"
30 : #include "hstyle.h"
31 : #include "hbox.h"
32 : #include "hpara.h"
33 : #include "htags.h"
34 : #include "hcode.h"
35 : #include "hstream.h"
36 :
37 : #include <osl/diagnose.h>
38 :
39 : #define HWPHeadLen 128
40 : #define HWPSummaryLen 1008
41 :
42 : #define FILESTG_SIGNATURE 0xF8995567
43 : #define FILESTG_SIGNATURE_NORMAL 0xF8995568
44 :
45 : HWPFile *HWPFile::cur_doc = 0;
46 : static int ccount = 0;
47 : static int pcount = 0;
48 : static int datecodecount = 0;
49 :
50 2 : HWPFile::HWPFile()
51 : : version(HWP_V30)
52 : , compressed(false)
53 : , encrypted(false)
54 : , linenumber(0)
55 : , info_block_len(0)
56 : , error_code(HWP_NoError)
57 : , oledata(0)
58 : , m_nCurrentPage(1)
59 : , m_nMaxSettedPage(0)
60 : , hiodev(0)
61 2 : , currenthyper(0)
62 : {
63 2 : SetCurrentDoc(this);
64 2 : }
65 :
66 4 : HWPFile::~HWPFile()
67 : {
68 2 : delete oledata;
69 2 : delete hiodev;
70 :
71 2 : std::list < ColumnInfo* >::iterator it_column = columnlist.begin();
72 4 : for (; it_column != columnlist.end(); ++it_column)
73 2 : delete *it_column;
74 :
75 2 : std::list < HWPPara* >::iterator it = plist.begin();
76 6 : for (; it != plist.end(); ++it)
77 4 : delete *it;
78 :
79 2 : std::list < Table* >::iterator tbl = tables.begin();
80 2 : for (; tbl != tables.end(); ++tbl)
81 0 : delete *tbl;
82 :
83 2 : std::list < HyperText* >::iterator hyp = hyperlist.begin();
84 2 : for (; hyp != hyperlist.end(); ++hyp)
85 : {
86 0 : delete *hyp;
87 : }
88 2 : }
89 :
90 4 : int HWPFile::ReadHwpFile(HStream & stream)
91 : {
92 4 : if (Open(stream) != HWP_NoError)
93 2 : return State();
94 2 : InfoRead();
95 2 : FontRead();
96 2 : StyleRead();
97 2 : AddColumnInfo();
98 2 : ParaListRead();
99 2 : TagsRead();
100 :
101 2 : return State();
102 : }
103 :
104 4 : int detect_hwp_version(const char *str)
105 : {
106 4 : if (memcmp(V20SIGNATURE, str, HWPIDLen) == 0)
107 0 : return HWP_V20;
108 4 : else if (memcmp(V21SIGNATURE, str, HWPIDLen) == 0)
109 0 : return HWP_V21;
110 4 : else if (memcmp(V30SIGNATURE, str, HWPIDLen) == 0)
111 2 : return HWP_V30;
112 2 : return 0;
113 : }
114 :
115 : // HIODev wrapper
116 :
117 4 : int HWPFile::Open(HStream & stream)
118 : {
119 4 : HStreamIODev *hstreamio = new HStreamIODev(stream);
120 :
121 4 : if (!hstreamio->open())
122 : {
123 0 : delete hstreamio;
124 :
125 0 : return SetState(HWP_EMPTY_FILE);
126 : }
127 :
128 4 : HIODev *pPrev = SetIODevice(hstreamio);
129 4 : delete pPrev;
130 :
131 : char idstr[HWPIDLen];
132 :
133 8 : if (ReadBlock(idstr, HWPIDLen) <= 0
134 4 : || HWP_V30 != (version = detect_hwp_version(idstr)))
135 : {
136 2 : return SetState(HWP_UNSUPPORTED_VERSION);
137 : }
138 2 : return HWP_NoError;
139 : }
140 :
141 :
142 176 : int HWPFile::State(void) const
143 : {
144 176 : return error_code;
145 : }
146 :
147 :
148 2 : int HWPFile::SetState(int errcode)
149 : {
150 2 : error_code = errcode;
151 2 : return error_code;
152 : }
153 :
154 :
155 0 : int HWPFile::Read1b(void)
156 : {
157 0 : return hiodev ? hiodev->read1b() : -1;
158 : }
159 :
160 :
161 1536 : int HWPFile::Read2b(void)
162 : {
163 1536 : return hiodev ? hiodev->read2b() : -1;
164 : }
165 :
166 :
167 4 : long HWPFile::Read4b(void)
168 : {
169 4 : return hiodev ? hiodev->read4b() : -1;
170 : }
171 :
172 :
173 2822 : int HWPFile::Read1b(void *ptr, size_t nmemb)
174 : {
175 2822 : return hiodev ? hiodev->read1b(ptr, nmemb) : 0;
176 : }
177 :
178 :
179 64 : int HWPFile::Read2b(void *ptr, size_t nmemb)
180 : {
181 64 : return hiodev ? hiodev->read2b(ptr, nmemb) : 0;
182 : }
183 :
184 :
185 6 : int HWPFile::Read4b(void *ptr, size_t nmemb)
186 : {
187 6 : return hiodev ? hiodev->read4b(ptr, nmemb) : 0;
188 : }
189 :
190 :
191 1434 : size_t HWPFile::ReadBlock(void *ptr, size_t size)
192 : {
193 1434 : return hiodev ? hiodev->readBlock(ptr, size) : 0;
194 : }
195 :
196 :
197 0 : size_t HWPFile::SkipBlock(size_t size)
198 : {
199 0 : return hiodev ? hiodev->skipBlock(size) : 0;
200 : }
201 :
202 :
203 2 : bool HWPFile::SetCompressed(bool flag)
204 : {
205 2 : return hiodev ? hiodev->setCompressed(flag) : false;
206 : }
207 :
208 :
209 4 : HIODev *HWPFile::SetIODevice(HIODev * new_hiodev)
210 : {
211 4 : HIODev *old_hiodev = hiodev;
212 :
213 4 : hiodev = new_hiodev;
214 :
215 4 : return old_hiodev;
216 : }
217 :
218 :
219 : // end of HIODev wrapper
220 :
221 2 : bool HWPFile::InfoRead(void)
222 : {
223 2 : return _hwpInfo.Read(*this);
224 : }
225 :
226 :
227 2 : bool HWPFile::FontRead(void)
228 : {
229 2 : return _hwpFont.Read(*this);
230 : }
231 :
232 :
233 2 : bool HWPFile::StyleRead(void)
234 : {
235 2 : return _hwpStyle.Read(*this);
236 : }
237 :
238 :
239 2 : bool HWPFile::ParaListRead(void)
240 : {
241 2 : return ReadParaList(plist);
242 : }
243 :
244 2 : bool HWPFile::ReadParaList(std::list < HWPPara* > &aplist, unsigned char flag)
245 : {
246 2 : HWPPara *spNode = new HWPPara;
247 : unsigned char tmp_etcflag;
248 2 : unsigned char prev_etcflag = 0;
249 8 : while (spNode->Read(*this, flag))
250 : {
251 4 : if( !(spNode->etcflag & 0x04) ){
252 4 : tmp_etcflag = spNode->etcflag;
253 4 : spNode->etcflag = prev_etcflag;
254 4 : prev_etcflag = tmp_etcflag;
255 : }
256 4 : if (spNode->nch && spNode->reuse_shape)
257 : {
258 0 : if (!aplist.empty()){
259 0 : spNode->pshape = aplist.back()->pshape;
260 : }
261 : else{
262 0 : spNode->nch = 0;
263 0 : spNode->reuse_shape = 0;
264 : }
265 : }
266 4 : spNode->pshape.pagebreak = spNode->etcflag;
267 4 : if( spNode->nch )
268 4 : AddParaShape( &spNode->pshape );
269 :
270 4 : if (!aplist.empty())
271 2 : aplist.back()->SetNext(spNode);
272 4 : aplist.push_back(spNode);
273 4 : spNode = new HWPPara;
274 : }
275 2 : delete spNode;
276 :
277 2 : return true;
278 : }
279 :
280 :
281 2 : bool HWPFile::TagsRead(void)
282 : {
283 : ulong tag;
284 : long size;
285 :
286 0 : while (1)
287 : {
288 2 : tag = Read4b();
289 2 : size = Read4b();
290 2 : if (size <= 0 && tag > 0){
291 0 : continue;
292 : }
293 :
294 2 : if (tag == FILETAG_END_OF_COMPRESSED ||
295 : tag == FILETAG_END_OF_UNCOMPRESSED)
296 4 : return true;
297 0 : switch (tag)
298 : {
299 : case FILETAG_EMBEDDED_PICTURE:
300 : {
301 0 : EmPicture *emb = new EmPicture(size);
302 :
303 0 : if (true == emb->Read(*this))
304 0 : emblist.push_back(emb);
305 : else
306 0 : delete emb;
307 : }
308 0 : break;
309 : case FILETAG_OLE_OBJECT:
310 0 : if (oledata)
311 0 : delete oledata;
312 0 : oledata = new OlePicture(size);
313 0 : oledata->Read(*this);
314 0 : break;
315 : case FILETAG_HYPERTEXT:
316 : {
317 0 : if( (size % 617) != 0 )
318 0 : SkipBlock( size );
319 : else
320 0 : for( int i = 0 ; i < size/617 ; i++)
321 : {
322 0 : HyperText *hypert = new HyperText;
323 0 : hypert->Read(*this);
324 0 : hyperlist.push_back(hypert);
325 : }
326 0 : break;
327 : }
328 : case 6:
329 : {
330 0 : ReadBlock(_hwpInfo.back_info.reserved1, 8);
331 0 : _hwpInfo.back_info.luminance = Read4b();
332 0 : _hwpInfo.back_info.contrast = Read4b();
333 0 : _hwpInfo.back_info.effect = sal::static_int_cast<char>(Read1b());
334 0 : ReadBlock(_hwpInfo.back_info.reserved2, 7);
335 0 : ReadBlock(_hwpInfo.back_info.filename, 260);
336 0 : ReadBlock(_hwpInfo.back_info.color, 3);
337 0 : unsigned short nFlag = sal::static_int_cast<unsigned short>(Read2b());
338 0 : _hwpInfo.back_info.flag = nFlag >> 8 ;
339 0 : int nRange = Read4b();
340 0 : _hwpInfo.back_info.range = nRange >> 24;
341 0 : ReadBlock(_hwpInfo.back_info.reserved3, 27);
342 0 : _hwpInfo.back_info.size = Read4b();
343 :
344 0 : _hwpInfo.back_info.data = new char[(unsigned int)_hwpInfo.back_info.size];
345 0 : ReadBlock(_hwpInfo.back_info.data, _hwpInfo.back_info.size);
346 :
347 0 : if( _hwpInfo.back_info.size > 0 )
348 0 : _hwpInfo.back_info.type = 2;
349 0 : else if( _hwpInfo.back_info.filename[0] )
350 0 : _hwpInfo.back_info.type = 1;
351 : else
352 0 : _hwpInfo.back_info.type = 0;
353 :
354 :
355 0 : _hwpInfo.back_info.isset = true;
356 :
357 0 : break;
358 : }
359 : case FILETAG_PRESENTATION:
360 : case FILETAG_PREVIEW_IMAGE:
361 : case FILETAG_PREVIEW_TEXT:
362 : default:
363 0 : SkipBlock(size);
364 : }
365 : }
366 : }
367 :
368 :
369 2 : ColumnDef *HWPFile::GetColumnDef(int num)
370 : {
371 2 : std::list<ColumnInfo*>::iterator it = columnlist.begin();
372 :
373 2 : for(int i = 0; it != columnlist.end() ; ++it, i++){
374 2 : if( i == num )
375 2 : break;
376 : }
377 :
378 2 : if( it != columnlist.end() )
379 2 : return (*it)->coldef;
380 : else
381 0 : return 0;
382 : }
383 : /* @return À妽º´Â 1ºÎÅÍ ½ÃÀÛÇÑ´Ù. */
384 2 : int HWPFile::GetPageMasterNum(int page)
385 : {
386 2 : std::list<ColumnInfo*>::iterator it = columnlist.begin();
387 2 : ColumnInfo *now = 0;
388 : int i;
389 :
390 4 : for( i = 1 ; it != columnlist.end() ; ++it, i++){
391 2 : now = *it;
392 2 : if( page < now->start_page )
393 0 : return i-1;
394 : }
395 2 : return i-1;
396 : }
397 :
398 0 : HyperText *HWPFile::GetHyperText()
399 : {
400 0 : std::list<HyperText*>::iterator it = hyperlist.begin();
401 :
402 0 : for( int i = 0; it != hyperlist.end(); ++it, i++ ){
403 0 : if( i == currenthyper )
404 0 : break;
405 : }
406 :
407 0 : currenthyper++;
408 0 : return *it;
409 : }
410 :
411 0 : EmPicture *HWPFile::GetEmPicture(Picture * pic)
412 : {
413 0 : char *name = pic->picinfo.picembed.embname;
414 :
415 0 : name[0] = 'H';
416 0 : name[1] = 'W';
417 0 : name[2] = 'P';
418 :
419 0 : std::list < EmPicture* >::iterator it = emblist.begin();
420 0 : for (; it != emblist.end(); ++it)
421 0 : if (strcmp(name, (*it)->name) == 0)
422 0 : return *it;
423 0 : return 0;
424 : }
425 :
426 0 : EmPicture *HWPFile::GetEmPictureByName(char * name)
427 : {
428 0 : name[0] = 'H';
429 0 : name[1] = 'W';
430 0 : name[2] = 'P';
431 :
432 0 : std::list < EmPicture* >::iterator it = emblist.begin();
433 0 : for (; it != emblist.end(); ++it)
434 0 : if (strcmp(name, (*it)->name) == 0)
435 0 : return *it;
436 0 : return 0;
437 : }
438 :
439 :
440 0 : void HWPFile::AddBox(FBox * box)
441 : {
442 : // LATER if we don't use box->next(),
443 : // AddBox() and GetBoxHead() are useless;
444 0 : if (!blist.empty())
445 : {
446 0 : box->prev = blist.back();
447 0 : box->prev->next = box;
448 : }
449 : else
450 0 : box->prev = 0;
451 0 : blist.push_back(box);
452 0 : }
453 :
454 :
455 4 : ParaShape *HWPFile::getParaShape(int index)
456 : {
457 4 : std::list<ParaShape*>::iterator it = pslist.begin();
458 :
459 4 : for( int i = 0; it != pslist.end(); ++it, i++ ){
460 4 : if( i == index )
461 4 : break;
462 : }
463 :
464 4 : return *it;
465 : }
466 :
467 :
468 8 : CharShape *HWPFile::getCharShape(int index)
469 : {
470 8 : std::list<CharShape*>::iterator it = cslist.begin();
471 :
472 8 : for( int i = 0; it != cslist.end(); ++it, i++ ){
473 8 : if( i == index )
474 8 : break;
475 : }
476 :
477 8 : return *it;
478 : }
479 :
480 :
481 0 : FBoxStyle *HWPFile::getFBoxStyle(int index)
482 : {
483 0 : std::list<FBoxStyle*>::iterator it = fbslist.begin();
484 :
485 0 : for( int i = 0; it != fbslist.end(); ++it, i++ ){
486 0 : if( i == index )
487 0 : break;
488 : }
489 :
490 0 : return *it;
491 : }
492 :
493 0 : DateCode *HWPFile::getDateCode(int index)
494 : {
495 0 : std::list<DateCode*>::iterator it = datecodes.begin();
496 :
497 0 : for( int i = 0; it != datecodes.end(); ++it, i++ ){
498 0 : if( i == index )
499 0 : break;
500 : }
501 :
502 0 : return *it;
503 : }
504 :
505 0 : HeaderFooter *HWPFile::getHeaderFooter(int index)
506 : {
507 0 : std::list<HeaderFooter*>::iterator it = headerfooters.begin();
508 :
509 0 : for( int i = 0; it != headerfooters.end(); ++it, i++ ){
510 0 : if( i == index )
511 0 : break;
512 : }
513 :
514 0 : return *it;
515 : }
516 :
517 0 : ShowPageNum *HWPFile::getPageNumber(int index)
518 : {
519 0 : std::list<ShowPageNum*>::iterator it = pagenumbers.begin();
520 :
521 0 : for( int i = 0; it != pagenumbers.end(); ++it, i++ ){
522 0 : if( i == index )
523 0 : break;
524 : }
525 :
526 0 : return *it;
527 :
528 : }
529 :
530 0 : Table *HWPFile::getTable(int index)
531 : {
532 0 : std::list<Table*>::iterator it = tables.begin();
533 :
534 0 : for( int i = 0; it != tables.end(); ++it, i++ ){
535 0 : if( i == index )
536 0 : break;
537 : }
538 :
539 0 : return *it;
540 : }
541 :
542 4 : void HWPFile::AddParaShape(ParaShape * pshape)
543 : {
544 4 : int nscount = 0;
545 136 : for(int j = 0 ; j < MAXTABS-1 ; j++)
546 : {
547 136 : if( j > 0 && pshape->tabs[j].position == 0 )
548 4 : break;
549 132 : if( pshape->tabs[0].position == 0 ){
550 132 : if( pshape->tabs[j].type || pshape->tabs[j].dot_continue ||
551 : (pshape->tabs[j].position != 1000 *j) )
552 0 : nscount = j;
553 : }
554 : else{
555 0 : if( pshape->tabs[j].type || pshape->tabs[j].dot_continue ||
556 : (pshape->tabs[j].position != 1000 * (j + 1)) )
557 0 : nscount = j;
558 : }
559 : }
560 4 : if( nscount )
561 0 : pshape->tabs[MAXTABS-1].type = sal::static_int_cast<char>(nscount);
562 4 : int value = compareParaShape(pshape);
563 4 : if( value == 0 || nscount )
564 : {
565 2 : pshape->index = ++pcount;
566 2 : pslist.push_back(pshape);
567 : }
568 : else
569 2 : pshape->index = value;
570 4 : }
571 :
572 :
573 8 : void HWPFile::AddCharShape(CharShape * cshape)
574 : {
575 8 : int value = compareCharShape(cshape);
576 8 : if( value == 0 )
577 : {
578 2 : cshape->index = ++ccount;
579 2 : cslist.push_back(cshape);
580 : }
581 : else
582 6 : cshape->index = value;
583 8 : }
584 :
585 2 : void HWPFile::AddColumnInfo()
586 : {
587 2 : ColumnInfo *cinfo = new ColumnInfo(m_nCurrentPage);
588 2 : columnlist.push_back(cinfo);
589 2 : setMaxSettedPage();
590 2 : }
591 :
592 0 : void HWPFile::SetColumnDef(ColumnDef *coldef)
593 : {
594 0 : ColumnInfo *cinfo = columnlist.back();
595 0 : if( cinfo->bIsSet )
596 0 : return;
597 0 : cinfo->coldef = coldef;
598 0 : cinfo->bIsSet = true;
599 : }
600 :
601 0 : void HWPFile::AddDateFormat(DateCode * hbox)
602 : {
603 0 : hbox->key = sal::static_int_cast<char>(++datecodecount);
604 0 : datecodes.push_back(hbox);
605 0 : }
606 :
607 0 : void HWPFile::AddPageNumber(ShowPageNum * hbox)
608 : {
609 0 : pagenumbers.push_back(hbox);
610 0 : }
611 :
612 0 : void HWPFile::AddHeaderFooter(HeaderFooter * hbox)
613 : {
614 0 : headerfooters.push_back(hbox);
615 0 : }
616 :
617 0 : void HWPFile::AddTable(Table * hbox)
618 : {
619 0 : tables.push_back(hbox);
620 0 : }
621 :
622 0 : void HWPFile::AddFBoxStyle(FBoxStyle * fbstyle)
623 : {
624 0 : fbslist.push_back(fbstyle);
625 0 : }
626 :
627 8 : int HWPFile::compareCharShape(CharShape *shape)
628 : {
629 8 : int count = cslist.size();
630 8 : if( count > 0 )
631 : {
632 6 : CharShape *cshape=0;
633 6 : for(int i = 0; i< count; i++)
634 : {
635 6 : cshape = getCharShape(i);
636 :
637 36 : if( shape->size == cshape->size &&
638 6 : shape->font[0] == cshape->font[0] &&
639 6 : shape->ratio[0] == cshape->ratio[0] &&
640 6 : shape->space[0] == cshape->space[0] &&
641 6 : shape->color[1] == cshape->color[1] &&
642 6 : shape->color[0] == cshape->color[0] &&
643 : shape->shade == cshape->shade &&
644 : shape->attr == cshape->attr )
645 : {
646 6 : return cshape->index;
647 : }
648 : }
649 : }
650 2 : return 0;
651 : }
652 :
653 :
654 4 : int HWPFile::compareParaShape(ParaShape *shape)
655 : {
656 4 : int count = pslist.size();
657 4 : if( count > 0 )
658 : {
659 2 : ParaShape *pshape=0;
660 2 : for(int i = 0; i< count; i++)
661 : {
662 2 : pshape = getParaShape(i);
663 2 : if( shape->left_margin == pshape->left_margin &&
664 : shape->right_margin == pshape->right_margin &&
665 : shape->pspacing_prev == pshape->pspacing_prev &&
666 : shape->pspacing_next == pshape->pspacing_next &&
667 : shape->indent == pshape->indent &&
668 : shape->lspacing == pshape->lspacing &&
669 : shape->arrange_type == pshape->arrange_type &&
670 : shape->outline == pshape->outline &&
671 : shape->pagebreak == pshape->pagebreak)
672 : {
673 12 : if( shape->cshape->size == pshape->cshape->size &&
674 2 : shape->cshape->font[0] == pshape->cshape->font[0] &&
675 2 : shape->cshape->ratio[0] == pshape->cshape->ratio[0] &&
676 2 : shape->cshape->space[0] == pshape->cshape->space[0] &&
677 2 : shape->cshape->color[1] == pshape->cshape->color[1] &&
678 2 : shape->cshape->color[0] == pshape->cshape->color[0] &&
679 : shape->cshape->shade == pshape->cshape->shade &&
680 : shape->cshape->attr == pshape->cshape->attr )
681 : {
682 2 : return pshape->index;
683 : }
684 : }
685 : }
686 : }
687 2 : return 0;
688 : }
689 :
690 :
691 0 : HWPFile *GetCurrentDoc(void)
692 : {
693 0 : return HWPFile::cur_doc;
694 : }
695 :
696 :
697 2 : HWPFile *SetCurrentDoc(HWPFile * hwpfp)
698 : {
699 2 : HWPFile *org = HWPFile::cur_doc;
700 :
701 2 : HWPFile::cur_doc = hwpfp;
702 2 : return org;
703 : }
704 :
705 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|