ObjFW  Diff

Differences From Artifact [d14fde3bb9]:

  • File src/OFDataArray+MessagePackValue.m — part of check-in [3d16a30f41] at 2013-06-22 12:12:36 on branch trunk — Rework exceptions.

    This mostly removes the argument for the class in which the exception
    occurred. As backtraces were recently added for all platforms, the
    passed class does not give any extra information on where the exception
    occurred anymore.

    This also removes a few other arguments which were not too helpful. In
    the past, the idea was to pass as many arguments as possible so that it
    is easier to find the origin of the exception. However, as backtraces
    are a much better way to find the origin, those are not useful anymore
    and just make the exception more cumbersome to use. The rule is now to
    only pass arguments that might help in recovering from the exception or
    provide information that is otherwise not easily accessible. (user: js, size: 11537) [annotate] [blame] [check-ins using]

To Artifact [660e8a746b]:


26
27
28
29
30
31
32
33

34
35
36

37
38
39
40
41
42

43
44
45
46
47
48
49

50
51
52
53
54
55
56
57
58

59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77

78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95

96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114

115
116
117
118
119
120
121
122
123

124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142

143
144
145
146
147
148
149
26
27
28
29
30
31
32

33
34
35

36
37
38
39
40
41

42
43
44
45
46
47
48

49
50
51
52
53
54
55
56
57

58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94

95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113

114
115
116
117
118
119
120
121
122

123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141

142
143
144
145
146
147
148
149







-
+


-
+





-
+






-
+








-
+


















-
+

















-
+


















-
+








-
+


















-
+







#import "OFInvalidFormatException.h"

#import "autorelease.h"
#import "macros.h"

int _OFDataArray_MessagePackValue_reference;

static size_t parse_object(const uint8_t*, size_t, id*);
static size_t parseObject(const uint8_t*, size_t, id*);

static uint16_t
read_uint16(const uint8_t *buffer)
readUInt16(const uint8_t *buffer)
{
	return ((uint16_t)buffer[0] << 8) | buffer[1];
}

static uint32_t
read_uint32(const uint8_t *buffer)
readUInt32(const uint8_t *buffer)
{
	return ((uint32_t)buffer[0] << 24) | ((uint32_t)buffer[1] << 16) |
	    ((uint32_t)buffer[2] << 8) | buffer[3];
}

static uint64_t
read_uint64(const uint8_t *buffer)
readUInt64(const uint8_t *buffer)
{
	return ((uint64_t)buffer[0] << 56) | ((uint64_t)buffer[1] << 48) |
	    ((uint64_t)buffer[2] << 40) | ((uint64_t)buffer[3] << 32) |
	    ((uint64_t)buffer[4] << 24) | ((uint64_t)buffer[5] << 16) |
	    ((uint64_t)buffer[6] << 8) | buffer[7];
}

static size_t
parse_array(const uint8_t *buffer, size_t length, id *object, size_t count)
parseArray(const uint8_t *buffer, size_t length, id *object, size_t count)
{
	void *pool;
	size_t i, pos;

	/*
	 * Don't use capacity! For data and strings, this is safe, as we can
	 * check if we still have enough bytes left. For an array however, we
	 * can't know this, as every child can be more than one byte.
	 */
	*object = [OFMutableArray array];
	pos = 0;

	for (i = 0; i < count; i++) {
		id child;
		size_t childLength;

		pool = objc_autoreleasePoolPush();

		childLength = parse_object(buffer + pos, length - pos, &child);
		childLength = parseObject(buffer + pos, length - pos, &child);
		if (childLength == 0 || child == nil) {
			objc_autoreleasePoolPop(pool);

			*object = nil;
			return 0;
		}
		pos += childLength;

		[*object addObject: child];

		objc_autoreleasePoolPop(pool);
	}

	return pos;
}

static size_t
parse_table(const uint8_t *buffer, size_t length, id *object, size_t count)
parseTable(const uint8_t *buffer, size_t length, id *object, size_t count)
{
	void *pool;
	size_t i, pos;

	/*
	 * Don't use capacity! For data and strings, this is safe, as we can
	 * check if we still have enough bytes left. For a dictionary however,
	 * we can't know this, as every key / value can be more than one byte.
	 */
	*object = [OFMutableDictionary dictionary];
	pos = 0;

	for (i = 0; i < count; i++) {
		id key, value;
		size_t keyLength, valueLength;

		pool = objc_autoreleasePoolPush();

		keyLength = parse_object(buffer + pos, length - pos, &key);
		keyLength = parseObject(buffer + pos, length - pos, &key);
		if (keyLength == 0 || key == nil) {
			objc_autoreleasePoolPop(pool);

			*object = nil;
			return 0;
		}
		pos += keyLength;

		valueLength = parse_object(buffer + pos, length - pos, &value);
		valueLength = parseObject(buffer + pos, length - pos, &value);
		if (valueLength == 0 || value == nil) {
			objc_autoreleasePoolPop(pool);

			*object = nil;
			return 0;
		}
		pos += valueLength;

		[*object setObject: value
			    forKey: key];

		objc_autoreleasePoolPop(pool);
	}

	return pos;
}

static size_t
parse_object(const uint8_t *buffer, size_t length, id *object)
parseObject(const uint8_t *buffer, size_t length, id *object)
{
	size_t i, count;
	int8_t type;
	OFDataArray *data;

	if (length < 1)
		goto error;
171
172
173
174
175
176
177
178

179
180
181
182
183

184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199

200
201
202
203
204
205

206
207
208
209
210
211

212
213
214
215
216
217
218
219
220
221
222
223
224

225
226
227
228
229
230

231
232
233
234
235
236

237
238
239
240
241
242
243
171
172
173
174
175
176
177

178
179
180
181
182

183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198

199
200
201
202
203
204

205
206
207
208
209
210

211
212
213
214
215
216
217
218
219
220
221
222
223

224
225
226
227
228
229

230
231
232
233
234
235

236
237
238
239
240
241
242
243







-
+




-
+















-
+





-
+





-
+












-
+





-
+





-
+







		    stringWithUTF8String: (const char*)buffer + 1
				  length: count];
		return count + 1;
	}

	/* fixarray */
	if ((buffer[0] & 0xF0) == 0x90)
		return parse_array(buffer + 1, length - 1, object,
		return parseArray(buffer + 1, length - 1, object,
		    buffer[0] & 0xF) + 1;

	/* fixmap */
	if ((buffer[0] & 0xF0) == 0x80)
		return parse_table(buffer + 1, length - 1, object,
		return parseTable(buffer + 1, length - 1, object,
		    buffer[0] & 0xF) + 1;

	/* Prefix byte */
	switch (*buffer) {
	/* Unsigned integers */
	case 0xCC: /* uint8 */
		if (length < 2)
			goto error;

		*object = [OFNumber numberWithUInt8: buffer[1]];
		return 2;
	case 0xCD: /* uint 16 */
		if (length < 3)
			goto error;

		*object = [OFNumber numberWithUInt16: read_uint16(buffer + 1)];
		*object = [OFNumber numberWithUInt16: readUInt16(buffer + 1)];
		return 3;
	case 0xCE: /* uint 32 */
		if (length < 5)
			goto error;

		*object = [OFNumber numberWithUInt32: read_uint32(buffer + 1)];
		*object = [OFNumber numberWithUInt32: readUInt32(buffer + 1)];
		return 5;
	case 0xCF: /* uint 64 */
		if (length < 9)
			goto error;

		*object = [OFNumber numberWithUInt64: read_uint64(buffer + 1)];
		*object = [OFNumber numberWithUInt64: readUInt64(buffer + 1)];
		return 9;
	/* Signed integers */
	case 0xD0: /* int 8 */
		if (length < 2)
			goto error;

		*object = [OFNumber numberWithInt8: buffer[1]];
		return 2;
	case 0xD1: /* int 16 */
		if (length < 3)
			goto error;

		*object = [OFNumber numberWithInt16: read_uint16(buffer + 1)];
		*object = [OFNumber numberWithInt16: readUInt16(buffer + 1)];
		return 3;
	case 0xD2: /* int 32 */
		if (length < 5)
			goto error;

		*object = [OFNumber numberWithInt32: read_uint32(buffer + 1)];
		*object = [OFNumber numberWithInt32: readUInt32(buffer + 1)];
		return 5;
	case 0xD3: /* int 64 */
		if (length < 9)
			goto error;

		*object = [OFNumber numberWithInt64: read_uint64(buffer + 1)];
		*object = [OFNumber numberWithInt64: readUInt64(buffer + 1)];
		return 9;
	/* Floating point */
	case 0xCA:; /* float 32 */
		union {
			uint8_t u8[4];
			float f;
		} f;
292
293
294
295
296
297
298
299

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

314
315
316
317
318
319
320
292
293
294
295
296
297
298

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

313
314
315
316
317
318
319
320







-
+













-
+







			    count: count];

		return count + 2;
	case 0xC5: /* bin 16 */
		if (length < 3)
			goto error;

		count = read_uint16(buffer + 1);
		count = readUInt16(buffer + 1);

		if (length < count + 3)
			goto error;

		*object = [OFDataArray dataArrayWithCapacity: count];
		[*object addItems: buffer + 3
			    count: count];

		return count + 3;
	case 0xC6: /* bin 32 */
		if (length < 5)
			goto error;

		count = read_uint32(buffer + 1);
		count = readUInt32(buffer + 1);

		if (length < count + 5)
			goto error;

		*object = [OFDataArray dataArrayWithCapacity: count];
		[*object addItems: buffer + 5
			    count: count];
345
346
347
348
349
350
351
352

353
354
355
356
357
358
359
345
346
347
348
349
350
351

352
353
354
355
356
357
358
359







-
+







		}

		return count + 3;
	case 0xC8: /* ext 16 */
		if (length < 4)
			goto error;

		count = read_uint16(buffer + 1);
		count = readUInt16(buffer + 1);

		if (length < count + 4)
			goto error;

		type = buffer[3];

		data = [[OFDataArray alloc] initWithCapacity: count];
369
370
371
372
373
374
375
376

377
378
379
380
381
382
383
369
370
371
372
373
374
375

376
377
378
379
380
381
382
383







-
+







		}

		return count + 4;
	case 0xC9: /* ext 32 */
		if (length < 6)
			goto error;

		count = read_uint32(buffer + 1);
		count = readUInt32(buffer + 1);

		if (length < count + 6)
			goto error;

		type = buffer[5];

		data = [[OFDataArray alloc] initWithCapacity: count];
501
502
503
504
505
506
507
508

509
510
511
512
513
514
515
516
517
518
519
520
521

522
523
524
525
526
527
528
529
530
531
532
533
534
535
536


537
538
539
540
541
542


543
544
545
546
547
548
549


550
551
552
553
554
555


556
557
558
559
560
561
562
563
564
565
566
567
568
569

570
571
572
573
574
575
501
502
503
504
505
506
507

508
509
510
511
512
513
514
515
516
517
518
519
520

521
522
523
524
525
526
527
528
529
530
531
532
533
534


535
536
537
538
539
540


541
542
543
544
545
546
547


548
549
550
551
552
553


554
555
556
557
558
559
560
561
562
563
564
565
566
567
568

569
570
571
572
573
574
575







-
+












-
+













-
-
+
+




-
-
+
+





-
-
+
+




-
-
+
+













-
+






		    stringWithUTF8String: (const char*)buffer + 2
				  length: count];
		return count + 2;
	case 0xDA: /* str 16 */
		if (length < 3)
			goto error;

		count = read_uint16(buffer + 1);
		count = readUInt16(buffer + 1);

		if (length < count + 3)
			goto error;

		*object = [OFString
		    stringWithUTF8String: (const char*)buffer + 3
				  length: count];
		return count + 3;
	case 0xDB: /* str 32 */
		if (length < 5)
			goto error;

		count = read_uint32(buffer + 1);
		count = readUInt32(buffer + 1);

		if (length < count + 5)
			goto error;

		*object = [OFString
		    stringWithUTF8String: (const char*)buffer + 5
				  length: count];
		return count + 5;
	/* Arrays */
	case 0xDC: /* array 16 */
		if (length < 3)
			goto error;

		return parse_array(buffer + 3, length - 3, object,
		    read_uint16(buffer + 1)) + 3;
		return parseArray(buffer + 3, length - 3, object,
		    readUInt16(buffer + 1)) + 3;
	case 0xDD: /* array 32 */
		if (length < 5)
			goto error;

		return parse_array(buffer + 5, length - 5, object,
		    read_uint32(buffer + 1)) + 5;
		return parseArray(buffer + 5, length - 5, object,
		    readUInt32(buffer + 1)) + 5;
	/* Maps */
	case 0xDE: /* map 16 */
		if (length < 3)
			goto error;

		return parse_table(buffer + 3, length - 3, object,
		    read_uint16(buffer + 1)) + 3;
		return parseTable(buffer + 3, length - 3, object,
		    readUInt16(buffer + 1)) + 3;
	case 0xDF: /* map 32 */
		if (length < 5)
			goto error;

		return parse_table(buffer + 5, length - 5, object,
		    read_uint32(buffer + 1)) + 5;
		return parseTable(buffer + 5, length - 5, object,
		    readUInt32(buffer + 1)) + 5;
	}

error:
	*object = nil;
	return 0;
}

@implementation OFDataArray (MessagePackValue)
- (id)messagePackValue
{
	size_t count = [self count];
	id object;

	if (parse_object([self items], count, &object) != count ||
	if (parseObject([self items], count, &object) != count ||
	    object == nil)
		@throw [OFInvalidFormatException exception];

	return object;
}
@end