ObjFW  Check-in [139591afe1]

Overview
Comment:Use isa instead of [self class].

Since we don't use Object anymore and define isa in OFObject, we can
rely on it.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 139591afe1abb02e8bc53b54fa08100f87e64dae5ef403dc19967aab43b2d123
User & Date: js on 2009-04-19 20:34:38
Other Links: manifest | tags
Context
2009-04-19
23:17
Remove long double from OFNumber as there's no type encoding for it. check-in: 8eb830d7c8 user: js tags: trunk
20:34
Use isa instead of [self class]. check-in: 139591afe1 user: js tags: trunk
20:29
Remove stuff forgotten after moving code in OFPlugin. check-in: 066bf9c8c3 user: js tags: trunk
Changes

Modified src/OFArray.m from [b65574e4bc] to [3315367580].

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

110
111
112
113
114
115
116
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
110
111
112
113
114
115
116







-
+












-
+














-
+














-
+







{
	return data;
}

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

	return data + item * itemsize;
}

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

- add: (void*)item
{
	if (SIZE_MAX - items < 1 || items + 1 > SIZE_MAX / itemsize)
		@throw [OFOutOfRangeException newWithClass: [self class]];
		@throw [OFOutOfRangeException newWithClass: isa];

	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 || items + nitems > SIZE_MAX / itemsize)
		@throw [OFOutOfRangeException newWithClass: [self class]];
		@throw [OFOutOfRangeException newWithClass: isa];

	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)
		@throw [OFOutOfRangeException newWithClass: [self class]];
		@throw [OFOutOfRangeException newWithClass: isa];

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

	items -= nitems;

140
141
142
143
144
145
146
147

148
149
150

151
152
153
154
155
156
157
140
141
142
143
144
145
146

147
148
149

150
151
152
153
154
155
156
157







-
+


-
+







}

- (int)compare: (id)obj
{
	int ret;

	if (![obj isKindOf: [OFArray class]])
		@throw [OFInvalidArgumentException newWithClass: [self class]
		@throw [OFInvalidArgumentException newWithClass: isa
						    andSelector: _cmd];
	if ([obj itemsize] != itemsize)
		@throw [OFInvalidArgumentException newWithClass: [self class]
		@throw [OFInvalidArgumentException newWithClass: isa
						    andSelector: _cmd];

	if ([obj items] == items)
		return memcmp(data, [obj data], items * itemsize);

	if (items > [obj items]) {
		if ((ret = memcmp(data, [obj data], [obj items] * itemsize)))
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
243
244
245
246
247
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
243
244
245
246
247







-
+



















-
+



















-
+







}

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

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

	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)
		@throw [OFOutOfRangeException newWithClass: [self class]];
		@throw [OFOutOfRangeException newWithClass: isa];

	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)
		@throw [OFOutOfRangeException newWithClass: [self class]];
		@throw [OFOutOfRangeException newWithClass: isa];

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

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

Modified src/OFConstString.m from [34a543db8a] to [4dca2f32e1].

54
55
56
57
58
59
60
61

62
63
64
65
66
67
68
54
55
56
57
58
59
60

61
62
63
64
65
66
67
68







-
+







	return YES;
}

- (int)compare: (id)obj
{
	if (![obj isKindOf: [OFString class]] &&
	    ![obj isKindOf: [OFConstString class]])
		@throw [OFInvalidArgumentException newWithClass: [self class]];
		@throw [OFInvalidArgumentException newWithClass: isa];

	return strcmp(string, [obj cString]);
}

- (uint32_t)hash
{
	uint32_t hash;

Modified src/OFDictionary.m from [065f154d58] to [ae861b76c6].

45
46
47
48
49
50
51
52

53
54
55
56
57
58
59
45
46
47
48
49
50
51

52
53
54
55
56
57
58
59







-
+







	return self;
}

- initWithHashSize: (int)hashsize
{
	if ((self = [super init])) {
		if (hashsize < 8 || hashsize > 31) {
			Class c = [self class];
			Class c = isa;
			[self free];
			@throw [OFInvalidArgumentException
			    newWithClass: c
			     andSelector: _cmd];
		}

		size = (size_t)1 << hashsize;

Modified src/OFFile.m from [08cbbc44cd] to [de67b84ef2].

80
81
82
83
84
85
86
87

88
89
90
91
92
93
94
80
81
82
83
84
85
86

87
88
89
90
91
92
93
94







-
+







- initWithPath: (const char*)path
       andMode: (const char*)mode
{
	Class c;

	if ((self = [super init])) {
		if ((fp = fopen(path, mode)) == NULL) {
			c = [self class];
			c = isa;
			[super free];
			@throw [OFOpenFileFailedException newWithClass: c 
							       andPath: path
							       andMode: mode];
		}
	}
	return self;
109
110
111
112
113
114
115
116

117
118
119
120
121
122
123
109
110
111
112
113
114
115

116
117
118
119
120
121
122
123







-
+







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

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

	return ret;
}

- (size_t)readNBytes: (size_t)size
132
133
134
135
136
137
138
139

140
141
142
143
144
145
146
132
133
134
135
136
137
138

139
140
141
142
143
144
145
146







-
+







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

	if ((ret = fwrite(buf, size, nitems, fp)) == 0 &&
	    size != 0 && nitems != 0)
		@throw [OFWriteFailedException newWithClass: [self class]
		@throw [OFWriteFailedException newWithClass: isa
						    andSize: size
						  andNItems: nitems];

	return ret;
}

- (size_t)writeNBytes: (size_t)size

Modified src/OFNumber.m from [b19ce3cb16] to [8e480899d7].

10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
10
11
12
13
14
15
16






















































17
18
19
20
21
22
23
24
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







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







 */

#import "config.h"

#import "OFNumber.h"
#import "OFExceptions.h"

#define RETURN_AS(t)							      \
	switch (type) {							      \
	case OF_NUMBER_CHAR:						      \
		return (t)value.char_;					      \
	case OF_NUMBER_SHORT:						      \
		return (t)value.short_;					      \
	case OF_NUMBER_INT:						      \
		return (t)value.int_;					      \
	case OF_NUMBER_LONG:						      \
		return (t)value.long_;					      \
	case OF_NUMBER_UCHAR:						      \
		return (t)value.uchar;					      \
	case OF_NUMBER_USHORT:						      \
		return (t)value.ushort;					      \
	case OF_NUMBER_UINT:						      \
		return (t)value.uint;					      \
	case OF_NUMBER_ULONG:						      \
		return (t)value.ulong;					      \
	case OF_NUMBER_INT8:						      \
		return (t)value.int8;					      \
	case OF_NUMBER_INT16:						      \
		return (t)value.int16;					      \
	case OF_NUMBER_INT32:						      \
		return (t)value.int32;					      \
	case OF_NUMBER_INT64:						      \
		return (t)value.int64;					      \
	case OF_NUMBER_UINT8:						      \
		return (t)value.uint8;					      \
	case OF_NUMBER_UINT16:						      \
		return (t)value.uint16;					      \
	case OF_NUMBER_UINT32:						      \
		return (t)value.uint32;					      \
	case OF_NUMBER_UINT64:						      \
		return (t)value.uint64;					      \
	case OF_NUMBER_SIZE:						      \
		return (t)value.size;					      \
	case OF_NUMBER_SSIZE:						      \
		return (t)value.ssize;					      \
	case OF_NUMBER_INTMAX:						      \
		return (t)value.intmax;					      \
	case OF_NUMBER_UINTMAX:						      \
		return (t)value.uintmax;				      \
	case OF_NUMBER_PTRDIFF:						      \
		return (t)value.ptrdiff;				      \
	case OF_NUMBER_INTPTR:						      \
		return (t)value.intptr;					      \
	case OF_NUMBER_FLOAT:						      \
		return (t)value.float_;					      \
	case OF_NUMBER_DOUBLE:						      \
		return (t)value.double_;				      \
	case OF_NUMBER_LONG_DOUBLE:					      \
		return (t)value.longdouble;				      \
	default:							      \
		@throw [OFInvalidFormatException newWithClass: [self class]]; \
#define RETURN_AS(t)							\
	switch (type) {							\
	case OF_NUMBER_CHAR:						\
		return (t)value.char_;					\
	case OF_NUMBER_SHORT:						\
		return (t)value.short_;					\
	case OF_NUMBER_INT:						\
		return (t)value.int_;					\
	case OF_NUMBER_LONG:						\
		return (t)value.long_;					\
	case OF_NUMBER_UCHAR:						\
		return (t)value.uchar;					\
	case OF_NUMBER_USHORT:						\
		return (t)value.ushort;					\
	case OF_NUMBER_UINT:						\
		return (t)value.uint;					\
	case OF_NUMBER_ULONG:						\
		return (t)value.ulong;					\
	case OF_NUMBER_INT8:						\
		return (t)value.int8;					\
	case OF_NUMBER_INT16:						\
		return (t)value.int16;					\
	case OF_NUMBER_INT32:						\
		return (t)value.int32;					\
	case OF_NUMBER_INT64:						\
		return (t)value.int64;					\
	case OF_NUMBER_UINT8:						\
		return (t)value.uint8;					\
	case OF_NUMBER_UINT16:						\
		return (t)value.uint16;					\
	case OF_NUMBER_UINT32:						\
		return (t)value.uint32;					\
	case OF_NUMBER_UINT64:						\
		return (t)value.uint64;					\
	case OF_NUMBER_SIZE:						\
		return (t)value.size;					\
	case OF_NUMBER_SSIZE:						\
		return (t)value.ssize;					\
	case OF_NUMBER_INTMAX:						\
		return (t)value.intmax;					\
	case OF_NUMBER_UINTMAX:						\
		return (t)value.uintmax;				\
	case OF_NUMBER_PTRDIFF:						\
		return (t)value.ptrdiff;				\
	case OF_NUMBER_INTPTR:						\
		return (t)value.intptr;					\
	case OF_NUMBER_FLOAT:						\
		return (t)value.float_;					\
	case OF_NUMBER_DOUBLE:						\
		return (t)value.double_;				\
	case OF_NUMBER_LONG_DOUBLE:					\
		return (t)value.longdouble;				\
	default:							\
		@throw [OFInvalidFormatException newWithClass: isa];	\
									      \
		/* Make gcc happy */					      \
		return 0;	/* Make gcc happy */			\
		return 0;						      \
	}

@implementation OFNumber
+ numberWithChar: (char)char_
{
	return [[[OFNumber alloc] initWithChar: char_] autorelease];
}
610
611
612
613
614
615
616
617

618
619
620
621
622
623
624
625
626
608
609
610
611
612
613
614

615
616
617
618
619
620
621
622
623
624







-
+









	case OF_NUMBER_INTPTR:
		return ([obj asUIntMax] == [self asUIntMax] ? YES : NO);
	case OF_NUMBER_FLOAT:
	case OF_NUMBER_DOUBLE:
	case OF_NUMBER_LONG_DOUBLE:
		return ([obj asLongDouble] == [self asLongDouble] ? YES : NO);
	default:
		@throw [OFInvalidArgumentException newWithClass: [self class]
		@throw [OFInvalidArgumentException newWithClass: isa
						    andSelector: _cmd];
	}
}

- (uint32_t)hash
{
	return [self asUInt32];
}
@end

Modified src/OFObject.m from [deb9ba26f1] to [850b7969ab].

198
199
200
201
202
203
204
205

206
207
208
209

210
211
212
213
214
215
216
198
199
200
201
202
203
204

205
206
207
208

209
210
211
212
213
214
215
216







-
+



-
+







	void **memchunks;
	size_t memchunks_size;

	memchunks_size = PRE_IVAR->memchunks_size + 1;

	if (SIZE_MAX - PRE_IVAR->memchunks_size < 1 ||
	    memchunks_size > SIZE_MAX / sizeof(void*))
		@throw [OFOutOfRangeException newWithClass: [self class]];
		@throw [OFOutOfRangeException newWithClass: isa];

	if ((memchunks = realloc(PRE_IVAR->memchunks,
	    memchunks_size * sizeof(void*))) == NULL)
		@throw [OFNoMemException newWithClass: [self class]
		@throw [OFNoMemException newWithClass: isa
					      andSize: memchunks_size];

	PRE_IVAR->memchunks = memchunks;
	PRE_IVAR->memchunks[PRE_IVAR->memchunks_size] = ptr;
	PRE_IVAR->memchunks_size = memchunks_size;

	return self;
224
225
226
227
228
229
230
231

232
233
234

235
236
237
238
239
240

241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258

259
260
261
262
263
264
265
224
225
226
227
228
229
230

231
232
233

234
235
236
237
238
239

240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257

258
259
260
261
262
263
264
265







-
+


-
+





-
+

















-
+







	if (size == 0)
		return NULL;

	memchunks_size = PRE_IVAR->memchunks_size + 1;

	if (SIZE_MAX - PRE_IVAR->memchunks_size == 0 ||
	    memchunks_size > SIZE_MAX / sizeof(void*))
		@throw [OFOutOfRangeException newWithClass: [self class]];
		@throw [OFOutOfRangeException newWithClass: isa];

	if ((ptr = malloc(size)) == NULL)
		@throw [OFNoMemException newWithClass: [self class]
		@throw [OFNoMemException newWithClass: isa
					      andSize: size];

	if ((memchunks = realloc(PRE_IVAR->memchunks,
	    memchunks_size * sizeof(void*))) == NULL) {
		free(ptr);
		@throw [OFNoMemException newWithClass: [self class]
		@throw [OFNoMemException newWithClass: isa
					      andSize: memchunks_size];
	}

	PRE_IVAR->memchunks = memchunks;
	PRE_IVAR->memchunks[PRE_IVAR->memchunks_size] = ptr;
	PRE_IVAR->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)
		@throw [OFOutOfRangeException newWithClass: [self class]];
		@throw [OFOutOfRangeException newWithClass: isa];

	return [self getMemWithSize: nitems * size];
}

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







-
+
-
-
+






-
+




















-
+







	}

	iter = PRE_IVAR->memchunks + PRE_IVAR->memchunks_size;

	while (iter-- > PRE_IVAR->memchunks) {
		if (OF_UNLIKELY(*iter == ptr)) {
			if (OF_UNLIKELY((ptr = realloc(ptr, size)) == NULL))
				@throw [OFNoMemException
				@throw [OFNoMemException newWithClass: isa
				    newWithClass: [self class]
					 andSize: size];
							      andSize: size];

			*iter = ptr;
			return ptr;
		}
	}

	@throw [OFMemNotPartOfObjException newWithClass: [self class]
	@throw [OFMemNotPartOfObjException newWithClass: isa
					     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)
		@throw [OFOutOfRangeException newWithClass: [self class]];
		@throw [OFOutOfRangeException newWithClass: isa];

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

- freeMem: (void*)ptr;
329
330
331
332
333
334
335
336

337
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
363
364

365
366
367
368
369
370
371
328
329
330
331
332
333
334

335
336
337
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

363
364
365
366
367
368
369
370







-
+















-
+











-
+







		if (OF_UNLIKELY(*iter == ptr)) {
			memchunks_size = PRE_IVAR->memchunks_size - 1;
			last = PRE_IVAR->memchunks[memchunks_size];

			if (OF_UNLIKELY(PRE_IVAR->memchunks_size == 0 ||
			    memchunks_size > SIZE_MAX / sizeof(void*)))
				@throw [OFOutOfRangeException
				    newWithClass: [self class]];
				    newWithClass: isa];

			if (OF_UNLIKELY(memchunks_size == 0)) {
				free(ptr);
				free(PRE_IVAR->memchunks);

				PRE_IVAR->memchunks = NULL;
				PRE_IVAR->memchunks_size = 0;

				return self;
			}

			if (OF_UNLIKELY((memchunks = realloc(
			    PRE_IVAR->memchunks, memchunks_size *
			    sizeof(void*))) == NULL))
				@throw [OFNoMemException
				    newWithClass: [self class]
				    newWithClass: isa
					 andSize: memchunks_size];

			free(ptr);
			PRE_IVAR->memchunks = memchunks;
			PRE_IVAR->memchunks[i] = last;
			PRE_IVAR->memchunks_size = memchunks_size;

			return self;
		}
	}

	@throw [OFMemNotPartOfObjException newWithClass: [self class]
	@throw [OFMemNotPartOfObjException newWithClass: isa
					     andPointer: ptr];
	return self;	/* never reached, but makes gcc happy */
}

- retain
{
	PRE_IVAR->retain_count++;

Modified src/OFString.m from [0784bdbb56] to [3c7d0ee24e].

154
155
156
157
158
159
160
161

162
163
164
165
166
167
168
154
155
156
157
158
159
160

161
162
163
164
165
166
167
168







-
+







			length = strlen(str);

			switch (check_utf8(str, length)) {
			case 1:
				is_utf8 = YES;
				break;
			case -1:
				c = [self class];
				c = isa;
				[super free];
				@throw [OFInvalidEncodingException
				    newWithClass: c];
			}

			@try {
				string = [self getMemWithSize: length + 1];
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
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







-
+





-
+












-
+







    andArguments: (va_list)args
{
	int t;
	Class c;

	if ((self = [super init])) {
		if (fmt == NULL) {
			c = [self class];
			c = isa;
			[super free];
			@throw [OFInvalidFormatException newWithClass: c];
		}

		if ((t = vasprintf(&string, fmt, args)) == -1) {
			c = [self class];
			c = isa;
			[super free];
			@throw [OFInitializationFailedException
			    newWithClass: c];
		}
		length = t;

		switch (check_utf8(string, length)) {
		case 1:
			is_utf8 = YES;
			break;
		case -1:
			free(string);
			c = [self class];
			c = isa;
			[super free];
			@throw [OFInvalidEncodingException newWithClass: c];
		}

		@try {
			[self addToMemoryPool: string];
		} @catch (OFException *e) {
262
263
264
265
266
267
268
269

270
271
272
273
274
275
276
262
263
264
265
266
267
268

269
270
271
272
273
274
275
276







-
+







		is_utf8 = YES;
		break;
	case -1:
		string = NULL;
		length = 0;
		is_utf8 = NO;

		@throw [OFInvalidEncodingException newWithClass: [self class]];
		@throw [OFInvalidEncodingException newWithClass: isa];
	}

	length = len;
	string = [self getMemWithSize: length + 1];
	memcpy(string, str, length + 1);

	return self;
287
288
289
290
291
292
293
294

295
296
297
298
299
300
301
287
288
289
290
291
292
293

294
295
296
297
298
299
300
301







-
+







	return YES;
}

- (int)compare: (id)obj
{
	if (![obj isKindOf: [OFString class]] &&
	    ![obj isKindOf: [OFConstString class]])
		@throw [OFInvalidArgumentException newWithClass: [self class]];
		@throw [OFInvalidArgumentException newWithClass: isa];

	return strcmp(string, [obj cString]);
}

- (uint32_t)hash
{
	uint32_t hash;
322
323
324
325
326
327
328
329

330
331
332
333
334
335
336
322
323
324
325
326
327
328

329
330
331
332
333
334
335
336







-
+







	strlength = strlen(str);

	switch (check_utf8(str, strlength)) {
	case 1:
		is_utf8 = YES;
		break;
	case -1:
		@throw [OFInvalidEncodingException newWithClass: [self class]];
		@throw [OFInvalidEncodingException newWithClass: isa];
	}

	newlen = length + strlength;
	newstr = [self resizeMem: string
			  toSize: newlen + 1];

	memcpy(newstr + length, str, strlength + 1);
356
357
358
359
360
361
362
363

364
365
366
367
368
369
370
371

372
373
374
375
376
377
378
356
357
358
359
360
361
362

363
364
365
366
367
368
369
370

371
372
373
374
375
376
377
378







-
+







-
+








- appendWithFormatCString: (const char*)fmt
	     andArguments: (va_list)args
{
	char *t;

	if (fmt == NULL)
		@throw [OFInvalidFormatException newWithClass: [self class]];
		@throw [OFInvalidFormatException newWithClass: isa];

	if ((vasprintf(&t, fmt, args)) == -1)
		/*
		 * This is only the most likely error to happen.
		 * Unfortunately, as errno isn't always thread-safe, there's
		 * no good way for us to find out what really happened.
		 */
		@throw [OFNoMemException newWithClass: [self class]];
		@throw [OFNoMemException newWithClass: isa];

	@try {
		[self appendCString: t];
	} @finally {
		free(t);
	}

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







-
+
-





-
+
-















-
+
-















-
+
-


















-
+












-
+












-
+







		/* ASCII */
		if (OF_LIKELY(!(string[i] & 0x80)))
			continue;

		/* A start byte can't happen first as we reversed everything */
		if (OF_UNLIKELY(string[i] & 0x40)) {
			madvise(string, len, MADV_NORMAL);
			@throw [OFInvalidEncodingException
			@throw [OFInvalidEncodingException newWithClass: isa];
			    newWithClass: [self class]];
		}

		/* Next byte must not be ASCII */
		if (OF_UNLIKELY(length < i + 1 || !(string[i + 1] & 0x80))) {
			madvise(string, len, MADV_NORMAL);
			@throw [OFInvalidEncodingException
			@throw [OFInvalidEncodingException newWithClass: isa];
			    newWithClass: [self class]];
		}

		/* Next byte is the start byte */
		if (OF_LIKELY(string[i + 1] & 0x40)) {
			string[i] ^= string[i + 1];
			string[i + 1] ^= string[i];
			string[i] ^= string[i + 1];

			i++;
			continue;
		}

		/* Second next byte must not be ASCII */
		if (OF_UNLIKELY(length < i + 2 || !(string[i + 2] & 0x80))) {
			madvise(string, len, MADV_NORMAL);
			@throw [OFInvalidEncodingException
			@throw [OFInvalidEncodingException newWithClass: isa];
			    newWithClass: [self class]];
		}

		/* Second next byte is the start byte */
		if (OF_LIKELY(string[i + 2] & 0x40)) {
			string[i] ^= string[i + 2];
			string[i + 2] ^= string[i];
			string[i] ^= string[i + 2];

			i += 2;
			continue;
		}

		/* Third next byte must not be ASCII */
		if (OF_UNLIKELY(length < i + 3 || !(string[i + 3] & 0x80))) {
			madvise(string, len, MADV_NORMAL);
			@throw [OFInvalidEncodingException
			@throw [OFInvalidEncodingException newWithClass: isa];
			    newWithClass: [self class]];
		}

		/* Third next byte is the start byte */
		if (OF_LIKELY(string[i + 3] & 0x40)) {
			string[i] ^= string[i + 3];
			string[i + 3] ^= string[i];
			string[i] ^= string[i + 3];

			string[i + 1] ^= string[i + 2];
			string[i + 2] ^= string[i + 1];
			string[i + 1] ^= string[i + 2];

			i += 3;
			continue;
		}

		/* UTF-8 does not allow more than 4 bytes per character */
		madvise(string, len, MADV_NORMAL);
		@throw [OFInvalidEncodingException newWithClass: [self class]];
		@throw [OFInvalidEncodingException newWithClass: isa];
	}

	madvise(string, len, MADV_NORMAL);

	return self;
}

- upper
{
	char *p = string + length;

	if (is_utf8)
		@throw [OFInvalidEncodingException newWithClass: [self class]];
		@throw [OFInvalidEncodingException newWithClass: isa];

	while (--p >= string)
		*p = toupper((int)*p);

	return self;
}

- lower
{
	char *p = string + length;

	if (is_utf8)
		@throw [OFInvalidEncodingException newWithClass: [self class]];
		@throw [OFInvalidEncodingException newWithClass: isa];

	while (--p >= string)
		*p = tolower((int)*p);

	return self;
}
@end

Modified src/OFTCPSocket.m from [b0a40b2d84] to [9dddc5248f].

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

179
180
181
182
183
184
185
186
187

188
189
190

191
192
193
194
195
196
197
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
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
177

178
179
180
181
182
183
184
185
186

187
188
189

190
191
192
193
194
195
196
197







-
+


-
+









-
+




















-
+














-
+


-
+


-
+












-
+





-
+













-
+


-
+








-
+


-
+







- connectTo: (const char*)host
     onPort: (uint16_t)port
{
	struct addrinfo hints, *res, *res0;
	char portstr[6];

	if (!port)
		@throw [OFInvalidPortException newWithClass: [self class]];
		@throw [OFInvalidPortException newWithClass: isa];

	if (sock != INVALID_SOCKET)
		@throw [OFAlreadyConnectedException newWithClass: [self class]];
		@throw [OFAlreadyConnectedException newWithClass: isa];

	memset(&hints, 0, sizeof(struct addrinfo));
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;

	snprintf(portstr, 6, "%d", port);

	if (getaddrinfo(host, portstr, &hints, &res0))
		@throw [OFAddressTranslationFailedException
		    newWithClass: [self class]
		    newWithClass: isa
			 andNode: host
		      andService: portstr];

	for (res = res0; res != NULL; res = res->ai_next) {
		if ((sock = socket(res->ai_family, res->ai_socktype,
		    res->ai_protocol)) == INVALID_SOCKET)
			continue;

		if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
			close(sock);
			sock = INVALID_SOCKET;
			continue;
		}

		break;
	}

	freeaddrinfo(res0);

	if (sock == INVALID_SOCKET)
		@throw [OFConnectionFailedException newWithClass: [self class]
		@throw [OFConnectionFailedException newWithClass: isa
							 andHost: host
							 andPort: port];

	return self;
}

-    bindOn: (const char*)host
   withPort: (uint16_t)port
  andFamily: (int)family
{
	struct addrinfo hints, *res;
	char portstr[6];

	if (!port)
		@throw [OFInvalidPortException newWithClass: [self class]];
		@throw [OFInvalidPortException newWithClass: isa];

	if (sock != INVALID_SOCKET)
		@throw [OFAlreadyConnectedException newWithClass: [self class]];
		@throw [OFAlreadyConnectedException newWithClass: isa];

	if ((sock = socket(family, SOCK_STREAM, 0)) == INVALID_SOCKET)
		@throw [OFBindFailedException newWithClass: [self class]
		@throw [OFBindFailedException newWithClass: isa
						   andHost: host
						   andPort: port
						 andFamily: family];

	memset(&hints, 0, sizeof(struct addrinfo));
	hints.ai_family = family;
	hints.ai_socktype = SOCK_STREAM;

	snprintf(portstr, 6, "%d", port);

	if (getaddrinfo(host, portstr, &hints, &res))
		@throw [OFAddressTranslationFailedException
		    newWithClass: [self class]
		    newWithClass: isa
			 andNode: host
		      andService: portstr];

	if (bind(sock, res->ai_addr, res->ai_addrlen) == -1) {
		freeaddrinfo(res);
		@throw [OFBindFailedException newWithClass: [self class]
		@throw [OFBindFailedException newWithClass: isa
						   andHost: host
						   andPort: port
						 andFamily: family];
	}

	freeaddrinfo(res);

	return self;
}

- listenWithBackLog: (int)backlog
{
	if (sock == INVALID_SOCKET)
		@throw [OFNotConnectedException newWithClass: [self class]];
		@throw [OFNotConnectedException newWithClass: isa];

	if (listen(sock, backlog) == -1)
		@throw [OFListenFailedException newWithClass: [self class]
		@throw [OFListenFailedException newWithClass: isa
						  andBackLog: backlog];

	return self;
}

- listen
{
	if (sock == INVALID_SOCKET)
		@throw [OFNotConnectedException newWithClass: [self class]];
		@throw [OFNotConnectedException newWithClass: isa];

	if (listen(sock, 5) == -1)
		@throw [OFListenFailedException newWithClass: [self class]
		@throw [OFListenFailedException newWithClass: isa
						  andBackLog: 5];

	return self;
}

- (OFTCPSocket*)accept
{
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
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
268
269
270
271
272
273
274
275
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
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
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
268
269
270
271
272
273
274
275

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







-
+















-
+



-
+

-
+













-
+


-
+









-
+











-
+







-
+




-
+










-
+







-
+










	} @catch(id e) {
		[newsock free];
		@throw e;
	}

	if ((s = accept(sock, addr, &addrlen)) == INVALID_SOCKET) {
		[newsock free];
		@throw [OFAcceptFailedException newWithClass: [self class]];
		@throw [OFAcceptFailedException newWithClass: isa];
	}

	[newsock setSocket: s];
	[newsock setSocketAddress: addr
		       withLength: addrlen];

	return newsock;
}

- (size_t)readNBytes: (size_t)size
	  intoBuffer: (char*)buf
{
	ssize_t ret;

	if (sock == INVALID_SOCKET)
		@throw [OFNotConnectedException newWithClass: [self class]];
		@throw [OFNotConnectedException newWithClass: isa];

	switch ((ret = recv(sock, buf, size, 0))) {
	case 0:
		@throw [OFNotConnectedException newWithClass: [self class]];
		@throw [OFNotConnectedException newWithClass: isa];
	case -1:
		@throw [OFReadFailedException newWithClass: [self class]
		@throw [OFReadFailedException newWithClass: isa
						   andSize: size];
	}

	/* This is safe, as we already checked < 1 */
	return ret;
}

- (size_t)writeNBytes: (size_t)size
	   fromBuffer: (const char*)buf
{
	ssize_t ret;

	if (sock == INVALID_SOCKET)
		@throw [OFNotConnectedException newWithClass: [self class]];
		@throw [OFNotConnectedException newWithClass: isa];

	if ((ret = send(sock, buf, size, 0)) == -1)
		@throw [OFWriteFailedException newWithClass: [self class]
		@throw [OFWriteFailedException newWithClass: isa
						    andSize: size];

	/* This is safe, as we already checked for -1 */
	return ret;
}

- (size_t)writeCString: (const char*)str
{
	if (sock == INVALID_SOCKET)
		@throw [OFNotConnectedException newWithClass: [self class]];
		@throw [OFNotConnectedException newWithClass: isa];

	return [self writeNBytes: strlen(str)
		      fromBuffer: str];
}

- setBlocking: (BOOL)enable
{
#ifndef _WIN32
	int flags;

	if ((flags = fcntl(sock, F_GETFL)) == -1)
		@throw [OFSetOptionFailedException newWithClass: [self class]];
		@throw [OFSetOptionFailedException newWithClass: isa];

	if (enable)
		flags &= ~O_NONBLOCK;
	else
		flags |= O_NONBLOCK;

	if (fcntl(sock, F_SETFL, flags) == -1)
		@throw [OFSetOptionFailedException newWithClass: [self class]];
		@throw [OFSetOptionFailedException newWithClass: isa];
#else
	u_long v = enable;

	if (ioctlsocket(sock, FIONBIO, &v) == SOCKET_ERROR)
		@throw [OFSetOptionFailedException newWithClass: [self class]];
		@throw [OFSetOptionFailedException newWithClass: isa];
#endif

	return self;
}

- enableKeepAlives: (BOOL)enable
{
	int v = enable;

	if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (char*)&v, sizeof(v)))
		@throw [OFSetOptionFailedException newWithClass: [self class]];
		@throw [OFSetOptionFailedException newWithClass: isa];

	return self;
}

- close
{
	if (sock == INVALID_SOCKET)
		@throw [OFNotConnectedException newWithClass: [self class]];
		@throw [OFNotConnectedException newWithClass: isa];

	sock = INVALID_SOCKET;

	if (saddr != NULL)
		[self freeMem: saddr];
	saddr_len = 0;

	return self;
}
@end