<commit_before>/* flac - Command-line FLAC encoder/decoder
 * Copyright (C) 2000,2001  <NAME>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#if defined _WIN32 && !defined __CYGWIN__
/* where MSVC puts unlink() */
# include <io.h>
#else
# include <unistd.h>
#endif
#include <assert.h> /* for FILE */
#include <stdio.h> /* for FILE */
#include <string.h> /* for strcmp() */
#include "FLAC/all.h"
#include "decode.h"

typedef struct {
	FILE *fout;
	bool abort_flag;
	bool analysis_mode;
	bool test_only;
	bool is_wave_out;
	bool is_big_endian;
	bool is_unsigned_samples;
	uint64 total_samples;
	unsigned bps;
	unsigned channels;
	unsigned sample_rate;
	bool verbose;
	uint64 skip;
	uint64 samples_processed;
	unsigned frame_counter;
} stream_info_struct;

static FLAC__FileDecoder *decoder;
static bool is_big_endian_host;

/* local routines */
static bool init(const char *infile, stream_info_struct *stream_info);
static bool write_little_endian_uint16(FILE *f, uint16 val);
static bool write_little_endian_uint32(FILE *f, uint32 val);
static FLAC__StreamDecoderWriteStatus write_callback(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const int32 *buffer[], void *client_data);
static void metadata_callback(const FLAC__FileDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data);
static void error_callback(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
static void print_stats(const stream_info_struct *stream_info);

int decode_wav(const char *infile, const char *outfile, bool analysis_mode, bool verbose, uint64 skip)
{
	bool md5_failure = false;
	stream_info_struct stream_info;

	decoder = 0;
	stream_info.abort_flag = false;
	stream_info.analysis_mode = analysis_mode;
	stream_info.test_only = (outfile == 0);
	stream_info.is_wave_out = true;
	stream_info.verbose = verbose;
	stream_info.skip = skip;
	stream_info.samples_processed = 0;
	stream_info.frame_counter = 0;

	assert(!(test_only && analysis_mode));

	if(!stream_info.test_only) {
		if(0 == strcmp(outfile, "-")) {
			stream_info.fout = stdout;
		}
		else {
			if(0 == (stream_info.fout = fopen(outfile, "wb"))) {
				fprintf(stderr, "ERROR: can't open output file %s\n", outfile);
				return false;
			}
		}
	}

	if(!init(infile, &stream_info))
		goto wav_abort_;

	if(skip > 0) {
		if(!FLAC__file_decoder_process_metadata(decoder)) {
			fprintf(stderr, "%s: ERROR during decoding\n", infile);
			goto wav_abort_;
		}
		if(!FLAC__file_decoder_seek_absolute(decoder, skip)) {
			fprintf(stderr, "%s: ERROR seeking while skipping bytes\n", infile);
			goto wav_abort_;
		}
		if(!FLAC__file_decoder_process_remaining_frames(decoder)) {
			if(verbose) { printf("\n"); fflush(stdout); }
			fprintf(stderr, "%s: ERROR during decoding\n", infile);
			goto wav_abort_;
		}
		if(decoder->state != FLAC__FILE_DECODER_OK && decoder->state != FLAC__FILE_DECODER_END_OF_FILE) {
			if(verbose) { printf("\n"); fflush(stdout); }
			fprintf(stderr, "%s: ERROR during decoding\n", infile);
			goto wav_abort_;
		}
	}
	else {
		if(!FLAC__file_decoder_process_whole_file(decoder)) {
			if(verbose) { printf("\n"); fflush(stdout); }
			fprintf(stderr, "%s: ERROR during decoding\n", infile);
			goto wav_abort_;
		}
		if(decoder->state != FLAC__FILE_DECODER_OK && decoder->state != FLAC__FILE_DECODER_END_OF_FILE) {
			if(verbose) { printf("\n"); fflush(stdout); }
			fprintf(stderr, "%s: ERROR during decoding, state=%d:%s\n", infile, decoder->state, FLAC__FileDecoderStateString[decoder->state]);
			goto wav_abort_;
		}
	}

	if(decoder) {
		if(decoder->state != FLAC__FILE_DECODER_UNINITIALIZED)
			md5_failure = !FLAC__file_decoder_finish(decoder);
		print_stats(&stream_info);
		FLAC__file_decoder_free_instance(decoder);
	}
	if(!stream_info.test_only)
		fclose(stream_info.fout);
	if(verbose)
		printf("\n");
	fflush(stdout);
	if(md5_failure) {
		fprintf(stderr, "%s: WARNING, MD5 signature mismatch\n", infile);
		return 1;
	}
	else {
		if(stream_info.test_only)
			printf("%s: ok\n", infile);
	}
	return 0;
wav_abort_:
	if(decoder) {
		if(decoder->state != FLAC__FILE_DECODER_UNINITIALIZED)
			FLAC__file_decoder_finish(decoder);
		FLAC__file_decoder_free_instance(decoder);
	}
	if(!stream_info.test_only) {
		fclose(stream_info.fout);
		unlink(outfile);
	}
	return 1;
}

int decode_raw(const char *infile, const char *outfile, bool analysis_mode, bool verbose, uint64 skip, bool is_big_endian, bool is_unsigned_samples)
{
	bool md5_failure = false;
	stream_info_struct stream_info;

	decoder = 0;
	stream_info.abort_flag = false;
	stream_info.analysis_mode = analysis_mode;
	stream_info.test_only = (outfile == 0);
	stream_info.is_wave_out = false;
	stream_info.is_big_endian = is_big_endian;
	stream_info.is_unsigned_samples = is_unsigned_samples;
	stream_info.verbose = verbose;
	stream_info.skip = skip;
	stream_info.samples_processed = 0;
	stream_info.frame_counter = 0;

	assert(!(test_only && analysis_mode));

	if(!stream_info.test_only) {
		if(0 == strcmp(outfile, "-")) {<commit_msg>fix bug with asserts<commit_after>/* flac - Command-line FLAC encoder/decoder
 * Copyright (C) 2000,2001  <NAME>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#if defined _WIN32 && !defined __CYGWIN__
/* where MSVC puts unlink() */
# include <io.h>
#else
# include <unistd.h>
#endif
#include <assert.h> /* for FILE */
#include <stdio.h> /* for FILE */
#include <string.h> /* for strcmp() */
#include "FLAC/all.h"
#include "decode.h"

typedef struct {
	FILE *fout;
	bool abort_flag;
	bool analysis_mode;
	bool test_only;
	bool is_wave_out;
	bool is_big_endian;
	bool is_unsigned_samples;
	uint64 total_samples;
	unsigned bps;
	unsigned channels;
	unsigned sample_rate;
	bool verbose;
	uint64 skip;
	uint64 samples_processed;
	unsigned frame_counter;
} stream_info_struct;

static FLAC__FileDecoder *decoder;
static bool is_big_endian_host;

/* local routines */
static bool init(const char *infile, stream_info_struct *stream_info);
static bool write_little_endian_uint16(FILE *f, uint16 val);
static bool write_little_endian_uint32(FILE *f, uint32 val);
static FLAC__StreamDecoderWriteStatus write_callback(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const int32 *buffer[], void *client_data);
static void metadata_callback(const FLAC__FileDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data);
static void error_callback(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
static void print_stats(const stream_info_struct *stream_info);

int decode_wav(const char *infile, const char *outfile, bool analysis_mode, bool verbose, uint64 skip)
{
	bool md5_failure = false;
	stream_info_struct stream_info;

	decoder = 0;
	stream_info.abort_flag = false;
	stream_info.analysis_mode = analysis_mode;
	stream_info.test_only = (outfile == 0);
	stream_info.is_wave_out = true;
	stream_info.verbose = verbose;
	stream_info.skip = skip;
	stream_info.samples_processed = 0;
	stream_info.frame_counter = 0;

	assert(!(stream_info.test_only && stream_info.analysis_mode));

	if(!stream_info.test_only) {
		if(0 == strcmp(outfile, "-")) {
			stream_info.fout = stdout;
		}
		else {
			if(0 == (stream_info.fout = fopen(outfile, "wb"))) {
				fprintf(stderr, "ERROR: can't open output file %s\n", outfile);
				return false;
			}
		}
	}

	if(!init(infile, &stream_info))
		goto wav_abort_;

	if(skip > 0) {
		if(!FLAC__file_decoder_process_metadata(decoder)) {
			fprintf(stderr, "%s: ERROR during decoding\n", infile);
			goto wav_abort_;
		}
		if(!FLAC__file_decoder_seek_absolute(decoder, skip)) {
			fprintf(stderr, "%s: ERROR seeking while skipping bytes\n", infile);
			goto wav_abort_;
		}
		if(!FLAC__file_decoder_process_remaining_frames(decoder)) {
			if(verbose) { printf("\n"); fflush(stdout); }
			fprintf(stderr, "%s: ERROR during decoding\n", infile);
			goto wav_abort_;
		}
		if(decoder->state != FLAC__FILE_DECODER_OK && decoder->state != FLAC__FILE_DECODER_END_OF_FILE) {
			if(verbose) { printf("\n"); fflush(stdout); }
			fprintf(stderr, "%s: ERROR during decoding\n", infile);
			goto wav_abort_;
		}
	}
	else {
		if(!FLAC__file_decoder_process_whole_file(decoder)) {
			if(verbose) { printf("\n"); fflush(stdout); }
			fprintf(stderr, "%s: ERROR during decoding\n", infile);
			goto wav_abort_;
		}
		if(decoder->state != FLAC__FILE_DECODER_OK && decoder->state != FLAC__FILE_DECODER_END_OF_FILE) {
			if(verbose) { printf("\n"); fflush(stdout); }
			fprintf(stderr, "%s: ERROR during decoding, state=%d:%s\n", infile, decoder->state, FLAC__FileDecoderStateString[decoder->state]);
			goto wav_abort_;
		}
	}

	if(decoder) {
		if(decoder->state != FLAC__FILE_DECODER_UNINITIALIZED)
			md5_failure = !FLAC__file_decoder_finish(decoder);
		print_stats(&stream_info);
		FLAC__file_decoder_free_instance(decoder);
	}
	if(!stream_info.test_only)
		fclose(stream_info.fout);
	if(verbose)
		printf("\n");
	fflush(stdout);
	if(md5_failure) {
		fprintf(stderr, "%s: WARNING, MD5 signature mismatch\n", infile);
		return 1;
	}
	else {
		if(stream_info.test_only)
			printf("%s: ok\n", infile);
	}
	return 0;
wav_abort_:
	if(decoder) {
		if(decoder->state != FLAC__FILE_DECODER_UNINITIALIZED)
			FLAC__file_decoder_finish(decoder);
		FLAC__file_decoder_free_instance(decoder);
	}
	if(!stream_info.test_only) {
		fclose(stream_info.fout);
		unlink(outfile);
	}
	return 1;
}

int decode_raw(const char *infile, const char *outfile, bool analysis_mode, bool verbose, uint64 skip, bool is_big_endian, bool is_unsigned_samples)
{
	bool md5_failure = false;
	stream_info_struct stream_info;

	decoder = 0;
	stream_info.abort_flag = false;
	stream_info.analysis_mode = analysis_mode;
	stream_info.test_only = (outfile == 0);
	stream_info.is_wave_out = false;
	stream_info.is_big_endian = is_big_endian;
	stream_info.is_unsigned_samples = is_unsigned_samples;
	stream_info.verbose = verbose;
	stream_info.skip = skip;
	stream_info.samples_processed = 0;
	stream_info.frame_counter = 0;

	assert(!(stream_info.test_only && stream_info.analysis_mode));

	if(!stream_info.test_only) {
		if(0 == strcmp(outfile, "-")) {
{'fraction_non_alphanumeric': 0.11565634539904056, 'fraction_numerical': 0.0113388573920628, 'mean_word_length': 5.204545454545454, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 33, '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': '6228308', 'n_tokens_mistral': 4303, 'n_tokens_neox': 3965, 'n_words': 971}