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