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; FLAC__ASSERT(0 != object); FLAC__ASSERT(0 != dest); FLAC__ASSERT(0 != src); FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT); FLAC__ASSERT((0 != src->entry && src->length > 0) || (0 == src->entry && src->length == 0)); 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; } else { /* either we're not copying or the src is null */ *dest = *src; } if(0 != save) free(save); vorbiscomment_calculate_length_(object); return true; }
FLAC_API FLAC__StreamMetadata *FLAC__metadata_object_new(FLAC__MetadataType type) { FLAC__StreamMetadata *object; if(type > FLAC__MAX_METADATA_TYPE_CODE) return 0; object = (FLAC__StreamMetadata*)calloc(1, sizeof(FLAC__StreamMetadata)); if(0 != object) { object->is_last = false; object->type = type; switch(type) { case FLAC__METADATA_TYPE_STREAMINFO: object->length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH; break; case FLAC__METADATA_TYPE_PADDING: /* calloc() took care of this for us: object->length = 0; */ break; case FLAC__METADATA_TYPE_APPLICATION: object->length = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8; /* calloc() took care of this for us: object->data.application.data = 0; */ break; case FLAC__METADATA_TYPE_SEEKTABLE: /* calloc() took care of this for us: object->length = 0; object->data.seek_table.num_points = 0; object->data.seek_table.points = 0; */ 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); } break; case FLAC__METADATA_TYPE_CUESHEET: cuesheet_calculate_length_(object); break; default: /* calloc() took care of this for us: object->length = 0; object->data.unknown.data = 0; */ break; } } return object; }
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; FLAC__ASSERT(0 != object); FLAC__ASSERT(0 != dest); FLAC__ASSERT(0 != src); FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT); FLAC__ASSERT((0 != src->entry && src->length > 0) || (0 == src->entry && src->length == 0)); save = dest->entry; 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 { /* the src is null */ *dest = *src; } if(0 != save) free(save); vorbiscomment_calculate_length_(object); return true; }
FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_resize_comments(FLAC__StreamMetadata *object, unsigned new_num_comments) { FLAC__ASSERT(0 != object); FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT); if(0 == object->data.vorbis_comment.comments) { FLAC__ASSERT(object->data.vorbis_comment.num_comments == 0); if(0 == new_num_comments) return true; else if(0 == (object->data.vorbis_comment.comments = vorbiscomment_entry_array_new_(new_num_comments))) 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); FLAC__ASSERT(object->data.vorbis_comment.num_comments > 0); /* if shrinking, free the truncated entries */ if(new_num_comments < object->data.vorbis_comment.num_comments) { unsigned i; for(i = new_num_comments; i < object->data.vorbis_comment.num_comments; i++) if(0 != object->data.vorbis_comment.comments[i].entry) free(object->data.vorbis_comment.comments[i].entry); } if(new_size == 0) { 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))) return false; /* if growing, zero all the length/pointers of new elements */ if(new_size > old_size) memset(object->data.vorbis_comment.comments + object->data.vorbis_comment.num_comments, 0, new_size - old_size); } object->data.vorbis_comment.num_comments = new_num_comments; vorbiscomment_calculate_length_(object); return true; }