/***************************************************************
 *
 * routine read_ev: routine to extract an event information 
 * from file acquired with Pamela acquisition programs
 *
 * June 2002
 *
 ***************************************************************/
/*
 * 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 */
#include "CRC.h"
//extern "C" {  
 extern struct {
     unsigned char buffer[16];
    } header_; 

    void data2ntp_();
    //Struct per il passaggio di dati da e verso la chiamata fortran
//}

void testcrc_(unsigned char *crc, int *error)
{
    int i,j;
    unsigned char ubuffer;
    unsigned char calcrc;
    unsigned char buff2[16];
    *error = 1; 

    j = 0;
    for (i = 0; i < 16; i++){
	//	memcpy(&ubuffer<<i, header_.buffer[i], sizeof(header_.buffer));
	if ( i < 8 ) {
	    memcpy(&buff2[7-i], &header_.buffer[i], sizeof(header_.buffer[i]));
	} else {
	    j++;
	    memcpy(&buff2[16-j], &header_.buffer[i], sizeof(unsigned char));
	}
    }
    //    for (i = 0; i < 16; i++){
    //printf("%i : buff2  %X\n",i,buff2[i]);
    //}
    
    calcrc=CM_Compute_CRC16(0, &buff2, 15);

    //printf("%X = %X ? \n",calcrc,*crc);
    
    if ((unsigned char)calcrc == (unsigned char)*crc) *error = 0;

}
