 /**[txh]********************************************************************
                                                                                
  Copyright (c) 2005 Juan Pablo D. Borgna <jpborgna en inti gov ar>
  Copyright (c) 2006-2007 Salvador E. Tropea <salvador en inti gov ar>
  Copyright (c) 2005-2007 Instituto Nacional de Tecnología Industrial
  
 This program 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; version 2.

 This program 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 this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 02111-1307, USA

  Description: Low level functions to output hexadecimal bitstreams.
                                                                                
***************************************************************************/    
/*****************************************************************************  
                                                                                
 Target:      Any                                                               
 Language:    C
 Compiler:    gcc 3.3.5 (Debian GNU/Linux)
 Text editor: SETEdit 0.5.5                                                     
                                                                                
*****************************************************************************/  

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "global.h"
#include "bitshandle.h"

/* This function returns the inverted bits of its argument */
unsigned char
inv_byte(unsigned char b)
{
 int i;
 unsigned char t=0;

 for (i=0;i<7;i++)
    {
     t|= (b & 0x01);
     t = t << 1;
     b = b >> 1;
    }
 t|=b & 0x01;
 return t;
}

/* This function is used to generate a string with the hex value of the
   inverted bits of from */
void
pbi(char *to, char *from,long nbytes)
{
 int i;
 int j;
 char t[]="ZZ";
 
 if (to==NULL) return;

 j=nbytes*2;
 
 for (i=0;i<nbytes;i++)
    {
     sprintf(t,"%02x",inv_byte(from[i]));
     j-=2;
     to[j]=t[0];
     to[j+1]=t[1];
    }
}

/* This function is used to generate a string with the hex value of the
   bits of from */
void
pb(char *to, char *from,long nbytes)
{
 int i;
 int j;
 char t[]="ZZ";
 
 if (to==NULL) return;

 j=0;
 
 for (i=0;i<nbytes;i++)
    {
     sprintf(t,"%02x",from[i]);
     j+=2;
     to[j]=t[0];
     to[j+1]=t[1];
    }
}

/*this function writes bytes to a file with a max nuber of
  chars per line */ /* it should not be here */
int
cutputs(char *string, FILE *fp, long block, int *col)
{
 long remaining;
 long wrote=0l;
 int c=*col;

 remaining=strlen(string);
 if (!cutlines)
   {
    fwrite(string,1,remaining,fp);
    col+=remaining;
    return 0;
   }
 if (remaining<block)
    block=remaining;

 while (remaining)
      {
       long to_write=block-c;
       if (to_write<0) to_write=0;
       if (to_write & 1) to_write++;
       wrote=fwrite(string,1,to_write,fp);
       if (wrote!=to_write)
         {
          perror("CUTPUTS");
          return 1;
         }
       c+=wrote;
       string+=wrote;
       remaining-=wrote;
       if (remaining)
         {
          fputs("\n",fp);
          c=0;
          if (remaining<block)
             block=remaining;
         }
      }
 *col=c;
 return 0;
}

