merge with 0.30.213
[util-vserver.git] / src / testsuite / hashcalc-plain.c
1 // $Id: hashcalc-plain.c 2245 2006-01-04 17:28:42Z ensc $    --*- c -*--
2
3 // Copyright (C) 2006 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 //  
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; version 2 of the License.
8 //  
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //  
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18
19 #ifdef HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22
23 #include <beecrypt/beecrypt.h>
24 #include <stdbool.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <sys/mman.h>
28
29 #define ENSC_TESTSUITE
30 #include "lib_internal/coreassert.h"
31
32 #define HASH_BLOCKSIZE          0x10000000u
33
34 static bool
35 convertDigest(char res[], hashFunctionContext * h_ctx)
36 {
37   static char const             HEX_DIGIT[] = "0123456789abcdef";
38   size_t                        d_size   = h_ctx->algo->digestsize;
39     
40   unsigned char                 digest[d_size];
41   size_t                        out = 0;
42
43   if (hashFunctionContextDigest(h_ctx, digest)==-1)
44     return false;
45   
46   for (size_t in=0; in<d_size; ++in) {
47     res[out++]  = HEX_DIGIT[digest[in] >>    4];
48     res[out++]  = HEX_DIGIT[digest[in] &  0x0f];
49   }
50   res[out++] = '\0';
51   
52   return true;
53 }
54
55 int main(int UNUSED argc, char *argv[])
56 {
57   int                   fd = open(argv[1], O_NOFOLLOW|O_NONBLOCK|O_RDONLY|O_NOCTTY);
58   hashFunctionContext   hash_context;
59   hashFunction const    *method;
60   struct stat           st;
61   off_t                 size;
62   loff_t                offset = 0;
63   char                  digest[1024];
64
65   assert((method = hashFunctionFind(argv[2]))!=0);
66   assert(hashFunctionContextInit(&hash_context, method)!=-1);
67
68   assert(fstat(fd, &st)!=-1);
69   assert(hashFunctionContextReset(&hash_context)!=-1);
70
71   size = st.st_size;
72
73   while (offset < size) {
74     loff_t volatile             buf_size = size-offset;
75     void const *                buf;
76     if (buf_size>HASH_BLOCKSIZE) buf_size = HASH_BLOCKSIZE;
77
78     assert((buf=mmap(0, buf_size, PROT_READ, MAP_SHARED, fd, offset))!=0);
79     offset += buf_size;
80     assert(hashFunctionContextUpdate(&hash_context, buf, buf_size)!=-1);
81     munmap((void *)(buf), buf_size);
82   }
83     
84   assert(convertDigest(digest, &hash_context));
85   
86   Vwrite(1, digest, strlen(digest));
87   Vwrite(1, "\n", 1);
88   
89   hashFunctionContextFree(&hash_context);
90   
91   return 0;
92 }