1 /*******************************************************************************
3 * Module Name: utnonansi - Non-ansi C library functions
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2015, 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.
48 #define _COMPONENT ACPI_UTILITIES
49 ACPI_MODULE_NAME ("utnonansi")
53 * Non-ANSI C library functions - strlwr, strupr, stricmp, and a 64-bit
57 /*******************************************************************************
59 * FUNCTION: AcpiUtStrlwr (strlwr)
61 * PARAMETERS: SrcString - The source string to convert
65 * DESCRIPTION: Convert a string to lowercase
67 ******************************************************************************/
76 ACPI_FUNCTION_ENTRY ();
84 /* Walk entire string, lowercasing the letters */
86 for (String = SrcString; *String; String++)
88 *String = (char) tolower ((int) *String);
93 /*******************************************************************************
95 * FUNCTION: AcpiUtStrupr (strupr)
97 * PARAMETERS: SrcString - The source string to convert
101 * DESCRIPTION: Convert a string to uppercase
103 ******************************************************************************/
112 ACPI_FUNCTION_ENTRY ();
120 /* Walk entire string, uppercasing the letters */
122 for (String = SrcString; *String; String++)
124 *String = (char) toupper ((int) *String);
129 /******************************************************************************
131 * FUNCTION: AcpiUtStricmp (stricmp)
133 * PARAMETERS: String1 - first string to compare
134 * String2 - second string to compare
136 * RETURN: int that signifies string relationship. Zero means strings
139 * DESCRIPTION: Case-insensitive string compare. Implementation of the
140 * non-ANSI stricmp function.
142 ******************************************************************************/
155 c1 = tolower ((int) *String1);
156 c2 = tolower ((int) *String2);
161 while ((c1 == c2) && (c1));
167 /*******************************************************************************
169 * FUNCTION: AcpiUtStrtoul64
171 * PARAMETERS: String - Null terminated string
172 * Base - Radix of the string: 16 or ACPI_ANY_BASE;
173 * ACPI_ANY_BASE means 'in behalf of ToInteger'
174 * RetInteger - Where the converted integer is returned
176 * RETURN: Status and Converted value
178 * DESCRIPTION: Convert a string into an unsigned value. Performs either a
179 * 32-bit or 64-bit conversion, depending on the current mode
180 * of the interpreter.
182 * NOTE: Does not support Octal strings, not needed.
184 ******************************************************************************/
192 UINT32 ThisDigit = 0;
193 UINT64 ReturnValue = 0;
196 UINT32 ToIntegerOp = (Base == ACPI_ANY_BASE);
197 UINT32 Mode32 = (AcpiGbl_IntegerByteWidth == 4);
198 UINT8 ValidDigits = 0;
203 ACPI_FUNCTION_TRACE_STR (UtStroul64, String);
217 return_ACPI_STATUS (AE_BAD_PARAMETER);
225 /* Skip over any white space in the buffer */
227 while ((*String) && (isspace ((int) *String) || *String == '\t'))
235 * Base equal to ACPI_ANY_BASE means 'ToInteger operation case'.
236 * We need to determine if it is decimal or hexadecimal.
238 if ((*String == '0') && (tolower ((int) *(String + 1)) == 'x'))
243 /* Skip over the leading '0x' */
252 /* Any string left? Check that '0x' is not followed by white space. */
254 if (!(*String) || isspace ((int) *String) || *String == '\t')
267 * Perform a 32-bit or 64-bit conversion, depending upon the current
268 * execution mode of the interpreter
270 Dividend = (Mode32) ? ACPI_UINT32_MAX : ACPI_UINT64_MAX;
272 /* Main loop: convert the string to a 32- or 64-bit integer */
276 if (isdigit ((int) *String))
278 /* Convert ASCII 0-9 to Decimal value */
280 ThisDigit = ((UINT8) *String) - '0';
284 /* Digit is out of range; possible in ToInteger case only */
290 ThisDigit = (UINT8) toupper ((int) *String);
291 if (isxdigit ((int) ThisDigit))
293 /* Convert ASCII Hex char to value */
295 ThisDigit = ThisDigit - 'A' + 10;
314 else if ((ValidDigits == 0) && (ThisDigit == 0) && !SignOf0x)
323 if (SignOf0x && ((ValidDigits > 16) || ((ValidDigits > 8) && Mode32)))
326 * This is ToInteger operation case.
327 * No any restrictions for string-to-integer conversion,
333 /* Divide the digit into the correct position */
335 (void) AcpiUtShortDivide (
336 (Dividend - (UINT64) ThisDigit), Base, &Quotient, NULL);
338 if (ReturnValue > Quotient)
351 ReturnValue += ThisDigit;
355 /* All done, normal exit */
359 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Converted value: %8.8X%8.8X\n",
360 ACPI_FORMAT_UINT64 (ReturnValue)));
362 *RetInteger = ReturnValue;
363 return_ACPI_STATUS (AE_OK);
367 /* Base was set/validated above */
371 return_ACPI_STATUS (AE_BAD_DECIMAL_CONSTANT);
375 return_ACPI_STATUS (AE_BAD_HEX_CONSTANT);
380 #if defined (ACPI_DEBUGGER) || defined (ACPI_APPLICATION)
381 /*******************************************************************************
383 * FUNCTION: AcpiUtSafeStrcpy, AcpiUtSafeStrcat, AcpiUtSafeStrncat
385 * PARAMETERS: Adds a "DestSize" parameter to each of the standard string
386 * functions. This is the size of the Destination buffer.
388 * RETURN: TRUE if the operation would overflow the destination buffer.
390 * DESCRIPTION: Safe versions of standard Clib string functions. Ensure that
391 * the result of the operation will not overflow the output string
394 * NOTE: These functions are typically only helpful for processing
395 * user input and command lines. For most ACPICA code, the
396 * required buffer length is precisely calculated before buffer
397 * allocation, so the use of these functions is unnecessary.
399 ******************************************************************************/
408 if (strlen (Source) >= DestSize)
413 strcpy (Dest, Source);
424 if ((strlen (Dest) + strlen (Source)) >= DestSize)
429 strcat (Dest, Source);
438 ACPI_SIZE MaxTransferLength)
440 ACPI_SIZE ActualTransferLength;
443 ActualTransferLength = ACPI_MIN (MaxTransferLength, strlen (Source));
445 if ((strlen (Dest) + ActualTransferLength) >= DestSize)
450 strncat (Dest, Source, MaxTransferLength);