in src/buffer.c [203:255]
int BUFFER_append_build(BUFFER_HANDLE handle, const unsigned char* source, size_t size)
{
int result;
if (handle == NULL || source == NULL || size == 0)
{
/* Codes_SRS_BUFFER_01_006: [ BUFFER_append_build shall return nonzero if handle or source are NULL or if size is 0. ] */
LogError("BUFFER_append_build failed invalid parameter handle: %p, source: %p, size: %lu", handle, source, (unsigned long)size);
result = MU_FAILURE;
}
else
{
if (handle->buffer == NULL)
{
/* Codes_SRS_BUFFER_01_007: [ if handle->buffer is NULL BUFFER_append_build shall allocate the a buffer of size bytes... ] */
if (BUFFER_safemalloc(handle, size) != 0)
{
/* Codes_SRS_BUFFER_07_035: [ If any error is encountered BUFFER_append_build shall return a non-null value. ] */
LogError("Failure with BUFFER_safemalloc");
result = MU_FAILURE;
}
else
{
/* Codes_SRS_BUFFER_01_008: [ ... and copy the contents of source to handle->buffer. ] */
(void)memcpy(handle->buffer, source, size);
/* Codes_SRS_BUFFER_07_034: [ On success BUFFER_append_build shall return 0 ] */
result = 0;
}
}
else
{
/* Codes_SRS_BUFFER_01_009: [ if handle->buffer is not NULL BUFFER_append_build shall realloc the buffer to be the handle->size + size ] */
unsigned char* temp = realloc_flex(handle->buffer, handle->size, size, 1);
if (temp == NULL)
{
/* Codes_SRS_BUFFER_07_035: [ If any error is encountered BUFFER_append_build shall return a non-null value. ] */
LogError("Failure reallocating temporary buffer realloc_flex(handle->buffer=%p, handle->size=%zu, size=%zu, 1);",
handle->buffer, handle->size, size);
result = MU_FAILURE;
}
else
{
/* Codes_SRS_BUFFER_01_010: [ ... and copy the contents of source to the end of the buffer. ] */
handle->buffer = temp;
// Append the BUFFER
(void)memcpy(&handle->buffer[handle->size], source, size);
handle->size += size;
/* Codes_SRS_BUFFER_07_034: [ On success BUFFER_append_build shall return 0 ] */
result = 0;
}
}
}
return result;
}