diff --git a/protocols.c b/protocols.c new file mode 100644 index 0000000..dc7cc2b --- /dev/null +++ b/protocols.c @@ -0,0 +1,234 @@ +#include +#include +#include +#include +#include +#include "varhexutils.h" +int CNT_PROTOCOLNUM=0; +struct protocol +{ + char hexcode[21]; + int deccode; + int size; + char name[30]; +}; +struct protocol *protocol_P; //Pointer for holding multiple structures +int protocol_REMOVE_id(int remid)//Function to remove & shift back all data, sort of like c++ vectors. +{ + + if(remid < CNT_PROTOCOLNUM && remid >= 0) //Checking to see if remid actually exists. + { + for(int i=remid; ihexcode, (protocol_P+i+1)->hexcode); //shift memory to the user we want to remove. + (protocol_P+i)->deccode = (protocol_P+i+1)->deccode; //Same as above + (protocol_P+i)->size = (protocol_P+i+1)->size; //Same as above + strcpy((protocol_P+i)->name, (protocol_P+i+1)->name); //Same as above + }//Overwriting user done. Time to get rid of that extra memory. + protocol_P = (struct protocol*) realloc(protocol_P, (CNT_PROTOCOLNUM-1) * sizeof(struct protocol)); + //Memory erased, + CNT_PROTOCOLNUM--; //Since the record no longer exists, we should decrease the ammount of users. + return 1; //Purely for error checking, in case someone ever wants it/ + } //1 = Success + else + { + printf("ERROR"); + return 0;//0 = Failure. + } +} +void load_protocols() +{ + FILE *FPROC_POINT; //File pointer. + FPROC_POINT = fopen("proto-dat", "r");//Opening proto-dat Or protocols.csv, I just formatted it to my liking. + if(FPROC_POINT != NULL) //While pointer is not null. + { + char W_BUFF[20] = "\0";//Char array to parse file. + for(int i=0; fscanf(FPROC_POINT, "%s", W_BUFF) != EOF; i++) // Scanning file and incrementing for processing. + { + switch(i) + { + case 0: //First word - HEXCODE + { + //ADD MEMORY FOR NEW ROWS + if(CNT_PROTOCOLNUM==0) //If there are no registered protocols yet, allocate memory to pointer. + { + protocol_P = (struct protocol*) malloc (sizeof(struct protocol)); + } + else //Reallocate memory to fit one more protocol + { + protocol_P = (struct protocol*) realloc(protocol_P, (CNT_PROTOCOLNUM+1) * sizeof(struct protocol)); + } + strcpy((protocol_P+CNT_PROTOCOLNUM)->hexcode, W_BUFF); //Copy word to structure at hexcode A hexcode is a string so we keep it as such + break; + } + case 1://Second word - DECCODE + { + (protocol_P+CNT_PROTOCOLNUM)->deccode= atoi(W_BUFF); //Copy word to structure at deccode after converting it to int. + break; + } + case 2://Third word - SIZE + { + (protocol_P+CNT_PROTOCOLNUM)->size= atoi(W_BUFF); //Copy word to structure at size after converting it to int. + break; + } + case 3://Fourth word - NAME + { + strcpy((protocol_P+CNT_PROTOCOLNUM)->name, W_BUFF); //Copy word to structure at name // String + i=-1; + CNT_PROTOCOLNUM++; + break; + } + default: + { + printf("HOUSTON WE HAVE A PROBLEM!!!!\n"); + break; + } + } + } + fclose(FPROC_POINT); + protocol_REMOVE_id(0); + } + else + { + perror("Fatal Error:"); + } +} +struct protocol * proto_with_name(char proto_w_name[]) //Search for protocol with inputted name +{ + + for(int i=0; iname) == 0) + { + return (protocol_P+i); + } + } +} +struct protocol * proto_with_deccode(int proto_w_deccode) //Search for protocol with inputted deccode +{ + for(int i=0; ideccode == proto_w_deccode) + { + return (protocol_P+i); + } + } +} +void pp() //Purely for debugging purposes, prints the entire loaded protocols. +{ + for(int i=0;i=9) + {printf("=========== ~%d~ ===========\n", i+1);} + else + {printf("=========== ~0%d~ ===========\n", i+1);} + printf(">> HEX-CODE: %s\n", (protocol_P+i)->hexcode); + printf(">> DEC-CODE: %d\n", (protocol_P+i)->deccode); + printf(">> SIZE: %d\n", (protocol_P+i)->size); + printf(">> NAME: %s\n", (protocol_P+i)->name); + } + printf("----------------------------\n"); + printf("TOTAL PROTOCOLS: %d\n",CNT_PROTOCOLNUM); +} +void protocols_with_string(char * meee,int sizi) // NOT FINISHED, DO NOT USE! +{ + int finalsize = 0; + + if(!isalnum(meee[sizi-1]) && !isalnum(meee[sizi-1])) + { + //Everything is alright, it's nul terminated!; + finalsize = sizi; + } + else + { + //Well houston we have a problem. + finalsize = sizi+2; + } + char mestring[finalsize]; + strcpy(mestring, meee); + if(sizi!=finalsize) + { + strcpy(mestring,"\0"); + } + + char * words[50] = { NULL }; + int atword = 0; + int mem = 0; + for(int i=0; iname, proto_with_name("onion")->size); + printf("The returned protocol is: %s\nSIZE: %d\n", proto_with_deccode(444)->name, proto_with_deccode(444)->size); + for(int i=0; i<14; i++) + { + uint64_t extract = (protocol_P+i)->deccode; + char exhexco[20] = "\0"; + strcpy(exhexco, Num_To_HexVar_64(extract)); + uint64_t decimalval = HexVar_To_Num_64(exhexco); + if(i<9){printf("PROTOCOL 0%d HAS HEXCODE: %s DECIMAL: %"PRIu64" \n", i+1, exhexco, decimalval);} + else{printf("PROTOCOL %d HAS HEXCODE: %s DECIMAL: %"PRIu64" \n", i+1, exhexco, decimalval);}; + } + free(protocol_P); + printf("1337 in Hexvar_64: %s\n", Num_To_HexVar_64(1337)); + char HEXSTR[20] = "B90A0000000000000000"; //\0 since it's not defined in a source string + uint64_t result = HexVar_To_Num_64(HEXSTR); + result = HexVar_To_Num_64(HEXSTR); + printf("Hexvar_To_Num_64: %"PRIu64"\n", result); + uint8_t Numinvar[10] = {0}; + memcpy(Numinvar, Num_To_Varint_64(1337), 10); + printf("Binary form unreadable obviously : %s\n", Numinvar); + uint64_t Varinnum = 0; + memcpy(&Varinnum, Varint_To_Num_64(Numinvar), sizeof(Varint_To_Num_64(Numinvar))); + printf("Number form now readable again:%"PRIu64"\n", Varinnum); + char converted2hex[20] = "\0"; + strcpy(converted2hex, Var_To_Hex(Numinvar)); + uint8_t converted2bin[10] = {0}; + memcpy(converted2bin, Hex_To_Var(converted2hex), 10); + printf("Encoding previous binary to hex now: %s\n", converted2hex); + printf("Encoding previous hex to binary now: %s\n", converted2bin); + //Series of bytes test + int8_t bcounter = 0; + bcounter = Var_Bytes_Count(Numinvar); + printf("Bytes_Count of Numinvar(1337): %d\n", bcounter); + + + return 0; +} \ No newline at end of file diff --git a/varhexutils.h b/varhexutils.h new file mode 100644 index 0000000..7082a09 --- /dev/null +++ b/varhexutils.h @@ -0,0 +1,170 @@ +#ifndef VARHEXUTILS +#define VARHEXUTILS +#include +#include +#include "varint.h" +#include +#include +int8_t Var_Bytes_Count(uint8_t countbytesofthis[10]) +{ + 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 +{ + static uint8_t buffy_001[10] = {0}; + uvarint_encode64(TOV64INPUT, buffy_001, 10); + return buffy_001; +} +uint8_t * Num_To_Varint_32(uint32_t TOV32INPUT) // UINT32_T TO VARINT +{ + static uint8_t buffy_032[10] = {0}; + uvarint_encode32(TOV32INPUT, buffy_032, 10); + return buffy_032; +} +uint64_t * Varint_To_Num_64(uint8_t TON64INPUT[10]) //VARINT TO UINT64_t +{ + static uint64_t varintdecode_001 = 0; + uvarint_decode64(TON64INPUT, 10, &varintdecode_001); + return &varintdecode_001; +} +uint32_t * Varint_To_Num_32(uint8_t TON32INPUT[10]) //VARINT TO UINT32_t +{ + static uint32_t varintdecode_032 = 0; + uvarint_decode32(TON32INPUT, 10, &varintdecode_032); + return &varintdecode_032; +} +// +char * Var_To_Hex(uint8_t TOHEXINPUT[10]) //VAR[binformat] TO HEX +{ + static char convert_resultz1[10]="\0"; + memset(convert_resultz1,0,sizeof(convert_resultz1)); + void convert(uint8_t buf[10]) + { + char conv_proc[10]="\0"; + for(int i=0; i < 10; i++) + { + sprintf (conv_proc, "%02X", buf[i]); + //printf("%d:%d\n",i, buf[i]); + strcat(convert_resultz1, conv_proc); + } + } + convert(TOHEXINPUT); + return convert_resultz1; +} +uint8_t * Hex_To_Var(char * Hexstr) //HEX TO VAR[BINFORMAT] +{ + static uint8_t buffy_HEX[10] = {0}; + char codo[20] = "\0"; + strcpy(codo, Hexstr); + char code[3] = "\0"; + int x = 0; + for(int i= 0;i<20;i++) + { + 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_HEX[x] = lu; + i++; + x++; + } + return buffy_HEX; +} +// +char * Num_To_HexVar_64(uint64_t TOHVINPUT) //UINT64 TO HEXIFIED VAR +{ //Code to varint - py + static char convert_result[10]="\0";//Note that the hex resulted from this will differ from py + memset(convert_result,0,sizeof(convert_result));//But if you make sure the string is always 20 chars in size + void convert(uint8_t buf[10]) //Both of them read them properly. + { + char conv_proc[10]="\0"; + int i=0; + for(; i < 10; i++) + { + sprintf (conv_proc, "%02X", buf[i]); + //printf("%d:%d\n",i, buf[i]); + strcat(convert_result, conv_proc); + } + } + uint8_t buf[10] = {0}; + uvarint_encode64(TOHVINPUT, buf, 10); + convert(buf); + + return convert_result; +} +char * Num_To_HexVar_32(uint32_t TOHVINPUT) //UINT32 TO HEXIFIED VAR +{ //Code to varint - py + static char convert_result2[10]="\0"; + memset(convert_result2,0,sizeof(convert_result2)); + void convert(uint8_t buf[10]) + { + char conv_proc[10]="\0"; + int i=0; + for(; i < 10; i++) + { + sprintf (conv_proc, "%02X", buf[i]); + printf("%d:%d\n",i, buf[i]); + strcat(convert_result2, conv_proc); + } + } + uint8_t buf[10] = {0}; + uvarint_encode32(TOHVINPUT, buf, 10); + convert(buf); + + return convert_result2; +} + +uint64_t HexVar_To_Num_64(char * theHEXstring) //HEXIFIED VAR TO UINT64_T +{ //Varint to code - py + uint8_t buffy[10] = {0}; + char codo[20] = "\0"; + strcpy(codo, theHEXstring); + char code[3] = "\0"; + int x = 0; + for(int i= 0;i<20;i++) + { + 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; + uvarint_decode64 (buffy, 10, &decoded); + return decoded; +} +uint32_t HexVar_To_Num_32(char theHEXstring[]) //HEXIFIED VAR TO UINT32_T +{ //Varint to code py + uint8_t buffy[10] = {0}; + char codo[20] = "\0"; + strcpy(codo, theHEXstring); + char code[2] = "\0"; + int x = 0; + for(int i= 0;i<20;i++) + { + 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 \ No newline at end of file diff --git a/varint.c b/varint.c new file mode 100644 index 0000000..a8fd1cc --- /dev/null +++ b/varint.c @@ -0,0 +1,8 @@ +#include "varint.h" + + +DEFN_ENCODER(32) +DEFN_DECODER(32) + +DEFN_ENCODER(64) +DEFN_DECODER(64) \ No newline at end of file diff --git a/varint.h b/varint.h new file mode 100644 index 0000000..69616cc --- /dev/null +++ b/varint.h @@ -0,0 +1,51 @@ +#ifndef VARINT +#define VARINT +#include /* size_t */ +#include /* uint8_t, uint64_t */ + + +#define DEFN_ENCODER(SIZE) \ + size_t \ + uvarint_encode##SIZE (uint##SIZE##_t val, uint8_t buf[], size_t bufsize) \ + { \ + size_t i = 0; \ + for (; i < (SIZE/8) && i < bufsize; i++) { \ + buf[i] = (uint8_t) ((val & 0xFF) | 0x80); \ + val >>= 7; \ + if (!val) \ + return i + 1; \ + } \ + return -1; \ + } + + +#define DEFN_DECODER(SIZE) \ + size_t \ + uvarint_decode##SIZE (uint8_t buf[], size_t bufsize, uint##SIZE##_t *val) \ + { \ + *val = 0; \ + size_t i = 0; \ + for (; i < (SIZE/8) && i < bufsize; i++) { \ + *val |= ((buf[i] & 0x7f) << (7 * i)); \ + if (!(buf[i] & 0x80)) \ + return i + 1; \ + } \ + return -1; \ + } + + +#define DECL_ENCODER(SIZE) \ + size_t \ + uvarint_encode##SIZE (uint##SIZE##_t val, uint8_t buf[], size_t bufsize); + +#define DECL_DECODER(SIZE) \ + size_t \ + uvarint_decode##SIZE (uint8_t buf[], size_t bufsize, uint##SIZE##_t *val); + + +DECL_ENCODER(32) +DECL_DECODER(32) + +DECL_ENCODER(64) +DECL_DECODER(64) +#endif \ No newline at end of file