]> git.sesse.net Git - ffmpeg/blob - libavformat/md5proto.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavformat / md5proto.c
1 /*
2  * Copyright (c) 2010 Mans Rullgard
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #include <stdio.h>
22 #include "libavutil/avstring.h"
23 #include "libavutil/md5.h"
24 #include "libavutil/mem.h"
25 #include "libavutil/error.h"
26 #include "avformat.h"
27 #include "avio.h"
28 #include "url.h"
29
30 #define PRIV_SIZE 128
31
32 static int md5_open(URLContext *h, const char *filename, int flags)
33 {
34     if (PRIV_SIZE < av_md5_size) {
35         av_log(NULL, AV_LOG_ERROR, "Insuffient size for MD5 context\n");
36         return -1;
37     }
38
39     if (!(flags & AVIO_FLAG_WRITE))
40         return AVERROR(EINVAL);
41
42     av_md5_init(h->priv_data);
43
44     return 0;
45 }
46
47 static int md5_write(URLContext *h, const unsigned char *buf, int size)
48 {
49     av_md5_update(h->priv_data, buf, size);
50     return size;
51 }
52
53 static int md5_close(URLContext *h)
54 {
55     const char *filename = h->filename;
56     uint8_t md5[16], buf[64];
57     URLContext *out;
58     int i, err = 0;
59
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]);
63     buf[i*2] = '\n';
64
65     av_strstart(filename, "md5:", &filename);
66
67     if (*filename) {
68         err = ffurl_open(&out, filename, AVIO_FLAG_WRITE,
69                          &h->interrupt_callback, NULL);
70         if (err)
71             return err;
72         err = ffurl_write(out, buf, i*2+1);
73         ffurl_close(out);
74     } else {
75         if (fwrite(buf, 1, i*2+1, stdout) < i*2+1)
76             err = AVERROR(errno);
77     }
78
79     return err;
80 }
81
82
83 URLProtocol ff_md5_protocol = {
84     .name                = "md5",
85     .url_open            = md5_open,
86     .url_write           = md5_write,
87     .url_close           = md5_close,
88     .priv_data_size      = PRIV_SIZE,
89 };