1 /* Copyright (C) 2018 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
2
3 This program is free software: you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation, either version 3 of the License, or
6 (at your option) any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program. If not, see <https://www.gnu.org/licenses/>.
15 */
16
17 #pragma once
18
19 #include <assert.h>
20 #include <errno.h>
21 #include <inttypes.h>
22 #include <limits.h>
23 #include <stddef.h>
24 #include <stdint.h>
25
26 #include "libknot/errcode.h"
27 #include "contrib/ctype.h"
28
intmax_from_str(const char * src,intmax_t * dst,intmax_t min,intmax_t max)29 inline static int intmax_from_str(const char *src, intmax_t *dst,
30 intmax_t min, intmax_t max)
31 {
32 if (!is_digit(*src) && *src != '-' && *src != '+') {
33 return KNOT_EINVAL;
34 }
35
36 errno = 0;
37 char *end = NULL;
38 intmax_t result = strtoimax(src, &end, 10);
39
40 if (errno == ERANGE) {
41 return KNOT_ERANGE;
42 }
43
44 if (src == end || *end != '\0') {
45 return KNOT_EINVAL;
46 }
47
48 if (result < min || result > max) {
49 return KNOT_ERANGE;
50 }
51
52 *dst = result;
53 return KNOT_EOK;
54 }
55
uintmax_from_str(const char * src,uintmax_t * dst,uintmax_t min,uintmax_t max)56 inline static int uintmax_from_str(const char *src, uintmax_t *dst,
57 uintmax_t min, uintmax_t max)
58 {
59 if (!is_digit(*src) && *src != '+') {
60 return KNOT_EINVAL;
61 }
62
63 errno = 0;
64 char *end = NULL;
65 uintmax_t result = strtoumax(src, &end, 10);
66
67 if (errno == ERANGE) {
68 return KNOT_ERANGE;
69 }
70
71 if (src == end || *end != '\0') {
72 return KNOT_EINVAL;
73 }
74
75 if (result < min || result > max) {
76 return KNOT_ERANGE;
77 }
78
79 *dst = result;
80 return KNOT_EOK;
81 }
82
83 #define CONVERT(prefix, type, min, max, src, dst) \
84 { \
85 assert(src && dst); \
86 prefix##max_t value; \
87 int result = prefix##max_from_str(src, &value, min, max); \
88 if (result != KNOT_EOK) { \
89 return result; \
90 } \
91 *dst = (type)value; \
92 return KNOT_EOK; \
93 }
94
str_to_int(const char * src,int * dst,int min,int max)95 inline static int str_to_int(const char *src, int *dst, int min, int max)
96 {
97 CONVERT(int, int, min, max, src, dst);
98 }
99
str_to_u8(const char * src,uint8_t * dst)100 inline static int str_to_u8(const char *src, uint8_t *dst)
101 {
102 CONVERT(uint, uint8_t, 0, UINT8_MAX, src, dst);
103 }
104
str_to_u16(const char * src,uint16_t * dst)105 inline static int str_to_u16(const char *src, uint16_t *dst)
106 {
107 CONVERT(uint, uint16_t, 0, UINT16_MAX, src, dst);
108 }
109
str_to_u32(const char * src,uint32_t * dst)110 inline static int str_to_u32(const char *src, uint32_t *dst)
111 {
112 CONVERT(uint, uint32_t, 0, UINT32_MAX, src, dst);
113 }
114
str_to_u64(const char * src,uint64_t * dst)115 inline static int str_to_u64(const char *src, uint64_t *dst)
116 {
117 CONVERT(uint, uint64_t, 0, UINT64_MAX, src, dst);
118 }
119
str_to_size(const char * src,size_t * dst,size_t min,size_t max)120 inline static int str_to_size(const char *src, size_t *dst, size_t min, size_t max)
121 {
122 CONVERT(uint, size_t, min, max, src, dst);
123 }
124
125 #undef CONVERT
126