From eed2d1323441861f2d41f0ecc0a72fcc9190fa5f Mon Sep 17 00:00:00 2001 From: Thorsten Töpper Date: Sat, 7 Feb 2026 21:43:17 +0100 Subject: file processor: Copied from my small-utils project --- include/file_processor.h | 41 +++++++++++++++++ include/hex_conversion.h | 113 +++++++++++++++++++++++++++++++++++++++++++++++ include/trace_macros.h | 23 ++++++++++ 3 files changed, 177 insertions(+) create mode 100644 include/file_processor.h create mode 100644 include/hex_conversion.h create mode 100644 include/trace_macros.h (limited to 'include') diff --git a/include/file_processor.h b/include/file_processor.h new file mode 100644 index 0000000..8cfb6de --- /dev/null +++ b/include/file_processor.h @@ -0,0 +1,41 @@ +/* SPDX-License-Identifier: Apache-2.0 */ + +/* Copyright 2026 Thorsten Töpper + * + * vim:ts=4:sw=4:expandtab + */ +#ifndef FILE_PROCESSOR_H +#define FILE_PROCESSOR_H + +#include + +#define DF_BYTE_SIZE_256 32 +#define DF_BYTE_SIZE_512 64 + +/* Aliases for convenience, currently all algorithms are part of the default + * provider. */ +#define DF_OSSL_BLAKE2 "BLAKE2B-512" +#define DF_OSSL_SHA256 "SHA2-256" +#define DF_OSSL_SHA512 "SHA2-512" + +/** + * information about a file + * Contains filepath, stat() results, hash values of multiple algorithms. + * TODO: Organize the paths in a global pool (list/tree/map) and only refer there + * without any free() calls triggered through the pointer in the struct. + */ +struct df_fileinfo { + char *path; /**< pointer to the path of the file */ + char *name; /**< pointer to the name of the file */ + unsigned char blake2[DF_BYTE_SIZE_512]; /**< The BLAKE2-512 hash in binary form */ + unsigned char sha256[DF_BYTE_SIZE_256]; /**< The SHA256 hash in binary form. */ + unsigned char sha512[DF_BYTE_SIZE_512]; /**< The SHA512 hash in binary form. */ + struct stat statbuf; /**< Result of lstat() call. Symlinks are to be ignored and filtered out earlier. */ +}; + + +/*=========== FUNCTIONS ===========*/ +int process_file(struct df_fileinfo *info); + +#endif + diff --git a/include/hex_conversion.h b/include/hex_conversion.h new file mode 100644 index 0000000..90ab9e4 --- /dev/null +++ b/include/hex_conversion.h @@ -0,0 +1,113 @@ +/* SPDX-License-Identifier: Apache-2.0 */ + +/* Copyright 2026 Thorsten Töpper + * + * vim:ts=4:sw=4:expandtab + */ +#ifndef HEX_CONVERSION_H +#define HEX_CONVERSION_H + +#include +#include +#include + +#ifdef DEBUGBUILD +#include "trace_macros.h" +#endif + +#define ishex_macro(c) ((c>='0' && c <= '9') || (c>='A' && c <= 'F') || (c>='a' && c <= 'f')) + +int convert_line(char *s); +int ishex(unsigned char c); +int ishex_string(const char *s, size_t l); +unsigned char *convert_to_binary(char *hex, unsigned char *out); +char *convert_from_binary(unsigned char *bin, size_t l, char *out); + +/* short inline functions are fine in header */ +inline int convert_line(char *s) { + size_t i = 0, l = 0; + if (s == NULL) + return -1; + l=strlen(s); + for (i=0; i='0' && c <= '9') || (c>='A' && c <= 'F') || (c>='a' && c <= 'f')) { + return 1; + } + return 0; +}; + +inline int ishex_string(const char *s, size_t l) { + size_t i = 0; + if (s == 0) + return 0; + if (l == 0) + l = strlen(s); + for (; i + +#ifndef LOGERR +#define LOGERR(...) {fprintf(stderr, "[%s:%d] %s: ", __FILE__, __LINE__, __func__); fprintf(stderr, __VA_ARGS__);} +#endif + +#ifdef DEBUGBUILD +#define DBGTRC(...) LOGERR(__VA_ARGS__) +#else +#define DBGTRC(...) +#endif + +#endif + -- cgit v1.3