From f23e9dc7bb7ffc4fe0b5c1151ecefa29a0ce5b79 Mon Sep 17 00:00:00 2001 From: Ivan Nardi <12729895+IvanNardi@users.noreply.github.com> Date: Thu, 4 Jan 2024 13:16:39 +0100 Subject: Add an implementation of the BSD function `strtonum` (#2238) The main difference with the original function is that we allow to specify the base. Credit for the original idea and the first implementation to @0xA50C1A1 --- example/ndpiReader.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'example/ndpiReader.c') diff --git a/example/ndpiReader.c b/example/ndpiReader.c index ce697026d..3fba97bbf 100644 --- a/example/ndpiReader.c +++ b/example/ndpiReader.c @@ -5377,6 +5377,29 @@ void compressedBitmapUnitTest() { /* *********************************************** */ +void strtonumUnitTest() { + const char *errstrp; + + assert(ndpi_strtonum("0", -10, +10, &errstrp, 10) == 0); + assert(errstrp == NULL); + assert(ndpi_strtonum("0", +10, -10, &errstrp, 10) == 0); + assert(errstrp != NULL); + assert(ndpi_strtonum(" -11 ", -10, +10, &errstrp, 10) == 0); + assert(errstrp != NULL); + assert(ndpi_strtonum(" -11 ", -100, +100, &errstrp, 10) == -11); + assert(errstrp == NULL); + assert(ndpi_strtonum("123abc", LLONG_MIN, LLONG_MAX, &errstrp, 10) == 123); + assert(errstrp == NULL); + assert(ndpi_strtonum("123abc", LLONG_MIN, LLONG_MAX, &errstrp, 16) == 0x123abc); + assert(errstrp == NULL); + assert(ndpi_strtonum(" 0x123abc", LLONG_MIN, LLONG_MAX, &errstrp, 16) == 0x123abc); + assert(errstrp == NULL); + assert(ndpi_strtonum("ghi", -10, +10, &errstrp, 10) == 0); + assert(errstrp != NULL); +} + +/* *********************************************** */ + void filterUnitTest() { ndpi_filter* f = ndpi_filter_alloc(); u_int32_t v, i; @@ -5628,6 +5651,7 @@ int main(int argc, char **argv) { ndpi_self_check_host_match(stderr); analysisUnitTest(); compressedBitmapUnitTest(); + strtonumUnitTest(); #endif } -- cgit v1.2.3