Compare commits
10 commits
72dd752533
...
ba359385bf
Author | SHA1 | Date | |
---|---|---|---|
|
ba359385bf | ||
|
80823af384 | ||
|
0c506c7dbc | ||
|
10dda30230 | ||
|
7972844d0a | ||
|
e4dd7aa335 | ||
|
4589bde673 | ||
|
79562d57a3 | ||
|
1aea270cf1 | ||
|
af1af490ef |
15 changed files with 849 additions and 0 deletions
74
.cproject
Normal file
74
.cproject
Normal file
|
@ -0,0 +1,74 @@
|
|||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<?fileVersion 4.0.0?><cproject storage_type_id="org.eclipse.cdt.core.XmlProjectDescriptionStorage">
|
||||
<storageModule moduleId="org.eclipse.cdt.core.settings">
|
||||
<cconfiguration id="cdt.managedbuild.toolchain.gnu.macosx.base.1883276717">
|
||||
<storageModule buildSystemId="org.eclipse.cdt.managedbuilder.core.configurationDataProvider" id="cdt.managedbuild.toolchain.gnu.macosx.base.1883276717" moduleId="org.eclipse.cdt.core.settings" name="Default">
|
||||
<externalSettings/>
|
||||
<extensions>
|
||||
<extension id="org.eclipse.cdt.core.MachO64" point="org.eclipse.cdt.core.BinaryParser"/>
|
||||
<extension id="org.eclipse.cdt.core.GASErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
|
||||
<extension id="org.eclipse.cdt.core.GmakeErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
|
||||
<extension id="org.eclipse.cdt.core.GLDErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
|
||||
<extension id="org.eclipse.cdt.core.CWDLocator" point="org.eclipse.cdt.core.ErrorParser"/>
|
||||
<extension id="org.eclipse.cdt.core.GCCErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
|
||||
</extensions>
|
||||
</storageModule>
|
||||
<storageModule moduleId="cdtBuildSystem" version="4.0.0">
|
||||
<configuration buildProperties="" id="cdt.managedbuild.toolchain.gnu.macosx.base.1883276717" name="Default" parent="org.eclipse.cdt.build.core.emptycfg">
|
||||
<folderInfo id="cdt.managedbuild.toolchain.gnu.macosx.base.1883276717.1669788131" name="/" resourcePath="">
|
||||
<toolChain id="cdt.managedbuild.toolchain.gnu.macosx.base.1835101379" name="MacOSX GCC" superClass="cdt.managedbuild.toolchain.gnu.macosx.base">
|
||||
<targetPlatform archList="all" binaryParser="org.eclipse.cdt.core.MachO64" id="cdt.managedbuild.target.gnu.platform.macosx.base.444260860" name="Debug Platform" osList="macosx" superClass="cdt.managedbuild.target.gnu.platform.macosx.base"/>
|
||||
<builder id="cdt.managedbuild.target.gnu.builder.macosx.base.1380122730" managedBuildOn="false" name="Gnu Make Builder.Default" superClass="cdt.managedbuild.target.gnu.builder.macosx.base"/>
|
||||
<tool id="cdt.managedbuild.tool.macosx.c.linker.macosx.base.2019361576" name="MacOS X C Linker" superClass="cdt.managedbuild.tool.macosx.c.linker.macosx.base"/>
|
||||
<tool id="cdt.managedbuild.tool.macosx.cpp.linker.macosx.base.826723335" name="MacOS X C++ Linker" superClass="cdt.managedbuild.tool.macosx.cpp.linker.macosx.base">
|
||||
<inputType id="cdt.managedbuild.tool.macosx.cpp.linker.input.944090650" superClass="cdt.managedbuild.tool.macosx.cpp.linker.input">
|
||||
<additionalInput kind="additionalinputdependency" paths="$(USER_OBJS)"/>
|
||||
<additionalInput kind="additionalinput" paths="$(LIBS)"/>
|
||||
</inputType>
|
||||
</tool>
|
||||
<tool id="cdt.managedbuild.tool.gnu.assembler.macosx.base.623402424" name="GCC Assembler" superClass="cdt.managedbuild.tool.gnu.assembler.macosx.base">
|
||||
<inputType id="cdt.managedbuild.tool.gnu.assembler.input.513603410" superClass="cdt.managedbuild.tool.gnu.assembler.input"/>
|
||||
</tool>
|
||||
<tool id="cdt.managedbuild.tool.gnu.archiver.macosx.base.752113044" name="GCC Archiver" superClass="cdt.managedbuild.tool.gnu.archiver.macosx.base"/>
|
||||
<tool id="cdt.managedbuild.tool.gnu.cpp.compiler.macosx.base.1514699136" name="GCC C++ Compiler" superClass="cdt.managedbuild.tool.gnu.cpp.compiler.macosx.base">
|
||||
<inputType id="cdt.managedbuild.tool.gnu.cpp.compiler.input.978502497" superClass="cdt.managedbuild.tool.gnu.cpp.compiler.input"/>
|
||||
</tool>
|
||||
<tool id="cdt.managedbuild.tool.gnu.c.compiler.macosx.base.451686039" name="GCC C Compiler" superClass="cdt.managedbuild.tool.gnu.c.compiler.macosx.base">
|
||||
<inputType id="cdt.managedbuild.tool.gnu.c.compiler.input.186626776" superClass="cdt.managedbuild.tool.gnu.c.compiler.input"/>
|
||||
</tool>
|
||||
</toolChain>
|
||||
</folderInfo>
|
||||
</configuration>
|
||||
</storageModule>
|
||||
<storageModule moduleId="org.eclipse.cdt.core.externalSettings"/>
|
||||
</cconfiguration>
|
||||
</storageModule>
|
||||
<storageModule moduleId="cdtBuildSystem" version="4.0.0">
|
||||
<project id="c-multicodec.null.1791769968" name="c-multicodec"/>
|
||||
</storageModule>
|
||||
<storageModule moduleId="scannerConfiguration">
|
||||
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
|
||||
</storageModule>
|
||||
<storageModule moduleId="org.eclipse.cdt.core.LanguageSettingsProviders"/>
|
||||
<storageModule moduleId="refreshScope"/>
|
||||
<storageModule moduleId="org.eclipse.cdt.make.core.buildtargets">
|
||||
<buildTargets>
|
||||
<target name="all" path="" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
|
||||
<buildCommand>make</buildCommand>
|
||||
<buildArguments/>
|
||||
<buildTarget>all</buildTarget>
|
||||
<stopOnError>true</stopOnError>
|
||||
<useDefaultCommand>true</useDefaultCommand>
|
||||
<runAllBuilders>true</runAllBuilders>
|
||||
</target>
|
||||
<target name="clean" path="" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
|
||||
<buildCommand>make</buildCommand>
|
||||
<buildArguments/>
|
||||
<buildTarget>clean</buildTarget>
|
||||
<stopOnError>true</stopOnError>
|
||||
<useDefaultCommand>true</useDefaultCommand>
|
||||
<runAllBuilders>true</runAllBuilders>
|
||||
</target>
|
||||
</buildTargets>
|
||||
</storageModule>
|
||||
</cproject>
|
9
.gitignore
vendored
Normal file
9
.gitignore
vendored
Normal file
|
@ -0,0 +1,9 @@
|
|||
*
|
||||
|
||||
!.gitignore
|
||||
!Makefile
|
||||
!**/
|
||||
|
||||
*.o
|
||||
.settings/language.settings.xml
|
||||
test/test_multicodec
|
27
.project
Normal file
27
.project
Normal file
|
@ -0,0 +1,27 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>c-multicodec</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.cdt.managedbuilder.core.genmakebuilder</name>
|
||||
<triggers>clean,full,incremental,</triggers>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder</name>
|
||||
<triggers>full,incremental,</triggers>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.cdt.core.cnature</nature>
|
||||
<nature>org.eclipse.cdt.core.ccnature</nature>
|
||||
<nature>org.eclipse.cdt.managedbuilder.core.managedBuildNature</nature>
|
||||
<nature>org.eclipse.cdt.managedbuilder.core.ScannerConfigNature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
24
LICENSE
Normal file
24
LICENSE
Normal file
|
@ -0,0 +1,24 @@
|
|||
MIT License
|
||||
|
||||
Copyright (c) 2019 AGORISE, LTD.
|
||||
An International Business Company, Cyprus Reg# ΗΕ375959
|
||||
|
||||
Contains works from 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.
|
7
Makefile
Normal file
7
Makefile
Normal file
|
@ -0,0 +1,7 @@
|
|||
clean:
|
||||
cd test; make clean;
|
||||
cd multicodec; make clean;
|
||||
|
||||
all:
|
||||
cd multicodec; make all;
|
||||
cd test; make all;
|
|
@ -1,2 +1,6 @@
|
|||
# c-multicodec
|
||||
multicodec for IPFS in C.
|
||||
|
||||
An implementation of multicodec in C. For the spec, check out https://github.com/multiformats/multicodec
|
||||
|
||||
For an implmentation in go, see https://github.com/multiformats/go-multicodec
|
||||
|
|
182
header.h
Normal file
182
header.h
Normal file
|
@ -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
|
12
multicodec/Makefile
Normal file
12
multicodec/Makefile
Normal file
|
@ -0,0 +1,12 @@
|
|||
CC = gcc
|
||||
CFLAGS = -O0
|
||||
DEPS = multicodec.h
|
||||
OBJS = multicodec.o
|
||||
|
||||
%.o: %.c $(DEPS)
|
||||
$(CC) -c -o $@ $< $(CFLAGS)
|
||||
|
||||
all: $(OBJS)
|
||||
|
||||
clean:
|
||||
rm -f multicodec.o
|
9
multicodec/multicodec.c
Normal file
9
multicodec/multicodec.c
Normal file
|
@ -0,0 +1,9 @@
|
|||
#include "multicodec.h"
|
||||
|
||||
int multicodec_encode(const char* codecName, char* inData, size_t inDataLength, char* outData, size_t maxOutDataLength) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int multicodec_decode(char* inData, size_t inDataLength, char* outData, size_t maxOutDataLength) {
|
||||
return 0;
|
||||
}
|
35
multicodec/multicodec.h
Normal file
35
multicodec/multicodec.h
Normal file
|
@ -0,0 +1,35 @@
|
|||
/**
|
||||
* An implementation of multicodec
|
||||
* See https://github.com/multiformats/multicodec
|
||||
*/
|
||||
|
||||
#ifndef __MULTICODEC_H__
|
||||
#define __MULTICODEC_H__
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#define MULTICODEC_RAW_BINARY 0x052f6269632f
|
||||
#define MULTICODEC_ASCII_BASE_2 0x042f62322f
|
||||
|
||||
/**
|
||||
* Encode a char array using the specified encoding
|
||||
* @param codecName a standard name of the codec
|
||||
* @param inData the incoming data to be encoded
|
||||
* @param inDataLength the length of the inData
|
||||
* @param outData a place to store the outgoing data that is at least maxOutDataLength in size
|
||||
* @param maxOutDataLength the max size for the output
|
||||
* @returns an error code or 0 if all is well
|
||||
*/
|
||||
int multicodec_encode(const char* codecName, char* inData, size_t inDataLength, char* outData, size_t maxOutDataLength);
|
||||
|
||||
/**
|
||||
* Decode a multicodec encoded data array
|
||||
* @param inData the data to be decoded
|
||||
* @param inDataLength the length of the data to be encoded
|
||||
* @param outData the array to put the results in
|
||||
* @param maxOutDataLength the maximum size of the data
|
||||
* @returns an error code or 0 if all is well
|
||||
*/
|
||||
int multicodec_decode(char* inData, size_t inDataLength, char* outData, size_t maxOutDataLength);
|
||||
|
||||
#endif // __MULTICODEC_H__
|
17
test/Makefile
Normal file
17
test/Makefile
Normal file
|
@ -0,0 +1,17 @@
|
|||
CC = gcc
|
||||
CFLAGS = -O0
|
||||
DEPS = ../multicodec/multicodec.h
|
||||
OBJS = test_multicodec.o ../multicodec/multicodec.o
|
||||
|
||||
%.o: %.c $(DEPS)
|
||||
$(CC) -c -o $@ $< $(CFLAGS)
|
||||
|
||||
test_multicodec: $(OBJS)
|
||||
|
||||
$(CC) -o $@ $(OBJS)
|
||||
|
||||
all: test_multicodec
|
||||
|
||||
clean:
|
||||
rm -f test_multicodec;
|
||||
rm -f test_multicodec.o;
|
25
test/test_multicodec.c
Normal file
25
test/test_multicodec.c
Normal file
|
@ -0,0 +1,25 @@
|
|||
#include "../multicodec/multicodec.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int testEncode() {
|
||||
char inData[10];
|
||||
char outData[15];
|
||||
multicodec_encode("ascii", inData, 10, outData, 15);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int testit(const char* name, int (*func)(void)) {
|
||||
printf("Running function %s... ", name);
|
||||
int retVal = func();
|
||||
if (retVal == 0)
|
||||
printf("Success\n");
|
||||
else
|
||||
printf("Failed\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
testit("testEncode", testEncode);
|
||||
return 0;
|
||||
}
|
8
varint.c
Normal file
8
varint.c
Normal file
|
@ -0,0 +1,8 @@
|
|||
#include "varint.h"
|
||||
|
||||
|
||||
DEFN_ENCODER(32)
|
||||
DEFN_DECODER(32)
|
||||
|
||||
DEFN_ENCODER(64)
|
||||
DEFN_DECODER(64)
|
51
varint.h
Normal file
51
varint.h
Normal file
|
@ -0,0 +1,51 @@
|
|||
#ifndef VARINT
|
||||
#define VARINT
|
||||
#include <stddef.h> /* size_t */
|
||||
#include <stdint.h> /* 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
|
365
vhutils.h
Normal file
365
vhutils.h
Normal file
|
@ -0,0 +1,365 @@
|
|||
#ifndef VARHEXUTILS
|
||||
#define VARHEXUTILS
|
||||
|
||||
#include <stdio.h>
|
||||
#include <inttypes.h>
|
||||
#include "varint.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#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<len;i++,j+=2)
|
||||
{
|
||||
ch = string[i];
|
||||
|
||||
if( ch >= 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<size;cz++)
|
||||
{
|
||||
buf[cz] = xbuf[cz];
|
||||
}
|
||||
//
|
||||
if(crrz01!=NULL)
|
||||
{
|
||||
char * crrz1 = NULL;
|
||||
crrz1 = crrz01;
|
||||
char conv_proc[800]="\0";
|
||||
int i;
|
||||
for(i=0; i < (size*2); i++)
|
||||
{
|
||||
if(buf[i]!='\0')
|
||||
{
|
||||
sprintf (conv_proc, "%02X", buf[i]);
|
||||
//printf("%d:%d\n",i, buf[i]);
|
||||
strcat(crrz1, conv_proc);
|
||||
}
|
||||
}
|
||||
crrz1 = NULL;
|
||||
}
|
||||
}
|
||||
char * Var_To_Hex(int realsize, uint8_t * TOHEXINPUT) //VAR[binformat] TO HEX
|
||||
{
|
||||
for(int ix=realsize;ix<400;ix++)
|
||||
{
|
||||
TOHEXINPUT[ix] = '\0';
|
||||
}
|
||||
if(TOHEXINPUT != NULL)
|
||||
{
|
||||
static char convert_resultz1[800]="\0";
|
||||
bzero(convert_resultz1,800);
|
||||
vthconvert(realsize, convert_resultz1, TOHEXINPUT);
|
||||
return convert_resultz1;
|
||||
}
|
||||
}
|
||||
uint8_t * Hex_To_Var(char * Hexstr) //HEX TO VAR[BINFORMAT]
|
||||
{
|
||||
static uint8_t buffy_HEX[400] = {0};
|
||||
bzero(buffy_HEX,400);
|
||||
int i;
|
||||
char codo[800] = "\0";
|
||||
bzero(codo,800);
|
||||
strcpy(codo, Hexstr);
|
||||
char code[3];
|
||||
bzero(code,3);
|
||||
code[3]='\0';
|
||||
int x = 0;
|
||||
int fori001=0;
|
||||
for(fori001=0;fori001<800;fori001++)
|
||||
{
|
||||
strncpy(&code[0],&codo[fori001],1);
|
||||
strncpy(&code[1],&codo[fori001+1],1);
|
||||
char *ck = NULL;
|
||||
uint64_t lu = 0;
|
||||
lu=strtoul(code, &ck, 16);
|
||||
buffy_HEX[x] = lu;
|
||||
//printf("%s - %lu\n",code,lu);
|
||||
fori001++;
|
||||
x++;
|
||||
}
|
||||
return buffy_HEX;
|
||||
}
|
||||
//
|
||||
void convert(char * convert_result, uint8_t * buf) //Both of them read them properly.
|
||||
{
|
||||
char conv_proc[800]="\0";
|
||||
bzero(conv_proc,800);
|
||||
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);
|
||||
}
|
||||
}
|
||||
char * Num_To_HexVar_64(uint64_t TOHVINPUT) //UINT64 TO HEXIFIED VAR
|
||||
{ //Code to varint - py
|
||||
static char convert_result[800]="\0";//Note that the hex resulted from this will differ from py
|
||||
bzero(convert_result,800);
|
||||
memset(convert_result,0,sizeof(convert_result));//But if you make sure the string is always 20 chars in size
|
||||
uint8_t buf[400] = {0};
|
||||
bzero(buf,400);
|
||||
uvarint_encode64(TOHVINPUT, buf, 800);
|
||||
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++)
|
||||
{
|
||||
sprintf (conv_proc, "%02X", buf[i]);
|
||||
//printf("aaaaaaaaaaah%d:%d\n",i, buf[i]);
|
||||
strcat(convert_result2, conv_proc);
|
||||
}
|
||||
buf = NULL;
|
||||
}
|
||||
char * Num_To_HexVar_32(uint32_t TOHVINPUT) //UINT32 TO HEXIFIED VAR
|
||||
{ //Code to varint - py
|
||||
static char convert_result2[3]="\0";
|
||||
bzero(convert_result2,3);
|
||||
convert_result2[2] = '\0';
|
||||
memset(convert_result2,0,sizeof(convert_result2));
|
||||
uint8_t buf[1] = {0};
|
||||
bzero(buf,1);
|
||||
uvarint_encode32(TOHVINPUT, buf, 1);
|
||||
convert2(convert_result2,buf);
|
||||
return convert_result2;
|
||||
}
|
||||
|
||||
uint64_t HexVar_To_Num_64(char * theHEXstring) //HEXIFIED VAR TO UINT64_T
|
||||
{ //Varint to code - py
|
||||
uint8_t buffy[400] = {0};
|
||||
char codo[800] = "\0";
|
||||
bzero(codo,800);
|
||||
strcpy(codo, theHEXstring);
|
||||
char code[3] = "\0";
|
||||
int x = 0;
|
||||
for(int i= 0;i<399;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, 400, &decoded);
|
||||
return decoded;
|
||||
}
|
||||
uint32_t HexVar_To_Num_32(char theHEXstring[]) //HEXIFIED VAR TO UINT32_T
|
||||
{ //Varint to code py
|
||||
uint8_t buffy[400] = {0};
|
||||
bzero(buffy,400);
|
||||
char codo[800] = "\0";
|
||||
bzero(codo,800);
|
||||
strcpy(codo, theHEXstring);
|
||||
char code[3] = "\0";
|
||||
bzero(code,3);
|
||||
code[3] = '\0';
|
||||
int x = 0;
|
||||
for(int i= 0;i<399;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
|
Loading…
Reference in a new issue