bring in what was FLAC__seek_table_is_valid()
[flac.git] / src / libFLAC / metadata_object.c
index a1b6989..aa379fa 100644 (file)
@@ -167,7 +167,7 @@ static FLAC__StreamMetaData_VorbisComment_Entry *vorbiscomment_entry_array_copy_
        return return_array;
 }
 
-static FLAC__bool vorbiscomment_set_entry_(FLAC__StreamMetaData *object, FLAC__StreamMetaData_VorbisComment_Entry *dest, FLAC__StreamMetaData_VorbisComment_Entry *src, FLAC__bool copy)
+static FLAC__bool vorbiscomment_set_entry_(FLAC__StreamMetaData *object, FLAC__StreamMetaData_VorbisComment_Entry *dest, const FLAC__StreamMetaData_VorbisComment_Entry *src, FLAC__bool copy)
 {
        FLAC__byte *save;
 
@@ -177,7 +177,7 @@ static FLAC__bool vorbiscomment_set_entry_(FLAC__StreamMetaData *object, FLAC__S
        FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
        FLAC__ASSERT((0 != src->entry && src->length > 0) || (0 == src->entry && src->length == 0 && copy == false));
 
-       save = src->entry;
+       save = dest->entry;
 
        /* do the copy first so that if we fail we leave the object untouched */
        if(copy) {
@@ -426,7 +426,7 @@ FLAC__bool FLAC__metadata_object_seektable_insert_point(FLAC__StreamMetaData *ob
        int i;
 
        FLAC__ASSERT(0 != object);
-       FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
+       FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
        FLAC__ASSERT(object->data.seek_table.num_points >= point_num);
 
        if(!FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points+1))
@@ -456,9 +456,36 @@ FLAC__bool FLAC__metadata_object_seektable_delete_point(FLAC__StreamMetaData *ob
        return FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points-1);
 }
 
-FLAC__bool FLAC__metadata_object_vorbiscomment_set_vendor_string(FLAC__StreamMetaData *object, FLAC__StreamMetaData_VorbisComment_Entry *entry, FLAC__bool copy)
+FLAC__bool FLAC__metadata_object_seektable_is_legal(const FLAC__StreamMetaData *object)
 {
-       return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.vendor_string, entry, copy);
+       unsigned i;
+       FLAC__uint64 last_sample_number = 0;
+       FLAC__bool got_last = false;
+
+       FLAC__ASSERT(0 != object);
+       FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
+
+       {
+               const FLAC__StreamMetaData_SeekTable *seek_table = &object->data.seek_table;
+
+               for(i = 0; i < seek_table->num_points; i++) {
+                       if(seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER) {
+                               if(got_last) {
+                                       if(seek_table->points[i].sample_number <= last_sample_number)
+                                               return false;
+                               }
+                               last_sample_number = seek_table->points[i].sample_number;
+                               got_last = true;
+                       }
+               }
+       }
+
+       return true;
+}
+
+FLAC__bool FLAC__metadata_object_vorbiscomment_set_vendor_string(FLAC__StreamMetaData *object, FLAC__StreamMetaData_VorbisComment_Entry entry, FLAC__bool copy)
+{
+       return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.vendor_string, &entry, copy);
 }
 
 FLAC__bool FLAC__metadata_object_vorbiscomment_resize_comments(FLAC__StreamMetaData *object, unsigned new_num_comments)
@@ -505,49 +532,50 @@ FLAC__bool FLAC__metadata_object_vorbiscomment_resize_comments(FLAC__StreamMetaD
        return true;
 }
 
-FLAC__bool FLAC__metadata_object_vorbiscomment_set_comment(FLAC__StreamMetaData *object, unsigned comment_num, FLAC__StreamMetaData_VorbisComment_Entry *entry, FLAC__bool copy)
+FLAC__bool FLAC__metadata_object_vorbiscomment_set_comment(FLAC__StreamMetaData *object, unsigned comment_num, FLAC__StreamMetaData_VorbisComment_Entry entry, FLAC__bool copy)
 {
-       return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.comments[comment_num], entry, copy);
+       return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.comments[comment_num], &entry, copy);
 }
 
-FLAC__bool FLAC__metadata_object_vorbiscomment_insert_comment(FLAC__StreamMetaData *object, unsigned comment_num, FLAC__StreamMetaData_VorbisComment_Entry *entry, FLAC__bool copy)
+FLAC__bool FLAC__metadata_object_vorbiscomment_insert_comment(FLAC__StreamMetaData *object, unsigned comment_num, FLAC__StreamMetaData_VorbisComment_Entry entry, FLAC__bool copy)
 {
-       int i;
+       FLAC__StreamMetaData_VorbisComment *vc;
 
        FLAC__ASSERT(0 != object);
-       FLAC__ASSERT(0 != entry);
        FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
        FLAC__ASSERT(object->data.vorbis_comment.num_comments >= comment_num);
 
-       if(!FLAC__metadata_object_vorbiscomment_resize_comments(object, object->data.vorbis_comment.num_comments+1))
+       vc = &object->data.vorbis_comment;
+
+       if(!FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments+1))
                return false;
 
        /* move all comments >= comment_num forward one space */
-       for(i = (int)object->data.vorbis_comment.num_comments-1; i > (int)comment_num; i--)
-               object->data.vorbis_comment.comments[i] = object->data.vorbis_comment.comments[i-1];
-       object->data.vorbis_comment.comments[i].length = 0;
-       object->data.vorbis_comment.comments[i].entry = 0;
+       memmove(&vc->comments[comment_num+1], &vc->comments[comment_num], sizeof(FLAC__StreamMetaData_VorbisComment_Entry)*(vc->num_comments-1-comment_num));
+       vc->comments[comment_num].length = 0;
+       vc->comments[comment_num].entry = 0;
 
        return FLAC__metadata_object_vorbiscomment_set_comment(object, comment_num, entry, copy);
 }
 
 FLAC__bool FLAC__metadata_object_vorbiscomment_delete_comment(FLAC__StreamMetaData *object, unsigned comment_num)
 {
-       unsigned i;
+       FLAC__StreamMetaData_VorbisComment *vc;
 
        FLAC__ASSERT(0 != object);
        FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
        FLAC__ASSERT(object->data.vorbis_comment.num_comments > comment_num);
 
+       vc = &object->data.vorbis_comment;
+
        /* free the comment at comment_num */
-       if(0 != object->data.vorbis_comment.comments[comment_num].entry)
-               free(object->data.vorbis_comment.comments[comment_num].entry);
+       if(0 != vc->comments[comment_num].entry)
+               free(vc->comments[comment_num].entry);
 
        /* move all comments > comment_num backward one space */
-       for(i = comment_num; i < object->data.vorbis_comment.num_comments-1; i++)
-               object->data.vorbis_comment.comments[i] = object->data.vorbis_comment.comments[i+1];
-       object->data.vorbis_comment.comments[i].length = 0;
-       object->data.vorbis_comment.comments[i].entry = 0;
+       memmove(&vc->comments[comment_num], &vc->comments[comment_num+1], sizeof(FLAC__StreamMetaData_VorbisComment_Entry)*(vc->num_comments-comment_num-1));
+       vc->comments[vc->num_comments-1].length = 0;
+       vc->comments[vc->num_comments-1].entry = 0;
 
-       return FLAC__metadata_object_vorbiscomment_resize_comments(object, object->data.vorbis_comment.num_comments-1);
+       return FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments-1);
 }