X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FCPak.cpp;h=1872fd930bf9df9330f3a6ae603cf3d8b78b0fdc;hb=690030e57c1f4aea6dbeef1da632ebb339d6844d;hp=65f4ab09656c1fc4489c65230302185e809ce909;hpb=a882955f339b23b5e61f92ef7cb5aae76405d95b;p=quix0rs-blobwars.git diff --git a/src/CPak.cpp b/src/CPak.cpp index 65f4ab0..1872fd9 100644 --- a/src/CPak.cpp +++ b/src/CPak.cpp @@ -1,5 +1,6 @@ /* -Copyright (C) 2004 Parallel Realities +Copyright (C) 2004-2011 Parallel Realities +Copyright (C) 2011-2015 Perpendicular Dimensions This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License @@ -29,21 +30,14 @@ Pak::Pak() listPos = 0; currentFile = NULL; - strcpy(pakFilename, ""); - strcpy(filename, ""); + pakFilename[0] = 0; + filename[0] = 0; } Pak::~Pak() { - if (input != NULL) - delete[] input; - - input = NULL; - - if (fd != NULL) - delete[] fd; - - fd = NULL; + delete[] input; + delete[] fd; } void Pak::showPakErrorAndExit() @@ -61,7 +55,7 @@ void Pak::showPakErrorAndExit() void Pak::setPakFile(const char *pakFilename) { #if USEPAK - strcpy(this->pakFilename, pakFilename); + strlcpy(this->pakFilename, pakFilename, sizeof this->pakFilename); debug(("Pak : Filename set to %s\n", pakFilename)); @@ -73,8 +67,16 @@ void Pak::setPakFile(const char *pakFilename) } fseek(pak, (-sizeof(Uint32)) * 2, SEEK_END); - fread(&listPos, sizeof(Uint32), 1, pak); - fread(&numberOfFiles, sizeof(Uint32), 1, pak); + if (fread(&listPos, sizeof(Uint32), 1, pak) != 1) + { + fclose(pak); + showPakErrorAndExit(); + } + if (fread(&numberOfFiles, sizeof(Uint32), 1, pak) != 1) + { + fclose(pak); + showPakErrorAndExit(); + } debug(("Pak : File list resides at %d\n", (int)listPos)); debug(("Pak : Number of files are %d\n", (int)numberOfFiles)); @@ -131,17 +133,17 @@ bool Pak::unpack(const char *filename, unsigned char **buffer) fseek(pak, currentFile->location, SEEK_SET); - if (input != NULL) - { - delete[] input; - } - + delete[] input; input = NULL; input = new unsigned char[(int)(currentFile->cSize * 1.01) + 12]; *buffer = new unsigned char[currentFile->fSize + 1]; - fread(input, 1, currentFile->cSize, pak); + if (fread(input, 1, currentFile->cSize, pak) != currentFile->cSize) + { + fclose(pak); + showPakErrorAndExit(); + } uLongf fSize = (uLongf)currentFile->fSize; @@ -150,9 +152,7 @@ bool Pak::unpack(const char *filename, unsigned char **buffer) fclose(pak); - if (input != NULL) - delete[] input; - + delete[] input; input = NULL; debug(("Pak : Unpack %s...Done\n", filename)); @@ -162,14 +162,6 @@ bool Pak::unpack(const char *filename, unsigned char **buffer) bool Pak::fileExists(const char *filename) { - unsigned int hashcode = 0; - unsigned int length = strlen(filename); - - for (unsigned int i = 0 ; i < length ; i++) - { - hashcode = hashcode + (filename[i] * 31 ^ (length - i)); - } - for (unsigned int i = 0 ; i < numberOfFiles ; i++) { if (strcmp(fd[i].filename, filename) == 0) @@ -181,7 +173,7 @@ bool Pak::fileExists(const char *filename) return false; } -unsigned int Pak::getUncompressedSize() +unsigned int Pak::getUncompressedSize() const { return (unsigned int)currentFile->fSize; }