add vorbiscomment validation functions to format.c and test code; validate name part of vorbiscomment in metadata and metadata++ interfaces

This commit is contained in:
Josh Coalson
2004-12-30 03:41:19 +00:00
parent e40480d300
commit 2de1124e36
14 changed files with 466 additions and 42 deletions

View File

@@ -550,6 +550,9 @@ namespace FLAC {
FLAC__ASSERT(is_valid());
FLAC__ASSERT(0 != field);
if(!::FLAC__format_vorbiscomment_entry_is_legal((const ::FLAC__byte*)field, field_length))
return is_valid_ = false;
clear_entry();
if(0 == (entry_.entry = (FLAC__byte*)malloc(field_length+1))) {
@@ -575,6 +578,9 @@ namespace FLAC {
FLAC__ASSERT(is_valid());
FLAC__ASSERT(0 != field_name);
if(!::FLAC__format_vorbiscomment_entry_name_is_legal(field_name))
return is_valid_ = false;
clear_field_name();
if(0 == (field_name_ = strdup(field_name))) {
@@ -593,6 +599,9 @@ namespace FLAC {
FLAC__ASSERT(is_valid());
FLAC__ASSERT(0 != field_value);
if(!::FLAC__format_vorbiscomment_entry_value_is_legal((const FLAC__byte*)field_value, field_value_length))
return is_valid_ = false;
clear_field_value();
if(0 == (field_value_ = (char *)malloc(field_value_length+1))) {
@@ -756,10 +765,10 @@ namespace FLAC {
return object_->data.vorbis_comment.num_comments;
}
VorbisComment::Entry VorbisComment::get_vendor_string() const
const FLAC__byte *VorbisComment::get_vendor_string() const
{
FLAC__ASSERT(is_valid());
return Entry((const char *)object_->data.vorbis_comment.vendor_string.entry, object_->data.vorbis_comment.vendor_string.length);
return object_->data.vorbis_comment.vendor_string.entry;
}
VorbisComment::Entry VorbisComment::get_comment(unsigned index) const
@@ -769,13 +778,11 @@ namespace FLAC {
return Entry((const char *)object_->data.vorbis_comment.comments[index].entry, object_->data.vorbis_comment.comments[index].length);
}
bool VorbisComment::set_vendor_string(const VorbisComment::Entry &entry)
bool VorbisComment::set_vendor_string(const FLAC__byte *string)
{
FLAC__ASSERT(is_valid());
// vendor_string is a special kind of entry
::FLAC__StreamMetadata_VorbisComment_Entry vendor_string;
vendor_string.length = entry.get_field_name_length();
vendor_string.entry = (FLAC__byte*)entry.get_field_name(); // we can cheat on const-ness because we make a copy below:
const ::FLAC__StreamMetadata_VorbisComment_Entry vendor_string = { strlen((const char *)string), (FLAC__byte*)string }; // we can cheat on const-ness because we make a copy below:
return (bool)::FLAC__metadata_object_vorbiscomment_set_vendor_string(object_, vendor_string, /*copy=*/true);
}

View File

@@ -254,6 +254,77 @@ FLAC_API unsigned FLAC__format_seektable_sort(FLAC__StreamMetadata_SeekTable *se
return j;
}
static __inline unsigned utf8len_(const FLAC__byte *utf8)
{
FLAC__ASSERT(0 != utf8);
if ((utf8[0] & 0x80) == 0)
return 1;
else if ((utf8[0] & 0xE0) == 0xC0 && (utf8[1] & 0xC0) == 0x80)
return 2;
else if ((utf8[0] & 0xF0) == 0xE0 && (utf8[1] & 0xC0) == 0x80 && (utf8[2] & 0xC0) == 0x80)
return 3;
else
return 0;
}
FLAC_API FLAC__bool FLAC__format_vorbiscomment_entry_name_is_legal(const char *name)
{
char c;
for(c = *name; c; c = *(++name))
if(c < 0x20 || c == 0x3d || c > 0x7d)
return false;
return true;
}
FLAC_API FLAC__bool FLAC__format_vorbiscomment_entry_value_is_legal(const FLAC__byte *value, unsigned length)
{
if(length == (unsigned)(-1)) {
while(*value) {
unsigned n = utf8len_(value);
if(n == 0)
return false;
value += n;
}
}
else {
const FLAC__byte *end = value + length;
while(value < end) {
unsigned n = utf8len_(value);
if(n == 0)
return false;
value += n;
}
if(value != end)
return false;
}
return true;
}
FLAC_API FLAC__bool FLAC__format_vorbiscomment_entry_is_legal(const FLAC__byte *entry, unsigned length)
{
const FLAC__byte *s, *end;
for(s = entry, end = s + length; s < end && *s != '='; s++) {
if(*s < 0x20 || *s > 0x7D)
return false;
}
if(s == end)
return false;
s++; /* skip '=' */
while(s < end) {
unsigned n = utf8len_(s);
if(n == 0)
return false;
s += n;
}
if(s != end)
return false;
return true;
}
FLAC_API FLAC__bool FLAC__format_cuesheet_is_legal(const FLAC__StreamMetadata_CueSheet *cue_sheet, FLAC__bool check_cd_da_subset, const char **violation)
{
unsigned i, j;

View File

@@ -964,6 +964,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);
}
@@ -1016,6 +1018,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);
}
@@ -1027,6 +1031,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))
@@ -1050,6 +1057,10 @@ FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_append_comment(FLAC__Str
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;
unsigned field_name_length;
@@ -1111,6 +1122,11 @@ FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_entry_from_name_value_pa
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(field_value, (unsigned)(-1)))
return false;
{
const size_t nn = strlen(field_name);
const size_t nv = strlen(field_value);
@@ -1131,6 +1147,10 @@ FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_entry_to_name_value_pair
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;

View File

@@ -1018,14 +1018,15 @@ bool test_metadata_object_vorbiscomment()
printf("OK\n");
printf("testing VorbisComment::set_vendor_string()... ");
if(!block.set_vendor_string(entry1))
if(!block.set_vendor_string((const FLAC__byte *)"mame0"))
return die_("returned false");
printf("OK\n");
vorbiscomment_.data.vorbis_comment.vendor_string.entry[0] = 'm';
printf("testing VorbisComment::get_vendor_string()... ");
if(block.get_vendor_string().get_field_name_length() != vorbiscomment_.data.vorbis_comment.vendor_string.length)
if(strlen((const char *)block.get_vendor_string()) != vorbiscomment_.data.vorbis_comment.vendor_string.length)
return die_("length mismatch");
if(0 != memcmp(block.get_vendor_string().get_field_name(), vorbiscomment_.data.vorbis_comment.vendor_string.entry, vorbiscomment_.data.vorbis_comment.vendor_string.length))
if(0 != memcmp(block.get_vendor_string(), vorbiscomment_.data.vorbis_comment.vendor_string.entry, vorbiscomment_.data.vorbis_comment.vendor_string.length))
return die_("value mismatch");
printf("OK\n");

View File

@@ -33,6 +33,7 @@ test_libFLAC_SOURCES = \
decoders.c \
encoders.c \
file_utils.c \
format.c \
main.c \
metadata.c \
metadata_manip.c \
@@ -42,5 +43,6 @@ test_libFLAC_SOURCES = \
decoders.h \
encoders.h \
file_utils.h \
format.h \
metadata.h \
metadata_utils.h

View File

@@ -37,6 +37,7 @@ SRCS_C = \
decoders.c \
encoders.c \
file_utils.c \
format.c \
main.c \
metadata.c \
metadata_manip.c \

222
src/test_libFLAC/format.c Normal file
View File

@@ -0,0 +1,222 @@
/* test_libFLAC - Unit tester for libFLAC
* Copyright (C) 2004 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/format.h"
#include <stdio.h>
static const char *true_false_string_[2] = { "false", "true" };
static struct {
unsigned rate;
FLAC__bool valid;
} SAMPLE_RATES[] = {
{ 0, false },
{ 1, true },
{ 9, true },
{ 10, true },
{ 4000, true },
{ 8000, true },
{ 11025, true },
{ 12000, true },
{ 16000, true },
{ 22050, true },
{ 24000, true },
{ 32000, true },
{ 32768, true },
{ 44100, true },
{ 48000, true },
{ 65000, true },
{ 65535, true },
{ 65536, false },
{ 65540, true },
{ 65550, true },
{ 65555, false },
{ 66000, true },
{ 66001, false },
{ 96000, true },
{ 100000, true },
{ 100001, false },
{ 192000, true },
{ 500000, true },
{ 500001, false },
{ 500010, true },
{ 700000, false },
{ 700010, false },
{ 1000000, false },
{ 1100000, false }
};
static struct {
const char *string;
FLAC__bool valid;
} VCENTRY_NAMES[] = {
{ "", true },
{ "a", true },
{ "=", false },
{ "a=", false },
{ "\x01", false },
{ "\x1f", false },
{ "\x7d", true },
{ "\x7e", false },
{ "\xff", false }
};
static struct {
unsigned length;
const FLAC__byte *string;
FLAC__bool valid;
} VCENTRY_VALUES[] = {
{ 0, (const FLAC__byte*)"", true },
{ 1, (const FLAC__byte*)"", true },
{ 1, (const FLAC__byte*)"\x01", true },
{ 1, (const FLAC__byte*)"\x7f", true },
{ 1, (const FLAC__byte*)"\x80", false },
{ 1, (const FLAC__byte*)"\x81", false },
{ 1, (const FLAC__byte*)"\xc0", false },
{ 1, (const FLAC__byte*)"\xe0", false },
{ 1, (const FLAC__byte*)"\xf0", false },
{ 2, (const FLAC__byte*)"\xc0\x41", false },
{ 2, (const FLAC__byte*)"\xc1\x41", false },
{ 2, (const FLAC__byte*)"\xc0\x85", true },
{ 2, (const FLAC__byte*)"\xc1\x85", true },
{ 2, (const FLAC__byte*)"\xe0\x41", false },
{ 2, (const FLAC__byte*)"\xe1\x41", false },
{ 2, (const FLAC__byte*)"\xe0\x85", false },
{ 2, (const FLAC__byte*)"\xe1\x85", false },
{ 3, (const FLAC__byte*)"\xe0\x85\x41", false },
{ 3, (const FLAC__byte*)"\xe1\x85\x41", false },
{ 3, (const FLAC__byte*)"\xe0\x85\x80", true },
{ 3, (const FLAC__byte*)"\xe1\x85\x80", true }
};
static struct {
const FLAC__byte *string;
FLAC__bool valid;
} VCENTRY_VALUES_NT[] = {
{ (FLAC__byte*)"", true },
{ (FLAC__byte*)"\x01", true },
{ (FLAC__byte*)"\x7f", true },
{ (FLAC__byte*)"\x80", false },
{ (FLAC__byte*)"\x81", false },
{ (FLAC__byte*)"\xc0", false },
{ (FLAC__byte*)"\xe0", false },
{ (FLAC__byte*)"\xf0", false },
{ (FLAC__byte*)"\xc0\x41", false },
{ (FLAC__byte*)"\xc1\x41", false },
{ (FLAC__byte*)"\xc0\x85", true },
{ (FLAC__byte*)"\xc1\x85", true },
{ (FLAC__byte*)"\xe0\x41", false },
{ (FLAC__byte*)"\xe1\x41", false },
{ (FLAC__byte*)"\xe0\x85", false },
{ (FLAC__byte*)"\xe1\x85", false },
{ (FLAC__byte*)"\xe0\x85\x41", false },
{ (FLAC__byte*)"\xe1\x85\x41", false },
{ (FLAC__byte*)"\xe0\x85\x80", true },
{ (FLAC__byte*)"\xe1\x85\x80", true }
};
static struct {
unsigned length;
const FLAC__byte *string;
FLAC__bool valid;
} VCENTRIES[] = {
{ 0, (const FLAC__byte*)"", false },
{ 1, (const FLAC__byte*)"a", false },
{ 1, (const FLAC__byte*)"=", true },
{ 2, (const FLAC__byte*)"a=", true },
{ 2, (const FLAC__byte*)"\x01=", false },
{ 2, (const FLAC__byte*)"\x1f=", false },
{ 2, (const FLAC__byte*)"\x7d=", true },
{ 2, (const FLAC__byte*)"\x7e=", false },
{ 2, (const FLAC__byte *)"\xff=", false },
{ 3, (const FLAC__byte*)"a=\x01", true },
{ 3, (const FLAC__byte*)"a=\x7f", true },
{ 3, (const FLAC__byte*)"a=\x80", false },
{ 3, (const FLAC__byte*)"a=\x81", false },
{ 3, (const FLAC__byte*)"a=\xc0", false },
{ 3, (const FLAC__byte*)"a=\xe0", false },
{ 3, (const FLAC__byte*)"a=\xf0", false },
{ 4, (const FLAC__byte*)"a=\xc0\x41", false },
{ 4, (const FLAC__byte*)"a=\xc1\x41", false },
{ 4, (const FLAC__byte*)"a=\xc0\x85", true },
{ 4, (const FLAC__byte*)"a=\xc1\x85", true },
{ 4, (const FLAC__byte*)"a=\xe0\x41", false },
{ 4, (const FLAC__byte*)"a=\xe1\x41", false },
{ 4, (const FLAC__byte*)"a=\xe0\x85", false },
{ 4, (const FLAC__byte*)"a=\xe1\x85", false },
{ 5, (const FLAC__byte*)"a=\xe0\x85\x41", false },
{ 5, (const FLAC__byte*)"a=\xe1\x85\x41", false },
{ 5, (const FLAC__byte*)"a=\xe0\x85\x80", true },
{ 5, (const FLAC__byte*)"a=\xe1\x85\x80", true }
};
FLAC__bool test_format()
{
unsigned i;
printf("\n+++ libFLAC unit test: format\n\n");
for(i = 0; i < sizeof(SAMPLE_RATES)/sizeof(SAMPLE_RATES[0]); i++) {
printf("testing FLAC__format_sample_rate_is_valid(%u)... ", SAMPLE_RATES[i].rate);
if(FLAC__format_sample_rate_is_valid(SAMPLE_RATES[i].rate) != SAMPLE_RATES[i].valid) {
printf("FAILED, expected %s, got %s\n", true_false_string_[SAMPLE_RATES[i].valid], true_false_string_[!SAMPLE_RATES[i].valid]);
return false;
}
printf("OK\n");
}
for(i = 0; i < sizeof(VCENTRY_NAMES)/sizeof(VCENTRY_NAMES[0]); i++) {
printf("testing FLAC__format_vorbiscomment_entry_name_is_legal(\"%s\")... ", VCENTRY_NAMES[i].string);
if(FLAC__format_vorbiscomment_entry_name_is_legal(VCENTRY_NAMES[i].string) != VCENTRY_NAMES[i].valid) {
printf("FAILED, expected %s, got %s\n", true_false_string_[VCENTRY_NAMES[i].valid], true_false_string_[!VCENTRY_NAMES[i].valid]);
return false;
}
printf("OK\n");
}
for(i = 0; i < sizeof(VCENTRY_VALUES)/sizeof(VCENTRY_VALUES[0]); i++) {
printf("testing FLAC__format_vorbiscomment_entry_value_is_legal(\"%s\", %u)... ", VCENTRY_VALUES[i].string, VCENTRY_VALUES[i].length);
if(FLAC__format_vorbiscomment_entry_value_is_legal(VCENTRY_VALUES[i].string, VCENTRY_VALUES[i].length) != VCENTRY_VALUES[i].valid) {
printf("FAILED, expected %s, got %s\n", true_false_string_[VCENTRY_VALUES[i].valid], true_false_string_[!VCENTRY_VALUES[i].valid]);
return false;
}
printf("OK\n");
}
for(i = 0; i < sizeof(VCENTRY_VALUES_NT)/sizeof(VCENTRY_VALUES_NT[0]); i++) {
printf("testing FLAC__format_vorbiscomment_entry_value_is_legal(\"%s\", -1)... ", VCENTRY_VALUES_NT[i].string);
if(FLAC__format_vorbiscomment_entry_value_is_legal(VCENTRY_VALUES_NT[i].string, (unsigned)(-1)) != VCENTRY_VALUES_NT[i].valid) {
printf("FAILED, expected %s, got %s\n", true_false_string_[VCENTRY_VALUES_NT[i].valid], true_false_string_[!VCENTRY_VALUES_NT[i].valid]);
return false;
}
printf("OK\n");
}
for(i = 0; i < sizeof(VCENTRIES)/sizeof(VCENTRIES[0]); i++) {
printf("testing FLAC__format_vorbiscomment_entry_is_legal(\"%s\", %u)... ", VCENTRIES[i].string, VCENTRIES[i].length);
if(FLAC__format_vorbiscomment_entry_is_legal(VCENTRIES[i].string, VCENTRIES[i].length) != VCENTRIES[i].valid) {
printf("FAILED, expected %s, got %s\n", true_false_string_[VCENTRIES[i].valid], true_false_string_[!VCENTRIES[i].valid]);
return false;
}
printf("OK\n");
}
printf("\nPASSED!\n");
return true;
}

26
src/test_libFLAC/format.h Normal file
View File

@@ -0,0 +1,26 @@
/* test_libFLAC - Unit tester for libFLAC
* Copyright (C) 2004 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.
*/
#ifndef FLAC__TEST_LIBFLAC_FORMAT_H
#define FLAC__TEST_LIBFLAC_FORMAT_H
#include "FLAC/ordinals.h"
FLAC__bool test_format();
#endif

View File

@@ -19,6 +19,7 @@
#include "bitbuffer.h"
#include "decoders.h"
#include "encoders.h"
#include "format.h"
#include "metadata.h"
int main(int argc, char *argv[])
@@ -28,6 +29,9 @@ int main(int argc, char *argv[])
if(!test_bitbuffer())
return 1;
if(!test_format())
return 1;
if(!test_encoders())
return 1;

View File

@@ -105,6 +105,10 @@ SOURCE=.\file_utils.c
# End Source File
# Begin Source File
SOURCE=.\format.c
# End Source File
# Begin Source File
SOURCE=.\main.c
# End Source File
# Begin Source File
@@ -145,6 +149,10 @@ SOURCE=.\file_utils.h
# End Source File
# Begin Source File
SOURCE=.\format.h
# End Source File
# Begin Source File
SOURCE=.\metadata.h
# End Source File
# Begin Source File