Debian Codegraph @

src/OVAL/probes/SEAP/generic/strto.c

raw symbols SCIP
1 /*
2 * Copyright 2009 Red Hat Inc., Durham, North Carolina.
3 * All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * "Daniel Kopecek" <[email protected]>
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <string.h>
28 #include <stdlib.h>
29 #include <errno.h>
30 #include <stdint.h>
31
32 #include "strto.h"
33
34 uint8_t strto_uint8_hex (const char *str, size_t len, char **endptr)
35 {
36 uint64_t r = strto_uint64(str, len, endptr, 16);
37
38 if (errno != 0 && r <= UINT16_MAX)
39 return (uint8_t)r;
40 else if (r > UINT8_MAX) {
41 errno = ERANGE;
42 return UINT8_MAX;
43 } else
44 return (uint8_t)r;
45 }
46
47 uint16_t strto_uint16_hex(const char *str, size_t len, char **endptr)
48 {
49 uint64_t r = strto_uint64(str, len, endptr, 16);
50
51 if (errno != 0 && r <= UINT16_MAX)
52 return (uint16_t)r;
53 else if (r > UINT16_MAX) {
54 errno = ERANGE;
55 return UINT16_MAX;
56 } else
57 return (uint16_t)r;
58 }
59
60 uint32_t strto_uint32_hex(const char *str, size_t len, char **endptr)
61 {
62 uint64_t r = strto_uint64(str, len, endptr, 16);
63
64 if (errno != 0 && r <= UINT32_MAX)
65 return (uint32_t)r;
66 else if (r > UINT32_MAX) {
67 errno = ERANGE;
68 return UINT32_MAX;
69 } else
70 return (uint32_t)r;
71 }
72
73 int64_t strto_int64 (const char *str, size_t len, char **endptr, int base)
74 {
75 int errno_copy = 0;
76 int64_t result = 0;
77 char *null_str = calloc(len + 1, sizeof(*str));
78
79 memcpy(null_str, str, len);
80 errno = 0;
81 result = strtoll(null_str, endptr, base);
82 errno_copy = errno;
83 free(null_str);
84 errno = errno_copy;
85
86 return result;
87 }
88
89 uint64_t strto_uint64 (const char *str, size_t len, char **endptr, int base)
90 {
91 int errno_copy = 0;
92 int64_t result = 0;
93 char *null_str = calloc(len + 1, sizeof(*str));
94
95 memcpy(null_str, str, len);
96 errno = 0;
97 result = strtoull(null_str, endptr, base);
98 errno_copy = errno;
99 free(null_str);
100 errno = errno_copy;
101
102 return result;
103 }
104
105 double strto_double (const char *str, size_t len, char **endptr)
106 {
107 int errno_copy = 0;
108 int64_t result = 0;
109 char *null_str = calloc(len + 1, sizeof(*str));
110
111 memcpy(null_str, str, len);
112 errno = 0;
113 result = strtod(null_str, endptr);
114 errno_copy = errno;
115 free(null_str);
116 errno = errno_copy;
117
118 return result;
119 }