ceptr
 All Data Structures Files Functions Variables Typedefs Macros Modules Pages
util.c
Go to the documentation of this file.
1 
5 #include "util.h"
6 #include <fcntl.h>
7 #include <unistd.h>
8 #include <errno.h>
9 #include <sys/stat.h>
10 #include <stdlib.h>
11 #include "ceptr_error.h"
12 
13 void hexDump(char *desc, void *addr, int len) {
14  int i;
15  unsigned char buff[17];
16  unsigned char *pc = addr;
17 
18  // Output description if given.
19  if (desc != NULL)
20  printf("%s:\n", desc);
21  // Process every byte in the data.
22  for (i = 0; i < len; i++) {
23  // Multiple of 16 means new line (with line offset).
24 
25  if ((i % 16) == 0) {
26  // Just don't print ASCII for the zeroth line.
27  if (i != 0)
28  printf(" %s\n", buff);
29 
30  // Output the offset.
31  printf(" %04x ", i);
32  }
33 
34  // Now the hex code for the specific character.
35  printf(" %02x", pc[i]);
36 
37  // And store a printable ASCII character for later.
38  if ((pc[i] < 0x20) || (pc[i] > 0x7e))
39  buff[i % 16] = '.';
40  else
41  buff[i % 16] = pc[i];
42  buff[(i % 16) + 1] = '\0';
43  }
44 }
45 
46 int strcicmp(char const *a, char const *b)
47 {
48  for (;; a++, b++) {
49  int d = tolower(*a) - tolower(*b);
50  if (d != 0 || !*a)
51  return d;
52  }
53 }
54 
55 void writeFile(char *fn,void *data,size_t size) {
56  FILE *ofp;
57 
58  ofp = fopen(fn, "w");
59  if (ofp == NULL) {
60  fprintf(stderr, "Can't open output file %s!\n",fn);
61  }
62  else {
63  fwrite(data, 1,size, ofp);
64  fclose(ofp);
65  }
66 }
67 
68 void *readFile(char *fn,size_t *size) {
69  off_t file_size;
70  char *buffer;
71  struct stat stbuf;
72  int fd;
73 
74  fd = open(fn, O_RDONLY);
75  if (fd == -1) {
76  raise_error("unable to open: %s",fn);
77  }
78 
79  if ((fstat(fd, &stbuf) != 0) || (!S_ISREG(stbuf.st_mode))) {
80  close(fd);
81  raise_error("not a regular file: %s",fn);
82  }
83 
84  file_size = stbuf.st_size;
85  if (size) *size = file_size;
86 
87  buffer = malloc(file_size+1);
88  if (buffer == NULL) {
89  close(fd);
90  raise_error("unable to allocate enough memory for contents of: %s",fn);
91  }
92  ssize_t bytes_read = read(fd,buffer,file_size);
93  if (bytes_read == -1) {
94  close(fd);
95  free(buffer);
96  raise_error("error reading %s: %d",fn,errno);
97  }
98  close(fd);
99  buffer[file_size]=0;
100  return buffer;
101 }
102 
103 uint64_t diff_micro(struct timespec *start, struct timespec *end)
104 {
105  /* us */
106  return ((end->tv_sec * (1000000)) + (end->tv_nsec / 1000)) -
107  ((start->tv_sec * 1000000) + (start->tv_nsec / 1000));
108 }
109 
110 #define MS_PER_NANO_SECOND 1000000L // 1 millisecond = 1,000,000 Nanoseconds
111 
112 void sleepms(long milliseconds) {
113  struct timespec ts;
114  ts.tv_sec = milliseconds/1000;
115  ts.tv_nsec = (milliseconds-(ts.tv_sec*1000))*MS_PER_NANO_SECOND;
116  nanosleep(&ts,NULL);
117 }
118 
119 void bin_to_strhex(unsigned char *bin, unsigned int binsz, char **result)
120 {
121  char hex_str[]= "0123456789abcdef";
122  unsigned int i;
123 
124  *result = (char *)malloc(binsz * 2 + 1);
125  (*result)[binsz * 2] = 0;
126 
127  if (!binsz)
128  return;
129 
130  for (i = 0; i < binsz; i++)
131  {
132  (*result)[i * 2 + 0] = hex_str[(bin[i] >> 4) & 0x0F];
133  (*result)[i * 2 + 1] = hex_str[(bin[i] ) & 0x0F];
134  }
135 }