LCOV - code coverage report
Current view: top level - libavutil - hash.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 100 103 97.1 %
Date: 2017-12-15 02:19:58 Functions: 11 11 100.0 %

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

Generated by: LCOV version 1.13