︙ | | |
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
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
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
|
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
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
|
-
-
+
+
-
+
-
+
-
+
-
+
-
-
-
+
-
+
-
+
-
-
+
+
-
+
-
-
+
+
-
-
-
-
-
-
+
-
-
+
+
-
+
-
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
+
+
+
-
+
-
-
-
+
+
+
-
-
-
-
-
-
-
|
#import "OFObject.h"
/**
* The OFException class is the base class for all exceptions in ObjFW.
*/
@interface OFException: OFObject
{
id object;
char *string;
Class class;
char *string;
}
/**
* Creates a new exception.
*
* \param obj The object which caused the exception
* \param class The class of the object which caused the exception
* \return A new exception
*/
+ newWithObject: (id)obj;
+ newWithClass: (Class)class;
/**
* Initializes an already allocated OFException.
*
* \param obj The object which caused the exception
* \param class The class of the object which caused the exception
* \return An initialized OFException
*/
- initWithObject: (id)obj;
- initWithClass: (Class)class;
- free;
/**
* \return The object that caused the exception
* \return The class of the object which caused the exception
*/
- (id)object;
- (Class)class;
/**
* \return An error message for the exception as a C string
*/
- (const char*)cString;
@end
/**
* An OFException indicating there is not enough memory available.
*/
@interface OFNoMemException: OFException
{
size_t req_size;
}
/**
* \param obj The object which caused the exception
* \param class The class of the object which caused the exception
* \param size The size of the memory that couldn't be allocated
* \return A new no memory exception
*/
+ newWithObject: (id)obj
andSize: (size_t)size;
+ newWithClass: (Class)class
andSize: (size_t)size;
/**
* Initializes an already allocated no memory exception.
*
* \param obj The object which caused the exception
* \param class The class of the object which caused the exception
* \param size The size of the memory that couldn't be allocated
* \return An initialized no memory exception
*/
- initWithObject: (id)obj
andSize: (size_t)size;
- initWithClass: (Class)class
andSize: (size_t)size;
/**
* \return An error message for the exception as a C string
*/
- (const char*)cString;
/**
* \return The size of the memoory that couldn't be allocated
*/
- (size_t)requestedSize;
@end
/**
* An OFException indicating the given memory is not part of the object.
*/
@interface OFMemNotPartOfObjException: OFException
{
void *pointer;
}
/**
* \param obj The object which caused the exception
* \param class The class of the object which caused the exception
* \param ptr A pointer to the memory that is not part of the object
* \return A new memory not part of object exception
*/
+ newWithObject: (id)obj
andPointer: (void*)ptr;
+ newWithClass: (Class)class
andPointer: (void*)ptr;
/**
* Initializes an already allocated memory not part of object exception.
*
* \param obj The object which caused the exception
* \param class The class of the object which caused the exception
* \param ptr A pointer to the memory that is not part of the object
* \return An initialized memory not part of object exception
*/
- initWithObject: (id)obj
andPointer: (void*)ptr;
- initWithClass: (Class)class
andPointer: (void*)ptr;
/**
* \return An error message for the exception as a C string
*/
- (const char*)cString;
/**
* \return A pointer to the memory which is not part of the object
*/
- (void*)pointer;
@end
/**
* An OFException indicating the given value is out of range.
*/
@interface OFOutOfRangeException: OFException {}
/**
* \return An error message for the exception as a C string
*/
- (const char*)cString;
@end
/**
* An OFException indicating that the encoding is invalid for this object.
*/
@interface OFInvalidEncodingException: OFException {}
/**
* \return An error message for the exception as a C string
*/
- (const char*)cString;
@end
/**
* An OFException indicating that the format is invalid.
*/
@interface OFInvalidFormatException: OFException {}
/**
* \return An error message for the exception as a C string
*/
- (const char*)cString;
@end
/**
* An OFException indicating that initializing something failed.
*/
@interface OFInitializationFailedException: OFException
{
Class class;
}
/**
* Creates a new exception.
*
* \param class The class which caused the exception
* \return A new exception
*/
+ newWithClass: (Class)class;
/**
* Initializes an already allocated OFException.
*
* \param obj The object which caused the exception
* \return An initialized OFException
*/
- initWithClass: (Class)class;
/**
* \return An error message for the exception as a C string
*/
- (const char*)cString;
/**
* \return The class which caused the exception
*/
- (Class)class;
@end
/**
* An OFException indicating the file couldn't be opened.
*/
@interface OFOpenFileFailedException: OFException
{
char *path;
char *mode;
int err;
}
/**
* \param obj The object which caused the exception
* \param class The class of the object which caused the exception
* \param path A C string of the path to the file tried to open
* \param mode A C string of the mode in which the file should have been opened
* \return A new open file failed exception
*/
+ newWithObject: (id)obj
andPath: (const char*)path
andMode: (const char*)mode;
+ newWithClass: (Class)class
andPath: (const char*)path
andMode: (const char*)mode;
/**
* Initializes an already allocated open file failed exception.
*
* \param obj The object which caused the exception
* \param class The class of the object which caused the exception
* \param path A C string of the path to the file which couldn't be opened
* \param mode A C string of the mode in which the file should have been opened
* \return An initialized open file failed exception
*/
- initWithObject: (id)obj
andPath: (const char*)path
andMode: (const char*)mode;
- initWithClass: (Class)class
andPath: (const char*)path
andMode: (const char*)mode;
- free;
/**
* \return An error message for the exception as a C string
*/
- (const char*)cString;
/**
* \return The errno from when the exception was created
*/
- (int)errNo;
/**
|
︙ | | |
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
|
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
|
-
+
-
-
-
+
+
+
-
+
-
-
+
+
-
+
-
-
-
+
+
+
-
+
-
-
+
+
|
size_t req_size;
size_t req_items;
BOOL has_items;
int err;
}
/**
* \param obj The object which caused the exception
* \param class The class of the object which caused the exception
* \param size The requested size of the data that couldn't be read / written
* \param nitems The requested number of items that couldn't be read / written
* \return A new open file failed exception
*/
+ newWithObject: (id)obj
andSize: (size_t)size
andNItems: (size_t)nitems;
+ newWithClass: (Class)class
andSize: (size_t)size
andNItems: (size_t)nitems;
/**
* \param obj The object which caused the exception
* \param class The class of the object which caused the exception
* \param size The requested size of the data that couldn't be read / written
* \return A new open file failed exception
*/
+ newWithObject: (id)obj
andSize: (size_t)size;
+ newWithClass: (Class)class
andSize: (size_t)size;
/**
* Initializes an already allocated read or write failed exception.
*
* \param obj The object which caused the exception
* \param class The class of the object which caused the exception
* \param size The requested size of the data that couldn't be read / written
* \param nitems The requested number of items that couldn't be read / written
* \return A new open file failed exception
*/
- initWithObject: (id)obj
andSize: (size_t)size
andNItems: (size_t)nitems;
- initWithClass: (Class)class
andSize: (size_t)size
andNItems: (size_t)nitems;
/**
* Initializes an already allocated read or write failed exception.
*
* \param obj The object which caused the exception
* \param class The class of the object which caused the exception
* \param size The requested size of the data that couldn't be read / written
* \return A new open file failed exception
*/
- initWithObject: (id)obj
andSize: (size_t)size;
- initWithClass: (Class)class
andSize: (size_t)size;
/**
* \return The errno from when the exception was created
*/
- (int)errNo;
/**
|
︙ | | |
315
316
317
318
319
320
321
322
323
324
325
326
327
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
371
372
373
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
|
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
+
+
+
-
+
-
-
-
+
+
+
-
-
-
-
-
-
-
|
- (BOOL)hasNItems;
@end
/**
* An OFException indicating a read to the file failed.
*/
@interface OFReadFailedException: OFReadOrWriteFailedException {}
/**
* \return An error message for the exception as a C string
*/
- (const char*)cString;
@end
/**
* An OFException indicating a write to the file failed.
*/
@interface OFWriteFailedException: OFReadOrWriteFailedException {}
/**
* \return An error message for the exception as a C string
*/
- (const char*)cString;
@end
/**
* An OFException indicating that setting an option failed.
*/
@interface OFSetOptionFailedException: OFException {}
/***
* \return An error message for the exception as a C string.
*/
- (const char*)cString;
@end
/**
* An OFException indicating a socket is not connected or bound.
*/
@interface OFNotConnectedException: OFException {}
/**
* \return An error message for the exception as a C string.
*/
- (const char*)cString;
@end
/**
* An OFException indicating an attempt to connect or bind an already connected
* or bound socket.
*/
@interface OFAlreadyConnectedException: OFException {}
/**
* \return An error message for the exception as a C string.
*/
- (const char*)cString;
@end
/**
* An OFException indicating that the specified port is invalid.
*/
@interface OFInvalidPortException: OFException {}
/**
* \return An error message for the exception as a C string.
*/
- (const char*)cString;
@end
/**
* An OFException indicating the translation of an address failed.
*/
@interface OFAddressTranslationFailedException: OFException
{
char *node;
char *service;
int err;
}
/**
* \param obj The object which caused the exception
* \param class The class of the object which caused the exception
* \param node The node for which translation was requested
* \param service The service of the node for which translation was requested
* \return A new address translation failed exception
*/
+ newWithObject: (id)obj
andNode: (const char*)node
andService: (const char*)service;
+ newWithClass: (Class)class
andNode: (const char*)node
andService: (const char*)service;
/**
* Initializes an already allocated address translation failed exception.
*
* \param obj The object which caused the exception
* \param class The class of the object which caused the exception
* \param node The node for which translation was requested
* \param service The service of the node for which translation was requested
* \return An initialized address translation failed exception
*/
- initWithObject: (id)obj
andNode: (const char*)node
andService: (const char*)service;
- initWithClass: (Class)class
andNode: (const char*)node
andService: (const char*)service;
- free;
/**
* \return An error message for the exception as a C string.
*/
- (const char*)cString;
/**
* \return The errno from when the exception was created
*/
- (int)errNo;
/**
|
︙ | | |
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
|
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
372
373
374
375
376
377
378
379
380
|
-
+
-
-
-
+
+
+
-
+
-
-
-
+
+
+
-
-
-
-
-
-
-
|
{
char *host;
uint16_t port;
int err;
}
/**
* \param obj The object which caused the exception
* \param class The class of the object which caused the exception
* \param host The host to which the connection failed
* \param port The port on the host to which the connection failed
* \return A new connection failed exception
*/
+ newWithObject: (id)obj
andHost: (const char*)host
andPort: (uint16_t)port;
+ newWithClass: (Class)class
andHost: (const char*)host
andPort: (uint16_t)port;
/**
* Initializes an already allocated connection failed exception.
*
* \param obj The object which caused the exception
* \param class The class of the object which caused the exception
* \param host The host to which the connection failed
* \param port The port on the host to which the connection failed
* \return An initialized connection failed exception
*/
- initWithObject: (id)obj
andHost: (const char*)host
andPort: (uint16_t)port;
- initWithClass: (Class)class
andHost: (const char*)host
andPort: (uint16_t)port;
- free;
/**
* \return An error message for the exception as a C string.
*/
- (const char*)cString;
/**
* \return The errno from when the exception was created
*/
- (int)errNo;
/**
|
︙ | | |
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
530
531
532
533
534
535
536
537
538
|
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
|
-
+
-
-
-
-
+
+
+
+
-
+
-
-
-
-
+
+
+
+
-
-
-
-
-
-
-
|
char *host;
uint16_t port;
int family;
int err;
}
/**
* \param obj The object which caused the exception
* \param class The class of the object which caused the exception
* \param host The host on which binding failed
* \param port The port on which binding failed
* \param family The family for which binnding failed
* \return A new bind failed exception
*/
+ newWithObject: (id)obj
andHost: (const char*)host
andPort: (uint16_t)port
andFamily: (int)family;
+ newWithClass: (Class)class
andHost: (const char*)host
andPort: (uint16_t)port
andFamily: (int)family;
/**
* Initializes an already allocated bind failed exception.
*
* \param obj The object which caused the exception
* \param class The class of the object which caused the exception
* \param host The host on which binding failed
* \param port The port on which binding failed
* \param family The family for which binnding failed
* \return An initialized bind failed exception
*/
- initWithObject: (id)obj
andHost: (const char*)host
andPort: (uint16_t)port
andFamily: (int)family;
- initWithClass: (Class)class
andHost: (const char*)host
andPort: (uint16_t)port
andFamily: (int)family;
- free;
/**
* \return An error message for the exception as a C string.
*/
- (const char*)cString;
/**
* \return The errno from when the exception was created
*/
- (int)errNo;
/**
|
︙ | | |
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
|
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
|
-
+
-
-
+
+
-
+
-
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|
@interface OFListenFailedException: OFException
{
int backlog;
int err;
}
/**
* \param obj The object which caused the exception
* \param class The class of the object which caused the exception
* \param backlog The requested size of the back log
* \return A new listen failed exception
*/
+ newWithObject: (id)obj
andBackLog: (int)backlog;
+ newWithClass: (Class)class
andBackLog: (int)backlog;
/**
* Initializes an already allocated listen failed exception
*
* \param obj The object which caused the exception
* \param class The class of the object which caused the exception
* \param backlog The requested size of the back log
* \return An initialized listen failed exception
*/
- initWithObject: (id)obj
andBackLog: (int)backlog;
- initWithClass: (Class)class
andBackLog: (int)backlog;
/**
* \return An error message for the exception as a C string.
*/
- (const char*)cString;
/**
* \return The errno from when the exception was created
*/
- (int)errNo;
/**
* \return The requested back log.
*/
- (int)backLog;
@end
/**
* An OFException indicating that accepting a connection failed.
*/
@interface OFAcceptFailedException: OFException
{
int err;
}
/**
* Initializes an already allocated accept failed exception.
*
* \param obj The object which caused the exception
* \return An initialized accept failed exception
*/
- initWithObject: (id)obj;
/**
* \return An error message for the exception as a C string.
*/
- (const char*)cString;
/**
* \return The errno from when the exception was created
*/
- (int)errNo;
@end
|
︙ | | |
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
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
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
|
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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
-
+
-
+
-
+
-
+
-
+
-
+
-
-
+
+
-
-
+
+
-
-
+
+
-
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
+
-
+
-
+
-
-
-
+
+
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
-
-
-
+
+
+
-
-
-
+
+
+
-
+
|
#import "OFExceptions.h"
#ifndef HAVE_ASPRINTF
#import "asprintf.h"
#endif
@implementation OFException
+ newWithObject: (id)obj
+ newWithClass: (Class)class_
{
return [[self alloc] initWithObject: obj];
return [[self alloc] initWithClass: class_];
}
- initWithObject: (id)obj
- initWithClass: (Class)class_
{
if ((self = [super init])) {
object = obj;
class = class_;
string = NULL;
}
return self;
}
- free
{
if (string != NULL)
free(string);
return [super free];
}
- (id)object
- (Class)class
{
return object;
return class;
}
- (const char*)cString
{
return string;
}
@end
@implementation OFNoMemException
+ newWithObject: (id)obj
andSize: (size_t)size
+ newWithClass: (Class)class_
andSize: (size_t)size
{
return [[self alloc] initWithObject: obj
andSize: size];
return [[self alloc] initWithClass: class_
andSize: size];
}
- initWithObject: (id)obj
andSize: (size_t)size
- initWithClass: (Class)class_
andSize: (size_t)size
{
if ((self = [super initWithObject: obj]))
if ((self = [super initWithClass: class_]))
req_size = size;
return self;
}
- (const char*)cString
{
if (string != NULL)
return string;
asprintf(&string, "Could not allocate %zu bytes for object of class "
"%s!", req_size, object != nil ? [object name] : "(null)");
asprintf(&string, "Could not allocate %zu bytes in class %s!",
req_size, [class name]);
return string;
}
- (size_t)requestedSize
{
return req_size;
}
@end
@implementation OFMemNotPartOfObjException
+ newWithObject: (id)obj
andPointer: (void*)ptr
+ newWithClass: (Class)class_
andPointer: (void*)ptr
{
return [[self alloc] initWithObject: obj
andPointer: ptr];
return [[self alloc] initWithClass: class_
andPointer: ptr];
}
- initWithObject: (id)obj
andPointer: (void*)ptr
- initWithClass: (Class)class_
andPointer: (void*)ptr
{
if ((self = [super initWithObject: obj]))
if ((self = [super initWithClass: class_]))
pointer = ptr;
return self;
}
- (const char*)cString
{
if (string != NULL)
return string;
asprintf(&string, "Memory at %p was not allocated as part of object "
"of class %s, thus the memory allocation was not changed! It is "
"also possible that there was an attempt to free the same memory "
"twice.", pointer, [object name]);
"twice.", pointer, [class name]);
return string;
}
- (void*)pointer
{
return pointer;
}
@end
@implementation OFOutOfRangeException
- (const char*)cString
{
if (string != NULL)
return string;
asprintf(&string, "Value out of range in object of class %s!",
asprintf(&string, "Value out of range in class %s!", [class name]);
object != nil ? [object name] : "(null)");
return string;
}
@end
@implementation OFInvalidEncodingException
- (const char*)cString
{
if (string != NULL)
return string;
asprintf(&string, "The encoding is invalid for object of classs %s!",
[object name]);
asprintf(&string, "The encoding is invalid for classs %s!",
[class name]);
return string;
}
@end
@implementation OFInvalidFormatException
- (const char*)cString
{
if (string != NULL)
return string;
asprintf(&string, "The format is invalid for object of classs %s!",
asprintf(&string, "The format is invalid for classs %s!", [class name]);
[object name]);
return string;
}
@end
@implementation OFInitializationFailedException
+ newWithClass: (Class)class_
{
return [[self alloc] initWithClass: class_];
}
- initWithClass: (Class)class_
{
if ((self = [super init])) {
object = nil;
string = NULL;
class = class_;
}
return self;
}
- (const char*)cString
{
if (string != NULL)
return string;
asprintf(&string, "Initialization failed for class %s!", [class name]);
return string;
}
- (Class)class
{
return class;
}
@end
@implementation OFOpenFileFailedException
+ newWithObject: (id)obj
andPath: (const char*)path_
andMode: (const char*)mode_
+ newWithClass: (Class)class_
andPath: (const char*)path_
andMode: (const char*)mode_
{
return [[self alloc] initWithObject: obj
andPath: path_
andMode: mode_];
return [[self alloc] initWithClass: class_
andPath: path_
andMode: mode_];
}
- initWithObject: (id)obj
andPath: (const char*)path_
andMode: (const char*)mode_
- initWithClass: (Class)class_
andPath: (const char*)path_
andMode: (const char*)mode_
{
if ((self = [super initWithObject: obj])) {
if ((self = [super initWithClass: class_])) {
path = (path_ != NULL ? strdup(path_) : NULL);
mode = (mode_ != NULL ? strdup(mode_) : NULL);
err = GET_ERR;
}
return self;
}
|
︙ | | |
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
-
-
+
+
|
}
- (const char*)cString
{
if (string != NULL)
return string;
asprintf(&string, "Failed to open file %s with mode %s in object of "
"class %s! " ERRFMT, path, mode, [self name], ERRPARAM);
asprintf(&string, "Failed to open file %s with mode %s in class %s! "
ERRFMT, path, mode, [self name], ERRPARAM);
return string;
}
- (int)errNo
{
return err;
|
︙ | | |
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
|
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
|
-
-
-
+
+
+
-
-
-
+
+
+
-
-
+
+
-
-
+
+
-
-
-
+
+
+
-
+
-
-
+
+
-
+
|
- (char*)mode
{
return mode;
}
@end
@implementation OFReadOrWriteFailedException
+ newWithObject: (id)obj
andSize: (size_t)size
andNItems: (size_t)nitems
+ newWithClass: (Class)class_
andSize: (size_t)size
andNItems: (size_t)nitems
{
return [[self alloc] initWithObject: obj
andSize: size
andNItems: nitems];
return [[self alloc] initWithClass: class_
andSize: size
andNItems: nitems];
}
+ newWithObject: (id)obj
andSize: (size_t)size
+ newWithClass: (Class)class_
andSize: (size_t)size
{
return [[self alloc] initWithObject: obj
andSize: size];
return [[self alloc] initWithClass: class_
andSize: size];
}
- initWithObject: (id)obj
andSize: (size_t)size
andNItems: (size_t)nitems
- initWithClass: (Class)class_
andSize: (size_t)size
andNItems: (size_t)nitems
{
if ((self = [super initWithObject: obj])) {
if ((self = [super initWithClass: class_])) {
req_size = size;
req_items = nitems;
has_items = YES;
err = GET_ERR;
}
return self;
}
- initWithObject: (id)obj
andSize: (size_t)size
- initWithClass: (Class)class_
andSize: (size_t)size
{
if ((self = [super initWithObject: obj])) {
if ((self = [super initWithClass: class_])) {
req_size = size;
req_items = 0;
has_items = NO;
err = GET_ERR;
}
return self;
|
︙ | | |
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
372
373
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
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
|
319
320
321
322
323
324
325
326
327
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
371
372
373
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
427
428
|
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
+
-
+
-
+
-
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
+
|
- (const char*)cString
{
if (string != NULL)
return string;;
if (has_items)
asprintf(&string, "Failed to read %zu items of size %zu in "
"object of class %s! " ERRFMT, req_items, req_size,
[object name], ERRPARAM);
"class %s! " ERRFMT, req_items, req_size, [class name],
ERRPARAM);
else
asprintf(&string, "Failed to read %zu bytes in object of class "
"%s! " ERRFMT, req_size, [object name], ERRPARAM);
asprintf(&string, "Failed to read %zu bytes in class %s! "
ERRFMT, req_size, [class name], ERRPARAM);
return string;
}
@end
@implementation OFWriteFailedException
- (const char*)cString
{
if (string != NULL)
return string;
if (has_items)
asprintf(&string, "Failed to write %zu items of size %zu in "
"object of class %s! " ERRFMT, req_items, req_size,
[object name], ERRPARAM);
"class %s! " ERRFMT, req_items, req_size, [class name],
ERRPARAM);
else
asprintf(&string, "Failed to write %zu bytes in object of "
"class %s! " ERRFMT, req_size, [object name], ERRFMT);
asprintf(&string, "Failed to write %zu bytes in class %s! "
ERRFMT, req_size, [class name], ERRFMT);
return string;
}
@end
@implementation OFSetOptionFailedException
- (const char*)cString
{
if (string != NULL)
return string;
asprintf(&string, "Setting an option for an object of type type %s "
"failed!", [object name]);
asprintf(&string, "Setting an option in class %s failed!",
[class name]);
return string;
}
@end
@implementation OFNotConnectedException
- (const char*)cString
{
if (string != NULL)
return string;
asprintf(&string, "The socket of type %s is not connected or bound!",
[object name]);
[class name]);
return string;
}
@end
@implementation OFAlreadyConnectedException
- (const char*)cString
{
if (string != NULL)
return string;
asprintf(&string, "The socket of type %s is already connected or bound "
"and thus can't be connected or bound again!", [object name]);
"and thus can't be connected or bound again!", [class name]);
return string;
}
@end
@implementation OFInvalidPortException
- (const char*)cString
{
if (string != NULL)
return string;
asprintf(&string, "The port specified is not valid for a socket of "
"type %s! This usually means you tried to use port 0, which is an "
"invalid port.", [object name]);
"invalid port.", [class name]);
return string;
}
@end
@implementation OFAddressTranslationFailedException
+ newWithObject: (id)obj
+ newWithClass: (Class)class_
andNode: (const char*)node_
andService: (const char*)service_
{
return [self newWithClass: class_
andNode: node_
andService: service_];
}
- initWithClass: (Class)class_
andNode: (const char*)node_
andService: (const char*)service_
{
return [self newWithObject: obj
andNode: node_
andService: service_];
}
- initWithObject: (id)obj
andNode: (const char*)node_
andService: (const char*)service_
{
if ((self = [super initWithObject: obj])) {
if ((self = [super initWithClass: class_])) {
node = (node_ != NULL ? strdup(node_) : NULL);
service = (service_ != NULL ? strdup(service_) : NULL);
err = GET_SOCK_ERR;
}
return self;
}
|
︙ | | |
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
|
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
|
-
-
-
-
-
+
+
+
+
+
|
- (const char*)cString
{
if (string != NULL)
return string;
asprintf(&string, "The service %s on %s could not be translated to an "
"address for an object of type %s. This means that either the node "
"was not found, there is no such service on the node, there was a "
"problem with the name server, there was a problem with your "
"network connection or you specified an invalid node or service. "
ERRFMT, service, node, [object name], ERRPARAM);
"address in class %s. This means that either the node was not "
"found, there is no such service on the node, there was a problem "
"with the name server, there was a problem with your network "
"connection or you specified an invalid node or service. " ERRFMT,
service, node, [class name], ERRPARAM);
return string;
}
- (int)errNo
{
return err;
|
︙ | | |
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
|
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
|
-
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
+
|
- (const char*)service
{
return service;
}
@end
@implementation OFConnectionFailedException
+ newWithObject: (id)obj
+ newWithClass: (Class)class_
andHost: (const char*)host_
andPort: (uint16_t)port_
{
return [self newWithClass: class_
andHost: host_
andPort: port_];
}
- initWithClass: (Class)class_
andHost: (const char*)host_
andPort: (uint16_t)port_
{
return [self newWithObject: obj
andHost: host_
andPort: port_];
}
- initWithObject: (id)obj
andHost: (const char*)host_
andPort: (uint16_t)port_
{
if ((self = [super initWithObject: obj])) {
if ((self = [super initWithClass: class_])) {
host = (host_ != NULL ? strdup(host_) : NULL);
port = port_;
err = GET_SOCK_ERR;
}
return self;
}
|
︙ | | |
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
|
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
|
-
+
|
- (const char*)cString
{
if (string != NULL)
return string;
asprintf(&string, "A connection to %s:%d could not be established in "
"object of type %s! " ERRFMT, host, port, [object name], ERRPARAM);
"class %s! " ERRFMT, host, port, [class name], ERRPARAM);
return string;
}
- (int)errNo
{
return err;
|
︙ | | |
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
|
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
|
-
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
+
|
- (uint16_t)port
{
return port;
}
@end
@implementation OFBindFailedException
+ newWithObject: (id)obj
+ newWithClass: (Class)class_
andHost: (const char*)host_
andPort: (uint16_t)port_
andFamily: (int)family_
{
return [self newWithClass: class_
andHost: host_
andPort: port_
andFamily: family_];
}
- initWithClass: (Class)class_
andHost: (const char*)host_
andPort: (uint16_t)port_
andFamily: (int)family_
{
return [self newWithObject: obj
andHost: host_
andPort: port_
andFamily: family_];
}
- initWithObject: (id)obj
andHost: (const char*)host_
andPort: (uint16_t)port_
andFamily: (int)family_
{
if ((self = [super initWithObject: obj])) {
if ((self = [super initWithClass: class_])) {
host = (host_ != NULL ? strdup(host_) : NULL);
port = port_;
family = family_;
err = GET_SOCK_ERR;
}
return self;
|
︙ | | |
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
|
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
|
-
+
-
|
- (const char*)cString
{
if (string != NULL)
return string;
asprintf(&string, "Binding to port %d on %s using family %d failed in "
"object of type %s! " ERRFMT, port, host, family, [object name],
"class %s! " ERRFMT, port, host, family, [class name], ERRPARAM);
ERRPARAM);
return string;
}
- (int)errNo
{
return err;
|
︙ | | |
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
|
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
|
-
-
+
+
-
-
+
+
-
-
+
+
-
+
-
+
-
+
-
+
-
+
|
- (int)family
{
return family;
}
@end
@implementation OFListenFailedException
+ newWithObject: (id)obj
andBackLog: (int)backlog_
+ newWithClass: (Class)class_
andBackLog: (int)backlog_
{
return [[self alloc] initWithObject: obj
andBackLog: backlog_];
return [[self alloc] initWithClass: class_
andBackLog: backlog_];
}
- initWithObject: (id)obj
andBackLog: (int)backlog_
- initWithClass: (Class)class_
andBackLog: (int)backlog_
{
if ((self = [super initWithObject: obj])) {
if ((self = [super initWithClass: class_])) {
backlog = backlog_;
err = GET_SOCK_ERR;
}
return self;
}
- (const char*)cString
{
if (string != NULL)
return string;
asprintf(&string, "Failed to listen in socket of type %s with a back "
"log of %d! "ERRFMT, [object name], backlog, ERRPARAM);
"log of %d! "ERRFMT, [class name], backlog, ERRPARAM);
return string;
}
- (int)errNo
{
return err;
}
- (int)backLog
{
return backlog;
}
@end
@implementation OFAcceptFailedException
- initWithObject: (id)obj
- initWithClass: (Class)class_
{
if ((self = [super initWithObject: obj]))
if ((self = [super initWithClass: class_]))
err = GET_SOCK_ERR;
return self;
}
- (const char*)cString
{
if (string != NULL)
return string;
asprintf(&string, "Failed to accept connection in socket of type %s! "
ERRFMT, [object name], ERRPARAM);
ERRFMT, [class name], ERRPARAM);
return string;
}
- (int)errNo
{
return err;
}
@end
|
︙ | | |
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
|
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
|
+
+
+
-
+
|
}
return self;
}
- initFromCString: (const char*)str
{
Class c;
if ((self = [super init])) {
if (str != NULL) {
length = strlen(str);
switch (check_utf8(str, length)) {
case 1:
is_utf8 = YES;
break;
case -1:
c = [self class];
[super free];
@throw [OFInvalidEncodingException
newWithObject: self];
newWithClass: c];
}
string = [self getMemWithSize: length + 1];
memcpy(string, str, length + 1);
}
}
|
︙ | | |
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
|
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
|
+
-
-
-
-
+
+
+
+
+
+
+
-
-
-
-
-
+
+
-
-
+
+
+
+
-
+
|
return ret;
}
- initFromFormatCString: (const char*)fmt
withArguments: (va_list)args
{
int t;
Class c;
if ((self = [super init])) {
if (fmt == NULL)
@throw [OFInvalidFormatException newWithObject: self];
if ((t = vasprintf(&string, fmt, args)) == -1)
if (fmt == NULL) {
c = [self class];
[super free];
@throw [OFInvalidFormatException newWithClass: c];
}
if ((t = vasprintf(&string, 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.
c = [self class];
[super free];
*/
@throw [OFNoMemException newWithObject: self];
@throw [OFInitializationFailedException
newWithClass: c];
}
length = t;
switch (check_utf8(string, length)) {
case 1:
is_utf8 = YES;
break;
case -1:
free(string);
c = [self class];
[super free];
@throw [OFInvalidEncodingException newWithObject: self];
@throw [OFInvalidEncodingException newWithClass: c];
}
@try {
[self addToMemoryPool: string];
} @catch (OFException *e) {
free(string);
@throw e;
|
︙ | | |
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
-
+
|
strlength = strlen(str);
switch (check_utf8(str, strlength)) {
case 1:
is_utf8 = YES;
break;
case -1:
@throw [OFInvalidEncodingException newWithObject: self];
@throw [OFInvalidEncodingException newWithClass: [self class]];
}
newlen = length + strlength;
newstr = [self resizeMem: string
toSize: newlen + 1];
memcpy(newstr + length, str, strlength + 1);
|
︙ | | |
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
|
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
|
-
+
-
+
+
-
-
+
+
+
+
|
- appendWithFormatCString: (const char*)fmt
andArguments: (va_list)args
{
char *t;
if (fmt == NULL)
@throw [OFInvalidFormatException newWithObject: self];
@throw [OFInvalidFormatException newWithClass: [self class]];
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 newWithObject: self];
@throw [OFNoMemException newWithClass: [self class]];
@try {
[self appendCString: t];
free(t);
[self appendCString: t];
} @finally {
free(t);
}
return self;
}
- reverse
{
size_t i, j, len = length / 2;
|
︙ | | |
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
372
373
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
427
428
429
430
431
|
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
372
373
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
|
-
+
+
-
+
+
-
+
+
-
+
+
-
+
-
+
-
+
|
/* 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 newWithObject: self];
@throw [OFInvalidEncodingException
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 newWithObject: self];
@throw [OFInvalidEncodingException
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 newWithObject: self];
@throw [OFInvalidEncodingException
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 newWithObject: self];
@throw [OFInvalidEncodingException
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 newWithObject: self];
@throw [OFInvalidEncodingException newWithClass: [self class]];
}
madvise(string, len, MADV_NORMAL);
return self;
}
- upper
{
size_t i = length;
if (is_utf8)
@throw [OFInvalidEncodingException newWithObject: self];
@throw [OFInvalidEncodingException newWithClass: [self class]];
while (i--)
string[i] = toupper(string[i]);
return self;
}
- lower
{
size_t i = length;
if (is_utf8)
@throw [OFInvalidEncodingException newWithObject: self];
@throw [OFInvalidEncodingException newWithClass: [self class]];
while (i--)
string[i] = tolower(string[i]);
return self;
}
@end
|
︙ | | |
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
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
|
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
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
|
-
+
-
+
-
-
-
+
+
+
-
-
-
+
+
+
-
+
-
+
-
-
-
-
+
+
+
+
-
-
-
+
+
+
-
-
-
-
+
+
+
+
-
+
-
-
+
+
-
+
-
-
+
+
|
- connectTo: (const char*)host
onPort: (uint16_t)port
{
struct addrinfo hints, *res, *res0;
char portstr[6];
if (!port)
@throw [OFInvalidPortException newWithObject: self];
@throw [OFInvalidPortException newWithClass: [self class]];
if (sock != INVALID_SOCKET)
@throw [OFAlreadyConnectedException newWithObject: self];
@throw [OFAlreadyConnectedException newWithClass: [self class]];
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
newWithObject: self
andNode: host
andService: portstr];
newWithClass: [self class]
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 newWithObject: self
andHost: host
andPort: port];
@throw [OFConnectionFailedException newWithClass: [self class]
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 newWithObject: self];
@throw [OFInvalidPortException newWithClass: [self class]];
if (sock != INVALID_SOCKET)
@throw [OFAlreadyConnectedException newWithObject: self];
@throw [OFAlreadyConnectedException newWithClass: [self class]];
if ((sock = socket(family, SOCK_STREAM, 0)) == INVALID_SOCKET)
@throw [OFBindFailedException newWithObject: self
andHost: host
andPort: port
andFamily: family];
@throw [OFBindFailedException newWithClass: [self class]
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
newWithObject: self
andNode: host
andService: portstr];
newWithClass: [self class]
andNode: host
andService: portstr];
if (bind(sock, res->ai_addr, res->ai_addrlen) == -1) {
freeaddrinfo(res);
@throw [OFBindFailedException newWithObject: self
andHost: host
andPort: port
andFamily: family];
@throw [OFBindFailedException newWithClass: [self class]
andHost: host
andPort: port
andFamily: family];
}
freeaddrinfo(res);
return self;
}
- listenWithBackLog: (int)backlog
{
if (sock == INVALID_SOCKET)
@throw [OFNotConnectedException newWithObject: self];
@throw [OFNotConnectedException newWithClass: [self class]];
if (listen(sock, backlog) == -1)
@throw [OFListenFailedException newWithObject: self
andBackLog: backlog];
@throw [OFListenFailedException newWithClass: [self class]
andBackLog: backlog];
return self;
}
- listen
{
if (sock == INVALID_SOCKET)
@throw [OFNotConnectedException newWithObject: self];
@throw [OFNotConnectedException newWithClass: [self class]];
if (listen(sock, 5) == -1)
@throw [OFListenFailedException newWithObject: self
andBackLog: 5];
@throw [OFListenFailedException newWithClass: [self class]
andBackLog: 5];
return self;
}
- (OFTCPSocket*)accept
{
OFTCPSocket *newsock;
|
︙ | | |
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
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
319
320
321
322
323
324
325
326
327
|
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
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
319
320
321
322
323
324
325
326
327
|
-
+
-
+
-
-
+
+
-
+
-
+
-
-
+
+
-
+
-
+
-
+
-
+
-
+
-
+
|
} @catch(id e) {
[newsock free];
@throw e;
}
if ((s = accept(sock, addr, &addrlen)) == INVALID_SOCKET) {
[newsock free];
@throw [OFAcceptFailedException newWithObject: self];
@throw [OFAcceptFailedException newWithClass: [self class]];
}
[newsock setSocket: s];
[newsock setSocketAddress: addr
withLength: addrlen];
return newsock;
}
- (size_t)readNBytes: (size_t)size
intoBuffer: (uint8_t*)buf
{
ssize_t ret;
if (sock == INVALID_SOCKET)
@throw [OFNotConnectedException newWithObject: self];
@throw [OFNotConnectedException newWithClass: [self class]];
if ((ret = recv(sock, (char*)buf, size, 0)) < 1)
@throw [OFReadFailedException newWithObject: self
andSize: size];
@throw [OFReadFailedException newWithClass: [self class]
andSize: size];
/* This is safe, as we already checked < 1 */
return ret;
}
- (uint8_t*)readNBytes: (size_t)size
{
uint8_t *ret;
if (sock == INVALID_SOCKET)
@throw [OFNotConnectedException newWithObject: self];
@throw [OFNotConnectedException newWithClass: [self class]];
ret = [self getMemWithSize: size];
@try {
[self readNBytes: size
intoBuffer: ret];
} @catch (id exception) {
[self freeMem: ret];
@throw exception;
}
return ret;
}
- (size_t)writeNBytes: (size_t)size
fromBuffer: (const uint8_t*)buf
{
ssize_t ret;
if (sock == INVALID_SOCKET)
@throw [OFNotConnectedException newWithObject: self];
@throw [OFNotConnectedException newWithClass: [self class]];
if ((ret = send(sock, (char*)buf, size, 0)) == -1)
@throw [OFWriteFailedException newWithObject: self
andSize: size];
@throw [OFWriteFailedException newWithClass: [self class]
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 newWithObject: self];
@throw [OFNotConnectedException newWithClass: [self class]];
return [self writeNBytes: strlen(str)
fromBuffer: (const uint8_t*)str];
}
- setBlocking: (BOOL)enable
{
#ifndef _WIN32
int flags;
if ((flags = fcntl(sock, F_GETFL)) == -1)
@throw [OFSetOptionFailedException newWithObject: self];
@throw [OFSetOptionFailedException newWithClass: [self class]];
if (enable)
flags &= ~O_NONBLOCK;
else
flags |= O_NONBLOCK;
if (fcntl(sock, F_SETFL, flags) == -1)
@throw [OFSetOptionFailedException newWithObject: self];
@throw [OFSetOptionFailedException newWithClass: [self class]];
#else
u_long v = enable;
if (ioctlsocket(sock, FIONBIO, &v) == SOCKET_ERROR)
@throw [OFSetOptionFailedException newWithObject: self];
@throw [OFSetOptionFailedException newWithClass: [self class]];
#endif
return self;
}
- enableKeepAlives: (BOOL)enable
{
int v = enable;
if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (char*)&v, sizeof(v)))
@throw [OFSetOptionFailedException newWithObject: self];
@throw [OFSetOptionFailedException newWithClass: [self class]];
return self;
}
- close
{
if (sock == INVALID_SOCKET)
@throw [OFNotConnectedException newWithObject: self];
@throw [OFNotConnectedException newWithClass: [self class]];
sock = INVALID_SOCKET;
if (saddr != NULL)
[self freeMem: saddr];
saddr_len = 0;
return self;
}
@end
|
︙ | | |
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
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
|
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
-
-
+
+
-
+
-
+
+
-
+
-
-
+
-
-
-
+
+
-
+
-
-
-
-
+
-
-
+
+
-
+
-
-
-
-
+
-
-
-
-
+
-
-
-
-
-
+
-
-
-
-
-
+
-
-
-
-
|
* 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.
*/
static inline BOOL
xf_resize_chars(char **str, size_t *len, size_t add)
static inline void
xf_resize_chars(char **str, size_t *len, size_t add, Class class)
{
char *str2;
size_t len2;
if (add > SIZE_MAX - *len)
@throw [OFOutOfRangeException newWithObject: nil];
@throw [OFOutOfRangeException newWithClass: class];
len2 = *len + add;
if ((str2 = realloc(*str, len2)) == NULL) {
if (*str)
free(*str);
*str = NULL;
return NO;
@throw [OFNoMemException newWithClass: class
andSize: len2];
}
*str = str2;
*len = len2;
}
return YES;
}
static inline BOOL
xf_add2chars(char **str, size_t *len, size_t *pos, const char *add)
static inline void
xf_add2chars(char **str, size_t *len, size_t *pos, const char *add, Class class)
{
size_t add_len;
add_len = strlen(add);
if (!xf_resize_chars(str, len, add_len))
xf_resize_chars(str, len, add_len, class);
return NO;
memcpy(*str + *pos, add, add_len);
*pos += add_len;
return YES;
}
@implementation OFXMLFactory
+ (char*)escapeCString: (const char*)s
{
char *ret;
size_t i, j, len, nlen;
len = nlen = strlen(s);
if (SIZE_MAX - len < 1)
@throw [OFOutOfRangeException newWithObject: nil];
@throw [OFOutOfRangeException newWithClass: self];
nlen++;
if ((ret = malloc(nlen)) == NULL)
@throw [OFNoMemException newWithObject: nil
andSize: nlen];
@throw [OFNoMemException newWithClass: self
andSize: nlen];
for (i = j = 0; i < len; i++) {
switch (s[i]) {
case '<':
if (OF_UNLIKELY(!xf_add2chars(&ret, &nlen, &j, "<")))
xf_add2chars(&ret, &nlen, &j, "<", self);
@throw [OFNoMemException
newWithObject: nil
andSize: nlen + 4];
break;
case '>':
if (OF_UNLIKELY(!xf_add2chars(&ret, &nlen, &j, ">")))
xf_add2chars(&ret, &nlen, &j, ">", self);
@throw [OFNoMemException
newWithObject: nil
andSize: nlen + 4];
break;
case '"':
if (OF_UNLIKELY(!xf_add2chars(&ret, &nlen, &j,
xf_add2chars(&ret, &nlen, &j, """, self);
""")))
@throw [OFNoMemException
newWithObject: nil
andSize: nlen + 6];
break;
case '\'':
if (OF_UNLIKELY(!xf_add2chars(&ret, &nlen, &j,
xf_add2chars(&ret, &nlen, &j, "'", self);
"'")))
@throw [OFNoMemException
newWithObject: nil
andSize: nlen + 6];
break;
case '&':
if (OF_UNLIKELY(!xf_add2chars(&ret, &nlen, &j,
xf_add2chars(&ret, &nlen, &j, "&", self);
"&")))
@throw [OFNoMemException
newWithObject: nil
andSize: nlen + 5];
break;
default:
ret[j++] = s[i];
break;
}
}
|
︙ | | |
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
|
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
|
-
+
-
-
+
+
-
+
-
-
+
+
-
-
+
-
-
-
+
+
-
+
-
-
-
-
-
-
-
+
+
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
-
+
-
-
-
-
+
-
-
-
-
-
+
|
char *arg, *val, *xml;
size_t i, len;
va_list args;
/* Start of tag */
len = strlen(name);
if (SIZE_MAX - len < 3)
@throw [OFOutOfRangeException newWithObject: nil];
@throw [OFOutOfRangeException newWithClass: self];
len += 3;
if ((xml = malloc(len)) == NULL)
@throw [OFNoMemException newWithObject: nil
andSize: len];
@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;
char *esc_val = NULL;
if (OF_UNLIKELY((esc_val =
[OFXMLFactory escapeCString: val]) == NULL)) {
@try {
esc_val = [OFXMLFactory escapeCString: val];
/*
* escapeCString already throws an exception,
} @catch (OFException *e) {
* no need to throw a second one here.
*/
free(xml);
return NULL;
@throw e;
}
@try {
if (OF_UNLIKELY(!xf_resize_chars(&xml, &len, 1 + strlen(arg) +
xf_resize_chars(&xml, &len, 1 + strlen(arg) + 2 +
2 + strlen(esc_val) + 1))) {
free(esc_val);
@throw [OFNoMemException
newWithObject: nil
andSize: len + 1 + strlen(arg) + 2 +
strlen(esc_val) + 1];
}
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);
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) {
if (!xf_resize_chars(&xml, &len, 2 - 1))
xf_resize_chars(&xml, &len, 2 - 1, self);
@throw [OFNoMemException
newWithObject: nil
andSize: len + 2 - 1];
xml[i++] = '/';
xml[i++] = '>';
} else {
if (!xf_resize_chars(&xml, &len, 1 + strlen(data) +
xf_resize_chars(&xml, &len, 1 + strlen(data) + 2 +
2 + strlen(name) + 1 - 1))
@throw [OFNoMemException
newWithObject: nil
andSize: len + 1 + strlen(data) + 2 +
strlen(name) + 1 - 1];
strlen(name) + 1 - 1, self);
xml[i++] = '>';
memcpy(xml + i, data, strlen(data));
i += strlen(data);
xml[i++] = '<';
xml[i++] = '/';
memcpy(xml + i, name, strlen(name));
|
︙ | | |
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
|
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
|
-
+
-
-
+
+
-
-
+
+
-
-
-
-
-
-
|
size_t i, len, pos;
if (strs[0] == NULL)
return NULL;
len = strlen(*strs);
if (SIZE_MAX - len < 1)
@throw [OFOutOfRangeException newWithObject: nil];
@throw [OFOutOfRangeException newWithClass: self];
len++;
if ((ret = malloc(len)) == NULL)
@throw [OFNoMemException newWithObject: nil
andSize: len];
@throw [OFNoMemException newWithClass: self
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]))) {
for (i = 1; strs[i] != NULL; i++)
xf_add2chars(&ret, &len, &pos, strs[i], self);
free(ret);
@throw [OFNoMemException
newWithObject: nil
andSize: len + strlen(strs[i])];
}
}
for (i = 0; strs[i] != NULL; i++)
free(strs[i]);
ret[pos] = 0;
return ret;
}
@end
|