]> FriiDump Source - friidump.git/blob - libfriidump/misc.h
Fix MSVC CMake SHA1 header resolution
[friidump.git] / libfriidump / misc.h
1 /***************************************************************************
2  *   Copyright (C) 2007 by Arep                                            *
3  *   Support is provided through the forums at                             *
4  *   http://wii.console-tribe.com                                          *
5  *                                                                         *
6  *   This program is free software; you can redistribute it and/or modify  *
7  *   it under the terms of the GNU General Public License as published by  *
8  *   the Free Software Foundation; either version 2 of the License, or     *
9  *   (at your option) any later version.                                   *
10  *                                                                         *
11  *   This program is distributed in the hope that it will be useful,       *
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14  *   GNU General Public License for more details.                          *
15  *                                                                         *
16  *   You should have received a copy of the GNU General Public License     *
17  *   along with this program; if not, write to the                         *
18  *   Free Software Foundation, Inc.,                                       *
19  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
20  ***************************************************************************/
21
22 #ifndef MISC_H_INCLUDED
23 #define MISC_H_INCLUDED
24
25 /*** Include site configuration ***/
26 #ifdef HAVE_CONFIG_H
27 #include <config.h>
28 #endif
29 /******/
30
31 /*** Windows stuff ***/
32 #include "win32compat.h"
33
34 #define _GNU_SOURCE             // For strndup()
35
36 #include <stdio.h>
37 #include <string.h>
38 #include <sys/types.h>
39
40
41 /*** ASSERTIONS ***/
42 #define MY_ASSERT(cond) \
43         if (!(cond)) { \
44                 fprintf (stderr, "*** ASSERTION FAILED at " __FILE__ ":%d: " # cond "\n", __LINE__); \
45                 exit (9); \
46         }
47
48 /*** LOGGING STUFF ***/
49 enum {
50         LOG_NORMAL              = 1 << 0,
51         LOG_WARNING             = 1 << 1,
52         LOG_ERROR               = 1 << 2,
53 #ifdef DEBUG
54         LOG_DEBUG               = 1 << 4,
55 #endif  
56         LOG_NOCR                = 1 << 15       /* Use in OR to avoid a trailing newline */
57 };
58
59 /* Don't use this function explicitly. Use the macros below. */
60 void _logprintf (int level, char tag[], char format[], ...);
61
62 #ifdef DEBUG
63         #define logprintf(level, ...) _logprintf (level, (char *) __FUNCTION__, __VA_ARGS__)
64
65         #define debug(...) logprintf(LOG_DEBUG, __VA_ARGS__)
66         #define debug_nocr(...) logprintf(LOG_DEBUG | LOG_NOCR, __VA_ARGS__)
67 #else
68         #define logprintf(level, ...) _logprintf (level, NULL, __VA_ARGS__)
69
70         #define debug(...) do {;} while (0);
71         #define debug_nocr(...) do {;} while (0);
72 #endif
73
74 #if defined (DEBUG) || defined (VERBOSE)
75 #define log(...) logprintf (LOG_NORMAL, __VA_ARGS__)
76 #define warning(...) logprintf (LOG_WARNING, __VA_ARGS__)
77 #define error(...) logprintf (LOG_ERROR, __VA_ARGS__)
78 #else
79 #define log(...)
80 #define warning(...)
81 #define error(...)
82 #endif
83 /******/
84
85 void hex_and_ascii_print_with_offset(const char *ident, register const u_int8_t *cp,
86                                                                         register u_int16_t length, register u_int16_t oset);
87 #define hex_and_ascii_print(ident, cp, length) hex_and_ascii_print_with_offset(ident, cp, length, 0)
88
89
90 #define my_strdup(dest, src) \
91         if (!(dest = strdup (src))) { \
92                 fprintf (stderr, "strdup() failed\n"); \
93                 exit (101); \
94         }
95
96 #define my_strndup(dest, src, c) \
97         if (!(dest = strndup ((const char *) src, c))) { \
98                 fprintf (stderr, "strndup() failed\n"); \
99                 exit (102); \
100         }
101
102 char *strtrimr (char *s);
103
104 #define my_free(p) \
105         if (p) { \
106                 free (p); \
107                 p = NULL; \
108         }
109
110
111 /*** STUFF FOR PORTABLE LARGE-FILES FSEEK ***/
112 #if defined(_MSC_VER)
113 typedef __int64 my_off_t;
114 #elif defined (HAVE_OFF_T) && SIZEOF_OFF_T >= 8
115 typedef off_t my_off_t;
116 #elif defined (HAVE_FPOS_T) && SIZEOF_FPOS_T >= 8
117 typedef fpos_t my_off_t;
118 #else
119 typedef u_int64_t my_off_t;
120 #endif
121
122 int my_fseek (FILE *fp, my_off_t offset, int whence);
123 my_off_t my_ftell (FILE* fp);
124 /*******/
125
126
127 /*** STUFF FOR DROPPING PRIVILEGES ***/
128 FRIIDUMPLIB_EXPORT void drop_euid ();
129 FRIIDUMPLIB_EXPORT void upgrade_euid ();
130 /******/
131
132
133 /*** STUFF FOR BOOLEAN DATA TYPE ***/
134 #ifdef HAVE_STDBOOL_H
135 /* If using a C99 compiler, use the builtin boolean type */
136 #include <stdbool.h>
137 #else
138 typedef enum {false,true} bool;
139 #endif
140 /******/
141
142 #endif