xref: /freebsd/share/examples/libusb20/util.c (revision f39bffc62c1395bde25d152c7f68fdf7cbaab414)
1 /* ----------------------------------------------------------------------------
2  * "THE BEER-WARE LICENSE" (Revision 42) (by Poul-Henning Kamp):
3  * <joerg@FreeBSD.ORG> wrote this file.  As long as you retain this notice you
4  * can do whatever you want with this stuff. If we meet some day, and you think
5  * this stuff is worth it, you can buy me a beer in return.        Joerg Wunsch
6  * ----------------------------------------------------------------------------
7  *
8  * $FreeBSD$
9  */
10 
11 /*
12  * Helper functions common to all examples
13  */
14 
15 #include <stdio.h>
16 #include <stdint.h>
17 #include <stdlib.h>
18 
19 #include <libusb20.h>
20 #include <libusb20_desc.h>
21 
22 #include "util.h"
23 
24 /*
25  * Print "len" bytes from "buf" in hex, followed by an ASCII
26  * representation (somewhat resembling the output of hd(1)).
27  */
28 void
29 print_formatted(uint8_t *buf, uint32_t len)
30 {
31   int i, j;
32 
33   for (j = 0; j < len; j += 16)
34     {
35       printf("%02x: ", j);
36 
37       for (i = 0; i < 16 && i + j < len; i++)
38 	printf("%02x ", buf[i + j]);
39       printf("  ");
40       for (i = 0; i < 16 && i + j < len; i++)
41 	{
42 	  uint8_t c = buf[i + j];
43 	  if(c >= ' ' && c <= '~')
44 	    printf("%c", (char)c);
45 	  else
46 	    putchar('.');
47 	}
48       putchar('\n');
49     }
50 }
51