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 terminate with no error */
51 if (!(ACPI_IS_OCTAL_DIGIT(*string
))) {
55 /* Convert and insert this octal digit into the accumulator */
57 status
= acpi_ut_insert_digit(&accumulated_value
, 8, *string
);
58 if (ACPI_FAILURE(status
)) {
59 status
= AE_OCTAL_OVERFLOW
;
66 /* Always return the value that has been accumulated */
68 *return_value_ptr
= accumulated_value
;
72 /*******************************************************************************
74 * FUNCTION: acpi_ut_convert_decimal_string
76 * PARAMETERS: string - Null terminated input string
77 * return_value_ptr - Where the converted value is returned
79 * RETURN: Status and 64-bit converted integer
81 * DESCRIPTION: Performs a base 10 conversion of the input string to an
82 * integer value, either 32 or 64 bits.
84 * NOTE: Maximum 64-bit unsigned decimal value is 18446744073709551615
85 * Maximum 32-bit unsigned decimal value is 4294967295
87 ******************************************************************************/
89 acpi_status
acpi_ut_convert_decimal_string(char *string
, u64
*return_value_ptr
)
91 u64 accumulated_value
= 0;
92 acpi_status status
= AE_OK
;
94 /* Convert each ASCII byte in the input string */
98 /* Character must be ASCII 0-9, otherwise terminate with no error */
100 if (!isdigit(*string
)) {
104 /* Convert and insert this decimal digit into the accumulator */
106 status
= acpi_ut_insert_digit(&accumulated_value
, 10, *string
);
107 if (ACPI_FAILURE(status
)) {
108 status
= AE_DECIMAL_OVERFLOW
;
115 /* Always return the value that has been accumulated */
117 *return_value_ptr
= accumulated_value
;
121 /*******************************************************************************
123 * FUNCTION: acpi_ut_convert_hex_string
125 * PARAMETERS: string - Null terminated input string
126 * return_value_ptr - Where the converted value is returned
128 * RETURN: Status and 64-bit converted integer
130 * DESCRIPTION: Performs a base 16 conversion of the input string to an
131 * integer value, either 32 or 64 bits.
133 * NOTE: Maximum 64-bit unsigned hex value is 0xFFFFFFFFFFFFFFFF
134 * Maximum 32-bit unsigned hex value is 0xFFFFFFFF
136 ******************************************************************************/
138 acpi_status
acpi_ut_convert_hex_string(char *string
, u64
*return_value_ptr
)
140 u64 accumulated_value
= 0;
141 acpi_status status
= AE_OK
;
143 /* Convert each ASCII byte in the input string */
147 /* Must be ASCII A-F, a-f, or 0-9, otherwise terminate with no error */
149 if (!isxdigit(*string
)) {
153 /* Convert and insert this hex digit into the accumulator */
155 status
= acpi_ut_insert_digit(&accumulated_value
, 16, *string
);
156 if (ACPI_FAILURE(status
)) {
157 status
= AE_HEX_OVERFLOW
;
164 /* Always return the value that has been accumulated */
166 *return_value_ptr
= accumulated_value
;
170 /*******************************************************************************
172 * FUNCTION: acpi_ut_remove_leading_zeros
174 * PARAMETERS: string - Pointer to input ASCII string
176 * RETURN: Next character after any leading zeros. This character may be
177 * used by the caller to detect end-of-string.
179 * DESCRIPTION: Remove any leading zeros in the input string. Return the
180 * next character after the final ASCII zero to enable the caller
181 * to check for the end of the string (NULL terminator).
183 ******************************************************************************/
185 char acpi_ut_remove_leading_zeros(char **string
)
188 while (**string
== ACPI_ASCII_ZERO
) {
195 /*******************************************************************************
197 * FUNCTION: acpi_ut_remove_whitespace
199 * PARAMETERS: string - Pointer to input ASCII string
201 * RETURN: Next character after any whitespace. This character may be
202 * used by the caller to detect end-of-string.
204 * DESCRIPTION: Remove any leading whitespace in the input string. Return the
205 * next character after the final ASCII zero to enable the caller
206 * to check for the end of the string (NULL terminator).
208 ******************************************************************************/
210 char acpi_ut_remove_whitespace(char **string
)
213 while (isspace((u8
)**string
)) {
220 /*******************************************************************************
222 * FUNCTION: acpi_ut_detect_hex_prefix
224 * PARAMETERS: string - Pointer to input ASCII string
226 * RETURN: TRUE if a "0x" prefix was found at the start of the string
228 * DESCRIPTION: Detect and remove a hex "0x" prefix
230 ******************************************************************************/
232 u8
acpi_ut_detect_hex_prefix(char **string
)
235 if ((**string
== ACPI_ASCII_ZERO
) &&
236 (tolower((int)*(*string
+ 1)) == 'x')) {
237 *string
+= 2; /* Go past the leading 0x */
241 return (FALSE
); /* Not a hex string */
244 /*******************************************************************************
246 * FUNCTION: acpi_ut_detect_octal_prefix
248 * PARAMETERS: string - Pointer to input ASCII string
250 * RETURN: True if an octal "0" prefix was found at the start of the
253 * DESCRIPTION: Detect and remove an octal prefix (zero)
255 ******************************************************************************/
257 u8
acpi_ut_detect_octal_prefix(char **string
)
260 if (**string
== ACPI_ASCII_ZERO
) {
261 *string
+= 1; /* Go past the leading 0 */
265 return (FALSE
); /* Not an octal string */
268 /*******************************************************************************
270 * FUNCTION: acpi_ut_insert_digit
272 * PARAMETERS: accumulated_value - Current value of the integer value
273 * accumulator. The new value is
275 * base - Radix, either 8/10/16
276 * ascii_digit - ASCII single digit to be inserted
278 * RETURN: Status and result of the convert/insert operation. The only
279 * possible returned exception code is numeric overflow of
280 * either the multiply or add conversion operations.
282 * DESCRIPTION: Generic conversion and insertion function for all bases:
284 * 1) Multiply the current accumulated/converted value by the
285 * base in order to make room for the new character.
287 * 2) Convert the new character to binary and add it to the
288 * current accumulated value.
290 * Note: The only possible exception indicates an integer
291 * overflow (AE_NUMERIC_OVERFLOW)
293 ******************************************************************************/
296 acpi_ut_insert_digit(u64
*accumulated_value
, u32 base
, int ascii_digit
)
301 /* Make room in the accumulated value for the incoming digit */
303 status
= acpi_ut_strtoul_multiply64(*accumulated_value
, base
, &product
);
304 if (ACPI_FAILURE(status
)) {
308 /* Add in the new digit, and store the sum to the accumulated value */
311 acpi_ut_strtoul_add64(product
,
312 acpi_ut_ascii_char_to_hex(ascii_digit
),
318 /*******************************************************************************
320 * FUNCTION: acpi_ut_strtoul_multiply64
322 * PARAMETERS: multiplicand - Current accumulated converted integer
324 * out_product - Where the product is returned
326 * RETURN: Status and 64-bit product
328 * DESCRIPTION: Multiply two 64-bit values, with checking for 64-bit overflow as
329 * well as 32-bit overflow if necessary (if the current global
330 * integer width is 32).
332 ******************************************************************************/
335 acpi_ut_strtoul_multiply64(u64 multiplicand
, u32 base
, u64
*out_product
)
340 /* Exit if either operand is zero */
343 if (!multiplicand
|| !base
) {
348 * Check for 64-bit overflow before the actual multiplication.
350 * Notes: 64-bit division is often not supported on 32-bit platforms
351 * (it requires a library function), Therefore ACPICA has a local
352 * 64-bit divide function. Also, Multiplier is currently only used
353 * as the radix (8/10/16), to the 64/32 divide will always work.
355 acpi_ut_short_divide(ACPI_UINT64_MAX
, base
, "ient
, NULL
);
356 if (multiplicand
> quotient
) {
357 return (AE_NUMERIC_OVERFLOW
);
360 product
= multiplicand
* base
;
362 /* Check for 32-bit overflow if necessary */
364 if ((acpi_gbl_integer_bit_width
== 32) && (product
> ACPI_UINT32_MAX
)) {
365 return (AE_NUMERIC_OVERFLOW
);
368 *out_product
= product
;
372 /*******************************************************************************
374 * FUNCTION: acpi_ut_strtoul_add64
376 * PARAMETERS: addend1 - Current accumulated converted integer
377 * digit - New hex value/char
378 * out_sum - Where sum is returned (Accumulator)
380 * RETURN: Status and 64-bit sum
382 * DESCRIPTION: Add two 64-bit values, with checking for 64-bit overflow as
383 * well as 32-bit overflow if necessary (if the current global
384 * integer width is 32).
386 ******************************************************************************/
388 static acpi_status
acpi_ut_strtoul_add64(u64 addend1
, u32 digit
, u64
*out_sum
)
392 /* Check for 64-bit overflow before the actual addition */
394 if ((addend1
> 0) && (digit
> (ACPI_UINT64_MAX
- addend1
))) {
395 return (AE_NUMERIC_OVERFLOW
);
398 sum
= addend1
+ digit
;
400 /* Check for 32-bit overflow if necessary */
402 if ((acpi_gbl_integer_bit_width
== 32) && (sum
> ACPI_UINT32_MAX
)) {
403 return (AE_NUMERIC_OVERFLOW
);