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
|
int _OFString_XMLEscaping_reference;
@implementation OFString (XMLEscaping)
- (OFString*)stringByXMLEscaping
{
char *retCString;
const char *append;
size_t retLength, appendLength;
size_t i, j;
OFString *ret;
j = 0;
retLength = length;
/*
* We can't use allocMemoryWithSize: here as it might be a @"" literal
*/
if ((retCString = malloc(retLength)) == NULL)
@throw [OFOutOfMemoryException newWithClass: isa
requestedSize: retLength];
for (i = 0; i < length; i++) {
switch (string[i]) {
case '<':
append = "<";
appendLength = 4;
break;
case '>':
append = ">";
appendLength = 4;
break;
case '"':
append = """;
appendLength = 6;
break;
case '\'':
append = "'";
appendLength = 6;
break;
case '&':
append = "&";
appendLength = 5;
break;
default:
append = NULL;
appendLength = 0;
}
if (append != NULL) {
char *newRetCString;
if ((newRetCString = realloc(retCString,
retLength + appendLength)) == NULL) {
free(retCString);
@throw [OFOutOfMemoryException
newWithClass: isa
requestedSize: retLength + appendLength];
}
retCString = newRetCString;
retLength += appendLength - 1;
memcpy(retCString + j, append, appendLength);
j += appendLength;
} else
retCString[j++] = string[i];
}
assert(j == retLength);
@try {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
int _OFString_XMLEscaping_reference;
@implementation OFString (XMLEscaping)
- (OFString*)stringByXMLEscaping
{
char *retCString;
const char *append;
size_t retLength, appendLen;
size_t i, j;
OFString *ret;
j = 0;
retLength = length;
/*
* We can't use allocMemoryWithSize: here as it might be a @"" literal
*/
if ((retCString = malloc(retLength)) == NULL)
@throw [OFOutOfMemoryException newWithClass: isa
requestedSize: retLength];
for (i = 0; i < length; i++) {
switch (string[i]) {
case '<':
append = "<";
appendLen = 4;
break;
case '>':
append = ">";
appendLen = 4;
break;
case '"':
append = """;
appendLen = 6;
break;
case '\'':
append = "'";
appendLen = 6;
break;
case '&':
append = "&";
appendLen = 5;
break;
default:
append = NULL;
appendLen = 0;
}
if (append != NULL) {
char *newRetCString;
if ((newRetCString = realloc(retCString,
retLength + appendLen)) == NULL) {
free(retCString);
@throw [OFOutOfMemoryException
newWithClass: isa
requestedSize: retLength + appendLen];
}
retCString = newRetCString;
retLength += appendLen - 1;
memcpy(retCString + j, append, appendLen);
j += appendLen;
} else
retCString[j++] = string[i];
}
assert(j == retLength);
@try {
|