File: pcpatch.c

package info (click to toggle)
aranym 1.1.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 25,488 kB
  • sloc: cpp: 178,117; ansic: 170,405; perl: 5,637; sh: 5,547; asm: 2,282; makefile: 1,324; objc: 218
file content (166 lines) | stat: -rw-r--r-- 3,942 bytes parent folder | download | duplicates (10)
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
#include <stdio.h>
#include <stdlib.h>


/*

A part of Fopen/Fcreate routine... (adding #100 to the file handle for in mem data)

L2334:
        move.l  a5,4(a4)                        ; 294D0004
        clr.l   (a4)                            ; 4294
        addq    #1,10(a5)                       ; 526D000A
        moveq   #100,d0                         ; 7064
        add     d4,d0                           ; D044
L2348:
        addq    #6,a7                           ; 5C4F
        movem.l (a7)+,d3-d4/a3-a5               ; 4CDF3818
        rts                                     ; 4E75
*/

unsigned char moveq100[] = {
	0x29, 0x4d, 0x00, 0x04,
	0x42, 0x94,
	0x52, 0x6d, 0x00, 0x0a,
	0x70, 0x64,
	0xd0, 0x44,
	0x5c, 0x4f,
	0x4c, 0xdf, 0x38, 0x18,
	0x4e, 0x75,
};


/*

A parts of Fread/Fdatime/Fseek/Fclose routines... (fh #100..#140)

L2356:
        movem.l d3-d4/a2-a4,-(a7)               ; 48E71838
        move    d0,d3                           ; 3600
        movea.l a0,a2                           ; 2448
        move.l  d1,d4                           ; 2801
        cmp     #100,d0                         ; B07C0064
        blt.s   L2378                           ; 6D06
        cmp     #140,d0                         ; B07C008C
        blt.s   L2392                           ; 6D0E
L2378:
        move.l  d4,d1                           ; 2204
        movea.l a2,a0                           ; 204A
        move    d3,d0                           ; 3003
        jsr     L173498                         ; 4EB90002A5BA
        bra.s   L2456                           ; 6040

L2392:
        sub     #100,d3                         ; 967C0064
        move    d3,d0                           ; 3003
        lsl     #3,d0                           ; E748
        lea     U223702,a3                      ; 47F9000369D6
        adda    d0,a3                           ; D6C0
*/

unsigned char cmp100140[] = {
	0xb0, 0x7c, 0x00, 0x64,
	0x6d, 0x06,
	0xb0, 0x7c, 0x00, 0x8c,
};

unsigned char sub100d3[] = {
	0x96, 0x7c, 0x00, 0x64,
	0x30, 0x03,
};



FILE *fh;
unsigned char patchBuffer[2];


int getByte( unsigned char *buff ) {
	return fread( (void*)buff, 1, 1, fh ) == -1 ? -1 : 1;
}


int findPart( unsigned char *buffer, size_t len )
{
	unsigned char charBuffer[2];
	int idx = 0;
	int total = 0;

	while ( getByte( charBuffer ) > 0 ) {
		if ( idx >= len )
			return 1;

		if ( *charBuffer != buffer[idx] )
			idx = 0;
		else
			idx++;

		if ( total++ > 2000 )
			return 0;
	}

	return 0;
}


int main() {
	char *result = NULL;

	fh = fopen ("PC.PRG", "rb+");
	if ( !fh ) {
		result = "PC.PRG not found in current directory";
		goto finalize;
	}

	if ( fseek( fh, 2000, SEEK_SET ) < 0 ) {
		result = "PC.PRG cannot seek to 2000 (too short file?)";
		goto finalize;
	}

	if ( ! findPart( moveq100, sizeof( moveq100 ) / sizeof( *moveq100 ) ) ) {
		result = "findPart(moveq100) failed";
		goto finalize;
	}

	*patchBuffer = 200;
	fseek( fh, -12, SEEK_CUR );
	fwrite( patchBuffer, 1, 1, fh );

	while ( 1 ) {
		if ( ! findPart( cmp100140, sizeof( cmp100140 ) / sizeof( *cmp100140 ) ) ) {
			result = "findPart(cmp100140) failed";
			break;
		}

		*patchBuffer = 200;
		fseek( fh, -8, SEEK_CUR );
		fwrite( patchBuffer, 1, 1, fh );

		*patchBuffer = 240;
		fseek( fh, 5, SEEK_CUR );
		fwrite( patchBuffer, 1, 1, fh );

		if ( ! findPart( sub100d3, sizeof( sub100d3 ) / sizeof( *sub100d3 ) ) ) {
			result = "findPart(sub100d3) failed";
			break;
		}

		*patchBuffer = 200;
		fseek( fh, -4, SEEK_CUR );
		fwrite( patchBuffer, 1, 1, fh );
	}

finalize:
	fclose( fh );

	if ( result == NULL ) {
		printf( "PureC was patched successfully.\n" );
	} else {
		printf( "Error patching PureC: %s\n", result );
	}

	puts("Press [Return] to quit.");
	getchar();

	return 0;
}