Esempio n. 1
0
void CRC32Test::testUpdateArrayIndexed() {

    static const int SIZE = 3;
    unsigned char byteArray[] = {1, 2, 3};
    CRC32 crc;

    int off = 2;// accessing the 2nd element of byteArray
    int len = 1;
    int lenError = 3;
    int offError = 4;
    crc.update( byteArray, SIZE, off, len );

    // Ran JDK and discovered that the value of the CRC should be
    // 1259060791
    CPPUNIT_ASSERT_EQUAL_MESSAGE( "update(unsigned char[],int,int) failed to update the checksum to the correct value ",
                                  1259060791LL, crc.getValue() );

    CPPUNIT_ASSERT_THROW_MESSAGE(
        "Should have thrown an IndexOutOfBoundsException for lenError",
        crc.update( byteArray, SIZE, off, lenError ),
        IndexOutOfBoundsException );

    CPPUNIT_ASSERT_THROW_MESSAGE(
        "Should have thrown an IndexOutOfBoundsException for offError",
        crc.update( byteArray, SIZE, offError, len ),
        IndexOutOfBoundsException );
}
Esempio n. 2
0
void testCRC32()
{
    cout << CRC32String("abc") << endl;
    cout << CRC32File("C:\\boot.ini") << endl;
    cout << CRC32File("C:\\a\\a.a") << endl;

    CRC32 crc32;
    crc32.Init();
    crc32.Update("", 0);
    crc32.Update("a", 1);
    crc32.Update("bc", 2);
    crc32.Update("defghijklmnopqrstuvwxyz",
                 (unsigned int)strlen("defghijklmnopqrstuvwxyz"));
    crc32.Final();
    Print("abcdefghijklmnopqrstuvwxyz", crc32);

    crc32.Init();
    crc32.Update("", 0);
    crc32.Update("a", 1);
    crc32.Update("bc", 2);
    crc32.Update("defghijklmnopqrstuvwxyz",
                 (unsigned int)strlen("defghijklmnopqrstuvwxyz"));
    crc32.Final();
    Print("abcdefghijklmnopqrstuvwxyz", crc32);

    CRC32 obj;
    obj.Init();
    obj.Update("abcdefghijklmnopqrstuvwxyz",
               (unsigned int)strlen("abcdefghijklmnopqrstuvwxyz"));
    obj.Final();
    Print("abcdefghijklmnopqrstuvwxyz", obj);
}
Esempio n. 3
0
void LogSequence2::save(std::ostream & out)
{
	CRC8 crch;
	CRC32 crcd;
	unsigned char data[9];
	unsigned int len;

	// Write type
	uint8_t type = TYPE_SEQLOG;
	crch.writeData(out, &type, sizeof(uint8_t));

	// Write numbits
	crch.writeData(out, &numbits, sizeof(numbits));

	// Write numentries
	len=csd::VByte::encode(data, numentries);
	crch.writeData(out, data, len);

	// Write Header CRC
	crch.writeCRC(out);

	// Write data
	size_t numbytes = numBytesFor(numbits, numentries);
	crcd.writeData(out, (unsigned char*)&array[0], numbytes);

	// Write Data CRC
	crcd.writeCRC(out);
}
Esempio n. 4
0
void CSD_PFC::save(ostream &out)
{
	CRC8 crch;
	CRC32 crcd;
	unsigned char buf[27]; // 9 bytes per VByte (max) * 3 values.

	// Save type
	crch.writeData(out, (unsigned char *)&type, sizeof(type));

	// Save sizes
	uint8_t pos = 0;
	pos += VByte::encode(&buf[pos], numstrings);
	pos += VByte::encode(&buf[pos], bytes);
	pos += VByte::encode(&buf[pos], blocksize);

	crch.writeData(out, buf, pos);
	crch.writeCRC(out);

	// Write block pointers
	if(!blocks) {
		hdt::LogSequence2 log;
		log.save(out);
	} else {
		blocks->save(out);
	}

	// Write packed data
	if(text) {
		crcd.writeData(out, text, bytes);
	} else {
		assert(numstrings==0);
		assert(bytes==0);
	}
	crcd.writeCRC(out);
}
Esempio n. 5
0
void CRC32Test::testUpdateI() {

    CRC32 crc;
    crc.update( 1 );

    // Ran JDK and discovered that the value of the CRC should be
    // 2768625435
    CPPUNIT_ASSERT_EQUAL_MESSAGE( "update(1) failed to update the checksum to the correct value ",
                                  2768625435LL, crc.getValue() );

    crc.reset();
    crc.update( Integer::MAX_VALUE );

    // Ran JDK and discovered that the value of the CRC should be
    // 4278190080
    CPPUNIT_ASSERT_EQUAL_MESSAGE( "update(max) failed to update the checksum to the correct value ",
                                  4278190080LL, crc.getValue() );

    crc.reset();
    crc.update( Integer::MIN_VALUE );

    // Ran JDK and discovered that the value of the CRC should be
    // 3523407757
    CPPUNIT_ASSERT_EQUAL_MESSAGE( "update(min) failed to update the checksum to the correct value ",
                                  3523407757LL, crc.getValue() );
}
Esempio n. 6
0
Uint32 MessageOut::addCRC(){
	CRC32 msgCRC;
	Uint32 crc;

	msgCRC.Init();
	crc = msgCRC.GetCRC(this->myData, (this->pos));

	write4(crc);

	return crc;
}
Esempio n. 7
0
void CRC32Test::testReset() {

    CRC32 crc;
    crc.update( 1 );

    // Ran JDK and discovered that the value of the CRC should be
    // 2768625435
    CPPUNIT_ASSERT_EQUAL_MESSAGE( "update(int) failed to update the checksum to the correct value ",
                                  2768625435LL, crc.getValue() );
    crc.reset();
    CPPUNIT_ASSERT_EQUAL_MESSAGE( "reset failed to reset the checksum value to zero",
                                  0LL, crc.getValue() );
}
Esempio n. 8
0
void LogSequence2::load(std::istream & input)
{
	CRC8 crch;
	CRC32 crcd;
	unsigned char buf[9];

	// Read type
	uint8_t type;
	crch.readData(input, (unsigned char*)&type, sizeof(type));
	if(type!=TYPE_SEQLOG) {
		//throw "Trying to read a LOGArray but data is not LogArray";
	}

	// Read numbits
	crch.readData(input, (unsigned char*)&numbits, sizeof(numbits));

	// Read numentries
	uint64_t numentries64 = csd::VByte::decode(input);
	unsigned int pos = csd::VByte::encode(buf, numentries64);
	crch.update(buf, pos);

	// Validate Checksum Header
	crc8_t filecrch = crc8_read(input);
	if(crch.getValue()!=filecrch) {
		throw "Checksum error while reading LogSequence2 header.";
	}

	// Update local variables and validate
	maxval = maxVal(numbits);
	numentries = (size_t) numentries64;
	if(numbits>sizeof(size_t)*8 || numentries64>std::numeric_limits<size_t>::max()) {
		throw "This data structure is too big for this machine";
	}

	// Calculate data size, reserve buffer.
	size_t numbytes = numBytesFor(numbits, numentries);
	data.resize(numElementsFor(numbits, numentries));
	arraysize = data.size();
    array = &data[0];

	// Read data
	crcd.readData(input, (unsigned char*)&array[0], numbytes);

	// Validate checksum data
	crc32_t filecrcd = crc32_read(input);
	if(crcd.getValue()!=filecrcd) {
		throw "Checksum error while reading LogSequence2 Data";
	}

	IsMapped = false;
}
Esempio n. 9
0
//! A CRC-32 is calculated over the load data, including any pad bytes
//! that are required in the last data cipher block. Including the
//! pad bytes in the CRC makes it vastly easier for the ROM to calculate
//! the CRC for validation.
uint32_t EncoreBootImage::LoadCommand::calculateCRC() const
{
	uint32_t result;
	CRC32 crc;
	crc.update(m_data, m_length);
	if (m_padCount)
	{
		// include random padding in the CRC
		crc.update(m_padding, m_padCount);
	}
	crc.truncatedFinal(reinterpret_cast<uint8_t*>(&result), sizeof(result));
	
	return result;
}
Esempio n. 10
0
void CDiskPartHandlerBase::stop()
{
    if (!eoi)
        checkFileCrc = false; // cannot perform file CRC if diskread has not read whole file.
    CRC32 fileCRC;
    close(fileCRC);
    if (!activity.abortSoon && checkFileCrc)
    {
        ActPrintLog(&activity, "%s[part=%d]: CRC Stored=%x, calculated=%x file(%s)", kindStr, which, storedCrc, fileCRC.get(), filename.get());
        if (fileCRC.get() != storedCrc)
            throw MakeThorOperatorException(TE_FileCrc, "CRC Failure having read file: %s", filename.get());
        checkFileCrc = false;
    }
}
Esempio n. 11
0
uint32_t File::crc32() const
{
	istream* stream = openInputStream(istream::binary);

	CRC32 crc;
	char buf[4096];

	while (!stream->eof()) {
		stream->read(buf, sizeof(buf));
		crc.append(buf, stream->gcount());
	}

	delete stream;

	return crc.getChecksum();
}
Esempio n. 12
0
 virtual void close(CRC32 &fileCRC)
 {
     CriticalBlock block(statsCs);
     xmlParser.clear();
     inputIOstream.clear();
     if (checkFileCrc)
     {
         fileCRC.reset(~crcStream->queryCrc()); // MORE should prob. change stream to use CRC32
         crcStream.clear();
     }
     mergeStats(fileStats, iFileIO);
     iFileIO.clear();
 }
Esempio n. 13
0
 virtual void close(CRC32 &fileCRC)
 {
     xmlParser.clear();
     inputIOstream.clear();
     if (checkFileCrc)
     {
         fileCRC.reset(~crcStream->queryCrc()); // MORE should prob. change stream to use CRC32
         crcStream.clear();
     }
     Owned<IFileIO> partFileIO;
     {
         CriticalBlock block(statsCs);
         partFileIO.setown(iFileIO.getClear());
     }
     mergeStats(fileStats, partFileIO);
 }
Esempio n. 14
0
void CRC32Test::testUpdateArray() {

    unsigned char byteArray[] = { 1, 2 };
    CRC32 crc;
    crc.update( byteArray, 2, 0, 2 );

    // Ran JDK and discovered that the value of the CRC should be
    // 3066839698
    CPPUNIT_ASSERT_EQUAL_MESSAGE( "update(unsigned char[]) failed to update the checksum to the correct value ",
                                  3066839698LL, crc.getValue() );

    crc.reset();
    std::vector<unsigned char> byteEmpty( 10000, 0 );
    crc.update( byteEmpty );

    // Ran JDK and discovered that the value of the CRC should be
    // 1295764014
    CPPUNIT_ASSERT_EQUAL_MESSAGE( "update(unsigned char[]) failed to update the checksum to the correct value ",
                                  1295764014LL, crc.getValue() );
}
Esempio n. 15
0
static void Print(const string &str, CRC32 &crc32)
{
    cout << "CRC32-32(\"" << str << "\") = " << crc32.ToString() << endl;
    cout << "CRC32-32(\"" << str << "\") = " << crc32.GetValue() << endl;
}
Esempio n. 16
0
CSD* CSD_PFC::load(istream & fp)
{
	CRC8 crch;
	CRC32 crcd;
	unsigned char buf[27]; // 9 bytes per VByte (max) * 3 values.
	CSD_PFC *dicc = new CSD_PFC();

	// Load variables
	dicc->type = PFC;   // Type already read by CSD
	dicc->numstrings = (uint32_t) VByte::decode(fp);
	dicc->bytes = VByte::decode(fp);
	dicc->blocksize = (uint32_t) VByte::decode(fp);

	// Calculate variables CRC
	crch.update(&dicc->type, sizeof(dicc->type));

	uint8_t pos = 0;
	pos += VByte::encode(&buf[pos], dicc->numstrings);
	pos += VByte::encode(&buf[pos], dicc->bytes);
	pos += VByte::encode(&buf[pos], dicc->blocksize);
	crch.update(buf, pos);

	crc8_t filecrc = crc8_read(fp);
	if(crch.getValue()!=filecrc) {
		throw std::runtime_error("Checksum error while reading Plain Front Coding Header.");
	}

	// Load blocks
	dicc->blocks = new hdt::LogSequence2();
	dicc->blocks->load(fp);
	dicc->nblocks = dicc->blocks->getNumberOfElements()-1;

	// Load strings
	if(dicc->bytes && dicc->numstrings) {
		dicc->text = (unsigned char *)malloc(dicc->bytes);
		const unsigned int blocksize = 8192;
		uint64_t counter=0;
		unsigned char *ptr = (unsigned char *)dicc->text;
		while(counter<dicc->bytes && fp.good()) {
			crcd.readData(fp, ptr, dicc->bytes-counter > blocksize ? blocksize : dicc->bytes-counter);

			ptr += fp.gcount();
			counter += fp.gcount();
		}
		if(counter!=dicc->bytes) {
			throw std::runtime_error("Could not read all the data section of the Plain Front Coding.");
		}
	} else {
		// Make sure that all is zero.
		dicc->text = NULL;
		dicc->numstrings = 0;
		dicc->bytes = 0;
		dicc->nblocks = 0;
		delete dicc->blocks;
	}

	crc32_t filecrcd = crc32_read(fp);
	if(filecrcd!=crcd.getValue()) {
		throw std::runtime_error("Checksum error in the data section of the Plain Front Coding.");
	}

	return dicc;
}
Esempio n. 17
0
std::string getCrc(const boost::filesystem::path& path)
{
    CRC32 algorithm;
    hashFile(path, algorithm);
    return algorithm.getHash();
}
Esempio n. 18
0
bool SyringeDebugger::RetrieveInfo(std::string filename)
{
	bControlLoaded = false;

	exe = std::move(filename);

	Log::WriteLine("SyringeDebugger::RetrieveInfo: Retrieving info from the executable file...");

	PortableExecutable pe(exe);
	if(pe.IsValid())
	{
		DWORD dwImageBase = pe.GetImageBase();

		//Creation time stamp
		dwTimeStamp = pe.GetPEHeader().FileHeader.TimeDateStamp;

		//Entry point
		pcEntryPoint = reinterpret_cast<void*>(dwImageBase + pe.GetPEHeader().OptionalHeader.AddressOfEntryPoint);

		//Get Imports
		pImLoadLibrary = nullptr;
		pImGetProcAddress = nullptr;

		for(const auto& import : pe.GetImports()) {
			if(_strcmpi(import.Name.c_str(), "KERNEL32.DLL") == 0) {
				for(const auto& thunk : import.vecThunkData) {
					if(_strcmpi(thunk.Name.c_str(), "GETPROCADDRESS") == 0) {
						pImGetProcAddress = reinterpret_cast<void*>(dwImageBase + thunk.Address);
					} else if(_strcmpi(thunk.Name.c_str(), "LOADLIBRARYA") == 0) {
						pImLoadLibrary = reinterpret_cast<void*>(dwImageBase + thunk.Address);
					}
				}
			}
		}

		if(!pImGetProcAddress || !pImLoadLibrary) {
			Log::WriteLine("SyringeDebugger::RetrieveInfo: ERROR: Either a LoadLibraryA or a GetProcAddress import could not be found!");
			return false;
		}
	} else {
		Log::WriteLine("SyringeDebugger::RetrieveInfo: Failed to open the executable!");
		return false;
	}

	// read meta information: size and checksum
	ifstream is;
	is.open(exe, ifstream::binary);
	is.seekg(0, ifstream::end);
	dwExeSize = static_cast<DWORD>(is.tellg());
	is.seekg(0, ifstream::beg);

	CRC32 crc;
	char buffer[0x1000];
	while(std::streamsize read = is.read(buffer, sizeof(buffer)).gcount()) {
		crc.compute(buffer, read);
	}
	dwExeCRC = crc.value();
	is.close();

	Log::WriteLine("SyringeDebugger::RetrieveInfo: Executable information successfully retrieved.");
	Log::WriteLine("\texe = %s", exe.c_str());
	Log::WriteLine("\tpImLoadLibrary = 0x%08X", pImLoadLibrary);
	Log::WriteLine("\tpImGetProcAddress = 0x%08X", pImGetProcAddress);
	Log::WriteLine("\tpcEntryPoint = 0x%08X", pcEntryPoint);
	Log::WriteLine("\tdwExeSize = 0x%08X", dwExeSize);
	Log::WriteLine("\tdwExeCRC = 0x%08X", dwExeCRC);
	Log::WriteLine("\tdwTimestamp = 0x%08X", dwTimeStamp);
	Log::WriteLine();

	Log::WriteLine("SyringeDebugger::RetrieveInfo: Opening %s to determine imports.", exe.c_str());

	bControlLoaded = true;
	return true;
}
Esempio n. 19
0
 void tally(CRC32 & crc) { crc.tally(sizeof(offset_t) + thisrowsize, buffer); }
Esempio n. 20
0
void CRC32Test::testConstructor() {

    CRC32 crc;
    CPPUNIT_ASSERT_EQUAL_MESSAGE( "Constructor of CRC32 failed", 0LL, crc.getValue() );
}
Esempio n. 21
0
void CRC32Test::testGetValue() {

    // test methods of java.util.zip.crc32.getValue()
    CRC32 crc;
    CPPUNIT_ASSERT_EQUAL_MESSAGE( "getValue() should return a zero as a result of constructing a CRC32 instance",
                                  0LL, crc.getValue() );

    crc.reset();
    crc.update( Integer::MAX_VALUE );

    // Ran JDK and discovered that the value of the CRC should be
    // 4278190080
    CPPUNIT_ASSERT_EQUAL_MESSAGE( "update(max) failed to update the checksum to the correct value ",
                                  4278190080LL, crc.getValue() );

    crc.reset();
    std::vector<unsigned char> byteEmpty( 10000, 0 );
    crc.update( byteEmpty );

    // Ran JDK and discovered that the value of the CRC should be
    // 1295764014
    CPPUNIT_ASSERT_EQUAL_MESSAGE( "update(byte[]) failed to update the checksum to the correct value ",
                                  1295764014LL, crc.getValue() );

    crc.reset();
    crc.update( 1 );

    // Ran JDK and discovered that the value of the CRC should be
    // 2768625435
    // CPPUNIT_ASSERT_EQUAL_MESSAGE( "update(int) failed to update the checksum to the correct
    // value ",2768625435L, crc.getValue());
    crc.reset();
    CPPUNIT_ASSERT_EQUAL_MESSAGE( "reset failed to reset the checksum value to zero",
                                  0LL, crc.getValue());
}