File: tools.cc

package info (click to toggle)
ht 2.0.20-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 6,324 kB
  • sloc: cpp: 97,563; ansic: 17,183; sh: 3,811; lex: 226; makefile: 213; yacc: 127
file content (184 lines) | stat: -rw-r--r-- 3,498 bytes parent folder | download | duplicates (8)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
 *	HT Editor
 *	tools.cc
 *
 *	Copyright (C) 1999-2002 Sebastian Biallas (sb@biallas.net)
 *
 *	This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License version 2 as
 *	published by the Free Software Foundation.
 *
 *	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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "tools.h"

#include "data.h"
#include "htdebug.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

uint32 delinearize(uint32 d)
{
	return d*0x8088405+1;	/* there's magic in here... */
}

uint64 delinearize64(uint64 d)
{
	return (uint64(delinearize(d))<<32)| delinearize(d>>32); /* there's less magic in here... */
}

int compare_keys_uint_delinear(Object *key_a, Object *key_b)
{
	uint a = delinearize(((UInt*)key_a)->value);
	uint b = delinearize(((UInt*)key_b)->value);
	if (a>b) return 1; else if (a<b) return -1;
	return 0;
}

int *random_permutation(int max)
{
	if (!max) return NULL;
	int *table= ht_malloc(max * sizeof(int));
	int i,j,k,l,m;
	for (i=0; i<max; i++) table[i] = i;
	for (i=0; i<max; i++) {
		k=rand()%(max);
		l=rand()%(max);
		m=rand()%(max);
		j=table[k];
		table[k]=table[l];
		table[l]=j;
		j=table[i];
		table[i]=table[m];
		table[m]=j;
	}
	return table;
}

/*
 *	entropy shit
 */
#define LN2 0.693147180559945309417232121458177

double calc_entropy(byte *buf, int size)
{
	int p[256];
	if (!size) return 0.0;
	memset(p, 0, sizeof p);
	for (int i=0; i<size; i++) {
		p[*buf]++;
		buf++;
	}
	double result = 0.0;
	for (int i=0; i<256; i++) {
		if (p[i]) {
			double pi = p[i];
			pi /= size;
			result += pi * log(pi) / LN2;
		}
	}
	return -result;
}

/*
 *	buffer size must be 64 bytes
 *	return value will be in range from 0 to 100
 */
int calc_entropy2(byte *buf, int size)
{
	int p[256];
	int result = 0;
	if (size<3) return 0;
	memset(p, 0, sizeof p);
	// pass1
	byte *b = buf;
	for (int i=0; i < size; i++) {
		if (p[*b]++ == 0) {
			result ++;
		}
		b++;
	}
	memset(p, 0, sizeof p);
	b = buf;
	// pass2
	size--;
	for (int i=0; i < size; i++) {
		int d = b[0]-b[1];
		if (d<0) d = -d;
		if (p[d]++ == 0) {
			result ++;
		}
		b++;
	}
	memset(p, 0, sizeof p);
	b = buf;
	// pass3
	size--;
	for (int i=0; i < size; i++) {
		int d = b[0]-b[1];
		int e = b[1]-b[2];
		if (d<0) d = -d;
		if (e<0) e = -e;
		d = d-e;
		if (d<0) d = -d;
		if (p[d]++ == 0) {
			result ++;
		}
		b++;
	}
	return (result-3)*100/(size*3+3);
}

/*
 * "out of memory" - handlers
 */

int out_of_memory(int size)
{
	printf("Out of memory.");
	exit(1);
	return OUT_OF_MEMORY_FAIL;
}

int (*out_of_memory_func)(int size)=&out_of_memory;

void *smalloc(size_t size)
{
	void *p;
retry:
	if ((p=malloc(size))) return p;
	switch (out_of_memory_func(size)) {
		case OUT_OF_MEMORY_IGNORE:
			return NULL;
		case OUT_OF_MEMORY_RETRY:
			goto retry;
		default:
			exit(666);
	}
}

void *smalloc0(size_t size)
{
	void *p;
retry0:
	if ((p=malloc(size))) return memset(p, 0, size);
	switch (out_of_memory_func(size)) {
		case OUT_OF_MEMORY_IGNORE:
			return NULL;
		case OUT_OF_MEMORY_RETRY:
			goto retry0;
		default:
			exit(666);
	}
}