summaryrefslogtreecommitdiff
path: root/gsmdecode/src/common.c
blob: decea53c1809bd72f412de61ab2b76cdd4172cb4 (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
/*
 */

#include "common.h"
#include <stdio.h>
#include <string.h>

void
hexdump(const unsigned char *data, size_t len)
{
        size_t n = 0;
	int line = 0;

	if (!len)
		return;

	printf("%03x: ", line++);
	while (1)
	{
		printf("%2.2x ", data[n++]);
		if (n >= len)
			break;
		if ((n % 8 == 0) && (n % 16 != 0))
			printf(" - "); 
		if (n % 16 == 0)
			printf("\n%03x: ", line++);
	}
	printf("\n");
}

#ifndef HAVE_STRLCPY
/*           
 * bsd'sh strlcpy().
 * The strlcpy() function copies up to size-1 characters from the
 * NUL-terminated string src to dst, NUL-terminating the result.
 * Return: total length of the string tried to create.
 */
size_t
strlcpy(char *dst, const char *src, size_t size)
{
        size_t len = strlen(src);
        size_t ret = len;

        if (size <= 0)
		return 0;
	if (len >= size)
		len = size - 1;
	memcpy(dst, src, len);
	dst[len] = 0;

	return ret;
}
#endif

/*
 * Debuggging...
 * Convert an interger to a bit string and output it.
 * Most significatn bit first.
 */
char *
int2bit(unsigned int val)
{
	static char buf[33 + 3];
	char *ptr = buf;
	unsigned int i = 0x1 << 31;
	int round = 0;

	while (i > 0)
	{

		if (val & i)
			*ptr++ = '1';
		else
			*ptr++ = '0';

		i = i >> 1;

		if ((++round % 8 == 0) && (i > 0))
			*ptr++ = '.';
	}

	*ptr = '\0';

	return buf;
}

personal git repositories of Harald Welte. Your mileage may vary