From 8f44c857db04812267928f69b69177ab8597949c Mon Sep 17 00:00:00 2001 From: jmjatlanta Date: Fri, 23 Dec 2016 20:12:51 -0500 Subject: [PATCH] Hashes match on large files --- importer/importer.c | 34 +++------------------------------- merkledag/merkledag.c | 1 + merkledag/node.c | 17 +++++++++-------- 3 files changed, 13 insertions(+), 39 deletions(-) diff --git a/importer/importer.c b/importer/importer.c index 89770f6..e438971 100644 --- a/importer/importer.c +++ b/importer/importer.c @@ -43,28 +43,6 @@ int ipfs_importer_add_filesize_to_data_section(struct Node* node, size_t bytes_r return 1; } -int test_hash(unsigned char* protobuf, size_t protobuf_length) { - size_t hash_size = 32; - unsigned char hash[32]; - if (hash == NULL) { - return 0; - } - if (libp2p_crypto_hashing_sha256(protobuf, protobuf_length, &hash[0]) == 0) { - return 0; - } - - // turn it into base58 - size_t buffer_len = 100; - unsigned char buffer[buffer_len]; - int retVal = ipfs_cid_hash_to_base58(hash, hash_size, buffer, buffer_len); - if (retVal == 0) { - printf("Unable to generate hash\n"); - return 0; - } - printf(" hash generated from %lu: %s\n", protobuf_length, buffer); - return 1; -} - /** * read the next chunk of bytes, create a node, and add a link to the node in the passed-in node * @param file the file handle @@ -80,10 +58,6 @@ size_t ipfs_import_chunk(FILE* file, struct Node* parent_node, struct FSRepo* fs struct Node* new_node = NULL; struct NodeLink* new_link = NULL; - //JMJ - printf("Raw from the file"); - test_hash(buffer, bytes_read); - // put the file bits into a new UnixFS file if (ipfs_unixfs_new(&new_unixfs) == 0) return 0; @@ -105,9 +79,7 @@ size_t ipfs_import_chunk(FILE* file, struct Node* parent_node, struct FSRepo* fs ipfs_unixfs_free(new_unixfs); return 0; } - //JMJ - printf("unixfs protobuf"); - test_hash(protobuf, bytes_written); + // we're done with the UnixFS object ipfs_unixfs_free(new_unixfs); @@ -125,7 +97,7 @@ size_t ipfs_import_chunk(FILE* file, struct Node* parent_node, struct FSRepo* fs } // put link in parent node - if (ipfs_node_link_create("", new_node->hash, new_node->hash_size, &new_link) == 0) { + if (ipfs_node_link_create(NULL, new_node->hash, new_node->hash_size, &new_link) == 0) { ipfs_node_free(new_node); return 0; } @@ -157,7 +129,7 @@ size_t ipfs_import_chunk(FILE* file, struct Node* parent_node, struct FSRepo* fs } // put link in parent node - if (ipfs_node_link_create("", new_node->hash, new_node->hash_size, &new_link) == 0) { + if (ipfs_node_link_create(NULL, new_node->hash, new_node->hash_size, &new_link) == 0) { ipfs_node_free(new_node); return 0; } diff --git a/merkledag/merkledag.c b/merkledag/merkledag.c index 09b353e..7baf71c 100644 --- a/merkledag/merkledag.c +++ b/merkledag/merkledag.c @@ -11,6 +11,7 @@ #include "ipfs/merkledag/merkledag.h" #include "ipfs/unixfs/unixfs.h" + /*** * Adds a node to the dagService and blockService * @param node the node to add diff --git a/merkledag/node.c b/merkledag/node.c index cca0c24..6b336a8 100644 --- a/merkledag/node.c +++ b/merkledag/node.c @@ -30,7 +30,7 @@ enum WireType ipfs_node_link_message_fields[] = { WIRETYPE_LENGTH_DELIMITED, WIR */ int ipfs_node_link_create(char * name, unsigned char * ahash, size_t hash_size, struct NodeLink** node_link) { - *node_link = malloc(sizeof(struct NodeLink)); + ipfs_node_link_new(node_link); if (*node_link == NULL) return 0; @@ -40,12 +40,14 @@ int ipfs_node_link_create(char * name, unsigned char * ahash, size_t hash_size, link->hash = (unsigned char*)malloc(hash_size); memcpy(link->hash, ahash, hash_size); // name - link->name = malloc(strlen(name) + 1); - if ( link->name == NULL) { - free(link); - return 0; + if (name != NULL && strlen(name) > 0) { + link->name = malloc(strlen(name) + 1); + if ( link->name == NULL) { + free(link); + return 0; + } + strcpy(link->name, name); } - strcpy(link->name, name); // t_size link->t_size = 0; // other, non-protobuffed data @@ -185,8 +187,7 @@ int ipfs_node_link_protobuf_decode(unsigned char* buffer, size_t buffer_length, goto exit; link->hash_size = hash_size - 2; link->hash = (unsigned char*)malloc(link->hash_size); - strncpy((char*)link->hash, (char*)hash, link->hash_size); - link->hash[link->hash_size-1] = 0; + memcpy((char*)link->hash, (char*)&hash[2], link->hash_size); free(hash); pos += bytes_read; break;