Azure IoT common library
Dependents: STM32F746_iothub_client_sample_mqtt f767zi_mqtt iothub_client_sample_amqp iothub_client_sample_http ... more
buffer.c@21:b92006c5b9ff, 2017-02-24 (annotated)
- Committer:
- AzureIoTClient
- Date:
- Fri Feb 24 14:01:41 2017 -0800
- Revision:
- 21:b92006c5b9ff
- Parent:
- 19:2e0811512ceb
- Child:
- 27:8656a313842b
1.1.8
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Azure.IoT Build | 0:fa2de1b79154 | 1 | // Copyright (c) Microsoft. All rights reserved. |
Azure.IoT Build | 0:fa2de1b79154 | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
Azure.IoT Build | 0:fa2de1b79154 | 3 | |
Azure.IoT Build | 0:fa2de1b79154 | 4 | #include <stdlib.h> |
Azure.IoT Build | 0:fa2de1b79154 | 5 | #include <stddef.h> |
Azure.IoT Build | 0:fa2de1b79154 | 6 | #include <string.h> |
AzureIoTClient | 19:2e0811512ceb | 7 | #include "azure_c_shared_utility/gballoc.h" |
Azure.IoT Build | 0:fa2de1b79154 | 8 | #include "azure_c_shared_utility/buffer_.h" |
AzureIoTClient | 21:b92006c5b9ff | 9 | #include "azure_c_shared_utility/optimize_size.h" |
Azure.IoT Build | 6:c55b013dfc2a | 10 | #include "azure_c_shared_utility/xlogging.h" |
Azure.IoT Build | 0:fa2de1b79154 | 11 | |
Azure.IoT Build | 0:fa2de1b79154 | 12 | typedef struct BUFFER_TAG |
Azure.IoT Build | 0:fa2de1b79154 | 13 | { |
Azure.IoT Build | 0:fa2de1b79154 | 14 | unsigned char* buffer; |
Azure.IoT Build | 0:fa2de1b79154 | 15 | size_t size; |
Azure.IoT Build | 0:fa2de1b79154 | 16 | }BUFFER; |
Azure.IoT Build | 0:fa2de1b79154 | 17 | |
Azure.IoT Build | 0:fa2de1b79154 | 18 | /* Codes_SRS_BUFFER_07_001: [BUFFER_new shall allocate a BUFFER_HANDLE that will contain a NULL unsigned char*.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 19 | BUFFER_HANDLE BUFFER_new(void) |
Azure.IoT Build | 0:fa2de1b79154 | 20 | { |
Azure.IoT Build | 0:fa2de1b79154 | 21 | BUFFER* temp = (BUFFER*)malloc(sizeof(BUFFER)); |
Azure.IoT Build | 0:fa2de1b79154 | 22 | /* Codes_SRS_BUFFER_07_002: [BUFFER_new shall return NULL on any error that occurs.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 23 | if (temp != NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 24 | { |
Azure.IoT Build | 0:fa2de1b79154 | 25 | temp->buffer = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 26 | temp->size = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 27 | } |
Azure.IoT Build | 0:fa2de1b79154 | 28 | return (BUFFER_HANDLE)temp; |
Azure.IoT Build | 0:fa2de1b79154 | 29 | } |
Azure.IoT Build | 0:fa2de1b79154 | 30 | |
AzureIoTClient | 10:1be0bc9a9deb | 31 | static int BUFFER_safemalloc(BUFFER* handleptr, size_t size) |
AzureIoTClient | 10:1be0bc9a9deb | 32 | { |
AzureIoTClient | 10:1be0bc9a9deb | 33 | int result; |
AzureIoTClient | 10:1be0bc9a9deb | 34 | size_t sizetomalloc = size; |
AzureIoTClient | 10:1be0bc9a9deb | 35 | if (size == 0) |
AzureIoTClient | 10:1be0bc9a9deb | 36 | { |
AzureIoTClient | 10:1be0bc9a9deb | 37 | sizetomalloc = 1; |
AzureIoTClient | 10:1be0bc9a9deb | 38 | } |
AzureIoTClient | 10:1be0bc9a9deb | 39 | handleptr->buffer = (unsigned char*)malloc(sizetomalloc); |
AzureIoTClient | 10:1be0bc9a9deb | 40 | if (handleptr->buffer == NULL) |
AzureIoTClient | 10:1be0bc9a9deb | 41 | { |
AzureIoTClient | 10:1be0bc9a9deb | 42 | /*Codes_SRS_BUFFER_02_003: [If allocating memory fails, then BUFFER_create shall return NULL.]*/ |
AzureIoTClient | 21:b92006c5b9ff | 43 | result = __FAILURE__; |
AzureIoTClient | 10:1be0bc9a9deb | 44 | } |
AzureIoTClient | 10:1be0bc9a9deb | 45 | else |
AzureIoTClient | 10:1be0bc9a9deb | 46 | { |
AzureIoTClient | 10:1be0bc9a9deb | 47 | // we still consider the real buffer size is 0 |
AzureIoTClient | 10:1be0bc9a9deb | 48 | handleptr->size = size; |
AzureIoTClient | 10:1be0bc9a9deb | 49 | result = 0; |
AzureIoTClient | 10:1be0bc9a9deb | 50 | } |
AzureIoTClient | 10:1be0bc9a9deb | 51 | return result; |
AzureIoTClient | 10:1be0bc9a9deb | 52 | } |
AzureIoTClient | 10:1be0bc9a9deb | 53 | |
Azure.IoT Build | 0:fa2de1b79154 | 54 | BUFFER_HANDLE BUFFER_create(const unsigned char* source, size_t size) |
Azure.IoT Build | 0:fa2de1b79154 | 55 | { |
Azure.IoT Build | 0:fa2de1b79154 | 56 | BUFFER* result; |
Azure.IoT Build | 0:fa2de1b79154 | 57 | /*Codes_SRS_BUFFER_02_001: [If source is NULL then BUFFER_create shall return NULL.]*/ |
Azure.IoT Build | 0:fa2de1b79154 | 58 | if (source == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 59 | { |
Azure.IoT Build | 0:fa2de1b79154 | 60 | result = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 61 | } |
Azure.IoT Build | 0:fa2de1b79154 | 62 | else |
Azure.IoT Build | 0:fa2de1b79154 | 63 | { |
Azure.IoT Build | 0:fa2de1b79154 | 64 | /*Codes_SRS_BUFFER_02_002: [Otherwise, BUFFER_create shall allocate memory to hold size bytes and shall copy from source size bytes into the newly allocated memory.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 65 | result = (BUFFER*)malloc(sizeof(BUFFER)); |
Azure.IoT Build | 0:fa2de1b79154 | 66 | if (result == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 67 | { |
Azure.IoT Build | 0:fa2de1b79154 | 68 | /*Codes_SRS_BUFFER_02_003: [If allocating memory fails, then BUFFER_create shall return NULL.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 69 | /*fallthrough*/ |
Azure.IoT Build | 0:fa2de1b79154 | 70 | } |
Azure.IoT Build | 0:fa2de1b79154 | 71 | else |
Azure.IoT Build | 0:fa2de1b79154 | 72 | { |
AzureIoTClient | 10:1be0bc9a9deb | 73 | /* 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.]*/ |
AzureIoTClient | 10:1be0bc9a9deb | 74 | if (BUFFER_safemalloc(result, size) != 0) |
Azure.IoT Build | 0:fa2de1b79154 | 75 | { |
AzureIoTClient | 10:1be0bc9a9deb | 76 | LogError("unable to BUFFER_safemalloc "); |
Azure.IoT Build | 0:fa2de1b79154 | 77 | free(result); |
Azure.IoT Build | 0:fa2de1b79154 | 78 | result = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 79 | } |
Azure.IoT Build | 0:fa2de1b79154 | 80 | else |
Azure.IoT Build | 0:fa2de1b79154 | 81 | { |
Azure.IoT Build | 0:fa2de1b79154 | 82 | /*Codes_SRS_BUFFER_02_004: [Otherwise, BUFFER_create shall return a non-NULL handle.] */ |
AzureIoTClient | 19:2e0811512ceb | 83 | (void)memcpy(result->buffer, source, size); |
Azure.IoT Build | 0:fa2de1b79154 | 84 | } |
Azure.IoT Build | 0:fa2de1b79154 | 85 | } |
Azure.IoT Build | 0:fa2de1b79154 | 86 | } |
Azure.IoT Build | 0:fa2de1b79154 | 87 | return (BUFFER_HANDLE)result; |
Azure.IoT Build | 0:fa2de1b79154 | 88 | } |
Azure.IoT Build | 0:fa2de1b79154 | 89 | |
Azure.IoT Build | 0:fa2de1b79154 | 90 | /* Codes_SRS_BUFFER_07_003: [BUFFER_delete shall delete the data associated with the BUFFER_HANDLE along with the Buffer.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 91 | void BUFFER_delete(BUFFER_HANDLE handle) |
Azure.IoT Build | 0:fa2de1b79154 | 92 | { |
Azure.IoT Build | 0:fa2de1b79154 | 93 | /* Codes_SRS_BUFFER_07_004: [BUFFER_delete shall not delete any BUFFER_HANDLE that is NULL.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 94 | if (handle != NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 95 | { |
Azure.IoT Build | 0:fa2de1b79154 | 96 | BUFFER* b = (BUFFER*)handle; |
Azure.IoT Build | 0:fa2de1b79154 | 97 | if (b->buffer != NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 98 | { |
Azure.IoT Build | 0:fa2de1b79154 | 99 | /* Codes_SRS_BUFFER_07_003: [BUFFER_delete shall delete the data associated with the BUFFER_HANDLE along with the Buffer.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 100 | free(b->buffer); |
Azure.IoT Build | 0:fa2de1b79154 | 101 | } |
Azure.IoT Build | 0:fa2de1b79154 | 102 | free(b); |
Azure.IoT Build | 0:fa2de1b79154 | 103 | } |
Azure.IoT Build | 0:fa2de1b79154 | 104 | } |
Azure.IoT Build | 0:fa2de1b79154 | 105 | |
Azure.IoT Build | 0:fa2de1b79154 | 106 | /*return 0 if the buffer was copied*/ |
Azure.IoT Build | 0:fa2de1b79154 | 107 | /*else return different than zero*/ |
Azure.IoT Build | 0:fa2de1b79154 | 108 | /* Codes_SRS_BUFFER_07_008: [BUFFER_build allocates size_t bytes, copies the unsigned char* into the buffer and returns zero on success.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 109 | int BUFFER_build(BUFFER_HANDLE handle, const unsigned char* source, size_t size) |
Azure.IoT Build | 0:fa2de1b79154 | 110 | { |
Azure.IoT Build | 0:fa2de1b79154 | 111 | int result; |
Azure.IoT Build | 0:fa2de1b79154 | 112 | if (handle == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 113 | { |
Azure.IoT Build | 0:fa2de1b79154 | 114 | /* Codes_SRS_BUFFER_07_009: [BUFFER_build shall return nonzero if handle is NULL ] */ |
AzureIoTClient | 21:b92006c5b9ff | 115 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 116 | } |
Azure.IoT Build | 0:fa2de1b79154 | 117 | /* Codes_SRS_BUFFER_01_002: [The size argument can be zero, in which case the underlying buffer held by the buffer instance shall be freed.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 118 | else if (size == 0) |
Azure.IoT Build | 0:fa2de1b79154 | 119 | { |
Azure.IoT Build | 0:fa2de1b79154 | 120 | /* Codes_SRS_BUFFER_01_003: [If size is zero, source can be NULL.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 121 | BUFFER* b = (BUFFER*)handle; |
Azure.IoT Build | 0:fa2de1b79154 | 122 | free(b->buffer); |
Azure.IoT Build | 0:fa2de1b79154 | 123 | b->buffer = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 124 | b->size = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 125 | |
Azure.IoT Build | 0:fa2de1b79154 | 126 | result = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 127 | } |
Azure.IoT Build | 0:fa2de1b79154 | 128 | else |
Azure.IoT Build | 0:fa2de1b79154 | 129 | { |
Azure.IoT Build | 0:fa2de1b79154 | 130 | if (source == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 131 | { |
Azure.IoT Build | 0:fa2de1b79154 | 132 | /* Codes_SRS_BUFFER_01_001: [If size is positive and source is NULL, BUFFER_build shall return nonzero] */ |
AzureIoTClient | 21:b92006c5b9ff | 133 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 134 | } |
Azure.IoT Build | 0:fa2de1b79154 | 135 | else |
Azure.IoT Build | 0:fa2de1b79154 | 136 | { |
Azure.IoT Build | 0:fa2de1b79154 | 137 | BUFFER* b = (BUFFER*)handle; |
Azure.IoT Build | 0:fa2de1b79154 | 138 | /* Codes_SRS_BUFFER_07_011: [BUFFER_build shall overwrite previous contents if the buffer has been previously allocated.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 139 | unsigned char* newBuffer = (unsigned char*)realloc(b->buffer, size); |
Azure.IoT Build | 0:fa2de1b79154 | 140 | if (newBuffer == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 141 | { |
Azure.IoT Build | 0:fa2de1b79154 | 142 | /* Codes_SRS_BUFFER_07_010: [BUFFER_build shall return nonzero if any error is encountered.] */ |
AzureIoTClient | 21:b92006c5b9ff | 143 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 144 | } |
Azure.IoT Build | 0:fa2de1b79154 | 145 | else |
Azure.IoT Build | 0:fa2de1b79154 | 146 | { |
Azure.IoT Build | 0:fa2de1b79154 | 147 | b->buffer = newBuffer; |
Azure.IoT Build | 0:fa2de1b79154 | 148 | b->size = size; |
Azure.IoT Build | 0:fa2de1b79154 | 149 | /* Codes_SRS_BUFFER_01_002: [The size argument can be zero, in which case nothing shall be copied from source.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 150 | (void)memcpy(b->buffer, source, size); |
Azure.IoT Build | 0:fa2de1b79154 | 151 | |
Azure.IoT Build | 0:fa2de1b79154 | 152 | result = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 153 | } |
Azure.IoT Build | 0:fa2de1b79154 | 154 | } |
Azure.IoT Build | 0:fa2de1b79154 | 155 | } |
Azure.IoT Build | 0:fa2de1b79154 | 156 | |
Azure.IoT Build | 0:fa2de1b79154 | 157 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 158 | } |
Azure.IoT Build | 0:fa2de1b79154 | 159 | |
Azure.IoT Build | 0:fa2de1b79154 | 160 | /*return 0 if the buffer was pre-build(that is, had its space allocated)*/ |
Azure.IoT Build | 0:fa2de1b79154 | 161 | /*else return different than zero*/ |
Azure.IoT Build | 0:fa2de1b79154 | 162 | /* Codes_SRS_BUFFER_07_005: [BUFFER_pre_build allocates size_t bytes of BUFFER_HANDLE and returns zero on success.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 163 | int BUFFER_pre_build(BUFFER_HANDLE handle, size_t size) |
Azure.IoT Build | 0:fa2de1b79154 | 164 | { |
Azure.IoT Build | 0:fa2de1b79154 | 165 | int result; |
Azure.IoT Build | 0:fa2de1b79154 | 166 | if (handle == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 167 | { |
Azure.IoT Build | 0:fa2de1b79154 | 168 | /* Codes_SRS_BUFFER_07_006: [If handle is NULL or size is 0 then BUFFER_pre_build shall return a nonzero value.] */ |
AzureIoTClient | 21:b92006c5b9ff | 169 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 170 | } |
Azure.IoT Build | 0:fa2de1b79154 | 171 | else if (size == 0) |
Azure.IoT Build | 0:fa2de1b79154 | 172 | { |
Azure.IoT Build | 0:fa2de1b79154 | 173 | /* Codes_SRS_BUFFER_07_006: [If handle is NULL or size is 0 then BUFFER_pre_build shall return a nonzero value.] */ |
AzureIoTClient | 21:b92006c5b9ff | 174 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 175 | } |
Azure.IoT Build | 0:fa2de1b79154 | 176 | else |
Azure.IoT Build | 0:fa2de1b79154 | 177 | { |
Azure.IoT Build | 0:fa2de1b79154 | 178 | BUFFER* b = (BUFFER*)handle; |
Azure.IoT Build | 0:fa2de1b79154 | 179 | if (b->buffer != NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 180 | { |
Azure.IoT Build | 0:fa2de1b79154 | 181 | /* Codes_SRS_BUFFER_07_007: [BUFFER_pre_build shall return nonzero if the buffer has been previously allocated and is not NULL.] */ |
AzureIoTClient | 21:b92006c5b9ff | 182 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 183 | } |
Azure.IoT Build | 0:fa2de1b79154 | 184 | else |
Azure.IoT Build | 0:fa2de1b79154 | 185 | { |
Azure.IoT Build | 0:fa2de1b79154 | 186 | if ((b->buffer = (unsigned char*)malloc(size)) == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 187 | { |
Azure.IoT Build | 0:fa2de1b79154 | 188 | /* Codes_SRS_BUFFER_07_013: [BUFFER_pre_build shall return nonzero if any error is encountered.] */ |
AzureIoTClient | 21:b92006c5b9ff | 189 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 190 | } |
Azure.IoT Build | 0:fa2de1b79154 | 191 | else |
Azure.IoT Build | 0:fa2de1b79154 | 192 | { |
Azure.IoT Build | 0:fa2de1b79154 | 193 | b->size = size; |
Azure.IoT Build | 0:fa2de1b79154 | 194 | result = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 195 | } |
Azure.IoT Build | 0:fa2de1b79154 | 196 | } |
Azure.IoT Build | 0:fa2de1b79154 | 197 | } |
Azure.IoT Build | 0:fa2de1b79154 | 198 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 199 | } |
Azure.IoT Build | 0:fa2de1b79154 | 200 | |
Azure.IoT Build | 0:fa2de1b79154 | 201 | /* Codes_SRS_BUFFER_07_019: [BUFFER_content shall return the data contained within the BUFFER_HANDLE.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 202 | int BUFFER_content(BUFFER_HANDLE handle, const unsigned char** content) |
Azure.IoT Build | 0:fa2de1b79154 | 203 | { |
Azure.IoT Build | 0:fa2de1b79154 | 204 | int result; |
Azure.IoT Build | 0:fa2de1b79154 | 205 | if ((handle == NULL) || (content == NULL)) |
Azure.IoT Build | 0:fa2de1b79154 | 206 | { |
Azure.IoT Build | 0:fa2de1b79154 | 207 | /* Codes_SRS_BUFFER_07_020: [If the handle and/or content*is NULL BUFFER_content shall return nonzero.] */ |
AzureIoTClient | 21:b92006c5b9ff | 208 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 209 | } |
Azure.IoT Build | 0:fa2de1b79154 | 210 | else |
Azure.IoT Build | 0:fa2de1b79154 | 211 | { |
Azure.IoT Build | 0:fa2de1b79154 | 212 | BUFFER* b = (BUFFER*)handle; |
Azure.IoT Build | 0:fa2de1b79154 | 213 | *content = b->buffer; |
Azure.IoT Build | 0:fa2de1b79154 | 214 | result = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 215 | } |
Azure.IoT Build | 0:fa2de1b79154 | 216 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 217 | } |
Azure.IoT Build | 0:fa2de1b79154 | 218 | |
Azure.IoT Build | 0:fa2de1b79154 | 219 | /*return 0 if everything went ok and whatever was built in the buffer was unbuilt*/ |
Azure.IoT Build | 0:fa2de1b79154 | 220 | /* Codes_SRS_BUFFER_07_012: [BUFFER_unbuild shall clear the underlying unsigned char* data associated with the BUFFER_HANDLE this will return zero on success.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 221 | extern int BUFFER_unbuild(BUFFER_HANDLE handle) |
Azure.IoT Build | 0:fa2de1b79154 | 222 | { |
Azure.IoT Build | 0:fa2de1b79154 | 223 | int result; |
Azure.IoT Build | 0:fa2de1b79154 | 224 | if (handle == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 225 | { |
Azure.IoT Build | 0:fa2de1b79154 | 226 | /* Codes_SRS_BUFFER_07_014: [BUFFER_unbuild shall return a nonzero value if BUFFER_HANDLE is NULL.] */ |
AzureIoTClient | 21:b92006c5b9ff | 227 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 228 | } |
Azure.IoT Build | 0:fa2de1b79154 | 229 | else |
Azure.IoT Build | 0:fa2de1b79154 | 230 | { |
Azure.IoT Build | 0:fa2de1b79154 | 231 | BUFFER* b = (BUFFER*)handle; |
Azure.IoT Build | 0:fa2de1b79154 | 232 | if (b->buffer != NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 233 | { |
Azure.IoT Build | 0:fa2de1b79154 | 234 | free(b->buffer); |
Azure.IoT Build | 0:fa2de1b79154 | 235 | b->buffer = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 236 | b->size = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 237 | result = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 238 | } |
Azure.IoT Build | 0:fa2de1b79154 | 239 | else |
Azure.IoT Build | 0:fa2de1b79154 | 240 | { |
Azure.IoT Build | 0:fa2de1b79154 | 241 | /* Codes_SRS_BUFFER_07_015: [BUFFER_unbuild shall return a nonzero value if the unsigned char* referenced by BUFFER_HANDLE is NULL.] */ |
AzureIoTClient | 21:b92006c5b9ff | 242 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 243 | } |
Azure.IoT Build | 0:fa2de1b79154 | 244 | } |
Azure.IoT Build | 0:fa2de1b79154 | 245 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 246 | } |
Azure.IoT Build | 0:fa2de1b79154 | 247 | |
Azure.IoT Build | 0:fa2de1b79154 | 248 | /* Codes_SRS_BUFFER_07_016: [BUFFER_enlarge shall increase the size of the unsigned char* referenced by BUFFER_HANDLE.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 249 | int BUFFER_enlarge(BUFFER_HANDLE handle, size_t enlargeSize) |
Azure.IoT Build | 0:fa2de1b79154 | 250 | { |
Azure.IoT Build | 0:fa2de1b79154 | 251 | int result; |
Azure.IoT Build | 0:fa2de1b79154 | 252 | if (handle == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 253 | { |
Azure.IoT Build | 0:fa2de1b79154 | 254 | /* Codes_SRS_BUFFER_07_017: [BUFFER_enlarge shall return a nonzero result if any parameters are NULL or zero.] */ |
AzureIoTClient | 21:b92006c5b9ff | 255 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 256 | } |
Azure.IoT Build | 0:fa2de1b79154 | 257 | else if (enlargeSize == 0) |
Azure.IoT Build | 0:fa2de1b79154 | 258 | { |
Azure.IoT Build | 0:fa2de1b79154 | 259 | /* Codes_SRS_BUFFER_07_017: [BUFFER_enlarge shall return a nonzero result if any parameters are NULL or zero.] */ |
AzureIoTClient | 21:b92006c5b9ff | 260 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 261 | } |
Azure.IoT Build | 0:fa2de1b79154 | 262 | else |
Azure.IoT Build | 0:fa2de1b79154 | 263 | { |
Azure.IoT Build | 0:fa2de1b79154 | 264 | BUFFER* b = (BUFFER*)handle; |
Azure.IoT Build | 0:fa2de1b79154 | 265 | unsigned char* temp = (unsigned char*)realloc(b->buffer, b->size + enlargeSize); |
Azure.IoT Build | 0:fa2de1b79154 | 266 | if (temp == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 267 | { |
Azure.IoT Build | 0:fa2de1b79154 | 268 | /* Codes_SRS_BUFFER_07_018: [BUFFER_enlarge shall return a nonzero result if any error is encountered.] */ |
AzureIoTClient | 21:b92006c5b9ff | 269 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 270 | } |
Azure.IoT Build | 0:fa2de1b79154 | 271 | else |
Azure.IoT Build | 0:fa2de1b79154 | 272 | { |
Azure.IoT Build | 0:fa2de1b79154 | 273 | b->buffer = temp; |
Azure.IoT Build | 0:fa2de1b79154 | 274 | b->size += enlargeSize; |
Azure.IoT Build | 0:fa2de1b79154 | 275 | result = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 276 | } |
Azure.IoT Build | 0:fa2de1b79154 | 277 | } |
Azure.IoT Build | 0:fa2de1b79154 | 278 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 279 | } |
Azure.IoT Build | 0:fa2de1b79154 | 280 | |
Azure.IoT Build | 0:fa2de1b79154 | 281 | /* Codes_SRS_BUFFER_07_021: [BUFFER_size shall place the size of the associated buffer in the size variable and return zero on success.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 282 | int BUFFER_size(BUFFER_HANDLE handle, size_t* size) |
Azure.IoT Build | 0:fa2de1b79154 | 283 | { |
Azure.IoT Build | 0:fa2de1b79154 | 284 | int result; |
Azure.IoT Build | 0:fa2de1b79154 | 285 | if ((handle == NULL) || (size == NULL)) |
Azure.IoT Build | 0:fa2de1b79154 | 286 | { |
Azure.IoT Build | 0:fa2de1b79154 | 287 | /* Codes_SRS_BUFFER_07_022: [BUFFER_size shall return a nonzero value for any error that is encountered.] */ |
AzureIoTClient | 21:b92006c5b9ff | 288 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 289 | } |
Azure.IoT Build | 0:fa2de1b79154 | 290 | else |
Azure.IoT Build | 0:fa2de1b79154 | 291 | { |
Azure.IoT Build | 0:fa2de1b79154 | 292 | BUFFER* b = (BUFFER*)handle; |
Azure.IoT Build | 0:fa2de1b79154 | 293 | *size = b->size; |
Azure.IoT Build | 0:fa2de1b79154 | 294 | result = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 295 | } |
Azure.IoT Build | 0:fa2de1b79154 | 296 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 297 | } |
Azure.IoT Build | 0:fa2de1b79154 | 298 | |
Azure.IoT Build | 0:fa2de1b79154 | 299 | /* Codes_SRS_BUFFER_07_024: [BUFFER_append concatenates b2 onto b1 without modifying b2 and shall return zero on success.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 300 | int BUFFER_append(BUFFER_HANDLE handle1, BUFFER_HANDLE handle2) |
Azure.IoT Build | 0:fa2de1b79154 | 301 | { |
Azure.IoT Build | 0:fa2de1b79154 | 302 | int result; |
Azure.IoT Build | 0:fa2de1b79154 | 303 | if ( (handle1 == NULL) || (handle2 == NULL) || (handle1 == handle2) ) |
Azure.IoT Build | 0:fa2de1b79154 | 304 | { |
Azure.IoT Build | 0:fa2de1b79154 | 305 | /* Codes_SRS_BUFFER_07_023: [BUFFER_append shall return a nonzero upon any error that is encountered.] */ |
AzureIoTClient | 21:b92006c5b9ff | 306 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 307 | } |
Azure.IoT Build | 0:fa2de1b79154 | 308 | else |
Azure.IoT Build | 0:fa2de1b79154 | 309 | { |
Azure.IoT Build | 0:fa2de1b79154 | 310 | BUFFER* b1 = (BUFFER*)handle1; |
Azure.IoT Build | 0:fa2de1b79154 | 311 | BUFFER* b2 = (BUFFER*)handle2; |
Azure.IoT Build | 0:fa2de1b79154 | 312 | if (b1->buffer == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 313 | { |
Azure.IoT Build | 0:fa2de1b79154 | 314 | /* Codes_SRS_BUFFER_07_023: [BUFFER_append shall return a nonzero upon any error that is encountered.] */ |
AzureIoTClient | 21:b92006c5b9ff | 315 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 316 | } |
Azure.IoT Build | 0:fa2de1b79154 | 317 | else if (b2->buffer == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 318 | { |
Azure.IoT Build | 0:fa2de1b79154 | 319 | /* Codes_SRS_BUFFER_07_023: [BUFFER_append shall return a nonzero upon any error that is encountered.] */ |
AzureIoTClient | 21:b92006c5b9ff | 320 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 321 | } |
Azure.IoT Build | 0:fa2de1b79154 | 322 | else |
Azure.IoT Build | 0:fa2de1b79154 | 323 | { |
AzureIoTClient | 10:1be0bc9a9deb | 324 | if (b2->size ==0) |
Azure.IoT Build | 0:fa2de1b79154 | 325 | { |
AzureIoTClient | 10:1be0bc9a9deb | 326 | // b2->size = 0, whatever b1->size is, do nothing |
AzureIoTClient | 10:1be0bc9a9deb | 327 | result = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 328 | } |
Azure.IoT Build | 0:fa2de1b79154 | 329 | else |
Azure.IoT Build | 0:fa2de1b79154 | 330 | { |
AzureIoTClient | 10:1be0bc9a9deb | 331 | // b2->size != 0, whatever b1->size is |
AzureIoTClient | 10:1be0bc9a9deb | 332 | unsigned char* temp = (unsigned char*)realloc(b1->buffer, b1->size + b2->size); |
AzureIoTClient | 10:1be0bc9a9deb | 333 | if (temp == NULL) |
AzureIoTClient | 10:1be0bc9a9deb | 334 | { |
AzureIoTClient | 10:1be0bc9a9deb | 335 | /* Codes_SRS_BUFFER_07_023: [BUFFER_append shall return a nonzero upon any error that is encountered.] */ |
AzureIoTClient | 21:b92006c5b9ff | 336 | result = __FAILURE__; |
AzureIoTClient | 10:1be0bc9a9deb | 337 | } |
AzureIoTClient | 10:1be0bc9a9deb | 338 | else |
AzureIoTClient | 10:1be0bc9a9deb | 339 | { |
AzureIoTClient | 10:1be0bc9a9deb | 340 | /* Codes_SRS_BUFFER_07_024: [BUFFER_append concatenates b2 onto b1 without modifying b2 and shall return zero on success.]*/ |
AzureIoTClient | 10:1be0bc9a9deb | 341 | b1->buffer = temp; |
AzureIoTClient | 10:1be0bc9a9deb | 342 | // Append the BUFFER |
AzureIoTClient | 10:1be0bc9a9deb | 343 | (void)memcpy(&b1->buffer[b1->size], b2->buffer, b2->size); |
AzureIoTClient | 10:1be0bc9a9deb | 344 | b1->size += b2->size; |
AzureIoTClient | 10:1be0bc9a9deb | 345 | result = 0; |
AzureIoTClient | 10:1be0bc9a9deb | 346 | } |
Azure.IoT Build | 0:fa2de1b79154 | 347 | } |
Azure.IoT Build | 0:fa2de1b79154 | 348 | } |
Azure.IoT Build | 0:fa2de1b79154 | 349 | } |
Azure.IoT Build | 0:fa2de1b79154 | 350 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 351 | } |
Azure.IoT Build | 0:fa2de1b79154 | 352 | |
Azure.IoT Build | 0:fa2de1b79154 | 353 | int BUFFER_prepend(BUFFER_HANDLE handle1, BUFFER_HANDLE handle2) |
Azure.IoT Build | 0:fa2de1b79154 | 354 | { |
Azure.IoT Build | 0:fa2de1b79154 | 355 | int result; |
Azure.IoT Build | 0:fa2de1b79154 | 356 | if ((handle1 == NULL) || (handle2 == NULL) || (handle1 == handle2)) |
Azure.IoT Build | 0:fa2de1b79154 | 357 | { |
AzureIoTClient | 10:1be0bc9a9deb | 358 | /* Codes_SRS_BUFFER_01_005: [ BUFFER_prepend shall return a non-zero upon value any error that is encountered. ]*/ |
AzureIoTClient | 21:b92006c5b9ff | 359 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 360 | } |
Azure.IoT Build | 0:fa2de1b79154 | 361 | else |
Azure.IoT Build | 0:fa2de1b79154 | 362 | { |
Azure.IoT Build | 0:fa2de1b79154 | 363 | BUFFER* b1 = (BUFFER*)handle1; |
Azure.IoT Build | 0:fa2de1b79154 | 364 | BUFFER* b2 = (BUFFER*)handle2; |
Azure.IoT Build | 0:fa2de1b79154 | 365 | if (b1->buffer == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 366 | { |
AzureIoTClient | 10:1be0bc9a9deb | 367 | /* Codes_SRS_BUFFER_01_005: [ BUFFER_prepend shall return a non-zero upon value any error that is encountered. ]*/ |
AzureIoTClient | 21:b92006c5b9ff | 368 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 369 | } |
Azure.IoT Build | 0:fa2de1b79154 | 370 | else if (b2->buffer == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 371 | { |
AzureIoTClient | 10:1be0bc9a9deb | 372 | /* Codes_SRS_BUFFER_01_005: [ BUFFER_prepend shall return a non-zero upon value any error that is encountered. ]*/ |
AzureIoTClient | 21:b92006c5b9ff | 373 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 374 | } |
Azure.IoT Build | 0:fa2de1b79154 | 375 | else |
Azure.IoT Build | 0:fa2de1b79154 | 376 | { |
AzureIoTClient | 10:1be0bc9a9deb | 377 | //put b2 ahead of b1: [b2][b1], return b1 |
AzureIoTClient | 10:1be0bc9a9deb | 378 | if (b2->size ==0) |
Azure.IoT Build | 0:fa2de1b79154 | 379 | { |
AzureIoTClient | 10:1be0bc9a9deb | 380 | // do nothing |
AzureIoTClient | 10:1be0bc9a9deb | 381 | result = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 382 | } |
Azure.IoT Build | 0:fa2de1b79154 | 383 | else |
Azure.IoT Build | 0:fa2de1b79154 | 384 | { |
AzureIoTClient | 10:1be0bc9a9deb | 385 | // b2->size != 0 |
AzureIoTClient | 10:1be0bc9a9deb | 386 | unsigned char* temp = (unsigned char*)malloc(b1->size + b2->size); |
AzureIoTClient | 10:1be0bc9a9deb | 387 | if (temp == NULL) |
AzureIoTClient | 10:1be0bc9a9deb | 388 | { |
AzureIoTClient | 10:1be0bc9a9deb | 389 | /* Codes_SRS_BUFFER_01_005: [ BUFFER_prepend shall return a non-zero upon value any error that is encountered. ]*/ |
AzureIoTClient | 21:b92006c5b9ff | 390 | result = __FAILURE__; |
AzureIoTClient | 10:1be0bc9a9deb | 391 | } |
AzureIoTClient | 10:1be0bc9a9deb | 392 | else |
AzureIoTClient | 10:1be0bc9a9deb | 393 | { |
AzureIoTClient | 10:1be0bc9a9deb | 394 | /* Codes_SRS_BUFFER_01_004: [ BUFFER_prepend concatenates handle1 onto handle2 without modifying handle1 and shall return zero on success. ]*/ |
AzureIoTClient | 10:1be0bc9a9deb | 395 | // Append the BUFFER |
AzureIoTClient | 10:1be0bc9a9deb | 396 | (void)memcpy(temp, b2->buffer, b2->size); |
AzureIoTClient | 10:1be0bc9a9deb | 397 | // start from b1->size to append b1 |
AzureIoTClient | 10:1be0bc9a9deb | 398 | (void)memcpy(&temp[b2->size], b1->buffer, b1->size); |
AzureIoTClient | 10:1be0bc9a9deb | 399 | free(b1->buffer); |
AzureIoTClient | 10:1be0bc9a9deb | 400 | b1->buffer = temp; |
AzureIoTClient | 10:1be0bc9a9deb | 401 | b1->size += b2->size; |
AzureIoTClient | 10:1be0bc9a9deb | 402 | result = 0; |
AzureIoTClient | 10:1be0bc9a9deb | 403 | } |
Azure.IoT Build | 0:fa2de1b79154 | 404 | } |
Azure.IoT Build | 0:fa2de1b79154 | 405 | } |
Azure.IoT Build | 0:fa2de1b79154 | 406 | } |
Azure.IoT Build | 0:fa2de1b79154 | 407 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 408 | } |
Azure.IoT Build | 0:fa2de1b79154 | 409 | |
Azure.IoT Build | 0:fa2de1b79154 | 410 | |
Azure.IoT Build | 0:fa2de1b79154 | 411 | /* Codes_SRS_BUFFER_07_025: [BUFFER_u_char shall return a pointer to the underlying unsigned char*.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 412 | unsigned char* BUFFER_u_char(BUFFER_HANDLE handle) |
Azure.IoT Build | 0:fa2de1b79154 | 413 | { |
AzureIoTClient | 10:1be0bc9a9deb | 414 | BUFFER* handleData = (BUFFER*)handle; |
Azure.IoT Build | 0:fa2de1b79154 | 415 | unsigned char* result; |
AzureIoTClient | 10:1be0bc9a9deb | 416 | if (handle == NULL || handleData->size == 0) |
Azure.IoT Build | 0:fa2de1b79154 | 417 | { |
Azure.IoT Build | 0:fa2de1b79154 | 418 | /* Codes_SRS_BUFFER_07_026: [BUFFER_u_char shall return NULL for any error that is encountered.] */ |
AzureIoTClient | 10:1be0bc9a9deb | 419 | /* Codes_SRS_BUFFER_07_029: [BUFFER_u_char shall return NULL if underlying buffer size is zero.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 420 | result = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 421 | } |
Azure.IoT Build | 0:fa2de1b79154 | 422 | else |
Azure.IoT Build | 0:fa2de1b79154 | 423 | { |
AzureIoTClient | 10:1be0bc9a9deb | 424 | result = handleData->buffer; |
Azure.IoT Build | 0:fa2de1b79154 | 425 | } |
Azure.IoT Build | 0:fa2de1b79154 | 426 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 427 | } |
Azure.IoT Build | 0:fa2de1b79154 | 428 | |
Azure.IoT Build | 0:fa2de1b79154 | 429 | /* Codes_SRS_BUFFER_07_027: [BUFFER_length shall return the size of the underlying buffer.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 430 | size_t BUFFER_length(BUFFER_HANDLE handle) |
Azure.IoT Build | 0:fa2de1b79154 | 431 | { |
Azure.IoT Build | 0:fa2de1b79154 | 432 | size_t result; |
Azure.IoT Build | 0:fa2de1b79154 | 433 | if (handle == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 434 | { |
Azure.IoT Build | 0:fa2de1b79154 | 435 | /* Codes_SRS_BUFFER_07_028: [BUFFER_length shall return zero for any error that is encountered.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 436 | result = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 437 | } |
Azure.IoT Build | 0:fa2de1b79154 | 438 | else |
Azure.IoT Build | 0:fa2de1b79154 | 439 | { |
Azure.IoT Build | 0:fa2de1b79154 | 440 | BUFFER* b = (BUFFER*)handle; |
Azure.IoT Build | 0:fa2de1b79154 | 441 | result = b->size; |
Azure.IoT Build | 0:fa2de1b79154 | 442 | } |
Azure.IoT Build | 0:fa2de1b79154 | 443 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 444 | } |
Azure.IoT Build | 0:fa2de1b79154 | 445 | |
Azure.IoT Build | 0:fa2de1b79154 | 446 | BUFFER_HANDLE BUFFER_clone(BUFFER_HANDLE handle) |
Azure.IoT Build | 0:fa2de1b79154 | 447 | { |
Azure.IoT Build | 0:fa2de1b79154 | 448 | BUFFER_HANDLE result; |
Azure.IoT Build | 0:fa2de1b79154 | 449 | if (handle == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 450 | { |
Azure.IoT Build | 0:fa2de1b79154 | 451 | result = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 452 | } |
Azure.IoT Build | 0:fa2de1b79154 | 453 | else |
Azure.IoT Build | 0:fa2de1b79154 | 454 | { |
Azure.IoT Build | 0:fa2de1b79154 | 455 | BUFFER* suppliedBuff = (BUFFER*)handle; |
AzureIoTClient | 10:1be0bc9a9deb | 456 | BUFFER* b = (BUFFER*)malloc(sizeof(BUFFER)); |
Azure.IoT Build | 0:fa2de1b79154 | 457 | if (b != NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 458 | { |
AzureIoTClient | 10:1be0bc9a9deb | 459 | if (BUFFER_safemalloc(b, suppliedBuff->size) != 0) |
Azure.IoT Build | 0:fa2de1b79154 | 460 | { |
Azure.IoT Build | 0:fa2de1b79154 | 461 | result = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 462 | } |
Azure.IoT Build | 0:fa2de1b79154 | 463 | else |
Azure.IoT Build | 0:fa2de1b79154 | 464 | { |
AzureIoTClient | 19:2e0811512ceb | 465 | (void)memcpy(b->buffer, suppliedBuff->buffer, suppliedBuff->size); |
Azure.IoT Build | 0:fa2de1b79154 | 466 | b->size = suppliedBuff->size; |
Azure.IoT Build | 0:fa2de1b79154 | 467 | result = (BUFFER_HANDLE)b; |
Azure.IoT Build | 0:fa2de1b79154 | 468 | } |
Azure.IoT Build | 0:fa2de1b79154 | 469 | } |
Azure.IoT Build | 0:fa2de1b79154 | 470 | else |
Azure.IoT Build | 0:fa2de1b79154 | 471 | { |
Azure.IoT Build | 0:fa2de1b79154 | 472 | result = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 473 | } |
Azure.IoT Build | 0:fa2de1b79154 | 474 | } |
Azure.IoT Build | 0:fa2de1b79154 | 475 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 476 | } |