/* methodsup.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 "methodsup.h"

bool MethodSupport_ExtendDepackCodeWithPart( byte ** lppbDepack, int * lpSize, byte * pbPart, int Extend) {
   bool fRetval = TRUE;
   byte * pbTemp;

   pbTemp = (byte *) realloc( *lppbDepack, *lpSize + Extend);
    if ( pbTemp == NULL) {
      fprintf( stderr, "Failed to claim memory in ExtendDepackCodeWithPart.\n");
      fRetval = FALSE;
    } else {
        *lppbDepack = pbTemp;
        pbTemp = NULL;
        memcpy( *lppbDepack + *lpSize, pbPart, Extend);
        *lpSize += Extend;
    }
    return fRetval;
}

void MethodSupport_InsertIntoStream( bits ** lppdwStream, bits * pdwStreamCache, int * lpCacheCount, bits Value, int Bits) {
  for ( ; --Bits >= 0 ; ) {
    *pdwStreamCache = *pdwStreamCache >> 1;
    if ( ( Value & ( 1 << ( Bits))) != 0) {
       *pdwStreamCache = *pdwStreamCache | (1<<31);
    }
    *lpCacheCount = *lpCacheCount + 1;
    if ( *lpCacheCount == 32) {
      **lppdwStream = *pdwStreamCache;
      *lppdwStream = *lppdwStream + 1;
      // *pdwStreamCache = 0;
      *lpCacheCount = 0;
    }
  }
}

byte * MethodSupport_FlushStream( bits ** lppdwStream, bits * pdwStreamCache, int * lpCacheCount) {
   byte * pbRetval;

   if ( *lpCacheCount > 0) {
      *pdwStreamCache = *pdwStreamCache >> ( 32 - *lpCacheCount);
      **lppdwStream = *pdwStreamCache;
      pbRetval = ((byte *) ( *lppdwStream)) + (int)(( *lpCacheCount + 7) / 8);
      *lppdwStream = *lppdwStream + 1;
      *lpCacheCount = 0;
   } else {
      pbRetval = (byte *) *lppdwStream;
   }
   return pbRetval;
}

void MethodSupport_InsertNiceByte( bits ** lppdwStream, bits * pdwStreamCache, int * lpCacheCount, bits Value) {

  if ( ( Value & 0xf0) == 0) {
    MethodSupport_InsertIntoStream( lppdwStream, pdwStreamCache, lpCacheCount, Value & 0xf, 5);
  } else {
    MethodSupport_InsertIntoStream( lppdwStream, pdwStreamCache, lpCacheCount, Value | (1<<8), 9);
  }
}
