Added multibase and cid

This commit is contained in:
John Jones 2016-11-13 21:01:51 -05:00
parent 9e303ef810
commit 02c5552cd6
17 changed files with 515 additions and 12 deletions

62
include/ipfs/cid/cid.h Normal file
View file

@ -0,0 +1,62 @@
/***
* A content id
*/
#ifndef __IPFS_CID_CID_H
#define __IPFS_CID_CID_H
#define CID_PROTOBUF 0x70
#define CID_CBOR 0x71
#define CID_RAW 0x72
#define CID_JSON 0x73
#define CID_ETHEREUM_BLOCK 0x90
#define CID_ETHEREUM_TX 0x91
#define CID_BITCOIN_BLOCK 0xb0
#define CID_BITCOIN_TX 0xb1
#define CID_ZCASH_BLOCK 0xc0
#define CID_ZCASH_TX 0xc1
struct Cid {
int version;
char codec;
unsigned char* hash; // a multihash
size_t hash_length;
};
/**
* Create a new CID based on the given hash
* @param version the version
* @param hash the multihash
* @param hash_length the length of the multihash in bytes
* @param codec the codec to be used (NOTE: For version 0, this should be CID_PROTOBUF)
* @param cid where to put the results
* @returns true(1) on success
*/
int cid_new(int version, unsigned char* hash, size_t hash_length, const char codec, struct Cid* cid);
/***
* Free the resources from a Cid
* @param cid the struct
* @returns 1
*/
int cid_free(struct Cid* cid);
/***
* Fill a Cid struct based on a base 58 encoded string
* @param incoming the string
* @param incoming_size the size of the string
* @cid the Cid struct to fill
* @return true(1) on success
*/
int cid_decode_from_string(const unsigned char* incoming, size_t incoming_length, struct Cid* cid);
/***
* Turn a multibase decoded string of bytes into a Cid struct
* @param incoming the multibase decoded array
* @param incoming_size the size of the array
* @param cid the Cid structure to fill
*/
int cid_cast(unsigned char* incoming, size_t incoming_size, struct Cid* cid);
#endif

View file

@ -0,0 +1,47 @@
#include <stdio.h>
#ifndef __IPFS_MULTIBASE_MULTIBASE_H__
#define __IPFS_MULTIBASE_MULTIBASE_H__
// the first digit of data, to determine the encoding used or using
#define MULTIBASE_BASE1 '1'
#define MULTIBASE_BASE2 '0'
#define MULTIBASE_BASE8 '7'
#define MULTIBASE_BASE10 '9'
#define MULTIBASE_BASE16 'f'
#define MULTIBASE_BASE58_FLICKR 'Z'
#define MULTIBASE_BASE58_BTC 'z'
/**
* Encode data in multibase format
* @param base the format to use (i.e. MULTIBASE_BASE58_BTC)
* @param incoming the data to encode
* @param incoming_length the length of the data to encode
* @param results where to put the results
* @param results_max_length the size of the results buffer
* @param results_length the size of the results after being encoded
* @returns true(1) on success
*/
int multibase_encode(const char base, const unsigned char* incoming, size_t incoming_length, unsigned char* results, size_t results_max_length, size_t* results_length);
/***
* Calculates the size of the buffer neccessary to encode the incoming byte array
* @param base the encoding to use
* @param incoming the incoming array of bytes
* @param incoming_length the length of the array in bytes
* @returns the appropriate size of the buffer
*/
int multibase_encode_size(const char base, const unsigned char* incoming, size_t incoming_length);
/**
* Decode data that was encoded in multibase format
* @param incoming the data to decode
* @param incoming_length the length of the data to decode
* @param results where to put the results
* @param results_max_length the size of the results buffer
* @param results_length the size of the results after being encoded
* @returns true(1) on success
*/
int multibase_decode(const unsigned char* incoming, size_t incoming_length, unsigned char* results, size_t results_max_length, size_t* results_length);
#endif

14
include/ipfs/node/node.h Normal file
View file

@ -0,0 +1,14 @@
/**
* An implementation of an IPFS node
* Copying the go-ipfs-node project
*/
#include <stdint.h>
struct Link {
char* name;
uint64_t size;
struct Cid cid; // content id
};