From 56a164185a603c013c327a431dd36fe6479d5bed Mon Sep 17 00:00:00 2001 From: fedora Cloud User Date: Tue, 2 Apr 2024 04:36:16 +0000 Subject: [PATCH] release candidate merge --- Makefile | 42 +++++++---- main.c | 202 ++++++++++++++++++++++++++++++++++++++++++++++++++++ main.h | 42 +++++++++++ testbench.c | 3 +- 4 files changed, 276 insertions(+), 13 deletions(-) create mode 100644 main.c create mode 100644 main.h diff --git a/Makefile b/Makefile index 1711ef3..c8ab3a7 100644 --- a/Makefile +++ b/Makefile @@ -1,31 +1,49 @@ -# arrayutils Makefile +# SPDX-License-Identifier: Apache-2.0 +# ****************************************************************************** +# +# @file Makefile +# +# @brief Makefile for Array Utility Function library +# +# @copyright Copyright (C) 2024 Jackrabbit Founders LLC. All rights reserved. +# +# @date Mar 2024 +# @author Barrett Edwards +# +# ****************************************************************************** + CC=gcc CFLAGS= -g3 -O0 -Wall -Wextra -INCLUDE_DIR=/usr/local/include/ +MACROS= +INCLUDE_DIR=/usr/local/include LIB_DIR=/usr/local/lib INCLUDE_PATH=-I $(INCLUDE_DIR) LIB_PATH=-L $(LIB_DIR) LIBS= +TARGET=arrayutils -all: testbench libarrayutils.a +all: testbench lib$(TARGET).a -testbench: testbench.c arrayutils.o - $(CC) $^ $(CFLAGS) $(INCLUDE_PATH) $(LIB_PATH) $(LIBS) -o $@ +testbench: testbench.c main.o + $(CC) $^ $(CFLAGS) $(MACROS) $(INCLUDE_PATH) $(LIB_PATH) $(LIBS) -o $@ -libarrayutils.a: arrayutils.o +lib$(TARGET).a: main.o ar rcs $@ $^ -arrayutils.o: arrayutils.c arrayutils.h - $(CC) -c $< $(CFLAGS) $(INCLUDE_PATH) -o $@ +main.o: main.c main.h + $(CC) -c $< $(CFLAGS) $(MACROS) $(INCLUDE_PATH) -o $@ clean: rm -rf ./*.o ./*.a testbench -install: libarrayutils.a - sudo cp libarrayutils.a $(LIB_DIR) - sudo cp arrayutils.h $(INCLUDE_DIR) +doc: + doxygen -.PHONY: all clean install +install: lib$(TARGET).a + sudo cp lib$(TARGET).a $(LIB_DIR)/ + sudo cp main.h $(INCLUDE_DIR)/$(TARGET).h + +.PHONY: all clean doc install # Variables # $^ Will expand to be all the sensitivity list diff --git a/main.c b/main.c new file mode 100644 index 0000000..fcc40de --- /dev/null +++ b/main.c @@ -0,0 +1,202 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +/** + * @file arrayutils.c + * + * @brief Code file for Array Utils library + * + * @copyright Copyright (C) 2024 Jackrabbit Founders LLC. All rights reserved. + * + * @date Feb 2024 + * @author Barrett Edwards + * + */ + +/* INCLUDES ==================================================================*/ + +/* printf() + */ +#include + +/* strtoul() + */ +#include + +/* memset() + */ +#include + +#include "arrayutils.h" + +/* MACROS ====================================================================*/ + +/* ENUMERATIONS ==============================================================*/ + +/* STRUCTS ===================================================================*/ + +/* GLOBAL VARIABLES ==========================================================*/ + +/* PROTOTYPES ================================================================*/ + +/* FUNCTIONS =================================================================*/ + +/* + * Print a unsigned char buffer + */ +void autl_prnt_buf(void *buf, unsigned long len, unsigned long width, int print_header) +{ + unsigned long i, j, k, rows; + __u8 *ptr; + + /* STEP 1: Verify Inputs */ + if ( buf == NULL) + return; + + if ( len == 0 ) + return; + + if ( width == 0 ) + width = AUVB_DEFAULT_WIDTH; + + if ( width < AUVB_MIN_WIDTH ) + width = AUVB_MIN_WIDTH; + + ptr = (__u8*) buf; + + /* Compute the number of rows to print */ + rows = len / width; + if ( (len % width) > 0) + rows++; + + /* Print index '0x0000: ' */ + if (print_header) { + printf(" "); + for ( i = 0 ; i < width ; i++ ) + printf("%02lu ", i); + printf("\n"); + } + + k = 0; + for ( i = 0 ; i < rows ; i++ ) { + printf("0x%08lx: ", i * width); + for ( j = 0 ; j < width ; j++ ) { + if (k >= len) + break; + + printf("%02x ", ptr[i*width + j]); + + k++; + } + printf("\n"); + } + + return; +} + +/** + * Split a comma separated string of __u8 into a __u8[] array + * + * @param src char* to a comma separated array of __u8 values + * @param dst __u8* array to store parsed values in + * @param max Maximum number of values to convert (i.e. size of dst array) + * @param hex 0=auto detect base, non zero = treat all values as hex + */ +int autl_csv_to_u8(__u8 *dst, char* src, unsigned max, int hex) +{ + char *head, *tail; + char buf[8]; + unsigned i; + unsigned base; + + // Initialize variables + i = 0; + head = src; + tail = src; + memset(buf,0,8); + if (hex) + base = 16; + else + base = 0; + + // Loop through the character array + while (*tail != 0) + { + if (*tail == ',') + { + memcpy(buf, head, tail - head); + dst[i] = strtoull(buf, NULL, base); + memset(buf,0,8); + i++; + tail++; + head = tail; + } + else + tail++; + + if (i >= max) + return i; + } + + // Catch last entry in the CSV list + if (head != tail) + { + memcpy(buf, head, tail - head); + dst[i] = strtoull(buf, NULL, base); + i++; + } + + return i; +} + +/** + * Split a comma separated string of __u64 into a __u64[] array + * + * @param src char* to a comma separated array of __u64 values + * @param dst __u64* array to store parsed values in + * @param max Maximum number of values to convert (i.e. size of dst array) + * @param hex 0=auto detect base, non zero = treat all values as hex + */ +int autl_csv_to_u64(__u64 *dst, char* src, unsigned max, int hex) +{ + char *head, *tail; + char buf[64]; + unsigned i; + unsigned base; + + i = 0; + head = src; + tail = src; + memset(buf,0,64); + if (hex) + base = 16; + else + base = 0; + + while (*tail != 0) + { + if (*tail == ',') + { + memcpy(buf, head, tail - head); + dst[i] = strtoull(buf, NULL, base); + memset(buf,0,64); + i++; + tail++; + head = tail; + } + else + tail++; + + if (i >= max) + return i; + } + + // Catch last entry in the CSV list + if (head != tail) + { + memcpy(buf, head, tail - head); + dst[i] = strtoull(buf, NULL, base); + i++; + } + + return i; +} + diff --git a/main.h b/main.h new file mode 100644 index 0000000..4e1bae1 --- /dev/null +++ b/main.h @@ -0,0 +1,42 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +/** + * @file arrayutils.h + * + * @brief Header file for Array Utils Library + * + * @copyright Copyright (C) 2024 Jackrabbit Founders LLC. All rights reserved. + * + * @date Feb 2024 + * @author Barrett Edwards + * + * Macro / Enumeration Prefixes + * AUVB - Verbose Macros + */ + +#ifndef _ARRAYUTILS_H +#define _ARRAYUTILS_H + +/* INCLUDES ==================================================================*/ + +#include + +/* MACROS ====================================================================*/ + +#define AUVB_DEFAULT_WIDTH 16 //!< Default width for printing __u8 buffers +#define AUVB_MIN_WIDTH 4 //!< Minium width for printing buffers + +/* ENUMERATIONS ==============================================================*/ + +/* STRUCTS ===================================================================*/ + +/* GLOBAL VARIABLES ==========================================================*/ + +/* PROTOTYPES ================================================================*/ + +void autl_prnt_buf(void *buf, unsigned long len, unsigned long width, int print_header); +int autl_csv_to_u8(__u8 *dst, char* src, unsigned max, int hex); +int autl_csv_to_u64(__u64 *dst, char* src, unsigned max, int hex); + +/* INLINE FUNCTIONS ==========================================================*/ + +#endif //ifndef _ARRAYUTILS_H diff --git a/testbench.c b/testbench.c index 6ad058f..5172336 100644 --- a/testbench.c +++ b/testbench.c @@ -1,3 +1,4 @@ +/* SPDX-License-Identifier: Apache-2.0 */ /** * @file testbench.c * @@ -6,7 +7,7 @@ * @copyright Copyright (C) 2024 Jackrabbit Founders LLC. All rights reserved. * * @date Feb 2024 - * @author Barrett Edwards + * @author Barrett Edwards * */