summaryrefslogtreecommitdiffstats
path: root/util
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--util/argconfig.c22
-rw-r--r--util/argconfig.h8
-rw-r--r--util/base64.c105
-rw-r--r--util/base64.h7
-rw-r--r--util/json.h1
-rw-r--r--util/log.c90
-rw-r--r--util/log.h34
-rw-r--r--util/meson.build8
-rw-r--r--util/parser.c46
9 files changed, 156 insertions, 165 deletions
diff --git a/util/argconfig.c b/util/argconfig.c
index 341a049..59d8f1d 100644
--- a/util/argconfig.c
+++ b/util/argconfig.c
@@ -39,6 +39,7 @@
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
+#include <stdbool.h>
static argconfig_help_func *help_funcs[MAX_HELP_FUNC] = { NULL };
@@ -176,17 +177,8 @@ int argconfig_parse(int argc, char *argv[], const char *program_desc,
if (s->option && strlen(s->option)) {
long_opts[option_index].name = s->option;
long_opts[option_index].has_arg = s->argument_type;
-
- if (s->argument_type == no_argument
- && s->default_value != NULL) {
- value_addr = (void *)(char *)s->default_value;
-
- long_opts[option_index].flag = value_addr;
- long_opts[option_index].val = 1;
- } else {
- long_opts[option_index].flag = NULL;
- long_opts[option_index].val = 0;
- }
+ long_opts[option_index].flag = NULL;
+ long_opts[option_index].val = 0;
}
option_index++;
}
@@ -219,10 +211,6 @@ int argconfig_parse(int argc, char *argv[], const char *program_desc,
}
if (option_index == options_count)
continue;
- if (long_opts[option_index].flag) {
- *(uint8_t *)(long_opts[option_index].flag) = 1;
- continue;
- }
}
s = &options[option_index];
@@ -282,7 +270,7 @@ int argconfig_parse(int argc, char *argv[], const char *program_desc,
}
*((uint32_t *) value_addr) = tmp;
} else if (s->config_type == CFG_INCREMENT) {
- (*((int *)value_addr))++;
+ *((int *)value_addr) += 1;
} else if (s->config_type == CFG_LONG) {
*((unsigned long *)value_addr) = strtoul(optarg, &endptr, 0);
if (errno || optarg == endptr) {
@@ -363,6 +351,8 @@ int argconfig_parse(int argc, char *argv[], const char *program_desc,
goto out;
}
*((FILE **) value_addr) = f;
+ } else if (s->config_type == CFG_FLAG) {
+ *((bool *)value_addr) = true;
}
}
free(short_opts);
diff --git a/util/argconfig.h b/util/argconfig.h
index 3147277..a11334b 100644
--- a/util/argconfig.h
+++ b/util/argconfig.h
@@ -38,7 +38,7 @@
#include <stdarg.h>
enum argconfig_types {
- CFG_NONE,
+ CFG_FLAG,
CFG_STRING,
CFG_INT,
CFG_SIZE,
@@ -65,7 +65,7 @@ enum argconfig_types {
#define OPT_END() { NULL }
#define OPT_FLAG(l, s, v, d) \
- {l, s, NULL, CFG_NONE, v, no_argument, d}
+ {l, s, NULL, CFG_FLAG, v, no_argument, d}
#define OPT_SUFFIX(l, s, v, d) \
{l, s, "IONUM", CFG_LONG_SUFFIX, v, required_argument, d}
@@ -91,12 +91,16 @@ enum argconfig_types {
#define OPT_SHRT(l, s, v, d) \
{l, s, "NUM", CFG_SHORT, v, required_argument, d}
+#define OPT_INCR(l, s, v, d) \
+ {l, s, "NUM", CFG_INCREMENT, v, no_argument, d}
+
#define OPT_STRING(l, s, m, v, d) \
{l, s, m, CFG_STRING, v, required_argument, d}
#define OPT_FMT(l, s, v, d) OPT_STRING(l, s, "FMT", v, d)
#define OPT_FILE(l, s, v, d) OPT_STRING(l, s, "FILE", v, d)
#define OPT_LIST(l, s, v, d) OPT_STRING(l, s, "LIST", v, d)
+#define OPT_STR(l, s, v, d) OPT_STRING(l, s, "STRING", v, d)
struct argconfig_commandline_options {
const char *option;
diff --git a/util/base64.c b/util/base64.c
new file mode 100644
index 0000000..e386c11
--- /dev/null
+++ b/util/base64.c
@@ -0,0 +1,105 @@
+/*
+ * base64.c - RFC4648-compliant base64 encoding
+ *
+ * Copyright (c) 2020 Hannes Reinecke, SUSE
+ *
+ * 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 2
+ * 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, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+
+static const char base64_table[65] =
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+
+/**
+ * base64_encode() - base64-encode some bytes
+ * @src: the bytes to encode
+ * @srclen: number of bytes to encode
+ * @dst: (output) the base64-encoded string. Not NUL-terminated.
+ *
+ * Encodes the input string using characters from the set [A-Za-z0-9+,].
+ * The encoded string is roughly 4/3 times the size of the input string.
+ *
+ * Return: length of the encoded string
+ */
+int base64_encode(const unsigned char *src, int srclen, char *dst)
+{
+ int i, bits = 0;
+ u_int32_t ac = 0;
+ char *cp = dst;
+
+ for (i = 0; i < srclen; i++) {
+ ac = (ac << 8) | src[i];
+ bits += 8;
+ do {
+ bits -= 6;
+ *cp++ = base64_table[(ac >> bits) & 0x3f];
+ } while (bits >= 6);
+ }
+ if (bits) {
+ *cp++ = base64_table[(ac << (6 - bits)) & 0x3f];
+ bits -= 6;
+ }
+ while (bits < 0) {
+ *cp++ = '=';
+ bits += 2;
+ }
+
+ return cp - dst;
+}
+
+/**
+ * base64_decode() - base64-decode some bytes
+ * @src: the base64-encoded string to decode
+ * @len: number of bytes to decode
+ * @dst: (output) the decoded bytes.
+ *
+ * Decodes the base64-encoded bytes @src according to RFC 4648.
+ *
+ * Return: number of decoded bytes
+ */
+int base64_decode(const char *src, int srclen, unsigned char *dst)
+{
+ u_int32_t ac = 0;
+ int i, bits = 0;
+ unsigned char *bp = dst;
+
+ for (i = 0; i < srclen; i++) {
+ const char *p = strchr(base64_table, src[i]);
+
+ if (src[i] == '=') {
+ ac = (ac << 6);
+ bits += 6;
+ if (bits >= 8)
+ bits -= 8;
+ continue;
+ }
+ if (p == NULL || src[i] == 0)
+ return -EINVAL;
+ ac = (ac << 6) | (p - base64_table);
+ bits += 6;
+ if (bits >= 8) {
+ bits -= 8;
+ *bp++ = (unsigned char)(ac >> bits);
+ }
+ }
+ if (ac && ((1 << bits) - 1))
+ return -EAGAIN;
+
+ return bp - dst;
+}
diff --git a/util/base64.h b/util/base64.h
new file mode 100644
index 0000000..609a877
--- /dev/null
+++ b/util/base64.h
@@ -0,0 +1,7 @@
+#ifndef _BASE64_H
+#define _BASE64_H
+
+int base64_encode(const unsigned char *src, int len, char *dst);
+int base64_decode(const char *src, int len, unsigned char *dst);
+
+#endif /* _BASE64_H */
diff --git a/util/json.h b/util/json.h
index 666c78d..2ef8632 100644
--- a/util/json.h
+++ b/util/json.h
@@ -54,6 +54,7 @@ int json_object_add_value_type(struct json_object *obj, const char *name, int ty
json_object_add_value_type((obj), name, JSON_TYPE_INTEGER, (long long) (val))
#define json_object_add_value_uint(obj, name, val) \
json_object_add_value_type((obj), name, JSON_TYPE_UINT, (unsigned long long) (val))
+#define json_object_add_value_uint64 json_object_add_value_uint
#define json_object_add_value_float(obj, name, val) \
json_object_add_value_type((obj), name, JSON_TYPE_FLOAT, (val))
#define json_object_add_value_string(obj, name, val) \
diff --git a/util/log.c b/util/log.c
deleted file mode 100644
index 4a22354..0000000
--- a/util/log.c
+++ /dev/null
@@ -1,90 +0,0 @@
-/*
- * Copyright (C) 2021 SUSE LLC
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License version
- * 2 as published by the Free Software Foundation.
- *
- * 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.
- *
- * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- *
- * This file implements basic logging functionality.
- */
-
-#include <sys/types.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <stdarg.h>
-#include <stdbool.h>
-#include <syslog.h>
-#include <unistd.h>
-#include <time.h>
-#define LOG_FUNCNAME 1
-#include "log.h"
-#include "cleanup.h"
-
-#ifndef LOG_CLOCK
-#define LOG_CLOCK CLOCK_MONOTONIC
-#endif
-
-int log_level = DEFAULT_LOGLEVEL;
-bool log_timestamp;
-bool log_pid;
-
-void __attribute__((format(printf, 3, 4)))
-__msg(int lvl, const char *func, const char *format, ...)
-{
- va_list ap;
- char pidbuf[16];
- char timebuf[32];
- static const char *const formats[] = {
- "%s%s%s",
- "%s%s%s: ",
- "%s<%s>%s ",
- "%s<%s> %s: ",
- "[%s] %s%s ",
- "[%s]%s %s: ",
- "[%s] <%s>%s ",
- "[%s] <%s> %s: ",
- };
- char *header __cleanup__(cleanup_charp) = NULL;
- char *message __cleanup__(cleanup_charp) = NULL;
- int idx;
-
- if (lvl > log_level)
- return;
-
- if (log_timestamp) {
- struct timespec now;
-
- clock_gettime(LOG_CLOCK, &now);
- snprintf(timebuf, sizeof(timebuf), "%6ld.%06ld",
- (long)now.tv_sec, now.tv_nsec / 1000);
- } else
- *timebuf = '\0';
-
- if (log_pid)
- snprintf(pidbuf, sizeof(pidbuf), "%ld", (long)getpid());
- else
- *pidbuf = '\0';
-
- idx = ((log_timestamp ? 1 : 0) << 2) |
- ((log_pid ? 1 : 0) << 1) | (func ? 1 : 0);
-
- if (asprintf(&header, formats[idx], timebuf, pidbuf, func ? func : "")
- == -1)
- header = NULL;
-
- va_start(ap, format);
- if (vasprintf(&message, format, ap) == -1)
- message = NULL;
- va_end(ap);
-
- fprintf(stderr, "%s%s", header ? header : "<error>",
- message ? message : "<error>");
-
-}
diff --git a/util/log.h b/util/log.h
deleted file mode 100644
index 15107a5..0000000
--- a/util/log.h
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * Copyright (c) 2021 Martin Wilck, SUSE LLC
- * SPDX-License-Identifier: LGPL-2.1-or-newer
- */
-#ifndef _LOG_H
-#define _LOG_H
-
-#ifndef MAX_LOGLEVEL
-# define MAX_LOGLEVEL LOG_DEBUG
-#endif
-#ifndef DEFAULT_LOGLEVEL
-# define DEFAULT_LOGLEVEL LOG_NOTICE
-#endif
-
-#if (LOG_FUNCNAME == 1)
-#define _log_func __func__
-#else
-#define _log_func NULL
-#endif
-
-extern int log_level;
-extern bool log_timestamp;
-extern bool log_pid;
-
-void __attribute__((format(printf, 3, 4)))
-__msg(int lvl, const char *func, const char *format, ...);
-
-#define msg(lvl, format, ...) \
- do { \
- if ((lvl) <= MAX_LOGLEVEL) \
- __msg(lvl, _log_func, format, ##__VA_ARGS__); \
- } while (0)
-
-#endif /* _LOG_H */
diff --git a/util/meson.build b/util/meson.build
new file mode 100644
index 0000000..e525f0e
--- /dev/null
+++ b/util/meson.build
@@ -0,0 +1,8 @@
+sources += [
+ 'util/argconfig.c',
+ 'util/cleanup.c',
+ 'util/json.c',
+ 'util/parser.c',
+ 'util/suffix.c',
+ 'util/base64.c',
+]
diff --git a/util/parser.c b/util/parser.c
index 2dd0922..bd74822 100644
--- a/util/parser.c
+++ b/util/parser.c
@@ -16,9 +16,9 @@
/**
* match_one: - Determines if a string matches a simple pattern
- * @s: the string to examine for presence of the pattern
- * @p: the string containing the pattern
- * @args: array of %MAX_OPT_ARGS &substring_t elements. Used to return match
+ * @s: the string to examine for presence of the pattern
+ * @p: the string containing the pattern
+ * @args: array of %MAX_OPT_ARGS &substring_t elements. Used to return match
* locations.
*
* Description: Determines if the pattern @p is present in string @s. Can only
@@ -94,12 +94,12 @@ static int match_one(char *s, const char *p, substring_t args[])
/**
* match_token: - Find a token (and optional args) in a string
- * @s: the string to examine for token/argument pairs
- * @table: match_table_t describing the set of allowed option tokens and the
- * arguments that may be associated with them. Must be terminated with a
- * &struct match_token whose pattern is set to the NULL pointer.
- * @args: array of %MAX_OPT_ARGS &substring_t elements. Used to return match
- * locations.
+ * @s: the string to examine for token/argument pairs
+ * @table: match_table_t describing the set of allowed option tokens and the
+ * arguments that may be associated with them. Must be terminated with a
+ * &struct match_token whose pattern is set to the NULL pointer.
+ * @args: array of %MAX_OPT_ARGS &substring_t elements. Used to return match
+ * locations.
*
* Description: Detects which if any of a set of token strings has been passed
* to it. Tokens can include up to MAX_OPT_ARGS instances of basic c-style
@@ -118,9 +118,9 @@ int match_token(char *s, const match_table_t table, substring_t args[])
/**
* match_number: scan a number in the given base from a substring_t
- * @s: substring to be scanned
- * @result: resulting integer on success
- * @base: base to use when converting string
+ * @s: substring to be scanned
+ * @result: resulting integer on success
+ * @base: base to use when converting string
*
* Description: Given a &substring_t and a base, attempts to parse the substring
* as a number in that base. On success, sets @result to the integer represented
@@ -154,8 +154,8 @@ static int match_number(substring_t *s, int *result, int base)
/**
* match_int: - scan a decimal representation of an integer from a substring_t
- * @s: substring_t to be scanned
- * @result: resulting integer on success
+ * @s: substring_t to be scanned
+ * @result: resulting integer on success
*
* Description: Attempts to parse the &substring_t @s as a decimal integer. On
* success, sets @result to the integer represented by the string and returns 0.
@@ -168,8 +168,8 @@ int match_int(substring_t *s, int *result)
/**
* match_octal: - scan an octal representation of an integer from a substring_t
- * @s: substring_t to be scanned
- * @result: resulting integer on success
+ * @s: substring_t to be scanned
+ * @result: resulting integer on success
*
* Description: Attempts to parse the &substring_t @s as an octal integer. On
* success, sets @result to the integer represented by the string and returns
@@ -182,8 +182,8 @@ int match_octal(substring_t *s, int *result)
/**
* match_hex: - scan a hex representation of an integer from a substring_t
- * @s: substring_t to be scanned
- * @result: resulting integer on success
+ * @s: substring_t to be scanned
+ * @result: resulting integer on success
*
* Description: Attempts to parse the &substring_t @s as a hexadecimal integer.
* On success, sets @result to the integer represented by the string and
@@ -196,8 +196,8 @@ int match_hex(substring_t *s, int *result)
/**
* match_wildcard: - parse if a string matches given wildcard pattern
- * @pattern: wildcard pattern
- * @str: the string to be parsed
+ * @pattern: wildcard pattern
+ * @str: the string to be parsed
*
* Description: Parse the string @str to check if matches wildcard
* pattern @pattern. The pattern may contain two type wildcardes:
@@ -246,9 +246,9 @@ bool match_wildcard(const char *pattern, const char *str)
/**
* match_strlcpy: - Copy the characters from a substring_t to a sized buffer
- * @dest: where to copy to
- * @src: &substring_t to copy
- * @size: size of destination buffer
+ * @dest: where to copy to
+ * @src: &substring_t to copy
+ * @size: size of destination buffer
*
* Description: Copy the characters in &substring_t @src to the
* c-style string @dest. Copy no more than @size - 1 characters, plus