c-ipfs/unixfs/format.c.old

43 lines
1.1 KiB
C
Raw Normal View History

2016-12-05 15:50:17 +00:00
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
2016-12-05 15:50:17 +00:00
#include "ipfs/unixfs/format.h"
2016-12-05 15:50:17 +00:00
/**
* 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));
2016-12-05 15:50:17 +00:00
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;
}