- /* filter crap for v5 */
-
- 0,0,2,2, 4,4,118,118, 8,9,10,11, 12,13,14,15,
- 16,16,18,18, 20,21,22,23, 24,25,26,27, 28,28,118,28,
- 0,16,2,18, 36,37,37,37, 147,41,42,43, 44,45,128,128,
- 128,148,148,148, 45,45,45,148, 0,57,58,59, 60,61,62,63,
-
- 64,65,66,67, 68,153,153,153, 153,153,153,153, 153,153,153,153,
- 153,153,153,153, 153,153,153,153, 153,153,153,153, 153,153,153,153,
- 153,153,153,153, 153,68,68,68,68, 68,68,68,68,118, 118,118,
- 118,118,114,115, 131,118,118,119, 120,121,122,118, 118,118,118,118,
-
- 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,153,153,153, 153,153,153,118, 168,169,170,171, 172,173,174,175,
- 176,177,178,179, 180,181,182,183, 184,185,186,187, 188,189,68,153,
-
- 153,153,153,153, 153,153,153,153, 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,153,153, 153,153,153,153, 153,153,153,153, 153,153,153,153
-};
-
-static unsigned char remap_v4[256] =
-{
- /* filter crap for v4 */
-
- 0,0,2,2, 4,4,118,118, 8,9,10,11, 12,13,14,15,
- 16,16,18,18, 20,21,22,23, 24,25,26,27, 28,28,118,28,
- 0,16,2,18, 36,37,37,37, 147,41,42,43, 44,45,128,128,
- 128,148,148,148, 45,45,45,148, 0,153,153,59, 60,61,62,63,
-
- 64,65,66,153, 153,153,153,153, 153,153,153,153, 153,153,153,153,
- 153,153,153,153, 153,153,153,153, 153,153,153,153, 153,153,153,153,
- 153,153,153,153, 153,153,153,153, 153,153,153,153, 153,153,153,153,
- 153,118,114,115, 131,118,118,119, 120,121,122,118, 118,118,118,118,
-
- 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,68,154,155, 156,157,158,160,
- 160,160,160,160, 160,160,160,160, 160,160,160,160, 160,160,160,175,
- 153,153,153,153, 153,153,153,153, 153,153,153,153, 153,153,68,153,
-
- 153,153,153,153, 153,153,153,153, 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,153,153, 153,153,153,153, 153,153,153,153, 153,153,153,153
-};
-
-static unsigned char remap_v4eater[28] =
-{
- /* filter crap for v4 */
-
- 128,18,2,0,4,8,16,20,28,37,
- 41,45,130,129,131,132,133,134,135,136,
- 146,147,175,65,66,64,2,18
-};
-
-static boolean filename_has_v1_format(char *filename)
-{
- char *basename = getBaseNamePtr(filename);
-
- return (strlen(basename) == 3 &&
- basename[0] == 'a' &&
- basename[1] >= 'a' && basename[1] <= 'k' &&
- basename[2] >= '0' && basename[2] <= '9');
-}
-
-int cleanup_em_level(unsigned char *src, int length, char *filename)
-{
- int file_version = FILE_VERSION_EM_UNKNOWN;
- int i;
-
- if (length >= 2172 &&
- src[2106] == 255 && /* version id: */
- src[2107] == 54 && /* '6' */
- src[2108] == 48 && /* '0' */
- src[2109] == 48) /* '0' */
- {
- /* ---------- this cave has V6 file format ---------- */
- file_version = FILE_VERSION_EM_V6;
-
- /* remap elements to internal EMC level format */
- for (i = 0; i < 2048; i++)
- src[i] = remap_v6[src[i]];
- for (i = 2048; i < 2084; i++)
- src[i] = remap_v6[src[i]];
- for (i = 2112; i < 2148; i++)
- src[i] = remap_v6[src[i]];
- }
- else if (length >= 2110 &&
- src[2106] == 255 && /* version id: */
- src[2107] == 53 && /* '5' */
- src[2108] == 48 && /* '0' */
- src[2109] == 48) /* '0' */
- {
- /* ---------- this cave has V5 file format ---------- */
- file_version = FILE_VERSION_EM_V5;
-
- /* remap elements to internal EMC level format */
- for (i = 0; i < 2048; i++)
- src[i] = remap_v5[src[i]];
- for (i = 2048; i < 2084; i++)
- src[i] = remap_v5[src[i]];
- for (i = 2112; i < 2148; i++)
- src[i] = src[i - 64];
- }
- else if (length >= 2106 &&
- (src[1983] == 27 || /* encrypted (only EM I/II/III) */
- src[1983] == 116 || /* unencrypted (usual case) */
- src[1983] == 131)) /* unencrypted (rare case) */
- {
- /* ---------- this cave has V1, V2 or V3 file format ---------- */
-
- boolean fix_copyright = FALSE;
-
- /*
- byte at position 1983 (0x07bf) is used as "magic byte":
- - 27 (0x1b) => encrypted level (V3 only / Kingsoft original games)
- - 116 (0x74) => unencrypted level (byte is corrected to 131 (0x83))
- - 131 (0x83) => unencrypted level (happens only in very rare cases)
- */
-
- if (src[1983] == 27) /* (0x1b) -- after decryption: 116 (0x74) */
- {
- /* this is original (encrypted) Emerald Mine I, II or III level file */
-
- int first_byte = src[0];
- unsigned char code0 = 0x65;
- unsigned char code1 = 0x11;
-
- /* decode encrypted level data */
- for (i = 0; i < 2106; i++)
- {
- src[i] ^= code0;
- src[i] -= code1;
-
- code0 = (code0 + 7) & 0xff;
- }
-
- src[1] = 131; /* needed for all Emerald Mine levels */
-
- /* first byte is either 0xf1 (EM I and III) or 0xf5 (EM II) */
- if (first_byte == 0xf5)
- {
- src[0] = 131; /* only needed for Emerald Mine II levels */
-
- fix_copyright = TRUE;
- }
-
- /* ---------- this cave has V3 file format ---------- */
- file_version = FILE_VERSION_EM_V3;
- }
- else if (filename_has_v1_format(filename))
- {
- /* ---------- this cave has V1 file format ---------- */
- file_version = FILE_VERSION_EM_V1;
- }
- else
- {
- /* ---------- this cave has V2 file format ---------- */
- file_version = FILE_VERSION_EM_V2;
- }
-
- /* remap elements to internal EMC level format */
- for (i = 0; i < 2048; i++)
- src[i] = remap_v4[src[i]];
- for (i = 2048; i < 2084; i++)
- src[i] = remap_v4eater[src[i] >= 28 ? 0 : src[i]];
- for (i = 2112; i < 2148; i++)
- src[i] = src[i - 64];
-
- if (fix_copyright) /* fix "(c)" sign in Emerald Mine II levels */
- {
- for (i = 0; i < 2048; i++)
- if (src[i] == 241)
- src[i] = 254; /* replace 'Xdecor_1' with 'Xalpha_copyr' */
- }
- }
- else
- {
- /* ---------- this cave has unknown file format ---------- */
-
- /* if file has length of old-style level file, print (wrong) magic byte */
- if (length < 2110)
- Error(ERR_WARN, "unknown magic byte 0x%02x at position 0x%04x",
- src[1983], 1983);
-
- return FILE_VERSION_EM_UNKNOWN;
- }
-
- if (file_version < FILE_VERSION_EM_V6)