Skip to content

Commit

Permalink
external libs: updated libflac build.
Browse files Browse the repository at this point in the history
  • Loading branch information
sezero committed Nov 17, 2019
1 parent 56a9b43 commit 06f3c89
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 35 deletions.
Binary file modified VisualC/external/lib/x64/libFLAC-8.dll
Binary file not shown.
Binary file modified VisualC/external/lib/x86/libFLAC-8.dll
Binary file not shown.
Binary file modified Xcode/Frameworks/FLAC.framework/Versions/A/FLAC
Binary file not shown.
96 changes: 85 additions & 11 deletions external/libflac/src/libFLAC/metadata_iterators.c
Expand Up @@ -839,6 +839,11 @@ FLAC_API FLAC__bool FLAC__metadata_simple_iterator_delete_block(FLAC__Metadata_S
FLAC__ASSERT_DECLARATION(FLAC__off_t debug_target_offset = iterator->offset[iterator->depth];)
FLAC__bool ret;

if(!iterator->is_writable) {
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_WRITABLE;
return false;
}

if(iterator->type == FLAC__METADATA_TYPE_STREAMINFO) {
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT;
return false;
Expand Down Expand Up @@ -1087,7 +1092,7 @@ static FLAC__bool chain_merge_adjacent_padding_(FLAC__Metadata_Chain *chain, FLA
{
if(node->data->type == FLAC__METADATA_TYPE_PADDING && 0 != node->next && node->next->data->type == FLAC__METADATA_TYPE_PADDING) {
const unsigned growth = FLAC__STREAM_METADATA_HEADER_LENGTH + node->next->data->length;
node->data->length += growth;
node->data->length += growth; /* new block size can be greater than max metadata block size, but it'll be fixed later in chain_prepare_for_write_() */

chain_delete_node_(chain, node->next);
return true;
Expand Down Expand Up @@ -1154,6 +1159,22 @@ static FLAC__off_t chain_prepare_for_write_(FLAC__Metadata_Chain *chain, FLAC__b
}
}

/* check sizes of all metadata blocks; reduce padding size if necessary */
{
FLAC__Metadata_Node *node;
for (node = chain->head; node; node = node->next) {
if(node->data->length >= (1u << FLAC__STREAM_METADATA_LENGTH_LEN)) {
if(node->data->type == FLAC__METADATA_TYPE_PADDING) {
node->data->length = (1u << FLAC__STREAM_METADATA_LENGTH_LEN) - 1;
current_length = chain_calculate_length_(chain);
} else {
chain->status = FLAC__METADATA_CHAIN_STATUS_BAD_METADATA;
return 0;
}
}
}
}

return current_length;
}

Expand Down Expand Up @@ -1599,37 +1620,87 @@ FLAC_API FLAC__bool FLAC__metadata_chain_read_ogg_with_callbacks(FLAC__Metadata_
return chain_read_with_callbacks_(chain, handle, callbacks, /*is_ogg=*/true);
}

typedef enum {
LBS_NONE = 0,
LBS_SIZE_CHANGED,
LBS_BLOCK_ADDED,
LBS_BLOCK_REMOVED
} LastBlockState;

FLAC_API FLAC__bool FLAC__metadata_chain_check_if_tempfile_needed(FLAC__Metadata_Chain *chain, FLAC__bool use_padding)
{
/* This does all the same checks that are in chain_prepare_for_write_()
* but doesn't actually alter the chain. Make sure to update the logic
* here if chain_prepare_for_write_() changes.
*/
const FLAC__off_t current_length = chain_calculate_length_(chain);
FLAC__off_t current_length;
LastBlockState lbs_state = LBS_NONE;
unsigned lbs_size = 0;

FLAC__ASSERT(0 != chain);

current_length = chain_calculate_length_(chain);

if(use_padding) {
const FLAC__Metadata_Node * const node = chain->tail;
/* if the metadata shrank and the last block is padding, we just extend the last padding block */
if(current_length < chain->initial_length && chain->tail->data->type == FLAC__METADATA_TYPE_PADDING)
return false;
if(current_length < chain->initial_length && node->data->type == FLAC__METADATA_TYPE_PADDING) {
lbs_state = LBS_SIZE_CHANGED;
lbs_size = node->data->length + (chain->initial_length - current_length);
}
/* if the metadata shrank more than 4 bytes then there's room to add another padding block */
else if(current_length + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH <= chain->initial_length)
return false;
else if(current_length + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH <= chain->initial_length) {
lbs_state = LBS_BLOCK_ADDED;
lbs_size = chain->initial_length - (current_length + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH);
}
/* if the metadata grew but the last block is padding, try cutting the padding to restore the original length so we don't have to rewrite the whole file */
else if(current_length > chain->initial_length) {
const FLAC__off_t delta = current_length - chain->initial_length;
if(chain->tail->data->type == FLAC__METADATA_TYPE_PADDING) {
if(node->data->type == FLAC__METADATA_TYPE_PADDING) {
/* if the delta is exactly the size of the last padding block, remove the padding block */
if((FLAC__off_t)chain->tail->data->length + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH == delta)
return false;
if((FLAC__off_t)node->data->length + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH == delta) {
lbs_state = LBS_BLOCK_REMOVED;
lbs_size = 0;
}
/* if there is at least 'delta' bytes of padding, trim the padding down */
else if((FLAC__off_t)chain->tail->data->length >= delta)
return false;
else if((FLAC__off_t)node->data->length >= delta) {
lbs_state = LBS_SIZE_CHANGED;
lbs_size = node->data->length - delta;
}
}
}
}

current_length = 0;
/* check sizes of all metadata blocks; reduce padding size if necessary */
{
const FLAC__Metadata_Node *node;
for(node = chain->head; node; node = node->next) {
unsigned block_len = node->data->length;
if(node == chain->tail) {
if(lbs_state == LBS_BLOCK_REMOVED)
continue;
else if(lbs_state == LBS_SIZE_CHANGED)
block_len = lbs_size;
}
if(block_len >= (1u << FLAC__STREAM_METADATA_LENGTH_LEN)) {
if(node->data->type == FLAC__METADATA_TYPE_PADDING)
block_len = (1u << FLAC__STREAM_METADATA_LENGTH_LEN) - 1;
else
return false /* the return value doesn't matter */;
}
current_length += (FLAC__STREAM_METADATA_HEADER_LENGTH + block_len);
}

if(lbs_state == LBS_BLOCK_ADDED) {
/* test added padding block */
unsigned block_len = lbs_size;
if(block_len >= (1u << FLAC__STREAM_METADATA_LENGTH_LEN))
block_len = (1u << FLAC__STREAM_METADATA_LENGTH_LEN) - 1;
current_length += (FLAC__STREAM_METADATA_HEADER_LENGTH + block_len);
}
}

return (current_length != chain->initial_length);
}

Expand Down Expand Up @@ -2533,6 +2604,9 @@ FLAC__bool write_metadata_block_header_cb_(FLAC__IOHandle handle, FLAC__IOCallba
FLAC__byte buffer[FLAC__STREAM_METADATA_HEADER_LENGTH];

FLAC__ASSERT(block->length < (1u << FLAC__STREAM_METADATA_LENGTH_LEN));
/* double protection */
if(block->length >= (1u << FLAC__STREAM_METADATA_LENGTH_LEN))
return false;

buffer[0] = (block->is_last? 0x80 : 0) | (FLAC__byte)block->type;
pack_uint32_(block->length, buffer + 1, 3);
Expand Down
32 changes: 13 additions & 19 deletions external/libflac/src/libFLAC/stream_decoder.c
Expand Up @@ -38,7 +38,7 @@
#include <stdlib.h> /* for malloc() */
#include <string.h> /* for memset/memcpy() */
#include <sys/types.h> /* for off_t */
#include <sys/stat.h> /* for stat() */
#include <sys/stat.h> /* for stat() */
#include "share/compat.h"
#include "FLAC/assert.h"
#include "share/alloc.h"
Expand Down Expand Up @@ -1264,9 +1264,6 @@ FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigne
unsigned i;
FLAC__int32 *tmp;

/* Make sure size is some sensible minimum value. Plumb through predictor_order maybe? */
size = size < FLAC__MAX_LPC_ORDER ? FLAC__MAX_LPC_ORDER : size ;

if(size <= decoder->private_->output_capacity && channels <= decoder->private_->output_channels)
return true;

Expand Down Expand Up @@ -2597,6 +2594,11 @@ FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel,
case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2:
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
return false; /* read_callback_ sets the state for us */
if(decoder->private_->frame.header.blocksize >> u32 < order) {
send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
return true;
}
subframe->entropy_coding_method.data.partitioned_rice.order = u32;
subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel];
break;
Expand Down Expand Up @@ -2681,6 +2683,11 @@ FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, un
case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2:
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
return false; /* read_callback_ sets the state for us */
if(decoder->private_->frame.header.blocksize >> u32 < order) {
send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
return true;
}
subframe->entropy_coding_method.data.partitioned_rice.order = u32;
subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel];
break;
Expand Down Expand Up @@ -2749,21 +2756,8 @@ FLAC__bool read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigne
const unsigned plen = is_extended? FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN : FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
const unsigned pesc = is_extended? FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_ESCAPE_PARAMETER : FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;

/* sanity checks */
if(partition_order == 0) {
if(decoder->private_->frame.header.blocksize < predictor_order) {
send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
return true;
}
}
else {
if(partition_samples < predictor_order) {
send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
return true;
}
}
/* invalid predictor and partition orders mush be handled in the callers */
FLAC__ASSERT(partition_order > 0? partition_samples >= predictor_order : decoder->private_->frame.header.blocksize >= predictor_order);

if(!FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, flac_max(6u, partition_order))) {
decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
Expand Down
8 changes: 3 additions & 5 deletions external/libflac/src/libFLAC/stream_encoder.c
Expand Up @@ -1287,7 +1287,9 @@ FLAC_API FLAC__bool FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder)
{
FLAC__bool error = false;

FLAC__ASSERT(0 != encoder);
if (encoder == NULL)
return false;

FLAC__ASSERT(0 != encoder->private_);
FLAC__ASSERT(0 != encoder->protected_);

Expand Down Expand Up @@ -4304,14 +4306,10 @@ FILE *get_binary_stdout_(void)
*/
#if defined _MSC_VER || defined __MINGW32__
_setmode(_fileno(stdout), _O_BINARY);
#elif defined __CYGWIN__
/* almost certainly not needed for any modern Cygwin, but let's be safe... */
setmode(_fileno(stdout), _O_BINARY);
#elif defined __EMX__
setmode(fileno(stdout), O_BINARY);
#endif

return stdout;
}

#endif /* FLAC_INCLUDE_ENCODER */
3 changes: 3 additions & 0 deletions external/libflac/src/libFLAC/stream_encoder_framing.c
Expand Up @@ -66,6 +66,9 @@ FLAC__bool FLAC__add_metadata_block(const FLAC__StreamMetadata *metadata, FLAC__
i += vendor_string_length;
}
FLAC__ASSERT(i < (1u << FLAC__STREAM_METADATA_LENGTH_LEN));
/* double protection */
if(i >= (1u << FLAC__STREAM_METADATA_LENGTH_LEN))
return false;
if(!FLAC__bitwriter_write_raw_uint32(bw, i, FLAC__STREAM_METADATA_LENGTH_LEN))
return false;

Expand Down

0 comments on commit 06f3c89

Please sign in to comment.