Exemplo n.º 1
0
uint128_t stream_md5( stream_t* stream )
{
	int64_t cur, ic, offset, lastc, num;
	md5_t* md5;
	uint128_t ret = {0};
	unsigned char buf[1025];
	bool ignore_lf = false;

	FOUNDATION_ASSERT( stream );
	if( stream->vtable->md5 )
		return stream->vtable->md5( stream );

	if( !stream || stream_is_sequential( stream ) || !( stream->mode & STREAM_IN ) )
		return ret;

	FOUNDATION_ASSERT( stream->vtable->read );

	cur = stream_tell( stream );
	stream_seek( stream, 0, STREAM_SEEK_BEGIN );

	md5 = md5_allocate();

	while( !stream_eos( stream ) )
	{
		num = (int64_t)stream->vtable->read( stream, buf, 1024 );
		if( !num )
			continue;
		if( stream->mode & STREAM_BINARY )
			md5_digest_raw( md5, buf, (size_t)num );
		else
		{
			//If last buffer ended with CR, ignore a leading LF
			offset = 0;
			lastc = 0;
			if( ignore_lf && ( buf[0] == '\n' ) )
				offset = 1;
			ignore_lf = false;

			//Treat all line endings (LF, CR, CR+LF) as Unix style LF. If file has mixed line endings (for example, one line ending in a single CR and next ending in a single LF), it will not work!
			for( ic = 0; ic < num; ++ic )
			{
				bool was_cr = ( buf[ic] == '\r' );
				bool was_lf = ( buf[ic] == '\n' );
				if( was_cr || was_lf )
				{
					if( was_cr && ( ic >= 1023 ) )
						ignore_lf = true;
					buf[ic] = '\n';
					md5_digest_raw( md5, buf + lastc, (size_t)( ic - lastc + 1 ) ); //Include the LF
					if( was_cr && ( buf[ic+1] == '\n' ) ) //Check for CR+LF
						++ic;
					lastc = ic + 1;
				}
			}
			if( lastc < num )
				md5_digest_raw( md5, buf + lastc, (size_t)( num - lastc ) );
		}
	}
	
	stream_seek( stream, cur, STREAM_SEEK_BEGIN );

	md5_finalize( md5 );
	ret = md5_get_digest_raw( md5 );

	md5_deallocate( md5 );

	return ret;
}
Exemplo n.º 2
0
uint128_t
stream_md5(stream_t* stream) {
	size_t cur, ic, lastc, num, limit;
	md5_t md5;
	uint128_t ret = uint128_null();
	unsigned char buf[1025];
	bool ignore_lf = false;

	if (stream->vtable->md5)
		return stream->vtable->md5(stream);

	if (stream_is_sequential(stream) || !(stream->mode & STREAM_IN))
		return ret;

	cur = stream_tell(stream);
	stream_seek(stream, 0, STREAM_SEEK_BEGIN);

	md5_initialize(&md5);
	limit = sizeof(buf)-1;
	buf[limit] = 0;

	while (!stream_eos(stream)) {
		num = stream->vtable->read(stream, buf, limit);
		if (!num)
			continue;
		if (stream->mode & STREAM_BINARY)
			md5_digest(&md5, buf, (size_t)num);
		else {
			//If last buffer ended with CR, ignore a leading LF
			lastc = 0;
			if (ignore_lf && (buf[0] == '\n'))
				lastc = 1;
			ignore_lf = false;

			//Digest one line at a time
			//Treat all line endings (LF, CR, CR+LF) as Unix style LF. If file has mixed line endings
			//(for example, one line ending in a single CR and next is empty and ending in a single LF),
			//it will not work!
			/*lint -e{850} */
			for (ic = lastc; ic < num && ic < limit; ++ic) {
				bool was_cr = (buf[ic] == '\r');
				bool was_lf = (buf[ic] == '\n');
				if (was_cr || was_lf) {
					if (was_cr && (ic == limit-1))
						ignore_lf = true; //Make next buffer ignore leading LF as it is part of CR+LF
					buf[ic] = '\n';
					md5_digest(&md5, buf + lastc, (size_t)((ic - lastc) + 1)); //Include the LF
					if (was_cr && (buf[ic + 1] == '\n'))  //Check for CR+LF
						++ic;
					lastc = ic + 1;
				}
			}
			if (lastc < num)
				md5_digest(&md5, buf + lastc, (size_t)(num - lastc));
		}
	}

	stream_seek(stream, (ssize_t)cur, STREAM_SEEK_BEGIN);

	md5_digest_finalize(&md5);
	ret = md5_get_digest_raw(&md5);
	md5_finalize(&md5);

	return ret;
}