aboutsummaryrefslogtreecommitdiff
path: root/editors/koffice-kde4/files/patch-filters_kword_rtf_import-rtfimport_tokenizer
blob: 78be2fe6f9ce9b519aad610f532f273e2fa42add (plain) (blame)
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
Index: filters/kword/rtf/import/rtfimport_tokenizer.cpp
===================================================================
--- filters/kword/rtf/import/rtfimport_tokenizer.cpp	(revision 463235)
+++ filters/kword/rtf/import/rtfimport_tokenizer.cpp	(working copy)
@@ -16,7 +16,7 @@
 
 RTFTokenizer::RTFTokenizer()
 {
-    tokenText.resize( 4112 );
+    tokenText.resize( 4113 );
     fileBuffer.resize( 4096 );
     infile = 0L;
 }
@@ -30,8 +30,25 @@
     fileBufferPtr = 0L;
     fileBufferEnd = 0L;
     infile = in;
+    type = RTFTokenizer::PlainText;
 }
 
+int RTFTokenizer::nextChar()
+{
+    if ( fileBufferPtr == fileBufferEnd ) {
+        int n = infile->readBlock( fileBuffer.data(), fileBuffer.size() );
+        fileBufferPtr = ( uchar* ) fileBuffer.data();
+        fileBufferEnd = fileBufferPtr;
+
+        if ( n <= 0 )
+            return -1;
+
+        fileBufferEnd = fileBufferPtr + n;
+    }
+    return *fileBufferPtr++;
+}
+
+
 /**
  * Reads the next token.
  */
@@ -42,22 +59,15 @@
     if (!infile)
 	return;
 
-    do
-    {
-	if (fileBufferPtr == fileBufferEnd)
-	{
-	    int n = infile->readBlock( fileBuffer.data(), fileBuffer.size() );
+    do {
+        int n = nextChar();
 
-	    if (n <= 0)
-	    {
-		// Return CloseGroup on EOF
-		ch = '}';
-		break;
-	    }
-	    fileBufferPtr = (uchar *)fileBuffer.data();
-	    fileBufferEnd = (fileBufferPtr + n);
-	}
-	ch = *fileBufferPtr++;
+        if ( n <= 0 ) {
+            ch = '}';
+            break;
+        }
+
+        ch = n;
     }
     while (ch == '\n' || ch == '\r' && ch != 0);
 
@@ -67,6 +77,7 @@
 
     uchar *_text = (uchar *)text;
 
+
     if (ch == '{')
 	type = RTFTokenizer::OpenGroup;
     else if (ch == '}')
@@ -75,20 +86,14 @@
     {
 	type = RTFTokenizer::ControlWord;
 
-	if (fileBufferPtr == fileBufferEnd)
-	{
-	    int n = infile->readBlock( fileBuffer.data(), fileBuffer.size() );
+        int n = nextChar();
 
-	    if (n <= 0)
-	    {
-		// Return CloseGroup on EOF
-		type = RTFTokenizer::CloseGroup;
-		return;
-	    }
-	    fileBufferPtr = (uchar *)fileBuffer.data();
-	    fileBufferEnd = (fileBufferPtr + n);
-	}
-	ch = *fileBufferPtr++;
+        if ( n <= 0 ) {
+            // Return CloseGroup on EOF
+            type = RTFTokenizer::CloseGroup;
+            return;
+        }
+	ch = n;
 
 	// Type is either control word or control symbol
 	if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
@@ -96,64 +101,41 @@
 	    int v = 0;
 
 	    // Read alphabetic string (command)
-	    while ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
+	    while (_text < ( uchar* )tokenText.data()+tokenText.size()-3 && 
+                  ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) )
 	    {
 		*_text++ = ch;
 
-		if (fileBufferPtr == fileBufferEnd)
-		{
-		    int n = infile->readBlock( fileBuffer.data(), fileBuffer.size() );
-
-		    if (n <= 0)
-		    {
-			ch = ' ';
-			break;
-		    }
-		    fileBufferPtr = (uchar *)fileBuffer.data();
-		    fileBufferEnd = (fileBufferPtr + n);
-		}
-		ch = *fileBufferPtr++;
+                int n = nextChar();
+                if ( n <= 0 ) {
+                    ch = ' ';
+                    break;
+                }
+                ch = n;
 	    }
 
 	    // Read numeric parameter (param)
 	    bool isneg = (ch == '-');
 
-	    if (isneg)
-	    {
-		if (fileBufferPtr == fileBufferEnd)
-		{
-		    int n = infile->readBlock( fileBuffer.data(), fileBuffer.size() );
-
-		    if (n <= 0)
-		    {
-			// Return CloseGroup on EOF
-			type = RTFTokenizer::CloseGroup;
-			return;
-		    }
-		    fileBufferPtr = (uchar *)fileBuffer.data();
-		    fileBufferEnd = (fileBufferPtr + n);
-		}
-		ch = *fileBufferPtr++;
+	    if (isneg) {
+                int n = nextChar();
+                if ( n <= 0 ) {
+                    type = RTFTokenizer::CloseGroup;
+                    return;
+                }
+		ch = n;
 	    }
-	    while (ch >= '0' && ch <= '9')
-	    {
+
+	    while (ch >= '0' && ch <= '9') {
 		v	 = (10 * v) + ch - '0';
 		hasParam = true;
 
-		if (fileBufferPtr == fileBufferEnd)
-		{
-		    int n = infile->readBlock( fileBuffer.data(), fileBuffer.size() );
+                int n = nextChar();
 
-		    if (n <= 0)
-		    {
-			ch = ' ';
-			break;
-		    }
-		    fileBufferPtr = (uchar *)fileBuffer.data();
-		    fileBufferEnd = (fileBufferPtr + n);
-		}
-		ch = *fileBufferPtr++;
-	    }
+                if ( n <= 0 )
+                    n = ' ';
+                ch = n;
+            }
 	    value = isneg ? -v : v;
 
 	    // If delimiter is a space, it's part of the control word
@@ -163,7 +145,7 @@
 	    }
 
             *_text = 0; // Just put an end of string for the test, it can then be over-written again
-            if ( !qstrncmp( tokenText.data()+1, "bin", 4 ) ) // Test the NULL too to avoid catching keywords starting with "bin"
+            if ( !memcmp( tokenText.data()+1, "bin", 4 ) )
             {   // We have \bin, so we need to read the bytes
                 kdDebug(30515) << "Token:" << tokenText << endl;
                 if (value > 0)
@@ -173,26 +155,15 @@
                     binaryData.resize(value);
                     for (int i=0; i<value; i++)
                     {
-                        if (fileBufferPtr == fileBufferEnd)
-                        {
-                            const int n = infile->readBlock( fileBuffer.data(), fileBuffer.size() );
-
-                            if (n <= 0)
-                            {
-                                kdError(30515) << "\\bin stream hit end of file." << endl;
-                                type = RTFTokenizer::CloseGroup;
-                                break;
-                            }
-                            fileBufferPtr = (uchar *)fileBuffer.data();
-                            fileBufferEnd = (fileBufferPtr + n);
+                        int n = nextChar();
+                        if ( n <= 0 ) {
+                            type = RTFTokenizer::CloseGroup;
+                            break;
                         }
-                        binaryData[i]=*fileBufferPtr++;
+
+                        binaryData[i] = n;
                     }
                 }
-                else
-                {
-                    kdError(30515) << "\\bin with negative value skipping" << endl;
-                }
             }
 
 	}
@@ -200,19 +171,13 @@
 	{
 	    type = RTFTokenizer::ControlWord;
 	    *_text++ = ch;
-	    if (fileBufferPtr == fileBufferEnd)
-	    {
-		int n = infile->readBlock( fileBuffer.data(), fileBuffer.size() );
 
-		if (n <= 0)
-		{
-		    // Return CloseGroup on EOF
-		    type = RTFTokenizer::CloseGroup;
-		    return;
-		}
-		fileBufferPtr = (uchar *)fileBuffer.data();
-		fileBufferEnd = (fileBufferPtr + n);
-	    }
+            int n = nextChar();
+
+            if ( n <= 0 ) {
+                type = RTFTokenizer::CloseGroup;
+                return;
+            }
 	    ch = *fileBufferPtr++;
 	    for(int i=0;i<2;i++)
 	    {
@@ -220,22 +185,16 @@
 		value<<=4;
 		value=value|((ch + ((ch & 16) ? 0 : 9)) & 0xf);
 
-		if (fileBufferPtr == fileBufferEnd)
-		{
-		    int n = infile->readBlock( fileBuffer.data(), fileBuffer.size() );
+                int n = nextChar();
 
-		    if (n <= 0)
-		    {
-			ch = ' ';
-			break;
-		    }
-		    fileBufferPtr = (uchar *)fileBuffer.data();
-		    fileBufferEnd = (fileBufferPtr + n);
-		}
+                if ( n <= 0 ) {
+                    ch = ' ';
+                    break;
+                }
 		ch = *fileBufferPtr++;
 	    }
-		--fileBufferPtr;
-	    }
+            --fileBufferPtr;
+        }
 	else
 	{
 	    type = RTFTokenizer::ControlWord;
@@ -248,14 +207,16 @@
 
 	// Everything until next backslash, opener or closer
 	while ( ch != '\\' && ch != '{' && ch != '}' && ch != '\n' &&
-		ch != '\r' && fileBufferPtr <= fileBufferEnd )
+		ch != '\r')
 	{
 	    *_text++ = ch;
+            if(fileBufferPtr >= fileBufferEnd)
+                break;
 	    ch = *fileBufferPtr++;
 	}
-
-	// Give back last char
-	--fileBufferPtr;
+        if(fileBufferPtr < fileBufferEnd)
+          --fileBufferPtr; // give back the last char
     }
     *_text++ = 0;
+
 }
Index: filters/kword/rtf/import/rtfimport_tokenizer.h
===================================================================
--- filters/kword/rtf/import/rtfimport_tokenizer.h	(revision 463235)
+++ filters/kword/rtf/import/rtfimport_tokenizer.h	(working copy)
@@ -50,6 +50,8 @@
 
     // tokenizer (private) data
 private:
+    int nextChar();
+
     QFile *infile;
     QByteArray fileBuffer;
     QCString tokenText;