Jungo WinDriver  
Official Documentation
kpstdlib.h
Go to the documentation of this file.
1/* @JUNGO_COPYRIGHT@ */
2
3#ifndef _KPSTDLIB_H_
4#define _KPSTDLIB_H_
5
6#ifndef __KERNEL__
7 #define __KERNEL__
8#endif
9
10#if !defined(UNIX) && defined(LINUX)
11 #define UNIX
12#endif
13
14#if defined(UNIX)
15 #include "windrvr.h" // for use of KDBG DWORD parameter.
16#endif
17
18#ifdef __cplusplus
19extern "C" {
20#endif
21
22/* Spinlocks and interlocked operations */
23
25typedef struct _KP_SPINLOCK KP_SPINLOCK;
35
47
59
71
73typedef volatile int KP_INTERLOCKED;
74
86
98
110
122
136int kp_interlocked_add(KP_INTERLOCKED *target, int val);
137
149
162void kp_interlocked_set(KP_INTERLOCKED *target, int val);
163
179
180#if defined(WINNT) || defined(WIN32)
181 #if defined(_WIN64) && !defined(KERNEL_64BIT)
182 #define KERNEL_64BIT
183 #endif
184 typedef unsigned long ULONG;
185 typedef unsigned short USHORT;
186 typedef unsigned char UCHAR;
187 typedef long LONG;
188 typedef short SHORT;
189 typedef char CHAR;
190 typedef ULONG DWORD;
191 typedef DWORD *PDWORD;
192 typedef unsigned char *PBYTE;
193 typedef USHORT WORD;
194 typedef void *PVOID;
195 typedef char *PCHAR;
196 typedef PVOID HANDLE;
197 typedef ULONG BOOL;
198 typedef unsigned __int64 UINT64;
199 #ifndef WINAPI
200 #define WINAPI
201 #endif
202#elif defined(UNIX)
203 #ifndef __cdecl
204 #define __cdecl
205 #endif
206#endif
207
208#if defined(WINNT) || defined(WIN32)
209 #define OS_needs_copy_from_user(fKernelMode) FALSE
210 #define COPY_FROM_USER(dst,src,n) memcpy(dst,src,n)
211 #define COPY_TO_USER(dst,src,n) memcpy(dst,src,n)
212#elif defined(LINUX)
213 #define OS_needs_copy_from_user(fKernelMode) (!fKernelMode)
214 #define COPY_FROM_USER(dst,src,n) LINUX_copy_from_user(dst,src,n)
215 #define COPY_TO_USER(dst,src,n) LINUX_copy_to_user(dst,src,n)
216#endif
217
240#define COPY_FROM_USER_OR_KERNEL(dst, src, n, fKernelMode) \
241{ \
242 if (OS_needs_copy_from_user(fKernelMode)) \
243 COPY_FROM_USER(dst, src, n); \
244 else \
245 memcpy(dst, src, n); \
246}
247
252#define COPY_TO_USER_OR_KERNEL(dst, src, n, fKernelMode) \
253{ \
254 if (OS_needs_copy_from_user(fKernelMode)) \
255 COPY_TO_USER(dst, src, n); \
256 else \
257 memcpy(dst, src, n); \
258}
259
260#ifndef FALSE
261 #define FALSE 0
262#endif
263
264#ifndef TRUE
265 #define TRUE 1
266#endif
267
268#ifndef NULL
269 #define NULL 0UL
270#endif
271
272int __cdecl KDBG(DWORD dwLevel, DWORD dwSection, const char *format, ...);
273
274#if defined(WIN32)
275 #if defined(KERNEL_64BIT)
276 #include <stdarg.h>
277 #else
278 #define _INTSIZEOF(n) ((sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1))
279 // Define varargs ANSI style
280 typedef char * va_list;
281 #define va_start(ap,v) ( ap = (va_list)&v + _INTSIZEOF(v) )
282 #define va_arg(ap,t) ( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)) )
283 #define va_end(ap) ( ap = (va_list)0 )
284
285 #endif
286
287 int __cdecl _snprintf(char *buffer, unsigned long Limit, const char *format,
288 ...);
289 int __cdecl _vsnprintf(char *buffer, unsigned long Limit, const char
290 *format, va_list Next);
291#endif
292
293char* __cdecl strcpy(char *s1, const char *s2);
294void* __cdecl malloc(unsigned long size);
295void __cdecl free(void *buf);
296
297#if defined(LINUX)
298 #include <linux_wrappers.h>
299 #define memset LINUX_memset
300 #define strncmp LINUX_strncmp
301 #define strcpy LINUX_strcpy
302 #define strcmp LINUX_strcmp
303 #define strncpy LINUX_strncpy
304 #define strcat LINUX_strcat
305 #define strncat LINUX_strncat
306 #define strlen LINUX_strlen
307 #define memcpy LINUX_memcpy
308 #define memcmp LINUX_memcmp
309 #define sprintf LINUX_sprintf
310 #define vsprintf LINUX_vsprintf
311 #define snprintf LINUX_snprintf
312 #define vsnprintf LINUX_vsnprintf
313#elif defined(WINNT)
314 #if !defined size_t
315 #if defined(KERNEL_64BIT)
316 typedef unsigned __int64 size_t;
317 #else
318 typedef unsigned int size_t;
319 #endif
320 #endif
321 void* __cdecl memcpy(void *dest, const void *src, size_t count);
322 void* __cdecl memset(void *dest, int c, size_t count);
323#if !defined(_STRNCPY)
324 char* _strncpy(char* s1, const char* s2, size_t limit);
325#endif
326 #define snprintf _snprintf
327 #define vsnprintf _vsnprintf
328 #define strncpy _strncpy
329#endif
330
341
352
363
364#ifdef __cplusplus
365}
366#endif
367
368#endif /* _KPSTDLIB_H_ */
369
int kp_interlocked_decrement(KP_INTERLOCKED *target)
Decrements the value of a Kernel PlugIn interlocked counter by one.
int kp_interlocked_add(KP_INTERLOCKED *target, int val)
Adds a specified value to the current value of a Kernel PlugIn interlocked counter.
void kp_interlocked_set(KP_INTERLOCKED *target, int val)
Sets the value of a Kernel PlugIn interlocked counter to the specified value.
void kp_spinlock_uninit(KP_SPINLOCK *spinlock)
Uninitializes a Kernel PlugIn spinlock object.
UINT64 kp_get_clock_nanoseconds(void)
Get the current time in nanoseconds, relative to a non user determined starting point (Starting point...
void *__cdecl malloc(unsigned long size)
DWORD kp_get_clock_seconds(void)
Get the current time in seconds, relative to a non user determined starting point (Starting point dep...
struct _KP_SPINLOCK KP_SPINLOCK
Kernel PlugIn spinlock object structure.
Definition kpstdlib.h:25
int kp_interlocked_increment(KP_INTERLOCKED *target)
Increments the value of a Kernel PlugIn interlocked counter by one.
volatile int KP_INTERLOCKED
a Kernel PlugIn interlocked operations counter
Definition kpstdlib.h:73
KP_SPINLOCK * kp_spinlock_init(void)
Initializes a new Kernel PlugIn spinlock object.
int __cdecl KDBG(DWORD dwLevel, DWORD dwSection, const char *format,...)
void kp_interlocked_uninit(KP_INTERLOCKED *target)
Uninitializes a Kernel PlugIn interlocked counter.
void kp_interlocked_init(KP_INTERLOCKED *target)
Initializes a Kernel PlugIn interlocked counter.
void __cdecl free(void *buf)
char *__cdecl strcpy(char *s1, const char *s2)
int kp_interlocked_exchange(KP_INTERLOCKED *target, int val)
Sets the value of a Kernel PlugIn interlocked counter to the specified value and returns the previous...
int kp_interlocked_read(KP_INTERLOCKED *target)
Reads to the value of a Kernel PlugIn interlocked counter.
DWORD kp_get_clock_milliseconds(void)
Get the current time in milliseconds, relative to a non user determined starting point (Starting poin...
void kp_spinlock_wait(KP_SPINLOCK *spinlock)
Waits on a Kernel PlugIn spinlock object.
void kp_spinlock_release(KP_SPINLOCK *spinlock)
Releases a Kernel PlugIn spinlock object.
unsigned short int WORD
Definition windrvr.h:337
unsigned __int64 UINT64
Definition windrvr.h:318