]> pd.if.org Git - zpackage/blob - libtomcrypt/src/misc/compare_testvector.c
commit files needed for zpm-fetchurl
[zpackage] / libtomcrypt / src / misc / compare_testvector.c
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
2  *
3  * LibTomCrypt is a library that provides various cryptographic
4  * algorithms in a highly modular and flexible manner.
5  *
6  * The library is free for all purposes without any express
7  * guarantee it works.
8  */
9
10 #include "tomcrypt.h"
11
12 /**
13   @file compare_testvector.c
14   Function to compare two testvectors and print a (detailed) error-message if required, Steffen Jaeckel
15 */
16
17 #if defined(LTC_TEST) && defined(LTC_TEST_DBG)
18 static void _print_hex(const char* what, const void* v, const unsigned long l)
19 {
20   const unsigned char* p = v;
21   unsigned long x, y = 0, z;
22   fprintf(stderr, "%s contents: \n", what);
23   for (x = 0; x < l; ) {
24       fprintf(stderr, "%02X ", p[x]);
25       if (!(++x % 16) || x == l) {
26          if((x % 16) != 0) {
27             z = 16 - (x % 16);
28             if(z >= 8)
29                fprintf(stderr, " ");
30             for (; z != 0; --z) {
31                fprintf(stderr, "   ");
32             }
33          }
34          fprintf(stderr, " | ");
35          for(; y < x; y++) {
36             if((y % 8) == 0)
37                fprintf(stderr, " ");
38             if(isgraph(p[y]))
39                fprintf(stderr, "%c", p[y]);
40             else
41                fprintf(stderr, ".");
42          }
43          fprintf(stderr, "\n");
44       }
45       else if((x % 8) == 0) {
46          fprintf(stderr, " ");
47       }
48   }
49 }
50 #endif
51
52 /**
53   Compare two test-vectors
54
55   @param is             The data as it is
56   @param is_len         The length of is
57   @param should         The data as it should
58   @param should_len     The length of should
59   @param what           The type of the data
60   @param which          The iteration count
61   @return 0 on equality, -1 or 1 on difference
62 */
63 int compare_testvector(const void* is, const unsigned long is_len, const void* should, const unsigned long should_len, const char* what, int which)
64 {
65    int res = 0;
66    if(is_len != should_len)
67       res = is_len > should_len ? -1 : 1;
68    else
69       res = XMEMCMP(is, should, is_len);
70
71 #if defined(LTC_TEST) && defined(LTC_TEST_DBG)
72    if (res != 0) {
73       fprintf(stderr, "Testvector #%i of %s failed:\n", which, what);
74       _print_hex("SHOULD", should, should_len);
75       _print_hex("IS    ", is, is_len);
76    }
77 #else
78    LTC_UNUSED_PARAM(which);
79    LTC_UNUSED_PARAM(what);
80 #endif
81
82    return res;
83 }
84
85 /* ref:         $Format:%D$ */
86 /* git commit:  $Format:%H$ */
87 /* commit time: $Format:%ai$ */