source: trunk/kLdr/kLdrHlp.h@ 2880

Last change on this file since 2880 was 2880, checked in by bird, 19 years ago

Loading of LX bits, including exepack and exepack2 decompression.

  • Property svn:keywords set to Id
File size: 8.3 KB
Line 
1/* $Id: kLdrHlp.h 2880 2006-11-13 20:20:19Z bird $ */
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
31/** @defgroup grp_kLdrHlp kLdrHlp - Helper Functions
32 * @internal
33 * @{ */
34
35/** Get the minimum of two values. */
36#define KLDR_MIN(a, b) ((a) <= (b) ? (a) : (b))
37/** Get the maximum of two values. */
38#define KLDR_MAX(a, b) ((a) >= (b) ? (a) : (b))
39/** Calculate the offset of a structure member. */
40#define KLDR_OFFSETOF(strct, memb) ( (size_t)( &((strct *)0)->memb ) )
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) ) )
45/** Number of elements in an array. */
46#define KLDR_ELEMENTS(a) ( sizeof(a) / sizeof((a)[0]) )
47
48/** @def KLDRHLP_LE2H_U16
49 * Unsigned 16-bit little-endian to host translation. */
50/** @def KLDRHLP_LE2H_U32
51 * Unsigned 32-bit little-endian to host translation. */
52#if 1
53# define KLDRHLP_LE2H_U16(u16) ((uint16_t)(u16))
54# define KLDRHLP_LE2H_U32(u32) ((uint32_t)(u32))
55#else
56# define KLDRHLP_LE2H_U16(u16) ( (uint16_t) (((u16) >> 8) | ((u16) << 8)) )
57# define KLDRHLP_LE2H_U32(u32) ( ( ((u32) & UINT32_C(0xff000000)) >> 24 ) \
58 | ( ((u32) & UINT32_C(0x00ff0000)) >> 8 ) \
59 | ( ((u32) & UINT32_C(0x0000ff00)) << 8 ) \
60 | ( ((u32) & UINT32_C(0x000000ff)) << 24 ) \
61 )
62#endif
63
64/*
65 * Compiler specific helpers.
66 * (I.e. operations that tend to have compiler intrinsic implementations).
67 */
68#ifdef __GNUC__
69/** memchr */
70# define kLdrHlpMemChr(a,b,c) __builtin_memchr(a,b,c)
71/** memcmp */
72# define kLdrHlpMemComp(a,b,c) __builtin_memcmp(a,b,c)
73/** memcpy */
74# define kLdrHlpMemCopy(a,b,c) __builtin_memcpy(a,b,c)
75/** memmove */
76# define kLdrHlpMemMove(a,b,c) __builtin_memmove(a,b,c)
77/** memset */
78# define kLdrHlpMemSet(a,b,c) __builtin_memset(a,b,c)
79/** strchr */
80# define kLdrHlpStrChr(a, b) __builtin_strchr(a, b)
81/** strcmp */
82# define kLdrHlpStrComp(a, b) __builtin_strcmp(a, b)
83/** strlen */
84# define kLdrHlpStrLen(a) __builtin_strlen(a)
85/** alloca */
86# define kLdrHlpAllocA(a) __builtin_alloca(a)
87/** int3 */
88# define kldrHlpBreakpoint() do { __asm__ __volatile__ ("int3\n\tnop"); } while (0)
89/** NULL */
90# ifndef NULL
91# define NULL 0
92# endif
93#endif
94
95#ifdef _MSC_VER
96# include <string.h>
97# include <malloc.h>
98# pragma intrinsic(memcmp, memcpy, memset, strcmp, strlen, __debugbreak)
99/** memchr */
100# define kLdrHlpMemChr_needed
101/** memcmp */
102# define kLdrHlpMemComp(a,b,c) memcmp(a,b,c)
103/** memcpy */
104# define kLdrHlpMemCopy(a,b,c) memcpy(a,b,c)
105/** memmove */
106# define kLdrHlpMemMove_needed
107/** memset */
108# define kLdrHlpMemSet(a,b,c) memset(a,b,c)
109/** strcmp */
110# define kLdrHlpStrComp(a, b) strcmp(a, b)
111/** strlen */
112# define kLdrHlpStrLen(a) strlen(a)
113/** strchr */
114# define kLdrHlpStrChr_needed
115/** alloca */
116# define kLdrHlpAllocA(a) alloca(a)
117/** int3 */
118# define kldrHlpBreakpoint() __debugbreak()
119/** NULL */
120# ifndef NULL
121# define NULL 0
122# endif
123#endif
124
125#ifdef kLdrHlpStrChr_needed
126char *kLdrHlpStrChr(const char *psz, int ch);
127#endif
128#ifdef kLdrHlpStrChr_needed
129void *kLdrHlpMemChr(const void *pv, int ch, size_t cb);
130#endif
131#ifdef kLdrHlpStrChr_needed
132void *kLdrHlpMemMove(void *pv1, const void *pv2, size_t cb);
133#endif
134int kLdrHlpMemIComp(const void *pv1, const void *pv2, size_t cb);
135int kLdrHlpStrIComp(const char *pv1, const char *pv2);
136
137
138#if (!defined(kLdrHlpMemChr) && !defined(kLdrHlpStrChr_needed))\
139 || !defined(kLdrHlpMemComp) \
140 || !defined(kLdrHlpMemCopy) \
141 || !defined(kLdrHlpMemSet) \
142 || (!defined(kLdrHlpStrChr) && !defined(kLdrHlpStrChr_needed)) \
143 || !defined(kLdrHlpStrLen) \
144 || !defined(kLdrHlpAllocA) \
145 || !defined(kldrHlpBreakpoint)
146# error "Needs porting to your compiler."
147#endif
148
149int kldrHlpSemInit(void);
150void kldrHlpSemTerm(void);
151int kldrHlpSemRequest(void);
152void kldrHlpSemRelease(void);
153
154int kldrHlpPageAlloc(void **ppv, size_t cb, KLDRPROT enmProt, unsigned fFixed);
155int kldrHlpPageProtect(void *pv, size_t cb, KLDRPROT enmProt);
156int kldrHlpPageFree(void *pv, size_t cb);
157
158int kldrHlpHeapInit(void);
159void kldrHlpHeapTerm(void);
160void kldrHlpHeapDonate(void *pv, size_t cb);
161void * kldrHlpAlloc(size_t cb);
162void * kldrHlpAllocZ(size_t cb);
163void kldrHlpFree(void *pv);
164
165int kldrHlpGetEnv(const char *pszVar, char *pszVal, size_t cchVal);
166int kldrHlpGetEnvUZ(const char *pszVar, size_t *pcb);
167char *kldrHlpGetFilename(const char *pszFilename);
168char *kldrHlpGetSuff(const char *pszFilename);
169char *kldrHlpGetExt(const char *pszFilename);
170int kldrHlpIsFilenameOnly(const char *pszFilename);
171void kldrHlpExit(int rc);
172void kldrHlpSleep(unsigned cMillies);
173char *kldrHlpInt2Ascii(char *psz, size_t cch, long lVal, unsigned iBase);
174void kldrHlpAssertMsg(const char *pszExpr, const char *pszFile, unsigned iLine, const char *pszFunction);
175
176/** Assertion macro.
177 * Users should wrap it since this is ALWAYS compiled in. */
178#define kldrHlpAssert(expr) \
179 do { \
180 if (!(expr)) \
181 { \
182 kldrHlpAssertMsg(#expr, __FILE__, __LINE__, __FUNCTION__); \
183 kldrHlpBreakpoint(); \
184 } \
185 } while (0)
186
187
188/** @name Parameter validation macros
189 * @{ */
190
191/** Crash validation of a string argument. */
192#define KLDRHLP_VALIDATE_STRING(str) \
193 do { strlen(str); } while (0)
194
195/** Crash validation of an optional string argument. */
196#define KLDRHLP_VALIDATE_OPTIONAL_STRING(str) \
197 do { if (str) { KLDRHLP_VALIDATE_STRING(str); } } while (0)
198
199/** Return/Crash validation of an output buffer. */
200#define KLDRHLP_VALIDATE_BUFFER(buf, cb) \
201 do { \
202 if ((cb)) \
203 { \
204 uint8_t __b; \
205 uint8_t volatile * __pb = (uint8_t volatile *)(buf); \
206 size_t __cbPage1 = 0x1000 - ((uintptr_t)(__pb) & 0xfff); /* ASSUMES page size! */ \
207 __b = *__pb; *__pb = 0xff; *__pb = __b; \
208 if ((cb) > __cbPage1) \
209 { \
210 size_t __cb = (cb) - __cbPage1; \
211 __pb -= __cbPage1; \
212 for (;;) \
213 { \
214 __b = *__pb; *__pb = 0xff; *__pb = __b; \
215 if (__cb < 0x1000) \
216 break; \
217 __pb += 0x1000; \
218 __cb -= 0x1000; \
219 } \
220 } \
221 } \
222 else \
223 return KLDR_ERR_INVALID_PARAMETER; \
224 } while (0)
225
226/** Crash validation of an optional output buffer. */
227#define KLDRHLP_VALIDATE_OPTIONAL_BUFFER(buf, cb) \
228 do { \
229 if ((buf) != NULL && (cb) != 0) \
230 { \
231 KLDRHLP_VALIDATE_BUFFER(buf, cb); \
232 } \
233 } while (0)
234
235/** Return validation of an enum argument. */
236#define KLDRHLP_VALIDATE_ENUM(arg, enumname) \
237 do { \
238 if ((arg) <= enumname##_INVALID || (arg) >= enumname##_END) \
239 { \
240 return KLDR_ERR_INVALID_PARAMETER; \
241 } \
242 } while (0)
243
244/** Return validation of a flags argument. */
245#define KLDRHLP_VALIDATE_FLAGS(arg, AllowedMask) \
246 do { \
247 if ((arg) & ~(AllowedMask)) \
248 { \
249 return KLDR_ERR_INVALID_PARAMETER; \
250 } \
251 } while (0)
252
253/** @} */
254
255
256/** @} */
257
258#endif /* __kLdrHlp_h__ */
259
Note: See TracBrowser for help on using the repository browser.