Commit f91251d1 authored by Josh Coalson's avatar Josh Coalson
Browse files

add flushed fwrite in Valgrind mode

parent 8b9a477a
......@@ -474,7 +474,7 @@ FLAC__bool DecoderSession_process(DecoderSession *d)
}
if(d->is_aiff_out && ((d->total_samples * d->channels * ((d->bps+7)/8)) & 1)) {
if(fwrite("\000", 1, 1, d->fout) != 1) {
if(flac__utils_fwrite("\000", 1, 1, d->fout) != 1) {
print_error_with_state(d, "ERROR writing pad byte to AIFF SSND chunk");
return false;
}
......@@ -596,7 +596,7 @@ FLAC__bool write_little_endian_uint16(FILE *f, FLAC__uint16 val)
FLAC__byte tmp;
tmp = b[1]; b[1] = b[0]; b[0] = tmp;
}
return fwrite(b, 1, 2, f) == 2;
return flac__utils_fwrite(b, 1, 2, f) == 2;
}
FLAC__bool write_little_endian_uint32(FILE *f, FLAC__uint32 val)
......@@ -607,7 +607,7 @@ FLAC__bool write_little_endian_uint32(FILE *f, FLAC__uint32 val)
tmp = b[3]; b[3] = b[0]; b[0] = tmp;
tmp = b[2]; b[2] = b[1]; b[1] = tmp;
}
return fwrite(b, 1, 4, f) == 4;
return flac__utils_fwrite(b, 1, 4, f) == 4;
}
FLAC__bool write_big_endian_uint16(FILE *f, FLAC__uint16 val)
......@@ -617,7 +617,7 @@ FLAC__bool write_big_endian_uint16(FILE *f, FLAC__uint16 val)
FLAC__byte tmp;
tmp = b[1]; b[1] = b[0]; b[0] = tmp;
}
return fwrite(b, 1, 2, f) == 2;
return flac__utils_fwrite(b, 1, 2, f) == 2;
}
FLAC__bool write_big_endian_uint32(FILE *f, FLAC__uint32 val)
......@@ -628,7 +628,7 @@ FLAC__bool write_big_endian_uint32(FILE *f, FLAC__uint32 val)
tmp = b[3]; b[3] = b[0]; b[0] = tmp;
tmp = b[2]; b[2] = b[1]; b[1] = tmp;
}
return fwrite(b, 1, 4, f) == 4;
return flac__utils_fwrite(b, 1, 4, f) == 4;
}
FLAC__bool write_sane_extended(FILE *f, unsigned val)
......@@ -807,7 +807,7 @@ FLAC__StreamDecoderWriteStatus write_callback(const void *decoder, const FLAC__F
for(channel = 0; channel < channels; channel++, sample++)
s8buffer[sample] = (FLAC__int8)(buffer[channel][wide_sample]);
}
if(fwrite(u8buffer, 1, sample, fout) != sample)
if(flac__utils_fwrite(u8buffer, 1, sample, fout) != sample)
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
}
else if(bps == 16) {
......@@ -830,7 +830,7 @@ FLAC__StreamDecoderWriteStatus write_callback(const void *decoder, const FLAC__F
u8buffer[byte+1] = tmp;
}
}
if(fwrite(u16buffer, 2, sample, fout) != sample)
if(flac__utils_fwrite(u16buffer, 2, sample, fout) != sample)
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
}
else if(bps == 24) {
......@@ -876,7 +876,7 @@ FLAC__StreamDecoderWriteStatus write_callback(const void *decoder, const FLAC__F
byte++;
}
}
if(fwrite(u8buffer, 3, sample, fout) != sample)
if(flac__utils_fwrite(u8buffer, 3, sample, fout) != sample)
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
}
else {
......@@ -951,7 +951,7 @@ void metadata_callback(const void *decoder, const FLAC__StreamMetadata *metadata
return;
}
if(decoder_session->is_wave_out) {
if(fwrite("RIFF", 1, 4, decoder_session->fout) != 4)
if(flac__utils_fwrite("RIFF", 1, 4, decoder_session->fout) != 4)
decoder_session->abort_flag = true;
if(decoder_session->wave_chunk_size_fixup.needs_fixup)
......@@ -960,13 +960,13 @@ void metadata_callback(const void *decoder, const FLAC__StreamMetadata *metadata
if(!write_little_endian_uint32(decoder_session->fout, (FLAC__uint32)(data_size+36))) /* filesize-8 */
decoder_session->abort_flag = true;
if(fwrite("WAVEfmt ", 1, 8, decoder_session->fout) != 8)
if(flac__utils_fwrite("WAVEfmt ", 1, 8, decoder_session->fout) != 8)
decoder_session->abort_flag = true;
if(fwrite("\020\000\000\000", 1, 4, decoder_session->fout) != 4) /* chunk size = 16 */
if(flac__utils_fwrite("\020\000\000\000", 1, 4, decoder_session->fout) != 4) /* chunk size = 16 */
decoder_session->abort_flag = true;
if(fwrite("\001\000", 1, 2, decoder_session->fout) != 2) /* compression code == 1 */
if(flac__utils_fwrite("\001\000", 1, 2, decoder_session->fout) != 2) /* compression code == 1 */
decoder_session->abort_flag = true;
if(!write_little_endian_uint16(decoder_session->fout, (FLAC__uint16)(decoder_session->channels)))
......@@ -984,7 +984,7 @@ void metadata_callback(const void *decoder, const FLAC__StreamMetadata *metadata
if(!write_little_endian_uint16(decoder_session->fout, (FLAC__uint16)(decoder_session->bps))) /* bits per sample */
decoder_session->abort_flag = true;
if(fwrite("data", 1, 4, decoder_session->fout) != 4)
if(flac__utils_fwrite("data", 1, 4, decoder_session->fout) != 4)
decoder_session->abort_flag = true;
if(decoder_session->wave_chunk_size_fixup.needs_fixup)
......@@ -996,7 +996,7 @@ void metadata_callback(const void *decoder, const FLAC__StreamMetadata *metadata
else {
const FLAC__uint32 aligned_data_size = (data_size+1) & (~1U);
if(fwrite("FORM", 1, 4, decoder_session->fout) != 4)
if(flac__utils_fwrite("FORM", 1, 4, decoder_session->fout) != 4)
decoder_session->abort_flag = true;
if(decoder_session->wave_chunk_size_fixup.needs_fixup)
......@@ -1005,10 +1005,10 @@ void metadata_callback(const void *decoder, const FLAC__StreamMetadata *metadata
if(!write_big_endian_uint32(decoder_session->fout, (FLAC__uint32)(aligned_data_size+46))) /* filesize-8 */
decoder_session->abort_flag = true;
if(fwrite("AIFFCOMM", 1, 8, decoder_session->fout) != 8)
if(flac__utils_fwrite("AIFFCOMM", 1, 8, decoder_session->fout) != 8)
decoder_session->abort_flag = true;
if(fwrite("\000\000\000\022", 1, 4, decoder_session->fout) != 4) /* chunk size = 18 */
if(flac__utils_fwrite("\000\000\000\022", 1, 4, decoder_session->fout) != 4) /* chunk size = 18 */
decoder_session->abort_flag = true;
if(!write_big_endian_uint16(decoder_session->fout, (FLAC__uint16)(decoder_session->channels)))
......@@ -1026,7 +1026,7 @@ void metadata_callback(const void *decoder, const FLAC__StreamMetadata *metadata
if(!write_sane_extended(decoder_session->fout, decoder_session->sample_rate))
decoder_session->abort_flag = true;
if(fwrite("SSND", 1, 4, decoder_session->fout) != 4)
if(flac__utils_fwrite("SSND", 1, 4, decoder_session->fout) != 4)
decoder_session->abort_flag = true;
if(decoder_session->wave_chunk_size_fixup.needs_fixup)
......
......@@ -1735,7 +1735,7 @@ FLAC__StreamEncoderWriteStatus ogg_stream_encoder_write_callback(const OggFLAC__
if(encoder_session->verbose && encoder_session->total_samples_to_encode > 0 && !(current_frame & encoder_session->stats_mask))
print_stats(encoder_session);
if(fwrite(buffer, sizeof(FLAC__byte), bytes, encoder_session->fout) == bytes)
if(flac__utils_fwrite(buffer, sizeof(FLAC__byte), bytes, encoder_session->fout) == bytes)
return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
else
return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
......@@ -1754,7 +1754,7 @@ FLAC__StreamEncoderWriteStatus flac_stream_encoder_write_callback(const FLAC__St
if(samples && encoder_session->verbose && encoder_session->total_samples_to_encode > 0 && !(current_frame & encoder_session->stats_mask))
print_stats(encoder_session);
if(fwrite(buffer, sizeof(FLAC__byte), bytes, encoder_session->fout) == bytes)
if(flac__utils_fwrite(buffer, sizeof(FLAC__byte), bytes, encoder_session->fout) == bytes)
return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
else
return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
......
......@@ -78,6 +78,16 @@ static FLAC__bool local__parse_timecode_(const char *s, double *value)
return true;
}
#ifdef FLAC__VALGRIND_TESTING
size_t flac__utils_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
{
size_t ret = fwrite(ptr, size, nmemb, stream);
if(!ferror(stream))
fflush(stream);
return ret;
}
#endif
FLAC__bool flac__utils_parse_skip_until_specification(const char *s, utils__SkipUntilSpecification *spec)
{
FLAC__uint64 val;
......
......@@ -20,6 +20,7 @@
#define flac__utils_h
#include "FLAC/ordinals.h"
#include <stdio.h> /* for FILE */
typedef struct {
FLAC__bool is_relative; /* i.e. specification string started with + or - */
......@@ -30,6 +31,11 @@ typedef struct {
} value;
} utils__SkipUntilSpecification;
#ifdef FLAC__VALGRIND_TESTING
size_t flac__utils_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
#else
#define flac__utils_fwrite fwrite
#endif
FLAC__bool flac__utils_parse_skip_until_specification(const char *s, utils__SkipUntilSpecification *spec);
void flac__utils_canonicalize_skip_until_specification(utils__SkipUntilSpecification *spec, unsigned sample_rate);
......
......@@ -699,6 +699,18 @@ FLAC__SeekableStreamEncoderSeekStatus seek_callback_(const FLAC__SeekableStreamE
return FLAC__SEEKABLE_STREAM_ENCODER_SEEK_STATUS_OK;
}
#ifdef FLAC__VALGRIND_TESTING
static size_t local__fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
{
size_t ret = fwrite(ptr, size, nmemb, stream);
if(!ferror(stream))
fflush(stream);
return ret;
}
#else
#define local__fwrite fwrite
#endif
FLAC__StreamEncoderWriteStatus write_callback_(const FLAC__SeekableStreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data)
{
FLAC__FileEncoder *file_encoder = (FLAC__FileEncoder*)client_data;
......@@ -707,7 +719,7 @@ FLAC__StreamEncoderWriteStatus write_callback_(const FLAC__SeekableStreamEncoder
FLAC__ASSERT(0 != file_encoder);
if(fwrite(buffer, sizeof(FLAC__byte), bytes, file_encoder->private_->file) == bytes) {
if(local__fwrite(buffer, sizeof(FLAC__byte), bytes, file_encoder->private_->file) == bytes) {
file_encoder->private_->bytes_written += bytes;
file_encoder->private_->samples_written += samples;
if(0 != file_encoder->private_->progress_callback && samples > 0)
......
......@@ -111,6 +111,18 @@ static void set_file_stats_(const char *filename, struct stat *stats);
static FLAC__Metadata_ChainStatus get_equivalent_status_(FLAC__Metadata_SimpleIteratorStatus status);
#ifdef FLAC__VALGRIND_TESTING
static size_t local__fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
{
size_t ret = fwrite(ptr, size, nmemb, stream);
if(!ferror(stream))
fflush(stream);
return ret;
}
#else
#define local__fwrite fwrite
#endif
/****************************************************************************
*
* Level 0 implementation
......@@ -1661,7 +1673,7 @@ FLAC__bool write_metadata_block_header_(FILE *file, FLAC__Metadata_SimpleIterato
buffer[0] = (block->is_last? 0x80 : 0) | (FLAC__byte)block->type;
pack_uint32_(block->length, buffer + 1, 3);
if(fwrite(buffer, 1, FLAC__STREAM_METADATA_HEADER_LENGTH, file) != FLAC__STREAM_METADATA_HEADER_LENGTH) {
if(local__fwrite(buffer, 1, FLAC__STREAM_METADATA_HEADER_LENGTH, file) != FLAC__STREAM_METADATA_HEADER_LENGTH) {
*status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
return false;
}
......@@ -1723,7 +1735,7 @@ FLAC__Metadata_SimpleIteratorStatus write_metadata_block_data_streaminfo_(FILE *
pack_uint32_((FLAC__uint32)block->total_samples, buffer+14, 4);
memcpy(buffer+18, block->md5sum, 16);
if(fwrite(buffer, 1, FLAC__STREAM_METADATA_STREAMINFO_LENGTH, file) != FLAC__STREAM_METADATA_STREAMINFO_LENGTH)
if(local__fwrite(buffer, 1, FLAC__STREAM_METADATA_STREAMINFO_LENGTH, file) != FLAC__STREAM_METADATA_STREAMINFO_LENGTH)
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
......@@ -1741,12 +1753,12 @@ FLAC__Metadata_SimpleIteratorStatus write_metadata_block_data_padding_(FILE *fil
memset(buffer, 0, 1024);
for(i = 0; i < n/1024; i++)
if(fwrite(buffer, 1, 1024, file) != 1024)
if(local__fwrite(buffer, 1, 1024, file) != 1024)
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
n %= 1024;
if(fwrite(buffer, 1, n, file) != n)
if(local__fwrite(buffer, 1, n, file) != n)
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
......@@ -1758,12 +1770,12 @@ FLAC__Metadata_SimpleIteratorStatus write_metadata_block_data_application_(FILE
FLAC__ASSERT(0 != file);
if(fwrite(block->id, 1, id_bytes, file) != id_bytes)
if(local__fwrite(block->id, 1, id_bytes, file) != id_bytes)
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
block_length -= id_bytes;
if(fwrite(block->data, 1, block_length, file) != block_length)
if(local__fwrite(block->data, 1, block_length, file) != block_length)
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
......@@ -1781,7 +1793,7 @@ FLAC__Metadata_SimpleIteratorStatus write_metadata_block_data_seektable_(FILE *f
pack_uint64_(block->points[i].sample_number, buffer, 8);
pack_uint64_(block->points[i].stream_offset, buffer+8, 8);
pack_uint32_(block->points[i].frame_samples, buffer+16, 2);
if(fwrite(buffer, 1, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH, file) != FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)
if(local__fwrite(buffer, 1, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH, file) != FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
}
......@@ -1799,20 +1811,20 @@ FLAC__Metadata_SimpleIteratorStatus write_metadata_block_data_vorbis_comment_(FI
FLAC__ASSERT(0 != file);
pack_uint32_little_endian_(block->vendor_string.length, buffer, entry_length_len);
if(fwrite(buffer, 1, entry_length_len, file) != entry_length_len)
if(local__fwrite(buffer, 1, entry_length_len, file) != entry_length_len)
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
if(fwrite(block->vendor_string.entry, 1, block->vendor_string.length, file) != block->vendor_string.length)
if(local__fwrite(block->vendor_string.entry, 1, block->vendor_string.length, file) != block->vendor_string.length)
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
pack_uint32_little_endian_(block->num_comments, buffer, num_comments_len);
if(fwrite(buffer, 1, num_comments_len, file) != num_comments_len)
if(local__fwrite(buffer, 1, num_comments_len, file) != num_comments_len)
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
for(i = 0; i < block->num_comments; i++) {
pack_uint32_little_endian_(block->comments[i].length, buffer, entry_length_len);
if(fwrite(buffer, 1, entry_length_len, file) != entry_length_len)
if(local__fwrite(buffer, 1, entry_length_len, file) != entry_length_len)
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
if(fwrite(block->comments[i].entry, 1, block->comments[i].length, file) != block->comments[i].length)
if(local__fwrite(block->comments[i].entry, 1, block->comments[i].length, file) != block->comments[i].length)
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
}
......@@ -1833,13 +1845,13 @@ FLAC__Metadata_SimpleIteratorStatus write_metadata_block_data_cuesheet_(FILE *fi
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN % 8 == 0);
len = FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN / 8;
if(fwrite(block->media_catalog_number, 1, len, file) != len)
if(local__fwrite(block->media_catalog_number, 1, len, file) != len)
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN % 8 == 0);
len = FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN / 8;
pack_uint64_(block->lead_in, buffer, len);
if(fwrite(buffer, 1, len, file) != len)
if(local__fwrite(buffer, 1, len, file) != len)
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
FLAC__ASSERT((FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN + FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN) % 8 == 0);
......@@ -1847,13 +1859,13 @@ FLAC__Metadata_SimpleIteratorStatus write_metadata_block_data_cuesheet_(FILE *fi
memset(buffer, 0, len);
if(block->is_cd)
buffer[0] |= 0x80;
if(fwrite(buffer, 1, len, file) != len)
if(local__fwrite(buffer, 1, len, file) != len)
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN % 8 == 0);
len = FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN / 8;
pack_uint32_(block->num_tracks, buffer, len);
if(fwrite(buffer, 1, len, file) != len)
if(local__fwrite(buffer, 1, len, file) != len)
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
for(i = 0; i < block->num_tracks; i++) {
......@@ -1862,31 +1874,31 @@ FLAC__Metadata_SimpleIteratorStatus write_metadata_block_data_cuesheet_(FILE *fi
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN % 8 == 0);
len = FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN / 8;
pack_uint64_(track->offset, buffer, len);
if(fwrite(buffer, 1, len, file) != len)
if(local__fwrite(buffer, 1, len, file) != len)
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN % 8 == 0);
len = FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN / 8;
pack_uint32_(track->number, buffer, len);
if(fwrite(buffer, 1, len, file) != len)
if(local__fwrite(buffer, 1, len, file) != len)
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN % 8 == 0);
len = FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN / 8;
if(fwrite(track->isrc, 1, len, file) != len)
if(local__fwrite(track->isrc, 1, len, file) != len)
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
FLAC__ASSERT((FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN) % 8 == 0);
len = (FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN) / 8;
memset(buffer, 0, len);
buffer[0] = (track->type << 7) | (track->pre_emphasis << 6);
if(fwrite(buffer, 1, len, file) != len)
if(local__fwrite(buffer, 1, len, file) != len)
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN % 8 == 0);
len = FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN / 8;
pack_uint32_(track->num_indices, buffer, len);
if(fwrite(buffer, 1, len, file) != len)
if(local__fwrite(buffer, 1, len, file) != len)
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
for(j = 0; j < track->num_indices; j++) {
......@@ -1895,19 +1907,19 @@ FLAC__Metadata_SimpleIteratorStatus write_metadata_block_data_cuesheet_(FILE *fi
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN % 8 == 0);
len = FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN / 8;
pack_uint64_(index->offset, buffer, len);
if(fwrite(buffer, 1, len, file) != len)
if(local__fwrite(buffer, 1, len, file) != len)
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN % 8 == 0);
len = FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN / 8;
pack_uint32_(index->number, buffer, len);
if(fwrite(buffer, 1, len, file) != len)
if(local__fwrite(buffer, 1, len, file) != len)
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN % 8 == 0);
len = FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN / 8;
memset(buffer, 0, len);
if(fwrite(buffer, 1, len, file) != len)
if(local__fwrite(buffer, 1, len, file) != len)
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
}
}
......@@ -2278,7 +2290,7 @@ FLAC__bool simple_iterator_copy_file_postfix_(FLAC__Metadata_SimpleIterator *ite
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
return false;
}
if(fwrite(&x, 1, 1, *tempfile) != 1) {
if(local__fwrite(&x, 1, 1, *tempfile) != 1) {
cleanup_tempfile_(tempfile, tempfilename);
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
return false;
......@@ -2321,7 +2333,7 @@ FLAC__bool copy_n_bytes_from_file_(FILE *file, FILE *tempfile, unsigned bytes/*@
*status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
return false;
}
if(fwrite(buffer, 1, n, tempfile) != n) {
if(local__fwrite(buffer, 1, n, tempfile) != n) {
*status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
return false;
}
......@@ -2342,7 +2354,7 @@ FLAC__bool copy_remaining_bytes_from_file_(FILE *file, FILE *tempfile, FLAC__Met
*status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
return false;
}
if(n > 0 && fwrite(buffer, 1, n, tempfile) != n) {
if(n > 0 && local__fwrite(buffer, 1, n, tempfile) != n) {
*status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
return false;
}
......
......@@ -568,7 +568,7 @@ void write_metadata(const char *filename, FLAC__StreamMetadata *block, unsigned
if(hexdump_application)
hexdump(filename, block->data.application.data, block->length - FLAC__STREAM_METADATA_HEADER_LENGTH, " ");
else
(void) fwrite(block->data.application.data, 1, block->length - FLAC__STREAM_METADATA_HEADER_LENGTH, stdout);
(void) local_fwrite(block->data.application.data, 1, block->length - FLAC__STREAM_METADATA_HEADER_LENGTH, stdout);
}
break;
case FLAC__METADATA_TYPE_SEEKTABLE:
......
......@@ -31,6 +31,16 @@ void die(const char *message)
exit(1);
}
#ifdef FLAC__VALGRIND_TESTING
size_t local_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
{
size_t ret = fwrite(ptr, size, nmemb, stream);
if(!ferror(stream))
fflush(stream);
return ret;
}
#endif
char *local_strdup(const char *source)
{
char *ret;
......@@ -166,17 +176,17 @@ void write_vc_field(const char *filename, const FLAC__StreamMetadata_VorbisComme
memcpy(terminated, entry->entry, entry->length);
terminated[entry->length] = '\0';
if(utf8_decode(terminated, &converted) >= 0) {
(void) fwrite(converted, 1, strlen(converted), f);
(void) local_fwrite(converted, 1, strlen(converted), f);
free(terminated);
free(converted);
}
else {
free(terminated);
(void) fwrite(entry->entry, 1, entry->length, f);
(void) local_fwrite(entry->entry, 1, entry->length, f);
}
}
else {
(void) fwrite(entry->entry, 1, entry->length, f);
(void) local_fwrite(entry->entry, 1, entry->length, f);
}
}
......
......@@ -23,6 +23,11 @@
#include <stdio.h> /* for FILE */
void die(const char *message);
#ifdef FLAC__VALGRIND_TESTING
size_t local_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
#else
#define local_fwrite fwrite
#endif
char *local_strdup(const char *source);
void local_strcat(char **dest, const char *source);
void hexdump(const char *filename, const FLAC__byte *buf, unsigned bytes, const char *indent);
......
......@@ -28,6 +28,18 @@
#endif
#define min(a,b) ((a)<(b)?(a):(b))
#ifdef FLAC__VALGRIND_TESTING
static size_t local__fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
{
size_t ret = fwrite(ptr, size, nmemb, stream);
if(!ferror(stream))
fflush(stream);
return ret;
}
#else
#define local__fwrite fwrite
#endif
typedef struct {
FILE *file;
} encoder_client_struct;
......@@ -38,7 +50,7 @@ static FLAC__StreamEncoderWriteStatus encoder_write_callback_(const FLAC__Stream
(void)encoder, (void)samples, (void)current_frame;
if(fwrite(buffer, 1, bytes, ecd->file) != bytes)
if(local__fwrite(buffer, 1, bytes, ecd->file) != bytes)
return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
else
return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
......
......@@ -28,6 +28,18 @@
#endif
#define min(a,b) ((a)<(b)?(a):(b))
#ifdef FLAC__VALGRIND_TESTING
static size_t local__fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
{
size_t ret = fwrite(ptr, size, nmemb, stream);
if(!ferror(stream))
fflush(stream);
return ret;
}
#else
#define local__fwrite fwrite
#endif
typedef struct {
FILE *file;
} encoder_client_struct;
......@@ -38,7 +50,7 @@ static FLAC__StreamEncoderWriteStatus encoder_write_callback_(const FLAC__Stream
(void)encoder, (void)samples, (void)current_frame;
if(fwrite(buffer, 1, bytes, ecd->file) != bytes)
if(local__fwrite(buffer, 1, bytes, ecd->file) != bytes)
return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
else
return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
......
......@@ -30,6 +30,18 @@
const long file_utils__serial_number = 12345;
#ifdef FLAC__VALGRIND_TESTING
static size_t local__fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
{
size_t ret = fwrite(ptr, size, nmemb, stream);
if(!ferror(stream))
fflush(stream);
return ret;
}
#else
#define local__fwrite fwrite
#endif
typedef struct {
FILE *file;
} encoder_client_struct;
......@@ -40,7 +52,7 @@ static FLAC__StreamEncoderWriteStatus encoder_write_callback_(const OggFLAC__Str
(void)encoder, (void)samples, (void)current_frame;
if(fwrite(buffer, 1, bytes, ecd->file) != bytes)
if(local__fwrite(buffer, 1, bytes, ecd->file) != bytes)
return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
else
return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
......
......@@ -30,6 +30,18 @@
const long file_utils__serial_number = 12345;
#ifdef FLAC__VALGRIND_TESTING
static size_t local__fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
{
size_t ret = fwrite(ptr, size, nmemb, stream);
if(!ferror(stream))
fflush(stream);
return ret;
}
#else
#define local__fwrite fwrite
#endif