<commit_before>///////////////////////////////////////////////////////////////////////////////
//
/// \file       delta_coder.c
/// \brief      Encoder and decoder for the Delta filter
//
//  Copyright (C) 2007 <NAME>
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
///////////////////////////////////////////////////////////////////////////////

#include "delta_coder.h"


struct lzma_coder_s {
	/// Next coder in the chain
	lzma_next_coder next;

	/// Uncompressed size - This is needed when we are the last
	/// filter in the chain.
	lzma_vli uncompressed_size;

	/// Delta distance
	size_t distance;

	/// True if we are encoding; false if decoding
	bool is_encoder;

	/// Position in history[]
	uint8_t pos;

	/// Buffer to hold history of the original data
	uint8_t history[LZMA_DELTA_DISTANCE_MAX];
};


static void
encode_buffer(lzma_coder *coder, uint8_t *buffer, size_t size)
{
	const size_t distance = coder->distance;

	for (size_t i = 0; i < size; ++i) {
		const uint8_t tmp = coder->history[
				(distance + coder->pos) & 0xFF];
		coder->history[coder->pos--] = buffer[i];
		buffer[i] -= tmp;
	}

	return;
}


static void
decode_buffer(lzma_coder *coder, uint8_t *buffer, size_t size)
{
	const size_t distance = coder->distance;

	for (size_t i = 0; i < size; ++i) {
		buffer[i] += coder->history[(distance + coder->pos) & 0xFF];
		coder->history[coder->pos--] = buffer[i];
	}

	return;
}


static lzma_ret
delta_code(lzma_coder *coder, lzma_allocator *allocator,
		const uint8_t *restrict in, size_t *restrict in_pos,
		size_t in_size, uint8_t *restrict out,
		size_t *restrict out_pos, size_t out_size, lzma_action action)
{
	const size_t out_start = *out_pos;
	size_t size;
	lzma_ret ret;

	if (coder->next.code == NULL) {
		const size_t in_avail = in_size - *in_pos;

		if (coder->is_encoder) {
			// Check that we don't have too much input.
			if ((lzma_vli)(in_avail) > coder->uncompressed_size)
				return LZMA_DATA_ERROR;

			// Check that once LZMA_FINISH has been given, the
			// amount of input matches uncompressed_size if it
			// is known.
			if (action == LZMA_FINISH && coder->uncompressed_size
						!= LZMA_VLI_VALUE_UNKNOWN
					&& coder->uncompressed_size
						!= (lzma_vli)(in_avail))
				return LZMA_DATA_ERROR;

		} else {
			// Limit in_size so that we don't copy too much.
			if ((lzma_vli)(in_avail) > coder->uncompressed_size)<commit_msg>Removed uncompressed size tracking from Delta encoder too.<commit_after>///////////////////////////////////////////////////////////////////////////////
//
/// \file       delta_coder.c
/// \brief      Encoder and decoder for the Delta filter
//
//  Copyright (C) 2007 <NAME>
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
///////////////////////////////////////////////////////////////////////////////

#include "delta_coder.h"


struct lzma_coder_s {
	/// Next coder in the chain
	lzma_next_coder next;

	/// Uncompressed size - This is needed when we are the last
	/// filter in the chain.
	lzma_vli uncompressed_size;

	/// Delta distance
	size_t distance;

	/// True if we are encoding; false if decoding
	bool is_encoder;

	/// Position in history[]
	uint8_t pos;

	/// Buffer to hold history of the original data
	uint8_t history[LZMA_DELTA_DISTANCE_MAX];
};


static void
encode_buffer(lzma_coder *coder, uint8_t *buffer, size_t size)
{
	const size_t distance = coder->distance;

	for (size_t i = 0; i < size; ++i) {
		const uint8_t tmp = coder->history[
				(distance + coder->pos) & 0xFF];
		coder->history[coder->pos--] = buffer[i];
		buffer[i] -= tmp;
	}

	return;
}


static void
decode_buffer(lzma_coder *coder, uint8_t *buffer, size_t size)
{
	const size_t distance = coder->distance;

	for (size_t i = 0; i < size; ++i) {
		buffer[i] += coder->history[(distance + coder->pos) & 0xFF];
		coder->history[coder->pos--] = buffer[i];
	}

	return;
}


static lzma_ret
delta_code(lzma_coder *coder, lzma_allocator *allocator,
		const uint8_t *restrict in, size_t *restrict in_pos,
		size_t in_size, uint8_t *restrict out,
		size_t *restrict out_pos, size_t out_size, lzma_action action)
{
	const size_t out_start = *out_pos;
	size_t size;
	lzma_ret ret;

	if (coder->next.code == NULL) {
		if (!coder->is_encoder) {
			// Limit in_size so that we don't copy too much.
			if ((lzma_vli)(in_size - *in_pos)
					> coder->uncompressed_size)
{'fraction_non_alphanumeric': 0.16304553183449838, 'fraction_numerical': 0.006423578310976762, 'mean_word_length': 2.6560773480662982, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 37, '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': '4490931', 'n_tokens_mistral': 1896, 'n_tokens_neox': 1762, 'n_words': 570}