/src/avahi/avahi-core/util.c
Line | Count | Source (jump to first uncovered line) |
1 | | /*** |
2 | | This file is part of avahi. |
3 | | |
4 | | avahi is free software; you can redistribute it and/or modify it |
5 | | under the terms of the GNU Lesser General Public License as |
6 | | published by the Free Software Foundation; either version 2.1 of the |
7 | | License, or (at your option) any later version. |
8 | | |
9 | | avahi is distributed in the hope that it will be useful, but WITHOUT |
10 | | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
11 | | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General |
12 | | Public License for more details. |
13 | | |
14 | | You should have received a copy of the GNU Lesser General Public |
15 | | License along with avahi; if not, write to the Free Software |
16 | | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
17 | | USA. |
18 | | ***/ |
19 | | |
20 | | #ifdef HAVE_CONFIG_H |
21 | | #include <config.h> |
22 | | #endif |
23 | | |
24 | | #include <string.h> |
25 | | #include <stdio.h> |
26 | | #include <stdlib.h> |
27 | | #include <assert.h> |
28 | | #include <ctype.h> |
29 | | |
30 | | #include <avahi-common/malloc.h> |
31 | | #include "util.h" |
32 | | |
33 | 0 | void avahi_hexdump(const void* p, size_t size) { |
34 | 0 | const uint8_t *c = p; |
35 | 0 | assert(p); |
36 | | |
37 | 0 | printf("Dumping %lu bytes from %p:\n", (unsigned long) size, p); |
38 | |
|
39 | 0 | while (size > 0) { |
40 | 0 | unsigned i; |
41 | |
|
42 | 0 | for (i = 0; i < 16; i++) { |
43 | 0 | if (i < size) |
44 | 0 | printf("%02x ", c[i]); |
45 | 0 | else |
46 | 0 | printf(" "); |
47 | 0 | } |
48 | |
|
49 | 0 | for (i = 0; i < 16; i++) { |
50 | 0 | if (i < size) |
51 | 0 | printf("%c", c[i] >= 32 && c[i] < 127 ? c[i] : '.'); |
52 | 0 | else |
53 | 0 | printf(" "); |
54 | 0 | } |
55 | |
|
56 | 0 | printf("\n"); |
57 | |
|
58 | 0 | c += 16; |
59 | |
|
60 | 0 | if (size <= 16) |
61 | 0 | break; |
62 | | |
63 | 0 | size -= 16; |
64 | 0 | } |
65 | 0 | } |
66 | | |
67 | 0 | char *avahi_format_mac_address(char *r, size_t l, const uint8_t* mac, size_t size) { |
68 | 0 | char *t = r; |
69 | 0 | unsigned i; |
70 | 0 | static const char hex[] = "0123456789abcdef"; |
71 | |
|
72 | 0 | assert(r); |
73 | 0 | assert(l > 0); |
74 | 0 | assert(mac); |
75 | | |
76 | 0 | if (size <= 0) { |
77 | 0 | *r = 0; |
78 | 0 | return r; |
79 | 0 | } |
80 | | |
81 | 0 | for (i = 0; i < size; i++) { |
82 | 0 | if (l < 3) |
83 | 0 | break; |
84 | | |
85 | 0 | *(t++) = hex[*mac >> 4]; |
86 | 0 | *(t++) = hex[*mac & 0xF]; |
87 | 0 | *(t++) = ':'; |
88 | |
|
89 | 0 | l -= 3; |
90 | |
|
91 | 0 | mac++; |
92 | 0 | } |
93 | |
|
94 | 0 | if (t > r) |
95 | 0 | *(t-1) = 0; |
96 | 0 | else |
97 | 0 | *r = 0; |
98 | |
|
99 | 0 | return r; |
100 | 0 | } |
101 | | |
102 | 0 | char *avahi_strup(char *s) { |
103 | 0 | char *c; |
104 | 0 | assert(s); |
105 | | |
106 | 0 | for (c = s; *c; c++) |
107 | 0 | *c = (char) toupper(*c); |
108 | |
|
109 | 0 | return s; |
110 | 0 | } |
111 | | |
112 | 0 | char *avahi_strdown(char *s) { |
113 | 0 | char *c; |
114 | 0 | assert(s); |
115 | | |
116 | 0 | for (c = s; *c; c++) |
117 | 0 | *c = (char) tolower(*c); |
118 | |
|
119 | 0 | return s; |
120 | 0 | } |