Azure IoT common library

Fork of azure_c_shared_utility by Azure IoT

Committer:
AzureIoTClient
Date:
Mon May 22 10:35:55 2017 -0700
Revision:
27:8656a313842b
Parent:
21:b92006c5b9ff
1.1.15

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