📄 metadata_object.c
字号:
/* test_libFLAC - Unit tester for libFLAC
* Copyright (C) 2002,2003,2004,2005 Josh Coalson
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "FLAC/assert.h"
#include "FLAC/metadata.h"
#include "metadata_utils.h"
#include <stdio.h>
#include <stdlib.h> /* for malloc() */
#include <string.h> /* for memcmp() */
static FLAC__byte *make_dummydata_(FLAC__byte *dummydata, unsigned len)
{
FLAC__byte *ret;
if(0 == (ret = (FLAC__byte*)malloc(len))) {
printf("FAILED, malloc error\n");
exit(1);
}
else
memcpy(ret, dummydata, len);
return ret;
}
static FLAC__bool compare_track_(const FLAC__StreamMetadata_CueSheet_Track *from, const FLAC__StreamMetadata_CueSheet_Track *to)
{
unsigned i;
if(from->offset != to->offset) {
printf("FAILED, track offset mismatch, expected %llu, got %llu\n", to->offset, from->offset);
return false;
}
if(from->number != to->number) {
printf("FAILED, track number mismatch, expected %u, got %u\n", (unsigned)to->number, (unsigned)from->number);
return false;
}
if(0 != strcmp(from->isrc, to->isrc)) {
printf("FAILED, track number mismatch, expected %s, got %s\n", to->isrc, from->isrc);
return false;
}
if(from->type != to->type) {
printf("FAILED, track type mismatch, expected %u, got %u\n", (unsigned)to->type, (unsigned)from->type);
return false;
}
if(from->pre_emphasis != to->pre_emphasis) {
printf("FAILED, track pre_emphasis mismatch, expected %u, got %u\n", (unsigned)to->pre_emphasis, (unsigned)from->pre_emphasis);
return false;
}
if(from->num_indices != to->num_indices) {
printf("FAILED, track num_indices mismatch, expected %u, got %u\n", (unsigned)to->num_indices, (unsigned)from->num_indices);
return false;
}
if(0 == to->indices || 0 == from->indices) {
if(to->indices != from->indices) {
printf("FAILED, track indices mismatch\n");
return false;
}
}
else {
for(i = 0; i < to->num_indices; i++) {
if(from->indices[i].offset != to->indices[i].offset) {
printf("FAILED, track indices[%u].offset mismatch, expected %llu, got %llu\n", i, to->indices[i].offset, from->indices[i].offset);
return false;
}
if(from->indices[i].number != to->indices[i].number) {
printf("FAILED, track indices[%u].number mismatch, expected %u, got %u\n", i, (unsigned)to->indices[i].number, (unsigned)from->indices[i].number);
return false;
}
}
}
return true;
}
static FLAC__bool compare_seekpoint_array_(const FLAC__StreamMetadata_SeekPoint *from, const FLAC__StreamMetadata_SeekPoint *to, unsigned n)
{
unsigned i;
FLAC__ASSERT(0 != from);
FLAC__ASSERT(0 != to);
for(i = 0; i < n; i++) {
if(from[i].sample_number != to[i].sample_number) {
printf("FAILED, point[%u].sample_number mismatch, expected %llu, got %llu\n", i, to[i].sample_number, from[i].sample_number);
return false;
}
if(from[i].stream_offset != to[i].stream_offset) {
printf("FAILED, point[%u].stream_offset mismatch, expected %llu, got %llu\n", i, to[i].stream_offset, from[i].stream_offset);
return false;
}
if(from[i].frame_samples != to[i].frame_samples) {
printf("FAILED, point[%u].frame_samples mismatch, expected %u, got %u\n", i, to[i].frame_samples, from[i].frame_samples);
return false;
}
}
return true;
}
static FLAC__bool check_seektable_(const FLAC__StreamMetadata *block, unsigned num_points, const FLAC__StreamMetadata_SeekPoint *array)
{
const unsigned expected_length = num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
if(block->length != expected_length) {
printf("FAILED, bad length, expected %u, got %u\n", expected_length, block->length);
return false;
}
if(block->data.seek_table.num_points != num_points) {
printf("FAILED, expected %u point, got %u\n", num_points, block->data.seek_table.num_points);
return false;
}
if(0 == array) {
if(0 != block->data.seek_table.points) {
printf("FAILED, 'points' pointer is not null\n");
return false;
}
}
else {
if(!compare_seekpoint_array_(block->data.seek_table.points, array, num_points))
return false;
}
printf("OK\n");
return true;
}
static void entry_new_(FLAC__StreamMetadata_VorbisComment_Entry *entry, const char *field)
{
entry->length = strlen(field);
entry->entry = (FLAC__byte*)malloc(entry->length+1);
FLAC__ASSERT(0 != entry->entry);
memcpy(entry->entry, field, entry->length);
entry->entry[entry->length] = '\0';
}
static void entry_clone_(FLAC__StreamMetadata_VorbisComment_Entry *entry)
{
FLAC__byte *x = (FLAC__byte*)malloc(entry->length+1);
FLAC__ASSERT(0 != x);
memcpy(x, entry->entry, entry->length);
x[entry->length] = '\0';
entry->entry = x;
}
static void vc_calc_len_(FLAC__StreamMetadata *block)
{
const FLAC__StreamMetadata_VorbisComment *vc = &block->data.vorbis_comment;
unsigned i;
block->length = FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN / 8;
block->length += vc->vendor_string.length;
block->length += FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN / 8;
for(i = 0; i < vc->num_comments; i++) {
block->length += FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN / 8;
block->length += vc->comments[i].length;
}
}
static void vc_resize_(FLAC__StreamMetadata *block, unsigned num)
{
FLAC__StreamMetadata_VorbisComment *vc = &block->data.vorbis_comment;
if(vc->num_comments != 0) {
FLAC__ASSERT(0 != vc->comments);
if(num < vc->num_comments) {
unsigned i;
for(i = num; i < vc->num_comments; i++) {
if(0 != vc->comments[i].entry)
free(vc->comments[i].entry);
}
}
}
if(num == 0) {
if(0 != vc->comments) {
free(vc->comments);
vc->comments = 0;
}
}
else {
vc->comments = (FLAC__StreamMetadata_VorbisComment_Entry*)realloc(vc->comments, sizeof(FLAC__StreamMetadata_VorbisComment_Entry)*num);
FLAC__ASSERT(0 != vc->comments);
if(num > vc->num_comments)
memset(vc->comments+vc->num_comments, 0, sizeof(FLAC__StreamMetadata_VorbisComment_Entry)*(num-vc->num_comments));
}
vc->num_comments = num;
vc_calc_len_(block);
}
static int vc_find_from_(FLAC__StreamMetadata *block, const char *name, unsigned start)
{
const unsigned n = strlen(name);
unsigned i;
for(i = start; i < block->data.vorbis_comment.num_comments; i++) {
const FLAC__StreamMetadata_VorbisComment_Entry *entry = &block->data.vorbis_comment.comments[i];
if(entry->length > n && 0 == strncmp(entry->entry, name, n) && entry->entry[n] == '=')
return (int)i;
}
return -1;
}
static void vc_set_vs_new_(FLAC__StreamMetadata_VorbisComment_Entry *entry, FLAC__StreamMetadata *block, const char *field)
{
if(0 != block->data.vorbis_comment.vendor_string.entry)
free(block->data.vorbis_comment.vendor_string.entry);
entry_new_(entry, field);
block->data.vorbis_comment.vendor_string = *entry;
vc_calc_len_(block);
}
static void vc_set_new_(FLAC__StreamMetadata_VorbisComment_Entry *entry, FLAC__StreamMetadata *block, unsigned pos, const char *field)
{
if(0 != block->data.vorbis_comment.comments[pos].entry)
free(block->data.vorbis_comment.comments[pos].entry);
entry_new_(entry, field);
block->data.vorbis_comment.comments[pos] = *entry;
vc_calc_len_(block);
}
static void vc_insert_new_(FLAC__StreamMetadata_VorbisComment_Entry *entry, FLAC__StreamMetadata *block, unsigned pos, const char *field)
{
vc_resize_(block, block->data.vorbis_comment.num_comments+1);
memmove(&block->data.vorbis_comment.comments[pos+1], &block->data.vorbis_comment.comments[pos], sizeof(FLAC__StreamMetadata_VorbisComment_Entry)*(block->data.vorbis_comment.num_comments-1-pos));
memset(&block->data.vorbis_comment.comments[pos], 0, sizeof(FLAC__StreamMetadata_VorbisComment_Entry));
vc_set_new_(entry, block, pos, field);
vc_calc_len_(block);
}
static void vc_delete_(FLAC__StreamMetadata *block, unsigned pos)
{
if(0 != block->data.vorbis_comment.comments[pos].entry)
free(block->data.vorbis_comment.comments[pos].entry);
memmove(&block->data.vorbis_comment.comments[pos], &block->data.vorbis_comment.comments[pos+1], sizeof(FLAC__StreamMetadata_VorbisComment_Entry)*(block->data.vorbis_comment.num_comments-pos-1));
block->data.vorbis_comment.comments[block->data.vorbis_comment.num_comments-1].entry = 0;
block->data.vorbis_comment.comments[block->data.vorbis_comment.num_comments-1].length = 0;
vc_resize_(block, block->data.vorbis_comment.num_comments-1);
vc_calc_len_(block);
}
static void vc_replace_new_(FLAC__StreamMetadata_VorbisComment_Entry *entry, FLAC__StreamMetadata *block, const char *field, FLAC__bool all)
{
int index;
char field_name[256];
const char *eq = strchr(field, '=');
FLAC__ASSERT(eq>field && (unsigned)(eq-field) < sizeof(field_name));
memcpy(field_name, field, eq-field);
field_name[eq-field]='\0';
index = vc_find_from_(block, field_name, 0);
if(index < 0)
vc_insert_new_(entry, block, block->data.vorbis_comment.num_comments, field);
else {
vc_set_new_(entry, block, (unsigned)index, field);
if(all) {
for(index = index+1; index >= 0 && (unsigned)index < block->data.vorbis_comment.num_comments; )
if((index = vc_find_from_(block, field_name, (unsigned)index)) >= 0)
vc_delete_(block, (unsigned)index);
}
}
vc_calc_len_(block);
}
static void track_new_(FLAC__StreamMetadata_CueSheet_Track *track, FLAC__uint64 offset, FLAC__byte number, const char *isrc, FLAC__bool data, FLAC__bool pre_em)
{
track->offset = offset;
track->number = number;
memcpy(track->isrc, isrc, sizeof(track->isrc));
track->type = data;
track->pre_emphasis = pre_em;
track->num_indices = 0;
track->indices = 0;
}
static void track_clone_(FLAC__StreamMetadata_CueSheet_Track *track)
{
if(track->num_indices > 0) {
size_t bytes = sizeof(FLAC__StreamMetadata_CueSheet_Index) * track->num_indices;
FLAC__StreamMetadata_CueSheet_Index *x = (FLAC__StreamMetadata_CueSheet_Index*)malloc(bytes);
FLAC__ASSERT(0 != x);
memcpy(x, track->indices, bytes);
track->indices = x;
}
}
static void cs_calc_len_(FLAC__StreamMetadata *block)
{
const FLAC__StreamMetadata_CueSheet *cs = &block->data.cue_sheet;
unsigned i;
block->length = (
FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN +
FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN +
FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN +
FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN +
FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN
) / 8;
block->length += cs->num_tracks * (
FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN +
FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN +
FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN +
FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN +
FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN +
FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN +
FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN
) / 8;
for(i = 0; i < cs->num_tracks; i++) {
block->length += cs->tracks[i].num_indices * (
FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN +
FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN +
FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN
) / 8;
}
}
static void tr_resize_(FLAC__StreamMetadata *block, unsigned track_num, unsigned num)
{
FLAC__StreamMetadata_CueSheet_Track *tr;
FLAC__ASSERT(track_num < block->data.cue_sheet.num_tracks);
tr = &block->data.cue_sheet.tracks[track_num];
if(tr->num_indices != 0) {
FLAC__ASSERT(0 != tr->indices);
}
if(num == 0) {
if(0 != tr->indices) {
free(tr->indices);
tr->indices = 0;
}
}
else {
tr->indices = (FLAC__StreamMetadata_CueSheet_Index*)realloc(tr->indices, sizeof(FLAC__StreamMetadata_CueSheet_Index)*num);
FLAC__ASSERT(0 != tr->indices);
if(num > tr->num_indices)
memset(tr->indices+tr->num_indices, 0, sizeof(FLAC__StreamMetadata_CueSheet_Index)*(num-tr->num_indices));
}
tr->num_indices = num;
cs_calc_len_(block);
}
static void tr_set_new_(FLAC__StreamMetadata *block, unsigned track_num, unsigned pos, FLAC__StreamMetadata_CueSheet_Index index)
{
FLAC__StreamMetadata_CueSheet_Track *tr;
FLAC__ASSERT(track_num < block->data.cue_sheet.num_tracks);
tr = &block->data.cue_sheet.tracks[track_num];
FLAC__ASSERT(pos < tr->num_indices);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -