1 /** @file
2 Architecture Independent Base Memory Library Implementation.
3
4 The following BaseMemoryLib instances contain the same copy of this file:
5 BaseMemoryLib
6 PeiMemoryLib
7 UefiMemoryLib
8
9 Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
10 This program and the accompanying materials
11 are licensed and made available under the terms and conditions of the BSD License
12 which accompanies this distribution. The full text of the license may be found at
13 http://opensource.org/licenses/bsd-license.php.
14
15 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
16 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17
18 **/
19
20 #include "MemLibInternals.h"
21
22 /**
23 Fills a target buffer with a 16-bit value, and returns the target buffer.
24
25 @param Buffer The pointer to the target buffer to fill.
26 @param Length The count of 16-bit value to fill.
27 @param Value The value with which to fill Length bytes of Buffer.
28
29 @return Buffer
30
31 **/
32 VOID *
33 EFIAPI
InternalMemSetMem16(OUT VOID * Buffer,IN UINTN Length,IN UINT16 Value)34 InternalMemSetMem16 (
35 OUT VOID *Buffer,
36 IN UINTN Length,
37 IN UINT16 Value
38 )
39 {
40 do {
41 ((UINT16*)Buffer)[--Length] = Value;
42 } while (Length != 0);
43 return Buffer;
44 }
45
46 /**
47 Fills a target buffer with a 32-bit value, and returns the target buffer.
48
49 @param Buffer The pointer to the target buffer to fill.
50 @param Length The count of 32-bit value to fill.
51 @param Value The value with which to fill Length bytes of Buffer.
52
53 @return Buffer
54
55 **/
56 VOID *
57 EFIAPI
InternalMemSetMem32(OUT VOID * Buffer,IN UINTN Length,IN UINT32 Value)58 InternalMemSetMem32 (
59 OUT VOID *Buffer,
60 IN UINTN Length,
61 IN UINT32 Value
62 )
63 {
64 do {
65 ((UINT32*)Buffer)[--Length] = Value;
66 } while (Length != 0);
67 return Buffer;
68 }
69
70 /**
71 Fills a target buffer with a 64-bit value, and returns the target buffer.
72
73 @param Buffer The pointer to the target buffer to fill.
74 @param Length The count of 64-bit value to fill.
75 @param Value The value with which to fill Length bytes of Buffer.
76
77 @return Buffer
78
79 **/
80 VOID *
81 EFIAPI
InternalMemSetMem64(OUT VOID * Buffer,IN UINTN Length,IN UINT64 Value)82 InternalMemSetMem64 (
83 OUT VOID *Buffer,
84 IN UINTN Length,
85 IN UINT64 Value
86 )
87 {
88 do {
89 ((UINT64*)Buffer)[--Length] = Value;
90 } while (Length != 0);
91 return Buffer;
92 }
93
94 /**
95 Set Buffer to 0 for Size bytes.
96
97 @param Buffer Memory to set.
98 @param Length The number of bytes to set
99
100 @return Buffer
101
102 **/
103 VOID *
104 EFIAPI
InternalMemZeroMem(OUT VOID * Buffer,IN UINTN Length)105 InternalMemZeroMem (
106 OUT VOID *Buffer,
107 IN UINTN Length
108 )
109 {
110 return InternalMemSetMem (Buffer, Length, 0);
111 }
112
113 /**
114 Compares two memory buffers of a given length.
115
116 @param DestinationBuffer The first memory buffer
117 @param SourceBuffer The second memory buffer
118 @param Length The length of DestinationBuffer and SourceBuffer memory
119 regions to compare. Must be non-zero.
120
121 @return 0 All Length bytes of the two buffers are identical.
122 @retval Non-zero The first mismatched byte in SourceBuffer subtracted from the first
123 mismatched byte in DestinationBuffer.
124
125 **/
126 INTN
127 EFIAPI
InternalMemCompareMem(IN CONST VOID * DestinationBuffer,IN CONST VOID * SourceBuffer,IN UINTN Length)128 InternalMemCompareMem (
129 IN CONST VOID *DestinationBuffer,
130 IN CONST VOID *SourceBuffer,
131 IN UINTN Length
132 )
133 {
134 while ((--Length != 0) &&
135 (*(INT8*)DestinationBuffer == *(INT8*)SourceBuffer)) {
136 DestinationBuffer = (INT8*)DestinationBuffer + 1;
137 SourceBuffer = (INT8*)SourceBuffer + 1;
138 }
139 return (INTN)*(UINT8*)DestinationBuffer - (INTN)*(UINT8*)SourceBuffer;
140 }
141
142 /**
143 Scans a target buffer for an 8-bit value, and returns a pointer to the
144 matching 8-bit value in the target buffer.
145
146 @param Buffer The pointer to the target buffer to scan.
147 @param Length The count of 8-bit value to scan. Must be non-zero.
148 @param Value The value to search for in the target buffer.
149
150 @return The pointer to the first occurrence or NULL if not found.
151
152 **/
153 CONST VOID *
154 EFIAPI
InternalMemScanMem8(IN CONST VOID * Buffer,IN UINTN Length,IN UINT8 Value)155 InternalMemScanMem8 (
156 IN CONST VOID *Buffer,
157 IN UINTN Length,
158 IN UINT8 Value
159 )
160 {
161 CONST UINT8 *Pointer;
162
163 Pointer = (CONST UINT8*)Buffer;
164 do {
165 if (*(Pointer++) == Value) {
166 return --Pointer;
167 }
168 } while (--Length != 0);
169 return NULL;
170 }
171
172 /**
173 Scans a target buffer for a 16-bit value, and returns a pointer to the
174 matching 16-bit value in the target buffer.
175
176 @param Buffer The pointer to the target buffer to scan.
177 @param Length The count of 16-bit value to scan. Must be non-zero.
178 @param Value The value to search for in the target buffer.
179
180 @return The pointer to the first occurrence or NULL if not found.
181
182 **/
183 CONST VOID *
184 EFIAPI
InternalMemScanMem16(IN CONST VOID * Buffer,IN UINTN Length,IN UINT16 Value)185 InternalMemScanMem16 (
186 IN CONST VOID *Buffer,
187 IN UINTN Length,
188 IN UINT16 Value
189 )
190 {
191 CONST UINT16 *Pointer;
192
193 Pointer = (CONST UINT16*)Buffer;
194 do {
195 if (*(Pointer++) == Value) {
196 return --Pointer;
197 }
198 } while (--Length != 0);
199 return NULL;
200 }
201
202 /**
203 Scans a target buffer for a 32-bit value, and returns a pointer to the
204 matching 32-bit value in the target buffer.
205
206 @param Buffer The pointer to the target buffer to scan.
207 @param Length The count of 32-bit value to scan. Must be non-zero.
208 @param Value The value to search for in the target buffer.
209
210 @return The pointer to the first occurrence or NULL if not found.
211
212 **/
213 CONST VOID *
214 EFIAPI
InternalMemScanMem32(IN CONST VOID * Buffer,IN UINTN Length,IN UINT32 Value)215 InternalMemScanMem32 (
216 IN CONST VOID *Buffer,
217 IN UINTN Length,
218 IN UINT32 Value
219 )
220 {
221 CONST UINT32 *Pointer;
222
223 Pointer = (CONST UINT32*)Buffer;
224 do {
225 if (*(Pointer++) == Value) {
226 return --Pointer;
227 }
228 } while (--Length != 0);
229 return NULL;
230 }
231
232 /**
233 Scans a target buffer for a 64-bit value, and returns a pointer to the
234 matching 64-bit value in the target buffer.
235
236 @param Buffer The pointer to the target buffer to scan.
237 @param Length The count of 64-bit value to scan. Must be non-zero.
238 @param Value The value to search for in the target buffer.
239
240 @return The pointer to the first occurrence or NULL if not found.
241
242 **/
243 CONST VOID *
244 EFIAPI
InternalMemScanMem64(IN CONST VOID * Buffer,IN UINTN Length,IN UINT64 Value)245 InternalMemScanMem64 (
246 IN CONST VOID *Buffer,
247 IN UINTN Length,
248 IN UINT64 Value
249 )
250 {
251 CONST UINT64 *Pointer;
252
253 Pointer = (CONST UINT64*)Buffer;
254 do {
255 if (*(Pointer++) == Value) {
256 return --Pointer;
257 }
258 } while (--Length != 0);
259 return NULL;
260 }
261