Skip to content

Commit 6ae274f

Browse files
committed
Varint in blob
1 parent b5333e8 commit 6ae274f

File tree

1 file changed

+47
-12
lines changed

1 file changed

+47
-12
lines changed

src/esp32.cpp

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,33 @@ extern "C" {
3131
}
3232
}
3333

34+
// From https://stackoverflow.com/questions/19758270/read-varint-from-linux-sockets#19760246
35+
// Encode an unsigned 64-bit varint. Returns number of encoded bytes.
36+
// 'buffer' must have room for up to 10 bytes.
37+
int encode_unsigned_varint(uint8_t *buffer, uint64_t value) {
38+
int encoded = 0;
39+
do {
40+
uint8_t next_byte = value & 0x7F;
41+
value >>= 7;
42+
if (value)
43+
next_byte |= 0x80;
44+
buffer[encoded++] = next_byte;
45+
} while (value);
46+
return encoded;
47+
}
48+
49+
uint64_t decode_unsigned_varint(const uint8_t *data, int &decoded_bytes) {
50+
int i = 0;
51+
uint64_t decoded_value = 0;
52+
int shift_amount = 0;
53+
do {
54+
decoded_value |= (uint64_t)(data[i] & 0x7F) << shift_amount;
55+
shift_amount += 7;
56+
} while ((data[i++] & 0x80) != 0);
57+
decoded_bytes = i;
58+
return decoded_value;
59+
}
60+
3461
int esp32_Close(sqlite3_file*);
3562
int esp32_Lock(sqlite3_file *, int);
3663
int esp32_Unlock(sqlite3_file*, int);
@@ -627,36 +654,44 @@ static void shox96_0_2c(sqlite3_context *context, int argc, sqlite3_value **argv
627654
long int nOut2;
628655
const unsigned char *inBuf;
629656
unsigned char *outBuf;
657+
unsigned char vInt[9];
658+
int vIntLen;
659+
630660
assert( argc==1 );
631661
nIn = sqlite3_value_bytes(argv[0]);
632662
inBuf = (unsigned char *) sqlite3_value_blob(argv[0]);
633663
nOut = 13 + nIn + (nIn+999)/1000;
634-
outBuf = (unsigned char *) malloc( nOut+4 );
635-
outBuf[0] = nIn>>24 & 0xff;
636-
outBuf[1] = nIn>>16 & 0xff;
637-
outBuf[2] = nIn>>8 & 0xff;
638-
outBuf[3] = nIn & 0xff;
639-
//nOut2 = (long int)nOut;
640-
nOut2 = shox96_0_2_0_compress((const char *) inBuf, nIn, (char *) &outBuf[4], NULL);
641-
sqlite3_result_blob(context, outBuf, nOut2+4, free);
664+
vIntLen = encode_unsigned_varint(vInt, (uint64_t) nIn);
665+
666+
outBuf = (unsigned char *) malloc( nOut+vIntLen );
667+
memcpy(outBuf, vInt, vIntLen);
668+
nOut2 = shox96_0_2_0_compress((const char *) inBuf, nIn, (char *) &outBuf[vIntLen], NULL);
669+
sqlite3_result_blob(context, outBuf, nOut2+vIntLen, free);
642670
}
643671

644672
static void shox96_0_2d(sqlite3_context *context, int argc, sqlite3_value **argv) {
645673
unsigned int nIn, nOut, rc;
646674
const unsigned char *inBuf;
647675
unsigned char *outBuf;
648676
long int nOut2;
649-
677+
uint64_t inBufLen64;
678+
int vIntLen;
679+
650680
assert( argc==1 );
681+
682+
if (sqlite3_value_type(argv[0]) != SQLITE_BLOB)
683+
return;
684+
651685
nIn = sqlite3_value_bytes(argv[0]);
652-
if( nIn<=4 ){
686+
if (nIn < 2){
653687
return;
654688
}
655689
inBuf = (unsigned char *) sqlite3_value_blob(argv[0]);
656-
nOut = (inBuf[0]<<24) + (inBuf[1]<<16) + (inBuf[2]<<8) + inBuf[3];
690+
inBufLen64 = decode_unsigned_varint(inBuf, vIntLen);
691+
nOut = (unsigned int) inBufLen64;
657692
outBuf = (unsigned char *) malloc( nOut );
658693
//nOut2 = (long int)nOut;
659-
nOut2 = shox96_0_2_0_decompress((const char *) &inBuf[4], nIn - 4, (char *) outBuf, NULL);
694+
nOut2 = shox96_0_2_0_decompress((const char *) (inBuf + vIntLen), nIn - vIntLen, (char *) outBuf, NULL);
660695
//if( rc!=Z_OK ){
661696
// free(outBuf);
662697
//}else{

0 commit comments

Comments
 (0)