2 * Copyright (c) 2010 Mans Rullgard
4 * This file is part of Libav.
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.
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.
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
22 #include "libavutil/avstring.h"
23 #include "libavutil/md5.h"
24 #include "libavutil/mem.h"
25 #include "libavutil/error.h"
32 static int md5_open(URLContext *h, const char *filename, int flags)
34 if (PRIV_SIZE < av_md5_size) {
35 av_log(NULL, AV_LOG_ERROR, "Insuffient size for MD5 context\n");
39 if (!(flags & AVIO_FLAG_WRITE))
40 return AVERROR(EINVAL);
42 av_md5_init(h->priv_data);
47 static int md5_write(URLContext *h, const unsigned char *buf, int size)
49 av_md5_update(h->priv_data, buf, size);
53 static int md5_close(URLContext *h)
55 const char *filename = h->filename;
56 uint8_t md5[16], buf[64];
60 av_md5_final(h->priv_data, md5);
61 for (i = 0; i < sizeof(md5); i++)
62 snprintf(buf + i*2, 3, "%02x", md5[i]);
65 av_strstart(filename, "md5:", &filename);
68 err = ffurl_open(&out, filename, AVIO_FLAG_WRITE,
69 &h->interrupt_callback, NULL);
72 err = ffurl_write(out, buf, i*2+1);
75 if (fwrite(buf, 1, i*2+1, stdout) < i*2+1)
83 URLProtocol ff_md5_protocol = {
86 .url_write = md5_write,
87 .url_close = md5_close,
88 .priv_data_size = PRIV_SIZE,