mirror of
https://github.com/claunia/flac.git
synced 2025-12-16 18:54:26 +00:00
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:
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user