descriptionCLI utility to swap consecutive hex digits (nibbles) in a string
owneralessio.chiapperini@nullbuffer.com
last changeFri, 9 Dec 2022 10:59:13 +0000 (9 11:59 +0100)
content tags
add:
README.md

nibswap

The nibswap utility swaps consecutive hex digits (nibbles) in a string.

Features

Limitations

Build dependencies

The only dependency is a toolchain supporting the following flags:

CFLAGS = -std=c99 -O2 -Wall -Wextra -Wpedantic \
        -Walloca -Wcast-qual -Wconversion -Wformat=2 -Wformat-security \
        -Wnull-dereference -Wstack-protector -Wvla -Warray-bounds \
        -Wbad-function-cast -Wconversion -Wshadow -Wstrict-overflow=4 -Wundef \
        -Wstrict-prototypes -Wswitch-default -Wfloat-equal -Wimplicit-fallthrough \
        -Wpointer-arith -Wswitch-enum \
        -D_FORTIFY_SOURCE=2 \
        -fstack-protector-strong -fPIE -fstack-clash-protection

LDFLAGS = -Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack -Wl,-z,separate-code

Otherwise you can just remove the security flags and compile it with

CFLAGS = -std=c99 -O2 -Wall -Wextra -Wpedantic
LDFLAGS =

or pass your own flags to make

make CC=gcc CFLAGS=... LDFLAGS=...

Installation

Clone this repository then

$ make PREFIX=/usr install

This will install the compiled binary under PREFIX (/usr/bin) in this case, if not specified PREFIX will default to /usr/local. For staged installs, DESTDIR is also supported. As the binary does not have any dependency it does not have to be installed before use.

Usage

nibswap receives as input one or more files. The files however, can be omitted, in this case the program takes the input from the standard input until EOF or ^D is reached. If a file is a single dash (‘-’), nibswap reads from standard input.

The options are as follows:

Examples

$ nibswap
A1B2C3
1A2B3C
$ cat strings
A1B2C3
1A2B3C4D

$ nibswap strings
1A2B3C
A1B2C3D4

Test suite

The test suite consists of a POSIX shell script called harness.sh contained in the test folder. It's output is similar to googletest's and it can be invoked with make test which, if everything is working should output something similar to

(cd test && ./harness.sh)
[----------] Test environment set-up.
[==========] Running 7 test cases.
[ RUN      ] should_handle_stdin
[       OK ] should_handle_stdin
[ RUN      ] should_handle_one_file
[       OK ] should_handle_one_file
[ RUN      ] should_handle_two_files
[       OK ] should_handle_two_files
[ RUN      ] should_handle_dash
[       OK ] should_handle_dash
[ RUN      ] should_refuse_odd_length_string
nibswap: the length of the string 'A1B2C' must be even
[       OK ] should_refuse_odd_length_string
[ RUN      ] should_handle_empty_string
[       OK ] should_handle_empty_string
[ RUN      ] should_handle_long_string
[       OK ] should_handle_long_string
[==========] 7 test cases ran.
[  PASSED  ] 7 tests.
[  FAILED  ] 0 tests.
[----------] Test environment teardown.

Static analysis

Static analysis on the code base is done by using clang's static analyzer run through scan-build.sh which wraps the scan-build utility. The checkers used are part of the Experimental Checkers (aka alpha checkers):

License

BSD 2-Clause FreeBSD License, see LICENSE.

shortlog
2022-12-09 Alessio ChiapperiniSuppress error during testmaster
2022-10-10 Alessio ChiapperiniMove const qualifier at the beginning
2022-10-10 Alessio ChiapperiniRemove quotes from modulo computation
2022-10-09 Alessio ChiapperiniUpdate README
2022-10-08 Alessio ChiapperiniUpdate README
2022-10-08 Alessio ChiapperiniAdd long string test case
2022-10-07 Alessio ChiapperiniUpdate README.md and manpage
2022-10-07 Alessio ChiapperiniAdd everything
2022-10-07 Alessio ChiapperiniAdd empty README.md
heads
16 months ago master