digests, copy-file: increase the IO buffer size from 4KiB to 32KiB
[gnulib.git] / lib / sha256.c
index 0ad9444..4bc8ac3 100644 (file)
@@ -25,6 +25,7 @@
 #include "sha256.h"
 
 #include <stddef.h>
+#include <stdlib.h>
 #include <string.h>
 
 #if USE_UNLOCKED_IO
@@ -38,7 +39,7 @@
     (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24))
 #endif
 
-#define BLOCKSIZE 4096
+#define BLOCKSIZE 32768
 #if BLOCKSIZE % 64 != 0
 # error "invalid BLOCKSIZE"
 #endif
@@ -169,9 +170,12 @@ int
 sha256_stream (FILE *stream, void *resblock)
 {
   struct sha256_ctx ctx;
-  char buffer[BLOCKSIZE + 72];
   size_t sum;
 
+  char *buffer = malloc (BLOCKSIZE + 72);
+  if (!buffer)
+    return 1;
+
   /* Initialize the computation context.  */
   sha256_init_ctx (&ctx);
 
@@ -200,7 +204,10 @@ sha256_stream (FILE *stream, void *resblock)
                 exit the loop after a partial read due to e.g., EAGAIN
                 or EWOULDBLOCK.  */
              if (ferror (stream))
-               return 1;
+               {
+                 free (buffer);
+                 return 1;
+               }
              goto process_partial_block;
            }
 
@@ -225,6 +232,7 @@ sha256_stream (FILE *stream, void *resblock)
 
   /* Construct result in desired memory.  */
   sha256_finish_ctx (&ctx, resblock);
+  free (buffer);
   return 0;
 }
 
@@ -233,9 +241,12 @@ int
 sha224_stream (FILE *stream, void *resblock)
 {
   struct sha256_ctx ctx;
-  char buffer[BLOCKSIZE + 72];
   size_t sum;
 
+  char *buffer = malloc (BLOCKSIZE + 72);
+  if (!buffer)
+    return 1;
+
   /* Initialize the computation context.  */
   sha224_init_ctx (&ctx);
 
@@ -264,7 +275,10 @@ sha224_stream (FILE *stream, void *resblock)
                 exit the loop after a partial read due to e.g., EAGAIN
                 or EWOULDBLOCK.  */
              if (ferror (stream))
-               return 1;
+               {
+                 free (buffer);
+                 return 1;
+               }
              goto process_partial_block;
            }
 
@@ -289,6 +303,7 @@ sha224_stream (FILE *stream, void *resblock)
 
   /* Construct result in desired memory.  */
   sha224_finish_ctx (&ctx, resblock);
+  free (buffer);
   return 0;
 }