xref: /illumos-gate/usr/src/cmd/smbsrv/test-crypt/utils.c (revision 7dbbfe7762f9eabac3999ee1a8b38311d428f7a8)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2021 Tintri by DDN, Inc. All rights reserved.
14  */
15 
16 #include <stdio.h>
17 #include <strings.h>
18 #include <sys/cmn_err.h>
19 #include <sys/varargs.h>
20 
21 #include "utils.h"
22 
23 void
24 hexdump(const uchar_t *buf, int len)
25 {
26 	int idx;
27 	char ascii[24];
28 	char *pa = ascii;
29 
30 	bzero(ascii, sizeof (ascii));
31 
32 	idx = 0;
33 	while (len--) {
34 		if ((idx & 15) == 0) {
35 			printf("%04X: ", idx);
36 			pa = ascii;
37 		}
38 		if (*buf > ' ' && *buf <= '~')
39 			*pa++ = *buf;
40 		else
41 			*pa++ = '.';
42 		printf("%02x ", *buf++);
43 
44 		idx++;
45 		if ((idx & 3) == 0) {
46 			*pa++ = ' ';
47 			(void) putchar(' ');
48 		}
49 		if ((idx & 15) == 0) {
50 			*pa = '\0';
51 			printf("%s\n", ascii);
52 		}
53 	}
54 
55 	if ((idx & 15) != 0) {
56 		*pa = '\0';
57 		/* column align the last ascii row */
58 		while ((idx & 15) != 0) {
59 			if ((idx & 3) == 0)
60 				(void) putchar(' ');
61 			printf("   ");
62 			idx++;
63 		}
64 		printf("%s\n", ascii);
65 	}
66 }
67 
68 /*
69  * Provide a real function (one that prints something) to replace
70  * the stub in libfakekernel.  This prints cmn_err() messages.
71  */
72 void
73 fakekernel_putlog(char *msg, size_t len, int flags)
74 {
75 
76 	(void) fwrite(msg, 1, len, stdout);
77 	(void) fflush(stdout);
78 }
79 
80 /*
81  * Build a UIO for the input or output buffer
82  * Arbitrarily splits into 2 segs for testing uio.
83  */
84 void
85 make_uio(void *buf, size_t buflen, uio_t *uio, iovec_t *iov, int iovmax)
86 {
87 	size_t maxseg = 512;
88 	size_t tlen;
89 	int i;
90 
91 	bzero(uio, sizeof (*uio));
92 	uio->uio_resid = buflen;
93 	uio->uio_segflg = UIO_SYSSPACE;
94 
95 	for (i = 0; i < iovmax; i++) {
96 		if (buflen <= 0)
97 			break;
98 		tlen = buflen;
99 		if (tlen > maxseg)
100 			tlen = maxseg;
101 		iov[i].iov_base = buf;
102 		iov[i].iov_len = tlen;
103 		buf += tlen;
104 		buflen -= tlen;
105 	}
106 
107 	uio->uio_iov = iov;
108 	uio->uio_iovcnt = i;
109 }
110