1 /* ----> DO NOT REMOVE THE FOLLOWING NOTICE <----
\r
3 Copyright (c) 2014-2015 Datalight, Inc.
\r
4 All Rights Reserved Worldwide.
\r
6 This program is free software; you can redistribute it and/or modify
\r
7 it under the terms of the GNU General Public License as published by
\r
8 the Free Software Foundation; use version 2 of the License.
\r
10 This program is distributed in the hope that it will be useful,
\r
11 but "AS-IS," WITHOUT ANY WARRANTY; without even the implied warranty
\r
12 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 GNU General Public License for more details.
\r
15 You should have received a copy of the GNU General Public License along
\r
16 with this program; if not, write to the Free Software Foundation, Inc.,
\r
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
\r
19 /* Businesses and individuals that for commercial or other reasons cannot
\r
20 comply with the terms of the GPLv2 license may obtain a commercial license
\r
21 before incorporating Reliance Edge into proprietary software for
\r
22 distribution in any form. Visit http://www.datalight.com/reliance-edge for
\r
26 @brief Implements utilities that convert strings to numbers.
\r
29 #include <redtestutils.h>
\r
32 #define ISHEXDIGITU(c) (((c) >= 'A') && ((c) <= 'F'))
\r
33 #define ISHEXDIGITL(c) (((c) >= 'a') && ((c) <= 'f'))
\r
34 #define ISHEXDIGIT(c) (ISHEXDIGITL(c) || ISHEXDIGITU(c))
\r
37 /** @brief Converts an ASCII number into an int32_t.
\r
39 Converts all decimal digit numbers up to the end of the string or to the
\r
40 first non-numerical character.
\r
42 @note This function does *not* ignore leading white space.
\r
44 @param pszNum Pointer to a constant array of characters.
\r
46 @return The integer represented in the string.
\r
52 int32_t lNegative = 1;
\r
53 uint32_t ulIdx = 0U;
\r
55 if(pszNum[ulIdx] == '+')
\r
59 else if(pszNum[ulIdx] == '-')
\r
66 /* No sign, implicitly positive.
\r
70 while(ISDIGIT(pszNum[ulIdx]))
\r
73 lValue += pszNum[ulIdx] - '0';
\r
77 lValue *= lNegative;
\r
83 /** @brief Convert a hexadecimal ASCII number into a uint32_t value.
\r
85 The function processes all hex digits up to a NUL-terminator, or to the
\r
86 first non-hex character. Only hexadecimal digits are processed, so leading
\r
87 white space, or a leading "0x" prefix are not allowed.
\r
89 If pachNum points to an empty string (points to a NUL), this function will
\r
90 return NULL, and the value at *pulNum will not be modified.
\r
92 @note This function does not check for overflow. If there are more
\r
93 significant digits than can be represented in a uint32_t variable, the
\r
94 output is unspecified.
\r
96 @param pachNum A pointer to a constant array of hex characters.
\r
97 @param pulNum A pointer to the location in which to store the uint32_t
\r
98 result. Upon return, this value will be modified ONLY if
\r
99 the function succeeds and the returned pointer is valid (not
\r
102 @return A pointer to the byte following the converted number or NULL to
\r
105 const char *RedHtoUL(
\r
106 const char *pszNum,
\r
110 const char *pszReturn;
\r
112 pszReturn = RedHtoULL(pszNum, &ullValue);
\r
113 if(pszReturn != NULL)
\r
115 if(ullValue < UINT32_MAX)
\r
117 *pulNum = (uint32_t)ullValue;
\r
129 /** @brief Convert a hexadecimal ASCII number into a D_UINT64 value.
\r
131 The function processes all hex digits up to a NUL-terminator, or to the
\r
132 first non-hex character. Only hexadecimal digits are processed, so leading
\r
133 white space, or a leading "0x" prefix are not allowed.
\r
135 If pachNum points to an empty string (points to a NUL), this function will
\r
136 return NULL, and the value at *pulNum will not be modified.
\r
138 @note This function does not check for overflow. If there are more
\r
139 significant digits than can be represented in a uint64_t variable, the
\r
140 output is unspecified.
\r
142 @param pszNum A pointer to a constant array of hex characters.
\r
143 @param pullNum A pointer to the location in which to store the uint64_t
\r
144 result. Upon return, this value will be modified ONLY if
\r
145 the function succeeds and the returned pointer is valid (not
\r
148 @return A pointer to the byte following the converted number, or NULL to
\r
151 const char *RedHtoULL(
\r
152 const char *pszNum,
\r
155 uint64_t ullValue = 0U;
\r
156 const char *pszReturn = NULL;
\r
157 uint32_t ulIdx = 0U;
\r
159 REDASSERT(pszNum != NULL);
\r
160 REDASSERT(pullNum != NULL);
\r
162 while(pszNum[ulIdx] != '\0')
\r
164 char cDigit = pszNum[ulIdx];
\r
166 if(ISDIGIT(cDigit))
\r
170 else if(ISHEXDIGITU(cDigit))
\r
172 cDigit -= ('A' - 10);
\r
174 else if(ISHEXDIGITL(cDigit))
\r
176 cDigit -= ('a' - 10);
\r
183 REDASSERT((ullValue & UINT64_SUFFIX(0xF000000000000000)) == 0U);
\r
186 ullValue += cDigit;
\r
189 pszReturn = &pszNum[ulIdx];
\r
192 /* Modify the number returned only if we found one or more valid hex
\r
195 if(pszReturn != NULL)
\r
197 *pullNum = ullValue;
\r
204 /** @brief Convert the ASCII number to a uint32_t value.
\r
206 The number may be hex or decimal. Hex numbers must be prefixed by '0x', and
\r
207 they may be upper or lower case. The conversion process will stop with the
\r
208 first non hex or decimal digit.
\r
210 If the number is negative (the first character is a '-' sign), the value
\r
211 will be range checked and returned as the equivalent unsigned value.
\r
213 @note This function will NOT fail for numbers which exceed the size of a
\r
216 @param pszNum A pointer to the ASCII number to convert
\r
217 @param pulNum A pointer to the uint32_t location to store the result.
\r
218 This value will be modified on return only if the function
\r
219 succeeds and the returned pointer is valid (not NULL).
\r
221 @return A pointer to the byte following the converted number, or NULL to
\r
224 const char *RedNtoUL(
\r
225 const char *pszNum,
\r
228 bool fNegative = false;
\r
229 uint32_t ulIdx = 0U;
\r
230 const char *pszReturn;
\r
232 REDASSERT(pszNum != NULL);
\r
233 REDASSERT(pulNum != NULL);
\r
235 if(pszNum[ulIdx] == '-')
\r
241 /* Hex numbers must be prefixed with '0x'.
\r
243 if((pszNum[ulIdx] == '0') && ((pszNum[ulIdx + 1U] == 'x') || (pszNum[ulIdx + 1U] == 'X')))
\r
247 if(ISDIGIT(pszNum[ulIdx]) || ISHEXDIGIT(pszNum[ulIdx]))
\r
249 pszReturn = RedHtoUL(&pszNum[ulIdx], pulNum);
\r
256 else if(ISDIGIT(pszNum[ulIdx]))
\r
260 ulTemp = RedAtoI(&pszNum[ulIdx]);
\r
262 while(ISDIGIT(pszNum[ulIdx]))
\r
269 /* Fail if the number is out of range.
\r
271 if(ulTemp > INT32_MAX)
\r
277 *pulNum = -((int32_t)ulTemp);
\r
278 pszReturn = &pszNum[ulIdx];
\r
284 pszReturn = &pszNum[ulIdx];
\r
289 /* Return an error if there is not at least one hex or decimal digit.
\r
298 /** @brief Convert the ASCII number pointed to by pachNum to a uint64_t value.
\r
300 The number may be hex or decimal. Hex numbers must be prefixed by '0x', and
\r
301 they may be upper or lower case. The conversion process will stop with the
\r
302 first non hex or decimal digit.
\r
304 If the number is negative (the first character is a '-' sign), the value
\r
305 will be range checked and returned as the equivalent unsigned value.
\r
307 @param pszNum A pointer to the ASCII number to convert.
\r
308 @param pullNum A pointer to the uint64_t location to store the result.
\r
309 This value will be modified on return only if the function
\r
310 succeeds and the returned pointer is valid (not NULL).
\r
312 @return A pointer to the byte following the converted number, or NULL to
\r
315 const char *RedNtoULL(
\r
316 const char *pszNum,
\r
319 bool fNegative = false;
\r
320 uint32_t ulIdx = 0U;
\r
321 const char *pszReturn;
\r
323 REDASSERT(pszNum != NULL);
\r
324 REDASSERT(pullNum != NULL);
\r
326 if(pszNum[ulIdx] == '-')
\r
332 /* Hex numbers must be prefixed with '0x'.
\r
334 if((pszNum[ulIdx] == '0') && ((pszNum[ulIdx + 1U] == 'x') || (pszNum[ulIdx + 1U] == 'X')))
\r
338 if(ISDIGIT(pszNum[ulIdx]) || ISHEXDIGIT(pszNum[ulIdx]))
\r
340 pszReturn = RedHtoULL(&pszNum[ulIdx], pullNum);
\r
347 else if(ISDIGIT(pszNum[ulIdx]))
\r
349 uint64_t ullTemp = 0U;
\r
351 while(ISDIGIT(pszNum[ulIdx]))
\r
354 ullTemp += pszNum[ulIdx] - '0';
\r
360 /* Fail if the number is out of range.
\r
362 if(ullTemp > INT64_MAX)
\r
368 *pullNum = (uint64_t)(-((int64_t)ullTemp));
\r
369 pszReturn = &pszNum[ulIdx];
\r
374 *pullNum = ullTemp;
\r
375 pszReturn = &pszNum[ulIdx];
\r
380 /* Return an error if there is not at least one hex or decimal digit.
\r
389 /** @brief Convert an ASCII hex or decimal number, which may may have a "B",
\r
390 "KB", or "MB" suffix (case insensitive), to a binary value.
\r
392 Hex numbers must be prefixed with "0x".
\r
394 @note If there is no postfix, KB is assumed!
\r
396 May fail due to bad formatting or overflow.
\r
398 @param pszNum A pointer to the ASCII number to convert.
\r
399 @param pulResult A pointer to a uint32_t in which to place the result.
\r
401 @return A pointer to the byte following the string, or NULL to indicate an
\r
402 error. In the event of an error, *pulResult will not be modified.
\r
404 const char *RedSizeToUL(
\r
405 const char *pszNum,
\r
406 uint32_t *pulResult)
\r
409 const char *pszSuffix;
\r
410 const char *pszReturn;
\r
411 uint32_t ulIdx = 0U;
\r
413 REDASSERT(pszNum != NULL);
\r
414 REDASSERT(pulResult != NULL);
\r
416 /* Do the basic hex/decimal conversion
\r
418 pszSuffix = RedNtoUL(pszNum, &ulResult);
\r
419 if(pszSuffix != NULL)
\r
421 if((pszSuffix[ulIdx] == 'B') || (pszSuffix[ulIdx] == 'b'))
\r
424 pszReturn = &pszSuffix[ulIdx];
\r
426 else if( ((pszSuffix[ulIdx] == 'M') || (pszSuffix[ulIdx] == 'm'))
\r
427 && ((pszSuffix[ulIdx + 1U] == 'B') || (pszSuffix[ulIdx + 1U] == 'b')))
\r
431 if(ulResult > (UINT32_MAX / (1024U * 1024U)))
\r
437 ulResult *= 1024U * 1024U;
\r
438 pszReturn = &pszSuffix[ulIdx];
\r
443 /* The number is either postfixed with "KB" or something
\r
444 else (we don't care), but we must increment the pointer
\r
445 if it is something recognize.
\r
447 if( ((pszSuffix[ulIdx] == 'K') || (pszSuffix[ulIdx] == 'k'))
\r
448 && ((pszSuffix[ulIdx + 1U] == 'B') || (pszSuffix[ulIdx + 1U] == 'b')))
\r
453 /* "B" or "MB" were not specified, so it must be "KB"
\r
455 if(ulResult > (UINT32_MAX / 1024U))
\r
461 ulResult *= 1024UL;
\r
462 pszReturn = &pszSuffix[ulIdx];
\r
466 if(pszReturn != NULL)
\r
468 *pulResult = ulResult;
\r