/************************************************************
*     11/9/2005 modified by david fedele to read buffer-data
*     instead raw-data-file 
**************************************************************
 
* include needed system headers
*/
#include <stdio.h>       /* include standard i/o library */
#include <stdlib.h>      /* include standard library */
#include <string.h>      /* include string library */
#include <unistd.h>      /* include unix standard library */
#include <sys/types.h>   /* */
#include <sys/stat.h>    /* */
#include <fcntl.h>       /* */
#include <errno.h>       /* error simbol definitions */
#include <time.h>        /* system time definitions */
#include <math.h>        /* math library */

/*   looks for a DSP header beginning (a word beginning with 1110)   */

/************************************************************
*     11/9/2005 modified by david fedele
*       void findstart_(int *error, int *fd)*/
void getbytes_(int *nbytes, char *buffer, int *p, unsigned int *value);

void findstart_(int *error,char *buffer, int *len, int *curpos)
{
    char *buff=buffer;
    int nbytes2=2;
    /* int nread */
/*     unsigned short word; */
/*     unsigned short temp; */
    unsigned int word;
/***********************************************************/
    
    *error=0;
    do {
/************************************************************/
/*     11/9/2005 modified by david fedele                   */
/*        nread=read(*fd, &word, 2); //reads 16 bits        */
/* 	if(nread == 0) { */
/* //	    printf("findstart: Unexpected end of file %d at %d \n",*fd,nread); */
/* 	    *error=1; */
/* 	    return ; */
/* 	} */
/* 	if(nread != 2) { */
/* //	    printf("findstart: Error on reading: bytes read -> nread = %d \n",nread); */
/* 	    *error=-1; */
/* 	    return ; */
/* 	} */

/* 	/\*** corrects byte ***\/ //endianess... */
/* 	temp = word; */
/* 	word = ( (temp&0x00ff) << 8 ) | ( (temp&0xff00) >> 8 ); */
/* //	printf("word %x , temp: %x: \n", word, temp);//???  */

/*         /\*** goes back 8 bits ***\/ */
/* 	lseek(*fd, -1, SEEK_CUR); //SEEK_CUR ---> from current position */

/* //	unsigned short num=lseek(*fd, -1, SEEK_CUR); //SEEK_CUR ---> from current position */
/* //	printf("%i \n",num); */
     
	getbytes_(&nbytes2,buff,curpos,&word);
           
      *curpos=*curpos-1;
      if(*curpos-1 > *len) {
/* 	printf("findstart: Unexpected end of file \n"); */
	    *error=1;
	    return ;
	}
/***********************************************************/
  
    }     while ((word&0xf000) != 0xe000);
    
/************************************************************/
/*     11/9/2005 modified by david fedele                   */
/*     /\*** goes back 8 bits ***\/ */
/*     lseek(*fd, -1, SEEK_CUR); */
    *curpos=*curpos-1;
    
/************************************************************/
    return;
}
