-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.c
More file actions
135 lines (116 loc) · 3.08 KB
/
util.c
File metadata and controls
135 lines (116 loc) · 3.08 KB
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
#include "util.h"
//
// get_bit - return a single bit from the given array
// array: pointer to the bit array
// bitno: bit offset to retrieve
// returns: 0 or 1 depending on the value of the bit
//
int get_bit ( unsigned char *array, int bitno )
{
int off = bitno / 8;
int bit = bitno % 8;
int mask = 1<<bit;
return array[off] & mask ? 1 : 0;
}
//
// clr_bit - clear a single bit in the array
// array: pointer to the bit array
// bitno: bit offset to clear
//
void clr_bit ( unsigned char *array, int bitno )
{
int off = bitno / 8;
int bit = bitno % 8;
int mask = 1<<bit;
array[off] &= ~mask;
}
//
// set_bit - set or clear a bit in the array
// array: pointer to the bit array
// bitno: bit offset to modify
// val : value to write (0 or 1)
//
void set_bit ( unsigned char *array, int bitno, int val )
{
int off = bitno / 8;
int bit = bitno % 8;
int mask = 1<<bit;
if ( val ) array[off] |= mask;
}
//
// set_dibit - store a two bit value into the array
// array: pointer to the dibit array
// bitno: dibit offset to modify
// val : value to store (0 or 1)
//
void set_dibit ( unsigned char *array, int bitno, int val )
{
int off = bitno / 4;
int bit = bitno % 4;
int mask = 1<<(bit*2);
if ( val ) array[off] |= mask;
}
//
// get_dibit - fetch a two bit value from the array
// array: pointer to the dibit array
// bitno: dibit offset to read
// returns: 0 or 1
//
int get_dibit ( unsigned char *array, int bitno )
{
int off = bitno / 4;
int bit = bitno % 4;
int mask = 1<<(bit*2);
return array[off] & mask ? 1 : 0;
}
//
// get_index_html - copy random data from index.html into buf
// buf : output buffer
// cnt : number of bytes to copy
// xsubi : seed for nrand48 used to randomize file offsets
// returns 1 on success, 0 on failure
//
int get_index_html ( char *buf, int cnt, unsigned short *xsubi )
{
static int fd = -1;
int sts;
static int msg_flag = 1;
static int first_random_place = 1;
int random_place;
if ( -1 == fd ) {
char *dibit_base;
fd = open ( "index.html", O_RDONLY );
if ( fd < 0 && (dibit_base = getenv("DIBIT_BASE")) != NULL ) {
char index_html_name [ 256 ];
sprintf( index_html_name, "%s/%s", dibit_base, "index.html" );
fd = open ( index_html_name , O_RDONLY );
}
}
if ( fd < 0 ) {
if ( msg_flag ) {
printf("To increase randomness of the nrand48 function,\n"
"(used to generate salt), create index.html in this directory.\n");
printf("Example: sh index.sh\n");
msg_flag = 0;
}
memset(buf,0,cnt);
return 0;
}
// go to some random place
if ( first_random_place ) {
struct stat sb;
fstat(fd,&sb);
first_random_place = 0;
random_place = nrand48(xsubi) % sb.st_size;
rw(lseek,fd,random_place,SEEK_SET);
}
sts = rw(read, fd, buf, cnt );
if ( sts < cnt ) {
// we could not get it all
// just reset to the front of the file and read again
if ( trace_flag ) printf("%s: resetting to front of file\n",__FUNCTION__);
rw(lseek,fd,0,SEEK_SET);
rw(read, fd, &buf[sts] , cnt - sts );
}
return 1;
}