-
Notifications
You must be signed in to change notification settings - Fork 0
/
utility.c
145 lines (124 loc) · 2.91 KB
/
utility.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
#include <stdint.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/uio.h>
#include <unistd.h>
#include <stdbool.h>
#include <stdarg.h>
#include "utility.h"
char *load_buffer_from_file(const char *path, size_t *out_size)
{
int fd = open(path, O_RDONLY);
if (fd == -1) {
fprintf(stderr, "failed to open(2) file %s: %s\n", path, strerror(errno));
exit(-1);
}
struct stat sb;
if (fstat(fd, &sb) != 0) {
fprintf(stderr, "failed to fstat(2) %s: %s\n", path, strerror(errno));
exit(-1);
}
off_t size = sb.st_size;
char *buf = calloc(1, size);
if (buf == NULL) {
fprintf(stderr, "failed to allocate input buffer\n");
exit(-1);
}
if (read(fd, buf, size) != size) {
fprintf(stderr, "failed to read %lld bytes from %s: %s\n", size, path, strerror(errno));
exit(-1);
}
close(fd);
if (out_size) {
*out_size = size;
}
return buf;
}
void foreach_line_in_file(const char *path, void (^handler)(const char *, size_t, int))
{
FILE *input = fopen(path, "r");
if (input == NULL) {
fprintf(stderr, "failed to open input '%s': %s\n", path, strerror(errno));
exit(-1);
}
char *line = NULL;
size_t linecap = 0;
ssize_t linelen = 0;
int index = 0;
while ((linelen = getline(&line, &linecap, input)) > 0) {
handler(line, linelen, index);
index++;
}
free(line);
fclose(input);
}
size_t memcmp_where(const char *lhs, const char *rhs, size_t size)
{
for (size_t i = 0; i < size; i++) {
if (lhs[i] != rhs[i]) {
return i;
}
}
return -1;
}
void dump_hex_fd(FILE *filedes, const char *string, size_t len)
{
for (size_t i = 0; i < len; i++) {
if (i % 16 == 0 && i > 0) {
fprintf(filedes, "\n");
}
fprintf(filedes, "%02x", (unsigned char)string[i]);
}
fprintf(filedes, "\n");
}
void dump_hex(const char *string, size_t len)
{
dump_hex_fd(stdout, string, len);
}
void dump_hex_label(FILE *filedes, const char *label, const char *string, size_t len)
{
fprintf(filedes, "%s:\t", label);
dump_hex_fd(filedes, string, len);
}
static void print_color(bool success, bool newline, const char *format, va_list ap)
{
int color = 31;
FILE *outfd = stderr;
if (success) {
color = 32;
outfd = stdout;
}
fprintf(outfd, "\033[0;%dm", color);
vfprintf(outfd, format, ap);
fprintf(outfd, "\033[0m");
if (newline) {
fprintf(outfd, "\n");
}
}
void print_success(const char *format, ...)
{
va_list ap;
va_start(ap, format);
print_color(true, true, format, ap);
va_end(ap);
}
void _print_fail(const char *format, ...)
{
va_list(ap);
va_start(ap, format);
print_color(false, false, format, ap);
va_end(ap);
}
void generate_random_string(char *buf, size_t len)
{
const char *printables = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()";
for (size_t i = 0; i < len; i++) {
uint32_t printable = arc4random_uniform(strlen(printables));
buf[i] = printables[printable];
}
}