Пример #1
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());
}
Пример #2
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() );
}
Пример #3
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() );
}
Пример #4
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 );
}
Пример #5
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() );
}
Пример #6
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;
}
Пример #7
0
void CRC32Test::testConstructor() {

    CRC32 crc;
    CPPUNIT_ASSERT_EQUAL_MESSAGE( "Constructor of CRC32 failed", 0LL, crc.getValue() );
}
Пример #8
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;
}