1 /*
2  * This file is part of mpv.
3  *
4  * mpv is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * mpv is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with mpv.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <stdlib.h>
19 #include <stdbool.h>
20 #include <string.h>
21 #include <inttypes.h>
22 
23 #include "mpv_talloc.h"
24 
25 #include "misc/bstr.h"
26 #include "common/common.h"
27 #include "common/tags.h"
28 
29 #include "cue.h"
30 
31 #define SECS_PER_CUE_FRAME (1.0/75.0)
32 
33 enum cue_command {
34     CUE_ERROR = -1,     // not a valid CUE command, or an unknown extension
35     CUE_EMPTY,          // line with whitespace only
36     CUE_UNUSED,         // valid CUE command, but ignored by this code
37     CUE_FILE,
38     CUE_TRACK,
39     CUE_INDEX,
40     CUE_TITLE,
41     CUE_PERFORMER,
42 };
43 
44 static const struct {
45     enum cue_command command;
46     const char *text;
47 } cue_command_strings[] = {
48     { CUE_FILE, "FILE" },
49     { CUE_TRACK, "TRACK" },
50     { CUE_INDEX, "INDEX" },
51     { CUE_TITLE, "TITLE" },
52     { CUE_UNUSED, "CATALOG" },
53     { CUE_UNUSED, "CDTEXTFILE" },
54     { CUE_UNUSED, "FLAGS" },
55     { CUE_UNUSED, "ISRC" },
56     { CUE_PERFORMER, "PERFORMER" },
57     { CUE_UNUSED, "POSTGAP" },
58     { CUE_UNUSED, "PREGAP" },
59     { CUE_UNUSED, "REM" },
60     { CUE_UNUSED, "SONGWRITER" },
61     { CUE_UNUSED, "MESSAGE" },
62     { -1 },
63 };
64 
65 static const uint8_t spaces[] = {' ', '\f', '\n', '\r', '\t', '\v', 0xA0};
66 
lstrip_whitespace(struct bstr data)67 static struct bstr lstrip_whitespace(struct bstr data)
68 {
69     while (data.len) {
70         bstr rest = data;
71         int code = bstr_decode_utf8(data, &rest);
72         if (code < 0) {
73             // Tolerate Latin1 => probing works (which doesn't convert charsets).
74             code = data.start[0];
75             rest.start += 1;
76             rest.len -= 1;
77         }
78         for (size_t n = 0; n < MP_ARRAY_SIZE(spaces); n++) {
79             if (spaces[n] == code) {
80                 data = rest;
81                 goto next;
82             }
83         }
84         break;
85     next: ;
86     }
87     return data;
88 }
89 
read_cmd(struct bstr * data,struct bstr * out_params)90 static enum cue_command read_cmd(struct bstr *data, struct bstr *out_params)
91 {
92     struct bstr line = bstr_strip_linebreaks(bstr_getline(*data, data));
93     line = lstrip_whitespace(line);
94     if (line.len == 0)
95         return CUE_EMPTY;
96     for (int n = 0; cue_command_strings[n].command != -1; n++) {
97         struct bstr name = bstr0(cue_command_strings[n].text);
98         if (bstr_case_startswith(line, name)) {
99             struct bstr rest = bstr_cut(line, name.len);
100             struct bstr par = lstrip_whitespace(rest);
101             if (rest.len && par.len == rest.len)
102                 continue;
103             if (out_params)
104                 *out_params = par;
105             return cue_command_strings[n].command;
106         }
107     }
108     return CUE_ERROR;
109 }
110 
eat_char(struct bstr * data,char ch)111 static bool eat_char(struct bstr *data, char ch)
112 {
113     if (data->len && data->start[0] == ch) {
114         *data = bstr_cut(*data, 1);
115         return true;
116     } else {
117         return false;
118     }
119 }
120 
read_quoted(void * talloc_ctx,struct bstr * data)121 static char *read_quoted(void *talloc_ctx, struct bstr *data)
122 {
123     *data = lstrip_whitespace(*data);
124     if (!eat_char(data, '"'))
125         return NULL;
126     int end = bstrchr(*data, '"');
127     if (end < 0)
128         return NULL;
129     struct bstr res = bstr_splice(*data, 0, end);
130     *data = bstr_cut(*data, end + 1);
131     return bstrto0(talloc_ctx, res);
132 }
133 
strip_quotes(struct bstr data)134 static struct bstr strip_quotes(struct bstr data)
135 {
136     bstr s = data;
137     if (bstr_eatstart0(&s, "\"") && bstr_eatend0(&s, "\""))
138         return s;
139     return data;
140 }
141 
142 // Read an unsigned decimal integer.
143 // Optionally check if it is 2 digit.
144 // Return -1 on failure.
read_int(struct bstr * data,bool two_digit)145 static int read_int(struct bstr *data, bool two_digit)
146 {
147     *data = lstrip_whitespace(*data);
148     if (data->len && data->start[0] == '-')
149         return -1;
150     struct bstr s = *data;
151     int res = (int)bstrtoll(s, &s, 10);
152     if (data->len == s.len || (two_digit && data->len - s.len > 2))
153         return -1;
154     *data = s;
155     return res;
156 }
157 
read_time(struct bstr * data)158 static double read_time(struct bstr *data)
159 {
160     struct bstr s = *data;
161     bool ok = true;
162     double t1 = read_int(&s, false);
163     ok = eat_char(&s, ':') && ok;
164     double t2 = read_int(&s, true);
165     ok = eat_char(&s, ':') && ok;
166     double t3 = read_int(&s, true);
167     ok = ok && t1 >= 0 && t2 >= 0 && t3 >= 0;
168     return ok ? t1 * 60.0 + t2 + t3 * SECS_PER_CUE_FRAME : 0;
169 }
170 
skip_utf8_bom(struct bstr data)171 static struct bstr skip_utf8_bom(struct bstr data)
172 {
173     return bstr_startswith0(data, "\xEF\xBB\xBF") ? bstr_cut(data, 3) : data;
174 }
175 
176 // Check if the text in data is most likely CUE data. This is used by the
177 // demuxer code to check the file type.
178 // data is the start of the probed file, possibly cut off at a random point.
mp_probe_cue(struct bstr data)179 bool mp_probe_cue(struct bstr data)
180 {
181     bool valid = false;
182     data = skip_utf8_bom(data);
183     for (;;) {
184         enum cue_command cmd = read_cmd(&data, NULL);
185         // End reached. Since the line was most likely cut off, don't use the
186         // result of the last parsing call.
187         if (data.len == 0)
188             break;
189         if (cmd == CUE_ERROR)
190             return false;
191         if (cmd != CUE_EMPTY)
192             valid = true;
193     }
194     return valid;
195 }
196 
mp_parse_cue(struct bstr data)197 struct cue_file *mp_parse_cue(struct bstr data)
198 {
199     struct cue_file *f = talloc_zero(NULL, struct cue_file);
200     f->tags = talloc_zero(f, struct mp_tags);
201 
202     data = skip_utf8_bom(data);
203 
204     char *filename = NULL;
205     // Global metadata, and copied into new tracks.
206     struct cue_track proto_track = {0};
207     struct cue_track *cur_track = NULL;
208 
209     while (data.len) {
210         struct bstr param;
211         int cmd = read_cmd(&data, &param);
212         switch (cmd) {
213         case CUE_ERROR:
214             talloc_free(f);
215             return NULL;
216         case CUE_TRACK: {
217             MP_TARRAY_GROW(f, f->tracks, f->num_tracks);
218             f->num_tracks += 1;
219             cur_track = &f->tracks[f->num_tracks - 1];
220             *cur_track = proto_track;
221             cur_track->tags = talloc_zero(f, struct mp_tags);
222             break;
223         }
224         case CUE_TITLE:
225         case CUE_PERFORMER: {
226             static const char *metanames[] = {
227                 [CUE_TITLE] = "title",
228                 [CUE_PERFORMER] = "performer",
229             };
230             struct mp_tags *tags = cur_track ? cur_track->tags : f->tags;
231             mp_tags_set_bstr(tags, bstr0(metanames[cmd]), strip_quotes(param));
232             break;
233         }
234         case CUE_INDEX: {
235             int type = read_int(&param, true);
236             double time = read_time(&param);
237             if (cur_track) {
238                 if (type == 1) {
239                     cur_track->start = time;
240                     cur_track->filename = filename;
241                 } else if (type == 0) {
242                     cur_track->pregap_start = time;
243                 }
244             }
245             break;
246         }
247         case CUE_FILE:
248             // NOTE: FILE comes before TRACK, so don't use cur_track->filename
249             filename = read_quoted(f, &param);
250             break;
251         }
252     }
253 
254     return f;
255 }
256 
mp_check_embedded_cue(struct cue_file * f)257 int mp_check_embedded_cue(struct cue_file *f)
258 {
259     char *fn0 = f->tracks[0].filename;
260     for (int n = 1; n < f->num_tracks; n++) {
261         char *fn = f->tracks[n].filename;
262         // both filenames have the same address (including NULL)
263         if (fn0 == fn)
264             continue;
265         // only one filename is NULL, or the strings don't match
266         if (!fn0 || !fn || strcmp(fn0, fn) != 0)
267             return -1;
268     }
269     return 0;
270 }
271