11 #include "ceptr_error.h"
13 void hexDump(
char *desc,
void *addr,
int len) {
15 unsigned char buff[17];
16 unsigned char *pc = addr;
20 printf(
"%s:\n", desc);
22 for (i = 0; i < len; i++) {
28 printf(
" %s\n", buff);
35 printf(
" %02x", pc[i]);
38 if ((pc[i] < 0x20) || (pc[i] > 0x7e))
42 buff[(i % 16) + 1] =
'\0';
46 int strcicmp(
char const *a,
char const *b)
49 int d = tolower(*a) - tolower(*b);
55 void writeFile(
char *fn,
void *data,
size_t size) {
60 fprintf(stderr,
"Can't open output file %s!\n",fn);
63 fwrite(data, 1,size, ofp);
68 void *readFile(
char *fn,
size_t *size) {
74 fd = open(fn, O_RDONLY);
76 raise_error(
"unable to open: %s",fn);
79 if ((fstat(fd, &stbuf) != 0) || (!S_ISREG(stbuf.st_mode))) {
81 raise_error(
"not a regular file: %s",fn);
84 file_size = stbuf.st_size;
85 if (size) *size = file_size;
87 buffer = malloc(file_size+1);
90 raise_error(
"unable to allocate enough memory for contents of: %s",fn);
92 ssize_t bytes_read = read(fd,buffer,file_size);
93 if (bytes_read == -1) {
96 raise_error(
"error reading %s: %d",fn,errno);
103 uint64_t diff_micro(
struct timespec *start,
struct timespec *end)
106 return ((end->tv_sec * (1000000)) + (end->tv_nsec / 1000)) -
107 ((start->tv_sec * 1000000) + (start->tv_nsec / 1000));
110 #define MS_PER_NANO_SECOND 1000000L // 1 millisecond = 1,000,000 Nanoseconds
112 void sleepms(
long milliseconds) {
114 ts.tv_sec = milliseconds/1000;
115 ts.tv_nsec = (milliseconds-(ts.tv_sec*1000))*MS_PER_NANO_SECOND;
119 void bin_to_strhex(
unsigned char *bin,
unsigned int binsz,
char **result)
121 char hex_str[]=
"0123456789abcdef";
124 *result = (
char *)malloc(binsz * 2 + 1);
125 (*result)[binsz * 2] = 0;
130 for (i = 0; i < binsz; i++)
132 (*result)[i * 2 + 0] = hex_str[(bin[i] >> 4) & 0x0F];
133 (*result)[i * 2 + 1] = hex_str[(bin[i] ) & 0x0F];