Azure IoT common library

Dependents:   STM32F746_iothub_client_sample_mqtt f767zi_mqtt iothub_client_sample_amqp iothub_client_sample_http ... more

Committer:
AzureIoTClient
Date:
Fri Feb 24 14:01:41 2017 -0800
Revision:
21:b92006c5b9ff
Parent:
19:2e0811512ceb
Child:
27:8656a313842b
1.1.8

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