]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseMemoryLib/MemLibGeneric.c
MdePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / MdePkg / Library / BaseMemoryLib / MemLibGeneric.c
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 - 2016, Intel Corporation. All rights reserved.<BR>
10 SPDX-License-Identifier: BSD-2-Clause-Patent
11
12 **/
13
14 #include "MemLibInternals.h"
15
16 /**
17 Fills a target buffer with a 16-bit value, and returns the target buffer.
18
19 @param Buffer The pointer to the target buffer to fill.
20 @param Length The count of 16-bit value to fill.
21 @param Value The value with which to fill Length bytes of Buffer.
22
23 @return Buffer
24
25 **/
26 VOID *
27 EFIAPI
28 InternalMemSetMem16 (
29 OUT VOID *Buffer,
30 IN UINTN Length,
31 IN UINT16 Value
32 )
33 {
34 for (; Length != 0; Length--) {
35 ((UINT16*)Buffer)[Length - 1] = Value;
36 }
37 return Buffer;
38 }
39
40 /**
41 Fills a target buffer with a 32-bit value, and returns the target buffer.
42
43 @param Buffer The pointer to the target buffer to fill.
44 @param Length The count of 32-bit value to fill.
45 @param Value The value with which to fill Length bytes of Buffer.
46
47 @return Buffer
48
49 **/
50 VOID *
51 EFIAPI
52 InternalMemSetMem32 (
53 OUT VOID *Buffer,
54 IN UINTN Length,
55 IN UINT32 Value
56 )
57 {
58 for (; Length != 0; Length--) {
59 ((UINT32*)Buffer)[Length - 1] = Value;
60 }
61 return Buffer;
62 }
63
64 /**
65 Fills a target buffer with a 64-bit value, and returns the target buffer.
66
67 @param Buffer The pointer to the target buffer to fill.
68 @param Length The count of 64-bit value to fill.
69 @param Value The value with which to fill Length bytes of Buffer.
70
71 @return Buffer
72
73 **/
74 VOID *
75 EFIAPI
76 InternalMemSetMem64 (
77 OUT VOID *Buffer,
78 IN UINTN Length,
79 IN UINT64 Value
80 )
81 {
82 for (; Length != 0; Length--) {
83 ((UINT64*)Buffer)[Length - 1] = Value;
84 }
85 return Buffer;
86 }
87
88 /**
89 Set Buffer to 0 for Size bytes.
90
91 @param Buffer Memory to set.
92 @param Length The number of bytes to set.
93
94 @return Buffer
95
96 **/
97 VOID *
98 EFIAPI
99 InternalMemZeroMem (
100 OUT VOID *Buffer,
101 IN UINTN Length
102 )
103 {
104 return InternalMemSetMem (Buffer, Length, 0);
105 }
106
107 /**
108 Compares two memory buffers of a given length.
109
110 @param DestinationBuffer The first memory buffer.
111 @param SourceBuffer The second memory buffer.
112 @param Length Length of DestinationBuffer and SourceBuffer memory
113 regions to compare. Must be non-zero.
114
115 @return 0 All Length bytes of the two buffers are identical.
116 @retval Non-zero The first mismatched byte in SourceBuffer subtracted from the first
117 mismatched byte in DestinationBuffer.
118
119 **/
120 INTN
121 EFIAPI
122 InternalMemCompareMem (
123 IN CONST VOID *DestinationBuffer,
124 IN CONST VOID *SourceBuffer,
125 IN UINTN Length
126 )
127 {
128 while ((--Length != 0) &&
129 (*(INT8*)DestinationBuffer == *(INT8*)SourceBuffer)) {
130 DestinationBuffer = (INT8*)DestinationBuffer + 1;
131 SourceBuffer = (INT8*)SourceBuffer + 1;
132 }
133 return (INTN)*(UINT8*)DestinationBuffer - (INTN)*(UINT8*)SourceBuffer;
134 }
135
136 /**
137 Scans a target buffer for an 8-bit value, and returns a pointer to the
138 matching 8-bit value in the target buffer.
139
140 @param Buffer The pointer to the target buffer to scan.
141 @param Length The count of 8-bit value to scan. Must be non-zero.
142 @param Value The value to search for in the target buffer.
143
144 @return The pointer to the first occurrence, or NULL if not found.
145
146 **/
147 CONST VOID *
148 EFIAPI
149 InternalMemScanMem8 (
150 IN CONST VOID *Buffer,
151 IN UINTN Length,
152 IN UINT8 Value
153 )
154 {
155 CONST UINT8 *Pointer;
156
157 Pointer = (CONST UINT8*)Buffer;
158 do {
159 if (*Pointer == Value) {
160 return Pointer;
161 }
162 ++Pointer;
163 } while (--Length != 0);
164 return NULL;
165 }
166
167 /**
168 Scans a target buffer for a 16-bit value, and returns a pointer to the
169 matching 16-bit value in the target buffer.
170
171 @param Buffer The pointer to the target buffer to scan.
172 @param Length The count of 16-bit value to scan. Must be non-zero.
173 @param Value The value to search for in the target buffer.
174
175 @return The pointer to the first occurrence, or NULL if not found.
176
177 **/
178 CONST VOID *
179 EFIAPI
180 InternalMemScanMem16 (
181 IN CONST VOID *Buffer,
182 IN UINTN Length,
183 IN UINT16 Value
184 )
185 {
186 CONST UINT16 *Pointer;
187
188 Pointer = (CONST UINT16*)Buffer;
189 do {
190 if (*Pointer == Value) {
191 return Pointer;
192 }
193 ++Pointer;
194 } while (--Length != 0);
195 return NULL;
196 }
197
198 /**
199 Scans a target buffer for a 32-bit value, and returns a pointer to the
200 matching 32-bit value in the target buffer.
201
202 @param Buffer The pointer to the target buffer to scan.
203 @param Length The count of 32-bit value to scan. Must be non-zero.
204 @param Value The value to search for in the target buffer.
205
206 @return The pointer to the first occurrence, or NULL if not found.
207
208 **/
209 CONST VOID *
210 EFIAPI
211 InternalMemScanMem32 (
212 IN CONST VOID *Buffer,
213 IN UINTN Length,
214 IN UINT32 Value
215 )
216 {
217 CONST UINT32 *Pointer;
218
219 Pointer = (CONST UINT32*)Buffer;
220 do {
221 if (*Pointer == Value) {
222 return Pointer;
223 }
224 ++Pointer;
225 } while (--Length != 0);
226 return NULL;
227 }
228
229 /**
230 Scans a target buffer for a 64-bit value, and returns a pointer to the
231 matching 64-bit value in the target buffer.
232
233 @param Buffer The pointer to the target buffer to scan.
234 @param Length The count of 64-bit value to scan. Must be non-zero.
235 @param Value The value to search for in the target buffer.
236
237 @return The pointer to the first occurrence, or NULL if not found.
238
239 **/
240 CONST VOID *
241 EFIAPI
242 InternalMemScanMem64 (
243 IN CONST VOID *Buffer,
244 IN UINTN Length,
245 IN UINT64 Value
246 )
247 {
248 CONST UINT64 *Pointer;
249
250 Pointer = (CONST UINT64*)Buffer;
251 do {
252 if (*Pointer == Value) {
253 return Pointer;
254 }
255 ++Pointer;
256 } while (--Length != 0);
257 return NULL;
258 }
259
260 /**
261 Checks whether the contents of a buffer are all zeros.
262
263 @param Buffer The pointer to the buffer to be checked.
264 @param Length The size of the buffer (in bytes) to be checked.
265
266 @retval TRUE Contents of the buffer are all zeros.
267 @retval FALSE Contents of the buffer are not all zeros.
268
269 **/
270 BOOLEAN
271 EFIAPI
272 InternalMemIsZeroBuffer (
273 IN CONST VOID *Buffer,
274 IN UINTN Length
275 )
276 {
277 CONST UINT8 *BufferData;
278 UINTN Index;
279
280 BufferData = Buffer;
281 for (Index = 0; Index < Length; Index++) {
282 if (BufferData[Index] != 0) {
283 return FALSE;
284 }
285 }
286 return TRUE;
287 }