forked from yannisroy/MYUtilities
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMYData.h
55 lines (43 loc) · 1.85 KB
/
MYData.h
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
//
// MYData.h
// MYUtilities
//
// Created by Jens Alfke on 9/13/13.
// Copyright (c) 2013 Jens Alfke. All rights reserved.
//
#import <Foundation/Foundation.h>
/** Encodes an unsigned integer of up to 64 bits as a varint.
The buffer must be large enough to hold the varint; 10 bytes is always sufficient.
See https://developers.google.com/protocol-buffers/docs/encoding#varints */
void* MYEncodeVarUInt(void* buf, UInt64 number);
/** Decodes a varint from a buffer. Returns a pointer to the byte past the end of the varint,
or NULL on failure. */
const void* MYDecodeVarUInt(const void* buf, const void* bufEnd, UInt64* outNumber);
/** Returns the number of bytes of the varint encoding of the number. */
size_t MYLengthOfVarUInt(UInt64 number);
/** A slice represents a range of addresses, without owning them. */
typedef struct {
const void* bytes;
size_t length;
} MYSlice;
static inline MYSlice MYMakeSlice(void* bytes, size_t length)
{return (MYSlice){bytes, length};}
MYSlice MYMakeSubSlice(MYSlice slice, size_t offset, size_t length);
static inline BOOL MYSliceIsEmpty(MYSlice slice)
{return slice.length == 0;}
static inline const void* MYSliceGetEnd(MYSlice slice)
{return slice.bytes + slice.length;}
static inline void MYSliceMoveStart(MYSlice *slice, size_t n)
{slice->bytes += n; slice->length -= n;}
static inline void MYSliceMoveStartTo(MYSlice *slice, void* start)
{slice->length -= start - slice->bytes; slice->bytes = start;}
BOOL MYSliceReadVarUInt(MYSlice* slice, UInt64* outResult);
BOOL MYSliceReadSlice(MYSlice* slice, size_t count, MYSlice* outResult);
@interface NSData (MYData)
+ (instancetype) my_dataWithSlice: (MYSlice)slice;
- (MYSlice) my_asSlice;
- (const void*) my_readVarUInt: (UInt64*)number at: (const void*)start;
@end
@interface NSMutableData (MYData)
- (void) my_appendVarUInt: (UInt64)number;
@end