/* * crc * Checks CRC values for resource files * * v. 1.00 (24 August 1993) * * © 1993-1998 Straylight */ /*----- Licensing note ----------------------------------------------------* * * This file is part of Straylight's Steel library. * * Steel is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * * Steel is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Steel. If not, write to the Free Software Foundation, * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include "crc.h" #include "crc32.h" #include "os.h" #include "res.h" #include "msgs.h" #include "werr.h" #include "wimpt.h" #include #include /* * long crc__doCRC(char *filename,int offset) * * Use * Returns the CRC value for a file starting from a certain offset. * * Parameters * char *filename == the resource filename to use * int offset == the offset to start from * * Returns * A 4-byte CRC value. */ static long crc__doCRC(char *filename,int offset) { FILE *fp; char buffer[1024]; int read; long c=0; fp=res_openfile(filename,"rb"); if (!fp) { werr(FALSE,msgs_lookup("crcFNF:Couldn't open file '%s'."),filename); exit(0); } fseek(fp,offset,SEEK_SET); do { read=fread(buffer,1,1024,fp); c=crc32(c,buffer,read,1); } while (read==1024); fclose(fp); return (c); } /* * long crc(char *filename) * * Use * Calculates the CRC value for the file specified. Uses the same method * as WindowEd etc. * * Parameters * char *filename == the filename of the file to check (leaf only - it's * passed through res_findname()). Note that CRC-checking the * '!RunImage' file is rather silly. * * Returns * A 4-byte CRC value */ long crc(char *filename) { return (crc__doCRC(filename,0)); } /* * void crc_check(char *filename,int check) * * Use * Checks a CRC value for a file and reports a fatal error if the check * fails. * * Parameters * char *filename == the leafname of the file * long check == the CRC number to check with */ void crc_check(char *filename,long check) { if (check==-1) return; if (crc__doCRC(filename,0)!=check) { werr ( FALSE, msgs_lookup("crcFC:%s resource file '%s' has been corrupted. " "Please restore this file from the distribution disk."), wimpt_programname(), filename ); exit(0); } } /* * void crc_checkRunImage(void) * * Use * Corruption-checks the main !RunImage file. It must have been passed * through CodeScr first, to install the CRC into the code. */ void crc_checkRunImage(void) { FILE *fp=res_openfile("!RunImage","r"); long c; if (!fp) werr(TRUE,msgs_lookup("crcFNF:Couldn't open file '%s'."),"!RunImage"); fseek(fp,8,SEEK_SET); fread(&c,4,1,fp); fclose(fp); if (c!=crc__doCRC("!RunImage",12)) { werr ( FALSE, msgs_lookup("crcFC:%s resource file '%s' has been corrupted. " "Please restore this file from the distribution disk."), wimpt_programname(), "!RunImage" ); exit(0); } }