Azure IoT common library

Dependents:   STM32F746_iothub_client_sample_mqtt f767zi_mqtt iothub_client_sample_amqp iothub_client_sample_http ... more

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