More work on Cid

This commit is contained in:
John Jones 2016-11-14 18:02:18 -05:00
parent 02c5552cd6
commit 77e2cc279f
5 changed files with 48 additions and 23 deletions

2
.gitignore vendored
View file

@ -8,4 +8,4 @@
.cproject
.project
.settings/language.settings.xml
test/test_ipfs

View file

@ -1,5 +1,5 @@
CC = gcc
CFLAGS = -O0 -I../include -I../../c-libp2p/include -I../../c-multihash/include
CFLAGS = -O0 -I../include -I../../c-libp2p/include -I../../c-multihash/include -I../../c-multiaddr/include
ifdef DEBUG
CFLAGS += -g3

View file

@ -11,6 +11,7 @@
#include "libp2p/crypto/encoding/base58.h"
#include "ipfs/multibase/multibase.h"
#include "mh/multihash.h"
#include "multiaddr/varint.h"
/**
@ -104,34 +105,29 @@ int cid_cast(unsigned char* incoming, size_t incoming_size, struct Cid* cid) {
cid->hash_length = mh_multihash_length(incoming, incoming_size);
cid->codec = CID_PROTOBUF;
cid->version = 0;
// allocate memory for hash
cid->hash = malloc(cid->hash_length);
if (cid->hash == NULL)
return 0;
mh_multihash_digest(incoming, incoming_size, &cid->hash, &cid->hash_length);
return 1;
}
/*
*TODO: Implement this
// This is not a multihash. Try to peel the information out of the bytes
// This is not a multihash. Perhaps it is using varints. Try to peel the information out of the bytes.
// first the version
int pos = 0, retVal = 0;
int num_bytes = 0;
retVal = varint_to_int(&incoming[pos], &cid->version, &num_bytes);
if (retVal == 0)
size_t num_bytes = 0;
num_bytes = uvarint_decode32(&incoming[pos], incoming_size - pos, &cid->version);
if (num_bytes < 0 || cid->version > 1 || cid->version < 0)
return 0;
pos = num_bytes;
// now the codec
retVal = varint_to_int(&incoming[pos], &cid->codec, &num_bytes);
if (retVal == 0)
uint32_t codec = 0;
num_bytes = uvarint_decode32(&incoming[pos], incoming_size - pos, &codec);
if (num_bytes < 0)
return 0;
cid->codec = codec;
pos += num_bytes;
// now what is left
cid->hash_length = incoming_size - pos;
// TODO: allocate memory
memcpy(cid->hash, &incoming[pos], cid->hash_length);
*/
return 0;
cid->hash = &incoming[pos];
return 1;
}

View file

@ -1,6 +1,6 @@
CC = gcc
CFLAGS = -O0 -I../include -I../../c-libp2p/include -I../../c-multihash/include -g3
LFLAGS = -L../../c-libp2p -L../../c-multihash -lp2p -lm -lmultihash
CFLAGS = -O0 -I../include -I../../c-libp2p/include -I../../c-multihash/include -I../../c-multiaddr/ -g3
LFLAGS = -L../../c-libp2p -L../../c-multihash -L../../c-multiaddr -lp2p -lm -lmultihash -lmultiaddr
DEPS = cmd/ipfs/test_init.h repo/test_repo_bootstrap_peers.h repo/test_repo_config.h repo/test_repo_identity.h cid/test_cid.h
OBJS = testit.o ../cmd/ipfs/init.o ../commands/argument.o ../commands/command_option.o \
../commands/command.o ../commands/cli/parse.o ../core/builder.o ../repo/fsrepo/fs_repo.o \

View file

@ -70,10 +70,39 @@ int test_cid_cast_multihash() {
}
int test_cid_cast_non_multihash() {
// this should turn a multibase encoded string into a cid struct
// first, build a multibase encoded string
// first, build a hash
char* string_to_hash = "Hello, World!";
unsigned char hashed[32];
memset(hashed, 0, 32);
// hash the string
libp2p_crypto_hashing_sha256(string_to_hash, strlen(string_to_hash), hashed);
// now make it a hash with a version and codec embedded in varints before the hash
size_t array_size = 34; // 32 for the hash, 2 for the 2 varints
unsigned char array[array_size];
memset(array, 0, array_size);
// first the version
array[0] = 0;
// then the codec
array[1] = CID_PROTOBUF;
// then the hash
memcpy(&array[2], hashed, 32);
// now call cast
struct Cid cid;
int retVal = cid_cast(array, array_size, &cid);
if (retVal == 0)
return 0;
// check results
return 0;
if (cid.version != 0)
return 0;
if (cid.hash_length != 32)
return 0;
if (cid.codec != CID_PROTOBUF)
return 0;
if (strncmp(hashed, cid.hash, 32) != 0)
return 0;
return 1;
}