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