ObjFW  Diff

Differences From Artifact [e395ba83dc]:

To Artifact [02a95773a3]:


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31


32

33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "config.h"

#define OF_XML_PARSER_M

#include <string.h>

#import "OFXMLParser.h"
#import "OFString.h"
#import "OFArray.h"
#import "OFDictionary.h"
#import "OFData.h"
#import "OFXMLAttribute.h"
#import "OFStream.h"
#ifdef OF_HAVE_FILES
# import "OFFile.h"
#endif


#import "OFSystemInfo.h"


#import "OFInitializationFailedException.h"
#import "OFInvalidArgumentException.h"
#import "OFInvalidEncodingException.h"
#import "OFInvalidFormatException.h"
#import "OFMalformedXMLException.h"
#import "OFOutOfRangeException.h"
#import "OFUnboundPrefixException.h"

@interface OFXMLParser () <OFStringXMLUnescapingDelegate>
@end

static void inByteOrderMarkState(OFXMLParser *);
static void outsideTagState(OFXMLParser *);
static void tagOpenedState(OFXMLParser *);
static void inProcessingInstructionsState(OFXMLParser *);
static void inTagNameState(OFXMLParser *);
static void inCloseTagNameState(OFXMLParser *);
static void inTagState(OFXMLParser *);
static void inAttributeNameState(OFXMLParser *);
static void expectAttributeEqualSignState(OFXMLParser *);
static void expectAttributeDelimiterState(OFXMLParser *);
static void inAttributeValueState(OFXMLParser *);







|
|
<

<
|



>
>

>















|







16
17
18
19
20
21
22
23
24

25

26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "config.h"

#define OF_XML_PARSER_M

#include <string.h>

#import "OFXMLParser.h"
#import "OFArray.h"
#import "OFCharacterSet.h"

#import "OFData.h"

#import "OFDictionary.h"
#ifdef OF_HAVE_FILES
# import "OFFile.h"
#endif
#import "OFStream.h"
#import "OFString.h"
#import "OFSystemInfo.h"
#import "OFXMLAttribute.h"

#import "OFInitializationFailedException.h"
#import "OFInvalidArgumentException.h"
#import "OFInvalidEncodingException.h"
#import "OFInvalidFormatException.h"
#import "OFMalformedXMLException.h"
#import "OFOutOfRangeException.h"
#import "OFUnboundPrefixException.h"

@interface OFXMLParser () <OFStringXMLUnescapingDelegate>
@end

static void inByteOrderMarkState(OFXMLParser *);
static void outsideTagState(OFXMLParser *);
static void tagOpenedState(OFXMLParser *);
static void inProcessingInstructionState(OFXMLParser *);
static void inTagNameState(OFXMLParser *);
static void inCloseTagNameState(OFXMLParser *);
static void inTagState(OFXMLParser *);
static void inAttributeNameState(OFXMLParser *);
static void expectAttributeEqualSignState(OFXMLParser *);
static void expectAttributeDelimiterState(OFXMLParser *);
static void inAttributeValueState(OFXMLParser *);
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
static void inCommentState2(OFXMLParser *);
static void inDOCTYPEState(OFXMLParser *);
typedef void (*state_function_t)(OFXMLParser *);
static state_function_t lookupTable[] = {
	[OF_XMLPARSER_IN_BYTE_ORDER_MARK] = inByteOrderMarkState,
	[OF_XMLPARSER_OUTSIDE_TAG] = outsideTagState,
	[OF_XMLPARSER_TAG_OPENED] = tagOpenedState,
	[OF_XMLPARSER_IN_PROCESSING_INSTRUCTIONS] =
	    inProcessingInstructionsState,
	[OF_XMLPARSER_IN_TAG_NAME] = inTagNameState,
	[OF_XMLPARSER_IN_CLOSE_TAG_NAME] = inCloseTagNameState,
	[OF_XMLPARSER_IN_TAG] = inTagState,
	[OF_XMLPARSER_IN_ATTRIBUTE_NAME] = inAttributeNameState,
	[OF_XMLPARSER_EXPECT_ATTRIBUTE_EQUAL_SIGN] =
	    expectAttributeEqualSignState,
	[OF_XMLPARSER_EXPECT_ATTRIBUTE_DELIMITER] =







|
<







64
65
66
67
68
69
70
71

72
73
74
75
76
77
78
static void inCommentState2(OFXMLParser *);
static void inDOCTYPEState(OFXMLParser *);
typedef void (*state_function_t)(OFXMLParser *);
static state_function_t lookupTable[] = {
	[OF_XMLPARSER_IN_BYTE_ORDER_MARK] = inByteOrderMarkState,
	[OF_XMLPARSER_OUTSIDE_TAG] = outsideTagState,
	[OF_XMLPARSER_TAG_OPENED] = tagOpenedState,
	[OF_XMLPARSER_IN_PROCESSING_INSTRUCTION] = inProcessingInstructionState,

	[OF_XMLPARSER_IN_TAG_NAME] = inTagNameState,
	[OF_XMLPARSER_IN_CLOSE_TAG_NAME] = inCloseTagNameState,
	[OF_XMLPARSER_IN_TAG] = inTagState,
	[OF_XMLPARSER_IN_ATTRIBUTE_NAME] = inAttributeNameState,
	[OF_XMLPARSER_EXPECT_ATTRIBUTE_EQUAL_SIGN] =
	    expectAttributeEqualSignState,
	[OF_XMLPARSER_EXPECT_ATTRIBUTE_DELIMITER] =
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
	if (self->_finishedParsing && self->_data[self->_i] != '!' &&
	    self->_data[self->_i] != '?')
		@throw [OFMalformedXMLException exceptionWithParser: self];

	switch (self->_data[self->_i]) {
	case '?':
		self->_last = self->_i + 1;
		self->_state = OF_XMLPARSER_IN_PROCESSING_INSTRUCTIONS;
		self->_level = 0;
		break;
	case '/':
		self->_last = self->_i + 1;
		self->_state = OF_XMLPARSER_IN_CLOSE_TAG_NAME;
		self->_acceptProlog = false;
		break;







|







348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
	if (self->_finishedParsing && self->_data[self->_i] != '!' &&
	    self->_data[self->_i] != '?')
		@throw [OFMalformedXMLException exceptionWithParser: self];

	switch (self->_data[self->_i]) {
	case '?':
		self->_last = self->_i + 1;
		self->_state = OF_XMLPARSER_IN_PROCESSING_INSTRUCTION;
		self->_level = 0;
		break;
	case '/':
		self->_last = self->_i + 1;
		self->_state = OF_XMLPARSER_IN_CLOSE_TAG_NAME;
		self->_acceptProlog = false;
		break;
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
		self->_i--;
		break;
	}
}

/* <?xml […]?> */
static bool
parseXMLProcessingInstructions(OFXMLParser *self, OFString *pi)
{
	const char *cString;
	size_t length, last;
	int PIState = 0;
	OFString *attribute = nil;
	OFMutableString *value = nil;
	char piDelimiter = 0;
	bool hasVersion = false;

	if (!self->_acceptProlog)
		return false;

	self->_acceptProlog = false;

	pi = [pi substringFromIndex: 3];
	pi = pi.stringByDeletingEnclosingWhitespaces;

	cString = pi.UTF8String;
	length = pi.UTF8StringLength;

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







|














<
<
<
|
|







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
		self->_i--;
		break;
	}
}

/* <?xml […]?> */
static bool
parseXMLProcessingInstruction(OFXMLParser *self, OFString *data)
{
	const char *cString;
	size_t length, last;
	int PIState = 0;
	OFString *attribute = nil;
	OFMutableString *value = nil;
	char piDelimiter = 0;
	bool hasVersion = false;

	if (!self->_acceptProlog)
		return false;

	self->_acceptProlog = false;




	cString = data.UTF8String;
	length = data.UTF8StringLength;

	last = 0;
	for (size_t i = 0; i < length; i++) {
		switch (PIState) {
		case 0:
			if (cString[i] == ' ' || cString[i] == '\t' ||
			    cString[i] == '\r' || cString[i] == '\n')
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

	if (PIState != 0 || !hasVersion)
		return false;

	return true;
}

/* Inside processing instructions */
static void
inProcessingInstructionsState(OFXMLParser *self)
{
	if (self->_data[self->_i] == '?')
		self->_level = 1;
	else if (self->_level == 1 && self->_data[self->_i] == '>') {
		void *pool = objc_autoreleasePoolPush();
		OFString *PI;



		appendToBuffer(self->_buffer, self->_data + self->_last,
		    self->_encoding, self->_i - self->_last);
		PI = transformString(self, self->_buffer, 1, false);








		if ([PI isEqual: @"xml"] || [PI hasPrefix: @"xml "] ||




		    [PI hasPrefix: @"xml\t"] || [PI hasPrefix: @"xml\r"] ||
		    [PI hasPrefix: @"xml\n"])
			if (!parseXMLProcessingInstructions(self, PI))
				@throw [OFMalformedXMLException
				    exceptionWithParser: self];

		if ([self->_delegate respondsToSelector:
		    @selector(parser:foundProcessingInstructions:)])
			[self->_delegate parser: self
			    foundProcessingInstructions: PI];


		objc_autoreleasePoolPop(pool);

		[self->_buffer removeAllItems];

		self->_last = self->_i + 1;
		self->_state = OF_XMLPARSER_OUTSIDE_TAG;







|

|





|
>
>





>
>
>
>
>
>
>
|
>
>
>
>
|
|
|



|
|

|
>







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

	if (PIState != 0 || !hasVersion)
		return false;

	return true;
}

/* Inside processing instruction */
static void
inProcessingInstructionState(OFXMLParser *self)
{
	if (self->_data[self->_i] == '?')
		self->_level = 1;
	else if (self->_level == 1 && self->_data[self->_i] == '>') {
		void *pool = objc_autoreleasePoolPush();
		OFString *PI, *target, *data = nil;
		OFCharacterSet *whitespaceCS;
		size_t pos;

		appendToBuffer(self->_buffer, self->_data + self->_last,
		    self->_encoding, self->_i - self->_last);
		PI = transformString(self, self->_buffer, 1, false);

		whitespaceCS = [OFCharacterSet
		    characterSetWithCharactersInString: @" \r\n\r"];
		pos = [PI indexOfCharacterFromSet: whitespaceCS];
		if (pos != OF_NOT_FOUND) {
			target = [PI substringToIndex: pos];
			data = [[PI substringFromIndex: pos + 1]
			    stringByDeletingEnclosingWhitespaces];

			if (data.length == 0)
				data = nil;
		} else
			target = PI;

		if ([target caseInsensitiveCompare: @"xml"] == OF_ORDERED_SAME)
			if (!parseXMLProcessingInstruction(self, data))
				@throw [OFMalformedXMLException
				    exceptionWithParser: self];

		if ([self->_delegate respondsToSelector: @selector(
		    parser:foundProcessingInstructionWithTarget:data:)])
			[self->_delegate parser: self
			    foundProcessingInstructionWithTarget: target
							    data: data];

		objc_autoreleasePoolPop(pool);

		[self->_buffer removeAllItems];

		self->_last = self->_i + 1;
		self->_state = OF_XMLPARSER_OUTSIDE_TAG;
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
				length: tmp - bufferCString];
	} else {
		self->_name = [bufferString copy];
		self->_prefix = nil;
	}

	if (self->_data[self->_i] == '>' || self->_data[self->_i] == '/') {
		OFString *namespace;

		namespace = namespaceForPrefix(self->_prefix,
		    self->_namespaces);

		if (self->_prefix != nil && namespace == nil)
			@throw [OFUnboundPrefixException
			    exceptionWithPrefix: self->_prefix
					 parser: self];








<
<
|







555
556
557
558
559
560
561


562
563
564
565
566
567
568
569
				length: tmp - bufferCString];
	} else {
		self->_name = [bufferString copy];
		self->_prefix = nil;
	}

	if (self->_data[self->_i] == '>' || self->_data[self->_i] == '/') {


		OFString *namespace = namespaceForPrefix(self->_prefix,
		    self->_namespaces);

		if (self->_prefix != nil && namespace == nil)
			@throw [OFUnboundPrefixException
			    exceptionWithPrefix: self->_prefix
					 parser: self];