1 /*******************************************************************************
3 * Module Name: utstrsuppt - Support functions for string-to-integer conversion
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2018, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
44 #include <acpi/acpi.h>
47 #define _COMPONENT ACPI_UTILITIES
48 ACPI_MODULE_NAME("utstrsuppt")
50 /* Local prototypes */
52 acpi_ut_insert_digit(u64
*accumulated_value
, u32 base
, int ascii_digit
);
55 acpi_ut_strtoul_multiply64(u64 multiplicand
, u32 base
, u64
*out_product
);
57 static acpi_status
acpi_ut_strtoul_add64(u64 addend1
, u32 digit
, u64
*out_sum
);
59 /*******************************************************************************
61 * FUNCTION: acpi_ut_convert_octal_string
63 * PARAMETERS: string - Null terminated input string
64 * return_value_ptr - Where the converted value is returned
66 * RETURN: Status and 64-bit converted integer
68 * DESCRIPTION: Performs a base 8 conversion of the input string to an
69 * integer value, either 32 or 64 bits.
71 * NOTE: Maximum 64-bit unsigned octal value is 01777777777777777777777
72 * Maximum 32-bit unsigned octal value is 037777777777
74 ******************************************************************************/
76 acpi_status
acpi_ut_convert_octal_string(char *string
, u64
*return_value_ptr
)
78 u64 accumulated_value
= 0;
79 acpi_status status
= AE_OK
;
81 /* Convert each ASCII byte in the input string */
85 /* Character must be ASCII 0-7, otherwise terminate with no error */
87 if (!(ACPI_IS_OCTAL_DIGIT(*string
))) {
91 /* Convert and insert this octal digit into the accumulator */
93 status
= acpi_ut_insert_digit(&accumulated_value
, 8, *string
);
94 if (ACPI_FAILURE(status
)) {
95 status
= AE_OCTAL_OVERFLOW
;
102 /* Always return the value that has been accumulated */
104 *return_value_ptr
= accumulated_value
;
108 /*******************************************************************************
110 * FUNCTION: acpi_ut_convert_decimal_string
112 * PARAMETERS: string - Null terminated input string
113 * return_value_ptr - Where the converted value is returned
115 * RETURN: Status and 64-bit converted integer
117 * DESCRIPTION: Performs a base 10 conversion of the input string to an
118 * integer value, either 32 or 64 bits.
120 * NOTE: Maximum 64-bit unsigned decimal value is 18446744073709551615
121 * Maximum 32-bit unsigned decimal value is 4294967295
123 ******************************************************************************/
125 acpi_status
acpi_ut_convert_decimal_string(char *string
, u64
*return_value_ptr
)
127 u64 accumulated_value
= 0;
128 acpi_status status
= AE_OK
;
130 /* Convert each ASCII byte in the input string */
134 /* Character must be ASCII 0-9, otherwise terminate with no error */
136 if (!isdigit(*string
)) {
140 /* Convert and insert this decimal digit into the accumulator */
142 status
= acpi_ut_insert_digit(&accumulated_value
, 10, *string
);
143 if (ACPI_FAILURE(status
)) {
144 status
= AE_DECIMAL_OVERFLOW
;
151 /* Always return the value that has been accumulated */
153 *return_value_ptr
= accumulated_value
;
157 /*******************************************************************************
159 * FUNCTION: acpi_ut_convert_hex_string
161 * PARAMETERS: string - Null terminated input string
162 * return_value_ptr - Where the converted value is returned
164 * RETURN: Status and 64-bit converted integer
166 * DESCRIPTION: Performs a base 16 conversion of the input string to an
167 * integer value, either 32 or 64 bits.
169 * NOTE: Maximum 64-bit unsigned hex value is 0xFFFFFFFFFFFFFFFF
170 * Maximum 32-bit unsigned hex value is 0xFFFFFFFF
172 ******************************************************************************/
174 acpi_status
acpi_ut_convert_hex_string(char *string
, u64
*return_value_ptr
)
176 u64 accumulated_value
= 0;
177 acpi_status status
= AE_OK
;
179 /* Convert each ASCII byte in the input string */
183 /* Must be ASCII A-F, a-f, or 0-9, otherwise terminate with no error */
185 if (!isxdigit(*string
)) {
189 /* Convert and insert this hex digit into the accumulator */
191 status
= acpi_ut_insert_digit(&accumulated_value
, 16, *string
);
192 if (ACPI_FAILURE(status
)) {
193 status
= AE_HEX_OVERFLOW
;
200 /* Always return the value that has been accumulated */
202 *return_value_ptr
= accumulated_value
;
206 /*******************************************************************************
208 * FUNCTION: acpi_ut_remove_leading_zeros
210 * PARAMETERS: string - Pointer to input ASCII string
212 * RETURN: Next character after any leading zeros. This character may be
213 * used by the caller to detect end-of-string.
215 * DESCRIPTION: Remove any leading zeros in the input string. Return the
216 * next character after the final ASCII zero to enable the caller
217 * to check for the end of the string (NULL terminator).
219 ******************************************************************************/
221 char acpi_ut_remove_leading_zeros(char **string
)
224 while (**string
== ACPI_ASCII_ZERO
) {
231 /*******************************************************************************
233 * FUNCTION: acpi_ut_remove_whitespace
235 * PARAMETERS: string - Pointer to input ASCII string
237 * RETURN: Next character after any whitespace. This character may be
238 * used by the caller to detect end-of-string.
240 * DESCRIPTION: Remove any leading whitespace in the input string. Return the
241 * next character after the final ASCII zero to enable the caller
242 * to check for the end of the string (NULL terminator).
244 ******************************************************************************/
246 char acpi_ut_remove_whitespace(char **string
)
249 while (isspace((u8
)**string
)) {
256 /*******************************************************************************
258 * FUNCTION: acpi_ut_detect_hex_prefix
260 * PARAMETERS: string - Pointer to input ASCII string
262 * RETURN: TRUE if a "0x" prefix was found at the start of the string
264 * DESCRIPTION: Detect and remove a hex "0x" prefix
266 ******************************************************************************/
268 u8
acpi_ut_detect_hex_prefix(char **string
)
271 if ((**string
== ACPI_ASCII_ZERO
) &&
272 (tolower((int)*(*string
+ 1)) == 'x')) {
273 *string
+= 2; /* Go past the leading 0x */
277 return (FALSE
); /* Not a hex string */
280 /*******************************************************************************
282 * FUNCTION: acpi_ut_detect_octal_prefix
284 * PARAMETERS: string - Pointer to input ASCII string
286 * RETURN: True if an octal "0" prefix was found at the start of the
289 * DESCRIPTION: Detect and remove an octal prefix (zero)
291 ******************************************************************************/
293 u8
acpi_ut_detect_octal_prefix(char **string
)
296 if (**string
== ACPI_ASCII_ZERO
) {
297 *string
+= 1; /* Go past the leading 0 */
301 return (FALSE
); /* Not an octal string */
304 /*******************************************************************************
306 * FUNCTION: acpi_ut_insert_digit
308 * PARAMETERS: accumulated_value - Current value of the integer value
309 * accumulator. The new value is
311 * base - Radix, either 8/10/16
312 * ascii_digit - ASCII single digit to be inserted
314 * RETURN: Status and result of the convert/insert operation. The only
315 * possible returned exception code is numeric overflow of
316 * either the multiply or add conversion operations.
318 * DESCRIPTION: Generic conversion and insertion function for all bases:
320 * 1) Multiply the current accumulated/converted value by the
321 * base in order to make room for the new character.
323 * 2) Convert the new character to binary and add it to the
324 * current accumulated value.
326 * Note: The only possible exception indicates an integer
327 * overflow (AE_NUMERIC_OVERFLOW)
329 ******************************************************************************/
332 acpi_ut_insert_digit(u64
*accumulated_value
, u32 base
, int ascii_digit
)
337 /* Make room in the accumulated value for the incoming digit */
339 status
= acpi_ut_strtoul_multiply64(*accumulated_value
, base
, &product
);
340 if (ACPI_FAILURE(status
)) {
344 /* Add in the new digit, and store the sum to the accumulated value */
347 acpi_ut_strtoul_add64(product
,
348 acpi_ut_ascii_char_to_hex(ascii_digit
),
354 /*******************************************************************************
356 * FUNCTION: acpi_ut_strtoul_multiply64
358 * PARAMETERS: multiplicand - Current accumulated converted integer
360 * out_product - Where the product is returned
362 * RETURN: Status and 64-bit product
364 * DESCRIPTION: Multiply two 64-bit values, with checking for 64-bit overflow as
365 * well as 32-bit overflow if necessary (if the current global
366 * integer width is 32).
368 ******************************************************************************/
371 acpi_ut_strtoul_multiply64(u64 multiplicand
, u32 base
, u64
*out_product
)
376 /* Exit if either operand is zero */
379 if (!multiplicand
|| !base
) {
384 * Check for 64-bit overflow before the actual multiplication.
386 * Notes: 64-bit division is often not supported on 32-bit platforms
387 * (it requires a library function), Therefore ACPICA has a local
388 * 64-bit divide function. Also, Multiplier is currently only used
389 * as the radix (8/10/16), to the 64/32 divide will always work.
391 acpi_ut_short_divide(ACPI_UINT64_MAX
, base
, "ient
, NULL
);
392 if (multiplicand
> quotient
) {
393 return (AE_NUMERIC_OVERFLOW
);
396 product
= multiplicand
* base
;
398 /* Check for 32-bit overflow if necessary */
400 if ((acpi_gbl_integer_bit_width
== 32) && (product
> ACPI_UINT32_MAX
)) {
401 return (AE_NUMERIC_OVERFLOW
);
404 *out_product
= product
;
408 /*******************************************************************************
410 * FUNCTION: acpi_ut_strtoul_add64
412 * PARAMETERS: addend1 - Current accumulated converted integer
413 * digit - New hex value/char
414 * out_sum - Where sum is returned (Accumulator)
416 * RETURN: Status and 64-bit sum
418 * DESCRIPTION: Add two 64-bit values, with checking for 64-bit overflow as
419 * well as 32-bit overflow if necessary (if the current global
420 * integer width is 32).
422 ******************************************************************************/
424 static acpi_status
acpi_ut_strtoul_add64(u64 addend1
, u32 digit
, u64
*out_sum
)
428 /* Check for 64-bit overflow before the actual addition */
430 if ((addend1
> 0) && (digit
> (ACPI_UINT64_MAX
- addend1
))) {
431 return (AE_NUMERIC_OVERFLOW
);
434 sum
= addend1
+ digit
;
436 /* Check for 32-bit overflow if necessary */
438 if ((acpi_gbl_integer_bit_width
== 32) && (sum
> ACPI_UINT32_MAX
)) {
439 return (AE_NUMERIC_OVERFLOW
);