1294332Sdes
2276707Sdes/******************************************************************************
3276707Sdes *
4276707Sdes * Module Name: asutils - common utilities
5276707Sdes *
6276707Sdes *****************************************************************************/
7276707Sdes
8276707Sdes/*
9276707Sdes * Copyright (C) 2000 - 2011, Intel Corp.
10276707Sdes * All rights reserved.
11276707Sdes *
12276707Sdes * Redistribution and use in source and binary forms, with or without
13276707Sdes * modification, are permitted provided that the following conditions
14276707Sdes * are met:
15276707Sdes * 1. Redistributions of source code must retain the above copyright
16276707Sdes *    notice, this list of conditions, and the following disclaimer,
17276707Sdes *    without modification.
18276707Sdes * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19276707Sdes *    substantially similar to the "NO WARRANTY" disclaimer below
20276707Sdes *    ("Disclaimer") and any redistribution must be conditioned upon
21276707Sdes *    including a substantially similar Disclaimer requirement for further
22276707Sdes *    binary redistribution.
23276707Sdes * 3. Neither the names of the above-listed copyright holders nor the names
24276707Sdes *    of any contributors may be used to endorse or promote products derived
25276707Sdes *    from this software without specific prior written permission.
26276707Sdes *
27276707Sdes * Alternatively, this software may be distributed under the terms of the
28276707Sdes * GNU General Public License ("GPL") version 2 as published by the Free
29276707Sdes * Software Foundation.
30276707Sdes *
31276707Sdes * NO WARRANTY
32276707Sdes * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33276707Sdes * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34276707Sdes * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35276707Sdes * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36276707Sdes * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37276707Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38276707Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39276707Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40276707Sdes * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41276707Sdes * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42276707Sdes * POSSIBILITY OF SUCH DAMAGES.
43276707Sdes */
44276707Sdes
45276707Sdes#include "acpisrc.h"
46276707Sdes
47294332Sdes
48276707Sdes/******************************************************************************
49276707Sdes *
50276707Sdes * FUNCTION:    AsSkipUntilChar
51276707Sdes *
52276707Sdes * DESCRIPTION: Find the next instance of the input character
53276707Sdes *
54276707Sdes ******************************************************************************/
55276707Sdes
56276707Sdeschar *
57276707SdesAsSkipUntilChar (
58276707Sdes    char                    *Buffer,
59276707Sdes    char                    Target)
60276707Sdes{
61276707Sdes
62276707Sdes    while (*Buffer != Target)
63276707Sdes    {
64276707Sdes        if (!*Buffer)
65276707Sdes        {
66276707Sdes            return NULL;
67276707Sdes        }
68276707Sdes
69276707Sdes        Buffer++;
70276707Sdes    }
71276707Sdes
72276707Sdes    return (Buffer);
73276707Sdes}
74276707Sdes
75276707Sdes
76276707Sdes/******************************************************************************
77276707Sdes *
78276707Sdes * FUNCTION:    AsSkipPastChar
79276707Sdes *
80276707Sdes * DESCRIPTION: Find the next instance of the input character, return a buffer
81276707Sdes *              pointer to this character+1.
82276707Sdes *
83276707Sdes ******************************************************************************/
84276707Sdes
85char *
86AsSkipPastChar (
87    char                    *Buffer,
88    char                    Target)
89{
90
91    while (*Buffer != Target)
92    {
93        if (!*Buffer)
94        {
95            return NULL;
96        }
97
98        Buffer++;
99    }
100
101    Buffer++;
102
103    return (Buffer);
104}
105
106
107/******************************************************************************
108 *
109 * FUNCTION:    AsReplaceData
110 *
111 * DESCRIPTION: This function inserts and removes data from the file buffer.
112 *              if more data is inserted than is removed, the data in the buffer
113 *              is moved to make room.  If less data is inserted than is removed,
114 *              the remaining data is moved to close the hole.
115 *
116 ******************************************************************************/
117
118char *
119AsReplaceData (
120    char                    *Buffer,
121    UINT32                  LengthToRemove,
122    char                    *BufferToAdd,
123    UINT32                  LengthToAdd)
124{
125    UINT32                  BufferLength;
126
127
128    /*
129     * Buffer is a string, so the length must include the terminating zero
130     */
131    BufferLength = strlen (Buffer) + 1;
132
133    if (LengthToRemove != LengthToAdd)
134    {
135        /*
136         * Move some of the existing data
137         * 1) If adding more bytes than removing, make room for the new data
138         * 2) if removing more bytes than adding, delete the extra space
139         */
140        if (LengthToRemove > 0)
141        {
142            Gbl_MadeChanges = TRUE;
143            memmove ((Buffer + LengthToAdd), (Buffer + LengthToRemove), (BufferLength - LengthToRemove));
144        }
145    }
146
147    /*
148     * Now we can move in the new data
149     */
150    if (LengthToAdd > 0)
151    {
152        Gbl_MadeChanges = TRUE;
153        memmove (Buffer, BufferToAdd, LengthToAdd);
154    }
155
156    return (Buffer + LengthToAdd);
157}
158
159
160/******************************************************************************
161 *
162 * FUNCTION:    AsInsertData
163 *
164 * DESCRIPTION: This function inserts and removes data from the file buffer.
165 *              if more data is inserted than is removed, the data in the buffer
166 *              is moved to make room.  If less data is inserted than is removed,
167 *              the remaining data is moved to close the hole.
168 *
169 ******************************************************************************/
170
171char *
172AsInsertData (
173    char                    *Buffer,
174    char                    *BufferToAdd,
175    UINT32                  LengthToAdd)
176{
177    UINT32                  BufferLength;
178
179
180    if (LengthToAdd > 0)
181    {
182        /*
183         * Buffer is a string, so the length must include the terminating zero
184         */
185        BufferLength = strlen (Buffer) + 1;
186
187        /*
188         * Move some of the existing data
189         * 1) If adding more bytes than removing, make room for the new data
190         * 2) if removing more bytes than adding, delete the extra space
191         */
192        Gbl_MadeChanges = TRUE;
193        memmove ((Buffer + LengthToAdd), Buffer, BufferLength);
194
195        /*
196         * Now we can move in the new data
197         */
198        memmove (Buffer, BufferToAdd, LengthToAdd);
199    }
200
201    return (Buffer + LengthToAdd);
202}
203
204
205/******************************************************************************
206 *
207 * FUNCTION:    AsRemoveData
208 *
209 * DESCRIPTION: This function inserts and removes data from the file buffer.
210 *              if more data is inserted than is removed, the data in the buffer
211 *              is moved to make room.  If less data is inserted than is removed,
212 *              the remaining data is moved to close the hole.
213 *
214 ******************************************************************************/
215
216char *
217AsRemoveData (
218    char                    *StartPointer,
219    char                    *EndPointer)
220{
221    UINT32                  BufferLength;
222
223
224    /*
225     * Buffer is a string, so the length must include the terminating zero
226     */
227    BufferLength = strlen (EndPointer) + 1;
228
229    Gbl_MadeChanges = TRUE;
230    memmove (StartPointer, EndPointer, BufferLength);
231
232    return (StartPointer);
233}
234
235