kernel: Sync ACPICA with Intel's version 20140114.
[dragonfly.git] / sys / contrib / dev / acpica / source / tools / acpisrc / asutils.c
1 /******************************************************************************
2  *
3  * Module Name: asutils - common utilities
4  *
5  *****************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2014, 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 "acpisrc.h"
45
46
47 /*******************************************************************************
48  *
49  * FUNCTION:    AsStrlwr (strlwr)
50  *
51  * PARAMETERS:  SrcString       - The source string to convert
52  *
53  * RETURN:      None
54  *
55  * DESCRIPTION: Convert string to lowercase
56  *
57  * NOTE: This is not a POSIX function, so it appears here so that we don't have
58  * header file issues with the various hosts/compilers/clibs.
59  *
60  ******************************************************************************/
61
62 void
63 AsStrlwr (
64     char                    *SrcString)
65 {
66     char                    *String;
67
68
69     /* Walk entire string, lowercasing the letters */
70
71     if (SrcString)
72     {
73         for (String = SrcString; *String; String++)
74         {
75             *String = (char) ACPI_TOLOWER (*String);
76         }
77     }
78 }
79
80
81 /******************************************************************************
82  *
83  * FUNCTION:    AsSkipUntilChar
84  *
85  * DESCRIPTION: Find the next instance of the input character
86  *
87  ******************************************************************************/
88
89 char *
90 AsSkipUntilChar (
91     char                    *Buffer,
92     char                    Target)
93 {
94
95     while (*Buffer != Target)
96     {
97         if (!*Buffer)
98         {
99             return (NULL);
100         }
101
102         Buffer++;
103     }
104
105     return (Buffer);
106 }
107
108
109 /******************************************************************************
110  *
111  * FUNCTION:    AsSkipPastChar
112  *
113  * DESCRIPTION: Find the next instance of the input character, return a buffer
114  *              pointer to this character+1.
115  *
116  ******************************************************************************/
117
118 char *
119 AsSkipPastChar (
120     char                    *Buffer,
121     char                    Target)
122 {
123
124     while (*Buffer != Target)
125     {
126         if (!*Buffer)
127         {
128             return (NULL);
129         }
130
131         Buffer++;
132     }
133
134     Buffer++;
135
136     return (Buffer);
137 }
138
139
140 /******************************************************************************
141  *
142  * FUNCTION:    AsReplaceData
143  *
144  * DESCRIPTION: This function inserts and removes data from the file buffer.
145  *              if more data is inserted than is removed, the data in the buffer
146  *              is moved to make room. If less data is inserted than is removed,
147  *              the remaining data is moved to close the hole.
148  *
149  ******************************************************************************/
150
151 char *
152 AsReplaceData (
153     char                    *Buffer,
154     UINT32                  LengthToRemove,
155     char                    *BufferToAdd,
156     UINT32                  LengthToAdd)
157 {
158     UINT32                  BufferLength;
159
160
161     /*
162      * Buffer is a string, so the length must include the terminating zero
163      */
164     BufferLength = strlen (Buffer) + 1;
165
166     if (LengthToRemove != LengthToAdd)
167     {
168         /*
169          * Move some of the existing data
170          * 1) If adding more bytes than removing, make room for the new data
171          * 2) if removing more bytes than adding, delete the extra space
172          */
173         if (LengthToRemove > 0)
174         {
175             Gbl_MadeChanges = TRUE;
176             memmove ((Buffer + LengthToAdd), (Buffer + LengthToRemove), (BufferLength - LengthToRemove));
177         }
178     }
179
180     /*
181      * Now we can move in the new data
182      */
183     if (LengthToAdd > 0)
184     {
185         Gbl_MadeChanges = TRUE;
186         memmove (Buffer, BufferToAdd, LengthToAdd);
187     }
188
189     return (Buffer + LengthToAdd);
190 }
191
192
193 /******************************************************************************
194  *
195  * FUNCTION:    AsInsertData
196  *
197  * DESCRIPTION: This function inserts and removes data from the file buffer.
198  *              if more data is inserted than is removed, the data in the buffer
199  *              is moved to make room. If less data is inserted than is removed,
200  *              the remaining data is moved to close the hole.
201  *
202  ******************************************************************************/
203
204 char *
205 AsInsertData (
206     char                    *Buffer,
207     char                    *BufferToAdd,
208     UINT32                  LengthToAdd)
209 {
210     UINT32                  BufferLength;
211
212
213     if (LengthToAdd > 0)
214     {
215         /*
216          * Buffer is a string, so the length must include the terminating zero
217          */
218         BufferLength = strlen (Buffer) + 1;
219
220         /*
221          * Move some of the existing data
222          * 1) If adding more bytes than removing, make room for the new data
223          * 2) if removing more bytes than adding, delete the extra space
224          */
225         Gbl_MadeChanges = TRUE;
226         memmove ((Buffer + LengthToAdd), Buffer, BufferLength);
227
228         /*
229          * Now we can move in the new data
230          */
231         memmove (Buffer, BufferToAdd, LengthToAdd);
232     }
233
234     return (Buffer + LengthToAdd);
235 }
236
237
238 /******************************************************************************
239  *
240  * FUNCTION:    AsRemoveData
241  *
242  * DESCRIPTION: This function inserts and removes data from the file buffer.
243  *              if more data is inserted than is removed, the data in the buffer
244  *              is moved to make room. If less data is inserted than is removed,
245  *              the remaining data is moved to close the hole.
246  *
247  ******************************************************************************/
248
249 char *
250 AsRemoveData (
251     char                    *StartPointer,
252     char                    *EndPointer)
253 {
254     UINT32                  BufferLength;
255
256
257     /*
258      * Buffer is a string, so the length must include the terminating zero
259      */
260     BufferLength = strlen (EndPointer) + 1;
261
262     Gbl_MadeChanges = TRUE;
263     memmove (StartPointer, EndPointer, BufferLength);
264
265     return (StartPointer);
266 }