00001
00002
00003
00004
00005
00006
00007
00008 #ifndef DEFINEH_H
00009 #define DEFINEH_H
00010
00011 #ifdef HAVE_CONFIG_H
00012 #include "config.h"
00013 #endif
00014
00015 #include "libpst.h"
00016 #include "timeconv.h"
00017 #include "libstrfunc.h"
00018 #include "vbuf.h"
00019
00020
00021 #ifdef HAVE_STRING_H
00022 #include <string.h>
00023 #endif
00024
00025 #ifdef HAVE_CTYPE_H
00026 #include <ctype.h>
00027 #endif
00028
00029 #ifdef HAVE_LIMITS_H
00030 #include <limits.h>
00031 #endif
00032
00033 #ifdef HAVE_WCHAR_H
00034 #include <wchar.h>
00035 #endif
00036
00037 #ifdef HAVE_SIGNAL_H
00038 #include <signal.h>
00039 #endif
00040
00041 #ifdef HAVE_ERRNO_H
00042 #include <errno.h>
00043 #endif
00044
00045 #ifdef HAVE_ICONV
00046 #include <iconv.h>
00047 #endif
00048
00049 #ifdef HAVE_REGEX_H
00050 #include <regex.h>
00051 #endif
00052
00053 #ifdef HAVE_GD_H
00054 #include <gd.h>
00055 #endif
00056
00057
00058 #define PERM_DIRS 0777
00059
00060 #ifdef _WIN32
00061 #include <direct.h>
00062
00063 #define D_MKDIR(x) mkdir(x)
00064 #define chdir _chdir
00065 #define strcasecmp _stricmp
00066 #define vsnprintf _vsnprintf
00067 #define snprintf _snprintf
00068 #ifdef _MSC_VER
00069 #define ftello _ftelli64
00070 #define fseeko _fseeki64
00071 #elif defined (__MINGW32__)
00072 #define ftello ftello64
00073 #define fseeko fseeko64
00074 #else
00075 #error Only MSC and mingw supported for Windows
00076 #endif
00077 #ifndef __MINGW32__
00078 #define size_t __int64
00079 #endif
00080 #ifndef UINT64_MAX
00081 #define UINT64_MAX ((uint64_t)0xffffffffffffffff)
00082 #endif
00083 #define PRIx64 "I64x"
00084 int __cdecl _fseeki64(FILE *, __int64, int);
00085 __int64 __cdecl _ftelli64(FILE *);
00086
00087 #ifdef __MINGW32__
00088 #include <getopt.h>
00089 #else
00090 #include "XGetopt.h"
00091 #endif
00092 #include <process.h>
00093 #undef gmtime_r
00094 #define gmtime_r(tp,tmp) (gmtime(tp)?(*(tmp)=*gmtime(tp),(tmp)):0)
00095 #define ctime_r(tp,tmp) (ctime(tp)?(strcpy((tmp),ctime((tp))),(tmp)):0)
00096 #else
00097 #ifdef __DJGPP__
00098 #define gmtime_r(tp,tmp) (gmtime(tp)?(*(tmp)=*gmtime(tp),(tmp)):0)
00099 #define ctime_r(tp,tmp) (ctime(tp)?(strcpy((tmp),ctime((tp))),(tmp)):0)
00100 #define fseeko(stream, offset, whence) fseek(stream, (long)offset, whence)
00101 #define ftello ftell
00102 #endif
00103 #ifdef HAVE_UNISTD_H
00104 #include <unistd.h>
00105 #else
00106 #include "XGetopt.h"
00107 #endif
00108 #define D_MKDIR(x) mkdir(x, PERM_DIRS)
00109 #endif
00110
00111 #ifdef HAVE_SYS_STAT_H
00112 #include <sys/stat.h>
00113 #endif
00114
00115 #ifdef HAVE_SYS_TYPES_H
00116 #include <sys/types.h>
00117 #endif
00118
00119 #ifdef HAVE_SYS_SHM_H
00120 #include <sys/shm.h>
00121 #endif
00122
00123 #ifdef HAVE_SYS_WAIT_H
00124 #include <sys/wait.h>
00125 #endif
00126
00127 #ifdef HAVE_DIRENT_H
00128 #include <dirent.h>
00129 #endif
00130
00131 #ifdef HAVE_SEMAPHORE_H
00132 #include <semaphore.h>
00133 #endif
00134
00135
00136 void pst_debug_lock();
00137 void pst_debug_unlock();
00138 void pst_debug_setlevel(int level);
00139 void pst_debug_init(const char* fname, void* output_mutex);
00140 void pst_debug_func(int level, const char* function);
00141 void pst_debug_func_ret(int level);
00142 void pst_debug(int level, int line, const char *file, const char *fmt, ...);
00143 void pst_debug_hexdump(int level, int line, const char *file, const char* buf, size_t size, int cols, int delta);
00144 void pst_debug_hexdumper(FILE* out, const char* buf, size_t size, int cols, int delta);
00145 void pst_debug_close();
00146 void* pst_malloc(size_t size);
00147 void *pst_realloc(void *ptr, size_t size);
00148
00149 #define MESSAGEPRINT1(...) pst_debug(1, __LINE__, __FILE__, __VA_ARGS__)
00150 #define MESSAGEPRINT2(...) pst_debug(2, __LINE__, __FILE__, __VA_ARGS__)
00151 #define MESSAGEPRINT3(...) pst_debug(3, __LINE__, __FILE__, __VA_ARGS__)
00152
00153 #define WARN(x) { \
00154 MESSAGEPRINT3 x; \
00155 pst_debug_lock(); \
00156 printf x; \
00157 fflush(stdout); \
00158 pst_debug_unlock(); \
00159 }
00160
00161 #define DIE(x) { \
00162 WARN(x); \
00163 exit(EXIT_FAILURE); \
00164 }
00165
00166 #define DEBUG_WARN(x) MESSAGEPRINT3 x
00167 #define DEBUG_INFO(x) MESSAGEPRINT2 x
00168 #define DEBUG_HEXDUMP(x, s) pst_debug_hexdump(1, __LINE__, __FILE__, (char*)x, s, 0x10, 0)
00169 #define DEBUG_HEXDUMPC(x, s, c) pst_debug_hexdump(1, __LINE__, __FILE__, (char*)x, s, c, 0)
00170
00171
00172 #define DEBUG_ENT(x) \
00173 { \
00174 pst_debug_func(1, x); \
00175 pst_debug(1, __LINE__, __FILE__, "Entering function\n"); \
00176 }
00177 #define DEBUG_RET() \
00178 { \
00179 pst_debug(1, __LINE__, __FILE__, "Leaving function\n"); \
00180 pst_debug_func_ret(1); \
00181 }
00182
00183 #define DEBUG_INIT(fname,mutex) {pst_debug_init(fname,mutex);}
00184 #define DEBUG_CLOSE() {pst_debug_close();}
00185 #define RET_DERROR(res, ret_val, x) if (res) { DIE(x);}
00186
00187
00188
00189 #if BYTE_ORDER == BIG_ENDIAN
00190 # define LE64_CPU(x) \
00191 x = ((((x) & UINT64_C(0xff00000000000000)) >> 56) | \
00192 (((x) & UINT64_C(0x00ff000000000000)) >> 40) | \
00193 (((x) & UINT64_C(0x0000ff0000000000)) >> 24) | \
00194 (((x) & UINT64_C(0x000000ff00000000)) >> 8 ) | \
00195 (((x) & UINT64_C(0x00000000ff000000)) << 8 ) | \
00196 (((x) & UINT64_C(0x0000000000ff0000)) << 24) | \
00197 (((x) & UINT64_C(0x000000000000ff00)) << 40) | \
00198 (((x) & UINT64_C(0x00000000000000ff)) << 56));
00199 # define LE32_CPU(x) \
00200 x = ((((x) & 0xff000000) >> 24) | \
00201 (((x) & 0x00ff0000) >> 8 ) | \
00202 (((x) & 0x0000ff00) << 8 ) | \
00203 (((x) & 0x000000ff) << 24));
00204 # define LE16_CPU(x) \
00205 x = ((((x) & 0xff00) >> 8) | \
00206 (((x) & 0x00ff) << 8));
00207 #elif BYTE_ORDER == LITTLE_ENDIAN
00208 # define LE64_CPU(x) {}
00209 # define LE32_CPU(x) {}
00210 # define LE16_CPU(x) {}
00211 #else
00212 # error "Byte order not supported by this library"
00213 #endif // BYTE_ORDER
00214
00215
00216 #define PST_LE_GET_UINT64(p) \
00217 (uint64_t)((((uint8_t const *)(p))[0] << 0) | \
00218 (((uint8_t const *)(p))[1] << 8) | \
00219 (((uint8_t const *)(p))[2] << 16) | \
00220 (((uint8_t const *)(p))[3] << 24) | \
00221 (((uint8_t const *)(p))[4] << 32) | \
00222 (((uint8_t const *)(p))[5] << 40) | \
00223 (((uint8_t const *)(p))[6] << 48) | \
00224 (((uint8_t const *)(p))[7] << 56))
00225
00226 #define PST_LE_GET_INT64(p) \
00227 (int64_t)((((uint8_t const *)(p))[0] << 0) | \
00228 (((uint8_t const *)(p))[1] << 8) | \
00229 (((uint8_t const *)(p))[2] << 16) | \
00230 (((uint8_t const *)(p))[3] << 24) | \
00231 (((uint8_t const *)(p))[4] << 32) | \
00232 (((uint8_t const *)(p))[5] << 40) | \
00233 (((uint8_t const *)(p))[6] << 48) | \
00234 (((uint8_t const *)(p))[7] << 56))
00235
00236 #define PST_LE_GET_UINT32(p) \
00237 (uint32_t)((((uint8_t const *)(p))[0] << 0) | \
00238 (((uint8_t const *)(p))[1] << 8) | \
00239 (((uint8_t const *)(p))[2] << 16) | \
00240 (((uint8_t const *)(p))[3] << 24))
00241
00242 #define PST_LE_GET_INT32(p) \
00243 (int32_t)((((uint8_t const *)(p))[0] << 0) | \
00244 (((uint8_t const *)(p))[1] << 8) | \
00245 (((uint8_t const *)(p))[2] << 16) | \
00246 (((uint8_t const *)(p))[3] << 24))
00247
00248 #define PST_LE_GET_UINT16(p) \
00249 (uint16_t)((((uint8_t const *)(p))[0] << 0) | \
00250 (((uint8_t const *)(p))[1] << 8))
00251
00252 #define PST_LE_GET_INT16(p) \
00253 (int16_t)((((uint8_t const *)(p))[0] << 0) | \
00254 (((uint8_t const *)(p))[1] << 8))
00255
00256 #define PST_LE_GET_UINT8(p) (*(uint8_t const *)(p))
00257
00258 #define PST_LE_GET_INT8(p) (*(int8_t const *)(p))
00259
00260
00261 #endif //DEFINEH_H