-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathlibxpwn.c
83 lines (67 loc) · 1.64 KB
/
libxpwn.c
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
#include <stdarg.h>
#include <string.h>
#include "common.h"
#include "libxpwn.h"
LogMessageCallback logCallback;
char endianness;
int GlobalLogLevel;
int Img3DecryptLast = TRUE; /* FALSE for <= 7a341, TRUE for >= 7c144 */
void TestByteOrder()
{
short int word = 0x0001;
char *byte = (char *) &word;
endianness = byte[0] ? IS_LITTLE_ENDIAN : IS_BIG_ENDIAN;
}
void defaultCallback(const char* Message) {
printf("%s", Message);
}
void init_libxpwn(int *argc, char *argv[]) {
int i, j, n = *argc;
for (i = 0; i < n; i++) {
if (!strcmp(argv[i], "--old-img3-decrypt")) {
n--;
memmove(&argv[i], &argv[i + 1], (n - i) * sizeof(char *));
Img3DecryptLast = FALSE;
}
}
argv[*argc = n] = NULL;
TestByteOrder();
GlobalLogLevel = 0xFF;
logCallback = defaultCallback;
}
void libxpwn_log(LogMessageCallback callback) {
logCallback = callback;
}
void libxpwn_loglevel(int logLevel) {
GlobalLogLevel = logLevel;
}
void Log(int level, const char* file, unsigned int line, const char* function, const char* format, ...) {
#ifdef HARD_LOG
static FILE* logFile = NULL;
if(logFile == NULL)
logFile = fopen("log.txt", "w");
#endif
char mainBuffer[1024];
char buffer[1024];
if(level >= GlobalLogLevel) {
return;
}
va_list args;
va_start(args, format);
vsnprintf(buffer, sizeof(buffer), format, args);
va_end(args);
switch(level) {
case 0:
case 1:
strcpy(mainBuffer, buffer);
break;
default:
snprintf(mainBuffer, sizeof(mainBuffer), "%s:%s:%d: %s", file, function, line, buffer);
}
logCallback(mainBuffer);
#ifdef HARD_LOG
strcat(mainBuffer, "\n");
fwrite(mainBuffer, 1, strlen(mainBuffer), logFile);
fflush(logFile);
#endif
}