Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MKPacketDataStream.m « src - github.com/mumble-voip/mumblekit.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a4c517f49856b8d5ac10d6f86a5a23b703acb2ac (plain)
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
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
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
// Copyright 2005-2012 The MumbleKit Developers. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

#import "MKPacketDataStream.h"

@interface MKPacketDataStream () {
    NSMutableData   *mutableData;
    NSData          *immutableData;
    unsigned char   *data;
    NSUInteger      maxSize;
    NSUInteger      offset;
    NSUInteger      overshoot;
    BOOL            ok;
}
@end

@implementation MKPacketDataStream

- (id) initWithData:(NSData *)ourContainer {
    if ((self = [super init])) {
        immutableData = ourContainer;
        [immutableData retain];
        data = (unsigned char *)[immutableData bytes];
        offset = 0;
        overshoot = 0;
        maxSize = [immutableData length];
        ok = YES;
    }
    return self;
}

- (id) initWithBuffer:(unsigned char *)buffer length:(NSUInteger)len {
    if ((self = [super init])) {
        data = buffer;
        offset = 0;
        overshoot = 0;
        maxSize = len;
        ok = YES;
    }
    return self;
}

- (void) dealloc {
    [mutableData release];
    [immutableData release];
    [super dealloc];
}

- (NSUInteger) size {
    return offset;
}

- (NSUInteger) capactiy {
    return maxSize;
}

- (NSUInteger) left {
    return maxSize - offset;
}

- (BOOL) valid {
    return ok;
}

- (void) appendValue:(uint64_t)value {
    assert(value <= 0xff);

    if (offset < maxSize)
        data[offset++] = (unsigned char)value;
    else {
        ok = NO;
        overshoot++;
    }
}

- (void) appendBytes:(unsigned char *)buffer length:(NSUInteger)len {
    if ([self left] >= len) {
        memcpy(&data[offset], buffer, len);
        offset += len;
    } else {
        NSUInteger l = [self left];
        memset(&data[offset], 0, l);
        overshoot += len - l;
        ok = NO;
    }
}

- (void) skip:(NSUInteger)amount {
    if ([self left] >= amount) {
        offset += amount;
    } else
        ok = NO;
}

- (uint64_t) next {
    if (offset < maxSize) {
        return data[offset++];
    } else {
        ok = NO;
        return 0;
    }
}

- (uint8_t) next8 {
    if (offset < maxSize) {
        return data[offset++];
    } else {
        ok = NO;
        return 0;
    }
}

- (void) rewind {
    offset = 0;
}

- (void) truncate {
    maxSize = offset;
}

- (unsigned char *) dataPtr {
    return (unsigned char *)&data[offset];
}

- (char *) charPtr {
    return (char *)&data[offset];
}

- (NSData *) data {
    return (NSData *)mutableData;
}

- (NSMutableData *) mutableData {
    return mutableData;
}

- (void) addVarint:(uint64_t)value {
    uint64_t i = value;

    if ((i & 0x8000000000000000LL) && (~i < 0x100000000LL)) {
        // Signed number.
        i = ~i;
        if (i <= 0x3) {
            // Shortcase for -1 to -4
            [self appendValue:(0xFC | i)];
        } else {
            [self appendValue:(0xF8)];
        }
    }
    if (i < 0x80) {
        // Need top bit clear
        [self appendValue:i];
    } else if (i < 0x4000) {
        // Need top two bits clear
        [self appendValue:((i >> 8) | 0x80)];
        [self appendValue:(i & 0xFF)];
    } else if (i < 0x200000) {
        // Need top three bits clear
        [self appendValue:((i >> 16) | 0xC0)];
        [self appendValue:((i >> 8) & 0xFF)];
        [self appendValue:(i & 0xFF)];
    } else if (i < 0x10000000) {
        // Need top four bits clear
        [self appendValue:((i >> 24) | 0xE0)];
        [self appendValue:((i >> 16) & 0xFF)];
        [self appendValue:((i >> 8) & 0xFF)];
        [self appendValue:(i & 0xFF)];
    } else if (i < 0x100000000LL) {
        // It's a full 32-bit integer.
        [self appendValue:(0xF0)];
        [self appendValue:((i >> 24) & 0xFF)];
        [self appendValue:((i >> 16) & 0xFF)];
        [self appendValue:((i >> 8) & 0xFF)];
        [self appendValue:(i & 0xFF)];
    } else {
        // It's a 64-bit value.
        [self appendValue:(0xF4)];
        [self appendValue:((i >> 56) & 0xFF)];
        [self appendValue:((i >> 48) & 0xFF)];
        [self appendValue:((i >> 40) & 0xFF)];
        [self appendValue:((i >> 32) & 0xFF)];
        [self appendValue:((i >> 24) & 0xFF)];
        [self appendValue:((i >> 16) & 0xFF)];
        [self appendValue:((i >> 8) & 0xFF)];
        [self appendValue:(i & 0xFF)];
    }
}

- (uint64_t) getVarint {
    uint64_t i = 0;
    uint64_t v = [self next];

    if ((v & 0x80) == 0x00) {
        i = (v & 0x7F);
    } else if ((v & 0xC0) == 0x80) {
        i = (v & 0x3F) << 8 | [self next];
    } else if ((v & 0xF0) == 0xF0) {
        switch (v & 0xFC) {
            case 0xF0:
                i=[self next] << 24 | [self next] << 16 | [self next] << 8 | [self next];
                break;
            case 0xF4:
                i = [self next] << 56 | [self next] << 48 | [self next] << 40 | [self next] << 32 | [self next] << 24 | [self next] << 16 | [self next] << 8 | [self next];
                break;
            case 0xF8:
                i = [self getVarint];
                i = ~i;
                break;
            case 0xFC:
                i = v & 0x03;
                i = ~i;
                break;
            default:
                ok = NO;
                i = 0;
                break;
        }
    } else if ((v & 0xF0) == 0xE0) {
        i = (v & 0x0F) << 24 | [self next] << 16 | [self next] << 8 | [self next];
    } else if ((v & 0xE0) == 0xC0) {
        i = (v & 0x1F) << 16 | [self next] << 8 | [self next];
    }
    return i;
}

- (unsigned int) getUnsignedInt {
    return (unsigned int) [self getVarint];
}

- (int) getInt {
    return (int) [self getVarint];
}

- (short) getShort {
    return (short) [self getVarint];
}

- (unsigned short) getUnsignedShort {
    return (unsigned short) [self getVarint];
}

- (char) getChar {
    return (char) [self getVarint];
}

- (unsigned char) getUnsignedChar {
    return (unsigned char) [self getVarint];
}

- (float) getFloat {
    float32u u;

    if ([self left] < 4) {
        ok = NO;
        return 0.0f;
    }

    u.b[0] = [self next8];
    u.b[1] = [self next8];
    u.b[2] = [self next8];
    u.b[3] = [self next8];

    return u.f;
}

- (double) getDouble {
    NSLog(@"PacketDataStream: getDouble not implemented yet.");
    return 0.0f;
}

- (NSData *) copyDataBlock:(NSUInteger)len {
    if ([self left] >= len) {
        NSData *db = [[NSData alloc] initWithBytes:[self dataPtr] length:len];
        offset += len;
        return db;
    } else {
        NSLog(@"PacketDataStream: Unable to copyDataBlock. Requsted=%lu, avail=%lu", (unsigned long)len, (unsigned long)[self left]);
        ok = NO;
        return nil;
    }
}

@end