Overview
Comment: | Clean up exceptions. |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
bb1fe8947807b2bec36fd4860520e636 |
User & Date: | js on 2009-01-04 01:40:17 |
Other Links: | manifest | tags |
Context
2009-01-04
| ||
02:46 | Work around a bug in gcc 4.0.1 (or is it Apple gcc only?). check-in: 95992fdc0e user: js tags: trunk | |
01:40 | Clean up exceptions. check-in: bb1fe89478 user: js tags: trunk | |
2009-01-03
| ||
22:57 | If we use -pthread(s) in CPPFLAGS, we need it in LIBS as well. check-in: 3d5b91a8c1 user: js tags: trunk | |
Changes
Modified src/OFArray.m from [c7e408a064] to [076c9e7d74].
︙ | ︙ | |||
54 55 56 57 58 59 60 | { return data; } - (void*)item: (size_t)item { if (item >= items) | | | | | | 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 | { return data; } - (void*)item: (size_t)item { if (item >= items) @throw [OFOutOfRangeException newWithClass: [self class]]; return data + item * itemsize; } - (void*)last { return data + (items - 1) * itemsize; } - add: (void*)item { if (SIZE_MAX - items < 1) @throw [OFOutOfRangeException newWithClass: [self class]]; 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) @throw [OFOutOfRangeException newWithClass: [self class]]; 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]]; data = [self resizeMem: data toNItems: items - nitems ofSize: itemsize]; items -= nitems; |
︙ | ︙ | |||
126 127 128 129 130 131 132 | } - add: (void*)item { size_t nsize; if (SIZE_MAX - items < 1 || items + 1 > SIZE_MAX / itemsize) | | | | | 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 | } - add: (void*)item { size_t nsize; if (SIZE_MAX - items < 1 || items + 1 > SIZE_MAX / itemsize) @throw [OFOutOfRangeException newWithClass: [self class]]; 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]]; 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]]; nsize = ((items - nitems) * itemsize + lastpagebyte) & ~lastpagebyte; if (size != nsize) data = [self resizeMem: data toSize: nsize]; items -= nitems; size = nsize; return self; } @end |
Modified src/OFExceptions.h from [46aed1bedf] to [cd6f8f6fc9].
︙ | ︙ | |||
12 13 14 15 16 17 18 | #import "OFObject.h" /** * The OFException class is the base class for all exceptions in ObjFW. */ @interface OFException: OFObject { | | | | | | < < | | | | | | < < < < < | | | | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | | | | < < < < < < < | 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 | #import "OFObject.h" /** * The OFException class is the base class for all exceptions in ObjFW. */ @interface OFException: OFObject { Class class; char *string; } /** * Creates a new exception. * * \param class The class of the object which caused the exception * \return A new exception */ + newWithClass: (Class)class; /** * Initializes an already allocated OFException. * * \param class The class of the object which caused the exception * \return An initialized OFException */ - initWithClass: (Class)class; /** * \return The class of the object which caused the exception */ - (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 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 */ + newWithClass: (Class)class andSize: (size_t)size; /** * Initializes an already allocated no memory 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 */ - initWithClass: (Class)class andSize: (size_t)size; /** * \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 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 */ + newWithClass: (Class)class andPointer: (void*)ptr; /** * Initializes an already allocated memory not part of object 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 */ - initWithClass: (Class)class andPointer: (void*)ptr; /** * \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 {} @end /** * An OFException indicating that the encoding is invalid for this object. */ @interface OFInvalidEncodingException: OFException {} @end /** * An OFException indicating that the format is invalid. */ @interface OFInvalidFormatException: OFException {} @end /** * An OFException indicating that initializing something failed. */ @interface OFInitializationFailedException: OFException @end /** * An OFException indicating the file couldn't be opened. */ @interface OFOpenFileFailedException: OFException { char *path; char *mode; int err; } /** * \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 */ + newWithClass: (Class)class andPath: (const char*)path andMode: (const char*)mode; /** * Initializes an already allocated open file failed 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 */ - initWithClass: (Class)class andPath: (const char*)path andMode: (const char*)mode; /** * \return The errno from when the exception was created */ - (int)errNo; /** * \return A C string of the path to the file which couldn't be opened |
︙ | ︙ | |||
251 252 253 254 255 256 257 | size_t req_size; size_t req_items; BOOL has_items; int err; } /** | | | | | | | | | | 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 | size_t req_size; size_t req_items; BOOL has_items; int err; } /** * \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 */ + newWithClass: (Class)class andSize: (size_t)size andNItems: (size_t)nitems; /** * \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 */ + newWithClass: (Class)class andSize: (size_t)size; /** * Initializes an already allocated read or write failed 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 */ - initWithClass: (Class)class andSize: (size_t)size andNItems: (size_t)nitems; /** * Initializes an already allocated read or write failed 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 */ - 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 | - (BOOL)hasNItems; @end /** * An OFException indicating a read to the file failed. */ @interface OFReadFailedException: OFReadOrWriteFailedException {} | < < < < < < < < < < < < < < < < < < < < < < < < | | | | < < < < < < < | 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 333 | - (BOOL)hasNItems; @end /** * An OFException indicating a read to the file failed. */ @interface OFReadFailedException: OFReadOrWriteFailedException {} @end /** * An OFException indicating a write to the file failed. */ @interface OFWriteFailedException: OFReadOrWriteFailedException {} @end /** * An OFException indicating that setting an option failed. */ @interface OFSetOptionFailedException: OFException {} @end /** * An OFException indicating a socket is not connected or bound. */ @interface OFNotConnectedException: OFException {} @end /** * An OFException indicating an attempt to connect or bind an already connected * or bound socket. */ @interface OFAlreadyConnectedException: OFException {} @end /** * An OFException indicating that the specified port is invalid. */ @interface OFInvalidPortException: OFException {} @end /** * An OFException indicating the translation of an address failed. */ @interface OFAddressTranslationFailedException: OFException { char *node; char *service; int err; } /** * \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 */ + newWithClass: (Class)class andNode: (const char*)node andService: (const char*)service; /** * Initializes an already allocated address translation failed 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 */ - initWithClass: (Class)class andNode: (const char*)node andService: (const char*)service; /** * \return The errno from when the exception was created */ - (int)errNo; /** * /return The node for which translation was requested |
︙ | ︙ | |||
438 439 440 441 442 443 444 | { char *host; uint16_t port; int err; } /** | | | | | < < < < < < < | 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 | { char *host; uint16_t port; int err; } /** * \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 */ + newWithClass: (Class)class andHost: (const char*)host andPort: (uint16_t)port; /** * Initializes an already allocated connection failed 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 */ - initWithClass: (Class)class andHost: (const char*)host andPort: (uint16_t)port; /** * \return The errno from when the exception was created */ - (int)errNo; /** * \return The host to which the connection failed |
︙ | ︙ | |||
494 495 496 497 498 499 500 | char *host; uint16_t port; int family; int err; } /** | | | | | < < < < < < < | 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 | char *host; uint16_t port; int family; int err; } /** * \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 */ + newWithClass: (Class)class andHost: (const char*)host andPort: (uint16_t)port andFamily: (int)family; /** * Initializes an already allocated bind failed 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 */ - initWithClass: (Class)class andHost: (const char*)host andPort: (uint16_t)port andFamily: (int)family; /** * \return The errno from when the exception was created */ - (int)errNo; /** * \return The host on which binding failed |
︙ | ︙ | |||
557 558 559 560 561 562 563 | @interface OFListenFailedException: OFException { int backlog; int err; } /** | | | | | < < < < < < < < < < < < < < < < < < | 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 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 */ + newWithClass: (Class)class andBackLog: (int)backlog; /** * Initializes an already allocated listen failed 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 */ - initWithClass: (Class)class andBackLog: (int)backlog; /** * \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; } /** * \return The errno from when the exception was created */ - (int)errNo; @end |
Modified src/OFExceptions.m from [2196c79e5f] to [fbe82e3f08].
︙ | ︙ | |||
33 34 35 36 37 38 39 | #import "OFExceptions.h" #ifndef HAVE_ASPRINTF #import "asprintf.h" #endif @implementation OFException | | | | | | | | | | | | | | | | | | | < | | | < < < < < < < < < < < < < < < < < < < < < < | | | | | 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 + newWithClass: (Class)class_ { return [[self alloc] initWithClass: class_]; } - initWithClass: (Class)class_ { if ((self = [super init])) { class = class_; string = NULL; } return self; } - free { if (string != NULL) free(string); return [super free]; } - (Class)class { return class; } - (const char*)cString { return string; } @end @implementation OFNoMemException + newWithClass: (Class)class_ andSize: (size_t)size { return [[self alloc] initWithClass: class_ andSize: size]; } - initWithClass: (Class)class_ andSize: (size_t)size { 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 in class %s!", req_size, [class name]); return string; } - (size_t)requestedSize { return req_size; } @end @implementation OFMemNotPartOfObjException + newWithClass: (Class)class_ andPointer: (void*)ptr { return [[self alloc] initWithClass: class_ andPointer: ptr]; } - initWithClass: (Class)class_ andPointer: (void*)ptr { 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, [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 class %s!", [class name]); return string; } @end @implementation OFInvalidEncodingException - (const char*)cString { if (string != NULL) return string; 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 classs %s!", [class name]); return string; } @end @implementation OFInitializationFailedException - (const char*)cString { if (string != NULL) return string; asprintf(&string, "Initialization failed for class %s!", [class name]); return string; } @end @implementation OFOpenFileFailedException + newWithClass: (Class)class_ andPath: (const char*)path_ andMode: (const char*)mode_ { return [[self alloc] initWithClass: class_ andPath: path_ andMode: mode_]; } - initWithClass: (Class)class_ andPath: (const char*)path_ andMode: (const char*)mode_ { 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 | } - (const char*)cString { if (string != NULL) return string; | | | | 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 class %s! " ERRFMT, path, mode, [self name], ERRPARAM); return string; } - (int)errNo { return err; |
︙ | ︙ | |||
270 271 272 273 274 275 276 | - (char*)mode { return mode; } @end @implementation OFReadOrWriteFailedException | | | | | | | | | | 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 + newWithClass: (Class)class_ andSize: (size_t)size andNItems: (size_t)nitems { return [[self alloc] initWithClass: class_ andSize: size andNItems: nitems]; } + newWithClass: (Class)class_ andSize: (size_t)size { return [[self alloc] initWithClass: class_ andSize: size]; } - initWithClass: (Class)class_ andSize: (size_t)size andNItems: (size_t)nitems { if ((self = [super initWithClass: class_])) { req_size = size; req_items = nitems; has_items = YES; err = GET_ERR; } return self; } - initWithClass: (Class)class_ andSize: (size_t)size { 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 | - (const char*)cString { if (string != NULL) return string;; if (has_items) asprintf(&string, "Failed to read %zu items of size %zu in " | | | | | | | | | | | | | | | | | | | 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 " "class %s! " ERRFMT, req_items, req_size, [class name], ERRPARAM); else 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 " "class %s! " ERRFMT, req_items, req_size, [class name], ERRPARAM); else 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 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!", [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!", [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.", [class name]); return string; } @end @implementation OFAddressTranslationFailedException + 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_ { 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 | - (const char*)cString { if (string != NULL) return string; asprintf(&string, "The service %s on %s could not be translated to an " | | | | | | | 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 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 | - (const char*)service { return service; } @end @implementation OFConnectionFailedException | | | | | | 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 + 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_ { 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 | - (const char*)cString { if (string != NULL) return string; asprintf(&string, "A connection to %s:%d could not be established in " | | | 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 " "class %s! " ERRFMT, host, port, [class name], ERRPARAM); return string; } - (int)errNo { return err; |
︙ | ︙ | |||
546 547 548 549 550 551 552 | - (uint16_t)port { return port; } @end @implementation OFBindFailedException | | | | | | 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 + 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_ { 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 | - (const char*)cString { if (string != NULL) return string; asprintf(&string, "Binding to port %d on %s using family %d failed in " | | < | 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 " "class %s! " ERRFMT, port, host, family, [class name], ERRPARAM); return string; } - (int)errNo { return err; |
︙ | ︙ | |||
614 615 616 617 618 619 620 | - (int)family { return family; } @end @implementation OFListenFailedException | | | | | | | | | | 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 + newWithClass: (Class)class_ andBackLog: (int)backlog_ { return [[self alloc] initWithClass: class_ andBackLog: backlog_]; } - initWithClass: (Class)class_ andBackLog: (int)backlog_ { 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, [class name], backlog, ERRPARAM); return string; } - (int)errNo { return err; } - (int)backLog { return backlog; } @end @implementation OFAcceptFailedException - initWithClass: (Class)class_ { 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, [class name], ERRPARAM); return string; } - (int)errNo { return err; } @end |
Modified src/OFFile.m from [d2aa51b6f7] to [1e28d0f42f].
︙ | ︙ | |||
76 77 78 79 80 81 82 83 | symlink(src, dest); #endif } - initWithPath: (const char*)path andMode: (const char*)mode { if ((self = [super init])) { | > > | > > | > | 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 | symlink(src, dest); #endif } - initWithPath: (const char*)path andMode: (const char*)mode { Class c; if ((self = [super init])) { if ((fp = fopen(path, mode)) == NULL) { c = [self class]; [super free]; @throw [OFOpenFileFailedException newWithClass: c andPath: path andMode: mode]; } } return self; } - free { fclose(fp); |
︙ | ︙ | |||
104 105 106 107 108 109 110 | - (size_t)readNItems: (size_t)nitems ofSize: (size_t)size intoBuffer: (uint8_t*)buf { size_t ret; if ((ret = fread(buf, size, nitems, fp)) == 0 && !feof(fp)) | | | 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: (uint8_t*)buf { size_t ret; if ((ret = fread(buf, size, nitems, fp)) == 0 && !feof(fp)) @throw [OFReadFailedException newWithClass: [self class] andSize: size andNItems: nitems]; return ret; } - (size_t)readNBytes: (size_t)size |
︙ | ︙ | |||
153 154 155 156 157 158 159 | ofSize: (size_t)size fromBuffer: (const uint8_t*)buf { size_t ret; if ((ret = fwrite(buf, size, nitems, fp)) == 0 && size != 0 && nitems != 0) | | | 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | ofSize: (size_t)size fromBuffer: (const uint8_t*)buf { size_t ret; if ((ret = fwrite(buf, size, nitems, fp)) == 0 && size != 0 && nitems != 0) @throw [OFWriteFailedException newWithClass: [self class] andSize: size andNItems: nitems]; return ret; } - (size_t)writeNBytes: (size_t)size |
︙ | ︙ |
Modified src/OFNumber.m from [f0e9fb484c] to [03cd55c54a].
︙ | ︙ | |||
57 58 59 60 61 62 63 | 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: \ | | | 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | 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]]; \ \ /* Make gcc happy */ \ return 0; \ } @implementation OFNumber + newWithChar: (char)char_ |
︙ | ︙ |
Modified src/OFObject.m from [e51bd21762] to [027dbab7f0].
︙ | ︙ | |||
47 48 49 50 51 52 53 | void **memchunks; size_t memchunks_size; memchunks_size = __memchunks_size + 1; if (SIZE_MAX - __memchunks_size == 0 || memchunks_size > SIZE_MAX / sizeof(void*)) | | | | 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | void **memchunks; size_t memchunks_size; memchunks_size = __memchunks_size + 1; if (SIZE_MAX - __memchunks_size == 0 || memchunks_size > SIZE_MAX / sizeof(void*)) @throw [OFOutOfRangeException newWithClass: [self class]]; if ((memchunks = realloc(__memchunks, memchunks_size * sizeof(void*))) == NULL) @throw [OFNoMemException newWithClass: [self class] andSize: memchunks_size]; __memchunks = memchunks; __memchunks[__memchunks_size] = ptr; __memchunks_size = memchunks_size; return ptr; |
︙ | ︙ | |||
73 74 75 76 77 78 79 | if (size == 0) return NULL; memchunks_size = __memchunks_size + 1; if (SIZE_MAX - __memchunks_size == 0 || memchunks_size > SIZE_MAX / sizeof(void*)) | | < | | | > | | | | | | 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 | if (size == 0) return NULL; memchunks_size = __memchunks_size + 1; if (SIZE_MAX - __memchunks_size == 0 || memchunks_size > SIZE_MAX / sizeof(void*)) @throw [OFOutOfRangeException newWithClass: [self class]]; if ((ptr = malloc(size)) == NULL) @throw [OFNoMemException newWithClass: [self class] andSize: size]; if ((memchunks = realloc(__memchunks, memchunks_size * sizeof(void*))) == NULL) { free(ptr); @throw [OFNoMemException newWithClass: [self class] andSize: memchunks_size]; } __memchunks = memchunks; __memchunks[__memchunks_size] = ptr; __memchunks_size = memchunks_size; return ptr; } - (void*)getMemForNItems: (size_t)nitems ofSize: (size_t)size { if (nitems == 0 || size == 0) return NULL; if (nitems > SIZE_MAX / size) @throw [OFOutOfRangeException newWithClass: [self class]]; return [self getMemWithSize: nitems * size]; } - (void*)resizeMem: (void*)ptr toSize: (size_t)size { |
︙ | ︙ | |||
123 124 125 126 127 128 129 | } iter = __memchunks + __memchunks_size; while (iter-- > __memchunks) { if (OF_UNLIKELY(*iter == ptr)) { if (OF_UNLIKELY((ptr = realloc(ptr, size)) == NULL)) | | > | | | 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 | } iter = __memchunks + __memchunks_size; while (iter-- > __memchunks) { if (OF_UNLIKELY(*iter == ptr)) { if (OF_UNLIKELY((ptr = realloc(ptr, size)) == NULL)) @throw [OFNoMemException newWithClass: [self class] andSize: size]; *iter = ptr; return ptr; } } @throw [OFMemNotPartOfObjException newWithClass: [self class] 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]]; memsize = nitems * size; return [self resizeMem: ptr toSize: memsize]; } - freeMem: (void*)ptr; |
︙ | ︙ | |||
177 178 179 180 181 182 183 | if (OF_UNLIKELY(*iter == ptr)) { memchunks_size = __memchunks_size - 1; last = __memchunks[memchunks_size]; if (OF_UNLIKELY(__memchunks_size == 0 || memchunks_size > SIZE_MAX / sizeof(void*))) @throw [OFOutOfRangeException | | | | | 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 | if (OF_UNLIKELY(*iter == ptr)) { memchunks_size = __memchunks_size - 1; last = __memchunks[memchunks_size]; if (OF_UNLIKELY(__memchunks_size == 0 || memchunks_size > SIZE_MAX / sizeof(void*))) @throw [OFOutOfRangeException newWithClass: [self class]]; if (OF_UNLIKELY(memchunks_size == 0)) { free(ptr); free(__memchunks); __memchunks = NULL; __memchunks_size = 0; return self; } if (OF_UNLIKELY((memchunks = realloc(__memchunks, memchunks_size * sizeof(void*))) == NULL)) @throw [OFNoMemException newWithClass: [self class] andSize: memchunks_size]; free(ptr); __memchunks = memchunks; __memchunks[i] = last; __memchunks_size = memchunks_size; return self; } } @throw [OFMemNotPartOfObjException newWithClass: [self class] andPointer: ptr]; return self; /* never reached, but makes gcc happy */ } @end |
Modified src/OFString.m from [781415eae9] to [0b49a22c9f].
︙ | ︙ | |||
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | } return self; } - initFromCString: (const char*)str { if ((self = [super init])) { if (str != NULL) { length = strlen(str); switch (check_utf8(str, length)) { case 1: is_utf8 = YES; break; case -1: [super free]; @throw [OFInvalidEncodingException | > > > | | 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 newWithClass: c]; } string = [self getMemWithSize: length + 1]; memcpy(string, str, length + 1); } } |
︙ | ︙ | |||
176 177 178 179 180 181 182 183 184 | return ret; } - initFromFormatCString: (const char*)fmt withArguments: (va_list)args { int t; if ((self = [super init])) { | > | > > | | > | < < < < > | < | > > > | | 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) { c = [self class]; [super free]; @throw [OFInvalidFormatException newWithClass: c]; } if ((t = vasprintf(&string, fmt, args)) == -1) { c = [self class]; [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]; [super free]; @throw [OFInvalidEncodingException newWithClass: c]; } @try { [self addToMemoryPool: string]; } @catch (OFException *e) { free(string); @throw e; |
︙ | ︙ | |||
258 259 260 261 262 263 264 | strlength = strlen(str); switch (check_utf8(str, strlength)) { case 1: is_utf8 = YES; break; case -1: | | | 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 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 | - appendWithFormatCString: (const char*)fmt andArguments: (va_list)args { char *t; if (fmt == NULL) | | | > > > | 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 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 newWithClass: [self class]]; @try { [self appendCString: t]; } @finally { free(t); } return self; } - reverse { size_t i, j, len = length / 2; |
︙ | ︙ | |||
334 335 336 337 338 339 340 | /* 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); | | > | > | > | > | | | | 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 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 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 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 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]]; } madvise(string, len, MADV_NORMAL); return self; } - upper { size_t i = length; if (is_utf8) @throw [OFInvalidEncodingException newWithClass: [self class]]; while (i--) string[i] = toupper(string[i]); return self; } - lower { size_t i = length; if (is_utf8) @throw [OFInvalidEncodingException newWithClass: [self class]]; while (i--) string[i] = tolower(string[i]); return self; } @end |
Modified src/OFTCPSocket.m from [180713756a] to [f0f26c68bf].
︙ | ︙ | |||
73 74 75 76 77 78 79 | - connectTo: (const char*)host onPort: (uint16_t)port { struct addrinfo hints, *res, *res0; char portstr[6]; if (!port) | | | | | | | | | | | | | | | 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 | - connectTo: (const char*)host onPort: (uint16_t)port { struct addrinfo hints, *res, *res0; char portstr[6]; if (!port) @throw [OFInvalidPortException newWithClass: [self class]]; if (sock != INVALID_SOCKET) @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 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 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 newWithClass: [self class]]; if (sock != INVALID_SOCKET) @throw [OFAlreadyConnectedException newWithClass: [self class]]; if ((sock = socket(family, SOCK_STREAM, 0)) == INVALID_SOCKET) @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 newWithClass: [self class] andNode: host andService: portstr]; if (bind(sock, res->ai_addr, res->ai_addrlen) == -1) { freeaddrinfo(res); @throw [OFBindFailedException newWithClass: [self class] andHost: host andPort: port andFamily: family]; } freeaddrinfo(res); return self; } - listenWithBackLog: (int)backlog { if (sock == INVALID_SOCKET) @throw [OFNotConnectedException newWithClass: [self class]]; if (listen(sock, backlog) == -1) @throw [OFListenFailedException newWithClass: [self class] andBackLog: backlog]; return self; } - listen { if (sock == INVALID_SOCKET) @throw [OFNotConnectedException newWithClass: [self class]]; if (listen(sock, 5) == -1) @throw [OFListenFailedException newWithClass: [self class] andBackLog: 5]; return self; } - (OFTCPSocket*)accept { |
︙ | ︙ | |||
201 202 203 204 205 206 207 | } @catch(id e) { [newsock free]; @throw e; } if ((s = accept(sock, addr, &addrlen)) == INVALID_SOCKET) { [newsock free]; | | | | | | | | | | | | | | 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 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 newWithClass: [self class]]; if ((ret = recv(sock, (char*)buf, size, 0)) < 1) @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 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 newWithClass: [self class]]; if ((ret = send(sock, (char*)buf, size, 0)) == -1) @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 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 newWithClass: [self class]]; if (enable) flags &= ~O_NONBLOCK; else flags |= O_NONBLOCK; if (fcntl(sock, F_SETFL, flags) == -1) @throw [OFSetOptionFailedException newWithClass: [self class]]; #else u_long v = enable; if (ioctlsocket(sock, FIONBIO, &v) == SOCKET_ERROR) @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 newWithClass: [self class]]; return self; } - close { if (sock == INVALID_SOCKET) @throw [OFNotConnectedException newWithClass: [self class]]; sock = INVALID_SOCKET; if (saddr != NULL) [self freeMem: saddr]; saddr_len = 0; return self; } @end |
Modified src/OFXMLFactory.m from [95cdbcf61d] to [0c4e28e6ff].
︙ | ︙ | |||
24 25 26 27 28 29 30 | * 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. */ | | | | | > | < | < | | | < < < | | | < < < | < < < | < < < < | < < < < | < < < < | 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 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 newWithClass: class]; len2 = *len + add; if ((str2 = realloc(*str, len2)) == NULL) { if (*str) free(*str); *str = NULL; @throw [OFNoMemException newWithClass: class andSize: len2]; } *str = str2; *len = len2; } 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); xf_resize_chars(str, len, add_len, class); memcpy(*str + *pos, add, add_len); *pos += add_len; } @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 newWithClass: self]; nlen++; if ((ret = malloc(nlen)) == NULL) @throw [OFNoMemException newWithClass: self andSize: nlen]; for (i = j = 0; i < len; i++) { switch (s[i]) { case '<': xf_add2chars(&ret, &nlen, &j, "<", self); break; case '>': xf_add2chars(&ret, &nlen, &j, ">", self); break; case '"': xf_add2chars(&ret, &nlen, &j, """, self); break; case '\'': xf_add2chars(&ret, &nlen, &j, "'", self); break; case '&': xf_add2chars(&ret, &nlen, &j, "&", self); break; default: ret[j++] = s[i]; break; } } |
︙ | ︙ | |||
134 135 136 137 138 139 140 | char *arg, *val, *xml; size_t i, len; va_list args; /* Start of tag */ len = strlen(name); if (SIZE_MAX - len < 3) | | | | | | < | < < | > | < < < < < | < | > | < < < | < < < < | | 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 newWithClass: self]; len += 3; if ((xml = malloc(len)) == NULL) @throw [OFNoMemException newWithClass: self andSize: len]; i = 0; xml[i++] = '<'; memcpy(xml + i, name, strlen(name)); i += strlen(name); /* Arguments */ va_start(args, data); while ((arg = va_arg(args, char*)) != NULL && (val = va_arg(args, char*)) != NULL) { char *esc_val = NULL; @try { esc_val = [OFXMLFactory escapeCString: val]; } @catch (OFException *e) { free(xml); @throw e; } @try { xf_resize_chars(&xml, &len, 1 + strlen(arg) + 2 + strlen(esc_val) + 1, self); xml[i++] = ' '; memcpy(xml + i, arg, strlen(arg)); i += strlen(arg); xml[i++] = '='; xml[i++] = '\''; memcpy(xml + i, esc_val, strlen(esc_val)); i += strlen(esc_val); xml[i++] = '\''; } @finally { free(esc_val); } } va_end(args); /* End of tag */ if (close) { if (data == NULL) { xf_resize_chars(&xml, &len, 2 - 1, self); xml[i++] = '/'; xml[i++] = '>'; } else { xf_resize_chars(&xml, &len, 1 + strlen(data) + 2 + 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 | size_t i, len, pos; if (strs[0] == NULL) return NULL; len = strlen(*strs); if (SIZE_MAX - len < 1) | | | | | < < < < < < | 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 newWithClass: self]; len++; if ((ret = malloc(len)) == NULL) @throw [OFNoMemException newWithClass: self andSize: len]; memcpy(ret, strs[0], len - 1); pos = len - 1; for (i = 1; strs[i] != NULL; i++) xf_add2chars(&ret, &len, &pos, strs[i], self); for (i = 0; strs[i] != NULL; i++) free(strs[i]); ret[pos] = 0; return ret; } @end |