/// @file zle.cpp
/// Zero length encoding
///
/// @remarks
/// Loco's first attempt to make a compressor for octane
///
/// @author loco
/// @date   2003.08.13
///
///

#include "zle.hpp"

#include <iomanip>
using namespace std;

zleCompressor GloballyInstantiated_zle(true);

zleCompressor::zleCompressor(bool registerme)
	:OctaneCompressor(registerme)
{
	if (registerme)CompressorManager_SingletonInsurer managerinsurance(this);
}

bool zleCompressor::DoProtectedCompress(bitreader &s, bitwriter &d)
{
	while (!s.empty())
	{
		unsigned char b = s.get_byte();
		unsigned char c;
		d.put_byte(b);

		if (!b)
		{
			for (c= 0; c < 255; c++)
			{
				if (s.empty()) break;
				if (s.get_byte())
				{
					s.seek_byte(-1, ios::cur);
					break;
				}
			}
			d.put_byte(c);
		}
	}
	return true;
}

bool zleCompressor::DoProtectedDecompress(bitreader &s, bitwriter &d)
{
	while (!s.empty())
	{
		unsigned char b = s.get_byte();
		d.put_byte(b);

		if (!b)
		{
			for (unsigned char c = s.get_byte(); c; c--) d.put_byte(0);
		}
	}
	return true;
}
