OpenClonk
CStdFile.cpp File Reference
#include "C4Include.h"
#include "c4group/CStdFile.h"
#include "lib/SHA1.h"
#include <zlib.h>
#include "zlib/gzio.h"
#include <sys/stat.h>
Include dependency graph for CStdFile.cpp:

Go to the source code of this file.

Functions

int UncompressedFileSize (const char *szFilename)
 
bool GetFileCRC (const char *szFilename, uint32_t *pCRC32)
 
bool GetFileSHA1 (const char *szFilename, BYTE *pSHA1)
 

Function Documentation

◆ GetFileCRC()

bool GetFileCRC ( const char *  szFilename,
uint32_t *  pCRC32 
)

Definition at line 355 of file CStdFile.cpp.

356 {
357  if (!pCRC32) return false;
358  // open file
359  CStdFile File;
360  if (!File.Open(szFilename))
361  return false;
362  // calculcate CRC
363  uint32_t iCRC32 = 0;
364  for (;;)
365  {
366  // read a chunk of data
367  BYTE szData[CStdFileBufSize]; size_t iSize = 0;
368  if (!File.Read(szData, CStdFileBufSize, &iSize))
369  if (!iSize)
370  break;
371  // update CRC
372  iCRC32 = crc32(iCRC32, szData, iSize);
373  }
374  // close file
375  File.Close();
376  // okay
377  *pCRC32 = iCRC32;
378  return true;
379 }
const int CStdFileBufSize
Definition: CStdFile.h:26
uint8_t BYTE
int iSize
Definition: TstC4NetIO.cpp:32
bool Close(StdBuf **ppMemory=nullptr)
Definition: CStdFile.cpp:151
bool Read(void *pBuffer, size_t iSize) override
Definition: CStdFile.h:60
bool Open(const char *szFileName, bool fCompressed=false)
Definition: CStdFile.cpp:95

References CStdFile::Close(), CStdFileBufSize, iSize, CStdFile::Open(), and CStdFile::Read().

Referenced by C4UpdatePackage::Check(), DisplayGroup(), C4UpdatePackage::Execute(), C4Network2Res::GetStandalone(), C4UpdatePackage::MakeUpdate(), and C4Network2Res::SetByFile().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetFileSHA1()

bool GetFileSHA1 ( const char *  szFilename,
BYTE pSHA1 
)

Definition at line 381 of file CStdFile.cpp.

382 {
383  if (!pSHA1) return false;
384  // open file
385  CStdFile File;
386  if (!File.Open(szFilename))
387  return false;
388  // calculcate CRC
389  sha1 ctx;
390  for (;;)
391  {
392  // read a chunk of data
393  BYTE szData[CStdFileBufSize]; size_t iSize = 0;
394  if (!File.Read(szData, CStdFileBufSize, &iSize))
395  if (!iSize)
396  break;
397  // update CRC
398  ctx.process_bytes(szData, iSize);
399  }
400  // close file
401  File.Close();
402  // finish calculation
403  ctx.get_digest((sha1::digest_type) *pSHA1);
404  return true;
405 }
Definition: SHA1.h:45
unsigned int(& digest_type)[5]
Definition: SHA1.h:47
void process_bytes(void const *buffer, std::size_t byte_count)
Definition: SHA1.h:111
void get_digest(digest_type digest)
Definition: SHA1.h:169

References CStdFile::Close(), CStdFileBufSize, sha1::get_digest(), iSize, CStdFile::Open(), sha1::process_bytes(), and CStdFile::Read().

Referenced by C4Network2Res::CalculateSHA(), C4StartupModsDownloader::ModInfo::LocalDiscoveryCheck::Execute(), and C4Record::Stop().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ UncompressedFileSize()

int UncompressedFileSize ( const char *  szFilename)

Definition at line 322 of file CStdFile.cpp.

323 {
324  int rd,rval=0;
325  BYTE buf[1024];
326  int flags = _O_BINARY|O_CLOEXEC|O_RDONLY;
327 #ifdef _WIN32
328  int mode = _S_IREAD|_S_IWRITE;
329  int fd = _wopen(GetWideChar(szFilename), flags, mode);
330 #else
331  mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
332  int fd = open(szFilename, flags, mode);
333 #endif
334  gzFile hFile;
335  if (!(hFile = c4_gzdopen(fd,"rb"))) return 0;
336  do
337  {
338  rd = c4_gzread(hFile,&buf,sizeof(buf));
339  if (rd < 0) break;
340  rval += rd;
341  }
342  while (rd == sizeof buf);
343  c4_gzclose(hFile);
344  return rval;
345 }
StdStrBuf::wchar_t_holder GetWideChar(const char *utf8, bool double_null_terminate=false)
#define _O_BINARY
#define O_CLOEXEC
int ZEXPORT c4_gzread(gzFile file, voidp buf, unsigned len)
Definition: gzio.c:405
gzFile ZEXPORT c4_gzdopen(int fd, const char *mode)
Definition: gzio.c:228
int ZEXPORT c4_gzclose(gzFile file)
Definition: gzio.c:964

References _O_BINARY, c4_gzclose(), c4_gzdopen(), c4_gzread(), GetWideChar(), and O_CLOEXEC.

Here is the call graph for this function: