Skip to content

C++ Binary Serialization Made Easy: Header-only, cross-platform, configurable endianness, no IDL, and no exceptions.

License

Notifications You must be signed in to change notification settings

masscollaborationlabs/btool

 
 

Repository files navigation

BytePack: Simple C++ Binary Serialization Library

BytePack: Simple C++ Binary Serialization Library Logo

License: MIT Windows build-test status GNU/Linux build-test status macOS build-test status

Header-only C++20 library designed for efficient and flexible binary serialization and deserialization, allowing users to specify the endianness. BytePack does not enforce specific standardization (IDL, metadata, etc.), making it highly adaptable for projects that rely on external interface, custom data formats, such as those found in Interface Control Documents (ICD), Interface Design Description (IDD), etc. BytePack seamlessly works with such specifications, making it straightforward to integrate into existing systems.

Features

  • Header-Only: Easy to integrate, just include bytepack.hpp in your project.
  • No Library-Specific Encoding: Avoids any custom encoding (IDL, metadata, etc.) facilitating interoperability and simplifying the troubleshooting of network packets.
  • Configurable Endianness Support: Choose the desired endianness for data serialization and deserialization.
  • No Exceptions: Ensures stable and predictable error handling.
  • Flexible Buffer Management: Utilizes a non-owning mutable buffer concept for efficient memory management.
  • Easy to Debug and Customize: Clear and concise codebase makes debugging easy. Adaptable for customization to address specific requirements.
  • Cross-platform compatible: Tested on Windows, GNU/Linux and macOS.

Usage Example

This is just one example among many possible scenarios demonstrating the diverse applications and versatility of BytePack. It provides a glimpse into the project's potential, encouraging users to explore a wide range of other use cases.

// Sample struct for a conceptual usage scenario
struct SensorData {
    std::int64_t timestamp;  // UNIX timestamp of measurement
    double value;            // Measured value
    char sensor_id[16];      // Identifier of the sensor

    void serialize(bytepack::binary_stream<>& stream) const {
        stream.write(timestamp);
        stream.write(value);
        stream.write(sensor_id);
    }

    void deserialize(bytepack::binary_stream<>& stream) {
        stream.read(timestamp);
        stream.read(value);
        stream.read(sensor_id);
    }
};

SensorData sensorData{ 1701037875, 23.6, "Sensor-001" };

// Default endianness is big-endian (network byte order).
// little-endian configuration: `bytepack::binary_stream<std::endian::little>`
// You can also supply your own buffer to serialize onto.
bytepack::binary_stream serializationStream(64); // 64 is the buffer size in bytes

sensorData.serialize(serializationStream);

bytepack::buffer_view buffer = serializationStream.data();

// e.g. char* dataPtr = buffer.as<char>();
std::uint8_t* dataPtr = buffer.as<std::uint8_t>();
std::size_t dataSize = buffer.size(); // returns the serialized data size in bytes
// you can send the dataPtr to a socket, etc.

// If you want to deserialize the data coming from a socket, etc.,
// you can initialize non-owning mutable buffer with the data pointer and size.
// e.g. `bytepack::buffer_view buffer(dataPtr, dataSize);`
bytepack::binary_stream deserializationStream(buffer);

SensorData sensorData_{};
sensorData_.deserialize(deserializationStream);

Requirements

  • Implemented in C++20 (uses concepts)
  • CMake 3.12 or higher.
  • GNU/Linux: GCC 10.1 or higher, Clang 11 or higher.
  • Windows: Visual Studio 2019 version 16.11.14 or higher.
  • macOS: Xcode 14.3 or higher.

Installation

Simply clone the repository or download the bytepack.hpp file and include it in your project.

git clone https://github.com/farukeryilmaz/bytepack.git

Include the library in your C++ project:

#include "bytepack/bytepack.hpp"

Design Philosophy

For more details: Motivation & Design Philosophy of BytePack

BytePack is a C++ library crafted with a clear focus on simplicity and flexibility in binary serialization, primarily for network communication. It does not enforce any standardization, versioning, or the use of Interface Description Language (IDL) in serialization, providing you with the freedom to define your data structures and protocols. This approach is ideal when interfacing with systems where data formats and protocols are defined externally, as is often the case in standards like IEEE 12207, Interface Control Documents (ICD), Interface Design Description (IDD), and other industry-specific specifications. It allows you to seamlessly integrate BytePack into diverse projects, accommodating a wide range of requirements and compliance standards.

Contributions and Feedback

Contributions are welcome! If you encounter issues, have suggestions, or want to contribute to the development of the library, please read the Contribution Guideline.

For any questions, feedback, or inquiries, you can also reach out to me directly:

  • Email: faruk [at] farukeryilmaz [dot] com
  • X (Twitter): @farukerylmz

License

BytePack is licensed under the MIT License.

MIT License

Copyright (c) 2023 Faruk Eryilmaz and contributors

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

C++ Binary Serialization Made Easy: Header-only, cross-platform, configurable endianness, no IDL, and no exceptions.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C++ 99.2%
  • CMake 0.8%