]> git.sesse.net Git - ffmpeg/blob - libavutil/hash.c
Merge commit '2d66a58ccde05e764594bd7e5f0f9244634d0b2c'
[ffmpeg] / libavutil / hash.c
1 /*
2  * Copyright (C) 2013 Reimar Döffinger <Reimar.Doeffinger@gmx.de>
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 #include <stdint.h>
21 #include "hash.h"
22
23 #include "adler32.h"
24 #include "crc.h"
25 #include "md5.h"
26 #include "murmur3.h"
27 #include "sha.h"
28
29 #include "avstring.h"
30 #include "error.h"
31 #include "intreadwrite.h"
32 #include "mem.h"
33
34 enum hashtype {
35     MD5,
36     MURMUR3,
37     SHA160,
38     SHA224,
39     SHA256,
40     CRC32,
41     ADLER32,
42     NUM_HASHES
43 };
44
45 typedef struct AVHashContext {
46     void *ctx;
47     enum hashtype type;
48     const AVCRC *crctab;
49     uint32_t crc;
50 } AVHashContext;
51
52 struct {
53     const char *name;
54     int size;
55 } hashdesc[] = {
56     [MD5]     = {"MD5",     16},
57     [MURMUR3] = {"murmur3", 16},
58     [SHA160]  = {"SHA160",  20},
59     [SHA224]  = {"SHA224",  28},
60     [SHA256]  = {"SHA256",  32},
61     [CRC32]   = {"CRC32",    4},
62     [ADLER32] = {"adler32",  4},
63 };
64
65 const char *av_hash_names(int i)
66 {
67     if (i < 0 || i >= NUM_HASHES) return NULL;
68     return hashdesc[i].name;
69 }
70
71 const char *av_hash_get_name(const AVHashContext *ctx)
72 {
73     return hashdesc[ctx->type].name;
74 }
75
76 int av_hash_get_size(const AVHashContext *ctx)
77 {
78     return hashdesc[ctx->type].size;
79 }
80
81 int av_hash_alloc(AVHashContext **ctx, const char *name)
82 {
83     AVHashContext *res;
84     int i;
85     *ctx = NULL;
86     for (i = 0; i < NUM_HASHES; i++)
87         if (av_strcasecmp(name, hashdesc[i].name) == 0)
88             break;
89     if (i >= NUM_HASHES) return AVERROR(EINVAL);
90     res = av_mallocz(sizeof(*res));
91     if (!res) return AVERROR(ENOMEM);
92     res->type = i;
93     switch (i) {
94     case MD5:     res->ctx = av_md5_alloc(); break;
95     case MURMUR3: res->ctx = av_murmur3_alloc(); break;
96     case SHA160:
97     case SHA224:
98     case SHA256:  res->ctx = av_sha_alloc(); break;
99     case CRC32:   res->crctab = av_crc_get_table(AV_CRC_32_IEEE_LE); break;
100     case ADLER32: break;
101     }
102     if (i != ADLER32 && i != CRC32 && !res->ctx) {
103         av_free(res);
104         return AVERROR(ENOMEM);
105     }
106     *ctx = res;
107     return 0;
108 }
109
110 void av_hash_init(AVHashContext *ctx)
111 {
112     switch (ctx->type) {
113     case MD5:     av_md5_init(ctx->ctx); break;
114     case MURMUR3: av_murmur3_init(ctx->ctx); break;
115     case SHA160:  av_sha_init(ctx->ctx, 160); break;
116     case SHA224:  av_sha_init(ctx->ctx, 224); break;
117     case SHA256:  av_sha_init(ctx->ctx, 256); break;
118     case CRC32:   ctx->crc = UINT32_MAX; break;
119     case ADLER32: ctx->crc = 1; break;
120     }
121 }
122
123 void av_hash_update(AVHashContext *ctx, const uint8_t *src, int len)
124 {
125     switch (ctx->type) {
126     case MD5:     av_md5_update(ctx->ctx, src, len); break;
127     case MURMUR3: av_murmur3_update(ctx->ctx, src, len); break;
128     case SHA160:
129     case SHA224:
130     case SHA256:  av_sha_update(ctx->ctx, src, len); break;
131     case CRC32:   ctx->crc = av_crc(ctx->crctab, ctx->crc, src, len); break;
132     case ADLER32: ctx->crc = av_adler32_update(ctx->crc, src, len); break;
133     }
134 }
135
136 void av_hash_final(AVHashContext *ctx, uint8_t *dst)
137 {
138     switch (ctx->type) {
139     case MD5:     av_md5_final(ctx->ctx, dst); break;
140     case MURMUR3: av_murmur3_final(ctx->ctx, dst); break;
141     case SHA160:
142     case SHA224:
143     case SHA256:  av_sha_final(ctx->ctx, dst); break;
144     case CRC32:   AV_WB32(dst, ctx->crc ^ UINT32_MAX); break;
145     case ADLER32: AV_WB32(dst, ctx->crc); break;
146     }
147 }
148
149 void av_hash_freep(AVHashContext **ctx)
150 {
151     if (*ctx)
152         av_freep(&(*ctx)->ctx);
153     av_freep(ctx);
154 }