From 1aea270cf19c516cc0520d88e664c60ab9a1f512 Mon Sep 17 00:00:00 2001 From: ken Code Date: Fri, 4 Nov 2016 12:29:36 +0100 Subject: [PATCH 1/2] Create LICENSE --- LICENSE | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..3fff97c --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2016 BitShares Munich IVS + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. From 79562d57a3db5907c9f957656645a6ed3a19d797 Mon Sep 17 00:00:00 2001 From: xethyrion Date: Thu, 17 Nov 2016 16:52:27 +0200 Subject: [PATCH 2/2] Add files via upload --- header.h | 182 +++++++++++++++++++++++++++ varint.c | 8 ++ varint.h | 51 ++++++++ vhutils.h | 365 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 606 insertions(+) create mode 100644 header.h create mode 100644 varint.c create mode 100644 varint.h create mode 100644 vhutils.h diff --git a/header.h b/header.h new file mode 100644 index 0000000..e20eb5b --- /dev/null +++ b/header.h @@ -0,0 +1,182 @@ +#ifndef MULTICODEC_HEADER +#define MULTICODEC_HEADER +#include "vhutils.h" +int header_from_string(char * dest,size_t destsize, char * src,size_t srcsize) +{ + char result[destsize+2]; + bzero(result,destsize+2); + strcpy(result, Int_To_Hex(srcsize-1)); + printf("SZ: %s\n",result); + char srchex[((srcsize-1)*2)+1]; + str2hex(srchex,src); + strcat(result,srchex); + strcpy(dest,result); + return 1; + char A[] = "/lol/meow"; + char B[((sizeof(A)-1)*2)+3]; + bzero(B,sizeof(B)); + header(B,sizeof(B),A,sizeof(A)); + printf("Result: %s\n", B); +} +/*///Header - Usage Example: + * unsigned char str[] = "/lol/meow/"; + * unsigned char strhex[((strlen(str)-1)*2)+1]; + * str2hex(strhex,str); + * unsigned char header_result[sizeof(strhex+2)]; // Extra 2 for size + * bzero(header_result, sizeof(header_result)); + * header(header_result, strhex); + * printf("header - Result: %s\n", header_result); + * unsigned char header_path_result[strlen(header_result)-2]; +*/ +int header(unsigned char * dest, unsigned char * src) +{ + if(!src) + { + return 0; + } + char size[3]; + bzero(size,3); + strcpy(size, Int_To_Hex((strlen(src)/2)-1)); + strcat(dest, size); + strcat(dest, src); + return 1; +} +/* + * //Header_Path - Usage Example + * header_path(header_path_result,header_result); + * printf("header_path - Result: %s\n",header_path_result); + */ +int header_path(unsigned char * dest, unsigned char * src) +{ + if(!src) + { + return 0; + } + sprintf(dest,"%s",src+2); + return 1; +} +/* + * //Add Header - Usage Example: + * char continut[] = "1234"; + * char continuthex[((sizeof(continut)-1)*2)+1]; + * bzero(continuthex, sizeof(continuthex)); + * str2hex(continuthex, continut); + * char cap[] = "/lol/"; + * char caphex[((sizeof(cap)-1)*2)+1]; + * bzero(caphex,sizeof(caphex)); + * str2hex(caphex,cap); + * //printf("caphex = %s\n", caphex); + * //printf("conhex = %s\n", continuthex); + * char add_hdr_dest[sizeof(continuthex)-1+sizeof(caphex)-1+3]; + * bzero(add_hdr_dest, sizeof(add_hdr_dest)); + * add_header(add_hdr_dest, caphex,sizeof(caphex), continuthex, sizeof(continuthex)); + * printf("add_header - Result: %s\n",add_hdr_dest); + */ +int add_header(unsigned char * dest,unsigned char * header,size_t headersize, unsigned char * content, size_t contentsize) +{ + if(!header||!content) + { + return 0; + } + int realsize = (headersize-1+contentsize-1)/2; + char szhex[3]; + bzero(szhex,3); + strcat(szhex, Int_To_Hex(realsize)); + strcat(dest, szhex); + strcat(dest, header); + strcat(dest, content); + return 1; +} +/*//Remove Header - Usage Example: + * char remove_hdr_dst[1000]; //Much bigger than needed tbh.. + * bzero(remove_hdr_dst,1000); + * remove_header(remove_hdr_dst, add_hdr_dest); + * printf("remove_header - Result: %s\n",remove_hdr_dst); + */ +int remove_header(unsigned char * dest, unsigned char * lel) +{ + if(!lel) + { + return 0; + } + int init = 0; + char s[(strlen(lel)/2)+1]; + int sizs = strlen(lel)/2+1; + hex2str(lel, s, sizs); + sprintf(s,"%s", s+1); + char * pch; + char * end; + pch = strtok_r(s,"/",&end); + int pos = 0; + while(pch) + { + //printf("PCH: %s\n", pch); + if(pos==1) + { + str2hex(dest, pch); + } + //sprintf(dest,"%s",pch); + pch = strtok_r(NULL,"/",&end); + pos=1; + } + return 1; +} +/* //getHeader + * char geth_dest[1000]; + * bzero(geth_dest,1000); + * get_header(geth_dest, add_hdr_dest); + * printf("get_header - Result: %s\n", geth_dest); + */ +int get_header(char * dest, unsigned char * src) +{ + if(!src) + { + return 0; + } + char ascii[strlen(src)/2]; + hex2str(src, ascii,strlen(src)/2); + sprintf(ascii, "%s",ascii+1); + char * pch; + char * end; + pch = strtok_r(ascii,"/",&end); + while(pch) + { + strcat(dest,pch); + break; + } + return 1; +} +/* + * //getcontent + * char getc_dest[1000]; + * bzero(getc_dest,1000); + * get_content(getc_dest, add_hdr_dest); + * printf("get_content - Result: %s\n", getc_dest); + */ +int get_content(char * dest, unsigned char * src) +{ + if(!src) + { + return 0; + } + char ascii[strlen(src)/2]; + hex2str(src, ascii,strlen(src)/2); + sprintf(ascii, "%s",ascii+1); + char * pch; + char * end; + int ax = 0; + pch = strtok_r(ascii,"/",&end); + while(pch) + { + //printf("PCH: %s", pch); + if(ax == 1) + { + strcat(dest,pch); + break; + } + ax=1; + pch = strtok_r(NULL,"/",&end); + } + return 1; +} +#endif 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 diff --git a/vhutils.h b/vhutils.h new file mode 100644 index 0000000..ae1f4a4 --- /dev/null +++ b/vhutils.h @@ -0,0 +1,365 @@ +#ifndef VARHEXUTILS +#define VARHEXUTILS + +#include +#include +#include "varint.h" +#include +#include +#include "endian.h" +void hex2str(const char * h, char * s, int sizs) +{ + static char hex[] = "0123456789ABCDEF"; + int i = 0; + int len = strlen(h)/2; + if (len > sizs) + len = sizs; + for (i = 0; i < len; ++i) + + { + char* p = strchr(hex, *(h+2*i)); + if (p != NULL) + { + s[i] = (char)(unsigned char)((p-hex)<<4); + p = strchr(hex, *(h+2*i+1)); + if (p != NULL) + { + s[i] |= (char)(p-hex); + } + else + s[i] = '?'; + } + else + s[i] = '?'; + } + if (i < sizs * 2) + *(s+i)='\0'; +} +void str2hex(unsigned char* hexstring, unsigned char* string) +{ + unsigned char ch,i,j,len; + + len = strlen(string); + + for(i=0,j=0;i= 0 && ch <= 0x0F) + { + hexstring[j] = 0x30; + + if(ch >= 0 && ch <= 9) + hexstring[j+1] = 0x30 + ch; + else + hexstring[j+1] = 0x37 + ch; + } + else if( ch >= 0x10 && ch <= 0x1F) + { + hexstring[j] = 0x31; + ch -= 0x10; + + if(ch >= 0 && ch <= 9) + hexstring[j+1] = 0x30 + ch; + else + hexstring[j+1] = 0x37 + ch; + } + else if( ch >= 0x20 && ch <= 0x2F) + { + hexstring[j] = 0x32; + ch -= 0x20; + + if(ch >= 0 && ch <= 9) + hexstring[j+1] = 0x30 + ch; + else + hexstring[j+1] = 0x37 + ch; + } + else if( ch >= 0x30 && ch <= 0x3F) + { + hexstring[j] = 0x33; + ch -= 0x30; + + if(ch >= 0 && ch <= 9) + hexstring[j+1] = 0x30 + ch; + else + hexstring[j+1] = 0x37 + ch; + } + else if( ch >= 0x40 && ch <= 0x4F) + { + hexstring[j] = 0x34; + ch -= 0x40; + + if(ch >= 0 && ch <= 9) + hexstring[j+1] = 0x30 + ch; + else + hexstring[j+1] = 0x37 + ch; + } + else if( ch >= 0x50 && ch <= 0x5F) + { + hexstring[j] = 0x35; + ch -= 0x50; + + if(ch >= 0 && ch <= 9) + hexstring[j+1] = 0x30 + ch; + else + hexstring[j+1] = 0x37 + ch; + } + else if( ch >= 0x60 && ch <= 0x6F) + { + hexstring[j] = 0x36; + ch -= 0x60; + + if(ch >= 0 && ch <= 9) + hexstring[j+1] = 0x30 + ch; + else + hexstring[j+1] = 0x37 + ch; + } + else if( ch >= 0x70 && ch <= 0x7F) + { + hexstring[j] = 0x37; + ch -= 0x70; + + if(ch >= 0 && ch <= 9) + hexstring[j+1] = 0x30 + ch; + else + hexstring[j+1] = 0x37 + ch; + } + } + hexstring[j] = 0x00; +} +int8_t Var_Bytes_Count(uint8_t * countbytesofthis) +{ + 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[60] = {0}; + uvarint_encode64(TOV64INPUT, buffy_001, 60); + return buffy_001; +} +uint8_t * Num_To_Varint_32(uint32_t TOV32INPUT) // UINT32_T TO VARINT +{ + static uint8_t buffy_032[60] = {0}; + uvarint_encode32(TOV32INPUT, buffy_032, 60); + return buffy_032; +} +uint64_t * Varint_To_Num_64(uint8_t TON64INPUT[60]) //VARINT TO UINT64_t +{ + static uint64_t varintdecode_001 = 0; + uvarint_decode64(TON64INPUT, 60, &varintdecode_001); + return &varintdecode_001; +} +uint32_t * Varint_To_Num_32(uint8_t TON32INPUT[60]) //VARINT TO UINT32_t +{ + static uint32_t varintdecode_032 = 0; + uvarint_decode32(TON32INPUT, 60, &varintdecode_032); + return &varintdecode_032; +} +// +char * Int_To_Hex(uint64_t int2hex) //VAR[binformat] TO HEX +{ + static char int2hex_result[800]="\0"; + memset(int2hex_result,0,sizeof(int2hex_result)); + sprintf (int2hex_result, "%02lX", int2hex); + return int2hex_result; +} +uint64_t Hex_To_Int(char * hax) +{ + char * hex = NULL; + hex=hax; + uint64_t val = 0; + while (*hex) + { + // 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; +} +// +void vthconvert(int size, char * crrz01, uint8_t * xbuf) +{ + uint8_t buf[400]; + bzero(buf,400); + + //fixing the buf + for(int cz=0; cz