Line | Count | Source |
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 <stdio.h> |
19 | | #include <stdlib.h> |
20 | | #include <limits.h> |
21 | | |
22 | | #include <assert.h> |
23 | | |
24 | | #include "osdep/io.h" |
25 | | |
26 | | #include "mpv_talloc.h" |
27 | | |
28 | | #include "config.h" |
29 | | |
30 | | #include "common/common.h" |
31 | | #include "common/global.h" |
32 | | #include "demux/demux.h" |
33 | | #include "misc/bstr.h" |
34 | | #include "misc/thread_tools.h" |
35 | | #include "common/msg.h" |
36 | | #include "options/m_config.h" |
37 | | #include "options/options.h" |
38 | | #include "options/path.h" |
39 | | #include "osdep/timer.h" |
40 | | #include "stream.h" |
41 | | |
42 | | #include "options/m_option.h" |
43 | | #include "options/m_config.h" |
44 | | |
45 | | extern const stream_info_t stream_info_mpv; |
46 | | extern const stream_info_t stream_info_cdda; |
47 | | extern const stream_info_t stream_info_dvb; |
48 | | extern const stream_info_t stream_info_null; |
49 | | extern const stream_info_t stream_info_memory; |
50 | | extern const stream_info_t stream_info_mf; |
51 | | extern const stream_info_t stream_info_ffmpeg; |
52 | | extern const stream_info_t stream_info_ffmpeg_unsafe; |
53 | | extern const stream_info_t stream_info_avdevice; |
54 | | extern const stream_info_t stream_info_file; |
55 | | extern const stream_info_t stream_info_slice; |
56 | | extern const stream_info_t stream_info_aes_ctr; |
57 | | extern const stream_info_t stream_info_fd; |
58 | | extern const stream_info_t stream_info_ifo_dvdnav; |
59 | | extern const stream_info_t stream_info_dvdnav; |
60 | | extern const stream_info_t stream_info_ifo_dvda; |
61 | | extern const stream_info_t stream_info_dvda; |
62 | | extern const stream_info_t stream_info_bdmv_dir; |
63 | | extern const stream_info_t stream_info_bluray; |
64 | | extern const stream_info_t stream_info_edl; |
65 | | extern const stream_info_t stream_info_libarchive; |
66 | | extern const stream_info_t stream_info_cb; |
67 | | extern const stream_info_t stream_info_curl; |
68 | | extern const stream_info_t stream_info_env; |
69 | | |
70 | | static const stream_info_t *const stream_list[] = { |
71 | | &stream_info_mpv, |
72 | | #if HAVE_CDDA |
73 | | &stream_info_cdda, |
74 | | #endif |
75 | | &stream_info_avdevice, |
76 | | #if HAVE_DVBIN |
77 | | &stream_info_dvb, |
78 | | #endif |
79 | | #if HAVE_DVDA |
80 | | &stream_info_ifo_dvda, |
81 | | &stream_info_dvda, |
82 | | #endif |
83 | | #if HAVE_DVDNAV |
84 | | &stream_info_ifo_dvdnav, |
85 | | &stream_info_dvdnav, |
86 | | #endif |
87 | | #if HAVE_LIBBLURAY |
88 | | &stream_info_bdmv_dir, |
89 | | &stream_info_bluray, |
90 | | #endif |
91 | | #if HAVE_LIBARCHIVE |
92 | | &stream_info_libarchive, |
93 | | #endif |
94 | | &stream_info_memory, |
95 | | &stream_info_null, |
96 | | &stream_info_mf, |
97 | | &stream_info_edl, |
98 | | &stream_info_file, |
99 | | &stream_info_slice, |
100 | | &stream_info_aes_ctr, |
101 | | &stream_info_fd, |
102 | | &stream_info_cb, |
103 | | #if HAVE_LIBCURL |
104 | | &stream_info_curl, |
105 | | #endif |
106 | | &stream_info_ffmpeg, |
107 | | &stream_info_ffmpeg_unsafe, |
108 | | &stream_info_env, |
109 | | }; |
110 | | |
111 | | // Because of guarantees documented on STREAM_BUFFER_SIZE. |
112 | | // Half the buffer is used as forward buffer, the other for seek-back. |
113 | | #define STREAM_MIN_BUFFER_SIZE (STREAM_BUFFER_SIZE * 2) |
114 | | // Sort of arbitrary; keep *2 of it comfortably within integer limits. |
115 | | // Must be power of 2. |
116 | | #define STREAM_MAX_BUFFER_SIZE (512 * 1024 * 1024) |
117 | | |
118 | | struct stream_opts { |
119 | | int64_t buffer_size; |
120 | | bool load_unsafe_playlists; |
121 | | }; |
122 | | |
123 | | #define OPT_BASE_STRUCT struct stream_opts |
124 | | |
125 | | const struct m_sub_options stream_conf = { |
126 | | .opts = (const struct m_option[]){ |
127 | | {"stream-buffer-size", OPT_BYTE_SIZE(buffer_size), |
128 | | M_RANGE(STREAM_MIN_BUFFER_SIZE, STREAM_MAX_BUFFER_SIZE)}, |
129 | | {"load-unsafe-playlists", OPT_BOOL(load_unsafe_playlists)}, |
130 | | {0} |
131 | | }, |
132 | | .size = sizeof(struct stream_opts), |
133 | | .defaults = &(const struct stream_opts){ |
134 | | .buffer_size = 128 * 1024, |
135 | | }, |
136 | | }; |
137 | | |
138 | | // return -1 if not hex char |
139 | | static int hex2dec(char c) |
140 | 1.62k | { |
141 | 1.62k | if (c >= '0' && c <= '9') |
142 | 115 | return c - '0'; |
143 | 1.50k | if (c >= 'A' && c <= 'F') |
144 | 68 | return 10 + c - 'A'; |
145 | 1.43k | if (c >= 'a' && c <= 'f') |
146 | 41 | return 10 + c - 'a'; |
147 | 1.39k | return -1; |
148 | 1.43k | } |
149 | | |
150 | | // Replace escape sequences in an URL (or a part of an URL) |
151 | | void mp_url_unescape_inplace(char *url) |
152 | 27.5M | { |
153 | 1.32G | for (int len = strlen(url), i = 0, o = 0; i <= len;) { |
154 | 1.29G | if ((url[i] != '%') || (i > len - 3)) { // %NN can't start after len-3 |
155 | 1.29G | url[o++] = url[i++]; |
156 | 1.29G | continue; |
157 | 1.29G | } |
158 | | |
159 | 811 | int msd = hex2dec(url[i + 1]), |
160 | 811 | lsd = hex2dec(url[i + 2]); |
161 | | |
162 | 811 | if (msd >= 0 && lsd >= 0) { |
163 | 53 | url[o++] = 16 * msd + lsd; |
164 | 53 | i += 3; |
165 | 758 | } else { |
166 | 758 | url[o++] = url[i++]; |
167 | 758 | url[o++] = url[i++]; |
168 | 758 | url[o++] = url[i++]; |
169 | 758 | } |
170 | 811 | } |
171 | 27.5M | } |
172 | | |
173 | | char *mp_url_unescape(void *talloc_ctx, const char *url) |
174 | 3.46k | { |
175 | 3.46k | char *unescaped = talloc_strdup(talloc_ctx, url); |
176 | 3.46k | mp_url_unescape_inplace(unescaped); |
177 | 3.46k | return unescaped; |
178 | 3.46k | } |
179 | | |
180 | | static const char hex_digits[] = "0123456789ABCDEF"; |
181 | | |
182 | | |
183 | | static const char url_default_ok[] = "abcdefghijklmnopqrstuvwxyz" |
184 | | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
185 | | "0123456789" |
186 | | "-._~"; |
187 | | |
188 | | // Escape according to http://tools.ietf.org/html/rfc3986#section-2.1 |
189 | | // Only unreserved characters are not escaped. |
190 | | // The argument ok (if not NULL) is as follows: |
191 | | // ok[0] != '~': additional characters that are not escaped |
192 | | // ok[0] == '~': do not escape anything but these characters |
193 | | // (can't override the unreserved characters, which are |
194 | | // never escaped) |
195 | | char *mp_url_escape(void *talloc_ctx, const char *url, const char *ok) |
196 | 0 | { |
197 | 0 | char *rv = talloc_size(talloc_ctx, strlen(url) * 3 + 1); |
198 | 0 | char *out = rv; |
199 | 0 | bool negate = ok && ok[0] == '~'; |
200 | |
|
201 | 0 | for (char c; (c = *url); url++) { |
202 | 0 | bool as_is = negate ? !strchr(ok + 1, c) |
203 | 0 | : (strchr(url_default_ok, c) || (ok && strchr(ok, c))); |
204 | 0 | if (as_is) { |
205 | 0 | *out++ = c; |
206 | 0 | } else { |
207 | 0 | unsigned char v = c; |
208 | 0 | *out++ = '%'; |
209 | 0 | *out++ = hex_digits[v / 16]; |
210 | 0 | *out++ = hex_digits[v % 16]; |
211 | 0 | } |
212 | 0 | } |
213 | |
|
214 | 0 | *out = 0; |
215 | 0 | return rv; |
216 | 0 | } |
217 | | |
218 | | static const char *match_proto(const char *url, const char *proto) |
219 | 1.26M | { |
220 | 1.26M | int l = strlen(proto); |
221 | 1.26M | if (l > 0) { |
222 | 1.20M | if (strncasecmp(url, proto, l) == 0 && strncmp("://", url + l, 3) == 0) |
223 | 128k | return url + l + 3; |
224 | 1.20M | } else if (!mp_is_url(bstr0(url))) { |
225 | 26.0k | return url; // pure filenames |
226 | 26.0k | } |
227 | 1.10M | return NULL; |
228 | 1.26M | } |
229 | | |
230 | | // src and new are both STREAM_ORIGIN_* values. This checks whether a stream |
231 | | // with flags "new" can be opened from the "src". On success, return |
232 | | // new origin, on incompatibility return 0. |
233 | | static int check_origin(int src, int new) |
234 | 102k | { |
235 | 102k | switch (src) { |
236 | 101k | case STREAM_ORIGIN_DIRECT: |
237 | 101k | case STREAM_ORIGIN_UNSAFE: |
238 | | // Allow anything, but constrain it to the new origin. |
239 | 101k | return new; |
240 | 303 | case STREAM_ORIGIN_FS: |
241 | | // From unix FS, allow all but unsafe. |
242 | 303 | if (new == STREAM_ORIGIN_FS || new == STREAM_ORIGIN_NET) |
243 | 289 | return new; |
244 | 14 | break; |
245 | 14 | case STREAM_ORIGIN_NET: |
246 | | // Allow only other network links. |
247 | 0 | if (new == STREAM_ORIGIN_NET) |
248 | 0 | return new; |
249 | 0 | break; |
250 | 102k | } |
251 | 14 | return 0; |
252 | 102k | } |
253 | | |
254 | | // Read len bytes from the start position, and wrap around as needed. Limit the |
255 | | // actually read data to the size of the buffer. Return amount of copied bytes. |
256 | | // len: max bytes to copy to dst |
257 | | // pos: index into s->buffer[], e.g. s->buf_start is byte 0 |
258 | | // returns: bytes copied to dst (limited by len and available buffered data) |
259 | | static int ring_copy(struct stream *s, void *dst, int len, int pos) |
260 | 708k | { |
261 | 708k | mp_assert(len >= 0); |
262 | | |
263 | 708k | if (pos < s->buf_start || pos > s->buf_end) |
264 | 0 | return 0; |
265 | | |
266 | 708k | int copied = 0; |
267 | 708k | len = MPMIN(len, s->buf_end - pos); |
268 | | |
269 | 708k | if (len && pos <= s->buffer_mask) { |
270 | 371k | int copy = MPMIN(len, s->buffer_mask + 1 - pos); |
271 | 371k | memcpy(dst, &s->buffer[pos], copy); |
272 | 371k | copied += copy; |
273 | 371k | len -= copy; |
274 | 371k | pos += copy; |
275 | 371k | } |
276 | | |
277 | 708k | if (len) { |
278 | 0 | memcpy((char *)dst + copied, &s->buffer[pos & s->buffer_mask], len); |
279 | 0 | copied += len; |
280 | 0 | } |
281 | | |
282 | 708k | return copied; |
283 | 708k | } |
284 | | |
285 | | // Resize the current stream buffer. Uses a larger size if needed to keep data. |
286 | | // Does nothing if the size is adequate. Calling this with 0 ensures it uses the |
287 | | // default buffer size if possible. |
288 | | // The caller must check whether enough data was really allocated. |
289 | | // keep: keep at least [buf_end-keep, buf_end] (used for mp_assert()s only) |
290 | | // new: new total size of buffer |
291 | | // returns: false if buffer allocation failed, true if reallocated or size ok |
292 | | static bool stream_resize_buffer(struct stream *s, int keep, int new) |
293 | 767k | { |
294 | 767k | mp_assert(keep >= s->buf_end - s->buf_cur); |
295 | 767k | mp_assert(keep <= new); |
296 | | |
297 | 767k | new = MPMAX(new, s->requested_buffer_size); |
298 | 767k | new = MPMIN(new, STREAM_MAX_BUFFER_SIZE); |
299 | 767k | new = mp_round_next_power_of_2(new); |
300 | | |
301 | 767k | mp_assert(keep <= new); // can't fail (if old buffer size was valid) |
302 | | |
303 | 767k | if (new == s->buffer_mask + 1) |
304 | 664k | return true; |
305 | | |
306 | 102k | int old_pos = s->buf_cur - s->buf_start; |
307 | 102k | int old_used_len = s->buf_end - s->buf_start; |
308 | 102k | int skip = old_used_len > new ? old_used_len - new : 0; |
309 | | |
310 | 102k | MP_DBG(s, "resize stream to %d bytes, drop %d bytes\n", new, skip); |
311 | | |
312 | 102k | void *nbuf = ta_alloc_size(s, new); |
313 | 102k | if (!nbuf) |
314 | 0 | return false; // oom; tolerate it, caller needs to check if required |
315 | | |
316 | 102k | int new_len = 0; |
317 | 102k | if (s->buffer) |
318 | 1 | new_len = ring_copy(s, nbuf, new, s->buf_start + skip); |
319 | 102k | mp_assert(new_len == old_used_len - skip); |
320 | 102k | mp_assert(old_pos >= skip); // "keep" too low |
321 | 102k | mp_assert(old_pos - skip <= new_len); |
322 | 102k | s->buf_start = 0; |
323 | 102k | s->buf_cur = old_pos - skip; |
324 | 102k | s->buf_end = new_len; |
325 | | |
326 | 102k | ta_free(s->buffer); |
327 | | |
328 | 102k | s->buffer = nbuf; |
329 | 102k | s->buffer_mask = new - 1; |
330 | | |
331 | 102k | return true; |
332 | 102k | } |
333 | | |
334 | | static int stream_create_instance(const stream_info_t *sinfo, |
335 | | struct stream_open_args *args, |
336 | | struct stream **ret) |
337 | 811k | { |
338 | 811k | const char *url = args->url; |
339 | 811k | int flags = args->flags; |
340 | | |
341 | 811k | *ret = NULL; |
342 | | |
343 | 811k | const char *path = url; |
344 | | |
345 | 811k | if (flags & STREAM_LOCAL_FS_ONLY) { |
346 | 0 | if (!sinfo->local_fs) |
347 | 0 | return STREAM_NO_MATCH; |
348 | 811k | } else { |
349 | 811k | char **get_protocols = sinfo->get_protocols ? sinfo->get_protocols() : NULL; |
350 | 811k | char **protocols = get_protocols ? get_protocols : (char **)sinfo->protocols; |
351 | | |
352 | 1.91M | for (int n = 0; protocols && protocols[n]; n++) { |
353 | 1.26M | path = match_proto(url, protocols[n]); |
354 | 1.26M | if (path) |
355 | 154k | break; |
356 | 1.26M | } |
357 | | |
358 | 811k | talloc_free(get_protocols); |
359 | | |
360 | 811k | if (!path) |
361 | 626k | return STREAM_NO_MATCH; |
362 | 811k | } |
363 | | |
364 | 184k | stream_t *s = talloc_zero(NULL, stream_t); |
365 | 184k | s->global = args->global; |
366 | 184k | struct stream_opts *opts = mp_get_config_group(s, s->global, &stream_conf); |
367 | 184k | if (flags & STREAM_SILENT) { |
368 | 64.1k | s->log = mp_null_log; |
369 | 120k | } else { |
370 | 120k | s->log = mp_log_new(s, s->global->log, sinfo->name); |
371 | 120k | } |
372 | 184k | s->info = sinfo; |
373 | 184k | s->autoprobed = !args->sinfo; |
374 | 184k | s->cancel = args->cancel; |
375 | 184k | s->url = talloc_strdup(s, url); |
376 | 184k | s->path = talloc_strdup(s, path); |
377 | 184k | s->mode = flags & (STREAM_READ | STREAM_WRITE); |
378 | 184k | s->requested_buffer_size = opts->buffer_size; |
379 | 184k | s->allow_partial_read = flags & STREAM_ALLOW_PARTIAL_READ; |
380 | | |
381 | 184k | if (flags & STREAM_LESS_NOISE) |
382 | 0 | mp_msg_set_max_level(s->log, MSGL_WARN); |
383 | | |
384 | 184k | struct demux_opts *demux_opts = mp_get_config_group(s, s->global, &demux_conf); |
385 | 184k | s->access_references = demux_opts->access_references; |
386 | 184k | talloc_free(demux_opts); |
387 | | |
388 | 184k | MP_VERBOSE(s, "Opening %s\n", url); |
389 | | |
390 | 184k | if (strlen(url) > INT_MAX / 8) { |
391 | 0 | MP_ERR(s, "URL too large.\n"); |
392 | 0 | talloc_free(s); |
393 | 0 | return STREAM_ERROR; |
394 | 0 | } |
395 | | |
396 | 184k | if ((s->mode & STREAM_WRITE) && !sinfo->can_write) { |
397 | 0 | MP_DBG(s, "No write access implemented.\n"); |
398 | 0 | talloc_free(s); |
399 | 0 | return STREAM_NO_MATCH; |
400 | 0 | } |
401 | | |
402 | 184k | s->stream_origin = flags & STREAM_ORIGIN_MASK; // pass through by default |
403 | 184k | if (opts->load_unsafe_playlists) { |
404 | 89 | s->stream_origin = STREAM_ORIGIN_DIRECT; |
405 | 184k | } else if (sinfo->stream_origin) { |
406 | 102k | s->stream_origin = check_origin(s->stream_origin, sinfo->stream_origin); |
407 | 102k | } |
408 | | |
409 | 184k | if (!s->stream_origin) { |
410 | 14 | talloc_free(s); |
411 | 14 | return STREAM_UNSAFE; |
412 | 14 | } |
413 | | |
414 | 184k | int r = STREAM_UNSUPPORTED; |
415 | 184k | if (sinfo->open2) { |
416 | 108k | r = sinfo->open2(s, args); |
417 | 108k | } else if (!args->special_arg) { |
418 | 76.0k | r = (sinfo->open)(s); |
419 | 76.0k | } |
420 | 184k | if (r != STREAM_OK) { |
421 | 81.9k | talloc_free(s); |
422 | 81.9k | return r; |
423 | 81.9k | } |
424 | | |
425 | 102k | if (!stream_resize_buffer(s, 0, 0)) { |
426 | 0 | free_stream(s); |
427 | 0 | return STREAM_ERROR; |
428 | 0 | } |
429 | | |
430 | 102k | mp_assert(s->seekable == !!s->seek); |
431 | | |
432 | 102k | if (s->mime_type) |
433 | 102k | MP_VERBOSE(s, "Mime-type: '%s'\n", s->mime_type); |
434 | 102k | if (s->server_filename) |
435 | 102k | MP_VERBOSE(s, "Server filename: '%s'\n", s->server_filename); |
436 | | |
437 | 102k | MP_DBG(s, "Stream opened successfully.\n"); |
438 | | |
439 | 102k | *ret = s; |
440 | 102k | return STREAM_OK; |
441 | 102k | } |
442 | | |
443 | | int stream_create_with_args(struct stream_open_args *args, struct stream **ret) |
444 | 155k | { |
445 | 155k | mp_assert(args->url); |
446 | | |
447 | 155k | int r = STREAM_NO_MATCH; |
448 | 155k | *ret = NULL; |
449 | | |
450 | | // Open stream proper |
451 | 155k | if (args->sinfo) { |
452 | 64.1k | r = stream_create_instance(args->sinfo, args, ret); |
453 | 91.4k | } else { |
454 | 748k | for (int i = 0; i < MP_ARRAY_SIZE(stream_list); i++) { |
455 | 747k | r = stream_create_instance(stream_list[i], args, ret); |
456 | 747k | if (r == STREAM_OK) |
457 | 38.7k | break; |
458 | 708k | if (r == STREAM_NO_MATCH || r == STREAM_UNSUPPORTED) |
459 | 656k | continue; |
460 | 51.8k | if (r == STREAM_UNSAFE) |
461 | 14 | continue; |
462 | 51.7k | break; |
463 | 51.8k | } |
464 | 91.4k | } |
465 | | |
466 | 155k | if (!*ret && !(args->flags & STREAM_SILENT) && !mp_cancel_test(args->cancel)) |
467 | 51.9k | { |
468 | 51.9k | struct mp_log *log = mp_log_new(NULL, args->global->log, "!stream"); |
469 | | |
470 | 51.9k | if (r == STREAM_UNSAFE) { |
471 | 0 | mp_err(log, "\nRefusing to load potentially unsafe URL from a playlist.\n" |
472 | 0 | "Use the --load-unsafe-playlists option to load it anyway.\n\n"); |
473 | 51.9k | } else if (r == STREAM_NO_MATCH || r == STREAM_UNSUPPORTED) { |
474 | 885 | mp_err(log, "No protocol handler found to open URL %s\n", args->url); |
475 | 885 | mp_err(log, "The protocol is either unsupported, or was disabled " |
476 | 885 | "at compile-time.\n"); |
477 | 51.1k | } else { |
478 | 51.1k | mp_err(log, "Failed to open %s.\n", args->url); |
479 | 51.1k | } |
480 | | |
481 | 51.9k | talloc_free(log); |
482 | 51.9k | } |
483 | | |
484 | 155k | return r; |
485 | 155k | } |
486 | | |
487 | | struct stream *stream_create(const char *url, int flags, |
488 | | struct mp_cancel *c, struct mpv_global *global) |
489 | 91.4k | { |
490 | 91.4k | struct stream_open_args args = { |
491 | 91.4k | .global = global, |
492 | 91.4k | .cancel = c, |
493 | 91.4k | .flags = flags, |
494 | 91.4k | .url = url, |
495 | 91.4k | }; |
496 | 91.4k | struct stream *s; |
497 | 91.4k | stream_create_with_args(&args, &s); |
498 | 91.4k | return s; |
499 | 91.4k | } |
500 | | |
501 | | stream_t *open_output_stream(const char *filename, struct mpv_global *global) |
502 | 0 | { |
503 | 0 | struct stream *s = stream_create(filename, STREAM_ORIGIN_DIRECT | STREAM_WRITE, |
504 | 0 | NULL, global); |
505 | 0 | if (s && s->is_directory) { |
506 | 0 | free_stream(s); |
507 | 0 | s = NULL; |
508 | 0 | } |
509 | 0 | return s; |
510 | 0 | } |
511 | | |
512 | | // Read function bypassing the local stream buffer. This will not write into |
513 | | // s->buffer, but into buf[0..len] instead. |
514 | | // Returns 0 on error or EOF, and length of bytes read on success. |
515 | | // Partial reads are possible, even if EOF is not reached. |
516 | | static int stream_read_unbuffered(stream_t *s, void *buf, int len) |
517 | 664k | { |
518 | 664k | mp_assert(len >= 0); |
519 | 664k | if (len <= 0) |
520 | 0 | return 0; |
521 | | |
522 | 664k | int res = 0; |
523 | | // we will retry even if we already reached EOF previously. |
524 | 664k | if (s->fill_buffer && !mp_cancel_test(s->cancel)) |
525 | 663k | res = s->fill_buffer(s, buf, len); |
526 | 664k | if (res <= 0) { |
527 | 594k | s->eof = 1; |
528 | 594k | return 0; |
529 | 594k | } |
530 | 69.7k | mp_assert(res <= len); |
531 | | // When reading succeeded we are obviously not at eof. |
532 | 69.7k | s->eof = 0; |
533 | 69.7k | s->pos += res; |
534 | 69.7k | s->total_unbuffered_read_bytes += res; |
535 | 69.7k | return res; |
536 | 69.7k | } |
537 | | |
538 | | // Ask for having at most "forward" bytes ready to read in the buffer. |
539 | | // To read everything, you may have to call this in a loop. |
540 | | // forward: desired amount of bytes in buffer after s->cur_pos |
541 | | // returns: progress (false on EOF or on OOM or if enough data was available) |
542 | | static bool stream_read_more(struct stream *s, int forward) |
543 | 768k | { |
544 | 768k | mp_assert(forward >= 0); |
545 | | |
546 | 768k | int forward_avail = s->buf_end - s->buf_cur; |
547 | 768k | if (forward_avail >= forward) |
548 | 103k | return false; |
549 | | |
550 | | // Avoid that many small reads will lead to many low-level read calls. |
551 | 664k | forward = MPMAX(forward, s->requested_buffer_size / 2); |
552 | 664k | mp_assert(forward_avail < forward); |
553 | | |
554 | | // Keep guaranteed seek-back. |
555 | 664k | int buf_old = MPMIN(s->buf_cur - s->buf_start, s->requested_buffer_size / 2); |
556 | | |
557 | | // Never shrink the buffer here. That's stream_drop_buffers()'s job. Otherwise |
558 | | // data fetched by earlier larger reads (e.g. demuxer probing) would be |
559 | | // discarded, forcing redundant re-reads on backward seeks. |
560 | 664k | int new_size = MPMAX(buf_old + forward, s->buffer_mask + 1); |
561 | | |
562 | 664k | if (!stream_resize_buffer(s, buf_old + forward_avail, new_size)) |
563 | 0 | return false; |
564 | | |
565 | 664k | int buf_alloc = s->buffer_mask + 1; |
566 | | |
567 | 664k | mp_assert(s->buf_start <= s->buf_cur); |
568 | 664k | mp_assert(s->buf_cur <= s->buf_end); |
569 | 664k | mp_assert(s->buf_cur < buf_alloc * 2); |
570 | 664k | mp_assert(s->buf_end < buf_alloc * 2); |
571 | 664k | mp_assert(s->buf_start < buf_alloc); |
572 | | |
573 | | // Note: read as much as possible, even if forward is much smaller. Do |
574 | | // this because the stream buffer is supposed to set an approx. minimum |
575 | | // read size on it. |
576 | 664k | int read = buf_alloc - (buf_old + forward_avail); // free buffer past end |
577 | | |
578 | 664k | int pos = s->buf_end & s->buffer_mask; |
579 | 664k | read = MPMIN(read, buf_alloc - pos); |
580 | | |
581 | | // Note: if wrap-around happens, we need to make two calls. This may |
582 | | // affect latency (e.g. waiting for new data on a socket), so do only |
583 | | // 1 read call always. |
584 | 664k | read = stream_read_unbuffered(s, &s->buffer[pos], read); |
585 | | |
586 | 664k | s->buf_end += read; |
587 | | |
588 | | // May have overwritten old data. |
589 | 664k | if (s->buf_end - s->buf_start >= buf_alloc) { |
590 | 0 | mp_assert(s->buf_end >= buf_alloc); |
591 | | |
592 | 0 | s->buf_start = s->buf_end - buf_alloc; |
593 | |
|
594 | 0 | mp_assert(s->buf_start <= s->buf_cur); |
595 | 0 | mp_assert(s->buf_cur <= s->buf_end); |
596 | | |
597 | 0 | if (s->buf_start >= buf_alloc) { |
598 | 0 | s->buf_start -= buf_alloc; |
599 | 0 | s->buf_cur -= buf_alloc; |
600 | 0 | s->buf_end -= buf_alloc; |
601 | 0 | } |
602 | 0 | } |
603 | | |
604 | | // Must not have overwritten guaranteed old data. |
605 | 664k | mp_assert(s->buf_cur - s->buf_start >= buf_old); |
606 | | |
607 | 664k | if (s->buf_cur < s->buf_end) |
608 | 328k | s->eof = 0; |
609 | | |
610 | 664k | return !!read; |
611 | 664k | } |
612 | | |
613 | | // Read between 1..buf_size bytes of data, return how much data has been read. |
614 | | // Return 0 on EOF, error, or if buf_size was 0. |
615 | | int stream_read_partial(stream_t *s, void *buf, int buf_size) |
616 | 30.5k | { |
617 | 30.5k | mp_assert(s->buf_cur <= s->buf_end); |
618 | 30.5k | mp_assert(buf_size >= 0); |
619 | 30.5k | if (s->buf_cur == s->buf_end && buf_size > 0) { |
620 | 25.4k | if (buf_size > (s->buffer_mask + 1) / 2) { |
621 | | // Direct read if the buffer is too small anyway. |
622 | 24 | stream_drop_buffers(s); |
623 | 24 | return stream_read_unbuffered(s, buf, buf_size); |
624 | 24 | } |
625 | 25.4k | stream_read_more(s, 1); |
626 | 25.4k | } |
627 | 30.4k | int res = ring_copy(s, buf, buf_size, s->buf_cur); |
628 | 30.4k | s->buf_cur += res; |
629 | 30.4k | return res; |
630 | 30.5k | } |
631 | | |
632 | | // Slow version of stream_read_char(); called by it if the buffer is empty. |
633 | | int stream_read_char_fallback(stream_t *s) |
634 | 5.30k | { |
635 | 5.30k | uint8_t c; |
636 | 5.30k | return stream_read_partial(s, &c, 1) ? c : STREAM_EOF; |
637 | 5.30k | } |
638 | | |
639 | | int stream_read(stream_t *s, void *mem, int total) |
640 | 5.38k | { |
641 | 5.38k | int len = total; |
642 | 10.1k | while (len > 0) { |
643 | 10.1k | int read = stream_read_partial(s, mem, len); |
644 | 10.1k | if (read <= 0) |
645 | 5.38k | break; // EOF |
646 | 4.75k | mem = (char *)mem + read; |
647 | 4.75k | len -= read; |
648 | 4.75k | } |
649 | 5.38k | total -= len; |
650 | 5.38k | return total; |
651 | 5.38k | } |
652 | | |
653 | | // Read ahead so that at least forward_size bytes are readable ahead. Returns |
654 | | // the actual forward amount available (restricted by EOF or buffer limits). |
655 | | int stream_peek(stream_t *s, int forward_size) |
656 | 677k | { |
657 | 742k | while (stream_read_more(s, forward_size)) {} |
658 | 677k | return s->buf_end - s->buf_cur; |
659 | 677k | } |
660 | | |
661 | | // Like stream_read(), but do not advance the current position. This may resize |
662 | | // the buffer to satisfy the read request. |
663 | | int stream_read_peek(stream_t *s, void *buf, int buf_size) |
664 | 677k | { |
665 | 677k | stream_peek(s, buf_size); |
666 | 677k | return ring_copy(s, buf, buf_size, s->buf_cur); |
667 | 677k | } |
668 | | |
669 | | int stream_write_buffer(stream_t *s, void *buf, int len) |
670 | 0 | { |
671 | 0 | if (!s->write_buffer) |
672 | 0 | return -1; |
673 | 0 | int orig_len = len; |
674 | 0 | while (len) { |
675 | 0 | int w = s->write_buffer(s, buf, len); |
676 | 0 | if (w <= 0) |
677 | 0 | return -1; |
678 | 0 | s->pos += w; |
679 | 0 | buf = (char *)buf + w; |
680 | 0 | len -= w; |
681 | 0 | } |
682 | 0 | return orig_len; |
683 | 0 | } |
684 | | |
685 | | // Drop len bytes form input, possibly reading more until all is skipped. If |
686 | | // EOF or an error was encountered before all could be skipped, return false, |
687 | | // otherwise return true. |
688 | | static bool stream_skip_read(struct stream *s, int64_t len) |
689 | 0 | { |
690 | 0 | while (len > 0) { |
691 | 0 | unsigned int left = s->buf_end - s->buf_cur; |
692 | 0 | if (!left) { |
693 | 0 | if (!stream_read_more(s, 1)) |
694 | 0 | return false; |
695 | 0 | continue; |
696 | 0 | } |
697 | 0 | unsigned skip = MPMIN(len, left); |
698 | 0 | s->buf_cur += skip; |
699 | 0 | len -= skip; |
700 | 0 | } |
701 | 0 | return true; |
702 | 0 | } |
703 | | |
704 | | // Drop the internal buffer. Note that this will advance the stream position |
705 | | // (as seen by stream_tell()), because the real stream position is ahead of the |
706 | | // logical stream position by the amount of buffered but not yet read data. |
707 | | void stream_drop_buffers(stream_t *s) |
708 | 66 | { |
709 | 66 | s->pos = stream_tell(s); |
710 | 66 | s->buf_start = s->buf_cur = s->buf_end = 0; |
711 | 66 | s->eof = 0; |
712 | 66 | stream_resize_buffer(s, 0, 0); |
713 | 66 | } |
714 | | |
715 | | // Declare the current position the new logical start of the stream. Used for |
716 | | // streams whose content is replaced mid-stream (disc navigation jumps). |
717 | | // Discards buffered data and clears EOF. |
718 | | void stream_rebase_position(stream_t *s) |
719 | 0 | { |
720 | 0 | stream_drop_buffers(s); |
721 | 0 | s->pos = 0; |
722 | 0 | } |
723 | | |
724 | | // Seek function bypassing the local stream buffer. |
725 | | static bool stream_seek_unbuffered(stream_t *s, int64_t newpos) |
726 | 42 | { |
727 | 42 | if (newpos != s->pos) { |
728 | 42 | MP_VERBOSE(s, "stream level seek from %" PRId64 " to %" PRId64 "\n", |
729 | 42 | s->pos, newpos); |
730 | | |
731 | 42 | s->total_stream_seeks++; |
732 | | |
733 | 42 | if (newpos > s->pos && !s->seekable) { |
734 | 0 | MP_ERR(s, "Cannot seek forward in this stream\n"); |
735 | 0 | return false; |
736 | 0 | } |
737 | 42 | if (newpos < s->pos && !s->seekable) { |
738 | 0 | MP_ERR(s, "Cannot seek backward in linear streams!\n"); |
739 | 0 | return false; |
740 | 0 | } |
741 | 42 | if (s->seek(s, newpos) <= 0) { |
742 | 0 | int level = mp_cancel_test(s->cancel) ? MSGL_V : MSGL_ERR; |
743 | 0 | MP_MSG(s, level, "Seek failed (to %lld, size %lld)\n", |
744 | 0 | (long long)newpos, (long long)stream_get_size(s)); |
745 | 0 | return false; |
746 | 0 | } |
747 | 42 | stream_drop_buffers(s); |
748 | 42 | s->pos = newpos; |
749 | 42 | } |
750 | 42 | return true; |
751 | 42 | } |
752 | | |
753 | | bool stream_seek(stream_t *s, int64_t pos) |
754 | 679k | { |
755 | 679k | MP_TRACE(s, "seek request from %" PRId64 " to %" PRId64 "\n", |
756 | 679k | stream_tell(s), pos); |
757 | | |
758 | 679k | s->eof = 0; // eof should be set only on read; seeking always clears it |
759 | | |
760 | 679k | if (pos < 0) { |
761 | 0 | MP_ERR(s, "Invalid seek to negative position %lld!\n", (long long)pos); |
762 | 0 | pos = 0; |
763 | 0 | } |
764 | | |
765 | 679k | if (pos <= s->pos) { |
766 | 679k | int64_t x = pos - (s->pos - (int)s->buf_end); |
767 | 679k | if (x >= (int)s->buf_start) { |
768 | 679k | s->buf_cur = x; |
769 | 679k | mp_assert(s->buf_cur >= s->buf_start); |
770 | 679k | mp_assert(s->buf_cur <= s->buf_end); |
771 | 679k | return true; |
772 | 679k | } |
773 | 679k | } |
774 | | |
775 | 42 | if (s->mode == STREAM_WRITE) |
776 | 0 | return s->seekable && s->seek(s, pos); |
777 | | |
778 | | // Skip data instead of performing a seek in some cases. |
779 | 42 | if (pos >= s->pos && |
780 | 21 | ((!s->seekable && s->fast_skip) || |
781 | 21 | pos - s->pos <= s->requested_buffer_size)) |
782 | 0 | { |
783 | 0 | return stream_skip_read(s, pos - stream_tell(s)); |
784 | 0 | } |
785 | | |
786 | 42 | return stream_seek_unbuffered(s, pos); |
787 | 42 | } |
788 | | |
789 | | // Like stream_seek(), but strictly prefer skipping data instead of failing, if |
790 | | // it's a forward-seek. |
791 | | bool stream_seek_skip(stream_t *s, int64_t pos) |
792 | 249k | { |
793 | 249k | uint64_t cur_pos = stream_tell(s); |
794 | | |
795 | 249k | if (cur_pos == pos) |
796 | 136k | return true; |
797 | | |
798 | 113k | return !s->seekable && pos > cur_pos |
799 | 113k | ? stream_skip_read(s, pos - cur_pos) |
800 | 113k | : stream_seek(s, pos); |
801 | 249k | } |
802 | | |
803 | | int stream_control(stream_t *s, int cmd, void *arg) |
804 | 5.50M | { |
805 | 5.50M | return s->control ? s->control(s, cmd, arg) : STREAM_UNSUPPORTED; |
806 | 5.50M | } |
807 | | |
808 | | // Return the current size of the stream, or a negative value if unknown. |
809 | | int64_t stream_get_size(stream_t *s) |
810 | 32.5k | { |
811 | 32.5k | return s->get_size ? s->get_size(s) : -1; |
812 | 32.5k | } |
813 | | |
814 | | void free_stream(stream_t *s) |
815 | 454k | { |
816 | 454k | if (!s) |
817 | 351k | return; |
818 | | |
819 | 102k | if (s->close) |
820 | 4.43k | s->close(s); |
821 | 102k | talloc_free(s); |
822 | 102k | } |
823 | | |
824 | | static const char *const bom[3] = {"\xEF\xBB\xBF", "\xFF\xFE", "\xFE\xFF"}; |
825 | | |
826 | | // Return utf16 argument for stream_read_line |
827 | | int stream_skip_bom(struct stream *s) |
828 | 63.8k | { |
829 | 63.8k | char buf[4]; |
830 | 63.8k | int len = stream_read_peek(s, buf, sizeof(buf)); |
831 | 63.8k | bstr data = {buf, len}; |
832 | 254k | for (int n = 0; n < 3; n++) { |
833 | 191k | if (bstr_startswith0(data, bom[n])) { |
834 | 894 | stream_seek_skip(s, stream_tell(s) + strlen(bom[n])); |
835 | 894 | return n; |
836 | 894 | } |
837 | 191k | } |
838 | 62.9k | return -1; // default to 8 bit codepages |
839 | 63.8k | } |
840 | | |
841 | | // Read the rest of the stream into memory (current pos to EOF), and return it. |
842 | | // talloc_ctx: used as talloc parent for the returned allocation |
843 | | // max_size: must be set to >=0. If the file is larger than that, it is treated |
844 | | // as error. This is a minor robustness measure. If the stream is |
845 | | // created with STREAM_ALLOW_PARTIAL_READ flag, partial result up to |
846 | | // max_size is returned instead. |
847 | | // returns: stream contents, or .start/.len set to NULL on error |
848 | | // If the file was empty, but no error happened, .start will be non-NULL and |
849 | | // .len will be 0. |
850 | | // For convenience, the returned buffer is padded with a 0 byte. The padding |
851 | | // is not included in the returned length. |
852 | | struct bstr stream_read_complete(struct stream *s, void *talloc_ctx, |
853 | | int max_size) |
854 | 5.38k | { |
855 | 5.38k | if (max_size < 0 || max_size > STREAM_MAX_READ_SIZE) |
856 | 0 | abort(); |
857 | 5.38k | if (s->is_directory) |
858 | 0 | return (struct bstr){NULL, 0}; |
859 | | |
860 | 5.38k | int bufsize; |
861 | 5.38k | int total_read = 0; |
862 | 5.38k | int padding = 1; |
863 | 5.38k | char *buf = NULL; |
864 | 5.38k | int64_t size = stream_get_size(s) - stream_tell(s); |
865 | 5.38k | if (size > max_size && !s->allow_partial_read) |
866 | 0 | return (struct bstr){NULL, 0}; |
867 | 5.38k | if (size > 0) |
868 | 4.75k | bufsize = size + padding; |
869 | 631 | else |
870 | 631 | bufsize = 1000; |
871 | 5.38k | if (s->allow_partial_read) |
872 | 0 | bufsize = MPMIN(bufsize, max_size + padding); |
873 | 5.38k | while (1) { |
874 | 5.38k | buf = talloc_realloc_size(talloc_ctx, buf, bufsize); |
875 | 5.38k | int readsize = stream_read(s, buf + total_read, bufsize - total_read); |
876 | 5.38k | total_read += readsize; |
877 | 5.38k | if (total_read >= max_size && s->allow_partial_read) { |
878 | 0 | total_read = max_size; |
879 | 0 | break; |
880 | 0 | } |
881 | 5.38k | if (total_read < bufsize) |
882 | 5.38k | break; |
883 | 0 | if (bufsize > max_size) { |
884 | 0 | talloc_free(buf); |
885 | 0 | return (struct bstr){NULL, 0}; |
886 | 0 | } |
887 | 0 | bufsize = MPMIN(bufsize + (bufsize >> 1), max_size + padding); |
888 | 0 | } |
889 | 5.38k | buf = talloc_realloc_size(talloc_ctx, buf, total_read + padding); |
890 | 5.38k | memset(&buf[total_read], 0, padding); |
891 | 5.38k | return (struct bstr){buf, total_read}; |
892 | 5.38k | } |
893 | | |
894 | | struct bstr stream_read_file(const char *filename, void *talloc_ctx, |
895 | | struct mpv_global *global, int max_size) |
896 | 0 | { |
897 | 0 | return stream_read_file2(filename, talloc_ctx, STREAM_READ_FILE_FLAGS_DEFAULT, |
898 | 0 | global, max_size); |
899 | 0 | } |
900 | | |
901 | | struct bstr stream_read_file2(const char *filename, void *talloc_ctx, |
902 | | int flags, struct mpv_global *global, int max_size) |
903 | 8.03k | { |
904 | 8.03k | struct bstr res = {0}; |
905 | 8.03k | stream_t *s = stream_create(filename, flags, NULL, global); |
906 | 8.03k | if (s) { |
907 | 644 | if (s->is_directory) |
908 | 0 | mp_err(s->log, "Failed to open %s (not a file).\n", filename); |
909 | 644 | else |
910 | 644 | res = stream_read_complete(s, talloc_ctx, max_size); |
911 | 644 | } |
912 | 8.03k | free_stream(s); |
913 | 8.03k | return res; |
914 | 8.03k | } |
915 | | |
916 | | char **stream_get_proto_list(bool safe_only) |
917 | 3 | { |
918 | 3 | char **list = NULL; |
919 | 3 | int num = 0; |
920 | 48 | for (int i = 0; i < MP_ARRAY_SIZE(stream_list); i++) { |
921 | 45 | const stream_info_t *stream_info = stream_list[i]; |
922 | 45 | if (safe_only && (stream_info->stream_origin & STREAM_ORIGIN_UNSAFE)) |
923 | 0 | continue; |
924 | | |
925 | 45 | char **get_protocols = stream_info->get_protocols ? stream_info->get_protocols() : NULL; |
926 | 45 | char **protocols = get_protocols ? get_protocols : (char **)stream_info->protocols; |
927 | | |
928 | 135 | for (int j = 0; protocols && protocols[j]; j++) { |
929 | 90 | if (*protocols[j] == '\0') |
930 | 3 | continue; |
931 | | |
932 | 87 | MP_TARRAY_APPEND(NULL, list, num, talloc_strdup(list, protocols[j])); |
933 | 87 | } |
934 | | |
935 | 45 | talloc_free(get_protocols); |
936 | 45 | } |
937 | 3 | MP_TARRAY_APPEND(NULL, list, num, NULL); |
938 | 3 | return list; |
939 | 3 | } |
940 | | |
941 | | void stream_print_proto_list(struct mp_log *log) |
942 | 0 | { |
943 | 0 | int count = 0; |
944 | |
|
945 | 0 | mp_info(log, "Protocols:\n\n"); |
946 | 0 | char **list = stream_get_proto_list(false); |
947 | 0 | for (int i = 0; list[i]; i++) { |
948 | 0 | mp_info(log, " %s://\n", list[i]); |
949 | 0 | count++; |
950 | 0 | } |
951 | 0 | talloc_free(list); |
952 | 0 | mp_info(log, "\nTotal: %d protocols\n", count); |
953 | 0 | } |
954 | | |
955 | | bool stream_has_proto(const char *proto) |
956 | 0 | { |
957 | 0 | for (int i = 0; i < MP_ARRAY_SIZE(stream_list); i++) { |
958 | 0 | const stream_info_t *stream_info = stream_list[i]; |
959 | |
|
960 | 0 | bool match = false; |
961 | 0 | char **get_protocols = stream_info->get_protocols ? stream_info->get_protocols() : NULL; |
962 | 0 | char **protocols = get_protocols ? get_protocols : (char **)stream_info->protocols; |
963 | |
|
964 | 0 | for (int j = 0; protocols && protocols[j]; j++) { |
965 | 0 | if (strcmp(protocols[j], proto) == 0) { |
966 | 0 | match = true; |
967 | 0 | break; |
968 | 0 | } |
969 | 0 | } |
970 | |
|
971 | 0 | talloc_free(get_protocols); |
972 | 0 | if (match) |
973 | 0 | return match; |
974 | 0 | } |
975 | | |
976 | 0 | return false; |
977 | 0 | } |