Libav
apetag.c
Go to the documentation of this file.
1 /*
2  * APE tag handling
3  * Copyright (c) 2007 Benjamin Zores <ben@geexbox.org>
4  * based upon libdemac from Dave Chapman.
5  *
6  * This file is part of Libav.
7  *
8  * Libav is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * Libav is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/dict.h"
25 #include "avformat.h"
26 #include "avio_internal.h"
27 #include "apetag.h"
28 #include "internal.h"
29 
30 #define APE_TAG_VERSION 2000
31 #define APE_TAG_FOOTER_BYTES 32
32 #define APE_TAG_FLAG_CONTAINS_HEADER (1 << 31)
33 #define APE_TAG_FLAG_CONTAINS_FOOTER (1 << 30)
34 #define APE_TAG_FLAG_IS_HEADER (1 << 29)
35 #define APE_TAG_FLAG_IS_BINARY (1 << 1)
36 
38 {
39  AVIOContext *pb = s->pb;
40  uint8_t key[1024], *value;
41  uint32_t size, flags;
42  int i, c;
43 
44  size = avio_rl32(pb); /* field size */
45  flags = avio_rl32(pb); /* field flags */
46  for (i = 0; i < sizeof(key) - 1; i++) {
47  c = avio_r8(pb);
48  if (c < 0x20 || c > 0x7E)
49  break;
50  else
51  key[i] = c;
52  }
53  key[i] = 0;
54  if (c != 0) {
55  av_log(s, AV_LOG_WARNING, "Invalid APE tag key '%s'.\n", key);
56  return -1;
57  }
58  if (size > INT32_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
59  av_log(s, AV_LOG_ERROR, "APE tag size too large.\n");
60  return AVERROR_INVALIDDATA;
61  }
62  if (flags & APE_TAG_FLAG_IS_BINARY) {
63  uint8_t filename[1024];
64  enum AVCodecID id;
66  if (!st)
67  return AVERROR(ENOMEM);
68 
69  size -= avio_get_str(pb, size, filename, sizeof(filename));
70  if (size <= 0) {
71  av_log(s, AV_LOG_WARNING, "Skipping binary tag '%s'.\n", key);
72  return 0;
73  }
74 
75  av_dict_set(&st->metadata, key, filename, 0);
76 
77  if ((id = ff_guess_image2_codec(filename)) != AV_CODEC_ID_NONE) {
78  AVPacket pkt;
79  int ret;
80 
81  ret = av_get_packet(s->pb, &pkt, size);
82  if (ret < 0) {
83  av_log(s, AV_LOG_ERROR, "Error reading cover art.\n");
84  return ret;
85  }
86 
89  st->codec->codec_id = id;
90 
91  st->attached_pic = pkt;
94  } else {
96  if (!st->codec->extradata)
97  return AVERROR(ENOMEM);
98  if (avio_read(pb, st->codec->extradata, size) != size) {
99  av_freep(&st->codec->extradata);
100  return AVERROR(EIO);
101  }
102  st->codec->extradata_size = size;
104  }
105  } else {
106  value = av_malloc(size+1);
107  if (!value)
108  return AVERROR(ENOMEM);
109  c = avio_read(pb, value, size);
110  if (c < 0) {
111  av_free(value);
112  return c;
113  }
114  value[c] = 0;
115  av_dict_set(&s->metadata, key, value, AV_DICT_DONT_STRDUP_VAL);
116  }
117  return 0;
118 }
119 
121 {
122  AVIOContext *pb = s->pb;
123  int64_t file_size = avio_size(pb);
124  uint32_t val, fields, tag_bytes;
125  uint8_t buf[8];
126  int64_t tag_start;
127  int i;
128 
129  if (file_size < APE_TAG_FOOTER_BYTES)
130  return 0;
131 
132  avio_seek(pb, file_size - APE_TAG_FOOTER_BYTES, SEEK_SET);
133 
134  avio_read(pb, buf, 8); /* APETAGEX */
135  if (strncmp(buf, "APETAGEX", 8)) {
136  return 0;
137  }
138 
139  val = avio_rl32(pb); /* APE tag version */
140  if (val > APE_TAG_VERSION) {
141  av_log(s, AV_LOG_ERROR, "Unsupported tag version. (>=%d)\n", APE_TAG_VERSION);
142  return 0;
143  }
144 
145  tag_bytes = avio_rl32(pb); /* tag size */
146  if (tag_bytes - APE_TAG_FOOTER_BYTES > (1024 * 1024 * 16)) {
147  av_log(s, AV_LOG_ERROR, "Tag size is way too big\n");
148  return 0;
149  }
150 
151  if (tag_bytes > file_size - APE_TAG_FOOTER_BYTES) {
152  av_log(s, AV_LOG_ERROR, "Invalid tag size %u.\n", tag_bytes);
153  return 0;
154  }
155  tag_start = file_size - tag_bytes - APE_TAG_FOOTER_BYTES;
156 
157  fields = avio_rl32(pb); /* number of fields */
158  if (fields > 65536) {
159  av_log(s, AV_LOG_ERROR, "Too many tag fields (%d)\n", fields);
160  return 0;
161  }
162 
163  val = avio_rl32(pb); /* flags */
164  if (val & APE_TAG_FLAG_IS_HEADER) {
165  av_log(s, AV_LOG_ERROR, "APE Tag is a header\n");
166  return 0;
167  }
168 
169  avio_seek(pb, file_size - tag_bytes, SEEK_SET);
170 
171  for (i=0; i<fields; i++)
172  if (ape_tag_read_field(s) < 0) break;
173 
174  return tag_start;
175 }
176 
178 {
179  AVDictionaryEntry *e = NULL;
180  int64_t start, end;
181  int size, count = 0;
182 
183  if (!s->pb->seekable)
184  return 0;
185 
186  start = avio_tell(s->pb);
187 
188  // header
189  avio_write(s->pb, "APETAGEX", 8); // id
190  avio_wl32 (s->pb, APE_TAG_VERSION); // version
191  avio_wl32(s->pb, 0); // reserve space for size
192  avio_wl32(s->pb, 0); // reserve space for tag count
193 
194  // flags
197  ffio_fill(s->pb, 0, 8); // reserved
198 
199  while ((e = av_dict_get(s->metadata, "", e, AV_DICT_IGNORE_SUFFIX))) {
200  int val_len = strlen(e->value);
201 
202  avio_wl32(s->pb, val_len); // value length
203  avio_wl32(s->pb, 0); // item flags
204  avio_put_str(s->pb, e->key); // key
205  avio_write(s->pb, e->value, val_len); // value
206  count++;
207  }
208 
209  size = avio_tell(s->pb) - start;
210 
211  // footer
212  avio_write(s->pb, "APETAGEX", 8); // id
213  avio_wl32 (s->pb, APE_TAG_VERSION); // version
214  avio_wl32(s->pb, size); // size
215  avio_wl32(s->pb, count); // tag count
216 
217  // flags
219  ffio_fill(s->pb, 0, 8); // reserved
220 
221  // update values in the header
222  end = avio_tell(s->pb);
223  avio_seek(s->pb, start + 12, SEEK_SET);
224  avio_wl32(s->pb, size);
225  avio_wl32(s->pb, count);
226  avio_seek(s->pb, end, SEEK_SET);
227 
228  return 0;
229 }