1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /*******************************************************************************
4 * Module Name: utnonansi - Non-ansi C library functions
6 ******************************************************************************/
11 #define _COMPONENT ACPI_UTILITIES
12 ACPI_MODULE_NAME("utnonansi")
15 * Non-ANSI C library functions - strlwr, strupr, stricmp, and "safe"
18 /*******************************************************************************
20 * FUNCTION: acpi_ut_strlwr (strlwr)
22 * PARAMETERS: src_string - The source string to convert
26 * DESCRIPTION: Convert a string to lowercase
28 ******************************************************************************/
29 void acpi_ut_strlwr(char *src_string
)
33 ACPI_FUNCTION_ENTRY();
39 /* Walk entire string, lowercasing the letters */
41 for (string
= src_string
; *string
; string
++) {
42 *string
= (char)tolower((int)*string
);
46 /*******************************************************************************
48 * FUNCTION: acpi_ut_strupr (strupr)
50 * PARAMETERS: src_string - The source string to convert
54 * DESCRIPTION: Convert a string to uppercase
56 ******************************************************************************/
58 void acpi_ut_strupr(char *src_string
)
62 ACPI_FUNCTION_ENTRY();
68 /* Walk entire string, uppercasing the letters */
70 for (string
= src_string
; *string
; string
++) {
71 *string
= (char)toupper((int)*string
);
75 /******************************************************************************
77 * FUNCTION: acpi_ut_stricmp (stricmp)
79 * PARAMETERS: string1 - first string to compare
80 * string2 - second string to compare
82 * RETURN: int that signifies string relationship. Zero means strings
85 * DESCRIPTION: Case-insensitive string compare. Implementation of the
86 * non-ANSI stricmp function.
88 ******************************************************************************/
90 int acpi_ut_stricmp(char *string1
, char *string2
)
96 c1
= tolower((int)*string1
);
97 c2
= tolower((int)*string2
);
102 while ((c1
== c2
) && (c1
));
107 #if defined (ACPI_DEBUGGER) || defined (ACPI_APPLICATION) || defined (ACPI_DEBUG_OUTPUT)
108 /*******************************************************************************
110 * FUNCTION: acpi_ut_safe_strcpy, acpi_ut_safe_strcat, acpi_ut_safe_strncat
112 * PARAMETERS: Adds a "DestSize" parameter to each of the standard string
113 * functions. This is the size of the Destination buffer.
115 * RETURN: TRUE if the operation would overflow the destination buffer.
117 * DESCRIPTION: Safe versions of standard Clib string functions. Ensure that
118 * the result of the operation will not overflow the output string
121 * NOTE: These functions are typically only helpful for processing
122 * user input and command lines. For most ACPICA code, the
123 * required buffer length is precisely calculated before buffer
124 * allocation, so the use of these functions is unnecessary.
126 ******************************************************************************/
128 u8
acpi_ut_safe_strcpy(char *dest
, acpi_size dest_size
, char *source
)
131 if (strlen(source
) >= dest_size
) {
135 strcpy(dest
, source
);
139 u8
acpi_ut_safe_strcat(char *dest
, acpi_size dest_size
, char *source
)
142 if ((strlen(dest
) + strlen(source
)) >= dest_size
) {
146 strcat(dest
, source
);
151 acpi_ut_safe_strncat(char *dest
,
153 char *source
, acpi_size max_transfer_length
)
155 acpi_size actual_transfer_length
;
157 actual_transfer_length
= ACPI_MIN(max_transfer_length
, strlen(source
));
159 if ((strlen(dest
) + actual_transfer_length
) >= dest_size
) {
163 strncat(dest
, source
, max_transfer_length
);
167 void acpi_ut_safe_strncpy(char *dest
, char *source
, acpi_size dest_size
)
169 /* Always terminate destination string */
171 strncpy(dest
, source
, dest_size
);
172 dest
[dest_size
- 1] = 0;