1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <rtftokenizer.hxx>
#include <svx/dialogs.hrc>
#include <vcl/svapp.hxx>
#include <rtl/strbuf.hxx>
#include <rtfskipdestination.hxx>
#include <rtl/character.hxx>
#include <com/sun/star/io/BufferSizeExceededException.hpp>
using namespace com::sun::star;
namespace writerfilter
{
namespace rtftok
{
std::vector<RTFSymbol> RTFTokenizer::s_aRTFControlWords;
bool RTFTokenizer::s_bControlWordsSorted;
std::vector<RTFMathSymbol> RTFTokenizer::s_aRTFMathControlWords;
bool RTFTokenizer::s_bMathControlWordsSorted;
RTFTokenizer::RTFTokenizer(RTFListener& rImport, SvStream* pInStream, uno::Reference<task::XStatusIndicator> const& xStatusIndicator)
: m_rImport(rImport),
m_pInStream(pInStream),
m_xStatusIndicator(xStatusIndicator),
m_nGroup(0),
m_nLineNumber(0),
m_nLineStartPos(0),
m_nGroupStart(0)
{
if (!RTFTokenizer::s_bControlWordsSorted)
{
RTFTokenizer::s_bControlWordsSorted = true;
s_aRTFControlWords = std::vector<RTFSymbol>(aRTFControlWords, aRTFControlWords + nRTFControlWords);
std::sort(s_aRTFControlWords.begin(), s_aRTFControlWords.end());
}
if (!RTFTokenizer::s_bMathControlWordsSorted)
{
RTFTokenizer::s_bMathControlWordsSorted = true;
s_aRTFMathControlWords = std::vector<RTFMathSymbol>(aRTFMathControlWords, aRTFMathControlWords + nRTFMathControlWords);
std::sort(s_aRTFMathControlWords.begin(), s_aRTFMathControlWords.end());
}
}
RTFTokenizer::~RTFTokenizer() = default;
RTFError RTFTokenizer::resolveParse()
{
SAL_INFO("writerfilter.rtf", OSL_THIS_FUNC);
char ch;
RTFError ret;
// for hex chars
int b = 0, count = 2;
std::size_t nPercentSize = 0;
sal_uInt64 nLastPos = 0;
if (m_xStatusIndicator.is())
{
static ResMgr* pResMgr = ResMgr::CreateResMgr("svx", Application::GetSettings().GetUILanguageTag());
OUString sDocLoad(ResId(RID_SVXSTR_DOC_LOAD, *pResMgr).toString());
sal_uInt64 const nCurrentPos = Strm().Tell();
sal_uInt64 const nEndPos = nCurrentPos + Strm().remainingSize();
m_xStatusIndicator->start(sDocLoad, nEndPos);
nPercentSize = nEndPos / 100;
m_xStatusIndicator->setValue(nLastPos = nCurrentPos);
}
while ((Strm().ReadChar(ch), !Strm().IsEof()))
{
//SAL_INFO("writerfilter", OSL_THIS_FUNC << ": parsing character '" << ch << "'");
sal_uInt64 const nCurrentPos = Strm().Tell();
if (m_xStatusIndicator.is() && nCurrentPos > (nLastPos + nPercentSize))
m_xStatusIndicator->setValue(nLastPos = nCurrentPos);
if (m_nGroup < 0)
return RTFError::GROUP_UNDER;
if (m_nGroup > 0 && m_rImport.getInternalState() == RTFInternalState::BIN)
{
ret = m_rImport.resolveChars(ch);
if (ret != RTFError::OK)
return ret;
}
else
{
switch (ch)
{
case '{':
m_nGroupStart = Strm().Tell() - 1;
ret = m_rImport.pushState();
if (ret != RTFError::OK)
return ret;
break;
case '}':
ret = m_rImport.popState();
if (ret != RTFError::OK)
return ret;
if (m_nGroup == 0)
{
if (m_rImport.isSubstream())
m_rImport.finishSubstream();
return RTFError::OK;
}
break;
case '\\':
ret = resolveKeyword();
if (ret != RTFError::OK)
return ret;
break;
case 0x0d:
break; // ignore this
case 0x0a:
m_nLineNumber++;
m_nLineStartPos = nCurrentPos;
break;
default:
if (m_nGroup == 0)
return RTFError::CHAR_OVER;
if (m_rImport.getInternalState() == RTFInternalState::NORMAL)
{
ret = m_rImport.resolveChars(ch);
if (ret != RTFError::OK)
return ret;
}
else
{
SAL_INFO("writerfilter.rtf", OSL_THIS_FUNC << ": hex internal state");
b = b << 4;
sal_Int8 parsed = asHex(ch);
if (parsed == -1)
return RTFError::HEX_INVALID;
b += parsed;
count--;
if (!count)
{
ret = m_rImport.resolveChars(b);
if (ret != RTFError::OK)
return ret;
count = 2;
b = 0;
m_rImport.setInternalState(RTFInternalState::NORMAL);
}
}
break;
}
}
}
if (m_nGroup < 0)
return RTFError::GROUP_UNDER;
else if (m_nGroup > 0)
return RTFError::GROUP_OVER;
return RTFError::OK;
}
int RTFTokenizer::asHex(char ch)
{
int ret = 0;
if (rtl::isAsciiDigit(static_cast<unsigned char>(ch)))
ret = ch - '0';
else
{
if (ch >= 'a' && ch <= 'f')
ret = ch - 'a';
else if (ch >= 'A' && ch <= 'F')
ret = ch - 'A';
else
return -1;
ret += 10;
}
return ret;
}
void RTFTokenizer::pushGroup()
{
m_nGroup++;
}
void RTFTokenizer::popGroup()
{
m_nGroup--;
}
RTFError RTFTokenizer::resolveKeyword()
{
char ch;
OStringBuffer aBuf;
bool bNeg = false;
bool bParam = false;
int nParam = 0;
Strm().ReadChar(ch);
if (Strm().IsEof())
return RTFError::UNEXPECTED_EOF;
if (!rtl::isAsciiAlpha(static_cast<unsigned char>(ch)))
{
aBuf.append(ch);
OString aKeyword = aBuf.makeStringAndClear();
// control symbols aren't followed by a space, so we can return here
// without doing any SeekRel()
return dispatchKeyword(aKeyword, bParam, nParam);
}
while (rtl::isAsciiAlpha(static_cast<unsigned char>(ch)))
{
aBuf.append(ch);
Strm().ReadChar(ch);
if (Strm().IsEof())
{
ch = ' ';
break;
}
}
if (aBuf.getLength() > 32)
// See RTF spec v1.9.1, page 7
// A control word's name cannot be longer than 32 letters.
throw io::BufferSizeExceededException();
if (ch == '-')
{
// in case we'll have a parameter, that will be negative
bNeg = true;
Strm().ReadChar(ch);
if (Strm().IsEof())
return RTFError::UNEXPECTED_EOF;
}
if (rtl::isAsciiDigit(static_cast<unsigned char>(ch)))
{
OStringBuffer aParameter;
// we have a parameter
bParam = true;
while (rtl::isAsciiDigit(static_cast<unsigned char>(ch)))
{
aParameter.append(ch);
Strm().ReadChar(ch);
if (Strm().IsEof())
{
ch = ' ';
break;
}
}
nParam = aParameter.makeStringAndClear().toInt32();
if (bNeg)
nParam = -nParam;
}
if (ch != ' ')
Strm().SeekRel(-1);
OString aKeyword = aBuf.makeStringAndClear();
return dispatchKeyword(aKeyword, bParam, nParam);
}
bool RTFTokenizer::lookupMathKeyword(RTFMathSymbol& rSymbol)
{
auto low = std::lower_bound(s_aRTFMathControlWords.begin(), s_aRTFMathControlWords.end(), rSymbol);
int i = low - s_aRTFMathControlWords.begin();
if (low == s_aRTFMathControlWords.end() || rSymbol < *low)
return false;
rSymbol = s_aRTFMathControlWords[i];
return true;
}
RTFError RTFTokenizer::dispatchKeyword(OString& rKeyword, bool bParam, int nParam)
{
if (m_rImport.getDestination() == Destination::SKIP)
{
// skip binary data explicitly, to not trip over rtf markup
// control characters
if (rKeyword.equals("bin") && nParam > 0)
Strm().SeekRel(nParam);
return RTFError::OK;
}
SAL_INFO("writerfilter.rtf", OSL_THIS_FUNC << ": keyword '\\" << rKeyword.getStr() <<
"' with param? " << (bParam ? 1 : 0) <<" param val: '" << (bParam ? nParam : 0) << "'");
RTFSymbol aSymbol;
aSymbol.sKeyword = rKeyword.getStr();
auto low = std::lower_bound(s_aRTFControlWords.begin(), s_aRTFControlWords.end(), aSymbol);
int i = low - s_aRTFControlWords.begin();
if (low == s_aRTFControlWords.end() || aSymbol < *low)
{
SAL_INFO("writerfilter.rtf", OSL_THIS_FUNC << ": unknown keyword '\\" << rKeyword.getStr() << "'");
RTFSkipDestination aSkip(m_rImport);
aSkip.setParsed(false);
return RTFError::OK;
}
RTFError ret;
switch (s_aRTFControlWords[i].nControlType)
{
case CONTROL_FLAG:
// flags ignore any parameter by definition
ret = m_rImport.dispatchFlag(s_aRTFControlWords[i].nIndex);
if (ret != RTFError::OK)
return ret;
break;
case CONTROL_DESTINATION:
// same for destinations
ret = m_rImport.dispatchDestination(s_aRTFControlWords[i].nIndex);
if (ret != RTFError::OK)
return ret;
break;
case CONTROL_SYMBOL:
// and symbols
ret = m_rImport.dispatchSymbol(s_aRTFControlWords[i].nIndex);
if (ret != RTFError::OK)
return ret;
break;
case CONTROL_TOGGLE:
ret = m_rImport.dispatchToggle(s_aRTFControlWords[i].nIndex, bParam, nParam);
if (ret != RTFError::OK)
return ret;
break;
case CONTROL_VALUE:
// Values require a parameter by definition, but Word doesn't respect this for \dibitmap.
if (bParam || s_aRTFControlWords[i].nIndex == RTF_DIBITMAP)
{
ret = m_rImport.dispatchValue(s_aRTFControlWords[i].nIndex, nParam);
if (ret != RTFError::OK)
return ret;
}
break;
}
return RTFError::OK;
}
OUString RTFTokenizer::getPosition()
{
OUStringBuffer aRet;
aRet.append(m_nLineNumber + 1);
aRet.append(",");
aRet.append(sal_Int32(Strm().Tell() - m_nLineStartPos + 1));
return aRet.makeStringAndClear();
}
} // namespace rtftok
} // namespace writerfilter
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|