-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathutils.c
113 lines (89 loc) · 1.69 KB
/
utils.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
#include "utils.h"
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#define LOG_DEGBUG 0
#define LOG_WARNING 1
#define LOG_ERROR 2
#define BUFF_MAX 1024*4
int get_longest_line(char *buff)
{
int longest_len=0, current_len=0;
char *c = buff;
while(c++)
{
current_len++;
if(*c=='\n')
{
if(current_len > longest_len)
{
longest_len = current_len;
current_len=0;
}
}
}
return longest_len;
}
// abbadoned function
int print_in_frame(char *format, ...)
{
va_list args;
va_start(args, format);
char buff[BUFF_MAX];
int ret;
ret = vsnprintf(buff, BUFF_MAX, format, args);
va_end(args);
if(ret > BUFF_MAX)
{
return -1;
}
int line_len = get_longest_line(buff) + 2;
// compose bufffer
char new_buff[BUFF_MAX*4];
char *cur_pos = new_buff;
char border_pixel = '*';
memset(new_buff, border_pixel, line_len);
cur_pos += line_len;
}
void logs(int log_level, char *format, ...)
{
va_list args;
va_start(args, format);
switch(log_level)
{
case LOG_DEGBUG:
fprintf(stderr, "\033[92m[+] ");
break;
case LOG_WARNING:
fprintf(stderr, "\033[93m[*] ");
break;
case LOG_ERROR:
fprintf(stderr, "\033[91m[!] ");
break;
}
vfprintf(stderr, format, args);
fprintf(stderr, "\033[0m\n");
va_end(args);
}
void print_binary(unsigned int v) {
unsigned int mask = ~(~(unsigned int) 0 >> 1U);
while (mask) {
putchar('0'+!!(v&mask));
mask >>= 1U;
}
putchar('\n');
}
void hex_dump(unsigned char *addres, unsigned int len)
{
printf("[0]\t");
for(int i=0;i<len;i++)
{
printf("%02x ",addres[i]);
if((i+1)%8==0)
{
if((i+1)%16==0)printf("\n[+%d]\t",i+1);
else printf(" ");
}
}
printf("\n");
}