[pacman-dev] [PATCH 1/3] libalpm md5: use larger and dynamic buffer

Dan McGee dan at archlinux.org
Thu Sep 2 13:30:52 EDT 2010


This gave at least a 10% improvement on a few tested platforms due to the
reduced number of read calls from files when computing the md5sum. It really
is just a precursor to another patch to come which is to use MD5 functions
that do the job a lot better than anything we can do.

Signed-off-by: Dan McGee <dan at archlinux.org>
---
 lib/libalpm/md5.c |   13 +++++++++++--
 1 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/lib/libalpm/md5.c b/lib/libalpm/md5.c
index 7d2716f..9063504 100644
--- a/lib/libalpm/md5.c
+++ b/lib/libalpm/md5.c
@@ -36,6 +36,8 @@
  *        int md5_file( char *path, unsigned char *output )
  *      to
  *        int md5_file( const char *path, unsigned char *output )
+ *  * use a dynamically-allocated buffer in md5_file, and increase the size
+ *    for performance reasons
  *  * various static/inline changes
  *
  *  NOTE: XySSL has been renamed to PolarSSL, which is available at
@@ -44,6 +46,7 @@
 
 #include <string.h>
 #include <stdio.h>
+#include <stdlib.h>
 
 #include "md5.h"
 
@@ -309,11 +312,16 @@ int md5_file( const char *path, unsigned char output[16] )
     FILE *f;
     size_t n;
     md5_context ctx;
-    unsigned char buf[1024];
+    unsigned char *buf;
 
-    if( ( f = fopen( path, "rb" ) ) == NULL )
+    if( ( buf = calloc(8192, sizeof(unsigned char)) ) == NULL )
         return( 1 );
 
+    if( ( f = fopen( path, "rb" ) ) == NULL ) {
+        free( buf );
+        return( 1 );
+    }
+
     md5_starts( &ctx );
 
     while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
@@ -322,6 +330,7 @@ int md5_file( const char *path, unsigned char output[16] )
     md5_finish( &ctx, output );
 
     memset( &ctx, 0, sizeof( md5_context ) );
+    free( buf );
 
     if( ferror( f ) != 0 )
     {
-- 
1.7.2.2



More information about the pacman-dev mailing list