1 /*******************************************************************************
3 * Module Name: utnonansi - Non-ansi C library functions
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2016, 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("utnonansi")
51 * Non-ANSI C library functions - strlwr, strupr, stricmp, and a 64-bit
54 /*******************************************************************************
56 * FUNCTION: acpi_ut_strlwr (strlwr)
58 * PARAMETERS: src_string - The source string to convert
62 * DESCRIPTION: Convert a string to lowercase
64 ******************************************************************************/
65 void acpi_ut_strlwr(char *src_string
)
69 ACPI_FUNCTION_ENTRY();
75 /* Walk entire string, lowercasing the letters */
77 for (string
= src_string
; *string
; string
++) {
78 *string
= (char)tolower((int)*string
);
82 /*******************************************************************************
84 * FUNCTION: acpi_ut_strupr (strupr)
86 * PARAMETERS: src_string - The source string to convert
90 * DESCRIPTION: Convert a string to uppercase
92 ******************************************************************************/
94 void acpi_ut_strupr(char *src_string
)
98 ACPI_FUNCTION_ENTRY();
104 /* Walk entire string, uppercasing the letters */
106 for (string
= src_string
; *string
; string
++) {
107 *string
= (char)toupper((int)*string
);
111 /******************************************************************************
113 * FUNCTION: acpi_ut_stricmp (stricmp)
115 * PARAMETERS: string1 - first string to compare
116 * string2 - second string to compare
118 * RETURN: int that signifies string relationship. Zero means strings
121 * DESCRIPTION: Case-insensitive string compare. Implementation of the
122 * non-ANSI stricmp function.
124 ******************************************************************************/
126 int acpi_ut_stricmp(char *string1
, char *string2
)
132 c1
= tolower((int)*string1
);
133 c2
= tolower((int)*string2
);
138 while ((c1
== c2
) && (c1
));
143 #if defined (ACPI_DEBUGGER) || defined (ACPI_APPLICATION)
144 /*******************************************************************************
146 * FUNCTION: acpi_ut_safe_strcpy, acpi_ut_safe_strcat, acpi_ut_safe_strncat
148 * PARAMETERS: Adds a "DestSize" parameter to each of the standard string
149 * functions. This is the size of the Destination buffer.
151 * RETURN: TRUE if the operation would overflow the destination buffer.
153 * DESCRIPTION: Safe versions of standard Clib string functions. Ensure that
154 * the result of the operation will not overflow the output string
157 * NOTE: These functions are typically only helpful for processing
158 * user input and command lines. For most ACPICA code, the
159 * required buffer length is precisely calculated before buffer
160 * allocation, so the use of these functions is unnecessary.
162 ******************************************************************************/
164 u8
acpi_ut_safe_strcpy(char *dest
, acpi_size dest_size
, char *source
)
167 if (strlen(source
) >= dest_size
) {
171 strcpy(dest
, source
);
175 u8
acpi_ut_safe_strcat(char *dest
, acpi_size dest_size
, char *source
)
178 if ((strlen(dest
) + strlen(source
)) >= dest_size
) {
182 strcat(dest
, source
);
187 acpi_ut_safe_strncat(char *dest
,
189 char *source
, acpi_size max_transfer_length
)
191 acpi_size actual_transfer_length
;
193 actual_transfer_length
= ACPI_MIN(max_transfer_length
, strlen(source
));
195 if ((strlen(dest
) + actual_transfer_length
) >= dest_size
) {
199 strncat(dest
, source
, max_transfer_length
);
204 /*******************************************************************************
206 * FUNCTION: acpi_ut_strtoul64
208 * PARAMETERS: string - Null terminated string
209 * base - Radix of the string: 16 or 10 or
211 * max_integer_byte_width - Maximum allowable integer,in bytes:
212 * 4 or 8 (32 or 64 bits)
213 * ret_integer - Where the converted integer is
216 * RETURN: Status and Converted value
218 * DESCRIPTION: Convert a string into an unsigned value. Performs either a
219 * 32-bit or 64-bit conversion, depending on the input integer
220 * size (often the current mode of the interpreter).
222 * NOTES: Negative numbers are not supported, as they are not supported
225 * acpi_gbl_integer_byte_width should be set to the proper width.
226 * For the core ACPICA code, this width depends on the DSDT
227 * version. For iASL, the default byte width is always 8 for the
228 * parser, but error checking is performed later to flag cases
229 * where a 64-bit constant is defined in a 32-bit DSDT/SSDT.
231 * Does not support Octal strings, not needed at this time.
233 ******************************************************************************/
236 acpi_ut_strtoul64(char *string
,
237 u32 base
, u32 max_integer_byte_width
, u64
*ret_integer
)
240 u64 return_value
= 0;
247 ACPI_FUNCTION_TRACE_STR(ut_strtoul64
, string
);
260 return_ACPI_STATUS(AE_BAD_PARAMETER
);
267 /* Skip over any white space in the buffer */
269 while ((*string
) && (isspace((int)*string
) || *string
== '\t')) {
273 if (base
== ACPI_ANY_BASE
) {
275 * Base equal to ACPI_ANY_BASE means 'Either decimal or hex'.
276 * We need to determine if it is decimal or hexadecimal.
278 if ((*string
== '0') && (tolower((int)*(string
+ 1)) == 'x')) {
282 /* Skip over the leading '0x' */
289 /* Any string left? Check that '0x' is not followed by white space. */
291 if (!(*string
) || isspace((int)*string
) || *string
== '\t') {
292 if (base
== ACPI_ANY_BASE
) {
300 * Perform a 32-bit or 64-bit conversion, depending upon the input
303 dividend
= (max_integer_byte_width
<= ACPI_MAX32_BYTE_WIDTH
) ?
304 ACPI_UINT32_MAX
: ACPI_UINT64_MAX
;
306 /* Main loop: convert the string to a 32- or 64-bit integer */
309 if (isdigit((int)*string
)) {
311 /* Convert ASCII 0-9 to Decimal value */
313 this_digit
= ((u8
)*string
) - '0';
314 } else if (base
== 10) {
316 /* Digit is out of range; possible in to_integer case only */
320 this_digit
= (u8
)toupper((int)*string
);
321 if (isxdigit((int)this_digit
)) {
323 /* Convert ASCII Hex char to value */
325 this_digit
= this_digit
- 'A' + 10;
332 if (base
== ACPI_ANY_BASE
) {
337 } else if ((valid_digits
== 0) && (this_digit
== 0)
347 if (sign_of0x
&& ((valid_digits
> 16) ||
349 && (max_integer_byte_width
<=
350 ACPI_MAX32_BYTE_WIDTH
)))) {
352 * This is to_integer operation case.
353 * No restrictions for string-to-integer conversion,
359 /* Divide the digit into the correct position */
361 (void)acpi_ut_short_divide((dividend
- (u64
)this_digit
), base
,
364 if (return_value
> quotient
) {
365 if (base
== ACPI_ANY_BASE
) {
372 return_value
*= base
;
373 return_value
+= this_digit
;
377 /* All done, normal exit */
381 ACPI_DEBUG_PRINT((ACPI_DB_EXEC
, "Converted value: %8.8X%8.8X\n",
382 ACPI_FORMAT_UINT64(return_value
)));
384 *ret_integer
= return_value
;
385 return_ACPI_STATUS(AE_OK
);
389 /* Base was set/validated above (10 or 16) */
392 return_ACPI_STATUS(AE_BAD_DECIMAL_CONSTANT
);
394 return_ACPI_STATUS(AE_BAD_HEX_CONSTANT
);
398 #ifdef _OBSOLETE_FUNCTIONS
399 /* Removed: 01/2016 */
401 /*******************************************************************************
403 * FUNCTION: strtoul64
405 * PARAMETERS: string - Null terminated string
406 * terminater - Where a pointer to the terminating byte
408 * base - Radix of the string
410 * RETURN: Converted value
412 * DESCRIPTION: Convert a string into an unsigned value.
414 ******************************************************************************/
416 acpi_status
strtoul64(char *string
, u32 base
, u64
*ret_integer
)
420 u64 return_value
= 0;
421 acpi_status status
= AE_OK
;
435 * The specified Base parameter is not in the domain of
438 return (AE_BAD_PARAMETER
);
441 /* Skip over any white space in the buffer: */
443 while (isspace((int)*string
) || *string
== '\t') {
448 * The buffer may contain an optional plus or minus sign.
449 * If it does, then skip over it but remember what is was:
451 if (*string
== '-') {
452 sign
= ACPI_SIGN_NEGATIVE
;
454 } else if (*string
== '+') {
456 sign
= ACPI_SIGN_POSITIVE
;
458 sign
= ACPI_SIGN_POSITIVE
;
462 * If the input parameter Base is zero, then we need to
463 * determine if it is octal, decimal, or hexadecimal:
466 if (*string
== '0') {
467 if (tolower((int)*(++string
)) == 'x') {
479 * For octal and hexadecimal bases, skip over the leading
480 * 0 or 0x, if they are present.
482 if (base
== 8 && *string
== '0') {
486 if (base
== 16 && *string
== '0' && tolower((int)*(++string
)) == 'x') {
490 /* Main loop: convert the string to an unsigned long */
493 if (isdigit((int)*string
)) {
494 index
= ((u8
)*string
) - '0';
496 index
= (u8
)toupper((int)*string
);
497 if (isupper((int)index
)) {
498 index
= index
- 'A' + 10;
508 /* Check to see if value is out of range: */
510 if (return_value
> ((ACPI_UINT64_MAX
- (u64
)index
) / (u64
)base
)) {
513 return_value
*= base
;
514 return_value
+= index
;
520 /* If a minus sign was present, then "the conversion is negated": */
522 if (sign
== ACPI_SIGN_NEGATIVE
) {
523 return_value
= (ACPI_UINT32_MAX
- return_value
) + 1;
526 *ret_integer
= return_value
;
533 status
= AE_BAD_OCTAL_CONSTANT
;
538 status
= AE_BAD_DECIMAL_CONSTANT
;
543 status
= AE_BAD_HEX_CONSTANT
;
548 /* Base validated above */