Nigel Rantor / azure_c_shared_utility

Fork of azure_c_shared_utility by Azure IoT

Files at this revision

API Documentation at this revision

Comitter:
AzureIoTClient
Date:
Fri Jul 14 16:38:40 2017 -0700
Parent:
29:e3ed192c35fb
Child:
31:6a55d47aea41
Commit message:
1.1.19

Changed in this revision

azure_c_shared_utility/buffer_.h Show annotated file Show diff for this revision Revisions of this file
azure_c_shared_utility/crt_abstractions.h Show annotated file Show diff for this revision Revisions of this file
azure_c_shared_utility/shared_util_options.h Show annotated file Show diff for this revision Revisions of this file
azure_c_shared_utility/singlylinkedlist.h Show annotated file Show diff for this revision Revisions of this file
azure_c_shared_utility/strings.h Show annotated file Show diff for this revision Revisions of this file
buffer.c Show annotated file Show diff for this revision Revisions of this file
singlylinkedlist.c Show annotated file Show diff for this revision Revisions of this file
strings.c Show annotated file Show diff for this revision Revisions of this file
tlsio_wolfssl.c Show annotated file Show diff for this revision Revisions of this file
--- a/azure_c_shared_utility/buffer_.h	Fri Jun 30 10:42:00 2017 -0700
+++ b/azure_c_shared_utility/buffer_.h	Fri Jul 14 16:38:40 2017 -0700
@@ -31,6 +31,7 @@
 MOCKABLE_FUNCTION(, int, BUFFER_size, BUFFER_HANDLE, handle, size_t*, size);
 MOCKABLE_FUNCTION(, int, BUFFER_append, BUFFER_HANDLE, handle1, BUFFER_HANDLE, handle2);
 MOCKABLE_FUNCTION(, int, BUFFER_prepend, BUFFER_HANDLE, handle1, BUFFER_HANDLE, handle2);
+MOCKABLE_FUNCTION(, int, BUFFER_fill, BUFFER_HANDLE, handle, unsigned char, fill_char);
 MOCKABLE_FUNCTION(, unsigned char*, BUFFER_u_char, BUFFER_HANDLE, handle);
 MOCKABLE_FUNCTION(, size_t, BUFFER_length, BUFFER_HANDLE, handle);
 MOCKABLE_FUNCTION(, BUFFER_HANDLE, BUFFER_clone, BUFFER_HANDLE, handle);
--- a/azure_c_shared_utility/crt_abstractions.h	Fri Jun 30 10:42:00 2017 -0700
+++ b/azure_c_shared_utility/crt_abstractions.h	Fri Jul 14 16:38:40 2017 -0700
@@ -12,11 +12,11 @@
 #include <cerrno>
 #include <cmath>
 extern "C" {
-#else
+#else // __cplusplus
 #include <stdio.h>
 #include <string.h>
 #include <errno.h>
-#endif
+#endif // __cplusplus
 
 #ifdef _MSC_VER
 
@@ -27,7 +27,7 @@
 #else
 /*galileo apparently has _Bool and bool as built in types*/
 #endif
-#endif
+#endif // QUARKGALILEO
 
 #ifndef _WIN32_WCE
 #define HAS_STDBOOL
@@ -35,10 +35,10 @@
 #include <cstdbool>
 /*because C++ doesn't do anything about _Bool... */
 #define _Bool bool
-#else
+#else // __cplusplus
 #include <stdbool.h>
-#endif
-#else 
+#endif // __cplusplus
+#else // _WIN32_WCE
 /* WINCE does not support bool as C datatype */
 #define __bool_true_false_are_defined	1
 
@@ -48,40 +48,42 @@
 
 #ifdef __cplusplus
 #define _CSTDBOOL_
-#else
+#else // __cplusplus
 typedef unsigned char bool;
 
 #define false	0
 #define true	1
-#endif
-#endif
-#else
+#endif // __cplusplus
+#endif // _WIN32_WCE
+
+#else //  _MSC_VER
+
 #if defined __STDC_VERSION__
 #if ((__STDC_VERSION__  == 199901L) || (__STDC_VERSION__ == 201000L) || (__STDC_VERSION__ == 201112L))
 /*C99 compiler or C11*/
 #define HAS_STDBOOL
 #include <stdbool.h>
-#endif
-#endif
-#endif
+#endif //  ((__STDC_VERSION__  == 199901L) || (__STDC_VERSION__ == 201000L) || (__STDC_VERSION__ == 201112L))
+#endif // __STDC_VERSION__
+#endif //  _MSC_VER
 
 #ifndef HAS_STDBOOL
 #ifdef __cplusplus
 #define _Bool bool
-#else
+#else // __cplusplus
 typedef unsigned char _Bool;
 typedef unsigned char bool;
 #define false 0
 #define true 1
-#endif
-#endif
+#endif // __cplusplus
+#endif // HAS_STDBOOL
 
 
 /* Codes_SRS_CRT_ABSTRACTIONS_99_001:[The module shall not redefine the secure functions implemented by Microsoft CRT.] */
 /* Codes_SRS_CRT_ABSTRACTIONS_99_040 : [The module shall still compile when building on a Microsoft platform.] */
 /* Codes_SRS_CRT_ABSTRACTIONS_99_002: [CRTAbstractions module shall expose the following API]*/
 #ifdef _MSC_VER
-#else
+#else // _MSC_VER
 #include "inttypes.h"
 
 /* Adding definitions from errno.h & crtdefs.h */
@@ -97,7 +99,7 @@
 extern int strcat_s(char* dst, size_t dstSizeInBytes, const char* src);
 extern int strncpy_s(char* dst, size_t dstSizeInBytes, const char* src, size_t maxCount);
 extern int sprintf_s(char* dst, size_t dstSizeInBytes, const char* format, ...);
-#endif
+#endif // _MSC_VER
 
 extern unsigned long long strtoull_s(const char* nptr, char** endPtr, int base);
 extern float strtof_s(const char* nptr, char** endPtr);
@@ -105,7 +107,7 @@
 
 #ifdef _MSC_VER
 #define stricmp _stricmp
-#endif
+#endif // _MSC_VER
 
 MOCKABLE_FUNCTION(, int, mallocAndStrcpy_s, char**, destination, const char*, source);
 MOCKABLE_FUNCTION(, int, unsignedIntToString, char*, destination, size_t, destinationSize, unsigned int, value);
@@ -122,26 +124,26 @@
 
 #ifdef _MSC_VER
 #define ISNAN _isnan
-#else
+#else // _MSC_VER
 #if defined __STDC_VERSION__
 #if ((__STDC_VERSION__  == 199901L) || (__STDC_VERSION__ == 201000L) || (__STDC_VERSION__ == 201112L))
 /*C99 compiler or C11*/
 #define ISNAN isnan
-#else
+#else //  ((__STDC_VERSION__  == 199901L) || (__STDC_VERSION__ == 201000L) || (__STDC_VERSION__ == 201112L))
 #error update this file to contain the latest C standard.
-#endif
-#else
+#endif // ((__STDC_VERSION__  == 199901L) || (__STDC_VERSION__ == 201000L) || (__STDC_VERSION__ == 201112L))
+#else // __STDC_VERSION__
 #ifdef __cplusplus
 /*C++ defines isnan... in C11*/
 extern "C++" {
 #define ISNAN std::isnan
 }
-#else
+#else // __cplusplus
 #error unknown (or C89) compiler, provide ISNAN with the same meaning as isnan in C99 standard  
-#endif
+#endif // __cplusplus
 
-#endif
-#endif
+#endif // __STDC_VERSION__
+#endif // _MSC_VER
 
 #ifdef _MSC_VER
 #define INT64_PRINTF "%I64d"
@@ -150,20 +152,20 @@
 #if ((__STDC_VERSION__  == 199901L) || (__STDC_VERSION__ == 201000L) || (__STDC_VERSION__ == 201112L))
 /*C99 compiler or C11*/
 #define INT64_PRINTF "%" PRId64 ""
-#else
+#else // ((__STDC_VERSION__  == 199901L) || (__STDC_VERSION__ == 201000L) || (__STDC_VERSION__ == 201112L))
 #error update this file to contain the latest C standard.
-#endif
-#else
+#endif // ((__STDC_VERSION__  == 199901L) || (__STDC_VERSION__ == 201000L) || (__STDC_VERSION__ == 201112L))
+#else // __STDC_VERSION__
 #ifdef __cplusplus 
 #define INT64_PRINTF "%" PRId64 ""
-#else
+#else // __cplusplus
 #error unknown (or C89) compiler, provide INT64_PRINTF with the same meaning as PRIdN in C99 standard
-#endif
-#endif
-#endif
+#endif // __cplusplus
+#endif // __STDC_VERSION__
+#endif // _MSC_VER
 
 #ifdef __cplusplus
 }
-#endif
+#endif // __cplusplus
 
 #endif /* CRT_ABSTRACTIONS_H */
--- a/azure_c_shared_utility/shared_util_options.h	Fri Jun 30 10:42:00 2017 -0700
+++ b/azure_c_shared_utility/shared_util_options.h	Fri Jul 14 16:38:40 2017 -0700
@@ -20,6 +20,8 @@
     static const char* OPTION_HTTP_PROXY = "proxy_data";
     static const char* OPTION_HTTP_TIMEOUT = "timeout";
 
+    static const char* OPTION_TRUSTED_CERT = "TrustedCerts";
+
     static const char* SU_OPTION_X509_CERT = "x509certificate";
     static const char* SU_OPTION_X509_PRIVATE_KEY = "x509privatekey";
 
--- a/azure_c_shared_utility/singlylinkedlist.h	Fri Jun 30 10:42:00 2017 -0700
+++ b/azure_c_shared_utility/singlylinkedlist.h	Fri Jul 14 16:38:40 2017 -0700
@@ -15,7 +15,29 @@
 
 typedef struct SINGLYLINKEDLIST_INSTANCE_TAG* SINGLYLINKEDLIST_HANDLE;
 typedef struct LIST_ITEM_INSTANCE_TAG* LIST_ITEM_HANDLE;
+
+/**
+* @brief						Function passed to singlylinkedlist_find, which returns whichever first list item that matches it.
+* @param list_item				Current list node being evaluated.
+* @param match_context			Context passed to singlylinkedlist_find.
+* @returns						True to indicate that the current list node is the one to be returned, or false to continue traversing the list.
+*/
 typedef bool (*LIST_MATCH_FUNCTION)(LIST_ITEM_HANDLE list_item, const void* match_context);
+/**
+* @brief						Function passed to singlylinkedlist_remove_if, which is used to define if an item of the list should be removed or not.
+* @param item					Value of the current list node being evaluated for removal.
+* @param match_context			Context passed to singlylinkedlist_remove_if.
+* @param continue_processing	Indicates if singlylinkedlist_remove_if shall continue iterating through the next nodes of the list or stop.
+* @returns						True to indicate that the current list node shall be removed, or false to not to.
+*/
+typedef bool (*LIST_CONDITION_FUNCTION)(const void* item, const void* match_context, bool* continue_processing);
+/**
+* @brief						Function passed to singlylinkedlist_foreach, which is called for the value of each node of the list.
+* @param item					Value of the current list node being processed.
+* @param action_context			Context passed to singlylinkedlist_foreach.
+* @param continue_processing	Indicates if singlylinkedlist_foreach shall continue iterating through the next nodes of the list or stop.
+*/
+typedef void (*LIST_ACTION_FUNCTION)(const void* item, const void* action_context, bool* continue_processing);
 
 MOCKABLE_FUNCTION(, SINGLYLINKEDLIST_HANDLE, singlylinkedlist_create);
 MOCKABLE_FUNCTION(, void, singlylinkedlist_destroy, SINGLYLINKEDLIST_HANDLE, list);
@@ -25,6 +47,8 @@
 MOCKABLE_FUNCTION(, LIST_ITEM_HANDLE, singlylinkedlist_get_next_item, LIST_ITEM_HANDLE, item_handle);
 MOCKABLE_FUNCTION(, LIST_ITEM_HANDLE, singlylinkedlist_find, SINGLYLINKEDLIST_HANDLE, list, LIST_MATCH_FUNCTION, match_function, const void*, match_context);
 MOCKABLE_FUNCTION(, const void*, singlylinkedlist_item_get_value, LIST_ITEM_HANDLE, item_handle);
+MOCKABLE_FUNCTION(, int, singlylinkedlist_remove_if, SINGLYLINKEDLIST_HANDLE, list, LIST_CONDITION_FUNCTION, condition_function, const void*, match_context);
+MOCKABLE_FUNCTION(, int, singlylinkedlist_foreach, SINGLYLINKEDLIST_HANDLE, list, LIST_ACTION_FUNCTION, action_function, const void*, action_context);
 
 #ifdef __cplusplus
 }
--- a/azure_c_shared_utility/strings.h	Fri Jun 30 10:42:00 2017 -0700
+++ b/azure_c_shared_utility/strings.h	Fri Jul 14 16:38:40 2017 -0700
@@ -33,6 +33,7 @@
 MOCKABLE_FUNCTION(, int, STRING_empty, STRING_HANDLE, handle);
 MOCKABLE_FUNCTION(, size_t, STRING_length, STRING_HANDLE, handle);
 MOCKABLE_FUNCTION(, int, STRING_compare, STRING_HANDLE, s1, STRING_HANDLE, s2);
+MOCKABLE_FUNCTION(, int, STRING_replace, STRING_HANDLE, handle, char, target, char, replace);
 
 extern STRING_HANDLE STRING_construct_sprintf(const char* format, ...);
 extern int STRING_sprintf(STRING_HANDLE s1, const char* format, ...);
--- a/buffer.c	Fri Jun 30 10:42:00 2017 -0700
+++ b/buffer.c	Fri Jul 14 16:38:40 2017 -0700
@@ -541,6 +541,29 @@
     return result;
 }
 
+int BUFFER_fill(BUFFER_HANDLE handle, unsigned char fill_char)
+{
+    int result;
+    if (handle == NULL)
+    {
+        /* Codes_SRS_BUFFER_07_002: [ If handle is NULL BUFFER_fill shall return a non-zero value. ] */
+        LogError("Invalid parameter specified, handle == NULL.");
+        result = __FAILURE__;
+    }
+    else
+    {
+        size_t index;
+        /* Codes_SRS_BUFFER_07_001: [ BUFFER_fill shall fill the supplied BUFFER_HANDLE with the supplied fill character. ] */
+        BUFFER* buffer_data = (BUFFER*)handle;
+        for (index = 0; index < buffer_data->size; index++)
+        {
+            buffer_data->buffer[index] = fill_char;
+        }
+        result = 0;
+    }
+    return result;
+}
+
 
 /* Codes_SRS_BUFFER_07_025: [BUFFER_u_char shall return a pointer to the underlying unsigned char*.] */
 unsigned char* BUFFER_u_char(BUFFER_HANDLE handle)
--- a/singlylinkedlist.c	Fri Jun 30 10:42:00 2017 -0700
+++ b/singlylinkedlist.c	Fri Jul 14 16:38:40 2017 -0700
@@ -5,6 +5,7 @@
 #include "azure_c_shared_utility/gballoc.h"
 #include "azure_c_shared_utility/singlylinkedlist.h"
 #include "azure_c_shared_utility/optimize_size.h"
+#include "azure_c_shared_utility/xlogging.h"
 
 typedef struct LIST_ITEM_INSTANCE_TAG
 {
@@ -59,6 +60,7 @@
     if ((list == NULL) ||
         (item == NULL))
     {
+		LogError("Invalid argument (list=%p, item=%p)", list, item);
         result = NULL;
     }
     else
@@ -105,6 +107,7 @@
 	if ((list == NULL) ||
         (item == NULL))
     {
+		LogError("Invalid argument (list=%p, item=%p)", list, item);
         result = __FAILURE__;
     }
     else
@@ -156,7 +159,8 @@
     if (list == NULL)
     {
         /* Codes_SRS_LIST_01_009: [If the list argument is NULL, singlylinkedlist_get_head_item shall return NULL.] */
-        result = NULL;
+		LogError("Invalid argument (list=NULL)");
+		result = NULL;
     }
     else
     {
@@ -176,6 +180,7 @@
 
     if (item_handle == NULL)
     {
+		LogError("Invalid argument (list is NULL)");
         /* Codes_SRS_LIST_01_019: [If item_handle is NULL then singlylinkedlist_get_next_item shall return NULL.] */
         result = NULL;
     }
@@ -194,6 +199,7 @@
 
     if (item_handle == NULL)
     {
+		LogError("Invalid argument (item_handle is NULL)");
         /* Codes_SRS_LIST_01_021: [If item_handle is NULL, singlylinkedlist_item_get_value shall return NULL.] */
         result = NULL;
     }
@@ -213,6 +219,7 @@
     if ((list == NULL) ||
         (match_function == NULL))
     {
+		LogError("Invalid argument (list=%p, match_function=%p)", list, match_function);
         /* Codes_SRS_LIST_01_012: [If the list or the match_function argument is NULL, singlylinkedlist_find shall return NULL.] */
         result = NULL;
     }
@@ -249,3 +256,103 @@
 
     return result;
 }
+
+int singlylinkedlist_remove_if(SINGLYLINKEDLIST_HANDLE list, LIST_CONDITION_FUNCTION condition_function, const void* match_context)
+{
+	int result;
+	/* Codes_SRS_LIST_09_001: [ If the list or the condition_function argument is NULL, singlylinkedlist_remove_if shall return non-zero value. ] */
+	if ((list == NULL) ||
+		(condition_function == NULL))
+	{
+		LogError("Invalid argument (list=%p, condition_function=%p)", list, condition_function);
+		result = __FAILURE__;
+	}
+	else
+	{
+		LIST_INSTANCE* list_instance = (LIST_INSTANCE*)list;
+		LIST_ITEM_INSTANCE* current_item = list_instance->head;
+		LIST_ITEM_INSTANCE* next_item = NULL;
+		LIST_ITEM_INSTANCE* previous_item = NULL;
+
+		/* Codes_SRS_LIST_09_002: [ singlylinkedlist_remove_if shall iterate through all items in a list and remove all that satisfies a certain condition function. ] */
+		while (current_item != NULL)
+		{
+			bool continue_processing = false;
+
+			next_item = (LIST_ITEM_INSTANCE*)current_item->next;
+
+			/* Codes_SRS_LIST_09_003: [ singlylinkedlist_remove_if shall determine whether an item satisfies the condition criteria by invoking the condition function for that item. ] */
+			/* Codes_SRS_LIST_09_004: [ If the condition function returns true, singlylinkedlist_find shall consider that item as to be removed. ] */
+			if (condition_function(current_item->item, match_context, &continue_processing) == true)
+			{
+				if (previous_item != NULL)
+				{
+					previous_item->next = next_item;
+				}
+				else
+				{
+					list_instance->head = next_item;
+				}
+
+				free(current_item);
+			}
+			/* Codes_SRS_LIST_09_005: [ If the condition function returns false, singlylinkedlist_find shall consider that item as not to be removed. ] */
+			else
+			{
+				previous_item = current_item;
+			}
+
+			/* Codes_SRS_LIST_09_006: [ If the condition function returns continue_processing as false, singlylinkedlist_remove_if shall stop iterating through the list and return. ] */
+			if (continue_processing == false)
+			{
+				break;
+			}
+
+			current_item = next_item;
+		}
+
+		/* Codes_SRS_LIST_09_007: [ If no errors occur, singlylinkedlist_remove_if shall return zero. ] */
+		result = 0;
+	}
+
+	return result;
+}
+
+int singlylinkedlist_foreach(SINGLYLINKEDLIST_HANDLE list, LIST_ACTION_FUNCTION action_function, const void* action_context)
+{
+	int result;
+
+	/* Codes_SRS_LIST_09_008: [ If the list or the action_function argument is NULL, singlylinkedlist_foreach shall return non-zero value. ] */
+	if ((list == NULL) ||
+		(action_function == NULL))
+	{
+		LogError("Invalid argument (list=%p, action_function=%p)", list, action_function);
+		result = __FAILURE__;
+	}
+	else
+	{
+		LIST_INSTANCE* list_instance = (LIST_INSTANCE*)list;
+		LIST_ITEM_INSTANCE* list_item = list_instance->head;
+
+		while (list_item != NULL)
+		{
+			bool continue_processing = false;
+
+			/* Codes_SRS_LIST_09_009: [ singlylinkedlist_foreach shall iterate through all items in a list and invoke action_function for each one of them. ] */
+			action_function(list_item->item, action_context, &continue_processing);
+
+			/* Codes_SRS_LIST_09_010: [ If the condition function returns continue_processing as false, singlylinkedlist_foreach shall stop iterating through the list and return. ] */
+			if (continue_processing == false)
+			{
+				break;
+			}
+
+			list_item = (LIST_ITEM_INSTANCE*)list_item->next;
+		}
+
+		/* Codes_SRS_LIST_09_011: [ If no errors occur, singlylinkedlist_foreach shall return zero. ] */
+		result = 0;
+	}
+
+	return result;
+}
\ No newline at end of file
--- a/strings.c	Fri Jun 30 10:42:00 2017 -0700
+++ b/strings.c	Fri Jul 14 16:38:40 2017 -0700
@@ -24,7 +24,7 @@
 typedef struct STRING_TAG
 {
     char* s;
-}STRING;
+} STRING;
 
 /*this function will allocate a new string with just '\0' in it*/
 /*return NULL if it fails*/
@@ -137,7 +137,7 @@
     if (format != NULL)
     {
         va_list arg_list;
-		int length;
+        int length;
         va_start(arg_list, format);
 
         /* Codes_SRS_STRING_07_041: [STRING_construct_sprintf shall determine the size of the resulting string and allocate the necessary memory.] */
@@ -534,7 +534,7 @@
     else
     {
         va_list arg_list;
-		int s2Length;
+        int s2Length;
         va_start(arg_list, format);
 
         s2Length = vsnprintf(buf, maxBufSize, format, arg_list);
@@ -553,7 +553,7 @@
         else
         {
             STRING* s1 = (STRING*)handle;
-			char* temp;
+            char* temp;
             size_t s1Length = strlen(s1->s);
             temp = (char*)realloc(s1->s, s1Length + s2Length + 1);
             if (temp != NULL)
@@ -807,3 +807,36 @@
     }
     return (STRING_HANDLE)result;
 }
+
+int STRING_replace(STRING_HANDLE handle, char target, char replace)
+{
+    int result;
+    if (handle == NULL)
+    {
+        /* Codes_SRS_STRING_07_046: [ If handle is NULL STRING_replace shall return a non-zero value. ] */
+        result = __FAILURE__;
+    }
+    else if (target == replace)
+    {
+        /* Codes_SRS_STRING_07_048: [ If target and replace are equal STRING_replace, shall do nothing shall return zero. ] */
+        result = 0;
+    }
+    else
+    {
+        size_t length;
+        size_t index;
+        /* Codes_SRS_STRING_07_047: [ STRING_replace shall replace all instances of target with replace. ] */
+        STRING* str_value = (STRING*)handle;
+        length = strlen(str_value->s);
+        for (index = 0; index < length; index++)
+        {
+            if (str_value->s[index] == target)
+            {
+                str_value->s[index] = replace;
+            }
+        }
+        /* Codes_SRS_STRING_07_049: [ On success STRING_replace shall return zero. ] */
+        result = 0;
+    }
+    return result;
+}
--- a/tlsio_wolfssl.c	Fri Jun 30 10:42:00 2017 -0700
+++ b/tlsio_wolfssl.c	Fri Jul 14 16:38:40 2017 -0700
@@ -166,7 +166,7 @@
                 result = NULL;
             }
             else if (
-                (tls_io_instance->x509privatekey != NULL) &&
+                (tls_io_instance->certificate != NULL) &&
                 (OptionHandler_AddOption(result, "TrustedCerts", tls_io_instance->certificate) != 0)
                 )
             {