xref: /openbmc/linux/drivers/acpi/acpica/utnonansi.c (revision 726bd223)
1 /*******************************************************************************
2  *
3  * Module Name: utnonansi - Non-ansi C library functions
4  *
5  ******************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2017, Intel Corp.
9  * All rights reserved.
10  *
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.
25  *
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.
29  *
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.
42  */
43 
44 #include <acpi/acpi.h>
45 #include "accommon.h"
46 
47 #define _COMPONENT          ACPI_UTILITIES
48 ACPI_MODULE_NAME("utnonansi")
49 
50 /*
51  * Non-ANSI C library functions - strlwr, strupr, stricmp, and "safe"
52  * string functions.
53  */
54 /*******************************************************************************
55  *
56  * FUNCTION:    acpi_ut_strlwr (strlwr)
57  *
58  * PARAMETERS:  src_string      - The source string to convert
59  *
60  * RETURN:      None
61  *
62  * DESCRIPTION: Convert a string to lowercase
63  *
64  ******************************************************************************/
65 void acpi_ut_strlwr(char *src_string)
66 {
67 	char *string;
68 
69 	ACPI_FUNCTION_ENTRY();
70 
71 	if (!src_string) {
72 		return;
73 	}
74 
75 	/* Walk entire string, lowercasing the letters */
76 
77 	for (string = src_string; *string; string++) {
78 		*string = (char)tolower((int)*string);
79 	}
80 }
81 
82 /*******************************************************************************
83  *
84  * FUNCTION:    acpi_ut_strupr (strupr)
85  *
86  * PARAMETERS:  src_string      - The source string to convert
87  *
88  * RETURN:      None
89  *
90  * DESCRIPTION: Convert a string to uppercase
91  *
92  ******************************************************************************/
93 
94 void acpi_ut_strupr(char *src_string)
95 {
96 	char *string;
97 
98 	ACPI_FUNCTION_ENTRY();
99 
100 	if (!src_string) {
101 		return;
102 	}
103 
104 	/* Walk entire string, uppercasing the letters */
105 
106 	for (string = src_string; *string; string++) {
107 		*string = (char)toupper((int)*string);
108 	}
109 }
110 
111 /******************************************************************************
112  *
113  * FUNCTION:    acpi_ut_stricmp (stricmp)
114  *
115  * PARAMETERS:  string1             - first string to compare
116  *              string2             - second string to compare
117  *
118  * RETURN:      int that signifies string relationship. Zero means strings
119  *              are equal.
120  *
121  * DESCRIPTION: Case-insensitive string compare. Implementation of the
122  *              non-ANSI stricmp function.
123  *
124  ******************************************************************************/
125 
126 int acpi_ut_stricmp(char *string1, char *string2)
127 {
128 	int c1;
129 	int c2;
130 
131 	do {
132 		c1 = tolower((int)*string1);
133 		c2 = tolower((int)*string2);
134 
135 		string1++;
136 		string2++;
137 	}
138 	while ((c1 == c2) && (c1));
139 
140 	return (c1 - c2);
141 }
142 
143 #if defined (ACPI_DEBUGGER) || defined (ACPI_APPLICATION)
144 /*******************************************************************************
145  *
146  * FUNCTION:    acpi_ut_safe_strcpy, acpi_ut_safe_strcat, acpi_ut_safe_strncat
147  *
148  * PARAMETERS:  Adds a "DestSize" parameter to each of the standard string
149  *              functions. This is the size of the Destination buffer.
150  *
151  * RETURN:      TRUE if the operation would overflow the destination buffer.
152  *
153  * DESCRIPTION: Safe versions of standard Clib string functions. Ensure that
154  *              the result of the operation will not overflow the output string
155  *              buffer.
156  *
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.
161  *
162  ******************************************************************************/
163 
164 u8 acpi_ut_safe_strcpy(char *dest, acpi_size dest_size, char *source)
165 {
166 
167 	if (strlen(source) >= dest_size) {
168 		return (TRUE);
169 	}
170 
171 	strcpy(dest, source);
172 	return (FALSE);
173 }
174 
175 u8 acpi_ut_safe_strcat(char *dest, acpi_size dest_size, char *source)
176 {
177 
178 	if ((strlen(dest) + strlen(source)) >= dest_size) {
179 		return (TRUE);
180 	}
181 
182 	strcat(dest, source);
183 	return (FALSE);
184 }
185 
186 u8
187 acpi_ut_safe_strncat(char *dest,
188 		     acpi_size dest_size,
189 		     char *source, acpi_size max_transfer_length)
190 {
191 	acpi_size actual_transfer_length;
192 
193 	actual_transfer_length = ACPI_MIN(max_transfer_length, strlen(source));
194 
195 	if ((strlen(dest) + actual_transfer_length) >= dest_size) {
196 		return (TRUE);
197 	}
198 
199 	strncat(dest, source, max_transfer_length);
200 	return (FALSE);
201 }
202 #endif
203