| 1 | /* |
| 2 | * OpenTyrian: A modern cross-platform port of Tyrian |
| 3 | * Copyright (C) 2007-2009 The OpenTyrian Development Team |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License |
| 7 | * as published by the Free Software Foundation; either version 2 |
| 8 | * of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 | */ |
| 19 | #include "editship.h" |
| 20 | |
| 21 | #include "config.h" |
| 22 | #include "file.h" |
| 23 | #include "opentyr.h" |
| 24 | |
| 25 | #define SAS (sizeof(JE_ShipsType) - 4) |
| 26 | |
| 27 | const JE_byte extraCryptKey[10] = { 58, 23, 16, 192, 254, 82, 113, 147, 62, 99 }; |
| 28 | |
| 29 | JE_boolean extraAvail; |
| 30 | JE_ShipsType extraShips; |
| 31 | void *extraShapes; |
| 32 | JE_word extraShapeSize; |
| 33 | |
| 34 | void JE_decryptShips(void) |
| 35 | { |
| 36 | JE_boolean correct = true; |
| 37 | JE_ShipsType s2; |
| 38 | JE_byte y; |
| 39 | |
| 40 | for (int x = SAS - 1; x >= 0; x--) |
| 41 | { |
| 42 | s2[x] = extraShips[x] ^ extraCryptKey[(x + 1) % 10]; |
| 43 | if (x > 0) |
| 44 | s2[x] ^= extraShips[x - 1]; |
| 45 | } /* <= Key Decryption Test (Reversed key) */ |
| 46 | |
| 47 | y = 0; |
| 48 | for (uint x = 0; x < SAS; x++) |
| 49 | y += s2[x]; |
| 50 | if (extraShips[SAS + 0] != y) |
| 51 | correct = false; |
| 52 | |
| 53 | y = 0; |
| 54 | for (uint x = 0; x < SAS; x++) |
| 55 | y -= s2[x]; |
| 56 | if (extraShips[SAS + 1] != y) |
| 57 | correct = false; |
| 58 | |
| 59 | y = 1; |
| 60 | for (uint x = 0; x < SAS; x++) |
| 61 | y = y * s2[x] + 1; |
| 62 | if (extraShips[SAS + 2] != y) |
| 63 | correct = false; |
| 64 | |
| 65 | y = 0; |
| 66 | for (uint x = 0; x < SAS; x++) |
| 67 | y ^= s2[x]; |
| 68 | if (extraShips[SAS + 3] != y) |
| 69 | correct = false; |
| 70 | |
| 71 | if (!correct) |
| 72 | exit(255); |
| 73 | |
| 74 | memcpy(extraShips, s2, sizeof(extraShips)); |
| 75 | } |
| 76 | |
| 77 | void JE_loadExtraShapes(void) |
| 78 | { |
| 79 | FILE *f = dir_fopen(get_user_directory(), "newsh$.shp", "rb"); |
| 80 | |
| 81 | if (f) |
| 82 | { |
| 83 | extraAvail = true; |
| 84 | extraShapeSize = ftell_eof(f) - sizeof(extraShips); |
| 85 | extraShapes = malloc(extraShapeSize); |
| 86 | fread_die(extraShapes, extraShapeSize, 1, f); |
| 87 | fread_die(extraShips, sizeof(extraShips), 1, f); |
| 88 | JE_decryptShips(); |
| 89 | fclose(f); |
| 90 | } |
| 91 | } |