Skip to content

Commit

Permalink
On WIN32, set stdout/stderr to UTF-8 (#714)
Browse files Browse the repository at this point in the history
When stderr (flac) or stdout (metaflac) is redirected to a file,
output of non-ASCII characters fails. This commit should fix that.

Fixes #713

NOTE: To enable this change, metaflac no longer outputs
application block data as binary directly, because that would
potentially result in incorrect UTF-8 codepoint. Instead, all
bytes that cannot be represented as printable ASCII are
replaced with the Unicode replacement character
  • Loading branch information
ktmf01 authored Jun 26, 2024
1 parent 706c061 commit dda3e77
Show file tree
Hide file tree
Showing 13 changed files with 450 additions and 414 deletions.
2 changes: 1 addition & 1 deletion src/flac/analyze.c
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ FLAC__bool dump_stats(const subframe_stats_t *stats, const char *filename)
outfile = flac_fopen(filename, "w");

if(0 == outfile) {
fprintf(stderr, "ERROR opening %s: %s\n", filename, strerror(errno));
flac_fprintf(stderr, "ERROR opening %s: %s\n", filename, strerror(errno));
return false;
}

Expand Down
2 changes: 2 additions & 0 deletions src/flac/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,8 @@ static int main_to_fuzz(int argc, char *argv[])
fprintf(stderr, "ERROR: failed to convert command line parameters to UTF-8\n");
return 1;
}
SetConsoleOutputCP(CP_UTF8);
_setmode(fileno(stderr),_O_U8TEXT);
#endif

srand((uint32_t)time(0));
Expand Down
8 changes: 4 additions & 4 deletions src/flac/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ void stats_new_file(void)
void stats_clear(void)
{
while (stats_char_count > 0 && stats_char_count--)
fprintf(stderr, "\b");
flac_fprintf(stderr, "\b");
}

void stats_print_name(int level, const char *name)
Expand Down Expand Up @@ -267,11 +267,11 @@ void stats_print_info(int level, const char *format, ...)
stats_clear();
if (len >= console_chars_left) {
clear_len = console_chars_left;
while (clear_len > 0 && clear_len--) fprintf(stderr, " ");
fprintf(stderr, "\n");
while (clear_len > 0 && clear_len--) flac_fprintf(stderr, " ");
flac_fprintf(stderr, "\n");
console_chars_left = console_width;
}
stats_char_count = fprintf(stderr, "%s", tmp);
stats_char_count = flac_fprintf(stderr, "%s", tmp);
fflush(stderr);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/libFLAC/cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@

#ifndef NDEBUG
#include <stdio.h>
#define dfprintf fprintf
#define dfprintf flac_fprintf
#else
/* This is bad practice, it should be a static void empty function */
#define dfprintf(file, format, ...)
Expand Down
44 changes: 22 additions & 22 deletions src/libFLAC/lpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,9 @@ int FLAC__lpc_quantize_coefficients(const FLAC__real lp_coeff[], uint32_t order,

#ifdef FLAC__OVERFLOW_DETECT
if(q > qmax+1) /* we expect q==qmax+1 occasionally due to rounding */
fprintf(stderr,"FLAC__lpc_quantize_coefficients: quantizer overflow: q>qmax %d>%d shift=%d cmax=%f precision=%u lpc[%u]=%f\n",q,qmax,*shift,cmax,precision+1,i,lp_coeff[i]);
flac_fprintf(stderr,"FLAC__lpc_quantize_coefficients: quantizer overflow: q>qmax %d>%d shift=%d cmax=%f precision=%u lpc[%u]=%f\n",q,qmax,*shift,cmax,precision+1,i,lp_coeff[i]);
else if(q < qmin)
fprintf(stderr,"FLAC__lpc_quantize_coefficients: quantizer overflow: q<qmin %d<%d shift=%d cmax=%f precision=%u lpc[%u]=%f\n",q,qmin,*shift,cmax,precision+1,i,lp_coeff[i]);
flac_fprintf(stderr,"FLAC__lpc_quantize_coefficients: quantizer overflow: q<qmin %d<%d shift=%d cmax=%f precision=%u lpc[%u]=%f\n",q,qmin,*shift,cmax,precision+1,i,lp_coeff[i]);
#endif
if(q > qmax)
q = qmax;
Expand All @@ -289,16 +289,16 @@ int FLAC__lpc_quantize_coefficients(const FLAC__real lp_coeff[], uint32_t order,
double error = 0.0;
FLAC__int32 q;
#ifndef NDEBUG
fprintf(stderr,"FLAC__lpc_quantize_coefficients: negative shift=%d order=%u cmax=%f\n", *shift, order, cmax);
flac_fprintf(stderr,"FLAC__lpc_quantize_coefficients: negative shift=%d order=%u cmax=%f\n", *shift, order, cmax);
#endif
for(i = 0; i < order; i++) {
error += lp_coeff[i] / (1 << nshift);
q = lround(error);
#ifdef FLAC__OVERFLOW_DETECT
if(q > qmax+1) /* we expect q==qmax+1 occasionally due to rounding */
fprintf(stderr,"FLAC__lpc_quantize_coefficients: quantizer overflow: q>qmax %d>%d shift=%d cmax=%f precision=%u lpc[%u]=%f\n",q,qmax,*shift,cmax,precision+1,i,lp_coeff[i]);
flac_fprintf(stderr,"FLAC__lpc_quantize_coefficients: quantizer overflow: q>qmax %d>%d shift=%d cmax=%f precision=%u lpc[%u]=%f\n",q,qmax,*shift,cmax,precision+1,i,lp_coeff[i]);
else if(q < qmin)
fprintf(stderr,"FLAC__lpc_quantize_coefficients: quantizer overflow: q<qmin %d<%d shift=%d cmax=%f precision=%u lpc[%u]=%f\n",q,qmin,*shift,cmax,precision+1,i,lp_coeff[i]);
flac_fprintf(stderr,"FLAC__lpc_quantize_coefficients: quantizer overflow: q<qmin %d<%d shift=%d cmax=%f precision=%u lpc[%u]=%f\n",q,qmin,*shift,cmax,precision+1,i,lp_coeff[i]);
#endif
if(q > qmax)
q = qmax;
Expand Down Expand Up @@ -327,10 +327,10 @@ void FLAC__lpc_compute_residual_from_qlp_coefficients(const FLAC__int32 * flac_r
const FLAC__int32 *history;

#ifdef FLAC__OVERFLOW_DETECT_VERBOSE
fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
flac_fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
for(i=0;i<order;i++)
fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
fprintf(stderr,"\n");
flac_fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
flac_fprintf(stderr,"\n");
#endif
FLAC__ASSERT(order > 0);

Expand All @@ -342,7 +342,7 @@ void FLAC__lpc_compute_residual_from_qlp_coefficients(const FLAC__int32 * flac_r
sum += qlp_coeff[j] * (*(--history));
sumo += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*history);
if(sumo > 2147483647ll || sumo < -2147483648ll)
fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients: OVERFLOW, i=%u, j=%u, c=%d, d=%d, sumo=%" PRId64 "\n",i,j,qlp_coeff[j],*history,sumo);
flac_fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients: OVERFLOW, i=%u, j=%u, c=%d, d=%d, sumo=%" PRId64 "\n",i,j,qlp_coeff[j],*history,sumo);
}
*(residual++) = *(data++) - (sum >> lp_quantization);
}
Expand Down Expand Up @@ -587,10 +587,10 @@ void FLAC__lpc_compute_residual_from_qlp_coefficients_wide(const FLAC__int32 * f
const FLAC__int32 *history;

#ifdef FLAC__OVERFLOW_DETECT_VERBOSE
fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
flac_fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
for(i=0;i<order;i++)
fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
fprintf(stderr,"\n");
flac_fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
flac_fprintf(stderr,"\n");
#endif
FLAC__ASSERT(order > 0);

Expand All @@ -600,7 +600,7 @@ void FLAC__lpc_compute_residual_from_qlp_coefficients_wide(const FLAC__int32 * f
for(j = 0; j < order; j++)
sum += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*(--history));
if(FLAC__bitmath_silog2((FLAC__int64)(*data) - (sum >> lp_quantization)) > 32) {
fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: OVERFLOW, i=%u, data=%d, sum=%" PRId64 ", residual=%" PRId64 "\n", i, *data, (int64_t)(sum >> lp_quantization), ((FLAC__int64)(*data) - (sum >> lp_quantization)));
flac_fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: OVERFLOW, i=%u, data=%d, sum=%" PRId64 ", residual=%" PRId64 "\n", i, *data, (int64_t)(sum >> lp_quantization), ((FLAC__int64)(*data) - (sum >> lp_quantization)));
break;
}
*(residual++) = *(data++) - (FLAC__int32)(sum >> lp_quantization);
Expand Down Expand Up @@ -984,10 +984,10 @@ void FLAC__lpc_restore_signal(const FLAC__int32 * flac_restrict residual, uint32
const FLAC__int32 *r = residual, *history;

#ifdef FLAC__OVERFLOW_DETECT_VERBOSE
fprintf(stderr,"FLAC__lpc_restore_signal: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
flac_fprintf(stderr,"FLAC__lpc_restore_signal: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
for(i=0;i<order;i++)
fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
fprintf(stderr,"\n");
flac_fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
flac_fprintf(stderr,"\n");
#endif
FLAC__ASSERT(order > 0);

Expand All @@ -1000,7 +1000,7 @@ void FLAC__lpc_restore_signal(const FLAC__int32 * flac_restrict residual, uint32
sumo += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*history);
#ifdef FLAC__OVERFLOW_DETECT
if(sumo > 2147483647ll || sumo < -2147483648ll)
fprintf(stderr,"FLAC__lpc_restore_signal: OVERFLOW, i=%u, j=%u, c=%d, d=%d, sumo=%" PRId64 "\n",i,j,qlp_coeff[j],*history,sumo);
flac_fprintf(stderr,"FLAC__lpc_restore_signal: OVERFLOW, i=%u, j=%u, c=%d, d=%d, sumo=%" PRId64 "\n",i,j,qlp_coeff[j],*history,sumo);
#endif
}
*(data++) = *(r++) + (sum >> lp_quantization);
Expand Down Expand Up @@ -1246,10 +1246,10 @@ void FLAC__lpc_restore_signal_wide(const FLAC__int32 * flac_restrict residual, u
const FLAC__int32 *r = residual, *history;

#ifdef FLAC__OVERFLOW_DETECT_VERBOSE
fprintf(stderr,"FLAC__lpc_restore_signal_wide: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
flac_fprintf(stderr,"FLAC__lpc_restore_signal_wide: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
for(i=0;i<order;i++)
fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
fprintf(stderr,"\n");
flac_fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
flac_fprintf(stderr,"\n");
#endif
FLAC__ASSERT(order > 0);

Expand All @@ -1260,7 +1260,7 @@ void FLAC__lpc_restore_signal_wide(const FLAC__int32 * flac_restrict residual, u
sum += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*(--history));
#ifdef FLAC__OVERFLOW_DETECT
if(FLAC__bitmath_silog2((FLAC__int64)(*r) + (sum >> lp_quantization)) > 32) {
fprintf(stderr,"FLAC__lpc_restore_signal_wide: OVERFLOW, i=%u, residual=%d, sum=%" PRId64 ", data=%" PRId64 "\n", i, *r, (sum >> lp_quantization), ((FLAC__int64)(*r) + (sum >> lp_quantization)));
flac_fprintf(stderr,"FLAC__lpc_restore_signal_wide: OVERFLOW, i=%u, residual=%d, sum=%" PRId64 ", data=%" PRId64 "\n", i, *r, (sum >> lp_quantization), ((FLAC__int64)(*r) + (sum >> lp_quantization)));
break;
}
#endif
Expand Down Expand Up @@ -1515,7 +1515,7 @@ void FLAC__lpc_restore_signal_wide_33bit(const FLAC__int32 * flac_restrict resid
sum += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*(--history));
#ifdef FLAC__OVERFLOW_DETECT
if(FLAC__bitmath_silog2((FLAC__int64)(*r) + (sum >> lp_quantization)) > 33) {
fprintf(stderr,"FLAC__lpc_restore_signal_33bit: OVERFLOW, i=%u, residual=%d, sum=%" PRId64 ", data=%" PRId64 "\n", i, *r, (sum >> lp_quantization), ((FLAC__int64)(*r) + (sum >> lp_quantization)));
flac_fprintf(stderr,"FLAC__lpc_restore_signal_33bit: OVERFLOW, i=%u, residual=%d, sum=%" PRId64 ", data=%" PRId64 "\n", i, *r, (sum >> lp_quantization), ((FLAC__int64)(*r) + (sum >> lp_quantization)));
break;
}
#endif
Expand Down
6 changes: 3 additions & 3 deletions src/libFLAC/stream_decoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -2228,14 +2228,14 @@ FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame, FL
* isn't checked, if the seek fails the decoder will continue anyway */
if(!FLAC__bitreader_rewind_to_after_last_seen_framesync(decoder->private_->input)){
#ifndef NDEBUG
fprintf(stderr, "Rewinding, seeking necessary\n");
flac_fprintf(stderr, "Rewinding, seeking necessary\n");
#endif
if(decoder->private_->seek_callback && decoder->private_->last_seen_framesync){
/* Last framesync isn't in bitreader anymore, rewind with seek if possible */
#ifndef NDEBUG
FLAC__uint64 current_decode_position;
if(FLAC__stream_decoder_get_decode_position(decoder, &current_decode_position))
fprintf(stderr, "Bitreader was %" PRIu64 " bytes short\n", current_decode_position-decoder->private_->last_seen_framesync);
flac_fprintf(stderr, "Bitreader was %" PRIu64 " bytes short\n", current_decode_position-decoder->private_->last_seen_framesync);
#endif
if(decoder->private_->seek_callback(decoder, decoder->private_->last_seen_framesync, decoder->private_->client_data) == FLAC__STREAM_DECODER_SEEK_STATUS_ERROR) {
decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
Expand All @@ -2249,7 +2249,7 @@ FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame, FL
}
#ifndef NDEBUG
else{
fprintf(stderr, "Rewinding, seeking not necessary\n");
flac_fprintf(stderr, "Rewinding, seeking not necessary\n");
}
#endif
}
Expand Down
10 changes: 5 additions & 5 deletions src/libFLAC/stream_encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -4420,19 +4420,19 @@ static void spotcheck_subframe_estimate_(
FLAC__bool ret;
FLAC__BitWriter *frame = FLAC__bitwriter_new();
if(frame == 0) {
fprintf(stderr, "EST: can't allocate frame\n");
flac_fprintf(stderr, "EST: can't allocate frame\n");
return;
}
if(!FLAC__bitwriter_init(frame)) {
fprintf(stderr, "EST: can't init frame\n");
flac_fprintf(stderr, "EST: can't init frame\n");
return;
}
ret = add_subframe_(encoder, blocksize, subframe_bps, subframe, frame);
FLAC__ASSERT(ret);
{
const uint32_t actual = FLAC__bitwriter_get_input_bits_unconsumed(frame);
if(estimate != actual)
fprintf(stderr, "EST: bad, frame#%u sub#%%d type=%8s est=%u, actual=%u, delta=%d\n", encoder->private_->current_frame_number, FLAC__SubframeTypeString[subframe->type], estimate, actual, (int)actual-(int)estimate);
flac_fprintf(stderr, "EST: bad, frame#%u sub#%%d type=%8s est=%u, actual=%u, delta=%d\n", encoder->private_->current_frame_number, FLAC__SubframeTypeString[subframe->type], estimate, actual, (int)actual-(int)estimate);
}
FLAC__bitwriter_delete(frame);
}
Expand Down Expand Up @@ -4995,7 +4995,7 @@ FLAC__bool set_partitioned_rice_(

if(rice_parameter >= rice_parameter_limit) {
#ifndef NDEBUG
fprintf(stderr, "clipping rice_parameter (%u -> %u) @6\n", rice_parameter, rice_parameter_limit - 1);
flac_fprintf(stderr, "clipping rice_parameter (%u -> %u) @6\n", rice_parameter, rice_parameter_limit - 1);
#endif
rice_parameter = rice_parameter_limit - 1;
}
Expand All @@ -5010,7 +5010,7 @@ FLAC__bool set_partitioned_rice_(
max_rice_parameter = rice_parameter + rice_parameter_search_dist;
if(max_rice_parameter >= rice_parameter_limit) {
#ifndef NDEBUG
fprintf(stderr, "clipping rice_parameter (%u -> %u) @7\n", max_rice_parameter, rice_parameter_limit - 1);
flac_fprintf(stderr, "clipping rice_parameter (%u -> %u) @7\n", max_rice_parameter, rice_parameter_limit - 1);
#endif
max_rice_parameter = rice_parameter_limit - 1;
}
Expand Down
Loading

0 comments on commit dda3e77

Please sign in to comment.