Zydis  v2.0.1
LibC.h
Go to the documentation of this file.
1 /***************************************************************************************************
2 
3  Zyan Disassembler Library (Zydis)
4 
5  Original Author : Joel Hoener
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_INTERNAL_LIBC_H
28 #define ZYDIS_INTERNAL_LIBC_H
29 
30 #include <Zydis/Defines.h>
31 
32 #ifndef ZYDIS_NO_LIBC
33 
34 /* ============================================================================================== */
35 /* LibC is available */
36 /* ============================================================================================== */
37 
38 # include <string.h>
39 # define ZydisMemoryCopy memcpy
40 # define ZydisMemorySet memset
41 # define ZydisStrLen strlen
42 
43 #else
44 
45 /* ============================================================================================== */
46 /* No LibC available, use our own functions */
47 /* ============================================================================================== */
48 
49 /*
50  * These implementations are by no means optimized and will be outperformed by pretty much any
51  * libc implementation out there. We do not aim towards providing competetive implementations here,
52  * but towards providing a last resort fallback for environments without a working libc.
53  */
54 
55 ZYDIS_INLINE void* ZydisMemorySet(void* ptr, int value, ZydisUSize num)
56 {
57  ZydisU8 c = value & 0xff;
58  for (ZydisUSize i = 0; i < num; ++i) ((ZydisU8*)ptr)[i] = c;
59  return ptr;
60 }
61 
62 ZYDIS_INLINE void* ZydisMemoryCopy(void* dst, const void* src, ZydisUSize num)
63 {
64  for (ZydisUSize i = 0; i < num; ++i)
65  {
66  ((ZydisU8*)dst)[i] = ((const ZydisU8*)src)[i];
67  }
68  return dst;
69 }
70 
71 ZYDIS_INLINE ZydisUSize ZydisStrLen(const char* str)
72 {
73  const char *s;
74  for (s = str; *s; ++s);
75  return s - str;
76 }
77 
78 /* ============================================================================================== */
79 
80 #endif
81 
82 #endif /* ZYDIS_INTERNAL_LIBC_H */
uint8_t ZydisU8
Definition: CommonTypes.h:45
size_t ZydisUSize
Definition: CommonTypes.h:53
General helper and platform detection macros.
#define ZYDIS_INLINE
Definition: Defines.h:121
#define ZydisMemoryCopy
Definition: LibC.h:39
#define ZydisMemorySet
Definition: LibC.h:40
#define ZydisStrLen
Definition: LibC.h:41