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