~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-04 20:02:43 UTC
  • mfrom: (1548.2.40 drizzle_pbms)
  • mto: This revision was merged to the branch mainline in revision 1644.
  • Revision ID: mordred@inaugust.com-20100704200243-2vkq9gi6ysauj2tb
Merge PBMS from Barry.

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