wfut 0.2.4
A client side C++ implementation of WFUT (WorldForge Update Tool).
crc32.cpp
1// This file may be redistributed and modified only under the terms of
2// the GNU Lesser General Public License (See COPYING for details).
3// Copyright (C) 2005 - 2007 Simon Goodall
4
5#include <zlib.h>
6#include <string>
7#include <cstdio>
8
9#include "crc32.h"
10namespace WFUT {
11
12int calcCRC32(const std::string &filename, uLong &crc) {
13 // Open file for writing
14 FILE *fp = fopen(filename.c_str(), "rb");
15 if (!fp) {
16 // Error!
17 return -1;
18 }
19 // Initialise CRC32 value
20 crc = crc32(0L, Z_NULL, 0);
21 Bytef buf[8192];
22 size_t len;
23 // Read all bytes in file and calculate the crc32 value
24 while ((len = fread(&buf[0], sizeof(Bytef), 8192, fp)) != 0) {
25 crc = crc32(crc, buf, len);
26 }
27 // Close file handle
28 fclose(fp);
29 // Return calculated CRC32
30 return 0;
31}
32
33}