︙ | | |
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
|
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
|
-
+
-
+
-
+
|
[buffer addItems: [tmp UTF8String]
count: [tmp UTF8StringLength]];
objc_autoreleasePoolPop(pool);
}
}
static OFString*
transform_string(OFDataArray *buffer, size_t cut, BOOL unescape,
transform_string(OFDataArray *buffer, size_t cut, bool unescape,
id <OFStringXMLUnescapingDelegate> delegate)
{
char *items;
size_t i, length;
BOOL hasEntities = NO;
bool hasEntities = false;
OFString *ret;
items = [buffer items];
length = [buffer count] - cut;
for (i = 0; i < length; i++) {
if (items[i] == '\r') {
if (i + 1 < length && items[i + 1] == '\n') {
[buffer removeItemAtIndex: i];
items = [buffer items];
i--;
length--;
} else
items[i] = '\n';
} else if (items[i] == '&')
hasEntities = YES;
hasEntities = true;
}
ret = [OFString stringWithUTF8String: items
length: length];
if (unescape && hasEntities)
return [ret stringByXMLUnescapingWithDelegate: delegate];
|
︙ | | |
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
-
+
|
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];
_acceptProlog = YES;
_acceptProlog = true;
_lineNumber = 1;
_encoding = OF_STRING_ENCODING_UTF_8;
_depthLimit = 32;
objc_autoreleasePoolPop(pool);
} @catch (id e) {
[self release];
|
︙ | | |
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
|
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
|
-
+
|
return;
if ((length = *i - *last) > 0)
buffer_append(_buffer, buffer + *last, _encoding, length);
if ([_buffer count] > 0) {
void *pool = objc_autoreleasePoolPush();
OFString *characters = transform_string(_buffer, 0, YES, self);
OFString *characters = transform_string(_buffer, 0, true, self);
if ([_delegate respondsToSelector:
@selector(parser:foundCharacters:)])
[_delegate parser: self
foundCharacters: characters];
objc_autoreleasePoolPop(pool);
|
︙ | | |
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
|
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
|
-
+
-
+
-
+
-
+
-
+
-
+
|
*last = *i + 1;
_state = OF_XMLPARSER_IN_PROCESSING_INSTRUCTIONS;
_level = 0;
break;
case '/':
*last = *i + 1;
_state = OF_XMLPARSER_IN_CLOSE_TAG_NAME;
_acceptProlog = NO;
_acceptProlog = false;
break;
case '!':
*last = *i + 1;
_state = OF_XMLPARSER_IN_EXCLAMATIONMARK;
_acceptProlog = NO;
_acceptProlog = false;
break;
default:
if (_depthLimit > 0 && [_previous count] >= _depthLimit)
@throw [OFMalformedXMLException
exceptionWithClass: [self class]
parser: self];
_state = OF_XMLPARSER_IN_TAG_NAME;
_acceptProlog = NO;
_acceptProlog = false;
(*i)--;
break;
}
}
/* <?xml […]?> */
- (BOOL)OF_parseXMLProcessingInstructions: (OFString*)pi
- (bool)OF_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;
return false;
_acceptProlog = NO;
_acceptProlog = false;
pi = [pi substringWithRange: of_range(3, [pi length] - 3)];
pi = [pi stringByDeletingEnclosingWhitespaces];
cString = [pi UTF8String];
length = [pi UTF8StringLength];
|
︙ | | |
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
|
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
|
-
+
-
+
-
+
-
+
-
+
-
+
|
length: i - last];
last = i + 1;
PIState = 2;
break;
case 2:
if (cString[i] != '\'' && cString[i] != '"')
return NO;
return false;
piDelimiter = cString[i];
last = i + 1;
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;
return false;
if ([attribute isEqual: @"encoding"]) {
[value lowercase];
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;
return false;
}
last = i + 1;
PIState = 0;
break;
}
}
if (PIState != 0)
return NO;
return false;
return YES;
return true;
}
/* 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] == '>') {
void *pool = objc_autoreleasePoolPush();
OFString *PI;
buffer_append(_buffer, buffer + *last, _encoding, *i - *last);
PI = transform_string(_buffer, 1, NO, nil);
PI = transform_string(_buffer, 1, false, 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])
@throw [OFMalformedXMLException
exceptionWithClass: [self class]
|
︙ | | |
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
|
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
|
-
+
|
@selector(parser:didEndElement:prefix:namespace:)])
[_delegate parser: self
didEndElement: _name
prefix: _prefix
namespace: namespace];
if ([_previous count] == 0)
_finishedParsing = YES;
_finishedParsing = true;
} else
[_previous addObject: bufferString];
[_name release];
[_prefix release];
_name = _prefix = nil;
|
︙ | | |
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
|
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
|
-
+
|
*last = *i + 1;
_state = (buffer[*i] == '>'
? OF_XMLPARSER_OUTSIDE_TAG
: OF_XMLPARSER_EXPECT_SPACE_OR_CLOSE);
if ([_previous count] == 0)
_finishedParsing = YES;
_finishedParsing = true;
}
/* Inside a tag, name found */
- (void)OF_parseInTagWithBuffer: (const char*)buffer
i: (size_t*)i
last: (size_t*)last
{
|
︙ | | |
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
|
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
|
-
+
|
@selector(parser:didEndElement:prefix:namespace:)])
[_delegate parser: self
didEndElement: _name
prefix: _prefix
namespace: namespace];
if ([_previous count] == 0)
_finishedParsing = YES;
_finishedParsing = true;
[_namespaces removeLastObject];
} else if (_prefix != nil) {
OFString *str = [OFString stringWithFormat: @"%@:%@",
_prefix, _name];
[_previous addObject: str];
} else
|
︙ | | |
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
|
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
|
-
+
|
if (buffer[*i] != _delimiter)
return;
if ((length = *i - *last) > 0)
buffer_append(_buffer, buffer + *last, _encoding, length);
pool = objc_autoreleasePoolPush();
attributeValue = transform_string(_buffer, 0, YES, self);
attributeValue = transform_string(_buffer, 0, true, self);
if (_attributePrefix == nil && [_attributeName isEqual: @"xmlns"])
[[_namespaces lastObject] setObject: attributeValue
forKey: @""];
if ([_attributePrefix isEqual: @"xmlns"])
[[_namespaces lastObject] setObject: attributeValue
forKey: _attributeName];
|
︙ | | |
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
|
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
|
-
+
|
return;
}
pool = objc_autoreleasePoolPush();
buffer_append(_buffer, buffer + *last, _encoding, *i - *last);
CDATA = transform_string(_buffer, 2, NO, nil);
CDATA = transform_string(_buffer, 2, false, nil);
if ([_delegate respondsToSelector: @selector(parser:foundCDATA:)])
[_delegate parser: self
foundCDATA: CDATA];
objc_autoreleasePoolPop(pool);
|
︙ | | |
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
|
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
|
-
+
|
if (buffer[*i] != '>')
@throw [OFMalformedXMLException exceptionWithClass: [self class]
parser: self];
pool = objc_autoreleasePoolPush();
buffer_append(_buffer, buffer + *last, _encoding, *i - *last);
comment = transform_string(_buffer, 2, NO, nil);
comment = transform_string(_buffer, 2, false, nil);
if ([_delegate respondsToSelector: @selector(parser:foundComment:)])
[_delegate parser: self
foundComment: comment];
objc_autoreleasePoolPop(pool);
|
︙ | | |
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
|
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
|
-
+
|
}
- (size_t)lineNumber
{
return _lineNumber;
}
- (BOOL)finishedParsing
- (bool)finishedParsing
{
return _finishedParsing;
}
- (OFString*)string: (OFString*)string
containsUnknownEntityNamed: (OFString*)entity
{
|
︙ | | |