Zydis  v2.0.0-beta1
String.h
Go to the documentation of this file.
1 /***************************************************************************************************
2 
3  Zyan Disassembler Library (Zydis)
4 
5  Original Author : Florian Bernd, Joel H�ner
6 
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in all
15  * copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24 
25 ***************************************************************************************************/
26 
27 #ifndef ZYDIS_STRING_H
28 #define ZYDIS_STRING_H
29 
30 #include <Zydis/CommonTypes.h>
31 #include <Zydis/Status.h>
32 #include <Zydis/Internal/LibC.h>
33 
34 /* ============================================================================================== */
35 /* Enums and types */
36 /* ============================================================================================== */
37 
38 /* ---------------------------------------------------------------------------------------------- */
39 /* String */
40 /* ---------------------------------------------------------------------------------------------- */
41 
45 typedef struct ZydisString_
46 {
50  char *buffer;
60 
61 /* ---------------------------------------------------------------------------------------------- */
62 /* Static string */
63 /* ---------------------------------------------------------------------------------------------- */
64 
65 #pragma pack(push, 1)
66 
72 typedef struct ZydisStaticString_
73 {
77  const char* buffer;
83 
84 #pragma pack(pop)
85 
86 /* ---------------------------------------------------------------------------------------------- */
87 /* Letter Case */
88 /* ---------------------------------------------------------------------------------------------- */
89 
94 
99 {
112 
117 };
118 
119 /* ---------------------------------------------------------------------------------------------- */
120 
121 /* ============================================================================================== */
122 /* Macros */
123 /* ============================================================================================== */
124 
125 /* ---------------------------------------------------------------------------------------------- */
126 /* Helper Macros */
127 /* ---------------------------------------------------------------------------------------------- */
128 
134 #define ZYDIS_MAKE_STRING(string) \
135  { (char*)string, sizeof(string) - 1, sizeof(string) - 1 }
136 
142 #define ZYDIS_MAKE_STATIC_STRING(string) \
143  { string, sizeof(string) - 1 }
144 
145 /* ---------------------------------------------------------------------------------------------- */
146 
147 /* ============================================================================================== */
148 /* Functions */
149 /* ============================================================================================== */
150 
151 /* ---------------------------------------------------------------------------------------------- */
152 /* Basic Operations */
153 /* ---------------------------------------------------------------------------------------------- */
154 
163 ZYDIS_NO_EXPORT ZYDIS_INLINE ZydisStatus ZydisStringInit(ZydisString* string, char* value)
164 {
165  if (!string || !value)
166  {
168  }
169 
170  const ZydisUSize length = ZydisStrLen(value);
171  string->buffer = value;
172  string->length = length;
173  string->capacity = length;
174 
175  return ZYDIS_STATUS_SUCCESS;
176 }
177 
186 {
187  if (!string)
188  {
190  }
191  if (string->length >= string->capacity)
192  {
194  }
195 
196  string->buffer[string->length] = 0;
197  return ZYDIS_STATUS_SUCCESS;
198 }
199 
200 /* ---------------------------------------------------------------------------------------------- */
201 
214 ZYDIS_NO_EXPORT ZydisStatus ZydisStringAppendEx(ZydisString* string, const ZydisString* text,
215  ZydisLetterCase letterCase);
216 
230  const char* text, ZydisLetterCase letterCase)
231 {
232  ZydisString other;
233  ZYDIS_CHECK(ZydisStringInit(&other, (char*)text));
234 
235  return ZydisStringAppendEx(string, &other, letterCase);
236 }
237 
251  const ZydisStaticString* text, ZydisLetterCase letterCase)
252 {
253  if (!text || !text->buffer)
254  {
256  }
257 
258  ZydisString other;
259  other.buffer = (char*)text->buffer;
260  other.length = text->length;
261 
262  return ZydisStringAppendEx(string, &other, letterCase);
263 }
264 
276  const ZydisString* text)
277 {
278  return ZydisStringAppendEx(string, text, ZYDIS_LETTER_CASE_DEFAULT);
279 }
280 
291 ZYDIS_NO_EXPORT ZYDIS_INLINE ZydisStatus ZydisStringAppendC(ZydisString* string, const char* text)
292 {
293  ZydisString other;
294  ZYDIS_CHECK(ZydisStringInit(&other, (char*)text));
295 
296  return ZydisStringAppendEx(string, &other, ZYDIS_LETTER_CASE_DEFAULT);
297 }
298 
310  const ZydisStaticString* text, ZydisLetterCase letterCase)
311 {
312  if (!text || !text->buffer)
313  {
315  }
316 
317  ZydisString other;
318  other.buffer = (char*)text->buffer;
319  other.length = text->length;
320 
321  return ZydisStringAppendEx(string, &other, letterCase);
322 }
323 
324 /* ---------------------------------------------------------------------------------------------- */
325 /* Formatting */
326 /* ---------------------------------------------------------------------------------------------- */
327 
344 ZYDIS_NO_EXPORT ZydisStatus ZydisPrintDecU(ZydisString* string, ZydisU64 value,
345  ZydisU8 paddingLength);
346 
363 ZYDIS_NO_EXPORT ZydisStatus ZydisPrintDecS(ZydisString* string, ZydisI64 value,
364  ZydisU8 paddingLength);
365 
386 ZYDIS_NO_EXPORT ZydisStatus ZydisPrintHexU(ZydisString* string, ZydisU64 value,
387  ZydisU8 paddingLength, ZydisBool uppercase, const ZydisString* prefix,
388  const ZydisString* suffix);
389 
410 ZYDIS_NO_EXPORT ZydisStatus ZydisPrintHexS(ZydisString* string, ZydisI64 value,
411  ZydisU8 paddingLength, ZydisBool uppercase, const ZydisString* prefix,
412  const ZydisString* suffix);
413 
414 /* ---------------------------------------------------------------------------------------------- */
415 
416 /* ============================================================================================== */
417 
418 #endif // ZYDIS_STRING_H
Includes and defines some default datatypes.
uint64_t ZydisU64
Definition: CommonTypes.h:48
uint8_t ZydisU8
Definition: CommonTypes.h:45
int64_t ZydisI64
Definition: CommonTypes.h:52
ZydisU8 ZydisBool
@briefs Defines the ZydisBool datatype.
Definition: CommonTypes.h:133
size_t ZydisUSize
Definition: CommonTypes.h:53
#define ZYDIS_INLINE
Definition: Defines.h:121
#define ZydisStrLen
Definition: LibC.h:41
Status code definitions and check macros.
@ ZYDIS_STATUS_INSUFFICIENT_BUFFER_SIZE
A buffer passed to a function was too small to complete the requested operation.
Definition: Status.h:74
@ ZYDIS_STATUS_SUCCESS
The operation completed successfully.
Definition: Status.h:62
@ ZYDIS_STATUS_INVALID_PARAMETER
An invalid parameter was passed to a function.
Definition: Status.h:66
ZydisU32 ZydisStatus
Defines the ZydisStatus datatype.
Definition: Status.h:48
#define ZYDIS_CHECK(status)
Checks if a zydis operation was successfull and returns the status-code, if not.
Definition: Status.h:164
ZYDIS_NO_EXPORT ZydisStatus ZydisPrintHexU(ZydisString *string, ZydisU64 value, ZydisU8 paddingLength, ZydisBool uppercase, const ZydisString *prefix, const ZydisString *suffix)
Formats the given unsigned ordinal value to its hexadecimal text-representation and appends it to the...
ZYDIS_NO_EXPORT ZYDIS_INLINE ZydisStatus ZydisStringAppend(ZydisString *string, const ZydisString *text)
Appends a ZydisString to another ZydisString.
Definition: String.h:275
ZYDIS_NO_EXPORT ZYDIS_INLINE ZydisStatus ZydisStringInit(ZydisString *string, char *value)
Initializes a ZydisString struct with a C-string.
Definition: String.h:163
ZYDIS_NO_EXPORT ZydisStatus ZydisPrintDecU(ZydisString *string, ZydisU64 value, ZydisU8 paddingLength)
Formats the given unsigned ordinal value to its decimal text-representation and appends it to s.
ZYDIS_NO_EXPORT ZYDIS_INLINE ZydisStatus ZydisStringAppendExC(ZydisString *string, const char *text, ZydisLetterCase letterCase)
Appends the given C-string to a ZydisString, converting it to the specified letter-case.
Definition: String.h:229
ZydisU8 ZydisLetterCase
Defines the ZydisLetterCase datatype.
Definition: String.h:93
ZYDIS_NO_EXPORT ZYDIS_INLINE ZydisStatus ZydisStringAppendStatic(ZydisString *string, const ZydisStaticString *text, ZydisLetterCase letterCase)
Appends the given 'ZydisStaticString' to a ZydisString.
Definition: String.h:309
ZYDIS_NO_EXPORT ZYDIS_INLINE ZydisStatus ZydisStringFinalize(ZydisString *string)
Finalizes a ZydisString struct by adding a terminating zero byte.
Definition: String.h:185
struct ZydisStaticString_ ZydisStaticString
Defines the ZydisStaticString struct.
ZYDIS_NO_EXPORT ZydisStatus ZydisPrintDecS(ZydisString *string, ZydisI64 value, ZydisU8 paddingLength)
Formats the given signed ordinal value to its decimal text-representation and appends it to s.
ZYDIS_NO_EXPORT ZYDIS_INLINE ZydisStatus ZydisStringAppendExStatic(ZydisString *string, const ZydisStaticString *text, ZydisLetterCase letterCase)
Appends the given 'ZydisStaticString' to a ZydisString, converting it to the specified letter-case.
Definition: String.h:250
ZYDIS_NO_EXPORT ZYDIS_INLINE ZydisStatus ZydisStringAppendC(ZydisString *string, const char *text)
Appends the given C-string to a ZydisString.
Definition: String.h:291
ZydisLetterCases
Values that represent letter cases.
Definition: String.h:99
@ ZYDIS_LETTER_CASE_UPPER
Converts the given text to uppercase letters.
Definition: String.h:111
@ ZYDIS_LETTER_CASE_MAX_VALUE
Maximum value of this enum.
Definition: String.h:116
@ ZYDIS_LETTER_CASE_DEFAULT
Uses the given text "as is".
Definition: String.h:103
@ ZYDIS_LETTER_CASE_LOWER
Converts the given text to lowercase letters.
Definition: String.h:107
ZYDIS_NO_EXPORT ZydisStatus ZydisStringAppendEx(ZydisString *string, const ZydisString *text, ZydisLetterCase letterCase)
Appends a ZydisString to another ZydisString, converting it to the specified letter-case.
struct ZydisString_ ZydisString
Defines the ZydisString struct.
ZYDIS_NO_EXPORT ZydisStatus ZydisPrintHexS(ZydisString *string, ZydisI64 value, ZydisU8 paddingLength, ZydisBool uppercase, const ZydisString *prefix, const ZydisString *suffix)
Formats the given signed ordinal value to its hexadecimal text-representation and appends it to the b...
Defines the ZydisStaticString struct.
Definition: String.h:73
ZydisU8 length
The length of the string (without 0-termination).
Definition: String.h:81
const char * buffer
The buffer that contains the actual string (0-termination is optional!).
Definition: String.h:77
Defines the ZydisString struct.
Definition: String.h:46
ZydisUSize length
The length of the string (without 0-termination).
Definition: String.h:54
ZydisUSize capacity
The total buffer capacity.
Definition: String.h:58
char * buffer
The buffer that contains the actual string (0-termination is optional!).
Definition: String.h:50