"Fossies" - the Fresh Open Source Software Archive

Member "littleutils-1.2.5/littleutils/b2sum.c" (29 Oct 2021, 2091 Bytes) of package /linux/privat/littleutils-1.2.5.tar.lz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "b2sum.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.2.4_vs_1.2.5.

    1 /*
    2    BLAKE2 reference source code package - b2sum tool
    3 
    4    Copyright 2012, Samuel Neves <sneves@dei.uc.pt>.  You may use this under the
    5    terms of the CC0, the OpenSSL Licence, or the Apache Public License 2.0, at
    6    your option.  The terms of these licenses can be found at:
    7 
    8    - CC0 1.0 Universal : https://creativecommons.org/publicdomain/zero/1.0
    9    - OpenSSL license   : https://www.openssl.org/source/license.html
   10    - Apache 2.0        : https://www.apache.org/licenses/LICENSE-2.0
   11 
   12    More information about the BLAKE2 hash function can be found at
   13    https://blake2.net.
   14 
   15    Modifications for portability and partial file reads by Brian Lindholm,
   16    2020-2021.
   17 */
   18 
   19 #include <config.h>
   20 
   21 #ifdef HAVE_STDIO_H
   22 # include <stdio.h>
   23 #endif
   24 #ifdef HAVE_STDLIB_H
   25 # include <stdlib.h>
   26 #endif
   27 #ifdef HAVE_STRINGS_H
   28 # include <string.h>
   29 #endif
   30 
   31 #include "blake2.h"
   32 
   33 int blake2b_stream( FILE *stream, void *resstream, size_t outbytes, off_t readbytes )
   34 {
   35   int ret = -1;
   36   size_t sum, n, target;
   37   blake2b_state S[1];
   38   static const size_t buffer_length = 32768;
   39   uint8_t *buffer = ( uint8_t * )malloc( buffer_length );
   40   off_t total = 0;
   41 
   42   if( !buffer ) return -1;
   43 
   44   blake2b_init( S, outbytes );
   45   if ( readbytes == 0 )
   46   {
   47     sum = 0;
   48     goto final_process;
   49   }
   50 
   51   while( 1 )
   52   {
   53     if ( ( readbytes < 0 ) || ( ( readbytes - total ) > (off_t) buffer_length ) )
   54       target = (size_t) buffer_length;
   55     else
   56       target = (size_t) ( readbytes - total );
   57     sum = 0;
   58 
   59     while( 1 )
   60     {
   61       if( ( (off_t) sum >= target ) || feof( stream ) )
   62         goto final_process;
   63 
   64       n = fread( buffer + sum, 1, target - sum, stream );
   65       sum += n;
   66 
   67       if( buffer_length == sum )
   68         break;
   69 
   70       if( 0 == n )
   71       {
   72         if( ferror( stream ) )
   73           goto cleanup_buffer;
   74 
   75         goto final_process;
   76       }
   77     }
   78     total += (off_t) sum;
   79 
   80     blake2b_update( S, buffer, buffer_length );
   81   }
   82 
   83 final_process:;
   84 
   85   if( sum > 0 ) blake2b_update( S, buffer, sum );
   86 
   87   blake2b_final( S, resstream, outbytes );
   88   ret = 0;
   89 cleanup_buffer:
   90   free( buffer );
   91   return ret;
   92 }