ObjFW  Diff

Differences From Artifact [b0c5452c32]:

To Artifact [ca4fab60df]:


114
115
116
117
118
119
120
121

122
123
124
125
126
127
128
129
130
131
132
133
134


135
136
137
138
139
140
141
114
115
116
117
118
119
120

121
122
123
124
125
126
127
128
129
130
131
132


133
134
135
136
137
138
139
140
141







-
+











-
-
+
+







}

static OF_INLINE void
resolve_attribute_namespace(OFXMLAttribute *attribute, OFArray *namespaces,
    OFXMLParser *self)
{
	OFString *attributeNS;
	OFString *attributePrefix = attribute->ns;
	OFString *attributePrefix = attribute->_namespace;

	if (attributePrefix == nil)
		return;

	attributeNS = namespace_for_prefix(attributePrefix, namespaces);

	if ((attributePrefix != nil && attributeNS == nil))
		@throw [OFUnboundNamespaceException
		    exceptionWithClass: [self class]
				prefix: attributePrefix];

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

@implementation OFXMLParser
+ (void)initialize
{
	size_t i;

181
182
183
184
185
186
187
188
189
190
191




192
193
194
195
196
197

198
199
200
201
202




203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222








223
224
225
226
227
228
229

230
231
232

233
234

235
236
237
238
239

240
241
242

243
244

245
246
247
248
249
250
251
252
253
254
255

256
257
258
259
260
261
262
263


264
265

266
267
268
269
270


271
272
273
274
275
276
277
181
182
183
184
185
186
187




188
189
190
191
192
193
194
195
196

197
198




199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214








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

229
230
231

232
233

234
235
236
237
238

239
240
241

242
243

244
245
246
247
248
249
250
251
252
253
254

255
256
257
258
259
260
261


262
263
264

265
266
267
268


269
270
271
272
273
274
275
276
277







-
-
-
-
+
+
+
+





-
+

-
-
-
-
+
+
+
+












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






-
+


-
+

-
+




-
+


-
+

-
+










-
+






-
-
+
+

-
+



-
-
+
+







{
	self = [super init];

	@try {
		void *pool;
		OFMutableDictionary *dict;

		cache = [[OFBigDataArray alloc] init];
		previous = [[OFMutableArray alloc] init];
		namespaces = [[OFMutableArray alloc] init];
		attributes = [[OFMutableArray alloc] init];
		_cache = [[OFBigDataArray alloc] init];
		_previous = [[OFMutableArray alloc] init];
		_namespaces = [[OFMutableArray alloc] init];
		_attributes = [[OFMutableArray alloc] init];

		pool = objc_autoreleasePoolPush();
		dict = [OFMutableDictionary dictionaryWithKeysAndObjects:
		    @"xml", @"http://www.w3.org/XML/1998/namespace",
		    @"xmlns", @"http://www.w3.org/2000/xmlns/", nil];
		[namespaces addObject: dict];
		[_namespaces addObject: dict];

		acceptProlog = YES;
		lineNumber = 1;
		encoding = OF_STRING_ENCODING_UTF_8;
		depthLimit = 32;
		_acceptProlog = YES;
		_lineNumber = 1;
		_encoding = OF_STRING_ENCODING_UTF_8;
		_depthLimit = 32;

		objc_autoreleasePoolPop(pool);
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	[cache release];
	[name release];
	[prefix release];
	[namespaces release];
	[attributes release];
	[attributeName release];
	[attributePrefix release];
	[previous release];
	[_cache release];
	[_name release];
	[_prefix release];
	[_namespaces release];
	[_attributes release];
	[_attributeName release];
	[_attributePrefix release];
	[_previous release];

	[super dealloc];
}

- (id <OFXMLParserDelegate>)delegate
{
	return delegate;
	return _delegate;
}

- (void)setDelegate: (id <OFXMLParserDelegate>)delegate_
- (void)setDelegate: (id <OFXMLParserDelegate>)delegate
{
	delegate = delegate_;
	_delegate = delegate;
}

- (size_t)depthLimit
{
	return depthLimit;
	return _depthLimit;
}

- (void)setDepthLimit: (size_t)depthLimit_
- (void)setDepthLimit: (size_t)depthLimit
{
	depthLimit = depthLimit_;
	_depthLimit = depthLimit;
}

- (void)parseBuffer: (const char*)buffer
	     length: (size_t)length
{
	size_t i, last = 0;

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

		lookupTable[state](self, selectors[state], buffer, &i, &last);
		lookupTable[_state](self, selectors[_state], buffer, &i, &last);

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

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

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

	/* In OF_XMLPARSER_IN_TAG, there can be only spaces */
	if (length - last > 0 && state != OF_XMLPARSER_IN_TAG)
		cache_append(cache, buffer + last, encoding, length - last);
	if (length - last > 0 && _state != OF_XMLPARSER_IN_TAG)
		cache_append(_cache, buffer + last, _encoding, length - last);
}

- (void)parseString: (OFString*)string
{
	[self parseBuffer: [string UTF8String]
		   length: [string UTF8StringLength]];
}
315
316
317
318
319
320
321
322

323
324
325
326
327
328
329
330
331
332

333
334

335
336

337
338

339
340
341


342
343
344
345
346

347
348
349

350
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
454
455
456
457
458
459

460
461

462
463
464

465
466
467

468
469
470
471
472
473
474

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
520

521
522
523
524
525
526
527
528
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
607
608
609
610
611
612
613
614
615
616
617

618
619
620
621
622
623
624
625

626
627
628
629
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
699
700
701
702
703
704
705


706
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
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
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


1047
1048
1049
1050


1051
1052

1053
1054
1055
1056
1057
1058
1059
1060

1061
1062
1063
1064
1065

1066
1067
1068
1069
1070
1071

1072
1073
1074


1075
1076
1077
1078
315
316
317
318
319
320
321

322
323
324
325
326
327
328
329
330
331

332
333

334
335

336
337

338
339


340
341
342
343
344
345

346
347
348

349
350
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
454
455
456
457
458

459
460

461
462
463

464
465
466

467
468
469
470
471
472
473

474
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

520
521
522
523
524
525
526
527
528
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
607
608
609
610
611
612
613
614
615
616

617

618
619
620
621
622
623

624
625
626
627


628
629
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
699
700
701
702


703
704
705

706
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
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017


1018
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
1047


1048
1049
1050

1051
1052
1053
1054
1055
1056
1057
1058

1059
1060
1061
1062
1063

1064
1065
1066
1067
1068
1069

1070
1071


1072
1073
1074
1075
1076
1077







-
+









-
+

-
+

-
+

-
+

-
-
+
+




-
+


-
+







-
+






-
-
+
+



-
-
+
+



-
-
+
+


-
+




-
-
+
+










-
+




-
+


-
+








-
+






-
+











-
+








-
+


















-
+

-
+


-
+


-
+






-
+





-
+











-
-
+
+

-
+

-
-
+
+

-
-
-
-
+
+
+
+




-
+

-
-
+
+



-
+


-
+

-
+

















-
+



-
-
+
+




-
+


-
+



-
-
+
+



-
+

-
+

-
+


-
+

-
+

-
-
-
-
-
+
+
+
+
+


-
+

-
-
-
-
+
+
+
+

-
-
+
+

-
+

-
-
-
+
+
+

-
+



-
+


-
+



-
+











-
+
-






-
+



-
-
+
+




-
+


-
+



-
-
+
+


-
+



-
+

-
+

-
-
+
+


-
+

-
+

-
-
-
-
+
+
+
+



-
-
-
-
+
+
+
+


-
+



-
-
+
+








-
+







-
+






-
-
+
+

-
+

-
+


-
+


-
+




-
+

-
-
-
-
-
+
+
+
+
+


-
+

-
-
-
-
+
+
+
+

-
-
+
+

-
-
+
+

-
-
+
+

-
+



-
-
-
-
+
+
+
+


-
+


















-
+



-
-
+
+








-
+


-
+



-
-
+
+




-
+


-
+

















-
-
+
+











-
+



-
+


-
+

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

-
-
-
+
+
+




-
-
-
-
+
+
+
+


-
+









-
+












-
+











-
+




-
+

-
-
+
+

-
-
+
+












-
+



-
-
-
+
+
+










-
+

-
+

-
-
+
+










-
-
+
+






-
-
+
+

-
-
-
+
+
+



-
+


-
+












-
-
+
+







-
+

-
+

-
-
+
+















-
-
+
+

-
-
-
+
+
+



-
+


-
+







-
-
+
+




-
-
+
+


-
-
+
+

-
+







-
+




-
+





-
+

-
-
+
+




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

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

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

	if ((length = *i - *last) > 0)
		cache_append(cache, buffer + *last, encoding, length);
		cache_append(_cache, buffer + *last, _encoding, length);

	if ([cache count] > 0) {
	if ([_cache count] > 0) {
		void *pool = objc_autoreleasePoolPush();
		OFString *characters = transform_string(cache, 0, YES, self);
		OFString *characters = transform_string(_cache, 0, YES, self);

		if ([delegate respondsToSelector:
		if ([_delegate respondsToSelector:
		    @selector(parser:foundCharacters:)])
			[delegate    parser: self
			    foundCharacters: characters];
			[_delegate parser: self
			  foundCharacters: characters];

		objc_autoreleasePoolPop(pool);
	}

	[cache removeAllItems];
	[_cache removeAllItems];

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

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

	switch (buffer[*i]) {
	case '?':
		*last = *i + 1;
		state = OF_XMLPARSER_IN_PROCESSING_INSTRUCTIONS;
		level = 0;
		_state = OF_XMLPARSER_IN_PROCESSING_INSTRUCTIONS;
		_level = 0;
		break;
	case '/':
		*last = *i + 1;
		state = OF_XMLPARSER_IN_CLOSE_TAG_NAME;
		acceptProlog = NO;
		_state = OF_XMLPARSER_IN_CLOSE_TAG_NAME;
		_acceptProlog = NO;
		break;
	case '!':
		*last = *i + 1;
		state = OF_XMLPARSER_IN_EXCLAMATIONMARK;
		acceptProlog = NO;
		_state = OF_XMLPARSER_IN_EXCLAMATIONMARK;
		_acceptProlog = NO;
		break;
	default:
		if (depthLimit > 0 && [previous count] >= depthLimit)
		if (_depthLimit > 0 && [_previous count] >= _depthLimit)
			@throw [OFMalformedXMLException
			    exceptionWithClass: [self class]
					parser: self];

		state = OF_XMLPARSER_IN_TAG_NAME;
		acceptProlog = NO;
		_state = OF_XMLPARSER_IN_TAG_NAME;
		_acceptProlog = NO;
		(*i)--;
		break;
	}
}

/* <?xml […]?> */
- (BOOL)OF_parseXMLProcessingInstructions: (OFString*)pi
{
	const char *cString;
	size_t i, last, length;
	int piState = 0;
	int PIState = 0;
	OFString *attribute = nil;
	OFMutableString *value = nil;
	char piDelimiter = 0;

	if (!acceptProlog)
	if (!_acceptProlog)
		return NO;

	acceptProlog = NO;
	_acceptProlog = NO;

	pi = [pi substringWithRange: of_range(3, [pi length] - 3)];
	pi = [pi stringByDeletingEnclosingWhitespaces];

	cString = [pi UTF8String];
	length = [pi UTF8StringLength];

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

			last = i;
			piState = 1;
			PIState = 1;
			i--;

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

			attribute = [OFString
			    stringWithUTF8String: cString + last
					  length: i - last];
			last = i + 1;
			piState = 2;
			PIState = 2;

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

			piDelimiter = cString[i];
			last = i + 1;
			piState = 3;
			PIState = 3;

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

			value = [OFMutableString
			    stringWithUTF8String: cString + last
					  length: i - last];

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

			if ([attribute isEqual: @"encoding"]) {
				[value lowercase];

				if ([value isEqual: @"utf-8"])
					encoding = OF_STRING_ENCODING_UTF_8;
					_encoding = OF_STRING_ENCODING_UTF_8;
				else if ([value isEqual: @"iso-8859-1"])
					encoding =
					_encoding =
					    OF_STRING_ENCODING_ISO_8859_1;
				else if ([value isEqual: @"iso-8859-15"])
					encoding =
					_encoding =
					    OF_STRING_ENCODING_ISO_8859_15;
				else if ([value isEqual: @"windows-1252"])
					encoding =
					_encoding =
					    OF_STRING_ENCODING_WINDOWS_1252;
				else
					return NO;
			}

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

			break;
		}
	}

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

	return YES;
}

/* Inside processing instructions */
- (void)OF_parseInProcessingInstructionsWithBuffer: (const char*)buffer
						 i: (size_t*)i
					      last: (size_t*)last
{
	if (buffer[*i] == '?')
		level = 1;
	else if (level == 1 && buffer[*i] == '>') {
		_level = 1;
	else if (_level == 1 && buffer[*i] == '>') {
		void *pool = objc_autoreleasePoolPush();
		OFString *pi;
		OFString *PI;

		cache_append(cache, buffer + *last, encoding, *i - *last);
		pi = transform_string(cache, 1, NO, nil);
		cache_append(_cache, buffer + *last, _encoding, *i - *last);
		PI = transform_string(_cache, 1, NO, nil);

		if ([pi isEqual: @"xml"] || [pi hasPrefix: @"xml "] ||
		    [pi hasPrefix: @"xml\t"] || [pi hasPrefix: @"xml\r"] ||
		    [pi hasPrefix: @"xml\n"])
			if (![self OF_parseXMLProcessingInstructions: pi])
		if ([PI isEqual: @"xml"] || [PI hasPrefix: @"xml "] ||
		    [PI hasPrefix: @"xml\t"] || [PI hasPrefix: @"xml\r"] ||
		    [PI hasPrefix: @"xml\n"])
			if (![self OF_parseXMLProcessingInstructions: PI])
				@throw [OFMalformedXMLException
				    exceptionWithClass: [self class]
						parser: self];

		if ([delegate respondsToSelector:
		if ([_delegate respondsToSelector:
		    @selector(parser:foundProcessingInstructions:)])
			[delegate		 parser: self
			    foundProcessingInstructions: pi];
			[_delegate		 parser: self
			    foundProcessingInstructions: PI];

		objc_autoreleasePoolPop(pool);

		[cache removeAllItems];
		[_cache removeAllItems];

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

/* Inside a tag, no name yet */
- (void)OF_parseInTagNameWithBuffer: (const char*)buffer
				  i: (size_t*)i
			       last: (size_t*)last
{
	void *pool;
	const char *cacheCString, *tmp;
	size_t length, cacheLength;
	OFString *cacheString;

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

	if ((length = *i - *last) > 0)
		cache_append(cache, buffer + *last, encoding, length);
		cache_append(_cache, buffer + *last, _encoding, length);

	pool = objc_autoreleasePoolPush();

	cacheCString = [cache items];
	cacheLength = [cache count];
	cacheCString = [_cache items];
	cacheLength = [_cache count];
	cacheString = [OFString stringWithUTF8String: cacheCString
					      length: cacheLength];

	if ((tmp = memchr(cacheCString, ':', cacheLength)) != NULL) {
		name = [[OFString alloc]
		_name = [[OFString alloc]
		    initWithUTF8String: tmp + 1
				length: cacheLength - (tmp - cacheCString) - 1];
		prefix = [[OFString alloc]
		_prefix = [[OFString alloc]
		    initWithUTF8String: cacheCString
				length: tmp - cacheCString];
	} else {
		name = [cacheString copy];
		prefix = nil;
		_name = [cacheString copy];
		_prefix = nil;
	}

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

		ns = namespace_for_prefix(prefix, namespaces);
		namespace = namespace_for_prefix(_prefix, _namespaces);

		if (prefix != nil && ns == nil)
		if (_prefix != nil && namespace == nil)
			@throw [OFUnboundNamespaceException
			    exceptionWithClass: [self class]
					prefix: prefix];
					prefix: _prefix];

		if ([delegate respondsToSelector: @selector(parser:
		if ([_delegate respondsToSelector: @selector(parser:
		    didStartElement:prefix:namespace:attributes:)])
			[delegate    parser: self
			    didStartElement: name
				     prefix: prefix
				  namespace: ns
				 attributes: nil];
			[_delegate parser: self
			  didStartElement: _name
				   prefix: _prefix
				namespace: namespace
			       attributes: nil];

		if (buffer[*i] == '/') {
			if ([delegate respondsToSelector:
			if ([_delegate respondsToSelector:
			    @selector(parser:didEndElement:prefix:namespace:)])
				[delegate  parser: self
				    didEndElement: name
					   prefix: prefix
					namespace: ns];
				[_delegate parser: self
				    didEndElement: _name
					   prefix: _prefix
					namespace: namespace];

			if ([previous count] == 0)
				finishedParsing = YES;
			if ([_previous count] == 0)
				_finishedParsing = YES;
		} else
			[previous addObject: cacheString];
			[_previous addObject: cacheString];

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

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

	if (buffer[*i] != '/')
		[namespaces addObject: [OFMutableDictionary dictionary]];
		[_namespaces addObject: [OFMutableDictionary dictionary]];

	objc_autoreleasePoolPop(pool);

	[cache removeAllItems];
	[_cache removeAllItems];
	*last = *i + 1;
}

/* Inside a close tag, no name yet */
- (void)OF_parseInCloseTagNameWithBuffer: (const char*)buffer
				       i: (size_t*)i
				    last: (size_t*)last
{
	void *pool;
	const char *cacheCString, *tmp;
	size_t length, cacheLength;
	OFString *cacheString;
	OFString *cacheString, *namespace;
	OFString *ns;

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

	if ((length = *i - *last) > 0)
		cache_append(cache, buffer + *last, encoding, length);
		cache_append(_cache, buffer + *last, _encoding, length);

	pool = objc_autoreleasePoolPush();

	cacheCString = [cache items];
	cacheLength = [cache count];
	cacheCString = [_cache items];
	cacheLength = [_cache count];
	cacheString = [OFString stringWithUTF8String: cacheCString
					      length: cacheLength];

	if ((tmp = memchr(cacheCString, ':', cacheLength)) != NULL) {
		name = [[OFString alloc]
		_name = [[OFString alloc]
		    initWithUTF8String: tmp + 1
				length: cacheLength - (tmp - cacheCString) - 1];
		prefix = [[OFString alloc]
		_prefix = [[OFString alloc]
		    initWithUTF8String: cacheCString
				length: tmp - cacheCString];
	} else {
		name = [cacheString copy];
		prefix = nil;
		_name = [cacheString copy];
		_prefix = nil;
	}

	if (![[previous lastObject] isEqual: cacheString])
	if (![[_previous lastObject] isEqual: cacheString])
		@throw [OFMalformedXMLException exceptionWithClass: [self class]
							    parser: self];

	[previous removeLastObject];
	[_previous removeLastObject];

	[cache removeAllItems];
	[_cache removeAllItems];

	ns = namespace_for_prefix(prefix, namespaces);
	if (prefix != nil && ns == nil)
	namespace = namespace_for_prefix(_prefix, _namespaces);
	if (_prefix != nil && namespace == nil)
		@throw [OFUnboundNamespaceException
		    exceptionWithClass: [self class]
				prefix: prefix];
				prefix: _prefix];

	if ([delegate respondsToSelector:
	if ([_delegate respondsToSelector:
	    @selector(parser:didEndElement:prefix:namespace:)])
		[delegate  parser: self
		    didEndElement: name
			   prefix: prefix
			namespace: ns];
		[_delegate parser: self
		    didEndElement: _name
			   prefix: _prefix
			namespace: namespace];

	objc_autoreleasePoolPop(pool);

	[namespaces removeLastObject];
	[name release];
	[prefix release];
	name = prefix = nil;
	[_namespaces removeLastObject];
	[_name release];
	[_prefix release];
	_name = _prefix = nil;

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

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

/* Inside a tag, name found */
- (void)OF_parseInTagWithBuffer: (const char*)buffer
			      i: (size_t*)i
			   last: (size_t*)last
{
	void *pool;
	OFString *ns;
	OFString *namespace;
	OFXMLAttribute **attributesObjects;
	size_t j, attributesCount;

	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;
			_state = OF_XMLPARSER_IN_ATTR_NAME;
			(*i)--;
		}

		return;
	}

	attributesObjects = [attributes objects];
	attributesCount = [attributes count];
	attributesObjects = [_attributes objects];
	attributesCount = [_attributes count];

	ns = namespace_for_prefix(prefix, namespaces);
	namespace = namespace_for_prefix(_prefix, _namespaces);

	if (prefix != nil && ns == nil)
	if (_prefix != nil && namespace == nil)
		@throw [OFUnboundNamespaceException
		    exceptionWithClass: [self class]
				prefix: prefix];
				prefix: _prefix];

	for (j = 0; j < attributesCount; j++)
		resolve_attribute_namespace(attributesObjects[j], namespaces,
		resolve_attribute_namespace(attributesObjects[j], _namespaces,
		    self);

	pool = objc_autoreleasePoolPush();

	if ([delegate respondsToSelector:
	if ([_delegate respondsToSelector:
	    @selector(parser:didStartElement:prefix:namespace:attributes:)])
		[delegate    parser: self
		    didStartElement: name
			     prefix: prefix
			  namespace: ns
			 attributes: attributes];
		[_delegate parser: self
		  didStartElement: _name
			   prefix: _prefix
			namespace: namespace
		       attributes: _attributes];

	if (buffer[*i] == '/') {
		if ([delegate respondsToSelector:
		if ([_delegate respondsToSelector:
		    @selector(parser:didEndElement:prefix:namespace:)])
			[delegate  parser: self
			    didEndElement: name
				   prefix: prefix
				namespace: ns];
			[_delegate parser: self
			    didEndElement: _name
				   prefix: _prefix
				namespace: namespace];

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

		[namespaces removeLastObject];
	} else if (prefix != nil) {
		[_namespaces removeLastObject];
	} else if (_prefix != nil) {
		OFString *str = [OFString stringWithFormat: @"%@:%@",
							    prefix, name];
		[previous addObject: str];
							    _prefix, _name];
		[_previous addObject: str];
	} else
		[previous addObject: name];
		[_previous addObject: _name];

	objc_autoreleasePoolPop(pool);

	[name release];
	[prefix release];
	[attributes removeAllObjects];
	name = prefix = nil;
	[_name release];
	[_prefix release];
	[_attributes removeAllObjects];
	_name = _prefix = nil;

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

/* Looking for attribute name */
- (void)OF_parseInAttributeNameWithBuffer: (const char*)buffer
					i: (size_t*)i
				     last: (size_t*)last
{
	void *pool;
	OFMutableString *cacheString;
	const char *cacheCString, *tmp;
	size_t length, cacheLength;

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

	if ((length = *i - *last) > 0)
		cache_append(cache, buffer + *last, encoding, length);
		cache_append(_cache, buffer + *last, _encoding, length);

	pool = objc_autoreleasePoolPush();

	cacheString = [OFMutableString stringWithUTF8String: [cache items]
						     length: [cache count]];
	cacheString = [OFMutableString stringWithUTF8String: [_cache items]
						     length: [_cache count]];
	[cacheString deleteEnclosingWhitespaces];
	/* Prevent a useless copy later */
	[cacheString makeImmutable];

	cacheCString = [cacheString UTF8String];
	cacheLength = [cacheString UTF8StringLength];

	if ((tmp = memchr(cacheCString, ':', cacheLength)) != NULL) {
		attributeName = [[OFString alloc]
		_attributeName = [[OFString alloc]
		    initWithUTF8String: tmp + 1
				length: cacheLength - (tmp - cacheCString) - 1];
		attributePrefix = [[OFString alloc]
		_attributePrefix = [[OFString alloc]
		    initWithUTF8String: cacheCString
				length: tmp - cacheCString];
	} else {
		attributeName = [cacheString copy];
		attributePrefix = nil;
		_attributeName = [cacheString copy];
		_attributePrefix = nil;
	}

	objc_autoreleasePoolPop(pool);

	[cache removeAllItems];
	[_cache removeAllItems];

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

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

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

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

	delimiter = buffer[*i];
	state = OF_XMLPARSER_IN_ATTR_VALUE;
	_delimiter = buffer[*i];
	_state = OF_XMLPARSER_IN_ATTR_VALUE;
}

/* Looking for attribute value */
- (void)OF_parseInAttributeValueWithBuffer: (const char*)buffer
					 i: (size_t*)i
				      last: (size_t*)last
{
	void *pool;
	OFString *attributeValue;
	size_t length;

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

	if ((length = *i - *last) > 0)
		cache_append(cache, buffer + *last, encoding, length);
		cache_append(_cache, buffer + *last, _encoding, length);

	pool = objc_autoreleasePoolPush();
	attributeValue = transform_string(cache, 0, YES, self);
	attributeValue = transform_string(_cache, 0, YES, self);

	if (attributePrefix == nil && [attributeName isEqual: @"xmlns"])
		[[namespaces lastObject] setObject: attributeValue
					    forKey: @""];
	if ([attributePrefix isEqual: @"xmlns"])
		[[namespaces lastObject] setObject: attributeValue
					    forKey: attributeName];
	if (_attributePrefix == nil && [_attributeName isEqual: @"xmlns"])
		[[_namespaces lastObject] setObject: attributeValue
					     forKey: @""];
	if ([_attributePrefix isEqual: @"xmlns"])
		[[_namespaces lastObject] setObject: attributeValue
					     forKey: _attributeName];

	[attributes addObject:
	    [OFXMLAttribute attributeWithName: attributeName
				    namespace: attributePrefix
	[_attributes addObject:
	    [OFXMLAttribute attributeWithName: _attributeName
				    namespace: _attributePrefix
				  stringValue: attributeValue]];

	objc_autoreleasePoolPop(pool);

	[cache removeAllItems];
	[attributeName release];
	[attributePrefix release];
	attributeName = attributePrefix = nil;
	[_cache removeAllItems];
	[_attributeName release];
	[_attributePrefix release];
	_attributeName = _attributePrefix = nil;

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

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

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

/* In <! */
- (void)OF_parseInExclamationMarkWithBuffer: (const char*)buffer
					  i: (size_t*)i
				       last: (size_t*)last
{
	if (finishedParsing && buffer[*i] != '-')
	if (_finishedParsing && buffer[*i] != '-')
		@throw [OFMalformedXMLException exceptionWithClass: [self class]
							    parser: self];

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

	*last = *i + 1;
}

/* CDATA */
- (void)OF_parseInCDATAOpeningWithBuffer: (const char*)buffer
				       i: (size_t*)i
				    last: (size_t*)last
{
	if (buffer[*i] != "CDATA["[level])
	if (buffer[*i] != "CDATA["[_level])
		@throw [OFMalformedXMLException exceptionWithClass: [self class]
							    parser: self];

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

	*last = *i + 1;
}

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

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

- (void)OF_parseInCDATA2WithBuffer: (const char*)buffer
				 i: (size_t*)i
			      last: (size_t*)last
{
	void *pool;
	OFString *CDATA;

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

		return;
	}

	pool = objc_autoreleasePoolPush();

	cache_append(cache, buffer + *last, encoding, *i - *last);
	CDATA = transform_string(cache, 2, NO, nil);
	cache_append(_cache, buffer + *last, _encoding, *i - *last);
	CDATA = transform_string(_cache, 2, NO, nil);

	if ([delegate respondsToSelector: @selector(parser:foundCDATA:)])
		[delegate parser: self
		      foundCDATA: CDATA];
	if ([_delegate respondsToSelector: @selector(parser:foundCDATA:)])
		[_delegate parser: self
		       foundCDATA: CDATA];

	objc_autoreleasePoolPop(pool);

	[cache removeAllItems];
	[_cache removeAllItems];

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

/* Comment */
- (void)OF_parseInCommentOpeningWithBuffer: (const char*)buffer
					 i: (size_t*)i
				      last: (size_t*)last
{
	if (buffer[*i] != '-')
		@throw [OFMalformedXMLException exceptionWithClass: [self class]
							    parser: self];

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

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

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

- (void)OF_parseInComment2WithBuffer: (const char*)buffer
				   i: (size_t*)i
				last: (size_t*)last
{
	void *pool;
	OFString *comment;

	if (buffer[*i] != '>')
		@throw [OFMalformedXMLException exceptionWithClass: [self class]
							    parser: self];

	pool = objc_autoreleasePoolPush();

	cache_append(cache, buffer + *last, encoding, *i - *last);
	comment = transform_string(cache, 2, NO, nil);
	cache_append(_cache, buffer + *last, _encoding, *i - *last);
	comment = transform_string(_cache, 2, NO, nil);

	if ([delegate respondsToSelector: @selector(parser:foundComment:)])
		[delegate parser: self
		    foundComment: comment];
	if ([_delegate respondsToSelector: @selector(parser:foundComment:)])
		[_delegate parser: self
		     foundComment: comment];

	objc_autoreleasePoolPop(pool);

	[cache removeAllItems];
	[_cache removeAllItems];

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

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

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

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

	*last = *i + 1;
}

- (size_t)lineNumber
{
	return lineNumber;
	return _lineNumber;
}

- (BOOL)finishedParsing
{
	return finishedParsing;
	return _finishedParsing;
}

-	   (OFString*)string: (OFString*)string
  containsUnknownEntityNamed: (OFString*)entity
{
	if ([delegate respondsToSelector:
	if ([_delegate respondsToSelector:
	    @selector(parser:foundUnknownEntityNamed:)])
		return [delegate     parser: self
		    foundUnknownEntityNamed: entity];
		return [_delegate parser: self
		 foundUnknownEntityNamed: entity];

	return nil;
}
@end