CONSTMAP_HANDLE ConstMap_Create(MAP_HANDLE sourceMap) { CONSTMAP_HANDLE_DATA* result = REFCOUNT_TYPE_CREATE(CONSTMAP_HANDLE_DATA); if (result == NULL) { LOG_CONSTMAP_ERROR(CONSTMAP_INVALIDARG); } else { /*Codes_SRS_CONSTMAP_17_048: [ConstMap_Create shall accept any non-NULL MAP_HANDLE as input.]*/ /*Codes_SRS_CONSTMAP_17_001: [ConstMap_Create shall create an immutable map, populated by the key, value pairs in the source map.]*/ result->map = Map_Clone(sourceMap); if (result->map == NULL) { free(result); /*Codes_SRS_CONSTMAP_17_002: [If during creation there are any errors, then ConstMap_Create shall return NULL.]*/ result = NULL; LOG_CONSTMAP_ERROR(CONSTMAP_ERROR); } } /*Codes_SRS_CONSTMAP_17_003: [Otherwise, it shall return a non-NULL handle that can be used in subsequent calls.]*/ return (CONSTMAP_HANDLE)result; }
HTTP_HEADERS_HANDLE HTTPHeaders_Clone(HTTP_HEADERS_HANDLE handle) { HTTP_HEADERS_HANDLE_DATA* result; /*Codes_SRS_HTTP_HEADERS_02_003: [If handle is NULL then HTTPHeaders_Clone shall return NULL.] */ if (handle == NULL) { result = NULL; } else { /*Codes_SRS_HTTP_HEADERS_02_004: [Otherwise HTTPHeaders_Clone shall clone the content of handle to a new handle.] */ result = malloc(sizeof(HTTP_HEADERS_HANDLE_DATA)); if (result == NULL) { /*Codes_SRS_HTTP_HEADERS_02_005: [If cloning fails for any reason, then HTTPHeaders_Clone shall return NULL.] */ } else { HTTP_HEADERS_HANDLE_DATA* handleData = handle; result->headers = Map_Clone(handleData->headers); if (result->headers == NULL) { /*Codes_SRS_HTTP_HEADERS_02_005: [If cloning fails for any reason, then HTTPHeaders_Clone shall return NULL.] */ free(result); result = NULL; } else { /*all is fine*/ } } } return result; }
MAP_HANDLE ConstMap_CloneWriteable(CONSTMAP_HANDLE handle) { MAP_HANDLE result = NULL; if (handle == NULL) { /*Codes_SRS_CONSTMAP_17_051: [ConstMap_CloneWriteable returns NULL if parameter handle is NULL. ]*/ LOG_CONSTMAP_ERROR(CONSTMAP_INVALIDARG); } else { /*Codes_SRS_CONSTMAP_17_052: [ConstMap_CloneWriteable shall create a new, writeable map, populated by the key, value pairs in the parameter defined by handle.]*/ /*Codes_SRS_CONSTMAP_17_053: [If during cloning, any operation fails, then ConstMap_CloneWriteableap_Clone shall return NULL.]*/ /*Codes_SRS_CONSTMAP_17_054: [Otherwise, ConstMap_CloneWriteable shall return a non-NULL handle that can be used in subsequent calls.]*/ result = Map_Clone(((CONSTMAP_HANDLE_DATA *)handle)->map); } return result; }
/*Codes_SRS_IOTHUBMESSAGE_03_001: [IoTHubMessage_Clone shall create a new IoT hub message with data content identical to that of the iotHubMessageHandle parameter.]*/ IOTHUB_MESSAGE_HANDLE IoTHubMessage_Clone(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle) { IOTHUB_MESSAGE_HANDLE_DATA* result; const IOTHUB_MESSAGE_HANDLE_DATA* source = (const IOTHUB_MESSAGE_HANDLE_DATA*)iotHubMessageHandle; /* Codes_SRS_IOTHUBMESSAGE_03_005: [IoTHubMessage_Clone shall return NULL if iotHubMessageHandle is NULL.] */ if (source == NULL) { result = NULL; LogError("iotHubMessageHandle parameter cannot be NULL for IoTHubMessage_Clone\r\n"); } else { result = (IOTHUB_MESSAGE_HANDLE_DATA*)malloc(sizeof(IOTHUB_MESSAGE_HANDLE_DATA)); /*Codes_SRS_IOTHUBMESSAGE_03_004: [IoTHubMessage_Clone shall return NULL if it fails for any reason.]*/ if (result == NULL) { /*Codes_SRS_IOTHUBMESSAGE_03_004: [IoTHubMessage_Clone shall return NULL if it fails for any reason.]*/ /*do nothing and return as is*/ LogError("unable to malloc\r\n"); } else { result->messageId = NULL; result->correlationId = NULL; if (source->messageId != NULL && mallocAndStrcpy_s(&result->messageId, source->messageId) != 0) { LogError("unable to Copy messageId\r\n"); free(result); result = NULL; } else if (source->correlationId != NULL && mallocAndStrcpy_s(&result->correlationId, source->correlationId) != 0) { LogError("unable to Copy correlationId\r\n"); if (result->messageId != NULL) { free(result->messageId); result->messageId = NULL; } free(result); result = NULL; } else if (source->contentType == IOTHUBMESSAGE_BYTEARRAY) { /*Codes_SRS_IOTHUBMESSAGE_02_006: [IoTHubMessage_Clone shall clone to content by a call to BUFFER_clone] */ if ((result->value.byteArray = BUFFER_clone(source->value.byteArray)) == NULL) { /*Codes_SRS_IOTHUBMESSAGE_03_004: [IoTHubMessage_Clone shall return NULL if it fails for any reason.]*/ LogError("unable to BUFFER_clone\r\n"); if (result->messageId) { free(result->messageId); result->messageId = NULL; } if (result->correlationId != NULL) { free(result->correlationId); result->correlationId = NULL; } free(result); result = NULL; } /*Codes_SRS_IOTHUBMESSAGE_02_005: [IoTHubMessage_Clone shall clone the properties map by using Map_Clone.] */ else if ((result->properties = Map_Clone(source->properties)) == NULL) { /*Codes_SRS_IOTHUBMESSAGE_03_004: [IoTHubMessage_Clone shall return NULL if it fails for any reason.]*/ LogError("unable to Map_Clone\r\n"); BUFFER_delete(result->value.byteArray); if (result->messageId) { free(result->messageId); result->messageId = NULL; } if (result->correlationId != NULL) { free(result->correlationId); result->correlationId = NULL; } free(result); result = NULL; } else { result->contentType = IOTHUBMESSAGE_BYTEARRAY; /*Codes_SRS_IOTHUBMESSAGE_03_002: [IoTHubMessage_Clone shall return upon success a non-NULL handle to the newly created IoT hub message.]*/ /*return as is, this is a good result*/ } } else /*can only be STRING*/ { /*Codes_SRS_IOTHUBMESSAGE_02_006: [IoTHubMessage_Clone shall clone the content by a call to BUFFER_clone or STRING_clone] */ if ((result->value.string = STRING_clone(source->value.string)) == NULL) { /*Codes_SRS_IOTHUBMESSAGE_03_004: [IoTHubMessage_Clone shall return NULL if it fails for any reason.]*/ if (result->messageId) { free(result->messageId); result->messageId = NULL; } if (result->correlationId != NULL) { free(result->correlationId); result->correlationId = NULL; } free(result); result = NULL; LogError("failed to STRING_clone\r\n"); } /*Codes_SRS_IOTHUBMESSAGE_02_005: [IoTHubMessage_Clone shall clone the properties map by using Map_Clone.] */ else if ((result->properties = Map_Clone(source->properties)) == NULL) { /*Codes_SRS_IOTHUBMESSAGE_03_004: [IoTHubMessage_Clone shall return NULL if it fails for any reason.]*/ LogError("unable to Map_Clone\r\n"); STRING_delete(result->value.string); if (result->messageId) { free(result->messageId); result->messageId = NULL; } if (result->correlationId != NULL) { free(result->correlationId); result->correlationId = NULL; } free(result); result = NULL; } else { result->contentType = IOTHUBMESSAGE_STRING; /*all is fine*/ } } } } return result; }
/*Codes_SRS_IOTHUBMESSAGE_03_001: [IoTHubMessage_Clone shall create a new IoT hub message with data content identical to that of the iotHubMessageHandle parameter.]*/ IOTHUB_MESSAGE_HANDLE IoTHubMessage_Clone(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle) { IOTHUB_MESSAGE_HANDLE_DATA* result; const IOTHUB_MESSAGE_HANDLE_DATA* source = (const IOTHUB_MESSAGE_HANDLE_DATA*)iotHubMessageHandle; /* Codes_SRS_IOTHUBMESSAGE_03_005: [IoTHubMessage_Clone shall return NULL if iotHubMessageHandle is NULL.] */ if (source == NULL) { result = NULL; LogError("iotHubMessageHandle parameter cannot be NULL for IoTHubMessage_Clone"); } else { result = (IOTHUB_MESSAGE_HANDLE_DATA*)malloc(sizeof(IOTHUB_MESSAGE_HANDLE_DATA)); /*Codes_SRS_IOTHUBMESSAGE_03_004: [IoTHubMessage_Clone shall return NULL if it fails for any reason.]*/ if (result == NULL) { /*Codes_SRS_IOTHUBMESSAGE_03_004: [IoTHubMessage_Clone shall return NULL if it fails for any reason.]*/ /*do nothing and return as is*/ LogError("unable to malloc"); } else { memset(result, 0, sizeof(*result)); result->contentType = source->contentType; if (source->messageId != NULL && mallocAndStrcpy_s(&result->messageId, source->messageId) != 0) { LogError("unable to Copy messageId"); DestroyMessageData(result); result = NULL; } else if (source->correlationId != NULL && mallocAndStrcpy_s(&result->correlationId, source->correlationId) != 0) { LogError("unable to Copy correlationId"); DestroyMessageData(result); result = NULL; } else if (source->userDefinedContentType != NULL && mallocAndStrcpy_s(&result->userDefinedContentType, source->userDefinedContentType) != 0) { LogError("unable to copy contentType"); DestroyMessageData(result); result = NULL; } else if (source->contentEncoding != NULL && mallocAndStrcpy_s(&result->contentEncoding, source->contentEncoding) != 0) { LogError("unable to copy contentEncoding"); DestroyMessageData(result); result = NULL; } else if (source->diagnosticData != NULL && (result->diagnosticData = CloneDiagnosticPropertyData(source->diagnosticData)) == NULL) { LogError("unable to CloneDiagnosticPropertyData"); DestroyMessageData(result); result = NULL; } else if (source->contentType == IOTHUBMESSAGE_BYTEARRAY) { /*Codes_SRS_IOTHUBMESSAGE_02_006: [IoTHubMessage_Clone shall clone to content by a call to BUFFER_clone] */ if ((result->value.byteArray = BUFFER_clone(source->value.byteArray)) == NULL) { /*Codes_SRS_IOTHUBMESSAGE_03_004: [IoTHubMessage_Clone shall return NULL if it fails for any reason.]*/ LogError("unable to BUFFER_clone"); DestroyMessageData(result); result = NULL; } /*Codes_SRS_IOTHUBMESSAGE_02_005: [IoTHubMessage_Clone shall clone the properties map by using Map_Clone.] */ else if ((result->properties = Map_Clone(source->properties)) == NULL) { /*Codes_SRS_IOTHUBMESSAGE_03_004: [IoTHubMessage_Clone shall return NULL if it fails for any reason.]*/ LogError("unable to Map_Clone"); DestroyMessageData(result); result = NULL; } /*Codes_SRS_IOTHUBMESSAGE_03_002: [IoTHubMessage_Clone shall return upon success a non-NULL handle to the newly created IoT hub message.]*/ } else /*can only be STRING*/ { /*Codes_SRS_IOTHUBMESSAGE_02_006: [IoTHubMessage_Clone shall clone the content by a call to BUFFER_clone or STRING_clone] */ if ((result->value.string = STRING_clone(source->value.string)) == NULL) { /*Codes_SRS_IOTHUBMESSAGE_03_004: [IoTHubMessage_Clone shall return NULL if it fails for any reason.]*/ LogError("failed to STRING_clone"); DestroyMessageData(result); result = NULL; } /*Codes_SRS_IOTHUBMESSAGE_02_005: [IoTHubMessage_Clone shall clone the properties map by using Map_Clone.] */ else if ((result->properties = Map_Clone(source->properties)) == NULL) { /*Codes_SRS_IOTHUBMESSAGE_03_004: [IoTHubMessage_Clone shall return NULL if it fails for any reason.]*/ LogError("unable to Map_Clone"); DestroyMessageData(result); result = NULL; } } } } return result; }