/* $Id: kLdrBase.h 3525 2007-08-19 22:14:55Z bird $ */ /** @file * * kLdr - The Dynamic Loader, Base Definitions and Typedefs. * * Copyright (c) 2006-2007 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 ___kLdrBase_h___ #define ___kLdrBase_h___ /** @defgroup grp_kLdrBase kLdrBase - Base Definitions And Typedefs * @{ */ /* * kLdr depend on size_t, [u]intNN_t, [u]intptr_t and some related constants. * If KLDR_ALREADY_INCLUDED_STD_TYPES is defined, these has already been defined. */ #ifndef KLDR_ALREADY_INCLUDED_STD_TYPES # include # include # ifdef _MSC_VER typedef signed char int8_t; typedef unsigned char uint8_t; typedef signed short int16_t; typedef unsigned short uint16_t; typedef signed int int32_t; typedef unsigned int uint32_t; typedef signed __int64 int64_t; typedef unsigned __int64 uint64_t; typedef int64_t intmax_t; typedef uint64_t uintmax_t; # define UINT8_C(c) (c) # define UINT16_C(c) (c) # define UINT32_C(c) (c ## U) # define UINT64_C(c) (c ## ULL) # define INT8_C(c) (c) # define INT16_C(c) (c) # define INT32_C(c) (c) # define INT64_C(c) (c ## LL) # define INT8_MIN (INT8_C(-0x7f) - 1) # define INT16_MIN (INT16_C(-0x7fff) - 1) # define INT32_MIN (INT32_C(-0x7fffffff) - 1) # define INT64_MIN (INT64_C(-0x7fffffffffffffff) - 1) # define INT8_MAX INT8_C(0x7f) # define INT16_MAX INT16_C(0x7fff) # define INT32_MAX INT32_C(0x7fffffff) # define INT64_MAX INT64_C(0x7fffffffffffffff) # define UINT8_MAX UINT8_C(0xff) # define UINT16_MAX UINT16_C(0xffff) # define UINT32_MAX UINT32_C(0xffffffff) # define UINT64_MAX UINT64_C(0xffffffffffffffff) # else # include # endif #endif /* !KLDR_ALREADY_INCLUDED_STD_TYPES */ /** 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) ) ) /** Align a size_t value. */ #define KLDR_ALIGN_ADDR(val, align) ( ((val) + ((align) - 1)) & ~(KLDRADDR)((align) - 1) ) /** Number of elements in an array. */ #define KLDR_ELEMENTS(a) ( sizeof(a) / sizeof((a)[0]) ) /** @def KLDR_LITTLE_ENDIAN * The kLdr build is for a little endian target. */ /** @def KLDR_BIG_ENDIAN * The kLdr build is for a big endian target. */ #if !defined(KLDR_LITTLE_ENDIAN) && !defined(KLDR_BIG_ENDIAN) # define KLDR_LITTLE_ENDIAN #endif #ifdef __DOXYGEN__ # define KLDR_BIG_ENDIAN #endif /** @name Endian Conversion * @{ */ /** @def KLDR_E2E_U16 * Convert the endian of an unsigned 16-bit value. */ # define KLDR_E2E_U16(u16) ( (uint16_t) (((u16) >> 8) | ((u16) << 8)) ) /** @def KLDR_E2E_U32 * Convert the endian of an unsigned 32-bit value. */ # define KLDR_E2E_U32(u32) ( ( ((u32) & UINT32_C(0xff000000)) >> 24 ) \ | ( ((u32) & UINT32_C(0x00ff0000)) >> 8 ) \ | ( ((u32) & UINT32_C(0x0000ff00)) << 8 ) \ | ( ((u32) & UINT32_C(0x000000ff)) << 24 ) \ ) /** @def KLDR_E2E_U64 * Convert the endian of an unsigned 64-bit value. */ # define KLDR_E2E_U64(u64) ( ( ((u64) & UINT64_C(0xff00000000000000)) >> 56 ) \ | ( ((u64) & UINT64_C(0x00ff000000000000)) >> 40 ) \ | ( ((u64) & UINT64_C(0x0000ff0000000000)) >> 24 ) \ | ( ((u64) & UINT64_C(0x000000ff00000000)) >> 8 ) \ | ( ((u64) & UINT64_C(0x00000000ff000000)) << 8 ) \ | ( ((u64) & UINT64_C(0x0000000000ff0000)) << 24 ) \ | ( ((u64) & UINT64_C(0x000000000000ff00)) << 40 ) \ | ( ((u64) & UINT64_C(0x00000000000000ff)) << 56 ) \ ) /** @def KLDR_LE2H_U16 * Unsigned 16-bit little-endian to host endian. */ /** @def KLDR_LE2H_U32 * Unsigned 32-bit little-endian to host endian. */ /** @def KLDR_LE2H_U64 * Unsigned 64-bit little-endian to host endian. */ /** @def KLDR_BE2H_U16 * Unsigned 16-bit big-endian to host endian. */ /** @def KLDR_BE2H_U32 * Unsigned 32-bit big-endian to host endian. */ /** @def KLDR_BE2H_U64 * Unsigned 64-bit big-endian to host endian. */ #ifdef KLDR_LITTLE_ENDIAN # define KLDR_LE2H_U16(u16) ((uint16_t)(u16)) # define KLDR_LE2H_U32(u32) ((uint32_t)(u32)) # define KLDR_LE2H_U64(u64) ((uint32_t)(u32)) # define KLDR_BE2H_U16(u16) KLDR_E2E_U16(u16) # define KLDR_BE2H_U32(u32) KLDR_E2E_U32(u32) # define KLDR_BE2H_U64(u64) KLDR_E2E_U64(u64) #elif defined(KLDR_BIG_ENDIAN) # define KLDR_LE2H_U16(u16) KLDR_E2E_U16(u16) # define KLDR_LE2H_U32(u32) KLDR_E2E_U32(u32) # define KLDR_LE2H_U32(u64) KLDR_E2E_U64(u64) # define KLDR_BE2H_U16(u16) ((uint16_t)(u16)) # define KLDR_BE2H_U32(u32) ((uint32_t)(u32)) # define KLDR_BE2H_U64(u64) ((uint32_t)(u32)) #else # error "KLDR_BIG_ENDIAN or KLDR_LITTLE_ENDIAN is supposed to be defined." #endif /** @} */ /** @} */ #endif