-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.c
106 lines (87 loc) · 2.22 KB
/
util.c
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
#include "util.h"
// get 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
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
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
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
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;
}
// copy a chunk of index.html to buf
// return 0 if we could not do it
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;
}