ObjFW  Check-in [b2dd4f049b]

Overview
Comment:OFXMLParser: Add support for different encodings and other improvements.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: b2dd4f049bcfbd088f1796eac1e44eae48ab7c112beaf5e80957bdade92c607e
User & Date: js on 2011-04-25 16:57:55
Other Links: manifest | tags
Context
2011-04-25
17:14
Skip possible BOMs in OFMutableString. check-in: 7aefcd5ede user: js tags: trunk
16:57
OFXMLParser: Add support for different encodings and other improvements. check-in: b2dd4f049b user: js tags: trunk
14:14
Fix a warning when sizeof(size_t) < sizeof(long long). check-in: e14961f94d user: js tags: trunk
Changes

Modified src/OFDataArray.m from [e4b0bd482e] to [738291762c].

37
38
39
40
41
42
43
44

45
46
47
48
49
50
51
#import "base64.h"
#import "macros.h"

/* References for static linking */
void _references_to_categories_of_OFDataArray(void)
{
	_OFDataArray_Hashing_reference = 1;
};


@implementation OFDataArray
+ dataArray
{
	return [[[self alloc] init] autorelease];
}








<
>







37
38
39
40
41
42
43

44
45
46
47
48
49
50
51
#import "base64.h"
#import "macros.h"

/* References for static linking */
void _references_to_categories_of_OFDataArray(void)
{
	_OFDataArray_Hashing_reference = 1;

}

@implementation OFDataArray
+ dataArray
{
	return [[[self alloc] init] autorelease];
}

Modified src/OFMutableString.h from [960bff6aa8] to [02e4c6ad2f].

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
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
#import "OFString.h"

/**
 * \brief A class for storing and modifying strings.
 */
@interface OFMutableString: OFString
/**
 * Sets the OFString to the specified UTF-8 encoded C string.
 *
 * \param string A UTF-8 encoded C string to set the OFString to.
 */
- (void)setToCString: (const char*)string;

/**
 * Appends a UTF-8 encoded C string to the OFString.
 *
 * \param string A UTF-8 encoded C string to append
 */
- (void)appendCString: (const char*)string;

/**
 * Appends a UTF-8 encoded C string with the specified length to the OFString.

 *
 * \param string A UTF-8 encoded C string to append
 * \param length The length of the UTF-8 encoded C string
 */
- (void)appendCString: (const char*)string
	   withLength: (size_t)length;

/**












 * Appends a UTF-8 encoded C string to the OFString without checking whether it
 * is valid UTF-8.
 *
 * Only use this if you are 100% sure the string you append is either ASCII or
 * UTF-8!
 *
 * \param string A UTF-8 encoded C string to append
 */
- (void)appendCStringWithoutUTF8Checking: (const char*)string;

/**
 * Appends a UTF-8 encoded C string with the specified length to the OFString
 * without checking whether it is valid UTF-8.
 *
 * Only use this if you are 100% sure the string you append is either ASCII or
 * UTF-8!
 *
 * \param string A UTF-8 encoded C string to append
 * \param length The length of the UTF-8 encoded C string
 */
- (void)appendCStringWithoutUTF8Checking: (const char*)string
				  length: (size_t)length;

/**
















 * Appends another OFString to the OFString.
 *
 * \param string An OFString to append
 */
- (void)appendString: (OFString*)string;

/**
 * Appends a formatted UTF-8 encoded C string to the OFString.
 * See printf for the format syntax.
 *
 * \param format A format string which generates the string to append
 */
- (void)appendFormat: (OFString*)format, ...;

/**
 * Appends a formatted UTF-8 encoded C string to the OFString.
 * See printf for the format syntax.
 *
 * \param format A format string which generates the string to append
 * \param arguments The arguments used in the format string
 */
- (void)appendFormat: (OFString*)format
       withArguments: (va_list)arguments;

/**
 * Prepends another OFString to the OFString.
 *
 * \param string An OFString to prepend
 */
- (void)prependString: (OFString*)string;

/**
 * Reverse the OFString.
 */
- (void)reverse;

/**
 * Upper the OFString.
 */
- (void)upper;

/**
 * Lower the OFString.
 */
- (void)lower;

/**
 * Inserts a string at the specified index.
 *
 * \param string The string to insert







|

|




|






|
>








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









|
|











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






|







|









|






|




|




|







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
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
141
142
143
144
145
146
147
148
149
150
151
152
#import "OFString.h"

/**
 * \brief A class for storing and modifying strings.
 */
@interface OFMutableString: OFString
/**
 * Sets the OFMutableString to the specified UTF-8 encoded C string.
 *
 * \param string A UTF-8 encoded C string to set the OFMutableString to.
 */
- (void)setToCString: (const char*)string;

/**
 * Appends a UTF-8 encoded C string to the OFMutableString.
 *
 * \param string A UTF-8 encoded C string to append
 */
- (void)appendCString: (const char*)string;

/**
 * Appends a UTF-8 encoded C string with the specified length to the
 * OFMutableString.
 *
 * \param string A UTF-8 encoded C string to append
 * \param length The length of the UTF-8 encoded C string
 */
- (void)appendCString: (const char*)string
	   withLength: (size_t)length;

/**
 * Appends a C string with the specified encoding and length to the
 * OFMutableString.
 *
 * \param string A C string to append
 * \param encoding The encoding of the C string
 * \param length The length of the UTF-8 encoded C string
 */
- (void)appendCString: (const char*)string
	 withEncoding: (of_string_encoding_t)encoding
	       length: (size_t)length;

/**
 * Appends a UTF-8 encoded C string to the OFMutableString without checking
 * whether it is valid UTF-8.
 *
 * Only use this if you are 100% sure the string you append is either ASCII or
 * UTF-8!
 *
 * \param string A UTF-8 encoded C string to append
 */
- (void)appendCStringWithoutUTF8Checking: (const char*)string;

/**
 * Appends a UTF-8 encoded C string with the specified length to the
 * OFMutableString without checking whether it is valid UTF-8.
 *
 * Only use this if you are 100% sure the string you append is either ASCII or
 * UTF-8!
 *
 * \param string A UTF-8 encoded C string to append
 * \param length The length of the UTF-8 encoded C string
 */
- (void)appendCStringWithoutUTF8Checking: (const char*)string
				  length: (size_t)length;

/**
 * Appends a C string with the specified encoding and length length to the
 * OFString without checking whether it is valid UTF-8 if the specified encoding
 * is UTF-8.
 *
 * Only use this if you are 100% sure the string you append is either ASCII or
 * UTF-8 if you specified UTF-8 as encoding!
 *
 * \param string A C string to append
 * \param encoding The encoding of the C string
 * \param length The length of the UTF-8 encoded C string
 */
- (void)appendCStringWithoutUTF8Checking: (const char*)string
				encoding: (of_string_encoding_t)encoding
				  length: (size_t)length;

/**
 * Appends another OFString to the OFMutableString.
 *
 * \param string An OFString to append
 */
- (void)appendString: (OFString*)string;

/**
 * Appends a formatted UTF-8 encoded C string to the OFMutableString.
 * See printf for the format syntax.
 *
 * \param format A format string which generates the string to append
 */
- (void)appendFormat: (OFString*)format, ...;

/**
 * Appends a formatted UTF-8 encoded C string to the OFMutableString.
 * See printf for the format syntax.
 *
 * \param format A format string which generates the string to append
 * \param arguments The arguments used in the format string
 */
- (void)appendFormat: (OFString*)format
       withArguments: (va_list)arguments;

/**
 * Prepends another OFString to the OFMutableString.
 *
 * \param string An OFString to prepend
 */
- (void)prependString: (OFString*)string;

/**
 * Reverse the OFMutableString.
 */
- (void)reverse;

/**
 * Upper the OFMutableString.
 */
- (void)upper;

/**
 * Lower the OFMutableString.
 */
- (void)lower;

/**
 * Inserts a string at the specified index.
 *
 * \param string The string to insert

Modified src/OFMutableString.m from [fa9bcee55c] to [9261f4b9e0].

25
26
27
28
29
30
31

32
33
34
35
36
37
38
#ifdef HAVE_MADVISE
# include <sys/mman.h>
#else
# define madvise(addr, len, advise)
#endif

#import "OFString.h"


#import "OFInvalidArgumentException.h"
#import "OFInvalidEncodingException.h"
#import "OFInvalidFormatException.h"
#import "OFOutOfMemoryException.h"
#import "OFOutOfRangeException.h"








>







25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#ifdef HAVE_MADVISE
# include <sys/mman.h>
#else
# define madvise(addr, len, advise)
#endif

#import "OFString.h"
#import "OFAutoreleasePool.h"

#import "OFInvalidArgumentException.h"
#import "OFInvalidEncodingException.h"
#import "OFInvalidFormatException.h"
#import "OFOutOfMemoryException.h"
#import "OFOutOfRangeException.h"

192
193
194
195
196
197
198
















199
200
201
202
203
204
205

	string = [self resizeMemory: string
			     toSize: length + length_ + 1];
	memcpy(string + length, string_, length_);
	length += length_;
	string[length] = 0;
}

















- (void)appendCStringWithoutUTF8Checking: (const char*)string_
{
	size_t len;

	len = strlen(string_);
	string = [self resizeMemory: string







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







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

	string = [self resizeMemory: string
			     toSize: length + length_ + 1];
	memcpy(string + length, string_, length_);
	length += length_;
	string[length] = 0;
}

- (void)appendCString: (const char*)string_
	 withEncoding: (of_string_encoding_t)encoding
	       length: (size_t)length_
{
	if (encoding == OF_STRING_ENCODING_UTF_8)
		[self appendCString: string_
			 withLength: length_];
	else {
		OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
		[self appendString: [OFString stringWithCString: string_
						       encoding: encoding
							 length: length_]];
		[pool release];
	}
}

- (void)appendCStringWithoutUTF8Checking: (const char*)string_
{
	size_t len;

	len = strlen(string_);
	string = [self resizeMemory: string
213
214
215
216
217
218
219
















220
221
222
223
224
225
226
{
	string = [self resizeMemory: string
			     toSize: length + length_ + 1];
	memcpy(string + length, string_, length_);
	length += length_;
	string[length] = 0;
}

















- (void)appendString: (OFString*)string_
{
	if (string_ == nil)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







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
{
	string = [self resizeMemory: string
			     toSize: length + length_ + 1];
	memcpy(string + length, string_, length_);
	length += length_;
	string[length] = 0;
}

- (void)appendCStringWithoutUTF8Checking: (const char*)string_
				encoding: (of_string_encoding_t)encoding
				  length: (size_t)length_
{
	if (encoding == OF_STRING_ENCODING_UTF_8)
		[self appendCStringWithoutUTF8Checking: string_
						length: length_];
	else {
		OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
		[self appendString: [OFString stringWithCString: string_
						       encoding: encoding
							 length: length_]];
		[pool release];
	}
}

- (void)appendString: (OFString*)string_
{
	if (string_ == nil)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

Modified src/OFXMLParser.h from [542d58ae0a] to [6fc60c0e72].

180
181
182
183
184
185
186

187
188
189
190
191
192
193
	of_xml_parser_unknown_entity_block_t unknownEntityHandler;
#endif
	size_t level;
	BOOL acceptProlog;
	size_t lineNumber;
	BOOL lastCarriageReturn;
	BOOL finishedParsing;

}

#ifdef OF_HAVE_PROPERTIES
@property (retain) id <OFXMLParserDelegate> delegate;
# ifdef OF_HAVE_BLOCKS
@property (copy) of_xml_parser_processing_instructions_block_t
    processingInstructionsHandler;







>







180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
	of_xml_parser_unknown_entity_block_t unknownEntityHandler;
#endif
	size_t level;
	BOOL acceptProlog;
	size_t lineNumber;
	BOOL lastCarriageReturn;
	BOOL finishedParsing;
	of_string_encoding_t encoding;
}

#ifdef OF_HAVE_PROPERTIES
@property (retain) id <OFXMLParserDelegate> delegate;
# ifdef OF_HAVE_BLOCKS
@property (copy) of_xml_parser_processing_instructions_block_t
    processingInstructionsHandler;

Modified src/OFXMLParser.m from [267ee48789] to [852bd85a55].

36
37
38
39
40
41
42
43
44
45
46
47
48
49
50




























51




52
53
54
55
56
57
58

#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,
    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];
	ssize_t i;







|
|






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







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


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








>







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

		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

						 length: length - last];
}

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







>







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
						      parser: self];

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

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

						 length: length];

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

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

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







>







|







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, 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
/* <?xml […]?> */
- (BOOL)_parseXMLProcessingInstructions: (OFString*)pi
{
	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;








|







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;
	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
			piState = 3;

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


			value = [OFString 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)












					return NO;


			last = i + 1;
			piState = 0;

			break;
		}
	}







>
|
|





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

>







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
			    stringWithCString: cString + last
				       length: i - last];

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

			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
					       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;
		size_t len;

		[cache appendCStringWithoutUTF8Checking: buffer + *last

						 length: *i - *last];
		pi = [[cache mutableCopy] autorelease];
		len = [pi length];

		[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







|
<


>

<
<
|
<
<
<
<
<
<
<
<
<







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];
		OFString *pi;


		[cache appendCStringWithoutUTF8Checking: buffer + *last
					       encoding: encoding
						 length: *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 _parseXMLProcessingInstructions: pi])
				@throw [OFMalformedXMLException
				    newWithClass: isa
488
489
490
491
492
493
494

495
496
497
498
499
500
501

	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

						 length: length];

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

	if ((tmp = memchr(cacheCString, ':', cacheLength)) != NULL) {
		name = [[OFString alloc] initWithCString: tmp + 1







>







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

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

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

						 length: length];

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

	if ((tmp = memchr(cacheCString, ':', cacheLength)) != NULL) {
		name = [[OFString alloc] initWithCString: tmp + 1
						  length: cacheLength -
							  (tmp - cacheCString) -







>

>







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
	size_t length, cacheLength;

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

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

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







>



>







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
	size_t length;

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

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

						 length: length];

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

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







>



|







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

- (void)_parseInCDATA2WithBuffer: (const char*)buffer
			       i: (size_t*)i
			    last: (size_t*)last
{
	OFAutoreleasePool *pool;
	OFMutableString *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

					 length: *i - *last];
	CDATA = [[cache mutableCopy] autorelease];
	length = [CDATA length];

	[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







|
<











>

<
<
|
<
<
<
<
<
<
<
<
<







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;
	OFString *CDATA;


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










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

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

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

	pool = [[OFAutoreleasePool alloc] init];

	[cache appendCStringWithoutUTF8Checking: buffer + *last

					 length: *i - *last];
	comment = [[cache mutableCopy] autorelease];
	length = [comment length];

	[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







|
<








>

<
<
|
<
<
<
<
<
<
<
<
<







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;
	OFString *comment;


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

	pool = [[OFAutoreleasePool alloc] init];

	[cache appendCStringWithoutUTF8Checking: buffer + *last
				       encoding: encoding
					 length: *i - *last];


	comment = transform_string(cache, 2, NO, nil);










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