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