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