ObjFW  Check-in [6ae98c1dcf]

Overview
Comment:Last round of renames (hopefully)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 6ae98c1dcfd20cfa5ddd37b680b41c717add56579efc1be703ebe67a3af1c563
User & Date: js on 2021-04-25 19:58:13
Other Links: manifest | tags
Context
2021-04-25
21:20
runtime: Fix cut & paste mistake check-in: bbf92b046a user: js tags: trunk
20:22
Merge trunk into branch "unix-sockets" check-in: fb40f7048d user: js tags: unix-sockets
20:12
Merge trunk into branch "sandbox" Leaf check-in: 1c5fb04713 user: js tags: sandbox
20:01
Merge trunk into 1.0 branch check-in: b9744f93bf user: js tags: 1.0
19:58
Last round of renames (hopefully) check-in: 6ae98c1dcf user: js tags: trunk
19:41
OFStrdup -> OFStrDup and slightly change behavior check-in: fd51df7022 user: js tags: trunk
Changes

Modified generators/unicode/TableGenerator.h from [bbac35a082] to [7eb32f5774].

37
38
39
40
41
42
43
44
45


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


44
45
46
47
48
49
50
51
52
53
54
55







-
-
+
+










	size_t _uppercaseTableSize;
	size_t _lowercaseTableSize;
	size_t _titlecaseTableSize;
	size_t _caseFoldingTableSize;
	size_t _decompositionTableSize;
	size_t _decompositionCompatTableSize;
	enum {
		STATE_UNICODE_DATA,
		STATE_CASE_FOLDING
		stateUnicodeData,
		stateCaseFolding
	} _state;
}

- (void)parseUnicodeData: (OFHTTPResponse *)response;
- (void)parseCaseFolding: (OFHTTPResponse *)response;
- (void)applyDecompositionRecursivelyForTable: (OFString *[0x110000])table;
- (void)writeFiles;
- (void)writeTablesToFile: (OFString *)path;
- (void)writeHeaderToFile: (OFString *)path;
@end

Modified generators/unicode/TableGenerator.m from [98580137e5] to [6b626ad955].

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







-
+
















-
+


-
+







}

- (void)applicationDidFinishLaunching
{
	OFHTTPRequest *request;

	[OFStdOut writeString: @"Downloading UnicodeData.txt…"];
	_state = STATE_UNICODE_DATA;
	_state = stateUnicodeData;
	request = [OFHTTPRequest requestWithURL:
	    [OFURL URLWithString: unicodeDataURL]];
	[_HTTPClient asyncPerformRequest: request];
}

-      (void)client: (OFHTTPClient *)client
  didPerformRequest: (OFHTTPRequest *)request
	   response: (OFHTTPResponse *)response
	  exception: (id)exception
{
	if (exception != nil)
		@throw exception;

	[OFStdOut writeLine: @" done"];

	switch (_state) {
	case STATE_UNICODE_DATA:
	case stateUnicodeData:
		[self parseUnicodeData: response];
		break;
	case STATE_CASE_FOLDING:
	case stateCaseFolding:
		[self parseCaseFolding: response];
		break;
	}
}

- (void)parseUnicodeData: (OFHTTPResponse *)response
{
163
164
165
166
167
168
169
170

171
172
173
174
175
176
177
163
164
165
166
167
168
169

170
171
172
173
174
175
176
177







-
+








	[self applyDecompositionRecursivelyForTable: _decompositionTable];
	[self applyDecompositionRecursivelyForTable: _decompositionCompatTable];

	[OFStdOut writeLine: @" done"];

	[OFStdOut writeString: @"Downloading CaseFolding.txt…"];
	_state = STATE_CASE_FOLDING;
	_state = stateCaseFolding;
	request = [OFHTTPRequest requestWithURL:
	    [OFURL URLWithString: caseFoldingURL]];
	[_HTTPClient asyncPerformRequest: request];
}

- (void)parseCaseFolding: (OFHTTPResponse *)response
{

Modified src/OFASPrintF.m from [2d355cbec1] to [88da2745a9].

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







-
-
-
-
-
+
+
+
+
+


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







	char subformat[maxSubformatLen + 1];
	size_t subformatLen;
	va_list arguments;
	char *buffer;
	size_t bufferLen;
	size_t i, last;
	enum {
		StateString,
		StateFormatFlags,
		StateFormatFieldWidth,
		StateFormatLengthModifier,
		StateFormatConversionSpecifier
		stateString,
		stateFormatFlags,
		stateFormatFieldWidth,
		stateFormatLengthModifier,
		stateFormatConversionSpecifier
	} state;
	enum {
		LengthModifierNone,
		LengthModifierHH,
		LengthModifierH,
		LengthModifierL,
		LengthModifierLL,
		LengthModifierJ,
		LengthModifierZ,
		LengthModifierT,
		LengthModifierCapitalL
		lengthModifierNone,
		lengthModifierHH,
		lengthModifierH,
		lengthModifierL,
		lengthModifierLL,
		lengthModifierJ,
		lengthModifierZ,
		lengthModifierT,
		lengthModifierCapitalL
	} lengthModifier;
	bool useLocale;
};

#ifdef HAVE_ASPRINTF_L
static locale_t cLocale;

190
191
192
193
194
195
196
197

198
199
200
201
202
203
204
190
191
192
193
194
195
196

197
198
199
200
201
202
203
204







-
+







			    ctx->i - ctx->last))
				return false;

		if (!appendSubformat(ctx, ctx->format + ctx->i, 1))
			return false;

		ctx->last = ctx->i + 1;
		ctx->state = StateFormatFlags;
		ctx->state = stateFormatFlags;
	}

	return true;
}

static bool
formatFlagsState(struct context *ctx)
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
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
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
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







-
+
















-
+


















-
+




-
+















-
+




-
+















-
+















-
+















-
+






-
+











-
+










-
+









-
+

















-
+








		break;
	case ',':
		/* ObjFW extension: Use decimal point from locale */
		ctx->useLocale = true;
		break;
	default:
		ctx->state = StateFormatFieldWidth;
		ctx->state = stateFormatFieldWidth;
		ctx->i--;

		break;
	}

	return true;
}

static bool
formatFieldWidthState(struct context *ctx)
{
	if ((ctx->format[ctx->i] >= '0' && ctx->format[ctx->i] <= '9') ||
	    ctx->format[ctx->i] == '*' || ctx->format[ctx->i] == '.') {
		if (!appendSubformat(ctx, ctx->format + ctx->i, 1))
			return false;
	} else {
		ctx->state = StateFormatLengthModifier;
		ctx->state = stateFormatLengthModifier;
		ctx->i--;
	}

	return true;
}

static bool
formatLengthModifierState(struct context *ctx)
{
	/* Only one allowed */
	switch (ctx->format[ctx->i]) {
	case 'h': /* and also hh */
		if (ctx->formatLen > ctx->i + 1 &&
		    ctx->format[ctx->i + 1] == 'h') {
			if (!appendSubformat(ctx, ctx->format + ctx->i, 2))
				return false;

			ctx->i++;
			ctx->lengthModifier = LengthModifierHH;
			ctx->lengthModifier = lengthModifierHH;
		} else {
			if (!appendSubformat(ctx, ctx->format + ctx->i, 1))
				return false;

			ctx->lengthModifier = LengthModifierH;
			ctx->lengthModifier = lengthModifierH;
		}

		break;
	case 'l': /* and also ll */
		if (ctx->formatLen > ctx->i + 1 &&
		    ctx->format[ctx->i + 1] == 'l') {
#ifndef OF_WINDOWS
			if (!appendSubformat(ctx, ctx->format + ctx->i, 2))
				return false;
#else
			if (!appendSubformat(ctx, "I64", 3))
				return false;
#endif

			ctx->i++;
			ctx->lengthModifier = LengthModifierLL;
			ctx->lengthModifier = lengthModifierLL;
		} else {
			if (!appendSubformat(ctx, ctx->format + ctx->i, 1))
				return false;

			ctx->lengthModifier = LengthModifierL;
			ctx->lengthModifier = lengthModifierL;
		}

		break;
	case 'j':
#if defined(OF_WINDOWS)
		if (!appendSubformat(ctx, "I64", 3))
			return false;
#elif defined(_NEWLIB_VERSION) || defined(OF_HPUX)
		if (!appendSubformat(ctx, "ll", 2))
			return false;
#else
		if (!appendSubformat(ctx, ctx->format + ctx->i, 1))
			return false;
#endif

		ctx->lengthModifier = LengthModifierJ;
		ctx->lengthModifier = lengthModifierJ;

		break;
	case 'z':
#if defined(OF_WINDOWS)
		if (sizeof(size_t) == 8)
			if (!appendSubformat(ctx, "I64", 3))
				return false;
#elif defined(_NEWLIB_VERSION) || defined(OF_HPUX)
		if (!appendSubformat(ctx, "l", 1))
			return false;
#else
		if (!appendSubformat(ctx, ctx->format + ctx->i, 1))
			return false;
#endif

		ctx->lengthModifier = LengthModifierZ;
		ctx->lengthModifier = lengthModifierZ;

		break;
	case 't':
#if defined(OF_WINDOWS)
		if (sizeof(ptrdiff_t) == 8)
			if (!appendSubformat(ctx, "I64", 3))
				return false;
#elif defined(_NEWLIB_VERSION) || defined(OF_HPUX)
		if (!appendSubformat(ctx, "l", 1))
			return false;
#else
		if (!appendSubformat(ctx, ctx->format + ctx->i, 1))
			return false;
#endif

		ctx->lengthModifier = LengthModifierT;
		ctx->lengthModifier = lengthModifierT;

		break;
	case 'L':
		if (!appendSubformat(ctx, ctx->format + ctx->i, 1))
			return false;

		ctx->lengthModifier = LengthModifierCapitalL;
		ctx->lengthModifier = lengthModifierCapitalL;

		break;
#ifdef OF_WINDOWS
	case 'I': /* win32 strangeness (I64 instead of ll or j) */
		if (ctx->formatLen > ctx->i + 2 &&
		    ctx->format[ctx->i + 1] == '6' &&
		    ctx->format[ctx->i + 2] == '4') {
			if (!appendSubformat(ctx, ctx->format + ctx->i, 3))
				return false;

			ctx->i += 2;
			ctx->lengthModifier = LengthModifierLL;
			ctx->lengthModifier = lengthModifierLL;
		} else
			ctx->i--;

		break;
#endif
#ifdef OF_IOS
	case 'q': /* iOS uses this for PRI?64 */
		if (!appendSubformat(ctx, ctx->format + ctx->i, 1))
			return false;

		ctx->lengthModifier = LengthModifierLL;
		ctx->lengthModifier = lengthModifierLL;

		break;
#endif
	default:
		ctx->i--;

		break;
	}

	ctx->state = StateFormatConversionSpecifier;
	ctx->state = stateFormatConversionSpecifier;
	return true;
}

static bool
formatConversionSpecifierState(struct context *ctx)
{
	char *tmp = NULL;
	int tmpLen = 0;
#ifndef HAVE_ASPRINTF_L
	OFString *point;
#endif

	if (!appendSubformat(ctx, ctx->format + ctx->i, 1))
		return false;

	switch (ctx->format[ctx->i]) {
	case '@':
		if (ctx->lengthModifier != LengthModifierNone)
		if (ctx->lengthModifier != lengthModifierNone)
			return false;

		ctx->subformat[ctx->subformatLen - 1] = 's';

		@try {
			id object;

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







-
+


















-
+







		} @catch (id e) {
			free(ctx->buffer);
			@throw e;
		}

		break;
	case 'C':
		if (ctx->lengthModifier != LengthModifierNone)
		if (ctx->lengthModifier != lengthModifierNone)
			return false;

		ctx->subformat[ctx->subformatLen - 1] = 's';

		{
			char buffer[5];
			size_t len = OFUTF8StringEncode(
			    va_arg(ctx->arguments, OFUnichar), buffer);

			if (len == 0)
				return false;

			buffer[len] = 0;
			tmpLen = asprintf(&tmp, ctx->subformat, buffer);
		}

		break;
	case 'S':
		if (ctx->lengthModifier != LengthModifierNone)
		if (ctx->lengthModifier != lengthModifierNone)
			return false;

		ctx->subformat[ctx->subformatLen - 1] = 's';

		{
			const OFUnichar *arg =
			    va_arg(ctx->arguments, const OFUnichar *);
466
467
468
469
470
471
472
473
474
475



476
477
478
479

480
481
482
483

484
485
486
487

488
489
490
491

492
493
494
495

496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511



512
513
514
515

516
517
518
519

520
521
522
523

524
525
526
527

528
529
530
531

532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550


551
552
553
554
555
556
557
558
559
560
561

562
563
564
565
566
567
568
466
467
468
469
470
471
472



473
474
475
476
477
478

479
480
481
482

483
484
485
486

487
488
489
490

491
492
493
494

495
496
497
498
499
500
501
502
503
504
505
506
507
508



509
510
511
512
513
514

515
516
517
518

519
520
521
522

523
524
525
526

527
528
529
530

531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548


549
550
551
552
553
554
555
556
557
558
559
560

561
562
563
564
565
566
567
568







-
-
-
+
+
+



-
+



-
+



-
+



-
+



-
+













-
-
-
+
+
+



-
+



-
+



-
+



-
+



-
+

















-
-
+
+










-
+







			free(buffer);
		}

		break;
	case 'd':
	case 'i':
		switch (ctx->lengthModifier) {
		case LengthModifierNone:
		case LengthModifierHH:
		case LengthModifierH:
		case lengthModifierNone:
		case lengthModifierHH:
		case lengthModifierH:
			tmpLen = asprintf(&tmp, ctx->subformat,
			    va_arg(ctx->arguments, int));
			break;
		case LengthModifierL:
		case lengthModifierL:
			tmpLen = asprintf(&tmp, ctx->subformat,
			    va_arg(ctx->arguments, long));
			break;
		case LengthModifierLL:
		case lengthModifierLL:
			tmpLen = asprintf(&tmp, ctx->subformat,
			    va_arg(ctx->arguments, long long));
			break;
		case LengthModifierJ:
		case lengthModifierJ:
			tmpLen = asprintf(&tmp, ctx->subformat,
			    va_arg(ctx->arguments, intmax_t));
			break;
		case LengthModifierZ:
		case lengthModifierZ:
			tmpLen = asprintf(&tmp, ctx->subformat,
			    va_arg(ctx->arguments, ssize_t));
			break;
		case LengthModifierT:
		case lengthModifierT:
			tmpLen = asprintf(&tmp, ctx->subformat,
			    va_arg(ctx->arguments, ptrdiff_t));
			break;
		default:
			return false;
		}

		break;
	case 'o':
	case 'u':
	case 'x':
	case 'X':
		switch (ctx->lengthModifier) {
		case LengthModifierNone:
		case LengthModifierHH:
		case LengthModifierH:
		case lengthModifierNone:
		case lengthModifierHH:
		case lengthModifierH:
			tmpLen = asprintf(&tmp, ctx->subformat,
			    va_arg(ctx->arguments, unsigned int));
			break;
		case LengthModifierL:
		case lengthModifierL:
			tmpLen = asprintf(&tmp, ctx->subformat,
			    va_arg(ctx->arguments, unsigned long));
			break;
		case LengthModifierLL:
		case lengthModifierLL:
			tmpLen = asprintf(&tmp, ctx->subformat,
			    va_arg(ctx->arguments, unsigned long long));
			break;
		case LengthModifierJ:
		case lengthModifierJ:
			tmpLen = asprintf(&tmp, ctx->subformat,
			    va_arg(ctx->arguments, uintmax_t));
			break;
		case LengthModifierZ:
		case lengthModifierZ:
			tmpLen = asprintf(&tmp, ctx->subformat,
			    va_arg(ctx->arguments, size_t));
			break;
		case LengthModifierT:
		case lengthModifierT:
			tmpLen = asprintf(&tmp, ctx->subformat,
			    va_arg(ctx->arguments, ptrdiff_t));
			break;
		default:
			return false;
		}

		break;
	case 'f':
	case 'F':
	case 'e':
	case 'E':
	case 'g':
	case 'G':
	case 'a':
	case 'A':
		switch (ctx->lengthModifier) {
		case LengthModifierNone:
		case LengthModifierL:
		case lengthModifierNone:
		case lengthModifierL:
#ifdef HAVE_ASPRINTF_L
			if (!ctx->useLocale)
				tmpLen = asprintf_l(&tmp, cLocale,
				    ctx->subformat,
				    va_arg(ctx->arguments, double));
			else
#endif
				tmpLen = asprintf(&tmp, ctx->subformat,
				    va_arg(ctx->arguments, double));
			break;
		case LengthModifierCapitalL:
		case lengthModifierCapitalL:
#ifdef HAVE_ASPRINTF_L
			if (!ctx->useLocale)
				tmpLen = asprintf_l(&tmp, cLocale,
				    ctx->subformat,
				    va_arg(ctx->arguments, long double));
			else
#endif
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
662
663

664
665
666

667
668
669
670

671
672
673
674

675
676
677
678

679
680
681
682

683
684
685
686

687
688
689
690

691
692
693
694
695
696
697
698
699
700

701
702
703
704
705
706
707
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
662

663
664
665

666
667
668
669

670
671
672
673

674
675
676
677

678
679
680
681

682
683
684
685

686
687
688
689

690
691
692
693
694
695
696
697
698
699

700
701
702
703
704
705
706
707







-
+



-
+

















-
+




-
+










-
+








-
+


-
+



-
+



-
+



-
+



-
+



-
+



-
+









-
+







			tmp = tmp2;
		}
#endif

		break;
	case 'c':
		switch (ctx->lengthModifier) {
		case LengthModifierNone:
		case lengthModifierNone:
			tmpLen = asprintf(&tmp, ctx->subformat,
			    va_arg(ctx->arguments, int));
			break;
		case LengthModifierL:
		case lengthModifierL:
#ifdef HAVE_WCHAR_H
# if WINT_MAX >= INT_MAX
			tmpLen = asprintf(&tmp, ctx->subformat,
			    va_arg(ctx->arguments, wint_t));
# else
			tmpLen = asprintf(&tmp, ctx->subformat,
			    va_arg(ctx->arguments, int));
# endif
			break;
#endif
		default:
			return false;
		}

		break;
	case 's':
		switch (ctx->lengthModifier) {
		case LengthModifierNone:
		case lengthModifierNone:
			tmpLen = asprintf(&tmp, ctx->subformat,
			    va_arg(ctx->arguments, const char *));
			break;
#ifdef HAVE_WCHAR_T
		case LengthModifierL:
		case lengthModifierL:
			tmpLen = asprintf(&tmp, ctx->subformat,
			    va_arg(ctx->arguments, const wchar_t *));
			break;
#endif
		default:
			return false;
		}

		break;
	case 'p':
		if (ctx->lengthModifier != LengthModifierNone)
		if (ctx->lengthModifier != lengthModifierNone)
			return false;

		tmpLen = asprintf(&tmp, ctx->subformat,
		    va_arg(ctx->arguments, void *));

		break;
	case 'n':
		switch (ctx->lengthModifier) {
		case LengthModifierNone:
		case lengthModifierNone:
			*va_arg(ctx->arguments, int *) = (int)ctx->bufferLen;
			break;
		case LengthModifierHH:
		case lengthModifierHH:
			*va_arg(ctx->arguments, signed char *) =
			    (signed char)ctx->bufferLen;
			break;
		case LengthModifierH:
		case lengthModifierH:
			*va_arg(ctx->arguments, short *) =
			    (short)ctx->bufferLen;
			break;
		case LengthModifierL:
		case lengthModifierL:
			*va_arg(ctx->arguments, long *) =
			    (long)ctx->bufferLen;
			break;
		case LengthModifierLL:
		case lengthModifierLL:
			*va_arg(ctx->arguments, long long *) =
			    (long long)ctx->bufferLen;
			break;
		case LengthModifierJ:
		case lengthModifierJ:
			*va_arg(ctx->arguments, intmax_t *) =
			    (intmax_t)ctx->bufferLen;
			break;
		case LengthModifierZ:
		case lengthModifierZ:
			*va_arg(ctx->arguments, size_t *) =
			    (size_t)ctx->bufferLen;
			break;
		case LengthModifierT:
		case lengthModifierT:
			*va_arg(ctx->arguments, ptrdiff_t *) =
			    (ptrdiff_t)ctx->bufferLen;
			break;
		default:
			return false;
		}

		break;
	case '%':
		if (ctx->lengthModifier != LengthModifierNone)
		if (ctx->lengthModifier != lengthModifierNone)
			return false;

		if (!appendString(ctx, "%", 1))
			return false;

		break;
	default:
718
719
720
721
722
723
724
725

726
727
728
729

730
731
732
733
734
735
736
718
719
720
721
722
723
724

725
726
727
728

729
730
731
732
733
734
735
736







-
+



-
+







		}

		free(tmp);
	}

	memset(ctx->subformat, 0, maxSubformatLen);
	ctx->subformatLen = 0;
	ctx->lengthModifier = LengthModifierNone;
	ctx->lengthModifier = lengthModifierNone;
	ctx->useLocale = false;

	ctx->last = ctx->i + 1;
	ctx->state = StateString;
	ctx->state = stateString;

	return true;
}

static bool (*states[])(struct context *) = {
	stringState,
	formatFlagsState,
747
748
749
750
751
752
753
754
755


756
757
758
759
760
761
762
763
764
765
766
767
768

769
770
771
772
773
774
775
747
748
749
750
751
752
753


754
755
756
757
758
759
760
761
762
763
764
765
766
767

768
769
770
771
772
773
774
775







-
-
+
+












-
+







	ctx.format = format;
	ctx.formatLen = strlen(format);
	memset(ctx.subformat, 0, maxSubformatLen + 1);
	ctx.subformatLen = 0;
	va_copy(ctx.arguments, arguments);
	ctx.bufferLen = 0;
	ctx.last = 0;
	ctx.state = StateString;
	ctx.lengthModifier = LengthModifierNone;
	ctx.state = stateString;
	ctx.lengthModifier = lengthModifierNone;
	ctx.useLocale = false;

	if ((ctx.buffer = malloc(1)) == NULL)
		return -1;

	for (ctx.i = 0; ctx.i < ctx.formatLen; ctx.i++) {
		if (!states[ctx.state](&ctx)) {
			free(ctx.buffer);
			return -1;
		}
	}

	if (ctx.state != StateString) {
	if (ctx.state != stateString) {
		free(ctx.buffer);
		return -1;
	}

	if (!appendString(&ctx, ctx.format + ctx.last,
	    ctx.formatLen - ctx.last)) {
		free(ctx.buffer);

Modified src/OFHTTPCookie.m from [d82c9df2c7] to [a0376d23d9].

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







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




-
+

-
+




-
+



-
+


-
+

-
+


-
+





-
+










-
+


-
+








-
+


-
+

-
+

-
+




-
+

-
+




-
+




-
+









-
+



-
+







	OFMutableArray OF_GENERIC(OFHTTPCookie *) *ret = [OFMutableArray array];
	void *pool = objc_autoreleasePoolPush();
	OFString *string = [headerFields objectForKey: @"Set-Cookie"];
	OFString *domain = URL.host;
	const OFUnichar *characters = string.characters;
	size_t length = string.length, last = 0;
	enum {
		StatePreName,
		StateName,
		StateExpectValue,
		StateValue,
		StateQuotedValue,
		StatePostQuotedValue,
		StatePreAttrName,
		StateAttrName,
		StateAttrValue
	} state = StatePreName;
		statePreName,
		stateName,
		stateExpectValue,
		stateValue,
		stateQuotedValue,
		statePostQuotedValue,
		statePreAttrName,
		stateAttrName,
		stateAttrValue
	} state = statePreName;
	OFString *name = nil, *value = nil;

	for (size_t i = 0; i < length; i++) {
		switch (state) {
		case StatePreName:
		case statePreName:
			if (characters[i] != ' ') {
				state = StateName;
				state = stateName;
				last = i;
				i--;
			}
			break;
		case StateName:
		case stateName:
			if (characters[i] == '=') {
				name = [string substringWithRange:
				    OFRangeMake(last, i - last)];
				state = StateExpectValue;
				state = stateExpectValue;
			}
			break;
		case StateExpectValue:
		case stateExpectValue:
			if (characters[i] == '"') {
				state = StateQuotedValue;
				state = stateQuotedValue;
				last = i + 1;
			} else {
				state = StateValue;
				state = stateValue;
				last = i;
			}

			i--;
			break;
		case StateValue:
		case stateValue:
			if (characters[i] == ';' || characters[i] == ',') {
				value = [string substringWithRange:
				    OFRangeMake(last, i - last)];

				[ret addObject:
				    [OFHTTPCookie cookieWithName: name
							   value: value
							  domain: domain]];

				state = (characters[i] == ';'
				    ? StatePreAttrName : StatePreName);
				    ? statePreAttrName : statePreName);
			}
			break;
		case StateQuotedValue:
		case stateQuotedValue:
			if (characters[i] == '"') {
				value = [string substringWithRange:
				    OFRangeMake(last, i - last)];
				[ret addObject:
				    [OFHTTPCookie cookieWithName: name
							   value: value
							  domain: domain]];

				state = StatePostQuotedValue;
				state = statePostQuotedValue;
			}
			break;
		case StatePostQuotedValue:
		case statePostQuotedValue:
			if (characters[i] == ';')
				state = StatePreAttrName;
				state = statePreAttrName;
			else if (characters[i] == ',')
				state = StatePreName;
				state = statePreName;
			else
				@throw [OFInvalidFormatException exception];

			break;
		case StatePreAttrName:
		case statePreAttrName:
			if (characters[i] != ' ') {
				state = StateAttrName;
				state = stateAttrName;
				last = i;
				i--;
			}
			break;
		case StateAttrName:
		case stateAttrName:
			if (characters[i] == '=') {
				name = [string substringWithRange:
				    OFRangeMake(last, i - last)];

				state = StateAttrValue;
				state = stateAttrValue;
				last = i + 1;
			} else if (characters[i] == ';' ||
			    characters[i] == ',') {
				name = [string substringWithRange:
				    OFRangeMake(last, i - last)];

				handleAttribute(ret.lastObject, name, nil);

				state = (characters[i] == ';'
				    ? StatePreAttrName : StatePreName);
				    ? statePreAttrName : statePreName);
			}

			break;
		case StateAttrValue:
		case stateAttrValue:
			if (characters[i] == ';' || characters[i] == ',') {
				value = [string substringWithRange:
				    OFRangeMake(last, i - last)];

				/*
				 * Expires often contains a comma, even though
				 * the comma is used as a separator for
194
195
196
197
198
199
200
201

202
203
204
205
206
207
208
209
210



211
212
213


214
215
216

217
218
219
220
221
222
223
224

225
226
227
228
229

230
231
232
233
234
235
236
237

238
239
240
241
242
243
244
194
195
196
197
198
199
200

201
202
203
204
205
206
207



208
209
210
211


212
213
214
215

216
217
218
219
220
221
222
223

224
225
226
227
228

229
230
231
232
233
234
235
236

237
238
239
240
241
242
243
244







-
+






-
-
-
+
+
+

-
-
+
+


-
+







-
+




-
+







-
+







				    [value isEqual: @"Sat"] ||
				    [value isEqual: @"Sun"]))
					break;

				handleAttribute(ret.lastObject, name, value);

				state = (characters[i] == ';'
				    ? StatePreAttrName : StatePreName);
				    ? statePreAttrName : statePreName);
			}
			break;
		}
	}

	switch (state) {
	case StatePreName:
	case StatePostQuotedValue:
	case StatePreAttrName:
	case statePreName:
	case statePostQuotedValue:
	case statePreAttrName:
		break;
	case StateName:
	case StateQuotedValue:
	case stateName:
	case stateQuotedValue:
		@throw [OFInvalidFormatException exception];
		break;
	case StateValue:
	case stateValue:
		value = [string substringWithRange:
		    OFRangeMake(last, length - last)];
		[ret addObject: [OFHTTPCookie cookieWithName: name
						       value: value
						      domain: domain]];
		break;
	/* We end up here if the cookie is just foo= */
	case StateExpectValue:
	case stateExpectValue:
		[ret addObject: [OFHTTPCookie cookieWithName: name
						       value: @""
						      domain: domain]];
		break;
	case StateAttrName:
	case stateAttrName:
		if (last != length) {
			name = [string substringWithRange:
			    OFRangeMake(last, length - last)];

			handleAttribute(ret.lastObject, name, nil);
		}
		break;
	case StateAttrValue:
	case stateAttrValue:
		value = [string substringWithRange:
		    OFRangeMake(last, length - last)];

		handleAttribute(ret.lastObject, name, value);

		break;
	}

Modified src/OFHTTPResponse.m from [85cc9e3ded] to [f1cfec9f86].

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







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






-
+

-
+



-
+



-
+



-
+





-
+



-
+

-
+


-
+



-
+










-
+



-
+








-
+


-
+

-
+






-
+








static OFStringEncoding
encodingForContentType(OFString *contentType)
{
	const char *UTF8String = contentType.UTF8String;
	size_t last, length = contentType.UTF8StringLength;
	enum {
		StateType,
		StateBeforeParamName,
		StateParamName,
		StateParamValueOrQuote,
		StateParamValue,
		StateParamQuotedValue,
		StateAfterParamValue
	} state = StateType;
		stateType,
		stateBeforeParamName,
		stateParamName,
		stateParamValueOrQuote,
		stateParamValue,
		stateParamQuotedValue,
		stateAfterParamValue
	} state = stateType;
	OFString *name = nil, *value = nil, *charset = nil;
	OFStringEncoding ret;

	last = 0;
	for (size_t i = 0; i < length; i++) {
		switch (state) {
		case StateType:
		case stateType:
			if (UTF8String[i] == ';') {
				state = StateBeforeParamName;
				state = stateBeforeParamName;
				last = i + 1;
			}
			break;
		case StateBeforeParamName:
		case stateBeforeParamName:
			if (UTF8String[i] == ' ')
				last = i + 1;
			else {
				state = StateParamName;
				state = stateParamName;
				i--;
			}
			break;
		case StateParamName:
		case stateParamName:
			if (UTF8String[i] == '=') {
				name = [OFString
				    stringWithUTF8String: UTF8String + last
						  length: i - last];

				state = StateParamValueOrQuote;
				state = stateParamValueOrQuote;
				last = i + 1;
			}
			break;
		case StateParamValueOrQuote:
		case stateParamValueOrQuote:
			if (UTF8String[i] == '"') {
				state = StateParamQuotedValue;
				state = stateParamQuotedValue;
				last = i + 1;
			} else {
				state = StateParamValue;
				state = stateParamValue;
				i--;
			}
			break;
		case StateParamValue:
		case stateParamValue:
			if (UTF8String[i] == ';') {
				value = [OFString
				    stringWithUTF8String: UTF8String + last
						  length: i - last];
				value =
				    value.stringByDeletingTrailingWhitespaces;

				if ([name isEqual: @"charset"])
					charset = value;

				state = StateBeforeParamName;
				state = stateBeforeParamName;
				last = i + 1;
			}
			break;
		case StateParamQuotedValue:
		case stateParamQuotedValue:
			if (UTF8String[i] == '"') {
				value = [OFString
				    stringWithUTF8String: UTF8String + last
						  length: i - last];

				if ([name isEqual: @"charset"])
					charset = value;

				state = StateAfterParamValue;
				state = stateAfterParamValue;
			}
			break;
		case StateAfterParamValue:
		case stateAfterParamValue:
			if (UTF8String[i] == ';') {
				state = StateBeforeParamName;
				state = stateBeforeParamName;
				last = i + 1;
			} else if (UTF8String[i] != ' ')
				return OFStringEncodingAutodetect;
			break;
		}
	}
	if (state == StateParamValue) {
	if (state == stateParamValue) {
		value = [OFString stringWithUTF8String: UTF8String + last
						length: length - last];
		value = value.stringByDeletingTrailingWhitespaces;

		if ([name isEqual: @"charset"])
			charset = value;
	}

Modified src/OFHTTPServer.m from [40f5819df6] to [76527fc01b].

70
71
72
73
74
75
76
77
78
79



80
81
82
83
84
85
86
70
71
72
73
74
75
76



77
78
79
80
81
82
83
84
85
86







-
-
-
+
+
+







@interface OFHTTPServerConnection: OFObject <OFTCPSocketDelegate>
{
@public
	OFStreamSocket *_socket;
	OFHTTPServer *_server;
	OFTimer *_timer;
	enum {
		StateAwaitingProlog,
		StateParsingHeaders,
		StateSendResponse
		stateAwaitingProlog,
		stateParsingHeaders,
		stateSendResponse
	} _state;
	uint8_t _HTTPMinorVersion;
	OFHTTPRequestMethod _method;
	OFString *_host, *_path;
	uint16_t _port;
	OFMutableDictionary *_headers;
	size_t _contentLength;
286
287
288
289
290
291
292
293

294
295
296
297
298
299
300
286
287
288
289
290
291
292

293
294
295
296
297
298
299
300







-
+







		_server = [server retain];
		_timer = [[OFTimer
		    scheduledTimerWithTimeInterval: 10
					    target: _socket
					  selector: @selector(
							cancelAsyncRequests)
					   repeats: false] retain];
		_state = StateAwaitingProlog;
		_state = stateAwaitingProlog;
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}
320
321
322
323
324
325
326
327

328
329

330
331
332
333
334
335
336
320
321
322
323
324
325
326

327
328

329
330
331
332
333
334
335
336







-
+

-
+







     exception: (id)exception
{
	if (line == nil || exception != nil)
		return false;

	@try {
		switch (_state) {
		case StateAwaitingProlog:
		case stateAwaitingProlog:
			return [self parseProlog: line];
		case StateParsingHeaders:
		case stateParsingHeaders:
			return [self parseHeaders: line];
		default:
			return false;
		}
	} @catch (OFWriteFailedException *e) {
		return false;
	}
385
386
387
388
389
390
391
392

393
394
395
396
397
398
399
385
386
387
388
389
390
391

392
393
394
395
396
397
398
399







-
+







	[path makeImmutable];

	if (![path hasPrefix: @"/"])
		return [self sendErrorAndClose: 400];

	_headers = [[OFMutableDictionary alloc] init];
	_path = [path copy];
	_state = StateParsingHeaders;
	_state = stateParsingHeaders;

	return true;
}

- (bool)parseHeaders: (OFString *)line
{
	OFString *key, *value, *old;
427
428
429
430
431
432
433
434

435
436
437
438
439
440
441
427
428
429
430
431
432
433

434
435
436
437
438
439
440
441







-
+







			     contentLength: contentLength];

			[_timer invalidate];
			[_timer release];
			_timer = nil;
		}

		_state = StateSendResponse;
		_state = stateSendResponse;
		[self createResponse];

		return false;
	}

	pos = [line rangeOfString: @":"].location;
	if (pos == OFNotFound)

Modified src/OFInflateStream.m from [f7f7b24442] to [94ff1319c4].

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







-
-
-
-
-
+
+
+
+
+



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







#ifndef OF_INFLATE64_STREAM_M
# define bufferSize OFInflateStreamBufferSize
#else
# define bufferSize OFInflate64StreamBufferSize
#endif

enum State {
	StateBlockHeader,
	StateUncompressedBlockHeader,
	StateUncompressedBlock,
	StateHuffmanTree,
	StateHuffmanBlock
	stateBlockHeader,
	stateUncompressedBlockHeader,
	stateUncompressedBlock,
	stateHuffmanTree,
	stateHuffmanBlock
};

enum HuffmanState {
	HuffmanStateWriteValue,
	HuffmanStateAwaitCode,
	HuffmanStateAwaitLengthExtraBits,
	HuffmanStateAwaitDistance,
	HuffmanStateAwaitDistanceExtraBits,
	HuffmanStateProcessPair
	huffmanStateWriteValue,
	huffmanStateAwaitCode,
	huffmanStateAwaitLengthExtraBits,
	huffmanStateAwaitDistance,
	huffmanStateAwaitDistanceExtraBits,
	huffmanStateProcessPair
};

#ifndef OF_INFLATE64_STREAM_M
static const uint8_t numDistanceCodes = 30;
static const uint8_t lengthCodes[29] = {
	/* indices are -257, values -3 */
	0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56,
205
206
207
208
209
210
211
212

213
214
215
216
217
218
219

220
221
222
223
224
225
226
205
206
207
208
209
210
211

212
213
214
215
216
217
218

219
220
221
222
223
224
225
226







-
+






-
+







- (void)dealloc
{
	if (_stream != nil)
		[self close];

	OFFreeMemory(_slidingWindow);

	if (_state == StateHuffmanTree) {
	if (_state == stateHuffmanTree) {
		OFFreeMemory(_context.huffmanTree.lengths);

		if (_context.huffmanTree.codeLenTree != NULL)
			OFHuffmanTreeFree(_context.huffmanTree.codeLenTree);
	}

	if (_state == StateHuffmanTree || _state == StateHuffmanBlock) {
	if (_state == stateHuffmanTree || _state == stateHuffmanBlock) {
		if (_context.huffman.litLenTree != fixedLitLenTree)
			OFHuffmanTreeFree(_context.huffman.litLenTree);
		if (_context.huffman.distTree != fixedDistTree)
			OFHuffmanTreeFree(_context.huffman.distTree);
	}

	[super dealloc];
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
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







-
+

















-
+





-
-
+
+





-
+












-
+

















-
+











-
+







		@throw [OFNotOpenException exceptionWithObject: self];

	if (_atEndOfStream)
		return 0;

start:
	switch ((enum State)_state) {
	case StateBlockHeader:
	case stateBlockHeader:
		if OF_UNLIKELY (_inLastBlock) {
			[_stream unreadFromBuffer: _buffer + _bufferIndex
					   length: _bufferLength -
						   _bufferIndex];
			_bufferIndex = _bufferLength = 0;

			_atEndOfStream = true;
			return bytesWritten;
		}

		if OF_UNLIKELY (!tryReadBits(self, &bits, 3))
			return bytesWritten;

		_inLastBlock = (bits & 1);

		switch (bits >> 1) {
		case 0: /* No compression */
			_state = StateUncompressedBlockHeader;
			_state = stateUncompressedBlockHeader;
			_bitIndex = 8;
			_context.uncompressedHeader.position = 0;
			memset(_context.uncompressedHeader.length, 0, 4);
			break;
		case 1: /* Fixed Huffman */
			_state = StateHuffmanBlock;
			_context.huffman.state = HuffmanStateAwaitCode;
			_state = stateHuffmanBlock;
			_context.huffman.state = huffmanStateAwaitCode;
			_context.huffman.litLenTree = fixedLitLenTree;
			_context.huffman.distTree = fixedDistTree;
			_context.huffman.treeIter = fixedLitLenTree;
			break;
		case 2: /* Dynamic Huffman */
			_state = StateHuffmanTree;
			_state = stateHuffmanTree;
			_context.huffmanTree.lengths = NULL;
			_context.huffmanTree.receivedCount = 0;
			_context.huffmanTree.value = 0xFE;
			_context.huffmanTree.litLenCodesCount = 0xFF;
			_context.huffmanTree.distCodesCount = 0xFF;
			_context.huffmanTree.codeLenCodesCount = 0xFF;
			break;
		default:
			@throw [OFInvalidFormatException exception];
		}

		goto start;
	case StateUncompressedBlockHeader:
	case stateUncompressedBlockHeader:
#define CTX _context.uncompressedHeader
		/* FIXME: This can be done more efficiently than unreading */
		[_stream unreadFromBuffer: _buffer + _bufferIndex
				   length: _bufferLength - _bufferIndex];
		_bufferIndex = _bufferLength = 0;

		CTX.position += [_stream
		    readIntoBuffer: CTX.length + CTX.position
			    length: 4 - CTX.position];

		if OF_UNLIKELY (CTX.position < 4)
			return bytesWritten;

		if OF_UNLIKELY ((CTX.length[0] | (CTX.length[1] << 8)) !=
		    (uint16_t)~(CTX.length[2] | (CTX.length[3] << 8)))
			@throw [OFInvalidFormatException exception];

		_state = StateUncompressedBlock;
		_state = stateUncompressedBlock;

		/*
		 * Do not reorder! _context.uncompressed.position and
		 * _context.uncompressedHeader.length overlap!
		 */
		_context.uncompressed.length =
		    CTX.length[0] | (CTX.length[1] << 8);
		_context.uncompressed.position = 0;

		goto start;
#undef CTX
	case StateUncompressedBlock:
	case stateUncompressedBlock:
#define CTX _context.uncompressed
		if OF_UNLIKELY (length == 0)
			return bytesWritten;

		tmp = (length < (size_t)CTX.length - CTX.position
		    ? (uint16_t)length : CTX.length - CTX.position);

339
340
341
342
343
344
345
346

347
348
349
350

351
352
353
354
355
356
357
339
340
341
342
343
344
345

346
347
348
349

350
351
352
353
354
355
356
357







-
+



-
+







		_slidingWindowIndex = slidingWindowIndex;

		length -= tmp;
		bytesWritten += tmp;

		CTX.position += tmp;
		if OF_UNLIKELY (CTX.position == CTX.length)
			_state = StateBlockHeader;
			_state = stateBlockHeader;

		goto start;
#undef CTX
	case StateHuffmanTree:
	case stateHuffmanTree:
#define CTX _context.huffmanTree
		if OF_LIKELY (CTX.value == 0xFE) {
			if OF_LIKELY (CTX.litLenCodesCount == 0xFF) {
				if OF_UNLIKELY (!tryReadBits(self, &bits, 5))
					return bytesWritten;

				if OF_UNLIKELY (bits > 29)
485
486
487
488
489
490
491
492
493


494
495
496
497
498

499
500
501
502
503

504
505
506
507
508
509
510
511
512
513
514
515

516
517
518
519
520

521
522
523
524
525
526
527

528
529
530
531
532

533
534
535
536
537
538
539
540
541
542
543
544
545
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
577
485
486
487
488
489
490
491


492
493
494
495
496
497

498
499
500
501
502

503
504
505
506
507
508
509
510
511
512
513
514

515
516
517
518
519

520
521
522
523
524
525
526

527
528
529
530
531

532
533
534
535
536
537
538
539
540
541
542
543
544
545
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
577







-
-
+
+




-
+




-
+











-
+




-
+






-
+




-
+














-
+









-
+

-
+






-
+



-
+







		OFFreeMemory(CTX.lengths);

		/*
		 * litLenTree and distTree are at the same location in
		 * _context.huffman and _context.huffmanTree, thus no need to
		 * set them.
		 */
		_state = StateHuffmanBlock;
		_context.huffman.state = HuffmanStateAwaitCode;
		_state = stateHuffmanBlock;
		_context.huffman.state = huffmanStateAwaitCode;
		_context.huffman.treeIter = CTX.litLenTree;

		goto start;
#undef CTX
	case StateHuffmanBlock:
	case stateHuffmanBlock:
#define CTX _context.huffman
		for (;;) {
			uint8_t extraBits, lengthCodeIndex;

			if OF_UNLIKELY (CTX.state == HuffmanStateWriteValue) {
			if OF_UNLIKELY (CTX.state == huffmanStateWriteValue) {
				if OF_UNLIKELY (length == 0)
					return bytesWritten;

				buffer[bytesWritten++] = CTX.value;
				length--;

				_slidingWindow[_slidingWindowIndex] = CTX.value;
				_slidingWindowIndex =
				    (_slidingWindowIndex + 1) &
				    _slidingWindowMask;

				CTX.state = HuffmanStateAwaitCode;
				CTX.state = huffmanStateAwaitCode;
				CTX.treeIter = CTX.litLenTree;
			}

			if OF_UNLIKELY (CTX.state ==
			    HuffmanStateAwaitLengthExtraBits) {
			    huffmanStateAwaitLengthExtraBits) {
				if OF_UNLIKELY (!tryReadBits(self, &bits,
				    CTX.extraBits))
					return bytesWritten;

				CTX.length += bits;

				CTX.state = HuffmanStateAwaitDistance;
				CTX.state = huffmanStateAwaitDistance;
				CTX.treeIter = CTX.distTree;
			}

			/* Distance of length distance pair */
			if (CTX.state == HuffmanStateAwaitDistance) {
			if (CTX.state == huffmanStateAwaitDistance) {
				if OF_UNLIKELY (!OFHuffmanTreeWalk(self,
				    tryReadBits, &CTX.treeIter, &value))
					return bytesWritten;

				if OF_UNLIKELY (value >= numDistanceCodes)
					@throw [OFInvalidFormatException
					    exception];

				CTX.distance = distanceCodes[value];
				extraBits = distanceExtraBits[value];

				if (extraBits > 0) {
					if OF_UNLIKELY (!tryReadBits(self,
					    &bits, extraBits)) {
#define HSADEB HuffmanStateAwaitDistanceExtraBits
#define HSADEB huffmanStateAwaitDistanceExtraBits
						CTX.state = HSADEB;
#undef HSADEB
						CTX.extraBits = extraBits;
						return bytesWritten;
					}

					CTX.distance += bits;
				}

				CTX.state = HuffmanStateProcessPair;
				CTX.state = huffmanStateProcessPair;
			} else if (CTX.state ==
			    HuffmanStateAwaitDistanceExtraBits) {
			    huffmanStateAwaitDistanceExtraBits) {
				if OF_UNLIKELY (!tryReadBits(self, &bits,
				    CTX.extraBits))
					return bytesWritten;

				CTX.distance += bits;

				CTX.state = HuffmanStateProcessPair;
				CTX.state = huffmanStateProcessPair;
			}

			/* Length distance pair */
			if (CTX.state == HuffmanStateProcessPair) {
			if (CTX.state == huffmanStateProcessPair) {
				for (uint_fast16_t j = 0; j < CTX.length; j++) {
					uint16_t idx;

					if OF_UNLIKELY (length == 0) {
						CTX.length -= j;
						return bytesWritten;
					}
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
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







-
+














-
+






-
+







					_slidingWindow[_slidingWindowIndex] =
					    value;
					_slidingWindowIndex =
					    (_slidingWindowIndex + 1) &
					    _slidingWindowMask;
				}

				CTX.state = HuffmanStateAwaitCode;
				CTX.state = huffmanStateAwaitCode;
				CTX.treeIter = CTX.litLenTree;
			}

			if OF_UNLIKELY (!OFHuffmanTreeWalk(self, tryReadBits,
			    &CTX.treeIter, &value))
				return bytesWritten;

			/* End of block */
			if OF_UNLIKELY (value == 256) {
				if (CTX.litLenTree != fixedLitLenTree)
					OFHuffmanTreeFree(CTX.litLenTree);
				if (CTX.distTree != fixedDistTree)
					OFHuffmanTreeFree(CTX.distTree);

				_state = StateBlockHeader;
				_state = stateBlockHeader;
				goto start;
			}

			/* Literal byte */
			if OF_LIKELY (value < 256) {
				if OF_UNLIKELY (length == 0) {
					CTX.state = HuffmanStateWriteValue;
					CTX.state = huffmanStateWriteValue;
					CTX.value = value;
					return bytesWritten;
				}

				buffer[bytesWritten++] = value;
				length--;

638
639
640
641
642
643
644
645

646
647
648
649
650
651
652
653

654
655
656
657
658
659
660
638
639
640
641
642
643
644

645
646
647
648
649
650
651
652

653
654
655
656
657
658
659
660







-
+







-
+







			extraBits = lengthExtraBits[lengthCodeIndex];

			if (extraBits > 0) {
				if OF_UNLIKELY (!tryReadBits(self, &bits,
				    extraBits)) {
					CTX.extraBits = extraBits;
					CTX.state =
					    HuffmanStateAwaitLengthExtraBits;
					    huffmanStateAwaitLengthExtraBits;
					return bytesWritten;
				}

				CTX.length += bits;
			}

			CTX.treeIter = CTX.distTree;
			CTX.state = HuffmanStateAwaitDistance;
			CTX.state = huffmanStateAwaitDistance;
		}

		break;
#undef CTX
	}

	OF_UNREACHABLE

Modified src/OFLHADecompressingStream.m from [c9d5ecbee3] to [73678fe27b].

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







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







#import "OFKernelEventObserver.h"

#import "OFHuffmanTree.h"

#import "OFInvalidFormatException.h"
#import "OFNotOpenException.h"

enum state {
	StateBlockHeader,
	StateCodeLenCodesCount,
	StateCodeLenTree,
	StateCodeLenTreeSingle,
	StateLitLenCodesCount,
	StateLitLenTree,
	StateLitLenTreeSingle,
	StateDistCodesCount,
	StateDistTree,
	StateDistTreeSingle,
	StateBlockLitLen,
	StateBlockDistLength,
	StateBlockDistLengthExtra,
	StateBlockLenDistPair
enum State {
	stateBlockHeader,
	stateCodeLenCodesCount,
	stateCodeLenTree,
	stateCodeLenTreeSingle,
	stateLitLenCodesCount,
	stateLitLenTree,
	stateLitLenTreeSingle,
	stateDistCodesCount,
	stateDistTree,
	stateDistTreeSingle,
	stateBlockLitLen,
	stateBlockDistLength,
	stateBlockDistLengthExtra,
	stateBlockLenDistPair
};

@implementation OFLHADecompressingStream
@synthesize bytesConsumed = _bytesConsumed;

static OF_INLINE bool
tryReadBits(OFLHADecompressingStream *stream, uint16_t *bits, uint8_t count)
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
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







-
+



-
-
+
+





-
+

-
+







-
+








-
+

-
+







	uint16_t bits = 0, value = 0;
	size_t bytesWritten = 0;

	if (_stream == nil)
		@throw [OFNotOpenException exceptionWithObject: self];

	if (_stream.atEndOfStream && _bufferLength - _bufferIndex == 0 &&
	    _state == StateBlockHeader)
	    _state == stateBlockHeader)
		return 0;

start:
	switch ((enum state)_state) {
	case StateBlockHeader:
	switch ((enum State)_state) {
	case stateBlockHeader:
		if OF_UNLIKELY (!tryReadBits(self, &bits, 16))
			return bytesWritten;

		_symbolsLeft = bits;

		_state = StateCodeLenCodesCount;
		_state = stateCodeLenCodesCount;
		goto start;
	case StateCodeLenCodesCount:
	case stateCodeLenCodesCount:
		if OF_UNLIKELY (!tryReadBits(self, &bits, 5))
			return bytesWritten;

		if OF_UNLIKELY (bits > 20)
			@throw [OFInvalidFormatException exception];

		if OF_UNLIKELY (bits == 0) {
			_state = StateCodeLenTreeSingle;
			_state = stateCodeLenTreeSingle;
			goto start;
		}

		_codesCount = bits;
		_codesReceived = 0;
		_codesLengths = OFAllocZeroedMemory(bits, 1);
		_skip = true;

		_state = StateCodeLenTree;
		_state = stateCodeLenTree;
		goto start;
	case StateCodeLenTree:
	case stateCodeLenTree:
		while (_codesReceived < _codesCount) {
			if OF_UNLIKELY (_currentIsExtendedLength) {
				if OF_UNLIKELY (!tryReadBits(self, &bits, 1))
					return bytesWritten;

				if OF_UNLIKELY (bits == 0) {
					_codesReceived++;
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
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







-
+

-
+





-
+

-
+










-
+









-
+

-
+







				_codesReceived++;
		}

		_codeLenTree = OFHuffmanTreeNew(_codesLengths, _codesCount);
		OFFreeMemory(_codesLengths);
		_codesLengths = NULL;

		_state = StateLitLenCodesCount;
		_state = stateLitLenCodesCount;
		goto start;
	case StateCodeLenTreeSingle:
	case stateCodeLenTreeSingle:
		if OF_UNLIKELY (!tryReadBits(self, &bits, 5))
			return bytesWritten;

		_codeLenTree = OFHuffmanTreeNewSingle(bits);

		_state = StateLitLenCodesCount;
		_state = stateLitLenCodesCount;
		goto start;
	case StateLitLenCodesCount:
	case stateLitLenCodesCount:
		if OF_UNLIKELY (!tryReadBits(self, &bits, 9))
			return bytesWritten;

		if OF_UNLIKELY (bits > 510)
			@throw [OFInvalidFormatException exception];

		if OF_UNLIKELY (bits == 0) {
			OFHuffmanTreeFree(_codeLenTree);
			_codeLenTree = NULL;

			_state = StateLitLenTreeSingle;
			_state = stateLitLenTreeSingle;
			goto start;
		}

		_codesCount = bits;
		_codesReceived = 0;
		_codesLengths = OFAllocZeroedMemory(bits, 1);
		_skip = false;

		_treeIter = _codeLenTree;
		_state = StateLitLenTree;
		_state = stateLitLenTree;
		goto start;
	case StateLitLenTree:
	case stateLitLenTree:
		while (_codesReceived < _codesCount) {
			if OF_UNLIKELY (_skip) {
				uint16_t skipCount;

				switch (_codesLengths[_codesReceived]) {
				case 0:
					skipCount = 1;
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
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







-
+

-
+





-
+

-
+







-
+








-
+

-
+







		_litLenTree = OFHuffmanTreeNew(_codesLengths, _codesCount);
		OFFreeMemory(_codesLengths);
		_codesLengths = NULL;

		OFHuffmanTreeFree(_codeLenTree);
		_codeLenTree = NULL;

		_state = StateDistCodesCount;
		_state = stateDistCodesCount;
		goto start;
	case StateLitLenTreeSingle:
	case stateLitLenTreeSingle:
		if OF_UNLIKELY (!tryReadBits(self, &bits, 9))
			return bytesWritten;

		_litLenTree = OFHuffmanTreeNewSingle(bits);

		_state = StateDistCodesCount;
		_state = stateDistCodesCount;
		goto start;
	case StateDistCodesCount:
	case stateDistCodesCount:
		if OF_UNLIKELY (!tryReadBits(self, &bits, _distanceBits))
			return bytesWritten;

		if OF_UNLIKELY (bits > _dictionaryBits)
			@throw [OFInvalidFormatException exception];

		if OF_UNLIKELY (bits == 0) {
			_state = StateDistTreeSingle;
			_state = stateDistTreeSingle;
			goto start;
		}

		_codesCount = bits;
		_codesReceived = 0;
		_codesLengths = OFAllocZeroedMemory(bits, 1);

		_treeIter = _codeLenTree;
		_state = StateDistTree;
		_state = stateDistTree;
		goto start;
	case StateDistTree:
	case stateDistTree:
		while (_codesReceived < _codesCount) {
			if OF_UNLIKELY (_currentIsExtendedLength) {
				if OF_UNLIKELY (!tryReadBits(self, &bits, 1))
					return bytesWritten;

				if OF_UNLIKELY (bits == 0) {
					_codesReceived++;
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
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







-
+

-
+






-
+

-
+





-
+







		}

		_distTree = OFHuffmanTreeNew(_codesLengths, _codesCount);
		OFFreeMemory(_codesLengths);
		_codesLengths = NULL;

		_treeIter = _litLenTree;
		_state = StateBlockLitLen;
		_state = stateBlockLitLen;
		goto start;
	case StateDistTreeSingle:
	case stateDistTreeSingle:
		if OF_UNLIKELY (!tryReadBits(self, &bits, _distanceBits))
			return bytesWritten;

		_distTree = OFHuffmanTreeNewSingle(bits);

		_treeIter = _litLenTree;
		_state = StateBlockLitLen;
		_state = stateBlockLitLen;
		goto start;
	case StateBlockLitLen:
	case stateBlockLitLen:
		if OF_UNLIKELY (_symbolsLeft == 0) {
			OFHuffmanTreeFree(_litLenTree);
			OFHuffmanTreeFree(_distTree);
			_litLenTree = _distTree = NULL;

			_state = StateBlockHeader;
			_state = stateBlockHeader;

			/*
			 * We must return here, as there is no indication
			 * whether this was the last block. Whoever called this
			 * method needs to check if everything has been read
			 * already and only call read again if that is not the
			 * case.
437
438
439
440
441
442
443
444

445
446
447
448

449
450
451
452
453
454
455
456

457
458

459
460
461
462
463
464

465
466

467
468
469
470
471
472
473
437
438
439
440
441
442
443

444
445
446
447

448
449
450
451
452
453
454
455

456
457

458
459
460
461
462
463

464
465

466
467
468
469
470
471
472
473







-
+



-
+







-
+

-
+





-
+

-
+







			    _slidingWindowMask;

			_symbolsLeft--;
			_treeIter = _litLenTree;
		} else {
			_length = value - 253;
			_treeIter = _distTree;
			_state = StateBlockDistLength;
			_state = stateBlockDistLength;
		}

		goto start;
	case StateBlockDistLength:
	case stateBlockDistLength:
		if OF_UNLIKELY (!OFHuffmanTreeWalk(self, tryReadBits,
		    &_treeIter, &value))
			return bytesWritten;

		_distance = value;

		_state = (value < 2
		    ? StateBlockLenDistPair : StateBlockDistLengthExtra);
		    ? stateBlockLenDistPair : stateBlockDistLengthExtra);
		goto start;
	case StateBlockDistLengthExtra:
	case stateBlockDistLengthExtra:
		if OF_UNLIKELY (!tryReadBits(self, &bits, _distance - 1))
			return bytesWritten;

		_distance = bits + (1u << (_distance - 1));

		_state = StateBlockLenDistPair;
		_state = stateBlockLenDistPair;
		goto start;
	case StateBlockLenDistPair:
	case stateBlockLenDistPair:
		for (uint_fast16_t i = 0; i < _length; i++) {
			uint32_t idx;

			if OF_UNLIKELY (length == 0) {
				_length -= i;
				return bytesWritten;
			}
483
484
485
486
487
488
489
490

491
492
493
494
495
496
497
498
499
500
501
502
503

504
505
506
507
508
509
510
483
484
485
486
487
488
489

490
491
492
493
494
495
496
497
498
499
500
501
502

503
504
505
506
507
508
509
510







-
+












-
+







			_slidingWindowIndex = (_slidingWindowIndex + 1) &
			    _slidingWindowMask;
		}

		_symbolsLeft--;

		_treeIter = _litLenTree;
		_state = StateBlockLitLen;
		_state = stateBlockLitLen;
		goto start;
	}

	OF_UNREACHABLE
}

- (bool)lowlevelIsAtEndOfStream
{
	if (_stream == nil)
		@throw [OFNotOpenException exceptionWithObject: self];

	return (_stream.atEndOfStream &&
	    _bufferLength - _bufferIndex == 0 && _state == StateBlockHeader);
	    _bufferLength - _bufferIndex == 0 && _state == stateBlockHeader);
}

- (int)fileDescriptorForReading
{
	return ((id <OFReadyForReadingObserving>)_stream)
	    .fileDescriptorForReading;
}

Modified src/OFMutableUTF8String.m from [0d95785aef] to [8129fb7234].

77
78
79
80
81
82
83
84

85
86
87
88
89
90
91
77
78
79
80
81
82
83

84
85
86
87
88
89
90
91







-
+








	if (!_s->isUTF8) {
		uint8_t t;
		const OFUnichar *const *table;

		assert(startTableSize >= 1 && middleTableSize >= 1);

		_s->hashed = false;
		_s->hasHash = false;

		for (i = 0; i < _s->cStringLength; i++) {
			if (isStart)
				table = startTable;
			else
				table = middleTable;

174
175
176
177
178
179
180
181

182
183
184
185
186
187
188
174
175
176
177
178
179
180

181
182
183
184
185
186
187
188







-
+







	}

	assert(j == newCStringLength);
	newCString[j] = 0;
	OFFreeMemory(unicodeString);

	OFFreeMemory(_s->cString);
	_s->hashed = false;
	_s->hasHash = false;
	_s->cString = newCString;
	_s->cStringLength = newCStringLength;

	/*
	 * Even though cStringLength can change, length cannot, therefore no
	 * need to change it.
	 */
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
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







-
+











-
+







		    _s->cStringLength);

	if (idx >= _s->cStringLength)
		@throw [OFOutOfRangeException exception];

	/* Shortcut if old and new character both are ASCII */
	if (character < 0x80 && !(_s->cString[idx] & 0x80)) {
		_s->hashed = false;
		_s->hasHash = false;
		_s->cString[idx] = character;
		return;
	}

	if ((lenNew = OFUTF8StringEncode(character, buffer)) == 0)
		@throw [OFInvalidEncodingException exception];

	if ((lenOld = OFUTF8StringDecode(_s->cString + idx,
	    _s->cStringLength - idx, &c)) <= 0)
		@throw [OFInvalidEncodingException exception];

	_s->hashed = false;
	_s->hasHash = false;

	if (lenNew == (size_t)lenOld)
		memcpy(_s->cString + idx, buffer, lenNew);
	else if (lenNew > (size_t)lenOld) {
		_s->cString = OFResizeMemory(_s->cString,
		    _s->cStringLength - lenOld + lenNew + 1, 1);

271
272
273
274
275
276
277
278

279
280
281
282
283
284
285
271
272
273
274
275
276
277

278
279
280
281
282
283
284
285







-
+







	case 1:
		_s->isUTF8 = true;
		break;
	case -1:
		@throw [OFInvalidEncodingException exception];
	}

	_s->hashed = false;
	_s->hasHash = false;
	_s->cString = OFResizeMemory(_s->cString,
	    _s->cStringLength + UTF8StringLength + 1, 1);
	memcpy(_s->cString + _s->cStringLength, UTF8String,
	    UTF8StringLength + 1);

	_s->cStringLength += UTF8StringLength;
	_s->length += length;
300
301
302
303
304
305
306
307

308
309
310
311
312
313
314
300
301
302
303
304
305
306

307
308
309
310
311
312
313
314







-
+







	case 1:
		_s->isUTF8 = true;
		break;
	case -1:
		@throw [OFInvalidEncodingException exception];
	}

	_s->hashed = false;
	_s->hasHash = false;
	_s->cString = OFResizeMemory(_s->cString,
	    _s->cStringLength + UTF8StringLength + 1, 1);
	memcpy(_s->cString + _s->cStringLength, UTF8String, UTF8StringLength);

	_s->cStringLength += UTF8StringLength;
	_s->length += length;

346
347
348
349
350
351
352
353

354
355
356
357
358
359
360
346
347
348
349
350
351
352

353
354
355
356
357
358
359
360







-
+







	size_t UTF8StringLength;

	if (string == nil)
		@throw [OFInvalidArgumentException exception];

	UTF8StringLength = string.UTF8StringLength;

	_s->hashed = false;
	_s->hasHash = false;
	_s->cString = OFResizeMemory(_s->cString,
	    _s->cStringLength + UTF8StringLength + 1, 1);
	memcpy(_s->cString + _s->cStringLength, string.UTF8String,
	    UTF8StringLength);

	_s->cStringLength += UTF8StringLength;
	_s->length += string.length;
387
388
389
390
391
392
393
394

395
396
397
398
399
400
401
387
388
389
390
391
392
393

394
395
396
397
398
399
400
401







-
+







				isUTF8 = true;

			j += len;
		}

		tmp[j] = '\0';

		_s->hashed = false;
		_s->hasHash = false;
		_s->cString = OFResizeMemory(_s->cString,
		    _s->cStringLength + j + 1, 1);
		memcpy(_s->cString + _s->cStringLength, tmp, j + 1);

		_s->cStringLength += j;
		_s->length += length;

425
426
427
428
429
430
431
432

433
434
435
436
437
438
439
425
426
427
428
429
430
431

432
433
434
435
436
437
438
439







-
+







	}
}

- (void)reverse
{
	size_t i, j;

	_s->hashed = false;
	_s->hasHash = false;

	/* We reverse all bytes and restore UTF-8 later, if necessary */
	for (i = 0, j = _s->cStringLength - 1; i < _s->cStringLength / 2;
	    i++, j--) {
		_s->cString[i] ^= _s->cString[j];
		_s->cString[j] ^= _s->cString[i];
		_s->cString[i] ^= _s->cString[j];
513
514
515
516
517
518
519
520

521
522
523
524
525
526
527
513
514
515
516
517
518
519

520
521
522
523
524
525
526
527







-
+







		@throw [OFOutOfRangeException exception];

	if (_s->isUTF8)
		idx = OFUTF8StringIndexToPosition(_s->cString, idx,
		    _s->cStringLength);

	newCStringLength = _s->cStringLength + string.UTF8StringLength;
	_s->hashed = false;
	_s->hasHash = false;
	_s->cString = OFResizeMemory(_s->cString, newCStringLength + 1, 1);

	memmove(_s->cString + idx + string.UTF8StringLength,
	    _s->cString + idx, _s->cStringLength - idx);
	memcpy(_s->cString + idx, string.UTF8String,
	    string.UTF8StringLength);
	_s->cString[newCStringLength] = '\0';
550
551
552
553
554
555
556
557

558
559
560
561
562
563
564
550
551
552
553
554
555
556

557
558
559
560
561
562
563
564







-
+







		    _s->cStringLength);
		end = OFUTF8StringIndexToPosition(_s->cString, end,
		    _s->cStringLength);
	}

	memmove(_s->cString + start, _s->cString + end,
	    _s->cStringLength - end);
	_s->hashed = false;
	_s->hasHash = false;
	_s->length -= range.length;
	_s->cStringLength -= end - start;
	_s->cString[_s->cStringLength] = 0;

	@try {
		_s->cString = OFResizeMemory(_s->cString, _s->cStringLength + 1,
		    1);
587
588
589
590
591
592
593
594

595
596
597
598
599
600
601
587
588
589
590
591
592
593

594
595
596
597
598
599
600
601







-
+







		    _s->cStringLength);
		end = OFUTF8StringIndexToPosition(_s->cString, end,
		    _s->cStringLength);
	}

	newCStringLength = _s->cStringLength - (end - start) +
	    replacement.UTF8StringLength;
	_s->hashed = false;
	_s->hasHash = false;

	/*
	 * If the new string is bigger, we need to resize it first so we can
	 * memmove() the rest of the string to the end.
	 *
	 * We must not resize the string if the new string is smaller, because
	 * then we can't memmove() the rest of the string forward as the rest is
698
699
700
701
702
703
704
705

706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726

727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746

747
748
749
750
751
752
753
698
699
700
701
702
703
704

705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725

726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745

746
747
748
749
750
751
752
753







-
+




















-
+



















-
+







	}
	memcpy(newCString + newCStringLength, _s->cString + last,
	    _s->cStringLength - last);
	newCStringLength += _s->cStringLength - last;
	newCString[newCStringLength] = 0;

	OFFreeMemory(_s->cString);
	_s->hashed = false;
	_s->hasHash = false;
	_s->cString = newCString;
	_s->cStringLength = newCStringLength;
	_s->length = newLength;

	if ([replacement isKindOfClass: [OFUTF8String class]] ||
	    [replacement isKindOfClass: [OFMutableUTF8String class]]) {
		if (((OFMutableUTF8String *)replacement)->_s->isUTF8)
			_s->isUTF8 = true;
	} else
		_s->isUTF8 = true;
}

- (void)deleteLeadingWhitespaces
{
	size_t i;

	for (i = 0; i < _s->cStringLength; i++)
		if (!OFASCIIIsSpace(_s->cString[i]))
			break;

	_s->hashed = false;
	_s->hasHash = false;
	_s->cStringLength -= i;
	_s->length -= i;

	memmove(_s->cString, _s->cString + i, _s->cStringLength);
	_s->cString[_s->cStringLength] = '\0';

	@try {
		_s->cString = OFResizeMemory(_s->cString, _s->cStringLength + 1,
		    1);
	} @catch (OFOutOfMemoryException *e) {
		/* We don't really care, as we only made it smaller */
	}
}

- (void)deleteTrailingWhitespaces
{
	size_t d;
	char *p;

	_s->hashed = false;
	_s->hasHash = false;

	d = 0;
	for (p = _s->cString + _s->cStringLength - 1; p >= _s->cString; p--) {
		if (!OFASCIIIsSpace(*p))
			break;

		*p = '\0';
766
767
768
769
770
771
772
773

774
775
776
777
778
779
780
766
767
768
769
770
771
772

773
774
775
776
777
778
779
780







-
+







}

- (void)deleteEnclosingWhitespaces
{
	size_t d, i;
	char *p;

	_s->hashed = false;
	_s->hasHash = false;

	d = 0;
	for (p = _s->cString + _s->cStringLength - 1; p >= _s->cString; p--) {
		if (!OFASCIIIsSpace(*p))
			break;

		*p = '\0';

Modified src/OFNumber.m from [4fabc7778d] to [34d1acfa38].

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56










57
58
59
60
61
62
63
40
41
42
43
44
45
46










47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63







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







@end

@interface OFNumberSingleton: OFNumber
@end

#ifdef OF_OBJFW_RUNTIME
enum Tag {
	TagChar,
	TagShort,
	TagInt,
	TagLong,
	TagLongLong,
	TagUnsignedChar,
	TagUnsignedShort,
	TagUnsignedInt,
	TagUnsignedLong,
	TagUnsignedLongLong,
	tagChar,
	tagShort,
	tagInt,
	tagLong,
	tagLongLong,
	tagUnsignedChar,
	tagUnsignedShort,
	tagUnsignedInt,
	tagUnsignedLong,
	tagUnsignedLongLong,
};
static const uint_fast8_t tagBits = 4;
static const uintptr_t tagMask = 0xF;

@interface OFTaggedPointerNumber: OFNumberSingleton
@end
#endif
170
171
172
173
174
175
176
177

178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196

197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215

216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234

235
236
237
238
239
240
241
242
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
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
170
171
172
173
174
175
176

177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195

196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214

215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233

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







-
+


















-
+


















-
+


















-
+



















-
+


















-
+


















-
+


















-
+


















-
+


















-
+







	if (value == 0) {
		static OFOnceControl onceControl = OFOnceControlInitValue;
		OFOnce(&onceControl, charZeroNumberInit);
		return (id)charZeroNumber;
#ifdef OF_OBJFW_RUNTIME
	} else if ((unsigned char)value <= (UINTPTR_MAX >> tagBits)) {
		id ret = objc_createTaggedPointer(numberTag,
		    ((uintptr_t)(unsigned char)value << tagBits) | TagChar);
		    ((uintptr_t)(unsigned char)value << tagBits) | tagChar);

		if (ret != nil)
			return ret;
#endif
	}

	return (id)[[OFNumber of_alloc] initWithChar: value];
}

- (instancetype)initWithShort: (short)value
{
	if (value == 0) {
		static OFOnceControl onceControl = OFOnceControlInitValue;
		OFOnce(&onceControl, shortZeroNumberInit);
		return (id)shortZeroNumber;
#ifdef OF_OBJFW_RUNTIME
	} else if ((unsigned short)value <= (UINTPTR_MAX >> tagBits)) {
		id ret = objc_createTaggedPointer(numberTag,
		    ((uintptr_t)(unsigned short)value << tagBits) | TagShort);
		    ((uintptr_t)(unsigned short)value << tagBits) | tagShort);

		if (ret != nil)
			return ret;
#endif
	}

	return (id)[[OFNumber of_alloc] initWithShort: value];
}

- (instancetype)initWithInt: (int)value
{
	if (value == 0) {
		static OFOnceControl onceControl = OFOnceControlInitValue;
		OFOnce(&onceControl, intZeroNumberInit);
		return (id)intZeroNumber;
#ifdef OF_OBJFW_RUNTIME
	} else if ((unsigned int)value <= (UINTPTR_MAX >> tagBits)) {
		id ret = objc_createTaggedPointer(numberTag,
		    ((uintptr_t)(unsigned int)value << tagBits) | TagInt);
		    ((uintptr_t)(unsigned int)value << tagBits) | tagInt);

		if (ret != nil)
			return ret;
#endif
	}

	return (id)[[OFNumber of_alloc] initWithInt: value];
}

- (instancetype)initWithLong: (long)value
{
	if (value == 0) {
		static OFOnceControl onceControl = OFOnceControlInitValue;
		OFOnce(&onceControl, longZeroNumberInit);
		return (id)longZeroNumber;
#ifdef OF_OBJFW_RUNTIME
	} else if ((unsigned long)value <= (UINTPTR_MAX >> tagBits)) {
		id ret = objc_createTaggedPointer(numberTag,
		    ((uintptr_t)(unsigned long)value << tagBits) | TagLong);
		    ((uintptr_t)(unsigned long)value << tagBits) | tagLong);

		if (ret != nil)
			return ret;
#endif
	}

	return (id)[[OFNumber of_alloc] initWithLong: value];
}

- (instancetype)initWithLongLong: (long long)value
{
	if (value == 0) {
		static OFOnceControl onceControl = OFOnceControlInitValue;
		OFOnce(&onceControl, longLongZeroNumberInit);
		return (id)longLongZeroNumber;
#ifdef OF_OBJFW_RUNTIME
	} else if ((unsigned long long)value <= (UINTPTR_MAX >> tagBits)) {
		id ret = objc_createTaggedPointer(numberTag,
		    ((uintptr_t)(unsigned long long)value << tagBits) |
		    TagLongLong);
		    tagLongLong);

		if (ret != nil)
			return ret;
#endif
	}

	return (id)[[OFNumber of_alloc] initWithLongLong: value];
}

- (instancetype)initWithUnsignedChar: (unsigned char)value
{
	if (value == 0) {
		static OFOnceControl onceControl = OFOnceControlInitValue;
		OFOnce(&onceControl, unsignedCharZeroNumberInit);
		return (id)unsignedCharZeroNumber;
#ifdef OF_OBJFW_RUNTIME
	} else if (value <= (UINTPTR_MAX >> tagBits)) {
		id ret = objc_createTaggedPointer(numberTag,
		    ((uintptr_t)value << tagBits) | TagUnsignedChar);
		    ((uintptr_t)value << tagBits) | tagUnsignedChar);

		if (ret != nil)
			return ret;
#endif
	}

	return (id)[[OFNumber of_alloc] initWithUnsignedChar: value];
}

- (instancetype)initWithUnsignedShort: (unsigned short)value
{
	if (value == 0) {
		static OFOnceControl onceControl = OFOnceControlInitValue;
		OFOnce(&onceControl, unsignedShortZeroNumberInit);
		return (id)unsignedShortZeroNumber;
#ifdef OF_OBJFW_RUNTIME
	} else if (value <= (UINTPTR_MAX >> tagBits)) {
		id ret = objc_createTaggedPointer(numberTag,
		    ((uintptr_t)value << tagBits) | TagUnsignedShort);
		    ((uintptr_t)value << tagBits) | tagUnsignedShort);

		if (ret != nil)
			return ret;
#endif
	}

	return (id)[[OFNumber of_alloc] initWithUnsignedShort: value];
}

- (instancetype)initWithUnsignedInt: (unsigned int)value
{
	if (value == 0) {
		static OFOnceControl onceControl = OFOnceControlInitValue;
		OFOnce(&onceControl, unsignedIntZeroNumberInit);
		return (id)unsignedIntZeroNumber;
#ifdef OF_OBJFW_RUNTIME
	} else if (value <= (UINTPTR_MAX >> tagBits)) {
		id ret = objc_createTaggedPointer(numberTag,
		    ((uintptr_t)value << tagBits) | TagUnsignedInt);
		    ((uintptr_t)value << tagBits) | tagUnsignedInt);

		if (ret != nil)
			return ret;
#endif
	}

	return (id)[[OFNumber of_alloc] initWithUnsignedInt: value];
}

- (instancetype)initWithUnsignedLong: (unsigned long)value
{
	if (value == 0) {
		static OFOnceControl onceControl = OFOnceControlInitValue;
		OFOnce(&onceControl, unsignedLongZeroNumberInit);
		return (id)unsignedLongZeroNumber;
#ifdef OF_OBJFW_RUNTIME
	} else if (value <= (UINTPTR_MAX >> tagBits)) {
		id ret = objc_createTaggedPointer(numberTag,
		    ((uintptr_t)value << tagBits) | TagUnsignedLong);
		    ((uintptr_t)value << tagBits) | tagUnsignedLong);

		if (ret != nil)
			return ret;
#endif
	}

	return (id)[[OFNumber of_alloc] initWithUnsignedLong: value];
}

- (instancetype)initWithUnsignedLongLong: (unsigned long long)value
{
	if (value == 0) {
		static OFOnceControl onceControl = OFOnceControlInitValue;
		OFOnce(&onceControl, unsignedLongLongZeroNumberInit);
		return (id)unsignedLongLongZeroNumber;
#ifdef OF_OBJFW_RUNTIME
	} else if (value <= (UINTPTR_MAX >> tagBits)) {
		id ret = objc_createTaggedPointer(numberTag,
		    ((uintptr_t)value << tagBits) | TagUnsignedLongLong);
		    ((uintptr_t)value << tagBits) | tagUnsignedLongLong);

		if (ret != nil)
			return ret;
#endif
	}

	return (id)[[OFNumber of_alloc] initWithUnsignedLongLong: value];
411
412
413
414
415
416
417
418

419
420

421
422

423
424

425
426

427
428

429
430

431
432

433
434

435
436

437
438
439
440
441
442
443
444
445
446
447

448
449

450
451

452
453

454
455

456
457

458
459

460
461

462
463

464
465

466
467
468
469
470
471
472
411
412
413
414
415
416
417

418
419

420
421

422
423

424
425

426
427

428
429

430
431

432
433

434
435

436
437
438
439
440
441
442
443
444
445
446

447
448

449
450

451
452

453
454

455
456

457
458

459
460

461
462

463
464

465
466
467
468
469
470
471
472







-
+

-
+

-
+

-
+

-
+

-
+

-
+

-
+

-
+

-
+










-
+

-
+

-
+

-
+

-
+

-
+

-
+

-
+

-
+

-
+







#ifdef OF_OBJFW_RUNTIME
@implementation OFTaggedPointerNumber
- (const char *)objCType
{
	uintptr_t value = object_getTaggedPointerValue(self);

	switch (value & tagMask) {
	case TagChar:
	case tagChar:
		return @encode(signed char);
	case TagShort:
	case tagShort:
		return @encode(short);
	case TagInt:
	case tagInt:
		return @encode(int);
	case TagLong:
	case tagLong:
		return @encode(long);
	case TagLongLong:
	case tagLongLong:
		return @encode(long long);
	case TagUnsignedChar:
	case tagUnsignedChar:
		return @encode(unsigned char);
	case TagUnsignedShort:
	case tagUnsignedShort:
		return @encode(unsigned short);
	case TagUnsignedInt:
	case tagUnsignedInt:
		return @encode(unsigned int);
	case TagUnsignedLong:
	case tagUnsignedLong:
		return @encode(unsigned long);
	case TagUnsignedLongLong:
	case tagUnsignedLongLong:
		return @encode(unsigned long long);
	default:
		@throw [OFInvalidArgumentException exception];
	}
}

# define RETURN_VALUE							  \
	uintptr_t value = object_getTaggedPointerValue(self);		  \
									  \
	switch (value & tagMask) {					  \
	case TagChar:							  \
	case tagChar:							  \
		return (signed char)(unsigned char)(value >> tagBits);	  \
	case TagShort:							  \
	case tagShort:							  \
		return (short)(unsigned short)(value >> tagBits);	  \
	case TagInt:							  \
	case tagInt:							  \
		return (int)(unsigned int)(value >> tagBits);		  \
	case TagLong:							  \
	case tagLong:							  \
		return (long)(unsigned long)(value >> tagBits);		  \
	case TagLongLong:						  \
	case tagLongLong:						  \
		return (long long)(unsigned long long)(value >> tagBits); \
	case TagUnsignedChar:						  \
	case tagUnsignedChar:						  \
		return (unsigned char)(value >> tagBits);		  \
	case TagUnsignedShort:						  \
	case tagUnsignedShort:						  \
		return (unsigned short)(value >> tagBits);		  \
	case TagUnsignedInt:						  \
	case tagUnsignedInt:						  \
		return (unsigned int)(value >> tagBits);		  \
	case TagUnsignedLong:						  \
	case tagUnsignedLong:						  \
		return (unsigned long)(value >> tagBits);		  \
	case TagUnsignedLongLong:					  \
	case tagUnsignedLongLong:					  \
		return (unsigned long long)(value >> tagBits);		  \
	default:							  \
		@throw [OFInvalidArgumentException exception];		  \
	}
- (long long)longLongValue
{
	RETURN_VALUE

Modified src/OFStrPTime.m from [c708164613] to [2eebe7783d].

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







-
-
-
+
+
+











-
+

-
+




-
-
+








#import "macros.h"

const char *
OFStrPTime(const char *buffer, const char *format, struct tm *tm, short *tz)
{
	enum {
		StateSearchConversionSpecifier,
		StateInConversionSpecifier
	} state = StateSearchConversionSpecifier;
		stateSearchConversionSpecifier,
		stateInConversionSpecifier
	} state = stateSearchConversionSpecifier;
	size_t j, bufferLen, formatLen;

	bufferLen = strlen(buffer);
	formatLen = strlen(format);

	j = 0;
	for (size_t i = 0; i < formatLen; i++) {
		if (j >= bufferLen)
			return NULL;

		switch (state) {
		case StateSearchConversionSpecifier:
		case stateSearchConversionSpecifier:
			if (format[i] == '%')
				state = StateInConversionSpecifier;
				state = stateInConversionSpecifier;
			else if (format[i] != buffer[j++])
				return NULL;

			break;

		case StateInConversionSpecifier:;
		case stateInConversionSpecifier:;
			int k, maxLen, number = 0;

			switch (format[i]) {
			case 'd':
			case 'e':
			case 'H':
			case 'm':
217
218
219
220
221
222
223
224

225
226
227
228
229
230
231
216
217
218
219
220
221
222

223
224
225
226
227
228
229
230







-
+







				break;
			case 't':
				if (buffer[j++] != '\t')
					return NULL;
				break;
			}

			state = StateSearchConversionSpecifier;
			state = stateSearchConversionSpecifier;

			break;
		}
	}

	return buffer + j;
}

Modified src/OFUTF8String.h from [7a3b643b9d] to [4e86bf2737].

27
28
29
30
31
32
33
34

35
36
37
38
39
40
41
27
28
29
30
31
32
33

34
35
36
37
38
39
40
41







-
+







	 * pointer to `&_storage`.
	 */
	struct OFUTF8StringIvars {
		char          *cString;
		size_t        cStringLength;
		bool          isUTF8;
		size_t        length;
		bool          hashed;
		bool          hasHash;
		unsigned long hash;
		bool          freeWhenDone;
	} *restrict _s;
	struct OFUTF8StringIvars _storage;
}
@end

Modified src/OFUTF8String.m from [d6113071ec] to [18e27cfd00].

787
788
789
790
791
792
793
794

795
796
797
798
799
800
801
787
788
789
790
791
792
793

794
795
796
797
798
799
800
801







-
+








	if (string.UTF8StringLength != _s->cStringLength ||
	    string.length != _s->length)
		return false;

	if (([string isKindOfClass: [OFUTF8String class]] ||
	    [string isKindOfClass: [OFMutableUTF8String class]]) &&
	    _s->hashed && string->_s->hashed && _s->hash != string->_s->hash)
	    _s->hasHash && string->_s->hasHash && _s->hash != string->_s->hash)
		return false;

	if (strcmp(_s->cString, string.UTF8String) != 0)
		return false;

	return true;
}
915
916
917
918
919
920
921
922

923
924
925
926
927
928
929
915
916
917
918
919
920
921

922
923
924
925
926
927
928
929







-
+







	return OFOrderedSame;
}

- (unsigned long)hash
{
	unsigned long hash;

	if (_s->hashed)
	if (_s->hasHash)
		return _s->hash;

	OFHashInit(&hash);

	for (size_t i = 0; i < _s->cStringLength; i++) {
		OFUnichar c;
		ssize_t length;
938
939
940
941
942
943
944
945

946
947
948
949
950
951
952
938
939
940
941
942
943
944

945
946
947
948
949
950
951
952







-
+








		i += length - 1;
	}

	OFHashFinalize(&hash);

	_s->hash = hash;
	_s->hashed = true;
	_s->hasHash = true;

	return hash;
}

- (OFUnichar)characterAtIndex: (size_t)idx
{
	OFUnichar character;

Modified utils/ofhttp/OFHTTP.m from [8591ab818f] to [c136ae289f].

128
129
130
131
132
133
134
135
136
137
138
139
140
141
142








143
144
145
146
147
148
149
150
151
152
153
154
155
156

157
158
159
160
161
162

163
164
165
166
167
168
169
170


171
172
173
174

175
176

177
178
179
180
181
182
183

184
185

186
187
188
189
190

191
192
193
194
195
196

197
198
199

200
201

202
203
204

205
206
207
208
209

210
211
212
213
214
215
216
217
218

219
220
221

222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239

240
241
242
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
128
129
130
131
132
133
134








135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155

156
157
158
159
160
161

162
163
164
165
166
167
168


169
170
171
172
173

174
175

176
177
178
179
180
181
182

183
184

185
186
187
188
189

190
191
192
193
194
195

196
197
198

199
200

201
202
203

204
205
206
207
208

209
210
211
212
213
214
215
216
217

218
219
220

221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238

239
240
241
242

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







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













-
+





-
+






-
-
+
+



-
+

-
+






-
+

-
+




-
+





-
+


-
+

-
+


-
+




-
+








-
+


-
+

















-
+



-
+

-
+









-
+






-
+







static OFString *
fileNameFromContentDisposition(OFString *contentDisposition)
{
	void *pool;
	const char *UTF8String;
	size_t UTF8StringLength;
	enum {
		StateDispositionType,
		StateDispositionTypeSemicolon,
		StateDispositionParamNameSkipSpace,
		StateDispositionParamName,
		StateDispositionParamValue,
		StateDispositionParamQuoted,
		StateDispositionParamUnquoted,
		StateDispositionExpectSemicolon
		stateDispositionType,
		stateDispositionTypeSemicolon,
		stateDispositionParamNameSkipSpace,
		stateDispositionParamName,
		stateDispositionParamValue,
		stateDispositionParamQuoted,
		stateDispositionParamUnquoted,
		stateDispositionExpectSemicolon
	} state;
	size_t last;
	OFString *type = nil, *paramName = nil, *paramValue;
	OFMutableDictionary *params;
	OFString *fileName;

	if (contentDisposition == nil)
		return nil;

	pool = objc_autoreleasePoolPush();

	UTF8String = contentDisposition.UTF8String;
	UTF8StringLength = contentDisposition.UTF8StringLength;
	state = StateDispositionType;
	state = stateDispositionType;
	params = [OFMutableDictionary dictionary];
	last = 0;

	for (size_t i = 0; i < UTF8StringLength; i++) {
		switch (state) {
		case StateDispositionType:
		case stateDispositionType:
			if (UTF8String[i] == ';' || UTF8String[i] == ' ') {
				type = [OFString
				    stringWithUTF8String: UTF8String
						  length: i];

				state = (UTF8String[i] == ';'
				    ? StateDispositionParamNameSkipSpace
				    : StateDispositionTypeSemicolon);
				    ? stateDispositionParamNameSkipSpace
				    : stateDispositionTypeSemicolon);
				last = i + 1;
			}
			break;
		case StateDispositionTypeSemicolon:
		case stateDispositionTypeSemicolon:
			if (UTF8String[i] == ';') {
				state = StateDispositionParamNameSkipSpace;
				state = stateDispositionParamNameSkipSpace;
				last = i + 1;
			} else if (UTF8String[i] != ' ') {
				objc_autoreleasePoolPop(pool);
				return nil;
			}
			break;
		case StateDispositionParamNameSkipSpace:
		case stateDispositionParamNameSkipSpace:
			if (UTF8String[i] != ' ') {
				state = StateDispositionParamName;
				state = stateDispositionParamName;
				last = i;
				i--;
			}
			break;
		case StateDispositionParamName:
		case stateDispositionParamName:
			if (UTF8String[i] == '=') {
				paramName = [OFString
				    stringWithUTF8String: UTF8String + last
						  length: i - last];

				state = StateDispositionParamValue;
				state = stateDispositionParamValue;
			}
			break;
		case StateDispositionParamValue:
		case stateDispositionParamValue:
			if (UTF8String[i] == '"') {
				state = StateDispositionParamQuoted;
				state = stateDispositionParamQuoted;
				last = i + 1;
			} else {
				state = StateDispositionParamUnquoted;
				state = stateDispositionParamUnquoted;
				last = i;
				i--;
			}
			break;
		case StateDispositionParamQuoted:
		case stateDispositionParamQuoted:
			if (UTF8String[i] == '"') {
				paramValue = [OFString
				    stringWithUTF8String: UTF8String + last
						  length: i - last];

				[params setObject: paramValue
					   forKey: paramName.lowercaseString];

				state = StateDispositionExpectSemicolon;
				state = stateDispositionExpectSemicolon;
			}
			break;
		case StateDispositionParamUnquoted:
		case stateDispositionParamUnquoted:
			if (UTF8String[i] <= 31 || UTF8String[i] >= 127)
				return nil;

			switch (UTF8String[i]) {
			case ' ': case '"': case '(': case ')': case ',':
			case '/': case ':': case '<': case '=': case '>':
			case '?': case '@': case '[': case '\\': case ']':
			case '{': case '}':
				return nil;
			case ';':
				paramValue = [OFString
				    stringWithUTF8String: UTF8String + last
						  length: i - last];

				[params setObject: paramValue
					   forKey: paramName.lowercaseString];

				state = StateDispositionParamNameSkipSpace;
				state = stateDispositionParamNameSkipSpace;
				break;
			}
			break;
		case StateDispositionExpectSemicolon:
		case stateDispositionExpectSemicolon:
			if (UTF8String[i] == ';') {
				state = StateDispositionParamNameSkipSpace;
				state = stateDispositionParamNameSkipSpace;
				last = i + 1;
			} else if (UTF8String[i] != ' ') {
				objc_autoreleasePoolPop(pool);
				return nil;
			}
			break;
		}
	}

	if (state == StateDispositionParamUnquoted) {
	if (state == stateDispositionParamUnquoted) {
		paramValue = [OFString
		    stringWithUTF8String: UTF8String + last
				  length: UTF8StringLength - last];

		[params setObject: paramValue
			   forKey: paramName.lowercaseString];
	} else if (state != StateDispositionExpectSemicolon) {
	} else if (state != stateDispositionExpectSemicolon) {
		objc_autoreleasePoolPop(pool);
		return nil;
	}

	if (![type isEqual: @"attachment"] ||
	    (fileName = [params objectForKey: @"filename"]) == nil) {
		objc_autoreleasePoolPop(pool);