#include "buf.h" buf_t* buf_init() { buf_t *ret = NULL; ret = malloc( sizeof(buf_t) ); memset(ret, 0, sizeof(buf_t)); return ret; } int buf_size( buf_t *bf, int size ) { if (bf->buf != NULL) { printf("Buffer should be empty\n"); return -1; } bf->buf = malloc( size ); bf->size = size; bf->buf_size = size; return 0; } int buf_used_size( buf_t *bf, int size ) { if ( size > bf->buf_size ) { printf("Cannot set buffer size more then buffer itself\n"); return -1; } if ( size < 0) { printf("Cannot set buffer less then zero\n"); return -1; } bf->size = size; return 0; } int buf_resize( buf_t *bf, int size ) { uint8_t *n=NULL; if ( size < 1) { printf("Cannot set buffer size less then 1\n"); return -1; } n = realloc( bf->buf, size ); //resized data is not nullified at the end? if ( n == NULL ) { printf("Buffer realloc failed\n"); return -1; } if ( size > bf->buf_size ) { memset(bf->buf+bf->buf_size-1, 0, size-(bf->buf_size)); //hope its correct } bf->buf_size = size; bf->buf = n; if (bf->size > bf->buf_size) { bf->size = bf->buf_size; } return 0; } int buf_zero( buf_t *bf ) { if ( bf->buf == NULL ) return -1; memset( bf->buf, 0, bf->buf_size ); return 0; } void buf_free( buf_t *bf ) { if ( bf == NULL ) return; if ( bf->buf != NULL ) { free( bf->buf ); bf->buf = NULL; } free( bf ); bf = NULL; }