File: prowiz.c

package info (click to toggle)
xmp 3.4.0-1.1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 4,212 kB
  • sloc: ansic: 47,206; sh: 2,933; asm: 1,013; makefile: 397; xml: 47; cpp: 40
file content (249 lines) | stat: -rw-r--r-- 4,870 bytes parent folder | download
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
 * Pro-Wizard_1.c
 *
 * Copyright (C) 1997-1999 Sylvain "Asle" Chipaux
 * Copyright (C) 2006-2007 Claudio Matsuoka
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "xmp.h"

#include "prowiz.h"

void register_format (char *, char *);

static int check (unsigned char *, int);

static LIST_HEAD(pw_format_list);


int pw_enable(char *id, int enable)
{
	struct list_head *tmp;
	struct pw_format *format;

	list_for_each(tmp, &pw_format_list) {
		format = list_entry(tmp, struct pw_format, list);
		if (!strcmp(id, format->id)) {
			format->enable = enable;
			return 0;
		}
	}

	return 1;
}

int pw_register(struct pw_format *f)
{
	f->enable = 1;
	list_add_tail(&f->list, &pw_format_list);
	register_format(f->id, f->name);
	return 0;
}

int pw_unregister(struct pw_format *f)
{
	list_del(&f->list);
	return 0;
}

int pw_move_data(FILE *out, FILE *in, int len)
{
	uint8 buf[1024];
	int l;

	do {
		l = fread(buf, 1, len > 1024 ? 1024 : len, in);
		fwrite(buf, 1, l, out);
		len -= l;
	} while (l > 0 && len > 0);

	return 0;
}

int pw_write_zero(FILE *out, int len)
{
	uint8 buf[1024];
	int l;
	
	do {
		l = len > 1024 ? 1024 : len;
		memset(buf, 0, l);
		fwrite(buf, 1, l, out);
		len -= l;
	} while (l > 0 && len > 0);

	return 0;
}

int pw_init()
{
	/* With signature */
	pw_register(&pw_ac1d);
	/* pw_register (&pw_emod); */
	pw_register(&pw_fchs);
	pw_register(&pw_fcm);
	pw_register(&pw_fuzz);
	pw_register(&pw_hrt);
	pw_register(&pw_kris);
	pw_register(&pw_ksm);
	pw_register(&pw_mp_id);
	pw_register(&pw_ntp);
	pw_register(&pw_p18a);
	pw_register(&pw_p10c);
	pw_register(&pw_pru1);
	pw_register(&pw_pru2);
	pw_register(&pw_pha);
	pw_register(&pw_wn);
	pw_register(&pw_unic_id);
	pw_register(&pw_tp3);
	pw_register(&pw_skyt);

	/* No signature */
	pw_register(&pw_xann);
	pw_register(&pw_mp_noid);	/* Must check before Heatseeker */
	pw_register(&pw_di);
	pw_register(&pw_eu);
	pw_register(&pw_p4x);
	pw_register(&pw_pp21);
	pw_register(&pw_p50a);
	pw_register(&pw_p60a);
	pw_register(&pw_p61a);
	pw_register(&pw_nru);
	pw_register(&pw_np2);
	pw_register(&pw_np1);
	pw_register(&pw_np3);
	pw_register(&pw_zen);
	pw_register(&pw_unic_emptyid);
	pw_register(&pw_unic_noid);
	pw_register(&pw_unic2);
	pw_register(&pw_crb);
	pw_register(&pw_tdd);
	pw_register(&pw_starpack);
	pw_register(&pw_gmc);
	pw_register(&pw_titanics);

	return 0;
}

struct list_head *checked_format = &pw_format_list;

int pw_wizardry(int in, int out, struct pw_format **fmt)
{
	struct list_head *tmp;
	struct pw_format *format;
	struct stat st;
	int size = -1, in_size;
	uint8 *data;
	FILE *file_in, *file_out;

	file_in = fdopen(dup(in), "rb");
	if (file_in == NULL)
		return -1;

	file_out = fdopen(dup(out), "w+b");

	if (fstat(fileno(file_in), &st) < 0)
		in_size = -1;
	else
		in_size = st.st_size;

	/* printf ("input file size : %d\n", in_size); */
	if (in_size < MIN_FILE_LENGHT)
		return -2;

	/* alloc mem */
	data = (uint8 *)malloc (in_size + 4096);	/* slack added */
	if (data == NULL) {
		perror("Couldn't allocate memory");
		return -1;
	}
	fread(data, in_size, 1, file_in);


  /********************************************************************/
  /**************************   SEARCH   ******************************/
  /********************************************************************/

	if (checked_format != &pw_format_list)
		goto checked;

	list_for_each(tmp, &pw_format_list) {
		format = list_entry(tmp, struct pw_format, list);
		_D("checking format: %s", format->name);
		if (format->test(data, in_size) >= 0)
			goto done;
	}
	return -1;

checked:
	format = list_entry(checked_format, struct pw_format, list);
	_D(_D_WARN "checked format: %s", format->name);
	checked_format = &pw_format_list;

done:
	fseek(file_in, 0, SEEK_SET);
	size = -1;	/* paranoia setting */
	if (format->depack) 
		size = format->depack(file_in, file_out);

	if (size < 0)
		return -1;

	//pw_crap(format, file_out);
	fclose(file_out);
	fclose(file_in);

	/*
	 * ADD: Rip based on size
	 */

	free(data);

	if (fmt)
		*fmt = format;

	return 0;
}

static struct list_head *shortcut = &pw_format_list;
static int check(unsigned char *b, int s)
{
	struct list_head *tmp;
	struct pw_format *format;
	int extra;

	list_for_each(tmp, shortcut) {
		if (tmp == &pw_format_list)
			break;
		format = list_entry(tmp, struct pw_format, list);
		_D("checking format [%d]: %s", s, format->name);
		if ((extra = format->test (b, s)) > 0) {
			_D("format: %s, extra: %d", format->id, extra);
			shortcut = tmp->prev;
			return extra;
		}
		if (extra == 0) {
			_D("format ok: %s", format->id);
			checked_format = tmp;
			shortcut = &pw_format_list;
			return 0;
		}
	}

	shortcut = &pw_format_list;
	return -1;
}

int pw_check(unsigned char *b, int s)
{
	return check(b, s);
}