57 lines
830 B
C
57 lines
830 B
C
#include "axl_vlq.h"
|
|
|
|
u32 axl_vlq_encode(u32 num, u8* out)
|
|
{
|
|
u8 buf[5];
|
|
int pos = 0;
|
|
|
|
if(num == 0)
|
|
{
|
|
out[0] = 0;
|
|
return 1;
|
|
}
|
|
|
|
while(num > 0)
|
|
{
|
|
buf[pos] = num & 0x7F;
|
|
num >>= 7;
|
|
pos++;
|
|
}
|
|
|
|
for(int j = pos - 1; j > 0; j--)
|
|
{
|
|
buf[j] |= 0x80;
|
|
}
|
|
|
|
for(int j = pos - 1; j >= 0; j--)
|
|
{
|
|
*out++ = buf[j];
|
|
}
|
|
|
|
return pos;
|
|
}
|
|
|
|
u32 axl_vlq_decode(const u8* in, u32* num)
|
|
{
|
|
*num = 0;
|
|
u32 pos = 0;
|
|
|
|
while(pos < AXL_VLQ_MAX_LEN)
|
|
{
|
|
u8 b = in[pos++];
|
|
|
|
*num = (*num << 7) | (b & 0x7F);
|
|
|
|
if(!(b & 0x80))
|
|
{
|
|
return pos;
|
|
}
|
|
|
|
if(*num > (U32_MAX >> 7))
|
|
{
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
return 0; //incomplete
|
|
}
|