Browse code

Add MD5 protocol

This is a write-only protocol which computes the md5sum of data written,
and on close writes this to the designated output or stdout if none
is specified. It can be used to test muxers without writing an actual
file.

Originally committed as revision 24309 to svn://svn.ffmpeg.org/ffmpeg/trunk

Måns Rullgård authored on 2010/07/19 05:19:08
Showing 3 changed files
... ...
@@ -284,6 +284,7 @@ OBJS-$(CONFIG_FILE_PROTOCOL)             += file.o
284 284
 OBJS-$(CONFIG_GOPHER_PROTOCOL)           += gopher.o
285 285
 OBJS-$(CONFIG_HTTP_PROTOCOL)             += http.o httpauth.o
286 286
 OBJS-$(CONFIG_MMST_PROTOCOL)             += mmst.o asf.o
287
+OBJS-$(CONFIG_MD5_PROTOCOL)              += md5proto.o
287 288
 OBJS-$(CONFIG_PIPE_PROTOCOL)             += file.o
288 289
 
289 290
 # external or internal rtmp
... ...
@@ -223,6 +223,7 @@ void av_register_all(void)
223 223
     REGISTER_PROTOCOL (GOPHER, gopher);
224 224
     REGISTER_PROTOCOL (HTTP, http);
225 225
     REGISTER_PROTOCOL (MMST, mmst);
226
+    REGISTER_PROTOCOL (MD5,  md5);
226 227
     REGISTER_PROTOCOL (PIPE, pipe);
227 228
     REGISTER_PROTOCOL (RTMP, rtmp);
228 229
 #if CONFIG_LIBRTMP
229 230
new file mode 100644
... ...
@@ -0,0 +1,92 @@
0
+/*
1
+ * Copyright (c) 2010 Mans Rullgard
2
+ *
3
+ * This file is part of FFmpeg.
4
+ *
5
+ * FFmpeg is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * FFmpeg is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with FFmpeg; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+#include <stdio.h>
21
+#include "libavutil/avstring.h"
22
+#include "libavutil/md5.h"
23
+#include "libavutil/mem.h"
24
+#include "libavutil/error.h"
25
+#include "avformat.h"
26
+#include "avio.h"
27
+
28
+#define PRIV_SIZE 128
29
+
30
+static int md5_open(URLContext *h, const char *filename, int flags)
31
+{
32
+    if (PRIV_SIZE < av_md5_size) {
33
+        av_log(NULL, AV_LOG_ERROR, "Insuffient size for MD5 context\n");
34
+        return -1;
35
+    }
36
+
37
+    if (flags != URL_WRONLY)
38
+        return AVERROR(EINVAL);
39
+
40
+    av_md5_init(h->priv_data);
41
+
42
+    return 0;
43
+}
44
+
45
+static int md5_write(URLContext *h, const unsigned char *buf, int size)
46
+{
47
+    av_md5_update(h->priv_data, buf, size);
48
+    return size;
49
+}
50
+
51
+static int md5_close(URLContext *h)
52
+{
53
+    const char *filename = h->filename;
54
+    uint8_t md5[16], buf[64];
55
+    URLContext *out;
56
+    int i, err = 0;
57
+
58
+    av_md5_final(h->priv_data, md5);
59
+    for (i = 0; i < sizeof(md5); i++)
60
+        snprintf(buf + i*2, 3, "%02x", md5[i]);
61
+    buf[i*2] = '\n';
62
+
63
+    av_strstart(filename, "md5:", &filename);
64
+
65
+    if (*filename) {
66
+        err = url_open(&out, filename, URL_WRONLY);
67
+        if (err)
68
+            return err;
69
+        err = url_write(out, buf, i*2+1);
70
+        url_close(out);
71
+    } else {
72
+        if (fwrite(buf, 1, i*2+1, stdout) < i*2+1)
73
+            err = AVERROR(errno);
74
+    }
75
+
76
+    return err;
77
+}
78
+
79
+static int md5_get_handle(URLContext *h)
80
+{
81
+    return (intptr_t)h->priv_data;
82
+}
83
+
84
+URLProtocol md5_protocol = {
85
+    .name                = "md5",
86
+    .url_open            = md5_open,
87
+    .url_write           = md5_write,
88
+    .url_close           = md5_close,
89
+    .url_get_file_handle = md5_get_handle,
90
+    .priv_data_size      = PRIV_SIZE,
91
+};