- memory allocation check in ByteBuffer_create and apply fomatter (LIB61850-517)

v1.6
Michael Zillgith 1 month ago
parent 81ba6534fd
commit 1fc842037c

@ -21,89 +21,104 @@
* See COPYING file for the complete license text. * See COPYING file for the complete license text.
*/ */
#include "libiec61850_platform_includes.h"
#include "byte_buffer.h" #include "byte_buffer.h"
#include "libiec61850_platform_includes.h"
ByteBuffer* ByteBuffer*
ByteBuffer_create(ByteBuffer* self, int maxSize) ByteBuffer_create(ByteBuffer* self, int maxSize)
{ {
if (self == NULL) { if (self == NULL)
self = (ByteBuffer*) GLOBAL_CALLOC(1, sizeof(ByteBuffer)); {
} self = (ByteBuffer*)GLOBAL_CALLOC(1, sizeof(ByteBuffer));
}
self->buffer = (uint8_t*) GLOBAL_CALLOC(maxSize, sizeof(uint8_t)); if (self)
self->maxSize = maxSize; {
self->size = 0; self->buffer = (uint8_t*)GLOBAL_CALLOC(maxSize, sizeof(uint8_t));
if (self->buffer == NULL)
{
GLOBAL_FREEMEM(self);
self = NULL;
}
else
{
self->maxSize = maxSize;
self->size = 0;
}
}
return self; return self;
} }
void void
ByteBuffer_destroy(ByteBuffer* self) ByteBuffer_destroy(ByteBuffer* self)
{ {
GLOBAL_FREEMEM(self->buffer); GLOBAL_FREEMEM(self->buffer);
GLOBAL_FREEMEM(self); GLOBAL_FREEMEM(self);
} }
void void
ByteBuffer_wrap(ByteBuffer* self, uint8_t* buf, int size, int maxSize) ByteBuffer_wrap(ByteBuffer* self, uint8_t* buf, int size, int maxSize)
{ {
self->buffer = buf; self->buffer = buf;
self->size = size; self->size = size;
self->maxSize = maxSize; self->maxSize = maxSize;
} }
int int
ByteBuffer_append(ByteBuffer* self, uint8_t* data, int dataSize) ByteBuffer_append(ByteBuffer* self, uint8_t* data, int dataSize)
{ {
if (self->size + dataSize <= self->maxSize) { if (self->size + dataSize <= self->maxSize)
memcpy(self->buffer + self->size, data, dataSize); {
self->size += dataSize; memcpy(self->buffer + self->size, data, dataSize);
return dataSize; self->size += dataSize;
} return dataSize;
else { }
return -1; else
} {
return -1;
}
} }
int int
ByteBuffer_appendByte(ByteBuffer* self, uint8_t byte) ByteBuffer_appendByte(ByteBuffer* self, uint8_t byte)
{ {
if (self->size < self->maxSize) { if (self->size < self->maxSize)
self->buffer[self->size] = byte; {
self->size ++; self->buffer[self->size] = byte;
return 1; self->size++;
} return 1;
else }
return 0; else
return 0;
} }
uint8_t* uint8_t*
ByteBuffer_getBuffer(ByteBuffer* self) ByteBuffer_getBuffer(ByteBuffer* self)
{ {
return self->buffer; return self->buffer;
} }
int int
ByteBuffer_getMaxSize(ByteBuffer* self) ByteBuffer_getMaxSize(ByteBuffer* self)
{ {
return self->maxSize; return self->maxSize;
} }
int int
ByteBuffer_getSize(ByteBuffer* self) ByteBuffer_getSize(ByteBuffer* self)
{ {
return self->size; return self->size;
} }
int int
ByteBuffer_setSize(ByteBuffer* self, int size) ByteBuffer_setSize(ByteBuffer* self, int size)
{ {
if (size <= self->maxSize) if (size <= self->maxSize)
self->size = size; self->size = size;
return self->size; return self->size;
} }
#if 0 #if 0

Loading…
Cancel
Save