~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/pbms/src/cslib/CSXML.cc

  • Committer: Monty Taylor
  • Date: 2010-07-06 00:44:32 UTC
  • mfrom: (1643.1.13 build)
  • Revision ID: mordred@inaugust.com-20100706004432-843uftc92rc2497l
Merged in PBMS, translation updates, a few build fixes and a few bug fixes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2010 PrimeBase Technologies GmbH, Germany
 
2
 *
 
3
 * PrimeBase Media Stream for MySQL
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; either version 2 of the License, or
 
8
 * (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
18
 *
 
19
 * Paul McCullagh (H&G2JCtL)
 
20
 *
 
21
 * 2010-01-12
 
22
 *
 
23
 * CORE SYSTEM:
 
24
 * XML Parsing
 
25
 *
 
26
 */
 
27
 
 
28
#include "CSConfig.h"
 
29
#include <inttypes.h>
 
30
 
 
31
 
 
32
#include <string.h>
 
33
#include <stdlib.h>
 
34
#include <ctype.h>
 
35
#include <stdio.h>
 
36
#include <errno.h>
 
37
 
 
38
#include <boost/algorithm/string.hpp>
 
39
 
 
40
#include "CSXML.h"
 
41
 
 
42
#define ISSPACE(ch)                     (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r')
 
43
#define ISSINGLE(ch)            (ch == '*' || ch == '+' || ch == '(' || ch == ')' || ch == ',' || ch == '|' || ch == '[' || ch == ']' || ch == '?' || ch == '/')
 
44
 
 
45
#define SET_CHAR(x, ch)         { x->buffer[0] = ch; x->count = 1; }
 
46
#define ADD_CHAR(x, ch)         { if (x->count < PARSE_BUFFER_SIZE) { x->buffer[x->count] = ch; x->count++; } else x->buffer[PARSE_BUFFER_SIZE-1] = ch; }
 
47
 
 
48
bool CSXMLParser::match_string(const char *ch)
 
49
{
 
50
        int32_t i;
 
51
        
 
52
        for (i=0; i<this->count; i++) {
 
53
                if (this->buffer[i] != *ch)
 
54
                        return false;
 
55
                ch++;
 
56
        }
 
57
        if (*ch)
 
58
                return false;
 
59
        return(i == this->count);
 
60
}
 
61
 
 
62
void CSXMLParser::increment_nesting(wchar_t ch)
 
63
{
 
64
        if (this->nesting < PARSE_STACK_SIZE) {
 
65
                switch (ch) {
 
66
                        case '/':
 
67
                                this->end_type[this->nesting] = XML_OP_1_END_CLOSE_TAG;
 
68
                                break;
 
69
                        case '?':
 
70
                                this->end_type[this->nesting] = XML_OP_1_END_PI_TAG;
 
71
                                break;
 
72
                        case '!':
 
73
                                this->end_type[this->nesting] = XML_OP_1_END_ENTITY_TAG;
 
74
                                break;
 
75
                        case '[':
 
76
                                this->end_type[this->nesting] = XML_OP_1_END_BRACKET_TAG;
 
77
                                break;
 
78
                        default:
 
79
                                if (ISSPACE(ch))
 
80
                                        this->end_type[this->nesting] = XML_OP_1_END_UNKNOWN_TAG;
 
81
                                else
 
82
                                        this->end_type[this->nesting] = XML_OP_1_END_TAG;
 
83
                                break;
 
84
                }
 
85
        }
 
86
        this->nesting++;
 
87
}
 
88
 
 
89
int32_t CSXMLParser::parseChar(wchar_t ch)
 
90
/* This function does the actual work of parsing. It is expects 
 
91
 * "complete" characters as input. This could be 4 byte characters
 
92
 * as long as it is able to recognize the characters that are
 
93
 * relevant to parsing.
 
94
 * The function outputs processing instructions, and indicates
 
95
 * how the output data is to be understood.
 
96
 */
 
97
{
 
98
        switch (this->state) {
 
99
                case XML_BEFORE_CDATA:
 
100
                        this->nesting = 0;
 
101
                        /* This is the initial state! */
 
102
                        if (ch == '<') {
 
103
                                this->state = XML_LT;
 
104
                                this->type = XML_noop;
 
105
                        }
 
106
                        else {
 
107
                                this->state = XML_IN_CDATA;
 
108
                                this->type = XML_CDATA_CH;
 
109
                        }
 
110
                        SET_CHAR(this, ch);
 
111
                        break;
 
112
                case XML_IN_CDATA:
 
113
                        if (ch == '<') {
 
114
                                this->state = XML_LT;
 
115
                                this->type = XML_noop;
 
116
                        }
 
117
                        else
 
118
                                this->type = XML_CDATA_CH;
 
119
                        SET_CHAR(this, ch);
 
120
                        break;
 
121
                case XML_LT:
 
122
                        if (ISSPACE(ch)) {
 
123
                                if (this->nesting) {
 
124
                                        this->state = XML_BEFORE_ATTR;
 
125
                                        if (this->step == XML_STEP_TAG)
 
126
                                                this->type = XML_start_tag_TAG_CH;
 
127
                                        else if (this->step == XML_STEP_NESTED)
 
128
                                                this->type = XML_TAG_CH;
 
129
                                        else if (this->step == XML_STEP_NONE)
 
130
                                                this->type = XML_end_cdata_TAG_CH;
 
131
                                        else
 
132
                                                this->type = XML_add_attr_TAG_CH;
 
133
                                        this->step = XML_STEP_TAG;
 
134
                                        increment_nesting(ch);
 
135
                                        this->count = 0;
 
136
                                }
 
137
                                else {
 
138
                                        this->state = XML_IN_CDATA;
 
139
                                        this->type = XML_CDATA_CH;
 
140
                                        ADD_CHAR(this, ch);
 
141
                                }
 
142
                        }
 
143
                        else if (ch == '!') {
 
144
                                this->state = XML_LT_BANG;
 
145
                                this->type = XML_noop;
 
146
                                ADD_CHAR(this, ch);
 
147
                        }
 
148
                        else {
 
149
                                this->state = XML_IN_TAG_NAME;
 
150
                                if (this->step == XML_STEP_TAG)
 
151
                                        this->type = XML_start_tag_TAG_CH;
 
152
                                else if (this->step == XML_STEP_NESTED)
 
153
                                        this->type = XML_TAG_CH;
 
154
                                else if (this->step == XML_STEP_NONE)
 
155
                                        this->type = XML_end_cdata_TAG_CH;
 
156
                                else
 
157
                                        this->type = XML_add_attr_TAG_CH;
 
158
                                this->step = XML_STEP_TAG;
 
159
                                increment_nesting(ch);
 
160
                                SET_CHAR(this, ch);
 
161
                        }
 
162
                        break;
 
163
                case XML_LT_BANG:
 
164
                        if (ch == '-') {
 
165
                                this->state = XML_LT_BANG_DASH;
 
166
                                this->type = XML_noop;
 
167
                        }
 
168
                        else if (ch == '[') {
 
169
                                this->state = XML_LT_BANG_SQR;
 
170
                                this->type = XML_noop;
 
171
                        }
 
172
                        else {
 
173
                                this->state = XML_IN_TAG_NAME;
 
174
                                if (this->step == XML_STEP_TAG)
 
175
                                        this->type = XML_start_tag_TAG_CH;
 
176
                                else if (this->step == XML_STEP_NESTED)
 
177
                                        this->type = XML_TAG_CH;
 
178
                                else if (this->step == XML_STEP_NONE)
 
179
                                        this->type = XML_end_cdata_TAG_CH;
 
180
                                else
 
181
                                        this->type = XML_add_attr_TAG_CH;
 
182
                                this->step = XML_STEP_TAG;
 
183
                                increment_nesting('!');
 
184
                                SET_CHAR(this, '!');
 
185
                        }
 
186
                        ADD_CHAR(this, ch);
 
187
                        break;
 
188
                case XML_LT_BANG_DASH:
 
189
                        if (ch == '-') {
 
190
                                this->state = XML_IN_COMMENT;
 
191
                                if (this->step == XML_STEP_TAG)
 
192
                                        this->type = XML_start_tag_start_comment;
 
193
                                else if (this->step == XML_STEP_NESTED)
 
194
                                        this->type = XML_start_comment;
 
195
                                else if (this->step == XML_STEP_NONE)
 
196
                                        this->type = XML_end_cdata_start_comment;
 
197
                                else
 
198
                                        this->type = XML_add_attr_start_comment;
 
199
                                increment_nesting(' ');
 
200
                        }
 
201
                        else {
 
202
                                this->state = XML_IN_CDATA;
 
203
                                this->type = XML_CDATA_CH;
 
204
                                ADD_CHAR(this, ch);
 
205
                        }
 
206
                        break;
 
207
                case XML_LT_BANG_SQR:
 
208
                        if (ISSPACE(ch))
 
209
                                this->type = XML_noop;
 
210
                        else if (ch == '[') {
 
211
                                this->state = XML_BEFORE_ATTR;
 
212
                                if (this->step == XML_STEP_TAG)
 
213
                                        this->type = XML_start_tag_TAG_CH;
 
214
                                else if (this->step == XML_STEP_NESTED)
 
215
                                        this->type = XML_TAG_CH;
 
216
                                else if (this->step == XML_STEP_NONE)
 
217
                                        this->type = XML_end_cdata_TAG_CH;
 
218
                                else
 
219
                                        this->type = XML_add_attr_TAG_CH;
 
220
                                this->step = XML_STEP_TAG;
 
221
                                increment_nesting('[');
 
222
                                SET_CHAR(this, '!');
 
223
                                ADD_CHAR(this, '[');
 
224
                        }
 
225
                        else {
 
226
                                this->state = XML_LT_BANG_SQR_IN_NAME;
 
227
                                this->type = XML_noop;
 
228
                                SET_CHAR(this, '!');
 
229
                                ADD_CHAR(this, '[');
 
230
                                ADD_CHAR(this, ch);
 
231
                        }
 
232
                        break;
 
233
                case XML_LT_BANG_SQR_IN_NAME:
 
234
                        if (ISSPACE(ch)) {
 
235
                                this->state = XML_LT_BANG_SQR_AFTER_NAME;
 
236
                                this->type = XML_noop;
 
237
                        }
 
238
                        else if (ch == '[') {
 
239
                                if (match_string("![CDATA")) {
 
240
                                        this->state = XML_IN_CDATA_TAG;
 
241
                                        if (this->step == XML_STEP_TAG)
 
242
                                                this->type = XML_start_tag_start_cdata_tag;
 
243
                                        else if (this->step == XML_STEP_NESTED)
 
244
                                                this->type = XML_start_cdata_tag;
 
245
                                        else if (this->step == XML_STEP_NONE)
 
246
                                                this->type = XML_end_cdata_start_cdata_tag;
 
247
                                        else
 
248
                                                this->type = XML_add_attr_start_cdata_tag;
 
249
                                        this->step = XML_STEP_TAG;
 
250
                                        increment_nesting('[');
 
251
                                }
 
252
                                else {
 
253
                                        this->state = XML_BEFORE_ATTR;
 
254
                                        if (this->step == XML_STEP_TAG)
 
255
                                                this->type = XML_start_tag_TAG_CH;
 
256
                                        else if (this->step == XML_STEP_NESTED)
 
257
                                                this->type = XML_TAG_CH;
 
258
                                        else if (this->step == XML_STEP_NONE)
 
259
                                                this->type = XML_end_cdata_TAG_CH;
 
260
                                        else
 
261
                                                this->type = XML_add_attr_TAG_CH;
 
262
                                        this->step = XML_STEP_TAG;
 
263
                                        increment_nesting('[');
 
264
                                }
 
265
                        }
 
266
                        else {
 
267
                                this->type = XML_noop;
 
268
                                ADD_CHAR(this, ch);
 
269
                        }
 
270
                        break;
 
271
                case XML_LT_BANG_SQR_AFTER_NAME:
 
272
                        if (ch == '[') {
 
273
                                if (match_string("![CDATA")) {
 
274
                                        this->state = XML_IN_CDATA_TAG;
 
275
                                        if (this->step == XML_STEP_TAG)
 
276
                                                this->type = XML_start_tag_start_cdata_tag;
 
277
                                        else if (this->step == XML_STEP_NESTED)
 
278
                                                this->type = XML_start_cdata_tag;
 
279
                                        else if (this->step == XML_STEP_NONE)
 
280
                                                this->type = XML_end_cdata_start_cdata_tag;
 
281
                                        else
 
282
                                                this->type = XML_add_attr_start_cdata_tag;
 
283
                                        increment_nesting('[');
 
284
                                }
 
285
                                else {
 
286
                                        this->state = XML_BEFORE_ATTR;
 
287
                                        if (this->step == XML_STEP_TAG)
 
288
                                                this->type = XML_start_tag_TAG_CH;
 
289
                                        else if (this->step == XML_STEP_NESTED)
 
290
                                                this->type = XML_TAG_CH;
 
291
                                        else if (this->step == XML_STEP_NONE)
 
292
                                                this->type = XML_end_cdata_TAG_CH;
 
293
                                        else
 
294
                                                this->type = XML_add_attr_TAG_CH;
 
295
                                        this->step = XML_STEP_TAG;
 
296
                                        increment_nesting('[');
 
297
                                }
 
298
                        }
 
299
                        else
 
300
                                /* Ignore data until the '['!!! */
 
301
                                this->type = XML_noop;
 
302
                        break;
 
303
                case XML_IN_TAG_NAME:
 
304
                        if (ISSPACE(ch)) {
 
305
                                this->state = XML_BEFORE_ATTR;
 
306
                                this->type = XML_noop;
 
307
                        }
 
308
                        else if (ch == '<') {
 
309
                                this->state = XML_LT;
 
310
                                this->type = XML_noop;
 
311
                        }
 
312
                        else if (ch == '>') {
 
313
                                if (this->step == XML_STEP_TAG)
 
314
                                        this->type = XML_start_tag_end_tag(END_TAG_TYPE(this));
 
315
                                else if (this->step == XML_STEP_NESTED)
 
316
                                        this->type = XML_end_tag(END_TAG_TYPE(this));
 
317
                                else
 
318
                                        this->type = XML_add_attr_end_tag(END_TAG_TYPE(this));
 
319
                                this->nesting--;
 
320
                                if (this->nesting) {
 
321
                                        this->step = XML_STEP_NESTED;
 
322
                                        this->state = XML_BEFORE_ATTR;
 
323
                                }
 
324
                                else {
 
325
                                        this->step = XML_STEP_NONE;
 
326
                                        this->state = XML_IN_CDATA;
 
327
                                }
 
328
                        }
 
329
                        else if (ch == '"' || ch == '\'') {
 
330
                                this->state = XML_QUOTE_BEFORE_VALUE;
 
331
                                this->quote = ch;
 
332
                                this->type = XML_noop;
 
333
                        }
 
334
                        else if (ch == '/' && (END_TAG_TYPE(this) == XML_OP_1_END_TAG)) {
 
335
                                this->state = XML_SLASH;
 
336
                                this->type = XML_noop;
 
337
                        }
 
338
                        else if (ch == '?' && (END_TAG_TYPE(this) == XML_OP_1_END_PI_TAG)) {
 
339
                                this->state = XML_QMARK;
 
340
                                this->type = XML_noop;
 
341
                        }
 
342
                        else if (ch == ']' && (END_TAG_TYPE(this) == XML_OP_1_END_BRACKET_TAG)) {
 
343
                                this->state = XML_SQR;
 
344
                                this->type = XML_noop;
 
345
                        }
 
346
                        else if (ISSINGLE(ch)) {
 
347
                                this->state = XML_BEFORE_ATTR;
 
348
                                if (this->step == XML_STEP_TAG)
 
349
                                        this->type = XML_start_tag_ATTR_CH;
 
350
                                else if (this->step == XML_STEP_NESTED)
 
351
                                        this->type = XML_ATTR_CH;
 
352
                                else
 
353
                                        this->type = XML_add_attr_ATTR_CH;
 
354
                                this->step = XML_STEP_ATTR;
 
355
                                SET_CHAR(this, ch);
 
356
                        }
 
357
                        else {
 
358
                                this->type = XML_TAG_CH;
 
359
                                SET_CHAR(this, ch);
 
360
                        }
 
361
                        break;
 
362
                case XML_BEFORE_ATTR:
 
363
                        if (ISSPACE(ch))
 
364
                                this->type = XML_noop;
 
365
                        else if (ch == '<') {
 
366
                                this->state = XML_LT;
 
367
                                this->type = XML_noop;
 
368
                        }
 
369
                        else if (ch == '>') {
 
370
                                if (this->step == XML_STEP_TAG)
 
371
                                        this->type = XML_start_tag_end_tag(END_TAG_TYPE(this));
 
372
                                else if (this->step == XML_STEP_NESTED)
 
373
                                        this->type = XML_end_tag(END_TAG_TYPE(this));
 
374
                                else
 
375
                                        this->type = XML_add_attr_end_tag(END_TAG_TYPE(this));
 
376
                                this->nesting--;
 
377
                                if (this->nesting) {
 
378
                                        this->step = XML_STEP_NESTED;
 
379
                                        this->state = XML_BEFORE_ATTR;
 
380
                                }
 
381
                                else {
 
382
                                        this->step = XML_STEP_NONE;
 
383
                                        this->state = XML_IN_CDATA;
 
384
                                }
 
385
                        }
 
386
                        else if (ch == '"' || ch == '\'') {
 
387
                                this->state = XML_QUOTE_BEFORE_VALUE;
 
388
                                this->quote = ch;
 
389
                                this->type = XML_noop;
 
390
                        }
 
391
                        else if (ch == '/' && (END_TAG_TYPE(this) == XML_OP_1_END_TAG)) {
 
392
                                this->state = XML_SLASH;
 
393
                                this->type = XML_noop;
 
394
                        }
 
395
                        else if (ch == '?' && (END_TAG_TYPE(this) == XML_OP_1_END_PI_TAG)) {
 
396
                                this->state = XML_QMARK;
 
397
                                this->type = XML_noop;
 
398
                        }
 
399
                        else if (ch == ']' && (END_TAG_TYPE(this) == XML_OP_1_END_BRACKET_TAG)) {
 
400
                                this->state = XML_SQR;
 
401
                                this->type = XML_noop;
 
402
                        }
 
403
                        else if (ISSINGLE(ch)) {
 
404
                                if (this->step == XML_STEP_TAG)
 
405
                                        this->type = XML_start_tag_ATTR_CH;
 
406
                                else if (this->step == XML_STEP_NESTED)
 
407
                                        this->type = XML_ATTR_CH;
 
408
                                else
 
409
                                        this->type = XML_add_attr_ATTR_CH;
 
410
                                this->step = XML_STEP_ATTR;
 
411
                                SET_CHAR(this, ch);
 
412
                        }
 
413
                        else {
 
414
                                this->state = XML_IN_ATTR;
 
415
                                if (this->step == XML_STEP_TAG)
 
416
                                        this->type = XML_start_tag_ATTR_CH;
 
417
                                else if (this->step == XML_STEP_NESTED)
 
418
                                        this->type = XML_ATTR_CH;
 
419
                                else
 
420
                                        this->type = XML_add_attr_ATTR_CH;
 
421
                                this->step = XML_STEP_ATTR;
 
422
                                SET_CHAR(this, ch);
 
423
                        }
 
424
                        break;
 
425
                case XML_IN_ATTR:
 
426
                        if (ISSPACE(ch)) {
 
427
                                this->state = XML_BEFORE_EQUAL;
 
428
                                this->type = XML_noop;
 
429
                        }
 
430
                        else if (ch == '<') {
 
431
                                this->state = XML_LT;
 
432
                                this->type = XML_noop;
 
433
                        }
 
434
                        else if (ch == '>') {
 
435
                                if (this->step == XML_STEP_TAG)
 
436
                                        this->type = XML_start_tag_end_tag(END_TAG_TYPE(this));
 
437
                                else if (this->step == XML_STEP_NESTED)
 
438
                                        this->type = XML_end_tag(END_TAG_TYPE(this));
 
439
                                else
 
440
                                        this->type = XML_add_attr_end_tag(END_TAG_TYPE(this));
 
441
                                this->nesting--;
 
442
                                if (this->nesting) {
 
443
                                        this->step = XML_STEP_NESTED;
 
444
                                        this->state = XML_BEFORE_ATTR;
 
445
                                }
 
446
                                else {
 
447
                                        this->step = XML_STEP_NONE;
 
448
                                        this->state = XML_IN_CDATA;
 
449
                                }
 
450
                        }
 
451
                        else if (ch == '"' || ch == '\'') {
 
452
                                this->state = XML_QUOTE_BEFORE_VALUE;
 
453
                                this->quote = ch;
 
454
                                this->type = XML_noop;
 
455
                        }
 
456
                        else if (ch == '/' && (END_TAG_TYPE(this) == XML_OP_1_END_TAG)) {
 
457
                                this->state = XML_SLASH;
 
458
                                this->type = XML_noop;
 
459
                        }
 
460
                        else if (ch == '?' && (END_TAG_TYPE(this) == XML_OP_1_END_PI_TAG)) {
 
461
                                this->state = XML_QMARK;
 
462
                                this->type = XML_noop;
 
463
                        }
 
464
                        else if (ch == ']' && (END_TAG_TYPE(this) == XML_OP_1_END_BRACKET_TAG)) {
 
465
                                this->state = XML_SQR;
 
466
                                this->type = XML_noop;
 
467
                        }
 
468
                        else if (ISSINGLE(ch)) {
 
469
                                this->state = XML_BEFORE_ATTR;
 
470
                                if (this->step == XML_STEP_TAG)
 
471
                                        this->type = XML_start_tag_ATTR_CH;
 
472
                                else if (this->step == XML_STEP_NESTED)
 
473
                                        this->type = XML_ATTR_CH;
 
474
                                else
 
475
                                        this->type = XML_add_attr_ATTR_CH;
 
476
                                this->step = XML_STEP_ATTR;
 
477
                                SET_CHAR(this, ch);
 
478
                        }
 
479
                        else if (ch == '=') {
 
480
                                this->state = XML_AFTER_EQUAL;
 
481
                                this->type = XML_noop;
 
482
                        }
 
483
                        else {
 
484
                                this->type = XML_ATTR_CH;
 
485
                                SET_CHAR(this, ch);
 
486
                        }
 
487
                        break;
 
488
                case XML_BEFORE_EQUAL:
 
489
                        if (ISSPACE(ch))
 
490
                                this->type = XML_noop;
 
491
                        else if (ch == '<') {
 
492
                                this->state = XML_LT;
 
493
                                this->type = XML_noop;
 
494
                        }
 
495
                        else if (ch == '>') {
 
496
                                if (this->step == XML_STEP_TAG)
 
497
                                        this->type = XML_start_tag_end_tag(END_TAG_TYPE(this));
 
498
                                else if (this->step == XML_STEP_NESTED)
 
499
                                        this->type = XML_end_tag(END_TAG_TYPE(this));
 
500
                                else
 
501
                                        this->type = XML_add_attr_end_tag(END_TAG_TYPE(this));
 
502
                                this->nesting--;
 
503
                                if (this->nesting) {
 
504
                                        this->step = XML_STEP_NESTED;
 
505
                                        this->state = XML_BEFORE_ATTR;
 
506
                                }
 
507
                                else {
 
508
                                        this->step = XML_STEP_NONE;
 
509
                                        this->state = XML_IN_CDATA;
 
510
                                }
 
511
                        }
 
512
                        else if (ch == '"' || ch == '\'') {
 
513
                                this->state = XML_QUOTE_BEFORE_VALUE;
 
514
                                this->quote = ch;
 
515
                                this->type = XML_noop;
 
516
                        }
 
517
                        else if (ch == '/' && (END_TAG_TYPE(this) == XML_OP_1_END_TAG)) {
 
518
                                this->state = XML_SLASH;
 
519
                                this->type = XML_noop;
 
520
                        }
 
521
                        else if (ch == '?' && (END_TAG_TYPE(this) == XML_OP_1_END_PI_TAG)) {
 
522
                                this->state = XML_QMARK;
 
523
                                this->type = XML_noop;
 
524
                        }
 
525
                        else if (ch == ']' && (END_TAG_TYPE(this) == XML_OP_1_END_BRACKET_TAG)) {
 
526
                                this->state = XML_SQR;
 
527
                                this->type = XML_noop;
 
528
                        }
 
529
                        else if (ISSINGLE(ch)) {
 
530
                                this->state = XML_BEFORE_ATTR;
 
531
                                if (this->step == XML_STEP_TAG)
 
532
                                        this->type = XML_start_tag_ATTR_CH;
 
533
                                else if (this->step == XML_STEP_NESTED)
 
534
                                        this->type = XML_ATTR_CH;
 
535
                                else
 
536
                                        this->type = XML_add_attr_ATTR_CH;
 
537
                                this->step = XML_STEP_ATTR;
 
538
                                SET_CHAR(this, ch);
 
539
                        }
 
540
                        else if (ch == '=') {
 
541
                                this->state = XML_AFTER_EQUAL;
 
542
                                this->type = XML_noop;
 
543
                        }
 
544
                        else {
 
545
                                this->state = XML_IN_ATTR;
 
546
                                if (this->step == XML_STEP_TAG)
 
547
                                        this->type = XML_start_tag_ATTR_CH;
 
548
                                else if (this->step == XML_STEP_NESTED)
 
549
                                        this->type = XML_ATTR_CH;
 
550
                                else
 
551
                                        this->type = XML_add_attr_ATTR_CH;
 
552
                                this->step = XML_STEP_ATTR;
 
553
                                SET_CHAR(this, ch);
 
554
                        }
 
555
                        break;
 
556
                case XML_AFTER_EQUAL:
 
557
                        if (ISSPACE(ch)) {
 
558
                                this->state = XML_AFTER_EQUAL;
 
559
                                this->type = XML_noop;
 
560
                        }
 
561
                        else if (ch == '<') {
 
562
                                this->state = XML_LT;
 
563
                                this->type = XML_noop;
 
564
                        }
 
565
                        else if (ch == '>') {
 
566
                                if (this->step == XML_STEP_TAG)
 
567
                                        this->type = XML_start_tag_end_tag(END_TAG_TYPE(this));
 
568
                                else if (this->step == XML_STEP_NESTED)
 
569
                                        this->type = XML_end_tag(END_TAG_TYPE(this));
 
570
                                else
 
571
                                        this->type = XML_add_attr_end_tag(END_TAG_TYPE(this));
 
572
                                this->nesting--;
 
573
                                if (this->nesting) {
 
574
                                        this->step = XML_STEP_NESTED;
 
575
                                        this->state = XML_BEFORE_ATTR;
 
576
                                }
 
577
                                else {
 
578
                                        this->step = XML_STEP_NONE;
 
579
                                        this->state = XML_IN_CDATA;
 
580
                                }
 
581
                        }
 
582
                        else if (ch == '"' || ch == '\'') {
 
583
                                this->state = XML_QUOTE_BEFORE_VALUE;
 
584
                                this->quote = ch;
 
585
                                this->type = XML_noop;
 
586
                        }
 
587
                        else if (ch == '/' && (END_TAG_TYPE(this) == XML_OP_1_END_TAG)) {
 
588
                                this->state = XML_SLASH;
 
589
                                this->type = XML_noop;
 
590
                        }
 
591
                        else if (ch == '?' && (END_TAG_TYPE(this) == XML_OP_1_END_PI_TAG)) {
 
592
                                this->state = XML_QMARK;
 
593
                                this->type = XML_noop;
 
594
                        }
 
595
                        else if (ch == ']' && (END_TAG_TYPE(this) == XML_OP_1_END_BRACKET_TAG)) {
 
596
                                this->state = XML_SQR;
 
597
                                this->type = XML_noop;
 
598
                        }
 
599
                        else if (ISSINGLE(ch)) {
 
600
                                this->state = XML_BEFORE_ATTR;
 
601
                                if (this->step == XML_STEP_TAG)
 
602
                                        this->type = XML_start_tag_ATTR_CH;
 
603
                                else if (this->step == XML_STEP_NESTED)
 
604
                                        this->type = XML_ATTR_CH;
 
605
                                else
 
606
                                        this->type = XML_add_attr_ATTR_CH;
 
607
                                this->step = XML_STEP_ATTR;
 
608
                                SET_CHAR(this, ch);
 
609
                        }
 
610
                        else {
 
611
                                this->state = XML_IN_VALUE;
 
612
                                this->quote = 0;
 
613
                                if (this->step == XML_STEP_TAG)
 
614
                                        this->type = XML_start_tag_VALUE_CH;
 
615
                                else if (this->step == XML_STEP_VALUE)
 
616
                                        this->type = XML_add_attr_VALUE_CH;
 
617
                                else
 
618
                                        this->type = XML_VALUE_CH;
 
619
                                this->step = XML_STEP_VALUE;
 
620
                                SET_CHAR(this, ch);
 
621
                        }
 
622
                        break;
 
623
                case XML_QUOTE_BEFORE_VALUE:
 
624
                        if (ch == this->quote) {
 
625
                                this->state = XML_QUOTE_AFTER_VALUE;
 
626
                                // Empty string:
 
627
                                if (this->step == XML_STEP_TAG)
 
628
                                        this->type = XML_start_tag_VALUE_CH;
 
629
                                else if (this->step == XML_STEP_VALUE)
 
630
                                        this->type = XML_add_attr_VALUE_CH;
 
631
                                else
 
632
                                        this->type = XML_VALUE_CH;
 
633
                                this->step = XML_STEP_VALUE;
 
634
                                this->count = 0;
 
635
                        }
 
636
                        else {
 
637
                                this->state = XML_IN_VALUE;
 
638
                                if (this->step == XML_STEP_TAG)
 
639
                                        this->type = XML_start_tag_VALUE_CH;
 
640
                                else if (this->step == XML_STEP_VALUE)
 
641
                                        this->type = XML_add_attr_VALUE_CH;
 
642
                                else
 
643
                                        this->type = XML_VALUE_CH;
 
644
                                this->step = XML_STEP_VALUE;
 
645
                                SET_CHAR(this, ch);
 
646
                        }
 
647
                        break;
 
648
                case XML_IN_VALUE:
 
649
                        if (this->quote) {
 
650
                                if (ch == this->quote) {
 
651
                                        this->state = XML_QUOTE_AFTER_VALUE;
 
652
                                        this->type = XML_noop;
 
653
                                }
 
654
                                else {
 
655
                                        this->type = XML_VALUE_CH;
 
656
                                        SET_CHAR(this, ch);
 
657
                                }
 
658
                        }
 
659
                        else {
 
660
                                /* A value without quotes (for HTML!) */
 
661
                                if (ISSPACE(ch)) {
 
662
                                        this->state = XML_BEFORE_ATTR;
 
663
                                        this->type = XML_noop;
 
664
                                }
 
665
                                else if (ch == '<') {
 
666
                                        this->state = XML_LT;
 
667
                                        this->type = XML_noop;
 
668
                                }
 
669
                                else if (ch == '>') {
 
670
                                        if (this->step == XML_STEP_TAG)
 
671
                                                this->type = XML_start_tag_end_tag(END_TAG_TYPE(this));
 
672
                                        else if (this->step == XML_STEP_NESTED)
 
673
                                                this->type = XML_end_tag(END_TAG_TYPE(this));
 
674
                                        else
 
675
                                                this->type = XML_add_attr_end_tag(END_TAG_TYPE(this));
 
676
                                        this->nesting--;
 
677
                                        if (this->nesting) {
 
678
                                                this->step = XML_STEP_NESTED;
 
679
                                                this->state = XML_BEFORE_ATTR;
 
680
                                        }
 
681
                                        else {
 
682
                                                this->step = XML_STEP_NONE;
 
683
                                                this->state = XML_IN_CDATA;
 
684
                                        }
 
685
                                }
 
686
                                else if (ch == '"' || ch == '\'') {
 
687
                                        this->state = XML_QUOTE_BEFORE_VALUE;
 
688
                                        this->quote = ch;
 
689
                                        this->type = XML_noop;
 
690
                                }
 
691
                                else {
 
692
                                        this->type = XML_VALUE_CH;
 
693
                                        SET_CHAR(this, ch);
 
694
                                }
 
695
                        }
 
696
                        break;
 
697
                case XML_QUOTE_AFTER_VALUE:
 
698
                        if (ISSPACE(ch)) {
 
699
                                this->state = XML_BEFORE_ATTR;
 
700
                                this->type = XML_noop;
 
701
                        }
 
702
                        else if (ch == '<') {
 
703
                                this->state = XML_LT;
 
704
                                this->type = XML_noop;
 
705
                        }
 
706
                        else if (ch == '>') {
 
707
                                if (this->step == XML_STEP_TAG)
 
708
                                        this->type = XML_start_tag_end_tag(END_TAG_TYPE(this));
 
709
                                else if (this->step == XML_STEP_NESTED)
 
710
                                        this->type = XML_end_tag(END_TAG_TYPE(this));
 
711
                                else
 
712
                                        this->type = XML_add_attr_end_tag(END_TAG_TYPE(this));
 
713
                                this->nesting--;
 
714
                                if (this->nesting) {
 
715
                                        this->step = XML_STEP_NESTED;
 
716
                                        this->state = XML_BEFORE_ATTR;
 
717
                                }
 
718
                                else {
 
719
                                        this->step = XML_STEP_NONE;
 
720
                                        this->state = XML_IN_CDATA;
 
721
                                }
 
722
                        }
 
723
                        else if (ch == '"' || ch == '\'') {
 
724
                                this->state = XML_QUOTE_BEFORE_VALUE;
 
725
                                this->quote = ch;
 
726
                                this->type = XML_noop;
 
727
                        }
 
728
                        else if (ch == '/' && (END_TAG_TYPE(this) == XML_OP_1_END_TAG)) {
 
729
                                this->state = XML_SLASH;
 
730
                                this->type = XML_noop;
 
731
                        }
 
732
                        else if (ch == '?' && (END_TAG_TYPE(this) == XML_OP_1_END_PI_TAG)) {
 
733
                                this->state = XML_QMARK;
 
734
                                this->type = XML_noop;
 
735
                        }
 
736
                        else if (ch == ']' && (END_TAG_TYPE(this) == XML_OP_1_END_BRACKET_TAG)) {
 
737
                                this->state = XML_SQR;
 
738
                                this->type = XML_noop;
 
739
                        }
 
740
                        else if (ISSINGLE(ch)) {
 
741
                                this->state = XML_BEFORE_ATTR;
 
742
                                if (this->step == XML_STEP_TAG)
 
743
                                        this->type = XML_start_tag_ATTR_CH;
 
744
                                else if (this->step == XML_STEP_NESTED)
 
745
                                        this->type = XML_ATTR_CH;
 
746
                                else
 
747
                                        this->type = XML_add_attr_ATTR_CH;
 
748
                                this->step = XML_STEP_ATTR;
 
749
                                SET_CHAR(this, ch);
 
750
                        }
 
751
                        else {
 
752
                                this->state = XML_IN_ATTR;
 
753
                                if (this->step == XML_STEP_TAG)
 
754
                                        this->type = XML_start_tag_ATTR_CH;
 
755
                                else if (this->step == XML_STEP_NESTED)
 
756
                                        this->type = XML_ATTR_CH;
 
757
                                else
 
758
                                        this->type = XML_add_attr_ATTR_CH;
 
759
                                this->step = XML_STEP_ATTR;
 
760
                                SET_CHAR(this, ch);
 
761
                        }
 
762
                        break;
 
763
                case XML_SQR:
 
764
                        SET_CHAR(this, ']');
 
765
                        goto cont;
 
766
                case XML_SLASH:
 
767
                        SET_CHAR(this, '/');
 
768
                        goto cont;
 
769
                case XML_QMARK:
 
770
                        SET_CHAR(this, '?');
 
771
                        cont:
 
772
                        if (ISSPACE(ch)) {
 
773
                                this->state = XML_BEFORE_ATTR;
 
774
                                if (this->step == XML_STEP_TAG)
 
775
                                        this->type = XML_start_tag_TAG_CH;
 
776
                                else if (this->step == XML_STEP_NESTED)
 
777
                                        this->type = XML_TAG_CH;
 
778
                                else if (this->step == XML_STEP_NONE)
 
779
                                        this->type = XML_end_cdata_TAG_CH;
 
780
                                else
 
781
                                        this->type = XML_add_attr_TAG_CH;
 
782
                                this->step = XML_STEP_ATTR;
 
783
                        }
 
784
                        else if (ch == '<') {
 
785
                                this->state = XML_LT;
 
786
                                if (this->step == XML_STEP_TAG)
 
787
                                        this->type = XML_start_tag_TAG_CH;
 
788
                                else if (this->step == XML_STEP_NESTED)
 
789
                                        this->type = XML_TAG_CH;
 
790
                                else if (this->step == XML_STEP_NONE)
 
791
                                        this->type = XML_end_cdata_TAG_CH;
 
792
                                else
 
793
                                        this->type = XML_add_attr_TAG_CH;
 
794
                                this->step = XML_STEP_TAG;
 
795
                        }
 
796
                        else if (ch == '>') {
 
797
                                if (this->state == XML_SLASH) {
 
798
                                        if (this->step == XML_STEP_TAG)
 
799
                                                this->type = XML_start_tag_end_empty_tag;
 
800
                                        else if (this->step == XML_STEP_NESTED)
 
801
                                                this->type = XML_end_empty_tag;
 
802
                                        else
 
803
                                                this->type = XML_add_attr_end_empty_tag;
 
804
                                }
 
805
                                else if (this->state == XML_SQR) {
 
806
                                        if (this->step == XML_STEP_TAG)
 
807
                                                this->type = XML_start_tag_end_tag(XML_OP_1_END_BRACKET_TAG);
 
808
                                        else if (this->step == XML_STEP_NESTED)
 
809
                                                this->type = XML_end_tag(XML_OP_1_END_BRACKET_TAG);
 
810
                                        else
 
811
                                                this->type = XML_add_attr_end_tag(XML_OP_1_END_BRACKET_TAG);
 
812
                                }
 
813
                                else {
 
814
                                        if (this->step == XML_STEP_TAG)
 
815
                                                this->type = XML_start_tag_end_pi_tag;
 
816
                                        else if (this->step == XML_STEP_NESTED)
 
817
                                                this->type = XML_end_pi_tag;
 
818
                                        else
 
819
                                                this->type = XML_add_attr_end_pi_tag;
 
820
                                }
 
821
                                this->nesting--;
 
822
                                if (this->nesting) {
 
823
                                        this->step = XML_STEP_NESTED;
 
824
                                        this->state = XML_BEFORE_ATTR;
 
825
                                }
 
826
                                else {
 
827
                                        this->step = XML_STEP_NONE;
 
828
                                        this->state = XML_IN_CDATA;
 
829
                                }
 
830
                        }
 
831
                        else if (ch == '"' || ch == '\'') {
 
832
                                this->state = XML_QUOTE_BEFORE_VALUE;
 
833
                                this->quote = ch;
 
834
                                if (this->step == XML_STEP_TAG)
 
835
                                        this->type = XML_start_tag_TAG_CH;
 
836
                                else if (this->step == XML_STEP_NESTED)
 
837
                                        this->type = XML_TAG_CH;
 
838
                                else if (this->step == XML_STEP_NONE)
 
839
                                        this->type = XML_end_cdata_TAG_CH;
 
840
                                else
 
841
                                        this->type = XML_add_attr_TAG_CH;
 
842
                                this->step = XML_STEP_ATTR;
 
843
                        }
 
844
                        else if (ch == '/' && (END_TAG_TYPE(this) == XML_OP_1_END_TAG)) {
 
845
                                this->state = XML_SLASH;
 
846
                                if (this->step == XML_STEP_TAG)
 
847
                                        this->type = XML_start_tag_TAG_CH;
 
848
                                else if (this->step == XML_STEP_NESTED)
 
849
                                        this->type = XML_TAG_CH;
 
850
                                else if (this->step == XML_STEP_NONE)
 
851
                                        this->type = XML_end_cdata_TAG_CH;
 
852
                                else
 
853
                                        this->type = XML_add_attr_TAG_CH;
 
854
                                this->step = XML_STEP_ATTR;
 
855
                        }
 
856
                        else if (ch == '?' && (END_TAG_TYPE(this) == XML_OP_1_END_PI_TAG)) {
 
857
                                this->state = XML_QMARK;
 
858
                                if (this->step == XML_STEP_TAG)
 
859
                                        this->type = XML_start_tag_TAG_CH;
 
860
                                else if (this->step == XML_STEP_NESTED)
 
861
                                        this->type = XML_TAG_CH;
 
862
                                else if (this->step == XML_STEP_NONE)
 
863
                                        this->type = XML_end_cdata_TAG_CH;
 
864
                                else
 
865
                                        this->type = XML_add_attr_TAG_CH;
 
866
                                this->step = XML_STEP_ATTR;
 
867
                        }
 
868
                        else if (ch == ']' && (END_TAG_TYPE(this) == XML_OP_1_END_BRACKET_TAG)) {
 
869
                                this->state = XML_SQR;
 
870
                                if (this->step == XML_STEP_TAG)
 
871
                                        this->type = XML_start_tag_TAG_CH;
 
872
                                else if (this->step == XML_STEP_NESTED)
 
873
                                        this->type = XML_TAG_CH;
 
874
                                else if (this->step == XML_STEP_NONE)
 
875
                                        this->type = XML_end_cdata_TAG_CH;
 
876
                                else
 
877
                                        this->type = XML_add_attr_TAG_CH;
 
878
                                this->step = XML_STEP_ATTR;
 
879
                        }
 
880
                        else if (ISSINGLE(ch)) {
 
881
                                this->state = XML_BEFORE_ATTR;
 
882
                                if (this->step == XML_STEP_TAG)
 
883
                                        this->type = XML_start_tag_TAG_CH;
 
884
                                else if (this->step == XML_STEP_NESTED)
 
885
                                        this->type = XML_TAG_CH;
 
886
                                else if (this->step == XML_STEP_NONE)
 
887
                                        this->type = XML_end_cdata_TAG_CH;
 
888
                                else
 
889
                                        this->type = XML_add_attr_TAG_CH;
 
890
                                this->step = XML_STEP_ATTR;
 
891
                                ADD_CHAR(this, ch);
 
892
                        }
 
893
                        else {
 
894
                                this->state = XML_IN_ATTR;
 
895
                                if (this->step == XML_STEP_TAG)
 
896
                                        this->type = XML_start_tag_TAG_CH;
 
897
                                else if (this->step == XML_STEP_NESTED)
 
898
                                        this->type = XML_TAG_CH;
 
899
                                else if (this->step == XML_STEP_NONE)
 
900
                                        this->type = XML_end_cdata_TAG_CH;
 
901
                                else
 
902
                                        this->type = XML_add_attr_TAG_CH;
 
903
                                this->step = XML_STEP_ATTR;
 
904
                                ADD_CHAR(this, ch);
 
905
                        }
 
906
                        break;
 
907
                case XML_IN_COMMENT:
 
908
                        if (ch == '-') {
 
909
                                this->state = XML_IN_COMMENT_DASH;
 
910
                                this->type = XML_noop;
 
911
                        }
 
912
                        else
 
913
                                this->type = XML_COMMENT_CH;
 
914
                        SET_CHAR(this, ch);
 
915
                        break;
 
916
                case XML_IN_COMMENT_DASH:
 
917
                        if (ch == '-') {
 
918
                                this->state = XML_IN_COMMENT_DASH_DASH;
 
919
                                this->type = XML_noop;
 
920
                        }
 
921
                        else {
 
922
                                this->state = XML_IN_COMMENT;
 
923
                                this->type = XML_COMMENT_CH;
 
924
                        }
 
925
                        ADD_CHAR(this, ch);
 
926
                        break;
 
927
                case XML_IN_COMMENT_DASH_DASH:
 
928
                        if (ch == '-') {
 
929
                                this->state = XML_IN_COMMENT_3_DASH;
 
930
                                this->type = XML_COMMENT_CH;
 
931
                                SET_CHAR(this, ch);
 
932
                        }
 
933
                        else if (ch == '>') {
 
934
                                this->type = XML_end_comment;
 
935
                                this->nesting--;
 
936
                                if (this->nesting) {
 
937
                                        this->step = XML_STEP_NESTED;
 
938
                                        this->state = XML_BEFORE_ATTR;
 
939
                                }
 
940
                                else {
 
941
                                        this->step = XML_STEP_NONE;
 
942
                                        this->state = XML_IN_CDATA;
 
943
                                }
 
944
                        }
 
945
                        else {
 
946
                                this->state = XML_IN_COMMENT;
 
947
                                this->type = XML_COMMENT_CH;
 
948
                                ADD_CHAR(this, ch);
 
949
                        }
 
950
                        break;
 
951
                case XML_IN_COMMENT_3_DASH:
 
952
                        if (ch == '-') {
 
953
                                this->type = XML_COMMENT_CH;
 
954
                                SET_CHAR(this, ch);
 
955
                        }
 
956
                        else if (ch == '>') {
 
957
                                this->type = XML_end_comment;
 
958
                                this->nesting--;
 
959
                                if (this->nesting) {
 
960
                                        this->step = XML_STEP_NESTED;
 
961
                                        this->state = XML_BEFORE_ATTR;
 
962
                                }
 
963
                                else {
 
964
                                        this->step = XML_STEP_NONE;
 
965
                                        this->state = XML_IN_CDATA;
 
966
                                }
 
967
                        }
 
968
                        else {
 
969
                                this->state = XML_IN_COMMENT;
 
970
                                this->type = XML_COMMENT_CH;
 
971
                                SET_CHAR(this, '-');
 
972
                                ADD_CHAR(this, '-');
 
973
                                ADD_CHAR(this, ch);
 
974
                        }
 
975
                        break;
 
976
                case XML_IN_CDATA_TAG:
 
977
                        if (ch == ']') {
 
978
                                this->state = XML_IN_CDATA_TAG_SQR;
 
979
                                this->type = XML_noop;
 
980
                        }
 
981
                        else
 
982
                                this->type = XML_CDATA_TAG_CH;
 
983
                        SET_CHAR(this, ch);
 
984
                        break;
 
985
                case XML_IN_CDATA_TAG_SQR:
 
986
                        if (ch == ']') {
 
987
                                this->state = XML_IN_CDATA_TAG_SQR_SQR;
 
988
                                this->type = XML_noop;
 
989
                        }
 
990
                        else {
 
991
                                this->state = XML_IN_CDATA_TAG;
 
992
                                this->type = XML_CDATA_TAG_CH;
 
993
                        }
 
994
                        ADD_CHAR(this, ch);
 
995
                        break;
 
996
                case XML_IN_CDATA_TAG_SQR_SQR:
 
997
                        if (ch == ']') {
 
998
                                this->state = XML_IN_CDATA_TAG_3_SQR;
 
999
                                this->type = XML_CDATA_TAG_CH;
 
1000
                                SET_CHAR(this, ch);
 
1001
                        }
 
1002
                        else if (ch == '>') {
 
1003
                                this->type = XML_end_cdata_tag;
 
1004
                                this->nesting--;
 
1005
                                if (this->nesting) {
 
1006
                                        this->step = XML_STEP_NESTED;
 
1007
                                        this->state = XML_BEFORE_ATTR;
 
1008
                                }
 
1009
                                else {
 
1010
                                        this->step = XML_STEP_NONE;
 
1011
                                        this->state = XML_IN_CDATA;
 
1012
                                }
 
1013
                        }
 
1014
                        else {
 
1015
                                this->state = XML_IN_CDATA_TAG;
 
1016
                                this->type = XML_CDATA_TAG_CH;
 
1017
                                ADD_CHAR(this, ch);
 
1018
                        }
 
1019
                        break;
 
1020
                case XML_IN_CDATA_TAG_3_SQR:
 
1021
                        if (ch == ']') {
 
1022
                                this->type = XML_CDATA_TAG_CH;
 
1023
                                SET_CHAR(this, ch);
 
1024
                        }
 
1025
                        else if (ch == '>') {
 
1026
                                this->type = XML_end_cdata_tag;
 
1027
                                this->nesting--;
 
1028
                                if (this->nesting) {
 
1029
                                        this->step = XML_STEP_NESTED;
 
1030
                                        this->state = XML_BEFORE_ATTR;
 
1031
                                }
 
1032
                                else {
 
1033
                                        this->step = XML_STEP_NONE;
 
1034
                                        this->state = XML_IN_CDATA;
 
1035
                                }
 
1036
                        }
 
1037
                        else {
 
1038
                                this->state = XML_IN_CDATA_TAG;
 
1039
                                this->type = XML_CDATA_TAG_CH;
 
1040
                                SET_CHAR(this, ']');
 
1041
                                ADD_CHAR(this, ']');
 
1042
                                ADD_CHAR(this, ch);
 
1043
                        }
 
1044
                        break;
 
1045
        }
 
1046
        return(this->type);
 
1047
}
 
1048
 
 
1049
/* ------------------------------------------------------------------- */
 
1050
/* CSXMLProcessor */
 
1051
 
 
1052
bool CSXMLProcessor::buildConversionTable()
 
1053
{
 
1054
        int32_t i;
 
1055
 
 
1056
        /* By default we don't know how to convert any charset
 
1057
         * other tha ISO-1 to unicode!
 
1058
         */
 
1059
        if (strcasecmp(charset, "ISO-8859-1") == 0) {
 
1060
                for (i=0; i<128; i++)
 
1061
                        conversion_table[i] = (wchar_t) (i + 128);
 
1062
        }
 
1063
        else {
 
1064
                for (i=0; i<128; i++)
 
1065
                        conversion_table[i] = '?';
 
1066
        }
 
1067
        return true;
 
1068
}
 
1069
 
 
1070
// Private use are: E000 - F8FF
 
1071
 
 
1072
int32_t CSXMLProcessor::capture_initializer(wchar_t ch)
 
1073
/* We capture tag and attribute data for the parsing purposes.
 
1074
 * The buffers are initialized here (at the lowest level)
 
1075
 * of processing after parsing.
 
1076
 */
 
1077
{
 
1078
        int32_t op;
 
1079
 
 
1080
        op = parseChar(ch);
 
1081
        switch (op & XML_OP_1_MASK) {
 
1082
                case XML_OP_1_START_TAG:
 
1083
                        this->tlength = 0;
 
1084
                        break;
 
1085
                case XML_OP_1_ADD_ATTR:
 
1086
                        this->nlength = 0;
 
1087
                        this->vlength = 0;
 
1088
                        break;
 
1089
        }
 
1090
        return(op);
 
1091
}
 
1092
 
 
1093
int32_t CSXMLProcessor::entity_translator(wchar_t ch)
 
1094
/* This function handles entities.
 
1095
 * Certain entities are translated into UNICODE characters.
 
1096
 * Strictly speaking, these enties are only recognised by HTML.
 
1097
 * The few entities that are recognised by XML are first translated
 
1098
 * into some reserved characters for the parser. This is to ensure
 
1099
 * that the parser does not recognize them as characters with special
 
1100
 * meaning! This includes '&', '<' and '>'.
 
1101
 */
 
1102
{
 
1103
        int32_t op;
 
1104
 
 
1105
        op = capture_initializer(ch);
 
1106
        return(op);
 
1107
}
 
1108
 
 
1109
/*
 
1110
 * This function translates the input character stream into UNICODE.
 
1111
 */
 
1112
int32_t CSXMLProcessor::charset_transformer(wchar_t ch)
 
1113
{
 
1114
        int32_t op;
 
1115
 
 
1116
        // Do transformation according to the charset.
 
1117
        switch (this->charset_type) {
 
1118
                case CHARSET_UTF_8:
 
1119
                        if (ch > 127 && ch < 256) {
 
1120
                                uint32_t utf_value;
 
1121
                                uint8_t utf_ch = (uint8_t)ch;
 
1122
 
 
1123
                                if ((utf_ch & 0xC0) != 0x80)
 
1124
                                        this->utf8_count = 0;
 
1125
                                if ((utf_ch & 0x80) == 0x00)
 
1126
                                        this->utf8_length = 1;
 
1127
                                else if ((utf_ch & 0xE0) == 0xC0)
 
1128
                                        this->utf8_length = 2;
 
1129
                                else if ((utf_ch & 0xF0) == 0xE0)
 
1130
                                        this->utf8_length = 3;
 
1131
                                else if ((utf_ch & 0xF8) == 0xF0)
 
1132
                                        this->utf8_length = 4;
 
1133
                                else if ((utf_ch & 0xFC) == 0xF8)
 
1134
                                        this->utf8_length = 5;
 
1135
                                else if ((utf_ch & 0xFE) == 0xFC)
 
1136
                                        this->utf8_length = 6;
 
1137
                                this->utf8_buffer[this->utf8_count] = (uint32_t) utf_ch;
 
1138
                                this->utf8_count++;
 
1139
                                if (this->utf8_count < this->utf8_length) {
 
1140
                                        // I need more bytes!
 
1141
                                        setDataType(XML_noop);
 
1142
                                        return(XML_noop);
 
1143
                                }
 
1144
                                utf_value = 0;
 
1145
                                switch (this->utf8_length) {
 
1146
                                        case 1:
 
1147
                                                utf_value = this->utf8_buffer[0] & 0x0000007F;
 
1148
                                                break;
 
1149
                                        case 2:
 
1150
                                                utf_value = ((this->utf8_buffer[0] & 0x0000001F) << 6) |
 
1151
                                                                        (this->utf8_buffer[1] & 0x0000003F);
 
1152
                                                if (utf_value < 0x00000080)
 
1153
                                                        utf_value = '?';
 
1154
                                                break;
 
1155
                                        case 3:
 
1156
                                                utf_value = ((this->utf8_buffer[0] & 0x0000000F) << 12) |
 
1157
                                                                        ((this->utf8_buffer[1] & 0x0000003F) << 6) |
 
1158
                                                                        (this->utf8_buffer[2] & 0x0000003F);
 
1159
                                                if (utf_value < 0x000000800)
 
1160
                                                        utf_value = '?';
 
1161
                                                break;
 
1162
                                        case 4:
 
1163
                                                utf_value = ((this->utf8_buffer[0] & 0x00000007) << 18) |
 
1164
                                                                        ((this->utf8_buffer[1] & 0x0000003F) << 12) |
 
1165
                                                                        ((this->utf8_buffer[2] & 0x0000003F) << 6) |
 
1166
                                                                        (this->utf8_buffer[3] & 0x0000003F);
 
1167
                                                if (utf_value < 0x00010000)
 
1168
                                                        utf_value = '?';
 
1169
                                                break;
 
1170
                                        case 5:
 
1171
                                                utf_value = ((this->utf8_buffer[0] & 0x00000003) << 24) |
 
1172
                                                                        ((this->utf8_buffer[1] & 0x0000003F) << 18) |
 
1173
                                                                        ((this->utf8_buffer[2] & 0x0000003F) << 12) |
 
1174
                                                                        ((this->utf8_buffer[3] & 0x0000003F) << 6) |
 
1175
                                                                        (this->utf8_buffer[4] & 0x0000003F);
 
1176
                                                if (utf_value < 0x00200000)
 
1177
                                                        utf_value = '?';
 
1178
                                                break;
 
1179
                                        case 6:
 
1180
                                                utf_value = ((this->utf8_buffer[0] & 0x00000001) << 30) |
 
1181
                                                                        ((this->utf8_buffer[1] & 0x0000003F) << 24) |
 
1182
                                                                        ((this->utf8_buffer[2] & 0x0000003F) << 18) |
 
1183
                                                                        ((this->utf8_buffer[3] & 0x0000003F) << 12) |
 
1184
                                                                        ((this->utf8_buffer[4] & 0x0000003F) << 6) |
 
1185
                                                                        (this->utf8_buffer[5] & 0x0000003F);
 
1186
                                                if (utf_value < 0x04000000)
 
1187
                                                        utf_value = '?';
 
1188
                                                break;
 
1189
                                }
 
1190
                                if (utf_value > 0x0000FFFF)
 
1191
                                        ch = '?';
 
1192
                                else
 
1193
                                        ch = utf_value;
 
1194
                        }
 
1195
                        break;
 
1196
                case CHARSET_TO_CONVERT_8_BIT:
 
1197
                        if (ch > 127 && ch < 256)
 
1198
                                ch = this->conversion_table[((unsigned char) ch) - 128];
 
1199
                        break;
 
1200
        }
 
1201
 
 
1202
        op = entity_translator(ch);
 
1203
 
 
1204
        // Determine the characters set:
 
1205
        switch (op & XML_OP_1_MASK) {
 
1206
                case XML_OP_1_START_TAG:
 
1207
                        if (strcmp(this->pr_tag, "?xml") == 0)
 
1208
                                this->ip = true;
 
1209
                        else
 
1210
                                this->ip = false;
 
1211
                        break;
 
1212
                case XML_OP_1_ADD_ATTR:
 
1213
                        if (this->ip) {
 
1214
                                if (strcasecmp(this->pr_name, "encoding") == 0) {
 
1215
                                        strcpy(this->charset, this->pr_value);
 
1216
                                        if (boost::ifind_first(this->charset, "utf-8"))
 
1217
                                                this->charset_type = CHARSET_UTF_8;
 
1218
                                        else if (boost::ifind_first(this->charset, "ucs-2") ||
 
1219
                                                boost::ifind_first(this->charset, "ucs-4") ||
 
1220
                                                boost::ifind_first(this->charset, "unicode"))
 
1221
                                                this->charset_type = CHARSET_STANDARD;
 
1222
                                        else {
 
1223
                                                this->charset_type = CHARSET_TO_CONVERT_8_BIT;
 
1224
                                                buildConversionTable();
 
1225
                                        }
 
1226
                                }
 
1227
                        }
 
1228
                        break;
 
1229
        }
 
1230
        return(op);
 
1231
}
 
1232
 
 
1233
void CSXMLProcessor::appendWCharToString(char *dstr, size_t *dlen, size_t dsize, wchar_t *schars, size_t slen)
 
1234
{
 
1235
        for (size_t i=0; i < slen; i++) {
 
1236
                if (*dlen < dsize-1) {
 
1237
                        if (*schars > 127)
 
1238
                                dstr[*dlen] = '~';
 
1239
                        else
 
1240
                                dstr[*dlen] = (char)*schars;
 
1241
                        (*dlen)++;
 
1242
                        schars++;
 
1243
                        dstr[*dlen] = 0;
 
1244
                }
 
1245
        }
 
1246
}
 
1247
 
 
1248
int32_t CSXMLProcessor::processChar(wchar_t ch)
 
1249
{
 
1250
        int32_t op;
 
1251
 
 
1252
        op = charset_transformer(ch);
 
1253
 
 
1254
        /*
 
1255
         * Capture output tag and attribute data.
 
1256
         * This must be done at the highest level, after
 
1257
         * parsing.
 
1258
         */
 
1259
        switch (op & XML_DATA_MASK) {
 
1260
                case XML_DATA_TAG:
 
1261
                        appendWCharToString(this->pr_tag, &this->tlength, CS_MAX_XML_NAME_SIZE, this->getDataPtr(), this->getDataLen());
 
1262
                        break;
 
1263
                case XML_DATA_ATTR:
 
1264
                        appendWCharToString(this->pr_name, &this->nlength, CS_MAX_XML_NAME_SIZE, this->getDataPtr(), this->getDataLen());
 
1265
                        break;
 
1266
                case XML_DATA_VALUE:
 
1267
                        appendWCharToString(this->pr_value, &this->vlength, CS_MAX_XML_NAME_SIZE, this->getDataPtr(), this->getDataLen());
 
1268
                        break;
 
1269
        }
 
1270
        return(op);
 
1271
}
 
1272
 
 
1273
bool CSXMLProcessor::getError(int32_t *err, char **msg)
 
1274
{
 
1275
        *err = err_no;
 
1276
        *msg = err_message;
 
1277
        return err_no != 0;
 
1278
}
 
1279
 
 
1280
void CSXMLProcessor::setError(int32_t err, char *msg)
 
1281
{
 
1282
        err_no = err;
 
1283
        if (msg) {
 
1284
                strncpy(err_message, msg, CS_XML_ERR_MSG_SIZE);
 
1285
                err_message[CS_XML_ERR_MSG_SIZE-1] = 0;
 
1286
                return;
 
1287
        }
 
1288
 
 
1289
        switch (err) {
 
1290
                case CS_XML_ERR_OUT_OF_MEMORY:
 
1291
                        sprintf(err_message, "AES parse error- insufficient memory");                   
 
1292
                        break;
 
1293
                case CS_XML_ERR_CHAR_TOO_LARGE:
 
1294
                        sprintf(err_message, "AES parse error- UNICODE character too large to be encoded as UTF-8");                    
 
1295
                        break;
 
1296
                default:
 
1297
                        sprintf(err_message, "AES parse error- %s", strerror(err));
 
1298
                        break;
 
1299
        }
 
1300
}
 
1301
 
 
1302
void CSXMLProcessor::printError(char *prefix)
 
1303
{
 
1304
        printf("%s%s", prefix, err_message);
 
1305
}
 
1306
 
 
1307
/* ------------------------------------------------------------------- */
 
1308
/* CSXMLString */
 
1309
 
 
1310
#ifdef DEBUG_ALL
 
1311
#define EXTRA_SIZE                      2
 
1312
#else
 
1313
#define EXTRA_SIZE                      100
 
1314
#endif
 
1315
 
 
1316
bool CSXMLString::addChar(char ch, CSXMLProcessor *xml)
 
1317
{
 
1318
        char *ptr;
 
1319
 
 
1320
        if (stringLen + 2 > stringSize) {
 
1321
                if (!(ptr = (char *) realloc(stringPtr, stringLen + 2 + EXTRA_SIZE))) {
 
1322
                        xml->setError(CS_XML_ERR_OUT_OF_MEMORY, NULL);
 
1323
                        return false;
 
1324
                }
 
1325
                stringPtr = ptr;
 
1326
                stringSize = stringLen + 2 + EXTRA_SIZE;
 
1327
        }
 
1328
        stringPtr[stringLen] = ch;
 
1329
        stringPtr[stringLen+1] = 0;
 
1330
        stringLen++;
 
1331
        return true;
 
1332
}
 
1333
 
 
1334
bool CSXMLString::addChars(size_t size, wchar_t *buffer, bool to_lower, CSXMLProcessor *xml)
 
1335
{
 
1336
        size_t          i;
 
1337
        uint32_t        uni_char;
 
1338
        int32_t                 shift;
 
1339
 
 
1340
        for (i=0; i<size; i++) {
 
1341
                uni_char = (uint32_t) buffer[i];
 
1342
                
 
1343
                /* Convertion to lower only done for ASCII! */
 
1344
                if (to_lower && uni_char <= 127)
 
1345
                        uni_char = (uint32_t) tolower((int32_t) uni_char);
 
1346
 
 
1347
                // Convert to UTF-8!
 
1348
                if (uni_char <= 0x0000007F) {
 
1349
                        if (!addChar((char) uni_char, xml))
 
1350
                                return false;
 
1351
                        shift = -6;
 
1352
                }
 
1353
                else if (uni_char <= 0x000007FF) {
 
1354
                        if (!addChar((char) ((0x000000C0) | ((uni_char >> 6) & 0x0000001F)), xml))
 
1355
                                return false;
 
1356
                        shift = 0;
 
1357
                }
 
1358
                else if (uni_char <= 0x00000FFFF) {
 
1359
                        if (!addChar((char) ((0x000000E0) | ((uni_char >> 12) & 0x0000000F)), xml))
 
1360
                                return false;
 
1361
                        shift = 6;
 
1362
                }
 
1363
                else if (uni_char <= 0x001FFFFF) {
 
1364
                        if (!addChar((char) ((0x000000F0) | ((uni_char >> 18) & 0x00000007)), xml))
 
1365
                                return false;
 
1366
                        shift = 12;
 
1367
                }
 
1368
                else if (uni_char <= 0x003FFFFFF) {
 
1369
                        if (!addChar((char) ((0x000000F0) | ((uni_char >> 24) & 0x00000003)), xml))
 
1370
                                return false;
 
1371
                        shift = 18;
 
1372
                }
 
1373
                else if (uni_char <= 0x07FFFFFFF) {
 
1374
                        if (!addChar((char) ((0x000000F0) | ((uni_char >> 30) & 0x00000001)), xml))
 
1375
                                return false;
 
1376
                        shift = 24;
 
1377
                }
 
1378
                else {
 
1379
                        xml->setError(CS_XML_ERR_CHAR_TOO_LARGE, NULL);
 
1380
                        return false;
 
1381
                }
 
1382
 
 
1383
                while (shift >= 0) {
 
1384
                        if (!addChar((char) ((0x00000080) | ((uni_char >> shift) & 0x0000003F)), xml))
 
1385
                                return false;
 
1386
                        shift -= 6;
 
1387
                }
 
1388
        }
 
1389
        return true;
 
1390
}
 
1391
 
 
1392
bool CSXMLString::addString(const char *string, CSXMLProcessor *xml)
 
1393
{
 
1394
        bool ok = true;
 
1395
        
 
1396
        while (*string && ok) {
 
1397
                ok = addChar(*string, xml);
 
1398
                string++;
 
1399
        }
 
1400
        return ok;
 
1401
}
 
1402
 
 
1403
void CSXMLString::setEmpty()
 
1404
{
 
1405
        stringLen = 0;
 
1406
        if (stringPtr)
 
1407
                *stringPtr = 0;
 
1408
}
 
1409
 
 
1410
void CSXMLString::setNull()
 
1411
{
 
1412
        if (stringPtr)
 
1413
                free(stringPtr);
 
1414
        stringPtr = NULL;
 
1415
        stringLen = 0;
 
1416
        stringSize = 0;
 
1417
}
 
1418
 
 
1419
char *CSXMLString::lastComponent()
 
1420
{
 
1421
        char *ptr;
 
1422
 
 
1423
        if (stringLen == 0)
 
1424
                return NULL;
 
1425
 
 
1426
        ptr = stringPtr + stringLen - 1;
 
1427
        while (ptr > stringPtr && *ptr != '/')
 
1428
                ptr--;
 
1429
        return ptr;
 
1430
}
 
1431
 
 
1432
/* We assume comp begins with a '/' */
 
1433
char *CSXMLString::findTrailingComponent(const char *comp)
 
1434
{
 
1435
        char *ptr, *last_slash;
 
1436
 
 
1437
        if (stringLen == 0)
 
1438
                return NULL;
 
1439
 
 
1440
        ptr = stringPtr + stringLen - 1;
 
1441
        last_slash = NULL;
 
1442
 
 
1443
        do {
 
1444
                /* Find the next '/' */
 
1445
                while (ptr > stringPtr && *ptr != '/')
 
1446
                        ptr--;
 
1447
                if (last_slash)
 
1448
                        *last_slash = 0;
 
1449
                if (strcmp(ptr, comp) == 0) {
 
1450
                        if (last_slash)
 
1451
                                *last_slash = '/';
 
1452
                        return ptr;
 
1453
                }
 
1454
                if (last_slash)
 
1455
                        *last_slash = '/';
 
1456
                last_slash = ptr;
 
1457
                ptr--;
 
1458
        }
 
1459
        while (ptr > stringPtr);
 
1460
        return NULL;
 
1461
}
 
1462
 
 
1463
void CSXMLString::truncate(char *ptr)
 
1464
{
 
1465
        *ptr = 0;
 
1466
        stringLen = ptr - stringPtr;
 
1467
}
 
1468
 
 
1469
/* ------------------------------------------------------------------- */
 
1470
/* CSXML */
 
1471
 
 
1472
#define IS_XML_CDATA                            0
 
1473
#define IS_XML_CDATA_TAG                        1
 
1474
#define IS_XML_TAG                                      2
 
1475
#define IS_XML_CLOSE_TAG                        3
 
1476
#define IS_XML_COMMENT                          4
 
1477
#define IS_XML_DTD                                      5
 
1478
#define IS_XML_PI                                       6
 
1479
#define IS_XML_PI_XML                           7
 
1480
#define IS_XML_IN_EX                            8
 
1481
#define IS_XML_OPEN_BRACKET                     9
 
1482
#define IS_XML_CLOSE_BRACKET            10
 
1483
 
 
1484
int32_t CSXML::nodeType(char *name)
 
1485
{
 
1486
        if (name) {
 
1487
                switch (*name) {
 
1488
                        case 0:
 
1489
                                return IS_XML_CDATA;
 
1490
                        case '[':
 
1491
                                if (strlen(name) == 1)
 
1492
                                        return IS_XML_OPEN_BRACKET;
 
1493
                                break;
 
1494
                        case ']':
 
1495
                                if (strlen(name) == 1)
 
1496
                                        return IS_XML_CLOSE_BRACKET;
 
1497
                                break;
 
1498
                        case '/':
 
1499
                                return IS_XML_CLOSE_TAG;
 
1500
                        case '!':
 
1501
                                if (strlen(name) > 1) {
 
1502
                                        if (strcasecmp(name, "!--") == 0)
 
1503
                                                return IS_XML_COMMENT;
 
1504
                                        if (name[1] == '[') {
 
1505
                                                if (strcasecmp(name, "![CDATA[") == 0)
 
1506
                                                        return IS_XML_CDATA_TAG;
 
1507
                                                return IS_XML_IN_EX;
 
1508
                                        }
 
1509
                                }
 
1510
                                return IS_XML_DTD;
 
1511
                        case '?':
 
1512
                                if (strcasecmp(name, "?xml") == 0)
 
1513
                                        return IS_XML_PI_XML;
 
1514
                                return IS_XML_PI;
 
1515
                }
 
1516
                return IS_XML_TAG;
 
1517
        }
 
1518
        return IS_XML_CDATA;
 
1519
}
 
1520
 
 
1521
bool CSXML::internalCloseNode(const char *name, bool single)
 
1522
{
 
1523
        bool    ok = true;
 
1524
        char    *ptr;
 
1525
 
 
1526
        if (single) {
 
1527
                if ((ptr = xml_path.lastComponent())) {
 
1528
                        ok = closeNode(xml_path.stringPtr);
 
1529
                        xml_path.truncate(ptr);
 
1530
                }
 
1531
        }
 
1532
        else if ((ptr = xml_path.findTrailingComponent(name))) {
 
1533
                /* Close the node that is named above. If the XML is
 
1534
                 * correct, then the node should be at the top of the
 
1535
                 * node stack (last element of the path).
 
1536
                 *
 
1537
                 * If not found, "ignore" the close.
 
1538
                 *
 
1539
                 * If not found on the top of the node stack, then
 
1540
                 * we close serveral nodes.
 
1541
                 */
 
1542
                for (;;) {
 
1543
                        if (!(ptr = xml_path.lastComponent()))
 
1544
                                break;
 
1545
                        if (!(ok = closeNode(xml_path.stringPtr)))
 
1546
                                break;
 
1547
                        if (strcmp(ptr, name) == 0) {
 
1548
                                xml_path.truncate(ptr);
 
1549
                                break;
 
1550
                        }
 
1551
                        xml_path.truncate(ptr);
 
1552
                }
 
1553
        }
 
1554
        return ok;
 
1555
}
 
1556
 
 
1557
bool CSXML::internalOpenNode(const char *name)
 
1558
{
 
1559
        bool ok;
 
1560
 
 
1561
        ok = xml_path.addString("/", this);
 
1562
        if (!ok)
 
1563
                return ok;
 
1564
        ok = xml_path.addString(name, this);
 
1565
        if (!ok)
 
1566
                return ok;
 
1567
        return openNode(this->xml_path.stringPtr, this->xml_value.stringPtr);
 
1568
}
 
1569
 
 
1570
bool CSXML::parseXML(int32_t my_flags)
 
1571
{
 
1572
        wchar_t ch;
 
1573
        bool    ok = true;
 
1574
        int32_t         op;
 
1575
        int32_t         tagtype;
 
1576
 
 
1577
        this->flags = my_flags;
 
1578
        ok = xml_path.addChars(0, NULL, false, this);
 
1579
        if (!ok)
 
1580
                goto exit;
 
1581
        ok = xml_name.addChars(0, NULL, false, this);
 
1582
        if (!ok)
 
1583
                goto exit;
 
1584
        ok = xml_value.addChars(0, NULL, false, this);
 
1585
        if (!ok)
 
1586
                goto exit;
 
1587
 
 
1588
        ok = getChar(&ch);
 
1589
        while (ch != CS_XML_EOF_CHAR && ok) {
 
1590
                op = processChar(ch);
 
1591
                switch (op & XML_OP_1_MASK) {
 
1592
                        case XML_OP_1_NOOP:
 
1593
                                break;
 
1594
                        case XML_OP_1_END_TAG:
 
1595
                                break;
 
1596
                        case XML_OP_1_END_CLOSE_TAG:
 
1597
                                break;
 
1598
                        case XML_OP_1_END_EMPTY_TAG:
 
1599
                                ok = internalCloseNode("/>", true);
 
1600
                                break;
 
1601
                        case XML_OP_1_END_PI_TAG:
 
1602
                                ok = internalCloseNode("?>", true);
 
1603
                                break;
 
1604
                        case XML_OP_1_END_ENTITY_TAG:
 
1605
                                ok = internalCloseNode(">", true);
 
1606
                                break;
 
1607
                        case XML_OP_1_END_BRACKET_TAG:
 
1608
                                ok = internalCloseNode("]>", true);
 
1609
                                break;
 
1610
                        case XML_OP_1_END_UNKNOWN_TAG:
 
1611
                                ok = internalCloseNode(">", true);
 
1612
                                break;
 
1613
                        case XML_OP_1_START_CDATA_TAG:
 
1614
                                break;
 
1615
                        case XML_OP_1_START_COMMENT:
 
1616
                                break;
 
1617
                        case XML_OP_1_START_TAG:
 
1618
                                if (nodeType(xml_name.stringPtr) == IS_XML_CLOSE_TAG)
 
1619
                                        ok = internalCloseNode(xml_name.stringPtr, false);
 
1620
                                else
 
1621
                                        ok = internalOpenNode(xml_name.stringPtr);
 
1622
                                xml_name.setEmpty();
 
1623
                                xml_value.setEmpty();
 
1624
                                break;
 
1625
                        case XML_OP_1_ADD_ATTR:
 
1626
                                tagtype = nodeType(xml_name.stringPtr);
 
1627
                                if (tagtype != IS_XML_OPEN_BRACKET && tagtype != IS_XML_CLOSE_BRACKET)
 
1628
                                        ok = addAttribute(xml_path.stringPtr, xml_name.stringPtr, xml_value.stringPtr);
 
1629
                                xml_name.setEmpty();
 
1630
                                xml_value.setEmpty();
 
1631
                                break;
 
1632
                        case XML_OP_1_END_CDATA:
 
1633
                                if (xml_value.stringLen || (my_flags & XML_KEEP_EMPTY_CDATA)) {
 
1634
                                        ok = internalOpenNode("");
 
1635
                                        xml_name.setEmpty();
 
1636
                                        xml_value.setEmpty();
 
1637
                                        ok = internalCloseNode("", true);
 
1638
                                }
 
1639
                                break;
 
1640
                        case XML_OP_1_END_CDATA_TAG:
 
1641
                                ok = internalOpenNode("![CDATA[");
 
1642
                                xml_name.setEmpty();
 
1643
                                xml_value.setEmpty();
 
1644
                                if (ok)
 
1645
                                        ok = internalCloseNode("]]>", true);
 
1646
                                break;
 
1647
                        case XML_OP_1_END_COMMENT:
 
1648
                                ok = internalOpenNode("!--");
 
1649
                                xml_name.setEmpty();
 
1650
                                xml_value.setEmpty();
 
1651
                                if (ok)
 
1652
                                        ok = internalCloseNode("-->", true);
 
1653
                                break;
 
1654
                }
 
1655
                if (!ok)
 
1656
                        break;
 
1657
                switch (op & XML_DATA_MASK) {
 
1658
                        case XML_DATA_TAG:
 
1659
                        case XML_DATA_ATTR:
 
1660
                                ok = xml_name.addChars(getDataLen(), getDataPtr(), true, this);
 
1661
                                break;
 
1662
                        case XML_DATA_CDATA:
 
1663
                        case XML_DATA_CDATA_TAG:
 
1664
                        case XML_COMMENT:
 
1665
                        case XML_DATA_VALUE:
 
1666
                                ok = xml_value.addChars(getDataLen(), getDataPtr(), false, this);
 
1667
                                break;
 
1668
                }
 
1669
                if (!ok)
 
1670
                        break;
 
1671
                switch (op & XML_OP_2_MASK) {
 
1672
                        case XML_OP_2_NOOP:
 
1673
                                break;
 
1674
                        case XML_OP_2_END_TAG:
 
1675
                                break;
 
1676
                        case XML_OP_2_END_CLOSE_TAG:
 
1677
                                break;
 
1678
                        case XML_OP_2_END_EMPTY_TAG:
 
1679
                                ok = internalCloseNode("/>", true);
 
1680
                                break;
 
1681
                        case XML_OP_2_END_PI_TAG:
 
1682
                                ok = internalCloseNode("?>", true);
 
1683
                                break;
 
1684
                        case XML_OP_2_END_ENTITY_TAG:
 
1685
                                ok = internalCloseNode(">", true);
 
1686
                                break;
 
1687
                        case XML_OP_2_END_BRACKET_TAG:
 
1688
                                ok = internalCloseNode("]>", true);
 
1689
                                break;
 
1690
                        case XML_OP_2_END_UNKNOWN_TAG:
 
1691
                                ok = internalCloseNode(">", true);
 
1692
                                break;
 
1693
                        case XML_OP_2_START_CDATA_TAG:
 
1694
                                break;
 
1695
                        case XML_OP_2_START_COMMENT:
 
1696
                                break;
 
1697
                }
 
1698
                ok = getChar(&ch);
 
1699
        }
 
1700
 
 
1701
        exit:
 
1702
        xml_path.setNull();
 
1703
        xml_name.setNull();
 
1704
        xml_value.setNull();
 
1705
        return ok;
 
1706
}
 
1707
 
 
1708
/* ------------------------------------------------------------------- */
 
1709
/* CSXMLPrint */
 
1710
 
 
1711
bool CSXMLPrint::openNode(char *path, char *value)
 
1712
{
 
1713
        printf("OPEN  %s\n", path);
 
1714
        if (value && *value)
 
1715
                printf("      %s\n", value);
 
1716
        return true;
 
1717
}
 
1718
 
 
1719
bool CSXMLPrint::closeNode(char *path)
 
1720
{
 
1721
        printf("close %s\n", path);
 
1722
        return true;
 
1723
}
 
1724
 
 
1725
bool CSXMLPrint::addAttribute(char *path, char *name, char *value)
 
1726
{
 
1727
        if (value)
 
1728
                printf("attr  %s %s=%s\n", path, name, value);
 
1729
        else
 
1730
                printf("attr  %s %s\n", path, name);
 
1731
        return true;
 
1732
}
 
1733
 
 
1734
/* ------------------------------------------------------------------- */
 
1735
/* CSXMLBuffer */
 
1736
 
 
1737
bool CSXMLBuffer::parseString(const char *data, int32_t my_flags)
 
1738
{
 
1739
        charData = data;
 
1740
        dataLen = strlen(data);
 
1741
        dataPos = 0;
 
1742
        return parseXML(my_flags);
 
1743
}
 
1744
 
 
1745
bool CSXMLBuffer::parseData(const char *data, size_t len, int32_t my_flags)
 
1746
{
 
1747
        charData = data;
 
1748
        dataLen = len;
 
1749
        dataPos = 0;
 
1750
        return parseXML(my_flags);
 
1751
}
 
1752
 
 
1753
bool CSXMLBuffer::getChar(wchar_t *ch)
 
1754
{
 
1755
        if (dataPos == dataLen)
 
1756
                *ch = CS_XML_EOF_CHAR;
 
1757
        else {
 
1758
                *ch = (wchar_t) (unsigned char) charData[dataPos];
 
1759
                dataPos++;
 
1760
        }
 
1761
        return true;
 
1762
}
 
1763
 
 
1764
/* ------------------------------------------------------------------- */
 
1765
/* CSXMLFile */
 
1766
 
 
1767
bool CSXMLFile::parseFile(char *file_name, int32_t my_flags)
 
1768
{
 
1769
        bool ok;
 
1770
 
 
1771
        if (!(this->file = fopen(file_name, "r"))) {
 
1772
                setError(errno, NULL);
 
1773
                return false;
 
1774
        }
 
1775
        ok = parseXML(my_flags);
 
1776
        fclose(this->file);
 
1777
        return ok;
 
1778
}
 
1779
 
 
1780
bool CSXMLFile::getChar(wchar_t *ch)
 
1781
{
 
1782
        int32_t next_ch;
 
1783
        
 
1784
        next_ch = fgetc(file);
 
1785
        if (next_ch == EOF) {
 
1786
                if (ferror(file)) {
 
1787
                        setError(errno, NULL);
 
1788
                        return false;
 
1789
                }
 
1790
                *ch = CS_XML_EOF_CHAR;
 
1791
        }
 
1792
        else
 
1793
                *ch = (wchar_t) next_ch;
 
1794
        return true;
 
1795
}
 
1796
 
 
1797