1 |
mocchiut |
1.1 |
/************************************************************ |
2 |
|
|
* 20/9/2005 modified by david fedele to read buffer-data |
3 |
|
|
* instead raw-data-file |
4 |
|
|
************************************************************** |
5 |
|
|
* include needed system headers |
6 |
|
|
*/ |
7 |
|
|
#include <stdio.h> /* include standard i/o library */ |
8 |
|
|
#include <stdlib.h> /* include standard library */ |
9 |
|
|
#include <string.h> /* include string library */ |
10 |
|
|
#include <unistd.h> /* include unix standard library */ |
11 |
|
|
#include <sys/types.h> /* */ |
12 |
|
|
#include <sys/stat.h> /* */ |
13 |
|
|
#include <fcntl.h> /* */ |
14 |
|
|
#include <errno.h> /* error simbol definitions */ |
15 |
|
|
#include <time.h> /* system time definitions */ |
16 |
|
|
#include <math.h> /* math library */ |
17 |
|
|
|
18 |
|
|
void unpacking(int ndata, unsigned short *output); |
19 |
|
|
|
20 |
|
|
/************************************************************ |
21 |
|
|
* 20/9/2005 modified by david fedele */ |
22 |
|
|
/* void dunpacker_(int *l_buffer, unsigned short *buffer, int *error, int *fd) */ |
23 |
|
|
void getbytes_(int *nbytes, char *buffer, int *p, unsigned int *value); |
24 |
|
|
|
25 |
|
|
void dunpacker_(int *l_buffer, unsigned short *buff, int *error,char *buffer, int *len,int *curpos) |
26 |
|
|
/**************************************************************/ |
27 |
|
|
{ |
28 |
|
|
int i; |
29 |
|
|
int nhead; |
30 |
|
|
|
31 |
|
|
nhead = 13; /* packed */ |
32 |
|
|
*error=0; |
33 |
|
|
/************************************************************ |
34 |
|
|
* 20/9/2005 modified by david fedele */ |
35 |
|
|
/* int nread; */ |
36 |
|
|
/* unsigned int word; */ |
37 |
|
|
|
38 |
|
|
/* nread=read(*fd, buffer, 2**l_buffer); */ |
39 |
|
|
|
40 |
|
|
/* if( nread==0 ) { */ |
41 |
|
|
/* printf("dunpacker: Unexpected end of file %d at %d \n",*fd,nread); */ |
42 |
|
|
/* *error=1; */ |
43 |
|
|
/* return ; */ |
44 |
|
|
/* } */ |
45 |
|
|
/* if ( nread != 2**l_buffer) { */ |
46 |
|
|
/* printf("dunpacker: Error on reading: nread = %d \n",nread); */ |
47 |
|
|
/* *error=-1; */ |
48 |
|
|
/* return ; */ |
49 |
|
|
/* } */ |
50 |
|
|
|
51 |
|
|
|
52 |
|
|
/* /\*** correct byte ***\/ */ |
53 |
|
|
/* for(i=0; i<*l_buffer; i++) { */ |
54 |
|
|
/* word = buffer[i]; */ |
55 |
|
|
/* buffer[i] = ( (word&0x00ff) << 8 ) | ( (word&0xff00) >> 8 ); */ |
56 |
|
|
/* } */ |
57 |
|
|
int nbytes2=2; |
58 |
|
|
unsigned int word_temp; |
59 |
|
|
unsigned short word[*l_buffer]; |
60 |
|
|
|
61 |
|
|
|
62 |
|
|
if( *curpos+(2*(*l_buffer))-1>*len ) { |
63 |
|
|
// printf("dunpacker: Unexpected end of file\n"); |
64 |
|
|
*error=1; |
65 |
|
|
return ; |
66 |
|
|
} |
67 |
|
|
|
68 |
|
|
for(i=0;i<*l_buffer;i++){ |
69 |
|
|
getbytes_(&nbytes2,buffer,curpos,&word_temp); |
70 |
|
|
word[i]=word_temp; |
71 |
|
|
buff[i]=word[i]; |
72 |
|
|
} |
73 |
|
|
|
74 |
|
|
/*** unpacking buffer data ***/ |
75 |
|
|
/************************************************************ |
76 |
|
|
* 20/9/2005 modified by david fedele */ |
77 |
|
|
/* if( nhead == 13) unpacking(*l_buffer, buffer); */ |
78 |
|
|
if( nhead == 13) unpacking(*l_buffer, buff); |
79 |
|
|
/**************************************************************/ |
80 |
|
|
|
81 |
|
|
|
82 |
|
|
/* int num = (*l_buffer-13-1)/13*16+3;//??? */ |
83 |
|
|
/* for( i=0; i<num; i++) printf("%i %x \n",i,buffer[i]);//??? */ |
84 |
|
|
|
85 |
|
|
/*** Checksum ***/ |
86 |
|
|
/* *oi = buffer; */ |
87 |
|
|
/* word = 0; */ |
88 |
|
|
/* for( i=0; i<*l_buffer; ++i) */ |
89 |
|
|
/* word = (word + *oi++)& 0x03ff; */ |
90 |
|
|
/* if( word == ( header[6] & 0x03ff ) ) */ |
91 |
|
|
/* /\* printf("Check Sum OK \n")*\/ ; */ |
92 |
|
|
/* else */ |
93 |
|
|
/* printf("Wrong Check Sum: %x, %x \n",word,header[6]&0x03ff); */ |
94 |
|
|
|
95 |
|
|
} |
96 |
|
|
|
97 |
|
|
/*========================================================= */ |
98 |
|
|
/*========================================================= */ |
99 |
|
|
/* |
100 |
|
|
* Routine to unpacking buffer file |
101 |
|
|
* |
102 |
|
|
*/ |
103 |
|
|
/*========================================================= */ |
104 |
|
|
/*========================================================= */ |
105 |
|
|
void unpacking(int nwords, unsigned short *output) { |
106 |
|
|
unsigned short out[nwords]; |
107 |
|
|
int k; |
108 |
|
|
unsigned short *oi, *oo, *o1; |
109 |
|
|
|
110 |
|
|
for( k=0; k<nwords; ++k) |
111 |
|
|
out[k]=0; |
112 |
|
|
oo=output; |
113 |
|
|
oi=out; |
114 |
|
|
for( k=0; k<nwords; ++k) { |
115 |
|
|
*oi++ = *oo; |
116 |
|
|
*oo++ = 0; |
117 |
|
|
} |
118 |
|
|
|
119 |
|
|
oo=output; |
120 |
|
|
oi=out; |
121 |
|
|
|
122 |
|
|
for(k=0; k<nwords; k+=13) { |
123 |
|
|
o1 = oi++; |
124 |
|
|
*oo++ = (*o1 & 0xfff8) >> 3; |
125 |
|
|
*oo++ = ( (*o1 & 0x0007) << 10 ) | ( (*oi & 0xffc0) >> 6); |
126 |
|
|
o1 = oi++; |
127 |
|
|
*oo++ = ( (*o1 & 0x003f) << 7 ) | ( (*oi & 0xfe00) >> 9); |
128 |
|
|
o1 = oi++; |
129 |
|
|
*oo++ = ( (*o1 & 0x01ff) << 4 ) | ( (*oi & 0xf000) >> 12); |
130 |
|
|
o1 = oi++; |
131 |
|
|
*oo++ = ( (*o1 & 0x0fff) << 1 ) | ( (*oi & 0x8000) >> 15); |
132 |
|
|
*oo++ = (*oi & 0x7ffc) >> 2; |
133 |
|
|
o1 = oi++; |
134 |
|
|
*oo++ = ( (*o1 & 0x0003) << 11 ) | ( (*oi & 0xffe0) >> 5); |
135 |
|
|
o1 = oi++; |
136 |
|
|
*oo++ = ( (*o1 & 0x001f) << 8 ) | ( (*oi & 0xff00) >> 8); |
137 |
|
|
o1 = oi++; |
138 |
|
|
*oo++ = ( (*o1 & 0x00ff) << 5 ) | ( (*oi & 0xf800) >> 11); |
139 |
|
|
o1 = oi++; |
140 |
|
|
*oo++ = ( (*o1 & 0x07ff) << 2 ) | ( (*oi & 0xc000) >> 14); |
141 |
|
|
*oo++ = (*oi & 0x3ffe) >> 1; |
142 |
|
|
o1 = oi++; |
143 |
|
|
*oo++ = ( (*o1 & 0x0001) << 12 ) | ( (*oi & 0xfff0) >> 4); |
144 |
|
|
o1 = oi++; |
145 |
|
|
*oo++ = ( (*o1 & 0x000f) << 9 ) | ( (*oi & 0xff80) >> 7); |
146 |
|
|
o1 = oi++; |
147 |
|
|
*oo++ = ( (*o1 & 0x007f) << 6 ) | ( (*oi & 0xfc00) >> 10); |
148 |
|
|
o1 = oi++; |
149 |
|
|
*oo++ = ( (*o1 & 0x03ff) << 3 ) | ( (*oi & 0xe000) >> 13); |
150 |
|
|
*oo++ = *oi++ & 0x1fff; |
151 |
|
|
} |
152 |
|
|
|
153 |
|
|
|
154 |
|
|
return; |
155 |
|
|
} |