Click here to Skip to main content
15,891,253 members
Articles / Desktop Programming / MFC

Auto-Task Tool for Web Game Travian

Rate me:
Please Sign up or sign in to vote.
4.84/5 (26 votes)
29 Oct 20075 min read 782.8K   7.1K   45  
Developing Auto-Task Tool for Web Game Travian
// zlib.cpp - written and placed in the public domain by Wei Dai

// "zlib" is the name of a well known C language compression library
// (http://www.zlib.org) and also the name of a compression format
// (RFC 1950) that the library implements. This file is part of a
// complete reimplementation of the zlib compression format.

#include "pch.h"
#include "zlib.h"
#include "zdeflate.h"
#include "zinflate.h"

NAMESPACE_BEGIN(CryptoPP)

static const byte DEFLATE_METHOD = 8;
static const byte FDICT_FLAG = 1 << 5;

// *************************************************************

void ZlibCompressor::WritePrestreamHeader()
{
	m_adler32.Restart();
	byte cmf = DEFLATE_METHOD | ((GetLog2WindowSize()-8) << 4);
	byte flags = GetCompressionLevel() << 6;
	AttachedTransformation()->PutWord16(RoundUpToMultipleOf(cmf*256+flags, 31));
}

void ZlibCompressor::ProcessUncompressedData(const byte *inString, size_t length)
{
	m_adler32.Update(inString, length);
}

void ZlibCompressor::WritePoststreamTail()
{
	FixedSizeSecBlock<byte, 4> adler32;
	m_adler32.Final(adler32);
	AttachedTransformation()->Put(adler32, 4);
}

unsigned int ZlibCompressor::GetCompressionLevel() const
{
	static const unsigned int deflateToCompressionLevel[] = {0, 1, 1, 1, 2, 2, 2, 2, 2, 3};
	return deflateToCompressionLevel[GetDeflateLevel()];
}

// *************************************************************

ZlibDecompressor::ZlibDecompressor(BufferedTransformation *attachment, bool repeat, int propagation)
	: Inflator(attachment, repeat, propagation)
{
}

void ZlibDecompressor::ProcessPrestreamHeader()
{
	m_adler32.Restart();

	byte cmf;
	byte flags;

	if (!m_inQueue.Get(cmf) || !m_inQueue.Get(flags))
		throw HeaderErr();

	if ((cmf*256+flags) % 31 != 0)
		throw HeaderErr();	// if you hit this exception, you're probably trying to decompress invalid data

	if ((cmf & 0xf) != DEFLATE_METHOD)
		throw UnsupportedAlgorithm();

	if (flags & FDICT_FLAG)
		throw UnsupportedPresetDictionary();

	m_log2WindowSize = 8 + (cmf >> 4);
}

void ZlibDecompressor::ProcessDecompressedData(const byte *inString, size_t length)
{
	AttachedTransformation()->Put(inString, length);
	m_adler32.Update(inString, length);
}

void ZlibDecompressor::ProcessPoststreamTail()
{
	FixedSizeSecBlock<byte, 4> adler32;
	if (m_inQueue.Get(adler32, 4) != 4)
		throw Adler32Err();
	if (!m_adler32.Verify(adler32))
		throw Adler32Err();
}

NAMESPACE_END

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions