Example #1
0
size_t ZipFile::findCentralDirectoryEnd(SeekableReadStream &zip) {
	const size_t uSizeFile = zip.size();
	const size_t uMaxBack  = MIN<size_t>(0xFFFF, uSizeFile); // Maximum size of global comment

	byte buf[BUFREADCOMMENT + 4];

	size_t uPosFound = 0;
	size_t uBackRead = 4;
	while ((uPosFound == 0) && (uBackRead < uMaxBack)) {
		uBackRead = MIN<size_t>(uMaxBack, uBackRead + BUFREADCOMMENT);

		const size_t uReadPos  = uSizeFile - uBackRead;
		const size_t uReadSize = MIN<size_t>(BUFREADCOMMENT + 4, uSizeFile - uReadPos);

		try {
			zip.seek(uReadPos);
		} catch (...) {
			return 0;
		}

		if (zip.read(buf, uReadSize) != uReadSize) {
			uPosFound = 0;
			break;
		}

		for (size_t i = (uReadSize - 3); i-- > 0; )
			if (READ_LE_UINT32(buf + i) == 0x06054B50) {
				uPosFound = uReadPos + i;
				break;
			}

	}

	return uPosFound;
}
Example #2
0
void printDataHex(SeekableReadStream &stream, size_t size) {
	size_t pos = stream.pos();

	size = MIN<size_t>(stream.size() - pos, size);

	if (size == 0)
		return;

	uint32 offset = 0;
	byte rowData[16];

	while (size > 0) {
		// At max 16 bytes printed per row
		uint32 n = MIN<size_t>(size, 16);
		if (stream.read(rowData, n) != n)
			throw Exception(kReadError);

		// Print an offset
		std::fprintf(stderr, "%08X  ", offset);

		// 2 "blobs" of each 8 bytes per row
		for (uint32 i = 0; i < 2; i++) {
			for (uint32 j = 0; j < 8; j++) {
				uint32 m = i * 8 + j;

				if (m < n)
					// Print the data
					std::fprintf(stderr, "%02X ", rowData[m]);
				else
					// Last row, data count not aligned to 16
					std::fprintf(stderr, "   ");
			}

			// Separate the blobs by an extra space
			std::fprintf(stderr, " ");
		}

		std::fprintf(stderr, "|");

		// If the data byte is a printable character, print it. If not, substitute a '.'
		for (uint32 i = 0; i < n; i++)
			std::fprintf(stderr, "%c", std::isprint(rowData[i]) ? rowData[i] : '.');

		std::fprintf(stderr, "|\n");

		size   -= n;
		offset += n;
	}

	// Seek back
	stream.seek(pos);
}
Example #3
0
void StreamTokenizer::nextChunk(SeekableReadStream &stream) {
	skipChunk(stream);

	byte c = stream.readByte();

	if (stream.eos() || stream.err())
		return;

	if (!isIn(c, _chunkEnds))
		stream.seek(-1, SEEK_CUR);
	else
		if (stream.pos() == stream.size())
			// This actually the last character, read one more byte to properly set the EOS state
			stream.readByte();
}
Example #4
0
size_t searchBackwards(SeekableReadStream &haystack, const byte *needle, size_t needleSize,
                       size_t maxReadBack) {

	if (needleSize == 0 || maxReadBack == 0)
		return SIZE_MAX;

	assert(maxReadBack >= needleSize);

	static const size_t kReadBufferSize = 0x400;

	const size_t sizeFile = haystack.size();
	const size_t maxBack  = MIN<size_t>(maxReadBack, sizeFile);

	ScopedArray<byte> buf(new byte[kReadBufferSize + needleSize]);

	size_t backRead = needleSize;
	while (backRead < maxBack) {
		backRead = MIN<size_t>(maxBack, backRead + kReadBufferSize);

		const size_t readPos  = sizeFile - backRead;
		const size_t readSize = MIN<size_t>(kReadBufferSize + needleSize, sizeFile - readPos);

		try {
			haystack.seek(readPos);
		} catch (...) {
			break;
		}

		if (haystack.read(buf.get(), readSize) != readSize)
			break;

		for (size_t i = (readSize - (needleSize - 1)); i-- > 0; )
			if (!memcmp(buf.get() + i, needle, needleSize))
				return readPos + i;
	}

	return SIZE_MAX;
}