ObjFW  Diff

Differences From Artifact [0c4e28e6ff]:

To Artifact [84c8f6247c]:


8
9
10
11
12
13
14

15
16
17
18
19
20
21
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#import "config.h"

#import <stdarg.h>

#import <stdlib.h>
#import <string.h>
#import <limits.h>

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







>







8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#import "config.h"

#import <stdarg.h>
#import <stdio.h>
#import <stdlib.h>
#import <string.h>
#import <limits.h>

#import "OFXMLFactory.h"
#import "OFExceptions.h"
#import "OFMacros.h"
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
153
154
155
156
157
158
159
160








161
162
163
164
165
166
167
	return ret;
}

+ (char*)createStanza: (const char*)name
	 withCloseTag: (BOOL)close
	      andData: (const char*)data, ...
{
	char *arg, *val, *xml;
	size_t i, len;
	va_list args;

	/* Start of tag */
	len = strlen(name);
	if (SIZE_MAX - len < 3)
		@throw [OFOutOfRangeException newWithClass: self];
	len += 3;

	if ((xml = malloc(len)) == NULL)
		@throw [OFNoMemException newWithClass: self
					      andSize: len];

	i = 0;
	xml[i++] = '<';
	memcpy(xml + i, name, strlen(name));
	i += strlen(name);


	/* Arguments */





	va_start(args, data);

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

		@try {
			esc_val = [OFXMLFactory escapeCString: val];
		} @catch (OFException *e) {
			free(xml);
			@throw e;
		}

		@try {
			xf_resize_chars(&xml, &len, 1 + strlen(arg) + 2 +
			    strlen(esc_val) + 1, self);

			xml[i++] = ' ';
			memcpy(xml + i, arg, strlen(arg));
			i += strlen(arg);
			xml[i++] = '=';
			xml[i++] = '\'';
			memcpy(xml + i, esc_val, strlen(esc_val));
			i += strlen(esc_val);
			xml[i++] = '\'';
		} @finally {
			free(esc_val);
		}
	}
	va_end(args);









	/* End of tag */
	if (close) {
		if (data == NULL) {
			xf_resize_chars(&xml, &len, 2 - 1, self);

			xml[i++] = '/';







|


















>
|
>
>
>
>
>
|
>
|
|
|
<
<

<
<
<
|
<
<











|


|
|
>
>
>
>
>
>
>
>







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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
	return ret;
}

+ (char*)createStanza: (const char*)name
	 withCloseTag: (BOOL)close
	      andData: (const char*)data, ...
{
	char *arg, *val, *xml, *esc_val;
	size_t i, len;
	va_list args;

	/* Start of tag */
	len = strlen(name);
	if (SIZE_MAX - len < 3)
		@throw [OFOutOfRangeException newWithClass: self];
	len += 3;

	if ((xml = malloc(len)) == NULL)
		@throw [OFNoMemException newWithClass: self
					      andSize: len];

	i = 0;
	xml[i++] = '<';
	memcpy(xml + i, name, strlen(name));
	i += strlen(name);

	/*
	 * Arguments
	 *
	 * va_start / va_end need to be INSIDE the @try block due to a bug in
	 * gcc 4.0.1. (Only in Apple gcc?)
	 */
	@try {
		va_start(args, data);

		while ((arg = va_arg(args, char*)) != NULL &&
		    (val = va_arg(args, char*)) != NULL) {
			esc_val = NULL;	/* Needed for our @catch */


			esc_val = [OFXMLFactory escapeCString: val];






			xf_resize_chars(&xml, &len, 1 + strlen(arg) + 2 +
			    strlen(esc_val) + 1, self);

			xml[i++] = ' ';
			memcpy(xml + i, arg, strlen(arg));
			i += strlen(arg);
			xml[i++] = '=';
			xml[i++] = '\'';
			memcpy(xml + i, esc_val, strlen(esc_val));
			i += strlen(esc_val);
			xml[i++] = '\'';

			free(esc_val);
		}

		va_end(args);
	} @catch (OFException *e) {
		if (esc_val != NULL)
			free(esc_val);
		if (xml != NULL)
			free(xml);

		@throw e;
	}

	/* End of tag */
	if (close) {
		if (data == NULL) {
			xf_resize_chars(&xml, &len, 2 - 1, self);

			xml[i++] = '/';