-
Notifications
You must be signed in to change notification settings - Fork 55
/
common.c
151 lines (123 loc) · 2.4 KB
/
common.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
#include <stdarg.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "common.h"
extern struct options_t options;
void mem_assign(
uint8_t *buf,
const size_t buf_sz,
const uint64_t val,
const size_t val_sz)
{
if (val_sz != 1 && val_sz != 2 && val_sz != 4 && val_sz != 8) {
fprintf(stderr, "%s: val_sz must be 1, 2, 4, or 8\n", __func__);
exit(EXIT_FAILURE);
}
if ((buf_sz % val_sz) != 0) {
fprintf(stderr, "%s: buf_sz (%zu) must be multiple of val_sz (%zu)\n", __func__, buf_sz, val_sz);
exit(EXIT_FAILURE);
}
if (val_sz == 1)
memset(buf, val, buf_sz);
else
for (uint64_t i = 0; i < buf_sz; i += val_sz)
memcpy(buf + i, &val, val_sz);
}
void* xmalloc(
const size_t n)
{
void *ptr = malloc(n);
if (ptr) return ptr;
perror("malloc");
abort();
}
void* xrealloc(
void *ptr,
const size_t n)
{
void *p = realloc(ptr, n);
if (p) return p;
perror("realloc");
abort();
}
const
size_t read_data(
const int fd,
uint8_t *const buf,
const size_t buf_sz)
{
size_t len = 0;
while (len < buf_sz) {
const ssize_t ret = read(fd, buf + len, buf_sz - len);
if (ret < 0) {
perror("read");
exit(EXIT_FAILURE);
}
if (ret == 0) break;
len += ret;
}
return len;
}
void write_data(
const int fd,
const uint8_t *const buf,
const size_t sz)
{
size_t written;
for (written = 0; written < sz;) {
const ssize_t ret = write(fd, buf + written, sz - written);
if (ret < 0) {
perror("write");
exit(EXIT_FAILURE);
}
if (ret == 0) break;
written += ret;
}
}
// Should this be a macro?
void verbose_printf(
const char *const fmt,
...)
{
if (!options.verbose) return;
va_list args;
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
}
void verbose_dump(
const uint8_t *const buf,
const size_t sz,
const unsigned long long base)
{
if (!options.verbose) return;
dump(buf, sz, base);
}
void dump(
const uint8_t *const buf,
const size_t sz,
const unsigned long base)
{
for (size_t i = 0; i < sz; i += 0x10) {
if (base != -1) printf(REGFMT ": ", base + i);
for (size_t j = i; j < (i + 0x10); j++) {
if (j < sz)
printf("%02x ", buf[j]);
else
printf(" ");
}
printf("\t");
for (size_t j = i; j < (i + 0x10) && j < sz; j++) {
if (j < sz) {
if (buf[j] > 0x1f && buf[j] < 0x7f)
printf("%c", buf[j]);
else
printf(".");
}
}
printf("\n");
}
}