xref: /netbsd-src/sys/external/bsd/acpica/dist/tools/acpisrc/asutils.c (revision 046a29855e04359424fd074e8313af6b6be8cfb6)
1 /******************************************************************************
2  *
3  * Module Name: asutils - common utilities
4  *
5  *****************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2023, 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 MERCHANTABILITY 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 *
AsSkipUntilChar(char * Buffer,char Target)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 *
AsSkipPastChar(char * Buffer,char Target)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 *
AsReplaceData(char * Buffer,UINT32 LengthToRemove,char * BufferToAdd,UINT32 LengthToAdd)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         Gbl_MadeChanges = TRUE;
139         memmove ((Buffer + LengthToAdd), (Buffer + LengthToRemove),
140             (BufferLength - LengthToRemove));
141     }
142 
143     /*
144      * Now we can move in the new data
145      */
146     if (LengthToAdd > 0)
147     {
148         Gbl_MadeChanges = TRUE;
149         memmove (Buffer, BufferToAdd, LengthToAdd);
150     }
151 
152     return (Buffer + LengthToAdd);
153 }
154 
155 
156 /******************************************************************************
157  *
158  * FUNCTION:    AsInsertData
159  *
160  * DESCRIPTION: This function inserts and removes data from the file buffer.
161  *              if more data is inserted than is removed, the data in the buffer
162  *              is moved to make room. If less data is inserted than is removed,
163  *              the remaining data is moved to close the hole.
164  *
165  ******************************************************************************/
166 
167 char *
AsInsertData(char * Buffer,char * BufferToAdd,UINT32 LengthToAdd)168 AsInsertData (
169     char                    *Buffer,
170     char                    *BufferToAdd,
171     UINT32                  LengthToAdd)
172 {
173     UINT32                  BufferLength;
174 
175 
176     if (LengthToAdd > 0)
177     {
178         /*
179          * Buffer is a string, so the length must include the terminating zero
180          */
181         BufferLength = strlen (Buffer) + 1;
182 
183         /*
184          * Move some of the existing data
185          * 1) If adding more bytes than removing, make room for the new data
186          * 2) if removing more bytes than adding, delete the extra space
187          */
188         Gbl_MadeChanges = TRUE;
189         memmove ((Buffer + LengthToAdd), Buffer, BufferLength);
190 
191         /*
192          * Now we can move in the new data
193          */
194         memmove (Buffer, BufferToAdd, LengthToAdd);
195     }
196 
197     return (Buffer + LengthToAdd);
198 }
199 
200 
201 /******************************************************************************
202  *
203  * FUNCTION:    AsRemoveData
204  *
205  * DESCRIPTION: This function inserts and removes data from the file buffer.
206  *              if more data is inserted than is removed, the data in the buffer
207  *              is moved to make room. If less data is inserted than is removed,
208  *              the remaining data is moved to close the hole.
209  *
210  ******************************************************************************/
211 
212 char *
AsRemoveData(char * StartPointer,char * EndPointer)213 AsRemoveData (
214     char                    *StartPointer,
215     char                    *EndPointer)
216 {
217     UINT32                  BufferLength;
218 
219 
220     /*
221      * Buffer is a string, so the length must include the terminating zero
222      */
223     BufferLength = strlen (EndPointer) + 1;
224 
225     Gbl_MadeChanges = TRUE;
226     memmove (StartPointer, EndPointer, BufferLength);
227 
228     return (StartPointer);
229 }
230