dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / acpi / common / utnonansi.c
blob70fb33e64c0c8fe088598107aba8654ed936469f
1 /*******************************************************************************
3 * Module Name: utnonansi - Non-ansi C library functions
5 ******************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2016, Intel Corp.
9 * All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
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.
30 * NO WARRANTY
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.h"
45 #include "accommon.h"
48 #define _COMPONENT ACPI_UTILITIES
49 ACPI_MODULE_NAME ("utnonansi")
53 * Non-ANSI C library functions - strlwr, strupr, stricmp, and a 64-bit
54 * version of strtoul.
57 /*******************************************************************************
59 * FUNCTION: AcpiUtStrlwr (strlwr)
61 * PARAMETERS: SrcString - The source string to convert
63 * RETURN: None
65 * DESCRIPTION: Convert a string to lowercase
67 ******************************************************************************/
69 void
70 AcpiUtStrlwr (
71 char *SrcString)
73 char *String;
76 ACPI_FUNCTION_ENTRY ();
79 if (!SrcString)
81 return;
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
99 * RETURN: None
101 * DESCRIPTION: Convert a string to uppercase
103 ******************************************************************************/
105 void
106 AcpiUtStrupr (
107 char *SrcString)
109 char *String;
112 ACPI_FUNCTION_ENTRY ();
115 if (!SrcString)
117 return;
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
137 * are equal.
139 * DESCRIPTION: Case-insensitive string compare. Implementation of the
140 * non-ANSI stricmp function.
142 ******************************************************************************/
145 AcpiUtStricmp (
146 char *String1,
147 char *String2)
149 int c1;
150 int c2;
155 c1 = tolower ((int) *String1);
156 c2 = tolower ((int) *String2);
158 String1++;
159 String2++;
161 while ((c1 == c2) && (c1));
163 return (c1 - c2);
167 #if defined (ACPI_DEBUGGER) || defined (ACPI_APPLICATION)
168 /*******************************************************************************
170 * FUNCTION: AcpiUtSafeStrcpy, AcpiUtSafeStrcat, AcpiUtSafeStrncat
172 * PARAMETERS: Adds a "DestSize" parameter to each of the standard string
173 * functions. This is the size of the Destination buffer.
175 * RETURN: TRUE if the operation would overflow the destination buffer.
177 * DESCRIPTION: Safe versions of standard Clib string functions. Ensure that
178 * the result of the operation will not overflow the output string
179 * buffer.
181 * NOTE: These functions are typically only helpful for processing
182 * user input and command lines. For most ACPICA code, the
183 * required buffer length is precisely calculated before buffer
184 * allocation, so the use of these functions is unnecessary.
186 ******************************************************************************/
188 BOOLEAN
189 AcpiUtSafeStrcpy (
190 char *Dest,
191 ACPI_SIZE DestSize,
192 char *Source)
195 if (strlen (Source) >= DestSize)
197 return (TRUE);
200 strcpy (Dest, Source);
201 return (FALSE);
204 BOOLEAN
205 AcpiUtSafeStrcat (
206 char *Dest,
207 ACPI_SIZE DestSize,
208 char *Source)
211 if ((strlen (Dest) + strlen (Source)) >= DestSize)
213 return (TRUE);
216 strcat (Dest, Source);
217 return (FALSE);
220 BOOLEAN
221 AcpiUtSafeStrncat (
222 char *Dest,
223 ACPI_SIZE DestSize,
224 char *Source,
225 ACPI_SIZE MaxTransferLength)
227 ACPI_SIZE ActualTransferLength;
230 ActualTransferLength = ACPI_MIN (MaxTransferLength, strlen (Source));
232 if ((strlen (Dest) + ActualTransferLength) >= DestSize)
234 return (TRUE);
237 strncat (Dest, Source, MaxTransferLength);
238 return (FALSE);
240 #endif
243 /*******************************************************************************
245 * FUNCTION: AcpiUtStrtoul64
247 * PARAMETERS: String - Null terminated string
248 * Base - Radix of the string: 16 or 10 or
249 * ACPI_ANY_BASE
250 * MaxIntegerByteWidth - Maximum allowable integer,in bytes:
251 * 4 or 8 (32 or 64 bits)
252 * RetInteger - Where the converted integer is
253 * returned
255 * RETURN: Status and Converted value
257 * DESCRIPTION: Convert a string into an unsigned value. Performs either a
258 * 32-bit or 64-bit conversion, depending on the input integer
259 * size (often the current mode of the interpreter).
261 * NOTES: Negative numbers are not supported, as they are not supported
262 * by ACPI.
264 * AcpiGbl_IntegerByteWidth should be set to the proper width.
265 * For the core ACPICA code, this width depends on the DSDT
266 * version. For iASL, the default byte width is always 8 for the
267 * parser, but error checking is performed later to flag cases
268 * where a 64-bit constant is defined in a 32-bit DSDT/SSDT.
270 * Does not support Octal strings, not needed at this time.
272 ******************************************************************************/
274 ACPI_STATUS
275 AcpiUtStrtoul64 (
276 char *String,
277 UINT32 Base,
278 UINT32 MaxIntegerByteWidth,
279 UINT64 *RetInteger)
281 UINT32 ThisDigit = 0;
282 UINT64 ReturnValue = 0;
283 UINT64 Quotient;
284 UINT64 Dividend;
285 UINT8 ValidDigits = 0;
286 UINT8 SignOf0x = 0;
287 UINT8 Term = 0;
290 ACPI_FUNCTION_TRACE_STR (UtStrtoul64, String);
293 switch (Base)
295 case ACPI_ANY_BASE:
296 case 10:
297 case 16:
299 break;
301 default:
303 /* Invalid Base */
305 return_ACPI_STATUS (AE_BAD_PARAMETER);
308 if (!String)
310 goto ErrorExit;
313 /* Skip over any white space in the buffer */
315 while ((*String) && (isspace ((int) *String) || *String == '\t'))
317 String++;
320 if (Base == ACPI_ANY_BASE)
323 * Base equal to ACPI_ANY_BASE means 'Either decimal or hex'.
324 * We need to determine if it is decimal or hexadecimal.
326 if ((*String == '0') && (tolower ((int) *(String + 1)) == 'x'))
328 SignOf0x = 1;
329 Base = 16;
331 /* Skip over the leading '0x' */
332 String += 2;
334 else
336 Base = 10;
340 /* Any string left? Check that '0x' is not followed by white space. */
342 if (!(*String) || isspace ((int) *String) || *String == '\t')
344 if (Base == ACPI_ANY_BASE)
346 goto ErrorExit;
348 else
350 goto AllDone;
355 * Perform a 32-bit or 64-bit conversion, depending upon the input
356 * byte width
358 Dividend = (MaxIntegerByteWidth <= ACPI_MAX32_BYTE_WIDTH) ?
359 ACPI_UINT32_MAX : ACPI_UINT64_MAX;
361 /* Main loop: convert the string to a 32- or 64-bit integer */
363 while (*String)
365 if (isdigit ((int) *String))
367 /* Convert ASCII 0-9 to Decimal value */
369 ThisDigit = ((UINT8) *String) - '0';
371 else if (Base == 10)
373 /* Digit is out of range; possible in ToInteger case only */
375 Term = 1;
377 else
379 ThisDigit = (UINT8) toupper ((int) *String);
380 if (isxdigit ((int) ThisDigit))
382 /* Convert ASCII Hex char to value */
384 ThisDigit = ThisDigit - 'A' + 10;
386 else
388 Term = 1;
392 if (Term)
394 if (Base == ACPI_ANY_BASE)
396 goto ErrorExit;
398 else
400 break;
403 else if ((ValidDigits == 0) && (ThisDigit == 0) && !SignOf0x)
405 /* Skip zeros */
406 String++;
407 continue;
410 ValidDigits++;
412 if (SignOf0x && ((ValidDigits > 16) ||
413 ((ValidDigits > 8) && (MaxIntegerByteWidth <= ACPI_MAX32_BYTE_WIDTH))))
416 * This is ToInteger operation case.
417 * No restrictions for string-to-integer conversion,
418 * see ACPI spec.
420 goto ErrorExit;
423 /* Divide the digit into the correct position */
425 (void) AcpiUtShortDivide (
426 (Dividend - (UINT64) ThisDigit), Base, &Quotient, NULL);
428 if (ReturnValue > Quotient)
430 if (Base == ACPI_ANY_BASE)
432 goto ErrorExit;
434 else
436 break;
440 ReturnValue *= Base;
441 ReturnValue += ThisDigit;
442 String++;
445 /* All done, normal exit */
447 AllDone:
449 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Converted value: %8.8X%8.8X\n",
450 ACPI_FORMAT_UINT64 (ReturnValue)));
452 *RetInteger = ReturnValue;
453 return_ACPI_STATUS (AE_OK);
456 ErrorExit:
458 /* Base was set/validated above (10 or 16) */
460 if (Base == 10)
462 return_ACPI_STATUS (AE_BAD_DECIMAL_CONSTANT);
464 else
466 return_ACPI_STATUS (AE_BAD_HEX_CONSTANT);
471 #ifdef _OBSOLETE_FUNCTIONS
472 /* Removed: 01/2016 */
474 /*******************************************************************************
476 * FUNCTION: strtoul64
478 * PARAMETERS: String - Null terminated string
479 * Terminater - Where a pointer to the terminating byte
480 * is returned
481 * Base - Radix of the string
483 * RETURN: Converted value
485 * DESCRIPTION: Convert a string into an unsigned value.
487 ******************************************************************************/
489 ACPI_STATUS
490 strtoul64 (
491 char *String,
492 UINT32 Base,
493 UINT64 *RetInteger)
495 UINT32 Index;
496 UINT32 Sign;
497 UINT64 ReturnValue = 0;
498 ACPI_STATUS Status = AE_OK;
501 *RetInteger = 0;
503 switch (Base)
505 case 0:
506 case 8:
507 case 10:
508 case 16:
510 break;
512 default:
514 * The specified Base parameter is not in the domain of
515 * this function:
517 return (AE_BAD_PARAMETER);
520 /* Skip over any white space in the buffer: */
522 while (isspace ((int) *String) || *String == '\t')
524 ++String;
528 * The buffer may contain an optional plus or minus sign.
529 * If it does, then skip over it but remember what is was:
531 if (*String == '-')
533 Sign = ACPI_SIGN_NEGATIVE;
534 ++String;
536 else if (*String == '+')
538 ++String;
539 Sign = ACPI_SIGN_POSITIVE;
541 else
543 Sign = ACPI_SIGN_POSITIVE;
547 * If the input parameter Base is zero, then we need to
548 * determine if it is octal, decimal, or hexadecimal:
550 if (Base == 0)
552 if (*String == '0')
554 if (tolower ((int) *(++String)) == 'x')
556 Base = 16;
557 ++String;
559 else
561 Base = 8;
564 else
566 Base = 10;
571 * For octal and hexadecimal bases, skip over the leading
572 * 0 or 0x, if they are present.
574 if (Base == 8 && *String == '0')
576 String++;
579 if (Base == 16 &&
580 *String == '0' &&
581 tolower ((int) *(++String)) == 'x')
583 String++;
586 /* Main loop: convert the string to an unsigned long */
588 while (*String)
590 if (isdigit ((int) *String))
592 Index = ((UINT8) *String) - '0';
594 else
596 Index = (UINT8) toupper ((int) *String);
597 if (isupper ((int) Index))
599 Index = Index - 'A' + 10;
601 else
603 goto ErrorExit;
607 if (Index >= Base)
609 goto ErrorExit;
612 /* Check to see if value is out of range: */
614 if (ReturnValue > ((ACPI_UINT64_MAX - (UINT64) Index) /
615 (UINT64) Base))
617 goto ErrorExit;
619 else
621 ReturnValue *= Base;
622 ReturnValue += Index;
625 ++String;
629 /* If a minus sign was present, then "the conversion is negated": */
631 if (Sign == ACPI_SIGN_NEGATIVE)
633 ReturnValue = (ACPI_UINT32_MAX - ReturnValue) + 1;
636 *RetInteger = ReturnValue;
637 return (Status);
640 ErrorExit:
641 switch (Base)
643 case 8:
645 Status = AE_BAD_OCTAL_CONSTANT;
646 break;
648 case 10:
650 Status = AE_BAD_DECIMAL_CONSTANT;
651 break;
653 case 16:
655 Status = AE_BAD_HEX_CONSTANT;
656 break;
658 default:
660 /* Base validated above */
662 break;
665 return (Status);
667 #endif