/* $Id: kLdrHlp.h 2891 2006-11-21 21:40:45Z 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 * @{ */ /** Get the minimum of two values. */ #define KLDR_MIN(a, b) ((a) <= (b) ? (a) : (b)) /** Get the maximum of two values. */ #define KLDR_MAX(a, b) ((a) >= (b) ? (a) : (b)) /** Calculate the offset of a structure member. */ #define KLDR_OFFSETOF(strct, memb) ( (size_t)( &((strct *)0)->memb ) ) /** Align a size_t value. */ #define KLDR_ALIGN_Z(val, align) ( ((val) + ((align) - 1)) & ~(size_t)((align) - 1) ) /** Align a void * value. */ #define KLDR_ALIGN_P(pv, align) ( (void *)( ((uintptr_t)(pv) + ((align) - 1)) & ~(uintptr_t)((align) - 1) ) ) /** Number of elements in an array. */ #define KLDR_ELEMENTS(a) ( sizeof(a) / sizeof((a)[0]) ) /** @def KLDRHLP_LE2H_U16 * Unsigned 16-bit little-endian to host translation. */ /** @def KLDRHLP_LE2H_U32 * Unsigned 32-bit little-endian to host translation. */ #if 1 # define KLDRHLP_LE2H_U16(u16) ((uint16_t)(u16)) # define KLDRHLP_LE2H_U32(u32) ((uint32_t)(u32)) #else # define KLDRHLP_LE2H_U16(u16) ( (uint16_t) (((u16) >> 8) | ((u16) << 8)) ) # define KLDRHLP_LE2H_U32(u32) ( ( ((u32) & UINT32_C(0xff000000)) >> 24 ) \ | ( ((u32) & UINT32_C(0x00ff0000)) >> 8 ) \ | ( ((u32) & UINT32_C(0x0000ff00)) << 8 ) \ | ( ((u32) & UINT32_C(0x000000ff)) << 24 ) \ ) #endif /* * Compiler specific helpers. * (I.e. operations that tend to have compiler intrinsic implementations). */ #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 int kLdrHlpMemIComp(const void *pv1, const void *pv2, size_t cb); int kLdrHlpStrIComp(const char *pv1, const char *pv2); #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 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); /** 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__ */