OpenCoverage

b2sum.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/coreutils/src/src/blake2/b2sum.c
Source codeSwitch to Preprocessed file
LineSourceCount
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-
16#include <stdio.h>-
17#include <stdlib.h>-
18#include <string.h>-
19#include <assert.h>-
20#include <errno.h>-
21-
22#include <ctype.h>-
23#include <unistd.h>-
24#include <getopt.h>-
25#include <stdbool.h>-
26-
27#include "blake2.h"-
28-
29/* This will help compatibility with coreutils */-
30int blake2b_stream( FILE *stream, void *resstream, size_t outbytes )-
31{-
32 int ret = -1;-
33 size_t sum, n;-
34 blake2b_state S[1];-
35 static const size_t buffer_length = 32768;-
36 uint8_t *buffer = ( uint8_t * )malloc( buffer_length );-
37-
38 if( !buffer ) return -1;
never executed: return -1;
!bufferDescription
TRUEnever evaluated
FALSEevaluated 38 times by 1 test
Evaluated by:
  • b2sum
0-38
39-
40 blake2b_init( S, outbytes );-
41-
42 while( 1 )-
43 {-
44 sum = 0;-
45-
46 while( 1 )-
47 {-
48 n = fread( buffer + sum, 1, buffer_length - sum, stream );-
49 sum += n;-
50-
51 if( buffer_length == sum )
buffer_length == sumDescription
TRUEnever evaluated
FALSEevaluated 38 times by 1 test
Evaluated by:
  • b2sum
0-38
52 break;
never executed: break;
0
53-
54 if( 0 == n )
0 == nDescription
TRUEevaluated 6 times by 1 test
Evaluated by:
  • b2sum
FALSEevaluated 32 times by 1 test
Evaluated by:
  • b2sum
6-32
55 {-
56 if( ferror( stream ) )
ferror( stream )Description
TRUEnever evaluated
FALSEevaluated 6 times by 1 test
Evaluated by:
  • b2sum
0-6
57 goto cleanup_buffer;
never executed: goto cleanup_buffer;
0
58-
59 goto final_process;
executed 6 times by 1 test: goto final_process;
Executed by:
  • b2sum
6
60 }-
61-
62 if( feof( stream ) )
feof( stream )Description
TRUEevaluated 32 times by 1 test
Evaluated by:
  • b2sum
FALSEnever evaluated
0-32
63 goto final_process;
executed 32 times by 1 test: goto final_process;
Executed by:
  • b2sum
32
64 }
never executed: end of block
0
65-
66 blake2b_update( S, buffer, buffer_length );-
67 }
never executed: end of block
0
68-
69final_process:;
code before this statement never executed: final_process:
0
70-
71 if( sum > 0 ) blake2b_update( S, buffer, sum );
executed 32 times by 1 test: blake2b_update( S, buffer, sum );
Executed by:
  • b2sum
sum > 0Description
TRUEevaluated 32 times by 1 test
Evaluated by:
  • b2sum
FALSEevaluated 6 times by 1 test
Evaluated by:
  • b2sum
6-32
72-
73 blake2b_final( S, resstream, outbytes );-
74 ret = 0;-
75cleanup_buffer:
code before this statement executed 38 times by 1 test: cleanup_buffer:
Executed by:
  • b2sum
38
76 free( buffer );-
77 return ret;
executed 38 times by 1 test: return ret;
Executed by:
  • b2sum
38
78}-
79-
80#if 0-
81int blake2s_stream( FILE *stream, void *resstream, size_t outbytes )-
82{-
83 int ret = -1;-
84 size_t sum, n;-
85 blake2s_state S[1];-
86 static const size_t buffer_length = 32768;-
87 uint8_t *buffer = ( uint8_t * )malloc( buffer_length );-
88-
89 if( !buffer ) return -1;-
90-
91 blake2s_init( S, outbytes );-
92-
93 while( 1 )-
94 {-
95 sum = 0;-
96-
97 while( 1 )-
98 {-
99 n = fread( buffer + sum, 1, buffer_length - sum, stream );-
100 sum += n;-
101-
102 if( buffer_length == sum )-
103 break;-
104-
105 if( 0 == n )-
106 {-
107 if( ferror( stream ) )-
108 goto cleanup_buffer;-
109-
110 goto final_process;-
111 }-
112-
113 if( feof( stream ) )-
114 goto final_process;-
115 }-
116-
117 blake2s_update( S, buffer, buffer_length );-
118 }-
119-
120final_process:;-
121-
122 if( sum > 0 ) blake2s_update( S, buffer, sum );-
123-
124 blake2s_final( S, resstream, outbytes );-
125 ret = 0;-
126cleanup_buffer:-
127 free( buffer );-
128 return ret;-
129}-
130-
131-
132int blake2sp_stream( FILE *stream, void *resstream, size_t outbytes )-
133{-
134 int ret = -1;-
135 size_t sum, n;-
136 blake2sp_state S[1];-
137 static const size_t buffer_length = 16 * ( 1UL << 20 );-
138 uint8_t *buffer = ( uint8_t * )malloc( buffer_length );-
139-
140 if( !buffer ) return -1;-
141-
142 blake2sp_init( S, outbytes );-
143-
144 while( 1 )-
145 {-
146 sum = 0;-
147-
148 while( 1 )-
149 {-
150 n = fread( buffer + sum, 1, buffer_length - sum, stream );-
151 sum += n;-
152-
153 if( buffer_length == sum )-
154 break;-
155-
156 if( 0 == n )-
157 {-
158 if( ferror( stream ) )-
159 goto cleanup_buffer;-
160-
161 goto final_process;-
162 }-
163-
164 if( feof( stream ) )-
165 goto final_process;-
166 }-
167-
168 blake2sp_update( S, buffer, buffer_length );-
169 }-
170-
171final_process:;-
172-
173 if( sum > 0 ) blake2sp_update( S, buffer, sum );-
174-
175 blake2sp_final( S, resstream, outbytes );-
176 ret = 0;-
177cleanup_buffer:-
178 free( buffer );-
179 return ret;-
180}-
181-
182-
183int blake2bp_stream( FILE *stream, void *resstream, size_t outbytes )-
184{-
185 int ret = -1;-
186 size_t sum, n;-
187 blake2bp_state S[1];-
188 static const size_t buffer_length = 16 * ( 1UL << 20 );-
189 uint8_t *buffer = ( uint8_t * )malloc( buffer_length );-
190-
191 if( !buffer ) return -1;-
192-
193 blake2bp_init( S, outbytes );-
194-
195 while( 1 )-
196 {-
197 sum = 0;-
198-
199 while( 1 )-
200 {-
201 n = fread( buffer + sum, 1, buffer_length - sum, stream );-
202 sum += n;-
203-
204 if( buffer_length == sum )-
205 break;-
206-
207 if( 0 == n )-
208 {-
209 if( ferror( stream ) )-
210 goto cleanup_buffer;-
211-
212 goto final_process;-
213 }-
214-
215 if( feof( stream ) )-
216 goto final_process;-
217 }-
218-
219 blake2bp_update( S, buffer, buffer_length );-
220 }-
221-
222final_process:;-
223-
224 if( sum > 0 ) blake2bp_update( S, buffer, sum );-
225-
226 blake2bp_final( S, resstream, outbytes );-
227 ret = 0;-
228cleanup_buffer:-
229 free( buffer );-
230 return ret;-
231}-
232-
233typedef int ( *blake2fn )( FILE *, void *, size_t );-
234-
235-
236static void usage( char **argv, int errcode )-
237{-
238 FILE *out = errcode ? stderr : stdout;-
239 fprintf( out, "Usage: %s [OPTION]... [FILE]...\n", argv[0] );-
240 fprintf( out, "\n" );-
241 fprintf( out, "With no FILE, or when FILE is -, read standard input.\n" );-
242 fprintf( out, "\n" );-
243 fprintf( out, " -a <algo> hash algorithm (blake2b is default): \n"-
244 " [blake2b|blake2s|blake2bp|blake2sp]\n" );-
245 fprintf( out, " -l <length> digest length in bits, must not exceed the maximum for\n"-
246 " the selected algorithm and must be a multiple of 8\n" );-
247 fprintf( out, " --tag create a BSD-style checksum\n" );-
248 fprintf( out, " --help display this help and exit\n" );-
249 exit( errcode );-
250}-
251-
252int main( int argc, char **argv )-
253{-
254 blake2fn blake2_stream = blake2b_stream;-
255 unsigned long maxbytes = BLAKE2B_OUTBYTES;-
256 const char *algorithm = "BLAKE2b";-
257 unsigned long outbytes = 0;-
258 unsigned char hash[BLAKE2B_OUTBYTES] = {0};-
259 bool bsdstyle = false;-
260 int c, i;-
261 opterr = 1;-
262-
263 while( 1 )-
264 {-
265 int option_index = 0;-
266 char *end = NULL;-
267 unsigned long outbits;-
268 static struct option long_options[] = {-
269 { "help", no_argument, 0, 0 },-
270 { "tag", no_argument, 0, 0 },-
271 { NULL, 0, NULL, 0 }-
272 };-
273-
274 c = getopt_long( argc, argv, "a:l:", long_options, &option_index );-
275 if( c == -1 ) break;-
276 switch( c )-
277 {-
278 case 'a':-
279 if( 0 == strcmp( optarg, "blake2b" ) )-
280 {-
281 blake2_stream = blake2b_stream;-
282 maxbytes = BLAKE2B_OUTBYTES;-
283 algorithm = "BLAKE2b";-
284 }-
285 else if ( 0 == strcmp( optarg, "blake2s" ) )-
286 {-
287 blake2_stream = blake2s_stream;-
288 maxbytes = BLAKE2S_OUTBYTES;-
289 algorithm = "BLAKE2s";-
290 }-
291 else if ( 0 == strcmp( optarg, "blake2bp" ) )-
292 {-
293 blake2_stream = blake2bp_stream;-
294 maxbytes = BLAKE2B_OUTBYTES;-
295 algorithm = "BLAKE2bp";-
296 }-
297 else if ( 0 == strcmp( optarg, "blake2sp" ) )-
298 {-
299 blake2_stream = blake2sp_stream;-
300 maxbytes = BLAKE2S_OUTBYTES;-
301 algorithm = "BLAKE2sp";-
302 }-
303 else-
304 {-
305 printf( "Invalid function name: `%s'\n", optarg );-
306 usage( argv, 111 );-
307 }-
308-
309 break;-
310-
311 case 'l':-
312 outbits = strtoul(optarg, &end, 10);-
313 if( !end || *end != '\0' || outbits % 8 != 0)-
314 {-
315 printf( "Invalid length argument: `%s'\n", optarg);-
316 usage( argv, 111 );-
317 }-
318 outbytes = outbits / 8;-
319 break;-
320-
321 case 0:-
322 if( 0 == strcmp( "help", long_options[option_index].name ) )-
323 usage( argv, 0 );-
324 else if( 0 == strcmp( "tag", long_options[option_index].name ) )-
325 bsdstyle = true;-
326 break;-
327-
328 case '?':-
329 usage( argv, 1 );-
330 break;-
331 }-
332 }-
333-
334 if(outbytes > maxbytes)-
335 {-
336 printf( "Invalid length argument: %lu\n", outbytes * 8 );-
337 printf( "Maximum digest length for %s is %lu\n", algorithm, maxbytes * 8 );-
338 usage( argv, 111 );-
339 }-
340 else if( outbytes == 0 )-
341 outbytes = maxbytes;-
342-
343 if( optind == argc )-
344 argv[argc++] = (char *) "-";-
345-
346 for( i = optind; i < argc; ++i )-
347 {-
348 FILE *f = NULL;-
349 if( argv[i][0] == '-' && argv[i][1] == '\0' )-
350 f = stdin;-
351 else-
352 f = fopen( argv[i], "rb" );-
353-
354 if( !f )-
355 {-
356 fprintf( stderr, "Could not open `%s': %s\n", argv[i], strerror( errno ) );-
357 continue;-
358 }-
359-
360 if( blake2_stream( f, hash, outbytes ) < 0 )-
361 {-
362 fprintf( stderr, "Failed to hash `%s'\n", argv[i] );-
363 }-
364 else-
365 {-
366 size_t j;-
367 if( bsdstyle )-
368 {-
369 if( outbytes < maxbytes )-
370 printf( "%s-%lu (%s) = ", algorithm, outbytes * 8, argv[i] );-
371 else-
372 printf( "%s (%s) = ", algorithm, argv[i] );-
373 }-
374-
375 for( j = 0; j < outbytes; ++j )-
376 printf( "%02x", hash[j] );-
377-
378 if( bsdstyle )-
379 printf( "\n" );-
380 else-
381 printf( " %s\n", argv[i] );-
382 }-
383-
384 if( f != stdin ) fclose( f );-
385 }-
386-
387 return 0;-
388}-
389#endif-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.1.2