Example #1
0
/*static*/ status_t
FSUtils::CompareFileContent(BPositionIO& content1, BPositionIO& content2,
                            bool& _equal)
{
    // get and compare content size
    off_t size1;
    status_t error = content1.GetSize(&size1);
    if (error != B_OK)
        return error;

    off_t size2;
    error = content2.GetSize(&size2);
    if (error != B_OK)
        return error;

    if (size1 != size2) {
        _equal = false;
        return B_OK;
    }

    if (size1 == 0) {
        _equal = true;
        return B_OK;
    }

    // allocate a data buffer
    uint8* buffer1 = new(std::nothrow) uint8[2 * kCompareDataBufferSize];
    if (buffer1 == NULL)
        return B_NO_MEMORY;
    ArrayDeleter<uint8> bufferDeleter(buffer1);
    uint8* buffer2 = buffer1 + kCompareDataBufferSize;

    // compare the data
    off_t offset = 0;
    while (offset < size1) {
        size_t toCompare = std::min(size_t(size1 - offset),
                                    kCompareDataBufferSize);
        ssize_t bytesRead = content1.ReadAt(offset, buffer1, toCompare);
        if (bytesRead < 0)
            return bytesRead;
        if ((size_t)bytesRead != toCompare)
            return B_ERROR;

        bytesRead = content2.ReadAt(offset, buffer2, toCompare);
        if (bytesRead < 0)
            return bytesRead;
        if ((size_t)bytesRead != toCompare)
            return B_ERROR;

        if (memcmp(buffer1, buffer2, toCompare) != 0) {
            _equal = false;
            return B_OK;
        }

        offset += bytesRead;
    }

    _equal = true;
    return B_OK;
}
Example #2
0
void
TiffUintField::LoadByte(IFDEntry &entry, BPositionIO &io, swap_action swp)
{
	// Make certain there is enough memory
	// before trying to do anything else
	fpByte = new uint8[entry.count];
	if (!fpByte) {
		finitStatus = B_NO_MEMORY;
		return;
	}
	
	if (entry.count <= 4) {
		// If all of the byte values can fit into the
		// IFD entry value bytes
		memcpy(fpByte, entry.bytevals, entry.count);
		finitStatus = B_OK;		
	} else {
	
		// entry.count > 4, use longval to find offset for byte data
		if (swap_data(B_UINT32_TYPE, &entry.longval, 4, swp) != B_OK)
			finitStatus = B_ERROR;
		else {
			ssize_t read;
			read = io.ReadAt(entry.longval, fpByte, entry.count);
			if (read != static_cast<ssize_t>(entry.count))
				finitStatus = B_IO_ERROR;
			else
				finitStatus = B_OK;
		}
		
	}
}
Example #3
0
// read_exactly
static
void
read_exactly(BPositionIO& file, off_t position, void* buffer, size_t size,
			 const char* errorMessage = NULL)
{
	ssize_t read = file.ReadAt(position, buffer, size);
	if (read < 0)
		throw Exception(read, errorMessage);
	else if ((size_t)read != size) {
		if (errorMessage) {
			throw Exception("%s Read to few bytes (%ld/%lu).", errorMessage,
							read, size);
		} else
			throw Exception("Read to few bytes (%ld/%lu).", read, size);
	}
}
Example #4
0
void
TiffUintField::LoadShort(IFDEntry &entry, BPositionIO &io, swap_action swp)
{
	// Make certain there is enough memory
	// before trying to do anything else
	fpShort = new uint16[entry.count];
	if (!fpShort) {
		finitStatus = B_NO_MEMORY;
		return;
	}
	
	if (entry.count <= 2) {
		// If all of the byte values can fit into the
		// IFD entry value bytes
		memcpy(fpShort, entry.shortvals, entry.count * 2);
		finitStatus = B_OK;		
	} else {
	
		// entry.count > 2, use longval to find offset for short data
		if (swap_data(B_UINT32_TYPE, &entry.longval, 4, swp) != B_OK)
			finitStatus = B_ERROR;
		else {
			ssize_t read;
			read = io.ReadAt(entry.longval, fpShort, entry.count * 2);
			if (read != static_cast<ssize_t>(entry.count) * 2)
				finitStatus = B_IO_ERROR;
			else
				finitStatus = B_OK;
		}
		
	}
	
	// If short values were successfully read in, swap them to
	// the correct byte order
	if (finitStatus == B_OK &&
		swap_data(B_UINT16_TYPE, fpShort, entry.count * 2, swp) != B_OK)
		finitStatus = B_ERROR;
}
Example #5
0
void
TiffUintField::LoadLong(IFDEntry &entry, BPositionIO &io, swap_action swp)
{
	// Make certain there is enough memory
	// before trying to do anything else
	fpLong = new uint32[entry.count];
	if (!fpLong) {
		finitStatus = B_NO_MEMORY;
		return;
	}
	
	if (entry.count == 1) {
		fpLong[0] = entry.longval;
		finitStatus = B_OK;		
	} else {
	
		// entry.count > 1, use longval to find offset for long data
		if (swap_data(B_UINT32_TYPE, &entry.longval, 4, swp) != B_OK)
			finitStatus = B_ERROR;
		else {
			ssize_t read;
			read = io.ReadAt(entry.longval, fpLong, entry.count * 4);
			if (read != static_cast<ssize_t>(entry.count) * 4)
				finitStatus = B_IO_ERROR;
			else
				finitStatus = B_OK;
		}
		
	}
	
	// If long values were successfully read in, swap them to
	// the correct byte order
	if (finitStatus == B_OK &&
		swap_data(B_UINT32_TYPE, fpLong, entry.count * 4, swp) != B_OK)
		finitStatus = B_ERROR;
}