aboutsummaryrefslogtreecommitdiff
path: root/engines/glk/magnetic/myth.cpp
blob: 3f8127e6137fb2ac3f1d1600fff9b04408c317bb (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
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
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

/*
 * Patches Myth C64 disk image
 * Written by David Kinder, based on code by Niclas Karlsson
 */

#define D64_SIZE (174848L)
unsigned char image[D64_SIZE];

void ungarble(unsigned char *block, signed long key) {
	unsigned char d;
	int i, j;

	d = (unsigned char)((key & 0x07) ^ 0xFF);
	if (d < 0xFF) {
		i = d;
		j = d + 1;
		while (j < 0x100)
			block[j++] ^= block[i];
	}

	i = 0xFF;
	j = d - 1;
	while (j >= 0)
		block[j--] ^= block[i--];

	for (i = 0; i < 128; i++) {
		d = block[i];
		block[i] = block[255 - i];
		block[255 - i] = d;
	}
}

void garble(unsigned char *block, int i1, int j1, int i2) {
	unsigned char d;
	int i, j;

	for (i = 0; i < 128; i++) {
		d = block[i];
		block[i] = block[255 - i];
		block[255 - i] = d;
	}

	i = i1;
	j = -1;
	while (j < j1)
		block[++j] ^= block[++i];

	i = i2;
	j = 0x100;
	while (j > i2 + 1)
		block[--j] ^= block[i];
}

/* This routine does the patching. */
void fixDiskImage(int code) {
	unsigned char *block = NULL;

	switch (code) {
	case 0:
		/* code[0x3056] = 0x60 */
		block = image + 0x1600;
		ungarble(block, 0x27L);
		block[0x56] = 0x60;
		garble(block, 0x07, 0xF7, 0xF8);
		break;
	case 1:
		/* code[0x3148] = 0x60 */
		/* code[0x314D] = 0x11 */
		block = image + 0x1700;
		ungarble(block, 0x28L);
		block[0x48] = 0x60;
		block[0x4D] = 0x11;
		garble(block, 0x00, 0xFE, 0xFF);
		break;
	}
}

void readDiskImage(const char *filename) {
	FILE *file = fopen(filename, "r+b");
	if (file == NULL)
		exit(1);
	fread(image, 1, D64_SIZE, file);
}

void writeDiskImage(const char *filename) {
	FILE *file = fopen(filename, "w+b");
	if (file == NULL)
		exit(1);
	fwrite(image, 1, D64_SIZE, file);
}

int main(int argc, char **argv) {
	if (argc == 4) {
		int code = -1;
		if (sscanf(argv[3], "%d", &code) == 1) {
			if ((code >= 0) && (code <= 1)) {
				readDiskImage(argv[1]);
				fixDiskImage(code);
				writeDiskImage(argv[2]);
				printf("Myth patched.\n");
				return 0;
			}
		}
	}

	printf("Removes password protection from Magnetic Scrolls' Myth.\n");
	printf("Use: myth input.d64 output.d64 code\n");
	printf("If code is 0, the password protection is completely removed.\n");
	printf("If code is 1, any user name and password will be accepted.\n");
	return 0;
}