[2826] | 1 | /* $Id: kLdrHlp.h 3525 2007-08-19 22:14:55Z 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 |
|
---|
[2825] | 35 | /*
|
---|
[2944] | 36 | * Compiler specific helpers / CRT.
|
---|
[2825] | 37 | * (I.e. operations that tend to have compiler intrinsic implementations).
|
---|
| 38 | */
|
---|
[2944] | 39 | #ifndef KLDR_USE_CRT
|
---|
| 40 |
|
---|
| 41 | # ifdef __GNUC__
|
---|
[2832] | 42 | /** memchr */
|
---|
[2944] | 43 | # define kLdrHlpMemChr(a,b,c) __builtin_memchr(a,b,c)
|
---|
[2825] | 44 | /** memcmp */
|
---|
[2944] | 45 | # define kLdrHlpMemComp(a,b,c) __builtin_memcmp(a,b,c)
|
---|
[2825] | 46 | /** memcpy */
|
---|
[2944] | 47 | # define kLdrHlpMemCopy(a,b,c) __builtin_memcpy(a,b,c)
|
---|
[2880] | 48 | /** memmove */
|
---|
[2883] | 49 | /*# define kLdrHlpMemMove(a,b,c) __builtin_memmove(a,b,c)*/
|
---|
[2944] | 50 | # define kLdrHlpMemMove_needed
|
---|
[2825] | 51 | /** memset */
|
---|
[2944] | 52 | # define kLdrHlpMemSet(a,b,c) __builtin_memset(a,b,c)
|
---|
[2832] | 53 | /** strchr */
|
---|
[2944] | 54 | # define kLdrHlpStrChr(a, b) __builtin_strchr(a, b)
|
---|
[2854] | 55 | /** strcmp */
|
---|
[2944] | 56 | # define kLdrHlpStrComp(a, b) __builtin_strcmp(a, b)
|
---|
[2891] | 57 | /** strncmp */
|
---|
[2944] | 58 | # define kLdrHlpStrNComp(a,b,c) __builtin_strncmp(a, b, c)
|
---|
[2825] | 59 | /** strlen */
|
---|
[2944] | 60 | # define kLdrHlpStrLen(a) __builtin_strlen(a)
|
---|
[2825] | 61 | /** alloca */
|
---|
[2944] | 62 | # define kLdrHlpAllocA(a) __builtin_alloca(a)
|
---|
[2825] | 63 | /** int3 */
|
---|
[2944] | 64 | # define kldrHlpBreakpoint() do { __asm__ __volatile__ ("int3\n\tnop"); } while (0)
|
---|
[2825] | 65 | /** NULL */
|
---|
[2944] | 66 | # ifndef NULL
|
---|
| 67 | # define NULL 0
|
---|
| 68 | # endif
|
---|
[2825] | 69 | # endif
|
---|
| 70 |
|
---|
[2944] | 71 | # ifdef _MSC_VER
|
---|
| 72 | # include <string.h>
|
---|
| 73 | # include <malloc.h>
|
---|
| 74 | # pragma intrinsic(memcmp, memcpy, memset, strcmp, strlen, __debugbreak)
|
---|
[2832] | 75 | /** memchr */
|
---|
[2944] | 76 | # define kLdrHlpMemChr_needed
|
---|
[2825] | 77 | /** memcmp */
|
---|
[2944] | 78 | # define kLdrHlpMemComp(a,b,c) memcmp(a,b,c)
|
---|
[2825] | 79 | /** memcpy */
|
---|
[2944] | 80 | # define kLdrHlpMemCopy(a,b,c) memcpy(a,b,c)
|
---|
[2880] | 81 | /** memmove */
|
---|
[2944] | 82 | # define kLdrHlpMemMove_needed
|
---|
[2825] | 83 | /** memset */
|
---|
[2944] | 84 | # define kLdrHlpMemSet(a,b,c) memset(a,b,c)
|
---|
[2854] | 85 | /** strcmp */
|
---|
[2944] | 86 | # define kLdrHlpStrComp(a, b) strcmp(a, b)
|
---|
[2891] | 87 | /** strncmp */
|
---|
[2944] | 88 | # define kLdrHlpStrNComp_needed
|
---|
[2825] | 89 | /** strlen */
|
---|
[2944] | 90 | # define kLdrHlpStrLen(a) strlen(a)
|
---|
[2832] | 91 | /** strchr */
|
---|
[2944] | 92 | # define kLdrHlpStrChr_needed
|
---|
[2825] | 93 | /** alloca */
|
---|
[2944] | 94 | # define kLdrHlpAllocA(a) alloca(a)
|
---|
[2825] | 95 | /** int3 */
|
---|
[2944] | 96 | # define kldrHlpBreakpoint() __debugbreak()
|
---|
[2825] | 97 | /** NULL */
|
---|
[2944] | 98 | # ifndef NULL
|
---|
| 99 | # define NULL 0
|
---|
| 100 | # endif
|
---|
[2825] | 101 | # endif
|
---|
| 102 |
|
---|
[2944] | 103 | # ifdef kLdrHlpStrChr_needed
|
---|
[2832] | 104 | char *kLdrHlpStrChr(const char *psz, int ch);
|
---|
[2944] | 105 | # endif
|
---|
| 106 | # ifdef kLdrHlpStrChr_needed
|
---|
[2891] | 107 | int kLdrHlpStrNComp(const char *psz1, const char *psz2, size_t cch);
|
---|
[2944] | 108 | # endif
|
---|
| 109 | # ifdef kLdrHlpMemChr_needed
|
---|
[2832] | 110 | void *kLdrHlpMemChr(const void *pv, int ch, size_t cb);
|
---|
[2944] | 111 | # endif
|
---|
| 112 | # ifdef kLdrHlpMemMove_needed
|
---|
[2880] | 113 | void *kLdrHlpMemMove(void *pv1, const void *pv2, size_t cb);
|
---|
[2944] | 114 | # endif
|
---|
[2832] | 115 |
|
---|
[2854] | 116 |
|
---|
[2944] | 117 | #else /* KLDR_USE_CRT */
|
---|
| 118 |
|
---|
| 119 | # include <string.h>
|
---|
| 120 | # include <stdlib.h>
|
---|
| 121 | # ifdef _MSC_VER
|
---|
| 122 | # include <malloc.h>
|
---|
| 123 | # endif
|
---|
| 124 |
|
---|
| 125 | # define kLdrHlpMemChr(a,b,c) memchr(a,b,c)
|
---|
| 126 | /** memcmp */
|
---|
| 127 | # define kLdrHlpMemComp(a,b,c) memcmp(a,b,c)
|
---|
| 128 | /** memcpy */
|
---|
| 129 | # define kLdrHlpMemCopy(a,b,c) memcpy(a,b,c)
|
---|
| 130 | /** memmove */
|
---|
| 131 | # define kLdrHlpMemMove(a,b,c) memmove(a,b,c)
|
---|
| 132 | /** memset */
|
---|
| 133 | # define kLdrHlpMemSet(a,b,c) memset(a,b,c)
|
---|
| 134 | /** strchr */
|
---|
| 135 | # define kLdrHlpStrChr(a, b) strchr(a, b)
|
---|
| 136 | /** strcmp */
|
---|
| 137 | # define kLdrHlpStrComp(a, b) strcmp(a, b)
|
---|
| 138 | /** strncmp */
|
---|
| 139 | # define kLdrHlpStrNComp(a,b,c) strncmp(a, b, c)
|
---|
| 140 | /** strlen */
|
---|
| 141 | # define kLdrHlpStrLen(a) strlen(a)
|
---|
| 142 | /** alloca */
|
---|
| 143 | # define kLdrHlpAllocA(a) alloca(a)
|
---|
| 144 | /** int3 */
|
---|
| 145 | # ifdef __GNUC__
|
---|
| 146 | # define kldrHlpBreakpoint() do { __asm__ __volatile__ ("int3\n\tnop"); } while (0)
|
---|
[2954] | 147 | # endif
|
---|
[2944] | 148 | # ifdef _MSC_VER
|
---|
| 149 | # define kldrHlpBreakpoint() __debugbreak()
|
---|
[2954] | 150 | # endif
|
---|
[2944] | 151 |
|
---|
[2954] | 152 | #endif
|
---|
[2944] | 153 |
|
---|
[2832] | 154 | #if (!defined(kLdrHlpMemChr) && !defined(kLdrHlpStrChr_needed))\
|
---|
| 155 | || !defined(kLdrHlpMemComp) \
|
---|
[2828] | 156 | || !defined(kLdrHlpMemCopy) \
|
---|
| 157 | || !defined(kLdrHlpMemSet) \
|
---|
[2832] | 158 | || (!defined(kLdrHlpStrChr) && !defined(kLdrHlpStrChr_needed)) \
|
---|
[2891] | 159 | || !defined(kLdrHlpStrComp) \
|
---|
| 160 | || (!defined(kLdrHlpStrNComp) && !defined(kLdrHlpStrNComp_needed)) \
|
---|
[2828] | 161 | || !defined(kLdrHlpStrLen) \
|
---|
| 162 | || !defined(kLdrHlpAllocA) \
|
---|
[2825] | 163 | || !defined(kldrHlpBreakpoint)
|
---|
| 164 | # error "Needs porting to your compiler."
|
---|
| 165 | #endif
|
---|
| 166 |
|
---|
[2944] | 167 | #ifdef __cplusplus
|
---|
| 168 | extern "C" {
|
---|
[2954] | 169 | #endif
|
---|
[2944] | 170 |
|
---|
[2955] | 171 | size_t kLdrHlpStrNLen(const char *psz, size_t cchMax);
|
---|
[2944] | 172 | int kLdrHlpMemIComp(const void *pv1, const void *pv2, size_t cb);
|
---|
| 173 | int kLdrHlpStrIComp(const char *pv1, const char *pv2);
|
---|
| 174 |
|
---|
[2830] | 175 | int kldrHlpSemInit(void);
|
---|
| 176 | void kldrHlpSemTerm(void);
|
---|
| 177 | int kldrHlpSemRequest(void);
|
---|
| 178 | void kldrHlpSemRelease(void);
|
---|
[2821] | 179 |
|
---|
[2830] | 180 | int kldrHlpPageAlloc(void **ppv, size_t cb, KLDRPROT enmProt, unsigned fFixed);
|
---|
| 181 | int kldrHlpPageProtect(void *pv, size_t cb, KLDRPROT enmProt);
|
---|
| 182 | int kldrHlpPageFree(void *pv, size_t cb);
|
---|
[2821] | 183 |
|
---|
[2825] | 184 | int kldrHlpHeapInit(void);
|
---|
| 185 | void kldrHlpHeapTerm(void);
|
---|
| 186 | void kldrHlpHeapDonate(void *pv, size_t cb);
|
---|
| 187 | void * kldrHlpAlloc(size_t cb);
|
---|
[2847] | 188 | void * kldrHlpAllocZ(size_t cb);
|
---|
[2825] | 189 | void kldrHlpFree(void *pv);
|
---|
[2821] | 190 |
|
---|
[2867] | 191 | int kldrHlpGetEnv(const char *pszVar, char *pszVal, size_t cchVal);
|
---|
[2846] | 192 | int kldrHlpGetEnvUZ(const char *pszVar, size_t *pcb);
|
---|
[2854] | 193 | char *kldrHlpGetFilename(const char *pszFilename);
|
---|
[2867] | 194 | char *kldrHlpGetSuff(const char *pszFilename);
|
---|
[2854] | 195 | char *kldrHlpGetExt(const char *pszFilename);
|
---|
[2867] | 196 | int kldrHlpIsFilenameOnly(const char *pszFilename);
|
---|
[2825] | 197 | void kldrHlpExit(int rc);
|
---|
[2830] | 198 | void kldrHlpSleep(unsigned cMillies);
|
---|
[2836] | 199 | char *kldrHlpInt2Ascii(char *psz, size_t cch, long lVal, unsigned iBase);
|
---|
[2825] | 200 | void kldrHlpAssertMsg(const char *pszExpr, const char *pszFile, unsigned iLine, const char *pszFunction);
|
---|
[2821] | 201 |
|
---|
[2944] | 202 | #ifdef __cplusplus
|
---|
| 203 | }
|
---|
[2954] | 204 | #endif
|
---|
[2944] | 205 |
|
---|
| 206 |
|
---|
[2825] | 207 | /** Assertion macro.
|
---|
| 208 | * Users should wrap it since this is ALWAYS compiled in. */
|
---|
| 209 | #define kldrHlpAssert(expr) \
|
---|
| 210 | do { \
|
---|
| 211 | if (!(expr)) \
|
---|
| 212 | { \
|
---|
| 213 | kldrHlpAssertMsg(#expr, __FILE__, __LINE__, __FUNCTION__); \
|
---|
| 214 | kldrHlpBreakpoint(); \
|
---|
| 215 | } \
|
---|
| 216 | } while (0)
|
---|
[2821] | 217 |
|
---|
| 218 |
|
---|
[2833] | 219 | /** @name Parameter validation macros
|
---|
| 220 | * @{ */
|
---|
| 221 |
|
---|
| 222 | /** Crash validation of a string argument. */
|
---|
| 223 | #define KLDRHLP_VALIDATE_STRING(str) \
|
---|
[2883] | 224 | do { kLdrHlpStrLen(str); } while (0)
|
---|
[2833] | 225 |
|
---|
| 226 | /** Crash validation of an optional string argument. */
|
---|
| 227 | #define KLDRHLP_VALIDATE_OPTIONAL_STRING(str) \
|
---|
| 228 | do { if (str) { KLDRHLP_VALIDATE_STRING(str); } } while (0)
|
---|
| 229 |
|
---|
| 230 | /** Return/Crash validation of an output buffer. */
|
---|
| 231 | #define KLDRHLP_VALIDATE_BUFFER(buf, cb) \
|
---|
| 232 | do { \
|
---|
| 233 | if ((cb)) \
|
---|
| 234 | { \
|
---|
| 235 | uint8_t __b; \
|
---|
| 236 | uint8_t volatile * __pb = (uint8_t volatile *)(buf); \
|
---|
| 237 | size_t __cbPage1 = 0x1000 - ((uintptr_t)(__pb) & 0xfff); /* ASSUMES page size! */ \
|
---|
| 238 | __b = *__pb; *__pb = 0xff; *__pb = __b; \
|
---|
| 239 | if ((cb) > __cbPage1) \
|
---|
| 240 | { \
|
---|
| 241 | size_t __cb = (cb) - __cbPage1; \
|
---|
| 242 | __pb -= __cbPage1; \
|
---|
| 243 | for (;;) \
|
---|
| 244 | { \
|
---|
| 245 | __b = *__pb; *__pb = 0xff; *__pb = __b; \
|
---|
| 246 | if (__cb < 0x1000) \
|
---|
| 247 | break; \
|
---|
| 248 | __pb += 0x1000; \
|
---|
| 249 | __cb -= 0x1000; \
|
---|
| 250 | } \
|
---|
| 251 | } \
|
---|
| 252 | } \
|
---|
| 253 | else \
|
---|
| 254 | return KLDR_ERR_INVALID_PARAMETER; \
|
---|
| 255 | } while (0)
|
---|
| 256 |
|
---|
| 257 | /** Crash validation of an optional output buffer. */
|
---|
| 258 | #define KLDRHLP_VALIDATE_OPTIONAL_BUFFER(buf, cb) \
|
---|
| 259 | do { \
|
---|
| 260 | if ((buf) != NULL && (cb) != 0) \
|
---|
| 261 | { \
|
---|
| 262 | KLDRHLP_VALIDATE_BUFFER(buf, cb); \
|
---|
| 263 | } \
|
---|
| 264 | } while (0)
|
---|
| 265 |
|
---|
| 266 | /** Return validation of an enum argument. */
|
---|
| 267 | #define KLDRHLP_VALIDATE_ENUM(arg, enumname) \
|
---|
| 268 | do { \
|
---|
| 269 | if ((arg) <= enumname##_INVALID || (arg) >= enumname##_END) \
|
---|
| 270 | { \
|
---|
| 271 | return KLDR_ERR_INVALID_PARAMETER; \
|
---|
| 272 | } \
|
---|
| 273 | } while (0)
|
---|
| 274 |
|
---|
[2851] | 275 | /** Return validation of a flags argument. */
|
---|
| 276 | #define KLDRHLP_VALIDATE_FLAGS(arg, AllowedMask) \
|
---|
| 277 | do { \
|
---|
| 278 | if ((arg) & ~(AllowedMask)) \
|
---|
| 279 | { \
|
---|
| 280 | return KLDR_ERR_INVALID_PARAMETER; \
|
---|
| 281 | } \
|
---|
| 282 | } while (0)
|
---|
| 283 |
|
---|
[2825] | 284 | /** @} */
|
---|
[2821] | 285 |
|
---|
| 286 |
|
---|
[2833] | 287 | /** @} */
|
---|
| 288 |
|
---|
[2825] | 289 | #endif /* __kLdrHlp_h__ */
|
---|
[2821] | 290 |
|
---|