md5proto.c 2.47 KB
Newer Older
Måns Rullgård's avatar
Måns Rullgård committed
1 2 3
/*
 * Copyright (c) 2010 Mans Rullgard
 *
4
 * This file is part of Libav.
Måns Rullgård's avatar
Måns Rullgård committed
5
 *
6
 * Libav is free software; you can redistribute it and/or
Måns Rullgård's avatar
Måns Rullgård committed
7 8 9 10
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * 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,
Måns Rullgård's avatar
Måns Rullgård committed
12 13 14 15 16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with Libav; if not, write to the Free Software
Måns Rullgård's avatar
Måns Rullgård committed
18 19 20 21 22 23 24 25 26 27
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <stdio.h>
#include "libavutil/avstring.h"
#include "libavutil/md5.h"
#include "libavutil/mem.h"
#include "libavutil/error.h"
#include "avformat.h"
#include "avio.h"
28
#include "url.h"
Måns Rullgård's avatar
Måns Rullgård committed
29

30 31 32
struct MD5Context {
    struct AVMD5 *md5;
};
Måns Rullgård's avatar
Måns Rullgård committed
33 34 35

static int md5_open(URLContext *h, const char *filename, int flags)
{
36
    struct MD5Context *c = h->priv_data;
Måns Rullgård's avatar
Måns Rullgård committed
37

38
    if (!(flags & AVIO_FLAG_WRITE))
Måns Rullgård's avatar
Måns Rullgård committed
39 40
        return AVERROR(EINVAL);

41 42 43 44
    c->md5 = av_md5_alloc();
    if (!c->md5)
        return AVERROR(ENOMEM);
    av_md5_init(c->md5);
Måns Rullgård's avatar
Måns Rullgård committed
45 46 47 48 49 50

    return 0;
}

static int md5_write(URLContext *h, const unsigned char *buf, int size)
{
51 52
    struct MD5Context *c = h->priv_data;
    av_md5_update(c->md5, buf, size);
Måns Rullgård's avatar
Måns Rullgård committed
53 54 55 56 57
    return size;
}

static int md5_close(URLContext *h)
{
58
    struct MD5Context *c = h->priv_data;
Måns Rullgård's avatar
Måns Rullgård committed
59 60 61 62 63
    const char *filename = h->filename;
    uint8_t md5[16], buf[64];
    URLContext *out;
    int i, err = 0;

64
    av_md5_final(c->md5, md5);
Måns Rullgård's avatar
Måns Rullgård committed
65 66 67 68 69 70 71
    for (i = 0; i < sizeof(md5); i++)
        snprintf(buf + i*2, 3, "%02x", md5[i]);
    buf[i*2] = '\n';

    av_strstart(filename, "md5:", &filename);

    if (*filename) {
72 73
        err = ffurl_open(&out, filename, AVIO_FLAG_WRITE,
                         &h->interrupt_callback, NULL);
Måns Rullgård's avatar
Måns Rullgård committed
74 75
        if (err)
            return err;
76
        err = ffurl_write(out, buf, i*2+1);
77
        ffurl_close(out);
Måns Rullgård's avatar
Måns Rullgård committed
78 79 80 81 82
    } else {
        if (fwrite(buf, 1, i*2+1, stdout) < i*2+1)
            err = AVERROR(errno);
    }

83 84
    av_freep(&c->md5);

Måns Rullgård's avatar
Måns Rullgård committed
85 86 87 88
    return err;
}


89
const URLProtocol ff_md5_protocol = {
Måns Rullgård's avatar
Måns Rullgård committed
90 91 92 93
    .name                = "md5",
    .url_open            = md5_open,
    .url_write           = md5_write,
    .url_close           = md5_close,
94
    .priv_data_size      = sizeof(struct MD5Context),
Måns Rullgård's avatar
Måns Rullgård committed
95
};