ObjFW  Check-in [d88aec8e95]

Overview
Comment:Don't use - raise anymore, but @throw.
- raise was only because at first, exceptions were self-raising, but
this was later changed so they had to be risen manually. - rise was
introduced for that, but it would've been better to use @throw
directly. Thus, this change now.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: d88aec8e9535af3d724f4ec3644ace24b182e95468e7d88c1ab26a2a8ea57374
User & Date: js on 2008-12-09 17:36:44
Other Links: manifest | tags
Context
2008-12-10
17:53
Always use [self alloc] in + new.
This way, derivated classes are not forced to always override + new.
check-in: f48ee629e3 user: js tags: trunk
2008-12-09
17:36
Don't use - raise anymore, but @throw.
- raise was only because at first, exceptions were self-raising, but
this was later changed so they had to be risen manually. - rise was
introduced for that, but it would've been better to use @throw
directly. Thus, this change now.
check-in: d88aec8e95 user: js tags: trunk
2008-12-08
16:51
Don't allow connecting/binding on an already opened socket.
Also, free mem on accepted sockets when close is called.
check-in: c8990ecd12 user: js tags: trunk
Changes

Modified src/OFArray.m from [c0503870b0] to [190e0d7eea].

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
105
106
107
108
109
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
105
106
107
108
109







-
+












-
+














-
+














-
+







{
	return data;
}

- (void*)item: (size_t)item
{
	if (item >= items)
		[[OFOutOfRangeException newWithObject: self] raise];
		@throw [OFOutOfRangeException newWithObject: self];

	return data + item * itemsize;
}

- (void*)last
{
	return data + (items - 1) * itemsize;
}

- add: (void*)item
{
	if (SIZE_MAX - items < 1)
		[[OFOutOfRangeException newWithObject: self] raise];
		@throw [OFOutOfRangeException newWithObject: self];

	data = [self resizeMem: data
		      toNItems: items + 1
			ofSize: itemsize];

	memcpy(data + items++ * itemsize, item, itemsize);

	return self;
}

- addNItems: (size_t)nitems
 fromCArray: (void*)carray
{
	if (nitems > SIZE_MAX - items)
		[[OFOutOfRangeException newWithObject: self] raise];
		@throw [OFOutOfRangeException newWithObject: self];

	data = [self resizeMem: data
		      toNItems: items + nitems
			ofSize: itemsize];

	memcpy(data + items * itemsize, carray, nitems * itemsize);
	items += nitems;

	return self;
}

- removeNItems: (size_t)nitems
{
	if (nitems > items)
		[[OFOutOfRangeException newWithObject: self] raise];
		@throw [OFOutOfRangeException newWithObject: self];

	data = [self resizeMem: data
		      toNItems: items - nitems
			ofSize: itemsize];

	items -= nitems;

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

177
178
179
180
181
182
183
184
185
186
187
188
189
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
177
178
179
180
181
182
183
184
185
186
187
188
189







-
+



















-
+



















-
+













}

- add: (void*)item
{
	size_t nsize;

	if (SIZE_MAX - items < 1 || items + 1 > SIZE_MAX / itemsize)
		[[OFOutOfRangeException newWithObject: self] raise];
		@throw [OFOutOfRangeException newWithObject: self];

	nsize = ((items + 1) * itemsize + lastpagebyte) & ~lastpagebyte;

	if (size != nsize)
		data = [self resizeMem: data
				toSize: nsize];

	memcpy(data + items++ * itemsize, item, itemsize);
	size = nsize;

	return self;
}

- addNItems: (size_t)nitems
 fromCArray: (void*)carray
{
	size_t nsize;

	if (nitems > SIZE_MAX - items || items + nitems > SIZE_MAX / itemsize)
		[[OFOutOfRangeException newWithObject: self] raise];
		@throw [OFOutOfRangeException newWithObject: self];

	nsize = ((items + nitems) * itemsize + lastpagebyte) & ~lastpagebyte;

	if (size != nsize)
		data = [self resizeMem: data
				toSize: nsize];

	memcpy(data + items * itemsize, carray, nitems * itemsize);
	items += nitems;
	size = nsize;

	return self;
}

- removeNItems: (size_t)nitems
{
	size_t nsize;

	if (nitems > items)
		[[OFOutOfRangeException newWithObject: self] raise];
		@throw [OFOutOfRangeException newWithObject: self];

	nsize = ((items - nitems) * itemsize + lastpagebyte) & ~lastpagebyte;

	if (size != nsize)
		data = [self resizeMem: data
				toSize: nsize];

	items -= nitems;
	size = nsize;

	return self;
}
@end

Modified src/OFExceptions.h from [60259d3ade] to [c0dcb82286].

36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
36
37
38
39
40
41
42






43
44
45
46
47
48
49







-
-
-
-
-
-







 * \param obj The object which caused the exception
 * \return An initialized OFException
 */
- initWithObject: (id)obj;

- free;

/**
 * Raises an OFException and aborts execution if the exception is not caught.
 */

- raise;

/**
 * \return An error message for the exception as a C String
 */
- (char*)cString;
@end

/**

Modified src/OFExceptions.m from [3ae2ac34e6] to [c6047432b6].

48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
48
49
50
51
52
53
54






55
56
57
58
59
60
61







-
-
-
-
-
-







{
	if (string != NULL)
		free(string);

	return [super free];
}

- raise
{
	@throw self;
	return self;
}

- (char*)cString
{
	return string;
}
@end

@implementation OFNoMemException

Modified src/OFFile.m from [249d4d336a] to [88173cbff8].

65
66
67
68
69
70
71
72
73
74



75
76
77
78
79
80
81
65
66
67
68
69
70
71



72
73
74
75
76
77
78
79
80
81







-
-
-
+
+
+







}

- initWithPath: (const char*)path
       andMode: (const char*)mode
{
	if ((self = [super init])) {
		if ((fp = fopen(path, mode)) == NULL)
			[[OFOpenFileFailedException newWithObject: self
							  andPath: path
							  andMode: mode] raise];
			@throw [OFOpenFileFailedException newWithObject: self
								andPath: path
								andMode: mode];
	}
	return self;
}

- free
{
	fclose(fp);
90
91
92
93
94
95
96
97
98
99



100
101
102
103
104
105
106
90
91
92
93
94
95
96



97
98
99
100
101
102
103
104
105
106







-
-
-
+
+
+







- (size_t)readNItems: (size_t)nitems
	      ofSize: (size_t)size
	  intoBuffer: (uint8_t*)buf
{
	size_t ret;

	if ((ret = fread(buf, size, nitems, fp)) == 0 && !feof(fp))
		[[OFReadFailedException newWithObject: self
					      andSize: size
					    andNItems: nitems] raise];
		@throw [OFReadFailedException newWithObject: self
						    andSize: size
						  andNItems: nitems];

	return ret;
}

- (size_t)readNBytes: (size_t)size
	  intoBuffer: (uint8_t*)buf
{
139
140
141
142
143
144
145
146
147
148



149
150
151
152
153
154
155
139
140
141
142
143
144
145



146
147
148
149
150
151
152
153
154
155







-
-
-
+
+
+







	       ofSize: (size_t)size
	   fromBuffer: (const uint8_t*)buf
{
	size_t ret;

	if ((ret = fwrite(buf, size, nitems, fp)) == 0 &&
	    size != 0 && nitems != 0)
		[[OFWriteFailedException newWithObject: self
					       andSize: size
					     andNItems: nitems] raise];
		@throw [OFWriteFailedException newWithObject: self
						     andSize: size
						   andNItems: nitems];
	
	return ret;
}

- (size_t)writeNBytes: (size_t)size
	   fromBuffer: (const uint8_t*)buf
{

Modified src/OFObject.m from [16b59ee246] to [19577f9cbf].

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
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







-
+



-
-
+
+



-
-
+
+
















-
+







	if (size == 0)
		return NULL;

	memchunks_size = __memchunks_size + 1;

	if (SIZE_MAX - __memchunks_size == 0 ||
	    memchunks_size > SIZE_MAX / sizeof(void*))
		[[OFOutOfRangeException newWithObject: self] raise];
		@throw [OFOutOfRangeException newWithObject: self];
	
	if ((memchunks = realloc(__memchunks,
	    memchunks_size * sizeof(void*))) == NULL)
		[[OFNoMemException newWithObject: self
					 andSize: memchunks_size] raise];
		@throw [OFNoMemException newWithObject: self
					       andSize: memchunks_size];

	if ((ptr = malloc(size)) == NULL) {
		free(memchunks);
		[[OFNoMemException newWithObject: self
					 andSize: size] raise];
		@throw [OFNoMemException newWithObject: self
					       andSize: size];
	}

	__memchunks = memchunks;
	__memchunks[__memchunks_size] = ptr;
	__memchunks_size = memchunks_size;

	return ptr;
}

- (void*)getMemForNItems: (size_t)nitems
		  ofSize: (size_t)size
{
	if (nitems == 0 || size == 0)
		return NULL;

	if (nitems > SIZE_MAX / size)
		[[OFOutOfRangeException newWithObject: self] raise];
		@throw [OFOutOfRangeException newWithObject: self];

	return [self getMemWithSize: nitems * size];
}

- (void*)resizeMem: (void*)ptr
	    toSize: (size_t)size
{
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
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







-
-
+
+






-
-
+
+



















-
+







	}

	iter = __memchunks + __memchunks_size;

	while (iter-- > __memchunks) {
		if (OF_UNLIKELY(*iter == ptr)) {
			if (OF_UNLIKELY((ptr = realloc(ptr, size)) == NULL))
				[[OFNoMemException newWithObject: self
							 andSize: size] raise];
				@throw [OFNoMemException newWithObject: self
							       andSize: size];
			
			*iter = ptr;
			return ptr;
		}
	}

	[[OFMemNotPartOfObjException newWithObject: self
					andPointer: ptr] raise];
	@throw [OFMemNotPartOfObjException newWithObject: self
					      andPointer: ptr];
	return NULL;	/* never reached, but makes gcc happy */
}

- (void*)resizeMem: (void*)ptr
	  toNItems: (size_t)nitems
	    ofSize: (size_t)size
{
	size_t memsize;

	if (ptr == NULL)
		return [self getMemForNItems: nitems
				      ofSize: size];
	
	if (nitems == 0 || size == 0) {
		[self freeMem: ptr];
		return NULL;
	}

	if (nitems > SIZE_MAX / size)
		[[OFOutOfRangeException newWithObject: self] raise];
		@throw [OFOutOfRangeException newWithObject: self];

	memsize = nitems * size;
	return [self resizeMem: ptr
			toSize: memsize];
}

- freeMem: (void*)ptr;
159
160
161
162
163
164
165
166
167


168
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
159
160
161
162
163
164
165


166
167
168
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







-
-
+
+













+
-
-
+
+
-
-










-
-
+
+




		if (OF_UNLIKELY(*iter == ptr)) {
			memchunks_size = __memchunks_size - 1;
			last = __memchunks[memchunks_size];

			if (OF_UNLIKELY(__memchunks_size == 0 ||
			    memchunks_size > SIZE_MAX / sizeof(void*)))
				[[OFOutOfRangeException newWithObject: self]
				    raise];
				@throw [OFOutOfRangeException
				    newWithObject: self];

			if (OF_UNLIKELY(memchunks_size == 0)) {
				free(ptr);
				free(__memchunks);

				__memchunks = NULL;
				__memchunks_size = 0;

				return self;
			}

			if (OF_UNLIKELY((memchunks = realloc(__memchunks,
			    memchunks_size * sizeof(void*))) == NULL))
				@throw [OFNoMemException
				[[OFNoMemException newWithObject: self
							 andSize:
				    newWithObject: self
					  andSize: memchunks_size];
							     memchunks_size]
				    raise];

			free(ptr);
			__memchunks = memchunks;
			__memchunks[i] = last;
			__memchunks_size = memchunks_size;

			return self;
		}
	}

	[[OFMemNotPartOfObjException newWithObject: self
					andPointer: ptr] raise];
	@throw [OFMemNotPartOfObjException newWithObject: self
					      andPointer: ptr];
	return self;	/* never reached, but makes gcc happy */
}
@end

Modified src/OFXMLFactory.m from [b1d2c0931c] to [cb13f5e9ce].

31
32
33
34
35
36
37
38

39
40
41
42
43
44
45
31
32
33
34
35
36
37

38
39
40
41
42
43
44
45







-
+







static inline BOOL
xf_resize_chars(char **str, size_t *len, size_t add)
{
	char *str2;
	size_t len2;

	if (add > SIZE_MAX - *len)
		[[OFOutOfRangeException newWithObject: nil] raise];
		@throw [OFOutOfRangeException newWithObject: nil];
	len2 = *len + add;
	
	if ((str2 = realloc(*str, len2)) == NULL) {
		if (*str)
			free(*str);
		*str = NULL;
		return NO;
54
55
56
57
58
59
60
61

62
63
64
65

66
67
68
69
70
71
72
54
55
56
57
58
59
60

61
62
63
64

65
66
67
68
69
70
71
72







-
+



-
+







static inline BOOL
xf_resize_wchars(wchar_t **str, size_t *len, size_t add)
{
	wchar_t *str2;
	size_t len2;

	if (add > SIZE_MAX - *len)
		[[OFOutOfRangeException newWithObject: nil] raise];
		@throw [OFOutOfRangeException newWithObject: nil];
	len2 = *len + add;

	if (len2 > SIZE_MAX / sizeof(wchar_t))
		[[OFOutOfRangeException newWithObject: nil] raise];
		@throw [OFOutOfRangeException newWithObject: nil];
	
	if ((str2 = realloc(*str, len2 * sizeof(wchar_t))) == NULL) {
		if (*str)
			free(*str);
		*str = NULL;
		return NO;
	}
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
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



225
226
227
228
229
230

231
232
233



234
235
236
237
238
239
240
241
242
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
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
225
226



227
228
229


230
231
232
233
234
235
236







-
+



-
-
+
+





+
-
-
+
+
-



+
-
-
+
+
-




+
-
-
+
+
-




+
-
-
+
+
-




+
-
-
+
+
-


















-
+



-
+


-
-
+
+
-






+
-
-
-
+
+
+
-
-




+
-
-
-
+
+
+
-
-




+
-
-
-
+
+
+
-
-




+
-
-
-
+
+
+
-
-




+
-
-
-
+
+
+
-
-







+ (char*)escapeCString: (const char*)s
{
	char *ret;
	size_t i, j, len, nlen;

	len = nlen = strlen(s);
	if (SIZE_MAX - len < 1)
		[[OFOutOfRangeException newWithObject: nil] raise];
		@throw [OFOutOfRangeException newWithObject: nil];
	nlen++;

	if ((ret = malloc(nlen)) == NULL)
		[[OFNoMemException newWithObject: nil
					 andSize: nlen] raise];
		@throw [OFNoMemException newWithObject: nil
					       andSize: nlen];

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

	ret[j] = 0;
	return ret;
}

+ (wchar_t*)escapeWideCString: (const wchar_t*)s
{
	wchar_t *ret;
	size_t i, j, len, nlen;

	len = nlen = wcslen(s);
	if (SIZE_MAX - len < 1)
		[[OFOutOfRangeException newWithObject: nil] raise];
		@throw [OFOutOfRangeException newWithObject: nil];
	nlen++;

	if (nlen > SIZE_MAX / sizeof(wchar_t))
		[[OFOutOfRangeException newWithObject: nil] raise];
		@throw [OFOutOfRangeException newWithObject: nil];

	if ((ret = malloc(nlen * sizeof(wchar_t))) == NULL)
		[[OFNoMemException newWithObject: nil
					 andSize: nlen * sizeof(wchar_t)]
		@throw [OFNoMemException newWithObject: nil
					       andSize: nlen * 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;")))
				@throw [OFNoMemException
				[[OFNoMemException newWithObject: nil
							 andSize: (nlen + 4) *
								  sizeof(
				    newWithObject: nil
					  andSize: (nlen + 4) *
						   sizeof(wchar_t)];
								  wchar_t)]
				    raise];
			break;
		case L'>':
			if (OF_UNLIKELY(!xf_add2wchars(&ret, &nlen, &j,
			    L"&gt;")))
				@throw [OFNoMemException
				[[OFNoMemException newWithObject: nil
							 andSize: (nlen + 4) *
								  sizeof(
				    newWithObject: nil
					  andSize: (nlen + 4) *
						   sizeof(wchar_t)];
								  wchar_t)]
				    raise];
			break;
		case L'"':
			if (OF_UNLIKELY(!xf_add2wchars(&ret, &nlen, &j,
			    L"&quot;")))
				@throw [OFNoMemException
				[[OFNoMemException newWithObject: nil
							 andSize: (nlen + 6) *
								  sizeof(
				    newWithObject: nil
					  andSize: (nlen + 6) *
						   sizeof(wchar_t)];
								  wchar_t)]
				    raise];
			break;
		case L'\'':
			if (OF_UNLIKELY(!xf_add2wchars(&ret, &nlen, &j,
			    L"&apos;")))
				@throw [OFNoMemException
				[[OFNoMemException newWithObject: nil
							 andSize: (nlen + 6) *
								  sizeof(
				    newWithObject: nil
					  andSize: (nlen + 6) *
						   sizeof(wchar_t)];
								  wchar_t)]
				    raise];
			break;
		case L'&':
			if (OF_UNLIKELY(!xf_add2wchars(&ret, &nlen, &j,
			    L"&amp;")))
				@throw [OFNoMemException
				[[OFNoMemException newWithObject: nil
							 andSize: (nlen + 5) *
								  sizeof(
				    newWithObject: nil
					  andSize: (nlen + 5) *
						   sizeof(wchar_t)];
								  wchar_t)]
				    raise];
			break;
		default:
			ret[j++] = s[i];
			break;
		}
	}

251
252
253
254
255
256
257
258

259
260
261
262
263


264
265
266
267
268
269
270
245
246
247
248
249
250
251

252
253
254
255


256
257
258
259
260
261
262
263
264







-
+



-
-
+
+







	char *arg, *val, *xml;
	size_t i, len;
	va_list args;

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

	if ((xml = malloc(len)) == NULL)
		[[OFNoMemException newWithObject: nil
					 andSize: len] raise];
		@throw [OFNoMemException newWithObject: nil
					       andSize: len];

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

	/* Arguments */
282
283
284
285
286
287
288

289
290


291
292

293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312

313
314


315
316
317
318
319
320
321

322
323


324
325
326

327
328
329
330
331
332
333
334
335
276
277
278
279
280
281
282
283


284
285


286

287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306


307
308

309
310
311
312
313
314
315


316
317



318


319
320
321
322
323
324
325







+
-
-
+
+
-
-
+
-



















+
-
-
+
+
-






+
-
-
+
+
-
-
-
+
-
-







			free(xml);
			return NULL;
		}

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

		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);

	/* End of tag */
	if (close) {
		if (data == NULL) {
			if (!xf_resize_chars(&xml, &len, 2 - 1))
				@throw [OFNoMemException
				[[OFNoMemException newWithObject: nil
							 andSize: len + 2 - 1]
				    newWithObject: nil
					  andSize: len + 2 - 1];
				    raise];
	
			xml[i++] = '/';
			xml[i++] = '>';
		} else {
			if (!xf_resize_chars(&xml, &len, 1 + strlen(data) +
			    2 + strlen(name) + 1 - 1))
				@throw [OFNoMemException
				[[OFNoMemException newWithObject: nil
							 andSize: len + 1 +
				    newWithObject: nil
					  andSize: len + 1 + strlen(data) + 2 +
								  strlen(data) +
								  2 +
								  strlen(name) +
						   strlen(name) + 1 - 1];
								  1 - 1]
				    raise];
	
			xml[i++] = '>';
			memcpy(xml + i, data, strlen(data));
			i += strlen(data);
			xml[i++] = '<';
			xml[i++] = '/';
			memcpy(xml + i, name, strlen(name));
350
351
352
353
354
355
356
357

358
359
360
361

362
363
364
365


366
367
368
369
370
371
372
340
341
342
343
344
345
346

347
348
349
350

351
352
353


354
355
356
357
358
359
360
361
362







-
+



-
+


-
-
+
+







	wchar_t *arg, *val, *xml;
	size_t i, len;
	va_list args;

	/* Start of tag */
	len = wcslen(name);
	if (SIZE_MAX - len < 3)
		[[OFOutOfRangeException newWithObject: nil] raise];
		@throw [OFOutOfRangeException newWithObject: nil];
	len += 3;

	if (len > SIZE_MAX / sizeof(wchar_t))
		[[OFOutOfRangeException newWithObject: nil] raise];
		@throw [OFOutOfRangeException newWithObject: nil];

	if ((xml = malloc(len * sizeof(wchar_t))) == NULL)
		[[OFNoMemException newWithObject: nil
					 andSize: len * sizeof(wchar_t)] raise];
		@throw [OFNoMemException newWithObject: nil
					       andSize: len * sizeof(wchar_t)];

	i = 0;
	xml[i++] = L'<';
	wmemcpy(xml + i, name, wcslen(name));
	i += wcslen(name);

	/* Arguments */
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
411
412
413
414
415

416
417


418
419

420
421
422
423
424
425
426

427
428


429
430
431

432
433
434

435
436
437
438
439
440
441
442
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
411
412
413
414
415


416
417



418



419

420
421
422
423
424
425
426







+
-
-
+
+
-
-
-
+
+
-



















+
-
-
+
+
-
-
+
-






+
-
-
+
+
-
-
-
+
-
-
-
+
-







			free(xml);
			return NULL;
		}

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

		xml[i++] = L' ';
		wmemcpy(xml + i, arg, wcslen(arg));
		i += wcslen(arg);
		xml[i++] = L'=';
		xml[i++] = L'\'';
		wmemcpy(xml + i, esc_val, wcslen(esc_val));
		i += wcslen(esc_val);
		xml[i++] = L'\'';

		free(esc_val);
	}
	va_end(args);

	/* End of tag */
	if (close) {
		if (data == NULL) {
			if (!xf_resize_wchars(&xml, &len, 2 - 1))
				@throw [OFNoMemException
				[[OFNoMemException newWithObject: nil
							 andSize: (len + 2
				    newWithObject: nil
					  andSize: (len + 2 - 1) *
								  - 1) * sizeof(
								  wchar_t)]
						   sizeof(wchar_t)];
				    raise];
	
			xml[i++] = L'/';
			xml[i++] = L'>';
		} else {
			if (!xf_resize_wchars(&xml, &len, 1 + wcslen(data) +
			    2 + wcslen(name) + 1 - 1))
				@throw [OFNoMemException
				[[OFNoMemException newWithObject: nil
							 andSize: (len + 1 +
				    newWithObject: nil
					  andSize: (len + 1 + wcslen(data) + 2 +
								  wcslen(data) +
								  2 +
								  wcslen(name) +
						   wcslen(name) + 1 - 1) *
								  1 -
								  1) * sizeof(
								  wchar_t)]
						   sizeof(wchar_t)];
				    raise];
	
			xml[i++] = L'>';
			wmemcpy(xml + i, data, wcslen(data));
			i += wcslen(data);
			xml[i++] = L'<';
			xml[i++] = L'/';
			wmemcpy(xml + i, name, wcslen(name));
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
506
507


508
509
510
511
512
513
514

515
516


517
518

519
520
521
522
523
524
525
526
527
528
529
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
506
507
508
509
510
511
512







-
+



-
-
+
+







+
-
-
+
+
-




















-
+



-
+


-
-
+
+







+
-
-
+
+
-
-
+
-










	size_t i, len, pos;

	if (strs[0] == NULL)
		return NULL;

	len = strlen(*strs);
	if (SIZE_MAX - len < 1)
		[[OFOutOfRangeException newWithObject: nil] raise];
		@throw [OFOutOfRangeException newWithObject: nil];
	len++;
	
	if ((ret = malloc(len)) == NULL)
		[[OFNoMemException newWithObject: nil
					 andSize: len] raise];
		@throw [OFNoMemException newWithObject: nil
					       andSize: len];

	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);
			@throw [OFNoMemException
			[[OFNoMemException newWithObject: nil
						 andSize: len + strlen(strs[i])]
			    newWithObject: nil
				  andSize: len + strlen(strs[i])];
			    raise];
		}
	}

	for (i = 0; strs[i] != NULL; i++)
		free(strs[i]);

	ret[pos] = 0;
	return ret;
}

+ (wchar_t*)concatAndFreeWideCStrings: (wchar_t**)strs
{
	wchar_t *ret;
	size_t i, len, pos;

	if (strs[0] == NULL)
		return NULL;

	len = wcslen(*strs);
	if (SIZE_MAX - len < 1)
		[[OFOutOfRangeException newWithObject: nil] raise];
		@throw [OFOutOfRangeException newWithObject: nil];
	len++;

	if (len > SIZE_MAX - sizeof(wchar_t))
		[[OFOutOfRangeException newWithObject: nil] raise];
		@throw [OFOutOfRangeException newWithObject: nil];

	if ((ret = malloc(len * sizeof(wchar_t))) == NULL)
		[[OFNoMemException newWithObject: nil
					 andSize: len * sizeof(wchar_t)] raise];
		@throw [OFNoMemException newWithObject: nil
					       andSize: len * sizeof(wchar_t)];

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

	for (i = 1; strs[i] != NULL; i++) {
		if (!xf_add2wchars(&ret, &len, &pos, strs[i])) {
			free(ret);
			@throw [OFNoMemException
			[[OFNoMemException newWithObject: nil
						 andSize: (wcslen(strs[i]) +
			    newWithObject: nil
				  andSize: (wcslen(strs[i]) + len) *
							  len) * sizeof(
							  wchar_t)]
					   sizeof(wchar_t)];
			    raise];
		}
	}

	for (i = 0; strs[i] != NULL; i++)
		free(strs[i]);

	ret[pos] = 0;
	return ret;
}
@end