#include "util.h"

namespace tile {
namespace compression {
bool
CopyDataToCompressionOutputStream(CompressionOutputStream *out, const void *data, std::size_t size)
{

    if (size == 0) { return true; }

    std::size_t current_pos  = 0;
    std::size_t left_to_copy = size;

    while (true) {
        void *next_data;
        std::size_t next_size;
        if (TILE_UNLIKELY(!out->Next(&next_data, &next_size))) { return false; }

        if (left_to_copy <= next_size) {
            memcpy(next_data, reinterpret_cast<const char *>(data) + current_pos, left_to_copy);
            out->BackUp(next_size - left_to_copy);
            return true;
        } else {
            memcpy(next_data, reinterpret_cast<const char *>(data) + current_pos, next_size);
            current_pos += next_size;
            left_to_copy -= next_size;
        }
    }
    TILE_UNREACHABLE("");
    return false;
}
}// namespace compression
}// namespace tile
