1 /*
2 * Copyright (C) 2004-2010 Geometer Plus <contact@geometerplus.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 */
19
20 #include <cstring>
21
22 #include <ZLFile.h>
23 #include <ZLZDecompressor.h>
24
25 #include "PluckerTextStream.h"
26 #include "PdbReader.h"
27 #include "DocDecompressor.h"
28
PluckerTextStream(const ZLFile & file)29 PluckerTextStream::PluckerTextStream(const ZLFile &file) : PdbStream(file) {
30 myFullBuffer = 0;
31 }
32
~PluckerTextStream()33 PluckerTextStream::~PluckerTextStream() {
34 close();
35 }
36
open()37 bool PluckerTextStream::open() {
38 if (!PdbStream::open()) {
39 return false;
40 }
41
42 PdbUtil::readUnsignedShort(*myBase, myCompressionVersion);
43
44 myBuffer = new char[65536];
45 myFullBuffer = new char[65536];
46
47 myRecordIndex = 0;
48
49 return true;
50 }
51
fillBuffer()52 bool PluckerTextStream::fillBuffer() {
53 while (myBufferOffset == myBufferLength) {
54 if (myRecordIndex + 1 > header().Offsets.size() - 1) {
55 return false;
56 }
57 ++myRecordIndex;
58 const size_t currentOffset = recordOffset(myRecordIndex);
59 if (currentOffset < myBase->offset()) {
60 return false;
61 }
62 myBase->seek(currentOffset, true);
63 const size_t nextOffset = recordOffset(myRecordIndex + 1);
64 if (nextOffset < currentOffset) {
65 return false;
66 }
67 processRecord(nextOffset - currentOffset);
68 }
69 return true;
70 }
71
close()72 void PluckerTextStream::close() {
73 if (myFullBuffer != 0) {
74 delete[] myFullBuffer;
75 myFullBuffer = 0;
76 }
77 PdbStream::close();
78 }
79
processRecord(size_t recordSize)80 void PluckerTextStream::processRecord(size_t recordSize) {
81 myBase->seek(2, false);
82
83 unsigned short paragraphs;
84 PdbUtil::readUnsignedShort(*myBase, paragraphs);
85
86 unsigned short size;
87 PdbUtil::readUnsignedShort(*myBase, size);
88
89 unsigned char type;
90 myBase->read((char*)&type, 1);
91 if (type > 1) { // this record is not text record
92 return;
93 }
94
95 myBase->seek(1, false);
96
97 std::vector<int> pars;
98 for (int i = 0; i < paragraphs; ++i) {
99 unsigned short pSize;
100 PdbUtil::readUnsignedShort(*myBase, pSize);
101 pars.push_back(pSize);
102 myBase->seek(2, false);
103 }
104
105 bool doProcess = false;
106 if (type == 0) {
107 doProcess = myBase->read(myFullBuffer, size) == size;
108 } else if (myCompressionVersion == 1) {
109 doProcess =
110 DocDecompressor().decompress(*myBase, myFullBuffer, recordSize - 8 - 4 * paragraphs, size) == size;
111 } else if (myCompressionVersion == 2) {
112 myBase->seek(2, false);
113 doProcess =
114 ZLZDecompressor(recordSize - 10 - 4 * paragraphs).decompress(*myBase, myFullBuffer, size) == size;
115 }
116 if (doProcess) {
117 myBufferLength = 0;
118 myBufferOffset = 0;
119
120 char *start = myFullBuffer;
121 char *end = myFullBuffer;
122
123 for (std::vector<int>::const_iterator it = pars.begin(); it != pars.end(); ++it) {
124 start = end;
125 end = start + *it;
126 if (end > myFullBuffer + size) {
127 break;
128 }
129 processTextParagraph(start, end);
130 }
131 }
132 }
133
processTextParagraph(char * start,char * end)134 void PluckerTextStream::processTextParagraph(char *start, char *end) {
135 char *textStart = start;
136 bool functionFlag = false;
137 for (char *ptr = start; ptr < end; ++ptr) {
138 if (*ptr == 0) {
139 functionFlag = true;
140 if (ptr != textStart) {
141 memcpy(myBuffer + myBufferLength, textStart, ptr - textStart);
142 myBufferLength += ptr - textStart;
143 }
144 } else if (functionFlag) {
145 int paramCounter = ((unsigned char)*ptr) % 8;
146 if (end - ptr > paramCounter + 1) {
147 ptr += paramCounter;
148 } else {
149 ptr = end - 1;
150 }
151 functionFlag = false;
152 textStart = ptr + 1;
153 }
154 }
155 if (end != textStart) {
156 memcpy(myBuffer + myBufferLength, textStart, end - textStart);
157 myBufferLength += end - textStart;
158 }
159 }
160