<commit_before>#include "fitz.h"

int
fz_read(fz_stream *stm, unsigned char *buf, int len)
{
	int count, n;

	count = MIN(len, stm->wp - stm->rp);
	if (count)
	{
		memcpy(buf, stm->rp, count);
		stm->rp += count;
	}

	if (count == len || stm->error || stm->eof)
		return count;

	assert(stm->rp == stm->wp);

	if (len - count < stm->ep - stm->bp)
	{
		n = stm->read(stm, stm->bp, stm->ep - stm->bp);
		if (n < 0)
		{
			stm->error = 1;
			return fz_rethrow(n, "read error");
		}
		else if (n == 0)
		{
			stm->eof = 1;
		}
		else if (n > 0)
		{
			stm->rp = stm->bp;
			stm->wp = stm->bp + n;
			stm->pos += n;
		}

		n = MIN(len - count, stm->wp - stm->rp);
		if (n)
		{
			memcpy(buf + count, stm->rp, n);
			stm->rp += n;
			count += n;
		}
	}
	else
	{
		n = stm->read(stm, buf + count, len - count);
		if (n < 0)
		{
			stm->error = 1;
			return fz_rethrow(n, "read error");
		}
		else if (n == 0)
		{
			stm->eof = 1;
		}
		else if (n > 0)
		{
			stm->pos += n;
			count += n;
		}
	}

	return count;
}

void
fz_fillbuffer(fz_stream *stm)
{
	int n;

	assert(stm->rp == stm->wp);

	if (stm->error || stm->eof)
		return;

	n = stm->read(stm, stm->bp, stm->ep - stm->bp);
	if (n < 0)
	{
		stm->error = 1;
		fz_catch(n, "read error; treating as end of file");
	}
	else if (n == 0)
	{
		stm->eof = 1;
	}
	else if (n > 0)
	{
		stm->rp = stm->bp;
		stm->wp = stm->bp + n;
		stm->pos += n;
	}
}

fz_error
fz_readall(fz_buffer **bufp, fz_stream *stm, int initial)
{
	fz_buffer *buf;
	int n;

	if (initial < 1024)
		initial = 1024;

	buf = fz_newbuffer(initial);

	while (1)
	{
		if (buf->len == buf->cap)
			fz_growbuffer(buf);

		if (buf->len > initial * 200)
		{
			fz_dropbuffer(buf);
			return fz_throw("compression bomb detected");
		}<commit_msg>Fix integer overflow in compression bomb multiplication by using a division instead.<commit_after>#include "fitz.h"

int
fz_read(fz_stream *stm, unsigned char *buf, int len)
{
	int count, n;

	count = MIN(len, stm->wp - stm->rp);
	if (count)
	{
		memcpy(buf, stm->rp, count);
		stm->rp += count;
	}

	if (count == len || stm->error || stm->eof)
		return count;

	assert(stm->rp == stm->wp);

	if (len - count < stm->ep - stm->bp)
	{
		n = stm->read(stm, stm->bp, stm->ep - stm->bp);
		if (n < 0)
		{
			stm->error = 1;
			return fz_rethrow(n, "read error");
		}
		else if (n == 0)
		{
			stm->eof = 1;
		}
		else if (n > 0)
		{
			stm->rp = stm->bp;
			stm->wp = stm->bp + n;
			stm->pos += n;
		}

		n = MIN(len - count, stm->wp - stm->rp);
		if (n)
		{
			memcpy(buf + count, stm->rp, n);
			stm->rp += n;
			count += n;
		}
	}
	else
	{
		n = stm->read(stm, buf + count, len - count);
		if (n < 0)
		{
			stm->error = 1;
			return fz_rethrow(n, "read error");
		}
		else if (n == 0)
		{
			stm->eof = 1;
		}
		else if (n > 0)
		{
			stm->pos += n;
			count += n;
		}
	}

	return count;
}

void
fz_fillbuffer(fz_stream *stm)
{
	int n;

	assert(stm->rp == stm->wp);

	if (stm->error || stm->eof)
		return;

	n = stm->read(stm, stm->bp, stm->ep - stm->bp);
	if (n < 0)
	{
		stm->error = 1;
		fz_catch(n, "read error; treating as end of file");
	}
	else if (n == 0)
	{
		stm->eof = 1;
	}
	else if (n > 0)
	{
		stm->rp = stm->bp;
		stm->wp = stm->bp + n;
		stm->pos += n;
	}
}

fz_error
fz_readall(fz_buffer **bufp, fz_stream *stm, int initial)
{
	fz_buffer *buf;
	int n;

	if (initial < 1024)
		initial = 1024;

	buf = fz_newbuffer(initial);

	while (1)
	{
		if (buf->len == buf->cap)
			fz_growbuffer(buf);

		if (buf->len / 200 > initial)
		{
			fz_dropbuffer(buf);
			return fz_throw("compression bomb detected");
		}
{'fraction_non_alphanumeric': 0.20168302945301542, 'fraction_numerical': 0.01514726507713885, 'mean_word_length': 2.396190476190476, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 105, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6224456', 'n_tokens_mistral': 1927, 'n_tokens_neox': 1700, 'n_words': 401}