File: ropy.c

package info (click to toggle)
libdisorder 0.0.2%2Bgit20130809.8062ee1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 220 kB
  • sloc: ansic: 287; makefile: 54; sh: 15
file content (154 lines) | stat: -rw-r--r-- 3,631 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/***************************************************************************
 *  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 <sys/stat.h> // to get file size in bytes for buffer
#include <string.h>
#include <stdio.h>
#include <stdlib.h>


static void
do_usage()
{
  fprintf(stderr,
	  "ropy [filename]\n");
  fprintf(stderr,
	  "ropy -v [filename]\n");
}

/**
 * Open and read a file, filling a buffer. After filing the buffer,
 * calculate the entropy of the buffer. Output the entropy of the file
 * in bits.
 *
 * An attacker could cause this program to read in a large number of
 * bytes or allocate a large amount of memory by supplying a large
 * file.
 * 
 * Usage:
 *  ./ropy [-v] [file]
 */
int
main(int argc, 
     char* argv[])
{
  float entropy_value = 0.0;
  char* filename = NULL;
  int fildes = -1;
  FILE* fin=NULL;
  struct stat stat_fd;
  long long file_size;
  char* buffer = NULL;
  unsigned char c;
  int x = 0;
  char* itr=NULL;
  //long long i = 0;
  long long how_many_bytes_read = 0L;
  int ret = 0;
  int verbose = 0; //be quiet by default

  if(2==argc)
  {
    //point filename to argv[1]; no copying.
    filename = argv[1];
  }else if(3==argc){
    if(0==strncmp(argv[1],"-v",2))
    {
      verbose = 1;
      filename = argv[2];
    }else{
      do_usage();
    }
  }else if(1==argc){
    //assume stdin is being piped to us
    //how to handle fstat in this case?
  }else{
    do_usage();
    return -1;
  }

  fin = fopen(filename, "r");
  if(NULL==fin)
  {
    perror("main(): problem opening file");
    return -1;
  }

  fildes = fileno(fin);

  ret = fstat(fildes, &stat_fd);
  if(-1==ret)
  {
    perror("main(): problem invoking fstat() of file");
  }

  file_size = (long long)stat_fd.st_size;

  if(verbose)
    fprintf(stdout,
	    "file size: %lld (bytes)\n",
	    file_size);

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

  //read the file into the buffer
  itr = buffer;
  //for(i=0;i<file_size;i++) //could also read until EOF
  while(EOF!=(x=fgetc(fin)))
  {
    //x = fgetc(fin);
    c = (unsigned char)x;
    *itr = c;
    itr++;
    how_many_bytes_read++;
  }
  if(verbose)
    fprintf(stdout,
	    "read %lld bytes\n",
	    how_many_bytes_read);

  entropy_value = shannon_H(buffer, how_many_bytes_read);
  fprintf(stdout,
	  "tokens: %d entropy: %f metric: %f maxent: %f ratio: %f\n",
	  get_num_tokens(),
	  entropy_value,
          entropy_value/how_many_bytes_read,
	  get_max_entropy(),
	  get_entropy_ratio()
	  );

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

  return 0;
}