util-vserver 0.30.215.
[util-vserver.git] / src / testsuite / hashcalc-plain.c
1 // $Id: hashcalc-plain.c 2685 2008-02-21 23:22:23Z 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 <lib_internal/crypto-wrapper.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[], ensc_hash_context * h_ctx)
36 {
37   static char const             HEX_DIGIT[] = "0123456789abcdef";
38   size_t                        d_size   = ensc_crypto_hashctx_get_digestsize(h_ctx);
39     
40   unsigned char                 digest[d_size];
41   size_t                        out = 0;
42
43   if (ensc_crypto_hashctx_get_digest(h_ctx, digest, NULL, d_size)==-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   ensc_hash_context             hash_context;
59   ensc_hash_method const        *method;
60   struct stat                   st;
61   off_t                         size;
62   loff_t                        offset = 0;
63   char                          digest[2048];
64
65   ensc_crypto_init();
66   assert((method = ensc_crypto_hash_find(argv[2]))!=0);
67   assert(ensc_crypto_hashctx_init(&hash_context, method)!=-1);
68
69   assert(fstat(fd, &st)!=-1);
70   assert(ensc_crypto_hashctx_reset(&hash_context)!=-1);
71
72   size = st.st_size;
73
74   while (offset < size) {
75     loff_t volatile             buf_size = size-offset;
76     void const *                buf;
77     if (buf_size>HASH_BLOCKSIZE) buf_size = HASH_BLOCKSIZE;
78
79     assert((buf=mmap(0, buf_size, PROT_READ, MAP_SHARED, fd, offset))!=0);
80     offset += buf_size;
81     assert(ensc_crypto_hashctx_update(&hash_context, buf, buf_size)!=-1);
82     munmap((void *)(buf), buf_size);
83   }
84     
85   assert(convertDigest(digest, &hash_context));
86   
87   Vwrite(1, digest, strlen(digest));
88   Vwrite(1, "\n", 1);
89   
90   ensc_crypto_hashctx_free(&hash_context);
91   
92   return 0;
93 }