-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcompat.h
129 lines (105 loc) · 3.48 KB
/
compat.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#ifndef __COMPAT_H__
#define __COMPAT_H__
// You should use strcasecmp/ftruncate. On Windows, MinGW automatically translates it into stricmp/chsize.
// Never use _lrotl/r. use lrotl/r.
// VisualC++ / C++Builder won't be supported.
#ifdef __cplusplus
extern "C"{
#endif
//well well... iPodLinux.
#ifndef NODLOPEN
#define _FILE_OFFSET_BITS 64
#endif
typedef unsigned char byte;
typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;
typedef unsigned long long u64;
typedef char s8;
typedef short s16;
typedef int s32;
typedef long long s64;
#if !defined(__cplusplus)
//typedef enum { false, true } bool;
#include <stdbool.h>
#endif
#if !defined(__cplusplus) && !defined(min)
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#endif
#define fourcc(a,b,c,d) ((u32)(a) | ((u32)(b)<<8) | ((u32)(c)<<16) | ((u32)(d)<<24))
#define between(a,x,b) ((a)<=(x)&&(x)<=(b))
#define upcase(c) (between('a',(unsigned char)(c),'z')?c-' ':c)
#define downcase(c) (between('A',(unsigned char)(c),'Z')?c+' ':c)
//I hope (sizeof(val)*CHAR_BIT-(rot)) will be precalculated in compilation.
#define lrotr(val,rot) (( (val)<<(sizeof(val)*CHAR_BIT-(rot)) )|( (val)>>(rot) ))
#define lrotl(val,rot) (( (val)<<(rot) )|( (val)>>(sizeof(val)*CHAR_BIT-(rot)) ))
#if defined(_WIN32) || (!defined(__GNUC__) && !defined(__clang__))
#define _myIsWindows 1
#include <windows.h>
#include <fcntl.h>
#include <io.h>
#define sleep(t) Sleep(1000*(t))
#define initstdio() setmode(fileno(stdin),O_BINARY),setmode(fileno(stdout),O_BINARY),setmode(fileno(stderr),O_BINARY);
//bah, msvcrt.a is obsolete...?
#define filelengthi64(f) filelength(f)
#define OPEN_BINARY O_BINARY
//because of nasty msvcrt
#define LLD "I64d"
#define LLU "I64u"
#define LLX "I64x"
#else
#define _myIsWindows 0
#include <unistd.h>
#include <sys/stat.h>
int filelength(int fd);
long long filelengthi64(int fd);
#define initstdio()
#define OPEN_BINARY 0
#define LLD "lld"
#define LLU "llu"
#define LLX "llx"
#include <fcntl.h>
#ifndef NODLOPEN //dynamic load
#if defined(__linux__) && !defined(_GNU_SOURCE) && !defined(DL_ANDROID)
typedef struct{
const char *dli_fname; /* File name of defining object. */
void *dli_fbase; /* Load address of that object. */
const char *dli_sname; /* Name of nearest symbol. */
void *dli_saddr; /* Exact value of nearest symbol. */
} Dl_info;
int dladdr(void *addr, Dl_info *info); //to check returned address validity
int dlinfo(void *handle, int request, void *info); //GetModuleFileNameA impl
#define RTLD_DI_LINKMAP 2
#endif
#include <dlfcn.h>
#define LoadLibraryA(filename) dlopen(filename,RTLD_NOW)
#define GetProcAddress dlsym
#define FreeLibrary dlclose
#define DLADDR_OK
int GetModuleFileNameA(void *hModule,char *pFilename,int nSize);
#endif
#endif
int sfilelength(const char *path);
long long sfilelengthi64(const char *path);
int filemode(int fd);
int sfilemode(const char *path);
//p should be 2,4,8,...
#define align2p(p,i) (((i)+((p)-1))&~((p)-1))
#define align2(i) align2p(2,i)
#define align4(i) align2p(4,i)
#define align8(i) align2p(8,i)
#define align256(i) align2p(256,i)
#define align512(i) align2p(512,i)
#define swiCRC16 crc16
#define BIT(n) (1<<(n))
#define isRedirected(file) (!isatty(fileno(file)))
//currently 4MB. some game cheat entry is more than 3MB...
#define BUFLEN (1<<22)
extern unsigned char buf[BUFLEN];
#define cbuf ((char*)buf)
#define FNAME_MAX32 512
#ifdef __cplusplus
}
#endif
#endif