<commit_before>/*  Molch, an implementation of the axolotl ratchet based on libsodium
 *  Copyright (C) 2015-2016 1984not Security GmbH
 *  Author: <NAME> (FSMaxB)
 *
 *  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.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
#include <stdio.h>
#include <stdlib.h>
#include <sodium.h>

#include "utils.h"

void print_hex(const buffer_t * const data) {
	static const int WIDTH = 30;
	//buffer for hex string
	buffer_t *hex = buffer_create_on_heap(2 * data->content_length + 1, 2 * data->content_length + 1);

	if (buffer_clone_as_hex(hex, data) != 0) {
		fprintf(stderr, "ERROR: Failed printing hex.\n");
		buffer_destroy_from_heap(hex);
		return;
	}

	for (size_t i = 0; i < 2 * data->content_length; i++) {
		if ((WIDTH != 0) && (i % WIDTH == 0) && (i != 0)) {
			putchar('\n');
		} else if ((i % 2 == 0) && (i != 0)) {
			putchar(' ');
		}
		putchar(hex->content[i]);
	}

	putchar('\n');

	//cleanup
	buffer_destroy_from_heap(hex);
}

void print_to_file(const buffer_t * const data, const char * const filename) {
	FILE *file = fopen(filename, "w");
	if (file == NULL) {
		return;
	}

	fprintf(file, "%.*s", (int)data->content_length, (char*)data->content);

	fclose(file);
}

void print_errors(return_status * const status) {
	if (status == NULL) {
		return;
	}

	fprintf(stderr, "ERROR STACK:\n");
	error_message *error = status->error;
	for (size_t i = 1; error != NULL; i++, error = error->next) {
		fprintf(stderr, "%zu: %s\n", i, error->message);
	}
}

void read_file(buffer_t ** const data, const char * const filename) {
	if (data == NULL) {
		return;
	}

	*data = NULL;

	FILE *file = NULL;

	file = fopen(filename, "r");

	//get the filesize<commit_msg>utils: print_to_file: enable writing binary data<commit_after>/*  Molch, an implementation of the axolotl ratchet based on libsodium
 *  Copyright (C) 2015-2016 1984not Security GmbH
 *  Author: <NAME> (FSMaxB)
 *
 *  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.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
#include <stdio.h>
#include <stdlib.h>
#include <sodium.h>

#include "utils.h"

void print_hex(const buffer_t * const data) {
	static const int WIDTH = 30;
	//buffer for hex string
	buffer_t *hex = buffer_create_on_heap(2 * data->content_length + 1, 2 * data->content_length + 1);

	if (buffer_clone_as_hex(hex, data) != 0) {
		fprintf(stderr, "ERROR: Failed printing hex.\n");
		buffer_destroy_from_heap(hex);
		return;
	}

	for (size_t i = 0; i < 2 * data->content_length; i++) {
		if ((WIDTH != 0) && (i % WIDTH == 0) && (i != 0)) {
			putchar('\n');
		} else if ((i % 2 == 0) && (i != 0)) {
			putchar(' ');
		}
		putchar(hex->content[i]);
	}

	putchar('\n');

	//cleanup
	buffer_destroy_from_heap(hex);
}

void print_to_file(const buffer_t * const data, const char * const filename) {
	FILE *file = fopen(filename, "w");
	if (file == NULL) {
		return;
	}

	fwrite(data->content, 1, data->content_length, file);

	fclose(file);
}

void print_errors(return_status * const status) {
	if (status == NULL) {
		return;
	}

	fprintf(stderr, "ERROR STACK:\n");
	error_message *error = status->error;
	for (size_t i = 1; error != NULL; i++, error = error->next) {
		fprintf(stderr, "%zu: %s\n", i, error->message);
	}
}

void read_file(buffer_t ** const data, const char * const filename) {
	if (data == NULL) {
		return;
	}

	*data = NULL;

	FILE *file = NULL;

	file = fopen(filename, "r");

	//get the filesize
{'fraction_non_alphanumeric': 0.13330520987133515, 'fraction_numerical': 0.017506855093862055, 'mean_word_length': 3.7467467467467466, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 29, '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': '1591329', 'n_tokens_mistral': 1716, 'n_tokens_neox': 1593, 'n_words': 562}