-
Notifications
You must be signed in to change notification settings - Fork 257
/
Copy pathgnb_nodeid.c
110 lines (83 loc) · 2.49 KB
/
gnb_nodeid.c
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
Copyright (C) gnbdev
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; either version 3 of the License, or
(at your option) any later version.
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, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <inttypes.h>
#include <errno.h>
#include "gnb_nodeid.h"
gnb_uuid_t gnb_str2nodeid(char *nodeidstr){
gnb_uuid_t nodeid;
char *endptr;
char *p;
int is_hex = 0;
int i;
p = nodeidstr;
if ( '$' == *nodeidstr ) {
is_hex = 1;
p = nodeidstr+1;
goto convert_begin;
}
if ( '0' == *nodeidstr && ( 'x' == *(nodeidstr+1) || 'X' == *(nodeidstr+1) ) ) {
is_hex = 1;
p = nodeidstr+2;
goto convert_begin;
}
for ( i=0; i<16; i++ ) {
if ( *p == '\0' ) {
break;
}
if ( (*p >='A' && *p <='F') || (*p >='a' && *p <='f') ) {
is_hex = 1;
p = nodeidstr;
goto convert_begin;
}
p++;
}
convert_begin:
if ( is_hex ) {
nodeid = strtoull(p, &endptr, 16);
} else {
nodeid = strtoull(nodeidstr, &endptr, 10);
}
if ( ERANGE == errno ) {
nodeid = 0xFFFFFFFFFFFFFFFF;
goto convert_end;
}
if ( endptr == p ) {
nodeid = 0xFFFFFFFFFFFFFFFF;
goto convert_end;
}
convert_end:
return nodeid;
}
char *gnb_nodeid2str(gnb_uuid_t nodeid, char *nodeidstr, int fmt){
#if 0
char buf[GNB_MAX_NODEID_STRING_SIZE];
switch (fmt) {
case GNB_NODEIS2STR_FMT_DEC|GNB_NODEIS2STR_FMT_HEX:
snprintf(nodeidstr, GNB_MAX_NODEID_STRING_SIZE, "%016"PRIX64"(%"PRIu64")", nodeid, nodeid);
break;
case GNB_NODEIS2STR_FMT_DEC:
snprintf(nodeidstr, GNB_MAX_NODEID_STRING_SIZE, "%"PRIu64"", nodeid);
break;
case GNB_NODEIS2STR_FMT_HEX:
snprintf(nodeidstr, GNB_MAX_NODEID_STRING_SIZE, "%016"PRIX64"", nodeid);
break;
default:
break;
}
#endif
return nodeidstr;
}