This software and its source code (if available) are licensed under the Apache-2.0 license (unless otherwise specified) which DOES NOT APPLY to any third-party code (such as zlib for example) and borrowed Assembler code (ripped code from other software executable files).
#include <io.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <stdint.h>
#define MAGIC_RS20 0x00325352
#define MAGIC_FAOE 0x454F4146
/* hardcoded names in executable file (case sensitive!) */
static char mm2_hardcoded[] =
".env\0" /* this one not actual file name */
"Audio.res\0"
"Bike.res\0"
"ColSeg.res\0"
"Eco.res\0"
"Engine.res\0"
"Fonts.res\0"
"Global.res\0"
"Models.res\0"
"ModelTGA.res\0"
"Objects.res\0"
"ProcVUE.res\0"
"Skies.res\0"
"uires.res\0";
char strcasecmp(char *a, char *b) {
char x, y;
do {
x = *a;
y = *b;
x += ((x >= 'A') && (x <= 'Z')) ? ('a' - 'A') : 0;
y += ((y >= 'A') && (y <= 'Z')) ? ('a' - 'A') : 0;
a++;
b++;
} while (x && y && (x == y));
return(x - y);
}
uint8_t mm2_find_key(char *s) {
uint8_t k, *p;
uint32_t i;
char *h;
/* name to uppercase (mostly for .env files) */
for (i = 0; s[i]; i++) {
s[i] -= ((s[i] >= 'a') && (s[i] <= 'z')) ? ('a' - 'A') : 0;
}
h = mm2_hardcoded;
/* .env file extension MUST be in lowercase! */
i = strlen(s);
if ((i >= 4) && (!strcasecmp(&s[i - 4], h))) {
strcpy(&s[i - 4], h);
}
/* find name */
while (*h) {
if (!strcasecmp(s, h)) {
s = h;
break;
}
h += strlen(h) + 1;
}
/* calculate name hash key */
p = (uint8_t *) s;
k = 0xFA;
while (*p) {
k += *p ^ (k + 3);
p++;
}
return(k);
}
void mm2_encrypt(uint8_t *p, uint32_t l, uint8_t k) {
while (l--) {
*p ^= k;
k += *p;
p++;
}
}
void mm2_decrypt(uint8_t *p, uint32_t l, uint8_t k) {
uint8_t b;
while (l--) {
b = *p ^ k;
k += *p;
*p = b;
p++;
}
}
uint32_t mm2_head_size(uint8_t *p, uint32_t l) {
uint32_t i, sz;
sz = 0;
if ((l >= 8) && (*((uint32_t *) &p[sz]) == MAGIC_RS20)) {
/* magic */
sz = 4;
/* file count */
i = *((uint32_t *) &p[sz]);
sz += 4;
while ((sz < l) && (i)) {
/* nlen name[nlen] offs size */
sz += 4 + (*((uint32_t *) &p[sz])) + 4 + 4;
i--;
}
sz = (sz < l) ? sz : l;
}
return(sz);
}
char *basename(char *s) {
char *r;
if (s) {
for (r = s; *r; r++) {
if ((*r == '/') || (*r == '\\')) {
s = &r[1];
}
}
}
return(s);
}
void readdata(FILE *fl, void *data, uint32_t size) {
if (data && size) {
memset(data, 0, size);
if (fl) {
fread(data, size, 1, fl);
}
}
}
int main(int argc, char *argv[]) {
uint32_t i, j, k, sz;
uint8_t *p, key;
FILE *fl;
printf(
"Motocross Madness 2 .ENV/.RES FAOE<->RS2 (de)crypter v1.0\n"
"(c) CTPAX-X Team 2024\n"
"http://www.CTPAX-X.org/\n\n"
);
if (argc != 3) {
printf(
"Usage: mm2crypt <input.ext> <output.ext>\n\n"
"/!\\ WARNING /!\\\n"
"File name and case sensitive for encrypted input and encrypted output files!\n\n"
);
return(1);
}
fl = fopen(argv[1], "rb");
if (!fl) {
printf("Error: can't open input file.\n\n");
return(2);
}
/* read magic */
readdata(fl, &i, 4);
/* get file size */
fseek(fl, 0, SEEK_END);
sz = ftell(fl);
if (
((i != MAGIC_FAOE) && (i != MAGIC_RS20)) ||
((sz < 8) && (i == MAGIC_RS20)) ||
((sz < 12) && (i == MAGIC_FAOE))
) {
fclose(fl);
printf("Error: invalid input file format.\n\n");
return(3);
}
i = (i == MAGIC_RS20) ? 1 : 0;
if (i) {
fseek(fl, 0, SEEK_SET);
key = mm2_find_key(basename(argv[2]));
} else {
fseek(fl, 4, SEEK_SET);
key = mm2_find_key(basename(argv[1]));
sz -= 4;
}
/* crypt back */
p = (uint8_t *) malloc(sz + 4);
if (!p) {
fclose(fl);
printf("Error: not enough memory for input file.\n\n");
return(4);
}
memset(p, 0, sz);
fread(&p[4], sz, 1, fl);
fclose(fl);
/* (de)crypt */
if (i) {
printf("Crypting file...");
*((uint32_t *) p) = MAGIC_FAOE;
/* encrypt each file data */
j = 4 + 4;
/* files count */
k = *((uint32_t *) &p[j]);
j += 4;
while (k--) {
j += 4 + (*((uint32_t *) &p[j]));
/* encrypt file data */
mm2_encrypt(
&p[4 + (*((uint32_t *) &p[j]))],
*((uint32_t *) &p[j + 4]),
key
);
j += 4 + 4;
}
/* get header size */
k = mm2_head_size(&p[4], sz);
/* encrypt header part only */
mm2_encrypt(&p[4], k, key);
} else {
printf("Decrypting file...\n");
/* check key */
k = *((uint32_t *) &p[4]);
mm2_decrypt((uint8_t *) &k, 4, key);
if (k != MAGIC_RS20) {
/* somehow the key still invalid - try to bruteforce it */
for (j = 0; j < 256; j++) {
k = *((uint32_t *) &p[4]);
mm2_decrypt((uint8_t *) &k, 4, j);
/* found right key */
if (k == MAGIC_RS20) {
key = j;
break;
}
}
}
/* decrypt whole file */
mm2_decrypt(&p[4], sz, key);
/* key resets for header and each file - figure out header size */
k = mm2_head_size(&p[4], sz);
/* crypt whole file back */
mm2_encrypt(&p[4], sz, key);
/* decrypt only header part */
mm2_decrypt(&p[4], k, key);
/* decrypt each file data */
j = 4 + 4;
/* files count */
k = *((uint32_t *) &p[j]);
j += 4;
while (k--) {
j += 4 + (*((uint32_t *) &p[j]));
/* decrypt file data */
mm2_decrypt(
&p[4 + (*((uint32_t *) &p[j]))],
*((uint32_t *) &p[j + 4]),
key
);
j += 4 + 4;
}
}
printf("ok\n\n");
/* save to disk */
fl = fopen(argv[2], "wb");
if (fl) {
fclose(fl);
/* delete if file already exists (force correct name) */
unlink(argv[2]);
}
fl = fopen(argv[2], "wb");
if (fl) {
printf("%s => %s\n", argv[1], argv[2]);
fwrite(&p[i ? 0 : 4], sz + (i ? 4 : 0), 1, fl);
fclose(fl);
printf("\ndone\n\n");
i = 0;
} else {
printf("Error: can't create output file.\n");
i = 5;
}
/* cleanup */
free(p);
return(i);
}