-
Notifications
You must be signed in to change notification settings - Fork 0
/
uintvar.c
42 lines (33 loc) · 889 Bytes
/
uintvar.c
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
#include <string.h>
#include "uintvar.h"
int uintvar_pack(void *buf, long len, unsigned long value)
{
int i, packlen, pos;
unsigned char pack[UINTVAR32_LEN];
pack[UINTVAR32_LEN-1] = value & 0x7f;
value >>= 7;
for( i = UINTVAR32_LEN-2 ; value > 0 && i >= 0 ; i-- ) {
pack[i] = 0x80 | (value & 0x7f);
value >>= 7;
}
pos = i + 1;
packlen = UINTVAR32_LEN - pos;
if( len < packlen )
return -1;
memcpy(buf, pack+pos, packlen);
return packlen;
}
int uintvar_unpack(unsigned long *value, const void *buf, long len)
{
int i;
unsigned long val = 0;
unsigned char *ptr = (unsigned char *)buf;
for( i = 0 ; i < UINTVAR32_LEN && i < len ; i++ ) {
val = (val << 7) | (ptr[i] & 0x7f);
if( !(ptr[i] & 0x80) ) {
*value = val;
return i + 1;
}
}
return -1;
}