initial commit

This commit is contained in:
Michael Krayer 2021-02-16 15:48:19 +01:00
commit d8c21c3650
3 changed files with 78 additions and 0 deletions

10
README.md Normal file
View File

@ -0,0 +1,10 @@
nullblocks
---
Scan a file for contiguous blocks of 'null' bytes.
Usage:
```bash
nullblocks <file>
```
The default minimum block size to be reported is 8192 bytes. Maybe this will
be adjustable as a command line argument in the future...

2
build Executable file
View File

@ -0,0 +1,2 @@
#!/bin/bash
g++ -o nullblocks src/nullblocks.cc

66
src/nullblocks.cc Normal file
View File

@ -0,0 +1,66 @@
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
const long IO_BUFFSIZE=(long)1024*1024; // read 1 KiB at a time
const long MIN_NULL_BLOCK_SIZE=(long)8*1024; // minimum number of consecutive NULL bytes
int main(int argc,char *argv[]){
int ifile, iblock;
char* buffer = new char[IO_BUFFSIZE];
long filesize, iosize, nullblock_len, nullblock_beg;
cout << "Checking files for NULL blocks (threshold: "
<< MIN_NULL_BLOCK_SIZE << " bytes)" << endl;
string filename;
for(ifile=1;ifile<argc;ifile++){
filename = argv[ifile];
ifstream file(filename,ios::in|ios::binary|ios::ate);
if(!file)
cerr << "Cannot open file: " << filename << endl;
filesize = (long)file.tellg();
file.seekg(0,ios::beg);
cout << filename << " (" << filesize << " bytes)" << endl;
iblock=0;
nullblock_beg=0;
nullblock_len=0;
bool trunc = false;
while(!trunc){
trunc = (filesize-iblock*IO_BUFFSIZE)<IO_BUFFSIZE;
if(trunc)
iosize = filesize-iblock*IO_BUFFSIZE;
else
iosize = IO_BUFFSIZE;
file.read(buffer,iosize);
if(file.fail()){
cerr << "I/O Error: " << filename << " @ " << file.tellg() << endl;
break;
}
for(int ii=0;ii<iosize;ii++){
if((int)buffer[ii]==0){
if(nullblock_beg==0){
nullblock_beg = iblock*IO_BUFFSIZE+ii;
}
nullblock_len++;
}
else{
if(nullblock_len>=MIN_NULL_BLOCK_SIZE){
cout << "NULL block: position = " << setw(16) << nullblock_beg
<< ", size = " << setw(16) << nullblock_len << endl;
}
nullblock_len = 0;
nullblock_beg = 0;
}
}
iblock++;
}
file.close();
}
delete[] buffer;
return 0;
}