Blob


1 /*
2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include "compat.h"
19 #include <ctype.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
24 #include "kamid.h"
25 #include "log.h"
26 #include "utils.h"
28 void *
29 xmalloc(size_t size)
30 {
31 void *r;
33 if ((r = malloc(size)) == NULL)
34 fatal("malloc");
35 return r;
36 }
38 void *
39 xcalloc(size_t nmemb, size_t size)
40 {
41 void *r;
43 if ((r = calloc(nmemb, size)) == NULL)
44 fatal("calloc");
45 return r;
46 }
48 char *
49 xstrdup(const char *s)
50 {
51 char *r;
53 if ((r = strdup(s)) == NULL)
54 fatal("strdup");
55 return r;
56 }
58 void *
59 xmemdup(const void *d, size_t len)
60 {
61 void *r;
63 if ((r = malloc(len)) == NULL)
64 fatal("malloc");
65 memcpy(r, d, len);
66 return r;
67 }
69 const char *
70 pp_msg_type(uint8_t type)
71 {
72 switch (type) {
73 case Tversion: return "Tversion";
74 case Rversion: return "Rversion";
75 case Tauth: return "Tauth";
76 case Rauth: return "Rauth";
77 case Tattach: return "Tattach";
78 case Rattach: return "Rattach";
79 case Terror: return "Terror"; /* illegal */
80 case Rerror: return "Rerror";
81 case Tflush: return "Tflush";
82 case Rflush: return "Rflush";
83 case Twalk: return "Twalk";
84 case Rwalk: return "Rwalk";
85 case Topen: return "Topen";
86 case Ropen: return "Ropen";
87 case Tcreate: return "Tcreate";
88 case Rcreate: return "Rcreate";
89 case Tread: return "Tread";
90 case Rread: return "Rread";
91 case Twrite: return "Twrite";
92 case Rwrite: return "Rwrite";
93 case Tclunk: return "Tclunk";
94 case Rclunk: return "Rclunk";
95 case Tremove: return "Tremove";
96 case Rremove: return "Rremove";
97 case Tstat: return "Tstat";
98 case Rstat: return "Rstat";
99 case Twstat: return "Twstat";
100 case Rwstat: return "Rwstat";
101 default: return "unknown";
105 static void
106 hexdump_ppline(int x, uint8_t *data, size_t len)
108 for (; x < 50; x++)
109 printf(" ");
111 printf("|");
113 for (x = 0; x < (int)len; ++x) {
114 if (isgraph(data[x]))
115 printf("%c", data[x]);
116 else
117 printf(".");
120 printf("|\n");
123 void
124 hexdump(const char *label, uint8_t *data, size_t len)
126 size_t i;
127 int x, n;
129 /*
130 * Layout:
131 * === first block === == second block == |........|\n
132 * first and second block are 8 bytes long (for a total of 48
133 * columns), plus two separator plus two | plus 16 chars, for
134 * a total of 68 characters.
135 */
137 printf("\nhexdump \"%s\": (%zu bytes)\n", label, len);
138 for (x = 0, n = 0, i = 0; i < len; ++i) {
139 if (i != 0 && i % 8 == 0) {
140 printf(" ");
141 x++;
144 if (n == 16) {
145 hexdump_ppline(x, &data[i - 16], 16);
146 x = 0;
147 n = 0;
150 printf("%02x ", data[i]);
151 x += 3;
152 n++;
155 if (n != 0)
156 hexdump_ppline(x, &data[i - n], n);
158 printf("\n");