Giter Site home page Giter Site logo

hominsu / bencode Goto Github PK

View Code? Open in Web Editor NEW
5.0 1.0 0.0 177 KB

Bencode parser/generator in C++20.

License: MIT License

CMake 12.20% C 4.16% C++ 82.14% Dockerfile 0.89% HCL 0.61%
bencode bencode-parser cpp generator parser bencoder deserialization serialization bittorrent cpp20

bencode's Introduction

Contributors Forks Stargazers Issues License Deploy


bencode

A header-only Bencode parser and generator for C++20. It supports both SAX and DOM style API.
Explore the docs »

Features · Examples · Building · Integration . License

Translations: English | 简体中文

Features

  • Simple, Fast. bencode library contains only header files, does not rely on Boost. You can "assemble" bencode library's Handler at will.
  • API simplicity. bencode library supports both DOM and SAX-style API, where SAX allows custom handlers for streaming processing.
  • Multiple input/output streams. bencode library has built-in string input/output streams and file input/output streams, and make full use of memory buffers to improve read and write speed.
  • STD Streams Wrapper. bencode library provides official wrappers for std::istream and std::ostream, can be combined with bencode library's built-in input/output streams.

Examples

Usage at a glance

This simple example parses a Bencode string into a document (DOM), make a simple modification of the DOM, and finally stringify the DOM to a Bencode string.

#include <cstdio>
#include <cstdlib>

#include "bencode/document.h"
#include "bencode/exception.h"
#include "bencode/string_write_stream.h"
#include "bencode/writer.h"
#include "sample.h"

int main(const int argc, char *argv[]) {
  (void)argc;
  (void)argv;

  // 1. Parse a Bencode string into DOM.
  bencode::Document doc;
  if (const auto err = doc.Parse(kSample[0]); err != bencode::error::OK) {
    puts(bencode::ParseErrorStr(err));
    return EXIT_FAILURE;
  }

  // 2. Modify it by DOM.
  auto &s = doc["creation date"];
  s.SetInteger(0);

  // 3. Stringify the DOM
  bencode::StringWriteStream os;
  bencode::Writer writer(os);
  doc.WriteTo(writer);

  // Output
  fprintf(stdout, "%.*s", static_cast<int>(os.get().length()), os.get().data());

  return 0;
}

Output:

d8:announce41:http://bttracker.debian.org:6969/announce7:comment35:"Debian CD from cdimage.debian.org"10:created by13:mktorrent 1.113:creation datei0e4:infod6:lengthi3909091328e4:name29:debian-11.6.0-amd64-DVD-1.iso12:piece lengthi262144e6:pieces41:(binary blob of the hashes of each piece)ee

Conversion between Bencode and JSON

Due to the excellent design of bencode library, the bencode library can be easily combined with neujson to achieve the mutual conversion of JSON and Bencode. Also, this is an example of SAX. Unlike DOM, SAX does not store data into memory, but writes directly from the input stream to the output stream

Bencode to JSON

#include <memory>
#include <string_view>

#include "bencode/non_copyable.h"
#include "bencode/reader.h"
#include "bencode/string_read_stream.h"
#include "sample.h"

#include "neujson/file_write_stream.h"
#include "neujson/pretty_writer.h"

template <typename Handler> class BencodeToJSON : bencode::NonCopyable {
  Handler &handler_;

public:
  explicit BencodeToJSON(Handler &handler) : handler_(handler) {}

  bool Null() { return true; }
  bool Integer(int64_t i64) { return handler_.Int64(i64); }
  bool String(std::string_view str) { return handler_.String(str); }
  bool Key(std::string_view str) { return handler_.Key(str); }
  bool StartList() { return handler_.StartArray(); }
  bool EndList() { return handler_.EndArray(); }
  bool StartDict() { return handler_.StartObject(); }
  bool EndDict() { return handler_.EndObject(); }
};

int main(const int argc, char *argv[]) {
  (void)argc;
  (void)argv;

  bencode::StringReadStream in(kSample[0]);

  neujson::FileWriteStream out(stdout);
  neujson::PrettyWriter pretty_writer(out);
  pretty_writer.SetIndent(' ', 2);
  BencodeToJSON to_json(pretty_writer);

  if (const auto err = bencode::Reader::Parse(in, to_json);
      err != bencode::error::OK) {
    puts(bencode::ParseErrorStr(err));
    return EXIT_FAILURE;
  }

  return 0;
}

Output:

{
  "announce": "http://bttracker.debian.org:6969/announce",
  "comment": "\"Debian CD from cdimage.debian.org\"",
  "created by": "mktorrent 1.1",
  "creation date": 1671279452,
  "info": {
    "length": 3909091328,
    "name": "debian-11.6.0-amd64-DVD-1.iso",
    "piece length": 262144,
    "pieces": "(binary blob of the hashes of each piece)"
  }
}

JSON to Bencode

#include <memory>
#include <string_view>

#include "bencode/file_write_stream.h"
#include "bencode/writer.h"
#include "sample.h"

#include "neujson/internal/ieee754.h"
#include "neujson/non_copyable.h"
#include "neujson/reader.h"
#include "neujson/string_read_stream.h"

template <typename Handler> class JSONToBencode : neujson::NonCopyable {
  Handler &handler_;

public:
  explicit JSONToBencode(Handler &handler) : handler_(handler) {}

  bool Null() { return handler_.Null(); }
  bool Bool(const bool b) {
    (void)b;
    return true;
  }
  bool Int32(int32_t i32) { return handler_.Integer(i32); }
  bool Int64(int64_t i64) { return handler_.Integer(i64); }
  bool Double(const neujson::internal::Double d) {
    (void)d;
    return true;
  }
  bool String(std::string_view str) { return handler_.String(str); }
  bool Key(std::string_view str) { return handler_.Key(str); }
  bool StartArray() { return handler_.StartList(); }
  bool EndArray() { return handler_.EndList(); }
  bool StartObject() { return handler_.StartDict(); }
  bool EndObject() { return handler_.EndDict(); }
};

int main(const int argc, char *argv[]) {
  (void)argc;
  (void)argv;

  neujson::StringReadStream in(kSample[1]);

  bencode::FileWriteStream out(stdout);
  bencode::Writer writer(out);
  JSONToBencode to_json(writer);

  if (const auto err = neujson::Reader::Parse(in, to_json);
      err != neujson::error::OK) {
    puts(neujson::ParseErrorStr(err));
    return EXIT_FAILURE;
  }

  return 0;
}

Output:

d8:announce41:http://bttracker.debian.org:6969/announce7:comment35:"Debian CD from cdimage.debian.org"10:created by13:mktorrent 1.113:creation datei1671279452e4:infod6:lengthi3909091328e4:name29:debian-11.6.0-amd64-DVD-1.iso12:piece lengthi262144e6:pieces41:(binary blob of the hashes of each piece)ee

Building

This project requires C++17. This library uses following projects:

When building tests:

When building examples:

All dependencies are automatically retrieved from github during building, and you do not need to configure them.

With the CMake build types, you can control whether examples and tests are built.

cmake -H. -Bbuild \
	-DCMAKE_BUILD_TYPE=Release \
	-DCMAKE_INSTALL_PREFIX=/Users/hominsu/utils/install \
	-BENCODE_BUILD_EXAMPLES=ON \
	-BENCODE_BUILD_TESTS=ON
cmake --build ./build --parallel $(nproc)
ctest -VV --test-dir ./build/ --output-on-failure
cmake --install ./build

Or just installed as a CMake package.

cmake -H. -Bbuild \
	-BENCODE_BUILD_EXAMPLES=OFF \
	-BENCODE_BUILD_TESTS=OFF
cmake --install ./build

Uinstall

cd build
make uninstall

Integration

Located with find_package in CMake.

find_package(bencode REQUIRED)
target_include_directories(foo PUBLIC ${bencode_INCLUDE_DIRS})

License

Distributed under the MIT license. See LICENSE for more information.

bencode's People

Contributors

hominsu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.