FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavutil/hash.c
Date: 2024-02-16 17:37:06
Exec Total Coverage
Lines: 100 103 97.1%
Functions: 11 11 100.0%
Branches: 55 66 83.3%

Line Branch Exec Source
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
21 #include <stddef.h>
22 #include <stdint.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include "hash.h"
26
27 #include "adler32.h"
28 #include "crc.h"
29 #include "md5.h"
30 #include "murmur3.h"
31 #include "ripemd.h"
32 #include "sha.h"
33 #include "sha512.h"
34
35 #include "avstring.h"
36 #include "base64.h"
37 #include "error.h"
38 #include "intreadwrite.h"
39 #include "mem.h"
40
41 enum hashtype {
42 MD5,
43 MURMUR3,
44 RIPEMD128,
45 RIPEMD160,
46 RIPEMD256,
47 RIPEMD320,
48 SHA160,
49 SHA224,
50 SHA256,
51 SHA512_224,
52 SHA512_256,
53 SHA384,
54 SHA512,
55 CRC32,
56 ADLER32,
57 NUM_HASHES
58 };
59
60 typedef struct AVHashContext {
61 void *ctx;
62 enum hashtype type;
63 const AVCRC *crctab;
64 uint32_t crc;
65 } AVHashContext;
66
67 static const struct {
68 const char *name;
69 int size;
70 } hashdesc[] = {
71 [MD5] = {"MD5", 16},
72 [MURMUR3] = {"murmur3", 16},
73 [RIPEMD128] = {"RIPEMD128", 16},
74 [RIPEMD160] = {"RIPEMD160", 20},
75 [RIPEMD256] = {"RIPEMD256", 32},
76 [RIPEMD320] = {"RIPEMD320", 40},
77 [SHA160] = {"SHA160", 20},
78 [SHA224] = {"SHA224", 28},
79 [SHA256] = {"SHA256", 32},
80 [SHA512_224] = {"SHA512/224", 28},
81 [SHA512_256] = {"SHA512/256", 32},
82 [SHA384] = {"SHA384", 48},
83 [SHA512] = {"SHA512", 64},
84 [CRC32] = {"CRC32", 4},
85 [ADLER32] = {"adler32", 4},
86 };
87
88 31 const char *av_hash_names(int i)
89 {
90
3/4
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 30 times.
31 if (i < 0 || i >= NUM_HASHES) return NULL;
91 30 return hashdesc[i].name;
92 }
93
94 2380 const char *av_hash_get_name(const AVHashContext *ctx)
95 {
96 2380 return hashdesc[ctx->type].name;
97 }
98
99 9077 int av_hash_get_size(const AVHashContext *ctx)
100 {
101 9077 return hashdesc[ctx->type].size;
102 }
103
104 656 int av_hash_alloc(AVHashContext **ctx, const char *name)
105 {
106 AVHashContext *res;
107 int i;
108 656 *ctx = NULL;
109
1/2
✓ Branch 0 taken 906 times.
✗ Branch 1 not taken.
906 for (i = 0; i < NUM_HASHES; i++)
110
2/2
✓ Branch 1 taken 656 times.
✓ Branch 2 taken 250 times.
906 if (av_strcasecmp(name, hashdesc[i].name) == 0)
111 656 break;
112
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 656 times.
656 if (i >= NUM_HASHES) return AVERROR(EINVAL);
113 656 res = av_mallocz(sizeof(*res));
114
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 656 times.
656 if (!res) return AVERROR(ENOMEM);
115 656 res->type = i;
116
7/8
✓ Branch 0 taken 631 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 10 times.
✓ Branch 6 taken 3 times.
✗ Branch 7 not taken.
656 switch (i) {
117 631 case MD5: res->ctx = av_md5_alloc(); break;
118 1 case MURMUR3: res->ctx = av_murmur3_alloc(); break;
119 4 case RIPEMD128:
120 case RIPEMD160:
121 case RIPEMD256:
122 4 case RIPEMD320: res->ctx = av_ripemd_alloc(); break;
123 3 case SHA160:
124 case SHA224:
125 3 case SHA256: res->ctx = av_sha_alloc(); break;
126 4 case SHA512_224:
127 case SHA512_256:
128 case SHA384:
129 4 case SHA512: res->ctx = av_sha512_alloc(); break;
130 10 case CRC32: res->crctab = av_crc_get_table(AV_CRC_32_IEEE_LE); break;
131 3 case ADLER32: break;
132 }
133
5/6
✓ Branch 0 taken 653 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 643 times.
✓ Branch 3 taken 10 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 643 times.
656 if (i != ADLER32 && i != CRC32 && !res->ctx) {
134 av_free(res);
135 return AVERROR(ENOMEM);
136 }
137 656 *ctx = res;
138 656 return 0;
139 }
140
141 8362 void av_hash_init(AVHashContext *ctx)
142 {
143
15/16
✓ Branch 0 taken 6315 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 3 times.
✓ Branch 6 taken 3 times.
✓ Branch 7 taken 3 times.
✓ Branch 8 taken 3 times.
✓ Branch 9 taken 3 times.
✓ Branch 10 taken 3 times.
✓ Branch 11 taken 3 times.
✓ Branch 12 taken 3 times.
✓ Branch 13 taken 2006 times.
✓ Branch 14 taken 5 times.
✗ Branch 15 not taken.
8362 switch (ctx->type) {
144 6315 case MD5: av_md5_init(ctx->ctx); break;
145 3 case MURMUR3: av_murmur3_init(ctx->ctx); break;
146 3 case RIPEMD128: av_ripemd_init(ctx->ctx, 128); break;
147 3 case RIPEMD160: av_ripemd_init(ctx->ctx, 160); break;
148 3 case RIPEMD256: av_ripemd_init(ctx->ctx, 256); break;
149 3 case RIPEMD320: av_ripemd_init(ctx->ctx, 320); break;
150 3 case SHA160: av_sha_init(ctx->ctx, 160); break;
151 3 case SHA224: av_sha_init(ctx->ctx, 224); break;
152 3 case SHA256: av_sha_init(ctx->ctx, 256); break;
153 3 case SHA512_224: av_sha512_init(ctx->ctx, 224); break;
154 3 case SHA512_256: av_sha512_init(ctx->ctx, 256); break;
155 3 case SHA384: av_sha512_init(ctx->ctx, 384); break;
156 3 case SHA512: av_sha512_init(ctx->ctx, 512); break;
157 2006 case CRC32: ctx->crc = UINT32_MAX; break;
158 5 case ADLER32: ctx->crc = 1; break;
159 }
160 8362 }
161
162 180862 void av_hash_update(AVHashContext *ctx, const uint8_t *src, size_t len)
163 {
164
7/8
✓ Branch 0 taken 178815 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 9 times.
✓ Branch 4 taken 12 times.
✓ Branch 5 taken 2006 times.
✓ Branch 6 taken 5 times.
✗ Branch 7 not taken.
180862 switch (ctx->type) {
165 178815 case MD5: av_md5_update(ctx->ctx, src, len); break;
166 3 case MURMUR3: av_murmur3_update(ctx->ctx, src, len); break;
167 12 case RIPEMD128:
168 case RIPEMD160:
169 case RIPEMD256:
170 12 case RIPEMD320: av_ripemd_update(ctx->ctx, src, len); break;
171 9 case SHA160:
172 case SHA224:
173 9 case SHA256: av_sha_update(ctx->ctx, src, len); break;
174 12 case SHA512_224:
175 case SHA512_256:
176 case SHA384:
177 12 case SHA512: av_sha512_update(ctx->ctx, src, len); break;
178 2006 case CRC32: ctx->crc = av_crc(ctx->crctab, ctx->crc, src, len); break;
179 5 case ADLER32: ctx->crc = av_adler32_update(ctx->crc, src, len); break;
180 }
181 180862 }
182
183 8362 void av_hash_final(AVHashContext *ctx, uint8_t *dst)
184 {
185
7/8
✓ Branch 0 taken 6315 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 9 times.
✓ Branch 4 taken 12 times.
✓ Branch 5 taken 2006 times.
✓ Branch 6 taken 5 times.
✗ Branch 7 not taken.
8362 switch (ctx->type) {
186 6315 case MD5: av_md5_final(ctx->ctx, dst); break;
187 3 case MURMUR3: av_murmur3_final(ctx->ctx, dst); break;
188 12 case RIPEMD128:
189 case RIPEMD160:
190 case RIPEMD256:
191 12 case RIPEMD320: av_ripemd_final(ctx->ctx, dst); break;
192 9 case SHA160:
193 case SHA224:
194 9 case SHA256: av_sha_final(ctx->ctx, dst); break;
195 12 case SHA512_224:
196 case SHA512_256:
197 case SHA384:
198 12 case SHA512: av_sha512_final(ctx->ctx, dst); break;
199 2006 case CRC32: AV_WB32(dst, ctx->crc ^ UINT32_MAX); break;
200 5 case ADLER32: AV_WB32(dst, ctx->crc); break;
201 }
202 8362 }
203
204 15 void av_hash_final_bin(struct AVHashContext *ctx, uint8_t *dst, int size)
205 {
206 uint8_t buf[AV_HASH_MAX_SIZE];
207 15 unsigned rsize = av_hash_get_size(ctx);
208
209 15 av_hash_final(ctx, buf);
210 15 memcpy(dst, buf, FFMIN(size, rsize));
211
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 if (size > rsize)
212 15 memset(dst + rsize, 0, size - rsize);
213 15 }
214
215 8332 void av_hash_final_hex(struct AVHashContext *ctx, uint8_t *dst, int size)
216 {
217 uint8_t buf[AV_HASH_MAX_SIZE];
218 8332 unsigned rsize = av_hash_get_size(ctx), i;
219
220 8332 av_hash_final(ctx, buf);
221
2/2
✓ Branch 0 taken 109412 times.
✓ Branch 1 taken 8332 times.
117744 for (i = 0; i < FFMIN(rsize, size / 2); i++)
222 109412 snprintf(dst + i * 2, size - i * 2, "%02x", buf[i]);
223 8332 }
224
225 15 void av_hash_final_b64(struct AVHashContext *ctx, uint8_t *dst, int size)
226 {
227 uint8_t buf[AV_HASH_MAX_SIZE], b64[AV_BASE64_SIZE(AV_HASH_MAX_SIZE)];
228 15 unsigned rsize = av_hash_get_size(ctx), osize;
229
230 15 av_hash_final(ctx, buf);
231 15 av_base64_encode(b64, sizeof(b64), buf, rsize);
232 15 osize = AV_BASE64_SIZE(rsize);
233 15 memcpy(dst, b64, FFMIN(osize, size));
234
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (size < osize)
235 dst[size - 1] = 0;
236 15 }
237
238 779 void av_hash_freep(AVHashContext **ctx)
239 {
240
2/2
✓ Branch 0 taken 656 times.
✓ Branch 1 taken 123 times.
779 if (*ctx)
241 656 av_freep(&(*ctx)->ctx);
242 779 av_freep(ctx);
243 779 }
244