CMS 3D CMS Logo

adler32.cc
Go to the documentation of this file.
1 /*
2 
3 
4  Program to calculate adler32 checksum for every file given
5  on the command line. Uses zlib's adler32 routine.
6 
7  For the CMS Experiment http://cms.cern.ch
8 
9  Author: Stephen J. Gowdy <gowdy@cern.ch>
10  Created: 6th Dec 2008
11 
12 */
13 
14 #define _LARGEFILE64_SOURCE
15 #include <cstdio>
16 #include <cstdlib>
17 #include <fcntl.h>
18 #include <fmt/format.h>
19 #include <iostream>
20 #include <libgen.h>
21 #include <sys/stat.h>
22 #include <unistd.h>
23 #include <zlib.h>
24 #ifdef __APPLE__
25 typedef off_t off64_t;
26 #define O_LARGEFILE 0
27 #endif
28 
29 constexpr int EDMFILEUTILADLERBUFSIZE = 10 * 1024 * 1024; // 10MB buffer
30 
31 int main(int argc, char* argv[]) {
32  if (argc == 1) {
33  std::cout << basename(argv[0]) << ": no files specified.\n";
34  exit(1);
35  }
36 
37  std::unique_ptr<unsigned char[]> buffer{new unsigned char[EDMFILEUTILADLERBUFSIZE]};
38  int fileNum = 0;
39  for (fileNum = 1; fileNum < argc; fileNum++) {
40  uLong adlerCksum = adler32(0, nullptr, 0);
41  off64_t fileSize = 0;
42 
43  int myFD = open(argv[fileNum], O_RDONLY | O_LARGEFILE);
44  if (myFD == -1) {
45  std::cout << basename(argv[0]) << ": failed to open file " << argv[fileNum] << ".\n";
46  continue;
47  }
48 
49  lseek(myFD, 0, SEEK_SET);
50 
51  int readSize = 0;
52  while ((readSize = read(myFD, buffer.get(), EDMFILEUTILADLERBUFSIZE)) > 0) {
53  adlerCksum = adler32(adlerCksum, buffer.get(), readSize);
54  fileSize += readSize;
55  }
56 
57  std::cout << fmt::format("{:x} {} {}\n", adlerCksum, fileSize, argv[fileNum]);
58  }
59 
60  return (0);
61 }
62 
63 /* Compile-line: gcc adler32.c -o adler32 -lz */
#define O_LARGEFILE
constexpr int EDMFILEUTILADLERBUFSIZE
Definition: adler32.cc:29
int main(int argc, char *argv[])
Definition: adler32.cc:31
def exit(msg="")