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