File: parse.c

package info (click to toggle)
lowpan-tools 0.3-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,940 kB
  • ctags: 957
  • sloc: sh: 11,792; ansic: 2,825; python: 262; yacc: 162; lex: 83; makefile: 83
file content (61 lines) | stat: -rw-r--r-- 1,493 bytes parent folder | download | duplicates (2)
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
/*
 * Linux IEEE 802.15.4 userspace tools
 *
 * Copyright (C) 2008, 2009 Sismens AG
 *
 * Written-by: Dmitry Eremin-Solenikov
 * Written-by: Sergey Lapin
 *
 *  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; version 2 of the License.
 *
 *  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, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <libcommon.h>
#include <errno.h>
#include <stdio.h>

int parse_hw_addr(const char *hw, unsigned char *buf) {
	int i = 0;

	while (*hw) {
		unsigned char c = *(hw++);
		switch (c) {
			case '0'...'9':
				c -= '0';
				break;
			case 'a'...'f':
				c -= 'a' - 10;
				break;
			case 'A'...'F':
				c -= 'A' - 10;
				break;
			case ':':
			case '.':
				continue;
			default:
				fprintf(stderr, "Bad HW address encountered (%c)\n", c);
				return -EINVAL;
		}
		buf[i / 2] = (buf[i/2] & (0xf << (4 * (i % 2)))) | (c << 4 * (1 -i % 2));

		i++;
		if (i == 16)
			return 0;
	}

	return -EINVAL;
}