ObjFW  Check-in [6fbc3b18c7]

Overview
Comment:Allow strings to contain \0

In order to not accidentally have C strings with \0, an
OFInvalidEncodingException is thrown when trying to get a C string for a
string that contains \0.

In order to get a C string with \0 anyway, a new method
-[insecureCStringWithEncoding:] is added.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 6fbc3b18c717dc97bc0169d28f19c0fcb629d6b69f710975d1620a8b6f4ed8e7
User & Date: js on 2024-10-27 22:27:20
Other Links: manifest | tags
References
2024-10-27
22:37 Fixed ticket [134d90a996]: String \0 safety plus 4 other changes artifact: 376510cc34 user: js
Context
2024-10-27
23:38
OFSystemInfo: Fix \0 in +[CPUModel] on macOS check-in: e061c2b66e user: js tags: trunk
22:27
Allow strings to contain \0 check-in: 6fbc3b18c7 user: js tags: trunk
18:38
Remove nullable in .m files check-in: 7d1275cb49 user: js tags: trunk
Changes

Modified src/OFConstantString.m from [e8a18a09ea] to [03e8d1030f].

95
96
97
98
99
100
101
102

103
104
105
106
107
108
109
95
96
97
98
99
100
101

102
103
104
105
106
107
108
109







-
+







			return;

		ivars = OFAllocZeroedMemory(1, sizeof(*ivars));
		ivars->cString = _cString;
		ivars->cStringLength = _cStringLength;

		switch (_OFUTF8StringCheck(ivars->cString, ivars->cStringLength,
		    &ivars->length)) {
		    &ivars->length, &ivars->containsNull)) {
		case 1:
			ivars->isUTF8 = true;
			break;
		case -1:
			OFFreeMemory(ivars);
			@throw [OFInvalidEncodingException exception];
		}

Modified src/OFMutableUTF8String.m from [521dc8393f] to [027a5197e3].

204
205
206
207
208
209
210



211
212
213
214
215
216
217
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220







+
+
+







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

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

	if (character == 0)
		_s->containsNull = true;

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

268
269
270
271
272
273
274
275


276
277
278
279
280
281
282
271
272
273
274
275
276
277

278
279
280
281
282
283
284
285
286







-
+
+








	if (UTF8StringLength >= 3 &&
	    memcmp(UTF8String, "\xEF\xBB\xBF", 3) == 0) {
		UTF8String += 3;
		UTF8StringLength -= 3;
	}

	switch (_OFUTF8StringCheck(UTF8String, UTF8StringLength, &length)) {
	switch (_OFUTF8StringCheck(UTF8String, UTF8StringLength, &length,
	    &_s->containsNull)) {
	case 1:
		_s->isUTF8 = true;
		break;
	case -1:
		@throw [OFInvalidEncodingException exception];
	}

297
298
299
300
301
302
303
304


305
306
307
308
309
310
311
301
302
303
304
305
306
307

308
309
310
311
312
313
314
315
316







-
+
+








	if (UTF8StringLength >= 3 &&
	    memcmp(UTF8String, "\xEF\xBB\xBF", 3) == 0) {
		UTF8String += 3;
		UTF8StringLength -= 3;
	}

	switch (_OFUTF8StringCheck(UTF8String, UTF8StringLength, &length)) {
	switch (_OFUTF8StringCheck(UTF8String, UTF8StringLength, &length,
	    &_s->containsNull)) {
	case 1:
		_s->isUTF8 = true;
		break;
	case -1:
		@throw [OFInvalidEncodingException exception];
	}

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







-
+
















+
+
+


-
+















-
+










+
+
+
















+
+
+







{
	const char *UTF8String;
	size_t UTF8StringLength;

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

	UTF8String = string.UTF8String;
	UTF8String = [string insecureCStringWithEncoding: OFStringEncodingUTF8];
	UTF8StringLength = string.UTF8StringLength;

	_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 += string.length;

	_s->cString[_s->cStringLength] = 0;

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

		if (((OFMutableUTF8String *)string)->_s->containsNull)
			_s->containsNull = true;
	} else {
		switch (_OFUTF8StringCheck(UTF8String, UTF8StringLength,
		    NULL)) {
		    NULL, &_s->containsNull)) {
		case 1:
			_s->isUTF8 = true;
			break;
		case -1:
			@throw [OFInvalidEncodingException exception];
		}
	}
}

- (void)appendCharacters: (const OFUnichar *)characters length: (size_t)length
{
	char *tmp = OFAllocMemory((length * 4) + 1, 1);

	@try {
		size_t j = 0;
		bool isUTF8 = false;
		bool isUTF8 = false, containsNull = false;

		for (size_t i = 0; i < length; i++) {
			size_t len = _OFUTF8StringEncode(characters[i],
			    tmp + j);

			if (len == 0)
				@throw [OFInvalidEncodingException exception];

			if (len > 1)
				isUTF8 = true;

			if (characters[i] == 0)
				containsNull = true;

			j += len;
		}

		tmp[j] = '\0';

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

		if (isUTF8)
			_s->isUTF8 = true;

		if (containsNull)
			_s->containsNull = true;
	} @finally {
		OFFreeMemory(tmp);
	}
}

- (void)appendFormat: (OFConstantString *)format arguments: (va_list)arguments
{
448
449
450
451
452
453
454
455

456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473



474
475
476

477
478
479
480
481
482
483
462
463
464
465
466
467
468

469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492

493
494
495
496
497
498
499
500







-
+


















+
+
+


-
+







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

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

	UTF8String = string.UTF8String;
	UTF8String = [string insecureCStringWithEncoding: OFStringEncodingUTF8];
	UTF8StringLength = string.UTF8StringLength;

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

	memmove(_s->cString + idx + UTF8StringLength, _s->cString + idx,
	    _s->cStringLength - idx);
	memcpy(_s->cString + idx, UTF8String, UTF8StringLength);
	_s->cString[newCStringLength] = '\0';

	_s->cStringLength = newCStringLength;
	_s->length += string.length;

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

		if (((OFMutableUTF8String *)string)->_s->containsNull)
			_s->containsNull = true;
	} else {
		switch (_OFUTF8StringCheck(UTF8String, UTF8StringLength,
		    NULL)) {
		    NULL, &_s->containsNull)) {
		case 1:
			_s->isUTF8 = true;
			break;
		case -1:
			@throw [OFInvalidEncodingException exception];
		}
	}
500
501
502
503
504
505
506








507
508
509
510
511
512
513
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538







+
+
+
+
+
+
+
+








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

	if (_s->containsNull) {
		_s->containsNull = false;

		for (size_t i = 0; i < _s->cStringLength; i++)
			if (_s->cString[i] == '\0')
				_s->containsNull = true;
	}

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


541
542
543
544
545
546
547
558
559
560
561
562
563
564

565
566
567
568
569
570
571
572
573







-
+
+







	if (_s->isUTF8) {
		start = _OFUTF8StringIndexToPosition(_s->cString, start,
		    _s->cStringLength);
		end = _OFUTF8StringIndexToPosition(_s->cString, end,
		    _s->cStringLength);
	}

	replacementString = replacement.UTF8String;
	replacementString =
	    [replacement insecureCStringWithEncoding: OFStringEncodingUTF8];
	replacementLength = replacement.UTF8StringLength;

	newCStringLength =
	    _s->cStringLength - (end - start) + replacementLength;
	_s->hasHash = false;

	/*
572
573
574
575
576
577
578



579
580
581

582
583
584
585
586
587
588








589
590
591
592
593
594
595
596
597




598
599
600
601
602
603
604
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







+
+
+


-
+







+
+
+
+
+
+
+
+







-
-
+
+
+
+







	_s->cStringLength = newCStringLength;
	_s->length = newLength;

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

		if (((OFMutableUTF8String *)replacement)->_s->containsNull)
			_s->containsNull = true;
	} else {
		switch (_OFUTF8StringCheck(replacementString, replacementLength,
		    NULL)) {
		    NULL, &_s->containsNull)) {
		case 1:
			_s->isUTF8 = true;
			break;
		case -1:
			@throw [OFInvalidEncodingException exception];
		}
	}

	if (_s->containsNull) {
		_s->containsNull = false;

		for (size_t i = 0; i < _s->cStringLength; i++)
			if (_s->cString[i] == '\0')
				_s->containsNull = true;
	}
}

- (void)replaceOccurrencesOfString: (OFString *)string
			withString: (OFString *)replacement
			   options: (int)options
			     range: (OFRange)range
{
	const char *searchString = string.UTF8String;
	const char *replacementString = replacement.UTF8String;
	const char *searchString =
	    [string insecureCStringWithEncoding: OFStringEncodingUTF8];
	const char *replacementString =
	    [replacement insecureCStringWithEncoding: OFStringEncodingUTF8];
	size_t searchLength = string.UTF8StringLength;
	size_t replacementLength = replacement.UTF8StringLength;
	size_t last, newCStringLength, newLength;
	char *newCString;

	if (string == nil || replacement == nil)
		@throw [OFInvalidArgumentException exception];
665
666
667
668
669
670
671



672
673
674

675
676
677
678
679
680
681








682
683
684
685
686
687
688
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







+
+
+


-
+







+
+
+
+
+
+
+
+







	_s->cStringLength = newCStringLength;
	_s->length = newLength;

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

		if (((OFMutableUTF8String *)replacement)->_s->containsNull)
			_s->containsNull = true;
	} else {
		switch (_OFUTF8StringCheck(replacementString, replacementLength,
		    NULL)) {
		    NULL, &_s->containsNull)) {
		case 1:
			_s->isUTF8 = true;
			break;
		case -1:
			@throw [OFInvalidEncodingException exception];
		}
	}

	if (_s->containsNull) {
		_s->containsNull = false;

		for (size_t i = 0; i < _s->cStringLength; i++)
			if (_s->cString[i] == '\0')
				_s->containsNull = true;
	}
}

- (void)deleteLeadingWhitespaces
{
	size_t i;

	for (i = 0; i < _s->cStringLength; i++)

Modified src/OFSequencedPacketSocket.h from [68ab17bfbd] to [fa3cdbb7dd].

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







-
+















-
+







		   runLoopMode: (OFRunLoopMode)runLoopMode;

#ifdef OF_HAVE_BLOCKS
/**
 * @brief Asynchronously receives a packet and stores it into the specified
 *	  buffer.
 *
 * @deprecated Use @ref asyncReceiveIntoBuffer:lenght:handler: instead.
 * @deprecated Use @ref asyncReceiveIntoBuffer:length:handler: instead.
 *
 * If the buffer is too small, the receive operation fails.
 *
 * @param buffer The buffer to write the packet to
 * @param length The length of the buffer
 * @param block The block to call when the packet has been received. If the
 *		block returns true, it will be called again with the same
 *		buffer and maximum length when more packets have been received.
 *		If you want the next method in the queue to handle the packet
 *		received next, you need to return false from the method.
 */
- (void)asyncReceiveIntoBuffer: (void *)buffer
			length: (size_t)length
			 block: (OFSequencedPacketSocketAsyncReceiveBlock)block
    OF_DEPRECATED(ObjFW, 1, 2,
	"Use -[asyncReceiveIntoBuffer:lenght:handler:] instead");
	"Use -[asyncReceiveIntoBuffer:length:handler:] instead");

/**
 * @brief Asynchronously receives a packet and stores it into the specified
 *	  buffer.
 *
 * If the buffer is too small, the receive operation fails.
 *

Modified src/OFString+JSONParsing.m from [87273342cc] to [08913e2453].

186
187
188
189
190
191
192




193
194
195
196
197
198
199
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203







+
+
+
+







				buffer[i++] = '\r';
				(*pointer)++;
				break;
			case 't':
				buffer[i++] = '\t';
				(*pointer)++;
				break;
			case '0':
				buffer[i++] = '\0';
				(*pointer)++;
				break;
			/* Parse Unicode escape sequence */
			case 'u':;
				OFChar16 c1, c2;
				OFUnichar c;
				size_t l;

				c1 = parseUnicodeEscape(*pointer - 1, stop);

Modified src/OFString.h from [5672da673b] to [dfff2d8730].

919
920
921
922
923
924
925













926
927
928
929
930
931
932
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945







+
+
+
+
+
+
+
+
+
+
+
+
+







 * copy it.
 *
 * @param encoding The encoding for the C string
 * @return The OFString as a C string in the specified encoding
 */
- (const char *)lossyCStringWithEncoding: (OFStringEncoding)encoding;

/**
 * @brief Returns the OFString as an insecure C string (meaning it can contain
 *	  `\0`) in the specified encoding.
 *
 * The result is valid until the autorelease pool is released. If you want to
 * use the result outside the scope of the current autorelease pool, you have to
 * copy it.
 *
 * @param encoding The encoding for the C string
 * @return The OFString as a C string in the specified encoding
 */
- (const char *)insecureCStringWithEncoding: (OFStringEncoding)encoding;

/**
 * @brief Returns the number of bytes the string needs in the specified
 *	  encoding.
 *
 * @param encoding The encoding for the string
 * @return The number of bytes the string needs in the specified encoding.
 * @throw OFInvalidEncodingException The string cannot be represented in the

Modified src/OFString.m from [0a6f9138dd] to [235c6ba0c3].

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







-
+
+

-
+
+









-
+

-
+

-
+

-
+

-
+

-
+

-
+

-
+

-
+

-
+

-
+

-
+

-
+







# endif
#endif

@interface OFString ()
- (size_t)of_getCString: (char *)cString
	      maxLength: (size_t)maxLength
	       encoding: (OFStringEncoding)encoding
		  lossy: (bool)lossy OF_DIRECT;
		  lossy: (bool)lossy
	       insecure: (bool)insecure OF_DIRECT;
- (const char *)of_cStringWithEncoding: (OFStringEncoding)encoding
				 lossy: (bool)lossy OF_DIRECT;
				 lossy: (bool)lossy
			      insecure: (bool)insecure OF_DIRECT;
- (OFString *)
    of_JSONRepresentationWithOptions: (OFJSONRepresentationOptions)options
			       depth: (size_t)depth;
@end

@interface OFPlaceholderString: OFString
@end

extern bool _OFUnicodeToISO8859_2(const OFUnichar *, unsigned char *,
    size_t, bool);
    size_t, bool, bool);
extern bool _OFUnicodeToISO8859_3(const OFUnichar *, unsigned char *,
    size_t, bool);
    size_t, bool, bool);
extern bool _OFUnicodeToISO8859_15(const OFUnichar *, unsigned char *,
    size_t, bool);
    size_t, bool, bool);
extern bool _OFUnicodeToWindows1250(const OFUnichar *, unsigned char *,
    size_t, bool);
    size_t, bool, bool);
extern bool _OFUnicodeToWindows1251(const OFUnichar *, unsigned char *,
    size_t, bool);
    size_t, bool, bool);
extern bool _OFUnicodeToWindows1252(const OFUnichar *, unsigned char *,
    size_t, bool);
    size_t, bool, bool);
extern bool _OFUnicodeToCodepage437(const OFUnichar *, unsigned char *,
    size_t, bool);
    size_t, bool, bool);
extern bool _OFUnicodeToCodepage850(const OFUnichar *, unsigned char *,
    size_t, bool);
    size_t, bool, bool);
extern bool _OFUnicodeToCodepage852(const OFUnichar *, unsigned char *,
    size_t, bool);
    size_t, bool, bool);
extern bool _OFUnicodeToCodepage858(const OFUnichar *, unsigned char *,
    size_t, bool);
    size_t, bool, bool);
extern bool _OFUnicodeToMacRoman(const OFUnichar *, unsigned char *,
    size_t, bool);
    size_t, bool, bool);
extern bool _OFUnicodeToKOI8R(const OFUnichar *, unsigned char *,
    size_t, bool);
    size_t, bool, bool);
extern bool _OFUnicodeToKOI8U(const OFUnichar *, unsigned char *,
    size_t, bool);
    size_t, bool, bool);

/* References for static linking */
void OF_VISIBILITY_HIDDEN
_references_to_categories_of_OFString(void)
{
	_OFString_CryptographicHashing_reference = 1;
	_OFString_JSONParsing_reference = 1;
373
374
375
376
377
378
379
380

381
382

383
384

385
386




387

388
389
390
391
392
393
394
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







-
+


+

-
+


+
+
+
+
-
+







	memcpy(copy, string, length + 1);

	return copy;
}

#ifdef OF_OBJFW_RUNTIME
static bool
isASCII(const char *string, size_t length)
isASCIIWithoutNull(const char *string, size_t length)
{
	uint8_t combined = 0;
	bool containsNull = false;

	for (size_t i = 0; i < length; i++)
	for (size_t i = 0; i < length; i++) {
		combined |= string[i];

		if (string[i] == '\0')
			containsNull = true;
	}

	return !(combined & ~0x7F);
	return !(combined & ~0x7F) && !containsNull;
}
#endif

@implementation OFPlaceholderString
#ifdef __clang__
/* We intentionally don't call into super, so silence the warning. */
# pragma clang diagnostic push
407
408
409
410
411
412
413
414

415
416
417
418
419
420
421
414
415
416
417
418
419
420

421
422
423
424
425
426
427
428







-
+







	void *storage;

	if (length == 0)
		return (id)@"";

#ifdef OF_OBJFW_RUNTIME
	if (length <= MAX_TAGGED_POINTER_LENGTH &&
	    isASCII(UTF8String, length)) {
	    isASCIIWithoutNull(UTF8String, length)) {
		id ret = [OFTaggedPointerString
		    stringWithASCIIString: UTF8String
				   length: length];

		if (ret != nil)
			return ret;
	}
435
436
437
438
439
440
441
442

443
444
445
446
447
448
449
442
443
444
445
446
447
448

449
450
451
452
453
454
455
456







-
+







	void *storage;

	if (UTF8StringLength == 0)
		return (id)@"";

#ifdef OF_OBJFW_RUNTIME
	if (UTF8StringLength <= MAX_TAGGED_POINTER_LENGTH &&
	    isASCII(UTF8String, UTF8StringLength)) {
	    isASCIIWithoutNull(UTF8String, UTF8StringLength)) {
		id ret = [OFTaggedPointerString
		    stringWithASCIIString: UTF8String
				   length: UTF8StringLength];

		if (ret != nil)
			return ret;
	}
485
486
487
488
489
490
491
492

493
494
495
496
497
498
499
492
493
494
495
496
497
498

499
500
501
502
503
504
505
506







-
+







		void *storage;

		if (length == 0)
			return (id)@"";

#ifdef OF_OBJFW_RUNTIME
		if (length <= MAX_TAGGED_POINTER_LENGTH &&
		    isASCII(cString, length)) {
		    isASCIIWithoutNull(cString, length)) {
			id ret = [OFTaggedPointerString
			    stringWithASCIIString: cString
					   length: length];

			if (ret != nil)
				return ret;
		}
521
522
523
524
525
526
527
528

529
530
531
532
533
534
535
528
529
530
531
532
533
534

535
536
537
538
539
540
541
542







-
+







		void *storage;

		if (cStringLength == 0)
			return (id)@"";

#ifdef OF_OBJFW_RUNTIME
		if (cStringLength <= MAX_TAGGED_POINTER_LENGTH &&
		    isASCII(cString, cStringLength)) {
		    isASCIIWithoutNull(cString, cStringLength)) {
			id ret = [OFTaggedPointerString
			    stringWithASCIIString: cString
					   length: cStringLength];

			if (ret != nil)
				return ret;
		}
565
566
567
568
569
570
571
572

573
574
575
576


577
578
579
580
581
582
583

584
585
586
587
588
589
590
572
573
574
575
576
577
578

579
580
581


582
583
584
585
586
587
588
589

590
591
592
593
594
595
596
597







-
+


-
-
+
+






-
+







{
	if (length == 0)
		return (id)@"";

#ifdef OF_OBJFW_RUNTIME
	if (length <= MAX_TAGGED_POINTER_LENGTH) {
		char buffer[MAX_TAGGED_POINTER_LENGTH];
		bool isUnicode = false;
		bool useTaggedPointer = true;

		for (size_t i = 0; i < length; i++) {
			if (string[i] >= 0x80) {
				isUnicode = true;
			if (string[i] >= 0x80 || string[i] == 0) {
				useTaggedPointer = false;
				break;
			}

			buffer[i] = (char)string[i];
		}

		if (!isUnicode) {
		if (useTaggedPointer) {
			id ret = [OFTaggedPointerString
			    stringWithASCIIString: buffer
					   length: length];

			if (ret != nil)
				return ret;
		}
1174
1175
1176
1177
1178
1179
1180

1181
1182
1183
1184
1185
1186
1187
1188
1189
1190





1191

1192
1193
1194
1195
1196
1197
1198
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203

1204
1205
1206
1207
1208
1209
1210
1211







+










+
+
+
+
+
-
+







	return self;
}

- (size_t)of_getCString: (char *)cString
	      maxLength: (size_t)maxLength
	       encoding: (OFStringEncoding)encoding
		  lossy: (bool)lossy
	       insecure: (bool)insecure
{
	const OFUnichar *characters = self.characters;
	size_t i, length = self.length;

	switch (encoding) {
	case OFStringEncodingUTF8:;
		size_t j = 0;

		for (i = 0; i < length; i++) {
			char buffer[4];
			size_t len;

			if OF_UNLIKELY (!insecure && characters[i] == 0)
				@throw [OFInvalidEncodingException exception];

			size_t len = _OFUTF8StringEncode(characters[i], buffer);
			len = _OFUTF8StringEncode(characters[i], buffer);

			/*
			 * Check for one more than the current index, as we
			 * need one for the terminating zero.
			 */
			if (j + len >= maxLength)
				@throw [OFOutOfRangeException exception];
1220
1221
1222
1223
1224
1225
1226



1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244



1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264

1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277

1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290

1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303

1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316

1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329

1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342

1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355

1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368

1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381

1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394

1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407

1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420

1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439


1440
1441
1442
1443
1444
1445
1446
1447
1448
1449


1450
1451
1452
1453

1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469


1470
1471
1472
1473
1474
1475
1476
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282

1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295

1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308

1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321

1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334

1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347

1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360

1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373

1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386

1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399

1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412

1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425

1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438

1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457

1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468

1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490

1491
1492
1493
1494
1495
1496
1497
1498
1499







+
+
+


















+
+
+



















-
+












-
+












-
+












-
+












-
+












-
+












-
+












-
+












-
+












-
+












-
+












-
+












-
+


















-
+
+









-
+
+




+















-
+
+








		return j;
	case OFStringEncodingASCII:
		if (length + 1 > maxLength)
			@throw [OFOutOfRangeException exception];

		for (i = 0; i < length; i++) {
			if OF_UNLIKELY (!insecure && characters[i] == 0)
				@throw [OFInvalidEncodingException exception];

			if OF_UNLIKELY (characters[i] > 0x80) {
				if (lossy)
					cString[i] = '?';
				else
					@throw [OFInvalidEncodingException
					    exception];
			} else
				cString[i] = (unsigned char)characters[i];
		}

		cString[i] = '\0';

		return length;
	case OFStringEncodingISO8859_1:
		if (length + 1 > maxLength)
			@throw [OFOutOfRangeException exception];

		for (i = 0; i < length; i++) {
			if OF_UNLIKELY (!insecure && characters[i] == 0)
				@throw [OFInvalidEncodingException exception];

			if OF_UNLIKELY (characters[i] > 0xFF) {
				if (lossy)
					cString[i] = '?';
				else
					@throw [OFInvalidEncodingException
					    exception];
			} else
				cString[i] = (unsigned char)characters[i];
		}

		cString[i] = '\0';

		return length;
#ifdef HAVE_ISO_8859_2
	case OFStringEncodingISO8859_2:
		if (length + 1 > maxLength)
			@throw [OFOutOfRangeException exception];

		if (!_OFUnicodeToISO8859_2(characters, (unsigned char *)cString,
		    length, lossy))
		    length, lossy, insecure))
			@throw [OFInvalidEncodingException exception];

		cString[length] = '\0';

		return length;
#endif
#ifdef HAVE_ISO_8859_3
	case OFStringEncodingISO8859_3:
		if (length + 1 > maxLength)
			@throw [OFOutOfRangeException exception];

		if (!_OFUnicodeToISO8859_3(characters, (unsigned char *)cString,
		    length, lossy))
		    length, lossy, insecure))
			@throw [OFInvalidEncodingException exception];

		cString[length] = '\0';

		return length;
#endif
#ifdef HAVE_ISO_8859_15
	case OFStringEncodingISO8859_15:
		if (length + 1 > maxLength)
			@throw [OFOutOfRangeException exception];

		if (!_OFUnicodeToISO8859_15(characters,
		    (unsigned char *)cString, length, lossy))
		    (unsigned char *)cString, length, lossy, insecure))
			@throw [OFInvalidEncodingException exception];

		cString[length] = '\0';

		return length;
#endif
#ifdef HAVE_WINDOWS_1250
	case OFStringEncodingWindows1250:
		if (length + 1 > maxLength)
			@throw [OFOutOfRangeException exception];

		if (!_OFUnicodeToWindows1250(characters,
		    (unsigned char *)cString, length, lossy))
		    (unsigned char *)cString, length, lossy, insecure))
			@throw [OFInvalidEncodingException exception];

		cString[length] = '\0';

		return length;
#endif
#ifdef HAVE_WINDOWS_1251
	case OFStringEncodingWindows1251:
		if (length + 1 > maxLength)
			@throw [OFOutOfRangeException exception];

		if (!_OFUnicodeToWindows1251(characters,
		    (unsigned char *)cString, length, lossy))
		    (unsigned char *)cString, length, lossy, insecure))
			@throw [OFInvalidEncodingException exception];

		cString[length] = '\0';

		return length;
#endif
#ifdef HAVE_WINDOWS_1252
	case OFStringEncodingWindows1252:
		if (length + 1 > maxLength)
			@throw [OFOutOfRangeException exception];

		if (!_OFUnicodeToWindows1252(characters,
		    (unsigned char *)cString, length, lossy))
		    (unsigned char *)cString, length, lossy, insecure))
			@throw [OFInvalidEncodingException exception];

		cString[length] = '\0';

		return length;
#endif
#ifdef HAVE_CODEPAGE_437
	case OFStringEncodingCodepage437:
		if (length + 1 > maxLength)
			@throw [OFOutOfRangeException exception];

		if (!_OFUnicodeToCodepage437(characters,
		    (unsigned char *)cString, length, lossy))
		    (unsigned char *)cString, length, lossy, insecure))
			@throw [OFInvalidEncodingException exception];

		cString[length] = '\0';

		return length;
#endif
#ifdef HAVE_CODEPAGE_850
	case OFStringEncodingCodepage850:
		if (length + 1 > maxLength)
			@throw [OFOutOfRangeException exception];

		if (!_OFUnicodeToCodepage850(characters,
		    (unsigned char *)cString, length, lossy))
		    (unsigned char *)cString, length, lossy, insecure))
			@throw [OFInvalidEncodingException exception];

		cString[length] = '\0';

		return length;
#endif
#ifdef HAVE_CODEPAGE_852
	case OFStringEncodingCodepage852:
		if (length + 1 > maxLength)
			@throw [OFOutOfRangeException exception];

		if (!_OFUnicodeToCodepage852(characters,
		    (unsigned char *)cString, length, lossy))
		    (unsigned char *)cString, length, lossy, insecure))
			@throw [OFInvalidEncodingException exception];

		cString[length] = '\0';

		return length;
#endif
#ifdef HAVE_CODEPAGE_858
	case OFStringEncodingCodepage858:
		if (length + 1 > maxLength)
			@throw [OFOutOfRangeException exception];

		if (!_OFUnicodeToCodepage858(characters,
		    (unsigned char *)cString, length, lossy))
		    (unsigned char *)cString, length, lossy, insecure))
			@throw [OFInvalidEncodingException exception];

		cString[length] = '\0';

		return length;
#endif
#ifdef HAVE_MAC_ROMAN
	case OFStringEncodingMacRoman:
		if (length + 1 > maxLength)
			@throw [OFOutOfRangeException exception];

		if (!_OFUnicodeToMacRoman(characters, (unsigned char *)cString,
		    length, lossy))
		    length, lossy, insecure))
			@throw [OFInvalidEncodingException exception];

		cString[length] = '\0';

		return length;
#endif
#ifdef HAVE_KOI8_R
	case OFStringEncodingKOI8R:
		if (length + 1 > maxLength)
			@throw [OFOutOfRangeException exception];

		if (!_OFUnicodeToKOI8R(characters, (unsigned char *)cString,
		    length, lossy))
		    length, lossy, insecure))
			@throw [OFInvalidEncodingException exception];

		cString[length] = '\0';

		return length;
#endif
#ifdef HAVE_KOI8_U
	case OFStringEncodingKOI8U:
		if (length + 1 > maxLength)
			@throw [OFOutOfRangeException exception];

		if (!_OFUnicodeToKOI8U(characters, (unsigned char *)cString,
		    length, lossy))
		    length, lossy, insecure))
			@throw [OFInvalidEncodingException exception];

		cString[length] = '\0';

		return length;
#endif
	default:
		@throw [OFInvalidArgumentException exception];
	}
}

- (size_t)getCString: (char *)cString
	   maxLength: (size_t)maxLength
	    encoding: (OFStringEncoding)encoding
{
	return [self of_getCString: cString
			 maxLength: maxLength
			  encoding: encoding
			     lossy: false];
			     lossy: false
			  insecure: false];
}

- (size_t)getLossyCString: (char *)cString
		maxLength: (size_t)maxLength
		 encoding: (OFStringEncoding)encoding
{
	return [self of_getCString: cString
			 maxLength: maxLength
			  encoding: encoding
			     lossy: true];
			     lossy: true
			  insecure: false];
}

- (const char *)of_cStringWithEncoding: (OFStringEncoding)encoding
				 lossy: (bool)lossy
			      insecure: (bool)insecure
{
	size_t length = self.length;
	char *cString;
	size_t cStringLength;
	const char *ret;

	switch (encoding) {
	case OFStringEncodingUTF8:
		cString = OFAllocMemory((length * 4) + 1, 1);

		@try {
			cStringLength = [self
			    of_getCString: cString
				maxLength: (length * 4) + 1
				 encoding: OFStringEncodingUTF8
				    lossy: lossy];
				    lossy: lossy
				 insecure: insecure];
		} @catch (id e) {
			OFFreeMemory(cString);
			@throw e;
		}

		@try {
			cString = OFResizeMemory(cString, cStringLength + 1, 1);
1496
1497
1498
1499
1500
1501
1502
1503


1504
1505
1506
1507
1508
1509
1510
1519
1520
1521
1522
1523
1524
1525

1526
1527
1528
1529
1530
1531
1532
1533
1534







-
+
+







	case OFStringEncodingKOI8U:
		cString = OFAllocMemory(length + 1, 1);

		@try {
			cStringLength = [self of_getCString: cString
						  maxLength: length + 1
						   encoding: encoding
						      lossy: lossy];
						      lossy: lossy
						   insecure: insecure];
		} @catch (id e) {
			OFFreeMemory(cString);
			@throw e;
		}

		break;
	default:
1521
1522
1523
1524
1525
1526
1527
1528



1529
1530
1531
1532
1533










1534
1535
1536
1537
1538



1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558

1559




1560
1561
1562
1563
1564
1565
1566
1545
1546
1547
1548
1549
1550
1551

1552
1553
1554
1555
1556
1557
1558

1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572

1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594

1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607







-
+
+
+




-
+
+
+
+
+
+
+
+
+
+




-
+
+
+



















-
+

+
+
+
+







	}

	return ret;
}

- (const char *)cStringWithEncoding: (OFStringEncoding)encoding
{
	return [self of_cStringWithEncoding: encoding lossy: false];
	return [self of_cStringWithEncoding: encoding
				      lossy: false
				   insecure: false];
}

- (const char *)lossyCStringWithEncoding: (OFStringEncoding)encoding
{
	return [self of_cStringWithEncoding: encoding lossy: true];
	return [self of_cStringWithEncoding: encoding
				      lossy: true
				   insecure: false];
}

- (const char *)insecureCStringWithEncoding: (OFStringEncoding)encoding
{
	return [self of_cStringWithEncoding: encoding
				      lossy: false
				   insecure: true];
}

- (const char *)UTF8String
{
	return [self cStringWithEncoding: OFStringEncodingUTF8];
	return [self of_cStringWithEncoding: OFStringEncodingUTF8
				      lossy: false
				   insecure: false];
}

- (size_t)length
{
	OF_UNRECOGNIZED_SELECTOR
}

- (size_t)cStringLengthWithEncoding: (OFStringEncoding)encoding
{
	switch (encoding) {
	case OFStringEncodingUTF8:;
		const OFUnichar *characters;
		size_t length, UTF8StringLength = 0;

		characters = self.characters;
		length = self.length;

		for (size_t i = 0; i < length; i++) {
			char buffer[4];
			size_t len = _OFUTF8StringEncode(characters[i], buffer);
			size_t len;

			if (characters[i] == 0)
				@throw [OFInvalidArgumentException exception];

			len = _OFUTF8StringEncode(characters[i], buffer);
			if (len == 0)
				@throw [OFInvalidEncodingException exception];

			UTF8StringLength += len;
		}

		return UTF8StringLength;
1797
1798
1799
1800
1801
1802
1803

1804
1805
1806

1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819

1820
1821
1822
1823
1824
1825
1826
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847

1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869







+


-
+













+







	[JSON replaceOccurrencesOfString: @"\b" withString: @"\\b"];
	[JSON replaceOccurrencesOfString: @"\f" withString: @"\\f"];
	[JSON replaceOccurrencesOfString: @"\r" withString: @"\\r"];
	[JSON replaceOccurrencesOfString: @"\t" withString: @"\\t"];

	if (options & OFJSONRepresentationOptionJSON5) {
		[JSON replaceOccurrencesOfString: @"\n" withString: @"\\\n"];
		[JSON replaceOccurrencesOfString: @"\0" withString: @"\\0"];

		if (options & OFJSONRepresentationOptionIsIdentifier) {
			const char *cString = self.UTF8String;
			const char *cString = JSON.UTF8String;

			if ((!OFASCIIIsAlpha(cString[0]) &&
			    cString[0] != '_' && cString[0] != '$') ||
			    strpbrk(cString, " \n\r\t\b\f\\\"'") != NULL) {
				[JSON insertString: @"\"" atIndex: 0];
				[JSON appendString: @"\""];
			}
		} else {
			[JSON insertString: @"\"" atIndex: 0];
			[JSON appendString: @"\""];
		}
	} else {
		[JSON replaceOccurrencesOfString: @"\n" withString: @"\\n"];
		[JSON replaceOccurrencesOfString: @"\0" withString: @"\\u0000"];

		[JSON insertString: @"\"" atIndex: 0];
		[JSON appendString: @"\""];
	}

	[JSON makeImmutable];

1859
1860
1861
1862
1863
1864
1865

1866

1867
1868
1869
1870
1871
1872
1873
1902
1903
1904
1905
1906
1907
1908
1909

1910
1911
1912
1913
1914
1915
1916
1917







+
-
+








		data = [OFMutableData dataWithCapacity: length + 5];
		[data addItem: &type];
		[data addItems: &tmp count: sizeof(tmp)];
	} else
		@throw [OFOutOfRangeException exception];

	[data addItems: [self insecureCStringWithEncoding: OFStringEncodingUTF8]
	[data addItems: self.UTF8String count: length];
		 count: length];

	return data;
}

- (OFRange)rangeOfString: (OFString *)string
{
	return [self rangeOfString: string
2654
2655
2656
2657
2658
2659
2660
2661

2662
2663
2664
2665
2666
2667
2668
2698
2699
2700
2701
2702
2703
2704

2705
2706
2707
2708
2709
2710
2711
2712







-
+







	/* Allocate memory for the worst case */
	buffer = OFAllocMemory((length + 1) * 2, sizeof(OFChar16));

	j = 0;
	for (size_t i = 0; i < length; i++) {
		OFUnichar c = characters[i];

		if (c > 0x10FFFF) {
		if (c > 0x10FFFF || c == 0) {
			OFFreeMemory(buffer);
			@throw [OFInvalidEncodingException exception];
		}

		if (swap) {
			if (c > 0xFFFF) {
				c -= 0x10000;
2729
2730
2731
2732
2733
2734
2735




2736

2737
2738

2739
2740
2741
2742
2743
2744
2745
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783

2784

2785
2786
2787
2788
2789
2790
2791
2792
2793







+
+
+
+
-
+
-

+







	const OFChar32 *ret;

	buffer = OFAllocMemory(length + 1, sizeof(OFChar32));
	@try {
		[self getCharacters: buffer inRange: OFMakeRange(0, length)];
		buffer[length] = 0;

		for (size_t i = 0; i < length; i++) {
			if (buffer[i] == 0)
				@throw [OFInvalidEncodingException exception];

		if (byteOrder != OFByteOrderNative)
			if (byteOrder != OFByteOrderNative)
			for (size_t i = 0; i < length; i++)
				buffer[i] = OFByteSwap32(buffer[i]);
		}

		ret = [[OFData dataWithItemsNoCopy: buffer
					     count: length + 1
					  itemSize: sizeof(OFChar32)
				      freeWhenDone: true] items];
	} @catch (id e) {
		OFFreeMemory(buffer);

Modified src/OFTCPSocket.h from [3d8f48f978] to [c7a38bf638].

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







-
+







@class OFTCPSocket;
@class OFString;

#ifdef OF_HAVE_BLOCKS
/**
 * @brief A block which is called when the socket connected.
 *
 * @deprecated Use @ref OFTCPSocketConnecetedHandler instead.
 * @deprecated Use @ref OFTCPSocketConnectedHandler instead.
 *
 * @param exception An exception which occurred while connecting the socket or
 *		    `nil` on success
 */
typedef void (^OFTCPSocketAsyncConnectBlock)(id _Nullable exception)
    OF_DEPRECATED(ObjFW, 1, 2, "Use OFTCPSocketConnecetedHandler instead");

Modified src/OFUTF8String+Private.h from [ccfb13bd6d] to [84456dce78].

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







-
+







			       length: (size_t)UTF8StringLength
			      storage: (char *)storage OF_METHOD_FAMILY(init);
@end

#ifdef __cplusplus
extern "C" {
#endif
extern int _OFUTF8StringCheck(const char *, size_t, size_t *_Nullable)
extern int _OFUTF8StringCheck(const char *, size_t, size_t *_Nullable, bool *)
    OF_VISIBILITY_HIDDEN;
extern size_t _OFUTF8StringIndexToPosition(const char *, size_t, size_t)
    OF_VISIBILITY_HIDDEN;
#ifdef __cplusplus
}
#endif

Modified src/OFUTF8String.h from [fe9a65353f] to [06f893bbd3].

29
30
31
32
33
34
35
36

37
38
39
40
41
42
43
29
30
31
32
33
34
35

36
37
38
39
40
41
42
43







-
+







	 * Since constant strings don't have `_storage`, they have to allocate
	 * it on the first access. Strings created at runtime just set the
	 * pointer to `&_storage`.
	 */
	struct OFUTF8StringIvars {
		char          *cString;
		size_t        cStringLength;
		bool          isUTF8;
		bool          isUTF8, containsNull;
		size_t        length;
		bool          hasHash;
		unsigned long hash;
		bool          freeWhenDone;
	} *restrict _s;
	struct OFUTF8StringIvars _storage;
}

Modified src/OFUTF8String.m from [eb22322b98] to [731d0242b9].

88
89
90
91
92
93
94
95


96
97
98

99
100



101
102
103
104
105
106
107
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







-
+
+



+


+
+
+







			return OFOrderedAscending;
	}

	return OFOrderedSame;
}

int
_OFUTF8StringCheck(const char *UTF8String, size_t UTF8Length, size_t *length)
_OFUTF8StringCheck(const char *UTF8String, size_t UTF8Length, size_t *length,
    bool *containsNull)
{
	size_t tmpLength = UTF8Length;
	int isUTF8 = 0;
	bool tmpContainsNull = false;

	for (size_t i = 0; i < UTF8Length; i++) {
		if OF_UNLIKELY (UTF8String[i] == '\0')
			tmpContainsNull = true;

		/* No sign of UTF-8 here */
		if OF_LIKELY (!(UTF8String[i] & 0x80))
			continue;

		isUTF8 = 1;

		/* We're missing a start byte here */
151
152
153
154
155
156
157



158
159
160
161
162
163
164
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172







+
+
+







		i += 3;
		tmpLength -= 3;
	}

	if (length != NULL)
		*length = tmpLength;

	if (containsNull != NULL)
		*containsNull = tmpContainsNull;

	return isUTF8;
}

static size_t
positionToIndex(const char *string, size_t position)
{
	size_t idx = position;
214
215
216
217
218
219
220
221

222
223
224
225
226
227
228
222
223
224
225
226
227
228

229
230
231
232
233
234
235
236







-
+








		_s = &_storage;

		_s->cString = storage;
		_s->cStringLength = UTF8StringLength;

		switch (_OFUTF8StringCheck(UTF8String, UTF8StringLength,
		    &_s->length)) {
		    &_s->length, &_s->containsNull)) {
		case 1:
			_s->isUTF8 = true;
			break;
		case -1:
			@throw [OFInvalidEncodingException exception];
		}

258
259
260
261
262
263
264
265

266
267
268
269
270
271
272
266
267
268
269
270
271
272

273
274
275
276
277
278
279
280







-
+







		_s->cString = OFAllocMemory(cStringLength + 1, 1);
		_s->cStringLength = cStringLength;
		_s->freeWhenDone = true;

		if (encoding == OFStringEncodingUTF8 ||
		    encoding == OFStringEncodingASCII) {
			switch (_OFUTF8StringCheck(cString, cStringLength,
			    &_s->length)) {
			    &_s->length, &_s->containsNull)) {
			case 1:
				if (encoding == OFStringEncodingASCII)
					@throw [OFInvalidEncodingException
					    exception];

				_s->isUTF8 = true;
				break;
291
292
293
294
295
296
297




298
299
300
301
302
303
304
305
306
307
299
300
301
302
303
304
305
306
307
308
309
310
311

312
313
314
315
316
317
318







+
+
+
+


-








				if (!(cString[i] & 0x80)) {
					_s->cString[j++] = cString[i];
					continue;
				}

				_s->isUTF8 = true;

				if OF_UNLIKELY (cString[i] == '\0')
					_s->containsNull = true;

				bytes = _OFUTF8StringEncode(
				    (uint8_t)cString[i], buffer);

				if (bytes == 0)
					@throw [OFInvalidEncodingException
					    exception];

				_s->cStringLength += bytes - 1;
				_s->cString = OFResizeMemory(_s->cString,
				    _s->cStringLength + 1, 1);
382
383
384
385
386
387
388



389
390
391
392
393
394
395
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409







+
+
+








			if (unichar < 0x7F) {
				_s->cString[j++] = (char)unichar;
				continue;
			}

			_s->isUTF8 = true;

			if OF_UNLIKELY (cString[i] == '\0')
				_s->containsNull = true;

			byteLength = _OFUTF8StringEncode(unichar, buffer);
			if (byteLength == 0)
				@throw [OFInvalidEncodingException exception];

			_s->cStringLength += byteLength - 1;
			_s->cString = OFResizeMemory(_s->cString,
429
430
431
432
433
434
435
436

437
438
439
440
441
442
443
443
444
445
446
447
448
449

450
451
452
453
454
455
456
457







-
+







		if (UTF8StringLength >= 3 &&
		    memcmp(UTF8String, "\xEF\xBB\xBF", 3) == 0) {
			UTF8String += 3;
			UTF8StringLength -= 3;
		}

		switch (_OFUTF8StringCheck(UTF8String, UTF8StringLength,
		    &_s->length)) {
		    &_s->length, &_s->containsNull)) {
		case 1:
			_s->isUTF8 = true;
			break;
		case -1:
			@throw [OFInvalidEncodingException exception];
		}

459
460
461
462
463
464
465
466



467
468
469
470

471


472

473
474

475
476
477
478
479
480
481
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







-
+
+
+



-
+

+
+
-
+

-
+







	@try {
		_s = &_storage;

		_s->cStringLength = string.UTF8StringLength;
		_s->length = string.length;

		_s->cString = OFAllocMemory(_s->cStringLength + 1, 1);
		memcpy(_s->cString, string.UTF8String, _s->cStringLength + 1);
		memcpy(_s->cString,
		    [string insecureCStringWithEncoding: OFStringEncodingUTF8],
		    _s->cStringLength + 1);
		_s->freeWhenDone = true;

		if ([string isKindOfClass: [OFUTF8String class]] ||
		    [string isKindOfClass: [OFMutableUTF8String class]])
		    [string isKindOfClass: [OFMutableUTF8String class]]) {
			_s->isUTF8 = ((OFUTF8String *)string)->_s->isUTF8;
			_s->containsNull =
			    ((OFUTF8String *)string)->_s->containsNull;
		else {
		} else {
			switch (_OFUTF8StringCheck(_s->cString,
			    _s->cStringLength, NULL)) {
			    _s->cStringLength, NULL, &_s->containsNull)) {
			case 1:
				_s->isUTF8 = true;
				break;
			case -1:
				@throw [OFInvalidEncodingException exception];
			}
		}
507
508
509
510
511
512
513



514
515
516
517
518
519
520
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541







+
+
+







			    _s->cString + j);

			if (len == 0)
				@throw [OFInvalidEncodingException exception];

			if (len > 1)
				_s->isUTF8 = true;

			if OF_UNLIKELY (characters[i] == 0)
				_s->containsNull = true;

			j += len;
		}

		_s->cString[j] = '\0';
		_s->cStringLength = j;

593
594
595
596
597
598
599



600
601
602
603
604
605
606
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630







+
+
+








			if (len == 0)
				@throw [OFInvalidEncodingException exception];

			if (len > 1)
				_s->isUTF8 = true;

			if OF_UNLIKELY (character == 0)
				_s->containsNull = true;

			j += len;
		}

		_s->cString[j] = '\0';
		_s->cStringLength = j;

		@try {
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
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







-
-
-
+
+
+
-

















+
+
+







		_s->cString = OFAllocMemory((length * 4) + 1, 1);
		_s->length = length;
		_s->freeWhenDone = true;

		j = 0;
		for (size_t i = 0; i < length; i++) {
			char buffer[4];
			size_t len = _OFUTF8StringEncode((swap
			    ? OFByteSwap32(characters[i])
			    : characters[i]),
			OFUnichar character = (swap
			    ? OFByteSwap32(characters[i]) : characters[i]);
			size_t len = _OFUTF8StringEncode(character, buffer);
			    buffer);

			switch (len) {
			case 1:
				_s->cString[j++] = buffer[0];
				break;
			case 2:
			case 3:
			case 4:
				_s->isUTF8 = true;

				memcpy(_s->cString + j, buffer, len);
				j += len;

				break;
			default:
				@throw [OFInvalidEncodingException exception];
			}

			if OF_UNLIKELY (character == 0)
				_s->containsNull = true;
		}

		_s->cString[j] = '\0';
		_s->cStringLength = j;

		@try {
			_s->cString = OFResizeMemory(_s->cString, j + 1, 1);
702
703
704
705
706
707
708
709

710
711
712
713
714
715
716
728
729
730
731
732
733
734

735
736
737
738
739
740
741
742







-
+







		    arguments)) == -1)
			@throw [OFInvalidFormatException exception];

		_s->cStringLength = cStringLength;

		@try {
			switch (_OFUTF8StringCheck(tmp, cStringLength,
			    &_s->length)) {
			    &_s->length, &_s->containsNull)) {
			case 1:
				_s->isUTF8 = true;
				break;
			case -1:
				@throw [OFInvalidEncodingException exception];
			}

736
737
738
739
740
741
742



743
744
745
746
747
748
749
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778







+
+
+







	[super dealloc];
}

- (size_t)getCString: (char *)cString
	   maxLength: (size_t)maxLength
	    encoding: (OFStringEncoding)encoding
{
	if (_s->containsNull)
		@throw [OFInvalidEncodingException exception];

	switch (encoding) {
	case OFStringEncodingASCII:
		if (_s->isUTF8)
			@throw [OFInvalidEncodingException exception];
		/* intentional fall-through */
	case OFStringEncodingUTF8:
		if (_s->cStringLength + 1 > maxLength)
757
758
759
760
761
762
763



764
765
766
767
768
769
770
771
772
773
774














775
776
777



778
779
780
781
782
783
784
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833







+
+
+











+
+
+
+
+
+
+
+
+
+
+
+
+
+



+
+
+







			       maxLength: maxLength
				encoding: encoding];
	}
}

- (const char *)cStringWithEncoding: (OFStringEncoding)encoding
{
	if (_s->containsNull)
		@throw [OFInvalidEncodingException exception];

	switch (encoding) {
	case OFStringEncodingASCII:
		if (_s->isUTF8)
			@throw [OFInvalidEncodingException exception];
		/* intentional fall-through */
	case OFStringEncodingUTF8:
		return _s->cString;
	default:
		return [super cStringWithEncoding: encoding];
	}
}

- (const char *)insecureCStringWithEncoding: (OFStringEncoding)encoding
{
	switch (encoding) {
	case OFStringEncodingASCII:
		if (_s->isUTF8)
			@throw [OFInvalidEncodingException exception];
		/* intentional fall-through */
	case OFStringEncodingUTF8:
		return _s->cString;
	default:
		return [super insecureCStringWithEncoding: encoding];
	}
}

- (const char *)UTF8String
{
	if (_s->containsNull)
		@throw [OFInvalidEncodingException exception];

	return _s->cString;
}

- (size_t)length
{
	return _s->length;
}
816
817
818
819
820
821
822
823



824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844


845
846
847
848
849
850
851
865
866
867
868
869
870
871

872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894

895
896
897
898
899
900
901
902
903







-
+
+
+




















-
+
+







		return false;

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

	if (strcmp(_s->cString, string.UTF8String) != 0)
	if (memcmp(_s->cString,
	    [string insecureCStringWithEncoding: OFStringEncodingUTF8],
	    _s->cStringLength) != 0)
		return false;

	return true;
}

- (OFComparisonResult)compare: (OFString *)string
{
	size_t otherCStringLength, minimumCStringLength;
	int compare;

	if (string == self)
		return OFOrderedSame;

	if (![string isKindOfClass: [OFString class]])
		@throw [OFInvalidArgumentException exception];

	otherCStringLength = string.UTF8StringLength;
	minimumCStringLength = (_s->cStringLength > otherCStringLength
	    ? otherCStringLength : _s->cStringLength);

	if ((compare = memcmp(_s->cString, string.UTF8String,
	if ((compare = memcmp(_s->cString,
	    [string insecureCStringWithEncoding: OFStringEncodingUTF8],
	    minimumCStringLength)) == 0) {
		if (_s->cStringLength > otherCStringLength)
			return OFOrderedDescending;
		if (_s->cStringLength < otherCStringLength)
			return OFOrderedAscending;
		return OFOrderedSame;
	}
864
865
866
867
868
869
870
871


872
873
874
875
876
877
878
916
917
918
919
920
921
922

923
924
925
926
927
928
929
930
931







-
+
+







	size_t i, j;
#endif
	int compare;

	if (string == self)
		return OFOrderedSame;

	otherCString = string.UTF8String;
	otherCString =
	    [string insecureCStringWithEncoding: OFStringEncodingUTF8];
	otherCStringLength = string.UTF8StringLength;

#ifdef OF_HAVE_UNICODE_TABLES
	if (!_s->isUTF8) {
#endif
		minimumCStringLength = (_s->cStringLength > otherCStringLength
		    ? otherCStringLength : _s->cStringLength);
1008
1009
1010
1011
1012
1013
1014
1015


1016
1017
1018
1019
1020
1021
1022
1061
1062
1063
1064
1065
1066
1067

1068
1069
1070
1071
1072
1073
1074
1075
1076







-
+
+







	objc_autoreleasePoolPop(pool);
}

- (OFRange)rangeOfString: (OFString *)string
		 options: (OFStringSearchOptions)options
		   range: (OFRange)range
{
	const char *cString = string.UTF8String;
	const char *cString =
	    [string insecureCStringWithEncoding: OFStringEncodingUTF8];
	size_t cStringLength = string.UTF8StringLength;
	size_t rangeLocation, rangeLength;

	if (range.length > SIZE_MAX - range.location ||
	    range.location + range.length > _s->length)
		@throw [OFOutOfRangeException exception];

1066
1067
1068
1069
1070
1071
1072
1073


1074
1075
1076
1077
1078
1079
1080
1120
1121
1122
1123
1124
1125
1126

1127
1128
1129
1130
1131
1132
1133
1134
1135







-
+
+







	}

	return OFMakeRange(OFNotFound, 0);
}

- (bool)containsString: (OFString *)string
{
	const char *cString = string.UTF8String;
	const char *cString =
	    [string insecureCStringWithEncoding: OFStringEncodingUTF8];
	size_t cStringLength = string.UTF8StringLength;

	if (cStringLength == 0)
		return true;

	if (cStringLength > _s->cStringLength)
		return false;
1108
1109
1110
1111
1112
1113
1114
1115



1116
1117
1118
1119
1120
1121
1122
1123
1124
1125

1126

1127
1128
1129
1130
1131
1132
1133
1163
1164
1165
1166
1167
1168
1169

1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183

1184
1185
1186
1187
1188
1189
1190
1191







-
+
+
+










+
-
+







- (bool)hasPrefix: (OFString *)prefix
{
	size_t cStringLength = prefix.UTF8StringLength;

	if (cStringLength > _s->cStringLength)
		return false;

	return (memcmp(_s->cString, prefix.UTF8String, cStringLength) == 0);
	return (memcmp(_s->cString,
	    [prefix insecureCStringWithEncoding: OFStringEncodingUTF8],
	    cStringLength) == 0);
}

- (bool)hasSuffix: (OFString *)suffix
{
	size_t cStringLength = suffix.UTF8StringLength;

	if (cStringLength > _s->cStringLength)
		return false;

	return (memcmp(_s->cString + (_s->cStringLength - cStringLength),
	    [suffix insecureCStringWithEncoding: OFStringEncodingUTF8],
	    suffix.UTF8String, cStringLength) == 0);
	    cStringLength) == 0);
}

- (OFArray *)componentsSeparatedByString: (OFString *)delimiter
				 options: (OFStringSeparationOptions)options
{
	void *pool;
	OFMutableArray *array;
1141
1142
1143
1144
1145
1146
1147
1148

1149
1150
1151
1152
1153
1154
1155
1199
1200
1201
1202
1203
1204
1205

1206
1207
1208
1209
1210
1211
1212
1213







-
+







		@throw [OFInvalidArgumentException exception];

	if (delimiter.length == 0)
		return [OFArray arrayWithObject: self];

	array = [OFMutableArray array];
	pool = objc_autoreleasePoolPush();
	cString = delimiter.UTF8String;
	cString = [delimiter insecureCStringWithEncoding: OFStringEncodingUTF8];
	cStringLength = delimiter.UTF8StringLength;

	if (cStringLength > _s->cStringLength) {
		[array addObject: [[self copy] autorelease]];
		objc_autoreleasePoolPop(pool);

		return array;
1212
1213
1214
1215
1216
1217
1218
1219

1220
1221
1222





1223
1224
1225
1226
1227
1228
1229
1270
1271
1272
1273
1274
1275
1276

1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292







-
+



+
+
+
+
+







	}

	return ret;
}

- (const OFChar32 *)UTF32StringWithByteOrder: (OFByteOrder)byteOrder
{
	OFChar32 *buffer = OFAllocMemory(_s->length + 1, sizeof(OFChar32));
	OFChar32 *buffer;
	size_t i = 0, j = 0;
	const OFChar32 *ret;

	if (_s->containsNull)
		@throw [OFInvalidEncodingException exception];

	buffer = OFAllocMemory(_s->length + 1, sizeof(OFChar32));

	while (i < _s->cStringLength) {
		OFChar32 c;
		ssize_t cLen;

		cLen = _OFUTF8StringDecode(_s->cString + i,
		    _s->cStringLength - i, &c);

Modified src/encodings/codepage-437.m from [0e1d4e03c0] to [ae0ed7f16e].

130
131
132
133
134
135
136
137

138
139
140






141
142
143
144
145
146
147
130
131
132
133
134
135
136

137
138
139

140
141
142
143
144
145
146
147
148
149
150
151
152







-
+


-
+
+
+
+
+
+







	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0xFE
};
static const uint8_t page25Start = 0x00;

bool OF_VISIBILITY_HIDDEN
_OFUnicodeToCodepage437(const OFUnichar *input, unsigned char *output,
    size_t length, bool lossy)
    size_t length, bool lossy, bool insecure)
{
	for (size_t i = 0; i < length; i++) {
		OFUnichar c = input[i];
		OFUnichar c;

		if OF_UNLIKELY (!insecure && input[i] == 0)
			return 0;

		c = input[i];

		if OF_UNLIKELY (c > 0x7F) {
			uint8_t idx;

			if OF_UNLIKELY (c > 0xFFFF) {
				if (lossy) {
					output[i] = '?';

Modified src/encodings/codepage-850.m from [116375a4bc] to [fa7a3fd8d6].

105
106
107
108
109
110
111
112

113
114
115






116
117
118
119
120
121
122
105
106
107
108
109
110
111

112
113
114

115
116
117
118
119
120
121
122
123
124
125
126
127







-
+


-
+
+
+
+
+
+







	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0xFE
};
static const uint8_t page25Start = 0x00;

bool OF_VISIBILITY_HIDDEN
_OFUnicodeToCodepage850(const OFUnichar *input, unsigned char *output,
    size_t length, bool lossy)
    size_t length, bool lossy, bool insecure)
{
	for (size_t i = 0; i < length; i++) {
		OFUnichar c = input[i];
		OFUnichar c;

		if OF_UNLIKELY (!insecure && input[i] == 0)
			return false;

		c = input[i];

		if OF_UNLIKELY (c > 0x7F) {
			uint8_t idx;

			if OF_UNLIKELY (c > 0xFFFF) {
				if (lossy) {
					output[i] = '?';

Modified src/encodings/codepage-852.m from [2e105e7181] to [555c3df35d].

110
111
112
113
114
115
116
117

118
119
120






121
122
123
124
125
126
127
110
111
112
113
114
115
116

117
118
119

120
121
122
123
124
125
126
127
128
129
130
131
132







-
+


-
+
+
+
+
+
+







	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0xFE
};
static const uint8_t page25Start = 0x00;

bool OF_VISIBILITY_HIDDEN
_OFUnicodeToCodepage852(const OFUnichar *input, unsigned char *output,
    size_t length, bool lossy)
    size_t length, bool lossy, bool insecure)
{
	for (size_t i = 0; i < length; i++) {
		OFUnichar c = input[i];
		OFUnichar c;

		if OF_UNLIKELY (!insecure && input[i] == 0)
			return false;

		c = input[i];

		if OF_UNLIKELY (c > 0x7F) {
			uint8_t idx;

			if OF_UNLIKELY (c > 0xFFFF) {
				if (lossy) {
					output[i] = '?';

Modified src/encodings/codepage-858.m from [0a443ea671] to [2c6eb1a5f6].

111
112
113
114
115
116
117
118

119
120
121






122
123
124
125
126
127
128
111
112
113
114
115
116
117

118
119
120

121
122
123
124
125
126
127
128
129
130
131
132
133







-
+


-
+
+
+
+
+
+







	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0xFE
};
static const uint8_t page25Start = 0x00;

bool OF_VISIBILITY_HIDDEN
_OFUnicodeToCodepage858(const OFUnichar *input, unsigned char *output,
    size_t length, bool lossy)
    size_t length, bool lossy, bool insecure)
{
	for (size_t i = 0; i < length; i++) {
		OFUnichar c = input[i];
		OFUnichar c;

		if OF_UNLIKELY (!insecure && input[i] == 0)
			return false;

		c = input[i];

		if OF_UNLIKELY (c > 0x7F) {
			uint8_t idx;

			if OF_UNLIKELY (c > 0xFFFF) {
				if (lossy) {
					output[i] = '?';

Modified src/encodings/iso-8859-15.m from [25e41edd49] to [24cf09fca0].

61
62
63
64
65
66
67
68

69
70
71






72
73
74
75
76
77
78
61
62
63
64
65
66
67

68
69
70

71
72
73
74
75
76
77
78
79
80
81
82
83







-
+


-
+
+
+
+
+
+







static const unsigned char page20[] = {
	0xA4
};
static const uint8_t page20Start = 0xAC;

bool OF_VISIBILITY_HIDDEN
_OFUnicodeToISO8859_15(const OFUnichar *input, unsigned char *output,
    size_t length, bool lossy)
    size_t length, bool lossy, bool insecure)
{
	for (size_t i = 0; i < length; i++) {
		OFUnichar c = input[i];
		OFUnichar c;

		if OF_UNLIKELY (!insecure && input[i] == 0)
			return false;

		c = input[i];

		if OF_UNLIKELY (c > 0x7F) {
			uint8_t idx;

			if OF_UNLIKELY (c > 0xFFFF) {
				if (lossy) {
					output[i] = '?';

Modified src/encodings/iso-8859-2.m from [bd3f7dada7] to [a88030091f].

81
82
83
84
85
86
87
88

89
90
91






92
93
94
95
96
97
98
81
82
83
84
85
86
87

88
89
90

91
92
93
94
95
96
97
98
99
100
101
102
103







-
+


-
+
+
+
+
+
+







	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0xA2, 0xFF, 0x00, 0xB2, 0x00, 0xBD
};
static const uint8_t page2Start = 0xC7;

bool OF_VISIBILITY_HIDDEN
_OFUnicodeToISO8859_2(const OFUnichar *input, unsigned char *output,
    size_t length, bool lossy)
    size_t length, bool lossy, bool insecure)
{
	for (size_t i = 0; i < length; i++) {
		OFUnichar c = input[i];
		OFUnichar c;

		if OF_UNLIKELY (!insecure && input[i] == 0)
			return false;

		c = input[i];

		if OF_UNLIKELY (c > 0x7F) {
			uint8_t idx;

			if OF_UNLIKELY (c > 0xFFFF) {
				if (lossy) {
					output[i] = '?';

Modified src/encodings/iso-8859-3.m from [bc9ab876ae] to [bf5185ce65].

78
79
80
81
82
83
84
85

86
87
88






89
90
91
92
93
94
95
78
79
80
81
82
83
84

85
86
87

88
89
90
91
92
93
94
95
96
97
98
99
100







-
+


-
+
+
+
+
+
+







static const unsigned char page2[] = {
	0xA2, 0xFF
};
static const uint8_t page2Start = 0xD8;

bool OF_VISIBILITY_HIDDEN
_OFUnicodeToISO8859_3(const OFUnichar *input, unsigned char *output,
    size_t length, bool lossy)
    size_t length, bool lossy, bool insecure)
{
	for (size_t i = 0; i < length; i++) {
		OFUnichar c = input[i];
		OFUnichar c;

		if OF_UNLIKELY (!insecure && input[i] == 0)
			return false;

		c = input[i];

		if OF_UNLIKELY (c > 0x7F) {
			uint8_t idx;

			if OF_UNLIKELY (c > 0xFFFF) {
				if (lossy) {
					output[i] = '?';

Modified src/encodings/koi8-r.m from [7d684b26ac] to [47c8151c6a].

116
117
118
119
120
121
122
123

124
125
126






127
128
129
130
131
132
133
116
117
118
119
120
121
122

123
124
125

126
127
128
129
130
131
132
133
134
135
136
137
138







-
+


-
+
+
+
+
+
+







	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x94
};
static const uint8_t page25Start = 0x00;

bool OF_VISIBILITY_HIDDEN
_OFUnicodeToKOI8R(const OFUnichar *input, unsigned char *output, size_t length,
    bool lossy)
    bool lossy, bool insecure)
{
	for (size_t i = 0; i < length; i++) {
		OFUnichar c = input[i];
		OFUnichar c;

		if OF_UNLIKELY (!insecure && input[i] == 0)
			return false;

		c = input[i];

		if OF_UNLIKELY (c > 0x7F) {
			uint8_t idx;

			if OF_UNLIKELY (c > 0xFFFF) {
				if (lossy) {
					output[i] = '?';

Modified src/encodings/koi8-u.m from [0f9ae48dcf] to [ae62fae518].

124
125
126
127
128
129
130
131

132
133
134






135
136
137
138
139
140
141
124
125
126
127
128
129
130

131
132
133

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







-
+


-
+
+
+
+
+
+







	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x94
};
static const uint8_t page25Start = 0x00;

bool OF_VISIBILITY_HIDDEN
_OFUnicodeToKOI8U(const OFUnichar *input, unsigned char *output, size_t length,
    bool lossy)
    bool lossy, bool insecure)
{
	for (size_t i = 0; i < length; i++) {
		OFUnichar c = input[i];
		OFUnichar c;

		if OF_UNLIKELY (!insecure && input[i] == 0)
			return false;

		c = input[i];

		if OF_UNLIKELY (c > 0x7F) {
			uint8_t idx;

			if OF_UNLIKELY (c > 0xFFFF) {
				if (lossy) {
					output[i] = '?';

Modified src/encodings/mac-roman.m from [48622afc6e] to [6cd071203c].

150
151
152
153
154
155
156
157

158
159
160






161
162
163
164
165
166
167
150
151
152
153
154
155
156

157
158
159

160
161
162
163
164
165
166
167
168
169
170
171
172







-
+


-
+
+
+
+
+
+







static const unsigned char pageFB[] = {
	0xDE, 0xDF
};
static const uint8_t pageFBStart = 0x01;

bool OF_VISIBILITY_HIDDEN
_OFUnicodeToMacRoman(const OFUnichar *input, unsigned char *output,
    size_t length, bool lossy)
    size_t length, bool lossy, bool insecure)
{
	for (size_t i = 0; i < length; i++) {
		OFUnichar c = input[i];
		OFUnichar c;

		if OF_UNLIKELY (!insecure && input[i] == 0)
			return false;

		c = input[i];

		if OF_UNLIKELY (c > 0x7F) {
			uint8_t idx;

			if OF_UNLIKELY (c > 0xFFFF) {
				if (lossy) {
					output[i] = '?';

Modified src/encodings/windows-1250.m from [66d83a46d2] to [bfff0cf391].

114
115
116
117
118
119
120
121

122
123
124






125
126
127
128
129
130
131
114
115
116
117
118
119
120

121
122
123

124
125
126
127
128
129
130
131
132
133
134
135
136







-
+


-
+
+
+
+
+
+







static const unsigned char page21[] = {
	0x99
};
static const uint8_t page21Start = 0x22;

bool OF_VISIBILITY_HIDDEN
_OFUnicodeToWindows1250(const OFUnichar *input, unsigned char *output,
    size_t length, bool lossy)
    size_t length, bool lossy, bool insecure)
{
	for (size_t i = 0; i < length; i++) {
		OFUnichar c = input[i];
		OFUnichar c;

		if OF_UNLIKELY (!insecure && input[i] == 0)
			return false;

		c = input[i];

		if OF_UNLIKELY (c > 0x7F) {
			uint8_t idx;

			if OF_UNLIKELY (c > 0xFFFF) {
				if (lossy) {
					output[i] = '?';

Modified src/encodings/windows-1251.m from [fb08e37f56] to [4405821c2a].

103
104
105
106
107
108
109
110

111
112
113






114
115
116
117
118
119
120
103
104
105
106
107
108
109

110
111
112

113
114
115
116
117
118
119
120
121
122
123
124
125







-
+


-
+
+
+
+
+
+







	0xB9, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x99
};
static const uint8_t page21Start = 0x16;

bool OF_VISIBILITY_HIDDEN
_OFUnicodeToWindows1251(const OFUnichar *input, unsigned char *output,
    size_t length, bool lossy)
    size_t length, bool lossy, bool insecure)
{
	for (size_t i = 0; i < length; i++) {
		OFUnichar c = input[i];
		OFUnichar c;

		if OF_UNLIKELY (!insecure && input[i] == 0)
			return false;

		c = input[i];

		if OF_UNLIKELY (c > 0x7F) {
			uint8_t idx;

			if OF_UNLIKELY (c > 0xFFFF) {
				if (lossy) {
					output[i] = '?';

Modified src/encodings/windows-1252.m from [1577a4fe9c] to [0d87252cdf].

99
100
101
102
103
104
105
106

107
108
109






110
111
112
113
114
115
116
99
100
101
102
103
104
105

106
107
108

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







-
+


-
+
+
+
+
+
+







static const unsigned char page21[] = {
	0x99
};
static const uint8_t page21Start = 0x22;

bool OF_VISIBILITY_HIDDEN
_OFUnicodeToWindows1252(const OFUnichar *input, unsigned char *output,
    size_t length, bool lossy)
    size_t length, bool lossy, bool insecure)
{
	for (size_t i = 0; i < length; i++) {
		OFUnichar c = input[i];
		OFUnichar c;

		if OF_UNLIKELY (!insecure && input[i] == 0)
			return false;

		c = input[i];

		if OF_UNLIKELY (c > 0x7F) {
			uint8_t idx;

			if OF_UNLIKELY (c > 0xFFFF) {
				if (lossy) {
					output[i] = '?';

Modified tests/OFJSONTests.m from [2ee8657285] to [4578e0b89d].

24
25
26
27
28
29
30
31

32
33
34
35
36
37
38
39
40

41
42
43
44
45
46
47
24
25
26
27
28
29
30

31
32
33
34
35
36
37
38
39

40
41
42
43
44
45
46
47







-
+








-
+








@interface OFJSONTests: OTTestCase
{
	OFDictionary *_dictionary;
}
@end

static OFString *string = @"{\"foo\"\t:'b\\na\\r', \"x\":/*foo*/ [.5\r,0xF,"
static OFString *string = @"{\"f\\0oo\"\t:'b\\na\\r', \"x\":/*foo*/ [.5\r,0xF,"
    @"null//bar\n,\"foo\",false]}";

@implementation OFJSONTests
- (void)setUp
{
	[super setUp];

	_dictionary = [[OTOrderedDictionary alloc] initWithKeysAndObjects:
	    @"foo", @"b\na\r",
	    @"f\0oo", @"b\na\r",
	    @"x", [OFArray arrayWithObjects:
		[OFNumber numberWithFloat: .5f],
		[OFNumber numberWithInt: 0xF],
		[OFNull null],
		@"foo",
		[OFNumber numberWithBool: false],
		nil],
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
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







-
+















-
+







-
+







{
	OTAssertEqualObjects(string.objectByParsingJSON, _dictionary);
}

- (void)testJSONRepresentation
{
	OTAssert(_dictionary.JSONRepresentation,
	    @"{\"foo\":\"b\\na\\r\",\"x\":[0.5,15,null,\"foo\",false]}");
	    @"{\"f\\u0000oo\":\"b\\na\\r\",\"x\":[0.5,15,null,\"foo\",false]}");
}

- (void)testSortedJSONRepresentation
{
	OTAssertEqualObjects(
	    [([OFDictionary dictionaryWithKeysAndObjects:
	    @"b", @"a", @"a", @"b", nil])
	    JSONRepresentationWithOptions: OFJSONRepresentationOptionSorted],
	    @"{\"a\":\"b\",\"b\":\"a\"}");
}

- (void)testPrettyJSONRepresentation
{
	OTAssertEqualObjects([_dictionary JSONRepresentationWithOptions:
	    OFJSONRepresentationOptionPretty],
	    @"{\n\t\"foo\": \"b\\na\\r\",\n\t\"x\": [\n\t\t0.5,\n\t\t15,"
	    @"{\n\t\"f\\u0000oo\": \"b\\na\\r\",\n\t\"x\": [\n\t\t0.5,\n\t\t15,"
	    @"\n\t\tnull,\n\t\t\"foo\",\n\t\tfalse\n\t]\n}");
}

- (void)testJSON5Representation
{
	OTAssertEqualObjects([_dictionary JSONRepresentationWithOptions:
	    OFJSONRepresentationOptionJSON5],
	    @"{foo:\"b\\\na\\r\",x:[0.5,15,null,\"foo\",false]}");
	    @"{\"f\\0oo\":\"b\\\na\\r\",x:[0.5,15,null,\"foo\",false]}");
}

- (void)testObjectByParsingJSONFailsWithInvalidJSON
{
	OTAssertThrowsSpecific([@"{" objectByParsingJSON],
	    OFInvalidJSONException);