blob: b255405d0ba81a9f5778e812162b13c0a9ea9855 [file] [log] [blame]
NingSun0c89b3c2018-02-08 08:34:03 -08001/* This code was taken from http://www.fourmilab.ch/random/ where it states that:
2
3 This software is in the public domain. Permission to use, copy, modify, and distribute
4 this software and its documentation for any purpose and without fee is hereby granted,
5 without any conditions or restrictions. This software is provided “as is” without
6 express or implied warranty. */
7
8/*
9 ENT -- Entropy calculation and analysis of putative
10 random sequences.
11
12 Designed and implemented by John "Random" Walker in May 1985.
13
14 Multiple analyses of random sequences added in December 1985.
15
16 Bit stream analysis added in September 1997.
17
18 Terse mode output, getopt() command line processing,
19 optional stdin input, and HTML documentation added in
20 October 1998.
21
22 Documentation for the -t (terse output) option added
23 in July 2006.
24
25 Replaced table look-up for chi square to probability
26 conversion with algorithmic computation in January 2008.
27
28 For additional information and the latest version,
29 see http://www.fourmilab.ch/random/
30
31*/
32
33#include <stdio.h>
34#include <string.h>
35#include <ctype.h>
36#include <math.h>
37#ifdef _WIN32
38#include <fcntl.h>
39#include <io.h>
40#else
41#include <unistd.h>
42#endif
43
44#include "iso8859.h"
45#include "randtest.h"
46
47#define UPDATE "January 28th, 2008"
48
49#define FALSE 0
50#define TRUE 1
51
52#ifdef M_PI
53#define PI M_PI
54#else
55#define PI 3.14159265358979323846
56#endif
57
58extern double pochisq(const double ax, const int df);
59
60/* Main program */
61
62void doEnt
63(
64 unsigned char* data,
65 size_t len,
66 double* pEntropy,
67 double* pChiProbability,
68 double* pArithMean,
69 double* pMontePi,
70 double* pSerialCorrelation
71)
72{
73 size_t s;
74 long ccount[256]; /* Bins to count occurrences of values */
75 double montepi, chip,
76 scc, ent, mean, chisq;
77
78 /* Initialise for calculations */
79
80 rt_init(FALSE);
81
82 /* Scan input file and count character occurrences */
83
84 for (s = 0; s < len; s++)
85 {
86 unsigned char ocb = data[s];
87
88 ccount[ocb]++; /* Update counter for this bin */
89 rt_add(&ocb, 1);
90 }
91
92 /* Complete calculation and return sequence metrics */
93
94 rt_end(&ent, &chisq, &mean, &montepi, &scc);
95
96 /* Calculate probability of observed distribution occurring from
97 the results of the Chi-Square test */
98
99 chip = pochisq(chisq, 255);
100
101 /* Print bin counts if requested */
102
103 /* Return calculated results */
104
105 *pEntropy = ent;
106 *pChiProbability = chip;
107 *pArithMean = mean;
108 *pMontePi = montepi;
109 *pSerialCorrelation = scc;
110}