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