From ee702d71e2635bc79fbcc8cd00cd74265cb5aed9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Mangano?= Date: Tue, 1 Jan 2013 14:25:20 +0100 Subject: [PATCH] parse tags --- opustags.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) diff --git a/opustags.c b/opustags.c index c66c0a7..5116820 100644 --- a/opustags.c +++ b/opustags.c @@ -4,6 +4,61 @@ #include #include +typedef struct { + uint32_t vendor_length; + const char *vendor_string; + uint32_t count; + uint32_t *lengths; + const char **comment; +} opus_tags; + +int parse_tags(char *data, long len, opus_tags *tags){ + long pos; + if(len < 8+4+4) + return -1; + if(strncmp(data, "OpusTags", 8) != 0) + return -1; + // Vendor + pos = 8; + tags->vendor_length = le32toh(*((uint32_t*) (data + pos))); + tags->vendor_string = data + pos + 4; + pos += 4 + tags->vendor_length; + if(pos + 4 > len) + return -1; + // Count + tags->count = le32toh(*((uint32_t*) (data + pos))); + if(tags->count == 0) + return 0; + tags->lengths = calloc(tags->count, sizeof(uint32_t)); + if(tags->lengths == NULL) + return -1; + tags->comment = calloc(tags->count, sizeof(const char*)); + if(tags->comment == NULL){ + free(tags->lengths); + return -1; + } + pos += 4; + // Comment + uint32_t i; + for(i=0; icount; i++){ + tags->lengths[i] = le32toh(*((uint32_t*) (data + pos))); + tags->comment[i] = data + pos + 4; + pos += 4 + tags->lengths[i]; + if(pos > len) + return -1; + } + if(pos != len) + return -1; + return 0; +} + +void free_tags(opus_tags *tags){ + if(tags->count > 0){ + free(tags->lengths); + free(tags->comment); + } +} + int main(int argc, char **argv){ FILE *in = fopen(argv[1], "r"); if(!in){ @@ -14,6 +69,7 @@ int main(int argc, char **argv){ ogg_stream_state os; ogg_page og; ogg_packet op; + opus_tags tags; ogg_sync_init(&oy); char *buf; size_t len; @@ -54,8 +110,16 @@ int main(int argc, char **argv){ error = "opustags: invalid identification header"; } if(packet_count == 2){ // Comment header - if(strncmp((char*) op.packet, "OpusTags", 8) != 0) + if(parse_tags((char*) op.packet, op.bytes, &tags) == -1) error = "opustags: invalid comment header"; + fwrite(tags.vendor_string, 1, tags.vendor_length, stdout); + puts(""); + int i; + for(i=0; i