~ubuntu-branches/debian/stretch/opentyrian/stretch

« back to all changes in this revision

Viewing changes to src/editship.c

  • Committer: Package Import Robot
  • Author(s): Etienne Millon
  • Date: 2015-03-31 08:48:54 UTC
  • Revision ID: package-import@ubuntu.com-20150331084854-f5a4uoz7uv3vopk6
Tags: upstream-2.1.20130907+dfsg
ImportĀ upstreamĀ versionĀ 2.1.20130907+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 "config.h"
 
20
#include "editship.h"
 
21
#include "file.h"
 
22
#include "opentyr.h"
 
23
 
 
24
#define SAS (sizeof(JE_ShipsType) - 4)
 
25
 
 
26
const JE_byte extraCryptKey[10] = { 58, 23, 16, 192, 254, 82, 113, 147, 62, 99 };
 
27
 
 
28
JE_boolean extraAvail;
 
29
JE_ShipsType extraShips;
 
30
void *extraShapes;
 
31
JE_word extraShapeSize;
 
32
 
 
33
void JE_decryptShips( void )
 
34
{
 
35
        JE_boolean correct = true;
 
36
        JE_ShipsType s2;
 
37
        JE_byte y;
 
38
        
 
39
        for (int x = SAS - 1; x >= 0; x--)
 
40
        {
 
41
                s2[x] = extraShips[x] ^ extraCryptKey[(x + 1) % 10];
 
42
                if (x > 0)
 
43
                        s2[x] ^= extraShips[x - 1];
 
44
        }  /*  <= Key Decryption Test (Reversed key) */
 
45
        
 
46
        y = 0;
 
47
        for (uint x = 0; x < SAS; x++)
 
48
                y += s2[x];
 
49
        if (extraShips[SAS + 0] != y)
 
50
                correct = false;
 
51
        
 
52
        y = 0;
 
53
        for (uint x = 0; x < SAS; x++)
 
54
                y -= s2[x];
 
55
        if (extraShips[SAS + 1] != y)
 
56
                correct = false;
 
57
        
 
58
        y = 1;
 
59
        for (uint x = 0; x < SAS; x++)
 
60
                y = y * s2[x] + 1;
 
61
        if (extraShips[SAS + 2] != y)
 
62
                correct = false;
 
63
        
 
64
        y = 0;
 
65
        for (uint x = 0; x < SAS; x++)
 
66
                y ^= s2[x];
 
67
        if (extraShips[SAS + 3] != y)
 
68
                correct = false;
 
69
        
 
70
        if (!correct)
 
71
                exit(255);
 
72
        
 
73
        memcpy(extraShips, s2, sizeof(extraShips));
 
74
}
 
75
 
 
76
void JE_loadExtraShapes( void )
 
77
{
 
78
        FILE *f = dir_fopen(get_user_directory(), "newsh$.shp", "rb");
 
79
        
 
80
        if (f)
 
81
        {
 
82
                extraAvail = true;
 
83
                extraShapeSize = ftell_eof(f) - sizeof(extraShips);
 
84
                extraShapes = malloc(extraShapeSize);
 
85
                efread(extraShapes, extraShapeSize, 1, f);
 
86
                efread(extraShips, sizeof(extraShips), 1, f);
 
87
                JE_decryptShips();
 
88
                fclose(f);
 
89
        }
 
90
}
 
91