This repository has been archived by the owner on Mar 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
util.c
268 lines (244 loc) · 5.38 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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
//
// util.c
// cdos
//
// Created by Andre Zay on 17.12.2017.
// Copyright © 2017 Andre Zay. All rights reserved.
//
#include "util.h"
#include "message.h"
#include <stdio.h>
#include <math.h>
#include <unistd.h>
#include <sys/types.h>
static uint32_t Q[4096], c = 362436;
void init_rand(uint32_t x)//cwmc
{
int i;
Q[0] = x;
Q[1] = x + PHI;
Q[2] = x + PHI + PHI;
for (i = 3; i < 4096; i++)
{
Q[i] = Q[i - 3] ^ Q[i - 2] ^ PHI ^ i;
}
}
int randport()
{
return rand() % 65534 + 1;
}
char randchar()
{
return rand() % 126 + 1;
}
char* randstring(size_t sz)
{
char* s = (char*)malloc(sz * sizeof(char));
for (int i = 0; i < sz; i++) {
s[i] = randchar();
}
return s;
}
int randrange(int start, int end)
{
assert(start <= end);
return rand() % (start + 1 - end) + start;
}
uint32_t rand_cmwc(void)
{
uint64_t t, a = 18782LL;
static uint32_t i = 4095;
uint32_t x, r = 0xfffffffe;
i = (i + 1) & 4095;
t = a * Q[i] + c;
c = (t >> 32);
x = (uint32_t)t + c;
if (x < c) {
x++;
c++;
}
return (Q[i] = r - x);
}
const char* getarg(const char arg[2], const char* argv[], int argc)
{
for (int i = 0; i < argc; i++) {
#ifdef DEBUG
printf("%s ", argv[i]);
#endif
if (!strcmp(arg, argv[i])) {
if (i < argc-1) {
return argv[i + 1];
}else{
die("Argument required for key:%s",arg);
}
}
}
return NULL;
}
const char* getlarg(const char* arg, const char* argv[], int argc)
{
for (int i = 0; i < argc; i++) {
#ifdef DEBUG
printf("%s ", argv[i]);
#endif
if (!strcmp(arg, argv[i])) {
if (i < argc-1) {
return argv[i + 1];
}else{
die("Argument required for key:%s",arg);
}
}
}
return NULL;
}
bool checkarg(const char arg[2], const char* argv[], int argc)
{
for (int i = 0; i < argc; i++) {
if (!strcmp(argv[i], arg)) {
return true;
}
}
return false;
}
bool checklarg(const char* arg, const char* argv[], int argc)
{
for (int i = 0; i < argc; i++) {
if (!strcmp(argv[i], arg)) {
return true;
}
}
return false;
}
char* readfile(char* filename)
{
char* buffer = 0;
long length;
FILE* f = fopen(filename, "rb");
if (f) {
fseek(f, 0, SEEK_END);
length = ftell(f);
fseek(f, 0, SEEK_SET);
buffer = malloc(length);
if (buffer) {
fread(buffer, 1, length, f);
}
fclose(f);
}
return buffer;
}
void sleep_ms(int milliseconds)
{
struct timespec ts;
ts.tv_sec = milliseconds / 1000;
ts.tv_nsec = (milliseconds % 1000) * 1000000;
nanosleep(&ts, NULL);
}
char* dtoa(double x){//double to char*
char *s=(char*)malloc(sizeof(x)+1);
sprintf(s,"%0.1f",x);
return s;
}
char* bytes2mb(int64_t bcount){
return strcat(dtoa(bcount/pow(1024.0,2)), "Mb");
}
char* bytes2any(int64_t bcount,uint8_t type){
return dtoa(bcount/pow(1024.0,2));
}
char* metrics2str(uint8_t type){
if(type==SIZE_BYTES){
return "B";
}else if(type==SIZE_KB){
return "Kb";
}else if(type==SIZE_MB){
return "Mb";
}else if(type==SIZE_GB){
return "Gb";
}else{
return "Tb";
}
}
uint8_t str2metrics(char* metrics){
if(strcmp(metrics, "b")==0){
return SIZE_BYTES;
}else if(strcmp(metrics, "Kb")==0){
return SIZE_KB;
}else if(strcmp(metrics, "Mb")==0){
return SIZE_MB;
}else if(strcmp(metrics, "Gb")==0){
return SIZE_GB;
}else if(strcmp(metrics, "Tb")==0){
return SIZE_TB;
}else{
return -1;
}
}
const char* sgetarg(const char arg[2], const char* argv[], int argc,const char* _default){
if(checkarg(arg, argv, argc)){
return getarg(arg, argv, argc);
}else{
return _default;
}
}
const char* sgetlarg(const char *arg, const char* argv[], int argc,const char* _default){
if(checklarg(arg, argv, argc)){
return getlarg(arg, argv, argc);
}else{
return _default;
}
}
bool is_root(void){
if(geteuid() != 0)
{
return false;
}else{
return true;
}
}
/*Cycled string list implementation*/
slist* create_slist(){
slist* _new=(slist*)malloc(sizeof(slist));
_new->first=NULL;
_new->last=NULL;
_new->length=0;
return _new;
}
void add_slist(slist* this,char* x){
_node* _new=(_node*)malloc(sizeof(_node));
_new->next=this->first;
_new->val=(char*)malloc(strlen(x)*sizeof(char));
strcpy(_new->val, x);
if(this->length==0){
_new->next=_new;
this->first=_new;
this->last=_new;
}else{
assert(this->last!=NULL);
this->last->next=_new;
this->last=_new;
}
this->length++;
}
void free_slist(slist *this){//frees all list
_node *cur=this->first;
_node *_first=this->first;
while(cur->next!=_first){
_node* _cur=cur->next;
free(cur);
cur=_cur;
}
free(cur);
free(this);
}
_node* nth_slist(slist* this,uint64_t n){
_node* cur=this->first;
for(int i=0;i<n;i++){
cur=cur->next;
}
return cur;
}
/*End cyclic list implementations*/
void assert_root(void){
if(!is_root()){
die("Root privlligies requeired to run this type of attack");
}
}