File: test.c

package info (click to toggle)
libdisorder 0.0.2%2Bgit20130809.8062ee1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 224 kB
  • sloc: ansic: 287; makefile: 56; sh: 15
file content (98 lines) | stat: -rw-r--r-- 2,564 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/***************************************************************************
 *  libdisorder: A Library for Measuring Byte Stream Entropy
 *  Copyright (C) 2010 Michael E. Locasto
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the:
 *       Free Software Foundation, Inc.
 *       59 Temple Place, Suite 330
 *       Boston, MA  02111-1307  USA
 *
 * $Id$
 **************************************************************************/

#include "../include/disorder.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#define LIMIT   LIBDO_BUFFER_LEN

/**
 * Open and read /dev/urandom, filling a buffer of size LIMIT. After
 * filing the buffer, calculate the entropy of the buffer. Repeat
 * forever.
 *
 */
int
main(int argc, 
     char* argv[])
{
  float entropy_value = 0.0;
  long long current_run = 0L;
  char* buffer = NULL;
  unsigned char c;
  int x = 0;
  FILE* fin=NULL;
  char* itr=NULL;
  int i = 0;

  fin = fopen("/dev/urandom","r");
  if(NULL==fin)
  {
    perror("main():");
    return -1;
  }

  buffer = (char*)calloc(LIMIT, sizeof(char));
  if(NULL==buffer)
  {
    perror("main():");
    return -2;
  }

  for(;;)
  {
    itr = buffer;
    for(i=0;i<LIMIT;i++)
    {
      x = fgetc(fin);
      c = (unsigned char)x;
      *itr=c;
      itr++;
    }
    //NB: doing this caluculation here may allow /dev/unrandom to
    //recover sufficiently to never show a decrease in entropy.
    //Using a larger buffer or using another thread to perform this
    //calculation while the main thread continues to fill the buffer
    //may provide the ability to observe a decrease in entropy.
    entropy_value = shannon_H(buffer, LIMIT);
    fprintf(stdout,
	    "[%8lld] entropy: %f maxent: %f ratio: %f\n",
	    current_run,
	    entropy_value,
	    get_max_entropy(),
	    get_entropy_ratio()
	    );
    current_run++;
    fflush(stdout);
  }

  free(buffer);
  buffer=NULL;
  if(EOF!=fclose(fin))
    perror("exiting main():");
  fin=NULL;

  return 0;
}