1 /******************************************************************************
2  *
3  * Module Name: asutils - common utilities
4  *
5  *****************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2015, 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:    AsSkipUntilChar
50  *
51  * DESCRIPTION: Find the next instance of the input character
52  *
53  ******************************************************************************/
54 
55 char *
56 AsSkipUntilChar (
57     char                    *Buffer,
58     char                    Target)
59 {
60 
61     while (*Buffer != Target)
62     {
63         if (!*Buffer)
64         {
65             return (NULL);
66         }
67 
68         Buffer++;
69     }
70 
71     return (Buffer);
72 }
73 
74 
75 /******************************************************************************
76  *
77  * FUNCTION:    AsSkipPastChar
78  *
79  * DESCRIPTION: Find the next instance of the input character, return a buffer
80  *              pointer to this character+1.
81  *
82  ******************************************************************************/
83 
84 char *
85 AsSkipPastChar (
86     char                    *Buffer,
87     char                    Target)
88 {
89 
90     while (*Buffer != Target)
91     {
92         if (!*Buffer)
93         {
94             return (NULL);
95         }
96 
97         Buffer++;
98     }
99 
100     Buffer++;
101     return (Buffer);
102 }
103 
104 
105 /******************************************************************************
106  *
107  * FUNCTION:    AsReplaceData
108  *
109  * DESCRIPTION: This function inserts and removes data from the file buffer.
110  *              if more data is inserted than is removed, the data in the buffer
111  *              is moved to make room. If less data is inserted than is removed,
112  *              the remaining data is moved to close the hole.
113  *
114  ******************************************************************************/
115 
116 char *
117 AsReplaceData (
118     char                    *Buffer,
119     UINT32                  LengthToRemove,
120     char                    *BufferToAdd,
121     UINT32                  LengthToAdd)
122 {
123     UINT32                  BufferLength;
124 
125 
126     /*
127      * Buffer is a string, so the length must include the terminating zero
128      */
129     BufferLength = strlen (Buffer) + 1;
130 
131     if (LengthToRemove != LengthToAdd)
132     {
133         /*
134          * Move some of the existing data
135          * 1) If adding more bytes than removing, make room for the new data
136          * 2) if removing more bytes than adding, delete the extra space
137          */
138         if (LengthToRemove > 0)
139         {
140             Gbl_MadeChanges = TRUE;
141             memmove ((Buffer + LengthToAdd), (Buffer + LengthToRemove),
142                 (BufferLength - LengthToRemove));
143         }
144     }
145 
146     /*
147      * Now we can move in the new data
148      */
149     if (LengthToAdd > 0)
150     {
151         Gbl_MadeChanges = TRUE;
152         memmove (Buffer, BufferToAdd, LengthToAdd);
153     }
154 
155     return (Buffer + LengthToAdd);
156 }
157 
158 
159 /******************************************************************************
160  *
161  * FUNCTION:    AsInsertData
162  *
163  * DESCRIPTION: This function inserts and removes data from the file buffer.
164  *              if more data is inserted than is removed, the data in the buffer
165  *              is moved to make room. If less data is inserted than is removed,
166  *              the remaining data is moved to close the hole.
167  *
168  ******************************************************************************/
169 
170 char *
171 AsInsertData (
172     char                    *Buffer,
173     char                    *BufferToAdd,
174     UINT32                  LengthToAdd)
175 {
176     UINT32                  BufferLength;
177 
178 
179     if (LengthToAdd > 0)
180     {
181         /*
182          * Buffer is a string, so the length must include the terminating zero
183          */
184         BufferLength = strlen (Buffer) + 1;
185 
186         /*
187          * Move some of the existing data
188          * 1) If adding more bytes than removing, make room for the new data
189          * 2) if removing more bytes than adding, delete the extra space
190          */
191         Gbl_MadeChanges = TRUE;
192         memmove ((Buffer + LengthToAdd), Buffer, BufferLength);
193 
194         /*
195          * Now we can move in the new data
196          */
197         memmove (Buffer, BufferToAdd, LengthToAdd);
198     }
199 
200     return (Buffer + LengthToAdd);
201 }
202 
203 
204 /******************************************************************************
205  *
206  * FUNCTION:    AsRemoveData
207  *
208  * DESCRIPTION: This function inserts and removes data from the file buffer.
209  *              if more data is inserted than is removed, the data in the buffer
210  *              is moved to make room. If less data is inserted than is removed,
211  *              the remaining data is moved to close the hole.
212  *
213  ******************************************************************************/
214 
215 char *
216 AsRemoveData (
217     char                    *StartPointer,
218     char                    *EndPointer)
219 {
220     UINT32                  BufferLength;
221 
222 
223     /*
224      * Buffer is a string, so the length must include the terminating zero
225      */
226     BufferLength = strlen (EndPointer) + 1;
227 
228     Gbl_MadeChanges = TRUE;
229     memmove (StartPointer, EndPointer, BufferLength);
230 
231     return (StartPointer);
232 }
233