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:
Mon May 22 10:35:55 2017 -0700
Parent:
26:b38c66e3e45e
Child:
28:8789208d2671
Commit message:
1.1.15

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/consolelogger.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/xlogging.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
consolelogger.c Show annotated file Show diff for this revision Revisions of this file
crt_abstractions.c Show annotated file Show diff for this revision Revisions of this file
httpapi_compact.c Show annotated file Show diff for this revision Revisions of this file
map.c Show annotated file Show diff for this revision Revisions of this file
xlogging.c Show annotated file Show diff for this revision Revisions of this file
--- a/azure_c_shared_utility/buffer_.h	Mon May 08 10:51:21 2017 -0700
+++ b/azure_c_shared_utility/buffer_.h	Mon May 22 10:35:55 2017 -0700
@@ -6,10 +6,12 @@
 
 #ifdef __cplusplus
 #include <cstddef>
+#include <cstdbool>
 extern "C"
 {
 #else
 #include <stddef.h>
+#include <stdbool.h>
 #endif
 
 #include "azure_c_shared_utility/umock_c_prod.h"
@@ -21,8 +23,10 @@
 MOCKABLE_FUNCTION(, void, BUFFER_delete, BUFFER_HANDLE, handle);
 MOCKABLE_FUNCTION(, int, BUFFER_pre_build, BUFFER_HANDLE, handle, size_t, size);
 MOCKABLE_FUNCTION(, int, BUFFER_build, BUFFER_HANDLE, handle, const unsigned char*, source, size_t, size);
+MOCKABLE_FUNCTION(, int, BUFFER_append_build, BUFFER_HANDLE, handle, const unsigned char*, source, size_t, size);
 MOCKABLE_FUNCTION(, int, BUFFER_unbuild, BUFFER_HANDLE, handle);
 MOCKABLE_FUNCTION(, int, BUFFER_enlarge, BUFFER_HANDLE, handle, size_t, enlargeSize);
+MOCKABLE_FUNCTION(, int, BUFFER_shrink, BUFFER_HANDLE, handle, size_t, decreaseSize, bool, fromEnd);
 MOCKABLE_FUNCTION(, int, BUFFER_content, BUFFER_HANDLE, handle, const unsigned char**, content);
 MOCKABLE_FUNCTION(, int, BUFFER_size, BUFFER_HANDLE, handle, size_t*, size);
 MOCKABLE_FUNCTION(, int, BUFFER_append, BUFFER_HANDLE, handle1, BUFFER_HANDLE, handle2);
--- a/azure_c_shared_utility/consolelogger.h	Mon May 08 10:51:21 2017 -0700
+++ b/azure_c_shared_utility/consolelogger.h	Mon May 22 10:35:55 2017 -0700
@@ -10,7 +10,11 @@
 
 #include "azure_c_shared_utility/xlogging.h"
 
-    extern void consolelogger_log(LOG_CATEGORY log_category, const char* file, const char* func, const int line, unsigned int options, const char* format, ...);
+    extern void consolelogger_log(LOG_CATEGORY log_category, const char* file, const char* func, int line, unsigned int options, const char* format, ...);
+
+#if (defined(_MSC_VER)) && (!(defined WINCE))
+    extern void consolelogger_log_with_GetLastError(const char* file, const char* func, int line, const char* format, ...);
+#endif
 
 #ifdef __cplusplus
 }
--- a/azure_c_shared_utility/shared_util_options.h	Mon May 08 10:51:21 2017 -0700
+++ b/azure_c_shared_utility/shared_util_options.h	Mon May 22 10:35:55 2017 -0700
@@ -32,6 +32,7 @@
     static const char* OPTION_CURL_FORBID_REUSE = "CURLOPT_FORBID_REUSE";
     static const char* OPTION_CURL_VERBOSE = "CURLOPT_VERBOSE";
 
+    static const char* OPTION_NET_INT_MAC_ADDRESS = "net_interface_mac_address";
 #ifdef __cplusplus
 }
 #endif
--- a/azure_c_shared_utility/xlogging.h	Mon May 08 10:51:21 2017 -0700
+++ b/azure_c_shared_utility/xlogging.h	Mon May 22 10:35:55 2017 -0700
@@ -30,7 +30,8 @@
 #define FUNC_NAME __func__
 #endif
 
-typedef void(*LOGGER_LOG)(LOG_CATEGORY log_category, const char* file, const char* func, const int line, unsigned int options, const char* format, ...);
+typedef void(*LOGGER_LOG)(LOG_CATEGORY log_category, const char* file, const char* func, int line, unsigned int options, const char* format, ...);
+typedef void(*LOGGER_LOG_GETLASTERROR)(const char* file, const char* func, int line, const char* format, ...);
 
 #define LOG_NONE 0x00
 #define LOG_LINE 0x01
@@ -59,7 +60,7 @@
         static const char flash_str[] ICACHE_RODATA_ATTR STORE_ATTR = FORMAT;  \
         printf(flash_str, ##__VA_ARGS__);   \
         printf("\n");\
-    } while(0)
+    } while((void)0,0)
     
 #define LogError LogInfo
 #define LOG(log_category, log_options, FORMAT, ...)  { \
@@ -105,19 +106,26 @@
 #endif
 
 #if defined _MSC_VER
-#define LogInfo(FORMAT, ...) do{LOG(AZ_LOG_INFO, LOG_LINE, FORMAT, __VA_ARGS__); }while(0)
+#define LogInfo(FORMAT, ...) do{LOG(AZ_LOG_INFO, LOG_LINE, FORMAT, __VA_ARGS__); }while((void)0,0)
 #else
-#define LogInfo(FORMAT, ...) do{LOG(AZ_LOG_INFO, LOG_LINE, FORMAT, ##__VA_ARGS__); }while(0)
+#define LogInfo(FORMAT, ...) do{LOG(AZ_LOG_INFO, LOG_LINE, FORMAT, ##__VA_ARGS__); }while((void)0,0)
 #endif
 
 #if defined _MSC_VER
-#define LogError(FORMAT, ...) do{ LOG(AZ_LOG_ERROR, LOG_LINE, FORMAT, __VA_ARGS__); }while(0)
+
+#if !defined(WINCE)
+extern void xlogging_set_log_function_GetLastError(LOGGER_LOG_GETLASTERROR log_function);
+extern LOGGER_LOG_GETLASTERROR xlogging_get_log_function_GetLastError(void);
+#define LogLastError(FORMAT, ...) do{ LOGGER_LOG_GETLASTERROR l = xlogging_get_log_function_GetLastError(); if(l!=NULL) l(__FILE__, FUNC_NAME, __LINE__, FORMAT, __VA_ARGS__); }while((void)0,0)
+#endif
+
+#define LogError(FORMAT, ...) do{ LOG(AZ_LOG_ERROR, LOG_LINE, FORMAT, __VA_ARGS__); }while((void)0,0)
 #define TEMP_BUFFER_SIZE 1024
 #define MESSAGE_BUFFER_SIZE 260
 #define LogErrorWinHTTPWithGetLastErrorAsString(FORMAT, ...) do { \
                 DWORD errorMessageID = GetLastError(); \
+                char messageBuffer[MESSAGE_BUFFER_SIZE]; \
                 LogError(FORMAT, __VA_ARGS__); \
-                CHAR messageBuffer[MESSAGE_BUFFER_SIZE]; \
                 if (errorMessageID == 0) \
                 {\
                     LogError("GetLastError() returned 0. Make sure you are calling this right after the code that failed. "); \
@@ -143,9 +151,9 @@
                         LogError("GetLastError: %s.", messageBuffer); \
                     }\
                 }\
-            } while(0)
+            } while((void)0,0)
 #else
-#define LogError(FORMAT, ...) do{ LOG(AZ_LOG_ERROR, LOG_LINE, FORMAT, ##__VA_ARGS__); }while(0)
+#define LogError(FORMAT, ...) do{ LOG(AZ_LOG_ERROR, LOG_LINE, FORMAT, ##__VA_ARGS__); }while((void)0,0)
 #endif
 
 #ifdef __cplusplus
--- a/buffer.c	Mon May 08 10:51:21 2017 -0700
+++ b/buffer.c	Mon May 22 10:35:55 2017 -0700
@@ -4,6 +4,7 @@
 #include <stdlib.h>
 #include <stddef.h>
 #include <string.h>
+#include <stdbool.h>
 #include "azure_c_shared_utility/gballoc.h"
 #include "azure_c_shared_utility/buffer_.h"
 #include "azure_c_shared_utility/optimize_size.h"
@@ -13,7 +14,7 @@
 {
     unsigned char* buffer;
     size_t size;
-}BUFFER;
+} BUFFER;
 
 /* Codes_SRS_BUFFER_07_001: [BUFFER_new shall allocate a BUFFER_HANDLE that will contain a NULL unsigned char*.] */
 BUFFER_HANDLE BUFFER_new(void)
@@ -40,6 +41,7 @@
     if (handleptr->buffer == NULL)
     {
         /*Codes_SRS_BUFFER_02_003: [If allocating memory fails, then BUFFER_create shall return NULL.]*/
+        LogError("Failure allocating data");
         result = __FAILURE__;
     }
     else
@@ -57,6 +59,7 @@
     /*Codes_SRS_BUFFER_02_001: [If source is NULL then BUFFER_create shall return NULL.]*/
     if (source == NULL)
     {
+        LogError("invalid parameter source: %p", source);
         result = NULL;
     }
     else
@@ -67,6 +70,7 @@
         {
             /*Codes_SRS_BUFFER_02_003: [If allocating memory fails, then BUFFER_create shall return NULL.] */
             /*fallthrough*/
+            LogError("Failure allocating BUFFER structure");
         }
         else
         {
@@ -140,6 +144,7 @@
             if (newBuffer == NULL)
             {
                 /* Codes_SRS_BUFFER_07_010: [BUFFER_build shall return nonzero if any error is encountered.] */
+                LogError("Failure reallocating buffer");
                 result = __FAILURE__;
             }
             else
@@ -157,6 +162,59 @@
     return result;
 }
 
+int BUFFER_append_build(BUFFER_HANDLE handle, const unsigned char* source, size_t size)
+{
+    int result;
+    if (handle == NULL || source == NULL || size == 0)
+    {
+        /* Codes_SRS_BUFFER_07_029: [ BUFFER_append_build shall return nonzero if handle or source are NULL or if size is 0. ] */
+        LogError("BUFFER_append_build failed invalid parameter handle: %p, source: %p, size: %uz", handle, source, size);
+        result = __FAILURE__;
+    }
+    else
+    {
+        if (handle->buffer == NULL)
+        {
+            /* Codes_SRS_BUFFER_07_030: [ if handle->buffer is NULL BUFFER_append_build shall allocate the a buffer of size bytes... ] */
+            if (BUFFER_safemalloc(handle, size) != 0)
+            {
+                /* Codes_SRS_BUFFER_07_035: [ If any error is encountered BUFFER_append_build shall return a non-null value. ] */
+                LogError("Failure with BUFFER_safemalloc");
+                result = __FAILURE__;
+            }
+            else
+            {
+                /* Codes_SRS_BUFFER_07_031: [ ... and copy the contents of source to handle->buffer. ] */
+                (void)memcpy(handle->buffer, source, size);
+                /* Codes_SRS_BUFFER_07_034: [ On success BUFFER_append_build shall return 0 ] */
+                result = 0;
+            }
+        }
+        else
+        {
+            /* Codes_SRS_BUFFER_07_032: [ if handle->buffer is not NULL BUFFER_append_build shall realloc the buffer to be the handle->size + size ] */
+            unsigned char* temp = (unsigned char*)realloc(handle->buffer, handle->size + size);
+            if (temp == NULL)
+            {
+                /* Codes_SRS_BUFFER_07_035: [ If any error is encountered BUFFER_append_build shall return a non-null value. ] */
+                LogError("Failure reallocating temporary buffer");
+                result = __FAILURE__;
+            }
+            else
+            {
+                /* Codes_SRS_BUFFER_07_033: [ ... and copy the contents of source to the end of the buffer. ] */
+                handle->buffer = temp;
+                // Append the BUFFER
+                (void)memcpy(&handle->buffer[handle->size], source, size);
+                handle->size += size;
+                /* Codes_SRS_BUFFER_07_034: [ On success BUFFER_append_build shall return 0 ] */
+                result = 0;
+            }
+        }
+    }
+    return result;
+}
+
 /*return 0 if the buffer was pre-build(that is, had its space allocated)*/
 /*else return different than zero*/
 /* Codes_SRS_BUFFER_07_005: [BUFFER_pre_build allocates size_t bytes of BUFFER_HANDLE and returns zero on success.] */
@@ -179,6 +237,7 @@
         if (b->buffer != NULL)
         {
             /* Codes_SRS_BUFFER_07_007: [BUFFER_pre_build shall return nonzero if the buffer has been previously allocated and is not NULL.] */
+            LogError("Failure buffer data is NULL");
             result = __FAILURE__;
         }
         else
@@ -186,6 +245,7 @@
             if ((b->buffer = (unsigned char*)malloc(size)) == NULL)
             {
                 /* Codes_SRS_BUFFER_07_013: [BUFFER_pre_build shall return nonzero if any error is encountered.] */
+                LogError("Failure allocating buffer");
                 result = __FAILURE__;
             }
             else
@@ -231,6 +291,7 @@
         BUFFER* b = (BUFFER*)handle;
         if (b->buffer != NULL)
         {
+            LogError("Failure buffer data is NULL");
             free(b->buffer);
             b->buffer = NULL;
             b->size = 0;
@@ -252,11 +313,13 @@
     if (handle == NULL)
     {
         /* Codes_SRS_BUFFER_07_017: [BUFFER_enlarge shall return a nonzero result if any parameters are NULL or zero.] */
+        LogError("Failure: handle is invalid.");
         result = __FAILURE__;
     }
     else if (enlargeSize == 0)
     {
         /* Codes_SRS_BUFFER_07_017: [BUFFER_enlarge shall return a nonzero result if any parameters are NULL or zero.] */
+        LogError("Failure: enlargeSize size is 0.");
         result = __FAILURE__;
     }
     else
@@ -266,6 +329,7 @@
         if (temp == NULL)
         {
             /* Codes_SRS_BUFFER_07_018: [BUFFER_enlarge shall return a nonzero result if any error is encountered.] */
+            LogError("Failure: allocating temp buffer.");
             result = __FAILURE__;
         }
         else
@@ -278,6 +342,74 @@
     return result;
 }
 
+int BUFFER_shrink(BUFFER_HANDLE handle, size_t decreaseSize, bool fromEnd)
+{
+    int result;
+    if (handle == NULL)
+    {
+        /* Codes_SRS_BUFFER_07_036: [ if handle is NULL, BUFFER_shrink shall return a non-null value ]*/
+        LogError("Failure: handle is invalid.");
+        result = __FAILURE__;
+    }
+    else if (decreaseSize == 0)
+    {
+        /* Codes_SRS_BUFFER_07_037: [ If decreaseSize is equal zero, BUFFER_shrink shall return a non-null value ] */
+        LogError("Failure: decrease size is 0.");
+        result = __FAILURE__;
+    }
+    else if (decreaseSize > handle->size)
+    {
+        /* Codes_SRS_BUFFER_07_038: [ If decreaseSize is less than the size of the buffer, BUFFER_shrink shall return a non-null value ] */
+        LogError("Failure: decrease size is less than buffer size.");
+        result = __FAILURE__;
+    }
+    else
+    {
+        /* Codes_SRS_BUFFER_07_039: [ BUFFER_shrink shall allocate a temporary buffer of existing buffer size minus decreaseSize. ] */
+        size_t alloc_size = handle->size - decreaseSize;
+        if (alloc_size == 0)
+        {
+            /* Codes_SRS_BUFFER_07_043: [ If the decreaseSize is equal the buffer size , BUFFER_shrink shall deallocate the buffer and set the size to zero. ] */
+            free(handle->buffer);
+            handle->buffer = NULL;
+            handle->size = 0;
+            result = 0;
+        }
+        else
+        {
+            unsigned char* tmp = malloc(alloc_size);
+            if (tmp == NULL)
+            {
+                /* Codes_SRS_BUFFER_07_042: [ If a failure is encountered, BUFFER_shrink shall return a non-null value ] */
+                LogError("Failure: allocating temp buffer.");
+                result = __FAILURE__;
+            }
+            else
+            {
+                if (fromEnd)
+                {
+                    /* Codes_SRS_BUFFER_07_040: [ if the fromEnd variable is true, BUFFER_shrink shall remove the end of the buffer of size decreaseSize. ] */
+                    memcpy(tmp, handle->buffer, alloc_size);
+                    free(handle->buffer);
+                    handle->buffer = tmp;
+                    handle->size = alloc_size;
+                    result = 0;
+                }
+                else
+                {
+                    /* Codes_SRS_BUFFER_07_041: [ if the fromEnd variable is false, BUFFER_shrink shall remove the beginning of the buffer of size decreaseSize. ] */
+                    memcpy(tmp, handle->buffer + decreaseSize, alloc_size);
+                    free(handle->buffer);
+                    handle->buffer = tmp;
+                    handle->size = alloc_size;
+                    result = 0;
+                }
+            }
+        }
+    }
+    return result;
+}
+
 /* Codes_SRS_BUFFER_07_021: [BUFFER_size shall place the size of the associated buffer in the size variable and return zero on success.] */
 int BUFFER_size(BUFFER_HANDLE handle, size_t* size)
 {
@@ -333,6 +465,7 @@
                 if (temp == NULL)
                 {
                     /* Codes_SRS_BUFFER_07_023: [BUFFER_append shall return a nonzero upon any error that is encountered.] */
+                    LogError("Failure: allocating temp buffer.");
                     result = __FAILURE__;
                 }
                 else
@@ -387,6 +520,7 @@
                 if (temp == NULL)
                 {
                     /* Codes_SRS_BUFFER_01_005: [ BUFFER_prepend shall return a non-zero upon value any error that is encountered. ]*/
+                    LogError("Failure: allocating temp buffer.");
                     result = __FAILURE__;
                 }
                 else
@@ -458,6 +592,7 @@
         {
             if (BUFFER_safemalloc(b, suppliedBuff->size) != 0)
             {
+                LogError("Failure: allocating temp buffer.");
                 result = NULL;
             }
             else
--- a/consolelogger.c	Mon May 08 10:51:21 2017 -0700
+++ b/consolelogger.c	Mon May 22 10:35:55 2017 -0700
@@ -7,15 +7,204 @@
 #include "azure_c_shared_utility/xlogging.h"
 #include "azure_c_shared_utility/consolelogger.h"
 
+#if (defined(_MSC_VER)) && (!(defined WINCE))
+#include "windows.h"
+
+/*returns a string as if printed by vprintf*/
+static char* vprintf_alloc(const char* format, va_list va)
+{
+    char* result;
+    int neededSize = vsnprintf(NULL, 0, format, va);
+    if (neededSize < 0)
+    {
+        result = NULL;
+    }
+    else
+    {
+        result = (char*)malloc(neededSize + 1);
+        if (result == NULL)
+        {
+            /*return as is*/
+        }
+        else
+        {
+            if (vsnprintf(result, neededSize + 1, format, va) != neededSize)
+            {
+                free(result);
+                result = NULL;
+            }
+        }
+    }
+    return result;
+}
+
+/*returns a string as if printed by printf*/
+static char* printf_alloc(const char* format, ...)
+{
+    char* result;
+    va_list va;
+    va_start(va, format);
+    result = vprintf_alloc(format, va);
+    va_end(va);
+    return result;
+}
+
+/*returns NULL if it fails*/
+static char* lastErrorToString(DWORD lastError)
+{
+    char* result;
+    if (lastError == 0)
+    {
+        result = printf_alloc(""); /*no error should appear*/
+        if (result == NULL)
+        {
+            (void)printf("failure in printf_alloc");
+        }
+        else
+        {
+            /*return as is*/
+        }
+    }
+    else
+    {
+        char temp[MESSAGE_BUFFER_SIZE];
+        if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, lastError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), temp, MESSAGE_BUFFER_SIZE, NULL) == 0)
+        {
+            result = printf_alloc("GetLastError()=0X%x", lastError);
+            if (result == NULL)
+            {
+                (void)printf("failure in printf_alloc\n");
+                /*return as is*/
+            }
+            else
+            {
+                /*return as is*/
+            }
+        }
+        else
+        {
+            /*eliminate the \r or \n from the string*/
+            /*one replace of each is enough*/
+            char* whereAreThey;
+            if ((whereAreThey = strchr(temp, '\r')) != NULL)
+            {
+                *whereAreThey = '\0';
+            }
+            if ((whereAreThey = strchr(temp, '\n')) != NULL)
+            {
+                *whereAreThey = '\0';
+            }
+
+            result = printf_alloc("GetLastError()==0X%x (%s)", lastError, temp);
+
+            if (result == NULL)
+            {
+                (void)printf("failure in printf_alloc\n");
+                /*return as is*/
+            }
+            else
+            {
+                /*return as is*/
+            }
+        }
+    }
+    return result;
+}
+/*this function will use 1x printf (in the happy case) .*/
+/*more than 1x printf / function call can mean intermingled LogErrors in a multithreaded env*/
+/*the function will also attempt to produce some human readable strings for GetLastError*/
+void consolelogger_log_with_GetLastError(const char* file, const char* func, int line, const char* format, ...)
+{
+	DWORD lastError;
+	char* lastErrorAsString;
+	int lastErrorAsString_should_be_freed;
+	time_t t;
+    int systemMessage_should_be_freed;
+	char* systemMessage;
+    int userMessage_should_be_freed;
+	char* userMessage;
+
+    va_list args;
+    va_start(args, format);
+
+    /*this is what this case will do:
+    1. snip the last error
+    2. create a string with what that last error means
+    3. printf the system message (__FILE__, __LINE__ etc) + the last error + whatever the user wanted
+    */
+    /*1. snip the last error*/
+    lastError = GetLastError();
+
+    /*2. create a string with what that last error means*/
+    lastErrorAsString = lastErrorToString(lastError);
+    if (lastErrorAsString == NULL)
+    {
+        (void)printf("failure in lastErrorToString");
+        lastErrorAsString = "";
+        lastErrorAsString_should_be_freed = 0;
+    }
+    else
+    {
+        lastErrorAsString_should_be_freed = 1;
+    }
+
+    t = time(NULL);
+    systemMessage = printf_alloc("Error: Time:%.24s File:%s Func:%s Line:%d %s", ctime(&t), file, func, line, lastErrorAsString);
+
+    if (systemMessage == NULL)
+    {
+        systemMessage = "";
+        (void)printf("Error: [FAILED] Time:%.24s File : %s Func : %s Line : %d %s", ctime(&t), file, func, line, lastErrorAsString);
+        systemMessage_should_be_freed = 0;
+    }
+    else
+    {
+        systemMessage_should_be_freed = 1;
+    }
+
+    userMessage = vprintf_alloc(format, args);
+    if (userMessage == NULL)
+    {
+        (void)printf("[FAILED] ");
+        (void)vprintf(format, args);
+        (void)printf("\n");
+        userMessage_should_be_freed = 0;
+    }
+    else
+    {
+        /*3. printf the system message(__FILE__, __LINE__ etc) + the last error + whatever the user wanted*/
+        (void)printf("%s %s\n", systemMessage, userMessage);
+        userMessage_should_be_freed = 1;
+    }
+
+    if (userMessage_should_be_freed == 1)
+    {
+        free(userMessage);
+    }
+
+    if (systemMessage_should_be_freed == 1)
+    {
+        free(systemMessage);
+    }
+
+    if (lastErrorAsString_should_be_freed == 1)
+    {
+        free(lastErrorAsString);
+    }
+    va_end(args);
+}
+#endif
+
 #if defined(__GNUC__)
 __attribute__ ((format (printf, 6, 7)))
 #endif
-void consolelogger_log(LOG_CATEGORY log_category, const char* file, const char* func, const int line, unsigned int options, const char* format, ...)
+void consolelogger_log(LOG_CATEGORY log_category, const char* file, const char* func, int line, unsigned int options, const char* format, ...)
 {
+    time_t t;
     va_list args;
     va_start(args, format);
 
-    time_t t = time(NULL); 
+    t = time(NULL); 
     
     switch (log_category)
     {
@@ -29,12 +218,13 @@
         break;
     }
 
-	(void)vprintf(format, args);
-	va_end(args);
+    (void)vprintf(format, args);
+    va_end(args);
 
     (void)log_category;
-	if (options & LOG_LINE)
-	{
-		(void)printf("\r\n");
-	}
+    if (options & LOG_LINE)
+    {
+        (void)printf("\r\n");
+    }
 }
+
--- a/crt_abstractions.c	Mon May 08 10:51:21 2017 -0700
+++ b/crt_abstractions.c	Mon May 22 10:35:55 2017 -0700
@@ -14,9 +14,21 @@
 #include "azure_c_shared_utility/optimize_size.h"
 #include "azure_c_shared_utility/crt_abstractions.h"
 
+// VS 2008 does not have INFINITY and all the nice goodies...
+#if defined (TIZENRT) || defined (WINCE)
+#define DEFINE_INFINITY 1
+#else
 
-#if defined (WINCE) || defined (TIZENRT)
-#pragma warning(disable:4756) // warning C4756: overflow in constant arithmetic
+#if defined _MSC_VER
+#if _MSC_VER <= 1500
+#define DEFINE_INFINITY 1
+#endif
+#endif
+#endif
+
+#if defined DEFINE_INFINITY
+
+#pragma warning(disable:4756 4056) // warning C4756: overflow in constant arithmetic
 
 // These defines are missing in math.h for WEC2013 SDK
 #ifndef _HUGE_ENUF
@@ -29,8 +41,6 @@
 #define NAN        ((float)(INFINITY * 0.0F))
 #endif
 
-
-
 #ifdef _MSC_VER
 #else
 
--- a/httpapi_compact.c	Mon May 08 10:51:21 2017 -0700
+++ b/httpapi_compact.c	Mon May 22 10:35:55 2017 -0700
@@ -16,6 +16,10 @@
 #include "azure_c_shared_utility/threadapi.h"
 #include "azure_c_shared_utility/shared_util_options.h"
 
+#ifdef _MSC_VER
+#define snprintf _snprintf
+#endif
+
 /*Codes_SRS_HTTPAPI_COMPACT_21_001: [ The httpapi_compact shall implement the methods defined by the `httpapi.h`. ]*/
 /*Codes_SRS_HTTPAPI_COMPACT_21_002: [ The httpapi_compact shall support the http requests. ]*/
 /*Codes_SRS_HTTPAPI_COMPACT_21_003: [ The httpapi_compact shall return error codes defined by HTTPAPI_RESULT. ]*/
@@ -731,10 +735,11 @@
             }
             else
             {
+				int countRetry;
                 /*Codes_SRS_HTTPAPI_COMPACT_21_033: [ If the whole process succeed, the HTTPAPI_ExecuteRequest shall retur HTTPAPI_OK. ]*/
                 result = HTTPAPI_OK;
                 /*Codes_SRS_HTTPAPI_COMPACT_21_077: [ The HTTPAPI_ExecuteRequest shall wait, at least, 10 seconds for the SSL open process. ]*/
-                int countRetry = MAX_OPEN_RETRY;
+                countRetry = MAX_OPEN_RETRY;
                 while ((http_instance->is_connected == 0) &&
                     (http_instance->is_io_error == 0))
                 {
@@ -835,9 +840,10 @@
         /*Codes_SRS_HTTPAPI_COMPACT_21_028: [ If the HTTPAPI_ExecuteRequest cannot send the request header, it shall return HTTPAPI_HTTP_HEADERS_FAILED. ]*/
     else if ((result = conn_send_all(http_instance, (const unsigned char*)buf, strlen(buf))) == HTTPAPI_OK)
     {
+		size_t i;
         //Send default headers
         /*Codes_SRS_HTTPAPI_COMPACT_21_033: [ If the whole process succeed, the HTTPAPI_ExecuteRequest shall retur HTTPAPI_OK. ]*/
-        for (size_t i = 0; ((i < headersCount) && (result == HTTPAPI_OK)); i++)
+        for (i = 0; ((i < headersCount) && (result == HTTPAPI_OK)); i++)
         {
             char* header;
             if (HTTPHeaders_GetHeader(httpHeadersHandle, i, &header) != HTTP_HEADERS_OK)
@@ -1254,12 +1260,14 @@
     }
     else if (strcmp("TrustedCerts", optionName) == 0)
     {
+		int len;
+
         if (http_instance->certificate)
         {
             free(http_instance->certificate);
         }
 
-        int len = (int)strlen((char*)value);
+        len = (int)strlen((char*)value);
         http_instance->certificate = (char*)malloc((len + 1) * sizeof(char));
         if (http_instance->certificate == NULL)
         {
@@ -1276,12 +1284,13 @@
     }
     else if (strcmp(SU_OPTION_X509_CERT, optionName) == 0)
     {
+		int len;
         if (http_instance->x509ClientCertificate)
         {
             free(http_instance->x509ClientCertificate);
         }
 
-        int len = (int)strlen((char*)value);
+        len = (int)strlen((char*)value);
         http_instance->x509ClientCertificate = (char*)malloc((len + 1) * sizeof(char));
         if (http_instance->x509ClientCertificate == NULL)
         {
@@ -1298,12 +1307,13 @@
     }
     else if (strcmp(SU_OPTION_X509_PRIVATE_KEY, optionName) == 0)
     {
+		int len;
         if (http_instance->x509ClientPrivateKey)
         {
             free(http_instance->x509ClientPrivateKey);
         }
 
-        int len = (int)strlen((char*)value);
+        len = (int)strlen((char*)value);
         http_instance->x509ClientPrivateKey = (char*)malloc((len + 1) * sizeof(char));
         if (http_instance->x509ClientPrivateKey == NULL)
         {
--- a/map.c	Mon May 08 10:51:21 2017 -0700
+++ b/map.c	Mon May 22 10:35:55 2017 -0700
@@ -137,9 +137,9 @@
                 }
                 else if ((result->values = Map_CloneVector((const char* const*)handleData->values, handleData->count)) == NULL)
                 {
+                    size_t i;
                     /*Codes_SRS_MAP_02_047: [If during cloning, any operation fails, then Map_Clone shall return NULL.] */
                     LogError("unable to clone values");
-                    size_t i;
                     for (i = 0; i < result->count; i++)
                     {
                         free(result->keys[i]); 
--- a/xlogging.c	Mon May 08 10:51:21 2017 -0700
+++ b/xlogging.c	Mon May 22 10:35:55 2017 -0700
@@ -10,7 +10,7 @@
 #ifdef WINCE
 #include <stdarg.h>
 
-void consolelogger_log(LOG_CATEGORY log_category, const char* file, const char* func, const int line, unsigned int options, const char* format, ...)
+void consolelogger_log(LOG_CATEGORY log_category, const char* file, const char* func, int line, unsigned int options, const char* format, ...)
 {
 	va_list args;
 	va_start(args, format);
@@ -42,7 +42,6 @@
 
 LOGGER_LOG global_log_function = consolelogger_log;
 
-
 void xlogging_set_log_function(LOGGER_LOG log_function)
 {
     global_log_function = log_function;
@@ -53,6 +52,21 @@
     return global_log_function;
 }
 
+#if (defined(_MSC_VER)) && (!(defined WINCE))
+
+LOGGER_LOG_GETLASTERROR global_log_function_GetLastError = consolelogger_log_with_GetLastError;
+
+void xlogging_set_log_function_GetLastError(LOGGER_LOG_GETLASTERROR log_function_GetLastError)
+{
+    global_log_function_GetLastError = log_function_GetLastError;
+}
+
+LOGGER_LOG_GETLASTERROR xlogging_get_log_function_GetLastError(void)
+{
+    return global_log_function_GetLastError;
+}
+#endif
+
 /* Print up to 16 bytes per line. */
 #define LINE_SIZE 16