forked from acidanthera/gfxutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.h
187 lines (154 loc) · 7.24 KB
/
utils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#ifndef _UTILS_H
#define _UTILS_H
#include "UefiDevicePathLib.h"
/*
* alpha = lowalpha | upalpha
*/
#define IS_ALPHA(x) (IS_LOWALPHA(x) || IS_UPALPHA(x))
/*
* lowalpha = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" |
* "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" |
* "u" | "v" | "w" | "x" | "y" | "z"
*/
#define IS_LOWALPHA(x) (((unsigned char)(x) >= 'a') && ((unsigned char)(x) <= 'z'))
/*
* upalpha = "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" |
* "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" | "S" | "T" |
* "U" | "V" | "W" | "X" | "Y" | "Z"
*/
#define IS_UPALPHA(x) (((unsigned char)(x) >= 'A') && ((unsigned char)(x) <= 'Z'))
#ifdef IS_DIGIT
#undef IS_DIGIT
#endif
/*
* digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
*/
#define IS_DIGIT(x) (((unsigned char)(x) >= '0') && ((unsigned char)(x) <= '9'))
/*
* alphanum = alpha | digit
*/
#define IS_ALPHANUM(x) (IS_ALPHA(x) || IS_DIGIT(x))
/*
* hex = digit | "A" | "B" | "C" | "D" | "E" | "F" |
* "a" | "b" | "c" | "d" | "e" | "f"
*/
#define IS_HEX(x) ((IS_DIGIT(x)) || (((unsigned char)(x) >= 'a') && ((unsigned char)(x) <= 'f')) || \
(((unsigned char)(x) >= 'A') && ((unsigned char)(x) <= 'F')))
#define IS_LOWER_HEX(x) ((IS_DIGIT(x)) || (((unsigned char)(x) >= 'a') && ((unsigned char)(x) <= 'f')))
/*
* mark = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")" | ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | "$" | "," |
* "[" | "]"
*/
#define IS_MARK(x) (((unsigned char)(x) == '-') || ((unsigned char)(x) == '_') || ((unsigned char)(x) == '.') || ((unsigned char)(x) == '!') || ((unsigned char)(x) == '~') || ((unsigned char)(x) == '*') || ((unsigned char)(x) == '\'') || \
((unsigned char)(x) == '(') || ((unsigned char)(x) == ')') || ((unsigned char)(x) == ';') || ((unsigned char)(x) == '/') || ((unsigned char)(x) == '?') || ((unsigned char)(x) == ':') || ((unsigned char)(x) == '@') || ((unsigned char)(x) == '&') || ((unsigned char)(x) == '=') || \
((unsigned char)(x) == '+') || ((unsigned char)(x) == '$') || ((unsigned char)(x) == ',') || ((unsigned char)(x) == '[') || ((unsigned char)(x) == ']'))
#define IS_SPACE(x) (((unsigned char)(x) == ' '))
/*
* alphanummark = alphanum | mark
*/
#define IS_ALPHANUMMARK(x) (IS_ALPHANUM(x) || IS_MARK(x) || IS_SPACE(x))
/*
* Read little-endian values of various sizes from memory buffers.
*/
#define _READ_BYTE(buf,offset) ((UINT32)(UINT8)(((UINT8 *)(buf))[(offset)]))
#define _READ_BYTE_SHIFT(buf,offset,shift) (((UINT32)(UINT8)(((UINT8 *)(buf))[(offset)])) << (shift))
#define READ_INT8(buf) ((INT8)(_READ_BYTE((buf), 0)))
#define READ_UINT8(buf) ((UINT8)(_READ_BYTE((buf), 0)))
#define READ_INT16(buf) ((INT16)(_READ_BYTE((buf), 0) | _READ_BYTE_SHIFT((buf), 1, 8)))
#define READ_UINT16(buf) ((UINT16)(_READ_BYTE((buf), 0) | _READ_BYTE_SHIFT((buf), 1, 8)))
#define READ_INT32(buf) ((INT32)(_READ_BYTE((buf), 0) | _READ_BYTE_SHIFT((buf), 1, 8) | _READ_BYTE_SHIFT((buf), 2, 16) | _READ_BYTE_SHIFT((buf), 3, 24)))
#define READ_UINT32(buf) ((UINT32)(_READ_BYTE((buf), 0) | _READ_BYTE_SHIFT((buf), 1, 8) | _READ_BYTE_SHIFT((buf), 2, 16) | _READ_BYTE_SHIFT((buf), 3, 24)))
#define READ_INT64(buf) (((INT64)(READ_UINT32((buf)))) | (((INT64)(READ_INT32(((UINT8 *)(buf)) + 4))) << 32))
#define READ_UINT64(buf) (((UINT64)(READ_UINT32((buf)))) | (((UINT64)(READ_UINT32(((UINT8 *)(buf)) + 4))) << 32))
/*
* Write little-endian values of various sizes to memory buffers.
*/
#define WRITE_UINT8(buf, value) \
do { \
(buf)[0] = (unsigned char)(value & 0xFF); \
} while (0)
#define WRITE_UINT16(buf, value) \
do { \
(buf)[0] = (unsigned char)(value & 0xFF); \
(buf)[1] = (unsigned char)(((value) >> 8) & 0xFF); \
} while (0)
#define WRITE_INT16(buf, value) WRITE_UINT16((buf), (UINT16)(value))
#define WRITE_UINT32(buf, value) \
do { \
(buf)[0] = (unsigned char)(value & 0xFF); \
(buf)[1] = (unsigned char)(((value) >> 8) & 0xFF); \
(buf)[2] = (unsigned char)(((value) >> 16) & 0xFF); \
(buf)[3] = (unsigned char)(((value) >> 24) & 0xFF); \
} while (0)
#define WRITE_INT32(buf, value) WRITE_UINT32((buf), (UINT32)(value))
#define WRITE_UINT64(buf, value) \
do { \
(buf)[0] = (unsigned char)(value & 0xFF); \
(buf)[1] = (unsigned char)(((value) >> 8) & 0xFF); \
(buf)[2] = (unsigned char)(((value) >> 16) & 0xFF); \
(buf)[3] = (unsigned char)(((value) >> 24) & 0xFF); \
(buf)[4] = (unsigned char)(((value) >> 32) & 0xFF); \
(buf)[5] = (unsigned char)(((value) >> 40) & 0xFF); \
(buf)[6] = (unsigned char)(((value) >> 48) & 0xFF); \
(buf)[7] = (unsigned char)(((value) >> 56) & 0xFF); \
} while (0)
#define WRITE_INT64(buf, value) WRITE_UINT64((buf), (UINT64)(value))
void assertion(int condition, char * message);
/*
* Read a UTF-8 character from a string position.
*/
unsigned long UTF8ReadChar(const void *str, int len, int *posn);
/*
* Write a UTF-8 character to a buffer. Returns the
* number of bytes used. If the buffer is NULL, then
* return the number of bytes needed.
*/
int UTF8WriteChar(char *str, unsigned long ch);
/*
* Read a UTF-16 character from a 16-bit string position.
* "len" and "posn" are indexes into a 16-bit array.
*/
unsigned long UTF16ReadChar(const unsigned short *str, int len, int *posn);
/*
* Read a UTF-16 character from a string as little-endian values.
* "len" and "posn" are indexes into a byte array.
*/
unsigned long UTF16ReadCharAsBytes(const void *str, int len, int *posn);
/*
* Convert a 32-bit Unicode character into UTF-16. Returns the
* number of 16-bit characters required (1 or 2), or zero if
* the character cannot be represented using UTF-16. If "buf"
* is NULL, then return the number of characters required.
*/
int UTF16WriteChar(unsigned short *buf, unsigned long ch);
/*
* Convert a 32-bit Unicode character into UTF-16, and store it
* using little-endian bytes at "buf". Returns the number of
* bytes (2 or 4), or zero if the character cannot be represented.
* If "buf" is NULL, then return the number of bytes required.
*/
int UTF16WriteCharAsBytes(void *buf, unsigned long ch);
// Skip the leading white space and '0x' or '0X' of a integer string
char * TrimHexStr (char *Str, int *IsHex);
// Convert hex string to uint
unsigned int Xtoi (char *Str, unsigned int *Bytes);
// Convert hex string to 64 bit data.
void Xtoi64 (char *Str, unsigned long *Data, unsigned int *Bytes);
// Convert decimal string to uint
unsigned int Dtoi (char *str);
// Convert decimal string to uint
void Dtoi64 (char *str,unsigned long *Data);
// Convert integer string to uint.
unsigned int Strtoi (char *Str, unsigned int *Bytes);
// Convert integer string to 64 bit data.
void Strtoi64 (char *Str, unsigned long *Data, unsigned int *Bytes);
int StrToBuf (unsigned char *Buf, unsigned int BufferLength, char *Str);
/* Converts Unicode string to binary buffer.
* The conversion may be partial.
* The first character in the string that is not hex digit stops the conversion.
* At a minimum, any blob of data could be represented as a hex string.
*/
int HexStringToBuf (unsigned char *Buf, unsigned int *Len, char *Str, unsigned int *ConvertedStrLen);
// Determines if a Unicode character is a hexadecimal digit.
int IsHexDigit (unsigned char *Digit, char Char);
#endif