diff --git a/src/opus.cc b/src/opus.cc index 1655e18..cb905fb 100644 --- a/src/opus.cc +++ b/src/opus.cc @@ -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; } diff --git a/src/opustags.cc b/src/opustags.cc index 4a3f90f..9bb1156 100644 --- a/src/opustags.cc +++ b/src/opustags.cc @@ -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; diff --git a/src/opustags.h b/src/opustags.h index 622aca4..4cedcde 100644 --- a/src/opustags.h +++ b/src/opustags.h @@ -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); diff --git a/t/opus.cc b/t/opus.cc index 46d82f4..8fb39d4 100644 --- a/t/opus.cc +++ b/t/opus.cc @@ -15,17 +15,19 @@ using namespace std::literals::string_literals; static void check_identification() { - ot::status rc; - rc = ot::validate_identification_header(reinterpret_cast("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("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("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"); }