[FFmpeg-devel] [PATCH 1/3] lavu/hash: add hash_final helpers.
Nicolas George
george at nsup.org
Mon Apr 21 17:39:54 CEST 2014
The helpers use local memory to compute the final hash,
making AV_HASH_MAX_SIZE safe to use.
TODO bump and apichanges entry
Signed-off-by: Nicolas George <george at nsup.org>
---
libavutil/hash.c | 35 +++++++++++++++++++++++++++++++++++
libavutil/hash.h | 22 ++++++++++++++++++++++
2 files changed, 57 insertions(+)
These one do allow to simplify existing code. See the next patch.
diff --git a/libavutil/hash.c b/libavutil/hash.c
index a177f48..8152fa3 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"
@@ -202,6 +203,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 9bf715e..d4bcbf8 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);
--
1.7.10.4
More information about the ffmpeg-devel
mailing list