bitmath: Finish up optimizations
[platform/upstream/flac.git] / src / libFLAC / metadata_object.c
index 589357b..7f178f8 100644 (file)
@@ -1,5 +1,5 @@
 /* libFLAC - Free Lossless Audio Codec library
- * Copyright (C) 2001,2002,2003,2004  Josh Coalson
+ * Copyright (C) 2001,2002,2003,2004,2005,2006,2007,2008,2009  Josh Coalson
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+#if HAVE_CONFIG_H
+#  include <config.h>
+#endif
+
 #include <stdlib.h>
 #include <string.h>
 
 #include "private/metadata.h"
 
 #include "FLAC/assert.h"
+#include "share/alloc.h"
 
 
 /****************************************************************************
  *
  ***************************************************************************/
 
+/* copy bytes:
+ *  from = NULL  && bytes = 0
+ *       to <- NULL
+ *  from != NULL && bytes > 0
+ *       to <- copy of from
+ *  else ASSERT
+ * malloc error leaves 'to' unchanged
+ */
 static FLAC__bool copy_bytes_(FLAC__byte **to, const FLAC__byte *from, unsigned bytes)
 {
+       FLAC__ASSERT(0 != to);
        if(bytes > 0 && 0 != from) {
                FLAC__byte *x;
-               if(0 == (x = (FLAC__byte*)malloc(bytes)))
+               if(0 == (x = safe_malloc_(bytes)))
                        return false;
                memcpy(x, from, bytes);
                *to = x;
@@ -60,6 +74,55 @@ static FLAC__bool copy_bytes_(FLAC__byte **to, const FLAC__byte *from, unsigned
        return true;
 }
 
+#if 0 /* UNUSED */
+/* like copy_bytes_(), but free()s the original '*to' if the copy succeeds and the original '*to' is non-NULL */
+static FLAC__bool free_copy_bytes_(FLAC__byte **to, const FLAC__byte *from, unsigned bytes)
+{
+       FLAC__byte *copy;
+       FLAC__ASSERT(0 != to);
+       if(copy_bytes_(&copy, from, bytes)) {
+               if(*to)
+                       free(*to);
+               *to = copy;
+               return true;
+       }
+       else
+               return false;
+}
+#endif
+
+/* reallocate entry to 1 byte larger and add a terminating NUL */
+/* realloc() failure leaves entry unchanged */
+static FLAC__bool ensure_null_terminated_(FLAC__byte **entry, unsigned length)
+{
+       FLAC__byte *x = safe_realloc_add_2op_(*entry, length, /*+*/1);
+       if(0 != x) {
+               x[length] = '\0';
+               *entry = x;
+               return true;
+       }
+       else
+               return false;
+}
+
+/* copies the NUL-terminated C-string 'from' to '*to', leaving '*to'
+ * unchanged if malloc fails, free()ing the original '*to' if it
+ * succeeds and the original '*to' was not NULL
+ */
+static FLAC__bool copy_cstring_(char **to, const char *from)
+{
+       char *copy = strdup(from);
+       FLAC__ASSERT(to);
+       if(copy) {
+               if(*to)
+                       free(*to);
+               *to = copy;
+               return true;
+       }
+       else
+               return false;
+}
+
 static FLAC__bool copy_vcentry_(FLAC__StreamMetadata_VorbisComment_Entry *to, const FLAC__StreamMetadata_VorbisComment_Entry *from)
 {
        to->length = from->length;
@@ -70,9 +133,10 @@ static FLAC__bool copy_vcentry_(FLAC__StreamMetadata_VorbisComment_Entry *to, co
        else {
                FLAC__byte *x;
                FLAC__ASSERT(from->length > 0);
-               if(0 == (x = (FLAC__byte*)malloc(from->length)))
+               if(0 == (x = safe_malloc_add_2op_(from->length, /*+*/1)))
                        return false;
                memcpy(x, from->entry, from->length);
+               x[from->length] = '\0';
                to->entry = x;
        }
        return true;
@@ -87,7 +151,7 @@ static FLAC__bool copy_track_(FLAC__StreamMetadata_CueSheet_Track *to, const FLA
        else {
                FLAC__StreamMetadata_CueSheet_Index *x;
                FLAC__ASSERT(from->num_indices > 0);
-               if(0 == (x = (FLAC__StreamMetadata_CueSheet_Index*)malloc(from->num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index))))
+               if(0 == (x = safe_malloc_mul_2op_(from->num_indices, /*times*/sizeof(FLAC__StreamMetadata_CueSheet_Index))))
                        return false;
                memcpy(x, from->indices, from->num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index));
                to->indices = x;
@@ -109,7 +173,7 @@ static FLAC__StreamMetadata_SeekPoint *seekpoint_array_new_(unsigned num_points)
 
        FLAC__ASSERT(num_points > 0);
 
-       object_array = (FLAC__StreamMetadata_SeekPoint*)malloc(num_points * sizeof(FLAC__StreamMetadata_SeekPoint));
+       object_array = safe_malloc_mul_2op_(num_points, /*times*/sizeof(FLAC__StreamMetadata_SeekPoint));
 
        if(0 != object_array) {
                unsigned i;
@@ -142,7 +206,7 @@ static FLAC__StreamMetadata_VorbisComment_Entry *vorbiscomment_entry_array_new_(
 {
        FLAC__ASSERT(num_comments > 0);
 
-       return (FLAC__StreamMetadata_VorbisComment_Entry*)calloc(num_comments, sizeof(FLAC__StreamMetadata_VorbisComment_Entry));
+       return safe_calloc_(num_comments, sizeof(FLAC__StreamMetadata_VorbisComment_Entry));
 }
 
 static void vorbiscomment_entry_array_delete_(FLAC__StreamMetadata_VorbisComment_Entry *object_array, unsigned num_comments)
@@ -194,13 +258,30 @@ static FLAC__bool vorbiscomment_set_entry_(FLAC__StreamMetadata *object, FLAC__S
 
        save = dest->entry;
 
-       /* do the copy first so that if we fail we leave the object untouched */
-       if(copy && (0 != src->entry && src->length > 0)) {
-               if(!copy_vcentry_(dest, src))
-                       return false;
+       if(0 != src->entry && src->length > 0) {
+               if(copy) {
+                       /* do the copy first so that if we fail we leave the dest object untouched */
+                       if(!copy_vcentry_(dest, src))
+                               return false;
+               }
+               else {
+                       /* we have to make sure that the string we're taking over is null-terminated */
+
+                       /*
+                        * Stripping the const from src->entry is OK since we're taking
+                        * ownership of the pointer.  This is a hack around a deficiency
+                        * in the API where the same function is used for 'copy' and
+                        * 'own', but the source entry is a const pointer.  If we were
+                        * precise, the 'own' flavor would be a separate function with a
+                        * non-const source pointer.  But it's not, so we hack away.
+                        */
+                       if(!ensure_null_terminated_((FLAC__byte**)(&src->entry), src->length))
+                               return false;
+                       *dest = *src;
+               }
        }
        else {
-               /* either we're not copying or the src is null */
+               /* the src is null */
                *dest = *src;
        }
 
@@ -211,6 +292,22 @@ static FLAC__bool vorbiscomment_set_entry_(FLAC__StreamMetadata *object, FLAC__S
        return true;
 }
 
+static int vorbiscomment_find_entry_from_(const FLAC__StreamMetadata *object, unsigned offset, const char *field_name, unsigned field_name_length)
+{
+       unsigned i;
+
+       FLAC__ASSERT(0 != object);
+       FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
+       FLAC__ASSERT(0 != field_name);
+
+       for(i = offset; i < object->data.vorbis_comment.num_comments; i++) {
+               if(FLAC__metadata_object_vorbiscomment_entry_matches(object->data.vorbis_comment.comments[i], field_name, field_name_length))
+                       return (int)i;
+       }
+
+       return -1;
+}
+
 static void cuesheet_calculate_length_(FLAC__StreamMetadata *object)
 {
        unsigned i;
@@ -248,14 +345,14 @@ static FLAC__StreamMetadata_CueSheet_Index *cuesheet_track_index_array_new_(unsi
 {
        FLAC__ASSERT(num_indices > 0);
 
-       return (FLAC__StreamMetadata_CueSheet_Index*)calloc(num_indices, sizeof(FLAC__StreamMetadata_CueSheet_Index));
+       return safe_calloc_(num_indices, sizeof(FLAC__StreamMetadata_CueSheet_Index));
 }
 
 static FLAC__StreamMetadata_CueSheet_Track *cuesheet_track_array_new_(unsigned num_tracks)
 {
        FLAC__ASSERT(num_tracks > 0);
 
-       return (FLAC__StreamMetadata_CueSheet_Track*)calloc(num_tracks, sizeof(FLAC__StreamMetadata_CueSheet_Track));
+       return safe_calloc_(num_tracks, sizeof(FLAC__StreamMetadata_CueSheet_Track));
 }
 
 static void cuesheet_track_array_delete_(FLAC__StreamMetadata_CueSheet_Track *object_array, unsigned num_tracks)
@@ -340,7 +437,7 @@ FLAC_API FLAC__StreamMetadata *FLAC__metadata_object_new(FLAC__MetadataType type
        if(type > FLAC__MAX_METADATA_TYPE_CODE)
                return 0;
 
-       object = (FLAC__StreamMetadata*)calloc(1, sizeof(FLAC__StreamMetadata));
+       object = calloc(1, sizeof(FLAC__StreamMetadata));
        if(0 != object) {
                object->is_last = false;
                object->type = type;
@@ -367,18 +464,51 @@ FLAC_API FLAC__StreamMetadata *FLAC__metadata_object_new(FLAC__MetadataType type
                                */
                                break;
                        case FLAC__METADATA_TYPE_VORBIS_COMMENT:
-                               {
-                                       object->data.vorbis_comment.vendor_string.length = (unsigned)strlen(FLAC__VENDOR_STRING);
-                                       if(!copy_bytes_(&object->data.vorbis_comment.vendor_string.entry, (const FLAC__byte*)FLAC__VENDOR_STRING, object->data.vorbis_comment.vendor_string.length)) {
-                                               free(object);
-                                               return 0;
-                                       }
-                                       vorbiscomment_calculate_length_(object);
+                               object->data.vorbis_comment.vendor_string.length = (unsigned)strlen(FLAC__VENDOR_STRING);
+                               if(!copy_bytes_(&object->data.vorbis_comment.vendor_string.entry, (const FLAC__byte*)FLAC__VENDOR_STRING, object->data.vorbis_comment.vendor_string.length+1)) {
+                                       free(object);
+                                       return 0;
                                }
+                               vorbiscomment_calculate_length_(object);
                                break;
                        case FLAC__METADATA_TYPE_CUESHEET:
                                cuesheet_calculate_length_(object);
                                break;
+                       case FLAC__METADATA_TYPE_PICTURE:
+                               object->length = (
+                                       FLAC__STREAM_METADATA_PICTURE_TYPE_LEN +
+                                       FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN + /* empty mime_type string */
+                                       FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN + /* empty description string */
+                                       FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN +
+                                       FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN +
+                                       FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN +
+                                       FLAC__STREAM_METADATA_PICTURE_COLORS_LEN +
+                                       FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN +
+                                       0 /* no data */
+                               ) / 8;
+                               object->data.picture.type = FLAC__STREAM_METADATA_PICTURE_TYPE_OTHER;
+                               object->data.picture.mime_type = 0;
+                               object->data.picture.description = 0;
+                               /* calloc() took care of this for us:
+                               object->data.picture.width = 0;
+                               object->data.picture.height = 0;
+                               object->data.picture.depth = 0;
+                               object->data.picture.colors = 0;
+                               object->data.picture.data_length = 0;
+                               object->data.picture.data = 0;
+                               */
+                               /* now initialize mime_type and description with empty strings to make things easier on the client */
+                               if(!copy_cstring_(&object->data.picture.mime_type, "")) {
+                                       free(object);
+                                       return 0;
+                               }
+                               if(!copy_cstring_((char**)(&object->data.picture.description), "")) {
+                                       if(object->data.picture.mime_type)
+                                               free(object->data.picture.mime_type);
+                                       free(object);
+                                       return 0;
+                               }
+                               break;
                        default:
                                /* calloc() took care of this for us:
                                object->length = 0;
@@ -408,6 +538,10 @@ FLAC_API FLAC__StreamMetadata *FLAC__metadata_object_clone(const FLAC__StreamMet
                        case FLAC__METADATA_TYPE_PADDING:
                                break;
                        case FLAC__METADATA_TYPE_APPLICATION:
+                               if(to->length < FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8) { /* underflow check */
+                                       FLAC__metadata_object_delete(to);
+                                       return 0;
+                               }
                                memcpy(&to->data.application.id, &object->data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8);
                                if(!copy_bytes_(&to->data.application.data, object->data.application.data, object->length - FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8)) {
                                        FLAC__metadata_object_delete(to);
@@ -416,6 +550,10 @@ FLAC_API FLAC__StreamMetadata *FLAC__metadata_object_clone(const FLAC__StreamMet
                                break;
                        case FLAC__METADATA_TYPE_SEEKTABLE:
                                to->data.seek_table.num_points = object->data.seek_table.num_points;
+                               if(to->data.seek_table.num_points > UINT32_MAX / sizeof(FLAC__StreamMetadata_SeekPoint)) { /* overflow check */
+                                       FLAC__metadata_object_delete(to);
+                                       return 0;
+                               }
                                if(!copy_bytes_((FLAC__byte**)&to->data.seek_table.points, (FLAC__byte*)object->data.seek_table.points, object->data.seek_table.num_points * sizeof(FLAC__StreamMetadata_SeekPoint))) {
                                        FLAC__metadata_object_delete(to);
                                        return 0;
@@ -458,6 +596,26 @@ FLAC_API FLAC__StreamMetadata *FLAC__metadata_object_clone(const FLAC__StreamMet
                                        }
                                }
                                break;
+                       case FLAC__METADATA_TYPE_PICTURE:
+                               to->data.picture.type = object->data.picture.type;
+                               if(!copy_cstring_(&to->data.picture.mime_type, object->data.picture.mime_type)) {
+                                       FLAC__metadata_object_delete(to);
+                                       return 0;
+                               }
+                               if(!copy_cstring_((char**)(&to->data.picture.description), (const char*)object->data.picture.description)) {
+                                       FLAC__metadata_object_delete(to);
+                                       return 0;
+                               }
+                               to->data.picture.width = object->data.picture.width;
+                               to->data.picture.height = object->data.picture.height;
+                               to->data.picture.depth = object->data.picture.depth;
+                               to->data.picture.colors = object->data.picture.colors;
+                               to->data.picture.data_length = object->data.picture.data_length;
+                               if(!copy_bytes_((&to->data.picture.data), object->data.picture.data, object->data.picture.data_length)) {
+                                       FLAC__metadata_object_delete(to);
+                                       return 0;
+                               }
+                               break;
                        default:
                                if(!copy_bytes_(&to->data.unknown.data, object->data.unknown.data, object->length)) {
                                        FLAC__metadata_object_delete(to);
@@ -506,6 +664,20 @@ void FLAC__metadata_object_delete_data(FLAC__StreamMetadata *object)
                                cuesheet_track_array_delete_(object->data.cue_sheet.tracks, object->data.cue_sheet.num_tracks);
                        }
                        break;
+               case FLAC__METADATA_TYPE_PICTURE:
+                       if(0 != object->data.picture.mime_type) {
+                               free(object->data.picture.mime_type);
+                               object->data.picture.mime_type = 0;
+                       }
+                       if(0 != object->data.picture.description) {
+                               free(object->data.picture.description);
+                               object->data.picture.description = 0;
+                       }
+                       if(0 != object->data.picture.data) {
+                               free(object->data.picture.data);
+                               object->data.picture.data = 0;
+                       }
+                       break;
                default:
                        if(0 != object->data.unknown.data) {
                                free(object->data.unknown.data);
@@ -660,6 +832,29 @@ static FLAC__bool compare_block_data_cuesheet_(const FLAC__StreamMetadata_CueShe
        return true;
 }
 
+static FLAC__bool compare_block_data_picture_(const FLAC__StreamMetadata_Picture *block1, const FLAC__StreamMetadata_Picture *block2)
+{
+       if(block1->type != block2->type)
+               return false;
+       if(block1->mime_type != block2->mime_type && (0 == block1->mime_type || 0 == block2->mime_type || strcmp(block1->mime_type, block2->mime_type)))
+               return false;
+       if(block1->description != block2->description && (0 == block1->description || 0 == block2->description || strcmp((const char *)block1->description, (const char *)block2->description)))
+               return false;
+       if(block1->width != block2->width)
+               return false;
+       if(block1->height != block2->height)
+               return false;
+       if(block1->depth != block2->depth)
+               return false;
+       if(block1->colors != block2->colors)
+               return false;
+       if(block1->data_length != block2->data_length)
+               return false;
+       if(block1->data != block2->data && (0 == block1->data || 0 == block2->data || memcmp(block1->data, block2->data, block1->data_length)))
+               return false;
+       return true;
+}
+
 static FLAC__bool compare_block_data_unknown_(const FLAC__StreamMetadata_Unknown *block1, const FLAC__StreamMetadata_Unknown *block2, unsigned block_length)
 {
        FLAC__ASSERT(0 != block1);
@@ -698,6 +893,8 @@ FLAC_API FLAC__bool FLAC__metadata_object_is_equal(const FLAC__StreamMetadata *b
                        return compare_block_data_vorbiscomment_(&block1->data.vorbis_comment, &block2->data.vorbis_comment);
                case FLAC__METADATA_TYPE_CUESHEET:
                        return compare_block_data_cuesheet_(&block1->data.cue_sheet, &block2->data.cue_sheet);
+               case FLAC__METADATA_TYPE_PICTURE:
+                       return compare_block_data_picture_(&block1->data.picture, &block2->data.picture);
                default:
                        return compare_block_data_unknown_(&block1->data.unknown, &block2->data.unknown, block1->length);
        }
@@ -742,8 +939,12 @@ FLAC_API FLAC__bool FLAC__metadata_object_seektable_resize_points(FLAC__StreamMe
                        return false;
        }
        else {
-               const unsigned old_size = object->data.seek_table.num_points * sizeof(FLAC__StreamMetadata_SeekPoint);
-               const unsigned new_size = new_num_points * sizeof(FLAC__StreamMetadata_SeekPoint);
+               const size_t old_size = object->data.seek_table.num_points * sizeof(FLAC__StreamMetadata_SeekPoint);
+               const size_t new_size = new_num_points * sizeof(FLAC__StreamMetadata_SeekPoint);
+
+               /* overflow check */
+               if(new_num_points > UINT32_MAX / sizeof(FLAC__StreamMetadata_SeekPoint))
+                       return false;
 
                FLAC__ASSERT(object->data.seek_table.num_points > 0);
 
@@ -751,7 +952,7 @@ FLAC_API FLAC__bool FLAC__metadata_object_seektable_resize_points(FLAC__StreamMe
                        free(object->data.seek_table.points);
                        object->data.seek_table.points = 0;
                }
-               else if(0 == (object->data.seek_table.points = (FLAC__StreamMetadata_SeekPoint*)realloc(object->data.seek_table.points, new_size)))
+               else if(0 == (object->data.seek_table.points = realloc(object->data.seek_table.points, new_size)))
                        return false;
 
                /* if growing, set new elements to placeholders */
@@ -885,7 +1086,7 @@ FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_spaced_point
        FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
        FLAC__ASSERT(total_samples > 0);
 
-       if(num > 0) {
+       if(num > 0 && total_samples > 0) {
                FLAC__StreamMetadata_SeekTable *seek_table = &object->data.seek_table;
                unsigned i, j;
 
@@ -904,6 +1105,39 @@ FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_spaced_point
        return true;
 }
 
+FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_spaced_points_by_samples(FLAC__StreamMetadata *object, unsigned samples, FLAC__uint64 total_samples)
+{
+       FLAC__ASSERT(0 != object);
+       FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
+       FLAC__ASSERT(samples > 0);
+       FLAC__ASSERT(total_samples > 0);
+
+       if(samples > 0 && total_samples > 0) {
+               FLAC__StreamMetadata_SeekTable *seek_table = &object->data.seek_table;
+               unsigned i, j;
+               FLAC__uint64 num, sample;
+
+               num = 1 + total_samples / samples; /* 1+ for the first sample at 0 */
+               /* now account for the fact that we don't place a seekpoint at "total_samples" since samples are number from 0: */
+               if(total_samples % samples == 0)
+                       num--;
+
+               i = seek_table->num_points;
+
+               if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + (unsigned)num))
+                       return false;
+
+               sample = 0;
+               for(j = 0; j < num; i++, j++, sample += samples) {
+                       seek_table->points[i].sample_number = sample;
+                       seek_table->points[i].stream_offset = 0;
+                       seek_table->points[i].frame_samples = 0;
+               }
+       }
+
+       return true;
+}
+
 FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_sort(FLAC__StreamMetadata *object, FLAC__bool compact)
 {
        unsigned unique;
@@ -918,6 +1152,8 @@ FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_sort(FLAC__StreamMe
 
 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_set_vendor_string(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy)
 {
+       if(!FLAC__format_vorbiscomment_entry_value_is_legal(entry.entry, entry.length))
+               return false;
        return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.vendor_string, &entry, copy);
 }
 
@@ -934,8 +1170,12 @@ FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_resize_comments(FLAC__St
                        return false;
        }
        else {
-               const unsigned old_size = object->data.vorbis_comment.num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry);
-               const unsigned new_size = new_num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry);
+               const size_t old_size = object->data.vorbis_comment.num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry);
+               const size_t new_size = new_num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry);
+
+               /* overflow check */
+               if(new_num_comments > UINT32_MAX / sizeof(FLAC__StreamMetadata_VorbisComment_Entry))
+                       return false;
 
                FLAC__ASSERT(object->data.vorbis_comment.num_comments > 0);
 
@@ -951,7 +1191,7 @@ FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_resize_comments(FLAC__St
                        free(object->data.vorbis_comment.comments);
                        object->data.vorbis_comment.comments = 0;
                }
-               else if(0 == (object->data.vorbis_comment.comments = (FLAC__StreamMetadata_VorbisComment_Entry*)realloc(object->data.vorbis_comment.comments, new_size)))
+               else if(0 == (object->data.vorbis_comment.comments = realloc(object->data.vorbis_comment.comments, new_size)))
                        return false;
 
                /* if growing, zero all the length/pointers of new elements */
@@ -970,6 +1210,8 @@ FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_set_comment(FLAC__Stream
        FLAC__ASSERT(0 != object);
        FLAC__ASSERT(comment_num < object->data.vorbis_comment.num_comments);
 
+       if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
+               return false;
        return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.comments[comment_num], &entry, copy);
 }
 
@@ -981,6 +1223,9 @@ FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_insert_comment(FLAC__Str
        FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
        FLAC__ASSERT(comment_num <= object->data.vorbis_comment.num_comments);
 
+       if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
+               return false;
+
        vc = &object->data.vorbis_comment;
 
        if(!FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments+1))
@@ -994,6 +1239,58 @@ FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_insert_comment(FLAC__Str
        return FLAC__metadata_object_vorbiscomment_set_comment(object, comment_num, entry, copy);
 }
 
+FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_append_comment(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy)
+{
+       FLAC__ASSERT(0 != object);
+       FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
+       return FLAC__metadata_object_vorbiscomment_insert_comment(object, object->data.vorbis_comment.num_comments, entry, copy);
+}
+
+FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_replace_comment(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool all, FLAC__bool copy)
+{
+       FLAC__ASSERT(0 != entry.entry && entry.length > 0);
+
+       if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
+               return false;
+
+       {
+               int i;
+               size_t field_name_length;
+               const FLAC__byte *eq = (FLAC__byte*)memchr(entry.entry, '=', entry.length);
+
+               FLAC__ASSERT(0 != eq);
+
+               if(0 == eq)
+                       return false; /* double protection */
+
+               field_name_length = eq-entry.entry;
+
+               i = vorbiscomment_find_entry_from_(object, 0, (const char *)entry.entry, field_name_length);
+               if(i >= 0) {
+                       unsigned index = (unsigned)i;
+                       if(!FLAC__metadata_object_vorbiscomment_set_comment(object, index, entry, copy))
+                               return false;
+                       entry = object->data.vorbis_comment.comments[index];
+                       index++; /* skip over replaced comment */
+                       if(all && index < object->data.vorbis_comment.num_comments) {
+                               i = vorbiscomment_find_entry_from_(object, index, (const char *)entry.entry, field_name_length);
+                               while(i >= 0) {
+                                       index = (unsigned)i;
+                                       if(!FLAC__metadata_object_vorbiscomment_delete_comment(object, index))
+                                               return false;
+                                       if(index < object->data.vorbis_comment.num_comments)
+                                               i = vorbiscomment_find_entry_from_(object, index, (const char *)entry.entry, field_name_length);
+                                       else
+                                               i = -1;
+                               }
+                       }
+                       return true;
+               }
+               else
+                       return FLAC__metadata_object_vorbiscomment_append_comment(object, entry, copy);
+       }
+}
+
 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_delete_comment(FLAC__StreamMetadata *object, unsigned comment_num)
 {
        FLAC__StreamMetadata_VorbisComment *vc;
@@ -1016,32 +1313,83 @@ FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_delete_comment(FLAC__Str
        return FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments-1);
 }
 
-FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_entry_matches(const FLAC__StreamMetadata_VorbisComment_Entry *entry, const char *field_name, unsigned field_name_length)
+FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(FLAC__StreamMetadata_VorbisComment_Entry *entry, const char *field_name, const char *field_value)
 {
-       const FLAC__byte *eq = (FLAC__byte*)memchr(entry->entry, '=', entry->length);
-#if defined _MSC_VER || defined __MINGW32__
+       FLAC__ASSERT(0 != entry);
+       FLAC__ASSERT(0 != field_name);
+       FLAC__ASSERT(0 != field_value);
+
+       if(!FLAC__format_vorbiscomment_entry_name_is_legal(field_name))
+               return false;
+       if(!FLAC__format_vorbiscomment_entry_value_is_legal((const FLAC__byte *)field_value, (unsigned)(-1)))
+               return false;
+
+       {
+               const size_t nn = strlen(field_name);
+               const size_t nv = strlen(field_value);
+               entry->length = nn + 1 /*=*/ + nv;
+               if(0 == (entry->entry = safe_malloc_add_4op_(nn, /*+*/1, /*+*/nv, /*+*/1)))
+                       return false;
+               memcpy(entry->entry, field_name, nn);
+               entry->entry[nn] = '=';
+               memcpy(entry->entry+nn+1, field_value, nv);
+               entry->entry[entry->length] = '\0';
+       }
+
+       return true;
+}
+
+FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_entry_to_name_value_pair(const FLAC__StreamMetadata_VorbisComment_Entry entry, char **field_name, char **field_value)
+{
+       FLAC__ASSERT(0 != entry.entry && entry.length > 0);
+       FLAC__ASSERT(0 != field_name);
+       FLAC__ASSERT(0 != field_value);
+
+       if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
+               return false;
+
+       {
+               const FLAC__byte *eq = (FLAC__byte*)memchr(entry.entry, '=', entry.length);
+               const size_t nn = eq-entry.entry;
+               const size_t nv = entry.length-nn-1; /* -1 for the '=' */
+               FLAC__ASSERT(0 != eq);
+               if(0 == eq)
+                       return false; /* double protection */
+               if(0 == (*field_name = safe_malloc_add_2op_(nn, /*+*/1)))
+                       return false;
+               if(0 == (*field_value = safe_malloc_add_2op_(nv, /*+*/1))) {
+                       free(*field_name);
+                       return false;
+               }
+               memcpy(*field_name, entry.entry, nn);
+               memcpy(*field_value, entry.entry+nn+1, nv);
+               (*field_name)[nn] = '\0';
+               (*field_value)[nv] = '\0';
+       }
+
+       return true;
+}
+
+FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_entry_matches(const FLAC__StreamMetadata_VorbisComment_Entry entry, const char *field_name, unsigned field_name_length)
+{
+       FLAC__ASSERT(0 != entry.entry && entry.length > 0);
+       {
+               const FLAC__byte *eq = (FLAC__byte*)memchr(entry.entry, '=', entry.length);
+#if defined _MSC_VER || defined __BORLANDC__ || defined __MINGW32__
 #define FLAC__STRNCASECMP strnicmp
 #else
 #define FLAC__STRNCASECMP strncasecmp
 #endif
-       return (0 != eq && (unsigned)(eq-entry->entry) == field_name_length && 0 == FLAC__STRNCASECMP(field_name, (const char *)entry->entry, field_name_length));
+               return (0 != eq && (unsigned)(eq-entry.entry) == field_name_length && 0 == FLAC__STRNCASECMP(field_name, (const char *)entry.entry, field_name_length));
 #undef FLAC__STRNCASECMP
+       }
 }
 
 FLAC_API int FLAC__metadata_object_vorbiscomment_find_entry_from(const FLAC__StreamMetadata *object, unsigned offset, const char *field_name)
 {
-       const unsigned field_name_length = strlen(field_name);
-       unsigned i;
-
-       FLAC__ASSERT(0 != object);
-       FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
-
-       for(i = offset; i < object->data.vorbis_comment.num_comments; i++) {
-               if(FLAC__metadata_object_vorbiscomment_entry_matches(object->data.vorbis_comment.comments + i, field_name, field_name_length))
-                       return (int)i;
-       }
+       FLAC__ASSERT(0 != field_name);
 
-       return -1;
+       return vorbiscomment_find_entry_from_(object, offset, field_name, strlen(field_name));
 }
 
 FLAC_API int FLAC__metadata_object_vorbiscomment_remove_entry_matching(FLAC__StreamMetadata *object, const char *field_name)
@@ -1053,7 +1401,7 @@ FLAC_API int FLAC__metadata_object_vorbiscomment_remove_entry_matching(FLAC__Str
        FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
 
        for(i = 0; i < object->data.vorbis_comment.num_comments; i++) {
-               if(FLAC__metadata_object_vorbiscomment_entry_matches(object->data.vorbis_comment.comments + i, field_name, field_name_length)) {
+               if(FLAC__metadata_object_vorbiscomment_entry_matches(object->data.vorbis_comment.comments[i], field_name, field_name_length)) {
                        if(!FLAC__metadata_object_vorbiscomment_delete_comment(object, i))
                                return -1;
                        else
@@ -1076,7 +1424,7 @@ FLAC_API int FLAC__metadata_object_vorbiscomment_remove_entries_matching(FLAC__S
 
        /* must delete from end to start otherwise it will interfere with our iteration */
        for(i = (int)object->data.vorbis_comment.num_comments - 1; ok && i >= 0; i--) {
-               if(FLAC__metadata_object_vorbiscomment_entry_matches(object->data.vorbis_comment.comments + i, field_name, field_name_length)) {
+               if(FLAC__metadata_object_vorbiscomment_entry_matches(object->data.vorbis_comment.comments[i], field_name, field_name_length)) {
                        matching++;
                        ok &= FLAC__metadata_object_vorbiscomment_delete_comment(object, (unsigned)i);
                }
@@ -1085,9 +1433,9 @@ FLAC_API int FLAC__metadata_object_vorbiscomment_remove_entries_matching(FLAC__S
        return ok? (int)matching : -1;
 }
 
-FLAC_API FLAC__StreamMetadata_CueSheet_Track *FLAC__metadata_object_cuesheet_track_new()
+FLAC_API FLAC__StreamMetadata_CueSheet_Track *FLAC__metadata_object_cuesheet_track_new(void)
 {
-       return (FLAC__StreamMetadata_CueSheet_Track*)calloc(1, sizeof(FLAC__StreamMetadata_CueSheet_Track));
+       return calloc(1, sizeof(FLAC__StreamMetadata_CueSheet_Track));
 }
 
 FLAC_API FLAC__StreamMetadata_CueSheet_Track *FLAC__metadata_object_cuesheet_track_clone(const FLAC__StreamMetadata_CueSheet_Track *object)
@@ -1139,8 +1487,12 @@ FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_resize_indices(FLAC__St
                        return false;
        }
        else {
-               const unsigned old_size = track->num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index);
-               const unsigned new_size = new_num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index);
+               const size_t old_size = track->num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index);
+               const size_t new_size = new_num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index);
+
+               /* overflow check */
+               if(new_num_indices > UINT32_MAX / sizeof(FLAC__StreamMetadata_CueSheet_Index))
+                       return false;
 
                FLAC__ASSERT(track->num_indices > 0);
 
@@ -1148,7 +1500,7 @@ FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_resize_indices(FLAC__St
                        free(track->indices);
                        track->indices = 0;
                }
-               else if(0 == (track->indices = (FLAC__StreamMetadata_CueSheet_Index*)realloc(track->indices, new_size)))
+               else if(0 == (track->indices = realloc(track->indices, new_size)))
                        return false;
 
                /* if growing, zero all the lengths/pointers of new elements */
@@ -1223,8 +1575,12 @@ FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_resize_tracks(FLAC__StreamMet
                        return false;
        }
        else {
-               const unsigned old_size = object->data.cue_sheet.num_tracks * sizeof(FLAC__StreamMetadata_CueSheet_Track);
-               const unsigned new_size = new_num_tracks * sizeof(FLAC__StreamMetadata_CueSheet_Track);
+               const size_t old_size = object->data.cue_sheet.num_tracks * sizeof(FLAC__StreamMetadata_CueSheet_Track);
+               const size_t new_size = new_num_tracks * sizeof(FLAC__StreamMetadata_CueSheet_Track);
+
+               /* overflow check */
+               if(new_num_tracks > UINT32_MAX / sizeof(FLAC__StreamMetadata_CueSheet_Track))
+                       return false;
 
                FLAC__ASSERT(object->data.cue_sheet.num_tracks > 0);
 
@@ -1240,7 +1596,7 @@ FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_resize_tracks(FLAC__StreamMet
                        free(object->data.cue_sheet.tracks);
                        object->data.cue_sheet.tracks = 0;
                }
-               else if(0 == (object->data.cue_sheet.tracks = (FLAC__StreamMetadata_CueSheet_Track*)realloc(object->data.cue_sheet.tracks, new_size)))
+               else if(0 == (object->data.cue_sheet.tracks = realloc(object->data.cue_sheet.tracks, new_size)))
                        return false;
 
                /* if growing, zero all the lengths/pointers of new elements */
@@ -1319,3 +1675,150 @@ FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_is_legal(const FLAC__StreamMe
 
        return FLAC__format_cuesheet_is_legal(&object->data.cue_sheet, check_cd_da_subset, violation);
 }
+
+static FLAC__uint64 get_index_01_offset_(const FLAC__StreamMetadata_CueSheet *cs, unsigned track)
+{
+       if (track >= (cs->num_tracks-1) || cs->tracks[track].num_indices < 1)
+               return 0;
+       else if (cs->tracks[track].indices[0].number == 1)
+               return cs->tracks[track].indices[0].offset + cs->tracks[track].offset + cs->lead_in;
+       else if (cs->tracks[track].num_indices < 2)
+               return 0;
+       else if (cs->tracks[track].indices[1].number == 1)
+               return cs->tracks[track].indices[1].offset + cs->tracks[track].offset + cs->lead_in;
+       else
+               return 0;
+}
+
+static FLAC__uint32 cddb_add_digits_(FLAC__uint32 x)
+{
+       FLAC__uint32 n = 0;
+       while (x) {
+               n += (x%10);
+               x /= 10;
+       }
+       return n;
+}
+
+/*@@@@add to tests*/
+FLAC_API FLAC__uint32 FLAC__metadata_object_cuesheet_calculate_cddb_id(const FLAC__StreamMetadata *object)
+{
+       const FLAC__StreamMetadata_CueSheet *cs;
+
+       FLAC__ASSERT(0 != object);
+       FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
+
+       cs = &object->data.cue_sheet;
+
+       if (cs->num_tracks < 2) /* need at least one real track and the lead-out track */
+               return 0;
+
+       {
+               FLAC__uint32 i, length, sum = 0;
+               for (i = 0; i < (cs->num_tracks-1); i++) /* -1 to avoid counting the lead-out */
+                       sum += cddb_add_digits_((FLAC__uint32)(get_index_01_offset_(cs, i) / 44100));
+               length = (FLAC__uint32)((cs->tracks[cs->num_tracks-1].offset+cs->lead_in) / 44100) - (FLAC__uint32)(get_index_01_offset_(cs, 0) / 44100);
+
+               return (sum % 0xFF) << 24 | length << 8 | (FLAC__uint32)(cs->num_tracks-1);
+       }
+}
+
+FLAC_API FLAC__bool FLAC__metadata_object_picture_set_mime_type(FLAC__StreamMetadata *object, char *mime_type, FLAC__bool copy)
+{
+       char *old;
+       size_t old_length, new_length;
+
+       FLAC__ASSERT(0 != object);
+       FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_PICTURE);
+       FLAC__ASSERT(0 != mime_type);
+
+       old = object->data.picture.mime_type;
+       old_length = old? strlen(old) : 0;
+       new_length = strlen(mime_type);
+
+       /* do the copy first so that if we fail we leave the object untouched */
+       if(copy) {
+               if(new_length >= SIZE_MAX) /* overflow check */
+                       return false;
+               if(!copy_bytes_((FLAC__byte**)(&object->data.picture.mime_type), (FLAC__byte*)mime_type, new_length+1))
+                       return false;
+       }
+       else {
+               object->data.picture.mime_type = mime_type;
+       }
+
+       if(0 != old)
+               free(old);
+
+       object->length -= old_length;
+       object->length += new_length;
+       return true;
+}
+
+FLAC_API FLAC__bool FLAC__metadata_object_picture_set_description(FLAC__StreamMetadata *object, FLAC__byte *description, FLAC__bool copy)
+{
+       FLAC__byte *old;
+       size_t old_length, new_length;
+
+       FLAC__ASSERT(0 != object);
+       FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_PICTURE);
+       FLAC__ASSERT(0 != description);
+
+       old = object->data.picture.description;
+       old_length = old? strlen((const char *)old) : 0;
+       new_length = strlen((const char *)description);
+
+       /* do the copy first so that if we fail we leave the object untouched */
+       if(copy) {
+               if(new_length >= SIZE_MAX) /* overflow check */
+                       return false;
+               if(!copy_bytes_(&object->data.picture.description, description, new_length+1))
+                       return false;
+       }
+       else {
+               object->data.picture.description = description;
+       }
+
+       if(0 != old)
+               free(old);
+
+       object->length -= old_length;
+       object->length += new_length;
+       return true;
+}
+
+FLAC_API FLAC__bool FLAC__metadata_object_picture_set_data(FLAC__StreamMetadata *object, FLAC__byte *data, FLAC__uint32 length, FLAC__bool copy)
+{
+       FLAC__byte *old;
+
+       FLAC__ASSERT(0 != object);
+       FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_PICTURE);
+       FLAC__ASSERT((0 != data && length > 0) || (0 == data && length == 0 && copy == false));
+
+       old = object->data.picture.data;
+
+       /* do the copy first so that if we fail we leave the object untouched */
+       if(copy) {
+               if(!copy_bytes_(&object->data.picture.data, data, length))
+                       return false;
+       }
+       else {
+               object->data.picture.data = data;
+       }
+
+       if(0 != old)
+               free(old);
+
+       object->length -= object->data.picture.data_length;
+       object->data.picture.data_length = length;
+       object->length += length;
+       return true;
+}
+
+FLAC_API FLAC__bool FLAC__metadata_object_picture_is_legal(const FLAC__StreamMetadata *object, const char **violation)
+{
+       FLAC__ASSERT(0 != object);
+       FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_PICTURE);
+
+       return FLAC__format_picture_is_legal(&object->data.picture, violation);
+}