Skip to content

Commit

Permalink
Switched to header-only
Browse files Browse the repository at this point in the history
  • Loading branch information
flybrianfly committed Dec 13, 2021
1 parent aa1b9b4 commit fcce104
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 107 deletions.
1 change: 0 additions & 1 deletion .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ Lint:
- bfs
script:
- cpplint --verbose=0 src/leb128.h
- cpplint --verbose=0 src/leb128.cpp

Compile:
stage: build
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## v2.1.0
- Removed the C++20 requirement
- Switched to header-only

## v2.0.1
- Conditionally include Arduino.h header.

Expand Down
16 changes: 3 additions & 13 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
cmake_minimum_required(VERSION 3.13)
# Project information
project(Leb128
VERSION 2.0.1
VERSION 2.1.0
DESCRIPTION "Little endian base 128 variable-length encoding."
LANGUAGES CXX
)
# Compile options
add_compile_options(
$<$<COMPILE_LANGUAGE:CXX>:-std=c++20>
)
# Add the library target
add_library(leb128
src/leb128.h
src/leb128.cpp
)
add_library(leb128 INTERFACE)
# Setup include directories
target_include_directories(leb128 PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
$<INSTALL_INTERFACE:include>
)
target_include_directories(leb128 PUBLIC INTERFACE src/)

# Example and unit testing if this project is built separately
if (PROJECT_NAME STREQUAL CMAKE_PROJECT_NAME)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
![Bolder Flight Systems Logo](img/logo-words_75.png) &nbsp; &nbsp; ![Arduino Logo](img/arduino_logo_75.png)

# Leb128
Signed little endian base 128 (LEB128) implementation, which stores arbitrarily large signed integers in a variable length format. This library is compatible with Arduino ARM and with CMake build systems.
Signed little endian base 128 (LEB128) implementation, which stores arbitrarily large signed integers in a variable length format. This library is compatible with Arduino ARM and with CMake build systems. It would also be easy to include with other projects, since it is a header only library consisting of a single file.
* [License](LICENSE.md)
* [Changelog](CHANGELOG.md)
* [Contributing guide](CONTRIBUTING.md)
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Bolder Flight Systems LEB128
version=2.0.1
version=2.1.0
author=Brian Taylor <brian.taylor@bolderflight.com>
maintainer=Brian Taylor <brian.taylor@bolderflight.com>
sentence=LEB128 library.
Expand Down
89 changes: 0 additions & 89 deletions src/leb128.cpp

This file was deleted.

53 changes: 51 additions & 2 deletions src/leb128.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,58 @@
namespace bfs {

std::size_t EncodeLeb128(int64_t val, uint8_t * const data,
const std::size_t len);
const std::size_t len) {
if (!data) {return 0;}
bool negative = (val < 0);
std::size_t i = 0;
while (1) {
/* Prevent buffer overflow */
if (i < len) {
uint8_t b = val & 0x7F;
/* Ensure an arithmetic shift */
val >>= 7;
if (negative) {
val |= (~0ULL << 57);
}
if (((val == 0) && (!(b & 0x40))) ||
((val == -1) && (b & 0x40))) {
data[i++] = b;
return i;
} else {
data[i++] = b | 0x80;
}
} else {
return 0;
}
}
}

std::size_t DecodeLeb128(uint8_t const * const data, const std::size_t len,
int64_t * const val);
int64_t * const val) {
/* Null pointer check */
if ((!data) || (!val)) {return 0;}
int64_t res = 0;
std::size_t shift = 0;
std::size_t i = 0;
while (1) {
if (i < len) {
uint8_t b = data[i++];
uint64_t slice = b & 0x7F;
res |= slice << shift;
shift += 7;
if (!(b & 0x80)) {
if ((shift < 64) && (b & 0x40)) {
*val = res | (-1ULL) << shift;
return i;
}
*val = res;
return i;
}
} else {
return 0;
}
}
}

} // namespace bfs

Expand Down
1 change: 1 addition & 0 deletions tests/leb128_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include "leb128.h"
#include "gtest/gtest.h"
#include <limits>

/* Test minimum input */
TEST(Leb128, MinInput) {
Expand Down

0 comments on commit fcce104

Please sign in to comment.