ObjFW  Diff

Differences From Artifact [267ee48789]:

To Artifact [852bd85a55]:


36
37
38
39
40
41
42
43
44


45
46
47
48
49
50




























51





52
53
54
55
56
57
58
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







-
-
+
+






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








#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 lookupTable[OF_XMLPARSER_NUM_STATES];

static OF_INLINE OFString*
transform_string(OFMutableString *cache,
static OFString*
transform_string(OFMutableString *cache, size_t cut, BOOL unescape,
    OFObject <OFStringXMLUnescapingDelegate> *delegate)
{
	[cache replaceOccurrencesOfString: @"\r\n"
			       withString: @"\n"];
	[cache replaceOccurrencesOfString: @"\r"
			       withString: @"\n"];

	if (cut > 0) {
		/*
		 * We need to create a mutable copy in order to detect possible
		 * UTF-8, as we never checked for UTF-8 when appending to the
		 * cache for performance reasons.
		 */
		OFMutableString *ret = [[cache mutableCopy] autorelease];
		size_t length;

		length = [ret length];
		[ret deleteCharactersFromIndex: length - cut
				       toIndex: length];

		if (unescape)
			return [ret stringByXMLUnescapingWithDelegate:
			    delegate];

		/*
		 * 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.
		 */
		ret->isa = [OFString class];

		return ret;
	} else {
		if (unescape)
	return [cache stringByXMLUnescapingWithDelegate: delegate];
			return [cache stringByXMLUnescapingWithDelegate:
			    delegate];
		else
			return [[cache copy] autorelease];
	}
}

static OFString*
namespace_for_prefix(OFString *prefix, OFArray *namespaces)
{
	OFDictionary **cArray = [namespaces cArray];
	ssize_t i;
156
157
158
159
160
161
162

163
164
165
166
167
168
169
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202







+







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

		acceptProlog = YES;
		lineNumber = 1;
		encoding = OF_STRING_ENCODING_UTF_8;

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

223
224
225
226
227
228
229

230
231
232
233
234
235
236
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270







+








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

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

- (void)parseString: (OFString*)string
{
	[self parseBuffer: [string cString]
	       withLength: [string cStringLength]];
285
286
287
288
289
290
291

292
293
294
295
296
297
298
299

300
301
302
303
304
305
306
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333

334
335
336
337
338
339
340
341







+







-
+







						      parser: self];

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

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

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

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

#if defined(OF_HAVE_PROPERTIES) && defined(OF_HAVE_BLOCKS)
		if (charactersHandler != NULL)
			charactersHandler(self, characters);
		else
#endif
			[delegate parser: self
351
352
353
354
355
356
357
358

359
360
361
362
363
364
365
386
387
388
389
390
391
392

393
394
395
396
397
398
399
400







-
+







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

	if (!acceptProlog)
		return NO;

	acceptProlog = NO;

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







+
-
-
+
+





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

+







			piState = 3;

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

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

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

			if ([attribute isEqual: @"encoding"])
				if ([value caseInsensitiveCompare: @"utf-8"] !=
				    OF_ORDERED_SAME)
			if ([attribute isEqual: @"encoding"]) {
				[value lower];

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

			last = i + 1;
			piState = 0;

			break;
		}
	}
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
484
485
486
487
488
489
490

491

492
493
494
495



496









497
498
499
500
501
502
503







-
+
-


+

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







					       i: (size_t*)i
					    last: (size_t*)last
{
	if (buffer[*i] == '?')
		level = 1;
	else if (level == 1 && buffer[*i] == '>') {
		OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
		OFMutableString *pi;
		OFString *pi;
		size_t len;

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

		pi = transform_string(cache, 1, NO, nil);
		[pi deleteCharactersFromIndex: len - 1
				      toIndex: len];

		/*
		 * 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.
		 */
		pi->isa = [OFString class];

		if ([pi isEqual: @"xml"] || [pi hasPrefix: @"xml "] ||
		    [pi hasPrefix: @"xml\t"] || [pi hasPrefix: @"xml\r"] ||
		    [pi hasPrefix: @"xml\n"])
			if (![self _parseXMLProcessingInstructions: pi])
				@throw [OFMalformedXMLException
				    newWithClass: isa
488
489
490
491
492
493
494

495
496
497
498
499
500
501
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540







+








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

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

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

	if ((tmp = memchr(cacheCString, ':', cacheLength)) != NULL) {
		name = [[OFString alloc] initWithCString: tmp + 1
582
583
584
585
586
587
588

589

590
591
592
593
594
595
596
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637







+

+








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

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

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

	if ((tmp = memchr(cacheCString, ':', cacheLength)) != NULL) {
		name = [[OFString alloc] initWithCString: tmp + 1
						  length: cacheLength -
							  (tmp - cacheCString) -
732
733
734
735
736
737
738

739
740
741

742
743
744
745
746
747
748
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791







+



+







	size_t length, cacheLength;

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

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

	[cache deleteLeadingAndTrailingWhitespaces];

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

	if ((tmp = memchr(cacheCString, ':', cacheLength)) != NULL) {
		attributeName = [[OFString alloc]
		    initWithCString: tmp + 1
			     length: cacheLength - (tmp - cacheCString) - 1];
789
790
791
792
793
794
795

796
797
798
799

800
801
802
803
804
805
806
832
833
834
835
836
837
838
839
840
841
842

843
844
845
846
847
848
849
850







+



-
+







	size_t length;

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

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

	pool = [[OFAutoreleasePool alloc] init];
	attributeValue = transform_string(cache, 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];
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
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







-
+
-











+

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







}

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

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

		return;
	}

	pool = [[OFAutoreleasePool alloc] init];

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

	CDATA = transform_string(cache, 2, NO, nil);
	[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];

#if defined(OF_HAVE_PROPERTIES) && defined(OF_HAVE_BLOCKS)
	if (CDATAHandler != NULL)
		CDATAHandler(self, CDATA);
	else
#endif
		[delegate parser: self
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
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







-
+
-








+

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







}

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

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

	pool = [[OFAutoreleasePool alloc] init];

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

	comment = transform_string(cache, 2, NO, nil);
	[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];

#if defined(OF_HAVE_PROPERTIES) && defined(OF_HAVE_BLOCKS)
	if (commentHandler != NULL)
		commentHandler(self, comment);
	else
#endif
		[delegate parser: self