2016-11-02 16:27:58 +00:00
|
|
|
#ifndef VARHEXUTILS
|
|
|
|
#define VARHEXUTILS
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include "varint.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "endian.h"
|
|
|
|
/*uint8_t * encode_big_endian_32(uint32_t ebex32)
|
|
|
|
{
|
|
|
|
uint8_t encbe[10] = {0};
|
|
|
|
memcpy(encbe, htobe32(ebex32));
|
|
|
|
return encbe;
|
|
|
|
}*/
|
2016-11-08 11:02:45 +00:00
|
|
|
int8_t Var_Bytes_Count(uint8_t * countbytesofthis)
|
2016-11-02 16:27:58 +00:00
|
|
|
{
|
|
|
|
static int8_t xrzk_bytescnt = 0;
|
|
|
|
for(int8_t i=0; i<10; i++)
|
|
|
|
{
|
|
|
|
if(countbytesofthis[i] != 0)
|
|
|
|
{
|
|
|
|
xrzk_bytescnt++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return xrzk_bytescnt;
|
|
|
|
}
|
|
|
|
uint8_t * Num_To_Varint_64(uint64_t TOV64INPUT) //UINT64_T TO VARINT
|
|
|
|
{
|
2016-11-08 11:02:45 +00:00
|
|
|
static uint8_t buffy_001[60] = {0};
|
|
|
|
uvarint_encode64(TOV64INPUT, buffy_001, 60);
|
2016-11-02 16:27:58 +00:00
|
|
|
return buffy_001;
|
|
|
|
}
|
|
|
|
uint8_t * Num_To_Varint_32(uint32_t TOV32INPUT) // UINT32_T TO VARINT
|
|
|
|
{
|
2016-11-08 11:02:45 +00:00
|
|
|
static uint8_t buffy_032[60] = {0};
|
|
|
|
uvarint_encode32(TOV32INPUT, buffy_032, 60);
|
2016-11-02 16:27:58 +00:00
|
|
|
return buffy_032;
|
|
|
|
}
|
2016-11-08 11:02:45 +00:00
|
|
|
uint64_t * Varint_To_Num_64(uint8_t TON64INPUT[60]) //VARINT TO UINT64_t
|
2016-11-02 16:27:58 +00:00
|
|
|
{
|
|
|
|
static uint64_t varintdecode_001 = 0;
|
2016-11-08 11:02:45 +00:00
|
|
|
uvarint_decode64(TON64INPUT, 60, &varintdecode_001);
|
2016-11-02 16:27:58 +00:00
|
|
|
return &varintdecode_001;
|
|
|
|
}
|
2016-11-08 11:02:45 +00:00
|
|
|
uint32_t * Varint_To_Num_32(uint8_t TON32INPUT[60]) //VARINT TO UINT32_t
|
2016-11-02 16:27:58 +00:00
|
|
|
{
|
|
|
|
static uint32_t varintdecode_032 = 0;
|
2016-11-08 11:02:45 +00:00
|
|
|
uvarint_decode32(TON32INPUT, 60, &varintdecode_032);
|
2016-11-02 16:27:58 +00:00
|
|
|
return &varintdecode_032;
|
|
|
|
}
|
|
|
|
//
|
|
|
|
char * Int_To_Hex(uint64_t int2hex) //VAR[binformat] TO HEX
|
|
|
|
{
|
2016-11-08 16:01:10 +00:00
|
|
|
static char int2hex_result[800]="\0";
|
2016-11-02 16:27:58 +00:00
|
|
|
memset(int2hex_result,0,sizeof(int2hex_result));
|
|
|
|
sprintf (int2hex_result, "%02lX", int2hex);
|
|
|
|
return int2hex_result;
|
|
|
|
}
|
2016-11-08 11:02:45 +00:00
|
|
|
uint64_t Hex_To_Int(char * hax)
|
|
|
|
{
|
|
|
|
char * hex = NULL;
|
|
|
|
hex=hax;
|
2016-11-06 01:57:36 +00:00
|
|
|
uint64_t val = 0;
|
|
|
|
while (*hex)
|
|
|
|
{
|
2016-11-08 11:02:45 +00:00
|
|
|
// get current character then increment
|
|
|
|
uint8_t byte = *hex++;
|
|
|
|
// transform hex character to the 4bit equivalent number, using the ascii table indexes
|
|
|
|
if (byte >= '0' && byte <= '9') byte = byte - '0';
|
|
|
|
else if (byte >= 'a' && byte <='f') byte = byte - 'a' + 10;
|
|
|
|
else if (byte >= 'A' && byte <='F') byte = byte - 'A' + 10;
|
|
|
|
// shift 4 to make space for new digit, and add the 4 bits of the new digit
|
|
|
|
val = (val << 4) | (byte & 0xF);
|
|
|
|
}
|
|
|
|
return val;
|
2016-11-06 01:57:36 +00:00
|
|
|
}
|
2016-11-02 16:27:58 +00:00
|
|
|
//
|
2016-11-08 11:02:45 +00:00
|
|
|
void vthconvert(int size, char * crrz01, uint8_t * xbuf)
|
2016-11-02 16:27:58 +00:00
|
|
|
{
|
2016-11-08 16:01:10 +00:00
|
|
|
uint8_t buf[400];
|
|
|
|
bzero(buf,400);
|
2016-11-08 11:02:45 +00:00
|
|
|
|
|
|
|
//fixing the buf
|
|
|
|
for(int cz=0; cz<size;cz++)
|
2016-11-06 01:57:36 +00:00
|
|
|
{
|
2016-11-08 11:02:45 +00:00
|
|
|
buf[cz] = xbuf[cz];
|
|
|
|
}
|
|
|
|
//
|
|
|
|
if(crrz01!=NULL)
|
2016-11-02 16:27:58 +00:00
|
|
|
{
|
2016-11-08 11:02:45 +00:00
|
|
|
char * crrz1 = NULL;
|
|
|
|
crrz1 = crrz01;
|
2016-11-08 16:01:10 +00:00
|
|
|
char conv_proc[800]="\0";
|
2016-11-08 11:02:45 +00:00
|
|
|
int i;
|
|
|
|
for(i=0; i < (size*2); i++)
|
2016-11-02 16:27:58 +00:00
|
|
|
{
|
2016-11-08 11:02:45 +00:00
|
|
|
if(buf[i]!='\0')
|
2016-11-06 01:57:36 +00:00
|
|
|
{
|
|
|
|
sprintf (conv_proc, "%02X", buf[i]);
|
|
|
|
//printf("%d:%d\n",i, buf[i]);
|
2016-11-08 11:02:45 +00:00
|
|
|
strcat(crrz1, conv_proc);
|
2016-11-06 01:57:36 +00:00
|
|
|
}
|
2016-11-02 16:27:58 +00:00
|
|
|
}
|
2016-11-08 11:02:45 +00:00
|
|
|
crrz1 = NULL;
|
2016-11-06 01:57:36 +00:00
|
|
|
}
|
2016-11-02 16:27:58 +00:00
|
|
|
}
|
2016-11-08 11:02:45 +00:00
|
|
|
char * Var_To_Hex(int realsize, uint8_t * TOHEXINPUT) //VAR[binformat] TO HEX
|
2016-11-02 16:27:58 +00:00
|
|
|
{
|
2016-11-08 16:01:10 +00:00
|
|
|
for(int ix=realsize;ix<400;ix++)
|
2016-11-08 11:02:45 +00:00
|
|
|
{
|
|
|
|
TOHEXINPUT[ix] = '\0';
|
|
|
|
}
|
|
|
|
if(TOHEXINPUT != NULL)
|
2016-11-06 01:57:36 +00:00
|
|
|
{
|
2016-11-08 16:01:10 +00:00
|
|
|
static char convert_resultz1[800]="\0";
|
|
|
|
bzero(convert_resultz1,800);
|
2016-11-08 11:02:45 +00:00
|
|
|
vthconvert(realsize, convert_resultz1, TOHEXINPUT);
|
|
|
|
return convert_resultz1;
|
2016-11-06 01:57:36 +00:00
|
|
|
}
|
2016-11-08 11:02:45 +00:00
|
|
|
}
|
|
|
|
uint8_t * Hex_To_Var(char * Hexstr) //HEX TO VAR[BINFORMAT]
|
|
|
|
{
|
2016-11-08 16:01:10 +00:00
|
|
|
static uint8_t buffy_HEX[400] = {0};
|
|
|
|
bzero(buffy_HEX,400);
|
2016-11-08 11:02:45 +00:00
|
|
|
int i;
|
2016-11-08 16:01:10 +00:00
|
|
|
char codo[800] = "\0";
|
|
|
|
bzero(codo,800);
|
2016-11-02 16:27:58 +00:00
|
|
|
strcpy(codo, Hexstr);
|
2016-11-08 11:02:45 +00:00
|
|
|
char code[3];
|
|
|
|
bzero(code,3);
|
|
|
|
code[3]='\0';
|
2016-11-02 16:27:58 +00:00
|
|
|
int x = 0;
|
2016-11-06 01:57:36 +00:00
|
|
|
int fori001=0;
|
2016-11-08 16:01:10 +00:00
|
|
|
for(fori001=0;fori001<800;fori001++)
|
2016-11-02 16:27:58 +00:00
|
|
|
{
|
2016-11-06 01:57:36 +00:00
|
|
|
strncpy(&code[0],&codo[fori001],1);
|
|
|
|
strncpy(&code[1],&codo[fori001+1],1);
|
2016-11-02 16:27:58 +00:00
|
|
|
char *ck = NULL;
|
|
|
|
uint64_t lu = 0;
|
|
|
|
lu=strtoul(code, &ck, 16);
|
|
|
|
buffy_HEX[x] = lu;
|
2016-11-06 01:57:36 +00:00
|
|
|
//printf("%s - %lu\n",code,lu);
|
|
|
|
fori001++;
|
2016-11-02 16:27:58 +00:00
|
|
|
x++;
|
|
|
|
}
|
|
|
|
return buffy_HEX;
|
|
|
|
}
|
|
|
|
//
|
2016-11-08 11:02:45 +00:00
|
|
|
void convert(char * convert_result, uint8_t * buf) //Both of them read them properly.
|
|
|
|
{
|
2016-11-08 16:01:10 +00:00
|
|
|
char conv_proc[800]="\0";
|
|
|
|
bzero(conv_proc,800);
|
2016-11-08 11:02:45 +00:00
|
|
|
int i;
|
|
|
|
for(i=0; i < 10; i++)
|
|
|
|
{
|
|
|
|
sprintf (conv_proc, "%02X", buf[i]);
|
|
|
|
//printf("%d:%d\n",i, buf[i]);
|
|
|
|
strcat(convert_result, conv_proc);
|
|
|
|
}
|
|
|
|
}
|
2016-11-02 16:27:58 +00:00
|
|
|
char * Num_To_HexVar_64(uint64_t TOHVINPUT) //UINT64 TO HEXIFIED VAR
|
|
|
|
{ //Code to varint - py
|
2016-11-08 16:01:10 +00:00
|
|
|
static char convert_result[800]="\0";//Note that the hex resulted from this will differ from py
|
|
|
|
bzero(convert_result,800);
|
2016-11-02 16:27:58 +00:00
|
|
|
memset(convert_result,0,sizeof(convert_result));//But if you make sure the string is always 20 chars in size
|
2016-11-08 16:01:10 +00:00
|
|
|
uint8_t buf[400] = {0};
|
|
|
|
bzero(buf,400);
|
|
|
|
uvarint_encode64(TOHVINPUT, buf, 800);
|
2016-11-08 11:02:45 +00:00
|
|
|
convert(convert_result,buf);
|
|
|
|
return convert_result;
|
|
|
|
}
|
|
|
|
void convert2(char * convert_result2, uint8_t * bufhx)
|
|
|
|
{
|
|
|
|
uint8_t * buf = NULL;
|
|
|
|
buf = bufhx;
|
|
|
|
char conv_proc[3]="\0";
|
|
|
|
conv_proc[3] = '\0';
|
|
|
|
bzero(conv_proc, 3);
|
|
|
|
int i;
|
|
|
|
for(i=0; i == 0; i++)
|
2016-11-02 16:27:58 +00:00
|
|
|
{
|
2016-11-08 11:02:45 +00:00
|
|
|
sprintf (conv_proc, "%02X", buf[i]);
|
|
|
|
//printf("aaaaaaaaaaah%d:%d\n",i, buf[i]);
|
|
|
|
strcat(convert_result2, conv_proc);
|
2016-11-02 16:27:58 +00:00
|
|
|
}
|
2016-11-08 11:02:45 +00:00
|
|
|
buf = NULL;
|
2016-11-02 16:27:58 +00:00
|
|
|
}
|
|
|
|
char * Num_To_HexVar_32(uint32_t TOHVINPUT) //UINT32 TO HEXIFIED VAR
|
|
|
|
{ //Code to varint - py
|
2016-11-08 11:02:45 +00:00
|
|
|
static char convert_result2[3]="\0";
|
|
|
|
bzero(convert_result2,3);
|
|
|
|
convert_result2[2] = '\0';
|
2016-11-02 16:27:58 +00:00
|
|
|
memset(convert_result2,0,sizeof(convert_result2));
|
2016-11-08 11:02:45 +00:00
|
|
|
uint8_t buf[1] = {0};
|
|
|
|
bzero(buf,1);
|
|
|
|
uvarint_encode32(TOHVINPUT, buf, 1);
|
|
|
|
convert2(convert_result2,buf);
|
2016-11-02 16:27:58 +00:00
|
|
|
return convert_result2;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t HexVar_To_Num_64(char * theHEXstring) //HEXIFIED VAR TO UINT64_T
|
|
|
|
{ //Varint to code - py
|
2016-11-08 16:01:10 +00:00
|
|
|
uint8_t buffy[400] = {0};
|
|
|
|
char codo[800] = "\0";
|
|
|
|
bzero(codo,800);
|
2016-11-02 16:27:58 +00:00
|
|
|
strcpy(codo, theHEXstring);
|
|
|
|
char code[3] = "\0";
|
|
|
|
int x = 0;
|
2016-11-08 16:01:10 +00:00
|
|
|
for(int i= 0;i<399;i++)
|
2016-11-02 16:27:58 +00:00
|
|
|
{
|
|
|
|
strncpy(&code[0],&codo[i],1);
|
|
|
|
strncpy(&code[1],&codo[i+1],1);
|
|
|
|
char *ck = NULL;
|
|
|
|
uint64_t lu = 0;
|
|
|
|
lu=strtoul(code, &ck, 16);
|
|
|
|
buffy[x] = lu;
|
|
|
|
i++;
|
|
|
|
x++;
|
|
|
|
}
|
|
|
|
static uint64_t decoded;
|
2016-11-08 16:01:10 +00:00
|
|
|
uvarint_decode64 (buffy, 400, &decoded);
|
2016-11-02 16:27:58 +00:00
|
|
|
return decoded;
|
|
|
|
}
|
|
|
|
uint32_t HexVar_To_Num_32(char theHEXstring[]) //HEXIFIED VAR TO UINT32_T
|
|
|
|
{ //Varint to code py
|
2016-11-08 16:01:10 +00:00
|
|
|
uint8_t buffy[400] = {0};
|
|
|
|
bzero(buffy,400);
|
|
|
|
char codo[800] = "\0";
|
|
|
|
bzero(codo,800);
|
2016-11-02 16:27:58 +00:00
|
|
|
strcpy(codo, theHEXstring);
|
2016-11-08 11:02:45 +00:00
|
|
|
char code[3] = "\0";
|
|
|
|
bzero(code,3);
|
|
|
|
code[3] = '\0';
|
2016-11-02 16:27:58 +00:00
|
|
|
int x = 0;
|
2016-11-08 16:01:10 +00:00
|
|
|
for(int i= 0;i<399;i++)
|
2016-11-02 16:27:58 +00:00
|
|
|
{
|
|
|
|
strncpy(&code[0],&codo[i],1);
|
|
|
|
strncpy(&code[1],&codo[i+1],1);
|
|
|
|
char *ck = NULL;
|
|
|
|
uint32_t lu = {0};
|
|
|
|
lu=strtoul(code, &ck, 16);
|
|
|
|
buffy[x] = lu;
|
|
|
|
i++;
|
|
|
|
x++;
|
|
|
|
}
|
|
|
|
static uint32_t decoded;
|
|
|
|
uvarint_decode32 (buffy, 10, &decoded);
|
|
|
|
return decoded;
|
|
|
|
}
|
|
|
|
#endif
|