[vlc-devel] [PATCH v2 03/15] src: add cleaner md5 API

Rémi Denis-Courmont remi at remlab.net
Wed Apr 8 11:00:19 CEST 2020


I wouldn't heap-allocate the string here. Just define a helper constant, the size is well known and fixed here.

Le 8 avril 2020 01:04:19 GMT+03:00, Marvin Scholz <epirat07 at gmail.com> a écrit :
>This API is intended to replace the existing MD5 hashing API.
>It uses properly prefixed functions and has documentation.
>---
> include/vlc_hash.h | 126 +++++++++++++++++++++++++++++++++++++++++++++
> src/Makefile.am    |   1 +
> src/libvlccore.sym |   4 ++
> src/misc/md5.c     |  37 ++++++++++++-
> 4 files changed, 167 insertions(+), 1 deletion(-)
> create mode 100644 include/vlc_hash.h
>
>diff --git a/include/vlc_hash.h b/include/vlc_hash.h
>new file mode 100644
>index 0000000000..19d137c9cc
>--- /dev/null
>+++ b/include/vlc_hash.h
>@@ -0,0 +1,126 @@
>+/*****************************************************************************
>+ * vlc_hash.h: Hash functions
>+
>*****************************************************************************
>+ * Copyright © 2004-2020 VLC authors and VideoLAN
>+ *
>+ * Authors: Rémi Denis-Courmont
>+ *          Rafaël Carré
>+ *          Marvin Scholz
>+ *
>+ * This program is free software; you can redistribute it and/or
>modify it
>+ * under the terms of the GNU Lesser General Public License as
>published by
>+ * the Free Software Foundation; either version 2.1 of the License, or
>+ * (at your option) any later version.
>+ *
>+ * This program is distributed in the hope that it will be useful,
>+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
>+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
>+ * GNU Lesser General Public License for more details.
>+ *
>+ * You should have received a copy of the GNU Lesser General Public
>License
>+ * along with this program; if not, write to the Free Software
>Foundation,
>+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
>+
>*****************************************************************************/
>+
>+#ifndef VLC_HASH_H
>+# define VLC_HASH_H
>+
>+#include <vlc_md5.h>
>+
>+/**
>+ * \defgroup vlc_hash  Hash functions
>+ * APIs for simple and frequently used hash algorithms in VLC
>+ * 
>+ * Each hash algorithm has a context object which stores all data
>needed for the
>+ * hash calculation, this context is not supposed to be modified
>directly by the
>+ * called but only with the functions listed here.
>+ *
>+ * Supported hash algorithms:
>+ *   - \ref vlc_hash_md5 "MD5"
>+ *
>+ * @{
>+ */
>+
>+/**
>+ * \defgroup vlc_hash_md5  MD5 hashing
>+ * APIs to hash data using the Message-Digest Algorithm 5 (MD5)
>+ * @{
>+ */
>+
>+/**
>+ * MD5 hash context
>+ */
>+typedef struct vlc_hash_md5_ctx
>+{
>+    struct md5_s priv; /**< \internal Private */
>+} vlc_hash_md5_t;
>+
>+/**
>+ * MD5 digest output size
>+ */
>+#define VLC_HASH_MD5_DIGEST_SIZE 16
>+
>+/**
>+ * Initialize MD5 context
>+ * 
>+ * Initializes the given MD5 hash function context, if the context is
>+ * already initialized, it is reset.
>+ * 
>+ * \param[out] ctx  MD5 hash context to init
>+ */
>+VLC_API void vlc_hash_md5_Init(vlc_hash_md5_t *ctx);
>+
>+/**
>+ * Update MD5 hash computation with new data
>+ * 
>+ * Updates the context with provided data which is used for the hash
>+ * calculation. Can be called repeatedly with new data. The final
>+ * hash represents the hash for the concatenation of all data.
>+ * 
>+ * \param[in,out] ctx    MD5 hash context to update
>+ * \param         data   Data to add
>+ * \param         size   Size of the data to add
>+ */
>+VLC_API void vlc_hash_md5_Update(vlc_hash_md5_t *ctx, const void
>*data, size_t size);
>+
>+/**
>+ * Finish MD5 hash computation
>+ * 
>+ * Finishes the MD5 hash computation and provides the hash for the
>+ * concatenation of all provided data by previous calls to \ref
>vlc_hash_md5_Update.
>+ * The result is written to the buffer pointe to by output, which must
>be at
>+ * least \ref VLC_HASH_MD5_DIGEST_SIZE big.
>+ * 
>+ * \note The context is reset.
>+ * 
>+ * \param[in,out] ctx    MD5 hash context to finish
>+ * \param[out]    output Output buffer to write to
>+ * \param         size   Output buffer size
>+ */
>+VLC_API void vlc_hash_md5_Finish(vlc_hash_md5_t *ctx, void *output,
>size_t size);
>+
>+/**
>+ * Finish MD5 hash computation and return hex representation
>+ * 
>+ * Finishes the MD5 hash computation and provides the hash for the
>+ * concatenation of all provided data by previous calls to \ref
>vlc_hash_md5_Update
>+ * in hex encoded format.
>+ * 
>+ * \note The context is reset, if this function returns successfully.
>On error, the
>+ *       context is not modified.
>+ * 
>+ * \param[in,out] ctx    MD5 hash context to finish
>+ * \return Heap-allocated zero-terminated string with the hexadecimal
>representation
>+ *         of the MD5 digest data, or NULL in case of error.
>+ */
>+VLC_API char* vlc_hash_md5_FinishHex(vlc_hash_md5_t *ctx);
>+
>+/**
>+ * @}
>+ */
>+
>+/**
>+ * @}
>+ */
>+
>+#endif
>diff --git a/src/Makefile.am b/src/Makefile.am
>index 88c6d903ae..7e99d03bc2 100644
>--- a/src/Makefile.am
>+++ b/src/Makefile.am
>@@ -54,6 +54,7 @@ pluginsinclude_HEADERS = \
> 	../include/vlc_fs.h \
> 	../include/vlc_gcrypt.h \
> 	../include/vlc_opengl.h \
>+	../include/vlc_hash.h \
> 	../include/vlc_http.h \
> 	../include/vlc_httpd.h \
> 	../include/vlc_image.h \
>diff --git a/src/libvlccore.sym b/src/libvlccore.sym
>index 17561db932..ed07d998e2 100644
>--- a/src/libvlccore.sym
>+++ b/src/libvlccore.sym
>@@ -144,6 +144,10 @@ httpd_HandlerNew
> httpd_HostDelete
> vlc_http_HostNew
> vlc_https_HostNew
>+vlc_hash_md5_Init
>+vlc_hash_md5_Update
>+vlc_hash_md5_Finish
>+vlc_hash_md5_FinishHex
> vlc_rtsp_HostNew
> httpd_MsgAdd
> httpd_MsgGet
>diff --git a/src/misc/md5.c b/src/misc/md5.c
>index adfd939c0a..24b940debf 100644
>--- a/src/misc/md5.c
>+++ b/src/misc/md5.c
>@@ -40,9 +40,11 @@
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
>+#include <assert.h>
> 
> #include <vlc_common.h>
>-#include <vlc_md5.h>
>+#include <vlc_strings.h>
>+#include <vlc_hash.h>
> 
> typedef uint32_t u32;
> typedef uint8_t byte;
>@@ -347,3 +349,36 @@ void EndMD5( struct md5_s *h )
> {
>     md5_final( h );
> }
>+
>+/* New API */
>+void vlc_hash_md5_Init(vlc_hash_md5_t *ctx)
>+{
>+  md5_init(&ctx->priv);
>+}
>+
>+void vlc_hash_md5_Update(vlc_hash_md5_t *ctx, const void *data, size_t
>length)
>+{
>+  md5_write(&ctx->priv, data, length);
>+}
>+
>+void vlc_hash_md5_Finish(vlc_hash_md5_t *ctx, void *output, size_t
>size)
>+{
>+  assert(size >= VLC_HASH_MD5_DIGEST_SIZE);
>+  md5_final(&ctx->priv);
>+  memcpy(output, ctx->priv.buf, VLC_HASH_MD5_DIGEST_SIZE);
>+  vlc_hash_md5_Init(ctx);
>+}
>+
>+char* vlc_hash_md5_FinishHex(vlc_hash_md5_t *ctx)
>+{
>+  char *hex = malloc(VLC_HASH_MD5_DIGEST_SIZE * 2 + 1); // Two chars
>per byte + NULL
>+  if (unlikely(hex == NULL))
>+    return NULL;
>+
>+  md5_final(&ctx->priv);
>+  vlc_hex_encode_binary(ctx->priv.buf, VLC_HASH_MD5_DIGEST_SIZE, hex);
>+  vlc_hash_md5_Init(ctx);
>+
>+  return hex;
>+}
>+
>-- 
>2.24.1 (Apple Git-126)
>
>_______________________________________________
>vlc-devel mailing list
>To unsubscribe or modify your subscription options:
>https://mailman.videolan.org/listinfo/vlc-devel

-- 
Envoyé de mon appareil Android avec Courriel K-9 Mail. Veuillez excuser ma brièveté.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.videolan.org/pipermail/vlc-devel/attachments/20200408/fa30445c/attachment.html>


More information about the vlc-devel mailing list