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