/* method2.c */

/****************************************************************************
 *
 *  Copyright (C) 2000-2001 Eli-Jean R. Leyssens, aka Pervect of Topix
 *
 *  This file is part of the CodePressor package.
 *
 *  CodePressor is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  CodePressor is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with CodePressor; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 *
 *  Eli-Jean R. Leyssens can be reached via email at eli@dnd.utwente.nl
 *  snail: E-J.R. Leyssens, Schivelbeinerstr. 5, 10439 Berlin, Germany
 *
 ***************************************************************************/

#include <malloc.h>
#include <stdio.h>
#include <oslib/types.h>

#include "method2.h"
#include "inputfile.h"
#include "outputfile.h"
#include "nibslide.h"
#include "tonyslide.h"
#include "methodsup.h"

extern byte depack2_part0_begin;
extern byte depack2_part0_start;
extern byte depack2_part0_adrdata;
extern byte depack2_part0_main_loop;
extern byte depack2_part0_end;

extern byte depack2_part1_begin;
extern byte depack2_part1_compare;
extern byte depack2_part1_jump_program;
extern byte depack2_part1_end;

extern byte depack2_part2_begin;
extern byte depack2_part2_call_get_bits1;
extern byte depack2_part2_call_get_bits2;
extern byte depack2_part2_end;

extern byte depack2_part3_begin;
extern byte depack2_part3_jump_program;
extern byte depack2_part3_end;

extern byte depack2_part4_begin;
extern byte depack2_part4_jump_main_loop;
extern byte depack2_part4_get_bits;
extern byte depack2_part4_data;
extern byte depack2_part4_end;

bool Method2_CompareFunction( byte bBestMatch, byte bBestMask, unsigned short usBestDistance, byte bMatch, byte bMask, unsigned short usDistance) {

    if ( bMatch > bBestMatch) {
        return TRUE;
    } else {
        if ( ( bMatch == bBestMatch) && ( (bMask & 0xf0) == 0) && ( bBestMask & 0xf0)) {
            //
            // Has just as many matching nibbles as best.
            // However, the new one has all its top nibbles matching
            // while the other hasn't. So, the new one's mask could
            // be encoded in 5 bits, rather than 9.
            //
            // If the currently best one was within 16 distance and
            // the new one isn't then we don't gain anything as
            // first the distance could be encoded as 5 bits and now 9
            // No gain, no loss, so don't even mind checking, just
            // use the new one anyway
            //
            return TRUE;
        } else {
            return FALSE;
        }
    }
}

bits Method2_Pack( bool fSave) {
    //
    // fSave = TRUE -> Also save out the result
    //
    bits dwRetval = FALSE;

    // Vars needed to build depack code
    byte * pbDepack = NULL;
    byte * pbBegin;
    bits Start;
    bits AdrData;
    bits MainLoop;
    bits Compare = 0;
    bits JumpProgram = 0;
    bits CallGetBits1;
    bits CallGetBits2;
    bits JumpMainLoop;
    bits GetBits;
    bits Data;
    int Size = 0;
    int Extend;

    // Vars needed to compress data
    int UncompressedSize;
    byte * pbMemory = NULL;
    TNibSlide * ptResult = NULL;
    byte * pbCompressed = NULL;
    int CompressedSize;
    bits * pdwStream;
    bits StreamCache;
    int CacheCount;
    byte * pbStreamEnd;
    bits * pdwHere;
    bits Here;
    byte bMask;

    int Index;

    pbBegin = &depack2_part0_begin;
    Start = &depack2_part0_start - pbBegin;
    AdrData = &depack2_part0_adrdata - pbBegin;
    MainLoop = &depack2_part0_main_loop - pbBegin;
    Extend = &depack2_part0_end - pbBegin;

    if ( !MethodSupport_ExtendDepackCodeWithPart( &pbDepack, &Size, pbBegin, Extend)) {
        goto ReturnPoint;
    }

    if ( tInputLoadAddress.Count == 1) {
        // Moveable load address, so we can use CMP r5, loadaddress
        pbBegin = &depack2_part1_begin;
        Compare = Size + &depack2_part1_compare - pbBegin;
        JumpProgram = Size + &depack2_part1_jump_program - pbBegin;
        Extend = &depack2_part1_end - pbBegin;
        if ( !MethodSupport_ExtendDepackCodeWithPart( &pbDepack, &Size, pbBegin, Extend)) {
            goto ReturnPoint;
        }
    }

    pbBegin = &depack2_part2_begin;
    CallGetBits1 = Size + &depack2_part2_call_get_bits1 - pbBegin;
    CallGetBits2 = Size + &depack2_part2_call_get_bits2 - pbBegin;
    Extend = &depack2_part2_end - pbBegin;

    if ( !MethodSupport_ExtendDepackCodeWithPart( &pbDepack, &Size, pbBegin, Extend)) {
        goto ReturnPoint;
    }

    if ( tInputLoadAddress.Count != 1) {
        pbBegin = &depack2_part3_begin;
        JumpProgram = Size + &depack2_part3_jump_program - pbBegin;
        Extend = &depack2_part3_end - pbBegin;
        if ( !MethodSupport_ExtendDepackCodeWithPart( &pbDepack, &Size, pbBegin, Extend)) {
            goto ReturnPoint;
        }
    }

    pbBegin = &depack2_part4_begin;
    JumpMainLoop = Size + &depack2_part4_jump_main_loop - pbBegin;
    GetBits = Size + &depack2_part4_get_bits - pbBegin;
    Data = Size + &depack2_part4_data - pbBegin;
    Extend = &depack2_part4_end - pbBegin;

    if ( !MethodSupport_ExtendDepackCodeWithPart( &pbDepack, &Size, pbBegin, Extend)) {
        goto ReturnPoint;
    }

    //
    // Fill in Values, ADRs and Branches
    //

    if ( tInputLoadAddress.Count == 1) {
       SetASMValue( pbDepack + Compare, tInputLoadAddress.ASMValues[ 0]);
    }

    if ( !SetADR( pbDepack + AdrData, AdrData, Data)) {
        fprintf( stderr, "ADR out of range in Method2_Pack.\n");
        goto ReturnPoint;
    }

    SetBranch( pbDepack + JumpMainLoop, JumpMainLoop, MainLoop);
    SetBranch( pbDepack + CallGetBits1, CallGetBits1, GetBits);
    SetBranch( pbDepack + CallGetBits2, CallGetBits2, GetBits);

    OutputLoadAddress = InputLoadAddress + InputAlignedSize;
    OutputExecAddress = OutputLoadAddress + Start;

    SetBranch( pbDepack + JumpProgram, OutputExecAddress + JumpProgram - Start, InputExecAddress);

    //
    //
    // Setup memory for "compression"
    //
    //
    UncompressedSize = InputAlignedSize;
    pbMemory = (byte *) malloc( UncompressedSize + Size);
    if ( pbMemory == NULL) {
        fprintf( stderr, "Failed to claim memory for input data +depack code in Method2_Pack.\n");
        goto ReturnPoint;
    }
    memcpy( pbMemory, pbInputFile, UncompressedSize);
    memcpy( pbMemory + UncompressedSize, pbDepack, Size);

    ptResult = ( TNibSlide *) malloc( sizeof( TNibSlide) * UncompressedSize / 4);
    if ( ptResult == NULL) {
        fprintf( stderr, "Failed to claim memory for result data in Method2_Pack.\n");
        goto ReturnPoint;
    }

    //
    // Get "best" results
    //
    DoTheHainesSlide( pbMemory, UncompressedSize, Size, -1, ptResult, -1, 32, Method2_CompareFunction);

    //
    // Actually generate the compressed data
    //
    pdwHere = (bits *) ( pbMemory + UncompressedSize - 4);
    pbCompressed = (byte *) malloc( UncompressedSize * 2);
    if ( pbCompressed == NULL) {
        fprintf( stderr, "Failed to claim memory for compressed data in Method2_Pack. Increase slotsize.\n");
        goto ReturnPoint;
    }
    pdwStream = (bits *) pbCompressed;
    CompressedSize = 0;
    StreamCache = 0;
    CacheCount = 0;
    Index = UncompressedSize / 4;

    while ( --Index >= 0) {
        if ( tInputLoadAddress.Count == 1) {
            bMask = ptResult[ Index].bMask;
        } else {
            if ( ptResult[ Index].bMask != 0xfe) {
               bMask = ptResult[ Index].bMask;
            } else {
               // 0xfe is used to indicate end of compressed data
               bMask = 0xff;
            }
        }
        MethodSupport_InsertIntoStream( &pdwStream, &StreamCache, &CacheCount, ptResult[ Index].usDistance, 5);
        MethodSupport_InsertNiceByte( &pdwStream, &StreamCache, &CacheCount, bMask);
        Here = pdwHere[ 0] ^ pdwHere[ ptResult[ Index].usDistance + 1];

        while ( bMask != 0) {
            if ( ( bMask & 0x01) != 0) {
              MethodSupport_InsertIntoStream( &pdwStream, &StreamCache, &CacheCount, Here & 0xf, 4);
            }
            Here = Here >> 4;
            bMask = (bMask >> 1) & 0xff;
        }
        pdwHere--;
    }

    if ( tInputLoadAddress.Count != 1) {
        MethodSupport_InsertIntoStream( &pdwStream, &StreamCache, &CacheCount, 0, 5);
        MethodSupport_InsertNiceByte( &pdwStream, &StreamCache, &CacheCount, 0xfe);
    }
    pbStreamEnd = MethodSupport_FlushStream( &pdwStream, &StreamCache, &CacheCount);

    dwRetval = Size + ( pbStreamEnd - pbCompressed);
    if ( fSave) {
        if ( OpenOutputFile()) {
             if ( !WriteToOutputFile( pbDepack, Size)) {
                dwRetval = FALSE;
             }
             if ( !WriteToOutputFile( pbCompressed, pbStreamEnd - pbCompressed)) {
                dwRetval = FALSE;
             }
             if ( !CloseOutputFile()) {
                dwRetval = FALSE;
             }
        }
    }

ReturnPoint:
    if ( pbDepack != NULL) {
      free( pbDepack);
      pbDepack = NULL;
    }
    if ( pbMemory != NULL) {
      free( pbMemory);
      pbMemory = NULL;
    }
    if ( ptResult != NULL) {
      free( ptResult);
      ptResult = NULL;
    }
    if ( pbCompressed != NULL) {
      free( pbCompressed);
      pbCompressed = NULL;
    }
    return dwRetval;
}
