]> git.meshlink.io Git - catta/blob - src/util.c
move public headers into their own directory
[catta] / src / util.c
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/malloc.h>
31 #include "util.h"
32
33 void avahi_hexdump(const void* p, size_t size) {
34     const uint8_t *c = p;
35     assert(p);
36
37     printf("Dumping %lu bytes from %p:\n", (unsigned long) size, p);
38
39     while (size > 0) {
40         unsigned i;
41
42         for (i = 0; i < 16; i++) {
43             if (i < size)
44                 printf("%02x ", c[i]);
45             else
46                 printf("   ");
47         }
48
49         for (i = 0; i < 16; i++) {
50             if (i < size)
51                 printf("%c", c[i] >= 32 && c[i] < 127 ? c[i] : '.');
52             else
53                 printf(" ");
54         }
55
56         printf("\n");
57
58         c += 16;
59
60         if (size <= 16)
61             break;
62
63         size -= 16;
64     }
65 }
66
67 char *avahi_format_mac_address(char *r, size_t l, const uint8_t* mac, size_t size) {
68     char *t = r;
69     unsigned i;
70     static const char hex[] = "0123456789abcdef";
71
72     assert(r);
73     assert(l > 0);
74     assert(mac);
75
76     if (size <= 0) {
77         *r = 0;
78         return r;
79     }
80
81     for (i = 0; i < size; i++) {
82         if (l < 3)
83             break;
84
85         *(t++) = hex[*mac >> 4];
86         *(t++) = hex[*mac & 0xF];
87         *(t++) = ':';
88
89         l -= 3;
90
91         mac++;
92     }
93
94     if (t > r)
95         *(t-1) = 0;
96     else
97         *r = 0;
98
99     return r;
100 }
101
102 char *avahi_strup(char *s) {
103     char *c;
104     assert(s);
105
106     for (c = s; *c; c++)
107         *c = (char) toupper(*c);
108
109     return s;
110 }
111
112 char *avahi_strdown(char *s) {
113     char *c;
114     assert(s);
115
116     for (c = s; *c; c++)
117         *c = (char) tolower(*c);
118
119     return s;
120 }