w_checksum.c
1 // 2 // Copyright(C) 1993-1996 Id Software, Inc. 3 // Copyright(C) 2005-2014 Simon Howard 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 // DESCRIPTION: 16 // Generate a checksum of the WAD directory. 17 // 18 19 #include <stdio.h> 20 #include <stdlib.h> 21 #include <string.h> 22 23 #include "m_misc.h" 24 #include "sha1.h" 25 #include "w_checksum.h" 26 #include "w_wad.h" 27 28 static wad_file_t **open_wadfiles = NULL; 29 static int num_open_wadfiles = 0; 30 31 static int GetFileNumber(wad_file_t *handle) 32 { 33 int i; 34 int result; 35 36 for (i=0; i<num_open_wadfiles; ++i) 37 { 38 if (open_wadfiles[i] == handle) 39 { 40 return i; 41 } 42 } 43 44 // Not found in list. This is a new file we haven't seen yet. 45 // Allocate another slot for this file. 46 47 open_wadfiles = realloc(open_wadfiles, 48 sizeof(wad_file_t *) * (num_open_wadfiles + 1)); 49 open_wadfiles[num_open_wadfiles] = handle; 50 51 result = num_open_wadfiles; 52 ++num_open_wadfiles; 53 54 return result; 55 } 56 57 static void ChecksumAddLump(sha1_context_t *sha1_context, lumpinfo_t *lump) 58 { 59 char buf[9]; 60 61 M_StringCopy(buf, lump->name, sizeof(buf)); 62 SHA1_UpdateString(sha1_context, buf); 63 SHA1_UpdateInt32(sha1_context, GetFileNumber(lump->wad_file)); 64 SHA1_UpdateInt32(sha1_context, lump->position); 65 SHA1_UpdateInt32(sha1_context, lump->size); 66 } 67 68 void W_Checksum(sha1_digest_t digest) 69 { 70 sha1_context_t sha1_context; 71 unsigned int i; 72 73 SHA1_Init(&sha1_context); 74 75 num_open_wadfiles = 0; 76 77 // Go through each entry in the WAD directory, adding information 78 // about each entry to the SHA1 hash. 79 80 for (i=0; i<numlumps; ++i) 81 { 82 ChecksumAddLump(&sha1_context, &lumpinfo[i]); 83 } 84 85 SHA1_Final(digest, &sha1_context); 86 } 87