From: Philippe Proulx Date: Mon, 25 Apr 2022 15:10:04 +0000 (-0400) Subject: src/cpp-common: add fixed-length integer reading functions X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=commitdiff_plain;h=82a36560253182a9e8bacf6a7a9fc30fc2826f71 src/cpp-common: add fixed-length integer reading functions This patch adds `src/cpp-common/read-fixed-len-int.hpp` which offers function templates to read fixed-length integers from a buffer. The bt2_common::readFixedLenIntLe() (little endian) and bt2_common::readFixedLenIntBe() (big endian) function templates accept one template parameter which is the integral type of the integer to read (and the return type of the function). For example, bt2_common::readFixedLenIntLe() reads a 32-bit signed little-endian integer and returns it. The function templates use bt2_common::littleEndianToNative() and bt2_common::bigEndianToNative() as needed. You may combine this with `bt2_common::StdIntT` to have the length in bits and the signedness as independent template parameters, for example: const auto val = readFixedLenIntBe>(myBuf); which is equivalent to: const auto val = readFixedLenIntBe(myBuf); Signed-off-by: Philippe Proulx Change-Id: I9c684bede60f7b6324a3140c5fd498c21ad513ab Reviewed-on: https://review.lttng.org/c/babeltrace/+/7939 Reviewed-by: Francis Deslauriers Reviewed-on: https://review.lttng.org/c/babeltrace/+/10840 Tested-by: jenkins --- diff --git a/src/cpp-common/Makefile.am b/src/cpp-common/Makefile.am index c9c07af4..534e6388 100644 --- a/src/cpp-common/Makefile.am +++ b/src/cpp-common/Makefile.am @@ -19,4 +19,5 @@ EXTRA_DIST = bt2 \ align.hpp \ uuid.hpp \ vector.hpp \ - std-int.hpp + std-int.hpp \ + read-fixed-len-int.hpp diff --git a/src/cpp-common/read-fixed-len-int.hpp b/src/cpp-common/read-fixed-len-int.hpp new file mode 100644 index 00000000..27799e17 --- /dev/null +++ b/src/cpp-common/read-fixed-len-int.hpp @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2022 Philippe Proulx + * + * SPDX-License-Identifier: MIT + */ + +#ifndef BABELTRACE_CPP_COMMON_READ_FIXED_LEN_INT_HPP +#define BABELTRACE_CPP_COMMON_READ_FIXED_LEN_INT_HPP + +#include +#include +#include +#include + +#include "cpp-common/endian.hpp" + +namespace bt2_common { + +/* + * Reads a fixed-length little-endian integer into a value of integral + * type `IntT` from the buffer `buf` and returns it. + */ +template +IntT readFixedLenIntLe(const std::uint8_t * const buf) +{ + static_assert(std::is_integral::value, "`IntT` is an integral type."); + + IntT val; + + std::memcpy(&val, buf, sizeof(val)); + return bt2_common::littleEndianToNative(val); +} + +/* + * Reads a fixed-length big-endian integer into a value of integral + * type `IntT` from the buffer `buf` and returns it. + */ +template +IntT readFixedLenIntBe(const std::uint8_t * const buf) +{ + static_assert(std::is_integral::value, "`IntT` is an integral type."); + + IntT val; + + std::memcpy(&val, buf, sizeof(val)); + return bt2_common::bigEndianToNative(val); +} + +} /* namespace bt2_common */ + +#endif /* BABELTRACE_CPP_COMMON_READ_FIXED_LEN_INT_HPP */