OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
CStdFile.h File Reference
#include <stdio.h>
#include "platform/StdSync.h"
#include <zlib.h>
Include dependency graph for CStdFile.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  CStdStream
 
class  CStdFile
 

Functions

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

Variables

const int CStdFileBufSize = 4096
 

Function Documentation

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

Definition at line 359 of file CStdFile.cpp.

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

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

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

Here is the call graph for this function:

Here is the caller graph for this function:

bool GetFileSHA1 ( const char *  szFilename,
BYTE pSHA1 
)

Definition at line 385 of file CStdFile.cpp.

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

Referenced by C4Network2Res::CalculateSHA(), and C4Record::Stop().

386 {
387  if (!pSHA1) return false;
388  // open file
389  CStdFile File;
390  if (!File.Open(szFilename))
391  return false;
392  // calculcate CRC
393  sha1 ctx;
394  for (;;)
395  {
396  // read a chunk of data
397  BYTE szData[CStdFileBufSize]; size_t iSize = 0;
398  if (!File.Read(szData, CStdFileBufSize, &iSize))
399  if (!iSize)
400  break;
401  // update CRC
402  ctx.process_bytes(szData, iSize);
403  }
404  // close file
405  File.Close();
406  // finish calculation
407  ctx.get_digest((sha1::digest_type) *pSHA1);
408  return true;
409 }
bool Close(StdBuf **ppMemory=nullptr)
Definition: CStdFile.cpp:155
const int CStdFileBufSize
Definition: CStdFile.h:27
uint8_t BYTE
bool Read(void *pBuffer, size_t iSize) override
Definition: CStdFile.h:61
bool Open(const char *szFileName, bool fCompressed=false)
Definition: CStdFile.cpp:99
int iSize
Definition: TstC4NetIO.cpp:35

Here is the call graph for this function:

Here is the caller graph for this function:

int UncompressedFileSize ( const char *  szFileName)

Definition at line 326 of file CStdFile.cpp.

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

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

Here is the call graph for this function:

Variable Documentation

const int CStdFileBufSize = 4096

Definition at line 27 of file CStdFile.h.

Referenced by GetFileCRC(), GetFileSHA1(), CStdFile::LoadBuffer(), and CStdFile::Write().