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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
/*
* Copyright (c) 2003-2017 Cavium Inc. (support@cavium.com). All rights
* reserved.
* Copyright 2018-present Facebook, Inc.
*
* SPDX-License-Identifier: BSD-3-Clause
*
* string.c: hastily cobbled-together string functions
*/
#include <assert.h>
#include <ctype.h>
#include <string.h>
#include <lame_string.h>
static int char_to_val(char c)
{
if (c >= '0' && c <= '9') /* digits */
return c - '0';
if (c >= 'A' && c <= 'F') /* uppercase */
return c - 'A' + 10;
if (c >= 'a' && c <= 'f') /* lowercase */
return c - 'a' + 10;
return -1;
}
unsigned long long int strtoull(const char *nptr, char **endptr, int base)
{
unsigned long long int val;
size_t i, error = 0;
/* TODO: enforce lameness of this API for now... */
assert((base == 0) || (base == 16) || base == 10);
if (!nptr)
return 0;
/* Trim whitespace */
for (i = 0; i < strlen(nptr); i++)
if (nptr[i] != ' ')
break;
if (base == 0) {
/* Autodetect base */
if (strlen(&nptr[i]) >= 2 && ((nptr[i] == '0') &&
((nptr[i + 1] == 'x') || (nptr[i + 1] == 'X')))) {
base = 16;
i += 2; /* start loop after prefix */
} else
base = 10;
}
val = 0;
for (; i < strlen(nptr); i++) {
if (base == 16) {
if (!isxdigit(nptr[i])) {
if (*endptr)
*endptr = (char *)&nptr[i];
error = 1;
break;
}
} else {
if (!isdigit(nptr[i])) {
if (*endptr)
*endptr = (char *)&nptr[i];
error = 1;
break;
}
}
val *= base;
val += char_to_val(nptr[i]);
}
if (error) {
printk(BIOS_ERR, "Failed to convert string '%s', base %d to "
"int\n", nptr, base);
return 0;
}
return val;
}
unsigned long int strtoul(const char *nptr, char **endptr, int base)
{
unsigned long long int u = strtol(nptr, endptr, base);
/* FIXME: check for overflow (u > max) */
return (unsigned long int)u;
}
long int strtol(const char *nptr, char **endptr, int base)
{
unsigned long long int u;
int is_neg = 0;
const char *p;
long int ret;
if (nptr[0] == '-') {
is_neg = 1;
p = &nptr[1];
} else {
p = &nptr[0];
}
u = strtoull(p, NULL, base);
/* FIXME: check for overflow (u > max) */
if (is_neg)
ret = 0 - (long int)u;
else
ret = (long int)u;
return ret;
}
long long int strtoll(const char *nptr, char **endptr, int base)
{
unsigned long long int u;
int is_neg = 0;
const char *p;
long long int ret;
if (nptr[0] == '-') {
is_neg = 1;
p = &nptr[1];
} else {
p = &nptr[0];
}
u = strtoull(p, NULL, base);
/* FIXME: check for overflow (sign-bit set) */
if (is_neg)
ret = 0 - (long long int)u;
else
ret = (long long int)u;
return ret;
}
/* FIXME: replace sscanf() usage for bdk_config_get_int. returns number of
* strings converted, so 1 if successful and 0 if not */
int str_to_int(const char *str, int64_t *val)
{
*val = strtol(str, NULL, 10);
return 1;
}
/* FIXME: replace sscanf() usage for bdk_config_get_int. returns number of
* strings converted, so 1 if successful and 0 if not */
int str_to_hex(const char *str, int64_t *val)
{
*val = strtol(str, NULL, 16);
return 1;
}
|