예제 #1
0
Status
chunkDecode(DataChunk &result, const std::string &in)
{
    // The string must be a multiple of the chunk size:
    if (in.size() % Chars)
        return ABC_ERROR(ABC_CC_ParseError, "Bad encoding");

    DataChunk out;
    out.reserve(Bytes * (in.size() / Chars));

    constexpr unsigned shift = 8 * Bytes / Chars; // Bits per character
    uint16_t buffer = 0; // Bits waiting to be written out, MSB first
    int bits = 0; // Number of bits currently in the buffer
    auto i = in.begin();
    while (i != in.end())
    {
        // Read one character from the string:
        int value = Decode(*i);
        if (value < 0)
            break;
        ++i;

        // Append the bits to the buffer:
        buffer |= value << (16 - bits - shift);
        bits += shift;

        // Write out some bits if the buffer has a byte's worth:
        if (8 <= bits)
        {
            out.push_back(buffer >> 8);
            buffer <<= 8;
            bits -= 8;
        }
    }
예제 #2
0
DataChunk
buildData(std::initializer_list<DataSlice> slices)
{
    size_t size = 0;
    for (auto slice: slices)
        size += slice.size();

    DataChunk out;
    out.reserve(size);
    for (auto slice: slices)
        out.insert(out.end(), slice.begin(), slice.end());
    return out;
}