/* $Id: kLdrHlp.h 3525 2007-08-19 22:14:55Z bird $ */ /** @file * * kLdr - The Dynamic Loader, Helper Functions. * * Copyright (c) 2006 knut st. osmundsen * * * This file is part of kLdr. * * kLdr is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * kLdr is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with kLdr; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ #ifndef __kLdrHlp_h__ #define __kLdrHlp_h__ /** @defgroup grp_kLdrHlp kLdrHlp - Helper Functions * @internal * @{ */ /* * Compiler specific helpers / CRT. * (I.e. operations that tend to have compiler intrinsic implementations). */ #ifndef KLDR_USE_CRT # ifdef __GNUC__ /** memchr */ # define kLdrHlpMemChr(a,b,c) __builtin_memchr(a,b,c) /** memcmp */ # define kLdrHlpMemComp(a,b,c) __builtin_memcmp(a,b,c) /** memcpy */ # define kLdrHlpMemCopy(a,b,c) __builtin_memcpy(a,b,c) /** memmove */ /*# define kLdrHlpMemMove(a,b,c) __builtin_memmove(a,b,c)*/ # define kLdrHlpMemMove_needed /** memset */ # define kLdrHlpMemSet(a,b,c) __builtin_memset(a,b,c) /** strchr */ # define kLdrHlpStrChr(a, b) __builtin_strchr(a, b) /** strcmp */ # define kLdrHlpStrComp(a, b) __builtin_strcmp(a, b) /** strncmp */ # define kLdrHlpStrNComp(a,b,c) __builtin_strncmp(a, b, c) /** strlen */ # define kLdrHlpStrLen(a) __builtin_strlen(a) /** alloca */ # define kLdrHlpAllocA(a) __builtin_alloca(a) /** int3 */ # define kldrHlpBreakpoint() do { __asm__ __volatile__ ("int3\n\tnop"); } while (0) /** NULL */ # ifndef NULL # define NULL 0 # endif # endif # ifdef _MSC_VER # include # include # pragma intrinsic(memcmp, memcpy, memset, strcmp, strlen, __debugbreak) /** memchr */ # define kLdrHlpMemChr_needed /** memcmp */ # define kLdrHlpMemComp(a,b,c) memcmp(a,b,c) /** memcpy */ # define kLdrHlpMemCopy(a,b,c) memcpy(a,b,c) /** memmove */ # define kLdrHlpMemMove_needed /** memset */ # define kLdrHlpMemSet(a,b,c) memset(a,b,c) /** strcmp */ # define kLdrHlpStrComp(a, b) strcmp(a, b) /** strncmp */ # define kLdrHlpStrNComp_needed /** strlen */ # define kLdrHlpStrLen(a) strlen(a) /** strchr */ # define kLdrHlpStrChr_needed /** alloca */ # define kLdrHlpAllocA(a) alloca(a) /** int3 */ # define kldrHlpBreakpoint() __debugbreak() /** NULL */ # ifndef NULL # define NULL 0 # endif # endif # ifdef kLdrHlpStrChr_needed char *kLdrHlpStrChr(const char *psz, int ch); # endif # ifdef kLdrHlpStrChr_needed int kLdrHlpStrNComp(const char *psz1, const char *psz2, size_t cch); # endif # ifdef kLdrHlpMemChr_needed void *kLdrHlpMemChr(const void *pv, int ch, size_t cb); # endif # ifdef kLdrHlpMemMove_needed void *kLdrHlpMemMove(void *pv1, const void *pv2, size_t cb); # endif #else /* KLDR_USE_CRT */ # include # include # ifdef _MSC_VER # include # endif # define kLdrHlpMemChr(a,b,c) memchr(a,b,c) /** memcmp */ # define kLdrHlpMemComp(a,b,c) memcmp(a,b,c) /** memcpy */ # define kLdrHlpMemCopy(a,b,c) memcpy(a,b,c) /** memmove */ # define kLdrHlpMemMove(a,b,c) memmove(a,b,c) /** memset */ # define kLdrHlpMemSet(a,b,c) memset(a,b,c) /** strchr */ # define kLdrHlpStrChr(a, b) strchr(a, b) /** strcmp */ # define kLdrHlpStrComp(a, b) strcmp(a, b) /** strncmp */ # define kLdrHlpStrNComp(a,b,c) strncmp(a, b, c) /** strlen */ # define kLdrHlpStrLen(a) strlen(a) /** alloca */ # define kLdrHlpAllocA(a) alloca(a) /** int3 */ # ifdef __GNUC__ # define kldrHlpBreakpoint() do { __asm__ __volatile__ ("int3\n\tnop"); } while (0) # endif # ifdef _MSC_VER # define kldrHlpBreakpoint() __debugbreak() # endif #endif #if (!defined(kLdrHlpMemChr) && !defined(kLdrHlpStrChr_needed))\ || !defined(kLdrHlpMemComp) \ || !defined(kLdrHlpMemCopy) \ || !defined(kLdrHlpMemSet) \ || (!defined(kLdrHlpStrChr) && !defined(kLdrHlpStrChr_needed)) \ || !defined(kLdrHlpStrComp) \ || (!defined(kLdrHlpStrNComp) && !defined(kLdrHlpStrNComp_needed)) \ || !defined(kLdrHlpStrLen) \ || !defined(kLdrHlpAllocA) \ || !defined(kldrHlpBreakpoint) # error "Needs porting to your compiler." #endif #ifdef __cplusplus extern "C" { #endif size_t kLdrHlpStrNLen(const char *psz, size_t cchMax); int kLdrHlpMemIComp(const void *pv1, const void *pv2, size_t cb); int kLdrHlpStrIComp(const char *pv1, const char *pv2); int kldrHlpSemInit(void); void kldrHlpSemTerm(void); int kldrHlpSemRequest(void); void kldrHlpSemRelease(void); int kldrHlpPageAlloc(void **ppv, size_t cb, KLDRPROT enmProt, unsigned fFixed); int kldrHlpPageProtect(void *pv, size_t cb, KLDRPROT enmProt); int kldrHlpPageFree(void *pv, size_t cb); int kldrHlpHeapInit(void); void kldrHlpHeapTerm(void); void kldrHlpHeapDonate(void *pv, size_t cb); void * kldrHlpAlloc(size_t cb); void * kldrHlpAllocZ(size_t cb); void kldrHlpFree(void *pv); int kldrHlpGetEnv(const char *pszVar, char *pszVal, size_t cchVal); int kldrHlpGetEnvUZ(const char *pszVar, size_t *pcb); char *kldrHlpGetFilename(const char *pszFilename); char *kldrHlpGetSuff(const char *pszFilename); char *kldrHlpGetExt(const char *pszFilename); int kldrHlpIsFilenameOnly(const char *pszFilename); void kldrHlpExit(int rc); void kldrHlpSleep(unsigned cMillies); char *kldrHlpInt2Ascii(char *psz, size_t cch, long lVal, unsigned iBase); void kldrHlpAssertMsg(const char *pszExpr, const char *pszFile, unsigned iLine, const char *pszFunction); #ifdef __cplusplus } #endif /** Assertion macro. * Users should wrap it since this is ALWAYS compiled in. */ #define kldrHlpAssert(expr) \ do { \ if (!(expr)) \ { \ kldrHlpAssertMsg(#expr, __FILE__, __LINE__, __FUNCTION__); \ kldrHlpBreakpoint(); \ } \ } while (0) /** @name Parameter validation macros * @{ */ /** Crash validation of a string argument. */ #define KLDRHLP_VALIDATE_STRING(str) \ do { kLdrHlpStrLen(str); } while (0) /** Crash validation of an optional string argument. */ #define KLDRHLP_VALIDATE_OPTIONAL_STRING(str) \ do { if (str) { KLDRHLP_VALIDATE_STRING(str); } } while (0) /** Return/Crash validation of an output buffer. */ #define KLDRHLP_VALIDATE_BUFFER(buf, cb) \ do { \ if ((cb)) \ { \ uint8_t __b; \ uint8_t volatile * __pb = (uint8_t volatile *)(buf); \ size_t __cbPage1 = 0x1000 - ((uintptr_t)(__pb) & 0xfff); /* ASSUMES page size! */ \ __b = *__pb; *__pb = 0xff; *__pb = __b; \ if ((cb) > __cbPage1) \ { \ size_t __cb = (cb) - __cbPage1; \ __pb -= __cbPage1; \ for (;;) \ { \ __b = *__pb; *__pb = 0xff; *__pb = __b; \ if (__cb < 0x1000) \ break; \ __pb += 0x1000; \ __cb -= 0x1000; \ } \ } \ } \ else \ return KLDR_ERR_INVALID_PARAMETER; \ } while (0) /** Crash validation of an optional output buffer. */ #define KLDRHLP_VALIDATE_OPTIONAL_BUFFER(buf, cb) \ do { \ if ((buf) != NULL && (cb) != 0) \ { \ KLDRHLP_VALIDATE_BUFFER(buf, cb); \ } \ } while (0) /** Return validation of an enum argument. */ #define KLDRHLP_VALIDATE_ENUM(arg, enumname) \ do { \ if ((arg) <= enumname##_INVALID || (arg) >= enumname##_END) \ { \ return KLDR_ERR_INVALID_PARAMETER; \ } \ } while (0) /** Return validation of a flags argument. */ #define KLDRHLP_VALIDATE_FLAGS(arg, AllowedMask) \ do { \ if ((arg) & ~(AllowedMask)) \ { \ return KLDR_ERR_INVALID_PARAMETER; \ } \ } while (0) /** @} */ /** @} */ #endif /* __kLdrHlp_h__ */