ObjFW  Diff

Differences From Artifact [c2066f8693]:

To Artifact [bbfbe023ae]:


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

int _OFString_XMLEscaping_reference;

@implementation OFString (XMLEscaping)
- (OFString*)stringByXMLEscaping
{
	char *str_c, *tmp;
	const char *append;
	size_t len, append_len;
	size_t i, j;
	OFString *ret;

	j = 0;
	len = length;

	/*
	 * We can't use allocMemoryWithSize: here as it might be a @"" literal
	 */
	if ((str_c = malloc(len)) == NULL)
		@throw [OFOutOfMemoryException newWithClass: isa
					      requestedSize: len];

	for (i = 0; i < length; i++) {
		switch (string[i]) {
			case '<':
				append = "&lt;";
				append_len = 4;
				break;
			case '>':
				append = "&gt;";
				append_len = 4;
				break;
			case '"':
				append = "&quot;";
				append_len = 6;
				break;
			case '\'':
				append = "&apos;";
				append_len = 6;
				break;
			case '&':
				append = "&amp;";
				append_len = 5;
				break;
			default:
				append = NULL;
				append_len = 0;
		}

		if (append != NULL) {

			if ((tmp = realloc(str_c, len + append_len)) == NULL) {


				free(str_c);
				@throw [OFOutOfMemoryException
				     newWithClass: isa
				    requestedSize: len + append_len];
			}
			str_c = tmp;
			len += append_len - 1;

			memcpy(str_c + j, append, append_len);
			j += append_len;
		} else
			str_c[j++] = string[i];
	}

	assert(j == len);

	@try {
		ret = [OFString stringWithCString: str_c
					   length: len];
	} @finally {
		free(str_c);
	}
	return ret;
}
@end







|

|




|




|

|





|



|



|



|



|



|



>
|
>
>
|


|

|
|

|
|

|


|


|
|

|




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

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 = "&lt;";
				appendLength = 4;
				break;
			case '>':
				append = "&gt;";
				appendLength = 4;
				break;
			case '"':
				append = "&quot;";
				appendLength = 6;
				break;
			case '\'':
				append = "&apos;";
				appendLength = 6;
				break;
			case '&':
				append = "&amp;";
				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 {
		ret = [OFString stringWithCString: retCString
					   length: retLength];
	} @finally {
		free(retCString);
	}
	return ret;
}
@end