Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: EthernetInterface NTPClient iothub_amqp_transport iothub_client mbed-rtos mbed
Fork of iothub_client_sample_amqp by
buffer.c
00001 // Copyright (c) Microsoft. All rights reserved. 00002 // Licensed under the MIT license. See LICENSE file in the project root for full license information. 00003 00004 // 00005 // PUT NO INCLUDES BEFORE HERE 00006 // 00007 #include <stdlib.h> 00008 #ifdef _CRTDBG_MAP_ALLOC 00009 #include <crtdbg.h> 00010 #endif 00011 #include "azure_c_shared_utility/gballoc.h" 00012 00013 #include <stddef.h> 00014 #include <string.h> 00015 // 00016 // PUT NO CLIENT LIBRARY INCLUDES BEFORE HERE 00017 // 00018 00019 #include "azure_c_shared_utility/buffer_.h" 00020 #include "azure_c_shared_utility/xlogging.h" 00021 00022 typedef struct BUFFER_TAG 00023 { 00024 unsigned char* buffer; 00025 size_t size; 00026 }BUFFER; 00027 00028 /* Codes_SRS_BUFFER_07_001: [BUFFER_new shall allocate a BUFFER_HANDLE that will contain a NULL unsigned char*.] */ 00029 BUFFER_HANDLE BUFFER_new(void) 00030 { 00031 BUFFER* temp = (BUFFER*)malloc(sizeof(BUFFER)); 00032 /* Codes_SRS_BUFFER_07_002: [BUFFER_new shall return NULL on any error that occurs.] */ 00033 if (temp != NULL) 00034 { 00035 temp->buffer = NULL; 00036 temp->size = 0; 00037 } 00038 return (BUFFER_HANDLE)temp; 00039 } 00040 00041 static int BUFFER_safemalloc(BUFFER* handleptr, size_t size) 00042 { 00043 int result; 00044 size_t sizetomalloc = size; 00045 if (size == 0) 00046 { 00047 sizetomalloc = 1; 00048 } 00049 handleptr->buffer = (unsigned char*)malloc(sizetomalloc); 00050 if (handleptr->buffer == NULL) 00051 { 00052 /*Codes_SRS_BUFFER_02_003: [If allocating memory fails, then BUFFER_create shall return NULL.]*/ 00053 result = __LINE__; 00054 } 00055 else 00056 { 00057 // we still consider the real buffer size is 0 00058 handleptr->size = size; 00059 result = 0; 00060 } 00061 return result; 00062 } 00063 00064 BUFFER_HANDLE BUFFER_create(const unsigned char* source, size_t size) 00065 { 00066 BUFFER* result; 00067 /*Codes_SRS_BUFFER_02_001: [If source is NULL then BUFFER_create shall return NULL.]*/ 00068 if (source == NULL) 00069 { 00070 result = NULL; 00071 } 00072 else 00073 { 00074 /*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.] */ 00075 result = (BUFFER*)malloc(sizeof(BUFFER)); 00076 if (result == NULL) 00077 { 00078 /*Codes_SRS_BUFFER_02_003: [If allocating memory fails, then BUFFER_create shall return NULL.] */ 00079 /*fallthrough*/ 00080 } 00081 else 00082 { 00083 /* 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.]*/ 00084 if (BUFFER_safemalloc(result, size) != 0) 00085 { 00086 LogError("unable to BUFFER_safemalloc "); 00087 free(result); 00088 result = NULL; 00089 } 00090 else 00091 { 00092 /*Codes_SRS_BUFFER_02_004: [Otherwise, BUFFER_create shall return a non-NULL handle.] */ 00093 memcpy(result->buffer, source, size); 00094 } 00095 } 00096 } 00097 return (BUFFER_HANDLE)result; 00098 } 00099 00100 /* Codes_SRS_BUFFER_07_003: [BUFFER_delete shall delete the data associated with the BUFFER_HANDLE along with the Buffer.] */ 00101 void BUFFER_delete(BUFFER_HANDLE handle) 00102 { 00103 /* Codes_SRS_BUFFER_07_004: [BUFFER_delete shall not delete any BUFFER_HANDLE that is NULL.] */ 00104 if (handle != NULL) 00105 { 00106 BUFFER* b = (BUFFER*)handle; 00107 if (b->buffer != NULL) 00108 { 00109 /* Codes_SRS_BUFFER_07_003: [BUFFER_delete shall delete the data associated with the BUFFER_HANDLE along with the Buffer.] */ 00110 free(b->buffer); 00111 } 00112 free(b); 00113 } 00114 } 00115 00116 /*return 0 if the buffer was copied*/ 00117 /*else return different than zero*/ 00118 /* Codes_SRS_BUFFER_07_008: [BUFFER_build allocates size_t bytes, copies the unsigned char* into the buffer and returns zero on success.] */ 00119 int BUFFER_build(BUFFER_HANDLE handle, const unsigned char* source, size_t size) 00120 { 00121 int result; 00122 if (handle == NULL) 00123 { 00124 /* Codes_SRS_BUFFER_07_009: [BUFFER_build shall return nonzero if handle is NULL ] */ 00125 result = __LINE__; 00126 } 00127 /* 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.] */ 00128 else if (size == 0) 00129 { 00130 /* Codes_SRS_BUFFER_01_003: [If size is zero, source can be NULL.] */ 00131 BUFFER* b = (BUFFER*)handle; 00132 free(b->buffer); 00133 b->buffer = NULL; 00134 b->size = 0; 00135 00136 result = 0; 00137 } 00138 else 00139 { 00140 if (source == NULL) 00141 { 00142 /* Codes_SRS_BUFFER_01_001: [If size is positive and source is NULL, BUFFER_build shall return nonzero] */ 00143 result = __LINE__; 00144 } 00145 else 00146 { 00147 BUFFER* b = (BUFFER*)handle; 00148 /* Codes_SRS_BUFFER_07_011: [BUFFER_build shall overwrite previous contents if the buffer has been previously allocated.] */ 00149 unsigned char* newBuffer = (unsigned char*)realloc(b->buffer, size); 00150 if (newBuffer == NULL) 00151 { 00152 /* Codes_SRS_BUFFER_07_010: [BUFFER_build shall return nonzero if any error is encountered.] */ 00153 result = __LINE__; 00154 } 00155 else 00156 { 00157 b->buffer = newBuffer; 00158 b->size = size; 00159 /* Codes_SRS_BUFFER_01_002: [The size argument can be zero, in which case nothing shall be copied from source.] */ 00160 (void)memcpy(b->buffer, source, size); 00161 00162 result = 0; 00163 } 00164 } 00165 } 00166 00167 return result; 00168 } 00169 00170 /*return 0 if the buffer was pre-build(that is, had its space allocated)*/ 00171 /*else return different than zero*/ 00172 /* Codes_SRS_BUFFER_07_005: [BUFFER_pre_build allocates size_t bytes of BUFFER_HANDLE and returns zero on success.] */ 00173 int BUFFER_pre_build(BUFFER_HANDLE handle, size_t size) 00174 { 00175 int result; 00176 if (handle == NULL) 00177 { 00178 /* Codes_SRS_BUFFER_07_006: [If handle is NULL or size is 0 then BUFFER_pre_build shall return a nonzero value.] */ 00179 result = __LINE__; 00180 } 00181 else if (size == 0) 00182 { 00183 /* Codes_SRS_BUFFER_07_006: [If handle is NULL or size is 0 then BUFFER_pre_build shall return a nonzero value.] */ 00184 result = __LINE__; 00185 } 00186 else 00187 { 00188 BUFFER* b = (BUFFER*)handle; 00189 if (b->buffer != NULL) 00190 { 00191 /* Codes_SRS_BUFFER_07_007: [BUFFER_pre_build shall return nonzero if the buffer has been previously allocated and is not NULL.] */ 00192 result = __LINE__; 00193 } 00194 else 00195 { 00196 if ((b->buffer = (unsigned char*)malloc(size)) == NULL) 00197 { 00198 /* Codes_SRS_BUFFER_07_013: [BUFFER_pre_build shall return nonzero if any error is encountered.] */ 00199 result = __LINE__; 00200 } 00201 else 00202 { 00203 b->size = size; 00204 result = 0; 00205 } 00206 } 00207 } 00208 return result; 00209 } 00210 00211 /* Codes_SRS_BUFFER_07_019: [BUFFER_content shall return the data contained within the BUFFER_HANDLE.] */ 00212 int BUFFER_content(BUFFER_HANDLE handle, const unsigned char** content) 00213 { 00214 int result; 00215 if ((handle == NULL) || (content == NULL)) 00216 { 00217 /* Codes_SRS_BUFFER_07_020: [If the handle and/or content*is NULL BUFFER_content shall return nonzero.] */ 00218 result = __LINE__; 00219 } 00220 else 00221 { 00222 BUFFER* b = (BUFFER*)handle; 00223 *content = b->buffer; 00224 result = 0; 00225 } 00226 return result; 00227 } 00228 00229 /*return 0 if everything went ok and whatever was built in the buffer was unbuilt*/ 00230 /* 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.] */ 00231 extern int BUFFER_unbuild(BUFFER_HANDLE handle) 00232 { 00233 int result; 00234 if (handle == NULL) 00235 { 00236 /* Codes_SRS_BUFFER_07_014: [BUFFER_unbuild shall return a nonzero value if BUFFER_HANDLE is NULL.] */ 00237 result = __LINE__; 00238 } 00239 else 00240 { 00241 BUFFER* b = (BUFFER*)handle; 00242 if (b->buffer != NULL) 00243 { 00244 free(b->buffer); 00245 b->buffer = NULL; 00246 b->size = 0; 00247 result = 0; 00248 } 00249 else 00250 { 00251 /* Codes_SRS_BUFFER_07_015: [BUFFER_unbuild shall return a nonzero value if the unsigned char* referenced by BUFFER_HANDLE is NULL.] */ 00252 result = __LINE__; 00253 } 00254 } 00255 return result; 00256 } 00257 00258 /* Codes_SRS_BUFFER_07_016: [BUFFER_enlarge shall increase the size of the unsigned char* referenced by BUFFER_HANDLE.] */ 00259 int BUFFER_enlarge(BUFFER_HANDLE handle, size_t enlargeSize) 00260 { 00261 int result; 00262 if (handle == NULL) 00263 { 00264 /* Codes_SRS_BUFFER_07_017: [BUFFER_enlarge shall return a nonzero result if any parameters are NULL or zero.] */ 00265 result = __LINE__; 00266 } 00267 else if (enlargeSize == 0) 00268 { 00269 /* Codes_SRS_BUFFER_07_017: [BUFFER_enlarge shall return a nonzero result if any parameters are NULL or zero.] */ 00270 result = __LINE__; 00271 } 00272 else 00273 { 00274 BUFFER* b = (BUFFER*)handle; 00275 unsigned char* temp = (unsigned char*)realloc(b->buffer, b->size + enlargeSize); 00276 if (temp == NULL) 00277 { 00278 /* Codes_SRS_BUFFER_07_018: [BUFFER_enlarge shall return a nonzero result if any error is encountered.] */ 00279 result = __LINE__; 00280 } 00281 else 00282 { 00283 b->buffer = temp; 00284 b->size += enlargeSize; 00285 result = 0; 00286 } 00287 } 00288 return result; 00289 } 00290 00291 /* Codes_SRS_BUFFER_07_021: [BUFFER_size shall place the size of the associated buffer in the size variable and return zero on success.] */ 00292 int BUFFER_size(BUFFER_HANDLE handle, size_t* size) 00293 { 00294 int result; 00295 if ((handle == NULL) || (size == NULL)) 00296 { 00297 /* Codes_SRS_BUFFER_07_022: [BUFFER_size shall return a nonzero value for any error that is encountered.] */ 00298 result = __LINE__; 00299 } 00300 else 00301 { 00302 BUFFER* b = (BUFFER*)handle; 00303 *size = b->size; 00304 result = 0; 00305 } 00306 return result; 00307 } 00308 00309 /* Codes_SRS_BUFFER_07_024: [BUFFER_append concatenates b2 onto b1 without modifying b2 and shall return zero on success.] */ 00310 int BUFFER_append(BUFFER_HANDLE handle1, BUFFER_HANDLE handle2) 00311 { 00312 int result; 00313 if ( (handle1 == NULL) || (handle2 == NULL) || (handle1 == handle2) ) 00314 { 00315 /* Codes_SRS_BUFFER_07_023: [BUFFER_append shall return a nonzero upon any error that is encountered.] */ 00316 result = __LINE__; 00317 } 00318 else 00319 { 00320 BUFFER* b1 = (BUFFER*)handle1; 00321 BUFFER* b2 = (BUFFER*)handle2; 00322 if (b1->buffer == NULL) 00323 { 00324 /* Codes_SRS_BUFFER_07_023: [BUFFER_append shall return a nonzero upon any error that is encountered.] */ 00325 result = __LINE__; 00326 } 00327 else if (b2->buffer == NULL) 00328 { 00329 /* Codes_SRS_BUFFER_07_023: [BUFFER_append shall return a nonzero upon any error that is encountered.] */ 00330 result = __LINE__; 00331 } 00332 else 00333 { 00334 if (b2->size ==0) 00335 { 00336 // b2->size = 0, whatever b1->size is, do nothing 00337 result = 0; 00338 } 00339 else 00340 { 00341 // b2->size != 0, whatever b1->size is 00342 unsigned char* temp = (unsigned char*)realloc(b1->buffer, b1->size + b2->size); 00343 if (temp == NULL) 00344 { 00345 /* Codes_SRS_BUFFER_07_023: [BUFFER_append shall return a nonzero upon any error that is encountered.] */ 00346 result = __LINE__; 00347 } 00348 else 00349 { 00350 /* Codes_SRS_BUFFER_07_024: [BUFFER_append concatenates b2 onto b1 without modifying b2 and shall return zero on success.]*/ 00351 b1->buffer = temp; 00352 // Append the BUFFER 00353 (void)memcpy(&b1->buffer[b1->size], b2->buffer, b2->size); 00354 b1->size += b2->size; 00355 result = 0; 00356 } 00357 } 00358 } 00359 } 00360 return result; 00361 } 00362 00363 int BUFFER_prepend(BUFFER_HANDLE handle1, BUFFER_HANDLE handle2) 00364 { 00365 int result; 00366 if ((handle1 == NULL) || (handle2 == NULL) || (handle1 == handle2)) 00367 { 00368 /* Codes_SRS_BUFFER_01_005: [ BUFFER_prepend shall return a non-zero upon value any error that is encountered. ]*/ 00369 result = __LINE__; 00370 } 00371 else 00372 { 00373 BUFFER* b1 = (BUFFER*)handle1; 00374 BUFFER* b2 = (BUFFER*)handle2; 00375 if (b1->buffer == NULL) 00376 { 00377 /* Codes_SRS_BUFFER_01_005: [ BUFFER_prepend shall return a non-zero upon value any error that is encountered. ]*/ 00378 result = __LINE__; 00379 } 00380 else if (b2->buffer == NULL) 00381 { 00382 /* Codes_SRS_BUFFER_01_005: [ BUFFER_prepend shall return a non-zero upon value any error that is encountered. ]*/ 00383 result = __LINE__; 00384 } 00385 else 00386 { 00387 //put b2 ahead of b1: [b2][b1], return b1 00388 if (b2->size ==0) 00389 { 00390 // do nothing 00391 result = 0; 00392 } 00393 else 00394 { 00395 // b2->size != 0 00396 unsigned char* temp = (unsigned char*)malloc(b1->size + b2->size); 00397 if (temp == NULL) 00398 { 00399 /* Codes_SRS_BUFFER_01_005: [ BUFFER_prepend shall return a non-zero upon value any error that is encountered. ]*/ 00400 result = __LINE__; 00401 } 00402 else 00403 { 00404 /* Codes_SRS_BUFFER_01_004: [ BUFFER_prepend concatenates handle1 onto handle2 without modifying handle1 and shall return zero on success. ]*/ 00405 // Append the BUFFER 00406 (void)memcpy(temp, b2->buffer, b2->size); 00407 // start from b1->size to append b1 00408 (void)memcpy(&temp[b2->size], b1->buffer, b1->size); 00409 free(b1->buffer); 00410 b1->buffer = temp; 00411 b1->size += b2->size; 00412 result = 0; 00413 } 00414 } 00415 } 00416 } 00417 return result; 00418 } 00419 00420 00421 /* Codes_SRS_BUFFER_07_025: [BUFFER_u_char shall return a pointer to the underlying unsigned char*.] */ 00422 unsigned char* BUFFER_u_char(BUFFER_HANDLE handle) 00423 { 00424 BUFFER* handleData = (BUFFER*)handle; 00425 unsigned char* result; 00426 if (handle == NULL || handleData->size == 0) 00427 { 00428 /* Codes_SRS_BUFFER_07_026: [BUFFER_u_char shall return NULL for any error that is encountered.] */ 00429 /* Codes_SRS_BUFFER_07_029: [BUFFER_u_char shall return NULL if underlying buffer size is zero.] */ 00430 result = NULL; 00431 } 00432 else 00433 { 00434 result = handleData->buffer; 00435 } 00436 return result; 00437 } 00438 00439 /* Codes_SRS_BUFFER_07_027: [BUFFER_length shall return the size of the underlying buffer.] */ 00440 size_t BUFFER_length(BUFFER_HANDLE handle) 00441 { 00442 size_t result; 00443 if (handle == NULL) 00444 { 00445 /* Codes_SRS_BUFFER_07_028: [BUFFER_length shall return zero for any error that is encountered.] */ 00446 result = 0; 00447 } 00448 else 00449 { 00450 BUFFER* b = (BUFFER*)handle; 00451 result = b->size; 00452 } 00453 return result; 00454 } 00455 00456 BUFFER_HANDLE BUFFER_clone(BUFFER_HANDLE handle) 00457 { 00458 BUFFER_HANDLE result; 00459 if (handle == NULL) 00460 { 00461 result = NULL; 00462 } 00463 else 00464 { 00465 BUFFER* suppliedBuff = (BUFFER*)handle; 00466 BUFFER* b = (BUFFER*)malloc(sizeof(BUFFER)); 00467 if (b != NULL) 00468 { 00469 if (BUFFER_safemalloc(b, suppliedBuff->size) != 0) 00470 { 00471 result = NULL; 00472 } 00473 else 00474 { 00475 memcpy(b->buffer, suppliedBuff->buffer, suppliedBuff->size); 00476 b->size = suppliedBuff->size; 00477 result = (BUFFER_HANDLE)b; 00478 } 00479 } 00480 else 00481 { 00482 result = NULL; 00483 } 00484 } 00485 return result; 00486 }
Generated on Tue Jul 12 2022 12:43:17 by
