Commit ca6285fd authored by Daniel Salzman's avatar Daniel Salzman

utils: move strtonum to contrib

parent 46a9ce0c
......@@ -51,6 +51,7 @@ src/contrib/sockaddr.c
src/contrib/sockaddr.h
src/contrib/string.c
src/contrib/string.h
src/contrib/strtonum.h
src/contrib/time.h
src/contrib/tolower.h
src/contrib/trim.h
......@@ -157,7 +158,6 @@ src/dnssec/shared/pem.c
src/dnssec/shared/pem.h
src/dnssec/shared/print.h
src/dnssec/shared/shared.h
src/dnssec/shared/strtonum.h
src/dnssec/shared/timestamp.c
src/dnssec/shared/timestamp.h
src/dnssec/shared/wire.h
......@@ -184,7 +184,6 @@ src/dnssec/tests/sample_keys.h
src/dnssec/tests/shared_base32hex.c
src/dnssec/tests/shared_bignum.c
src/dnssec/tests/shared_dname.c
src/dnssec/tests/shared_strtonum.c
src/dnssec/tests/shared_timestamp.c
src/dnssec/tests/shared_wire.c
src/dnssec/tests/sign.c
......@@ -234,6 +233,18 @@ src/knot/dnssec/zone-nsec.c
src/knot/dnssec/zone-nsec.h
src/knot/dnssec/zone-sign.c
src/knot/dnssec/zone-sign.h
src/knot/events/events.c
src/knot/events/events.h
src/knot/events/handlers.h
src/knot/events/handlers/dnssec.c
src/knot/events/handlers/expire.c
src/knot/events/handlers/flush.c
src/knot/events/handlers/load.c
src/knot/events/handlers/notify.c
src/knot/events/handlers/refresh.c
src/knot/events/handlers/update.c
src/knot/events/replan.c
src/knot/events/replan.h
src/knot/modules/dnsproxy.c
src/knot/modules/dnsproxy.h
src/knot/modules/dnstap.c
......@@ -249,20 +260,17 @@ src/knot/modules/synth_record.c
src/knot/modules/synth_record.h
src/knot/nameserver/axfr.c
src/knot/nameserver/axfr.h
src/knot/nameserver/capture.c
src/knot/nameserver/capture.h
src/knot/nameserver/chaos.c
src/knot/nameserver/chaos.h
src/knot/nameserver/internet.c
src/knot/nameserver/internet.h
src/knot/nameserver/ixfr.c
src/knot/nameserver/ixfr.h
src/knot/nameserver/log.h
src/knot/nameserver/notify.c
src/knot/nameserver/notify.h
src/knot/nameserver/nsec_proofs.c
src/knot/nameserver/nsec_proofs.h
src/knot/nameserver/process_answer.c
src/knot/nameserver/process_answer.h
src/knot/nameserver/process_query.c
src/knot/nameserver/process_query.h
src/knot/nameserver/query_module.c
......@@ -271,6 +279,14 @@ src/knot/nameserver/tsig_ctx.c
src/knot/nameserver/tsig_ctx.h
src/knot/nameserver/update.c
src/knot/nameserver/update.h
src/knot/query/capture.c
src/knot/query/capture.h
src/knot/query/layer.c
src/knot/query/layer.h
src/knot/query/query.c
src/knot/query/query.h
src/knot/query/requestor.c
src/knot/query/requestor.h
src/knot/server/dthreads.c
src/knot/server/dthreads.h
src/knot/server/journal.c
......@@ -301,12 +317,6 @@ src/knot/worker/queue.c
src/knot/worker/queue.h
src/knot/zone/contents.c
src/knot/zone/contents.h
src/knot/zone/events/events.c
src/knot/zone/events/events.h
src/knot/zone/events/handlers.c
src/knot/zone/events/handlers.h
src/knot/zone/events/replan.c
src/knot/zone/events/replan.h
src/knot/zone/node.c
src/knot/zone/node.h
src/knot/zone/semantic-check.c
......@@ -362,12 +372,6 @@ src/libknot/packet/rrset-wire.c
src/libknot/packet/rrset-wire.h
src/libknot/packet/wire.c
src/libknot/packet/wire.h
src/libknot/processing/layer.c
src/libknot/processing/layer.h
src/libknot/processing/overlay.c
src/libknot/processing/overlay.h
src/libknot/processing/requestor.c
src/libknot/processing/requestor.h
src/libknot/rdata.c
src/libknot/rdata.h
src/libknot/rdataset.c
......@@ -429,7 +433,6 @@ src/utils/common/resolv.c
src/utils/common/resolv.h
src/utils/common/sign.c
src/utils/common/sign.h
src/utils/common/strtonum.h
src/utils/common/token.c
src/utils/common/token.h
src/utils/kdig/kdig_exec.c
......@@ -509,6 +512,7 @@ tests/conf.c
tests/conf_tools.c
tests/confdb.c
tests/confio.c
tests/contrib/test_strtonum.c
tests/control.c
tests/db.c
tests/descriptor.c
......@@ -528,7 +532,6 @@ tests/modules/online_sign.c
tests/net.c
tests/net_shortwrite.c
tests/node.c
tests/overlay.c
tests/pkt.c
tests/process_answer.c
tests/process_query.c
......
libtap
src
src/contrib
src/dnssec
src/dnssec/lib
src/dnssec/lib/dnssec
src/dnssec/shared
......
......@@ -59,6 +59,7 @@ libcontrib_la_SOURCES = \
contrib/sockaddr.h \
contrib/string.c \
contrib/string.h \
contrib/strtonum.h \
contrib/time.h \
contrib/tolower.h \
contrib/trim.h \
......@@ -204,7 +205,6 @@ libknotus_la_SOURCES = \
utils/common/resolv.h \
utils/common/sign.c \
utils/common/sign.h \
utils/common/strtonum.h \
utils/common/token.c \
utils/common/token.h
......
/* Copyright (C) 2014 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
/* Copyright (C) 2016 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
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
......@@ -20,15 +20,14 @@
#include <inttypes.h>
#include <limits.h>
#include <stdint.h>
#include <stdlib.h>
#include <ctype.h>
#include "error.h"
#include "libknot/errcode.h"
inline static int intmax_from_str(const char *src, intmax_t *dest)
{
if (!isdigit((int)*src) && *src != '-' && *src != '+') {
return DNSSEC_MALFORMED_DATA;
return KNOT_EINVAL;
}
errno = 0;
......@@ -36,21 +35,21 @@ inline static int intmax_from_str(const char *src, intmax_t *dest)
intmax_t result = strtoimax(src, &end, 10);
if (errno == ERANGE) {
return DNSSEC_OUT_OF_RANGE;
return KNOT_ERANGE;
}
if (src == end || *end != '\0') {
return DNSSEC_MALFORMED_DATA;
return KNOT_EINVAL;
}
*dest = result;
return DNSSEC_EOK;
return KNOT_EOK;
}
inline static int uintmax_from_str(const char *src, uintmax_t *dest)
{
if (!isdigit((int)*src) && *src != '-' && *src != '+') {
return DNSSEC_MALFORMED_DATA;
return KNOT_EINVAL;
}
errno = 0;
......@@ -58,29 +57,29 @@ inline static int uintmax_from_str(const char *src, uintmax_t *dest)
uintmax_t result = strtoumax(src, &end, 10);
if (errno == ERANGE) {
return DNSSEC_OUT_OF_RANGE;
return KNOT_ERANGE;
}
if (src == end || *end != '\0') {
return DNSSEC_MALFORMED_DATA;
return KNOT_EINVAL;
}
*dest = result;
return DNSSEC_EOK;
return KNOT_EOK;
}
#define CONVERT(prefix, type, min, max, src, dest) \
{ \
prefix##max_t value; \
int result = prefix##max_from_str(src, &value); \
if (result != DNSSEC_EOK) { \
if (result != KNOT_EOK) { \
return result; \
} \
if (CHECK_MIN_##min(value, min) || value > (max)) { \
return DNSSEC_OUT_OF_RANGE; \
return KNOT_ERANGE; \
} \
*dest = (type)value; \
return DNSSEC_EOK; \
return KNOT_EOK; \
}
#define CHECK_MIN_0(value, min) 0
......
......@@ -37,7 +37,6 @@ libshared_la_SOURCES = \
shared/pem.c \
shared/pem.h \
shared/shared.h \
shared/strtonum.h \
shared/timestamp.c \
shared/timestamp.h \
shared/wire.h
......
......@@ -23,7 +23,6 @@
#include "key.h"
#include "keyid.h"
#include "shared.h"
#include "strtonum.h"
#include "timestamp.h"
int decode_ignore(_unused_ const json_t *value, _unused_ void *result)
......
......@@ -25,7 +25,6 @@
/shared_base32hex
/shared_bignum
/shared_dname
/shared_strtonum
/shared_timestamp
/shared_wire
/sign
......
......@@ -42,7 +42,6 @@ check_PROGRAMS = \
shared_base32hex \
shared_bignum \
shared_dname \
shared_strtonum \
shared_timestamp \
shared_wire \
tsig
......
/* Copyright (C) 2011 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
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/>.
*/
/*!
* \file strtonum.h
*
* \brief Universal interface for safe conversion of strings to numbers.
*
* \author Jan Vcelak <jan.vcelak@nic.cz>
*
* \addtogroup common_lib
* @{
*/
#pragma once
#include <limits.h>
#include <stdint.h>
#include <stdlib.h>
#include "libknot/libknot.h"
typedef long long int knot_strtoll_result_t;
typedef unsigned long long int knot_strtoull_result_t;
/*!
* \brief Convert string to signed integer.
*
* \param[in] src Input string.
* \param[out] dest Output integral value.
*
* \return Error code.
* \retval KNOT_EOK The conversion was successful.
* \retval KNOT_ERANGE The value is outside target type range.
* \retval KNOT_EMALF The input value is not terminated.
*/
static int knot_strtoll(const char *src, knot_strtoll_result_t *dest)
{
char *end;
knot_strtoll_result_t result = strtoll(src, &end, 10);
if (errno == ERANGE)
return KNOT_ERANGE;
if (src == end || *end != '\0')
return KNOT_EMALF;
*dest = result;
return KNOT_EOK;
}
/*!
* \brief Convert string to unsigned integer.
*
* \see knot_strtoll
*/
static int knot_strtoull(const char *src, knot_strtoull_result_t *dest)
{
char *end;
knot_strtoull_result_t result = strtoull(src, &end, 10);
if (errno == ERANGE)
return KNOT_ERANGE;
if (src == end || *end != '\0')
return KNOT_EMALF;
*dest = result;
return KNOT_EOK;
}
/*!
* \brief Helper macro defining body of individual conversion functions.
*
* \param type Target data type.
* \param function Underlying conversion function.
* \param min Minimal value valid for given data type.
* \param max Maximal value valid for given data type.
* \param src Pointer to source string.
* \param dest Pointer to destination type.
*
* \return Error code.
* \retval KNOT_EOK The conversion was successful.
* \retval KNOT_ERANGE The value is outside target type range.
* \retval KNOT_EMALF The input value is not terminated.
*/
#define KNOT_STR2NUM(type, function, min, max, src, dest) \
{ \
function##_result_t value; \
errno = 0; \
int result = function((src), &value); \
if (result != KNOT_EOK) \
return result; \
\
if (value < (min) || value > (max)) \
return KNOT_ERANGE; \
\
*(dest) = (type)value; \
return KNOT_EOK; \
}
inline static int knot_str2int(const char *src, int *dest)
{
KNOT_STR2NUM(int, knot_strtoll, INT_MIN, INT_MAX, src, dest)
}
inline static int knot_str2uint8t(const char *src, uint8_t *dest)
{
KNOT_STR2NUM(uint8_t, knot_strtoull, 0, UINT8_MAX, src, dest)
}
inline static int knot_str2uint16t(const char *src, uint16_t *dest)
{
KNOT_STR2NUM(uint16_t, knot_strtoull, 0, UINT16_MAX, src, dest)
}
/*! @} */
......@@ -30,11 +30,11 @@
#include "cmdparse/command.h"
#include "cmdparse/parameter.h"
#include "cmdparse/value.h"
#include "contrib/strtonum.h"
#include "legacy/key.h"
#include "shared/dname.h"
#include "shared/print.h"
#include "shared/shared.h"
#include "shared/strtonum.h"
#include "shared/wire.h"
#define PROGRAM_NAME "keymgr"
......@@ -206,7 +206,7 @@ static bool keytag_match(uint16_t keytag, const char *filter)
{
uint16_t converted = 0;
return str_to_u16(filter, &converted) == DNSSEC_EOK &&
return str_to_u16(filter, &converted) == KNOT_EOK &&
keytag == converted;
}
......@@ -300,7 +300,7 @@ static int search_str_to_keytag(const char *search)
uint16_t keytag = 0;
int r = str_to_u16(search, &keytag);
return (r == DNSSEC_EOK ? keytag : -1);
return (r == KNOT_EOK ? keytag : -1);
}
/*!
......
......@@ -17,10 +17,10 @@
#include <ctype.h>
#include <string.h>
#include "contrib/strtonum.h"
#include "dnssec/binary.h"
#include "dnssec/error.h"
#include "shared/shared.h"
#include "shared/strtonum.h"
#include "utils/keymgr/legacy/privkey.h"
/* -- private key params conversion ---------------------------------------- */
......@@ -95,7 +95,7 @@ static int parse_algorithm(char *string, void *_algorithm)
uint8_t *algorithm = _algorithm;
int r = str_to_u8(string, algorithm);
return (r == DNSSEC_EOK ? DNSSEC_EOK : DNSSEC_INVALID_KEY_ALGORITHM);
return (r == KNOT_EOK ? DNSSEC_EOK : DNSSEC_INVALID_KEY_ALGORITHM);
}
/*!
......
......@@ -17,9 +17,9 @@
#include <getopt.h>
#include <stdio.h>
#include "contrib/strtonum.h"
#include "knot/common/log.h"
#include "utils/common/params.h"
#include "utils/common/strtonum.h"
#include "utils/knotc/commands.h"
#include "utils/knotc/interactive.h"
#include "utils/knotc/process.h"
......@@ -85,7 +85,7 @@ int main(int argc, char **argv)
params.socket = optarg;
break;
case 't':
if (knot_str2int(optarg, &params.timeout) != KNOT_EOK) {
if (str_to_int(optarg, &params.timeout) != KNOT_EOK) {
print_help();
return EXIT_FAILURE;
}
......
......@@ -21,13 +21,13 @@
#include <stdio.h>
#include <string.h>
#include "contrib/strtonum.h"
#include "dnssec/error.h"
#include "dnssec/nsec.h"
#include "shared/base32hex.h"
#include "shared/dname.h"
#include "shared/hex.h"
#include "shared/print.h"
#include "shared/strtonum.h"
#define PROGRAM_NAME "knsec3hash"
......@@ -69,15 +69,15 @@ static bool parse_nsec3_params(dnssec_nsec3_params_t *params, const char *salt_s
{
uint8_t algorithm = 0;
int r = str_to_u8(algorithm_str, &algorithm);
if (r != DNSSEC_EOK) {
if (r != KNOT_EOK) {
error("Invalid algorithm number.");
return false;
}
uint16_t iterations = 0;
r = str_to_u16(iterations_str, &iterations);
if (r != DNSSEC_EOK) {
error("Invalid iteration count, %s.", dnssec_strerror(r));
if (r != KNOT_EOK) {
error("Invalid iteration count.");
return false;
}
......
......@@ -12,6 +12,7 @@ conf_tools
confdb
confio
control
contrib/test_strtonum
db
descriptor
dname
......
......@@ -64,6 +64,7 @@ check_PROGRAMS = \
ztree
check_PROGRAMS += \
contrib/test_strtonum \
modules/online_sign \
utils/test_lookup
......
/* Copyright (C) 2015 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
/* Copyright (C) 2016 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
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
......@@ -16,12 +16,11 @@
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <tap/basic.h>
#include <assert.h>
#include <inttypes.h>
#include "contrib/strtonum.h"
#include "dnssec/error.h"
#include "strtonum.h"
static void test_u8(const char *in, uint8_t expected, int errcode)
{
......@@ -29,9 +28,9 @@ static void test_u8(const char *in, uint8_t expected, int errcode)
assert(expected != out);
ok(str_to_u8(in, &out) == errcode &&
(errcode != DNSSEC_EOK || out == expected),
(errcode != KNOT_EOK || out == expected),
"str_to_u8 %s on \"%s\"",
(errcode == DNSSEC_EOK ? "succeeds" : "fails"), in);
(errcode == KNOT_EOK ? "succeeds" : "fails"), in);
}
static void test_u16(const char *in, uint16_t expected, int errcode)
......@@ -40,9 +39,9 @@ static void test_u16(const char *in, uint16_t expected, int errcode)
assert(expected != out);
ok(str_to_u16(in, &out) == errcode &&
(errcode != DNSSEC_EOK || out == expected),
(errcode != KNOT_EOK || out == expected),
"str_to_u16 %s on \"%s\"",
(errcode == DNSSEC_EOK ? "succeeds" : "fails"), in);
(errcode == KNOT_EOK ? "succeeds" : "fails"), in);
}
static void test_int(const char *in, int expected, int errcode)
......@@ -51,9 +50,9 @@ static void test_int(const char *in, int expected, int errcode)
assert(expected != out);
ok(str_to_int(in, &out) == errcode &&
(errcode != DNSSEC_EOK || out == expected),
(errcode != KNOT_EOK || out == expected),
"str_to_int %s on \"%s\"",
(errcode == DNSSEC_EOK ? "succeeds" : "fails"), in);
(errcode == KNOT_EOK ? "succeeds" : "fails"), in);
}
// mute warn_unused_result
......@@ -65,25 +64,25 @@ int main(int argc, char *argv[])
{
plan_lazy();
test_u8("-1", 0, DNSSEC_OUT_OF_RANGE);
test_u8("256", 0, DNSSEC_OUT_OF_RANGE);
test_u8("0x1", 0, DNSSEC_MALFORMED_DATA);
test_u8(" 1", 0, DNSSEC_MALFORMED_DATA);
test_u8("1 ", 0, DNSSEC_MALFORMED_DATA);
test_u8("0", 0, DNSSEC_EOK);
test_u8("42", 42, DNSSEC_EOK);
test_u8("+84", 84, DNSSEC_EOK);
test_u8("255", 0xff, DNSSEC_EOK);
test_u16("-1", 0, DNSSEC_OUT_OF_RANGE);
test_u16("65536", 0, DNSSEC_OUT_OF_RANGE);
test_u16("0x1", 0, DNSSEC_MALFORMED_DATA);
test_u16(" 1", 0, DNSSEC_MALFORMED_DATA);
test_u16("1 ", 0, DNSSEC_MALFORMED_DATA);
test_u16("0", 0, DNSSEC_EOK);
test_u16("65280", 65280, DNSSEC_EOK);
test_u16("+256", 256, DNSSEC_EOK);
test_u16("65535", 65535, DNSSEC_EOK);
test_u8("-1", 0, KNOT_ERANGE);
test_u8("256", 0, KNOT_ERANGE);
test_u8("0x1", 0, KNOT_EINVAL);
test_u8(" 1", 0, KNOT_EINVAL);
test_u8("1 ", 0, KNOT_EINVAL);
test_u8("0", 0, KNOT_EOK);
test_u8("42", 42, KNOT_EOK);
test_u8("+84", 84, KNOT_EOK);
test_u8("255", 0xff, KNOT_EOK);
test_u16("-1", 0, KNOT_ERANGE);
test_u16("65536", 0, KNOT_ERANGE);
test_u16("0x1", 0, KNOT_EINVAL);
test_u16(" 1", 0, KNOT_EINVAL);
test_u16("1 ", 0, KNOT_EINVAL);
test_u16("0", 0, KNOT_EOK);
test_u16("65280", 65280, KNOT_EOK);
test_u16("+256", 256, KNOT_EOK);
test_u16("65535", 65535, KNOT_EOK);
char *int_under = NULL;
asprintf(&int_under, "%lld", (long long)INT_MIN - 1);
......@@ -94,16 +93,16 @@ int main(int argc, char *argv[])
char *int_over = NULL;
asprintf(&int_over, "%lld", (long long)INT_MAX + 1);
test_int(int_under, 0, DNSSEC_OUT_OF_RANGE);
test_int(int_over, 0, DNSSEC_OUT_OF_RANGE);
test_int("0x1", 0, DNSSEC_MALFORMED_DATA);
test_int(" 1", 0, DNSSEC_MALFORMED_DATA);
test_int("1 ", 0, DNSSEC_MALFORMED_DATA);
test_int(int_min, INT_MIN, DNSSEC_EOK);
test_int("0", 0, DNSSEC_EOK);
test_int("268435459", 268435459, DNSSEC_EOK);
test_int("+1073741827", 1073741827, DNSSEC_EOK);
test_int(int_max, INT_MAX, DNSSEC_EOK);
test_int(int_under, 0, KNOT_ERANGE);
test_int(int_over, 0, KNOT_ERANGE);
test_int("0x1", 0, KNOT_EINVAL);
test_int(" 1", 0, KNOT_EINVAL);
test_int("1 ", 0, KNOT_EINVAL);
test_int(int_min, INT_MIN, KNOT_EOK);
test_int("0", 0, KNOT_EOK);
test_int("268435459", 268435459, KNOT_EOK);
test_int("+1073741827", 1073741827, KNOT_EOK);
test_int(int_max, INT_MAX, KNOT_EOK);
free(int_under);
free(int_min);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment