forked from agorise/c-ipfs
914d3caaed
I am attempting to match the storage format of the reference implementation, so as to generate the same hashes.
42 lines
1.1 KiB
C
42 lines
1.1 KiB
C
#include <string.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
#include "ipfs/unixfs/format.h"
|
|
|
|
|
|
/**
|
|
* Copy data into a new struct
|
|
* @param type the type of data
|
|
* @param data the acutal data
|
|
* @param data_length the length of the data array
|
|
* @param result where the struct will be stored
|
|
* @returns true(1) on success
|
|
*/
|
|
int ipfs_unixfs_format_new(enum UnixFSFormatType type, const unsigned char* data,
|
|
size_t data_length, struct UnixFSData** result) {
|
|
struct UnixFSData* new_struct = (struct UnixFSData*)malloc(sizeof(struct UnixFSData));
|
|
if (new_struct == NULL)
|
|
return 0;
|
|
|
|
new_struct->data = (unsigned char*) malloc(sizeof(unsigned char) * data_length);
|
|
if (new_struct->data == NULL) {
|
|
free (new_struct);
|
|
return 0;
|
|
}
|
|
memcpy(new_struct->data, data, data_length);
|
|
new_struct->type = type;
|
|
new_struct->data_length = data_length;
|
|
return 1;
|
|
}
|
|
|
|
/**
|
|
* Free memory allocated by _new
|
|
* @param data the struct
|
|
* @returns true(1) on success
|
|
*/
|
|
int ipfs_unixfs_format_free(struct UnixFSData* unixfs_data) {
|
|
free(unixfs_data->data);
|
|
free(unixfs_data);
|
|
return 0;
|
|
}
|