RC-1 array_utils, Jackrabbit Labs
This commit is contained in:
parent
fb0f83246f
commit
dad42d7215
34
Makefile
Normal file
34
Makefile
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
# arrayutils Makefile
|
||||||
|
CC=gcc
|
||||||
|
CFLAGS= -g3 -O0 -Wall -Wextra
|
||||||
|
INCLUDE_DIR=/usr/local/include/
|
||||||
|
LIB_DIR=/usr/local/lib
|
||||||
|
INCLUDE_PATH=-I $(INCLUDE_DIR)
|
||||||
|
LIB_PATH=-L $(LIB_DIR)
|
||||||
|
LIBS=
|
||||||
|
|
||||||
|
all: testbench libarrayutils.a
|
||||||
|
|
||||||
|
testbench: testbench.c arrayutils.o
|
||||||
|
$(CC) $^ $(CFLAGS) $(INCLUDE_PATH) $(LIB_PATH) $(LIBS) -o $@
|
||||||
|
|
||||||
|
libarrayutils.a: arrayutils.o
|
||||||
|
ar rcs $@ $^
|
||||||
|
|
||||||
|
arrayutils.o: arrayutils.c arrayutils.h
|
||||||
|
$(CC) -c $< $(CFLAGS) $(INCLUDE_PATH) -o $@
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -rf ./*.o ./*.a testbench
|
||||||
|
|
||||||
|
install: libarrayutils.a
|
||||||
|
sudo cp libarrayutils.a $(LIB_DIR)
|
||||||
|
sudo cp arrayutils.h $(INCLUDE_DIR)
|
||||||
|
|
||||||
|
.PHONY: all clean install
|
||||||
|
|
||||||
|
# Variables
|
||||||
|
# $^ Will expand to be all the sensitivity list
|
||||||
|
# $< Will expand to be the frist file in sensitivity list
|
||||||
|
# $@ Will expand to be the target name (the left side of the ":" )
|
||||||
|
# -c gcc will compile but not try and link
|
||||||
201
arrayutils.c
Normal file
201
arrayutils.c
Normal file
@ -0,0 +1,201 @@
|
|||||||
|
/**
|
||||||
|
* @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 <barrett@jrlabs.io>
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* INCLUDES ==================================================================*/
|
||||||
|
|
||||||
|
/* printf()
|
||||||
|
*/
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
/* strtoul()
|
||||||
|
*/
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
/* memset()
|
||||||
|
*/
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#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;
|
||||||
|
}
|
||||||
|
|
||||||
41
arrayutils.h
Normal file
41
arrayutils.h
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
/**
|
||||||
|
* @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 <barrett@jrlabs.io>
|
||||||
|
*
|
||||||
|
* Macro / Enumeration Prefixes
|
||||||
|
* AUVB - Verbose Macros
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _ARRAYUTILS_H
|
||||||
|
#define _ARRAYUTILS_H
|
||||||
|
|
||||||
|
/* INCLUDES ==================================================================*/
|
||||||
|
|
||||||
|
#include <linux/types.h>
|
||||||
|
|
||||||
|
/* 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
|
||||||
186
testbench.c
Normal file
186
testbench.c
Normal file
@ -0,0 +1,186 @@
|
|||||||
|
/**
|
||||||
|
* @file testbench.c
|
||||||
|
*
|
||||||
|
* @brief Testbench code file for Array Utils library
|
||||||
|
*
|
||||||
|
* @copyright Copyright (C) 2024 Jackrabbit Founders LLC. All rights reserved.
|
||||||
|
*
|
||||||
|
* @date Feb 2024
|
||||||
|
* @author Barrett Edwards <barrett@jrlabs.io>
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* INCLUDES ==================================================================*/
|
||||||
|
|
||||||
|
/* printf()
|
||||||
|
*/
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
/* memset()
|
||||||
|
*/
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
/* malloc()
|
||||||
|
* free()
|
||||||
|
*/
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
/* __u8
|
||||||
|
* __u64
|
||||||
|
*/
|
||||||
|
#include <linux/types.h>
|
||||||
|
|
||||||
|
#include "arrayutils.h"
|
||||||
|
|
||||||
|
/* MACROS ====================================================================*/
|
||||||
|
|
||||||
|
/* ENUMERATIONS ==============================================================*/
|
||||||
|
|
||||||
|
/* STRUCTS ===================================================================*/
|
||||||
|
|
||||||
|
/* GLOBAL VARIABLES ==========================================================*/
|
||||||
|
|
||||||
|
/* PROTOTYPES ================================================================*/
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
|
||||||
|
// TEST 1: Print u8 buffer
|
||||||
|
printf("TEST 1: Print u8 buffer\n");
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
__u8 *u8;
|
||||||
|
|
||||||
|
// Allocate memory
|
||||||
|
u8 = (__u8*) malloc(64);
|
||||||
|
|
||||||
|
// Fill with data
|
||||||
|
for ( i = 0 ; i < 64 ; i ++ )
|
||||||
|
u8[i] = i;
|
||||||
|
|
||||||
|
// Print buffer
|
||||||
|
autl_prnt_buf(u8, 64, 16, 1);
|
||||||
|
|
||||||
|
// Clean up
|
||||||
|
free(u8);
|
||||||
|
}
|
||||||
|
|
||||||
|
// TEST 2: convert CSV string of __u8 values into a __u8[] array
|
||||||
|
printf("\n");
|
||||||
|
printf("TEST 2: convert CSV string of __u8 values into a __u8[] array\n");
|
||||||
|
{
|
||||||
|
int rv;
|
||||||
|
char str[] = "1,2,3,4,5,6,7,8,9,10";
|
||||||
|
__u8 array[32];
|
||||||
|
|
||||||
|
memset(array, 0, 32);
|
||||||
|
|
||||||
|
rv = autl_csv_to_u8(array, str, 32, 0);
|
||||||
|
|
||||||
|
printf("STR: %s\n", str);
|
||||||
|
printf("Number of values converted: %d\n", rv);
|
||||||
|
autl_prnt_buf(array, 32, 4, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// TEST 3: convert CSV string of __u8 values into a __u8[] array with trailing ,
|
||||||
|
printf("\n");
|
||||||
|
printf("TEST 3: convert CSV string of __u8 values into a __u8[] array with trailing ,\n");
|
||||||
|
{
|
||||||
|
int rv;
|
||||||
|
char str[] = "1,2,3,4,5,6,7,8,9,10,";
|
||||||
|
__u8 array[32];
|
||||||
|
|
||||||
|
rv = autl_csv_to_u8(array, str, 32, 0);
|
||||||
|
|
||||||
|
printf("STR: %s\n", str);
|
||||||
|
printf("Number of values converted: %d\n", rv);
|
||||||
|
autl_prnt_buf(array, 10, 4, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// TEST 4: Convert a mixed decimal / hex string
|
||||||
|
printf("\n");
|
||||||
|
printf("TEST 4: Convert a mixed decimal / hex string\n");
|
||||||
|
{
|
||||||
|
int rv;
|
||||||
|
char str[] = "0x1,0x2,0x3,4,5,6,7,8,9,10,0x10";
|
||||||
|
__u8 array[32];
|
||||||
|
|
||||||
|
memset(array, 0, 32);
|
||||||
|
|
||||||
|
rv = autl_csv_to_u8(array, str, 32, 0);
|
||||||
|
|
||||||
|
printf("STR: %s\n", str);
|
||||||
|
printf("Number of values converted: %d\n", rv);
|
||||||
|
autl_prnt_buf(array, 32, 4, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// TEST 5: Test max count
|
||||||
|
printf("\n");
|
||||||
|
printf("TEST 5: Test max count\n");
|
||||||
|
{
|
||||||
|
int rv;
|
||||||
|
char str[] = "0x1,0x2,0x3,4,5,6,7,8,9,10,0x10";
|
||||||
|
__u8 array[32];
|
||||||
|
|
||||||
|
memset(array, 0, 32);
|
||||||
|
|
||||||
|
rv = autl_csv_to_u8(array, str, 5, 0);
|
||||||
|
|
||||||
|
printf("STR: %s\n", str);
|
||||||
|
printf("Number of values converted: %d\n", rv);
|
||||||
|
autl_prnt_buf(array, 32, 4, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// TEST 6: Test base 16
|
||||||
|
printf("\n");
|
||||||
|
printf("TEST 6: Test base 16\n");
|
||||||
|
{
|
||||||
|
int rv;
|
||||||
|
char str[] = "0x1,0x2,0x3,4,5,6,7,8,9,10,0x10";
|
||||||
|
__u8 array[32];
|
||||||
|
|
||||||
|
memset(array, 0, 32);
|
||||||
|
|
||||||
|
rv = autl_csv_to_u8(array, str, 32, 1);
|
||||||
|
|
||||||
|
printf("STR: %s\n", str);
|
||||||
|
printf("Number of values converted: %d\n", rv);
|
||||||
|
autl_prnt_buf(array, 32, 4, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// TEST 7: Test __u64 conversion
|
||||||
|
printf("\n");
|
||||||
|
printf("TEST 7: Test __u64 conversion\n");
|
||||||
|
{
|
||||||
|
int rv;
|
||||||
|
char str[] = "12345678, 0x12345678, 0x12345678a1a2a3a4";
|
||||||
|
__u64 array[8];
|
||||||
|
|
||||||
|
memset(array, 0, 8*sizeof(__u64));
|
||||||
|
|
||||||
|
rv = autl_csv_to_u64(array, str, 8, 0);
|
||||||
|
|
||||||
|
printf("STR: %s\n", str);
|
||||||
|
printf("Number of values converted: %d\n", rv);
|
||||||
|
autl_prnt_buf(array, 8*sizeof(*array), 4, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// TEST 8: Test __u64 conversion all hex
|
||||||
|
printf("\n");
|
||||||
|
printf("TEST 8: Test __u64 conversion all hex\n");
|
||||||
|
{
|
||||||
|
int rv;
|
||||||
|
char str[] = "12345678, 0x12345678, 0x12345678a1a2a3a4";
|
||||||
|
__u64 array[8];
|
||||||
|
|
||||||
|
memset(array, 0, 8*sizeof(__u64));
|
||||||
|
|
||||||
|
rv = autl_csv_to_u64(array, str, 8, 1);
|
||||||
|
|
||||||
|
printf("STR: %s\n", str);
|
||||||
|
printf("Number of values converted: %d\n", rv);
|
||||||
|
autl_prnt_buf(array, 8*sizeof(*array), 4, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user