Commit c3d0f1de authored by Greg Williams's avatar Greg Williams
Browse files

Refactored test utility for API changes, and consolidated all code into...

Refactored test utility for API changes, and consolidated all code into main.c, since the individual source files ended up being too small to break out.
Reenabled validation of test utility, linking against kinetic-c lib, dynamic lib generation, and updated installation.
parent 7a2eef9b
Loading
Loading
Loading
Loading
+99 −142
Original line number Diff line number Diff line
PROJECT = kinetic-c-client
UTILITY = kinetic-c-util

#===============================================================================
# Configuration of Shared Paths
#===============================================================================
OUT_DIR = ./obj
BIN_DIR = ./bin
PUB_INC = ./include
API_NAME = kinetic_client
LIB_DIR = ./src/lib
PBC_INC = ./vendor/protobuf-c
PBC_LIB = ./vendor/protobuf-c
SOCKET99 = ./vendor/socket99
VND_INC = ./vendor
BIN = $(BIN_DIR)/kinetic_client
LDFLAGS += -lm -l crypto -l ssl

PREFIX ?= /usr/local
INSTALL ?= install
RM ?= rm

VERSION_FILE = ./VERSION
VERSION = ${shell head -n1 $(VERSION_FILE)}
KINETIC_LIB_NAME = $(PROJECT).$(VERSION)
KINETIC_LIB = $(BIN_DIR)/lib$(KINETIC_LIB_NAME).a
KINETIC_SO_DEV = $(BIN_DIR)/lib$(KINETIC_LIB_NAME).so
KINETIC_SO_RELEASE = $(PREFIX)/lib$(KINETIC_LIB_NAME).so
#===============================================================================
# Shared Build Variables
#===============================================================================
CC = gcc
OPTIMIZE = -O3
WARN = -Wall -Wextra -pedantic
# This is necessary because the library depends on
# both C99 _and_ POSIX (for the BSD sockets API).
CDEFS += -D_POSIX_C_SOURCE=1 -D_C99_SOURCE=1
CFLAGS += -std=c99 -fPIC -g $(WARN) $(CDEFS) $(OPTIMIZE)
LDFLAGS += -lm -l crypto -l ssl

#===============================================================================
# Kinetic-C Library Build Support
#===============================================================================

PROJECT = kinetic-c-client

LIB_DIR = ./src/lib
VENDOR = ./vendor
PROTOBUFC = $(VENDOR)/protobuf-c
SOCKET99 = $(VENDOR)/socket99
VERSION_FILE = ./VERSION
VERSION = ${shell head -n1 $(VERSION_FILE)}

LIB_INCS = -I$(LIB_DIR) -I$(PUB_INC) -I$(PBC_LIB) -I$(PBC_INC) -I$(VND_INC)
KINETIC_LIB_NAME = $(PROJECT).$(VERSION)
KINETIC_LIB = $(BIN_DIR)/lib$(KINETIC_LIB_NAME).a
LIB_INCS = -I$(LIB_DIR) -I$(PUB_INC) -I$(PROTOBUFC) -I$(VENDOR)
LIB_DEPS = $(PUB_INC)/kinetic_client.h $(PUB_INC)/byte_array.h $(PUB_INC)/kinetic_types.h $(LIB_DIR)/kinetic_connection.h $(LIB_DIR)/kinetic_hmac.h $(LIB_DIR)/kinetic_logger.h $(LIB_DIR)/kinetic_message.h $(LIB_DIR)/kinetic_nbo.h $(LIB_DIR)/kinetic_operation.h $(LIB_DIR)/kinetic_pdu.h $(LIB_DIR)/kinetic_proto.h $(LIB_DIR)/kinetic_socket.h $(LIB_DIR)/kinetic_types_internal.h
# LIB_OBJ = $(patsubst %,$(OUT_DIR)/%,$(LIB_OBJS))
LIB_OBJS = $(OUT_DIR)/kinetic_allocator.o $(OUT_DIR)/kinetic_nbo.o $(OUT_DIR)/kinetic_operation.o $(OUT_DIR)/kinetic_pdu.o $(OUT_DIR)/kinetic_proto.o $(OUT_DIR)/kinetic_socket.o $(OUT_DIR)/kinetic_message.o $(OUT_DIR)/kinetic_logger.o $(OUT_DIR)/kinetic_hmac.o $(OUT_DIR)/kinetic_connection.o $(OUT_DIR)/kinetic_types.o $(OUT_DIR)/kinetic_types_internal.o $(OUT_DIR)/byte_array.o $(OUT_DIR)/kinetic_client.o $(OUT_DIR)/socket99.o $(OUT_DIR)/protobuf-c.o
KINETIC_LIB_OTHER_DEPS = Makefile Rakefile $(VERSION_FILE)

default: $(KINETIC_LIB)

all: clean test default run

ci: uninstall all install
	@echo
	@echo --------------------------------------------------------------------------------
	@echo $(PROJECT) build completed successfully!
	@echo --------------------------------------------------------------------------------
	@echo $(PROJECT) v$(VERSION) is in working order
	@echo

test: Rakefile $(LIB_OBJS)
	@echo
	@echo --------------------------------------------------------------------------------
@@ -53,7 +63,6 @@ clean:

# $(OUT_DIR)/%.o: %.c $(DEPS)
# 	$(CC) -c -o $@ $< $(CFLAGS)

$(OUT_DIR)/kinetic_allocator.o: $(LIB_DIR)/kinetic_allocator.c $(LIB_DEPS)
	$(CC) -c -o $@ $< $(CFLAGS) $(LIB_INCS)
$(OUT_DIR)/kinetic_types_internal.o: $(LIB_DIR)/kinetic_types_internal.c $(LIB_DEPS)
@@ -82,12 +91,22 @@ $(OUT_DIR)/byte_array.o: $(LIB_DIR)/byte_array.c $(LIB_DEPS)
	$(CC) -c -o $@ $< $(CFLAGS) $(LIB_INCS)
$(OUT_DIR)/socket99.o: $(SOCKET99)/socket99.c $(SOCKET99)/socket99.h
	$(CC) -c -o $@ $< $(CFLAGS) -I$(SOCKET99)
$(OUT_DIR)/protobuf-c.o: $(PBC_LIB)/protobuf-c/protobuf-c.c $(PBC_LIB)/protobuf-c/protobuf-c.h
	$(CC) -c -o $@ $< -std=c99 -fPIC -g -Wall $(OPTIMIZE) -I$(PBC_LIB)
$(OUT_DIR)/protobuf-c.o: $(PROTOBUFC)/protobuf-c/protobuf-c.c $(PROTOBUFC)/protobuf-c/protobuf-c.h
	$(CC) -c -o $@ $< -std=c99 -fPIC -g -Wall $(OPTIMIZE) -I$(PROTOBUFC)
$(OUT_DIR)/kinetic_client.o: $(LIB_DIR)/kinetic_client.c $(LIB_DEPS)
	$(CC) -c -o $@ $< $(CFLAGS) $(LIB_INCS)

$(KINETIC_LIB): $(LIB_OBJS) $(VERSION_FILE)


#-------------------------------------------------------------------------------
# Static and Dynamic Library Build Support
#-------------------------------------------------------------------------------

PREFIX ?= /usr/local
KINETIC_SO_DEV = $(BIN_DIR)/lib$(KINETIC_LIB_NAME).so
KINETIC_SO_RELEASE = $(PREFIX)/lib$(KINETIC_LIB_NAME).so

$(KINETIC_LIB): $(LIB_OBJS) $(KINETIC_LIB_OTHER_DEPS)
	@echo
	@echo --------------------------------------------------------------------------------
	@echo Building static library: $(KINETIC_LIB)
@@ -95,114 +114,24 @@ $(KINETIC_LIB): $(LIB_OBJS) $(VERSION_FILE)
	ar -rcs $@ $(LIB_OBJS)
	ar -t $@

$(KINETIC_SO): $(KINETIC_LIB)
$(KINETIC_SO_DEV): $(LIB_OBJS) $(KINETIC_LIB_OTHER_DEPS)
	@echo
	@echo --------------------------------------------------------------------------------
	@echo Building dynamic library: $(KINETIC_SO)
	@echo Building dynamic library: $(KINETIC_SO_DEV)
	@echo --------------------------------------------------------------------------------
	$(CC)(-) $(KINETIC_SO_DEV) -shared $(LDFLAGS) $(LIB_OBJS)
	$(CC) -o $@ -shared $(LDFLAGS) $(LIB_OBJS)

libso: $(KINETIC_SO)

UTIL_DIR = ./src/utility
UTIL_EX = $(UTIL_DIR)/examples
UTIL_EXEC_NAME = $(UTILITY)
UTIL_INTERNAL_OBJS = $(OUT_DIR)/noop.o $(OUT_DIR)/put.o $(OUT_DIR)/get.o $(OUT_DIR)/delete.o
UTIL_OBJS = $(OUT_DIR)/main.o $(UTIL_INTERNAL_OBJS)
DEV_UTIL_INCS = -I./include -I$(UTIL_DIR)
DEV_UTIL_LDFLAGS += $(LDFLAGS) $(KINETIC_LIB)

$(OUT_DIR)/noop.o: $(UTIL_EX)/noop.c $(UTIL_EX)/noop.h
	$(CC) -c -o $@ $< $(CFLAGS) $(DEV_UTIL_INCS)
$(OUT_DIR)/put.o: $(UTIL_EX)/put.c $(UTIL_EX)/put.h
	$(CC) -c -o $@ $< $(CFLAGS) $(DEV_UTIL_INCS)
$(OUT_DIR)/get.o: $(UTIL_EX)/get.c $(UTIL_EX)/get.h
	$(CC) -c -o $@ $< $(CFLAGS) $(DEV_UTIL_INCS)
$(OUT_DIR)/delete.o: $(UTIL_EX)/delete.c $(UTIL_EX)/delete.h
	$(CC) -c -o $@ $< $(CFLAGS) $(DEV_UTIL_INCS)
$(OUT_DIR)/main.o: $(UTIL_DIR)/main.c $(UTIL_INTERNAL_OBJS)
	$(CC) -c -o $@ $< $(CFLAGS) $(DEV_UTIL_INCS)

UTIL_NAME = $(UTIL_EXEC_NAME)
DEV_UTIL_EXEC = $(BIN_DIR)/$(DEV_UTIL_NAME)

$(DEV_UTIL_EXEC): $(UTIL_OBJS) $(KINETIC_LIB)
	@echo
	@echo --------------------------------------------------------------------------------
	@echo Building development test utility: $(DEV_UTIL_EXEC)
	@echo --------------------------------------------------------------------------------
	$(CC) -o $@ $(UTIL_OBJS) $(CFLAGS) $(DEV_UTIL_LDFLAGS)

# utility: $(UTIL_EXEC) $(REL_UTIL_DYN)
utility: $(DEV_UTIL_EXEC)
#-------------------------------------------------------------------------------
# Installation Support
#-------------------------------------------------------------------------------

# Configure to launch java simulator
# JAVA=$(JAVA_HOME)/bin/java
SIM_JARS_PREFIX = vendor/kinetic-java/kinetic-simulator-0.7.0.2-kinetic-proto-2.0.6-SNAPSHOT
CLASSPATH = $(JAVA_HOME)/lib/tools.jar:$(SIM_JARS_PREFIX)-jar-with-dependencies.jar:$(SIM_JARS_PREFIX)-sources.jar:$(SIM_JARS_PREFIX).jar
SIM_RUNNER = com.seagate.kinetic.simulator.internal.SimulatorRunner
SIM_ADMIN = com.seagate.kinetic.admin.cli.KineticAdminCLI

run: $(DEV_UTIL_EXEC)
	@echo
	@echo --------------------------------------------------------------------------------
	@echo Running test utility: $(DEV_UTIL_EXEC)
	@echo --------------------------------------------------------------------------------
	@sleep 2
	exec java -classpath "$(CLASSPATH)" $(SIM_RUNNER) "$@" &
	@sleep 5
	exec java -classpath "$(CLASSPATH)" $(SIM_ADMIN) -setup -erase true
	$(DEV_UTIL_EXEC) noop
	$(DEV_UTIL_EXEC) put
	$(DEV_UTIL_EXEC) get
	$(DEV_UTIL_EXEC) delete
	exec pkill -f 'java.*kinetic-simulator'
API_NAME = kinetic_client
INSTALL ?= install
RM ?= rm

# # Release build of test utility against installed library
# REL_UTIL_INCS = -I$(PREFIX)/include -I$(UTIL_DIR)
# REL_UTIL_LDFLAGS += $(LDFLAGS) -l $(KINETIC_LIB)
#
# REL_UTIL_EXEC_STATIC_NAME = $(UTIL_EXEC_NAME)-rel-static
# REL_UTIL_EXEC_STATIC = $(BIN_DIR)/$(REL_UTIL_EXEC_STATIC_NAME)
#
# $(REL_UTIL_EXEC_STATIC): $(UTIL_OBJS)
# 	@echo
# 	@echo --------------------------------------------------------------------------------
# 	@echo Building release $(UTIL_EXEC) against installed static library
# 	@echo --------------------------------------------------------------------------------
# 	$(CC) -o $@ $(UTIL_OBJS) $(REL_UTIL_INCS) $(CFLAGS) $(LDFLAGS) 
#
# REL_UTIL_EXEC_NAME = $(UTIL_EXEC_NAME)
# REL_UTIL_EXEC = $(BIN_DIR)/$(REL_UTIL_EXEC_NAME)
#
# $(REL_UTIL_EXEC): $(UTIL_DIR)/main.c $(UTIL_OBJS)
# 	@echo
# 	@echo --------------------------------------------------------------------------------
# 	@echo Building release $(UTIL_EXEC) against installed dynamic library
# 	@echo --------------------------------------------------------------------------------
# 	$(CC) -o $@ -L$(PREFIX) -l $(PROJECT).so.$(VERSION) $< $(UTIL_OBJS) $(REL_UTIL_INCS) $(CFLAGS) $(LDFLAGS)
# rund: $(REL_UTIL_DYN)
# 	@echo
# 	@echo --------------------------------------------------------------------------------
# 	@echo Running $(UTIL_EXEC) $(PROJECT) test utility \(dynamically linked\)
# 	@echo --------------------------------------------------------------------------------
# 	@sleep 2
# 	exec java -classpath "$(CLASSPATH)" $(SIM_RUNNER) "$@" &
# 	@sleep 5
# 	exec java -classpath "$(CLASSPATH)" $(SIM_ADMIN) -setup -erase true
# 	$(REL_UTIL_DYN) noop
# 	$(REL_UTIL_DYN) put
# 	$(REL_UTIL_DYN) get
# 	$(REL_UTIL_DYN) delete
# 	exec pkill -f 'java.*kinetic-simulator'

# Installation
KINETIC_LIB_NAME = $(PROJECT).$(VERSION)
KINETIC_LIB = $(BIN_DIR)/lib$(KINETIC_LIB_NAME).a
KINETIC_SO_DEV = $(BIN_DIR)/lib$(KINETIC_LIB_NAME).so
KINETIC_SO_RELEASE = $(PREFIX)/lib$(KINETIC_LIB_NAME).so
# install: $(KINETIC_LIB) $(KINETIC_SO_DEV)
install: $(KINETIC_LIB)
install: $(KINETIC_LIB) $(KINETIC_SO_DEV)
	@echo
	@echo --------------------------------------------------------------------------------
	@echo Installing $(PROJECT) v$(VERSION) into $(PREFIX)
@@ -210,10 +139,9 @@ install: $(KINETIC_LIB)
	@echo
	$(INSTALL) -d $(PREFIX)/lib/
	$(INSTALL) -c $(KINETIC_LIB) $(PREFIX)/lib/
	# $(INSTALL) -c $(KINETIC_SO_DEV) $(PREFIX)/lib/
	$(INSTALL) -d $(PREFIX)/include/
	$(INSTALL) -c ./include/$(API_NAME).h $(PREFIX)/include/
	$(INSTALL) -c ./include/kinetic_types.h $(PREFIX)/include/
	$(INSTALL) -c $(PUB_INC)/$(API_NAME).h $(PREFIX)/include/
	$(INSTALL) -c $(PUB_INC)/kinetic_types.h $(PREFIX)/include/

uninstall:
	@echo
@@ -229,18 +157,47 @@ uninstall:
	$(RM) -f $(PREFIX)/include/protobuf-c/protobuf-c.h
	$(RM) -f $(PREFIX)/include/protobuf-c.h

# all: clean test default run
all: clean test default

#ci: uninstall all install
ci: uninstall all install
#===============================================================================
# Test Utility Build Support
#===============================================================================

UTILITY = kinetic-c-util
UTIL_DIR = ./src/utility
UTIL_EXEC = $(BIN_DIR)/$(UTILITY)
UTIL_OBJ = $(OUT_DIR)/main.o

$(UTIL_OBJ): $(UTIL_DIR)/main.c
	$(CC) -c -o $@ $< $(CFLAGS) -I$(PUB_INC) -I$(UTIL_DIR)

$(UTIL_EXEC): $(UTIL_OBJ) $(KINETIC_LIB)
	@echo
	@echo --------------------------------------------------------------------------------
	@echo $(PROJECT) build completed successfully!
	@echo Building development test utility: $(UTIL_EXEC)
	@echo --------------------------------------------------------------------------------
	@echo $(PROJECT) v$(VERSION) is in working order
	@echo
	$(CC) -o $@ $< $(CFLAGS) $(LDFLAGS) $(KINETIC_LIB)

utility: $(UTIL_EXEC)

build: $(KINETIC_LIB) $(KINETIC_SO_DEV) utility

# Other dependencies
$(KINETIC_LIB): Makefile Rakefile VERSION
# kinetic-lib.o: kinetic_client.h kinetic_connection.h kinetic_hmac.h kinetic_logger.h kinetic_message.h kinetic_nbo.h kinetic_operation.h kinetic_pdu.h kinetic_proto.h kinetic_socket.h protobuf-c.h socket99.h
#-------------------------------------------------------------------------------
# Support for Simulator and Exection of Test Utility
#-------------------------------------------------------------------------------
# JAVA=$(JAVA_HOME)/bin/java
SIM_JARS_PREFIX = vendor/kinetic-java/kinetic-simulator-0.7.0.2-kinetic-proto-2.0.6-SNAPSHOT
CLASSPATH = $(JAVA_HOME)/lib/tools.jar:$(SIM_JARS_PREFIX)-jar-with-dependencies.jar:$(SIM_JARS_PREFIX)-sources.jar:$(SIM_JARS_PREFIX).jar
SIM_RUNNER = com.seagate.kinetic.simulator.internal.SimulatorRunner
SIM_ADMIN = com.seagate.kinetic.admin.cli.KineticAdminCLI

run: $(UTIL_EXEC)
	@echo
	@echo --------------------------------------------------------------------------------
	@echo Running test utility: $(UTIL_EXEC)
	@echo --------------------------------------------------------------------------------
	@sleep 2
	exec java -classpath "$(CLASSPATH)" $(SIM_RUNNER) "$@" &
	@sleep 5
	exec java -classpath "$(CLASSPATH)" $(SIM_ADMIN) -setup -erase true
	$(UTIL_EXEC) noop put get delete
	exec pkill -f 'java.*kinetic-simulator'
+11 −39
Original line number Diff line number Diff line
@@ -253,13 +253,6 @@ task :apply_license do
  end
end

desc "Validate .travis.yml config file"
namespace :travis do
  task :validate do
    execute_command "bundle exec travis lint --skip-version-check --skip-completion-check", "Validating Travis CI Configuration"
  end
end

desc "Enable verbose output"
task :verbose do
  Rake::Task[:verbosity].invoke(3) # Set verbosity to 3:semi-obnoxious for debugging
@@ -316,14 +309,9 @@ namespace :tests do
  end

  desc "Run Kinetic Client Utility tests"
  task :utility => [
    'release',
    'ruby_sim:shutdown',
    'tests:utility:noop',
    'tests:utility:put',
    'tests:utility:get',
    'tests:utility:delete',
  ]
  task :utility => ['ruby_sim:shutdown'] do
    sh "make run"
  end

  namespace :utility do

@@ -336,7 +324,7 @@ namespace :tests do
      end
    end

    task :noop => ['release', 'ruby_sim:shutdown'] do
    task :noop => ['ruby_sim:shutdown'] do
      java_sim_erase_drive
      with_test_server("Testing NoOp Operation") do
        execute_command "./kinetic-c noop"
@@ -346,14 +334,14 @@ namespace :tests do
      end
    end

    task :put => ['release', 'ruby_sim:shutdown'] do
    task :put => ['ruby_sim:shutdown'] do
      java_sim_erase_drive
      with_test_server("Testing Put operation") do
        execute_command "./kinetic-c put"
      end
    end

    task :get => ['release', 'ruby_sim:shutdown'] do
    task :get => ['ruby_sim:shutdown'] do
      java_sim_erase_drive
      with_test_server("Testing Get operation") do
        execute_command "./kinetic-c put"
@@ -392,31 +380,15 @@ task :run => ['utility'] do
  sh "make run" 
end

task :test_all => [
  'tests:unit',
  'tests:integration',
  'tests:system',
]
task :test_all => ['tests:unit', 'tests:integration', 'tests:system']

desc "Build all and run test utility"
task :all => [
  'cppcheck',
  'test_all',
  'lib',
  #'utility',
  #'run'
]
task :all => ['cppcheck', 'test_all', 'lib', 'utility', 'run']

desc "Run full CI build"
task :ci => [
  'clobber',
  'all'
]

task :default => [
  'test:delta',
  'release'
]
task :ci => ['clobber', 'all']

task :default => ['test:delta']

END {
  # Ensure java simlator is shutdown prior to rake exiting
+3 −6
Original line number Diff line number Diff line
@@ -133,13 +133,11 @@ typedef struct _KineticSession {
    memcpy((_session)->hmacKey.data, (_hmacKey).data, (_hmacKey).len); \
}

// Operation handle
typedef int KineticOperationHandle;

// Kinetic Status Codes
typedef enum {
    KINETIC_STATUS_INVALID = -1,        // Status not available (no reponse/status available)
    KINETIC_STATUS_SUCCESS = 0,         // Operation successful
    KINETIC_STATUS_NOT_ATTEMPTED = 0,   // No operation has been attempted
    KINETIC_STATUS_SUCCESS = 1,         // Operation successful
    KINETIC_STATUS_SESSION_EMPTY,       // Session was NULL in request
    KINETIC_STATUS_SESSION_INVALID,     // Session configuration was invalid or NULL
    KINETIC_STATUS_HOST_EMPTY,          // Host was empty in request
@@ -158,8 +156,8 @@ typedef enum {
    KINETIC_STATUS_SOCKET_ERROR,        // An I/O error occurred during a socket operation
    KINETIC_STATUS_COUNT                // Number of status codes in KineticStatusDescriptor
} KineticStatus;
extern const char* KineticStatusDescriptor[];

const char* Kinetic_GetStatusDescription(KineticStatus status);

// KineticEntry - byte arrays need to be preallocated by the client
typedef struct _KineticEntry {
@@ -174,5 +172,4 @@ typedef struct _KineticEntry {
    ByteBuffer value;
} KineticEntry;


#endif // _KINETIC_TYPES_H
+15 −15
Original line number Diff line number Diff line
@@ -75,56 +75,56 @@ ByteArray ByteBuffer_Consume(ByteBuffer* buffer, size_t len)
    return slice;
}

bool ByteBuffer_Append(ByteBuffer* buffer,
                       const void* data, size_t len)
ByteBuffer* ByteBuffer_Append(ByteBuffer* buffer, const void* data, size_t len)
{
    assert(buffer != NULL);
    assert(buffer->array.data != NULL);
    assert(data != NULL);
    if (len == 0 || ((buffer->bytesUsed + len) > buffer->array.len)) {
        return false;
        return NULL;
    }
    memcpy(&buffer->array.data[buffer->bytesUsed], data, len);
    buffer->bytesUsed += len;
    return true;
    return buffer;
}

bool ByteBuffer_AppendArray(ByteBuffer* buffer, const ByteArray array)
ByteBuffer* ByteBuffer_AppendArray(ByteBuffer* buffer, const ByteArray array)
{
    assert(buffer != NULL);
    assert(buffer->array.data != NULL);
    assert(array.data != NULL);
    if (array.len == 0 || ((buffer->bytesUsed + array.len) > buffer->array.len)) {
        return false;
        return NULL;
    }
    memcpy(&buffer->array.data[buffer->bytesUsed], array.data, array.len);
    buffer->bytesUsed += array.len;
    return true;
    return buffer;
}

bool ByteBuffer_AppendCString(ByteBuffer* buffer, const char* str)
ByteBuffer* ByteBuffer_AppendCString(ByteBuffer* buffer, const char* str)
{
    assert(buffer != NULL);
    assert(buffer->array.data != NULL);
    assert(str != NULL);
    int len = strlen(str);
    if (len == 0 || ((buffer->bytesUsed + len) > buffer->array.len)) {
        return false;
        return NULL;
    }
    memcpy(&buffer->array.data[buffer->bytesUsed], str, len);
    buffer->bytesUsed += len;
    return true;
    return buffer;
}

#include <stdio.h>

bool ByteBuffer_AppendDummyData(ByteBuffer* buffer, size_t len)
ByteBuffer* ByteBuffer_AppendDummyData(ByteBuffer* buffer, size_t len)
{
    assert(buffer != NULL);
    assert(buffer->array.data != NULL);
    if (len == 0 || ((buffer->bytesUsed + len) > buffer->array.len)) {
        return false;
        return NULL;
    }
    for (size_t i = 0; i < len; i++) {
        buffer->array.data[buffer->bytesUsed + i] = (uint8_t)(i & 0x0FFu);
    }
    buffer->bytesUsed += len;
    return true;
    return buffer;
}

src/lib/byte_array.h

0 → 100644
+52 −0
Original line number Diff line number Diff line
#ifndef _BYTE_ARRAY_H
#define _BYTE_ARRAY_H

#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>

/**
 * @brief Structure for handling generic arrays of bytes
 *
 * The data contained in a `ByteArray` is an arbitrary sequence of
 * bytes. It may contain embedded `NULL` characters and is not required to be
 * `NULL`-terminated.
 */
typedef struct _ByteArray {
    const size_t  len;    /**< Number of bytes in the `data` field. */
    uint8_t* const data;  /**< Pointer to an allocated array of data bytes. */
} ByteArray;

/** @brief Convenience macro to represent an empty array with no data */
#define BYTE_ARRAY_NONE (ByteArray){.len = 0, .data = NULL}

ByteArray ByteArray_Create(void* data, size_t len);
ByteArray ByteArray_CreateWithCString(char* str);
void ByteArray_FillWithDummyData(const ByteArray array);
ByteArray ByteArray_GetSlice(const ByteArray array, size_t start, size_t len);

/**
 * @brief Structure for an embedded ByteArray as a buffer
 *
 * The `bytesUsed` field is initialized to zero, and is to incremented as each
 * byte is consumed, but shall not exceed the `array` length
 */
typedef struct {
    const ByteArray array;     /**< ByteArray holding allocated array w/length = allocated size */
    size_t          bytesUsed; /**< Reflects the number of bytes used from the `array` */
} ByteBuffer;

/** @brief Convenience macro to represent an empty buffer with no data */
#define BYTE_BUFFER_NONE (ByteBuffer){.array = BYTE_ARRAY_NONE}

ByteBuffer ByteBuffer_Create(void* data, size_t max_len);
ByteBuffer ByteBuffer_CreateWithArray(ByteArray array);
void ByteBuffer_Reset(ByteBuffer* buffer);
long ByteBuffer_BytesRemaining(const ByteBuffer buffer);
ByteArray ByteBuffer_Consume(ByteBuffer* buffer, size_t len);
ByteBuffer* ByteBuffer_Append(ByteBuffer* buffer, const void* data, size_t len);
ByteBuffer* ByteBuffer_AppendArray(ByteBuffer* buffer, const ByteArray array);
ByteBuffer* ByteBuffer_AppendCString(ByteBuffer* buffer, const char* data);
ByteBuffer* ByteBuffer_AppendDummyData(ByteBuffer* buffer, size_t len);

#endif // _BYTE_ARRAY_H
Loading