FFmpeg  2.1.1
hash.c
Go to the documentation of this file.
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 "ripemd.h"
28 #include "sha.h"
29 #include "sha512.h"
30 
31 #include "avstring.h"
32 #include "error.h"
33 #include "intreadwrite.h"
34 #include "mem.h"
35 
36 enum hashtype {
37  MD5,
53 };
54 
55 typedef struct AVHashContext {
56  void *ctx;
57  enum hashtype type;
58  const AVCRC *crctab;
59  uint32_t crc;
61 
62 struct {
63  const char *name;
64  int size;
65 } hashdesc[] = {
66  [MD5] = {"MD5", 16},
67  [MURMUR3] = {"murmur3", 16},
68  [RIPEMD128] = {"RIPEMD128", 16},
69  [RIPEMD160] = {"RIPEMD160", 20},
70  [RIPEMD256] = {"RIPEMD256", 32},
71  [RIPEMD320] = {"RIPEMD320", 40},
72  [SHA160] = {"SHA160", 20},
73  [SHA224] = {"SHA224", 28},
74  [SHA256] = {"SHA256", 32},
75  [SHA512_224] = {"SHA512/224", 28},
76  [SHA512_256] = {"SHA512/256", 32},
77  [SHA384] = {"SHA384", 48},
78  [SHA512] = {"SHA512", 64},
79  [CRC32] = {"CRC32", 4},
80  [ADLER32] = {"adler32", 4},
81 };
82 
83 const char *av_hash_names(int i)
84 {
85  if (i < 0 || i >= NUM_HASHES) return NULL;
86  return hashdesc[i].name;
87 }
88 
89 const char *av_hash_get_name(const AVHashContext *ctx)
90 {
91  return hashdesc[ctx->type].name;
92 }
93 
95 {
96  return hashdesc[ctx->type].size;
97 }
98 
99 int av_hash_alloc(AVHashContext **ctx, const char *name)
100 {
102  int i;
103  *ctx = NULL;
104  for (i = 0; i < NUM_HASHES; i++)
105  if (av_strcasecmp(name, hashdesc[i].name) == 0)
106  break;
107  if (i >= NUM_HASHES) return AVERROR(EINVAL);
108  res = av_mallocz(sizeof(*res));
109  if (!res) return AVERROR(ENOMEM);
110  res->type = i;
111  switch (i) {
112  case MD5: res->ctx = av_md5_alloc(); break;
113  case MURMUR3: res->ctx = av_murmur3_alloc(); break;
114  case RIPEMD128:
115  case RIPEMD160:
116  case RIPEMD256:
117  case RIPEMD320: res->ctx = av_ripemd_alloc(); break;
118  case SHA160:
119  case SHA224:
120  case SHA256: res->ctx = av_sha_alloc(); break;
121  case SHA512_224:
122  case SHA512_256:
123  case SHA384:
124  case SHA512: res->ctx = av_sha512_alloc(); break;
125  case CRC32: res->crctab = av_crc_get_table(AV_CRC_32_IEEE_LE); break;
126  case ADLER32: break;
127  }
128  if (i != ADLER32 && i != CRC32 && !res->ctx) {
129  av_free(res);
130  return AVERROR(ENOMEM);
131  }
132  *ctx = res;
133  return 0;
134 }
135 
137 {
138  switch (ctx->type) {
139  case MD5: av_md5_init(ctx->ctx); break;
140  case MURMUR3: av_murmur3_init(ctx->ctx); break;
141  case RIPEMD128: av_ripemd_init(ctx->ctx, 128); break;
142  case RIPEMD160: av_ripemd_init(ctx->ctx, 160); break;
143  case RIPEMD256: av_ripemd_init(ctx->ctx, 256); break;
144  case RIPEMD320: av_ripemd_init(ctx->ctx, 320); break;
145  case SHA160: av_sha_init(ctx->ctx, 160); break;
146  case SHA224: av_sha_init(ctx->ctx, 224); break;
147  case SHA256: av_sha_init(ctx->ctx, 256); break;
148  case SHA512_224: av_sha512_init(ctx->ctx, 224); break;
149  case SHA512_256: av_sha512_init(ctx->ctx, 256); break;
150  case SHA384: av_sha512_init(ctx->ctx, 384); break;
151  case SHA512: av_sha512_init(ctx->ctx, 512); break;
152  case CRC32: ctx->crc = UINT32_MAX; break;
153  case ADLER32: ctx->crc = 1; break;
154  }
155 }
156 
157 void av_hash_update(AVHashContext *ctx, const uint8_t *src, int len)
158 {
159  switch (ctx->type) {
160  case MD5: av_md5_update(ctx->ctx, src, len); break;
161  case MURMUR3: av_murmur3_update(ctx->ctx, src, len); break;
162  case RIPEMD128:
163  case RIPEMD160:
164  case RIPEMD256:
165  case RIPEMD320: av_ripemd_update(ctx->ctx, src, len); break;
166  case SHA160:
167  case SHA224:
168  case SHA256: av_sha_update(ctx->ctx, src, len); break;
169  case SHA512_224:
170  case SHA512_256:
171  case SHA384:
172  case SHA512: av_sha512_update(ctx->ctx, src, len); break;
173  case CRC32: ctx->crc = av_crc(ctx->crctab, ctx->crc, src, len); break;
174  case ADLER32: ctx->crc = av_adler32_update(ctx->crc, src, len); break;
175  }
176 }
177 
179 {
180  switch (ctx->type) {
181  case MD5: av_md5_final(ctx->ctx, dst); break;
182  case MURMUR3: av_murmur3_final(ctx->ctx, dst); break;
183  case RIPEMD128:
184  case RIPEMD160:
185  case RIPEMD256:
186  case RIPEMD320: av_ripemd_final(ctx->ctx, dst); break;
187  case SHA160:
188  case SHA224:
189  case SHA256: av_sha_final(ctx->ctx, dst); break;
190  case SHA512_224:
191  case SHA512_256:
192  case SHA384:
193  case SHA512: av_sha512_final(ctx->ctx, dst); break;
194  case CRC32: AV_WB32(dst, ctx->crc ^ UINT32_MAX); break;
195  case ADLER32: AV_WB32(dst, ctx->crc); break;
196  }
197 }
198 
200 {
201  if (*ctx)
202  av_freep(&(*ctx)->ctx);
203  av_freep(ctx);
204 }
const char * name
Definition: avisynth_c.h:675
void av_sha_final(struct AVSHA *context, uint8_t *digest)
Finish hashing and output digest value.
Definition: sha.c:341
struct AVSHA512 * av_sha512_alloc(void)
Allocate an AVSHA512 context.
Definition: sha512.c:43
int size
void av_sha_update(struct AVSHA *context, const uint8_t *data, unsigned int len)
Update hash value.
Definition: sha.c:314
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition: crc.c:306
struct AVRIPEMD * av_ripemd_alloc(void)
Allocate an AVRIPEMD context.
Definition: ripemd.c:44
void av_ripemd_update(struct AVRIPEMD *context, const uint8_t *data, unsigned int len)
Update hash value.
Definition: ripemd.c:423
void av_murmur3_update(struct AVMurMur3 *c, const uint8_t *src, int len)
Definition: murmur3.c:92
int av_sha_init(struct AVSHA *context, int bits)
Initialize SHA-1 or SHA-2 hashing.
Definition: sha.c:273
void av_murmur3_init(struct AVMurMur3 *c)
Definition: murmur3.c:43
Definition: hash.c:48
Definition: hash.c:40
struct @167 hashdesc[]
void av_md5_update(struct AVMD5 *ctx, const uint8_t *src, int len)
Update hash value.
Definition: md5.c:148
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:234
struct AVMurMur3 * av_murmur3_alloc(void)
Definition: murmur3.c:32
static uint8_t * res
Definition: ffhash.c:43
void av_ripemd_final(struct AVRIPEMD *context, uint8_t *digest)
Finish hashing and output digest value.
Definition: ripemd.c:450
uint8_t
Definition: hash.c:44
Definition: hash.c:38
void av_hash_init(AVHashContext *ctx)
Initialize or reset a hash context.
Definition: hash.c:136
unsigned long av_adler32_update(unsigned long adler, const uint8_t *buf, unsigned int len) av_pure
Calculate the Adler32 checksum of a buffer.
Definition: adler32.c:35
Definition: hash.c:37
uint32_t crc
Definition: hash.c:59
#define AV_WB32(p, darg)
Definition: intreadwrite.h:265
void av_murmur3_final(struct AVMurMur3 *c, uint8_t dst[16])
Definition: murmur3.c:141
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:219
struct AVMD5 * av_md5_alloc(void)
Allocate an AVMD5 context.
Definition: md5.c:47
Definition: hash.c:41
int av_strcasecmp(const char *a, const char *b)
Locale-independent case-insensitive compare.
Definition: avstring.c:212
int av_hash_alloc(AVHashContext **ctx, const char *name)
Allocate a hash context for the algorithm specified by name.
Definition: hash.c:99
Definition: hash.c:39
const char * av_hash_get_name(const AVHashContext *ctx)
Definition: hash.c:89
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length) av_pure
Calculate the CRC of a block.
Definition: crc.c:320
void av_hash_freep(AVHashContext **ctx)
Free hash context.
Definition: hash.c:199
Definition: hash.c:50
void av_hash_final(AVHashContext *ctx, uint8_t *dst)
Finalize a hash context and compute the actual hash value.
Definition: hash.c:178
Definition: hash.c:43
AVS_Value src
Definition: avisynth_c.h:523
const char * av_hash_names(int i)
Get the names of available hash algorithms.
Definition: hash.c:83
int av_hash_get_size(const AVHashContext *ctx)
Definition: hash.c:94
memory handling functions
void av_sha512_update(struct AVSHA512 *context, const uint8_t *data, unsigned int len)
Update hash value.
Definition: sha512.c:242
void av_md5_init(struct AVMD5 *ctx)
Initialize MD5 hashing.
Definition: md5.c:138
int av_sha512_init(struct AVSHA512 *context, int bits)
Initialize SHA-2 512 hashing.
Definition: sha512.c:191
void av_hash_update(AVHashContext *ctx, const uint8_t *src, int len)
Update a hash context with additional data.
Definition: hash.c:157
error code definitions
void av_md5_final(struct AVMD5 *ctx, uint8_t *dst)
Finish hashing and output digest value.
Definition: md5.c:183
enum hashtype type
Definition: hash.c:57
Definition: hash.c:42
Definition: hash.c:49
Definition: hash.c:45
int av_ripemd_init(struct AVRIPEMD *context, int bits)
Initialize RIPEMD hashing.
Definition: ripemd.c:369
const AVCRC * crctab
Definition: hash.c:58
void * ctx
Definition: hash.c:56
hashtype
Definition: hash.c:36
int len
void av_sha512_final(struct AVSHA512 *context, uint8_t *digest)
Finish hashing and output digest value.
Definition: sha512.c:269
#define AVERROR(e)
uint32_t AVCRC
Definition: crc.h:34
void * av_mallocz(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:241
struct AVSHA * av_sha_alloc(void)
Allocate an AVSHA context.
Definition: sha.c:45
Definition: hash.c:51