Statistics
| Branch: | Revision:

ffmpeg / libavformat / md5proto.c @ f87b1b37

History | View | Annotate | Download (2.41 KB)

1
/*
2
 * Copyright (c) 2010 Mans Rullgard
3
 *
4
 * This file is part of Libav.
5
 *
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.
10
 *
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.
15
 *
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
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_WRONLY)
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_WRONLY);
69
        if (err)
70
            return err;
71
        err = ffurl_write(out, buf, i*2+1);
72
        ffurl_close(out);
73
    } else {
74
        if (fwrite(buf, 1, i*2+1, stdout) < i*2+1)
75
            err = AVERROR(errno);
76
    }
77

    
78
    return err;
79
}
80

    
81
static int md5_get_handle(URLContext *h)
82
{
83
    return (intptr_t)h->priv_data;
84
}
85

    
86
URLProtocol ff_md5_protocol = {
87
    .name                = "md5",
88
    .url_open            = md5_open,
89
    .url_write           = md5_write,
90
    .url_close           = md5_close,
91
    .url_get_file_handle = md5_get_handle,
92
    .priv_data_size      = PRIV_SIZE,
93
};