yading@11: /* yading@11: * Copyright (c) 2012 Nicolas George yading@11: * yading@11: * This file is part of FFmpeg. yading@11: * yading@11: * FFmpeg is free software; you can redistribute it and/or yading@11: * modify it under the terms of the GNU Lesser General Public License yading@11: * as published by the Free Software Foundation; either yading@11: * version 2.1 of the License, or (at your option) any later version. yading@11: * yading@11: * FFmpeg is distributed in the hope that it will be useful, yading@11: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the yading@11: * GNU Lesser General Public License for more details. yading@11: * yading@11: * You should have received a copy of the GNU Lesser General Public License yading@11: * along with FFmpeg; if not, write to the Free Software Foundation, Inc., yading@11: * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@11: */ yading@11: yading@11: #include yading@11: #include "libavutil/avstring.h" yading@11: #include "libavutil/base64.h" yading@11: #include "url.h" yading@11: yading@11: typedef struct { yading@11: const uint8_t *data; yading@11: void *tofree; yading@11: size_t size; yading@11: size_t pos; yading@11: } DataContext; yading@11: yading@11: static av_cold int data_open(URLContext *h, const char *uri, int flags) yading@11: { yading@11: DataContext *dc = h->priv_data; yading@11: const char *data, *opt, *next; yading@11: char *ddata; yading@11: int ret, base64 = 0; yading@11: size_t in_size; yading@11: yading@11: /* data:content/type[;base64],payload */ yading@11: yading@11: av_strstart(uri, "data:", &uri); yading@11: data = strchr(uri, ','); yading@11: if (!data) { yading@11: av_log(h, AV_LOG_ERROR, "No ',' delimiter in URI\n"); yading@11: return AVERROR(EINVAL); yading@11: } yading@11: opt = uri; yading@11: while (opt < data) { yading@11: next = av_x_if_null(memchr(opt, ';', data - opt), data); yading@11: if (opt == uri) { yading@11: if (!memchr(opt, '/', next - opt)) { /* basic validity check */ yading@11: av_log(h, AV_LOG_ERROR, "Invalid content-type '%.*s'\n", yading@11: (int)(next - opt), opt); yading@11: return AVERROR(EINVAL); yading@11: } yading@11: av_log(h, AV_LOG_VERBOSE, "Content-type: %.*s\n", yading@11: (int)(next - opt), opt); yading@11: } else { yading@11: if (!av_strncasecmp(opt, "base64", next - opt)) { yading@11: base64 = 1; yading@11: } else { yading@11: av_log(h, AV_LOG_VERBOSE, "Ignoring option '%.*s'\n", yading@11: (int)(next - opt), opt); yading@11: } yading@11: } yading@11: opt = next + 1; yading@11: } yading@11: yading@11: data++; yading@11: in_size = strlen(data); yading@11: if (base64) { yading@11: size_t out_size = 3 * (in_size / 4) + 1; yading@11: yading@11: if (out_size > INT_MAX || !(ddata = av_malloc(out_size))) yading@11: return AVERROR(ENOMEM); yading@11: if ((ret = av_base64_decode(ddata, data, out_size)) < 0) { yading@11: av_free(ddata); yading@11: av_log(h, AV_LOG_ERROR, "Invalid base64 in URI\n"); yading@11: return ret; yading@11: } yading@11: dc->data = dc->tofree = ddata; yading@11: dc->size = ret; yading@11: } else { yading@11: dc->data = data; yading@11: dc->size = in_size; yading@11: } yading@11: return 0; yading@11: } yading@11: yading@11: static av_cold int data_close(URLContext *h) yading@11: { yading@11: DataContext *dc = h->priv_data; yading@11: yading@11: av_freep(&dc->tofree); yading@11: return 0; yading@11: } yading@11: yading@11: static int data_read(URLContext *h, unsigned char *buf, int size) yading@11: { yading@11: DataContext *dc = h->priv_data; yading@11: yading@11: if (dc->pos >= dc->size) yading@11: return AVERROR_EOF; yading@11: size = FFMIN(size, dc->size - dc->pos); yading@11: memcpy(buf, dc->data + dc->pos, size); yading@11: dc->pos += size; yading@11: return size; yading@11: } yading@11: yading@11: URLProtocol ff_data_protocol = { yading@11: .name = "data", yading@11: .url_open = data_open, yading@11: .url_close = data_close, yading@11: .url_read = data_read, yading@11: .priv_data_size = sizeof(DataContext), yading@11: };