Blame IlmImfFuzzTest/fuzzFile.cpp

Packit Service 6754ca
///////////////////////////////////////////////////////////////////////////
Packit Service 6754ca
//
Packit Service 6754ca
// Copyright (c) 2007, Industrial Light & Magic, a division of Lucas
Packit Service 6754ca
// Digital Ltd. LLC
Packit Service 6754ca
// 
Packit Service 6754ca
// All rights reserved.
Packit Service 6754ca
// 
Packit Service 6754ca
// Redistribution and use in source and binary forms, with or without
Packit Service 6754ca
// modification, are permitted provided that the following conditions are
Packit Service 6754ca
// met:
Packit Service 6754ca
// *       Redistributions of source code must retain the above copyright
Packit Service 6754ca
// notice, this list of conditions and the following disclaimer.
Packit Service 6754ca
// *       Redistributions in binary form must reproduce the above
Packit Service 6754ca
// copyright notice, this list of conditions and the following disclaimer
Packit Service 6754ca
// in the documentation and/or other materials provided with the
Packit Service 6754ca
// distribution.
Packit Service 6754ca
// *       Neither the name of Industrial Light & Magic nor the names of
Packit Service 6754ca
// its contributors may be used to endorse or promote products derived
Packit Service 6754ca
// from this software without specific prior written permission. 
Packit Service 6754ca
// 
Packit Service 6754ca
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit Service 6754ca
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit Service 6754ca
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Packit Service 6754ca
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Packit Service 6754ca
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit Service 6754ca
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit Service 6754ca
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit Service 6754ca
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit Service 6754ca
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit Service 6754ca
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit Service 6754ca
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit Service 6754ca
//
Packit Service 6754ca
///////////////////////////////////////////////////////////////////////////
Packit Service 6754ca
Packit Service 6754ca
Packit Service 6754ca
#include <fuzzFile.h>
Packit Service 6754ca
Packit Service 6754ca
#include <ImfRgbaFile.h>
Packit Service 6754ca
#include <ImfArray.h>
Packit Service 6754ca
#include <Iex.h>
Packit Service 6754ca
#include <half.h>
Packit Service 6754ca
Packit Service 6754ca
#include <iostream>
Packit Service 6754ca
#include <fstream>
Packit Service 6754ca
Packit Service 6754ca
// Handle the case when the custom namespace is not exposed
Packit Service 6754ca
#include <OpenEXRConfig.h>
Packit Service 6754ca
using namespace OPENEXR_IMF_INTERNAL_NAMESPACE;
Packit Service 6754ca
using namespace std;
Packit Service 6754ca
using namespace IMATH_NAMESPACE;
Packit Service 6754ca
Packit Service 6754ca
Packit Service 6754ca
namespace {
Packit Service 6754ca
Packit Service 6754ca
Int64
Packit Service 6754ca
lengthOfFile (const char fileName[])
Packit Service 6754ca
{
Packit Service 6754ca
    ifstream ifs (fileName, ios_base::binary);
Packit Service 6754ca
Packit Service 6754ca
    if (!ifs)
Packit Service 6754ca
	return 0;
Packit Service 6754ca
Packit Service 6754ca
    ifs.seekg (0, ios_base::end);
Packit Service 6754ca
    return ifs.tellg();
Packit Service 6754ca
}
Packit Service 6754ca
Packit Service 6754ca
Packit Service 6754ca
void
Packit Service 6754ca
fuzzFile (const char goodFile[],
Packit Service 6754ca
          const char brokenFile[],
Packit Service 6754ca
	  Int64 offset,
Packit Service 6754ca
	  Int64 windowSize,
Packit Service 6754ca
	  Rand48 &random,
Packit Service 6754ca
	  double fuzzAmount)
Packit Service 6754ca
{
Packit Service 6754ca
    //
Packit Service 6754ca
    // Read the input file.
Packit Service 6754ca
    //
Packit Service 6754ca
Packit Service 6754ca
    ifstream ifs (goodFile, ios_base::binary);
Packit Service 6754ca
Packit Service 6754ca
    if (!ifs)
Packit Service 6754ca
	THROW_ERRNO ("Cannot open file " << goodFile << " (%T).");
Packit Service 6754ca
Packit Service 6754ca
    ifs.seekg (0, ios_base::end);
Packit Service 6754ca
    Int64 fileLength = ifs.tellg();
Packit Service 6754ca
    ifs.seekg (0, ios_base::beg);
Packit Service 6754ca
Packit Service 6754ca
    Array<char> data (fileLength);
Packit Service 6754ca
    ifs.read (data, fileLength);
Packit Service 6754ca
Packit Service 6754ca
    if (!ifs)
Packit Service 6754ca
	THROW_ERRNO ("Cannot read file " << goodFile << " (%T)." << endl);
Packit Service 6754ca
Packit Service 6754ca
    //
Packit Service 6754ca
    // Damage the contents of the file by overwriting some of the bytes
Packit Service 6754ca
    // in a window of size windowSize, starting at the specified offset.
Packit Service 6754ca
    // 
Packit Service 6754ca
Packit Service 6754ca
    for (Int64 i = offset; i < offset + windowSize; ++i)
Packit Service 6754ca
    {
Packit Service 6754ca
	if (random.nextf() < fuzzAmount)
Packit Service 6754ca
	    data[i] = char (random.nexti());
Packit Service 6754ca
    }
Packit Service 6754ca
Packit Service 6754ca
    //
Packit Service 6754ca
    // Save the damaged file contents in the output file.
Packit Service 6754ca
    //
Packit Service 6754ca
Packit Service 6754ca
    ofstream ofs (brokenFile, ios_base::binary);
Packit Service 6754ca
Packit Service 6754ca
    if (!ofs)
Packit Service 6754ca
	THROW_ERRNO ("Cannot open file " << brokenFile << " (%T)." << endl);
Packit Service 6754ca
Packit Service 6754ca
    ofs.write (data, fileLength);
Packit Service 6754ca
Packit Service 6754ca
    if (!ofs)
Packit Service 6754ca
	THROW_ERRNO ("Cannot write file " << brokenFile << " (%T)." << endl);
Packit Service 6754ca
}
Packit Service 6754ca
Packit Service 6754ca
} // namespace
Packit Service 6754ca
Packit Service 6754ca
Packit Service 6754ca
void
Packit Service 6754ca
fuzzFile (const char goodFile[],
Packit Service 6754ca
          const char brokenFile[],
Packit Service 6754ca
	  void (*readFile) (const char[]),
Packit Service 6754ca
	  int nSlidingWindow,
Packit Service 6754ca
	  int nFixedWindow,
Packit Service 6754ca
	  Rand48 &random)
Packit Service 6754ca
{
Packit Service 6754ca
    //
Packit Service 6754ca
    // We want to test how resilient the IlmImf library is with respect
Packit Service 6754ca
    // to malformed OpenEXR input files.  In order to do this we damage
Packit Service 6754ca
    // a good input file by overwriting parts of it with random data.
Packit Service 6754ca
    // We then call function readFile() to try and read the damaged file.
Packit Service 6754ca
    // Provided the IlmImf library works as advertised, a try/catch(...)
Packit Service 6754ca
    // block in readFile() should be able to handle all errors that could
Packit Service 6754ca
    // possibly result from reading a broken OpenEXR file.  We repeat
Packit Service 6754ca
    // this damage/read cycle many times, overwriting different parts
Packit Service 6754ca
    // of the file:
Packit Service 6754ca
    //
Packit Service 6754ca
    // First we slide a window along the file.  The size of the window
Packit Service 6754ca
    // is fileSize*2/nSlidingWindow bytes.  In each damage/read cycle
Packit Service 6754ca
    // we overwrite up to 10% of the bytes the window, try to read the
Packit Service 6754ca
    // file, and advance the window by fileSize/nSlidingWindow bytes.
Packit Service 6754ca
    //
Packit Service 6754ca
    // Next we overwrite up to 10% of the file's first 2048 bytes and
Packit Service 6754ca
    // try to read the file.  We repeat this nFixedWindow times.
Packit Service 6754ca
    //
Packit Service 6754ca
Packit Service 6754ca
    {
Packit Service 6754ca
	Int64 fileSize = lengthOfFile (goodFile);
Packit Service 6754ca
	Int64 windowSize = fileSize * 2 / nSlidingWindow;
Packit Service 6754ca
	Int64 lastWindowOffset = fileSize - windowSize;
Packit Service 6754ca
Packit Service 6754ca
	cout << "sliding " << windowSize << "-byte window" << endl;
Packit Service 6754ca
Packit Service 6754ca
	for (int i = 0; i < nSlidingWindow; ++i)
Packit Service 6754ca
	{
Packit Service 6754ca
	    if (i % 100 == 0)
Packit Service 6754ca
		cout << i << "\r" << flush;
Packit Service 6754ca
Packit Service 6754ca
	    Int64 offset = lastWindowOffset * i / (nSlidingWindow - 1);
Packit Service 6754ca
	    double fuzzAmount = random.nextf (0.0, 0.1);
Packit Service 6754ca
Packit Service 6754ca
	    fuzzFile (goodFile, brokenFile,
Packit Service 6754ca
		      offset, windowSize,
Packit Service 6754ca
		      random, fuzzAmount);
Packit Service 6754ca
Packit Service 6754ca
	    readFile (brokenFile);
Packit Service 6754ca
	}
Packit Service 6754ca
Packit Service 6754ca
	cout << nSlidingWindow << endl;
Packit Service 6754ca
    }
Packit Service 6754ca
Packit Service 6754ca
    {
Packit Service 6754ca
	Int64 windowSize = 2048;
Packit Service 6754ca
Packit Service 6754ca
	cout << windowSize << "-byte window at start of file" << endl;
Packit Service 6754ca
Packit Service 6754ca
	for (int i = 0; i < nFixedWindow; ++i)
Packit Service 6754ca
	{
Packit Service 6754ca
	    if (i % 100 == 0)
Packit Service 6754ca
		cout << i << "\r" << flush;
Packit Service 6754ca
Packit Service 6754ca
	    double fuzzAmount = random.nextf (0.0, 0.1);
Packit Service 6754ca
Packit Service 6754ca
	    fuzzFile (goodFile, brokenFile,
Packit Service 6754ca
		      0, windowSize,
Packit Service 6754ca
		      random, fuzzAmount);
Packit Service 6754ca
Packit Service 6754ca
	    readFile (brokenFile);
Packit Service 6754ca
	}
Packit Service 6754ca
Packit Service 6754ca
	cout << nFixedWindow << endl;
Packit Service 6754ca
    }
Packit Service 6754ca
}