dccb19223ba2bac09aa8cde2ad84050648e9dd46
[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_UINTN MAX_ADDRESS
26 #define MAX_UINT64 ((UINT64)0xFFFFFFFFFFFFFFFFULL)
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 UINTN
254 StrHexToUintn (
255 CONST CHAR16 *String
256 );
257
258 UINTN
259 StrDecimalToUintn (
260 CONST CHAR16 *String
261 );
262
263 UINT64
264 StrHexToUint64 (
265 CONST CHAR16 *String
266 );
267
268 UINT64
269 StrDecimalToUint64 (
270 CONST CHAR16 *String
271 );
272
273 RETURN_STATUS
274 StrHexToUint64S (
275 CONST CHAR16 *String,
276 CHAR16 **EndPointer,
277 UINT64 *Data
278 );
279
280 RETURN_STATUS
281 StrHexToUintnS (
282 CONST CHAR16 *String,
283 CHAR16 **EndPointer, OPTIONAL
284 UINTN *Data
285 );
286
287 RETURN_STATUS
288 StrDecimalToUint64S (
289 CONST CHAR16 *String,
290 CHAR16 **EndPointer, OPTIONAL
291 UINT64 *Data
292 );
293
294 RETURN_STATUS
295 StrDecimalToUintnS (
296 CONST CHAR16 *String,
297 CHAR16 **EndPointer, OPTIONAL
298 UINTN *Data
299 );
300
301 VOID *
302 ReallocatePool (
303 UINTN OldSize,
304 UINTN NewSize,
305 VOID *OldBuffer OPTIONAL
306 );
307
308 VOID *
309 InternalReallocatePool (
310 UINTN OldSize,
311 UINTN NewSize,
312 VOID *OldBuffer OPTIONAL
313 );
314
315 VOID *
316 InternalAllocateZeroPool (
317 UINTN AllocationSize
318 ) ;
319
320 VOID *
321 InternalAllocatePool (
322 UINTN AllocationSize
323 );
324
325 UINTN
326 StrnLenS (
327 CONST CHAR16 *String,
328 UINTN MaxSize
329 );
330
331 CHAR16
332 InternalCharToUpper (
333 CHAR16 Char
334 );
335
336 INTN
337 StrCmp (
338 CONST CHAR16 *FirstString,
339 CONST CHAR16 *SecondString
340 );
341
342 UINT64
343 SwapBytes64 (
344 UINT64 Value
345 );
346
347 UINT64
348 InternalMathSwapBytes64 (
349 UINT64 Operand
350 );
351
352 RETURN_STATUS
353 StrToIpv4Address (
354 CONST CHAR16 *String,
355 CHAR16 **EndPointer,
356 EFI_IPv4_ADDRESS *Address,
357 UINT8 *PrefixLength
358 );
359
360 RETURN_STATUS
361 StrToIpv6Address (
362 CONST CHAR16 *String,
363 CHAR16 **EndPointer,
364 EFI_IPv6_ADDRESS *Address,
365 UINT8 *PrefixLength
366 );
367
368 RETURN_STATUS
369 StrCpyS (
370 CHAR16 *Destination,
371 UINTN DestMax,
372 CONST CHAR16 *Source
373 );
374
375 RETURN_STATUS
376 UnicodeStrToAsciiStrS (
377 CONST CHAR16 *Source,
378 CHAR8 *Destination,
379 UINTN DestMax
380 );
381 VOID *
382 AllocatePool (
383 UINTN AllocationSize
384 );
385
386 UINT16
387 WriteUnaligned16 (
388 UINT16 *Buffer,
389 UINT16 Value
390 );
391
392 UINT16
393 ReadUnaligned16 (
394 CONST UINT16 *Buffer
395 );
396
397 VOID *
398 AllocateZeroPool (
399 UINTN AllocationSize
400 );
401
402 BOOLEAN
403 InternalIsHexaDecimalDigitCharacter (
404 CHAR16 Char
405 );
406
407 BOOLEAN
408 InternalSafeStringIsOverlap (
409 IN VOID *Base1,
410 IN UINTN Size1,
411 IN VOID *Base2,
412 IN UINTN Size2
413 );
414
415 BOOLEAN
416 InternalSafeStringNoStrOverlap (
417 IN CHAR16 *Str1,
418 IN UINTN Size1,
419 IN CHAR16 *Str2,
420 IN UINTN Size2
421 );
422
423 BOOLEAN
424 IsHexStr (
425 CHAR16 *Str
426 );
427
428 UINTN
429 Strtoi (
430 CHAR16 *Str
431 );
432
433 VOID
434 Strtoi64 (
435 CHAR16 *Str,
436 UINT64 *Data
437 );
438
439 VOID
440 StrToAscii (
441 CHAR16 *Str,
442 CHAR8 **AsciiStr
443 );
444
445 CHAR16 *
446 SplitStr (
447 CHAR16 **List,
448 CHAR16 Separator
449 );
450
451 /*++
452
453 Routine Description:
454 Convert FileName to the long file path, which can support larger than 260 length.
455
456 Arguments:
457 FileName - FileName.
458
459 Returns:
460 LongFilePath A pointer to the converted long file path.
461
462 --*/
463
464 #ifdef __cplusplus
465 }
466 #endif
467
468 #ifdef __GNUC__
469 #include <stdio.h>
470 #include <sys/stat.h>
471 #define stricmp strcasecmp
472 #define _stricmp strcasecmp
473 #define strnicmp strncasecmp
474 #define strcmpi strcasecmp
475 size_t _filelength(int fd);
476 #ifndef __CYGWIN__
477 char *strlwr(char *s);
478 #endif
479 #endif
480
481 //
482 // On windows, mkdir only has one parameter.
483 // On unix, it has two parameters
484 //
485 #if defined(__GNUC__)
486 #define mkdir(dir, perm) mkdir(dir, perm)
487 #else
488 #define mkdir(dir, perm) mkdir(dir)
489 #endif
490
491 #endif