1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /*******************************************************************************
4 * Module Name: utstrsuppt - Support functions for string-to-integer conversion
6 ******************************************************************************/
11 #define _COMPONENT ACPI_UTILITIES
12 ACPI_MODULE_NAME("utstrsuppt")
14 /* Local prototypes */
16 acpi_ut_insert_digit(u64
*accumulated_value
, u32 base
, int ascii_digit
);
19 acpi_ut_strtoul_multiply64(u64 multiplicand
, u32 base
, u64
*out_product
);
21 static acpi_status
acpi_ut_strtoul_add64(u64 addend1
, u32 digit
, u64
*out_sum
);
23 /*******************************************************************************
25 * FUNCTION: acpi_ut_convert_octal_string
27 * PARAMETERS: string - Null terminated input string
28 * return_value_ptr - Where the converted value is returned
30 * RETURN: Status and 64-bit converted integer
32 * DESCRIPTION: Performs a base 8 conversion of the input string to an
33 * integer value, either 32 or 64 bits.
35 * NOTE: Maximum 64-bit unsigned octal value is 01777777777777777777777
36 * Maximum 32-bit unsigned octal value is 037777777777
38 ******************************************************************************/
40 acpi_status
acpi_ut_convert_octal_string(char *string
, u64
*return_value_ptr
)
42 u64 accumulated_value
= 0;
43 acpi_status status
= AE_OK
;
45 /* Convert each ASCII byte in the input string */
49 * Character must be ASCII 0-7, otherwise:
50 * 1) Runtime: terminate with no error, per the ACPI spec
51 * 2) Compiler: return an error
53 if (!(ACPI_IS_OCTAL_DIGIT(*string
))) {
54 #ifdef ACPI_ASL_COMPILER
55 status
= AE_BAD_OCTAL_CONSTANT
;
60 /* Convert and insert this octal digit into the accumulator */
62 status
= acpi_ut_insert_digit(&accumulated_value
, 8, *string
);
63 if (ACPI_FAILURE(status
)) {
64 status
= AE_OCTAL_OVERFLOW
;
71 /* Always return the value that has been accumulated */
73 *return_value_ptr
= accumulated_value
;
77 /*******************************************************************************
79 * FUNCTION: acpi_ut_convert_decimal_string
81 * PARAMETERS: string - Null terminated input string
82 * return_value_ptr - Where the converted value is returned
84 * RETURN: Status and 64-bit converted integer
86 * DESCRIPTION: Performs a base 10 conversion of the input string to an
87 * integer value, either 32 or 64 bits.
89 * NOTE: Maximum 64-bit unsigned decimal value is 18446744073709551615
90 * Maximum 32-bit unsigned decimal value is 4294967295
92 ******************************************************************************/
94 acpi_status
acpi_ut_convert_decimal_string(char *string
, u64
*return_value_ptr
)
96 u64 accumulated_value
= 0;
97 acpi_status status
= AE_OK
;
99 /* Convert each ASCII byte in the input string */
103 * Character must be ASCII 0-9, otherwise:
104 * 1) Runtime: terminate with no error, per the ACPI spec
105 * 2) Compiler: return an error
107 if (!isdigit(*string
)) {
108 #ifdef ACPI_ASL_COMPILER
109 status
= AE_BAD_DECIMAL_CONSTANT
;
114 /* Convert and insert this decimal digit into the accumulator */
116 status
= acpi_ut_insert_digit(&accumulated_value
, 10, *string
);
117 if (ACPI_FAILURE(status
)) {
118 status
= AE_DECIMAL_OVERFLOW
;
125 /* Always return the value that has been accumulated */
127 *return_value_ptr
= accumulated_value
;
131 /*******************************************************************************
133 * FUNCTION: acpi_ut_convert_hex_string
135 * PARAMETERS: string - Null terminated input string
136 * return_value_ptr - Where the converted value is returned
138 * RETURN: Status and 64-bit converted integer
140 * DESCRIPTION: Performs a base 16 conversion of the input string to an
141 * integer value, either 32 or 64 bits.
143 * NOTE: Maximum 64-bit unsigned hex value is 0xFFFFFFFFFFFFFFFF
144 * Maximum 32-bit unsigned hex value is 0xFFFFFFFF
146 ******************************************************************************/
148 acpi_status
acpi_ut_convert_hex_string(char *string
, u64
*return_value_ptr
)
150 u64 accumulated_value
= 0;
151 acpi_status status
= AE_OK
;
153 /* Convert each ASCII byte in the input string */
157 * Character must be ASCII A-F, a-f, or 0-9, otherwise:
158 * 1) Runtime: terminate with no error, per the ACPI spec
159 * 2) Compiler: return an error
161 if (!isxdigit(*string
)) {
162 #ifdef ACPI_ASL_COMPILER
163 status
= AE_BAD_HEX_CONSTANT
;
168 /* Convert and insert this hex digit into the accumulator */
170 status
= acpi_ut_insert_digit(&accumulated_value
, 16, *string
);
171 if (ACPI_FAILURE(status
)) {
172 status
= AE_HEX_OVERFLOW
;
179 /* Always return the value that has been accumulated */
181 *return_value_ptr
= accumulated_value
;
185 /*******************************************************************************
187 * FUNCTION: acpi_ut_remove_leading_zeros
189 * PARAMETERS: string - Pointer to input ASCII string
191 * RETURN: Next character after any leading zeros. This character may be
192 * used by the caller to detect end-of-string.
194 * DESCRIPTION: Remove any leading zeros in the input string. Return the
195 * next character after the final ASCII zero to enable the caller
196 * to check for the end of the string (NULL terminator).
198 ******************************************************************************/
200 char acpi_ut_remove_leading_zeros(char **string
)
203 while (**string
== ACPI_ASCII_ZERO
) {
210 /*******************************************************************************
212 * FUNCTION: acpi_ut_remove_whitespace
214 * PARAMETERS: string - Pointer to input ASCII string
216 * RETURN: Next character after any whitespace. This character may be
217 * used by the caller to detect end-of-string.
219 * DESCRIPTION: Remove any leading whitespace in the input string. Return the
220 * next character after the final ASCII zero to enable the caller
221 * to check for the end of the string (NULL terminator).
223 ******************************************************************************/
225 char acpi_ut_remove_whitespace(char **string
)
228 while (isspace((u8
)**string
)) {
235 /*******************************************************************************
237 * FUNCTION: acpi_ut_detect_hex_prefix
239 * PARAMETERS: string - Pointer to input ASCII string
241 * RETURN: TRUE if a "0x" prefix was found at the start of the string
243 * DESCRIPTION: Detect and remove a hex "0x" prefix
245 ******************************************************************************/
247 u8
acpi_ut_detect_hex_prefix(char **string
)
249 char *initial_position
= *string
;
251 acpi_ut_remove_hex_prefix(string
);
252 if (*string
!= initial_position
) {
253 return (TRUE
); /* String is past leading 0x */
256 return (FALSE
); /* Not a hex string */
259 /*******************************************************************************
261 * FUNCTION: acpi_ut_remove_hex_prefix
263 * PARAMETERS: string - Pointer to input ASCII string
267 * DESCRIPTION: Remove a hex "0x" prefix
269 ******************************************************************************/
271 void acpi_ut_remove_hex_prefix(char **string
)
273 if ((**string
== ACPI_ASCII_ZERO
) &&
274 (tolower((int)*(*string
+ 1)) == 'x')) {
275 *string
+= 2; /* Go past the leading 0x */
279 /*******************************************************************************
281 * FUNCTION: acpi_ut_detect_octal_prefix
283 * PARAMETERS: string - Pointer to input ASCII string
285 * RETURN: True if an octal "0" prefix was found at the start of the
288 * DESCRIPTION: Detect and remove an octal prefix (zero)
290 ******************************************************************************/
292 u8
acpi_ut_detect_octal_prefix(char **string
)
295 if (**string
== ACPI_ASCII_ZERO
) {
296 *string
+= 1; /* Go past the leading 0 */
300 return (FALSE
); /* Not an octal string */
303 /*******************************************************************************
305 * FUNCTION: acpi_ut_insert_digit
307 * PARAMETERS: accumulated_value - Current value of the integer value
308 * accumulator. The new value is
310 * base - Radix, either 8/10/16
311 * ascii_digit - ASCII single digit to be inserted
313 * RETURN: Status and result of the convert/insert operation. The only
314 * possible returned exception code is numeric overflow of
315 * either the multiply or add conversion operations.
317 * DESCRIPTION: Generic conversion and insertion function for all bases:
319 * 1) Multiply the current accumulated/converted value by the
320 * base in order to make room for the new character.
322 * 2) Convert the new character to binary and add it to the
323 * current accumulated value.
325 * Note: The only possible exception indicates an integer
326 * overflow (AE_NUMERIC_OVERFLOW)
328 ******************************************************************************/
331 acpi_ut_insert_digit(u64
*accumulated_value
, u32 base
, int ascii_digit
)
336 /* Make room in the accumulated value for the incoming digit */
338 status
= acpi_ut_strtoul_multiply64(*accumulated_value
, base
, &product
);
339 if (ACPI_FAILURE(status
)) {
343 /* Add in the new digit, and store the sum to the accumulated value */
346 acpi_ut_strtoul_add64(product
,
347 acpi_ut_ascii_char_to_hex(ascii_digit
),
353 /*******************************************************************************
355 * FUNCTION: acpi_ut_strtoul_multiply64
357 * PARAMETERS: multiplicand - Current accumulated converted integer
359 * out_product - Where the product is returned
361 * RETURN: Status and 64-bit product
363 * DESCRIPTION: Multiply two 64-bit values, with checking for 64-bit overflow as
364 * well as 32-bit overflow if necessary (if the current global
365 * integer width is 32).
367 ******************************************************************************/
370 acpi_ut_strtoul_multiply64(u64 multiplicand
, u32 base
, u64
*out_product
)
375 /* Exit if either operand is zero */
378 if (!multiplicand
|| !base
) {
383 * Check for 64-bit overflow before the actual multiplication.
385 * Notes: 64-bit division is often not supported on 32-bit platforms
386 * (it requires a library function), Therefore ACPICA has a local
387 * 64-bit divide function. Also, Multiplier is currently only used
388 * as the radix (8/10/16), to the 64/32 divide will always work.
390 acpi_ut_short_divide(ACPI_UINT64_MAX
, base
, "ient
, NULL
);
391 if (multiplicand
> quotient
) {
392 return (AE_NUMERIC_OVERFLOW
);
395 product
= multiplicand
* base
;
397 /* Check for 32-bit overflow if necessary */
399 if ((acpi_gbl_integer_bit_width
== 32) && (product
> ACPI_UINT32_MAX
)) {
400 return (AE_NUMERIC_OVERFLOW
);
403 *out_product
= product
;
407 /*******************************************************************************
409 * FUNCTION: acpi_ut_strtoul_add64
411 * PARAMETERS: addend1 - Current accumulated converted integer
412 * digit - New hex value/char
413 * out_sum - Where sum is returned (Accumulator)
415 * RETURN: Status and 64-bit sum
417 * DESCRIPTION: Add two 64-bit values, with checking for 64-bit overflow as
418 * well as 32-bit overflow if necessary (if the current global
419 * integer width is 32).
421 ******************************************************************************/
423 static acpi_status
acpi_ut_strtoul_add64(u64 addend1
, u32 digit
, u64
*out_sum
)
427 /* Check for 64-bit overflow before the actual addition */
429 if ((addend1
> 0) && (digit
> (ACPI_UINT64_MAX
- addend1
))) {
430 return (AE_NUMERIC_OVERFLOW
);
433 sum
= addend1
+ digit
;
435 /* Check for 32-bit overflow if necessary */
437 if ((acpi_gbl_integer_bit_width
== 32) && (sum
> ACPI_UINT32_MAX
)) {
438 return (AE_NUMERIC_OVERFLOW
);