Azure IoT common library

Dependents:   STM32F746_iothub_client_sample_mqtt f767zi_mqtt iothub_client_sample_amqp iothub_client_sample_http ... more

Committer:
AzureIoTClient
Date:
Sat Jan 28 09:35:22 2017 -0800
Revision:
19:2e0811512ceb
Parent:
18:6d8a413a4d9a
Child:
21:b92006c5b9ff
1.1.6

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