]> git.sesse.net Git - ffmpeg/blob - libavformat/id3v2enc.c
crypto: consistently use size_t as type for length parameters
[ffmpeg] / libavformat / id3v2enc.c
1 /*
2  * ID3v2 header writer
3  *
4  * This file is part of Libav.
5  *
6  * Libav is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * Libav is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with Libav; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #include <stdint.h>
22 #include <string.h>
23
24 #include "libavutil/avstring.h"
25 #include "libavutil/dict.h"
26 #include "libavutil/intreadwrite.h"
27 #include "avformat.h"
28 #include "avio.h"
29 #include "id3v2.h"
30
31 static void id3v2_put_size(AVIOContext *pb, int size)
32 {
33     avio_w8(pb, size >> 21 & 0x7f);
34     avio_w8(pb, size >> 14 & 0x7f);
35     avio_w8(pb, size >> 7  & 0x7f);
36     avio_w8(pb, size       & 0x7f);
37 }
38
39 static int string_is_ascii(const uint8_t *str)
40 {
41     while (*str && *str < 128) str++;
42     return !*str;
43 }
44
45 static void id3v2_encode_string(AVIOContext *pb, const uint8_t *str,
46                                enum ID3v2Encoding enc)
47 {
48     int (*put)(AVIOContext*, const char*);
49
50     if (enc == ID3v2_ENCODING_UTF16BOM) {
51         avio_wl16(pb, 0xFEFF);      /* BOM */
52         put = avio_put_str16le;
53     } else
54         put = avio_put_str;
55
56     put(pb, str);
57 }
58
59 /**
60  * Write a text frame with one (normal frames) or two (TXXX frames) strings
61  * according to encoding (only UTF-8 or UTF-16+BOM supported).
62  * @return number of bytes written or a negative error code.
63  */
64 static int id3v2_put_ttag(ID3v2EncContext *id3, AVIOContext *avioc, const char *str1, const char *str2,
65                           uint32_t tag, enum ID3v2Encoding enc)
66 {
67     int len;
68     uint8_t *pb;
69     AVIOContext *dyn_buf;
70     if (avio_open_dyn_buf(&dyn_buf) < 0)
71         return AVERROR(ENOMEM);
72
73     /* check if the strings are ASCII-only and use UTF16 only if
74      * they're not */
75     if (enc == ID3v2_ENCODING_UTF16BOM && string_is_ascii(str1) &&
76         (!str2 || string_is_ascii(str2)))
77         enc = ID3v2_ENCODING_ISO8859;
78
79     avio_w8(dyn_buf, enc);
80     id3v2_encode_string(dyn_buf, str1, enc);
81     if (str2)
82         id3v2_encode_string(dyn_buf, str2, enc);
83     len = avio_close_dyn_buf(dyn_buf, &pb);
84
85     avio_wb32(avioc, tag);
86     /* ID3v2.3 frame size is not sync-safe */
87     if (id3->version == 3)
88         avio_wb32(avioc, len);
89     else
90         id3v2_put_size(avioc, len);
91     avio_wb16(avioc, 0);
92     avio_write(avioc, pb, len);
93
94     av_freep(&pb);
95     return len + ID3v2_HEADER_SIZE;
96 }
97
98 static int id3v2_check_write_tag(ID3v2EncContext *id3, AVIOContext *pb, AVDictionaryEntry *t,
99                                  const char table[][4], enum ID3v2Encoding enc)
100 {
101     uint32_t tag;
102     int i;
103
104     if (t->key[0] != 'T' || strlen(t->key) != 4)
105         return -1;
106     tag = AV_RB32(t->key);
107     for (i = 0; *table[i]; i++)
108         if (tag == AV_RB32(table[i]))
109             return id3v2_put_ttag(id3, pb, t->value, NULL, tag, enc);
110     return -1;
111 }
112
113 void ff_id3v2_start(ID3v2EncContext *id3, AVIOContext *pb, int id3v2_version,
114                     const char *magic)
115 {
116     id3->version = id3v2_version;
117
118     avio_wb32(pb, MKBETAG(magic[0], magic[1], magic[2], id3v2_version));
119     avio_w8(pb, 0);
120     avio_w8(pb, 0); /* flags */
121
122     /* reserve space for size */
123     id3->size_pos = avio_tell(pb);
124     avio_wb32(pb, 0);
125 }
126
127 int ff_id3v2_write_metadata(AVFormatContext *s, ID3v2EncContext *id3)
128 {
129     AVDictionaryEntry *t = NULL;
130     int enc = id3->version == 3 ? ID3v2_ENCODING_UTF16BOM :
131                                   ID3v2_ENCODING_UTF8;
132
133     ff_metadata_conv(&s->metadata, ff_id3v2_34_metadata_conv, NULL);
134     if (id3->version == 4)
135         ff_metadata_conv(&s->metadata, ff_id3v2_4_metadata_conv, NULL);
136
137     while ((t = av_dict_get(s->metadata, "", t, AV_DICT_IGNORE_SUFFIX))) {
138         int ret;
139
140         if ((ret = id3v2_check_write_tag(id3, s->pb, t, ff_id3v2_tags, enc)) > 0) {
141             id3->len += ret;
142             continue;
143         }
144         if ((ret = id3v2_check_write_tag(id3, s->pb, t, id3->version == 3 ?
145                                                ff_id3v2_3_tags : ff_id3v2_4_tags, enc)) > 0) {
146             id3->len += ret;
147             continue;
148         }
149
150         /* unknown tag, write as TXXX frame */
151         if ((ret = id3v2_put_ttag(id3, s->pb, t->key, t->value, MKBETAG('T', 'X', 'X', 'X'), enc)) < 0)
152             return ret;
153         id3->len += ret;
154     }
155
156     return 0;
157 }
158
159 int ff_id3v2_write_apic(AVFormatContext *s, ID3v2EncContext *id3, AVPacket *pkt)
160 {
161     AVStream *st = s->streams[pkt->stream_index];
162     AVDictionaryEntry *e;
163
164     AVIOContext *dyn_buf;
165     uint8_t     *buf;
166     const CodecMime *mime = ff_id3v2_mime_tags;
167     const char  *mimetype = NULL, *desc = "";
168     int enc = id3->version == 3 ? ID3v2_ENCODING_UTF16BOM :
169                                   ID3v2_ENCODING_UTF8;
170     int i, len, type = 0;
171
172     /* get the mimetype*/
173     while (mime->id != AV_CODEC_ID_NONE) {
174         if (mime->id == st->codecpar->codec_id) {
175             mimetype = mime->str;
176             break;
177         }
178         mime++;
179     }
180     if (!mimetype) {
181         av_log(s, AV_LOG_ERROR, "No mimetype is known for stream %d, cannot "
182                "write an attached picture.\n", st->index);
183         return AVERROR(EINVAL);
184     }
185
186     /* get the picture type */
187     e = av_dict_get(st->metadata, "comment", NULL, 0);
188     for (i = 0; e && i < FF_ARRAY_ELEMS(ff_id3v2_picture_types); i++) {
189         if (!av_strcasecmp(e->value, ff_id3v2_picture_types[i])) {
190             type = i;
191             break;
192         }
193     }
194
195     /* get the description */
196     if ((e = av_dict_get(st->metadata, "title", NULL, 0)))
197         desc = e->value;
198
199     /* start writing */
200     if (avio_open_dyn_buf(&dyn_buf) < 0)
201         return AVERROR(ENOMEM);
202
203     avio_w8(dyn_buf, enc);
204     avio_put_str(dyn_buf, mimetype);
205     avio_w8(dyn_buf, type);
206     id3v2_encode_string(dyn_buf, desc, enc);
207     avio_write(dyn_buf, pkt->data, pkt->size);
208     len = avio_close_dyn_buf(dyn_buf, &buf);
209
210     avio_wb32(s->pb, MKBETAG('A', 'P', 'I', 'C'));
211     if (id3->version == 3)
212         avio_wb32(s->pb, len);
213     else
214         id3v2_put_size(s->pb, len);
215     avio_wb16(s->pb, 0);
216     avio_write(s->pb, buf, len);
217     av_freep(&buf);
218
219     id3->len += len + ID3v2_HEADER_SIZE;
220
221     return 0;
222 }
223
224 void ff_id3v2_finish(ID3v2EncContext *id3, AVIOContext *pb)
225 {
226     int64_t cur_pos = avio_tell(pb);
227     avio_seek(pb, id3->size_pos, SEEK_SET);
228     id3v2_put_size(pb, id3->len);
229     avio_seek(pb, cur_pos, SEEK_SET);
230 }
231
232 int ff_id3v2_write_simple(struct AVFormatContext *s, int id3v2_version,
233                           const char *magic)
234 {
235     ID3v2EncContext id3 = { 0 };
236     int ret;
237
238     ff_id3v2_start(&id3, s->pb, id3v2_version, magic);
239     if ((ret = ff_id3v2_write_metadata(s, &id3)) < 0)
240         return ret;
241     ff_id3v2_finish(&id3, s->pb);
242
243     return 0;
244 }