[2826] | 1 | /* $Id: kLdrHlp.h 2846 2006-11-01 18:26:35Z bird $ */
|
---|
[2821] | 2 | /** @file
|
---|
| 3 | *
|
---|
| 4 | * kLdr - The Dynamic Loader, Helper Functions.
|
---|
| 5 | *
|
---|
| 6 | * Copyright (c) 2006 knut st. osmundsen <bird@anduin.net>
|
---|
| 7 | *
|
---|
| 8 | *
|
---|
| 9 | * This file is part of kLdr.
|
---|
| 10 | *
|
---|
| 11 | * kLdr is free software; you can redistribute it and/or modify
|
---|
| 12 | * it under the terms of the GNU General Public License as published by
|
---|
| 13 | * the Free Software Foundation; either version 2 of the License, or
|
---|
| 14 | * (at your option) any later version.
|
---|
| 15 | *
|
---|
| 16 | * kLdr is distributed in the hope that it will be useful,
|
---|
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 19 | * GNU General Public License for more details.
|
---|
| 20 | *
|
---|
| 21 | * You should have received a copy of the GNU General Public License
|
---|
| 22 | * along with kLdr; if not, write to the Free Software
|
---|
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
| 24 | *
|
---|
| 25 | */
|
---|
| 26 |
|
---|
| 27 |
|
---|
| 28 | #ifndef __kLdrHlp_h__
|
---|
| 29 | #define __kLdrHlp_h__
|
---|
| 30 |
|
---|
[2825] | 31 | /** @defgroup grp_kLdrHlp kLdrHlp - Helper Functions
|
---|
| 32 | * @internal
|
---|
| 33 | * @{ */
|
---|
[2827] | 34 |
|
---|
[2824] | 35 | /** Get the minimum of two values. */
|
---|
| 36 | #define KLDR_MIN(a, b) ((a) <= (b) ? (a) : (b))
|
---|
[2846] | 37 | /** Get the maximum of two values. */
|
---|
| 38 | #define KLDR_MAX(a, b) ((a) >= (b) ? (a) : (b))
|
---|
[2827] | 39 | /** Calculate the offset of a structure member. */
|
---|
| 40 | #define KLDR_OFFSETOF(strct, memb) ( (size_t)( ((strct *)0)->memb ) )
|
---|
[2825] | 41 | /** Align a size_t value. */
|
---|
| 42 | #define KLDR_ALIGN_Z(val, align) ( ((val) + ((align) - 1)) & ~(size_t)((align) - 1) )
|
---|
| 43 | /** Align a void * value. */
|
---|
| 44 | #define KLDR_ALIGN_P(pv, align) ( (void *)( ((uintptr_t)(pv) + ((align) - 1)) & ~(uintptr_t)((align) - 1) ) )
|
---|
[2827] | 45 | /** @def KLDRHLP_LE2H_U16
|
---|
| 46 | * Unsigned 16-bit little-endian to host translation. */
|
---|
| 47 | /** @def KLDRHLP_LE2H_U32
|
---|
| 48 | * Unsigned 32-bit little-endian to host translation. */
|
---|
| 49 | #if 1
|
---|
| 50 | # define KLDRHLP_LE2H_U16(u16) ((uint16_t)(u16))
|
---|
| 51 | # define KLDRHLP_LE2H_U32(u32) ((uint32_t)(u32))
|
---|
| 52 | #else
|
---|
| 53 | # define KLDRHLP_LE2H_U16(u16) ( (uint16_t) (((u16) >> 8) | ((u16) << 8)) )
|
---|
| 54 | # define KLDRHLP_LE2H_U32(u32) ( ( ((u32) & UINT32_C(0xff000000)) >> 24 ) \
|
---|
| 55 | | ( ((u32) & UINT32_C(0x00ff0000)) >> 8 ) \
|
---|
| 56 | | ( ((u32) & UINT32_C(0x0000ff00)) << 8 ) \
|
---|
| 57 | | ( ((u32) & UINT32_C(0x000000ff)) << 24 ) \
|
---|
| 58 | )
|
---|
| 59 | #endif
|
---|
[2821] | 60 |
|
---|
[2825] | 61 | /*
|
---|
| 62 | * Compiler specific helpers.
|
---|
| 63 | * (I.e. operations that tend to have compiler intrinsic implementations).
|
---|
| 64 | */
|
---|
| 65 | #ifdef __GNUC__
|
---|
[2832] | 66 | /** memchr */
|
---|
| 67 | # define kLdrHlpMemChr(a,b,c) __builtin_memchr(a,b,c)
|
---|
[2825] | 68 | /** memcmp */
|
---|
[2828] | 69 | # define kLdrHlpMemComp(a,b,c) __builtin_memcmp(a,b,c)
|
---|
[2825] | 70 | /** memcpy */
|
---|
[2828] | 71 | # define kLdrHlpMemCopy(a,b,c) __builtin_memcpy(a,b,c)
|
---|
[2825] | 72 | /** memset */
|
---|
[2828] | 73 | # define kLdrHlpMemSet(a,b,c) __builtin_memset(a,b,c)
|
---|
[2832] | 74 | /** strchr */
|
---|
| 75 | # define kLdrHlpStrChr(a, b) __builtin_strchr(a, b)
|
---|
[2825] | 76 | /** strlen */
|
---|
[2828] | 77 | # define kLdrHlpStrLen(a) __builtin_strlen(a)
|
---|
[2825] | 78 | /** alloca */
|
---|
[2828] | 79 | # define kLdrHlpAllocA(a) __builtin_alloca(a)
|
---|
[2825] | 80 | /** int3 */
|
---|
| 81 | # define kldrHlpBreakpoint() do { __asm__ __volatile__ ("int3\n\tnop"); } while (0)
|
---|
| 82 | /** NULL */
|
---|
| 83 | # ifndef NULL
|
---|
| 84 | # define NULL 0
|
---|
| 85 | # endif
|
---|
| 86 | #endif
|
---|
| 87 |
|
---|
| 88 | #ifdef _MSC_VER
|
---|
| 89 | # include <string.h>
|
---|
| 90 | # include <malloc.h>
|
---|
| 91 | # pragma intrinsic(memcmp, memcpy, memset, strlen, __debugbreak)
|
---|
[2832] | 92 | /** memchr */
|
---|
| 93 | # define kLdrHlpMemChr_needed
|
---|
[2825] | 94 | /** memcmp */
|
---|
[2828] | 95 | # define kLdrHlpMemComp(a,b,c) memcmp(a,b,c)
|
---|
[2825] | 96 | /** memcpy */
|
---|
[2828] | 97 | # define kLdrHlpMemCopy(a,b,c) memcpy(a,b,c)
|
---|
[2825] | 98 | /** memset */
|
---|
[2828] | 99 | # define kLdrHlpMemSet(a,b,c) memset(a,b,c)
|
---|
[2825] | 100 | /** strlen */
|
---|
[2828] | 101 | # define kLdrHlpStrLen(a) strlen(a)
|
---|
[2832] | 102 | /** strchr */
|
---|
| 103 | # define kLdrHlpStrChr_needed
|
---|
[2825] | 104 | /** alloca */
|
---|
[2828] | 105 | # define kLdrHlpAllocA(a) alloca(a)
|
---|
[2825] | 106 | /** int3 */
|
---|
| 107 | # define kldrHlpBreakpoint() __debugbreak()
|
---|
| 108 | /** NULL */
|
---|
| 109 | # ifndef NULL
|
---|
| 110 | # define NULL 0
|
---|
| 111 | # endif
|
---|
| 112 | #endif
|
---|
| 113 |
|
---|
[2832] | 114 | #ifdef kLdrHlpStrChr_needed
|
---|
| 115 | char *kLdrHlpStrChr(const char *psz, int ch);
|
---|
| 116 | #endif
|
---|
| 117 | #ifdef kLdrHlpStrChr_needed
|
---|
| 118 | void *kLdrHlpMemChr(const void *pv, int ch, size_t cb);
|
---|
| 119 | #endif
|
---|
| 120 |
|
---|
| 121 | #if (!defined(kLdrHlpMemChr) && !defined(kLdrHlpStrChr_needed))\
|
---|
| 122 | || !defined(kLdrHlpMemComp) \
|
---|
[2828] | 123 | || !defined(kLdrHlpMemCopy) \
|
---|
| 124 | || !defined(kLdrHlpMemSet) \
|
---|
[2832] | 125 | || (!defined(kLdrHlpStrChr) && !defined(kLdrHlpStrChr_needed)) \
|
---|
[2828] | 126 | || !defined(kLdrHlpStrLen) \
|
---|
| 127 | || !defined(kLdrHlpAllocA) \
|
---|
[2825] | 128 | || !defined(kldrHlpBreakpoint)
|
---|
| 129 | # error "Needs porting to your compiler."
|
---|
| 130 | #endif
|
---|
| 131 |
|
---|
[2830] | 132 | int kldrHlpSemInit(void);
|
---|
| 133 | void kldrHlpSemTerm(void);
|
---|
| 134 | int kldrHlpSemRequest(void);
|
---|
| 135 | void kldrHlpSemRelease(void);
|
---|
[2821] | 136 |
|
---|
[2830] | 137 | int kldrHlpPageAlloc(void **ppv, size_t cb, KLDRPROT enmProt, unsigned fFixed);
|
---|
| 138 | int kldrHlpPageProtect(void *pv, size_t cb, KLDRPROT enmProt);
|
---|
| 139 | int kldrHlpPageFree(void *pv, size_t cb);
|
---|
[2821] | 140 |
|
---|
[2825] | 141 | int kldrHlpHeapInit(void);
|
---|
| 142 | void kldrHlpHeapTerm(void);
|
---|
| 143 | void kldrHlpHeapDonate(void *pv, size_t cb);
|
---|
| 144 | void * kldrHlpAlloc(size_t cb);
|
---|
| 145 | void kldrHlpFree(void *pv);
|
---|
[2821] | 146 |
|
---|
[2825] | 147 | int kldrHlpGetEnv(const char *pszVar, char *pszVal, size_t *pcchVal);
|
---|
[2846] | 148 | int kldrHlpGetEnvUZ(const char *pszVar, size_t *pcb);
|
---|
[2825] | 149 | void kldrHlpExit(int rc);
|
---|
[2830] | 150 | void kldrHlpSleep(unsigned cMillies);
|
---|
[2836] | 151 | char *kldrHlpInt2Ascii(char *psz, size_t cch, long lVal, unsigned iBase);
|
---|
[2825] | 152 | void kldrHlpAssertMsg(const char *pszExpr, const char *pszFile, unsigned iLine, const char *pszFunction);
|
---|
[2821] | 153 |
|
---|
[2825] | 154 | /** Assertion macro.
|
---|
| 155 | * Users should wrap it since this is ALWAYS compiled in. */
|
---|
| 156 | #define kldrHlpAssert(expr) \
|
---|
| 157 | do { \
|
---|
| 158 | if (!(expr)) \
|
---|
| 159 | { \
|
---|
| 160 | kldrHlpAssertMsg(#expr, __FILE__, __LINE__, __FUNCTION__); \
|
---|
| 161 | kldrHlpBreakpoint(); \
|
---|
| 162 | } \
|
---|
| 163 | } while (0)
|
---|
[2821] | 164 |
|
---|
| 165 |
|
---|
[2833] | 166 | /** @name Parameter validation macros
|
---|
| 167 | * @{ */
|
---|
| 168 |
|
---|
| 169 | /** Crash validation of a string argument. */
|
---|
| 170 | #define KLDRHLP_VALIDATE_STRING(str) \
|
---|
| 171 | do { strlen(str); } while (0)
|
---|
| 172 |
|
---|
| 173 | /** Crash validation of an optional string argument. */
|
---|
| 174 | #define KLDRHLP_VALIDATE_OPTIONAL_STRING(str) \
|
---|
| 175 | do { if (str) { KLDRHLP_VALIDATE_STRING(str); } } while (0)
|
---|
| 176 |
|
---|
| 177 | /** Return/Crash validation of an output buffer. */
|
---|
| 178 | #define KLDRHLP_VALIDATE_BUFFER(buf, cb) \
|
---|
| 179 | do { \
|
---|
| 180 | if ((cb)) \
|
---|
| 181 | { \
|
---|
| 182 | uint8_t __b; \
|
---|
| 183 | uint8_t volatile * __pb = (uint8_t volatile *)(buf); \
|
---|
| 184 | size_t __cbPage1 = 0x1000 - ((uintptr_t)(__pb) & 0xfff); /* ASSUMES page size! */ \
|
---|
| 185 | __b = *__pb; *__pb = 0xff; *__pb = __b; \
|
---|
| 186 | if ((cb) > __cbPage1) \
|
---|
| 187 | { \
|
---|
| 188 | size_t __cb = (cb) - __cbPage1; \
|
---|
| 189 | __pb -= __cbPage1; \
|
---|
| 190 | for (;;) \
|
---|
| 191 | { \
|
---|
| 192 | __b = *__pb; *__pb = 0xff; *__pb = __b; \
|
---|
| 193 | if (__cb < 0x1000) \
|
---|
| 194 | break; \
|
---|
| 195 | __pb += 0x1000; \
|
---|
| 196 | __cb -= 0x1000; \
|
---|
| 197 | } \
|
---|
| 198 | } \
|
---|
| 199 | } \
|
---|
| 200 | else \
|
---|
| 201 | return KLDR_ERR_INVALID_PARAMETER; \
|
---|
| 202 | } while (0)
|
---|
| 203 |
|
---|
| 204 | /** Crash validation of an optional output buffer. */
|
---|
| 205 | #define KLDRHLP_VALIDATE_OPTIONAL_BUFFER(buf, cb) \
|
---|
| 206 | do { \
|
---|
| 207 | if ((buf) != NULL && (cb) != 0) \
|
---|
| 208 | { \
|
---|
| 209 | KLDRHLP_VALIDATE_BUFFER(buf, cb); \
|
---|
| 210 | } \
|
---|
| 211 | } while (0)
|
---|
| 212 |
|
---|
| 213 | /** Return validation of an enum argument. */
|
---|
| 214 | #define KLDRHLP_VALIDATE_ENUM(arg, enumname) \
|
---|
| 215 | do { \
|
---|
| 216 | if ((arg) <= enumname##_INVALID || (arg) >= enumname##_END) \
|
---|
| 217 | { \
|
---|
| 218 | return KLDR_ERR_INVALID_PARAMETER; \
|
---|
| 219 | } \
|
---|
| 220 | } while (0)
|
---|
| 221 |
|
---|
[2825] | 222 | /** @} */
|
---|
[2821] | 223 |
|
---|
| 224 |
|
---|
[2833] | 225 | /** @} */
|
---|
| 226 |
|
---|
[2825] | 227 | #endif /* __kLdrHlp_h__ */
|
---|
[2821] | 228 |
|
---|