Azure IoT common library
Dependents: STM32F746_iothub_client_sample_mqtt f767zi_mqtt iothub_client_sample_amqp iothub_client_sample_http ... more
crt_abstractions.c@6:c55b013dfc2a, 2016-07-01 (annotated)
- Committer:
- Azure.IoT Build
- Date:
- Fri Jul 01 10:43:23 2016 -0700
- Revision:
- 6:c55b013dfc2a
- Parent:
- 0:fa2de1b79154
- Child:
- 7:1af47e3a19b6
1.0.10
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Azure.IoT Build | 0:fa2de1b79154 | 1 | // Copyright (c) Microsoft. All rights reserved. |
Azure.IoT Build | 0:fa2de1b79154 | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
Azure.IoT Build | 0:fa2de1b79154 | 3 | |
Azure.IoT Build | 0:fa2de1b79154 | 4 | #define __STDC_WANT_LIB_EXT1__ 1 |
Azure.IoT Build | 0:fa2de1b79154 | 5 | |
Azure.IoT Build | 0:fa2de1b79154 | 6 | #include <stdlib.h> |
Azure.IoT Build | 0:fa2de1b79154 | 7 | #ifdef _CRTDBG_MAP_ALLOC |
Azure.IoT Build | 0:fa2de1b79154 | 8 | #include <crtdbg.h> |
Azure.IoT Build | 0:fa2de1b79154 | 9 | #endif |
Azure.IoT Build | 0:fa2de1b79154 | 10 | #include "azure_c_shared_utility/gballoc.h" |
Azure.IoT Build | 0:fa2de1b79154 | 11 | |
Azure.IoT Build | 0:fa2de1b79154 | 12 | #include "azure_c_shared_utility/crt_abstractions.h" |
Azure.IoT Build | 0:fa2de1b79154 | 13 | #include "errno.h" |
Azure.IoT Build | 0:fa2de1b79154 | 14 | #include <stddef.h> |
Azure.IoT Build | 0:fa2de1b79154 | 15 | #include <limits.h> |
Azure.IoT Build | 0:fa2de1b79154 | 16 | |
Azure.IoT Build | 0:fa2de1b79154 | 17 | #ifdef _MSC_VER |
Azure.IoT Build | 0:fa2de1b79154 | 18 | #else |
Azure.IoT Build | 0:fa2de1b79154 | 19 | |
Azure.IoT Build | 0:fa2de1b79154 | 20 | #include <stdarg.h> |
Azure.IoT Build | 0:fa2de1b79154 | 21 | |
Azure.IoT Build | 0:fa2de1b79154 | 22 | /*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 | 23 | int strcat_s(char* dst, size_t dstSizeInBytes, const char* src) |
Azure.IoT Build | 0:fa2de1b79154 | 24 | { |
Azure.IoT Build | 0:fa2de1b79154 | 25 | int result; |
Azure.IoT Build | 0:fa2de1b79154 | 26 | /*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 | 27 | if (dst == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 28 | { |
Azure.IoT Build | 0:fa2de1b79154 | 29 | result = EINVAL; |
Azure.IoT Build | 0:fa2de1b79154 | 30 | } |
Azure.IoT Build | 0:fa2de1b79154 | 31 | /*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 | 32 | else if (src == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 33 | { |
Azure.IoT Build | 0:fa2de1b79154 | 34 | dst[0] = '\0'; |
Azure.IoT Build | 0:fa2de1b79154 | 35 | result = EINVAL; |
Azure.IoT Build | 0:fa2de1b79154 | 36 | } |
Azure.IoT Build | 0:fa2de1b79154 | 37 | else |
Azure.IoT Build | 0:fa2de1b79154 | 38 | { |
Azure.IoT Build | 0:fa2de1b79154 | 39 | /*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 | 40 | if (dstSizeInBytes == 0) |
Azure.IoT Build | 0:fa2de1b79154 | 41 | { |
Azure.IoT Build | 0:fa2de1b79154 | 42 | result = ERANGE; |
Azure.IoT Build | 0:fa2de1b79154 | 43 | dst[0] = '\0'; |
Azure.IoT Build | 0:fa2de1b79154 | 44 | } |
Azure.IoT Build | 0:fa2de1b79154 | 45 | else |
Azure.IoT Build | 0:fa2de1b79154 | 46 | { |
Azure.IoT Build | 0:fa2de1b79154 | 47 | size_t dstStrLen = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 48 | #ifdef __STDC_LIB_EXT1__ |
Azure.IoT Build | 0:fa2de1b79154 | 49 | dstStrLen = strnlen_s(dst, dstSizeInBytes); |
Azure.IoT Build | 0:fa2de1b79154 | 50 | #else |
Azure.IoT Build | 0:fa2de1b79154 | 51 | size_t i; |
Azure.IoT Build | 0:fa2de1b79154 | 52 | for(i=0; (i < dstSizeInBytes) && (dst[i]!= '\0'); i++) |
Azure.IoT Build | 0:fa2de1b79154 | 53 | { |
Azure.IoT Build | 0:fa2de1b79154 | 54 | } |
Azure.IoT Build | 0:fa2de1b79154 | 55 | dstStrLen = i; |
Azure.IoT Build | 0:fa2de1b79154 | 56 | #endif |
Azure.IoT Build | 0:fa2de1b79154 | 57 | /*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 | 58 | if (dstSizeInBytes == dstStrLen) /* this means the dst string is not terminated*/ |
Azure.IoT Build | 0:fa2de1b79154 | 59 | { |
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_009: [The initial character of src shall overwrite the terminating null character of dst.]*/ |
Azure.IoT Build | 0:fa2de1b79154 | 65 | (void)strncpy(&dst[dstStrLen], src, dstSizeInBytes - dstStrLen); |
Azure.IoT Build | 0:fa2de1b79154 | 66 | /*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 | 67 | if (dst[dstSizeInBytes-1] != '\0') |
Azure.IoT Build | 0:fa2de1b79154 | 68 | { |
Azure.IoT Build | 0:fa2de1b79154 | 69 | dst[0] = '\0'; |
Azure.IoT Build | 0:fa2de1b79154 | 70 | result = ERANGE; |
Azure.IoT Build | 0:fa2de1b79154 | 71 | } |
Azure.IoT Build | 0:fa2de1b79154 | 72 | else |
Azure.IoT Build | 0:fa2de1b79154 | 73 | { |
Azure.IoT Build | 0:fa2de1b79154 | 74 | /*Codes_SRS_CRT_ABSTRACTIONS_99_003: [strcat_s shall return Zero upon success.]*/ |
Azure.IoT Build | 0:fa2de1b79154 | 75 | result = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 76 | } |
Azure.IoT Build | 0:fa2de1b79154 | 77 | } |
Azure.IoT Build | 0:fa2de1b79154 | 78 | } |
Azure.IoT Build | 0:fa2de1b79154 | 79 | } |
Azure.IoT Build | 0:fa2de1b79154 | 80 | |
Azure.IoT Build | 0:fa2de1b79154 | 81 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 82 | } |
Azure.IoT Build | 0:fa2de1b79154 | 83 | |
Azure.IoT Build | 0:fa2de1b79154 | 84 | /*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 | 85 | int strncpy_s(char* dst, size_t dstSizeInBytes, const char* src, size_t maxCount) |
Azure.IoT Build | 0:fa2de1b79154 | 86 | { |
Azure.IoT Build | 0:fa2de1b79154 | 87 | int result; |
Azure.IoT Build | 0:fa2de1b79154 | 88 | int truncationFlag = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 89 | /*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 | 90 | if (dst == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 91 | { |
Azure.IoT Build | 0:fa2de1b79154 | 92 | result = EINVAL; |
Azure.IoT Build | 0:fa2de1b79154 | 93 | } |
Azure.IoT Build | 0:fa2de1b79154 | 94 | /*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 | 95 | else if (src == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 96 | { |
Azure.IoT Build | 0:fa2de1b79154 | 97 | dst[0] = '\0'; |
Azure.IoT Build | 0:fa2de1b79154 | 98 | result = EINVAL; |
Azure.IoT Build | 0:fa2de1b79154 | 99 | } |
Azure.IoT Build | 0:fa2de1b79154 | 100 | /*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 | 101 | else if (dstSizeInBytes == 0) |
Azure.IoT Build | 0:fa2de1b79154 | 102 | { |
Azure.IoT Build | 0:fa2de1b79154 | 103 | result = EINVAL; |
Azure.IoT Build | 0:fa2de1b79154 | 104 | } |
Azure.IoT Build | 0:fa2de1b79154 | 105 | else |
Azure.IoT Build | 0:fa2de1b79154 | 106 | { |
Azure.IoT Build | 0:fa2de1b79154 | 107 | size_t srcLength = strlen(src); |
Azure.IoT Build | 0:fa2de1b79154 | 108 | if (maxCount != _TRUNCATE) |
Azure.IoT Build | 0:fa2de1b79154 | 109 | { |
Azure.IoT Build | 0:fa2de1b79154 | 110 | /*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 | 111 | if (srcLength > maxCount) |
Azure.IoT Build | 0:fa2de1b79154 | 112 | { |
Azure.IoT Build | 0:fa2de1b79154 | 113 | srcLength = maxCount; |
Azure.IoT Build | 0:fa2de1b79154 | 114 | } |
Azure.IoT Build | 0:fa2de1b79154 | 115 | |
Azure.IoT Build | 0:fa2de1b79154 | 116 | /*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 | 117 | if (srcLength + 1 > dstSizeInBytes) |
Azure.IoT Build | 0:fa2de1b79154 | 118 | { |
Azure.IoT Build | 0:fa2de1b79154 | 119 | dst[0] = '\0'; |
Azure.IoT Build | 0:fa2de1b79154 | 120 | result = ERANGE; |
Azure.IoT Build | 0:fa2de1b79154 | 121 | } |
Azure.IoT Build | 0:fa2de1b79154 | 122 | else |
Azure.IoT Build | 0:fa2de1b79154 | 123 | { |
Azure.IoT Build | 0:fa2de1b79154 | 124 | (void)strncpy(dst, src, srcLength); |
Azure.IoT Build | 0:fa2de1b79154 | 125 | dst[srcLength] = '\0'; |
Azure.IoT Build | 0:fa2de1b79154 | 126 | /*Codes_SRS_CRT_ABSTRACTIONS_99_018: [strncpy_s shall return Zero upon success]*/ |
Azure.IoT Build | 0:fa2de1b79154 | 127 | result = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 128 | } |
Azure.IoT Build | 0:fa2de1b79154 | 129 | } |
Azure.IoT Build | 0:fa2de1b79154 | 130 | /*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 | 131 | else |
Azure.IoT Build | 0:fa2de1b79154 | 132 | { |
Azure.IoT Build | 0:fa2de1b79154 | 133 | if (srcLength + 1 > dstSizeInBytes ) |
Azure.IoT Build | 0:fa2de1b79154 | 134 | { |
Azure.IoT Build | 0:fa2de1b79154 | 135 | srcLength = dstSizeInBytes - 1; |
Azure.IoT Build | 0:fa2de1b79154 | 136 | truncationFlag = 1; |
Azure.IoT Build | 0:fa2de1b79154 | 137 | } |
Azure.IoT Build | 0:fa2de1b79154 | 138 | (void)strncpy(dst, src, srcLength); |
Azure.IoT Build | 0:fa2de1b79154 | 139 | dst[srcLength] = '\0'; |
Azure.IoT Build | 0:fa2de1b79154 | 140 | result = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 141 | } |
Azure.IoT Build | 0:fa2de1b79154 | 142 | } |
Azure.IoT Build | 0:fa2de1b79154 | 143 | |
Azure.IoT Build | 0:fa2de1b79154 | 144 | /*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 | 145 | if (truncationFlag == 1) |
Azure.IoT Build | 0:fa2de1b79154 | 146 | { |
Azure.IoT Build | 0:fa2de1b79154 | 147 | result = STRUNCATE; |
Azure.IoT Build | 0:fa2de1b79154 | 148 | } |
Azure.IoT Build | 0:fa2de1b79154 | 149 | |
Azure.IoT Build | 0:fa2de1b79154 | 150 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 151 | } |
Azure.IoT Build | 0:fa2de1b79154 | 152 | |
Azure.IoT Build | 0:fa2de1b79154 | 153 | /* 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 | 154 | int strcpy_s(char* dst, size_t dstSizeInBytes, const char* src) |
Azure.IoT Build | 0:fa2de1b79154 | 155 | { |
Azure.IoT Build | 0:fa2de1b79154 | 156 | int result; |
Azure.IoT Build | 0:fa2de1b79154 | 157 | |
Azure.IoT Build | 0:fa2de1b79154 | 158 | /* 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 | 159 | if (dst == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 160 | { |
Azure.IoT Build | 0:fa2de1b79154 | 161 | result = EINVAL; |
Azure.IoT Build | 0:fa2de1b79154 | 162 | } |
Azure.IoT Build | 0:fa2de1b79154 | 163 | /* 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 | 164 | else if (src == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 165 | { |
Azure.IoT Build | 0:fa2de1b79154 | 166 | dst[0] = '\0'; |
Azure.IoT Build | 0:fa2de1b79154 | 167 | result = EINVAL; |
Azure.IoT Build | 0:fa2de1b79154 | 168 | } |
Azure.IoT Build | 0:fa2de1b79154 | 169 | /* 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 | 170 | else if (dstSizeInBytes == 0) |
Azure.IoT Build | 0:fa2de1b79154 | 171 | { |
Azure.IoT Build | 0:fa2de1b79154 | 172 | dst[0] = '\0'; |
Azure.IoT Build | 0:fa2de1b79154 | 173 | result = ERANGE; |
Azure.IoT Build | 0:fa2de1b79154 | 174 | } |
Azure.IoT Build | 0:fa2de1b79154 | 175 | else |
Azure.IoT Build | 0:fa2de1b79154 | 176 | { |
Azure.IoT Build | 0:fa2de1b79154 | 177 | size_t neededBuffer = strlen(src); |
Azure.IoT Build | 0:fa2de1b79154 | 178 | /* 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 | 179 | if (neededBuffer + 1 > dstSizeInBytes) |
Azure.IoT Build | 0:fa2de1b79154 | 180 | { |
Azure.IoT Build | 0:fa2de1b79154 | 181 | dst[0] = '\0'; |
Azure.IoT Build | 0:fa2de1b79154 | 182 | result = ERANGE; |
Azure.IoT Build | 0:fa2de1b79154 | 183 | } |
Azure.IoT Build | 0:fa2de1b79154 | 184 | else |
Azure.IoT Build | 0:fa2de1b79154 | 185 | { |
Azure.IoT Build | 0:fa2de1b79154 | 186 | memcpy(dst, src, neededBuffer + 1); |
Azure.IoT Build | 0:fa2de1b79154 | 187 | /*Codes_SRS_CRT_ABSTRACTIONS_99_011: [strcpy_s shall return Zero upon success]*/ |
Azure.IoT Build | 0:fa2de1b79154 | 188 | result = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 189 | } |
Azure.IoT Build | 0:fa2de1b79154 | 190 | } |
Azure.IoT Build | 0:fa2de1b79154 | 191 | |
Azure.IoT Build | 0:fa2de1b79154 | 192 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 193 | } |
Azure.IoT Build | 0:fa2de1b79154 | 194 | |
Azure.IoT Build | 0:fa2de1b79154 | 195 | /*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 | 196 | /*Codes_SRS_CRT_ABSTRACTIONS_99_031: [A null character is appended after the last character written.]*/ |
Azure.IoT Build | 0:fa2de1b79154 | 197 | int sprintf_s(char* dst, size_t dstSizeInBytes, const char* format, ...) |
Azure.IoT Build | 0:fa2de1b79154 | 198 | { |
Azure.IoT Build | 0:fa2de1b79154 | 199 | int result; |
Azure.IoT Build | 0:fa2de1b79154 | 200 | /*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 | 201 | if ((dst == NULL) || |
Azure.IoT Build | 0:fa2de1b79154 | 202 | (format == NULL)) |
Azure.IoT Build | 0:fa2de1b79154 | 203 | { |
Azure.IoT Build | 0:fa2de1b79154 | 204 | errno = EINVAL; |
Azure.IoT Build | 0:fa2de1b79154 | 205 | result = -1; |
Azure.IoT Build | 0:fa2de1b79154 | 206 | } |
Azure.IoT Build | 0:fa2de1b79154 | 207 | else |
Azure.IoT Build | 0:fa2de1b79154 | 208 | { |
Azure.IoT Build | 0:fa2de1b79154 | 209 | /*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 | 210 | |
Azure.IoT Build | 0:fa2de1b79154 | 211 | #if defined _MSC_VER |
Azure.IoT Build | 0:fa2de1b79154 | 212 | #error crt_abstractions is not provided for Microsoft Compilers |
Azure.IoT Build | 0:fa2de1b79154 | 213 | #else |
Azure.IoT Build | 0:fa2de1b79154 | 214 | /*not Microsoft compiler... */ |
Azure.IoT Build | 0:fa2de1b79154 | 215 | #if defined (__STDC_VERSION__) || (__cplusplus) |
Azure.IoT Build | 0:fa2de1b79154 | 216 | #if ( \ |
Azure.IoT Build | 0:fa2de1b79154 | 217 | ((__STDC_VERSION__ == 199901L) || (__STDC_VERSION__ == 201000L) || (__STDC_VERSION__ == 201112L)) || \ |
Azure.IoT Build | 0:fa2de1b79154 | 218 | (defined __cplusplus) \ |
Azure.IoT Build | 0:fa2de1b79154 | 219 | ) |
Azure.IoT Build | 0:fa2de1b79154 | 220 | /*C99 compiler*/ |
Azure.IoT Build | 0:fa2de1b79154 | 221 | va_list args; |
Azure.IoT Build | 0:fa2de1b79154 | 222 | va_start(args, format); |
Azure.IoT Build | 0:fa2de1b79154 | 223 | /*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 | 224 | result = vsnprintf(dst, dstSizeInBytes, format, args); |
Azure.IoT Build | 0:fa2de1b79154 | 225 | va_end(args); |
Azure.IoT Build | 0:fa2de1b79154 | 226 | |
Azure.IoT Build | 0:fa2de1b79154 | 227 | /*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 | 228 | if (result < 0) |
Azure.IoT Build | 0:fa2de1b79154 | 229 | { |
Azure.IoT Build | 0:fa2de1b79154 | 230 | result = -1; |
Azure.IoT Build | 0:fa2de1b79154 | 231 | } |
Azure.IoT Build | 0:fa2de1b79154 | 232 | else if ((size_t)result >= dstSizeInBytes) |
Azure.IoT Build | 0:fa2de1b79154 | 233 | { |
Azure.IoT Build | 0:fa2de1b79154 | 234 | /*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 | 235 | dst[0] = '\0'; |
Azure.IoT Build | 0:fa2de1b79154 | 236 | result = -1; |
Azure.IoT Build | 0:fa2de1b79154 | 237 | } |
Azure.IoT Build | 0:fa2de1b79154 | 238 | else |
Azure.IoT Build | 0:fa2de1b79154 | 239 | { |
Azure.IoT Build | 0:fa2de1b79154 | 240 | /*do nothing, all is fine*/ |
Azure.IoT Build | 0:fa2de1b79154 | 241 | } |
Azure.IoT Build | 0:fa2de1b79154 | 242 | #else |
Azure.IoT Build | 0:fa2de1b79154 | 243 | #error STDC_VERSION defined, but of unknown value; unable to sprinf_s, or provide own implementation |
Azure.IoT Build | 0:fa2de1b79154 | 244 | #endif |
Azure.IoT Build | 0:fa2de1b79154 | 245 | #else |
Azure.IoT Build | 0:fa2de1b79154 | 246 | #error for STDC_VERSION undefined (assumed C89), provide own implementation of sprintf_s |
Azure.IoT Build | 0:fa2de1b79154 | 247 | #endif |
Azure.IoT Build | 0:fa2de1b79154 | 248 | #endif |
Azure.IoT Build | 0:fa2de1b79154 | 249 | } |
Azure.IoT Build | 0:fa2de1b79154 | 250 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 251 | } |
Azure.IoT Build | 0:fa2de1b79154 | 252 | #endif /* _MSC_VER */ |
Azure.IoT Build | 0:fa2de1b79154 | 253 | |
Azure.IoT Build | 0:fa2de1b79154 | 254 | /*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 | 255 | int mallocAndStrcpy_s(char** destination, const char* source) |
Azure.IoT Build | 0:fa2de1b79154 | 256 | { |
Azure.IoT Build | 0:fa2de1b79154 | 257 | int result; |
Azure.IoT Build | 0:fa2de1b79154 | 258 | /*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 | 259 | if ((destination == NULL) || (source == NULL)) |
Azure.IoT Build | 0:fa2de1b79154 | 260 | { |
Azure.IoT Build | 0:fa2de1b79154 | 261 | /*If strDestination or strSource is a null pointer[...]these functions return EINVAL */ |
Azure.IoT Build | 0:fa2de1b79154 | 262 | result = EINVAL; |
Azure.IoT Build | 0:fa2de1b79154 | 263 | } |
Azure.IoT Build | 0:fa2de1b79154 | 264 | else |
Azure.IoT Build | 0:fa2de1b79154 | 265 | { |
Azure.IoT Build | 0:fa2de1b79154 | 266 | size_t l = strlen(source); |
Azure.IoT Build | 6:c55b013dfc2a | 267 | char* temp = (char*)malloc(l + 1); |
Azure.IoT Build | 6:c55b013dfc2a | 268 | |
Azure.IoT Build | 0:fa2de1b79154 | 269 | /*Codes_SRS_CRT_ABSTRACTIONS_99_037: [Upon failure to allocate memory for the destination, the function will return ENOMEM.]*/ |
Azure.IoT Build | 6:c55b013dfc2a | 270 | if (temp == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 271 | { |
Azure.IoT Build | 0:fa2de1b79154 | 272 | result = ENOMEM; |
Azure.IoT Build | 0:fa2de1b79154 | 273 | } |
Azure.IoT Build | 0:fa2de1b79154 | 274 | else |
Azure.IoT Build | 0:fa2de1b79154 | 275 | { |
Azure.IoT Build | 6:c55b013dfc2a | 276 | *destination = temp; |
Azure.IoT Build | 0:fa2de1b79154 | 277 | /*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.]*/ |
Azure.IoT Build | 0:fa2de1b79154 | 278 | int temp = strcpy_s(*destination, l + 1, source); |
Azure.IoT Build | 0:fa2de1b79154 | 279 | if (temp < 0) /*strcpy_s error*/ |
Azure.IoT Build | 0:fa2de1b79154 | 280 | { |
Azure.IoT Build | 0:fa2de1b79154 | 281 | free(*destination); |
Azure.IoT Build | 0:fa2de1b79154 | 282 | *destination = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 283 | result = temp; |
Azure.IoT Build | 0:fa2de1b79154 | 284 | } |
Azure.IoT Build | 0:fa2de1b79154 | 285 | else |
Azure.IoT Build | 0:fa2de1b79154 | 286 | { |
Azure.IoT Build | 0:fa2de1b79154 | 287 | /*Codes_SRS_CRT_ABSTRACTIONS_99_035: [mallocAndstrcpy_s shall return Zero upon success]*/ |
Azure.IoT Build | 0:fa2de1b79154 | 288 | result = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 289 | } |
Azure.IoT Build | 0:fa2de1b79154 | 290 | } |
Azure.IoT Build | 0:fa2de1b79154 | 291 | } |
Azure.IoT Build | 0:fa2de1b79154 | 292 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 293 | } |
Azure.IoT Build | 0:fa2de1b79154 | 294 | |
Azure.IoT Build | 0:fa2de1b79154 | 295 | /*takes "value" and transforms it into a decimal string*/ |
Azure.IoT Build | 0:fa2de1b79154 | 296 | /*10 => "10"*/ |
Azure.IoT Build | 0:fa2de1b79154 | 297 | /*return 0 when everything went ok*/ |
Azure.IoT Build | 0:fa2de1b79154 | 298 | /*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 | 299 | int unsignedIntToString(char* destination, size_t destinationSize, unsigned int value) |
Azure.IoT Build | 0:fa2de1b79154 | 300 | { |
Azure.IoT Build | 0:fa2de1b79154 | 301 | int result; |
Azure.IoT Build | 0:fa2de1b79154 | 302 | size_t pos; |
Azure.IoT Build | 0:fa2de1b79154 | 303 | /*the below loop gets the number in reverse order*/ |
Azure.IoT Build | 0:fa2de1b79154 | 304 | /*Codes_SRS_CRT_ABSTRACTIONS_02_003: [If destination is NULL then unsignedIntToString shall fail.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 305 | /*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 | 306 | if ( |
Azure.IoT Build | 0:fa2de1b79154 | 307 | (destination == NULL) || |
Azure.IoT Build | 0:fa2de1b79154 | 308 | (destinationSize < 2) /*because the smallest number is '0\0' which requires 2 characters*/ |
Azure.IoT Build | 0:fa2de1b79154 | 309 | ) |
Azure.IoT Build | 0:fa2de1b79154 | 310 | { |
Azure.IoT Build | 0:fa2de1b79154 | 311 | result = __LINE__; |
Azure.IoT Build | 0:fa2de1b79154 | 312 | } |
Azure.IoT Build | 0:fa2de1b79154 | 313 | else |
Azure.IoT Build | 0:fa2de1b79154 | 314 | { |
Azure.IoT Build | 0:fa2de1b79154 | 315 | pos = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 316 | do |
Azure.IoT Build | 0:fa2de1b79154 | 317 | { |
Azure.IoT Build | 0:fa2de1b79154 | 318 | destination[pos++] = '0' + (value % 10); |
Azure.IoT Build | 0:fa2de1b79154 | 319 | value /= 10; |
Azure.IoT Build | 0:fa2de1b79154 | 320 | } while ((value > 0) && (pos < (destinationSize-1))); |
Azure.IoT Build | 0:fa2de1b79154 | 321 | |
Azure.IoT Build | 0:fa2de1b79154 | 322 | if (value == 0) |
Azure.IoT Build | 0:fa2de1b79154 | 323 | { |
Azure.IoT Build | 0:fa2de1b79154 | 324 | size_t w; |
Azure.IoT Build | 0:fa2de1b79154 | 325 | destination[pos] = '\0'; |
Azure.IoT Build | 0:fa2de1b79154 | 326 | /*all converted and they fit*/ |
Azure.IoT Build | 0:fa2de1b79154 | 327 | for (w = 0; w <= (pos-1) >> 1; w++) |
Azure.IoT Build | 0:fa2de1b79154 | 328 | { |
Azure.IoT Build | 0:fa2de1b79154 | 329 | char temp; |
Azure.IoT Build | 0:fa2de1b79154 | 330 | temp = destination[w]; |
Azure.IoT Build | 0:fa2de1b79154 | 331 | destination[w] = destination[pos - 1 - w]; |
Azure.IoT Build | 0:fa2de1b79154 | 332 | destination[pos -1 - w] = temp; |
Azure.IoT Build | 0:fa2de1b79154 | 333 | } |
Azure.IoT Build | 0:fa2de1b79154 | 334 | /*Codes_SRS_CRT_ABSTRACTIONS_02_004: [If the conversion has been successfull then unsignedIntToString shall return 0.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 335 | result = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 336 | } |
Azure.IoT Build | 0:fa2de1b79154 | 337 | else |
Azure.IoT Build | 0:fa2de1b79154 | 338 | { |
Azure.IoT Build | 0:fa2de1b79154 | 339 | /*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 | 340 | result = __LINE__; |
Azure.IoT Build | 0:fa2de1b79154 | 341 | } |
Azure.IoT Build | 0:fa2de1b79154 | 342 | } |
Azure.IoT Build | 0:fa2de1b79154 | 343 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 344 | } |
Azure.IoT Build | 0:fa2de1b79154 | 345 | |
Azure.IoT Build | 0:fa2de1b79154 | 346 | /*takes "value" and transforms it into a decimal string*/ |
Azure.IoT Build | 0:fa2de1b79154 | 347 | /*10 => "10"*/ |
Azure.IoT Build | 0:fa2de1b79154 | 348 | /*return 0 when everything went ok*/ |
Azure.IoT Build | 0:fa2de1b79154 | 349 | /*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 | 350 | int size_tToString(char* destination, size_t destinationSize, size_t value) |
Azure.IoT Build | 0:fa2de1b79154 | 351 | { |
Azure.IoT Build | 0:fa2de1b79154 | 352 | int result; |
Azure.IoT Build | 0:fa2de1b79154 | 353 | size_t pos; |
Azure.IoT Build | 0:fa2de1b79154 | 354 | /*the below loop gets the number in reverse order*/ |
Azure.IoT Build | 0:fa2de1b79154 | 355 | /*Codes_SRS_CRT_ABSTRACTIONS_02_003: [If destination is NULL then unsignedIntToString shall fail.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 356 | /*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 | 357 | if ( |
Azure.IoT Build | 0:fa2de1b79154 | 358 | (destination == NULL) || |
Azure.IoT Build | 0:fa2de1b79154 | 359 | (destinationSize < 2) /*because the smallest number is '0\0' which requires 2 characters*/ |
Azure.IoT Build | 0:fa2de1b79154 | 360 | ) |
Azure.IoT Build | 0:fa2de1b79154 | 361 | { |
Azure.IoT Build | 0:fa2de1b79154 | 362 | result = __LINE__; |
Azure.IoT Build | 0:fa2de1b79154 | 363 | } |
Azure.IoT Build | 0:fa2de1b79154 | 364 | else |
Azure.IoT Build | 0:fa2de1b79154 | 365 | { |
Azure.IoT Build | 0:fa2de1b79154 | 366 | pos = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 367 | do |
Azure.IoT Build | 0:fa2de1b79154 | 368 | { |
Azure.IoT Build | 0:fa2de1b79154 | 369 | destination[pos++] = '0' + (value % 10); |
Azure.IoT Build | 0:fa2de1b79154 | 370 | value /= 10; |
Azure.IoT Build | 0:fa2de1b79154 | 371 | } while ((value > 0) && (pos < (destinationSize - 1))); |
Azure.IoT Build | 0:fa2de1b79154 | 372 | |
Azure.IoT Build | 0:fa2de1b79154 | 373 | if (value == 0) |
Azure.IoT Build | 0:fa2de1b79154 | 374 | { |
Azure.IoT Build | 0:fa2de1b79154 | 375 | size_t w; |
Azure.IoT Build | 0:fa2de1b79154 | 376 | destination[pos] = '\0'; |
Azure.IoT Build | 0:fa2de1b79154 | 377 | /*all converted and they fit*/ |
Azure.IoT Build | 0:fa2de1b79154 | 378 | for (w = 0; w <= (pos - 1) >> 1; w++) |
Azure.IoT Build | 0:fa2de1b79154 | 379 | { |
Azure.IoT Build | 0:fa2de1b79154 | 380 | char temp; |
Azure.IoT Build | 0:fa2de1b79154 | 381 | temp = destination[w]; |
Azure.IoT Build | 0:fa2de1b79154 | 382 | destination[w] = destination[pos - 1 - w]; |
Azure.IoT Build | 0:fa2de1b79154 | 383 | destination[pos - 1 - w] = temp; |
Azure.IoT Build | 0:fa2de1b79154 | 384 | } |
Azure.IoT Build | 0:fa2de1b79154 | 385 | /*Codes_SRS_CRT_ABSTRACTIONS_02_004: [If the conversion has been successfull then unsignedIntToString shall return 0.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 386 | result = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 387 | } |
Azure.IoT Build | 0:fa2de1b79154 | 388 | else |
Azure.IoT Build | 0:fa2de1b79154 | 389 | { |
Azure.IoT Build | 0:fa2de1b79154 | 390 | /*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 | 391 | result = __LINE__; |
Azure.IoT Build | 0:fa2de1b79154 | 392 | } |
Azure.IoT Build | 0:fa2de1b79154 | 393 | } |
Azure.IoT Build | 0:fa2de1b79154 | 394 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 395 | } |