summaryrefslogtreecommitdiff
path: root/src/common/print.c
blob: 69703c660a58c63b1a974865787c1e781c5dfcc6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*  Copyright (C) 2011 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <config.h>
#include <stdio.h>
#include <ctype.h>
#include "common/print.h"

void array_printf(const uint8_t *data, const unsigned length,
                  printf_t print_handler, const char type)
{
	for (unsigned i = 0; i < length; i++) {
		uint8_t ch = data[i];

		switch (type) {
		case 't':
			if (isprint(ch) != 0) {
				print_handler("%c  ", ch);
			} else {
				print_handler("   ");
			}
			break;
		case 'x':
			print_handler("%02X ", ch);
			break;
		default:
			print_handler("0x%02X ", ch);
		}
	}
	print_handler("\n");
}

void hex_print(const uint8_t *data, unsigned length)
{
	array_printf(data, length, &printf, 0);
}

void short_hex_print(const uint8_t *data, unsigned length)
{
	array_printf(data, length, &printf, 'x');
}

void txt_print(const uint8_t *data, unsigned length)
{
	array_printf(data, length, &printf, 't');
}

void bit_printf(const uint8_t *data, unsigned length, printf_t print_handler)
{
	unsigned char mask = 0x01;
	unsigned ptr;
	int bit = 0;
	for (ptr = 0; ptr < length; ptr++) {
		for (bit = 7; bit >= 0; bit--) {
			if ((mask << bit) & (unsigned char)*(data + ptr)) {
				print_handler("1");
			} else {
				print_handler("0");
			}
		}
		print_handler(" ");
	}
	print_handler("\n");
}

void bit_print(const uint8_t *data, unsigned length)
{
	bit_printf(data, length, &printf);
}

float time_diff(struct timeval *begin, struct timeval *end)
{
	return (end->tv_sec - begin->tv_sec) * 1000 +
	       (end->tv_usec - begin->tv_usec) / 1000.0;

}