]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/C/Common/CommonLib.h
4e1541bc705e47278d52ed271f04cd560e7cd959
[mirror_edk2.git] / BaseTools / Source / C / Common / CommonLib.h
1 /** @file
2 Common library assistance routines.
3
4 Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #ifndef _EFI_COMMON_LIB_H
16 #define _EFI_COMMON_LIB_H
17
18 #include <Common/UefiBaseTypes.h>
19 #include <Common/BuildVersion.h>
20 #include <assert.h>
21 #define PRINTED_GUID_BUFFER_SIZE 37 // including null-termination
22
23 #define MAX_LONG_FILE_PATH 500
24
25 #define MAX_UINT64 ((UINT64)0xFFFFFFFFFFFFFFFFULL)
26 #define MAX_UINT32 ((UINT32)0xFFFFFFFF)
27 #define MAX_UINT16 ((UINT16)0xFFFF)
28 #define MAX_UINT8 ((UINT8)0xFF)
29 #define ARRAY_SIZE(Array) (sizeof (Array) / sizeof ((Array)[0]))
30 #define ASCII_RSIZE_MAX 1000000
31 #undef RSIZE_MAX
32 #define RSIZE_MAX 1000000
33
34 #define IS_COMMA(a) ((a) == L',')
35 #define IS_HYPHEN(a) ((a) == L'-')
36 #define IS_DOT(a) ((a) == L'.')
37 #define IS_LEFT_PARENTH(a) ((a) == L'(')
38 #define IS_RIGHT_PARENTH(a) ((a) == L')')
39 #define IS_SLASH(a) ((a) == L'/')
40 #define IS_NULL(a) ((a) == L'\0')
41
42 #define ASSERT(x) assert(x)
43
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47
48 //
49 // Function declarations
50 //
51 VOID
52 PeiZeroMem (
53 IN VOID *Buffer,
54 IN UINTN Size
55 )
56 ;
57
58 VOID
59 PeiCopyMem (
60 IN VOID *Destination,
61 IN VOID *Source,
62 IN UINTN Length
63 )
64 ;
65
66 VOID
67 ZeroMem (
68 IN VOID *Buffer,
69 IN UINTN Size
70 )
71 ;
72
73 VOID
74 CopyMem (
75 IN VOID *Destination,
76 IN VOID *Source,
77 IN UINTN Length
78 )
79 ;
80
81 INTN
82 CompareGuid (
83 IN EFI_GUID *Guid1,
84 IN EFI_GUID *Guid2
85 )
86 ;
87
88 EFI_STATUS
89 GetFileImage (
90 IN CHAR8 *InputFileName,
91 OUT CHAR8 **InputFileImage,
92 OUT UINT32 *BytesRead
93 )
94 ;
95
96 EFI_STATUS
97 PutFileImage (
98 IN CHAR8 *OutputFileName,
99 IN CHAR8 *OutputFileImage,
100 IN UINT32 BytesToWrite
101 )
102 ;
103 /*++
104
105 Routine Description:
106
107 This function opens a file and writes OutputFileImage into the file.
108
109 Arguments:
110
111 OutputFileName The name of the file to write.
112 OutputFileImage A pointer to the memory buffer.
113 BytesToWrite The size of the memory buffer.
114
115 Returns:
116
117 EFI_SUCCESS The function completed successfully.
118 EFI_INVALID_PARAMETER One of the input parameters was invalid.
119 EFI_ABORTED An error occurred.
120 EFI_OUT_OF_RESOURCES No resource to complete operations.
121
122 **/
123
124 UINT8
125 CalculateChecksum8 (
126 IN UINT8 *Buffer,
127 IN UINTN Size
128 )
129 ;
130
131 UINT8
132 CalculateSum8 (
133 IN UINT8 *Buffer,
134 IN UINTN Size
135 )
136 ;
137
138 UINT16
139 CalculateChecksum16 (
140 IN UINT16 *Buffer,
141 IN UINTN Size
142 )
143 ;
144
145 UINT16
146 CalculateSum16 (
147 IN UINT16 *Buffer,
148 IN UINTN Size
149 )
150 ;
151
152 EFI_STATUS
153 PrintGuid (
154 IN EFI_GUID *Guid
155 )
156 ;
157
158 #define PRINTED_GUID_BUFFER_SIZE 37 // including null-termination
159 EFI_STATUS
160 PrintGuidToBuffer (
161 IN EFI_GUID *Guid,
162 IN OUT UINT8 *Buffer,
163 IN UINT32 BufferLen,
164 IN BOOLEAN Uppercase
165 )
166 ;
167
168 CHAR8 *
169 LongFilePath (
170 IN CHAR8 *FileName
171 );
172
173 UINTN
174 StrLen (
175 CONST CHAR16 *String
176 );
177
178 VOID *
179 AllocateCopyPool (
180 UINTN AllocationSize,
181 CONST VOID *Buffer
182 );
183
184 INTN
185 StrnCmp (
186 CONST CHAR16 *FirstString,
187 CONST CHAR16 *SecondString,
188 UINTN Length
189 );
190
191 RETURN_STATUS
192 StrToGuid (
193 CONST CHAR16 *String,
194 EFI_GUID *Guid
195 );
196
197 RETURN_STATUS
198 StrHexToBytes (
199 CONST CHAR16 *String,
200 UINTN Length,
201 UINT8 *Buffer,
202 UINTN MaxBufferSize
203 );
204
205 UINTN
206 InternalHexCharToUintn (
207 CHAR16 Char
208 );
209
210 VOID *
211 InternalAllocateCopyPool (
212 UINTN AllocationSize,
213 CONST VOID *Buffer
214 );
215
216 BOOLEAN
217 InternalIsDecimalDigitCharacter (
218 CHAR16 Char
219 );
220
221 UINT32
222 SwapBytes32 (
223 UINT32 Value
224 );
225
226 UINT16
227 SwapBytes16 (
228 UINT16 Value
229 );
230
231 EFI_GUID *
232 CopyGuid (
233 EFI_GUID *DestinationGuid,
234 CONST EFI_GUID *SourceGuid
235 );
236
237 UINT64
238 WriteUnaligned64 (
239 UINT64 *Buffer,
240 UINT64 Value
241 );
242
243 UINT64
244 ReadUnaligned64 (
245 CONST UINT64 *Buffer
246 );
247
248 UINTN
249 StrSize (
250 CONST CHAR16 *String
251 );
252
253 UINT64
254 StrHexToUint64 (
255 CONST CHAR16 *String
256 );
257
258 UINT64
259 StrDecimalToUint64 (
260 CONST CHAR16 *String
261 );
262
263 RETURN_STATUS
264 StrHexToUint64S (
265 CONST CHAR16 *String,
266 CHAR16 **EndPointer,
267 UINT64 *Data
268 );
269
270 RETURN_STATUS
271 StrDecimalToUint64S (
272 CONST CHAR16 *String,
273 CHAR16 **EndPointer, OPTIONAL
274 UINT64 *Data
275 );
276
277 VOID *
278 ReallocatePool (
279 UINTN OldSize,
280 UINTN NewSize,
281 VOID *OldBuffer OPTIONAL
282 );
283
284 VOID *
285 InternalReallocatePool (
286 UINTN OldSize,
287 UINTN NewSize,
288 VOID *OldBuffer OPTIONAL
289 );
290
291 VOID *
292 InternalAllocateZeroPool (
293 UINTN AllocationSize
294 ) ;
295
296 VOID *
297 InternalAllocatePool (
298 UINTN AllocationSize
299 );
300
301 UINTN
302 StrnLenS (
303 CONST CHAR16 *String,
304 UINTN MaxSize
305 );
306
307 CHAR16
308 InternalCharToUpper (
309 CHAR16 Char
310 );
311
312 INTN
313 StrCmp (
314 CONST CHAR16 *FirstString,
315 CONST CHAR16 *SecondString
316 );
317
318 UINT64
319 SwapBytes64 (
320 UINT64 Value
321 );
322
323 UINT64
324 InternalMathSwapBytes64 (
325 UINT64 Operand
326 );
327
328 RETURN_STATUS
329 StrToIpv4Address (
330 CONST CHAR16 *String,
331 CHAR16 **EndPointer,
332 EFI_IPv4_ADDRESS *Address,
333 UINT8 *PrefixLength
334 );
335
336 RETURN_STATUS
337 StrToIpv6Address (
338 CONST CHAR16 *String,
339 CHAR16 **EndPointer,
340 EFI_IPv6_ADDRESS *Address,
341 UINT8 *PrefixLength
342 );
343
344 RETURN_STATUS
345 StrCpyS (
346 CHAR16 *Destination,
347 UINTN DestMax,
348 CONST CHAR16 *Source
349 );
350
351 RETURN_STATUS
352 UnicodeStrToAsciiStrS (
353 CONST CHAR16 *Source,
354 CHAR8 *Destination,
355 UINTN DestMax
356 );
357 VOID *
358 AllocatePool (
359 UINTN AllocationSize
360 );
361
362 UINT16
363 WriteUnaligned16 (
364 UINT16 *Buffer,
365 UINT16 Value
366 );
367
368 UINT16
369 ReadUnaligned16 (
370 CONST UINT16 *Buffer
371 );
372
373 VOID *
374 AllocateZeroPool (
375 UINTN AllocationSize
376 );
377
378 BOOLEAN
379 InternalIsHexaDecimalDigitCharacter (
380 CHAR16 Char
381 );
382
383 BOOLEAN
384 InternalSafeStringIsOverlap (
385 IN VOID *Base1,
386 IN UINTN Size1,
387 IN VOID *Base2,
388 IN UINTN Size2
389 );
390
391 BOOLEAN
392 InternalSafeStringNoStrOverlap (
393 IN CHAR16 *Str1,
394 IN UINTN Size1,
395 IN CHAR16 *Str2,
396 IN UINTN Size2
397 );
398
399 BOOLEAN
400 IsHexStr (
401 CHAR16 *Str
402 );
403
404 UINTN
405 Strtoi (
406 CHAR16 *Str
407 );
408
409 VOID
410 Strtoi64 (
411 CHAR16 *Str,
412 UINT64 *Data
413 );
414
415 VOID
416 StrToAscii (
417 CHAR16 *Str,
418 CHAR8 **AsciiStr
419 );
420
421 CHAR16 *
422 SplitStr (
423 CHAR16 **List,
424 CHAR16 Separator
425 );
426
427 /*++
428
429 Routine Description:
430 Convert FileName to the long file path, which can support larger than 260 length.
431
432 Arguments:
433 FileName - FileName.
434
435 Returns:
436 LongFilePath A pointer to the converted long file path.
437
438 --*/
439
440 #ifdef __cplusplus
441 }
442 #endif
443
444 #ifdef __GNUC__
445 #include <stdio.h>
446 #include <sys/stat.h>
447 #define stricmp strcasecmp
448 #define _stricmp strcasecmp
449 #define strnicmp strncasecmp
450 #define strcmpi strcasecmp
451 size_t _filelength(int fd);
452 #ifndef __CYGWIN__
453 char *strlwr(char *s);
454 #endif
455 #endif
456
457 //
458 // On windows, mkdir only has one parameter.
459 // On unix, it has two parameters
460 //
461 #if defined(__GNUC__)
462 #define mkdir(dir, perm) mkdir(dir, perm)
463 #else
464 #define mkdir(dir, perm) mkdir(dir)
465 #endif
466
467 #endif