<commit_before>/*
   ________        _____     ____________
   ___  __ )____  ___  /_______  ___/_  /__________________ _______ ___
   __  __  |_  / / /  __/  _ \____ \_  __/_  ___/  _ \  __ `/_  __ `__ \
   _  /_/ /_  /_/ // /_ /  __/___/ // /_ _  /   /  __/ /_/ /_  / / / / /
   /_____/ _\__, / \__/ \___//____/ \__/ /_/    \___/\__,_/ /_/ /_/ /_/
           /____/

   Byte stream manipulation library.
   Copyright (C) 2013  <NAME> <<EMAIL>>

   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 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.

   A copy of the GNU Lesser General Public License is available within
   COPYING.LGPL; alternatively write to the Free Software Foundation, Inc.,
   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include "bs.h"
#include "bs_alloc.h"
#include <stdlib.h>

BSresult
bs_load_binary(BS *bs, const unsigned char *data, size_t length)
{
	size_t ibStream;
	BSresult result;

	result = bs_malloc(bs, length);
	if (result != BS_OK) {
		return result;
	}

	for (ibStream = 0; ibStream < length; ibStream++) {
		bs->pbBytes[ibStream] = data[ibStream];
	}

	return BS_OK;
}

BSresult
bs_save_binary(const BS *bs, unsigned char **data, size_t *length)
{
	size_t ibStream;

	*length = bs_size(bs);
	if (*length == 0) {
		*data = NULL;
		return BS_OK;
	}

	*data = malloc(*length * sizeof(**data));
	if (*data == NULL) {
		*length = 0;
		return BS_MEMORY;
	}

	for (ibStream = 0; ibStream < bs_size(bs); ibStream++) {
		(*data)[ibStream] = bs->pbBytes[ibStream];
	}

	return BS_OK;
}

BSresult
bs_load_string(BS *bs, const char *string, size_t length)
{
	size_t ibStream;
	BSresult result;

	result = bs_malloc(bs, length);
	if (result != BS_OK) {
		return result;
	}

	for (ibStream = 0; ibStream < length; ibStream++) {
		if (string[ibStream] < 0) { /* Negative values not allowed */
			bs_malloc(bs, 0);
			return BS_INVALID;
		}
		bs->pbBytes[ibStream] = (BSbyte)string[ibStream];
	}

	return BS_OK;
}
<commit_msg>Modified bs_save_binary to use the new bs_malloc_output helper.<commit_after>/*
   ________        _____     ____________
   ___  __ )____  ___  /_______  ___/_  /__________________ _______ ___
   __  __  |_  / / /  __/  _ \____ \_  __/_  ___/  _ \  __ `/_  __ `__ \
   _  /_/ /_  /_/ // /_ /  __/___/ // /_ _  /   /  __/ /_/ /_  / / / / /
   /_____/ _\__, / \__/ \___//____/ \__/ /_/    \___/\__,_/ /_/ /_/ /_/
           /____/

   Byte stream manipulation library.
   Copyright (C) 2013  <NAME> <<EMAIL>>

   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 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.

   A copy of the GNU Lesser General Public License is available within
   COPYING.LGPL; alternatively write to the Free Software Foundation, Inc.,
   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include "bs.h"
#include "bs_alloc.h"
#include <stdlib.h>

BSresult
bs_load_binary(BS *bs, const unsigned char *data, size_t length)
{
	size_t ibStream;
	BSresult result;

	result = bs_malloc(bs, length);
	if (result != BS_OK) {
		return result;
	}

	for (ibStream = 0; ibStream < length; ibStream++) {
		bs->pbBytes[ibStream] = data[ibStream];
	}

	return BS_OK;
}

BSresult
bs_save_binary(const BS *bs, unsigned char **data, size_t *length)
{
	size_t ibStream;
	BSresult result;

	result = bs_malloc_output(
		bs_size(bs) * sizeof(**data),
		(void **) data,
		length
	);
	if (result != BS_OK) {
		return result;
	}

	for (ibStream = 0; ibStream < bs_size(bs); ibStream++) {
		(*data)[ibStream] = bs->pbBytes[ibStream];
	}

	return BS_OK;
}

BSresult
bs_load_string(BS *bs, const char *string, size_t length)
{
	size_t ibStream;
	BSresult result;

	result = bs_malloc(bs, length);
	if (result != BS_OK) {
		return result;
	}

	for (ibStream = 0; ibStream < length; ibStream++) {
		if (string[ibStream] < 0) { /* Negative values not allowed */
			bs_malloc(bs, 0);
			return BS_INVALID;
		}
		bs->pbBytes[ibStream] = (BSbyte)string[ibStream];
	}

	return BS_OK;
}

{'fraction_non_alphanumeric': 0.11987381703470032, 'fraction_numerical': 0.00967402733964248, 'mean_word_length': 2.9176276771004943, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 6, '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': '298110', 'n_tokens_mistral': 1767, 'n_tokens_neox': 1636, 'n_words': 474}