Azure IoT common library

Fork of azure_c_shared_utility by Azure IoT

Committer:
wiggly
Date:
Thu Aug 24 14:14:15 2017 +0100
Revision:
34:651c23af382c
Parent:
30:ce3813c5a692
Pass in network stack to platform initialisation
Remove NTP setup from azure platform code

Who changed what in which revision?

UserRevisionLine numberNew 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 27:8656a313842b 7 #include <stdbool.h>
AzureIoTClient 19:2e0811512ceb 8 #include "azure_c_shared_utility/gballoc.h"
Azure.IoT Build 0:fa2de1b79154 9 #include "azure_c_shared_utility/buffer_.h"
AzureIoTClient 21:b92006c5b9ff 10 #include "azure_c_shared_utility/optimize_size.h"
Azure.IoT Build 6:c55b013dfc2a 11 #include "azure_c_shared_utility/xlogging.h"
Azure.IoT Build 0:fa2de1b79154 12
Azure.IoT Build 0:fa2de1b79154 13 typedef struct BUFFER_TAG
Azure.IoT Build 0:fa2de1b79154 14 {
Azure.IoT Build 0:fa2de1b79154 15 unsigned char* buffer;
Azure.IoT Build 0:fa2de1b79154 16 size_t size;
AzureIoTClient 27:8656a313842b 17 } BUFFER;
Azure.IoT Build 0:fa2de1b79154 18
Azure.IoT Build 0:fa2de1b79154 19 /* Codes_SRS_BUFFER_07_001: [BUFFER_new shall allocate a BUFFER_HANDLE that will contain a NULL unsigned char*.] */
Azure.IoT Build 0:fa2de1b79154 20 BUFFER_HANDLE BUFFER_new(void)
Azure.IoT Build 0:fa2de1b79154 21 {
Azure.IoT Build 0:fa2de1b79154 22 BUFFER* temp = (BUFFER*)malloc(sizeof(BUFFER));
Azure.IoT Build 0:fa2de1b79154 23 /* Codes_SRS_BUFFER_07_002: [BUFFER_new shall return NULL on any error that occurs.] */
Azure.IoT Build 0:fa2de1b79154 24 if (temp != NULL)
Azure.IoT Build 0:fa2de1b79154 25 {
Azure.IoT Build 0:fa2de1b79154 26 temp->buffer = NULL;
Azure.IoT Build 0:fa2de1b79154 27 temp->size = 0;
Azure.IoT Build 0:fa2de1b79154 28 }
Azure.IoT Build 0:fa2de1b79154 29 return (BUFFER_HANDLE)temp;
Azure.IoT Build 0:fa2de1b79154 30 }
Azure.IoT Build 0:fa2de1b79154 31
AzureIoTClient 10:1be0bc9a9deb 32 static int BUFFER_safemalloc(BUFFER* handleptr, size_t size)
AzureIoTClient 10:1be0bc9a9deb 33 {
AzureIoTClient 10:1be0bc9a9deb 34 int result;
AzureIoTClient 10:1be0bc9a9deb 35 size_t sizetomalloc = size;
AzureIoTClient 10:1be0bc9a9deb 36 if (size == 0)
AzureIoTClient 10:1be0bc9a9deb 37 {
AzureIoTClient 10:1be0bc9a9deb 38 sizetomalloc = 1;
AzureIoTClient 10:1be0bc9a9deb 39 }
AzureIoTClient 10:1be0bc9a9deb 40 handleptr->buffer = (unsigned char*)malloc(sizetomalloc);
AzureIoTClient 10:1be0bc9a9deb 41 if (handleptr->buffer == NULL)
AzureIoTClient 10:1be0bc9a9deb 42 {
AzureIoTClient 10:1be0bc9a9deb 43 /*Codes_SRS_BUFFER_02_003: [If allocating memory fails, then BUFFER_create shall return NULL.]*/
AzureIoTClient 27:8656a313842b 44 LogError("Failure allocating data");
AzureIoTClient 21:b92006c5b9ff 45 result = __FAILURE__;
AzureIoTClient 10:1be0bc9a9deb 46 }
AzureIoTClient 10:1be0bc9a9deb 47 else
AzureIoTClient 10:1be0bc9a9deb 48 {
AzureIoTClient 10:1be0bc9a9deb 49 // we still consider the real buffer size is 0
AzureIoTClient 10:1be0bc9a9deb 50 handleptr->size = size;
AzureIoTClient 10:1be0bc9a9deb 51 result = 0;
AzureIoTClient 10:1be0bc9a9deb 52 }
AzureIoTClient 10:1be0bc9a9deb 53 return result;
AzureIoTClient 10:1be0bc9a9deb 54 }
AzureIoTClient 10:1be0bc9a9deb 55
Azure.IoT Build 0:fa2de1b79154 56 BUFFER_HANDLE BUFFER_create(const unsigned char* source, size_t size)
Azure.IoT Build 0:fa2de1b79154 57 {
Azure.IoT Build 0:fa2de1b79154 58 BUFFER* result;
Azure.IoT Build 0:fa2de1b79154 59 /*Codes_SRS_BUFFER_02_001: [If source is NULL then BUFFER_create shall return NULL.]*/
Azure.IoT Build 0:fa2de1b79154 60 if (source == NULL)
Azure.IoT Build 0:fa2de1b79154 61 {
AzureIoTClient 27:8656a313842b 62 LogError("invalid parameter source: %p", source);
Azure.IoT Build 0:fa2de1b79154 63 result = NULL;
Azure.IoT Build 0:fa2de1b79154 64 }
Azure.IoT Build 0:fa2de1b79154 65 else
Azure.IoT Build 0:fa2de1b79154 66 {
Azure.IoT Build 0:fa2de1b79154 67 /*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 68 result = (BUFFER*)malloc(sizeof(BUFFER));
Azure.IoT Build 0:fa2de1b79154 69 if (result == NULL)
Azure.IoT Build 0:fa2de1b79154 70 {
Azure.IoT Build 0:fa2de1b79154 71 /*Codes_SRS_BUFFER_02_003: [If allocating memory fails, then BUFFER_create shall return NULL.] */
Azure.IoT Build 0:fa2de1b79154 72 /*fallthrough*/
AzureIoTClient 27:8656a313842b 73 LogError("Failure allocating BUFFER structure");
Azure.IoT Build 0:fa2de1b79154 74 }
Azure.IoT Build 0:fa2de1b79154 75 else
Azure.IoT Build 0:fa2de1b79154 76 {
AzureIoTClient 10:1be0bc9a9deb 77 /* 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 78 if (BUFFER_safemalloc(result, size) != 0)
Azure.IoT Build 0:fa2de1b79154 79 {
AzureIoTClient 10:1be0bc9a9deb 80 LogError("unable to BUFFER_safemalloc ");
Azure.IoT Build 0:fa2de1b79154 81 free(result);
Azure.IoT Build 0:fa2de1b79154 82 result = NULL;
Azure.IoT Build 0:fa2de1b79154 83 }
Azure.IoT Build 0:fa2de1b79154 84 else
Azure.IoT Build 0:fa2de1b79154 85 {
Azure.IoT Build 0:fa2de1b79154 86 /*Codes_SRS_BUFFER_02_004: [Otherwise, BUFFER_create shall return a non-NULL handle.] */
AzureIoTClient 19:2e0811512ceb 87 (void)memcpy(result->buffer, source, size);
Azure.IoT Build 0:fa2de1b79154 88 }
Azure.IoT Build 0:fa2de1b79154 89 }
Azure.IoT Build 0:fa2de1b79154 90 }
Azure.IoT Build 0:fa2de1b79154 91 return (BUFFER_HANDLE)result;
Azure.IoT Build 0:fa2de1b79154 92 }
Azure.IoT Build 0:fa2de1b79154 93
Azure.IoT Build 0:fa2de1b79154 94 /* 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 95 void BUFFER_delete(BUFFER_HANDLE handle)
Azure.IoT Build 0:fa2de1b79154 96 {
Azure.IoT Build 0:fa2de1b79154 97 /* Codes_SRS_BUFFER_07_004: [BUFFER_delete shall not delete any BUFFER_HANDLE that is NULL.] */
Azure.IoT Build 0:fa2de1b79154 98 if (handle != NULL)
Azure.IoT Build 0:fa2de1b79154 99 {
Azure.IoT Build 0:fa2de1b79154 100 BUFFER* b = (BUFFER*)handle;
Azure.IoT Build 0:fa2de1b79154 101 if (b->buffer != NULL)
Azure.IoT Build 0:fa2de1b79154 102 {
Azure.IoT Build 0:fa2de1b79154 103 /* 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 104 free(b->buffer);
Azure.IoT Build 0:fa2de1b79154 105 }
Azure.IoT Build 0:fa2de1b79154 106 free(b);
Azure.IoT Build 0:fa2de1b79154 107 }
Azure.IoT Build 0:fa2de1b79154 108 }
Azure.IoT Build 0:fa2de1b79154 109
Azure.IoT Build 0:fa2de1b79154 110 /*return 0 if the buffer was copied*/
Azure.IoT Build 0:fa2de1b79154 111 /*else return different than zero*/
Azure.IoT Build 0:fa2de1b79154 112 /* 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 113 int BUFFER_build(BUFFER_HANDLE handle, const unsigned char* source, size_t size)
Azure.IoT Build 0:fa2de1b79154 114 {
Azure.IoT Build 0:fa2de1b79154 115 int result;
Azure.IoT Build 0:fa2de1b79154 116 if (handle == NULL)
Azure.IoT Build 0:fa2de1b79154 117 {
Azure.IoT Build 0:fa2de1b79154 118 /* Codes_SRS_BUFFER_07_009: [BUFFER_build shall return nonzero if handle is NULL ] */
AzureIoTClient 21:b92006c5b9ff 119 result = __FAILURE__;
Azure.IoT Build 0:fa2de1b79154 120 }
Azure.IoT Build 0:fa2de1b79154 121 /* 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 122 else if (size == 0)
Azure.IoT Build 0:fa2de1b79154 123 {
Azure.IoT Build 0:fa2de1b79154 124 /* Codes_SRS_BUFFER_01_003: [If size is zero, source can be NULL.] */
Azure.IoT Build 0:fa2de1b79154 125 BUFFER* b = (BUFFER*)handle;
Azure.IoT Build 0:fa2de1b79154 126 free(b->buffer);
Azure.IoT Build 0:fa2de1b79154 127 b->buffer = NULL;
Azure.IoT Build 0:fa2de1b79154 128 b->size = 0;
Azure.IoT Build 0:fa2de1b79154 129
Azure.IoT Build 0:fa2de1b79154 130 result = 0;
Azure.IoT Build 0:fa2de1b79154 131 }
Azure.IoT Build 0:fa2de1b79154 132 else
Azure.IoT Build 0:fa2de1b79154 133 {
Azure.IoT Build 0:fa2de1b79154 134 if (source == NULL)
Azure.IoT Build 0:fa2de1b79154 135 {
Azure.IoT Build 0:fa2de1b79154 136 /* Codes_SRS_BUFFER_01_001: [If size is positive and source is NULL, BUFFER_build shall return nonzero] */
AzureIoTClient 21:b92006c5b9ff 137 result = __FAILURE__;
Azure.IoT Build 0:fa2de1b79154 138 }
Azure.IoT Build 0:fa2de1b79154 139 else
Azure.IoT Build 0:fa2de1b79154 140 {
Azure.IoT Build 0:fa2de1b79154 141 BUFFER* b = (BUFFER*)handle;
Azure.IoT Build 0:fa2de1b79154 142 /* Codes_SRS_BUFFER_07_011: [BUFFER_build shall overwrite previous contents if the buffer has been previously allocated.] */
Azure.IoT Build 0:fa2de1b79154 143 unsigned char* newBuffer = (unsigned char*)realloc(b->buffer, size);
Azure.IoT Build 0:fa2de1b79154 144 if (newBuffer == NULL)
Azure.IoT Build 0:fa2de1b79154 145 {
Azure.IoT Build 0:fa2de1b79154 146 /* Codes_SRS_BUFFER_07_010: [BUFFER_build shall return nonzero if any error is encountered.] */
AzureIoTClient 27:8656a313842b 147 LogError("Failure reallocating buffer");
AzureIoTClient 21:b92006c5b9ff 148 result = __FAILURE__;
Azure.IoT Build 0:fa2de1b79154 149 }
Azure.IoT Build 0:fa2de1b79154 150 else
Azure.IoT Build 0:fa2de1b79154 151 {
Azure.IoT Build 0:fa2de1b79154 152 b->buffer = newBuffer;
Azure.IoT Build 0:fa2de1b79154 153 b->size = size;
Azure.IoT Build 0:fa2de1b79154 154 /* 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 155 (void)memcpy(b->buffer, source, size);
Azure.IoT Build 0:fa2de1b79154 156
Azure.IoT Build 0:fa2de1b79154 157 result = 0;
Azure.IoT Build 0:fa2de1b79154 158 }
Azure.IoT Build 0:fa2de1b79154 159 }
Azure.IoT Build 0:fa2de1b79154 160 }
Azure.IoT Build 0:fa2de1b79154 161
Azure.IoT Build 0:fa2de1b79154 162 return result;
Azure.IoT Build 0:fa2de1b79154 163 }
Azure.IoT Build 0:fa2de1b79154 164
AzureIoTClient 27:8656a313842b 165 int BUFFER_append_build(BUFFER_HANDLE handle, const unsigned char* source, size_t size)
AzureIoTClient 27:8656a313842b 166 {
AzureIoTClient 27:8656a313842b 167 int result;
AzureIoTClient 27:8656a313842b 168 if (handle == NULL || source == NULL || size == 0)
AzureIoTClient 27:8656a313842b 169 {
AzureIoTClient 27:8656a313842b 170 /* Codes_SRS_BUFFER_07_029: [ BUFFER_append_build shall return nonzero if handle or source are NULL or if size is 0. ] */
AzureIoTClient 27:8656a313842b 171 LogError("BUFFER_append_build failed invalid parameter handle: %p, source: %p, size: %uz", handle, source, size);
AzureIoTClient 27:8656a313842b 172 result = __FAILURE__;
AzureIoTClient 27:8656a313842b 173 }
AzureIoTClient 27:8656a313842b 174 else
AzureIoTClient 27:8656a313842b 175 {
AzureIoTClient 27:8656a313842b 176 if (handle->buffer == NULL)
AzureIoTClient 27:8656a313842b 177 {
AzureIoTClient 27:8656a313842b 178 /* Codes_SRS_BUFFER_07_030: [ if handle->buffer is NULL BUFFER_append_build shall allocate the a buffer of size bytes... ] */
AzureIoTClient 27:8656a313842b 179 if (BUFFER_safemalloc(handle, size) != 0)
AzureIoTClient 27:8656a313842b 180 {
AzureIoTClient 27:8656a313842b 181 /* Codes_SRS_BUFFER_07_035: [ If any error is encountered BUFFER_append_build shall return a non-null value. ] */
AzureIoTClient 27:8656a313842b 182 LogError("Failure with BUFFER_safemalloc");
AzureIoTClient 27:8656a313842b 183 result = __FAILURE__;
AzureIoTClient 27:8656a313842b 184 }
AzureIoTClient 27:8656a313842b 185 else
AzureIoTClient 27:8656a313842b 186 {
AzureIoTClient 27:8656a313842b 187 /* Codes_SRS_BUFFER_07_031: [ ... and copy the contents of source to handle->buffer. ] */
AzureIoTClient 27:8656a313842b 188 (void)memcpy(handle->buffer, source, size);
AzureIoTClient 27:8656a313842b 189 /* Codes_SRS_BUFFER_07_034: [ On success BUFFER_append_build shall return 0 ] */
AzureIoTClient 27:8656a313842b 190 result = 0;
AzureIoTClient 27:8656a313842b 191 }
AzureIoTClient 27:8656a313842b 192 }
AzureIoTClient 27:8656a313842b 193 else
AzureIoTClient 27:8656a313842b 194 {
AzureIoTClient 27:8656a313842b 195 /* Codes_SRS_BUFFER_07_032: [ if handle->buffer is not NULL BUFFER_append_build shall realloc the buffer to be the handle->size + size ] */
AzureIoTClient 27:8656a313842b 196 unsigned char* temp = (unsigned char*)realloc(handle->buffer, handle->size + size);
AzureIoTClient 27:8656a313842b 197 if (temp == NULL)
AzureIoTClient 27:8656a313842b 198 {
AzureIoTClient 27:8656a313842b 199 /* Codes_SRS_BUFFER_07_035: [ If any error is encountered BUFFER_append_build shall return a non-null value. ] */
AzureIoTClient 27:8656a313842b 200 LogError("Failure reallocating temporary buffer");
AzureIoTClient 27:8656a313842b 201 result = __FAILURE__;
AzureIoTClient 27:8656a313842b 202 }
AzureIoTClient 27:8656a313842b 203 else
AzureIoTClient 27:8656a313842b 204 {
AzureIoTClient 27:8656a313842b 205 /* Codes_SRS_BUFFER_07_033: [ ... and copy the contents of source to the end of the buffer. ] */
AzureIoTClient 27:8656a313842b 206 handle->buffer = temp;
AzureIoTClient 27:8656a313842b 207 // Append the BUFFER
AzureIoTClient 27:8656a313842b 208 (void)memcpy(&handle->buffer[handle->size], source, size);
AzureIoTClient 27:8656a313842b 209 handle->size += size;
AzureIoTClient 27:8656a313842b 210 /* Codes_SRS_BUFFER_07_034: [ On success BUFFER_append_build shall return 0 ] */
AzureIoTClient 27:8656a313842b 211 result = 0;
AzureIoTClient 27:8656a313842b 212 }
AzureIoTClient 27:8656a313842b 213 }
AzureIoTClient 27:8656a313842b 214 }
AzureIoTClient 27:8656a313842b 215 return result;
AzureIoTClient 27:8656a313842b 216 }
AzureIoTClient 27:8656a313842b 217
Azure.IoT Build 0:fa2de1b79154 218 /*return 0 if the buffer was pre-build(that is, had its space allocated)*/
Azure.IoT Build 0:fa2de1b79154 219 /*else return different than zero*/
Azure.IoT Build 0:fa2de1b79154 220 /* 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 221 int BUFFER_pre_build(BUFFER_HANDLE handle, size_t size)
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_006: [If handle is NULL or size is 0 then BUFFER_pre_build shall return a nonzero value.] */
AzureIoTClient 21:b92006c5b9ff 227 result = __FAILURE__;
Azure.IoT Build 0:fa2de1b79154 228 }
Azure.IoT Build 0:fa2de1b79154 229 else if (size == 0)
Azure.IoT Build 0:fa2de1b79154 230 {
Azure.IoT Build 0:fa2de1b79154 231 /* 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 232 result = __FAILURE__;
Azure.IoT Build 0:fa2de1b79154 233 }
Azure.IoT Build 0:fa2de1b79154 234 else
Azure.IoT Build 0:fa2de1b79154 235 {
Azure.IoT Build 0:fa2de1b79154 236 BUFFER* b = (BUFFER*)handle;
Azure.IoT Build 0:fa2de1b79154 237 if (b->buffer != NULL)
Azure.IoT Build 0:fa2de1b79154 238 {
Azure.IoT Build 0:fa2de1b79154 239 /* Codes_SRS_BUFFER_07_007: [BUFFER_pre_build shall return nonzero if the buffer has been previously allocated and is not NULL.] */
AzureIoTClient 27:8656a313842b 240 LogError("Failure buffer data is NULL");
AzureIoTClient 21:b92006c5b9ff 241 result = __FAILURE__;
Azure.IoT Build 0:fa2de1b79154 242 }
Azure.IoT Build 0:fa2de1b79154 243 else
Azure.IoT Build 0:fa2de1b79154 244 {
Azure.IoT Build 0:fa2de1b79154 245 if ((b->buffer = (unsigned char*)malloc(size)) == NULL)
Azure.IoT Build 0:fa2de1b79154 246 {
Azure.IoT Build 0:fa2de1b79154 247 /* Codes_SRS_BUFFER_07_013: [BUFFER_pre_build shall return nonzero if any error is encountered.] */
AzureIoTClient 27:8656a313842b 248 LogError("Failure allocating buffer");
AzureIoTClient 21:b92006c5b9ff 249 result = __FAILURE__;
Azure.IoT Build 0:fa2de1b79154 250 }
Azure.IoT Build 0:fa2de1b79154 251 else
Azure.IoT Build 0:fa2de1b79154 252 {
Azure.IoT Build 0:fa2de1b79154 253 b->size = size;
Azure.IoT Build 0:fa2de1b79154 254 result = 0;
Azure.IoT Build 0:fa2de1b79154 255 }
Azure.IoT Build 0:fa2de1b79154 256 }
Azure.IoT Build 0:fa2de1b79154 257 }
Azure.IoT Build 0:fa2de1b79154 258 return result;
Azure.IoT Build 0:fa2de1b79154 259 }
Azure.IoT Build 0:fa2de1b79154 260
Azure.IoT Build 0:fa2de1b79154 261 /* Codes_SRS_BUFFER_07_019: [BUFFER_content shall return the data contained within the BUFFER_HANDLE.] */
Azure.IoT Build 0:fa2de1b79154 262 int BUFFER_content(BUFFER_HANDLE handle, const unsigned char** content)
Azure.IoT Build 0:fa2de1b79154 263 {
Azure.IoT Build 0:fa2de1b79154 264 int result;
Azure.IoT Build 0:fa2de1b79154 265 if ((handle == NULL) || (content == NULL))
Azure.IoT Build 0:fa2de1b79154 266 {
Azure.IoT Build 0:fa2de1b79154 267 /* Codes_SRS_BUFFER_07_020: [If the handle and/or content*is NULL BUFFER_content shall return nonzero.] */
AzureIoTClient 21:b92006c5b9ff 268 result = __FAILURE__;
Azure.IoT Build 0:fa2de1b79154 269 }
Azure.IoT Build 0:fa2de1b79154 270 else
Azure.IoT Build 0:fa2de1b79154 271 {
Azure.IoT Build 0:fa2de1b79154 272 BUFFER* b = (BUFFER*)handle;
Azure.IoT Build 0:fa2de1b79154 273 *content = b->buffer;
Azure.IoT Build 0:fa2de1b79154 274 result = 0;
Azure.IoT Build 0:fa2de1b79154 275 }
Azure.IoT Build 0:fa2de1b79154 276 return result;
Azure.IoT Build 0:fa2de1b79154 277 }
Azure.IoT Build 0:fa2de1b79154 278
Azure.IoT Build 0:fa2de1b79154 279 /*return 0 if everything went ok and whatever was built in the buffer was unbuilt*/
Azure.IoT Build 0:fa2de1b79154 280 /* 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 281 extern int BUFFER_unbuild(BUFFER_HANDLE handle)
Azure.IoT Build 0:fa2de1b79154 282 {
Azure.IoT Build 0:fa2de1b79154 283 int result;
Azure.IoT Build 0:fa2de1b79154 284 if (handle == NULL)
Azure.IoT Build 0:fa2de1b79154 285 {
Azure.IoT Build 0:fa2de1b79154 286 /* Codes_SRS_BUFFER_07_014: [BUFFER_unbuild shall return a nonzero value if BUFFER_HANDLE is NULL.] */
AzureIoTClient 21:b92006c5b9ff 287 result = __FAILURE__;
Azure.IoT Build 0:fa2de1b79154 288 }
Azure.IoT Build 0:fa2de1b79154 289 else
Azure.IoT Build 0:fa2de1b79154 290 {
Azure.IoT Build 0:fa2de1b79154 291 BUFFER* b = (BUFFER*)handle;
Azure.IoT Build 0:fa2de1b79154 292 if (b->buffer != NULL)
Azure.IoT Build 0:fa2de1b79154 293 {
AzureIoTClient 27:8656a313842b 294 LogError("Failure buffer data is NULL");
Azure.IoT Build 0:fa2de1b79154 295 free(b->buffer);
Azure.IoT Build 0:fa2de1b79154 296 b->buffer = NULL;
Azure.IoT Build 0:fa2de1b79154 297 b->size = 0;
Azure.IoT Build 0:fa2de1b79154 298 result = 0;
Azure.IoT Build 0:fa2de1b79154 299 }
Azure.IoT Build 0:fa2de1b79154 300 else
Azure.IoT Build 0:fa2de1b79154 301 {
Azure.IoT Build 0:fa2de1b79154 302 /* 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 303 result = __FAILURE__;
Azure.IoT Build 0:fa2de1b79154 304 }
Azure.IoT Build 0:fa2de1b79154 305 }
Azure.IoT Build 0:fa2de1b79154 306 return result;
Azure.IoT Build 0:fa2de1b79154 307 }
Azure.IoT Build 0:fa2de1b79154 308
Azure.IoT Build 0:fa2de1b79154 309 /* Codes_SRS_BUFFER_07_016: [BUFFER_enlarge shall increase the size of the unsigned char* referenced by BUFFER_HANDLE.] */
Azure.IoT Build 0:fa2de1b79154 310 int BUFFER_enlarge(BUFFER_HANDLE handle, size_t enlargeSize)
Azure.IoT Build 0:fa2de1b79154 311 {
Azure.IoT Build 0:fa2de1b79154 312 int result;
Azure.IoT Build 0:fa2de1b79154 313 if (handle == NULL)
Azure.IoT Build 0:fa2de1b79154 314 {
Azure.IoT Build 0:fa2de1b79154 315 /* Codes_SRS_BUFFER_07_017: [BUFFER_enlarge shall return a nonzero result if any parameters are NULL or zero.] */
AzureIoTClient 27:8656a313842b 316 LogError("Failure: handle is invalid.");
AzureIoTClient 21:b92006c5b9ff 317 result = __FAILURE__;
Azure.IoT Build 0:fa2de1b79154 318 }
Azure.IoT Build 0:fa2de1b79154 319 else if (enlargeSize == 0)
Azure.IoT Build 0:fa2de1b79154 320 {
Azure.IoT Build 0:fa2de1b79154 321 /* Codes_SRS_BUFFER_07_017: [BUFFER_enlarge shall return a nonzero result if any parameters are NULL or zero.] */
AzureIoTClient 27:8656a313842b 322 LogError("Failure: enlargeSize size is 0.");
AzureIoTClient 21:b92006c5b9ff 323 result = __FAILURE__;
Azure.IoT Build 0:fa2de1b79154 324 }
Azure.IoT Build 0:fa2de1b79154 325 else
Azure.IoT Build 0:fa2de1b79154 326 {
Azure.IoT Build 0:fa2de1b79154 327 BUFFER* b = (BUFFER*)handle;
Azure.IoT Build 0:fa2de1b79154 328 unsigned char* temp = (unsigned char*)realloc(b->buffer, b->size + enlargeSize);
Azure.IoT Build 0:fa2de1b79154 329 if (temp == NULL)
Azure.IoT Build 0:fa2de1b79154 330 {
Azure.IoT Build 0:fa2de1b79154 331 /* Codes_SRS_BUFFER_07_018: [BUFFER_enlarge shall return a nonzero result if any error is encountered.] */
AzureIoTClient 27:8656a313842b 332 LogError("Failure: allocating temp buffer.");
AzureIoTClient 21:b92006c5b9ff 333 result = __FAILURE__;
Azure.IoT Build 0:fa2de1b79154 334 }
Azure.IoT Build 0:fa2de1b79154 335 else
Azure.IoT Build 0:fa2de1b79154 336 {
Azure.IoT Build 0:fa2de1b79154 337 b->buffer = temp;
Azure.IoT Build 0:fa2de1b79154 338 b->size += enlargeSize;
Azure.IoT Build 0:fa2de1b79154 339 result = 0;
Azure.IoT Build 0:fa2de1b79154 340 }
Azure.IoT Build 0:fa2de1b79154 341 }
Azure.IoT Build 0:fa2de1b79154 342 return result;
Azure.IoT Build 0:fa2de1b79154 343 }
Azure.IoT Build 0:fa2de1b79154 344
AzureIoTClient 27:8656a313842b 345 int BUFFER_shrink(BUFFER_HANDLE handle, size_t decreaseSize, bool fromEnd)
AzureIoTClient 27:8656a313842b 346 {
AzureIoTClient 27:8656a313842b 347 int result;
AzureIoTClient 27:8656a313842b 348 if (handle == NULL)
AzureIoTClient 27:8656a313842b 349 {
AzureIoTClient 27:8656a313842b 350 /* Codes_SRS_BUFFER_07_036: [ if handle is NULL, BUFFER_shrink shall return a non-null value ]*/
AzureIoTClient 27:8656a313842b 351 LogError("Failure: handle is invalid.");
AzureIoTClient 27:8656a313842b 352 result = __FAILURE__;
AzureIoTClient 27:8656a313842b 353 }
AzureIoTClient 27:8656a313842b 354 else if (decreaseSize == 0)
AzureIoTClient 27:8656a313842b 355 {
AzureIoTClient 27:8656a313842b 356 /* Codes_SRS_BUFFER_07_037: [ If decreaseSize is equal zero, BUFFER_shrink shall return a non-null value ] */
AzureIoTClient 27:8656a313842b 357 LogError("Failure: decrease size is 0.");
AzureIoTClient 27:8656a313842b 358 result = __FAILURE__;
AzureIoTClient 27:8656a313842b 359 }
AzureIoTClient 27:8656a313842b 360 else if (decreaseSize > handle->size)
AzureIoTClient 27:8656a313842b 361 {
AzureIoTClient 27:8656a313842b 362 /* Codes_SRS_BUFFER_07_038: [ If decreaseSize is less than the size of the buffer, BUFFER_shrink shall return a non-null value ] */
AzureIoTClient 27:8656a313842b 363 LogError("Failure: decrease size is less than buffer size.");
AzureIoTClient 27:8656a313842b 364 result = __FAILURE__;
AzureIoTClient 27:8656a313842b 365 }
AzureIoTClient 27:8656a313842b 366 else
AzureIoTClient 27:8656a313842b 367 {
AzureIoTClient 27:8656a313842b 368 /* Codes_SRS_BUFFER_07_039: [ BUFFER_shrink shall allocate a temporary buffer of existing buffer size minus decreaseSize. ] */
AzureIoTClient 27:8656a313842b 369 size_t alloc_size = handle->size - decreaseSize;
AzureIoTClient 27:8656a313842b 370 if (alloc_size == 0)
AzureIoTClient 27:8656a313842b 371 {
AzureIoTClient 27:8656a313842b 372 /* Codes_SRS_BUFFER_07_043: [ If the decreaseSize is equal the buffer size , BUFFER_shrink shall deallocate the buffer and set the size to zero. ] */
AzureIoTClient 27:8656a313842b 373 free(handle->buffer);
AzureIoTClient 27:8656a313842b 374 handle->buffer = NULL;
AzureIoTClient 27:8656a313842b 375 handle->size = 0;
AzureIoTClient 27:8656a313842b 376 result = 0;
AzureIoTClient 27:8656a313842b 377 }
AzureIoTClient 27:8656a313842b 378 else
AzureIoTClient 27:8656a313842b 379 {
AzureIoTClient 27:8656a313842b 380 unsigned char* tmp = malloc(alloc_size);
AzureIoTClient 27:8656a313842b 381 if (tmp == NULL)
AzureIoTClient 27:8656a313842b 382 {
AzureIoTClient 27:8656a313842b 383 /* Codes_SRS_BUFFER_07_042: [ If a failure is encountered, BUFFER_shrink shall return a non-null value ] */
AzureIoTClient 27:8656a313842b 384 LogError("Failure: allocating temp buffer.");
AzureIoTClient 27:8656a313842b 385 result = __FAILURE__;
AzureIoTClient 27:8656a313842b 386 }
AzureIoTClient 27:8656a313842b 387 else
AzureIoTClient 27:8656a313842b 388 {
AzureIoTClient 27:8656a313842b 389 if (fromEnd)
AzureIoTClient 27:8656a313842b 390 {
AzureIoTClient 27:8656a313842b 391 /* Codes_SRS_BUFFER_07_040: [ if the fromEnd variable is true, BUFFER_shrink shall remove the end of the buffer of size decreaseSize. ] */
AzureIoTClient 27:8656a313842b 392 memcpy(tmp, handle->buffer, alloc_size);
AzureIoTClient 27:8656a313842b 393 free(handle->buffer);
AzureIoTClient 27:8656a313842b 394 handle->buffer = tmp;
AzureIoTClient 27:8656a313842b 395 handle->size = alloc_size;
AzureIoTClient 27:8656a313842b 396 result = 0;
AzureIoTClient 27:8656a313842b 397 }
AzureIoTClient 27:8656a313842b 398 else
AzureIoTClient 27:8656a313842b 399 {
AzureIoTClient 27:8656a313842b 400 /* Codes_SRS_BUFFER_07_041: [ if the fromEnd variable is false, BUFFER_shrink shall remove the beginning of the buffer of size decreaseSize. ] */
AzureIoTClient 27:8656a313842b 401 memcpy(tmp, handle->buffer + decreaseSize, alloc_size);
AzureIoTClient 27:8656a313842b 402 free(handle->buffer);
AzureIoTClient 27:8656a313842b 403 handle->buffer = tmp;
AzureIoTClient 27:8656a313842b 404 handle->size = alloc_size;
AzureIoTClient 27:8656a313842b 405 result = 0;
AzureIoTClient 27:8656a313842b 406 }
AzureIoTClient 27:8656a313842b 407 }
AzureIoTClient 27:8656a313842b 408 }
AzureIoTClient 27:8656a313842b 409 }
AzureIoTClient 27:8656a313842b 410 return result;
AzureIoTClient 27:8656a313842b 411 }
AzureIoTClient 27:8656a313842b 412
Azure.IoT Build 0:fa2de1b79154 413 /* 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 414 int BUFFER_size(BUFFER_HANDLE handle, size_t* size)
Azure.IoT Build 0:fa2de1b79154 415 {
Azure.IoT Build 0:fa2de1b79154 416 int result;
Azure.IoT Build 0:fa2de1b79154 417 if ((handle == NULL) || (size == NULL))
Azure.IoT Build 0:fa2de1b79154 418 {
Azure.IoT Build 0:fa2de1b79154 419 /* Codes_SRS_BUFFER_07_022: [BUFFER_size shall return a nonzero value for any error that is encountered.] */
AzureIoTClient 21:b92006c5b9ff 420 result = __FAILURE__;
Azure.IoT Build 0:fa2de1b79154 421 }
Azure.IoT Build 0:fa2de1b79154 422 else
Azure.IoT Build 0:fa2de1b79154 423 {
Azure.IoT Build 0:fa2de1b79154 424 BUFFER* b = (BUFFER*)handle;
Azure.IoT Build 0:fa2de1b79154 425 *size = b->size;
Azure.IoT Build 0:fa2de1b79154 426 result = 0;
Azure.IoT Build 0:fa2de1b79154 427 }
Azure.IoT Build 0:fa2de1b79154 428 return result;
Azure.IoT Build 0:fa2de1b79154 429 }
Azure.IoT Build 0:fa2de1b79154 430
Azure.IoT Build 0:fa2de1b79154 431 /* 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 432 int BUFFER_append(BUFFER_HANDLE handle1, BUFFER_HANDLE handle2)
Azure.IoT Build 0:fa2de1b79154 433 {
Azure.IoT Build 0:fa2de1b79154 434 int result;
Azure.IoT Build 0:fa2de1b79154 435 if ( (handle1 == NULL) || (handle2 == NULL) || (handle1 == handle2) )
Azure.IoT Build 0:fa2de1b79154 436 {
Azure.IoT Build 0:fa2de1b79154 437 /* Codes_SRS_BUFFER_07_023: [BUFFER_append shall return a nonzero upon any error that is encountered.] */
AzureIoTClient 21:b92006c5b9ff 438 result = __FAILURE__;
Azure.IoT Build 0:fa2de1b79154 439 }
Azure.IoT Build 0:fa2de1b79154 440 else
Azure.IoT Build 0:fa2de1b79154 441 {
Azure.IoT Build 0:fa2de1b79154 442 BUFFER* b1 = (BUFFER*)handle1;
Azure.IoT Build 0:fa2de1b79154 443 BUFFER* b2 = (BUFFER*)handle2;
Azure.IoT Build 0:fa2de1b79154 444 if (b1->buffer == NULL)
Azure.IoT Build 0:fa2de1b79154 445 {
Azure.IoT Build 0:fa2de1b79154 446 /* Codes_SRS_BUFFER_07_023: [BUFFER_append shall return a nonzero upon any error that is encountered.] */
AzureIoTClient 21:b92006c5b9ff 447 result = __FAILURE__;
Azure.IoT Build 0:fa2de1b79154 448 }
Azure.IoT Build 0:fa2de1b79154 449 else if (b2->buffer == NULL)
Azure.IoT Build 0:fa2de1b79154 450 {
Azure.IoT Build 0:fa2de1b79154 451 /* Codes_SRS_BUFFER_07_023: [BUFFER_append shall return a nonzero upon any error that is encountered.] */
AzureIoTClient 21:b92006c5b9ff 452 result = __FAILURE__;
Azure.IoT Build 0:fa2de1b79154 453 }
Azure.IoT Build 0:fa2de1b79154 454 else
Azure.IoT Build 0:fa2de1b79154 455 {
AzureIoTClient 10:1be0bc9a9deb 456 if (b2->size ==0)
Azure.IoT Build 0:fa2de1b79154 457 {
AzureIoTClient 10:1be0bc9a9deb 458 // b2->size = 0, whatever b1->size is, do nothing
AzureIoTClient 10:1be0bc9a9deb 459 result = 0;
Azure.IoT Build 0:fa2de1b79154 460 }
Azure.IoT Build 0:fa2de1b79154 461 else
Azure.IoT Build 0:fa2de1b79154 462 {
AzureIoTClient 10:1be0bc9a9deb 463 // b2->size != 0, whatever b1->size is
AzureIoTClient 10:1be0bc9a9deb 464 unsigned char* temp = (unsigned char*)realloc(b1->buffer, b1->size + b2->size);
AzureIoTClient 10:1be0bc9a9deb 465 if (temp == NULL)
AzureIoTClient 10:1be0bc9a9deb 466 {
AzureIoTClient 10:1be0bc9a9deb 467 /* Codes_SRS_BUFFER_07_023: [BUFFER_append shall return a nonzero upon any error that is encountered.] */
AzureIoTClient 27:8656a313842b 468 LogError("Failure: allocating temp buffer.");
AzureIoTClient 21:b92006c5b9ff 469 result = __FAILURE__;
AzureIoTClient 10:1be0bc9a9deb 470 }
AzureIoTClient 10:1be0bc9a9deb 471 else
AzureIoTClient 10:1be0bc9a9deb 472 {
AzureIoTClient 10:1be0bc9a9deb 473 /* Codes_SRS_BUFFER_07_024: [BUFFER_append concatenates b2 onto b1 without modifying b2 and shall return zero on success.]*/
AzureIoTClient 10:1be0bc9a9deb 474 b1->buffer = temp;
AzureIoTClient 10:1be0bc9a9deb 475 // Append the BUFFER
AzureIoTClient 10:1be0bc9a9deb 476 (void)memcpy(&b1->buffer[b1->size], b2->buffer, b2->size);
AzureIoTClient 10:1be0bc9a9deb 477 b1->size += b2->size;
AzureIoTClient 10:1be0bc9a9deb 478 result = 0;
AzureIoTClient 10:1be0bc9a9deb 479 }
Azure.IoT Build 0:fa2de1b79154 480 }
Azure.IoT Build 0:fa2de1b79154 481 }
Azure.IoT Build 0:fa2de1b79154 482 }
Azure.IoT Build 0:fa2de1b79154 483 return result;
Azure.IoT Build 0:fa2de1b79154 484 }
Azure.IoT Build 0:fa2de1b79154 485
Azure.IoT Build 0:fa2de1b79154 486 int BUFFER_prepend(BUFFER_HANDLE handle1, BUFFER_HANDLE handle2)
Azure.IoT Build 0:fa2de1b79154 487 {
Azure.IoT Build 0:fa2de1b79154 488 int result;
Azure.IoT Build 0:fa2de1b79154 489 if ((handle1 == NULL) || (handle2 == NULL) || (handle1 == handle2))
Azure.IoT Build 0:fa2de1b79154 490 {
AzureIoTClient 10:1be0bc9a9deb 491 /* Codes_SRS_BUFFER_01_005: [ BUFFER_prepend shall return a non-zero upon value any error that is encountered. ]*/
AzureIoTClient 21:b92006c5b9ff 492 result = __FAILURE__;
Azure.IoT Build 0:fa2de1b79154 493 }
Azure.IoT Build 0:fa2de1b79154 494 else
Azure.IoT Build 0:fa2de1b79154 495 {
Azure.IoT Build 0:fa2de1b79154 496 BUFFER* b1 = (BUFFER*)handle1;
Azure.IoT Build 0:fa2de1b79154 497 BUFFER* b2 = (BUFFER*)handle2;
Azure.IoT Build 0:fa2de1b79154 498 if (b1->buffer == NULL)
Azure.IoT Build 0:fa2de1b79154 499 {
AzureIoTClient 10:1be0bc9a9deb 500 /* Codes_SRS_BUFFER_01_005: [ BUFFER_prepend shall return a non-zero upon value any error that is encountered. ]*/
AzureIoTClient 21:b92006c5b9ff 501 result = __FAILURE__;
Azure.IoT Build 0:fa2de1b79154 502 }
Azure.IoT Build 0:fa2de1b79154 503 else if (b2->buffer == NULL)
Azure.IoT Build 0:fa2de1b79154 504 {
AzureIoTClient 10:1be0bc9a9deb 505 /* Codes_SRS_BUFFER_01_005: [ BUFFER_prepend shall return a non-zero upon value any error that is encountered. ]*/
AzureIoTClient 21:b92006c5b9ff 506 result = __FAILURE__;
Azure.IoT Build 0:fa2de1b79154 507 }
Azure.IoT Build 0:fa2de1b79154 508 else
Azure.IoT Build 0:fa2de1b79154 509 {
AzureIoTClient 10:1be0bc9a9deb 510 //put b2 ahead of b1: [b2][b1], return b1
AzureIoTClient 10:1be0bc9a9deb 511 if (b2->size ==0)
Azure.IoT Build 0:fa2de1b79154 512 {
AzureIoTClient 10:1be0bc9a9deb 513 // do nothing
AzureIoTClient 10:1be0bc9a9deb 514 result = 0;
Azure.IoT Build 0:fa2de1b79154 515 }
Azure.IoT Build 0:fa2de1b79154 516 else
Azure.IoT Build 0:fa2de1b79154 517 {
AzureIoTClient 10:1be0bc9a9deb 518 // b2->size != 0
AzureIoTClient 10:1be0bc9a9deb 519 unsigned char* temp = (unsigned char*)malloc(b1->size + b2->size);
AzureIoTClient 10:1be0bc9a9deb 520 if (temp == NULL)
AzureIoTClient 10:1be0bc9a9deb 521 {
AzureIoTClient 10:1be0bc9a9deb 522 /* Codes_SRS_BUFFER_01_005: [ BUFFER_prepend shall return a non-zero upon value any error that is encountered. ]*/
AzureIoTClient 27:8656a313842b 523 LogError("Failure: allocating temp buffer.");
AzureIoTClient 21:b92006c5b9ff 524 result = __FAILURE__;
AzureIoTClient 10:1be0bc9a9deb 525 }
AzureIoTClient 10:1be0bc9a9deb 526 else
AzureIoTClient 10:1be0bc9a9deb 527 {
AzureIoTClient 10:1be0bc9a9deb 528 /* Codes_SRS_BUFFER_01_004: [ BUFFER_prepend concatenates handle1 onto handle2 without modifying handle1 and shall return zero on success. ]*/
AzureIoTClient 10:1be0bc9a9deb 529 // Append the BUFFER
AzureIoTClient 10:1be0bc9a9deb 530 (void)memcpy(temp, b2->buffer, b2->size);
AzureIoTClient 10:1be0bc9a9deb 531 // start from b1->size to append b1
AzureIoTClient 10:1be0bc9a9deb 532 (void)memcpy(&temp[b2->size], b1->buffer, b1->size);
AzureIoTClient 10:1be0bc9a9deb 533 free(b1->buffer);
AzureIoTClient 10:1be0bc9a9deb 534 b1->buffer = temp;
AzureIoTClient 10:1be0bc9a9deb 535 b1->size += b2->size;
AzureIoTClient 10:1be0bc9a9deb 536 result = 0;
AzureIoTClient 10:1be0bc9a9deb 537 }
Azure.IoT Build 0:fa2de1b79154 538 }
Azure.IoT Build 0:fa2de1b79154 539 }
Azure.IoT Build 0:fa2de1b79154 540 }
Azure.IoT Build 0:fa2de1b79154 541 return result;
Azure.IoT Build 0:fa2de1b79154 542 }
Azure.IoT Build 0:fa2de1b79154 543
AzureIoTClient 30:ce3813c5a692 544 int BUFFER_fill(BUFFER_HANDLE handle, unsigned char fill_char)
AzureIoTClient 30:ce3813c5a692 545 {
AzureIoTClient 30:ce3813c5a692 546 int result;
AzureIoTClient 30:ce3813c5a692 547 if (handle == NULL)
AzureIoTClient 30:ce3813c5a692 548 {
AzureIoTClient 30:ce3813c5a692 549 /* Codes_SRS_BUFFER_07_002: [ If handle is NULL BUFFER_fill shall return a non-zero value. ] */
AzureIoTClient 30:ce3813c5a692 550 LogError("Invalid parameter specified, handle == NULL.");
AzureIoTClient 30:ce3813c5a692 551 result = __FAILURE__;
AzureIoTClient 30:ce3813c5a692 552 }
AzureIoTClient 30:ce3813c5a692 553 else
AzureIoTClient 30:ce3813c5a692 554 {
AzureIoTClient 30:ce3813c5a692 555 size_t index;
AzureIoTClient 30:ce3813c5a692 556 /* Codes_SRS_BUFFER_07_001: [ BUFFER_fill shall fill the supplied BUFFER_HANDLE with the supplied fill character. ] */
AzureIoTClient 30:ce3813c5a692 557 BUFFER* buffer_data = (BUFFER*)handle;
AzureIoTClient 30:ce3813c5a692 558 for (index = 0; index < buffer_data->size; index++)
AzureIoTClient 30:ce3813c5a692 559 {
AzureIoTClient 30:ce3813c5a692 560 buffer_data->buffer[index] = fill_char;
AzureIoTClient 30:ce3813c5a692 561 }
AzureIoTClient 30:ce3813c5a692 562 result = 0;
AzureIoTClient 30:ce3813c5a692 563 }
AzureIoTClient 30:ce3813c5a692 564 return result;
AzureIoTClient 30:ce3813c5a692 565 }
AzureIoTClient 30:ce3813c5a692 566
Azure.IoT Build 0:fa2de1b79154 567
Azure.IoT Build 0:fa2de1b79154 568 /* Codes_SRS_BUFFER_07_025: [BUFFER_u_char shall return a pointer to the underlying unsigned char*.] */
Azure.IoT Build 0:fa2de1b79154 569 unsigned char* BUFFER_u_char(BUFFER_HANDLE handle)
Azure.IoT Build 0:fa2de1b79154 570 {
AzureIoTClient 10:1be0bc9a9deb 571 BUFFER* handleData = (BUFFER*)handle;
Azure.IoT Build 0:fa2de1b79154 572 unsigned char* result;
AzureIoTClient 10:1be0bc9a9deb 573 if (handle == NULL || handleData->size == 0)
Azure.IoT Build 0:fa2de1b79154 574 {
Azure.IoT Build 0:fa2de1b79154 575 /* Codes_SRS_BUFFER_07_026: [BUFFER_u_char shall return NULL for any error that is encountered.] */
AzureIoTClient 10:1be0bc9a9deb 576 /* Codes_SRS_BUFFER_07_029: [BUFFER_u_char shall return NULL if underlying buffer size is zero.] */
Azure.IoT Build 0:fa2de1b79154 577 result = NULL;
Azure.IoT Build 0:fa2de1b79154 578 }
Azure.IoT Build 0:fa2de1b79154 579 else
Azure.IoT Build 0:fa2de1b79154 580 {
AzureIoTClient 10:1be0bc9a9deb 581 result = handleData->buffer;
Azure.IoT Build 0:fa2de1b79154 582 }
Azure.IoT Build 0:fa2de1b79154 583 return result;
Azure.IoT Build 0:fa2de1b79154 584 }
Azure.IoT Build 0:fa2de1b79154 585
Azure.IoT Build 0:fa2de1b79154 586 /* Codes_SRS_BUFFER_07_027: [BUFFER_length shall return the size of the underlying buffer.] */
Azure.IoT Build 0:fa2de1b79154 587 size_t BUFFER_length(BUFFER_HANDLE handle)
Azure.IoT Build 0:fa2de1b79154 588 {
Azure.IoT Build 0:fa2de1b79154 589 size_t result;
Azure.IoT Build 0:fa2de1b79154 590 if (handle == NULL)
Azure.IoT Build 0:fa2de1b79154 591 {
Azure.IoT Build 0:fa2de1b79154 592 /* Codes_SRS_BUFFER_07_028: [BUFFER_length shall return zero for any error that is encountered.] */
Azure.IoT Build 0:fa2de1b79154 593 result = 0;
Azure.IoT Build 0:fa2de1b79154 594 }
Azure.IoT Build 0:fa2de1b79154 595 else
Azure.IoT Build 0:fa2de1b79154 596 {
Azure.IoT Build 0:fa2de1b79154 597 BUFFER* b = (BUFFER*)handle;
Azure.IoT Build 0:fa2de1b79154 598 result = b->size;
Azure.IoT Build 0:fa2de1b79154 599 }
Azure.IoT Build 0:fa2de1b79154 600 return result;
Azure.IoT Build 0:fa2de1b79154 601 }
Azure.IoT Build 0:fa2de1b79154 602
Azure.IoT Build 0:fa2de1b79154 603 BUFFER_HANDLE BUFFER_clone(BUFFER_HANDLE handle)
Azure.IoT Build 0:fa2de1b79154 604 {
Azure.IoT Build 0:fa2de1b79154 605 BUFFER_HANDLE result;
Azure.IoT Build 0:fa2de1b79154 606 if (handle == NULL)
Azure.IoT Build 0:fa2de1b79154 607 {
Azure.IoT Build 0:fa2de1b79154 608 result = NULL;
Azure.IoT Build 0:fa2de1b79154 609 }
Azure.IoT Build 0:fa2de1b79154 610 else
Azure.IoT Build 0:fa2de1b79154 611 {
Azure.IoT Build 0:fa2de1b79154 612 BUFFER* suppliedBuff = (BUFFER*)handle;
AzureIoTClient 10:1be0bc9a9deb 613 BUFFER* b = (BUFFER*)malloc(sizeof(BUFFER));
Azure.IoT Build 0:fa2de1b79154 614 if (b != NULL)
Azure.IoT Build 0:fa2de1b79154 615 {
AzureIoTClient 10:1be0bc9a9deb 616 if (BUFFER_safemalloc(b, suppliedBuff->size) != 0)
Azure.IoT Build 0:fa2de1b79154 617 {
AzureIoTClient 27:8656a313842b 618 LogError("Failure: allocating temp buffer.");
Azure.IoT Build 0:fa2de1b79154 619 result = NULL;
Azure.IoT Build 0:fa2de1b79154 620 }
Azure.IoT Build 0:fa2de1b79154 621 else
Azure.IoT Build 0:fa2de1b79154 622 {
AzureIoTClient 19:2e0811512ceb 623 (void)memcpy(b->buffer, suppliedBuff->buffer, suppliedBuff->size);
Azure.IoT Build 0:fa2de1b79154 624 b->size = suppliedBuff->size;
Azure.IoT Build 0:fa2de1b79154 625 result = (BUFFER_HANDLE)b;
Azure.IoT Build 0:fa2de1b79154 626 }
Azure.IoT Build 0:fa2de1b79154 627 }
Azure.IoT Build 0:fa2de1b79154 628 else
Azure.IoT Build 0:fa2de1b79154 629 {
Azure.IoT Build 0:fa2de1b79154 630 result = NULL;
Azure.IoT Build 0:fa2de1b79154 631 }
Azure.IoT Build 0:fa2de1b79154 632 }
Azure.IoT Build 0:fa2de1b79154 633 return result;
Azure.IoT Build 0:fa2de1b79154 634 }