aboutsummaryrefslogtreecommitdiff
path: root/tools/create_teenagent/create_teenagent.cpp
blob: 60b291982181f5cf0c56c1ebab75b84f50e455c4 (plain)
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
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "md5.h"

static void print_hex(FILE * f, const uint8 * data, size_t len) {
	for (size_t i = 0; i < len; ++i) {
		fprintf(f, "%02x", data[i]);
	}
}

static void extract(FILE * fout, FILE *fin, size_t pos, size_t size, const char *what) {
	char buf[0x10000];
	assert(size < sizeof(buf));

	if (fseek(fin, pos, SEEK_SET) != 0) {
		perror(what);
		exit(1);
	}
	
	if (fread(buf, size, 1, fin) != 1) {
		perror(what);
		exit(1);
	}
	
	if (fwrite(buf, size, 1, fout) != 1) {
		perror(what);
		exit(1);
	}
}

int main(int argc, char *argv[]) {
	if (argc < 2) {
		fprintf(stderr, "usage: %s: Teenagnt.exe (unpacked one)\n", argv[0]);
		exit(1);
	}
	const char * fname = argv[1];
	
	uint8 digest[16];
	if (!md5_file(fname, digest, 0)) {
		fprintf(stderr, "cannot calculate md5 for %s", fname);
		exit(1);
	}
	
	const uint8 ethalon[16] = { 
		0x51, 0xb6, 0xd6, 0x47, 0x21, 0xf7, 0xc4, 0xb4, 
		0x98, 0xbf, 0xc0, 0xf3, 0x23, 0x01, 0x3e, 0x36, 
	};
	
	if (memcmp(digest, ethalon, 16) != 0) {
		fprintf(stderr, "cannot extract data, your md5: ");
		print_hex(stderr, digest, 16);
		fprintf(stderr, ", need md5: ");
		print_hex(stderr, ethalon, 16);
		fprintf(stderr, ", sorry\n");
		exit(1);
	}
	FILE *fin = fopen(fname, "rb");
	if (fin == NULL) {
		perror("opening input file");
		exit(1);
	}
	
	const char * dat_name = "teenagent.dat";
	FILE *fout = fopen(dat_name, "wb");
	if (fout == NULL) {
		perror("opening output file");
		exit(1);
	}
	//0x0200, 0xb5b0, 0x1c890
	extract(fout, fin, 0x00200, 0xb3b0, "extracting code segment");
	extract(fout, fin, 0x0b5b0, 0xe790, "extracting data segment");
	extract(fout, fin, 0x1c890, 0x8be2, "extracting second data segment");
	
	fclose(fin);
	fclose(fout);
	
	fprintf(stderr, "please run \"gzip -n %s\"\n", dat_name);
	
	return 0;
}