ObjFW  Diff

Differences From Artifact [f51f5629f8]:

To Artifact [130441ad5f]:


34
35
36
37
38
39
40
41

42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

58
59
60
61
62
63
64
65
66

67
68
69
70
71
72
73
74
75


76
77
78


79
80

81
82
83

84
85
86
87




88
89
90


91
92
93
94
95
96
97
98
99
100
101
102
103
104

105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125

126
127
128
129
130
131
132

133
134
135
136
137
138
139
34
35
36
37
38
39
40

41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

57
58
59
60
61
62
63
64
65

66
67
68
69
70
71
72
73


74
75
76


77
78
79

80
81
82

83
84



85
86
87
88
89


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104

105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125

126
127
128
129
130
131
132

133
134
135
136
137
138
139
140







-
+















-
+








-
+







-
-
+
+

-
-
+
+

-
+


-
+

-
-
-
+
+
+
+

-
-
+
+













-
+




















-
+






-
+







#import "OFMalformedXMLException.h"
#import "OFUnboundNamespaceException.h"

#import "macros.h"

typedef void (*state_function)(id, SEL, const char*, size_t*, size_t*);
static SEL selectors[OF_XMLPARSER_NUM_STATES];
static state_function lookup_table[OF_XMLPARSER_NUM_STATES];
static state_function lookupTable[OF_XMLPARSER_NUM_STATES];

static OF_INLINE OFString*
transform_string(OFMutableString *cache,
    OFObject <OFStringXMLUnescapingDelegate> *delegate)
{
	[cache replaceOccurrencesOfString: @"\r\n"
			       withString: @"\n"];
	[cache replaceOccurrencesOfString: @"\r"
			       withString: @"\n"];
	return [cache stringByXMLUnescapingWithDelegate: delegate];
}

static OFString*
namespace_for_prefix(OFString *prefix, OFArray *namespaces)
{
	OFDictionary **carray = [namespaces cArray];
	OFDictionary **cArray = [namespaces cArray];
	ssize_t i;

	if (prefix == nil)
		prefix = @"";

	for (i = [namespaces count] - 1; i >= 0; i--) {
		OFString *tmp;

		if ((tmp = [carray[i] objectForKey: prefix]) != nil)
		if ((tmp = [cArray[i] objectForKey: prefix]) != nil)
			return tmp;
	}

	return nil;
}

static OF_INLINE void
resolve_attr_namespace(OFXMLAttribute *attr, OFString *prefix, OFString *ns,
    OFArray *namespaces, Class isa)
resolve_attribute_namespace(OFXMLAttribute *attribute, OFString *prefix,
    OFString *ns, OFArray *namespaces, Class isa)
{
	OFString *attr_ns;
	OFString *attr_prefix = attr->ns;
	OFString *attributeNS;
	OFString *attributePrefix = attribute->ns;

	if (attr_prefix == nil)
	if (attributePrefix == nil)
		return;

	attr_ns = namespace_for_prefix(attr_prefix, namespaces);
	attributeNS = namespace_for_prefix(attributePrefix, namespaces);

	if ((attr_prefix != nil && attr_ns == nil))
		@throw [OFUnboundNamespaceException newWithClass: isa
							  prefix: attr_prefix];
	if ((attributePrefix != nil && attributeNS == nil))
		@throw [OFUnboundNamespaceException
		    newWithClass: isa
			  prefix: attributePrefix];

	[attr->ns release];
	attr->ns = [attr_ns retain];
	[attribute->ns release];
	attribute->ns = [attributeNS retain];
}

@implementation OFXMLParser
#if defined(OF_HAVE_PROPERTIES) && defined(OF_HAVE_BLOCKS)
@synthesize processingInstructionsHandler, elementStartHandler;
@synthesize elementEndHandler, charactersHandler, CDATAHandler, commentHandler;
@synthesize unknownEntityHandler;
#endif

+ (void)initialize
{
	size_t i;

	const SEL sels[] = {
	const SEL selectors_[] = {
		@selector(_parseOutsideTagWithBuffer:i:last:),
		@selector(_parseTagOpenedWithBuffer:i:last:),
		@selector(_parseInProcessingInstructionsWithBuffer:i:last:),
		@selector(_parseInTagNameWithBuffer:i:last:),
		@selector(_parseInCloseTagNameWithBuffer:i:last:),
		@selector(_parseInTagWithBuffer:i:last:),
		@selector(_parseInAttributeNameWithBuffer:i:last:),
		@selector(_parseExpectDelimiterWithBuffer:i:last:),
		@selector(_parseInAttributeValueWithBuffer:i:last:),
		@selector(_parseExpectCloseWithBuffer:i:last:),
		@selector(_parseExpectSpaceOrCloseWithBuffer:i:last:),
		@selector(_parseInExclamationMarkWithBuffer:i:last:),
		@selector(_parseInCDATAOpeningWithBuffer:i:last:),
		@selector(_parseInCDATA1WithBuffer:i:last:),
		@selector(_parseInCDATA2WithBuffer:i:last:),
		@selector(_parseInCommentOpeningWithBuffer:i:last:),
		@selector(_parseInComment1WithBuffer:i:last:),
		@selector(_parseInComment2WithBuffer:i:last:),
		@selector(_parseInDoctypeWithBuffer:i:last:),
	};
	memcpy(selectors, sels, sizeof(sels));
	memcpy(selectors, selectors_, sizeof(selectors_));

	for (i = 0; i < OF_XMLPARSER_NUM_STATES; i++) {
		if (![self instancesRespondToSelector: selectors[i]])
			@throw [OFInitializationFailedException
			    newWithClass: self];

		lookup_table[i] = (state_function)
		lookupTable[i] = (state_function)
		    [self instanceMethodForSelector: selectors[i]];
	}
}

+ parser
{
	return [[[self alloc] init] autorelease];
173
174
175
176
177
178
179
180
181
182



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198

199
200
201
202
203
204
205

206
207
208
209

210
211
212
213

214
215
216

217
218
219
220
221
222
223
224
225
226
227
228
229

230
231

232
233
234

235
236
237


238
239
240
241
242

243
244
245
246
247


248
249
250


251
252
253

254
255
256
257
258
259
260
174
175
176
177
178
179
180



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198

199
200
201
202
203



204
205
206
207

208
209
210
211

212
213
214

215
216
217
218
219
220
221
222
223
224
225
226
227

228
229

230
231
232

233
234


235
236
237
238
239
240

241
242
243
244


245
246
247


248
249
250
251

252
253
254
255
256
257
258
259







-
-
-
+
+
+















-
+




-
-
-
+



-
+



-
+


-
+












-
+

-
+


-
+

-
-
+
+




-
+



-
-
+
+

-
-
+
+


-
+







{
	[(id)delegate release];

	[cache release];
	[name release];
	[prefix release];
	[namespaces release];
	[attrs release];
	[attrName release];
	[attrPrefix release];
	[attributes release];
	[attributeName release];
	[attributePrefix release];
	[previous release];
#if defined(OF_HAVE_PROPERTIES) && defined(OF_HAVE_BLOCKS)
	[elementStartHandler release];
	[elementEndHandler release];
	[charactersHandler release];
	[CDATAHandler release];
	[commentHandler release];
	[unknownEntityHandler release];
#endif

	[super dealloc];
}

- (id <OFXMLParserDelegate>)delegate
{
	return [[(id)delegate retain] autorelease];
	OF_GETTER(delegate, YES)
}

- (void)setDelegate: (id <OFXMLParserDelegate>)delegate_
{
	[(id)delegate_ retain];
	[(id)delegate release];
	delegate = delegate_;
	OF_SETTER(delegate, delegate_, YES, NO)
}

- (void)parseBuffer: (const char*)buf
	   withSize: (size_t)size
	 withLength: (size_t)length
{
	size_t i, last = 0;

	for (i = 0; i < size; i++) {
	for (i = 0; i < length; i++) {
		size_t j = i;

		lookup_table[state](self, selectors[state], buf, &i, &last);
		lookupTable[state](self, selectors[state], buf, &i, &last);

		/* Ensure we don't count this character twice */
		if (i != j)
			continue;

		if (buf[i] == '\r' || (buf[i] == '\n' && !lastCarriageReturn))
			lineNumber++;

		lastCarriageReturn = (buf[i] == '\r');
	}

	/* In OF_XMLPARSER_IN_TAG, there can be only spaces */
	if (size - last > 0 && state != OF_XMLPARSER_IN_TAG)
	if (length - last > 0 && state != OF_XMLPARSER_IN_TAG)
		[cache appendCStringWithoutUTF8Checking: buf + last
						 length: size - last];
						 length: length - last];
}

- (void)parseString: (OFString*)str
- (void)parseString: (OFString*)string
{
	[self parseBuffer: [str cString]
		 withSize: [str cStringLength]];
	[self parseBuffer: [string cString]
	       withLength: [string cStringLength]];
}

- (void)parseStream: (OFStream*)stream
{
	char *buf = [self allocMemoryWithSize: of_pagesize];
	char *buffer = [self allocMemoryWithSize: of_pagesize];

	@try {
		while (![stream isAtEndOfStream]) {
			size_t len = [stream readNBytes: of_pagesize
					     intoBuffer: buf];
			size_t length = [stream readNBytes: of_pagesize
						intoBuffer: buffer];

			[self parseBuffer: buf
				 withSize: len];
			[self parseBuffer: buffer
			       withLength: length];
		}
	} @finally {
		[self freeMemory: buf];
		[self freeMemory: buffer];
	}
}

- (void)parseFile: (OFString*)path
{
	OFFile *file = [[OFFile alloc] initWithPath: path
					       mode: @"rb"];
269
270
271
272
273
274
275
276

277
278
279
280

281
282
283
284



285
286
287
288

289
290
291
292
293



294
295
296

297
298
299
300

301
302
303
304

305
306
307
308

309
310
311
312
313
314
315
316
317
318
319
320

321
322
323
324

325
326
327
328

329
330
331
332
333
334
335
268
269
270
271
272
273
274

275
276
277
278

279
280



281
282
283
284
285
286

287
288
289



290
291
292
293
294

295
296
297
298

299
300
301
302

303
304
305
306

307
308
309
310
311
312
313
314
315
316
317
318

319
320
321
322

323
324
325
326

327
328
329
330
331
332
333
334







-
+



-
+

-
-
-
+
+
+



-
+


-
-
-
+
+
+


-
+



-
+



-
+



-
+











-
+



-
+



-
+







/*
 * The following methods handle the different states of the parser. They are
 * looked up in +[initialize] and put in a lookup table to speed things up.
 * One dispatch for every character would be way too slow!
 */

/* Not in a tag */
- (void)_parseOutsideTagWithBuffer: (const char*)buf
- (void)_parseOutsideTagWithBuffer: (const char*)buffer
				 i: (size_t*)i
			      last: (size_t*)last
{
	size_t len;
	size_t length;

	if ((finishedParsing || [previous count] < 1) && buf[*i] != ' ' &&
	    buf[*i] != '\t' && buf[*i] != '\n' && buf[*i] != '\r' &&
	    buf[*i] != '<')
	if ((finishedParsing || [previous count] < 1) && buffer[*i] != ' ' &&
	    buffer[*i] != '\t' && buffer[*i] != '\n' && buffer[*i] != '\r' &&
	    buffer[*i] != '<')
		@throw [OFMalformedXMLException newWithClass: isa
						      parser: self];

	if (buf[*i] != '<')
	if (buffer[*i] != '<')
		return;

	if ((len = *i - *last) > 0)
		[cache appendCStringWithoutUTF8Checking: buf + *last
						 length: len];
	if ((length = *i - *last) > 0)
		[cache appendCStringWithoutUTF8Checking: buffer + *last
						 length: length];

	if ([cache cStringLength] > 0) {
		OFString *str;
		OFString *characters;
		OFAutoreleasePool *pool;

		pool = [[OFAutoreleasePool alloc] init];
		str = transform_string(cache, self);
		characters = transform_string(cache, self);

#if defined(OF_HAVE_PROPERTIES) && defined(OF_HAVE_BLOCKS)
		if (charactersHandler != NULL)
			charactersHandler(self, str);
			charactersHandler(self, characters);
		else
#endif
			[delegate parser: self
			 foundCharacters: str];
			 foundCharacters: characters];

		[pool release];
	}

	[cache setToCString: ""];

	*last = *i + 1;
	state = OF_XMLPARSER_TAG_OPENED;
}

/* Tag was just opened */
- (void)_parseTagOpenedWithBuffer: (const char*)buf
- (void)_parseTagOpenedWithBuffer: (const char*)buffer
				i: (size_t*)i
			     last: (size_t*)last
{
	if (finishedParsing && buf[*i] != '!' && buf[*i] != '?')
	if (finishedParsing && buffer[*i] != '!' && buffer[*i] != '?')
		@throw [OFMalformedXMLException newWithClass: isa
						      parser: self];

	switch (buf[*i]) {
	switch (buffer[*i]) {
		case '?':
			*last = *i + 1;
			state = OF_XMLPARSER_IN_PROCESSING_INSTRUCTIONS;
			level = 0;
			break;
		case '/':
			*last = *i + 1;
348
349
350
351
352
353
354
355
356
357
358
359
360






361
362
363
364
365
366
367
368
369
370
371
372


373
374
375


376
377
378


379
380
381
382

383
384
385
386
387

388
389
390
391


392
393

394
395
396
397

398
399
400

401
402

403
404
405
406

407
408
409
410


411
412
413


414
415
416
417


418
419
420
421
422

423
424
425
426
427
428

429
430
431
432
433
434
435

436
437
438
439

440
441

442
443
444
445
446

447
448
449
450
451
452
453
347
348
349
350
351
352
353






354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369


370
371
372


373
374
375


376
377
378
379
380

381
382
383
384
385

386
387
388


389
390
391

392
393
394
395

396
397
398

399
400

401
402
403
404

405
406
407


408
409
410


411
412
413
414


415
416
417
418
419
420

421
422
423
424
425
426

427
428
429
430
431
432
433

434
435
436
437

438
439

440
441
442
443
444

445
446
447
448
449
450
451
452







-
-
-
-
-
-
+
+
+
+
+
+










-
-
+
+

-
-
+
+

-
-
+
+



-
+




-
+


-
-
+
+

-
+



-
+


-
+

-
+



-
+


-
-
+
+

-
-
+
+


-
-
+
+




-
+





-
+






-
+



-
+

-
+




-
+







			break;
	}
}

/* <?xml […]?> */
- (BOOL)_parseXMLProcessingInstructions: (OFString*)pi
{
	const char *pi_c;
	size_t i, last, pi_len;
	int xstate = 0;
	OFString *attr = nil;
	OFString *val = nil;
	char xdelim = 0;
	const char *cString;
	size_t i, last, length;
	int piState = 0;
	OFString *attribute = nil;
	OFString *value = nil;
	char piDelimiter = 0;

	if (!acceptProlog)
		return NO;

	acceptProlog = NO;

	pi = [pi substringFromIndex: 3
			    toIndex: [pi length]];
	pi = [pi stringByDeletingLeadingAndTrailingWhitespaces];

	pi_c = [pi cString];
	pi_len = [pi cStringLength];
	cString = [pi cString];
	length = [pi cStringLength];

	for (i = last = 0; i < pi_len; i++) {
		switch (xstate) {
	for (i = last = 0; i < length; i++) {
		switch (piState) {
		case 0:
			if (pi_c[i] == ' ' || pi_c[i] == '\t' ||
			    pi_c[i] == '\r' || pi_c[i] == '\n')
			if (cString[i] == ' ' || cString[i] == '\t' ||
			    cString[i] == '\r' || cString[i] == '\n')
				continue;

			last = i;
			xstate = 1;
			piState = 1;
			i--;

			break;
		case 1:
			if (pi_c[i] != '=')
			if (cString[i] != '=')
				continue;

			attr = [OFString stringWithCString: pi_c + last
						    length: i - last];
			attribute = [OFString stringWithCString: cString + last
							 length: i - last];
			last = i + 1;
			xstate = 2;
			piState = 2;

			break;
		case 2:
			if (pi_c[i] != '\'' && pi_c[i] != '"')
			if (cString[i] != '\'' && cString[i] != '"')
				return NO;

			xdelim = pi_c[i];
			piDelimiter = cString[i];
			last = i + 1;
			xstate = 3;
			piState = 3;

			break;
		case 3:
			if (pi_c[i] != xdelim)
			if (cString[i] != piDelimiter)
				continue;

			val = [OFString stringWithCString: pi_c + last
						   length: i - last];
			value = [OFString stringWithCString: cString + last
						     length: i - last];

			if ([attr isEqual: @"version"])
				if (![val hasPrefix: @"1."])
			if ([attribute isEqual: @"version"])
				if (![value hasPrefix: @"1."])
					return NO;

			if ([attr isEqual: @"encoding"])
				if ([val caseInsensitiveCompare: @"utf-8"] !=
			if ([attribute isEqual: @"encoding"])
				if ([value caseInsensitiveCompare: @"utf-8"] !=
				    OF_ORDERED_SAME)
					return NO;

			last = i + 1;
			xstate = 0;
			piState = 0;

			break;
		}
	}

	if (xstate != 0)
	if (piState != 0)
		return NO;

	return YES;
}

/* Inside processing instructions */
- (void)_parseInProcessingInstructionsWithBuffer: (const char*)buf
- (void)_parseInProcessingInstructionsWithBuffer: (const char*)buffer
					       i: (size_t*)i
					    last: (size_t*)last
{
	if (buf[*i] == '?')
	if (buffer[*i] == '?')
		level = 1;
	else if (level == 1 && buf[*i] == '>') {
	else if (level == 1 && buffer[*i] == '>') {
		OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
		OFMutableString *pi;
		size_t len;

		[cache appendCStringWithoutUTF8Checking: buf + *last
		[cache appendCStringWithoutUTF8Checking: buffer + *last
						 length: *i - *last];
		pi = [[cache mutableCopy] autorelease];
		len = [pi length];

		[pi deleteCharactersFromIndex: len - 1
				      toIndex: len];

476
477
478
479
480
481
482
483

484
485
486
487
488


489
490
491


492
493
494
495
496



497
498
499


500
501

502
503
504
505
506





507
508
509
510
511
512

513
514
515
516
517
518
519
475
476
477
478
479
480
481

482
483
484
485


486
487
488


489
490
491
492



493
494
495
496


497
498
499

500
501




502
503
504
505
506
507
508
509
510
511

512
513
514
515
516
517
518
519







-
+



-
-
+
+

-
-
+
+


-
-
-
+
+
+

-
-
+
+

-
+

-
-
-
-
+
+
+
+
+





-
+







		*last = *i + 1;
		state = OF_XMLPARSER_OUTSIDE_TAG;
	} else
		level = 0;
}

/* Inside a tag, no name yet */
- (void)_parseInTagNameWithBuffer: (const char*)buf
- (void)_parseInTagNameWithBuffer: (const char*)buffer
				i: (size_t*)i
			     last: (size_t*)last
{
	const char *cache_c, *tmp;
	size_t len, cache_len;
	const char *cacheCString, *tmp;
	size_t length, cacheLength;

	if (buf[*i] != ' ' && buf[*i] != '\t' && buf[*i] != '\n' &&
	    buf[*i] != '\r' && buf[*i] != '>' && buf[*i] != '/')
	if (buffer[*i] != ' ' && buffer[*i] != '\t' && buffer[*i] != '\n' &&
	    buffer[*i] != '\r' && buffer[*i] != '>' && buffer[*i] != '/')
		return;

	if ((len = *i - *last) > 0)
		[cache appendCStringWithoutUTF8Checking: buf + *last
						 length: len];
	if ((length = *i - *last) > 0)
		[cache appendCStringWithoutUTF8Checking: buffer + *last
						 length: length];

	cache_c = [cache cString];
	cache_len = [cache cStringLength];
	cacheCString = [cache cString];
	cacheLength = [cache cStringLength];

	if ((tmp = memchr(cache_c, ':', cache_len)) != NULL) {
	if ((tmp = memchr(cacheCString, ':', cacheLength)) != NULL) {
		name = [[OFString alloc] initWithCString: tmp + 1
						  length: cache_len -
							  (tmp - cache_c) - 1];
		prefix = [[OFString alloc] initWithCString: cache_c
						    length: tmp - cache_c];
						  length: cacheLength -
							  (tmp - cacheCString) -
							  1];
		prefix = [[OFString alloc] initWithCString: cacheCString
						    length: tmp - cacheCString];
	} else {
		name = [cache copy];
		prefix = nil;
	}

	if (buf[*i] == '>' || buf[*i] == '/') {
	if (buffer[*i] == '>' || buffer[*i] == '/') {
		OFAutoreleasePool *pool;
		OFString *ns;

		ns = namespace_for_prefix(prefix, namespaces);

		if (prefix != nil && ns == nil)
			@throw
529
530
531
532
533
534
535
536

537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555

556
557
558
559
560
561

562
563
564
565
566
567
568
569
570
571
572
573
574

575
576
577
578
579
580


581
582
583
584


585
586
587
588
589
590
591





592
593

594
595
596
597
598





599
600
601
602
603
604
605
529
530
531
532
533
534
535

536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554

555
556
557
558
559
560

561
562
563
564
565
566
567
568
569
570
571
572
573

574
575
576
577
578


579
580
581
582


583
584
585
586





587
588
589
590
591
592

593
594




595
596
597
598
599
600
601
602
603
604
605
606







-
+


















-
+





-
+












-
+




-
-
+
+


-
-
+
+


-
-
-
-
-
+
+
+
+
+

-
+

-
-
-
-
+
+
+
+
+







#endif
			[delegate parser: self
			 didStartElement: name
			      withPrefix: prefix
			       namespace: ns
			      attributes: nil];

		if (buf[*i] == '/') {
		if (buffer[*i] == '/') {
#if defined(OF_HAVE_PROPERTIES) && defined(OF_HAVE_BLOCKS)
			if (elementEndHandler != NULL)
				elementEndHandler(self, name, prefix, ns);
			else
#endif
				[delegate parser: self
				   didEndElement: name
				      withPrefix: prefix
				       namespace: ns];
		} else
			[previous addObject: [[cache copy] autorelease]];

			[pool release];

			[name release];
			[prefix release];
			name = prefix = nil;

			state = (buf[*i] == '/'
			state = (buffer[*i] == '/'
			    ? OF_XMLPARSER_EXPECT_CLOSE
			    : OF_XMLPARSER_OUTSIDE_TAG);
	} else
		state = OF_XMLPARSER_IN_TAG;

	if (buf[*i] != '/') {
	if (buffer[*i] != '/') {
		OFAutoreleasePool *pool;

		pool = [[OFAutoreleasePool alloc] init];
		[namespaces addObject: [OFMutableDictionary dictionary]];
		[pool release];
	}

	[cache setToCString: ""];
	*last = *i + 1;
}

/* Inside a close tag, no name yet */
- (void)_parseInCloseTagNameWithBuffer: (const char*)buf
- (void)_parseInCloseTagNameWithBuffer: (const char*)buffer
				     i: (size_t*)i
				  last: (size_t*)last
{
	OFAutoreleasePool *pool;
	const char *cache_c, *tmp;
	size_t len, cache_len;
	const char *cacheCString, *tmp;
	size_t length, cacheLength;
	OFString *ns;

	if (buf[*i] != ' ' && buf[*i] != '\t' && buf[*i] != '\n' &&
	    buf[*i] != '\r' && buf[*i] != '>')
	if (buffer[*i] != ' ' && buffer[*i] != '\t' && buffer[*i] != '\n' &&
	    buffer[*i] != '\r' && buffer[*i] != '>')
		return;

	if ((len = *i - *last) > 0)
		[cache appendCStringWithoutUTF8Checking: buf + *last
						 length: len];
	cache_c = [cache cString];
	cache_len = [cache cStringLength];
	if ((length = *i - *last) > 0)
		[cache appendCStringWithoutUTF8Checking: buffer + *last
						 length: length];
	cacheCString = [cache cString];
	cacheLength = [cache cStringLength];

	if ((tmp = memchr(cache_c, ':', cache_len)) != NULL) {
	if ((tmp = memchr(cacheCString, ':', cacheLength)) != NULL) {
		name = [[OFString alloc] initWithCString: tmp + 1
						  length: cache_len -
							  (tmp - cache_c) - 1];
		prefix = [[OFString alloc] initWithCString: cache_c
						    length: tmp - cache_c];
						  length: cacheLength -
							  (tmp - cacheCString) -
							  1];
		prefix = [[OFString alloc] initWithCString: cacheCString
						    length: tmp - cacheCString];
	} else {
		name = [cache copy];
		prefix = nil;
	}

	if (![[previous lastObject] isEqual: cache])
		@throw [OFMalformedXMLException newWithClass: isa
630
631
632
633
634
635
636
637

638
639
640
641
642
643
644
645
646

647
648
649
650
651
652
653


654
655
656
657



658
659
660
661
662
663
664
665
666
667


668
669
670
671
672
673
674
675
676



677
678
679
680
681
682

683
684
685
686
687
688
689

690
691

692
693
694
695
696
697
698
631
632
633
634
635
636
637

638
639
640
641
642
643
644
645
646

647
648
649
650
651
652


653
654
655



656
657
658
659
660
661
662
663
664
665
666


667
668
669
670
671
672
673
674
675


676
677
678
679
680
681
682
683

684
685
686
687
688
689
690

691
692

693
694
695
696
697
698
699
700







-
+








-
+





-
-
+
+

-
-
-
+
+
+








-
-
+
+







-
-
+
+
+





-
+






-
+

-
+








	[namespaces removeNObjects: 1];
	[name release];
	[prefix release];
	name = prefix = nil;

	*last = *i + 1;
	state = (buf[*i] == '>'
	state = (buffer[*i] == '>'
	    ? OF_XMLPARSER_OUTSIDE_TAG
	    : OF_XMLPARSER_EXPECT_SPACE_OR_CLOSE);

	if ([previous count] == 0)
		finishedParsing = YES;
}

/* Inside a tag, name found */
- (void)_parseInTagWithBuffer: (const char*)buf
- (void)_parseInTagWithBuffer: (const char*)buffer
			    i: (size_t*)i
			 last: (size_t*)last
{
	OFAutoreleasePool *pool;
	OFString *ns;
	OFXMLAttribute **attrs_c;
	size_t j, attrs_cnt;
	OFXMLAttribute **attributesCArray;
	size_t j, attributesCount;

	if (buf[*i] != '>' && buf[*i] != '/') {
		if (buf[*i] != ' ' && buf[*i] != '\t' && buf[*i] != '\n' &&
		    buf[*i] != '\r') {
	if (buffer[*i] != '>' && buffer[*i] != '/') {
		if (buffer[*i] != ' ' && buffer[*i] != '\t' &&
		    buffer[*i] != '\n' && buffer[*i] != '\r') {
			*last = *i;
			state = OF_XMLPARSER_IN_ATTR_NAME;
			(*i)--;
		}

		return;
	}

	attrs_c = [attrs cArray];
	attrs_cnt = [attrs count];
	attributesCArray = [attributes cArray];
	attributesCount = [attributes count];

	ns = namespace_for_prefix(prefix, namespaces);

	if (prefix != nil && ns == nil)
		@throw [OFUnboundNamespaceException newWithClass: isa
							  prefix: prefix];

	for (j = 0; j < attrs_cnt; j++)
		resolve_attr_namespace(attrs_c[j], prefix, ns, namespaces, isa);
	for (j = 0; j < attributesCount; j++)
		resolve_attribute_namespace(attributesCArray[j], prefix, ns,
		    namespaces, isa);

	pool = [[OFAutoreleasePool alloc] init];

#if defined(OF_HAVE_PROPERTIES) && defined(OF_HAVE_BLOCKS)
	if (elementStartHandler != NULL)
		elementStartHandler(self, name, prefix, ns, attrs);
		elementStartHandler(self, name, prefix, ns, attributes);
	else
#endif
		[delegate parser: self
		 didStartElement: name
		      withPrefix: prefix
		       namespace: ns
		      attributes: attrs];
		      attributes: attributes];

	if (buf[*i] == '/') {
	if (buffer[*i] == '/') {
#if defined(OF_HAVE_PROPERTIES) && defined(OF_HAVE_BLOCKS)
		if (elementEndHandler != NULL)
			elementEndHandler(self, name, prefix, ns);
		else
#endif
			[delegate parser: self
			   didEndElement: name
707
708
709
710
711
712
713
714

715
716

717
718
719

720
721
722
723
724
725

726
727
728
729
730


731
732

733
734
735
736
737



738
739
740
741


742
743
744
745




746
747
748
749



750
751
752


753
754
755
756
757
758
759
760
761
762

763
764
765
766
767
768
769


770
771
772

773
774
775
776

777
778
779
780
781

782
783
784
785
786
787


788
789

790
791
792
793
794



795
796
797

798
799
800


801
802
803
804



805
806
807


808

809
810
811



812
813
814
815
816
817
818



819
820
821
822
823
824
825

826
827
828
829

830
831
832
833
834
835
836
837
838

839
840
841
842

843
844
845
846


847
848
849
850
851
852

853
854
855
856

857
858
859
860

861
862

863
864
865

866
867
868
869
870
871
872
873
874
875
876

877
878
879
880

881
882
883
884
885
886
887
888
889
890
891
892

893
894
895
896

897
898
899
900
901
902
903
904
905

906
907
908
909
910
911


912
913

914
915

916
917
918
919
920
921
922

923
924
925


926
927
928


929
930
931
932
933
934
935

936
937
938
939

940
941
942
943

944
945
946
947
948
949
950
951
952
953
954

955
956
957
958

959
960
961
962
963
964
965
966
967

968
969
970
971

972
973
974
975
976
977
978
979
980

981
982
983
984
985
986

987
988

989
990
991
992
993
994

995
996
997

998
999
1000


1001
1002
1003
1004
1005
1006
1007
709
710
711
712
713
714
715

716
717

718
719
720

721
722
723
724
725
726

727
728
729
730


731
732
733

734
735
736



737
738
739
740
741


742
743
744



745
746
747
748




749
750
751
752


753
754
755
756
757
758
759
760
761
762
763

764
765
766
767
768
769


770
771
772
773

774
775
776
777

778
779
780
781
782

783
784
785
786
787


788
789
790

791
792
793



794
795
796
797
798

799
800


801
802
803



804
805
806
807


808
809
810
811



812
813
814
815
816
817
818



819
820
821
822
823
824
825
826
827

828
829
830
831

832
833
834
835
836
837
838
839
840

841
842
843
844

845
846
847


848
849
850
851
852
853
854

855
856
857
858

859
860
861
862

863
864

865
866
867

868
869
870
871
872
873
874
875
876
877
878

879
880
881
882

883
884
885
886
887
888
889
890
891
892
893
894

895
896
897
898

899
900
901
902
903
904
905
906
907

908
909
910
911
912


913
914
915

916
917

918
919
920
921
922
923
924

925
926


927
928
929


930
931
932
933
934
935
936
937

938
939
940
941

942
943
944
945

946
947
948
949
950
951
952
953
954
955
956

957
958
959
960

961
962
963
964
965
966
967
968
969

970
971
972
973

974
975
976
977
978
979
980
981
982

983
984
985
986
987
988

989
990

991
992
993
994
995
996

997
998
999

1000
1001


1002
1003
1004
1005
1006
1007
1008
1009
1010







-
+

-
+


-
+





-
+



-
-
+
+

-
+


-
-
-
+
+
+


-
-
+
+

-
-
-
+
+
+
+
-
-
-
-
+
+
+

-
-
+
+









-
+





-
-
+
+


-
+



-
+




-
+




-
-
+
+

-
+


-
-
-
+
+
+


-
+

-
-
+
+

-
-
-
+
+
+

-
-
+
+

+
-
-
-
+
+
+




-
-
-
+
+
+






-
+



-
+








-
+



-
+


-
-
+
+





-
+



-
+



-
+

-
+


-
+










-
+



-
+











-
+



-
+








-
+




-
-
+
+

-
+

-
+






-
+

-
-
+
+

-
-
+
+






-
+



-
+



-
+










-
+



-
+








-
+



-
+








-
+





-
+

-
+





-
+


-
+

-
-
+
+







	} else
		[previous addObject: name];

	[pool release];

	[name release];
	[prefix release];
	[attrs release];
	[attributes release];
	name = prefix = nil;
	attrs = nil;
	attributes = nil;

	*last = *i + 1;
	state = (buf[*i] == '/'
	state = (buffer[*i] == '/'
	    ? OF_XMLPARSER_EXPECT_CLOSE
	    : OF_XMLPARSER_OUTSIDE_TAG);
}

/* Looking for attribute name */
- (void)_parseInAttributeNameWithBuffer: (const char*)buf
- (void)_parseInAttributeNameWithBuffer: (const char*)buffer
				      i: (size_t*)i
				   last: (size_t*)last
{
	const char *cache_c, *tmp;
	size_t len, cache_len;
	const char *cacheCString, *tmp;
	size_t length, cacheLength;

	if (buf[*i] != '=')
	if (buffer[*i] != '=')
		return;

	if ((len = *i - *last) > 0)
		[cache appendCStringWithoutUTF8Checking: buf + *last
						 length: len];
	if ((length = *i - *last) > 0)
		[cache appendCStringWithoutUTF8Checking: buffer + *last
						 length: length];

	[cache deleteLeadingAndTrailingWhitespaces];
	cache_c = [cache cString];
	cache_len = [cache cStringLength];
	cacheCString = [cache cString];
	cacheLength = [cache cStringLength];

	if ((tmp = memchr(cache_c, ':', cache_len)) != NULL) {
		attrName = [[OFString alloc] initWithCString: tmp + 1
						      length: cache_len -
	if ((tmp = memchr(cacheCString, ':', cacheLength)) != NULL) {
		attributeName = [[OFString alloc]
		    initWithCString: tmp + 1
			     length: cacheLength - (tmp - cacheCString) - 1];
							      (tmp - cache_c) -
							      1];
		attrPrefix = [[OFString alloc] initWithCString: cache_c
							length: tmp - cache_c];
		attributePrefix = [[OFString alloc]
		    initWithCString: cacheCString
			     length: tmp - cacheCString];
	} else {
		attrName = [cache copy];
		attrPrefix = nil;
		attributeName = [cache copy];
		attributePrefix = nil;
	}

	[cache setToCString: ""];

	*last = *i + 1;
	state = OF_XMLPARSER_EXPECT_DELIM;
}

/* Expecting delimiter */
- (void)_parseExpectDelimiterWithBuffer: (const char*)buf
- (void)_parseExpectDelimiterWithBuffer: (const char*)buffer
				      i: (size_t*)i
				   last: (size_t*)last
{
	*last = *i + 1;

	if (buf[*i] == ' ' || buf[*i] == '\t' || buf[*i] == '\n' ||
	    buf[*i] == '\r')
	if (buffer[*i] == ' ' || buffer[*i] == '\t' || buffer[*i] == '\n' ||
	    buffer[*i] == '\r')
		return;

	if (buf[*i] != '\'' && buf[*i] != '"')
	if (buffer[*i] != '\'' && buffer[*i] != '"')
		@throw [OFMalformedXMLException newWithClass: isa
						      parser: self];

	delim = buf[*i];
	delimiter = buffer[*i];
	state = OF_XMLPARSER_IN_ATTR_VALUE;
}

/* Looking for attribute value */
- (void)_parseInAttributeValueWithBuffer: (const char*)buf
- (void)_parseInAttributeValueWithBuffer: (const char*)buffer
				       i: (size_t*)i
				    last: (size_t*)last
{
	OFAutoreleasePool *pool;
	OFString *attr_val;
	size_t len;
	OFString *attributeValue;
	size_t length;

	if (buf[*i] != delim)
	if (buffer[*i] != delimiter)
		return;

	if ((len = *i - *last) > 0)
		[cache appendCStringWithoutUTF8Checking: buf + *last
						 length: len];
	if ((length = *i - *last) > 0)
		[cache appendCStringWithoutUTF8Checking: buffer + *last
						 length: length];

	pool = [[OFAutoreleasePool alloc] init];
	attr_val = transform_string(cache, self);
	attributeValue = transform_string(cache, self);

	if (attrPrefix == nil && [attrName isEqual: @"xmlns"])
		[[namespaces lastObject] setObject: attr_val
	if (attributePrefix == nil && [attributeName isEqual: @"xmlns"])
		[[namespaces lastObject] setObject: attributeValue
					    forKey: @""];
	if ([attrPrefix isEqual: @"xmlns"])
		[[namespaces lastObject] setObject: attr_val
					    forKey: attrName];
	if ([attributePrefix isEqual: @"xmlns"])
		[[namespaces lastObject] setObject: attributeValue
					    forKey: attributeName];

	if (attrs == nil)
		attrs = [[OFMutableArray alloc] init];
	if (attributes == nil)
		attributes = [[OFMutableArray alloc] init];

	[attributes addObject:
	[attrs addObject: [OFXMLAttribute attributeWithName: attrName
						  namespace: attrPrefix
						stringValue: attr_val]];
	    [OFXMLAttribute attributeWithName: attributeName
				    namespace: attributePrefix
				  stringValue: attributeValue]];

	[pool release];

	[cache setToCString: ""];
	[attrName release];
	[attrPrefix release];
	attrName = attrPrefix = nil;
	[attributeName release];
	[attributePrefix release];
	attributeName = attributePrefix = nil;

	*last = *i + 1;
	state = OF_XMLPARSER_IN_TAG;
}

/* Expecting closing '>' */
- (void)_parseExpectCloseWithBuffer: (const char*)buf
- (void)_parseExpectCloseWithBuffer: (const char*)buffer
				  i: (size_t*)i
			       last: (size_t*)last
{
	if (buf[*i] == '>') {
	if (buffer[*i] == '>') {
		*last = *i + 1;
		state = OF_XMLPARSER_OUTSIDE_TAG;
	} else
		@throw [OFMalformedXMLException newWithClass: isa
						      parser: self];
}

/* Expecting closing '>' or space */
- (void)_parseExpectSpaceOrCloseWithBuffer: (const char*)buf
- (void)_parseExpectSpaceOrCloseWithBuffer: (const char*)buffer
					 i: (size_t*)i
				      last: (size_t*)last
{
	if (buf[*i] == '>') {
	if (buffer[*i] == '>') {
		*last = *i + 1;
		state = OF_XMLPARSER_OUTSIDE_TAG;
	} else if (buf[*i] != ' ' && buf[*i] != '\t' && buf[*i] != '\n' &&
	    buf[*i] != '\r')
	} else if (buffer[*i] != ' ' && buffer[*i] != '\t' &&
	    buffer[*i] != '\n' && buffer[*i] != '\r')
		@throw [OFMalformedXMLException newWithClass: isa
						      parser: self];
}

/* In <! */
- (void)_parseInExclamationMarkWithBuffer: (const char*)buf
- (void)_parseInExclamationMarkWithBuffer: (const char*)buffer
					i: (size_t*)i
				     last: (size_t*)last
{
	if (finishedParsing && buf[*i] != '-')
	if (finishedParsing && buffer[*i] != '-')
		@throw [OFMalformedXMLException newWithClass: isa
						      parser: self];

	if (buf[*i] == '-')
	if (buffer[*i] == '-')
		state = OF_XMLPARSER_IN_COMMENT_OPENING;
	else if (buf[*i] == '[') {
	else if (buffer[*i] == '[') {
		state = OF_XMLPARSER_IN_CDATA_OPENING;
		level = 0;
	} else if (buf[*i] == 'D') {
	} else if (buffer[*i] == 'D') {
		state = OF_XMLPARSER_IN_DOCTYPE;
		level = 0;
	} else
		@throw [OFMalformedXMLException newWithClass: isa
						      parser: self];

	*last = *i + 1;
}

/* CDATA */
- (void)_parseInCDATAOpeningWithBuffer: (const char*)buf
- (void)_parseInCDATAOpeningWithBuffer: (const char*)buffer
				     i: (size_t*)i
				  last: (size_t*)last
{
	if (buf[*i] != "CDATA["[level])
	if (buffer[*i] != "CDATA["[level])
		@throw [OFMalformedXMLException newWithClass: isa
						      parser: self];

	if (++level == 6) {
		state = OF_XMLPARSER_IN_CDATA_1;
		level = 0;
	}

	*last = *i + 1;
}

- (void)_parseInCDATA1WithBuffer: (const char*)buf
- (void)_parseInCDATA1WithBuffer: (const char*)buffer
			       i: (size_t*)i
			    last: (size_t*)last
{
	if (buf[*i] == ']')
	if (buffer[*i] == ']')
		level++;
	else
		level = 0;

	if (level == 2)
		state = OF_XMLPARSER_IN_CDATA_2;
}

- (void)_parseInCDATA2WithBuffer: (const char*)buf
- (void)_parseInCDATA2WithBuffer: (const char*)buffer
			       i: (size_t*)i
			    last: (size_t*)last
{
	OFAutoreleasePool *pool;
	OFMutableString *cdata;
	size_t len;
	OFMutableString *CDATA;
	size_t length;

	if (buf[*i] != '>') {
	if (buffer[*i] != '>') {
		state = OF_XMLPARSER_IN_CDATA_1;
		level = (buf[*i] == ']' ? 1 : 0);
		level = (buffer[*i] == ']' ? 1 : 0);

		return;
	}

	pool = [[OFAutoreleasePool alloc] init];

	[cache appendCStringWithoutUTF8Checking: buf + *last
	[cache appendCStringWithoutUTF8Checking: buffer + *last
					 length: *i - *last];
	cdata = [[cache mutableCopy] autorelease];
	len = [cdata length];
	CDATA = [[cache mutableCopy] autorelease];
	length = [CDATA length];

	[cdata deleteCharactersFromIndex: len - 2
				 toIndex: len];
	[CDATA deleteCharactersFromIndex: length - 2
				 toIndex: length];

	/*
	 * Class swizzle the string to be immutable. We pass it as OFString*, so
	 * it can't be modified anyway. But not swizzling it would create a
	 * real copy each time -[copy] is called.
	 */
	cdata->isa = [OFString class];
	CDATA->isa = [OFString class];

#if defined(OF_HAVE_PROPERTIES) && defined(OF_HAVE_BLOCKS)
	if (CDATAHandler != NULL)
		CDATAHandler(self, cdata);
		CDATAHandler(self, CDATA);
	else
#endif
		[delegate parser: self
		      foundCDATA: cdata];
		      foundCDATA: CDATA];

	[pool release];

	[cache setToCString: ""];

	*last = *i + 1;
	state = OF_XMLPARSER_OUTSIDE_TAG;
}

/* Comment */
- (void)_parseInCommentOpeningWithBuffer: (const char*)buf
- (void)_parseInCommentOpeningWithBuffer: (const char*)buffer
				       i: (size_t*)i
				    last: (size_t*)last
{
	if (buf[*i] != '-')
	if (buffer[*i] != '-')
		@throw [OFMalformedXMLException newWithClass: isa
						      parser: self];

	*last = *i + 1;
	state = OF_XMLPARSER_IN_COMMENT_1;
	level = 0;
}

- (void)_parseInComment1WithBuffer: (const char*)buf
- (void)_parseInComment1WithBuffer: (const char*)buffer
				 i: (size_t*)i
			      last: (size_t*)last
{
	if (buf[*i] == '-')
	if (buffer[*i] == '-')
		level++;
	else
		level = 0;

	if (level == 2)
		state = OF_XMLPARSER_IN_COMMENT_2;
}

- (void)_parseInComment2WithBuffer: (const char*)buf
- (void)_parseInComment2WithBuffer: (const char*)buffer
				 i: (size_t*)i
			      last: (size_t*)last
{
	OFAutoreleasePool *pool;
	OFMutableString *comment;
	size_t len;
	size_t length;

	if (buf[*i] != '>')
	if (buffer[*i] != '>')
		@throw [OFMalformedXMLException newWithClass: isa
						      parser: self];

	pool = [[OFAutoreleasePool alloc] init];

	[cache appendCStringWithoutUTF8Checking: buf + *last
	[cache appendCStringWithoutUTF8Checking: buffer + *last
					 length: *i - *last];
	comment = [[cache mutableCopy] autorelease];
	len = [comment length];
	length = [comment length];

	[comment deleteCharactersFromIndex: len - 2
				   toIndex: len];
	[comment deleteCharactersFromIndex: length - 2
				   toIndex: length];

	/*
	 * Class swizzle the string to be immutable. We pass it as OFString*, so
	 * it can't be modified anyway. But not swizzling it would create a
	 * real copy each time -[copy] is called.
	 */
	comment->isa = [OFString class];
1019
1020
1021
1022
1023
1024
1025
1026

1027
1028
1029
1030
1031
1032



1033
1034
1035
1036

1037
1038
1039

1040
1041
1042
1043
1044
1045
1046
1022
1023
1024
1025
1026
1027
1028

1029
1030
1031
1032



1033
1034
1035
1036
1037
1038

1039
1040
1041

1042
1043
1044
1045
1046
1047
1048
1049







-
+



-
-
-
+
+
+



-
+


-
+







	[cache setToCString: ""];

	*last = *i + 1;
	state = OF_XMLPARSER_OUTSIDE_TAG;
}

/* In <!DOCTYPE ...> */
- (void)_parseInDoctypeWithBuffer: (const char*)buf
- (void)_parseInDoctypeWithBuffer: (const char*)buffer
				i: (size_t*)i
			     last: (size_t*)last
{
	if ((level < 6 && buf[*i] != "OCTYPE"[level]) ||
	    (level == 6 && buf[*i] != ' ' && buf[*i] != '\t' &&
	    buf[*i] != '\n' && buf[*i] != '\r'))
	if ((level < 6 && buffer[*i] != "OCTYPE"[level]) ||
	    (level == 6 && buffer[*i] != ' ' && buffer[*i] != '\t' &&
	    buffer[*i] != '\n' && buffer[*i] != '\r'))
		@throw [OFMalformedXMLException newWithClass: isa
						      parser: self];

	if (level < 7 || buf[*i] == '<')
	if (level < 7 || buffer[*i] == '<')
		level++;

	if (buf[*i] == '>') {
	if (buffer[*i] == '>') {
		if (level == 7)
			state = OF_XMLPARSER_OUTSIDE_TAG;
		else
			level--;
	}

	*last = *i + 1;
1075
1076
1077
1078
1079
1080
1081
1082

1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094

1095
1096
1097
1098
1099

1100
1101
1102
1103
1104
1105
1106
1078
1079
1080
1081
1082
1083
1084

1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096

1097
1098
1099
1100
1101

1102
1103
1104
1105
1106
1107
1108
1109







-
+











-
+




-
+







{
}

-    (void)parser: (OFXMLParser*)parser
  didStartElement: (OFString*)name
       withPrefix: (OFString*)prefix
	namespace: (OFString*)ns
       attributes: (OFArray*)attrs
       attributes: (OFArray*)attributes
{
}

-  (void)parser: (OFXMLParser*)parser
  didEndElement: (OFString*)name
     withPrefix: (OFString*)prefix
      namespace: (OFString*)ns
{
}

-    (void)parser: (OFXMLParser*)parser
  foundCharacters: (OFString*)string
  foundCharacters: (OFString*)characters
{
}

- (void)parser: (OFXMLParser*)parser
    foundCDATA: (OFString*)cdata
    foundCDATA: (OFString*)CDATA
{
}

- (void)parser: (OFXMLParser*)parser
  foundComment: (OFString*)comment
{
}