Sync ACPICA with Intel's version 20170119.
[dragonfly.git] / sys / contrib / dev / acpica / source / components / utilities / utnonansi.c
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.h"
45 #include "accommon.h"
46
47
48 #define _COMPONENT          ACPI_UTILITIES
49         ACPI_MODULE_NAME    ("utnonansi")
50
51 /*
52  * Non-ANSI C library functions - strlwr, strupr, stricmp, and "safe"
53  * string functions.
54  */
55
56 /*******************************************************************************
57  *
58  * FUNCTION:    AcpiUtStrlwr (strlwr)
59  *
60  * PARAMETERS:  SrcString       - The source string to convert
61  *
62  * RETURN:      None
63  *
64  * DESCRIPTION: Convert a string to lowercase
65  *
66  ******************************************************************************/
67
68 void
69 AcpiUtStrlwr (
70     char                    *SrcString)
71 {
72     char                    *String;
73
74
75     ACPI_FUNCTION_ENTRY ();
76
77
78     if (!SrcString)
79     {
80         return;
81     }
82
83     /* Walk entire string, lowercasing the letters */
84
85     for (String = SrcString; *String; String++)
86     {
87         *String = (char) tolower ((int) *String);
88     }
89 }
90
91
92 /*******************************************************************************
93  *
94  * FUNCTION:    AcpiUtStrupr (strupr)
95  *
96  * PARAMETERS:  SrcString       - The source string to convert
97  *
98  * RETURN:      None
99  *
100  * DESCRIPTION: Convert a string to uppercase
101  *
102  ******************************************************************************/
103
104 void
105 AcpiUtStrupr (
106     char                    *SrcString)
107 {
108     char                    *String;
109
110
111     ACPI_FUNCTION_ENTRY ();
112
113
114     if (!SrcString)
115     {
116         return;
117     }
118
119     /* Walk entire string, uppercasing the letters */
120
121     for (String = SrcString; *String; String++)
122     {
123         *String = (char) toupper ((int) *String);
124     }
125 }
126
127
128 /******************************************************************************
129  *
130  * FUNCTION:    AcpiUtStricmp (stricmp)
131  *
132  * PARAMETERS:  String1             - first string to compare
133  *              String2             - second string to compare
134  *
135  * RETURN:      int that signifies string relationship. Zero means strings
136  *              are equal.
137  *
138  * DESCRIPTION: Case-insensitive string compare. Implementation of the
139  *              non-ANSI stricmp function.
140  *
141  ******************************************************************************/
142
143 int
144 AcpiUtStricmp (
145     char                    *String1,
146     char                    *String2)
147 {
148     int                     c1;
149     int                     c2;
150
151
152     do
153     {
154         c1 = tolower ((int) *String1);
155         c2 = tolower ((int) *String2);
156
157         String1++;
158         String2++;
159     }
160     while ((c1 == c2) && (c1));
161
162     return (c1 - c2);
163 }
164
165
166 #if defined (ACPI_DEBUGGER) || defined (ACPI_APPLICATION)
167 /*******************************************************************************
168  *
169  * FUNCTION:    AcpiUtSafeStrcpy, AcpiUtSafeStrcat, AcpiUtSafeStrncat
170  *
171  * PARAMETERS:  Adds a "DestSize" parameter to each of the standard string
172  *              functions. This is the size of the Destination buffer.
173  *
174  * RETURN:      TRUE if the operation would overflow the destination buffer.
175  *
176  * DESCRIPTION: Safe versions of standard Clib string functions. Ensure that
177  *              the result of the operation will not overflow the output string
178  *              buffer.
179  *
180  * NOTE:        These functions are typically only helpful for processing
181  *              user input and command lines. For most ACPICA code, the
182  *              required buffer length is precisely calculated before buffer
183  *              allocation, so the use of these functions is unnecessary.
184  *
185  ******************************************************************************/
186
187 BOOLEAN
188 AcpiUtSafeStrcpy (
189     char                    *Dest,
190     ACPI_SIZE               DestSize,
191     char                    *Source)
192 {
193
194     if (strlen (Source) >= DestSize)
195     {
196         return (TRUE);
197     }
198
199     strcpy (Dest, Source);
200     return (FALSE);
201 }
202
203 BOOLEAN
204 AcpiUtSafeStrcat (
205     char                    *Dest,
206     ACPI_SIZE               DestSize,
207     char                    *Source)
208 {
209
210     if ((strlen (Dest) + strlen (Source)) >= DestSize)
211     {
212         return (TRUE);
213     }
214
215     strcat (Dest, Source);
216     return (FALSE);
217 }
218
219 BOOLEAN
220 AcpiUtSafeStrncat (
221     char                    *Dest,
222     ACPI_SIZE               DestSize,
223     char                    *Source,
224     ACPI_SIZE               MaxTransferLength)
225 {
226     ACPI_SIZE               ActualTransferLength;
227
228
229     ActualTransferLength = ACPI_MIN (MaxTransferLength, strlen (Source));
230
231     if ((strlen (Dest) + ActualTransferLength) >= DestSize)
232     {
233         return (TRUE);
234     }
235
236     strncat (Dest, Source, MaxTransferLength);
237     return (FALSE);
238 }
239 #endif