-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsaveutils.c
120 lines (98 loc) · 2.84 KB
/
saveutils.c
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
/* =============================================================================
* PROGRAM: ularn
* FILENAME: saveutils.c
*
* DESCRIPTION:
* This module contains utilities used in loading and saving games.
*
* =============================================================================
* EXPORTED VARIABLES
*
* FileSum : The current checksum for the file being written/read.
*
* =============================================================================
* EXPORTED FUNCTIONS
*
* sum : Checksum calculation function
* bwrite : Binary write with checksum update
* bread : Binary read with checksum update.
*
* =============================================================================
*/
#include <errno.h>
#include <stdio.h>
#include "saveutils.h"
#include "scores.h"
#include "ularn_win.h"
/* =============================================================================
* Local variables
*/
/* The number of characters written */
static int w = 0;
/* The number of characters read */
static int r = 0;
/* =============================================================================
* Exported variables
*/
int FileSum;
/* =============================================================================
* Exported functions
*/
/* =============================================================================
* FUNCTION: sum
*/
unsigned int sum(unsigned char *data, int n) {
unsigned int sum;
int c, nb;
sum = nb = 0;
while (nb++ < n) {
c = *data++;
if (sum & 01)
sum = (sum >> 1) + 0x8000;
else
sum >>= 1;
sum += c;
sum &= 0xFFFF;
}
return sum;
}
/* =============================================================================
* FUNCTION: bwrite
*/
void bwrite(FILE *fp, char *buf, long num) {
int nwrote;
static int ncalls = 0;
ncalls++;
nwrote = fwrite(buf, 1, num, fp);
w += nwrote;
if (nwrote != num) {
Printf("Error writing to save file\n");
Printf("wrote %d, wanted %d\n", nwrote, num);
// Printf("errno = %d\t[%s]\n", errno, sys_errlist[errno]);
Printf(" Wrote %d bytes so far\n", w);
Printf(" Call: %d\n", ncalls);
nap(4000);
died(DIED_POST_MORTEM_DEATH, 0);
}
FileSum += sum((unsigned char *)buf, num);
}
/* =============================================================================
* FUNCTION: bread
*/
void bread(FILE *fp, char *buf, long num) {
int nread;
static int ncalls = 0;
ncalls++;
nread = fread(buf, 1, num, fp);
r += nread;
if (nread != num) {
Printf("Error reading from save file\n");
Printf(" Got %d, wanted %d bytes\n", nread, num);
// Printf("errno = %d\t[%s]\n",errno,sys_errlist[errno]);
Printf(" Read %d bytes so far\n", w);
Printf(" Call: %d\n", ncalls);
nap(4000);
died(DIED_POST_MORTEM_DEATH, 0);
}
FileSum += sum((unsigned char *)buf, num);
}