libavformat/apetag.c
Go to the documentation of this file.
00001 /*
00002  * APE tag handling
00003  * Copyright (c) 2007 Benjamin Zores <ben@geexbox.org>
00004  *  based upon libdemac from Dave Chapman.
00005  *
00006  * This file is part of Libav.
00007  *
00008  * Libav is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * Libav is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with Libav; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00021  */
00022 
00023 #include "libavutil/intreadwrite.h"
00024 #include "libavutil/dict.h"
00025 #include "avformat.h"
00026 #include "apetag.h"
00027 
00028 #define APE_TAG_VERSION               2000
00029 #define APE_TAG_FOOTER_BYTES          32
00030 #define APE_TAG_FLAG_CONTAINS_HEADER  (1 << 31)
00031 #define APE_TAG_FLAG_IS_HEADER        (1 << 29)
00032 
00033 static int ape_tag_read_field(AVFormatContext *s)
00034 {
00035     AVIOContext *pb = s->pb;
00036     uint8_t key[1024], *value;
00037     uint32_t size;
00038     int i, c;
00039 
00040     size = avio_rl32(pb);  /* field size */
00041     avio_skip(pb, 4);      /* field flags */
00042     for (i = 0; i < sizeof(key) - 1; i++) {
00043         c = avio_r8(pb);
00044         if (c < 0x20 || c > 0x7E)
00045             break;
00046         else
00047             key[i] = c;
00048     }
00049     key[i] = 0;
00050     if (c != 0) {
00051         av_log(s, AV_LOG_WARNING, "Invalid APE tag key '%s'.\n", key);
00052         return -1;
00053     }
00054     if (size > INT32_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
00055         av_log(s, AV_LOG_ERROR, "APE tag size too large.\n");
00056         return AVERROR_INVALIDDATA;
00057     }
00058     value = av_malloc(size+1);
00059     if (!value)
00060         return AVERROR(ENOMEM);
00061     avio_read(pb, value, size);
00062     value[size] = 0;
00063     av_dict_set(&s->metadata, key, value, AV_DICT_DONT_STRDUP_VAL);
00064     return 0;
00065 }
00066 
00067 void ff_ape_parse_tag(AVFormatContext *s)
00068 {
00069     AVIOContext *pb = s->pb;
00070     int64_t file_size = avio_size(pb);
00071     uint32_t val, fields, tag_bytes;
00072     uint8_t buf[8];
00073     int i;
00074 
00075     if (file_size < APE_TAG_FOOTER_BYTES)
00076         return;
00077 
00078     avio_seek(pb, file_size - APE_TAG_FOOTER_BYTES, SEEK_SET);
00079 
00080     avio_read(pb, buf, 8);     /* APETAGEX */
00081     if (strncmp(buf, "APETAGEX", 8)) {
00082         return;
00083     }
00084 
00085     val = avio_rl32(pb);       /* APE tag version */
00086     if (val > APE_TAG_VERSION) {
00087         av_log(s, AV_LOG_ERROR, "Unsupported tag version. (>=%d)\n", APE_TAG_VERSION);
00088         return;
00089     }
00090 
00091     tag_bytes = avio_rl32(pb); /* tag size */
00092     if (tag_bytes - APE_TAG_FOOTER_BYTES > (1024 * 1024 * 16)) {
00093         av_log(s, AV_LOG_ERROR, "Tag size is way too big\n");
00094         return;
00095     }
00096 
00097     fields = avio_rl32(pb);    /* number of fields */
00098     if (fields > 65536) {
00099         av_log(s, AV_LOG_ERROR, "Too many tag fields (%d)\n", fields);
00100         return;
00101     }
00102 
00103     val = avio_rl32(pb);       /* flags */
00104     if (val & APE_TAG_FLAG_IS_HEADER) {
00105         av_log(s, AV_LOG_ERROR, "APE Tag is a header\n");
00106         return;
00107     }
00108 
00109     avio_seek(pb, file_size - tag_bytes, SEEK_SET);
00110 
00111     for (i=0; i<fields; i++)
00112         if (ape_tag_read_field(s) < 0) break;
00113 }