validate_identification_header: take the ogg_packet

This commit is contained in:
Frédéric Mangano-Tarumi 2018-11-13 18:51:28 -05:00
parent 351d6149c9
commit 82ff7f7751
4 changed files with 18 additions and 12 deletions

View File

@ -34,11 +34,14 @@
#define le32toh(x) OSSwapLittleToHostInt32(x)
#endif
ot::status ot::validate_identification_header(const unsigned char* data, size_t size)
/**
* \todo Validate more properties of the packet, like the sequence number.
*/
ot::status ot::validate_identification_header(const ogg_packet& packet)
{
if (size < 8)
if (packet.bytes < 8)
return ot::status::bad_identification_header;
if (memcmp(data, "OpusHead", 8) != 0)
if (memcmp(packet.packet, "OpusHead", 8) != 0)
return ot::status::bad_identification_header;
return ot::status::ok;
}

View File

@ -109,7 +109,7 @@ static int run(ot::options& opt)
while(ogg_stream_packetout(&reader.stream, &reader.packet) == 1){
packet_count++;
if (packet_count == 1) { // Identification header
rc = ot::validate_identification_header(reader.packet.packet, reader.packet.bytes);
rc = ot::validate_identification_header(reader.packet);
if (rc != ot::status::ok) {
error = ot::error_message(rc);
break;

View File

@ -200,9 +200,10 @@ struct opus_tags {
/**
* Validate the content of the first packet of an Ogg stream to ensure it's a valid OpusHead.
*
* Returns #ot::status::ok on success, #ot::status::bad_identification_header on error.
*/
status validate_identification_header(const unsigned char* data, size_t size);
status validate_identification_header(const ogg_packet& packet);
status parse_tags(const char *data, long len, opus_tags *tags);
int render_tags(opus_tags *tags, ogg_packet *op);

View File

@ -15,17 +15,19 @@ using namespace std::literals::string_literals;
static void check_identification()
{
ot::status rc;
rc = ot::validate_identification_header(reinterpret_cast<const unsigned char*>("OpusHead.."), 10);
if (rc != ot::status::ok)
ogg_packet packet {};
packet.packet = (unsigned char*) "OpusHead..";
packet.bytes = 10;
if (ot::validate_identification_header(packet) != ot::status::ok)
throw failure("did not accept a good OpusHead");
rc = ot::validate_identification_header(reinterpret_cast<const unsigned char*>("OpusHead"), 7);
if (rc != ot::status::bad_identification_header)
packet.bytes = 7;
if (ot::validate_identification_header(packet) != ot::status::bad_identification_header)
throw failure("accepted an OpusHead that is too short");
rc = ot::validate_identification_header(reinterpret_cast<const unsigned char*>("NotOpusHead"), 11);
if (rc != ot::status::bad_identification_header)
packet.packet = (unsigned char*) "NotOpusHead";
packet.bytes = 11;
if (ot::validate_identification_header(packet) != ot::status::bad_identification_header)
throw failure("did not report the right status for a bad OpusHead");
}