diff options
author | Nicolas George <george@nsup.org> | 2014-04-21 11:01:10 +0200 |
---|---|---|
committer | Nicolas George <george@nsup.org> | 2014-04-29 13:24:11 +0200 |
commit | b804eb4323a01f55727ac48475a9e3c257532ab5 (patch) | |
tree | a018de97b33ffc73263f1e7a30c9137f3b49d30f | |
parent | 5b881499a8e40ad0aa3424ec4d9adeb8008c4189 (diff) | |
download | ffmpeg-b804eb4323a01f55727ac48475a9e3c257532ab5.tar.gz |
lavu/hash: add hash_final helpers.
The helpers use local memory to compute the final hash,
making AV_HASH_MAX_SIZE safe to use.
-rw-r--r-- | doc/APIchanges | 3 | ||||
-rw-r--r-- | libavutil/hash.c | 35 | ||||
-rw-r--r-- | libavutil/hash.h | 22 | ||||
-rw-r--r-- | libavutil/version.h | 2 |
4 files changed, 61 insertions, 1 deletions
diff --git a/doc/APIchanges b/doc/APIchanges index 8fe5ff2e7c..8d001cd6c4 100644 --- a/doc/APIchanges +++ b/doc/APIchanges @@ -15,6 +15,9 @@ libavutil: 2012-10-22 API changes, most recent first: +2014-04-29 - xxxxxxx - lavu 52.80.0 - hash.h + Add av_hash_final_bin(), av_hash_final_hex() and av_hash_final_b64(). + 2014-03-07 - 8b2a130 - lavc 55.50.0 / 55.53.100 - dxva2.h Add FF_DXVA2_WORKAROUND_INTEL_CLEARVIDEO for old Intel GPUs. diff --git a/libavutil/hash.c b/libavutil/hash.c index a8cf80b577..773f29e23b 100644 --- a/libavutil/hash.c +++ b/libavutil/hash.c @@ -29,6 +29,7 @@ #include "sha512.h" #include "avstring.h" +#include "base64.h" #include "error.h" #include "intreadwrite.h" #include "mem.h" @@ -196,6 +197,40 @@ void av_hash_final(AVHashContext *ctx, uint8_t *dst) } } +void av_hash_final_bin(struct AVHashContext *ctx, uint8_t *dst, int size) +{ + uint8_t buf[AV_HASH_MAX_SIZE]; + unsigned rsize = av_hash_get_size(ctx); + + av_hash_final(ctx, buf); + memcpy(dst, buf, FFMIN(size, rsize)); + if (size > rsize) + memset(dst + rsize, 0, size - rsize); +} + +void av_hash_final_hex(struct AVHashContext *ctx, uint8_t *dst, int size) +{ + uint8_t buf[AV_HASH_MAX_SIZE]; + unsigned rsize = av_hash_get_size(ctx), i; + + av_hash_final(ctx, buf); + for (i = 0; i < FFMIN(rsize, size / 2); i++) + snprintf(dst + i * 2, size - i * 2, "%02x", buf[i]); +} + +void av_hash_final_b64(struct AVHashContext *ctx, uint8_t *dst, int size) +{ + uint8_t buf[AV_HASH_MAX_SIZE], b64[AV_BASE64_SIZE(AV_HASH_MAX_SIZE)]; + unsigned rsize = av_hash_get_size(ctx), osize; + + av_hash_final(ctx, buf); + av_base64_encode(b64, sizeof(b64), buf, rsize); + osize = AV_BASE64_SIZE(rsize); + memcpy(dst, b64, FFMIN(osize, size)); + if (size < osize) + dst[size - 1] = 0; +} + void av_hash_freep(AVHashContext **ctx) { if (*ctx) diff --git a/libavutil/hash.h b/libavutil/hash.h index 9bf715e1ac..d4bcbf8cc8 100644 --- a/libavutil/hash.h +++ b/libavutil/hash.h @@ -83,6 +83,28 @@ void av_hash_update(struct AVHashContext *ctx, const uint8_t *src, int len); void av_hash_final(struct AVHashContext *ctx, uint8_t *dst); /** + * Finalize a hash context and compute the actual hash value. + * If size is smaller than the hash size, the hash is truncated; + * if size is larger, the buffer is padded with 0. + */ +void av_hash_final_bin(struct AVHashContext *ctx, uint8_t *dst, int size); + +/** + * Finalize a hash context and compute the actual hash value as a hex string. + * The string is always 0-terminated. + * If size is smaller than 2 * hash_size + 1, the hex string is truncated. + */ +void av_hash_final_hex(struct AVHashContext *ctx, uint8_t *dst, int size); + +/** + * Finalize a hash context and compute the actual hash value as a base64 string. + * The string is always 0-terminated. + * If size is smaller than AV_BASE64_SIZE(hash_size), the base64 string is + * truncated. + */ +void av_hash_final_b64(struct AVHashContext *ctx, uint8_t *dst, int size); + +/** * Free hash context. */ void av_hash_freep(struct AVHashContext **ctx); diff --git a/libavutil/version.h b/libavutil/version.h index edafa30a3f..a17cb506de 100644 --- a/libavutil/version.h +++ b/libavutil/version.h @@ -56,7 +56,7 @@ */ #define LIBAVUTIL_VERSION_MAJOR 52 -#define LIBAVUTIL_VERSION_MINOR 79 +#define LIBAVUTIL_VERSION_MINOR 80 #define LIBAVUTIL_VERSION_MICRO 100 #define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \ |