obiblob.c 2.32 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/****************************************************************************
 * Obiblob functions                                                       *
 ****************************************************************************/

/**
 * @file obiblob.c
 * @author Celine Mercier
 * @date April 11th 2016
 * @brief Functions handling Obiblob structures.
 */


#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
16
#include <math.h>
17

18
#include "obiblob.h"
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
#include "obierrno.h"
#include "obitypes.h"	// For byte_t type
#include "obidebug.h"


#define DEBUG_LEVEL 0	// TODO has to be defined somewhere else (cython compil flag?)


// TODO: endianness problem?


Obi_blob_p obi_blob(byte_t* encoded_value, uint8_t element_size, int32_t length_encoded_value, int32_t length_decoded_value)
{
	Obi_blob_p blob;

	// Allocate the memory for the blob structure
35
	blob = (Obi_blob_p) calloc(sizeof(Obi_blob_t) + length_encoded_value, sizeof(byte_t));
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
	if (blob == NULL)
	{
		obi_set_errno(OBI_MALLOC_ERROR);
		obidebug(1, "\nError allocating memory for a blob");
		return NULL;
	}

	// Store the number of bits on which each element is encoded
	blob->element_size = element_size;

	// Store the length (in bytes) of the encoded value
	blob->length_encoded_value = length_encoded_value;

	// Store the initial length (in bytes) of the decoded value
	blob->length_decoded_value = length_decoded_value;

	// Store the encoded value
	memcpy(blob->value, encoded_value, length_encoded_value);

	return blob;
}

58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

int obi_blob_compare(Obi_blob_p value_1, Obi_blob_p value_2)
{
	int 	comp;
	int32_t b;

	if (value_1->element_size != value_2->element_size)
		return (value_1->element_size - value_2->element_size);

	if (value_1->length_encoded_value != value_2->length_encoded_value)
		return (value_1->length_encoded_value - value_2->length_encoded_value);

	if (value_1->element_size != ELEMENT_SIZE_STR) // because if so, length_decoded_value == length_encoded_value
	{
		if (value_1->length_decoded_value != value_2->length_decoded_value)
			return (value_1->length_decoded_value - value_2->length_decoded_value);
	}

	b = 0;
	comp = 0;
	while (!comp && (b < value_1->length_encoded_value))
	{
		comp = *((value_1->value)+b) - *((value_2->value)+b);
		b++;
	}
	return comp;
}


int obi_blob_sizeof(Obi_blob_p value)
{
	return (sizeof(Obi_blob_t) + (value->length_encoded_value));
}