Mark Radbourne / Mbed 2 deprecated iothub_client_sample_amqp

Dependencies:   EthernetInterface NTPClient iothub_amqp_transport iothub_client mbed-rtos mbed

Fork of iothub_client_sample_amqp by Azure IoT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers constbuffer.c Source File

constbuffer.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 "azure_c_shared_utility/constbuffer.h"
00014 #include "azure_c_shared_utility/xlogging.h"
00015 #include "azure_c_shared_utility/refcount.h"
00016 
00017 typedef struct CONSTBUFFER_HANDLE_DATA_TAG
00018 {
00019     CONSTBUFFER alias;
00020 }CONSTBUFFER_HANDLE_DATA;
00021 
00022 DEFINE_REFCOUNT_TYPE(CONSTBUFFER_HANDLE_DATA);
00023 
00024 static CONSTBUFFER_HANDLE CONSTBUFFER_Create_Internal(const unsigned char* source, size_t size)
00025 {
00026     CONSTBUFFER_HANDLE_DATA* result;
00027     /*Codes_SRS_CONSTBUFFER_02_005: [The non-NULL handle returned by CONSTBUFFER_Create shall have its ref count set to "1".]*/
00028     /*Codes_SRS_CONSTBUFFER_02_010: [The non-NULL handle returned by CONSTBUFFER_CreateFromBuffer shall have its ref count set to "1".]*/
00029     result = REFCOUNT_TYPE_CREATE(CONSTBUFFER_HANDLE_DATA);
00030     if (result == NULL)
00031     {
00032         /*Codes_SRS_CONSTBUFFER_02_003: [If creating the copy fails then CONSTBUFFER_Create shall return NULL.]*/
00033         /*Codes_SRS_CONSTBUFFER_02_008: [If copying the content fails, then CONSTBUFFER_CreateFromBuffer shall fail and return NULL.] */
00034         LogError("unable to malloc");
00035         /*return as is*/
00036     }
00037     else
00038     {
00039         /*Codes_SRS_CONSTBUFFER_02_002: [Otherwise, CONSTBUFFER_Create shall create a copy of the memory area pointed to by source having size bytes.]*/
00040         result->alias.size = size;
00041         if (size == 0)
00042         {
00043             result->alias.buffer = NULL;
00044         }
00045         else
00046         {
00047             unsigned char* temp = (unsigned char*)malloc(size);
00048             if (temp == NULL)
00049             {
00050                 /*Codes_SRS_CONSTBUFFER_02_003: [If creating the copy fails then CONSTBUFFER_Create shall return NULL.]*/
00051                 /*Codes_SRS_CONSTBUFFER_02_008: [If copying the content fails, then CONSTBUFFER_CreateFromBuffer shall fail and return NULL.] */
00052                 LogError("unable to malloc");
00053                 free(result);
00054                 result = NULL;
00055             }
00056             else
00057             {
00058                 
00059                 /*Codes_SRS_CONSTBUFFER_02_004: [Otherwise CONSTBUFFER_Create shall return a non-NULL handle.]*/
00060                 /*Codes_SRS_CONSTBUFFER_02_007: [Otherwise, CONSTBUFFER_CreateFromBuffer shall copy the content of buffer.]*/
00061                 /*Codes_SRS_CONSTBUFFER_02_009: [Otherwise, CONSTBUFFER_CreateFromBuffer shall return a non-NULL handle.]*/
00062                 memcpy(temp, source, size);
00063                 result->alias.buffer = temp;
00064             }
00065         }
00066     }
00067     return (CONSTBUFFER_HANDLE)result;
00068 }
00069 
00070 CONSTBUFFER_HANDLE CONSTBUFFER_Create(const unsigned char* source, size_t size)
00071 {
00072     CONSTBUFFER_HANDLE_DATA* result;
00073     /*Codes_SRS_CONSTBUFFER_02_001: [If source is NULL and size is different than 0 then CONSTBUFFER_Create shall fail and return NULL.]*/
00074     if (
00075         (source == NULL) &&
00076         (size != 0)
00077         )
00078     {
00079         LogError("invalid arguments passes to CONSTBUFFER_Create");
00080         result = NULL;
00081     }
00082     else
00083     {
00084         result = (CONSTBUFFER_HANDLE_DATA*)CONSTBUFFER_Create_Internal(source, size);
00085     }
00086     return (CONSTBUFFER_HANDLE)result;
00087 }
00088 
00089 /*this creates a new constbuffer from an existing BUFFER_HANDLE*/
00090 CONSTBUFFER_HANDLE CONSTBUFFER_CreateFromBuffer(BUFFER_HANDLE buffer)
00091 {
00092     CONSTBUFFER_HANDLE_DATA* result;
00093     /*Codes_SRS_CONSTBUFFER_02_006: [If buffer is NULL then CONSTBUFFER_CreateFromBuffer shall fail and return NULL.]*/
00094     if (buffer == NULL)
00095     {
00096         LogError("invalid arg passed to CONSTBUFFER_CreateFromBuffer");
00097         result = NULL;
00098     }
00099     else
00100     {
00101         size_t length = BUFFER_length(buffer);
00102         unsigned char* rawBuffer = BUFFER_u_char(buffer);
00103         result = (CONSTBUFFER_HANDLE_DATA*)CONSTBUFFER_Create_Internal(rawBuffer, length);
00104     }
00105     return (CONSTBUFFER_HANDLE)result;
00106 }
00107 
00108 CONSTBUFFER_HANDLE CONSTBUFFER_Clone(CONSTBUFFER_HANDLE constbufferHandle)
00109 {
00110     if (constbufferHandle == NULL)
00111     {
00112         /*Codes_SRS_CONSTBUFFER_02_013: [If constbufferHandle is NULL then CONSTBUFFER_Clone shall fail and return NULL.]*/
00113         LogError("invalid arg");
00114     }
00115     else
00116     {
00117         /*Codes_SRS_CONSTBUFFER_02_014: [Otherwise, CONSTBUFFER_Clone shall increment the reference count and return constbufferHandle.]*/
00118         INC_REF(CONSTBUFFER_HANDLE_DATA, constbufferHandle);
00119     }
00120     return constbufferHandle;
00121 }
00122 
00123 const CONSTBUFFER* CONSTBUFFER_GetContent(CONSTBUFFER_HANDLE constbufferHandle)
00124 {
00125     const CONSTBUFFER* result;
00126     if (constbufferHandle == NULL)
00127     {
00128         /*Codes_SRS_CONSTBUFFER_02_011: [If constbufferHandle is NULL then CONSTBUFFER_GetContent shall return NULL.]*/
00129         result = NULL;
00130         LogError("invalid arg");
00131     }
00132     else
00133     {
00134         /*Codes_SRS_CONSTBUFFER_02_012: [Otherwise, CONSTBUFFER_GetContent shall return a const CONSTBUFFER* that matches byte by byte the original bytes used to created the const buffer and has the same length.]*/
00135         result = &(((CONSTBUFFER_HANDLE_DATA*)constbufferHandle)->alias);
00136     }
00137     return result;
00138 }
00139 
00140 void CONSTBUFFER_Destroy(CONSTBUFFER_HANDLE constbufferHandle)
00141 {
00142     /*Codes_SRS_CONSTBUFFER_02_015: [If constbufferHandle is NULL then CONSTBUFFER_Destroy shall do nothing.]*/
00143     if (constbufferHandle != NULL)
00144     {
00145         /*Codes_SRS_CONSTBUFFER_02_016: [Otherwise, CONSTBUFFER_Destroy shall decrement the refcount on the constbufferHandle handle.]*/
00146         if (DEC_REF(CONSTBUFFER_HANDLE_DATA, constbufferHandle) == DEC_RETURN_ZERO)
00147         {
00148             /*Codes_SRS_CONSTBUFFER_02_017: [If the refcount reaches zero, then CONSTBUFFER_Destroy shall deallocate all resources used by the CONSTBUFFER_HANDLE.]*/
00149             CONSTBUFFER_HANDLE_DATA* constbufferHandleData = (CONSTBUFFER_HANDLE_DATA*)constbufferHandle;
00150             free((void*)constbufferHandleData->alias.buffer);
00151             free(constbufferHandleData);
00152         }
00153     }
00154 }