Azure IoT common library

Fork of azure_c_shared_utility by Azure IoT

Committer:
Azure.IoT Build
Date:
Fri Jul 01 10:43:23 2016 -0700
Revision:
6:c55b013dfc2a
Parent:
0:fa2de1b79154
Child:
11:77df6d7e65ae
1.0.10

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 //
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 "azure_c_shared_utility/constbuffer.h"
Azure.IoT Build 6:c55b013dfc2a 14 #include "azure_c_shared_utility/xlogging.h"
Azure.IoT Build 0:fa2de1b79154 15 #include "azure_c_shared_utility/refcount.h"
Azure.IoT Build 0:fa2de1b79154 16
Azure.IoT Build 0:fa2de1b79154 17 typedef struct CONSTBUFFER_HANDLE_DATA_TAG
Azure.IoT Build 0:fa2de1b79154 18 {
Azure.IoT Build 0:fa2de1b79154 19 CONSTBUFFER alias;
Azure.IoT Build 0:fa2de1b79154 20 }CONSTBUFFER_HANDLE_DATA;
Azure.IoT Build 0:fa2de1b79154 21
Azure.IoT Build 0:fa2de1b79154 22 DEFINE_REFCOUNT_TYPE(CONSTBUFFER_HANDLE_DATA);
Azure.IoT Build 0:fa2de1b79154 23
Azure.IoT Build 0:fa2de1b79154 24 static CONSTBUFFER_HANDLE CONSTBUFFER_Create_Internal(const unsigned char* source, size_t size)
Azure.IoT Build 0:fa2de1b79154 25 {
Azure.IoT Build 0:fa2de1b79154 26 CONSTBUFFER_HANDLE_DATA* result;
Azure.IoT Build 0:fa2de1b79154 27 /*Codes_SRS_CONSTBUFFER_02_005: [The non-NULL handle returned by CONSTBUFFER_Create shall have its ref count set to "1".]*/
Azure.IoT Build 0:fa2de1b79154 28 /*Codes_SRS_CONSTBUFFER_02_010: [The non-NULL handle returned by CONSTBUFFER_CreateFromBuffer shall have its ref count set to "1".]*/
Azure.IoT Build 0:fa2de1b79154 29 result = REFCOUNT_TYPE_CREATE(CONSTBUFFER_HANDLE_DATA);
Azure.IoT Build 0:fa2de1b79154 30 if (result == NULL)
Azure.IoT Build 0:fa2de1b79154 31 {
Azure.IoT Build 0:fa2de1b79154 32 /*Codes_SRS_CONSTBUFFER_02_003: [If creating the copy fails then CONSTBUFFER_Create shall return NULL.]*/
Azure.IoT Build 0:fa2de1b79154 33 /*Codes_SRS_CONSTBUFFER_02_008: [If copying the content fails, then CONSTBUFFER_CreateFromBuffer shall fail and return NULL.] */
Azure.IoT Build 0:fa2de1b79154 34 LogError("unable to malloc");
Azure.IoT Build 0:fa2de1b79154 35 /*return as is*/
Azure.IoT Build 0:fa2de1b79154 36 }
Azure.IoT Build 0:fa2de1b79154 37 else
Azure.IoT Build 0:fa2de1b79154 38 {
Azure.IoT Build 0:fa2de1b79154 39 /*Codes_SRS_CONSTBUFFER_02_002: [Otherwise, CONSTBUFFER_Create shall create a copy of the memory area pointed to by source having size bytes.]*/
Azure.IoT Build 0:fa2de1b79154 40 result->alias.size = size;
Azure.IoT Build 0:fa2de1b79154 41 if (size == 0)
Azure.IoT Build 0:fa2de1b79154 42 {
Azure.IoT Build 0:fa2de1b79154 43 result->alias.buffer = NULL;
Azure.IoT Build 0:fa2de1b79154 44 }
Azure.IoT Build 0:fa2de1b79154 45 else
Azure.IoT Build 0:fa2de1b79154 46 {
Azure.IoT Build 0:fa2de1b79154 47 unsigned char* temp = (unsigned char*)malloc(size);
Azure.IoT Build 0:fa2de1b79154 48 if (temp == NULL)
Azure.IoT Build 0:fa2de1b79154 49 {
Azure.IoT Build 0:fa2de1b79154 50 /*Codes_SRS_CONSTBUFFER_02_003: [If creating the copy fails then CONSTBUFFER_Create shall return NULL.]*/
Azure.IoT Build 0:fa2de1b79154 51 /*Codes_SRS_CONSTBUFFER_02_008: [If copying the content fails, then CONSTBUFFER_CreateFromBuffer shall fail and return NULL.] */
Azure.IoT Build 0:fa2de1b79154 52 LogError("unable to malloc");
Azure.IoT Build 0:fa2de1b79154 53 free(result);
Azure.IoT Build 0:fa2de1b79154 54 result = NULL;
Azure.IoT Build 0:fa2de1b79154 55 }
Azure.IoT Build 0:fa2de1b79154 56 else
Azure.IoT Build 0:fa2de1b79154 57 {
Azure.IoT Build 0:fa2de1b79154 58
Azure.IoT Build 0:fa2de1b79154 59 /*Codes_SRS_CONSTBUFFER_02_004: [Otherwise CONSTBUFFER_Create shall return a non-NULL handle.]*/
Azure.IoT Build 0:fa2de1b79154 60 /*Codes_SRS_CONSTBUFFER_02_007: [Otherwise, CONSTBUFFER_CreateFromBuffer shall copy the content of buffer.]*/
Azure.IoT Build 0:fa2de1b79154 61 /*Codes_SRS_CONSTBUFFER_02_009: [Otherwise, CONSTBUFFER_CreateFromBuffer shall return a non-NULL handle.]*/
Azure.IoT Build 0:fa2de1b79154 62 memcpy(temp, source, size);
Azure.IoT Build 0:fa2de1b79154 63 result->alias.buffer = temp;
Azure.IoT Build 0:fa2de1b79154 64 }
Azure.IoT Build 0:fa2de1b79154 65 }
Azure.IoT Build 0:fa2de1b79154 66 }
Azure.IoT Build 0:fa2de1b79154 67 return (CONSTBUFFER_HANDLE)result;
Azure.IoT Build 0:fa2de1b79154 68 }
Azure.IoT Build 0:fa2de1b79154 69
Azure.IoT Build 0:fa2de1b79154 70 CONSTBUFFER_HANDLE CONSTBUFFER_Create(const unsigned char* source, size_t size)
Azure.IoT Build 0:fa2de1b79154 71 {
Azure.IoT Build 0:fa2de1b79154 72 CONSTBUFFER_HANDLE_DATA* result;
Azure.IoT Build 0:fa2de1b79154 73 /*Codes_SRS_CONSTBUFFER_02_001: [If source is NULL and size is different than 0 then CONSTBUFFER_Create shall fail and return NULL.]*/
Azure.IoT Build 0:fa2de1b79154 74 if (
Azure.IoT Build 0:fa2de1b79154 75 (source == NULL) &&
Azure.IoT Build 0:fa2de1b79154 76 (size != 0)
Azure.IoT Build 0:fa2de1b79154 77 )
Azure.IoT Build 0:fa2de1b79154 78 {
Azure.IoT Build 0:fa2de1b79154 79 LogError("invalid arguments passes to CONSTBUFFER_Create");
Azure.IoT Build 0:fa2de1b79154 80 result = NULL;
Azure.IoT Build 0:fa2de1b79154 81 }
Azure.IoT Build 0:fa2de1b79154 82 else
Azure.IoT Build 0:fa2de1b79154 83 {
Azure.IoT Build 0:fa2de1b79154 84 result = (CONSTBUFFER_HANDLE_DATA*)CONSTBUFFER_Create_Internal(source, size);
Azure.IoT Build 0:fa2de1b79154 85 }
Azure.IoT Build 0:fa2de1b79154 86 return (CONSTBUFFER_HANDLE)result;
Azure.IoT Build 0:fa2de1b79154 87 }
Azure.IoT Build 0:fa2de1b79154 88
Azure.IoT Build 0:fa2de1b79154 89 /*this creates a new constbuffer from an existing BUFFER_HANDLE*/
Azure.IoT Build 0:fa2de1b79154 90 CONSTBUFFER_HANDLE CONSTBUFFER_CreateFromBuffer(BUFFER_HANDLE buffer)
Azure.IoT Build 0:fa2de1b79154 91 {
Azure.IoT Build 0:fa2de1b79154 92 CONSTBUFFER_HANDLE_DATA* result;
Azure.IoT Build 0:fa2de1b79154 93 /*Codes_SRS_CONSTBUFFER_02_006: [If buffer is NULL then CONSTBUFFER_CreateFromBuffer shall fail and return NULL.]*/
Azure.IoT Build 0:fa2de1b79154 94 if (buffer == NULL)
Azure.IoT Build 0:fa2de1b79154 95 {
Azure.IoT Build 0:fa2de1b79154 96 LogError("invalid arg passed to CONSTBUFFER_CreateFromBuffer");
Azure.IoT Build 0:fa2de1b79154 97 result = NULL;
Azure.IoT Build 0:fa2de1b79154 98 }
Azure.IoT Build 0:fa2de1b79154 99 else
Azure.IoT Build 0:fa2de1b79154 100 {
Azure.IoT Build 0:fa2de1b79154 101 result = (CONSTBUFFER_HANDLE_DATA*)CONSTBUFFER_Create_Internal(BUFFER_u_char(buffer), BUFFER_length(buffer));
Azure.IoT Build 0:fa2de1b79154 102 }
Azure.IoT Build 0:fa2de1b79154 103 return (CONSTBUFFER_HANDLE)result;
Azure.IoT Build 0:fa2de1b79154 104 }
Azure.IoT Build 0:fa2de1b79154 105
Azure.IoT Build 0:fa2de1b79154 106 CONSTBUFFER_HANDLE CONSTBUFFER_Clone(CONSTBUFFER_HANDLE constbufferHandle)
Azure.IoT Build 0:fa2de1b79154 107 {
Azure.IoT Build 0:fa2de1b79154 108 if (constbufferHandle == NULL)
Azure.IoT Build 0:fa2de1b79154 109 {
Azure.IoT Build 0:fa2de1b79154 110 /*Codes_SRS_CONSTBUFFER_02_013: [If constbufferHandle is NULL then CONSTBUFFER_Clone shall fail and return NULL.]*/
Azure.IoT Build 0:fa2de1b79154 111 LogError("invalid arg");
Azure.IoT Build 0:fa2de1b79154 112 }
Azure.IoT Build 0:fa2de1b79154 113 else
Azure.IoT Build 0:fa2de1b79154 114 {
Azure.IoT Build 0:fa2de1b79154 115 /*Codes_SRS_CONSTBUFFER_02_014: [Otherwise, CONSTBUFFER_Clone shall increment the reference count and return constbufferHandle.]*/
Azure.IoT Build 0:fa2de1b79154 116 INC_REF(CONSTBUFFER_HANDLE_DATA, constbufferHandle);
Azure.IoT Build 0:fa2de1b79154 117 }
Azure.IoT Build 0:fa2de1b79154 118 return constbufferHandle;
Azure.IoT Build 0:fa2de1b79154 119 }
Azure.IoT Build 0:fa2de1b79154 120
Azure.IoT Build 0:fa2de1b79154 121 const CONSTBUFFER* CONSTBUFFER_GetContent(CONSTBUFFER_HANDLE constbufferHandle)
Azure.IoT Build 0:fa2de1b79154 122 {
Azure.IoT Build 0:fa2de1b79154 123 const CONSTBUFFER* result;
Azure.IoT Build 0:fa2de1b79154 124 if (constbufferHandle == NULL)
Azure.IoT Build 0:fa2de1b79154 125 {
Azure.IoT Build 0:fa2de1b79154 126 /*Codes_SRS_CONSTBUFFER_02_011: [If constbufferHandle is NULL then CONSTBUFFER_GetContent shall return NULL.]*/
Azure.IoT Build 0:fa2de1b79154 127 result = NULL;
Azure.IoT Build 0:fa2de1b79154 128 LogError("invalid arg");
Azure.IoT Build 0:fa2de1b79154 129 }
Azure.IoT Build 0:fa2de1b79154 130 else
Azure.IoT Build 0:fa2de1b79154 131 {
Azure.IoT Build 0:fa2de1b79154 132 /*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.]*/
Azure.IoT Build 0:fa2de1b79154 133 result = &(((CONSTBUFFER_HANDLE_DATA*)constbufferHandle)->alias);
Azure.IoT Build 0:fa2de1b79154 134 }
Azure.IoT Build 0:fa2de1b79154 135 return result;
Azure.IoT Build 0:fa2de1b79154 136 }
Azure.IoT Build 0:fa2de1b79154 137
Azure.IoT Build 0:fa2de1b79154 138 void CONSTBUFFER_Destroy(CONSTBUFFER_HANDLE constbufferHandle)
Azure.IoT Build 0:fa2de1b79154 139 {
Azure.IoT Build 0:fa2de1b79154 140 /*Codes_SRS_CONSTBUFFER_02_015: [If constbufferHandle is NULL then CONSTBUFFER_Destroy shall do nothing.]*/
Azure.IoT Build 0:fa2de1b79154 141 if (constbufferHandle != NULL)
Azure.IoT Build 0:fa2de1b79154 142 {
Azure.IoT Build 0:fa2de1b79154 143 /*Codes_SRS_CONSTBUFFER_02_016: [Otherwise, CONSTBUFFER_Destroy shall decrement the refcount on the constbufferHandle handle.]*/
Azure.IoT Build 0:fa2de1b79154 144 if (DEC_REF(CONSTBUFFER_HANDLE_DATA, constbufferHandle) == DEC_RETURN_ZERO)
Azure.IoT Build 0:fa2de1b79154 145 {
Azure.IoT Build 0:fa2de1b79154 146 /*Codes_SRS_CONSTBUFFER_02_017: [If the refcount reaches zero, then CONSTBUFFER_Destroy shall deallocate all resources used by the CONSTBUFFER_HANDLE.]*/
Azure.IoT Build 0:fa2de1b79154 147 CONSTBUFFER_HANDLE_DATA* constbufferHandleData = (CONSTBUFFER_HANDLE_DATA*)constbufferHandle;
Azure.IoT Build 0:fa2de1b79154 148 free((void*)constbufferHandleData->alias.buffer);
Azure.IoT Build 0:fa2de1b79154 149 free(constbufferHandleData);
Azure.IoT Build 0:fa2de1b79154 150 }
Azure.IoT Build 0:fa2de1b79154 151 }
Azure.IoT Build 0:fa2de1b79154 152 }