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
|
#include <sys/time.h>
#include <netinet/in.h>
#include <time.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "common.h"
#ifdef DEBUG
void
debugp (char *filename, const char *str, ...)
{
FILE *fp; /* temporary file pointer */
va_list vl;
fp = fopen (filename, "a");
if (fp == NULL)
return;
va_start (vl, str);
vfprintf (fp, str, vl);
va_end (vl);
fclose (fp);
return;
}
void
hexdump (char *filename, unsigned char *data, unsigned int amount)
{
FILE *fp; /* temporary file pointer */
unsigned int dp, p; /* data pointer */
const char trans[] =
"................................ !\"#$%&'()*+,-./0123456789"
":;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklm"
"nopqrstuvwxyz{|}~...................................."
"....................................................."
"........................................";
fp = fopen (filename, "a");
if (fp == NULL)
return;
fprintf (fp, "\n-packet-\n");
for (dp = 1; dp <= amount; dp++) {
fprintf (fp, "%02x ", data[dp-1]);
if ((dp % 8) == 0)
fprintf (fp, " ");
if ((dp % 16) == 0) {
fprintf (fp, "| ");
p = dp;
for (dp -= 16; dp < p; dp++)
fprintf (fp, "%c", trans[data[dp]]);
fflush (fp);
fprintf (fp, "\n");
}
fflush (fp);
}
if ((amount % 16) != 0) {
p = dp = 16 - (amount % 16);
for (dp = p; dp > 0; dp--) {
fprintf (fp, " ");
if (((dp % 8) == 0) && (p != 8))
fprintf (fp, " ");
fflush (fp);
}
fprintf (fp, " | ");
for (dp = (amount - (16 - p)); dp < amount; dp++)
fprintf (fp, "%c", trans[data[dp]]);
fflush (fp);
}
fprintf (fp, "\n");
fclose (fp);
return;
}
#endif
void *
xrealloc (void *m_ptr, size_t newsize)
{
void *n_ptr;
n_ptr = realloc (m_ptr, newsize);
if (n_ptr == NULL && newsize != 0) {
fprintf (stderr, "realloc failed\n");
assert (0);
exit (EXIT_FAILURE);
}
return (n_ptr);
}
char *
xstrdup (char *str)
{
char *b;
b = strdup (str);
if (b == NULL) {
fprintf (stderr, "strdup failed\n");
exit (EXIT_FAILURE);
}
return (b);
}
void *
xcalloc (int factor, size_t size)
{
void *bla;
bla = calloc (factor, size);
if (bla == NULL) {
fprintf (stderr, "no memory left\n");
assert (0);
exit (EXIT_FAILURE);
}
return (bla);
}
unsigned int
array_compaction (void *arr_ptr, unsigned int el_count)
{
unsigned int sidx, /* source index */
didx; /* destination index */
char ** arr = (char **) arr_ptr;
for (sidx = 0, didx = 0 ; sidx < el_count ; ++sidx) {
if (arr[sidx] == NULL)
continue;
arr[didx] = arr[sidx];
didx += 1;
}
return (didx);
}
|