ObjFW  Diff

Differences From Artifact [5ac96ead53]:

To Artifact [117b639361]:


12
13
14
15
16
17
18

19
20
21
22
23
24
25
#import <stdarg.h>
#import <stddef.h>
#import <stdlib.h>
#import <string.h>

#import "OFXMLFactory.h"
#import "OFExceptions.h"


/*
 * We don't use OFString in this file for performance reasons!
 *
 * We already have a clue about how big the resulting string will get, so we
 * can prealloc and only resize when really necessary - OFString would always
 * resize when we append, which would be slow here.







>







12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#import <stdarg.h>
#import <stddef.h>
#import <stdlib.h>
#import <string.h>

#import "OFXMLFactory.h"
#import "OFExceptions.h"
#import "OFMacros.h"

/*
 * We don't use OFString in this file for performance reasons!
 *
 * We already have a clue about how big the resulting string will get, so we
 * can prealloc and only resize when really necessary - OFString would always
 * resize when we append, which would be slow here.
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
	if ((ret = malloc(len + 1)) == NULL)
		[[OFNoMemException newWithObject: nil
					 andSize: len + 1] raise];

	for (i = j = 0; i < len; i++) {
		switch (s[i]) {
		case '<':
			if (!xf_add2chars(&ret, &nlen, &j, "&lt;"))
				[[OFNoMemException newWithObject: nil
							 andSize: nlen + 4]
				    raise];
			break;
		case '>':
			if (!xf_add2chars(&ret, &nlen, &j, "&gt;"))
				[[OFNoMemException newWithObject: nil
							 andSize: nlen + 4]
				    raise];
			break;
		case '"':
			if (!xf_add2chars(&ret, &nlen, &j, "&quot;"))

				[[OFNoMemException newWithObject: nil
							 andSize: nlen + 6]
				    raise];
			break;
		case '\'':
			if (!xf_add2chars(&ret, &nlen, &j, "&apos;"))

				[[OFNoMemException newWithObject: nil
							 andSize: nlen + 6]
				    raise];
			break;
		case '&':
			if (!xf_add2chars(&ret, &nlen, &j, "&amp;"))

				[[OFNoMemException newWithObject: nil
							 andSize: nlen + 5]
				    raise];
			break;
		default:
			ret[j++] = s[i];
			break;







|





|





|
>





|
>





|
>







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
153
154
155
156
	if ((ret = malloc(len + 1)) == NULL)
		[[OFNoMemException newWithObject: nil
					 andSize: len + 1] raise];

	for (i = j = 0; i < len; i++) {
		switch (s[i]) {
		case '<':
			if (OF_UNLIKELY(!xf_add2chars(&ret, &nlen, &j, "&lt;")))
				[[OFNoMemException newWithObject: nil
							 andSize: nlen + 4]
				    raise];
			break;
		case '>':
			if (OF_UNLIKELY(!xf_add2chars(&ret, &nlen, &j, "&gt;")))
				[[OFNoMemException newWithObject: nil
							 andSize: nlen + 4]
				    raise];
			break;
		case '"':
			if (OF_UNLIKELY(!xf_add2chars(&ret, &nlen, &j,
			    "&quot;")))
				[[OFNoMemException newWithObject: nil
							 andSize: nlen + 6]
				    raise];
			break;
		case '\'':
			if (OF_UNLIKELY(!xf_add2chars(&ret, &nlen, &j,
			    "&apos;")))
				[[OFNoMemException newWithObject: nil
							 andSize: nlen + 6]
				    raise];
			break;
		case '&':
			if (OF_UNLIKELY(!xf_add2chars(&ret, &nlen, &j,
			    "&amp;")))
				[[OFNoMemException newWithObject: nil
							 andSize: nlen + 5]
				    raise];
			break;
		default:
			ret[j++] = s[i];
			break;
169
170
171
172
173
174
175
176

177
178
179
180
181
182
183
184

185
186
187
188
189
190
191
192

193
194
195
196
197
198
199
200

201
202
203
204
205
206
207
208

209
210
211
212
213
214
215
		[[OFNoMemException newWithObject: nil
					 andSize: (len + 1) * sizeof(wchar_t)]
		     raise];

	for (i = j = 0; i < len; i++) {
		switch (s[i]) {
		case L'<':
			if (!xf_add2wchars(&ret, &nlen, &j, L"&lt;"))

				[[OFNoMemException newWithObject: nil
							 andSize: (nlen + 4) *
								  sizeof(
								  wchar_t)]
				    raise];
			break;
		case L'>':
			if (!xf_add2wchars(&ret, &nlen, &j, L"&gt;"))

				[[OFNoMemException newWithObject: nil
							 andSize: (nlen + 4) *
								  sizeof(
								  wchar_t)]
				    raise];
			break;
		case L'"':
			if (!xf_add2wchars(&ret, &nlen, &j, L"&quot;"))

				[[OFNoMemException newWithObject: nil
							 andSize: (nlen + 6) *
								  sizeof(
								  wchar_t)]
				    raise];
			break;
		case L'\'':
			if (!xf_add2wchars(&ret, &nlen, &j, L"&apos;"))

				[[OFNoMemException newWithObject: nil
							 andSize: (nlen + 6) *
								  sizeof(
								  wchar_t)]
				    raise];
			break;
		case L'&':
			if (!xf_add2wchars(&ret, &nlen, &j, L"&amp;"))

				[[OFNoMemException newWithObject: nil
							 andSize: (nlen + 5) *
								  sizeof(
								  wchar_t)]
				    raise];
			break;
		default:







|
>







|
>







|
>







|
>







|
>







173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
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
223
224
		[[OFNoMemException newWithObject: nil
					 andSize: (len + 1) * sizeof(wchar_t)]
		     raise];

	for (i = j = 0; i < len; i++) {
		switch (s[i]) {
		case L'<':
			if (OF_UNLIKELY(!xf_add2wchars(&ret, &nlen, &j,
			    L"&lt;")))
				[[OFNoMemException newWithObject: nil
							 andSize: (nlen + 4) *
								  sizeof(
								  wchar_t)]
				    raise];
			break;
		case L'>':
			if (OF_UNLIKELY(!xf_add2wchars(&ret, &nlen, &j,
			    L"&gt;")))
				[[OFNoMemException newWithObject: nil
							 andSize: (nlen + 4) *
								  sizeof(
								  wchar_t)]
				    raise];
			break;
		case L'"':
			if (OF_UNLIKELY(!xf_add2wchars(&ret, &nlen, &j,
			    L"&quot;")))
				[[OFNoMemException newWithObject: nil
							 andSize: (nlen + 6) *
								  sizeof(
								  wchar_t)]
				    raise];
			break;
		case L'\'':
			if (OF_UNLIKELY(!xf_add2wchars(&ret, &nlen, &j,
			    L"&apos;")))
				[[OFNoMemException newWithObject: nil
							 andSize: (nlen + 6) *
								  sizeof(
								  wchar_t)]
				    raise];
			break;
		case L'&':
			if (OF_UNLIKELY(!xf_add2wchars(&ret, &nlen, &j,
			    L"&amp;")))
				[[OFNoMemException newWithObject: nil
							 andSize: (nlen + 5) *
								  sizeof(
								  wchar_t)]
				    raise];
			break;
		default:
243
244
245
246
247
248
249

250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267

	/* Arguments */
	va_start(args, data);
	while ((arg = va_arg(args, char*)) != NULL &&
	    (val = va_arg(args, char*)) != NULL) {
		char *esc_val;


		if ((esc_val = [OFXMLFactory escapeCString: val]) == NULL) {
			/*
			 * escapeCString already throws an exception,
			 * no need to throw a second one here.
			 */
			free(xml);
			return NULL;
		}

		if (!xf_resize_chars(&xml, &len, 1 + strlen(arg) + 2 +
		    strlen(esc_val) + 1)) {
			free(esc_val);
			[[OFNoMemException newWithObject: nil
						 andSize: len + 1 +
							  strlen(arg) + 2 +
							  strlen(esc_val) + 1]
			    raise];
		}







>
|








|
|







252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277

	/* Arguments */
	va_start(args, data);
	while ((arg = va_arg(args, char*)) != NULL &&
	    (val = va_arg(args, char*)) != NULL) {
		char *esc_val;

		if (OF_UNLIKELY((esc_val =
		    [OFXMLFactory escapeCString: val]) == NULL)) {
			/*
			 * escapeCString already throws an exception,
			 * no need to throw a second one here.
			 */
			free(xml);
			return NULL;
		}

		if (OF_UNLIKELY(!xf_resize_chars(&xml, &len, 1 + strlen(arg) + 
		    2 + strlen(esc_val) + 1))) {
			free(esc_val);
			[[OFNoMemException newWithObject: nil
						 andSize: len + 1 +
							  strlen(arg) + 2 +
							  strlen(esc_val) + 1]
			    raise];
		}
338
339
340
341
342
343
344

345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362

	/* Arguments */
	va_start(args, data);
	while ((arg = va_arg(args, wchar_t*)) != NULL &&
	    (val = va_arg(args, wchar_t*)) != NULL) {
		wchar_t *esc_val;


		if ((esc_val = [OFXMLFactory escapeWideCString: val]) == NULL) {
			/*
			 * escapeWideCString already throws an exception,
			 * no need to throw a second one here.
			 */
			free(xml);
			return NULL;
		}

		if (!xf_resize_wchars(&xml, &len, 1 + wcslen(arg) + 2 +
		    wcslen(esc_val) + 1)) {
			free(esc_val);
			[[OFNoMemException newWithObject: nil
						 andSize: (len + 1 +
							  wcslen(arg) + 2 +
							  wcslen(esc_val) + 1) *
							  sizeof(wchar_t)]
			    raise];







>
|








|
|







348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373

	/* Arguments */
	va_start(args, data);
	while ((arg = va_arg(args, wchar_t*)) != NULL &&
	    (val = va_arg(args, wchar_t*)) != NULL) {
		wchar_t *esc_val;

		if (OF_UNLIKELY((esc_val =
		    [OFXMLFactory escapeWideCString: val]) == NULL)) {
			/*
			 * escapeWideCString already throws an exception,
			 * no need to throw a second one here.
			 */
			free(xml);
			return NULL;
		}

		if (OF_UNLIKELY(!xf_resize_wchars(&xml, &len, 1 + wcslen(arg) +
		    2 + wcslen(esc_val) + 1))) {
			free(esc_val);
			[[OFNoMemException newWithObject: nil
						 andSize: (len + 1 +
							  wcslen(arg) + 2 +
							  wcslen(esc_val) + 1) *
							  sizeof(wchar_t)]
			    raise];
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
		[[OFNoMemException newWithObject: nil
					 andSize: len] raise];

	memcpy(ret, strs[0], len - 1);
	pos = len - 1;

	for (i = 1; strs[i] != NULL; i++) {
		if (!xf_add2chars(&ret, &len, &pos, strs[i])) {
			free(ret);
			[[OFNoMemException newWithObject: nil
						 andSize: len + strlen(strs[i])]
			    raise];
		}
	}








|







441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
		[[OFNoMemException newWithObject: nil
					 andSize: len] raise];

	memcpy(ret, strs[0], len - 1);
	pos = len - 1;

	for (i = 1; strs[i] != NULL; i++) {
		if (OF_UNLIKELY(!xf_add2chars(&ret, &len, &pos, strs[i]))) {
			free(ret);
			[[OFNoMemException newWithObject: nil
						 andSize: len + strlen(strs[i])]
			    raise];
		}
	}