Initial revision
[ssr] / StraySrc / Libraries / Steel / c / crc
1 /*
2 * crc
3 * Checks CRC values for resource files
4 *
5 * v. 1.00 (24 August 1993)
6 *
7 * © 1993-1998 Straylight
8 */
9
10 /*----- Licensing note ----------------------------------------------------*
11 *
12 * This file is part of Straylight's Steel library.
13 *
14 * Steel is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2, or (at your option)
17 * any later version.
18 *
19 * Steel is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with Steel. If not, write to the Free Software Foundation,
26 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 */
28
29 #include "crc.h"
30 #include "crc32.h"
31 #include "os.h"
32 #include "res.h"
33 #include "msgs.h"
34 #include "werr.h"
35 #include "wimpt.h"
36 #include <stdio.h>
37 #include <stdlib.h>
38
39 /*
40 * long crc__doCRC(char *filename,int offset)
41 *
42 * Use
43 * Returns the CRC value for a file starting from a certain offset.
44 *
45 * Parameters
46 * char *filename == the resource filename to use
47 * int offset == the offset to start from
48 *
49 * Returns
50 * A 4-byte CRC value.
51 */
52
53 static long crc__doCRC(char *filename,int offset)
54 {
55 FILE *fp;
56 char buffer[1024];
57 int read;
58 long c=0;
59 fp=res_openfile(filename,"rb");
60 if (!fp)
61 {
62 werr(FALSE,msgs_lookup("crcFNF:Couldn't open file '%s'."),filename);
63 exit(0);
64 }
65 fseek(fp,offset,SEEK_SET);
66 do
67 {
68 read=fread(buffer,1,1024,fp);
69 c=crc32(c,buffer,read,1);
70 }
71 while (read==1024);
72 fclose(fp);
73 return (c);
74 }
75
76 /*
77 * long crc(char *filename)
78 *
79 * Use
80 * Calculates the CRC value for the file specified. Uses the same method
81 * as WindowEd etc.
82 *
83 * Parameters
84 * char *filename == the filename of the file to check (leaf only - it's
85 * passed through res_findname()). Note that CRC-checking the
86 * '!RunImage' file is rather silly.
87 *
88 * Returns
89 * A 4-byte CRC value
90 */
91
92 long crc(char *filename)
93 {
94 return (crc__doCRC(filename,0));
95 }
96
97 /*
98 * void crc_check(char *filename,int check)
99 *
100 * Use
101 * Checks a CRC value for a file and reports a fatal error if the check
102 * fails.
103 *
104 * Parameters
105 * char *filename == the leafname of the file
106 * long check == the CRC number to check with
107 */
108
109 void crc_check(char *filename,long check)
110 {
111 if (check==-1)
112 return;
113 if (crc__doCRC(filename,0)!=check)
114 {
115 werr
116 (
117 FALSE,
118 msgs_lookup("crcFC:%s resource file '%s' has been corrupted. "
119 "Please restore this file from the distribution disk."),
120 wimpt_programname(),
121 filename
122 );
123 exit(0);
124 }
125 }
126
127 /*
128 * void crc_checkRunImage(void)
129 *
130 * Use
131 * Corruption-checks the main !RunImage file. It must have been passed
132 * through CodeScr first, to install the CRC into the code.
133 */
134
135 void crc_checkRunImage(void)
136 {
137 FILE *fp=res_openfile("!RunImage","r");
138 long c;
139 if (!fp)
140 werr(TRUE,msgs_lookup("crcFNF:Couldn't open file '%s'."),"!RunImage");
141 fseek(fp,8,SEEK_SET);
142 fread(&c,4,1,fp);
143 fclose(fp);
144 if (c!=crc__doCRC("!RunImage",12))
145 {
146 werr
147 (
148 FALSE,
149 msgs_lookup("crcFC:%s resource file '%s' has been corrupted. "
150 "Please restore this file from the distribution disk."),
151 wimpt_programname(),
152 "!RunImage"
153 );
154 exit(0);
155 }
156 }