Azure IoT common library
Dependents: STM32F746_iothub_client_sample_mqtt f767zi_mqtt iothub_client_sample_amqp iothub_client_sample_http ... more
Diff: buffer.c
- Revision:
- 10:1be0bc9a9deb
- Parent:
- 6:c55b013dfc2a
- Child:
- 19:2e0811512ceb
diff -r 079c39803432 -r 1be0bc9a9deb buffer.c --- a/buffer.c Fri Aug 12 13:49:58 2016 -0700 +++ b/buffer.c Fri Aug 26 12:59:40 2016 -0700 @@ -38,6 +38,29 @@ return (BUFFER_HANDLE)temp; } +static int BUFFER_safemalloc(BUFFER* handleptr, size_t size) +{ + int result; + size_t sizetomalloc = size; + if (size == 0) + { + sizetomalloc = 1; + } + handleptr->buffer = (unsigned char*)malloc(sizetomalloc); + if (handleptr->buffer == NULL) + { + /*Codes_SRS_BUFFER_02_003: [If allocating memory fails, then BUFFER_create shall return NULL.]*/ + result = __LINE__; + } + else + { + // we still consider the real buffer size is 0 + handleptr->size = size; + result = 0; + } + return result; +} + BUFFER_HANDLE BUFFER_create(const unsigned char* source, size_t size) { BUFFER* result; @@ -57,10 +80,10 @@ } else { - result->buffer = (unsigned char*)malloc(size); - if (result->buffer == NULL) + /* Codes_SRS_BUFFER_02_005: [If size parameter is 0 then 1 byte of memory shall be allocated yet size of the buffer shall be set to 0.]*/ + if (BUFFER_safemalloc(result, size) != 0) { - /*Codes_SRS_BUFFER_02_003: [If allocating memory fails, then BUFFER_create shall return NULL.]*/ + LogError("unable to BUFFER_safemalloc "); free(result); result = NULL; } @@ -68,7 +91,6 @@ { /*Codes_SRS_BUFFER_02_004: [Otherwise, BUFFER_create shall return a non-NULL handle.] */ memcpy(result->buffer, source, size); - result->size = size; } } } @@ -309,19 +331,29 @@ } else { - unsigned char* temp = (unsigned char*)realloc(b1->buffer, b1->size + b2->size); - if (temp == NULL) + if (b2->size ==0) { - /* Codes_SRS_BUFFER_07_023: [BUFFER_append shall return a nonzero upon any error that is encountered.] */ - result = __LINE__; + // b2->size = 0, whatever b1->size is, do nothing + result = 0; } else { - b1->buffer = temp; - // Append the BUFFER - memcpy(&b1->buffer[b1->size], b2->buffer, b2->size); - b1->size += b2->size; - result = 0; + // b2->size != 0, whatever b1->size is + unsigned char* temp = (unsigned char*)realloc(b1->buffer, b1->size + b2->size); + if (temp == NULL) + { + /* Codes_SRS_BUFFER_07_023: [BUFFER_append shall return a nonzero upon any error that is encountered.] */ + result = __LINE__; + } + else + { + /* Codes_SRS_BUFFER_07_024: [BUFFER_append concatenates b2 onto b1 without modifying b2 and shall return zero on success.]*/ + b1->buffer = temp; + // Append the BUFFER + (void)memcpy(&b1->buffer[b1->size], b2->buffer, b2->size); + b1->size += b2->size; + result = 0; + } } } } @@ -333,7 +365,7 @@ int result; if ((handle1 == NULL) || (handle2 == NULL) || (handle1 == handle2)) { - /* : [BUFFER_append shall return a nonzero upon any error that is encountered.] */ + /* Codes_SRS_BUFFER_01_005: [ BUFFER_prepend shall return a non-zero upon value any error that is encountered. ]*/ result = __LINE__; } else @@ -342,31 +374,43 @@ BUFFER* b2 = (BUFFER*)handle2; if (b1->buffer == NULL) { - /* : [BUFFER_append shall return a nonzero upon any error that is encountered.] */ + /* Codes_SRS_BUFFER_01_005: [ BUFFER_prepend shall return a non-zero upon value any error that is encountered. ]*/ result = __LINE__; } else if (b2->buffer == NULL) { - /* : [BUFFER_append shall return a nonzero upon any error that is encountered.] */ + /* Codes_SRS_BUFFER_01_005: [ BUFFER_prepend shall return a non-zero upon value any error that is encountered. ]*/ result = __LINE__; } else { - unsigned char* temp = (unsigned char*)malloc(b1->size + b2->size); - if (temp == NULL) + //put b2 ahead of b1: [b2][b1], return b1 + if (b2->size ==0) { - /* : [BUFFER_append shall return a nonzero upon any error that is encountered.] */ - result = __LINE__; + // do nothing + result = 0; } else { - // Append the BUFFER - memcpy(temp, b2->buffer, b2->size); - memcpy(&temp[b2->size], b1->buffer, b1->size); - free(b1->buffer); - b1->buffer = temp; - b1->size += b2->size; - result = 0; + // b2->size != 0 + unsigned char* temp = (unsigned char*)malloc(b1->size + b2->size); + if (temp == NULL) + { + /* Codes_SRS_BUFFER_01_005: [ BUFFER_prepend shall return a non-zero upon value any error that is encountered. ]*/ + result = __LINE__; + } + else + { + /* Codes_SRS_BUFFER_01_004: [ BUFFER_prepend concatenates handle1 onto handle2 without modifying handle1 and shall return zero on success. ]*/ + // Append the BUFFER + (void)memcpy(temp, b2->buffer, b2->size); + // start from b1->size to append b1 + (void)memcpy(&temp[b2->size], b1->buffer, b1->size); + free(b1->buffer); + b1->buffer = temp; + b1->size += b2->size; + result = 0; + } } } } @@ -377,16 +421,17 @@ /* Codes_SRS_BUFFER_07_025: [BUFFER_u_char shall return a pointer to the underlying unsigned char*.] */ unsigned char* BUFFER_u_char(BUFFER_HANDLE handle) { + BUFFER* handleData = (BUFFER*)handle; unsigned char* result; - if (handle == NULL) + if (handle == NULL || handleData->size == 0) { /* Codes_SRS_BUFFER_07_026: [BUFFER_u_char shall return NULL for any error that is encountered.] */ + /* Codes_SRS_BUFFER_07_029: [BUFFER_u_char shall return NULL if underlying buffer size is zero.] */ result = NULL; } else { - BUFFER* b = (BUFFER*)handle; - result = b->buffer; + result = handleData->buffer; } return result; } @@ -418,12 +463,11 @@ else { BUFFER* suppliedBuff = (BUFFER*)handle; - BUFFER* b = (BUFFER*)malloc(sizeof(BUFFER) ); + BUFFER* b = (BUFFER*)malloc(sizeof(BUFFER)); if (b != NULL) { - if ( (b->buffer = (unsigned char*)malloc(suppliedBuff->size) ) == NULL) + if (BUFFER_safemalloc(b, suppliedBuff->size) != 0) { - free(b); result = NULL; } else