ObjFW  Diff

Differences From Artifact [f391445888]:

To Artifact [1633386c30]:


11
12
13
14
15
16
17


18
19
20
21
22
23
24
25
26
27
28
29


30
31
32
33
34
35
36
37
38

39
40

41
42
43
44

45
46
47
48
49
50
51
52
53
54
55

56
57
58
59
60
61
62
63
64
65
66

67
68
69
70
71
72

73

74
75
76


77
78
79
80
81
82
83

#import "config.h"

#import <stdlib.h>
#import <string.h>
#import <ctype.h>



#import "OFString.h"
#import "OFExceptions.h"
#import "OFMacros.h"

static OF_INLINE int
check_utf8(const char *str, size_t len)
{
	size_t i;
	BOOL utf8;

	utf8 = NO;



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

		utf8 = YES;

		/* We're missing a start byte here */
		if (OF_UNLIKELY(~str[i] & 0x40))

			return -1;


		/* We have at minimum a 2 byte character -> check next byte */
		if (OF_UNLIKELY(len < i + 1 || ~str[i + 1] & 0x80 ||
		    str[i + 1] & 0x40))
			return -1;


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

		/* We have at minimum a 3 byte char -> check second next byte */
		if (OF_UNLIKELY(len < i + 2 || ~str[i + 2] & 0x80 ||
		    str[i + 2] & 0x40))
			return -1;


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

		/* We have a 4 byte character -> check third next byte */
		if (OF_UNLIKELY(len < i + 3 || ~str[i + 3] & 0x80 ||
		    str[i + 3] & 0x40))
			return -1;


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

			return -1;


		i += 3;
	}



	return (utf8 ? 1 : 0);
}

@implementation OFString
+ new
{







>
>












>
>








|
>

|
>

|
|

>








|
|

>








|
|

>





|
>

>



>
>







11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96

#import "config.h"

#import <stdlib.h>
#import <string.h>
#import <ctype.h>

#import <sys/mman.h>

#import "OFString.h"
#import "OFExceptions.h"
#import "OFMacros.h"

static OF_INLINE int
check_utf8(const char *str, size_t len)
{
	size_t i;
	BOOL utf8;

	utf8 = NO;

	madvise((void*)str, len, MADV_SEQUENTIAL);

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

		utf8 = YES;

		/* We're missing a start byte here */
		if (OF_UNLIKELY(~str[i] & 0x40)) {
			madvise((void*)str, len, MADV_NORMAL);
			return -1;
		}

		/* We have at minimum a 2 byte character -> check next byte */
		if (OF_UNLIKELY(len < i + 1 || (str[i + 1] & 0xC0) != 0x80)) {
			madvise((void*)str, len, MADV_NORMAL);
			return -1;
		}

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

		/* We have at minimum a 3 byte char -> check second next byte */
		if (OF_UNLIKELY(len < i + 2 || (str[i + 2] & 0xC0) != 0x80)) {
			madvise((void*)str, len, MADV_NORMAL);
			return -1;
		}

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

		/* We have a 4 byte character -> check third next byte */
		if (OF_UNLIKELY(len < i + 3 || (str[i + 3] & 0xC0) != 0x80)) {
			madvise((void*)str, len, MADV_NORMAL);
			return -1;
		}

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

		i += 3;
	}

	madvise((void*)str, len, MADV_NORMAL);

	return (utf8 ? 1 : 0);
}

@implementation OFString
+ new
{
184
185
186
187
188
189
190


191
192
193
194
195
196
197
198
199

200

201
202
203
204
205
206
207
208

209
210

211
212

213

214
215
216
217
218
219
220
221
222
223
224
225
226

227

228
229
230
231
232
233
234
235
236
237
238
239
240

241

242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257

258
259


260
261
262
263
264
265
266

	return self;
}

- reverse
{
	size_t i, j, len = length / 2;



	/* We reverse all bytes and restore UTF-8 later, if necessary */
	for (i = 0, j = length - 1; i < len; i++, j--) {
		string[i] ^= string[j];
		string[j] ^= string[i];
		string[i] ^= string[j];
	}

	if (!is_utf8)

		return self;


	for (i = 0; i < length; i++) {
		/* ASCII */
		if (OF_LIKELY(~string[i] & 0x80))
			continue;

		/* A start byte can't happen first as we reversed everything */
		if (OF_UNLIKELY(string[i] & 0x40))

			@throw [OFInvalidEncodingException newWithObject: self];


		/* Next byte must not be ASCII */
		if (OF_UNLIKELY(length < i + 1 || ~string[i + 1] & 0x80))

			@throw [OFInvalidEncodingException newWithObject: self];


		/* Next byte is the start byte */
		if (OF_LIKELY(string[i + 1] & 0x40)) {
			string[i] ^= string[i + 1];
			string[i + 1] ^= string[i];
			string[i] ^= string[i + 1];

			i++;
			continue;
		}

		/* Second next byte must not be ASCII */
		if (OF_UNLIKELY(length < i + 2 || ~string[i + 2] & 0x80))

			@throw [OFInvalidEncodingException newWithObject: self];


		/* Second next byte is the start byte */
		if (OF_LIKELY(string[i + 2] & 0x40)) {
			string[i] ^= string[i + 2];
			string[i + 2] ^= string[i];
			string[i] ^= string[i + 2];

			i += 2;
			continue;
		}

		/* Third next byte must not be ASCII */
		if (OF_UNLIKELY(length < i + 3 || ~string[i + 3] & 0x80))

			@throw [OFInvalidEncodingException newWithObject: self];


		/* Third next byte is the start byte */
		if (OF_LIKELY(string[i + 3] & 0x40)) {
			string[i] ^= string[i + 3];
			string[i + 3] ^= string[i];
			string[i] ^= string[i + 3];

			string[i + 1] ^= string[i + 2];
			string[i + 2] ^= string[i + 1];
			string[i + 1] ^= string[i + 2];

			i += 3;
			continue;
		}

		/* UTF-8 does not allow more than 4 bytes per character */

		@throw [OFInvalidEncodingException newWithObject: self];
	}



	return self;
}

- upper
{
	size_t i = length;







>
>








|
>

>







|
>

|
>

|
>

>












|
>

>












|
>

>
















>


>
>







197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294

	return self;
}

- reverse
{
	size_t i, j, len = length / 2;

	madvise(string, len, MADV_SEQUENTIAL);

	/* We reverse all bytes and restore UTF-8 later, if necessary */
	for (i = 0, j = length - 1; i < len; i++, j--) {
		string[i] ^= string[j];
		string[j] ^= string[i];
		string[i] ^= string[j];
	}

	if (!is_utf8) {
		madvise(string, len, MADV_NORMAL);
		return self;
	}

	for (i = 0; i < length; i++) {
		/* ASCII */
		if (OF_LIKELY(~string[i] & 0x80))
			continue;

		/* A start byte can't happen first as we reversed everything */
		if (OF_UNLIKELY(string[i] & 0x40)) {
			madvise(string, len, MADV_NORMAL);
			@throw [OFInvalidEncodingException newWithObject: self];
		}

		/* Next byte must not be ASCII */
		if (OF_UNLIKELY(length < i + 1 || ~string[i + 1] & 0x80)) {
			madvise(string, len, MADV_NORMAL);
			@throw [OFInvalidEncodingException newWithObject: self];
		}

		/* Next byte is the start byte */
		if (OF_LIKELY(string[i + 1] & 0x40)) {
			string[i] ^= string[i + 1];
			string[i + 1] ^= string[i];
			string[i] ^= string[i + 1];

			i++;
			continue;
		}

		/* Second next byte must not be ASCII */
		if (OF_UNLIKELY(length < i + 2 || ~string[i + 2] & 0x80)) {
			madvise(string, len, MADV_NORMAL);
			@throw [OFInvalidEncodingException newWithObject: self];
		}

		/* Second next byte is the start byte */
		if (OF_LIKELY(string[i + 2] & 0x40)) {
			string[i] ^= string[i + 2];
			string[i + 2] ^= string[i];
			string[i] ^= string[i + 2];

			i += 2;
			continue;
		}

		/* Third next byte must not be ASCII */
		if (OF_UNLIKELY(length < i + 3 || ~string[i + 3] & 0x80)) {
			madvise(string, len, MADV_NORMAL);
			@throw [OFInvalidEncodingException newWithObject: self];
		}

		/* Third next byte is the start byte */
		if (OF_LIKELY(string[i + 3] & 0x40)) {
			string[i] ^= string[i + 3];
			string[i + 3] ^= string[i];
			string[i] ^= string[i + 3];

			string[i + 1] ^= string[i + 2];
			string[i + 2] ^= string[i + 1];
			string[i + 1] ^= string[i + 2];

			i += 3;
			continue;
		}

		/* UTF-8 does not allow more than 4 bytes per character */
		madvise(string, len, MADV_NORMAL);
		@throw [OFInvalidEncodingException newWithObject: self];
	}

	madvise(string, len, MADV_NORMAL);

	return self;
}

- upper
{
	size_t i = length;