UINT32 AcpiUtStrtoul ( const char *String, char **Terminator, UINT32 Base) { UINT32 converted = 0; UINT32 index; UINT32 sign; const char *StringStart; UINT32 ReturnValue = 0; ACPI_STATUS Status = AE_OK; /* * Save the value of the pointer to the buffer's first * character, save the current errno value, and then * skip over any white space in the buffer: */ StringStart = String; while (ACPI_IS_SPACE (*String) || *String == '\t') { ++String; } /* * The buffer may contain an optional plus or minus sign. * If it does, then skip over it but remember what is was: */ if (*String == '-') { sign = NEGATIVE; ++String; } else if (*String == '+') { ++String; sign = POSITIVE; } else { sign = POSITIVE; } /* * If the input parameter Base is zero, then we need to * determine if it is octal, decimal, or hexadecimal: */ if (Base == 0) { if (*String == '0') { if (AcpiUtToLower (*(++String)) == 'x') { Base = 16; ++String; } else { Base = 8; } } else { Base = 10; } } else if (Base < 2 || Base > 36) { /* * The specified Base parameter is not in the domain of * this function: */ goto done; } /* * For octal and hexadecimal bases, skip over the leading * 0 or 0x, if they are present. */ if (Base == 8 && *String == '0') { String++; } if (Base == 16 && *String == '0' && AcpiUtToLower (*(++String)) == 'x') { String++; } /* * Main loop: convert the string to an unsigned long: */ while (*String) { if (ACPI_IS_DIGIT (*String)) { index = (UINT32) ((UINT8) *String - '0'); } else { index = (UINT32) AcpiUtToUpper (*String); if (ACPI_IS_UPPER (index)) { index = index - 'A' + 10; } else { goto done; } } if (index >= Base) { goto done; } /* * Check to see if value is out of range: */ if (ReturnValue > ((ACPI_UINT32_MAX - (UINT32) index) / (UINT32) Base)) { Status = AE_ERROR; ReturnValue = 0; /* reset */ } else { ReturnValue *= Base; ReturnValue += index; converted = 1; } ++String; } done: /* * If appropriate, update the caller's pointer to the next * unconverted character in the buffer. */ if (Terminator) { if (converted == 0 && ReturnValue == 0 && String != NULL) { *Terminator = (char *) StringStart; } else { *Terminator = (char *) String; } } if (Status == AE_ERROR) { ReturnValue = ACPI_UINT32_MAX; } /* * If a minus sign was present, then "the conversion is negated": */ if (sign == NEGATIVE) { ReturnValue = (ACPI_UINT32_MAX - ReturnValue) + 1; } return (ReturnValue); }
acpi_status acpi_ut_strtoul64(char *string, u32 base, u64 *ret_integer) { u32 this_digit = 0; u64 return_value = 0; u64 quotient; u64 dividend; u32 to_integer_op = (base == ACPI_ANY_BASE); u32 mode32 = (acpi_gbl_integer_byte_width == 4); u8 valid_digits = 0; u8 sign_of0x = 0; u8 term = 0; ACPI_FUNCTION_TRACE_STR(ut_stroul64, string); switch (base) { case ACPI_ANY_BASE: case 16: break; default: /* Invalid Base */ return_ACPI_STATUS(AE_BAD_PARAMETER); } if (!string) { goto error_exit; } /* Skip over any white space in the buffer */ while ((*string) && (ACPI_IS_SPACE(*string) || *string == '\t')) { string++; } if (to_integer_op) { /* * Base equal to ACPI_ANY_BASE means 'ToInteger operation case'. * We need to determine if it is decimal or hexadecimal. */ if ((*string == '0') && (ACPI_TOLOWER(*(string + 1)) == 'x')) { sign_of0x = 1; base = 16; /* Skip over the leading '0x' */ string += 2; } else { base = 10; } } /* Any string left? Check that '0x' is not followed by white space. */ if (!(*string) || ACPI_IS_SPACE(*string) || *string == '\t') { if (to_integer_op) { goto error_exit; } else { goto all_done; } } /* * Perform a 32-bit or 64-bit conversion, depending upon the current * execution mode of the interpreter */ dividend = (mode32) ? ACPI_UINT32_MAX : ACPI_UINT64_MAX; /* Main loop: convert the string to a 32- or 64-bit integer */ while (*string) { if (ACPI_IS_DIGIT(*string)) { /* Convert ASCII 0-9 to Decimal value */ this_digit = ((u8)*string) - '0'; } else if (base == 10) { /* Digit is out of range; possible in to_integer case only */ term = 1; } else { this_digit = (u8)ACPI_TOUPPER(*string); if (ACPI_IS_XDIGIT((char)this_digit)) { /* Convert ASCII Hex char to value */ this_digit = this_digit - 'A' + 10; } else { term = 1; } } if (term) { if (to_integer_op) { goto error_exit; } else { break; } } else if ((valid_digits == 0) && (this_digit == 0) && !sign_of0x) { /* Skip zeros */ string++; continue; } valid_digits++; if (sign_of0x && ((valid_digits > 16) || ((valid_digits > 8) && mode32))) { /* * This is to_integer operation case. * No any restrictions for string-to-integer conversion, * see ACPI spec. */ goto error_exit; } /* Divide the digit into the correct position */ (void)acpi_ut_short_divide((dividend - (u64)this_digit), base, "ient, NULL); if (return_value > quotient) { if (to_integer_op) { goto error_exit; } else { break; } } return_value *= base; return_value += this_digit; string++; } /* All done, normal exit */ all_done: ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "Converted value: %8.8X%8.8X\n", ACPI_FORMAT_UINT64(return_value))); *ret_integer = return_value; return_ACPI_STATUS(AE_OK); error_exit: /* Base was set/validated above */ if (base == 10) { return_ACPI_STATUS(AE_BAD_DECIMAL_CONSTANT); } else { return_ACPI_STATUS(AE_BAD_HEX_CONSTANT); } }
acpi_status acpi_ut_strtoul64 ( char *string, u32 base, acpi_integer *ret_integer) { u32 this_digit = 0; acpi_integer return_value = 0; acpi_integer quotient; ACPI_FUNCTION_TRACE ("ut_stroul64"); if ((!string) || !(*string)) { goto error_exit; } switch (base) { case ACPI_ANY_BASE: case 10: case 16: break; default: /* Invalid Base */ return_ACPI_STATUS (AE_BAD_PARAMETER); } /* Skip over any white space in the buffer */ while (ACPI_IS_SPACE (*string) || *string == '\t') { string++; } /* * If the input parameter Base is zero, then we need to * determine if it is decimal or hexadecimal: */ if (base == 0) { if ((*string == '0') && (ACPI_TOLOWER (*(string + 1)) == 'x')) { base = 16; string += 2; } else { base = 10; } } /* * For hexadecimal base, skip over the leading * 0 or 0x, if they are present. */ if ((base == 16) && (*string == '0') && (ACPI_TOLOWER (*(string + 1)) == 'x')) { string += 2; } /* Any string left? */ if (!(*string)) { goto error_exit; } /* Main loop: convert the string to a 64-bit integer */ while (*string) { if (ACPI_IS_DIGIT (*string)) { /* Convert ASCII 0-9 to Decimal value */ this_digit = ((u8) *string) - '0'; } else { if (base == 10) { /* Digit is out of range */ goto error_exit; } this_digit = (u8) ACPI_TOUPPER (*string); if (ACPI_IS_XDIGIT ((char) this_digit)) { /* Convert ASCII Hex char to value */ this_digit = this_digit - 'A' + 10; } else { /* * We allow non-hex chars, just stop now, same as end-of-string. * See ACPI spec, string-to-integer conversion. */ break; } } /* Divide the digit into the correct position */ (void) acpi_ut_short_divide ((ACPI_INTEGER_MAX - (acpi_integer) this_digit), base, "ient, NULL); if (return_value > quotient) { goto error_exit; } return_value *= base; return_value += this_digit; string++; } /* All done, normal exit */ *ret_integer = return_value; return_ACPI_STATUS (AE_OK); error_exit: /* Base was set/validated above */ if (base == 10) { return_ACPI_STATUS (AE_BAD_DECIMAL_CONSTANT); } else { return_ACPI_STATUS (AE_BAD_HEX_CONSTANT); } }
ACPI_STATUS AcpiUtStrtoul64 ( char *String, UINT32 Base, UINT64 *RetInteger) { UINT32 ThisDigit = 0; UINT64 ReturnValue = 0; UINT64 Quotient; UINT64 Dividend; UINT32 ToIntegerOp = (Base == ACPI_ANY_BASE); UINT32 Mode32 = (AcpiGbl_IntegerByteWidth == 4); UINT8 ValidDigits = 0; UINT8 SignOf0x = 0; UINT8 Term = 0; ACPI_FUNCTION_TRACE_STR (UtStroul64, String); switch (Base) { case ACPI_ANY_BASE: case 16: break; default: /* Invalid Base */ return_ACPI_STATUS (AE_BAD_PARAMETER); } if (!String) { goto ErrorExit; } /* Skip over any white space in the buffer */ while ((*String) && (ACPI_IS_SPACE (*String) || *String == '\t')) { String++; } if (ToIntegerOp) { /* * Base equal to ACPI_ANY_BASE means 'ToInteger operation case'. * We need to determine if it is decimal or hexadecimal. */ if ((*String == '0') && (ACPI_TOLOWER (*(String + 1)) == 'x')) { SignOf0x = 1; Base = 16; /* Skip over the leading '0x' */ String += 2; } else { Base = 10; } } /* Any string left? Check that '0x' is not followed by white space. */ if (!(*String) || ACPI_IS_SPACE (*String) || *String == '\t') { if (ToIntegerOp) { goto ErrorExit; } else { goto AllDone; } } /* * Perform a 32-bit or 64-bit conversion, depending upon the current * execution mode of the interpreter */ Dividend = (Mode32) ? ACPI_UINT32_MAX : ACPI_UINT64_MAX; /* Main loop: convert the string to a 32- or 64-bit integer */ while (*String) { if (ACPI_IS_DIGIT (*String)) { /* Convert ASCII 0-9 to Decimal value */ ThisDigit = ((UINT8) *String) - '0'; } else if (Base == 10) { /* Digit is out of range; possible in ToInteger case only */ Term = 1; } else { ThisDigit = (UINT8) ACPI_TOUPPER (*String); if (ACPI_IS_XDIGIT ((char) ThisDigit)) { /* Convert ASCII Hex char to value */ ThisDigit = ThisDigit - 'A' + 10; } else { Term = 1; } } if (Term) { if (ToIntegerOp) { goto ErrorExit; } else { break; } } else if ((ValidDigits == 0) && (ThisDigit == 0) && !SignOf0x) { /* Skip zeros */ String++; continue; } ValidDigits++; if (SignOf0x && ((ValidDigits > 16) || ((ValidDigits > 8) && Mode32))) { /* * This is ToInteger operation case. * No any restrictions for string-to-integer conversion, * see ACPI spec. */ goto ErrorExit; } /* Divide the digit into the correct position */ (void) AcpiUtShortDivide ((Dividend - (UINT64) ThisDigit), Base, &Quotient, NULL); if (ReturnValue > Quotient) { if (ToIntegerOp) { goto ErrorExit; } else { break; } } ReturnValue *= Base; ReturnValue += ThisDigit; String++; } /* All done, normal exit */ AllDone: ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Converted value: %8.8X%8.8X\n", ACPI_FORMAT_UINT64 (ReturnValue))); *RetInteger = ReturnValue; return_ACPI_STATUS (AE_OK); ErrorExit: /* Base was set/validated above */ if (Base == 10) { return_ACPI_STATUS (AE_BAD_DECIMAL_CONSTANT); } else { return_ACPI_STATUS (AE_BAD_HEX_CONSTANT); } }
ACPI_STATUS AcpiUtStrtoul64 ( char *String, UINT32 Base, ACPI_INTEGER *RetInteger) { UINT32 Index; ACPI_INTEGER ReturnValue = 0; ACPI_STATUS Status = AE_OK; ACPI_INTEGER Dividend; ACPI_INTEGER Quotient; *RetInteger = 0; switch (Base) { case 0: case 8: case 10: case 16: break; default: /* * The specified Base parameter is not in the domain of * this function: */ return (AE_BAD_PARAMETER); } /* * skip over any white space in the buffer: */ while (ACPI_IS_SPACE (*String) || *String == '\t') { ++String; } /* * If the input parameter Base is zero, then we need to * determine if it is octal, decimal, or hexadecimal: */ if (Base == 0) { if (*String == '0') { if (ACPI_TOLOWER (*(++String)) == 'x') { Base = 16; ++String; } else { Base = 8; } } else { Base = 10; } } /* * For octal and hexadecimal bases, skip over the leading * 0 or 0x, if they are present. */ if (Base == 8 && *String == '0') { String++; } if (Base == 16 && *String == '0' && ACPI_TOLOWER (*(++String)) == 'x') { String++; } /* Main loop: convert the string to an unsigned long */ while (*String) { if (ACPI_IS_DIGIT (*String)) { Index = ((UINT8) *String) - '0'; } else { Index = (UINT8) ACPI_TOUPPER (*String); if (ACPI_IS_UPPER ((char) Index)) { Index = Index - 'A' + 10; } else { goto ErrorExit; } } if (Index >= Base) { goto ErrorExit; } /* Check to see if value is out of range: */ Dividend = ACPI_INTEGER_MAX - (ACPI_INTEGER) Index; (void) AcpiUtShortDivide (&Dividend, Base, &Quotient, NULL); if (ReturnValue > Quotient) { goto ErrorExit; } ReturnValue *= Base; ReturnValue += Index; ++String; } *RetInteger = ReturnValue; return (Status); ErrorExit: switch (Base) { case 8: Status = AE_BAD_OCTAL_CONSTANT; break; case 10: Status = AE_BAD_DECIMAL_CONSTANT; break; case 16: Status = AE_BAD_HEX_CONSTANT; break; default: /* Base validated above */ break; } return (Status); }