ObjFW  Check-in [9cfefe9c30]

Overview
Comment:Move UTF-8 functions to OFString_UTF8.m.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 9cfefe9c30b7d22f346c2af13be6c92fb3fc1b888c18f805d7ab1dab58885003
User & Date: js on 2012-10-13 20:50:09
Other Links: manifest | tags
Context
2012-10-13
21:06
of_bswap* -> OF_BSWAP*. check-in: 1cb8fee5c3 user: js tags: trunk
20:50
Move UTF-8 functions to OFString_UTF8.m. check-in: 9cfefe9c30 user: js tags: trunk
20:35
OFMutableString_UTF8: Add missing range check. check-in: 41bfdb858c user: js tags: trunk
Changes

Modified src/OFConstantString.m from [93ca41c6dc] to [0925478785].

151
152
153
154
155
156
157
158

159
160
161
162
163
164
165
151
152
153
154
155
156
157

158
159
160
161
162
163
164
165







-
+







		@throw [OFOutOfMemoryException
		    exceptionWithClass: [self class]
			 requestedSize: sizeof(*ivars)];

	ivars->cString = cString;
	ivars->cStringLength = cStringLength;

	switch (of_string_check_utf8(ivars->cString, ivars->cStringLength,
	switch (of_string_utf8_check(ivars->cString, ivars->cStringLength,
	    &ivars->length)) {
	case 1:
		ivars->UTF8 = YES;
		break;
	case -1:
		free(ivars);
		@throw [OFInvalidEncodingException

Modified src/OFMutableString_UTF8.m from [e0a807cffb] to [e8d0af35fc].

111
112
113
114
115
116
117
118

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

118
119
120
121
122
123
124
125







-
+







			table = startTable;
			tableSize = middleTableSize;
		} else {
			table = middleTable;
			tableSize = middleTableSize;
		}

		cLen = of_string_utf8_to_unicode(s->cString + i,
		cLen = of_string_utf8_decode(s->cString + i,
		    s->cStringLength - i, &c);

		if (cLen == 0 || c > 0x10FFFF) {
			[self freeMemory: unicodeString];
			@throw [OFInvalidEncodingException
			    exceptionWithClass: [self class]];
		}
169
170
171
172
173
174
175
176

177
178
179
180
181
182
183
169
170
171
172
173
174
175

176
177
178
179
180
181
182
183







-
+







	}

	j = 0;

	for (i = 0; i < unicodeLen; i++) {
		size_t d;

		if ((d = of_string_unicode_to_utf8(unicodeString[i],
		if ((d = of_string_utf8_encode(unicodeString[i],
		    newCString + j)) == 0) {
			[self freeMemory: unicodeString];
			[self freeMemory: newCString];
			@throw [OFInvalidEncodingException
			    exceptionWithClass: [self class]];
		}
		j += d;
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
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







-
+


-
+





+






-
+



-
+






-
-
+
+
-
-
-
+
-

-
-
+
+

-
-
-
-
+
+
+
+

-
-
+
+




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

-
-
+
+








-
+
-
-
-
-
+












-
+







}

- (void)setCharacter: (of_unichar_t)character
	     atIndex: (size_t)index
{
	char buffer[4];
	of_unichar_t c;
	size_t length, oldLength;
	size_t lenNew, lenOld;

	if (s->UTF8)
		index = of_string_index_to_position(s->cString, index,
		index = of_string_utf8_get_position(s->cString, index,
		    s->cStringLength);

	if (index > s->cStringLength)
		@throw [OFOutOfRangeException exceptionWithClass: [self class]];

	/* Shortcut if old and new character both are ASCII */
	if (!(character & 0x80) && !(s->cString[index] & 0x80)) {
		s->hashed = NO;
		s->cString[index] = character;
		return;
	}

	if ((length = of_string_unicode_to_utf8(character, buffer)) == 0)
	if ((lenNew = of_string_utf8_encode(character, buffer)) == 0)
		@throw [OFInvalidEncodingException
		    exceptionWithClass: [self class]];

	if ((oldLength = of_string_utf8_to_unicode(s->cString + index,
	if ((lenOld = of_string_utf8_decode(s->cString + index,
	    s->cStringLength - index, &c)) == 0)
		@throw [OFInvalidEncodingException
		    exceptionWithClass: [self class]];

	s->hashed = NO;

	if (length == oldLength) {
		memcpy(s->cString + index, buffer, length);
	if (lenNew == lenOld)
		memcpy(s->cString + index, buffer, lenNew);
		return;
	}

	else if (lenNew > lenOld) {
	if (length > oldLength) {
		s->cString = [self resizeMemory: s->cString
					   size: s->cStringLength - oldLength +
						 length + 1];
					   size: s->cStringLength -
						 lenOld + lenNew + 1];

		memmove(s->cString + index + length,
		    s->cString + index + oldLength,
		    s->cStringLength - index - oldLength);
		memcpy(s->cString + index, buffer, length);
		memmove(s->cString + index + lenNew,
		    s->cString + index + lenOld,
		    s->cStringLength - index - lenOld);
		memcpy(s->cString + index, buffer, lenNew);

		s->cStringLength -= oldLength;
		s->cStringLength += length;
		s->cStringLength -= lenOld;
		s->cStringLength += lenNew;
		s->cString[s->cStringLength] = '\0';

		if (character & 0x80)
			s->UTF8 = YES;

	} else if (lenNew < lenOld) {
		return;
	}

	if (length < oldLength) {
		memmove(s->cString + index + length,
		    s->cString + index + oldLength,
		    s->cStringLength - index - oldLength);
		memcpy(s->cString + index, buffer, length);
		memmove(s->cString + index + lenNew,
		    s->cString + index + lenOld,
		    s->cStringLength - index - lenOld);
		memcpy(s->cString + index, buffer, lenNew);

		s->cStringLength -= oldLength;
		s->cStringLength += length;
		s->cStringLength -= lenOld;
		s->cStringLength += lenNew;
		s->cString[s->cStringLength] = '\0';

		@try {
			s->cString = [self resizeMemory: s->cString
						   size: s->cStringLength + 1];
		} @catch (OFOutOfMemoryException *e) {
			/* We don't really care, as we only made it smaller */
		}

	} else
		return;
	}

	assert(0);
		assert(0);
}

- (void)appendUTF8String: (const char*)UTF8String
{
	size_t UTF8StringLength = strlen(UTF8String);
	size_t length;

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

	switch (of_string_check_utf8(UTF8String, UTF8StringLength, &length)) {
	switch (of_string_utf8_check(UTF8String, UTF8StringLength, &length)) {
	case 1:
		s->UTF8 = YES;
		break;
	case -1:
		@throw [OFInvalidEncodingException
		    exceptionWithClass: [self class]];
	}
312
313
314
315
316
317
318
319

320
321
322
323
324
325
326
303
304
305
306
307
308
309

310
311
312
313
314
315
316
317







-
+







	size_t length;

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

	switch (of_string_check_utf8(UTF8String, UTF8StringLength, &length)) {
	switch (of_string_utf8_check(UTF8String, UTF8StringLength, &length)) {
	case 1:
		s->UTF8 = YES;
		break;
	case -1:
		@throw [OFInvalidEncodingException
		    exceptionWithClass: [self class]];
	}
508
509
510
511
512
513
514
515

516
517
518
519
520
521
522
499
500
501
502
503
504
505

506
507
508
509
510
511
512
513







-
+







	size_t newCStringLength;

	if (index > s->length)
		@throw [OFOutOfRangeException
		    exceptionWithClass: [self class]];

	if (s->UTF8)
		index = of_string_index_to_position(s->cString, index,
		index = of_string_utf8_get_position(s->cString, index,
		    s->cStringLength);

	newCStringLength = s->cStringLength + [string UTF8StringLength];
	s->hashed = NO;
	s->cString = [self resizeMemory: s->cString
				   size: newCStringLength + 1];

545
546
547
548
549
550
551
552

553
554

555
556
557
558
559
560
561
536
537
538
539
540
541
542

543
544

545
546
547
548
549
550
551
552







-
+

-
+







	if (end > s->length)
		@throw [OFOutOfRangeException exceptionWithClass: [self class]];

	s->hashed = NO;
	s->length -= end - start;

	if (s->UTF8) {
		start = of_string_index_to_position(s->cString, start,
		start = of_string_utf8_get_position(s->cString, start,
		    s->cStringLength);
		end = of_string_index_to_position(s->cString, end,
		end = of_string_utf8_get_position(s->cString, end,
		    s->cStringLength);
	}

	memmove(s->cString + start, s->cString + end, s->cStringLength - end);
	s->cStringLength -= end - start;
	s->cString[s->cStringLength] = 0;

576
577
578
579
580
581
582
583

584
585

586
587
588
589
590
591
592
567
568
569
570
571
572
573

574
575

576
577
578
579
580
581
582
583







-
+

-
+








	if (end > s->length)
		@throw [OFOutOfRangeException exceptionWithClass: [self class]];

	newLength = s->length - (end - start) + [replacement length];

	if (s->UTF8) {
		start = of_string_index_to_position(s->cString, start,
		start = of_string_utf8_get_position(s->cString, start,
		    s->cStringLength);
		end = of_string_index_to_position(s->cString, end,
		end = of_string_utf8_get_position(s->cString, end,
		    s->cStringLength);
	}

	newCStringLength = s->cStringLength - (end - start) +
	    [replacement UTF8StringLength];
	s->hashed = NO;
	s->cString = [self resizeMemory: s->cString
610
611
612
613
614
615
616
617

618
619

620
621
622
623
624
625
626
601
602
603
604
605
606
607

608
609

610
611
612
613
614
615
616
617







-
+

-
+







	const char *replacementString = [replacement UTF8String];
	size_t searchLength = [string UTF8StringLength];
	size_t replacementLength = [replacement UTF8StringLength];
	size_t i, last, newCStringLength, newLength;
	char *newCString;

	if (s->UTF8) {
		range.start = of_string_index_to_position(s->cString,
		range.start = of_string_utf8_get_position(s->cString,
		    range.start, s->cStringLength);
		range.length = of_string_index_to_position(s->cString,
		range.length = of_string_utf8_get_position(s->cString,
		    range.start + range.length, s->cStringLength) - range.start;
	}

	if (range.start + range.length > [self UTF8StringLength])
		@throw [OFOutOfRangeException exceptionWithClass: [self class]];

	if ([string UTF8StringLength] > range.length)

Modified src/OFString+JSONValue.m from [a76e28c035] to [12270ead53].

203
204
205
206
207
208
209
210

211
212
213
214
215
216
217
203
204
205
206
207
208
209

210
211
212
213
214
215
216
217







-
+







				if ((c1 & 0xFC00) == 0xDC00) {
					free(buffer);
					return nil;
				}

				/* Normal character */
				if ((c1 & 0xFC00) != 0xD800) {
					l = of_string_unicode_to_utf8(c1,
					l = of_string_utf8_encode(c1,
					    buffer + i);
					if (l == 0) {
						free(buffer);
						return nil;
					}

					i += l;
230
231
232
233
234
235
236
237

238
239
240
241
242
243
244
230
231
232
233
234
235
236

237
238
239
240
241
242
243
244







-
+







					free(buffer);
					return nil;
				}

				c = (((c1 & 0x3FF) << 10) |
				    (c2 & 0x3FF)) + 0x10000;

				l = of_string_unicode_to_utf8(c, buffer + i);
				l = of_string_utf8_encode(c, buffer + i);
				if (l == 0) {
					free(buffer);
					return nil;
				}

				i += l;
				*pointer += 11;
327
328
329
330
331
332
333
334

335
336
337
338
339
340
341
327
328
329
330
331
332
333

334
335
336
337
338
339
340
341







-
+







			if ((c1 & 0xFC00) == 0xDC00) {
				free(buffer);
				return nil;
			}

			/* Normal character */
			if ((c1 & 0xFC00) != 0xD800) {
				l = of_string_unicode_to_utf8(c1, buffer + i);
				l = of_string_utf8_encode(c1, buffer + i);
				if (l == 0) {
					free(buffer);
					return nil;
				}

				i += l;
				*pointer += 5;
352
353
354
355
356
357
358
359

360
361
362
363
364
365
366
352
353
354
355
356
357
358

359
360
361
362
363
364
365
366







-
+







			if (c2 == 0xFFFF) {
				free(buffer);
				return nil;
			}

			c = (((c1 & 0x3FF) << 10) | (c2 & 0x3FF)) + 0x10000;

			l = of_string_unicode_to_utf8(c, buffer + i);
			l = of_string_utf8_encode(c, buffer + i);
			if (l == 0) {
				free(buffer);
				return nil;
			}

			i += l;
			*pointer += 11;

Modified src/OFString+XMLUnescaping.m from [a0597bf098] to [d167de896a].

63
64
65
66
67
68
69
70

71
72
73
74
75
76
77
63
64
65
66
67
68
69

70
71
72
73
74
75
76
77







-
+







			if (entity[i] >= '0' && entity[i] <= '9')
				c = (c * 10) + (entity[i] - '0');
			else
				return nil;
		}
	}

	if ((i = of_string_unicode_to_utf8(c, buffer)) == 0)
	if ((i = of_string_utf8_encode(c, buffer)) == 0)
		return nil;
	buffer[i] = 0;

	return [OFString stringWithUTF8String: buffer
				       length: i];
}

Modified src/OFString.h from [3ded5f2be1] to [505bd888a0].

55
56
57
58
59
60
61
62
63
64
65
66





67
68
69
70
71
72
73
55
56
57
58
59
60
61





62
63
64
65
66
67
68
69
70
71
72
73







-
-
-
-
-
+
+
+
+
+







#ifdef OF_HAVE_BLOCKS
typedef void (^of_string_line_enumeration_block_t)(OFString *line, BOOL *stop);
#endif

#ifdef __cplusplus
extern "C" {
#endif
extern int of_string_check_utf8(const char*, size_t, size_t*);
extern size_t of_string_unicode_to_utf8(of_unichar_t, char*);
extern size_t of_string_utf8_to_unicode(const char*, size_t, of_unichar_t*);
extern size_t of_string_position_to_index(const char*, size_t);
extern size_t of_string_index_to_position(const char*, size_t, size_t);
extern int of_string_utf8_check(const char*, size_t, size_t*);
extern size_t of_string_utf8_encode(of_unichar_t, char*);
extern size_t of_string_utf8_decode(const char*, size_t, of_unichar_t*);
extern size_t of_string_utf8_get_index(const char*, size_t);
extern size_t of_string_utf8_get_position(const char*, size_t, size_t);
extern size_t of_unicode_string_length(const of_unichar_t*);
extern size_t of_utf16_string_length(const uint16_t*);
#ifdef __cplusplus
}
#endif

@class OFArray;

Modified src/OFString.m from [6a39e4a81c] to [2ae4fee8c1].

67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
67
68
69
70
71
72
73


































































































































































74
75
76
77
78
79
80







-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-







}

void _reference_to_OFConstantString(void)
{
	[OFConstantString class];
}

int
of_string_check_utf8(const char *cString, size_t cStringLength, size_t *length)
{
	size_t i, tmpLength = cStringLength;
	int UTF8 = 0;

	for (i = 0; i < cStringLength; i++) {
		/* No sign of UTF-8 here */
		if OF_LIKELY (!(cString[i] & 0x80))
			continue;

		UTF8 = 1;

		/* We're missing a start byte here */
		if OF_UNLIKELY (!(cString[i] & 0x40))
			return -1;

		/* 2 byte sequences for code points 0 - 127 are forbidden */
		if OF_UNLIKELY ((cString[i] & 0x7E) == 0x40)
			return -1;

		/* We have at minimum a 2 byte character -> check next byte */
		if OF_UNLIKELY (cStringLength <= i + 1 ||
		    (cString[i + 1] & 0xC0) != 0x80)
			return -1;

		/* Check if we have at minimum a 3 byte character */
		if OF_LIKELY (!(cString[i] & 0x20)) {
			i++;
			tmpLength--;
			continue;
		}

		/* We have at minimum a 3 byte char -> check second next byte */
		if OF_UNLIKELY (cStringLength <= i + 2 ||
		    (cString[i + 2] & 0xC0) != 0x80)
			return -1;

		/* Check if we have a 4 byte character */
		if OF_LIKELY (!(cString[i] & 0x10)) {
			i += 2;
			tmpLength -= 2;
			continue;
		}

		/* We have a 4 byte character -> check third next byte */
		if OF_UNLIKELY (cStringLength <= i + 3 ||
		    (cString[i + 3] & 0xC0) != 0x80)
			return -1;

		/*
		 * Just in case, check if there's a 5th character, which is
		 * forbidden by UTF-8
		 */
		if OF_UNLIKELY (cString[i] & 0x08)
			return -1;

		i += 3;
		tmpLength -= 3;
	}

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

	return UTF8;
}

size_t
of_string_unicode_to_utf8(of_unichar_t character, char *buffer)
{
	size_t i = 0;

	if (character < 0x80) {
		buffer[i] = character;
		return 1;
	}
	if (character < 0x800) {
		buffer[i++] = 0xC0 | (character >> 6);
		buffer[i] = 0x80 | (character & 0x3F);
		return 2;
	}
	if (character < 0x10000) {
		buffer[i++] = 0xE0 | (character >> 12);
		buffer[i++] = 0x80 | (character >> 6 & 0x3F);
		buffer[i] = 0x80 | (character & 0x3F);
		return 3;
	}
	if (character < 0x110000) {
		buffer[i++] = 0xF0 | (character >> 18);
		buffer[i++] = 0x80 | (character >> 12 & 0x3F);
		buffer[i++] = 0x80 | (character >> 6 & 0x3F);
		buffer[i] = 0x80 | (character & 0x3F);
		return 4;
	}

	return 0;
}

size_t
of_string_utf8_to_unicode(const char *buffer_, size_t length, of_unichar_t *ret)
{
	const uint8_t *buffer = (const uint8_t*)buffer_;

	if (!(*buffer & 0x80)) {
		*ret = buffer[0];
		return 1;
	}

	if ((*buffer & 0xE0) == 0xC0) {
		if OF_UNLIKELY (length < 2)
			return 0;

		*ret = ((buffer[0] & 0x1F) << 6) | (buffer[1] & 0x3F);
		return 2;
	}

	if ((*buffer & 0xF0) == 0xE0) {
		if OF_UNLIKELY (length < 3)
			return 0;

		*ret = ((buffer[0] & 0x0F) << 12) | ((buffer[1] & 0x3F) << 6) |
		    (buffer[2] & 0x3F);
		return 3;
	}

	if ((*buffer & 0xF8) == 0xF0) {
		if OF_UNLIKELY (length < 4)
			return 0;

		*ret = ((buffer[0] & 0x07) << 18) | ((buffer[1] & 0x3F) << 12) |
		    ((buffer[2] & 0x3F) << 6) | (buffer[3] & 0x3F);
		return 4;
	}

	return 0;
}

size_t
of_string_position_to_index(const char *string, size_t position)
{
	size_t i, index = position;

	for (i = 0; i < position; i++)
		if OF_UNLIKELY ((string[i] & 0xC0) == 0x80)
			index--;

	return index;
}

size_t
of_string_index_to_position(const char *string, size_t index, size_t length)
{
	size_t i;

	for (i = 0; i <= index; i++)
		if OF_UNLIKELY ((string[i] & 0xC0) == 0x80)
			if (++index > length)
				return OF_INVALID_INDEX;

	return index;
}

size_t
of_unicode_string_length(const of_unichar_t *string)
{
	const of_unichar_t *string_ = string;

	while (*string_ != 0)
		string_++;
984
985
986
987
988
989
990
991
992

993
994

995
996
997
998
999
1000
1001
822
823
824
825
826
827
828


829
830

831
832
833
834
835
836
837
838







-
-
+

-
+







	OFObject *object;

	object = [[[OFObject alloc] init] autorelease];
	UTF8String = [object allocMemoryWithSize: (length * 4) + 1];

	for (i = 0; i < length; i++) {
		char buffer[4];
		size_t characterLen = of_string_unicode_to_utf8(
		    unicodeString[i], buffer);
		size_t len = of_string_utf8_encode(unicodeString[i], buffer);

		switch (characterLen) {
		switch (len) {
		case 1:
			UTF8String[j++] = buffer[0];
			break;
		case 2:
			UTF8StringLength++;

			memcpy(UTF8String + j, buffer, 2);
1054
1055
1056
1057
1058
1059
1060
1061
1062

1063
1064

1065
1066
1067
1068

1069
1070
1071
1072
1073
1074
1075
891
892
893
894
895
896
897


898
899

900
901
902
903

904
905
906
907
908
909
910
911







-
-
+

-
+



-
+







{
	const of_unichar_t *unicodeString = [self unicodeString];
	size_t length = [self length];
	size_t i, UTF8StringLength = 0;

	for (i = 0; i < length; i++) {
		char buffer[4];
		size_t characterLen = of_string_unicode_to_utf8(
		    unicodeString[i], buffer);
		size_t len = of_string_utf8_encode(unicodeString[i], buffer);

		if (characterLen == 0)
		if (len == 0)
			@throw [OFInvalidEncodingException
			    exceptionWithClass: [self class]];

		UTF8StringLength += characterLen;
		UTF8StringLength += len;
	}

	return UTF8StringLength;
}

- (size_t)cStringLengthWithEncoding: (of_string_encoding_t)encoding
{

Modified src/OFString_UTF8.m from [4e25f0432f] to [b3064980ae].

53
54
55
56
57
58
59































































































































































60
61
62
63
64
65
66
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225







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







			return OF_ORDERED_DESCENDING;
		if (tolower((int)first[i]) < tolower((int)second[i]))
			return OF_ORDERED_ASCENDING;
	}

	return OF_ORDERED_SAME;
}

int
of_string_utf8_check(const char *UTF8String, size_t UTF8Length, size_t *length)
{
	size_t i, tmpLength = UTF8Length;
	int isUTF8 = 0;

	for (i = 0; i < UTF8Length; i++) {
		/* No sign of UTF-8 here */
		if OF_LIKELY (!(UTF8String[i] & 0x80))
			continue;

		isUTF8 = 1;

		/* We're missing a start byte here */
		if OF_UNLIKELY (!(UTF8String[i] & 0x40))
			return -1;

		/* 2 byte sequences for code points 0 - 127 are forbidden */
		if OF_UNLIKELY ((UTF8String[i] & 0x7E) == 0x40)
			return -1;

		/* We have at minimum a 2 byte character -> check next byte */
		if OF_UNLIKELY (UTF8Length <= i + 1 ||
		    (UTF8String[i + 1] & 0xC0) != 0x80)
			return -1;

		/* Check if we have at minimum a 3 byte character */
		if OF_LIKELY (!(UTF8String[i] & 0x20)) {
			i++;
			tmpLength--;
			continue;
		}

		/* We have at minimum a 3 byte char -> check second next byte */
		if OF_UNLIKELY (UTF8Length <= i + 2 ||
		    (UTF8String[i + 2] & 0xC0) != 0x80)
			return -1;

		/* Check if we have a 4 byte character */
		if OF_LIKELY (!(UTF8String[i] & 0x10)) {
			i += 2;
			tmpLength -= 2;
			continue;
		}

		/* We have a 4 byte character -> check third next byte */
		if OF_UNLIKELY (UTF8Length <= i + 3 ||
		    (UTF8String[i + 3] & 0xC0) != 0x80)
			return -1;

		/*
		 * Just in case, check if there's a 5th character, which is
		 * forbidden by UTF-8
		 */
		if OF_UNLIKELY (UTF8String[i] & 0x08)
			return -1;

		i += 3;
		tmpLength -= 3;
	}

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

	return isUTF8;
}

size_t
of_string_utf8_encode(of_unichar_t character, char *buffer)
{
	size_t i = 0;

	if (character < 0x80) {
		buffer[i] = character;
		return 1;
	} else if (character < 0x800) {
		buffer[i++] = 0xC0 | (character >> 6);
		buffer[i] = 0x80 | (character & 0x3F);
		return 2;
	} else if (character < 0x10000) {
		buffer[i++] = 0xE0 | (character >> 12);
		buffer[i++] = 0x80 | (character >> 6 & 0x3F);
		buffer[i] = 0x80 | (character & 0x3F);
		return 3;
	} else if (character < 0x110000) {
		buffer[i++] = 0xF0 | (character >> 18);
		buffer[i++] = 0x80 | (character >> 12 & 0x3F);
		buffer[i++] = 0x80 | (character >> 6 & 0x3F);
		buffer[i] = 0x80 | (character & 0x3F);
		return 4;
	}

	return 0;
}

size_t
of_string_utf8_decode(const char *buffer_, size_t length, of_unichar_t *ret)
{
	const uint8_t *buffer = (const uint8_t*)buffer_;

	if (!(*buffer & 0x80)) {
		*ret = buffer[0];
		return 1;
	}

	if ((*buffer & 0xE0) == 0xC0) {
		if OF_UNLIKELY (length < 2)
			return 0;

		*ret = ((buffer[0] & 0x1F) << 6) | (buffer[1] & 0x3F);
		return 2;
	}

	if ((*buffer & 0xF0) == 0xE0) {
		if OF_UNLIKELY (length < 3)
			return 0;

		*ret = ((buffer[0] & 0x0F) << 12) | ((buffer[1] & 0x3F) << 6) |
		    (buffer[2] & 0x3F);
		return 3;
	}

	if ((*buffer & 0xF8) == 0xF0) {
		if OF_UNLIKELY (length < 4)
			return 0;

		*ret = ((buffer[0] & 0x07) << 18) | ((buffer[1] & 0x3F) << 12) |
		    ((buffer[2] & 0x3F) << 6) | (buffer[3] & 0x3F);
		return 4;
	}

	return 0;
}

size_t
of_string_utf8_get_index(const char *string, size_t position)
{
	size_t i, index = position;

	for (i = 0; i < position; i++)
		if OF_UNLIKELY ((string[i] & 0xC0) == 0x80)
			index--;

	return index;
}

size_t
of_string_utf8_get_position(const char *string, size_t index, size_t length)
{
	size_t i;

	for (i = 0; i <= index; i++)
		if OF_UNLIKELY ((string[i] & 0xC0) == 0x80)
			if (++index > length)
				return OF_INVALID_INDEX;

	return index;
}

@implementation OFString_UTF8
- init
{
	self = [super init];

	@try {
90
91
92
93
94
95
96
97

98
99
100
101
102
103
104
249
250
251
252
253
254
255

256
257
258
259
260
261
262
263







-
+







		}

		s = &s_store;

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

		switch (of_string_check_utf8(UTF8String, UTF8StringLength,
		switch (of_string_utf8_check(UTF8String, UTF8StringLength,
		    &s->length)) {
		case 1:
			s->UTF8 = YES;
			break;
		case -1:
			@throw [OFInvalidEncodingException
			    exceptionWithClass: [self class]];
133
134
135
136
137
138
139
140

141
142
143
144
145
146
147
292
293
294
295
296
297
298

299
300
301
302
303
304
305
306







-
+







		s = &s_store;

		s->cString = [self allocMemoryWithSize: cStringLength + 1];
		s->cStringLength = cStringLength;

		if (encoding == OF_STRING_ENCODING_UTF_8 ||
		    encoding == OF_STRING_ENCODING_ASCII) {
			switch (of_string_check_utf8(cString, cStringLength,
			switch (of_string_utf8_check(cString, cStringLength,
			    &s->length)) {
			case 1:
				if (encoding == OF_STRING_ENCODING_ASCII)
					@throw [OFInvalidEncodingException
					    exceptionWithClass: [self class]];

				s->UTF8 = YES;
167
168
169
170
171
172
173
174

175
176
177
178
179
180
181
326
327
328
329
330
331
332

333
334
335
336
337
338
339
340







-
+








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

				s->UTF8 = YES;
				bytes = of_string_unicode_to_utf8(
				bytes = of_string_utf8_encode(
				    (uint8_t)cString[i], buffer);

				if (bytes == 0)
					@throw [OFInvalidEncodingException
					    exceptionWithClass: [self class]];

				s->cStringLength += bytes - 1;
217
218
219
220
221
222
223
224

225
226
227
228
229
230
231
376
377
378
379
380
381
382

383
384
385
386
387
388
389
390







-
+







			character = table[(uint8_t)cString[i]];

			if (character == 0xFFFD)
				@throw [OFInvalidEncodingException
				    exceptionWithClass: [self class]];

			s->UTF8 = YES;
			characterBytes = of_string_unicode_to_utf8(character,
			characterBytes = of_string_utf8_encode(character,
			    buffer);

			if (characterBytes == 0)
				@throw [OFInvalidEncodingException
				    exceptionWithClass: [self class]];

			s->cStringLength += characterBytes - 1;
263
264
265
266
267
268
269
270

271
272
273
274
275
276
277
422
423
424
425
426
427
428

429
430
431
432
433
434
435
436







-
+







		}

		s = &s_store;

		s->cString = (char*)UTF8String;
		s->cStringLength = UTF8StringLength;

		switch (of_string_check_utf8(UTF8String, UTF8StringLength,
		switch (of_string_utf8_check(UTF8String, UTF8StringLength,
		    &s->length)) {
		case 1:
			s->UTF8 = YES;
			break;
		case -1:
			@throw [OFInvalidEncodingException
			    exceptionWithClass: [self class]];
335
336
337
338
339
340
341
342

343
344
345
346
347
348
349
494
495
496
497
498
499
500

501
502
503
504
505
506
507
508







-
+








		s->cStringLength = length;
		s->cString = [self allocMemoryWithSize: (length * 4) + 1];
		s->length = length;

		for (i = 0; i < length; i++) {
			char buffer[4];
			size_t characterLen = of_string_unicode_to_utf8(
			size_t characterLen = of_string_utf8_encode(
			    (swap ? of_bswap32(string[i]) : string[i]),
			    buffer);

			switch (characterLen) {
			case 1:
				s->cString[j++] = buffer[0];
				break;
444
445
446
447
448
449
450
451

452
453
454
455
456
457
458
459
603
604
605
606
607
608
609

610

611
612
613
614
615
616
617







-
+
-







				    (nextCharacter & 0x3FF)) + 0x10000;

				i++;
				s->cStringLength--;
				s->length--;
			}

			characterLen = of_string_unicode_to_utf8(
			characterLen = of_string_utf8_encode(character, buffer);
			    character, buffer);

			switch (characterLen) {
			case 1:
				s->cString[j++] = buffer[0];
				break;
			case 2:
				s->UTF8 = YES;
521
522
523
524
525
526
527
528

529
530
531
532
533
534
535
679
680
681
682
683
684
685

686
687
688
689
690
691
692
693







-
+







		    arguments)) == -1)
			@throw [OFInvalidFormatException
			    exceptionWithClass: [self class]];

		s->cStringLength = cStringLength;

		@try {
			switch (of_string_check_utf8(tmp, cStringLength,
			switch (of_string_utf8_check(tmp, cStringLength,
			    &s->length)) {
			case 1:
				s->UTF8 = YES;
				break;
			case -1:
				@throw [OFInvalidEncodingException
				    exceptionWithClass: [self class]];
764
765
766
767
768
769
770
771

772
773

774
775
776
777
778
779
780
922
923
924
925
926
927
928

929
930

931
932
933
934
935
936
937
938







-
+

-
+








	i = j = 0;

	while (i < s->cStringLength && j < otherCStringLength) {
		of_unichar_t c1, c2;
		size_t l1, l2;

		l1 = of_string_utf8_to_unicode(s->cString + i,
		l1 = of_string_utf8_decode(s->cString + i,
		    s->cStringLength - i, &c1);
		l2 = of_string_utf8_to_unicode(otherCString + j,
		l2 = of_string_utf8_decode(otherCString + j,
		    otherCStringLength - j, &c2);

		if (l1 == 0 || l2 == 0 || c1 > 0x10FFFF || c2 > 0x10FFFF)
			@throw [OFInvalidEncodingException
			    exceptionWithClass: [self class]];

		if (c1 >> 8 < OF_UNICODE_CASEFOLDING_TABLE_SIZE) {
820
821
822
823
824
825
826
827

828
829
830
831
832
833
834
978
979
980
981
982
983
984

985
986
987
988
989
990
991
992







-
+








	OF_HASH_INIT(hash);

	for (i = 0; i < s->cStringLength; i++) {
		of_unichar_t c;
		size_t length;

		if ((length = of_string_utf8_to_unicode(s->cString + i,
		if ((length = of_string_utf8_decode(s->cString + i,
		    s->cStringLength - i, &c)) == 0)
			@throw [OFInvalidEncodingException
			    exceptionWithClass: [self class]];

		OF_HASH_ADD(hash, (c & 0xFF0000) >> 16);
		OF_HASH_ADD(hash, (c & 0x00FF00) >>  8);
		OF_HASH_ADD(hash,  c & 0x0000FF);
850
851
852
853
854
855
856
857

858
859
860
861


862
863
864
865
866
867
868
1008
1009
1010
1011
1012
1013
1014

1015
1016
1017


1018
1019
1020
1021
1022
1023
1024
1025
1026







-
+


-
-
+
+








	if (index >= s->length)
		@throw [OFOutOfRangeException exceptionWithClass: [self class]];

	if (!s->UTF8)
		return s->cString[index];

	index = of_string_index_to_position(s->cString, index,
	index = of_string_utf8_get_position(s->cString, index,
	    s->cStringLength);

	if (!of_string_utf8_to_unicode(s->cString + index,
	    s->cStringLength - index, &character))
	if (!of_string_utf8_decode(s->cString + index, s->cStringLength - index,
	    &character))
		@throw [OFInvalidEncodingException
		    exceptionWithClass: [self class]];

	return character;
}

- (void)getCharacters: (of_unichar_t*)buffer
890
891
892
893
894
895
896
897

898
899
900
901
902
903
904
905
906
907
908

909
910
911
912
913
914
915
1048
1049
1050
1051
1052
1053
1054

1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065

1066
1067
1068
1069
1070
1071
1072
1073







-
+










-
+







	if (cStringLength > s->cStringLength)
		return of_range(OF_INVALID_INDEX, 0);

	if (options & OF_STRING_SEARCH_BACKWARDS) {
		for (i = s->cStringLength - cStringLength;; i--) {
			if (!memcmp(s->cString + i, cString, cStringLength))
				return of_range(
				    of_string_position_to_index(s->cString, i),
				    of_string_utf8_get_index(s->cString, i),
				    [string length]);

			/* Did not match and we're at the last char */
			if (i == 0)
				return of_range(OF_INVALID_INDEX, 0);
		}
	} else {
		for (i = 0; i <= s->cStringLength - cStringLength; i++)
			if (!memcmp(s->cString + i, cString, cStringLength))
				return of_range(
				    of_string_position_to_index(s->cString, i),
				    of_string_utf8_get_index(s->cString, i),
				    [string length]);
	}

	return of_range(OF_INVALID_INDEX, 0);
}

- (BOOL)containsString: (OFString*)string
935
936
937
938
939
940
941
942

943
944

945
946
947
948
949
950
951
1093
1094
1095
1096
1097
1098
1099

1100
1101

1102
1103
1104
1105
1106
1107
1108
1109







-
+

-
+







	size_t start = range.start;
	size_t end = range.start + range.length;

	if (end > s->length)
		@throw [OFOutOfRangeException exceptionWithClass: [self class]];

	if (s->UTF8) {
		start = of_string_index_to_position(s->cString, start,
		start = of_string_utf8_get_position(s->cString, start,
		    s->cStringLength);
		end = of_string_index_to_position(s->cString, end,
		end = of_string_utf8_get_position(s->cString, end,
		    s->cStringLength);
	}

	return [OFString stringWithUTF8String: s->cString + start
				       length: end - start];
}

1146
1147
1148
1149
1150
1151
1152
1153

1154
1155
1156
1157
1158
1159
1160
1304
1305
1306
1307
1308
1309
1310

1311
1312
1313
1314
1315
1316
1317
1318







-
+







	i = 0;
	j = 0;

	while (i < s->cStringLength) {
		of_unichar_t c;
		size_t cLen;

		cLen = of_string_utf8_to_unicode(s->cString + i,
		cLen = of_string_utf8_decode(s->cString + i,
		    s->cStringLength - i, &c);

		if (cLen == 0 || c > 0x10FFFF)
			@throw [OFInvalidEncodingException
			    exceptionWithClass: [self class]];

		ret[j++] = c;