/** * Calculate the MD5 sum for a GRF, and store it in the config. * @param config GRF to compute. * @param subdir The subdirectory to look in. * @return MD5 sum was successfully computed */ static bool CalcGRFMD5Sum(GRFConfig *config, Subdirectory subdir) { FILE *f; Md5 checksum; uint8 buffer[1024]; size_t len, size; /* open the file */ f = FioFOpenFile(config->filename, "rb", subdir, &size); if (f == NULL) return false; long start = ftell(f); size = min(size, GRFGetSizeOfDataSection(f)); if (start < 0 || fseek(f, start, SEEK_SET) < 0) { FioFCloseFile(f); return false; } /* calculate md5sum */ while ((len = fread(buffer, 1, (size > sizeof(buffer)) ? sizeof(buffer) : size, f)) != 0 && size != 0) { size -= len; checksum.Append(buffer, len); } checksum.Finish(config->ident.md5sum); FioFCloseFile(f); return true; }
/* Add the file and calculate the md5 sum. */ virtual bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename) { Md5 checksum; uint8 buffer[1024]; size_t len, size; byte tmp_md5sum[16]; /* Open the file ... */ FILE *f = FioFOpenFile(filename, "rb", this->dir, &size); if (f == NULL) return false; /* ... calculate md5sum... */ while ((len = fread(buffer, 1, (size > sizeof(buffer)) ? sizeof(buffer) : size, f)) != 0 && size != 0) { size -= len; checksum.Append(buffer, len); } checksum.Finish(tmp_md5sum); FioFCloseFile(f); /* ... and xor it to the overall md5sum. */ for (uint i = 0; i < sizeof(md5sum); i++) this->md5sum[i] ^= tmp_md5sum[i]; return true; }
/** * Calculate and check the MD5 hash of the supplied filename. * @param subdir The sub directory to get the files from * @return * - #CR_MATCH if the MD5 hash matches * - #CR_MISMATCH if the MD5 does not match * - #CR_NO_FILE if the file misses */ MD5File::ChecksumResult MD5File::CheckMD5(Subdirectory subdir) const { size_t size; FILE *f = FioFOpenFile(this->filename, "rb", subdir, &size); if (f == NULL) return CR_NO_FILE; Md5 checksum; uint8 buffer[1024]; uint8 digest[16]; size_t len; while ((len = fread(buffer, 1, (size > sizeof(buffer)) ? sizeof(buffer) : size, f)) != 0 && size != 0) { size -= len; checksum.Append(buffer, len); } FioFCloseFile(f); checksum.Finish(digest); return memcmp(this->hash, digest, sizeof(this->hash)) == 0 ? CR_MATCH : CR_MISMATCH; }
/** * Calculate the MD5 sum for a GRF, and store it in the config. * @param config GRF to compute. * @return MD5 sum was successfully computed */ static bool CalcGRFMD5Sum(GRFConfig *config) { FILE *f; Md5 checksum; uint8 buffer[1024]; size_t len, size; /* open the file */ f = FioFOpenFile(config->filename, "rb", DATA_DIR, &size); if (f == NULL) return false; /* calculate md5sum */ while ((len = fread(buffer, 1, (size > sizeof(buffer)) ? sizeof(buffer) : size, f)) != 0 && size != 0) { size -= len; checksum.Append(buffer, len); } checksum.Finish(config->ident.md5sum); FioFCloseFile(f); return true; }