Azure IoT common library

Dependents:   STM32F746_iothub_client_sample_mqtt f767zi_mqtt iothub_client_sample_amqp iothub_client_sample_http ... more

Committer:
AzureIoTClient
Date:
Fri Jul 29 16:01:07 2016 -0700
Revision:
7:1af47e3a19b6
Parent:
6:c55b013dfc2a
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 #define __STDC_WANT_LIB_EXT1__ 1
Azure.IoT Build 0:fa2de1b79154 5
Azure.IoT Build 0:fa2de1b79154 6 #include <stdlib.h>
Azure.IoT Build 0:fa2de1b79154 7 #ifdef _CRTDBG_MAP_ALLOC
Azure.IoT Build 0:fa2de1b79154 8 #include <crtdbg.h>
Azure.IoT Build 0:fa2de1b79154 9 #endif
Azure.IoT Build 0:fa2de1b79154 10 #include "azure_c_shared_utility/gballoc.h"
Azure.IoT Build 0:fa2de1b79154 11
Azure.IoT Build 0:fa2de1b79154 12 #include "azure_c_shared_utility/crt_abstractions.h"
Azure.IoT Build 0:fa2de1b79154 13 #include "errno.h"
Azure.IoT Build 0:fa2de1b79154 14 #include <stddef.h>
Azure.IoT Build 0:fa2de1b79154 15 #include <limits.h>
AzureIoTClient 7:1af47e3a19b6 16 #include <float.h>
AzureIoTClient 7:1af47e3a19b6 17 #include <math.h>
AzureIoTClient 7:1af47e3a19b6 18
AzureIoTClient 7:1af47e3a19b6 19
AzureIoTClient 7:1af47e3a19b6 20 #ifdef WINCE
AzureIoTClient 7:1af47e3a19b6 21 #pragma warning(disable:4756) // warning C4756: overflow in constant arithmetic
AzureIoTClient 7:1af47e3a19b6 22
AzureIoTClient 7:1af47e3a19b6 23 // These defines are missing in math.h for WEC2013 SDK
AzureIoTClient 7:1af47e3a19b6 24 #ifndef _HUGE_ENUF
AzureIoTClient 7:1af47e3a19b6 25 #define _HUGE_ENUF 1e+300 // _HUGE_ENUF*_HUGE_ENUF must overflow
AzureIoTClient 7:1af47e3a19b6 26 #endif
AzureIoTClient 7:1af47e3a19b6 27
AzureIoTClient 7:1af47e3a19b6 28 #define INFINITY ((float)(_HUGE_ENUF * _HUGE_ENUF))
AzureIoTClient 7:1af47e3a19b6 29 #define HUGE_VALF ((float)INFINITY)
AzureIoTClient 7:1af47e3a19b6 30 #define HUGE_VALL ((long double)INFINITY)
AzureIoTClient 7:1af47e3a19b6 31 #define NAN ((float)(INFINITY * 0.0F))
AzureIoTClient 7:1af47e3a19b6 32 #endif
AzureIoTClient 7:1af47e3a19b6 33
AzureIoTClient 7:1af47e3a19b6 34
Azure.IoT Build 0:fa2de1b79154 35
Azure.IoT Build 0:fa2de1b79154 36 #ifdef _MSC_VER
Azure.IoT Build 0:fa2de1b79154 37 #else
Azure.IoT Build 0:fa2de1b79154 38
Azure.IoT Build 0:fa2de1b79154 39 #include <stdarg.h>
Azure.IoT Build 0:fa2de1b79154 40
Azure.IoT Build 0:fa2de1b79154 41 /*Codes_SRS_CRT_ABSTRACTIONS_99_008: [strcat_s shall append the src to dst and terminates the resulting string with a null character.]*/
Azure.IoT Build 0:fa2de1b79154 42 int strcat_s(char* dst, size_t dstSizeInBytes, const char* src)
Azure.IoT Build 0:fa2de1b79154 43 {
Azure.IoT Build 0:fa2de1b79154 44 int result;
Azure.IoT Build 0:fa2de1b79154 45 /*Codes_SRS_CRT_ABSTRACTIONS_99_004: [If dst is NULL or unterminated, the error code returned shall be EINVAL & dst shall not be modified.]*/
Azure.IoT Build 0:fa2de1b79154 46 if (dst == NULL)
Azure.IoT Build 0:fa2de1b79154 47 {
Azure.IoT Build 0:fa2de1b79154 48 result = EINVAL;
Azure.IoT Build 0:fa2de1b79154 49 }
Azure.IoT Build 0:fa2de1b79154 50 /*Codes_SRS_CRT_ABSTRACTIONS_99_005: [If src is NULL, the error code returned shall be EINVAL and dst[0] shall be set to 0.]*/
Azure.IoT Build 0:fa2de1b79154 51 else if (src == NULL)
Azure.IoT Build 0:fa2de1b79154 52 {
Azure.IoT Build 0:fa2de1b79154 53 dst[0] = '\0';
Azure.IoT Build 0:fa2de1b79154 54 result = EINVAL;
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 /*Codes_SRS_CRT_ABSTRACTIONS_99_006: [If the dstSizeInBytes is 0 or smaller than the required size for dst & src, the error code returned shall be ERANGE & dst[0] set to 0.]*/
Azure.IoT Build 0:fa2de1b79154 59 if (dstSizeInBytes == 0)
Azure.IoT Build 0:fa2de1b79154 60 {
Azure.IoT Build 0:fa2de1b79154 61 result = ERANGE;
Azure.IoT Build 0:fa2de1b79154 62 dst[0] = '\0';
Azure.IoT Build 0:fa2de1b79154 63 }
Azure.IoT Build 0:fa2de1b79154 64 else
Azure.IoT Build 0:fa2de1b79154 65 {
Azure.IoT Build 0:fa2de1b79154 66 size_t dstStrLen = 0;
Azure.IoT Build 0:fa2de1b79154 67 #ifdef __STDC_LIB_EXT1__
Azure.IoT Build 0:fa2de1b79154 68 dstStrLen = strnlen_s(dst, dstSizeInBytes);
Azure.IoT Build 0:fa2de1b79154 69 #else
Azure.IoT Build 0:fa2de1b79154 70 size_t i;
Azure.IoT Build 0:fa2de1b79154 71 for(i=0; (i < dstSizeInBytes) && (dst[i]!= '\0'); i++)
Azure.IoT Build 0:fa2de1b79154 72 {
Azure.IoT Build 0:fa2de1b79154 73 }
Azure.IoT Build 0:fa2de1b79154 74 dstStrLen = i;
Azure.IoT Build 0:fa2de1b79154 75 #endif
Azure.IoT Build 0:fa2de1b79154 76 /*Codes_SRS_CRT_ABSTRACTIONS_99_004: [If dst is NULL or unterminated, the error code returned shall be EINVAL & dst shall not be modified.]*/
Azure.IoT Build 0:fa2de1b79154 77 if (dstSizeInBytes == dstStrLen) /* this means the dst string is not terminated*/
Azure.IoT Build 0:fa2de1b79154 78 {
Azure.IoT Build 0:fa2de1b79154 79 result = EINVAL;
Azure.IoT Build 0:fa2de1b79154 80 }
Azure.IoT Build 0:fa2de1b79154 81 else
Azure.IoT Build 0:fa2de1b79154 82 {
Azure.IoT Build 0:fa2de1b79154 83 /*Codes_SRS_CRT_ABSTRACTIONS_99_009: [The initial character of src shall overwrite the terminating null character of dst.]*/
Azure.IoT Build 0:fa2de1b79154 84 (void)strncpy(&dst[dstStrLen], src, dstSizeInBytes - dstStrLen);
Azure.IoT Build 0:fa2de1b79154 85 /*Codes_SRS_CRT_ABSTRACTIONS_99_006: [If the dstSizeInBytes is 0 or smaller than the required size for dst & src, the error code returned shall be ERANGE & dst[0] set to 0.]*/
Azure.IoT Build 0:fa2de1b79154 86 if (dst[dstSizeInBytes-1] != '\0')
Azure.IoT Build 0:fa2de1b79154 87 {
Azure.IoT Build 0:fa2de1b79154 88 dst[0] = '\0';
Azure.IoT Build 0:fa2de1b79154 89 result = ERANGE;
Azure.IoT Build 0:fa2de1b79154 90 }
Azure.IoT Build 0:fa2de1b79154 91 else
Azure.IoT Build 0:fa2de1b79154 92 {
Azure.IoT Build 0:fa2de1b79154 93 /*Codes_SRS_CRT_ABSTRACTIONS_99_003: [strcat_s shall return Zero upon success.]*/
Azure.IoT Build 0:fa2de1b79154 94 result = 0;
Azure.IoT Build 0:fa2de1b79154 95 }
Azure.IoT Build 0:fa2de1b79154 96 }
Azure.IoT Build 0:fa2de1b79154 97 }
Azure.IoT Build 0:fa2de1b79154 98 }
Azure.IoT Build 0:fa2de1b79154 99
Azure.IoT Build 0:fa2de1b79154 100 return result;
Azure.IoT Build 0:fa2de1b79154 101 }
Azure.IoT Build 0:fa2de1b79154 102
Azure.IoT Build 0:fa2de1b79154 103 /*Codes_SRS_CRT_ABSTRACTIONS_99_025: [strncpy_s shall copy the first N characters of src to dst, where N is the lesser of MaxCount and the length of src.]*/
Azure.IoT Build 0:fa2de1b79154 104 int strncpy_s(char* dst, size_t dstSizeInBytes, const char* src, size_t maxCount)
Azure.IoT Build 0:fa2de1b79154 105 {
Azure.IoT Build 0:fa2de1b79154 106 int result;
Azure.IoT Build 0:fa2de1b79154 107 int truncationFlag = 0;
Azure.IoT Build 0:fa2de1b79154 108 /*Codes_SRS_CRT_ABSTRACTIONS_99_020: [If dst is NULL, the error code returned shall be EINVAL and dst shall not be modified.]*/
Azure.IoT Build 0:fa2de1b79154 109 if (dst == NULL)
Azure.IoT Build 0:fa2de1b79154 110 {
Azure.IoT Build 0:fa2de1b79154 111 result = EINVAL;
Azure.IoT Build 0:fa2de1b79154 112 }
Azure.IoT Build 0:fa2de1b79154 113 /*Codes_SRS_CRT_ABSTRACTIONS_99_021: [If src is NULL, the error code returned shall be EINVAL and dst[0] shall be set to 0.]*/
Azure.IoT Build 0:fa2de1b79154 114 else if (src == NULL)
Azure.IoT Build 0:fa2de1b79154 115 {
Azure.IoT Build 0:fa2de1b79154 116 dst[0] = '\0';
Azure.IoT Build 0:fa2de1b79154 117 result = EINVAL;
Azure.IoT Build 0:fa2de1b79154 118 }
Azure.IoT Build 0:fa2de1b79154 119 /*Codes_SRS_CRT_ABSTRACTIONS_99_022: [If the dstSizeInBytes is 0, the error code returned shall be EINVAL and dst shall not be modified.]*/
Azure.IoT Build 0:fa2de1b79154 120 else if (dstSizeInBytes == 0)
Azure.IoT Build 0:fa2de1b79154 121 {
Azure.IoT Build 0:fa2de1b79154 122 result = EINVAL;
Azure.IoT Build 0:fa2de1b79154 123 }
Azure.IoT Build 0:fa2de1b79154 124 else
Azure.IoT Build 0:fa2de1b79154 125 {
Azure.IoT Build 0:fa2de1b79154 126 size_t srcLength = strlen(src);
Azure.IoT Build 0:fa2de1b79154 127 if (maxCount != _TRUNCATE)
Azure.IoT Build 0:fa2de1b79154 128 {
Azure.IoT Build 0:fa2de1b79154 129 /*Codes_SRS_CRT_ABSTRACTIONS_99_041: [If those N characters will fit within dst (whose size is given as dstSizeInBytes) and still leave room for a null terminator, then those characters shall be copied and a terminating null is appended; otherwise, strDest[0] is set to the null character and ERANGE error code returned.]*/
Azure.IoT Build 0:fa2de1b79154 130 if (srcLength > maxCount)
Azure.IoT Build 0:fa2de1b79154 131 {
Azure.IoT Build 0:fa2de1b79154 132 srcLength = maxCount;
Azure.IoT Build 0:fa2de1b79154 133 }
Azure.IoT Build 0:fa2de1b79154 134
Azure.IoT Build 0:fa2de1b79154 135 /*Codes_SRS_CRT_ABSTRACTIONS_99_023: [If dst is not NULL & dstSizeInBytes is smaller than the required size for the src string, the error code returned shall be ERANGE and dst[0] shall be set to 0.]*/
Azure.IoT Build 0:fa2de1b79154 136 if (srcLength + 1 > dstSizeInBytes)
Azure.IoT Build 0:fa2de1b79154 137 {
Azure.IoT Build 0:fa2de1b79154 138 dst[0] = '\0';
Azure.IoT Build 0:fa2de1b79154 139 result = ERANGE;
Azure.IoT Build 0:fa2de1b79154 140 }
Azure.IoT Build 0:fa2de1b79154 141 else
Azure.IoT Build 0:fa2de1b79154 142 {
Azure.IoT Build 0:fa2de1b79154 143 (void)strncpy(dst, src, srcLength);
Azure.IoT Build 0:fa2de1b79154 144 dst[srcLength] = '\0';
Azure.IoT Build 0:fa2de1b79154 145 /*Codes_SRS_CRT_ABSTRACTIONS_99_018: [strncpy_s shall return Zero upon success]*/
Azure.IoT Build 0:fa2de1b79154 146 result = 0;
Azure.IoT Build 0:fa2de1b79154 147 }
Azure.IoT Build 0:fa2de1b79154 148 }
Azure.IoT Build 0:fa2de1b79154 149 /*Codes_SRS_CRT_ABSTRACTIONS_99_026: [If MaxCount is _TRUNCATE (defined as -1), then as much of src as will fit into dst shall be copied while still leaving room for the terminating null to be appended.]*/
Azure.IoT Build 0:fa2de1b79154 150 else
Azure.IoT Build 0:fa2de1b79154 151 {
Azure.IoT Build 0:fa2de1b79154 152 if (srcLength + 1 > dstSizeInBytes )
Azure.IoT Build 0:fa2de1b79154 153 {
Azure.IoT Build 0:fa2de1b79154 154 srcLength = dstSizeInBytes - 1;
Azure.IoT Build 0:fa2de1b79154 155 truncationFlag = 1;
Azure.IoT Build 0:fa2de1b79154 156 }
Azure.IoT Build 0:fa2de1b79154 157 (void)strncpy(dst, src, srcLength);
Azure.IoT Build 0:fa2de1b79154 158 dst[srcLength] = '\0';
Azure.IoT Build 0:fa2de1b79154 159 result = 0;
Azure.IoT Build 0:fa2de1b79154 160 }
Azure.IoT Build 0:fa2de1b79154 161 }
Azure.IoT Build 0:fa2de1b79154 162
Azure.IoT Build 0:fa2de1b79154 163 /*Codes_SRS_CRT_ABSTRACTIONS_99_019: [If truncation occurred as a result of the copy, the error code returned shall be STRUNCATE.]*/
Azure.IoT Build 0:fa2de1b79154 164 if (truncationFlag == 1)
Azure.IoT Build 0:fa2de1b79154 165 {
Azure.IoT Build 0:fa2de1b79154 166 result = STRUNCATE;
Azure.IoT Build 0:fa2de1b79154 167 }
Azure.IoT Build 0:fa2de1b79154 168
Azure.IoT Build 0:fa2de1b79154 169 return result;
Azure.IoT Build 0:fa2de1b79154 170 }
Azure.IoT Build 0:fa2de1b79154 171
Azure.IoT Build 0:fa2de1b79154 172 /* Codes_SRS_CRT_ABSTRACTIONS_99_016: [strcpy_s shall copy the contents in the address of src, including the terminating null character, to the location that's specified by dst.]*/
Azure.IoT Build 0:fa2de1b79154 173 int strcpy_s(char* dst, size_t dstSizeInBytes, const char* src)
Azure.IoT Build 0:fa2de1b79154 174 {
Azure.IoT Build 0:fa2de1b79154 175 int result;
Azure.IoT Build 0:fa2de1b79154 176
Azure.IoT Build 0:fa2de1b79154 177 /* Codes_SRS_CRT_ABSTRACTIONS_99_012: [If dst is NULL, the error code returned shall be EINVAL & dst shall not be modified.]*/
Azure.IoT Build 0:fa2de1b79154 178 if (dst == NULL)
Azure.IoT Build 0:fa2de1b79154 179 {
Azure.IoT Build 0:fa2de1b79154 180 result = EINVAL;
Azure.IoT Build 0:fa2de1b79154 181 }
Azure.IoT Build 0:fa2de1b79154 182 /* Codes_SRS_CRT_ABSTRACTIONS_99_013: [If src is NULL, the error code returned shall be EINVAL and dst[0] shall be set to 0.]*/
Azure.IoT Build 0:fa2de1b79154 183 else if (src == NULL)
Azure.IoT Build 0:fa2de1b79154 184 {
Azure.IoT Build 0:fa2de1b79154 185 dst[0] = '\0';
Azure.IoT Build 0:fa2de1b79154 186 result = EINVAL;
Azure.IoT Build 0:fa2de1b79154 187 }
Azure.IoT Build 0:fa2de1b79154 188 /* Codes_SRS_CRT_ABSTRACTIONS_99_014: [If the dstSizeInBytes is 0 or smaller than the required size for the src string, the error code returned shall be ERANGE & dst[0] set to 0.]*/
Azure.IoT Build 0:fa2de1b79154 189 else if (dstSizeInBytes == 0)
Azure.IoT Build 0:fa2de1b79154 190 {
Azure.IoT Build 0:fa2de1b79154 191 dst[0] = '\0';
Azure.IoT Build 0:fa2de1b79154 192 result = ERANGE;
Azure.IoT Build 0:fa2de1b79154 193 }
Azure.IoT Build 0:fa2de1b79154 194 else
Azure.IoT Build 0:fa2de1b79154 195 {
Azure.IoT Build 0:fa2de1b79154 196 size_t neededBuffer = strlen(src);
Azure.IoT Build 0:fa2de1b79154 197 /* Codes_SRS_CRT_ABSTRACTIONS_99_014: [If the dstSizeInBytes is 0 or smaller than the required size for the src string, the error code returned shall be ERANGE & dst[0] set to 0.]*/
Azure.IoT Build 0:fa2de1b79154 198 if (neededBuffer + 1 > dstSizeInBytes)
Azure.IoT Build 0:fa2de1b79154 199 {
Azure.IoT Build 0:fa2de1b79154 200 dst[0] = '\0';
Azure.IoT Build 0:fa2de1b79154 201 result = ERANGE;
Azure.IoT Build 0:fa2de1b79154 202 }
Azure.IoT Build 0:fa2de1b79154 203 else
Azure.IoT Build 0:fa2de1b79154 204 {
Azure.IoT Build 0:fa2de1b79154 205 memcpy(dst, src, neededBuffer + 1);
Azure.IoT Build 0:fa2de1b79154 206 /*Codes_SRS_CRT_ABSTRACTIONS_99_011: [strcpy_s shall return Zero upon success]*/
Azure.IoT Build 0:fa2de1b79154 207 result = 0;
Azure.IoT Build 0:fa2de1b79154 208 }
Azure.IoT Build 0:fa2de1b79154 209 }
Azure.IoT Build 0:fa2de1b79154 210
Azure.IoT Build 0:fa2de1b79154 211 return result;
Azure.IoT Build 0:fa2de1b79154 212 }
Azure.IoT Build 0:fa2de1b79154 213
Azure.IoT Build 0:fa2de1b79154 214 /*Codes_SRS_CRT_ABSTRACTIONS_99_029: [The sprintf_s function shall format and store series of characters and values in dst. Each argument (if any) is converted and output according to the corresponding Format Specification in the format variable.]*/
Azure.IoT Build 0:fa2de1b79154 215 /*Codes_SRS_CRT_ABSTRACTIONS_99_031: [A null character is appended after the last character written.]*/
Azure.IoT Build 0:fa2de1b79154 216 int sprintf_s(char* dst, size_t dstSizeInBytes, const char* format, ...)
Azure.IoT Build 0:fa2de1b79154 217 {
Azure.IoT Build 0:fa2de1b79154 218 int result;
Azure.IoT Build 0:fa2de1b79154 219 /*Codes_SRS_CRT_ABSTRACTIONS_99_028: [If dst or format is a null pointer, sprintf_s shall return -1 and set errno to EINVAL]*/
Azure.IoT Build 0:fa2de1b79154 220 if ((dst == NULL) ||
Azure.IoT Build 0:fa2de1b79154 221 (format == NULL))
Azure.IoT Build 0:fa2de1b79154 222 {
Azure.IoT Build 0:fa2de1b79154 223 errno = EINVAL;
Azure.IoT Build 0:fa2de1b79154 224 result = -1;
Azure.IoT Build 0:fa2de1b79154 225 }
Azure.IoT Build 0:fa2de1b79154 226 else
Azure.IoT Build 0:fa2de1b79154 227 {
Azure.IoT Build 0:fa2de1b79154 228 /*Codes_SRS_CRT_ABSTRACTIONS_99_033: [sprintf_s shall check the format string for valid formatting characters. If the check fails, the function returns -1.]*/
Azure.IoT Build 0:fa2de1b79154 229
Azure.IoT Build 0:fa2de1b79154 230 #if defined _MSC_VER
Azure.IoT Build 0:fa2de1b79154 231 #error crt_abstractions is not provided for Microsoft Compilers
Azure.IoT Build 0:fa2de1b79154 232 #else
Azure.IoT Build 0:fa2de1b79154 233 /*not Microsoft compiler... */
Azure.IoT Build 0:fa2de1b79154 234 #if defined (__STDC_VERSION__) || (__cplusplus)
Azure.IoT Build 0:fa2de1b79154 235 #if ( \
Azure.IoT Build 0:fa2de1b79154 236 ((__STDC_VERSION__ == 199901L) || (__STDC_VERSION__ == 201000L) || (__STDC_VERSION__ == 201112L)) || \
Azure.IoT Build 0:fa2de1b79154 237 (defined __cplusplus) \
Azure.IoT Build 0:fa2de1b79154 238 )
Azure.IoT Build 0:fa2de1b79154 239 /*C99 compiler*/
Azure.IoT Build 0:fa2de1b79154 240 va_list args;
Azure.IoT Build 0:fa2de1b79154 241 va_start(args, format);
Azure.IoT Build 0:fa2de1b79154 242 /*Codes_SRS_CRT_ABSTRACTIONS_99_027: [sprintf_s shall return the number of characters stored in dst upon success. This number shall not include the terminating null character.]*/
Azure.IoT Build 0:fa2de1b79154 243 result = vsnprintf(dst, dstSizeInBytes, format, args);
Azure.IoT Build 0:fa2de1b79154 244 va_end(args);
Azure.IoT Build 0:fa2de1b79154 245
Azure.IoT Build 0:fa2de1b79154 246 /*C99: Thus, the null-terminated output has been completely written if and only if the returned value is nonnegative and less than n*/
Azure.IoT Build 0:fa2de1b79154 247 if (result < 0)
Azure.IoT Build 0:fa2de1b79154 248 {
Azure.IoT Build 0:fa2de1b79154 249 result = -1;
Azure.IoT Build 0:fa2de1b79154 250 }
Azure.IoT Build 0:fa2de1b79154 251 else if ((size_t)result >= dstSizeInBytes)
Azure.IoT Build 0:fa2de1b79154 252 {
Azure.IoT Build 0:fa2de1b79154 253 /*Codes_SRS_CRT_ABSTRACTIONS_99_034: [If the dst buffer is too small for the text being printed, then dst is set to an empty string and the function shall return -1.]*/
Azure.IoT Build 0:fa2de1b79154 254 dst[0] = '\0';
Azure.IoT Build 0:fa2de1b79154 255 result = -1;
Azure.IoT Build 0:fa2de1b79154 256 }
Azure.IoT Build 0:fa2de1b79154 257 else
Azure.IoT Build 0:fa2de1b79154 258 {
Azure.IoT Build 0:fa2de1b79154 259 /*do nothing, all is fine*/
Azure.IoT Build 0:fa2de1b79154 260 }
Azure.IoT Build 0:fa2de1b79154 261 #else
Azure.IoT Build 0:fa2de1b79154 262 #error STDC_VERSION defined, but of unknown value; unable to sprinf_s, or provide own implementation
Azure.IoT Build 0:fa2de1b79154 263 #endif
Azure.IoT Build 0:fa2de1b79154 264 #else
Azure.IoT Build 0:fa2de1b79154 265 #error for STDC_VERSION undefined (assumed C89), provide own implementation of sprintf_s
Azure.IoT Build 0:fa2de1b79154 266 #endif
Azure.IoT Build 0:fa2de1b79154 267 #endif
Azure.IoT Build 0:fa2de1b79154 268 }
Azure.IoT Build 0:fa2de1b79154 269 return result;
Azure.IoT Build 0:fa2de1b79154 270 }
Azure.IoT Build 0:fa2de1b79154 271 #endif /* _MSC_VER */
Azure.IoT Build 0:fa2de1b79154 272
AzureIoTClient 7:1af47e3a19b6 273 /*Codes_SRS_CRT_ABSTRACTIONS_21_006: [The strtoull_s must use the letters from a(or A) through z(or Z) to represent the numbers between 10 to 35.]*/
AzureIoTClient 7:1af47e3a19b6 274 /* returns the integer value that correspond to the character 'c'. If the character is invalid, it returns -1. */
AzureIoTClient 7:1af47e3a19b6 275 #define DIGIT_VAL(c) (((c>='0') && (c<='9')) ? (c-'0') : ((c>='a') && (c<='z')) ? (c-'a'+10) : ((c>='A') && (c<='Z')) ? (c-'A'+10) : -1)
AzureIoTClient 7:1af47e3a19b6 276 #define IN_BASE_RANGE(d, b) ((d >= 0) && (d < b))
AzureIoTClient 7:1af47e3a19b6 277
AzureIoTClient 7:1af47e3a19b6 278 /*Codes_SRS_CRT_ABSTRACTIONS_21_010: [The white-space must be one of the characters ' ', '\f', '\n', '\r', '\t', '\v'.]*/
AzureIoTClient 7:1af47e3a19b6 279 #define IS_SPACE(c) (c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v')
AzureIoTClient 7:1af47e3a19b6 280
AzureIoTClient 7:1af47e3a19b6 281 /*Codes_SRS_CRT_ABSTRACTIONS_21_001: [The strtoull_s must convert the initial portion of the string pointed to by nptr to unsigned long long int representation.]*/
AzureIoTClient 7:1af47e3a19b6 282 /*Codes_SRS_CRT_ABSTRACTIONS_21_002: [The strtoull_s must resembling an integer represented in some radix determined by the value of base.]*/
AzureIoTClient 7:1af47e3a19b6 283 /*Codes_SRS_CRT_ABSTRACTIONS_21_003: [The strtoull_s must return the integer that represents the value in the initial part of the string. If any.]*/
AzureIoTClient 7:1af47e3a19b6 284 unsigned long long strtoull_s(const char* nptr, char** endptr, int base)
AzureIoTClient 7:1af47e3a19b6 285 {
AzureIoTClient 7:1af47e3a19b6 286 unsigned long long result = 0ULL;
AzureIoTClient 7:1af47e3a19b6 287 bool validStr = true;
AzureIoTClient 7:1af47e3a19b6 288 char* runner = (char*)nptr;
AzureIoTClient 7:1af47e3a19b6 289 bool isNegative = false;
AzureIoTClient 7:1af47e3a19b6 290 int digitVal;
AzureIoTClient 7:1af47e3a19b6 291
AzureIoTClient 7:1af47e3a19b6 292 /*Codes_SRS_CRT_ABSTRACTIONS_21_005: [The strtoull_s must convert number using base 2 to 36.]*/
AzureIoTClient 7:1af47e3a19b6 293 /*Codes_SRS_CRT_ABSTRACTIONS_21_012: [If the subject sequence is empty or does not have the expected form, the strtoull_s must not perform any conversion; the value of nptr is stored in the object pointed to by endptr, provided that endptr is not a NULL pointer.]*/
AzureIoTClient 7:1af47e3a19b6 294 /*Codes_SRS_CRT_ABSTRACTIONS_21_013: [If no conversion could be performed, the strtoull_s returns the value 0L.]*/
AzureIoTClient 7:1af47e3a19b6 295 /*Codes_SRS_CRT_ABSTRACTIONS_21_035: [If the nptr is NULL, the strtoull_s must **not** perform any conversion and must returns 0L; endptr must receive NULL, provided that endptr is not a NULL pointer.]*/
AzureIoTClient 7:1af47e3a19b6 296 if (((base >= 2) || (base == 0)) && (base <= 36) && (runner != NULL))
AzureIoTClient 7:1af47e3a19b6 297 {
AzureIoTClient 7:1af47e3a19b6 298 /*Codes_SRS_CRT_ABSTRACTIONS_21_011: [The valid sequence starts after the first non-white-space character, followed by an optional positive or negative sign, a number or a letter(depending of the base).]*/
AzureIoTClient 7:1af47e3a19b6 299 /*Codes_SRS_CRT_ABSTRACTIONS_21_010: [The white-space must be one of the characters ' ', '\f', '\n', '\r', '\t', '\v'.]*/
AzureIoTClient 7:1af47e3a19b6 300 while (IS_SPACE(*runner))
AzureIoTClient 7:1af47e3a19b6 301 {
AzureIoTClient 7:1af47e3a19b6 302 runner++;
AzureIoTClient 7:1af47e3a19b6 303 }
AzureIoTClient 7:1af47e3a19b6 304 if ((*runner) == '+')
AzureIoTClient 7:1af47e3a19b6 305 {
AzureIoTClient 7:1af47e3a19b6 306 runner++;
AzureIoTClient 7:1af47e3a19b6 307 }
AzureIoTClient 7:1af47e3a19b6 308 else if ((*runner) == '-')
AzureIoTClient 7:1af47e3a19b6 309 {
AzureIoTClient 7:1af47e3a19b6 310 /*Codes_SRS_CRT_ABSTRACTIONS_21_038: [If the subject sequence starts with a negative sign, the strtoull_s will convert it to the posive representation of the negative value.]*/
AzureIoTClient 7:1af47e3a19b6 311 isNegative = true;
AzureIoTClient 7:1af47e3a19b6 312 runner++;
AzureIoTClient 7:1af47e3a19b6 313 }
AzureIoTClient 7:1af47e3a19b6 314
AzureIoTClient 7:1af47e3a19b6 315 if ((*runner) == '0')
AzureIoTClient 7:1af47e3a19b6 316 {
AzureIoTClient 7:1af47e3a19b6 317 if ((*(runner+1) == 'x') || (*(runner+1) == 'X'))
AzureIoTClient 7:1af47e3a19b6 318 {
AzureIoTClient 7:1af47e3a19b6 319 /*Codes_SRS_CRT_ABSTRACTIONS_21_008: [If the base is 0 and '0x' or '0X' precedes the number, strtoull_s must convert to a hexadecimal (base 16).]*/
AzureIoTClient 7:1af47e3a19b6 320 /* hexadecimal... */
AzureIoTClient 7:1af47e3a19b6 321 if ((base == 0) || (base == 16))
AzureIoTClient 7:1af47e3a19b6 322 {
AzureIoTClient 7:1af47e3a19b6 323 base = 16;
AzureIoTClient 7:1af47e3a19b6 324 runner += 2;
AzureIoTClient 7:1af47e3a19b6 325 }
AzureIoTClient 7:1af47e3a19b6 326 }
AzureIoTClient 7:1af47e3a19b6 327 else if((base == 0) || (base == 8))
AzureIoTClient 7:1af47e3a19b6 328 {
AzureIoTClient 7:1af47e3a19b6 329 /*Codes_SRS_CRT_ABSTRACTIONS_21_009: [If the base is 0 and '0' precedes the number, strtoull_s must convert to an octal (base 8).]*/
AzureIoTClient 7:1af47e3a19b6 330 /* octal... */
AzureIoTClient 7:1af47e3a19b6 331 base = 8;
AzureIoTClient 7:1af47e3a19b6 332 runner++;
AzureIoTClient 7:1af47e3a19b6 333 }
AzureIoTClient 7:1af47e3a19b6 334 }
AzureIoTClient 7:1af47e3a19b6 335
AzureIoTClient 7:1af47e3a19b6 336 if(base == 0)
AzureIoTClient 7:1af47e3a19b6 337 {
AzureIoTClient 7:1af47e3a19b6 338 /*Codes_SRS_CRT_ABSTRACTIONS_21_007: [If the base is 0 and no special chars precedes the number, strtoull_s must convert to a decimal (base 10).]*/
AzureIoTClient 7:1af47e3a19b6 339 /* decimal... */
AzureIoTClient 7:1af47e3a19b6 340 base = 10;
AzureIoTClient 7:1af47e3a19b6 341 }
AzureIoTClient 7:1af47e3a19b6 342
AzureIoTClient 7:1af47e3a19b6 343 digitVal = DIGIT_VAL(*runner);
AzureIoTClient 7:1af47e3a19b6 344 if (validStr && IN_BASE_RANGE(digitVal, base))
AzureIoTClient 7:1af47e3a19b6 345 {
AzureIoTClient 7:1af47e3a19b6 346 errno = 0;
AzureIoTClient 7:1af47e3a19b6 347 do
AzureIoTClient 7:1af47e3a19b6 348 {
AzureIoTClient 7:1af47e3a19b6 349 if (((ULLONG_MAX - digitVal) / base) < result)
AzureIoTClient 7:1af47e3a19b6 350 {
AzureIoTClient 7:1af47e3a19b6 351 /*Codes_SRS_CRT_ABSTRACTIONS_21_014: [If the correct value is outside the range, the strtoull_s returns the value ULLONG_MAX, and errno will receive the value ERANGE.]*/
AzureIoTClient 7:1af47e3a19b6 352 /* overflow... */
AzureIoTClient 7:1af47e3a19b6 353 result = ULLONG_MAX;
AzureIoTClient 7:1af47e3a19b6 354 errno = ERANGE;
AzureIoTClient 7:1af47e3a19b6 355 }
AzureIoTClient 7:1af47e3a19b6 356 else
AzureIoTClient 7:1af47e3a19b6 357 {
AzureIoTClient 7:1af47e3a19b6 358 result = result * base + digitVal;
AzureIoTClient 7:1af47e3a19b6 359 }
AzureIoTClient 7:1af47e3a19b6 360 runner++;
AzureIoTClient 7:1af47e3a19b6 361 digitVal = DIGIT_VAL(*runner);
AzureIoTClient 7:1af47e3a19b6 362 } while (IN_BASE_RANGE(digitVal, base));
AzureIoTClient 7:1af47e3a19b6 363 }
AzureIoTClient 7:1af47e3a19b6 364 else
AzureIoTClient 7:1af47e3a19b6 365 {
AzureIoTClient 7:1af47e3a19b6 366 runner = (char*)nptr;
AzureIoTClient 7:1af47e3a19b6 367 }
AzureIoTClient 7:1af47e3a19b6 368 }
AzureIoTClient 7:1af47e3a19b6 369
AzureIoTClient 7:1af47e3a19b6 370 /*Codes_SRS_CRT_ABSTRACTIONS_21_004: [The strtoull_s must return in endptr a final string of one or more unrecognized characters, including the terminating null character of the input string.]*/
AzureIoTClient 7:1af47e3a19b6 371 if (endptr != NULL)
AzureIoTClient 7:1af47e3a19b6 372 {
AzureIoTClient 7:1af47e3a19b6 373 (*endptr) = (char*)runner;
AzureIoTClient 7:1af47e3a19b6 374 }
AzureIoTClient 7:1af47e3a19b6 375
AzureIoTClient 7:1af47e3a19b6 376 /*Codes_SRS_CRT_ABSTRACTIONS_21_038: [If the subject sequence starts with a negative sign, the strtoull_s will convert it to the posive representation of the negative value.]*/
AzureIoTClient 7:1af47e3a19b6 377 if (isNegative)
AzureIoTClient 7:1af47e3a19b6 378 {
AzureIoTClient 7:1af47e3a19b6 379 result = ULLONG_MAX - result + 1;
AzureIoTClient 7:1af47e3a19b6 380 }
AzureIoTClient 7:1af47e3a19b6 381
AzureIoTClient 7:1af47e3a19b6 382 return result;
AzureIoTClient 7:1af47e3a19b6 383 }
AzureIoTClient 7:1af47e3a19b6 384
AzureIoTClient 7:1af47e3a19b6 385 /*Codes_SRS_CRT_ABSTRACTIONS_21_023: [If the string is 'INF' of 'INFINITY' (ignoring case), the strtof_s must return the INFINITY value for float.]*/
AzureIoTClient 7:1af47e3a19b6 386 /*Codes_SRS_CRT_ABSTRACTIONS_21_024: [If the string is 'NAN' or 'NAN(...)' (ignoring case), the strtof_s must return 0.0f and points endptr to the first character after the 'NAN' sequence.]*/
AzureIoTClient 7:1af47e3a19b6 387 /*Codes_SRS_CRT_ABSTRACTIONS_21_033: [If the string is 'INF' of 'INFINITY' (ignoring case), the strtold_s must return the INFINITY value for long double.]*/
AzureIoTClient 7:1af47e3a19b6 388 /*Codes_SRS_CRT_ABSTRACTIONS_21_034: [If the string is 'NAN' or 'NAN(...)' (ignoring case), the strtold_s must return 0.0 and points endptr to the first character after the 'NAN' sequence.]*/
AzureIoTClient 7:1af47e3a19b6 389 #define TOUPPER(c) (((c>='a') && (c<='z'))?c-'a'+'A':c)
AzureIoTClient 7:1af47e3a19b6 390 static int substricmp(const char* nptr, char* subsrt)
AzureIoTClient 7:1af47e3a19b6 391 {
AzureIoTClient 7:1af47e3a19b6 392 int result = 0;
AzureIoTClient 7:1af47e3a19b6 393 while (((*subsrt) != '\0') && (result == 0))
AzureIoTClient 7:1af47e3a19b6 394 {
AzureIoTClient 7:1af47e3a19b6 395 result = TOUPPER(*nptr) - TOUPPER(*subsrt);
AzureIoTClient 7:1af47e3a19b6 396 nptr++;
AzureIoTClient 7:1af47e3a19b6 397 subsrt++;
AzureIoTClient 7:1af47e3a19b6 398 }
AzureIoTClient 7:1af47e3a19b6 399 return result;
AzureIoTClient 7:1af47e3a19b6 400 }
AzureIoTClient 7:1af47e3a19b6 401
AzureIoTClient 7:1af47e3a19b6 402 /*Codes_SRS_CRT_ABSTRACTIONS_21_023: [If the string is 'INF' of 'INFINITY' (ignoring case), the strtof_s must return the INFINITY value for float.]*/
AzureIoTClient 7:1af47e3a19b6 403 /*Codes_SRS_CRT_ABSTRACTIONS_21_033: [If the string is 'INF' of 'INFINITY' (ignoring case), the strtold_s must return the INFINITY value for long double.]*/
AzureIoTClient 7:1af47e3a19b6 404 static bool isInfinity(const char** endptr)
AzureIoTClient 7:1af47e3a19b6 405 {
AzureIoTClient 7:1af47e3a19b6 406 bool result = false;
AzureIoTClient 7:1af47e3a19b6 407 if (substricmp((*endptr), "INF") == 0)
AzureIoTClient 7:1af47e3a19b6 408 {
AzureIoTClient 7:1af47e3a19b6 409 (*endptr) += 3;
AzureIoTClient 7:1af47e3a19b6 410 result = true;
AzureIoTClient 7:1af47e3a19b6 411 if (substricmp((*endptr), "INITY") == 0)
AzureIoTClient 7:1af47e3a19b6 412 {
AzureIoTClient 7:1af47e3a19b6 413 (*endptr) += 5;
AzureIoTClient 7:1af47e3a19b6 414 }
AzureIoTClient 7:1af47e3a19b6 415 }
AzureIoTClient 7:1af47e3a19b6 416 return result;
AzureIoTClient 7:1af47e3a19b6 417 }
AzureIoTClient 7:1af47e3a19b6 418
AzureIoTClient 7:1af47e3a19b6 419 /*Codes_SRS_CRT_ABSTRACTIONS_21_024: [If the string is 'NAN' or 'NAN(...)' (ignoring case), the strtof_s must return 0.0f and points endptr to the first character after the 'NAN' sequence.]*/
AzureIoTClient 7:1af47e3a19b6 420 /*Codes_SRS_CRT_ABSTRACTIONS_21_034: [If the string is 'NAN' or 'NAN(...)' (ignoring case), the strtold_s must return 0.0 and points endptr to the first character after the 'NAN' sequence.]*/
AzureIoTClient 7:1af47e3a19b6 421 static bool isNaN(const char** endptr)
AzureIoTClient 7:1af47e3a19b6 422 {
AzureIoTClient 7:1af47e3a19b6 423 const char* runner = (*endptr);
AzureIoTClient 7:1af47e3a19b6 424 bool result = false;
AzureIoTClient 7:1af47e3a19b6 425 if (substricmp(runner, "NAN") == 0)
AzureIoTClient 7:1af47e3a19b6 426 {
AzureIoTClient 7:1af47e3a19b6 427 runner += 3;
AzureIoTClient 7:1af47e3a19b6 428 result = true;
AzureIoTClient 7:1af47e3a19b6 429 if ((*runner) == '(')
AzureIoTClient 7:1af47e3a19b6 430 {
AzureIoTClient 7:1af47e3a19b6 431 do
AzureIoTClient 7:1af47e3a19b6 432 {
AzureIoTClient 7:1af47e3a19b6 433 runner++;
AzureIoTClient 7:1af47e3a19b6 434 } while (((*runner) != '\0') && ((*runner) != ')'));
AzureIoTClient 7:1af47e3a19b6 435 if ((*runner) == ')')
AzureIoTClient 7:1af47e3a19b6 436 runner++;
AzureIoTClient 7:1af47e3a19b6 437 else
AzureIoTClient 7:1af47e3a19b6 438 result = false;
AzureIoTClient 7:1af47e3a19b6 439 }
AzureIoTClient 7:1af47e3a19b6 440 }
AzureIoTClient 7:1af47e3a19b6 441 if (result)
AzureIoTClient 7:1af47e3a19b6 442 (*endptr) = runner;
AzureIoTClient 7:1af47e3a19b6 443 return result;
AzureIoTClient 7:1af47e3a19b6 444 }
AzureIoTClient 7:1af47e3a19b6 445
AzureIoTClient 7:1af47e3a19b6 446 typedef enum
AzureIoTClient 7:1af47e3a19b6 447 {
AzureIoTClient 7:1af47e3a19b6 448 FST_INFINITY,
AzureIoTClient 7:1af47e3a19b6 449 FST_NAN,
AzureIoTClient 7:1af47e3a19b6 450 FST_NUMBER,
AzureIoTClient 7:1af47e3a19b6 451 FST_OVERFLOW,
AzureIoTClient 7:1af47e3a19b6 452 FST_ERROR
AzureIoTClient 7:1af47e3a19b6 453 } FLOAT_STRING_TYPE;
AzureIoTClient 7:1af47e3a19b6 454
AzureIoTClient 7:1af47e3a19b6 455 static FLOAT_STRING_TYPE splitFloatString(const char* nptr, char** endptr, int *signal, double *fraction, int *exponential)
AzureIoTClient 7:1af47e3a19b6 456 {
AzureIoTClient 7:1af47e3a19b6 457 FLOAT_STRING_TYPE result = FST_ERROR;
AzureIoTClient 7:1af47e3a19b6 458
AzureIoTClient 7:1af47e3a19b6 459 unsigned long long ullInteger = 0;
AzureIoTClient 7:1af47e3a19b6 460 unsigned long long ullFraction = 0;
AzureIoTClient 7:1af47e3a19b6 461 int integerSize = 0;
AzureIoTClient 7:1af47e3a19b6 462 int fractionSize = 0;
AzureIoTClient 7:1af47e3a19b6 463 char* startptr;
AzureIoTClient 7:1af47e3a19b6 464
AzureIoTClient 7:1af47e3a19b6 465 (*endptr) = (char*)nptr;
AzureIoTClient 7:1af47e3a19b6 466
AzureIoTClient 7:1af47e3a19b6 467 /*Codes_SRS_CRT_ABSTRACTIONS_21_018: [The white-space for strtof_s must be one of the characters ' ', '\f', '\n', '\r', '\t', '\v'.]*/
AzureIoTClient 7:1af47e3a19b6 468 /*Codes_SRS_CRT_ABSTRACTIONS_21_028: [The white-space for strtold_s must be one of the characters ' ', '\f', '\n', '\r', '\t', '\v'.]*/
AzureIoTClient 7:1af47e3a19b6 469 while (IS_SPACE(**endptr))
AzureIoTClient 7:1af47e3a19b6 470 {
AzureIoTClient 7:1af47e3a19b6 471 (*endptr)++;
AzureIoTClient 7:1af47e3a19b6 472 }
AzureIoTClient 7:1af47e3a19b6 473
AzureIoTClient 7:1af47e3a19b6 474 /*Codes_SRS_CRT_ABSTRACTIONS_21_019: [The valid sequence for strtof_s starts after the first non-white - space character, followed by an optional positive or negative sign, a number, 'INF', or 'NAN' (ignoring case).]*/
AzureIoTClient 7:1af47e3a19b6 475 /*Codes_SRS_CRT_ABSTRACTIONS_21_029: [The valid sequence for strtold_s starts after the first non-white - space character, followed by an optional positive or negative sign, a number, 'INF', or 'NAN' (ignoring case).]*/
AzureIoTClient 7:1af47e3a19b6 476 (*signal) = +1;
AzureIoTClient 7:1af47e3a19b6 477 if ((**endptr) == '+')
AzureIoTClient 7:1af47e3a19b6 478 {
AzureIoTClient 7:1af47e3a19b6 479 (*endptr)++;
AzureIoTClient 7:1af47e3a19b6 480 }
AzureIoTClient 7:1af47e3a19b6 481 else if ((**endptr) == '-')
AzureIoTClient 7:1af47e3a19b6 482 {
AzureIoTClient 7:1af47e3a19b6 483 (*signal) = -1;
AzureIoTClient 7:1af47e3a19b6 484 (*endptr)++;
AzureIoTClient 7:1af47e3a19b6 485 }
AzureIoTClient 7:1af47e3a19b6 486
AzureIoTClient 7:1af47e3a19b6 487 /*Codes_SRS_CRT_ABSTRACTIONS_21_023: [If the string is 'INF' of 'INFINITY' (ignoring case), the strtof_s must return the INFINITY value for float.]*/
AzureIoTClient 7:1af47e3a19b6 488 /*Codes_SRS_CRT_ABSTRACTIONS_21_033: [If the string is 'INF' of 'INFINITY' (ignoring case), the strtold_s must return the INFINITY value for long double.]*/
AzureIoTClient 7:1af47e3a19b6 489 if (isInfinity((const char**)endptr))
AzureIoTClient 7:1af47e3a19b6 490 {
AzureIoTClient 7:1af47e3a19b6 491 result = FST_INFINITY;
AzureIoTClient 7:1af47e3a19b6 492 }
AzureIoTClient 7:1af47e3a19b6 493 /*Codes_SRS_CRT_ABSTRACTIONS_21_034: [If the string is 'NAN' or 'NAN(...)' (ignoring case), the strtold_s must return 0.0 and points endptr to the first character after the 'NAN' sequence.]*/
AzureIoTClient 7:1af47e3a19b6 494 /*Codes_SRS_CRT_ABSTRACTIONS_21_024: [If the string is 'NAN' or 'NAN(...)' (ignoring case), the strtof_s must return 0.0f and points endptr to the first character after the 'NAN' sequence.]*/
AzureIoTClient 7:1af47e3a19b6 495 else if (isNaN((const char**)endptr))
AzureIoTClient 7:1af47e3a19b6 496 {
AzureIoTClient 7:1af47e3a19b6 497 result = FST_NAN;
AzureIoTClient 7:1af47e3a19b6 498 }
AzureIoTClient 7:1af47e3a19b6 499 else if (IN_BASE_RANGE(DIGIT_VAL(**endptr), 10))
AzureIoTClient 7:1af47e3a19b6 500 {
AzureIoTClient 7:1af47e3a19b6 501 result = FST_NUMBER;
AzureIoTClient 7:1af47e3a19b6 502 startptr = *endptr;
AzureIoTClient 7:1af47e3a19b6 503 /* integers will go to the fraction and exponential. */
AzureIoTClient 7:1af47e3a19b6 504 ullInteger = strtoull_s(startptr, endptr, 10);
AzureIoTClient 7:1af47e3a19b6 505 integerSize = (int)((*endptr) - startptr);
AzureIoTClient 7:1af47e3a19b6 506 if ((ullInteger == ULLONG_MAX) && (errno != 0))
AzureIoTClient 7:1af47e3a19b6 507 {
AzureIoTClient 7:1af47e3a19b6 508 result = FST_OVERFLOW;
AzureIoTClient 7:1af47e3a19b6 509 }
AzureIoTClient 7:1af47e3a19b6 510
AzureIoTClient 7:1af47e3a19b6 511 /* get the real fraction part, if exist. */
AzureIoTClient 7:1af47e3a19b6 512 if ((**endptr) == '.')
AzureIoTClient 7:1af47e3a19b6 513 {
AzureIoTClient 7:1af47e3a19b6 514 startptr = (*endptr) + 1;
AzureIoTClient 7:1af47e3a19b6 515 ullFraction = strtoull_s(startptr, endptr, 10);
AzureIoTClient 7:1af47e3a19b6 516 fractionSize = (int)((*endptr) - startptr);
AzureIoTClient 7:1af47e3a19b6 517 if ((ullFraction == ULLONG_MAX) && (errno != 0))
AzureIoTClient 7:1af47e3a19b6 518 {
AzureIoTClient 7:1af47e3a19b6 519 result = FST_OVERFLOW;
AzureIoTClient 7:1af47e3a19b6 520 }
AzureIoTClient 7:1af47e3a19b6 521 }
AzureIoTClient 7:1af47e3a19b6 522
AzureIoTClient 7:1af47e3a19b6 523 if (((**endptr) == 'e') || ((**endptr) == 'E'))
AzureIoTClient 7:1af47e3a19b6 524 {
AzureIoTClient 7:1af47e3a19b6 525 startptr = (*endptr) + 1;
AzureIoTClient 7:1af47e3a19b6 526 (*exponential) = strtol(startptr, endptr, 10);
AzureIoTClient 7:1af47e3a19b6 527 if (((*exponential) < (DBL_MAX_10_EXP * (-1))) || ((*exponential) > DBL_MAX_10_EXP))
AzureIoTClient 7:1af47e3a19b6 528 {
AzureIoTClient 7:1af47e3a19b6 529 result = FST_OVERFLOW;
AzureIoTClient 7:1af47e3a19b6 530 }
AzureIoTClient 7:1af47e3a19b6 531 }
AzureIoTClient 7:1af47e3a19b6 532 else
AzureIoTClient 7:1af47e3a19b6 533 {
AzureIoTClient 7:1af47e3a19b6 534 (*exponential) = 0;
AzureIoTClient 7:1af47e3a19b6 535 }
AzureIoTClient 7:1af47e3a19b6 536
AzureIoTClient 7:1af47e3a19b6 537 if (result == FST_NUMBER)
AzureIoTClient 7:1af47e3a19b6 538 {
AzureIoTClient 7:1af47e3a19b6 539 /* Add ullInteger to ullFraction. */
AzureIoTClient 7:1af47e3a19b6 540 ullFraction += (ullInteger * (unsigned long long)(pow(10, (double)fractionSize)));
AzureIoTClient 7:1af47e3a19b6 541 (*fraction) = ((double)ullFraction / (pow(10.0f, (double)(fractionSize + integerSize - 1))));
AzureIoTClient 7:1af47e3a19b6 542
AzureIoTClient 7:1af47e3a19b6 543 /* Unify rest of integerSize and fractionSize in the exponential. */
AzureIoTClient 7:1af47e3a19b6 544 (*exponential) += integerSize - 1;
AzureIoTClient 7:1af47e3a19b6 545 }
AzureIoTClient 7:1af47e3a19b6 546 }
AzureIoTClient 7:1af47e3a19b6 547
AzureIoTClient 7:1af47e3a19b6 548 return result;
AzureIoTClient 7:1af47e3a19b6 549 }
AzureIoTClient 7:1af47e3a19b6 550
AzureIoTClient 7:1af47e3a19b6 551 /*Codes_SRS_CRT_ABSTRACTIONS_21_015: [The strtof_s must convert the initial portion of the string pointed to by nptr to float representation.]*/
AzureIoTClient 7:1af47e3a19b6 552 /*Codes_SRS_CRT_ABSTRACTIONS_21_016: [The strtof_s must return the float that represents the value in the initial part of the string. If any.]*/
AzureIoTClient 7:1af47e3a19b6 553 float strtof_s(const char* nptr, char** endptr)
AzureIoTClient 7:1af47e3a19b6 554 {
AzureIoTClient 7:1af47e3a19b6 555 int signal = 1;
AzureIoTClient 7:1af47e3a19b6 556 double fraction;
AzureIoTClient 7:1af47e3a19b6 557 int exponential;
AzureIoTClient 7:1af47e3a19b6 558 char* runner = (char*)nptr;
AzureIoTClient 7:1af47e3a19b6 559 double val;
AzureIoTClient 7:1af47e3a19b6 560
AzureIoTClient 7:1af47e3a19b6 561 /*Codes_SRS_CRT_ABSTRACTIONS_21_021: [If no conversion could be performed, the strtof_s returns the value 0.0.]*/
AzureIoTClient 7:1af47e3a19b6 562 float result = 0.0;
AzureIoTClient 7:1af47e3a19b6 563
AzureIoTClient 7:1af47e3a19b6 564 /*Codes_SRS_CRT_ABSTRACTIONS_21_036: [**If the nptr is NULL, the strtof_s must not perform any conversion and must returns 0.0f; endptr must receive NULL, provided that endptr is not a NULL pointer.]*/
AzureIoTClient 7:1af47e3a19b6 565 if (nptr != NULL)
AzureIoTClient 7:1af47e3a19b6 566 {
AzureIoTClient 7:1af47e3a19b6 567 switch (splitFloatString(nptr, &runner, &signal, &fraction, &exponential))
AzureIoTClient 7:1af47e3a19b6 568 {
AzureIoTClient 7:1af47e3a19b6 569 case FST_INFINITY:
AzureIoTClient 7:1af47e3a19b6 570 /*Codes_SRS_CRT_ABSTRACTIONS_21_023: [If the string is 'INF' of 'INFINITY' (ignoring case), the strtof_s must return the INFINITY value for float.]*/
AzureIoTClient 7:1af47e3a19b6 571 result = INFINITY * (signal);
AzureIoTClient 7:1af47e3a19b6 572 errno = 0;
AzureIoTClient 7:1af47e3a19b6 573 break;
AzureIoTClient 7:1af47e3a19b6 574 case FST_NAN:
AzureIoTClient 7:1af47e3a19b6 575 /*Codes_SRS_CRT_ABSTRACTIONS_21_024: [If the string is 'NAN' or 'NAN(...)' (ignoring case), the strtof_s must return 0.0f and points endptr to the first character after the 'NAN' sequence.]*/
AzureIoTClient 7:1af47e3a19b6 576 result = NAN;
AzureIoTClient 7:1af47e3a19b6 577 break;
AzureIoTClient 7:1af47e3a19b6 578 case FST_NUMBER:
AzureIoTClient 7:1af47e3a19b6 579 val = fraction * pow(10.0, (double)exponential) * (double)signal;
AzureIoTClient 7:1af47e3a19b6 580 if ((val >= (FLT_MAX * (-1))) && (val <= FLT_MAX))
AzureIoTClient 7:1af47e3a19b6 581 {
AzureIoTClient 7:1af47e3a19b6 582 /*Codes_SRS_CRT_ABSTRACTIONS_21_016: [The strtof_s must return the float that represents the value in the initial part of the string. If any.]*/
AzureIoTClient 7:1af47e3a19b6 583 result = (float)val;
AzureIoTClient 7:1af47e3a19b6 584 }
AzureIoTClient 7:1af47e3a19b6 585 else
AzureIoTClient 7:1af47e3a19b6 586 {
AzureIoTClient 7:1af47e3a19b6 587 /*Codes_SRS_CRT_ABSTRACTIONS_21_022: [If the correct value is outside the range, the strtof_s returns the value plus or minus HUGE_VALF, and errno will receive the value ERANGE.]*/
AzureIoTClient 7:1af47e3a19b6 588 result = HUGE_VALF * (signal);
AzureIoTClient 7:1af47e3a19b6 589 errno = ERANGE;
AzureIoTClient 7:1af47e3a19b6 590 }
AzureIoTClient 7:1af47e3a19b6 591 break;
AzureIoTClient 7:1af47e3a19b6 592 case FST_OVERFLOW:
AzureIoTClient 7:1af47e3a19b6 593 /*Codes_SRS_CRT_ABSTRACTIONS_21_022: [If the correct value is outside the range, the strtof_s returns the value plus or minus HUGE_VALF, and errno will receive the value ERANGE.]*/
AzureIoTClient 7:1af47e3a19b6 594 result = HUGE_VALF * (signal);
AzureIoTClient 7:1af47e3a19b6 595 errno = ERANGE;
AzureIoTClient 7:1af47e3a19b6 596 break;
AzureIoTClient 7:1af47e3a19b6 597 default:
AzureIoTClient 7:1af47e3a19b6 598 /*Codes_SRS_CRT_ABSTRACTIONS_21_020: [If the subject sequence is empty or does not have the expected form, the strtof_s must not perform any conversion and must returns 0.0f; the value of nptr is stored in the object pointed to by endptr, provided that endptr is not a NULL pointer.]*/
AzureIoTClient 7:1af47e3a19b6 599 runner = (char*)nptr;
AzureIoTClient 7:1af47e3a19b6 600 break;
AzureIoTClient 7:1af47e3a19b6 601 }
AzureIoTClient 7:1af47e3a19b6 602 }
AzureIoTClient 7:1af47e3a19b6 603
AzureIoTClient 7:1af47e3a19b6 604 /*Codes_SRS_CRT_ABSTRACTIONS_21_017: [The strtof_s must return in endptr a final string of one or more unrecognized characters, including the terminating null character of the input string.]*/
AzureIoTClient 7:1af47e3a19b6 605 if (endptr != NULL)
AzureIoTClient 7:1af47e3a19b6 606 {
AzureIoTClient 7:1af47e3a19b6 607 (*endptr) = runner;
AzureIoTClient 7:1af47e3a19b6 608 }
AzureIoTClient 7:1af47e3a19b6 609
AzureIoTClient 7:1af47e3a19b6 610 return result;
AzureIoTClient 7:1af47e3a19b6 611 }
AzureIoTClient 7:1af47e3a19b6 612
AzureIoTClient 7:1af47e3a19b6 613 /*Codes_SRS_CRT_ABSTRACTIONS_21_025: [The strtold_s must convert the initial portion of the string pointed to by nptr to long double representation.]*/
AzureIoTClient 7:1af47e3a19b6 614 /*Codes_SRS_CRT_ABSTRACTIONS_21_026: [The strtold_s must return the long double that represents the value in the initial part of the string. If any.]*/
AzureIoTClient 7:1af47e3a19b6 615 long double strtold_s(const char* nptr, char** endptr)
AzureIoTClient 7:1af47e3a19b6 616 {
AzureIoTClient 7:1af47e3a19b6 617 int signal = 1;
AzureIoTClient 7:1af47e3a19b6 618 double fraction;
AzureIoTClient 7:1af47e3a19b6 619 int exponential;
AzureIoTClient 7:1af47e3a19b6 620 char* runner = (char*)nptr;
AzureIoTClient 7:1af47e3a19b6 621
AzureIoTClient 7:1af47e3a19b6 622 /*Codes_SRS_CRT_ABSTRACTIONS_21_031: [If no conversion could be performed, the strtold_s returns the value 0.0.]*/
AzureIoTClient 7:1af47e3a19b6 623 long double result = 0.0;
AzureIoTClient 7:1af47e3a19b6 624
AzureIoTClient 7:1af47e3a19b6 625 /*Codes_SRS_CRT_ABSTRACTIONS_21_037: [If the nptr is NULL, the strtold_s must not perform any conversion and must returns 0.0; endptr must receive NULL, provided that endptr is not a NULL pointer.]*/
AzureIoTClient 7:1af47e3a19b6 626 if (nptr != NULL)
AzureIoTClient 7:1af47e3a19b6 627 {
AzureIoTClient 7:1af47e3a19b6 628 switch (splitFloatString(nptr, &runner, &signal, &fraction, &exponential))
AzureIoTClient 7:1af47e3a19b6 629 {
AzureIoTClient 7:1af47e3a19b6 630 case FST_INFINITY:
AzureIoTClient 7:1af47e3a19b6 631 /*Codes_SRS_CRT_ABSTRACTIONS_21_033: [If the string is 'INF' of 'INFINITY' (ignoring case), the strtold_s must return the INFINITY value for long double.]*/
AzureIoTClient 7:1af47e3a19b6 632 result = INFINITY * (signal);
AzureIoTClient 7:1af47e3a19b6 633 errno = 0;
AzureIoTClient 7:1af47e3a19b6 634 break;
AzureIoTClient 7:1af47e3a19b6 635 case FST_NAN:
AzureIoTClient 7:1af47e3a19b6 636 /*Codes_SRS_CRT_ABSTRACTIONS_21_034: [If the string is 'NAN' or 'NAN(...)' (ignoring case), the strtold_s must return 0.0 and points endptr to the first character after the 'NAN' sequence.]*/
AzureIoTClient 7:1af47e3a19b6 637 result = NAN;
AzureIoTClient 7:1af47e3a19b6 638 break;
AzureIoTClient 7:1af47e3a19b6 639 case FST_NUMBER:
AzureIoTClient 7:1af47e3a19b6 640 if ((exponential != DBL_MAX_10_EXP || (fraction <= 1.7976931348623158)) &&
AzureIoTClient 7:1af47e3a19b6 641 (exponential != (DBL_MAX_10_EXP * (-1)) || (fraction <= 2.2250738585072014)))
AzureIoTClient 7:1af47e3a19b6 642 {
AzureIoTClient 7:1af47e3a19b6 643 /*Codes_SRS_CRT_ABSTRACTIONS_21_026: [The strtold_s must return the long double that represents the value in the initial part of the string. If any.]*/
AzureIoTClient 7:1af47e3a19b6 644 result = fraction * pow(10.0, (double)exponential) * (double)signal;
AzureIoTClient 7:1af47e3a19b6 645 }
AzureIoTClient 7:1af47e3a19b6 646 else
AzureIoTClient 7:1af47e3a19b6 647 {
AzureIoTClient 7:1af47e3a19b6 648 /*Codes_SRS_CRT_ABSTRACTIONS_21_032: [If the correct value is outside the range, the strtold_s returns the value plus or minus HUGE_VALL, and errno will receive the value ERANGE.]*/
AzureIoTClient 7:1af47e3a19b6 649 result = HUGE_VALF * (signal);
AzureIoTClient 7:1af47e3a19b6 650 errno = ERANGE;
AzureIoTClient 7:1af47e3a19b6 651 }
AzureIoTClient 7:1af47e3a19b6 652 break;
AzureIoTClient 7:1af47e3a19b6 653 case FST_OVERFLOW:
AzureIoTClient 7:1af47e3a19b6 654 /*Codes_SRS_CRT_ABSTRACTIONS_21_032: [If the correct value is outside the range, the strtold_s returns the value plus or minus HUGE_VALL, and errno will receive the value ERANGE.]*/
AzureIoTClient 7:1af47e3a19b6 655 result = HUGE_VALF * (signal);
AzureIoTClient 7:1af47e3a19b6 656 errno = ERANGE;
AzureIoTClient 7:1af47e3a19b6 657 break;
AzureIoTClient 7:1af47e3a19b6 658 default:
AzureIoTClient 7:1af47e3a19b6 659 /*Codes_SRS_CRT_ABSTRACTIONS_21_030: [If the subject sequence is empty or does not have the expected form, the strtold_s must not perform any conversion and must returns 0.0; the value of nptr is stored in the object pointed to by endptr, provided that endptr is not a NULL pointer.]*/
AzureIoTClient 7:1af47e3a19b6 660 runner = (char*)nptr;
AzureIoTClient 7:1af47e3a19b6 661 break;
AzureIoTClient 7:1af47e3a19b6 662 }
AzureIoTClient 7:1af47e3a19b6 663 }
AzureIoTClient 7:1af47e3a19b6 664
AzureIoTClient 7:1af47e3a19b6 665 /*Codes_SRS_CRT_ABSTRACTIONS_21_027: [The strtold_s must return in endptr a final string of one or more unrecognized characters, including the terminating null character of the input string.]*/
AzureIoTClient 7:1af47e3a19b6 666 if (endptr != NULL)
AzureIoTClient 7:1af47e3a19b6 667 {
AzureIoTClient 7:1af47e3a19b6 668 (*endptr) = runner;
AzureIoTClient 7:1af47e3a19b6 669 }
AzureIoTClient 7:1af47e3a19b6 670
AzureIoTClient 7:1af47e3a19b6 671 return result;
AzureIoTClient 7:1af47e3a19b6 672 }
AzureIoTClient 7:1af47e3a19b6 673
AzureIoTClient 7:1af47e3a19b6 674
Azure.IoT Build 0:fa2de1b79154 675 /*Codes_SRS_CRT_ABSTRACTIONS_99_038: [mallocAndstrcpy_s shall allocate memory for destination buffer to fit the string in the source parameter.]*/
Azure.IoT Build 0:fa2de1b79154 676 int mallocAndStrcpy_s(char** destination, const char* source)
Azure.IoT Build 0:fa2de1b79154 677 {
Azure.IoT Build 0:fa2de1b79154 678 int result;
AzureIoTClient 7:1af47e3a19b6 679 int copied_result;
Azure.IoT Build 0:fa2de1b79154 680 /*Codes_SRS_CRT_ABSTRACTIONS_99_036: [destination parameter or source parameter is NULL, the error code returned shall be EINVAL and destination shall not be modified.]*/
Azure.IoT Build 0:fa2de1b79154 681 if ((destination == NULL) || (source == NULL))
Azure.IoT Build 0:fa2de1b79154 682 {
AzureIoTClient 7:1af47e3a19b6 683 /*If strDestination or strSource is a NULL pointer[...]these functions return EINVAL */
Azure.IoT Build 0:fa2de1b79154 684 result = EINVAL;
Azure.IoT Build 0:fa2de1b79154 685 }
Azure.IoT Build 0:fa2de1b79154 686 else
Azure.IoT Build 0:fa2de1b79154 687 {
Azure.IoT Build 0:fa2de1b79154 688 size_t l = strlen(source);
Azure.IoT Build 6:c55b013dfc2a 689 char* temp = (char*)malloc(l + 1);
Azure.IoT Build 6:c55b013dfc2a 690
Azure.IoT Build 0:fa2de1b79154 691 /*Codes_SRS_CRT_ABSTRACTIONS_99_037: [Upon failure to allocate memory for the destination, the function will return ENOMEM.]*/
Azure.IoT Build 6:c55b013dfc2a 692 if (temp == NULL)
Azure.IoT Build 0:fa2de1b79154 693 {
Azure.IoT Build 0:fa2de1b79154 694 result = ENOMEM;
Azure.IoT Build 0:fa2de1b79154 695 }
Azure.IoT Build 0:fa2de1b79154 696 else
Azure.IoT Build 0:fa2de1b79154 697 {
Azure.IoT Build 6:c55b013dfc2a 698 *destination = temp;
Azure.IoT Build 0:fa2de1b79154 699 /*Codes_SRS_CRT_ABSTRACTIONS_99_039: [mallocAndstrcpy_s shall copy the contents in the address source, including the terminating null character into location specified by the destination pointer after the memory allocation.]*/
AzureIoTClient 7:1af47e3a19b6 700 copied_result = strcpy_s(*destination, l + 1, source);
AzureIoTClient 7:1af47e3a19b6 701 if (copied_result < 0) /*strcpy_s error*/
Azure.IoT Build 0:fa2de1b79154 702 {
Azure.IoT Build 0:fa2de1b79154 703 free(*destination);
Azure.IoT Build 0:fa2de1b79154 704 *destination = NULL;
AzureIoTClient 7:1af47e3a19b6 705 result = copied_result;
Azure.IoT Build 0:fa2de1b79154 706 }
Azure.IoT Build 0:fa2de1b79154 707 else
Azure.IoT Build 0:fa2de1b79154 708 {
Azure.IoT Build 0:fa2de1b79154 709 /*Codes_SRS_CRT_ABSTRACTIONS_99_035: [mallocAndstrcpy_s shall return Zero upon success]*/
Azure.IoT Build 0:fa2de1b79154 710 result = 0;
Azure.IoT Build 0:fa2de1b79154 711 }
Azure.IoT Build 0:fa2de1b79154 712 }
Azure.IoT Build 0:fa2de1b79154 713 }
Azure.IoT Build 0:fa2de1b79154 714 return result;
Azure.IoT Build 0:fa2de1b79154 715 }
Azure.IoT Build 0:fa2de1b79154 716
Azure.IoT Build 0:fa2de1b79154 717 /*takes "value" and transforms it into a decimal string*/
Azure.IoT Build 0:fa2de1b79154 718 /*10 => "10"*/
Azure.IoT Build 0:fa2de1b79154 719 /*return 0 when everything went ok*/
Azure.IoT Build 0:fa2de1b79154 720 /*Codes_SRS_CRT_ABSTRACTIONS_02_001: [unsignedIntToString shall convert the parameter value to its decimal representation as a string in the buffer indicated by parameter destination having the size indicated by parameter destinationSize.] */
Azure.IoT Build 0:fa2de1b79154 721 int unsignedIntToString(char* destination, size_t destinationSize, unsigned int value)
Azure.IoT Build 0:fa2de1b79154 722 {
Azure.IoT Build 0:fa2de1b79154 723 int result;
Azure.IoT Build 0:fa2de1b79154 724 size_t pos;
Azure.IoT Build 0:fa2de1b79154 725 /*the below loop gets the number in reverse order*/
Azure.IoT Build 0:fa2de1b79154 726 /*Codes_SRS_CRT_ABSTRACTIONS_02_003: [If destination is NULL then unsignedIntToString shall fail.] */
Azure.IoT Build 0:fa2de1b79154 727 /*Codes_SRS_CRT_ABSTRACTIONS_02_002: [If the conversion fails for any reason (for example, insufficient buffer space), a non-zero return value shall be supplied and unsignedIntToString shall fail.] */
Azure.IoT Build 0:fa2de1b79154 728 if (
Azure.IoT Build 0:fa2de1b79154 729 (destination == NULL) ||
Azure.IoT Build 0:fa2de1b79154 730 (destinationSize < 2) /*because the smallest number is '0\0' which requires 2 characters*/
Azure.IoT Build 0:fa2de1b79154 731 )
Azure.IoT Build 0:fa2de1b79154 732 {
Azure.IoT Build 0:fa2de1b79154 733 result = __LINE__;
Azure.IoT Build 0:fa2de1b79154 734 }
Azure.IoT Build 0:fa2de1b79154 735 else
Azure.IoT Build 0:fa2de1b79154 736 {
Azure.IoT Build 0:fa2de1b79154 737 pos = 0;
Azure.IoT Build 0:fa2de1b79154 738 do
Azure.IoT Build 0:fa2de1b79154 739 {
Azure.IoT Build 0:fa2de1b79154 740 destination[pos++] = '0' + (value % 10);
Azure.IoT Build 0:fa2de1b79154 741 value /= 10;
Azure.IoT Build 0:fa2de1b79154 742 } while ((value > 0) && (pos < (destinationSize-1)));
Azure.IoT Build 0:fa2de1b79154 743
Azure.IoT Build 0:fa2de1b79154 744 if (value == 0)
Azure.IoT Build 0:fa2de1b79154 745 {
Azure.IoT Build 0:fa2de1b79154 746 size_t w;
Azure.IoT Build 0:fa2de1b79154 747 destination[pos] = '\0';
Azure.IoT Build 0:fa2de1b79154 748 /*all converted and they fit*/
Azure.IoT Build 0:fa2de1b79154 749 for (w = 0; w <= (pos-1) >> 1; w++)
Azure.IoT Build 0:fa2de1b79154 750 {
Azure.IoT Build 0:fa2de1b79154 751 char temp;
Azure.IoT Build 0:fa2de1b79154 752 temp = destination[w];
Azure.IoT Build 0:fa2de1b79154 753 destination[w] = destination[pos - 1 - w];
Azure.IoT Build 0:fa2de1b79154 754 destination[pos -1 - w] = temp;
Azure.IoT Build 0:fa2de1b79154 755 }
Azure.IoT Build 0:fa2de1b79154 756 /*Codes_SRS_CRT_ABSTRACTIONS_02_004: [If the conversion has been successfull then unsignedIntToString shall return 0.] */
Azure.IoT Build 0:fa2de1b79154 757 result = 0;
Azure.IoT Build 0:fa2de1b79154 758 }
Azure.IoT Build 0:fa2de1b79154 759 else
Azure.IoT Build 0:fa2de1b79154 760 {
Azure.IoT Build 0:fa2de1b79154 761 /*Codes_SRS_CRT_ABSTRACTIONS_02_002: [If the conversion fails for any reason (for example, insufficient buffer space), a non-zero return value shall be supplied and unsignedIntToString shall fail.] */
Azure.IoT Build 0:fa2de1b79154 762 result = __LINE__;
Azure.IoT Build 0:fa2de1b79154 763 }
Azure.IoT Build 0:fa2de1b79154 764 }
Azure.IoT Build 0:fa2de1b79154 765 return result;
Azure.IoT Build 0:fa2de1b79154 766 }
Azure.IoT Build 0:fa2de1b79154 767
Azure.IoT Build 0:fa2de1b79154 768 /*takes "value" and transforms it into a decimal string*/
Azure.IoT Build 0:fa2de1b79154 769 /*10 => "10"*/
Azure.IoT Build 0:fa2de1b79154 770 /*return 0 when everything went ok*/
Azure.IoT Build 0:fa2de1b79154 771 /*Codes_SRS_CRT_ABSTRACTIONS_02_001: [unsignedIntToString shall convert the parameter value to its decimal representation as a string in the buffer indicated by parameter destination having the size indicated by parameter destinationSize.] */
Azure.IoT Build 0:fa2de1b79154 772 int size_tToString(char* destination, size_t destinationSize, size_t value)
Azure.IoT Build 0:fa2de1b79154 773 {
Azure.IoT Build 0:fa2de1b79154 774 int result;
Azure.IoT Build 0:fa2de1b79154 775 size_t pos;
Azure.IoT Build 0:fa2de1b79154 776 /*the below loop gets the number in reverse order*/
Azure.IoT Build 0:fa2de1b79154 777 /*Codes_SRS_CRT_ABSTRACTIONS_02_003: [If destination is NULL then unsignedIntToString shall fail.] */
Azure.IoT Build 0:fa2de1b79154 778 /*Codes_SRS_CRT_ABSTRACTIONS_02_002: [If the conversion fails for any reason (for example, insufficient buffer space), a non-zero return value shall be supplied and unsignedIntToString shall fail.] */
Azure.IoT Build 0:fa2de1b79154 779 if (
Azure.IoT Build 0:fa2de1b79154 780 (destination == NULL) ||
Azure.IoT Build 0:fa2de1b79154 781 (destinationSize < 2) /*because the smallest number is '0\0' which requires 2 characters*/
Azure.IoT Build 0:fa2de1b79154 782 )
Azure.IoT Build 0:fa2de1b79154 783 {
Azure.IoT Build 0:fa2de1b79154 784 result = __LINE__;
Azure.IoT Build 0:fa2de1b79154 785 }
Azure.IoT Build 0:fa2de1b79154 786 else
Azure.IoT Build 0:fa2de1b79154 787 {
Azure.IoT Build 0:fa2de1b79154 788 pos = 0;
Azure.IoT Build 0:fa2de1b79154 789 do
Azure.IoT Build 0:fa2de1b79154 790 {
Azure.IoT Build 0:fa2de1b79154 791 destination[pos++] = '0' + (value % 10);
Azure.IoT Build 0:fa2de1b79154 792 value /= 10;
Azure.IoT Build 0:fa2de1b79154 793 } while ((value > 0) && (pos < (destinationSize - 1)));
Azure.IoT Build 0:fa2de1b79154 794
Azure.IoT Build 0:fa2de1b79154 795 if (value == 0)
Azure.IoT Build 0:fa2de1b79154 796 {
Azure.IoT Build 0:fa2de1b79154 797 size_t w;
Azure.IoT Build 0:fa2de1b79154 798 destination[pos] = '\0';
Azure.IoT Build 0:fa2de1b79154 799 /*all converted and they fit*/
Azure.IoT Build 0:fa2de1b79154 800 for (w = 0; w <= (pos - 1) >> 1; w++)
Azure.IoT Build 0:fa2de1b79154 801 {
Azure.IoT Build 0:fa2de1b79154 802 char temp;
Azure.IoT Build 0:fa2de1b79154 803 temp = destination[w];
Azure.IoT Build 0:fa2de1b79154 804 destination[w] = destination[pos - 1 - w];
Azure.IoT Build 0:fa2de1b79154 805 destination[pos - 1 - w] = temp;
Azure.IoT Build 0:fa2de1b79154 806 }
Azure.IoT Build 0:fa2de1b79154 807 /*Codes_SRS_CRT_ABSTRACTIONS_02_004: [If the conversion has been successfull then unsignedIntToString shall return 0.] */
Azure.IoT Build 0:fa2de1b79154 808 result = 0;
Azure.IoT Build 0:fa2de1b79154 809 }
Azure.IoT Build 0:fa2de1b79154 810 else
Azure.IoT Build 0:fa2de1b79154 811 {
Azure.IoT Build 0:fa2de1b79154 812 /*Codes_SRS_CRT_ABSTRACTIONS_02_002: [If the conversion fails for any reason (for example, insufficient buffer space), a non-zero return value shall be supplied and unsignedIntToString shall fail.] */
Azure.IoT Build 0:fa2de1b79154 813 result = __LINE__;
Azure.IoT Build 0:fa2de1b79154 814 }
Azure.IoT Build 0:fa2de1b79154 815 }
Azure.IoT Build 0:fa2de1b79154 816 return result;
Azure.IoT Build 0:fa2de1b79154 817 }