forked from yannisroy/MYUtilities
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMYData.m
141 lines (111 loc) · 3.44 KB
/
MYData.m
1
2
3
4
5
6
7
8
9
10
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
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
//
// MYData.m
// MYUtilities
//
// Created by Jens Alfke on 9/13/13.
// Copyright (c) 2013 Jens Alfke. All rights reserved.
//
#import "MYData.h"
#import "Test.h"
void* MYEncodeVarUInt(void* buf, UInt64 number) {
UInt8* dst = buf;
do {
UInt8 byte = number & 0x7F;
number >>= 7;
if (number != 0)
byte |= 0x80;
*dst++ = byte;
} while (number != 0);
return dst;
}
const void* MYDecodeVarUInt(const void* buf, const void* bufEnd, UInt64* outNumber) {
UInt64 result = 0;
const UInt8* src = buf;
UInt8 byte;
unsigned shift = 0;
while (src < (const UInt8*)bufEnd) {
byte = *src++;
result |= (UInt64)(byte & 0x7F) << shift;
if (!(byte & 0x80)) {
*outNumber = result;
return src;
}
shift += 7;
}
return NULL;
}
size_t MYLengthOfVarUInt(UInt64 number) {
size_t length = 0;
while (number) {
length++;
number >>= 7;
}
return length;
}
@implementation NSData (MYData)
- (MYSlice) my_asSlice {
return (MYSlice){(void*)self.bytes, self.length};
}
+ (NSData*) my_dataWithSlice: (MYSlice)slice {
return [self dataWithBytes: slice.bytes length: slice.length];
}
- (const void*) my_readVarUInt: (UInt64*)outNumber at: (const void*)start {
const void *bytes = self.bytes, *end = bytes+self.length;
Assert(start >= bytes && start <= end);
return MYDecodeVarUInt(start, end, outNumber);
}
@end
@implementation NSMutableData (MYData)
- (void) my_appendVarUInt: (UInt64)number {
UInt8 buf[10];
UInt8* end = MYEncodeVarUInt(buf, number);
[self appendBytes: buf length: (end-buf)];
}
@end
MYSlice MYMakeSubSlice(MYSlice slice, size_t offset, size_t length) {
if (offset > slice.length) {
offset = slice.length;
length = 0;
} else if (offset + length > slice.length) {
length = slice.length - offset;
}
return (MYSlice){slice.bytes + offset, length};
}
BOOL MYSliceReadVarUInt(MYSlice* slice, UInt64* outResult) {
const void* next = MYDecodeVarUInt(slice->bytes, MYSliceGetEnd(*slice), outResult);
if (!next)
return NO;
MYSliceMoveStartTo(slice, (void*)next);
return YES;
}
BOOL MYSliceReadSlice(MYSlice* slice, size_t count, MYSlice* outResult) {
if (count > slice->length)
return NO;
*outResult = (MYSlice){slice->bytes, count};
MYSliceMoveStart(slice, count);
return YES;
}
TestCase(VarInt) {
struct {UInt64 number; unsigned len; UInt8 encoded[12];} tests[] = {
{0x0000, 1, {0x00}},
{0x0013, 1, {0x13}},
{0x007F, 1, {0x7F}},
{0x0080, 2, {0x80, 0x01}},
{0x1234, 2, {0xB4, 0x24}},
{0xFFFF, 3, {0xFF, 0xFF, 0x03}},
{0x12345678, 5, {0xF8, 0xAC, 0xD1, 0x91, 0x01}},
{0x123456789, 5, {0x89, 0xCF, 0x95, 0x9A, 0x12}},
{0x123456787654321, 9, {0xA1, 0x86, 0x95, 0xBB, 0xF8, 0xAC, 0xD1, 0x91, 0x01}},
};
UInt8 buf[12];
for (int i=0; i<sizeof(tests)/sizeof(*tests); i++) {
UInt8* dst = MYEncodeVarUInt(buf, tests[i].number);
Log(@"VarInt encoded 0x%llx --> %@", tests[i].number, [NSData dataWithBytes: buf length: dst-buf]);
CAssertEq(dst-buf, tests[i].len);
CAssert(memcmp(buf, tests[i].encoded, tests[i].len) == 0);
UInt64 n;
const void* end = MYDecodeVarUInt(buf, &buf[12], &n);
CAssertEq(n, tests[i].number);
CAssertEq((const UInt8*)end-buf, tests[i].len);
}
}