1 |
/* |
/************************************************************ |
2 |
|
* 12/9/2005 modified by david fedele to read buffer-data |
3 |
|
* instead raw-data-file |
4 |
|
************************************************************** |
5 |
|
* |
6 |
* include needed system headers |
* include needed system headers |
7 |
*/ |
*/ |
8 |
#include <stdio.h> /* include standard i/o library */ |
#include <stdio.h> /* include standard i/o library */ |
15 |
#include <errno.h> /* error simbol definitions */ |
#include <errno.h> /* error simbol definitions */ |
16 |
#include <time.h> /* system time definitions */ |
#include <time.h> /* system time definitions */ |
17 |
#include <math.h> /* math library */ |
#include <math.h> /* math library */ |
18 |
|
/************************************************************ |
19 |
void hunpacker_(unsigned short *buffer, int *error, int *fd) |
* 12/9/2005 modified by david fedele |
20 |
|
void hunpacker_(unsigned short *buffer, int *error, int *fd)*/ |
21 |
|
void getbytes_(int *nbytes, char *buffer, int *p, unsigned int *value); |
22 |
|
void hunpacker_(unsigned short *header, int *error,char *buffer, int *len,int *curpos,int *startcrc) |
23 |
|
/************************************************************/ |
24 |
{ |
{ |
25 |
|
|
26 |
int i; |
int i; |
27 |
int nread,nhead; |
int nhead; |
28 |
unsigned short word; |
unsigned int buffer_temp[13], *oo, *oi, *o1; //packed mode |
29 |
unsigned short buffer_temp[13], *oo, *oi, *o1; //packed mode |
/************************************************************ |
30 |
|
* 12/9/2005 modified by david fedele */ |
31 |
|
/* int nread,nhead; */ |
32 |
|
/* unsigned short word; */ |
33 |
|
|
34 |
///*** goes back a word***/ |
/* ///\*** goes back a word***\/ */ |
35 |
// lseek(*fd, -2, SEEK_CUR); //SEEK_CUR ---> from current position |
/* // lseek(*fd, -2, SEEK_CUR); //SEEK_CUR ---> from current position */ |
36 |
|
|
37 |
*error=0; |
/* *error=0; */ |
38 |
|
|
39 |
|
/* /\*** read the buffer ***\/ */ |
40 |
|
/* nhead = 13; /\* packed *\/ */ |
41 |
|
/* nread=read(*fd, buffer_temp, 2*nhead); */ |
42 |
|
/* if( nread==0 ) { */ |
43 |
|
/* printf("hunpacker: Unexpected end of file %d at %d \n",*fd,nread); */ |
44 |
|
/* *error=1; */ |
45 |
|
/* return ; */ |
46 |
|
/* } */ |
47 |
|
/* if ( nread != 2*nhead) { */ |
48 |
|
/* printf("hunpacker: Error on reading: nread = %d \n",nread); */ |
49 |
|
/* *error=-1; */ |
50 |
|
/* return ; */ |
51 |
|
/* } */ |
52 |
|
|
53 |
|
/* /\*** correct byte ***\/ //endianess... */ |
54 |
|
/* for(i=0; i<nhead; i++) { */ |
55 |
|
/* word = buffer_temp[i]; */ |
56 |
|
/* buffer_temp[i] = ( (word&0x00ff) << 8 ) | ( (word&0xff00) >> 8 ); */ |
57 |
|
/* } */ |
58 |
|
|
59 |
|
unsigned int word[16]; |
60 |
|
int nbytes2=2; |
61 |
|
char *buff=buffer; |
62 |
|
*error=0; |
63 |
/*** read the buffer ***/ |
/*** read the buffer ***/ |
64 |
nhead = 13; /* packed */ |
nhead = 13; /* packed */ |
65 |
nread=read(*fd, buffer_temp, 2*nhead); |
|
66 |
if( nread==0 ) { |
*startcrc=*curpos; |
67 |
//printf("hunpacker: Unexpected end of file %d at %d \n",*fd,nread); |
|
68 |
|
if( *curpos+(2*13)>*len ) { |
69 |
|
// printf("hunpacker: Unexpected end of file\n"); |
70 |
*error=1; |
*error=1; |
71 |
return ; |
return ; |
72 |
} |
} |
73 |
if ( nread != 2*nhead) { |
|
74 |
//printf("hunpacker: Error on reading: nread = %d \n",nread); |
for (i=0;i<13;i++) |
75 |
*error=-1; |
getbytes_(&nbytes2,buff,curpos,&buffer_temp[i]); |
76 |
return ; |
|
77 |
} |
/****************************************************************/ |
78 |
|
|
79 |
|
|
|
/*** correct byte ***/ //endianess... |
|
|
for(i=0; i<nhead; i++) { |
|
|
word = buffer_temp[i]; |
|
|
buffer_temp[i] = ( (word&0x00ff) << 8 ) | ( (word&0xff00) >> 8 ); |
|
|
} |
|
80 |
|
|
81 |
|
|
82 |
/*** buffer unpacking ***/ |
/*** buffer unpacking ***/ |
83 |
if( nhead == 16 ) { // NO!??? |
if( nhead == 16 ) { // NO!??? |
84 |
oo=buffer; /* nopacking buffer */ |
/************************************************************ |
85 |
|
* 12/9/2005 modified by david fedele */ |
86 |
|
/* oo=buffer; /\* nopacking buffer *\/ */ |
87 |
|
oo=word; |
88 |
|
/**********************************************************/ |
89 |
oi=buffer_temp; |
oi=buffer_temp; |
90 |
for( i=0; i<nhead; i++) *oo++ = *oi++; |
for( i=0; i<nhead; i++) *oo++ = *oi++; |
91 |
} |
} |
92 |
if( nhead == 13 ) { |
if( nhead == 13 ) { |
93 |
oo=buffer; /* unpacking buffer */ |
/************************************************************ |
94 |
|
* 12/9/2005 modified by david fedele */ |
95 |
|
/* oo=buffer; /\* unpacking buffer *\/ */ |
96 |
|
oo=word; |
97 |
|
/**********************************************************/ |
98 |
oi=buffer_temp; |
oi=buffer_temp; |
99 |
o1 = oi++; |
o1 = oi++; |
100 |
*oo++ = (*o1 & 0xfff8) >> 3; |
*oo++ = (*o1 & 0xfff8) >> 3; |
126 |
*oo++ = *oi++ & 0x1fff; |
*oo++ = *oi++ & 0x1fff; |
127 |
} |
} |
128 |
|
|
129 |
// for( i=0; i<16; i++) printf("%x \n",buffer[i]);//??? |
/************************************************************ |
130 |
|
* 23/9/2005 modified by david fedele */ |
131 |
|
for( i=0; i<16; i++) { |
132 |
|
header[i]=word[i]; |
133 |
|
/* printf("%x \n",word[i]);//??? */ |
134 |
|
/* printf("%x \n",header[i]);//??? */ |
135 |
|
} |
136 |
|
/**************************************************************/ |
137 |
return; |
return; |
138 |
} |
} |