MGS::CODEC – A Lightweight and Efficient C++14 Codec Library

MGS::CODEC – A Lightweight and Efficient C++14 Codec Library

Overview

MGS::CODEC is a lightweight codec library developed based on the C++14 standard, version 0.2.1. This library provides rich data encoding and decoding functionalities, supporting various encoding formats including Base64, Base32, Base16 (hexadecimal), ASCII85, and more, featuring a simple interface, high performance, and strong extensibility.

Core Features

1. Support for Multiple Encoding Formats

  • Base16 (hexadecimal) encoding
  • Base32 (RFC 4648) encoding
  • Base64 (RFC 4648) encoding
  • ASCII85 encoding
  • Quoted-Printable encoding

2. Modern C++ Design

  • Utilizes C++14 standard features
  • Template metaprogramming optimizations
  • Iterator pattern support
  • Exception safety guarantees

3. Flexible Interface Design

  • Stream processing interface
  • Range operation support
  • Custom output containers
  • Error handling mechanisms

Code Examples

Basic Usage

#include <mgs/codec.hpp>
#include <iostream>
#include <string>
#include <vector>

using namespace mgs::codec;

void basic_example() {
    std::string original = "Hello, MGS Codec!";

    // Base64 encoding
    auto encoded = base64::encode(original);
    std::cout << "Base64 Encoded: " << encoded << std::endl;

    // Base64 decoding
    auto decoded = base64::decode<std::string>(encoded);
    std::cout << "Base64 Decoded: " << decoded << std::endl;

    // Base16 (hexadecimal) encoding
    auto hex_encoded = base16::encode(original);
    std::cout << "Hex Encoded: " << hex_encoded << std::endl;
}

Stream Processing

#include <mgs/codec/stream.hpp>
#include <sstream>

void stream_example() {
    std::string data = "Stream processing example";

    // Encoding stream
    std::stringstream input_stream(data);
    std::ostringstream encoded_stream;

    base64::ostream encoder(encoded_stream);
    encoder << input_stream.rdbuf();
    encoder.finalize();

    std::cout << "Stream encoded: " << encoded_stream.str() << std::endl;

    // Decoding stream
    std::istringstream encoded_input(encoded_stream.str());
    std::ostringstream decoded_stream;

    base64::istream decoder(encoded_input);
    decoded_stream << decoder.rdbuf();

    std::cout << "Stream decoded: " << decoded_stream.str() << std::endl;
}

Custom Container Output

#include <mgs/codec.hpp>
#include <vector>
#include <array>

void custom_container_example() {
    std::vector<unsigned char> binary_data = {0x48, 0x65, 0x6C, 0x6C, 0x6F};

    // Encoding to vector
    auto encoded_vec = base64::encode<std::vector<char>>(binary_data);

    // Encoding to string
    auto encoded_str = base64::encode<std::string>(binary_data);

    // Using array
    std::array<unsigned char, 5> array_data = {'W', 'o', 'r', 'l', 'd'};
    auto encoded_array = base64::encode<std::string>(array_data);

    std::cout << "Vector encoded: " << std::string(encoded_vec.begin(), encoded_vec.end()) << std::endl;
    std::cout << "String encoded: " << encoded_str << std::endl;
    std::cout << "Array encoded: " << encoded_array << std::endl;
}

Error Handling

#include <mgs/codec/exceptions.hpp>

void error_handling_example() {
    try {
        // Invalid Base64 data
        std::string invalid_base64 = "Invalid!!Base64@@Data";
        auto result = base64::decode<std::string>(invalid_base64);
    }
    catch (const mgs::codec::invalid_input_error& e) {
        std::cerr << "Invalid input error: " << e.what() << std::endl;
    }
    catch (const mgs::codec::codec_error& e) {
        std::cerr << "Codec error: " << e.what() << std::endl;
    }

    // Using no_throw version
    auto result = base64::decode<std::string>("InvalidData", base64::no_throw);
    if (!result) {
        std::cerr << "Decoding failed safely" << std::endl;
    }
}

Range Operations

#include <mgs/codec.hpp>
#include <algorithm>

void range_example() {
    std::vector<int> data = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    // Encoding data range
    auto encoded = base64::encode(data);
    std::cout << "Range encoded: " << encoded << std::endl;

    // Using iterators
    std::string text_data = "Iterator Example";
    auto encoded_iter = base64::encode(text_data.begin(), text_data.end());
    std::cout << "Iterator encoded: " << encoded_iter << std::endl;
}

Performance Optimization Example

#include <mgs/codec.hpp>
#include <chrono>

void performance_example() {
    // Large data processing
    std::vector<char> large_data(1024 * 1024, 'A'); // 1MB data

    auto start = std::chrono::high_resolution_clock::now();

    // Pre-allocate output buffer
    auto encoded = base64::encode<std::vector<char>>(large_data);

    auto end = std::chrono::high_resolution_clock::now();
    auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);

    std::cout << "Encoded 1MB data in " << duration.count() << "ms" << std::endl;
    std::cout << "Encoded size: " << encoded.size() << " bytes" << std::endl;
}

Advanced Features

Custom Encoder

#include <mgs/codec/base_n.hpp>

// Create a custom Base32 encoder
using custom_base32 = mgs::codec::base_n<
    mgs::codec::base_n_traits<32>,
    mgs::codec::base_n_padding_policy::optional
>;

void custom_encoder_example() {
    std::string data = "Custom encoder example";

    auto encoded = custom_base32::encode(data);
    auto decoded = custom_base32::decode<std::string>(encoded);

    std::cout << "Custom encoded: " << encoded << std::endl;
    std::cout << "Custom decoded: " << decoded << std::endl;
}

Integration and Build

CMake Integration

find_package(mgs-codec REQUIRED)

target_link_libraries(your_target PRIVATE mgs::codec)

Compilation Options

# Using vcpkg
vcpkg install mgs-codec

# Or compile from source
git clone https://github.com/your-repo/mgs-codec.git
cd mgs-codec
mkdir build && cd build
cmake ..
make
sudo make install

Conclusion

MGS::CODEC 0.2.1, as a modern C++14 codec library, has the following advantages:

  1. Easy to use: Simple API design, quick to get started
  2. Type-safe: Fully utilizes the C++14 type system
  3. High performance: Optimized algorithm implementation, supports large data processing
  4. Extensible: Easy to add new encoding formats
  5. Cross-platform: Standard C++14, supports multiple platforms and compilers

Leave a Comment