Debian Codegraph @

biosig4c++/t210/sopen_famos_read.c

raw symbols SCIP
1 /*
2
3 $Id$
4 Copyright (C) 2008,2009,2012 Alois Schloegl <[email protected]>
5 This file is part of the "BioSig for C/C++" repository
6 (biosig4c++) at http://biosig.sf.net/
7
8 BioSig is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 3
11 of the License, or (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20
21
22 */
23
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/stat.h>
27 #include "../biosig-dev.h"
28
29 #define min(a,b) (((a) < (b)) ? (a) : (b))
30 #define max(a,b) (((a) > (b)) ? (a) : (b))
31
32 /*
33 FIXME:
34
35 The switch __4HAERTEL__ is for some specific FAMOS file not supported yet by the default code.
36 currently, it is not clear (at least to me) how support for such a file should
37 be incorporated it the standard solution.
38
39 #define __4HAERTEL__
40 */
41
42
43 EXTERN_C void sopen_FAMOS_read(HDRTYPE* hdr) {
44 #define Header1 ((char*)hdr->AS.Header)
45
46 size_t count = hdr->HeadLen;
47
48 char *t, *t2;
49 const char EOL[] = "|;\xA\xD";
50 size_t pos, l1, len;
51 pos = strspn(Header1, EOL);
52 uint16_t gdftyp, CHAN=0;
53 char OnOff=1;
54 double Fs = NAN;
55 uint32_t NoChanCurrentGroup = 0; // number of (undefined) channels of current group
56 int level = 0; // to check consistency of file
57
58 char flag_AbstandFile = 0; // interleaved format ??? used for experimental code
59
60 /* FIXME: the current implemention has a number of flaws and limitations,
61 also the following security vulnerabilities are known:
62 CVE-2024-21812, CVE-2024-23313, CVE-2024-23310, CVE-2024-23606
63 Most likely a major rewrite is needed to fix those.
64 Only then, the following guard should be removed.
65 */
66 char *FLAG_ENABLE_FAMOS = getenv("BIOSIG_FAMOS_TRUST_INPUT");
67 if (FLAG_ENABLE_FAMOS==NULL || strcmp(FLAG_ENABLE_FAMOS,"1") ) {
68 biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "FAMOS not supported unless BIOSIG_FAMOS_TRUST_INPUT=1 is set");
69 return;
70 }
71
72 fprintf(stdout,"SOPEN(FAMOS): support is experimental. Only time series with equidistant sampling and single sampling rate are supported.\n");
73
74 while (pos < count-20) {
75 t = Header1+pos; // start of line
76
77 l1 = strcspn(t+5, ",");
78 t[l1+5] = 0;
79 len = atol(t+5);
80 pos += 6+l1;
81 t2 = Header1+pos; // start of line
82 if (count < max(pos,hdr->HeadLen)+256) {
83 size_t bufsiz = 4095;
84 hdr->AS.Header = (uint8_t*)realloc(hdr->AS.Header, count+bufsiz+1);
85 count += ifread(hdr->AS.Header+count,1,bufsiz,hdr);
86 }
87 pos += len+1;
88
89
90 if (VERBOSE_LEVEL>7)
91 fprintf(stdout,"FAMOS %i <%s>: %i,%i OnOff=%i\n",(int)pos,t,(int)l1,(int)len,OnOff);
92
93
94 if (!strncmp(t,"CF,2",4) && (level==0)) {
95 level = 1;
96 }
97 else if (!strncmp(t,"CK,1",4) && (level==1)) {
98 level = 2;
99 }
100 else if (!strncmp(t,"NO,1",4) && ((level==1) || (level==2))) {
101 int p;
102 // Ursprung
103 p = strcspn(t2,",");
104 t2[p] = 0;
105 // NameLang
106 t2 += p+1;
107 p = strcspn(t2,",");
108 t2[p] = 0;
109 size_t len = min(MAX_LENGTH_MANUF,atol(t2));
110 // Name
111 t2 += p+1;
112 strncpy(hdr->ID.Manufacturer._field,t2,len);
113 hdr->ID.Manufacturer._field[len]=0;
114 hdr->ID.Manufacturer.Name =hdr->ID.Manufacturer._field;
115 }
116 else if (!strncmp(t,"CT,1",4) && (level>1)) {
117 }
118
119 else if (!strncmp(t,"Cb,1",4)) // Buffer Beschreibung
120 {
121 // AnzahlBufferInKey
122 int p = strcspn(t2,",");
123 t2[p] = 0;
124 if (atoi(t2) != 1) {
125 biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "FAMOS: more than one buffer not supported");
126 }
127 // BytesInUserInfo
128 t2 += 1+p;
129 p = strcspn(t2,",");
130 t2[p] = 0;
131 // Buffer Referenz
132 t2 += 1+p;
133 p = strcspn(t2,",");
134 t2[p] = 0;
135 // IndexSamplesKey
136 t2 += 1+p;
137 p = strcspn(t2,",");
138 t2[p] = 0;
139 // OffsetBufferInSamplesKey
140 t2 += 1+p;
141 p = strcspn(t2,",");
142 t2[p] = 0;
143 hdr->CHANNEL[CHAN].bi = atol(t2);
144 // BufferLangBytes
145 t2 += 1+p;
146 p = strcspn(t2,",");
147 t2[p] = 0;
148 // OffsetFirstSampleInBuffer,
149 t2 += 1+p;
150 p = strcspn(t2,",");
151 t2[p] = 0;
152 // BufferFilledBytes,
153 t2 += 1+p;
154 p = strcspn(t2,",");
155 t2[p] = 0;
156 size_t bpb = atol(t2);
157
158 if (VERBOSE_LEVEL>7) fprintf(stdout,"146 famos: <%s>%d %d %d %d %d [%d] %d\n",t2,(int)bpb,CHAN,(int)bpb,hdr->NS,(int)(hdr->SPR*hdr->NRec),hdr->CHANNEL[CHAN].GDFTYP,GDFTYP_BITS[hdr->CHANNEL[CHAN].GDFTYP]);
159 /*
160 This command causes "Caught MathWorks::System::FatalException" on mexw32 when compiled mingw-cross-env
161 /scratch/schloegl/src/mingw-cross-env/usr/bin/i686-pc-mingw32-gcc v4.6.1 with Optimization -O1 or -O2
162 Turning optimization off solves the problem
163 */
164 hdr->CHANNEL[CHAN].SPR = 8*bpb/GDFTYP_BITS[hdr->CHANNEL[CHAN].GDFTYP];
165
166
167 if (VERBOSE_LEVEL) fprintf(stdout,"famos123: %i %i %i\n", OnOff, CHAN, hdr->CHANNEL[CHAN].SPR);
168
169 if ((OnOff) && (CHAN==0)) hdr->SPR = hdr->CHANNEL[CHAN].SPR;
170
171 if (hdr->SPR != hdr->CHANNEL[CHAN].SPR) {
172 fprintf(stdout,"Warning SOPEN(FAMOS): multiple sampling (%i:%i) rates not supported. Channel %i ignored!\n", hdr->SPR, hdr->CHANNEL[CHAN].SPR, CHAN+1);
173 OnOff = 0;
174 }
175 if (!OnOff) hdr->CHANNEL[CHAN].SPR = 0;
176
177 hdr->AS.bpb = hdr->CHANNEL[CHAN].bi + bpb;
178
179 // 0,
180 t2 += 1+p;
181 p = strcspn(t2,",");
182 t2[p] = 0;
183 // X0,
184 t2 += 1+p;
185 p = strcspn(t2,",");
186 t2[p] = 0;
187 // AddZeit,
188 t2 += 1+p;
189 p = strcspn(t2,",");
190 t2[p] = 0;
191 // UserInfo
192 t2 += 1+p;
193 p = strcspn(t2,EOL);
194 }
195
196 else if (!strncmp(t,"CB,1",4) && (level>1)) {
197 // Gruppen Definition
198 // Index der Gruppe
199 int p = strcspn(t2,",");
200 t2[p] = 0;
201 if (atol(t2)>1) {
202 biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "FAMOS: more than one group not supported");
203 }
204 level = 2;
205 }
206
207 else if (!strncmp(t,"CI,1",4)) {
208 }
209
210 else if (!strncmp(t,"CG,1",4)) // Definition eines Datenfeldes
211 {
212 int p;
213 // Anzahl Komponenten
214 p = strcspn(t2,",");
215 t2[p] = 0;
216 NoChanCurrentGroup = atol(t2); // additional channels
217 hdr->NS += NoChanCurrentGroup;
218 // Feldtyp
219 p = strcspn(t2,",");
220 t2[p] = 0;
221 OnOff = 1;
222 if (atoi(t2) != 1) {
223 // OnOff = 0;
224 // biosigERROR(hdr, B4C_DATATYPE_UNSUPPORTED, "FAMOS: data is not real and aquidistant sampled");
225 }
226 // Dimension
227 p = strcspn(t2,",");
228 t2[p] = 0;
229
230 hdr->CHANNEL = (CHANNEL_TYPE*)realloc(hdr->CHANNEL, hdr->NS*sizeof(CHANNEL_TYPE));
231 level = 3;
232 }
233
234 else if (!strncmp(t,"CD,",3) && (level>=3)) {
235 int p;
236 // dx
237 p = strcspn(t2,",");
238 t2[p] = 0;
239 Fs = 1.0/atof(t2);
240 // kalibriert
241 t2 += p+1;
242 p = strcspn(t2,",");
243 t2[p] = 0;
244 // EinheitLang
245 t2 += p+1;
246 p = strcspn(t2,",");
247 t2[p] = 0;
248 //size_t len = max(atol(t2),MAX_LENGTH_PHYSDIM);
249 // Einheit
250 t2 += p+1;
251 if (strncmp(t,"s",strlen(t))) {
252 // turn off channel if unit of x-axis is not seconds [s]
253 // OnOff = 0;
254 }
255
256 //PhysDimCode(t2);
257
258 }
259 else if (!strncmp(t,"NT,1",4) && (level>=3)) {
260 struct tm tm_time;
261
262 int p = strcspn(t2,",");
263 t2[p] = 0;
264 tm_time.tm_mday = atoi(t2);
265 t2 += p+1;
266 p = strcspn(t2,",");
267 t2[p] = 0;
268 tm_time.tm_mon = atoi(t2)-1;
269 t2 += p+1;
270 p = strcspn(t2,",");
271 t2[p] = 0;
272 tm_time.tm_year = atoi(t2)-1900;
273 t2 += p+1;
274 p = strcspn(t2,",");
275 t2[p] = 0;
276 tm_time.tm_hour = atoi(t2);
277 t2 += p+1;
278 p = strcspn(t2,",");
279 t2[p] = 0;
280 tm_time.tm_min = atoi(t2);
281 p = strcspn(t2,EOL);
282 // t2[p] = 0;
283 char tmp[10];
284 strncpy(tmp,t2,p);
285 tmp[p]=0;
286 tm_time.tm_sec = atoi(tmp);
287
288 hdr->T0 = tm_time2gdf_time(&tm_time);
289 }
290 else if (!strncmp(t,"CZ,1",4) && (level==3)) {
291 }
292 else if (!strncmp(t,"CC,1",4) && (level>=3)) {
293 if (NoChanCurrentGroup<1) {
294 biosigERROR(hdr, B4C_UNSPECIFIC_ERROR, "FAMOS: too many CC definitions in group");
295 }
296 CHAN = hdr->NS - NoChanCurrentGroup--;
297
298 if (CHAN==0)
299 hdr->SampleRate = Fs;
300 else if (OnOff && (fabs(hdr->SampleRate - Fs)>1e-9*Fs)) {
301 fprintf(stdout,"ERR2: %i %f %f\n",CHAN,hdr->SampleRate, Fs);
302 // biosigERROR(hdr, B4C_DATATYPE_UNSUPPORTED, "FAMOS: multiple sampling rates not supported");
303 }
304 if (VERBOSE_LEVEL>7)
305 fprintf(stdout,"CC: %i#%i Fs=%f,%i\n",OnOff,CHAN,Fs,(int)len);
306
307 /*
308 int p = strcspn(t2,",");
309 t2[p] = 0;
310 Fs = 1.0/atof(t2);
311 */
312 level = 4;
313 }
314 else if (!strncmp(t,"CP,1",4) && (level==4)) {
315 int p;
316
317 char s[21]; strncpy(s,t2,20);s[20]=0;
318 if (VERBOSE_LEVEL>7)
319 fprintf(stdout,"CHAN=%i tag=<%s>\n",CHAN,s);
320
321 // Bufferreferenz
322 p = strcspn(t2,",");
323 t2[p] = 0;
324
325 // Bytes
326 t2 += p+1;
327 p = strcspn(t2,",");
328 t2[p] = 0;
329
330 // Zahlenformat
331 t2 += p+1;
332 p = strcspn(t2,",");
333 t2[p] = 0;
334 gdftyp = atoi(t2);
335
336 if (VERBOSE_LEVEL>7)
337 fprintf(stdout,"CHAN=%i tag=<%s> gdf=%i\n",CHAN,s,gdftyp);
338
339 // SignBits
340 t2 += p+1;
341 p = strcspn(t2,",");
342 t2[p] = 0;
343
344 // Mask
345 t2 += p+1;
346 p = strcspn(t2,",");
347 t2[p] = 0;
348 if (atoi(t2)) {
349 biosigERROR(hdr, B4C_DATATYPE_UNSUPPORTED, "FAMOS: Mask != 0 not supported");
350 };
351
352 // Offset
353 t2 += p+1;
354 p = strcspn(t2,",");
355 t2[p] = 0;
356 if (atoi(t2)) {
357 fprintf(stdout,"Offset:<%s>\n",t2);
358 flag_AbstandFile = 1;
359 #ifndef __4HAERTEL__
360 biosigERROR(hdr, B4C_DATATYPE_UNSUPPORTED, "FAMOS: Offset != 0 not supported");
361 #endif
362 };
363 // DirekteFolgeAnzahl
364 t2 += p+1;
365 p = strcspn(t2,",");
366 t2[p] = 0;
367 if (atoi(t2) != 1) {
368 biosigERROR(hdr, B4C_DATATYPE_UNSUPPORTED, "FAMOS: DirekteFolgeAnzahl != 1 not supported");
369 };
370
371 // AbstandBytes
372 t2 += p+1;
373 p = strcspn(t2,EOL);
374 t2[p] = 0;
375 if (atoi(t2)) {
376 fprintf(stdout,"Abstandbytes:<%s>\n",t2);
377 flag_AbstandFile = 1;
378 #ifndef __4HAERTEL__
379 biosigERROR(hdr, B4C_DATATYPE_UNSUPPORTED, "FAMOS: AbstandBytes != 0 not supported");
380 #endif
381 };
382
383 double digmax=1e6,digmin=-1e6;
384 switch (gdftyp) {
385 case 1:
386 gdftyp = 2; // uint8
387 digmax = 255;
388 digmin = 0;
389 break;
390 case 2:
391 gdftyp = 1; // int8
392 digmax = 127;
393 digmin = -128;
394 break;
395 case 3:
396 case 9:
397 case 11:
398 gdftyp = 4; // uint16
399 digmax = 65535;
400 digmin = 0;
401 break;
402 case 4:
403 gdftyp = 3; // int16
404 digmax = 32767;
405 digmin = -32768;
406 break;
407 case 5:
408 gdftyp = 6; // uint32
409 digmax = (uint32_t)0xffffffff;
410 digmin = 0;
411 break;
412 case 6:
413 gdftyp = 5; // int32
414 digmax = (int32_t)0x7fffffff;
415 digmin = (int32_t)0x80000000;
416 break;
417 case 7:
418 gdftyp = 16; // float32
419 break;
420 case 8:
421 gdftyp = 17; // float64
422 break;
423 case 10:
424 gdftyp = 0;
425 break;
426 case 13:
427 gdftyp = 511+48; // float64
428 break;
429 default:
430 gdftyp = 0;
431 biosigERROR(hdr, B4C_DATATYPE_UNSUPPORTED, "FAMOS: unknown datatype");
432 };
433
434 hdr->CHANNEL[CHAN].LeadIdCode = 0;
435 hdr->CHANNEL[CHAN].OnOff = OnOff;
436 hdr->CHANNEL[CHAN].GDFTYP = gdftyp;
437 hdr->CHANNEL[CHAN].DigMax = digmax;
438 hdr->CHANNEL[CHAN].DigMin = digmin;
439 hdr->CHANNEL[CHAN].PhysMax = digmax;
440 hdr->CHANNEL[CHAN].PhysMin = digmin;
441
442 // initialize undefined values
443 hdr->CHANNEL[CHAN].LowPass = -1.0;
444 hdr->CHANNEL[CHAN].HighPass= -1.0;
445 hdr->CHANNEL[CHAN].Notch = -1.0;
446 hdr->CHANNEL[CHAN].Label[0] = 0;
447 hdr->CHANNEL[CHAN].Transducer[0]=0;
448 hdr->CHANNEL[CHAN].PhysDimCode = 0;
449 hdr->CHANNEL[CHAN].SPR = 1; // one sample per block
450 hdr->CHANNEL[CHAN].Impedance = INFINITY;
451 hdr->CHANNEL[CHAN].XYZ[0] = 0.0;
452 hdr->CHANNEL[CHAN].XYZ[1] = 0.0;
453 hdr->CHANNEL[CHAN].XYZ[2] = 0.0;
454
455 if (VERBOSE_LEVEL>7)
456 fprintf(stdout,"#%i\t%i %i\n",CHAN,gdftyp, hdr->CHANNEL[CHAN].GDFTYP);
457
458 }
459 else if (!strncmp(t,"CR,1",4)) {
460 int p;
461 t2[len] = 0;
462 // Transformieren
463 p = strcspn(t2,",");
464 t2[p] = 0;
465 int flagTransform = atoi(t2);
466 // Faktor
467 t2 += p+1;
468 p = strcspn(t2,",");
469 t2[p] = 0;
470 hdr->CHANNEL[CHAN].Cal = flagTransform ? atof(t2) : 1.0;
471 // Offset
472 t2 += p+1;
473 p = strcspn(t2,",");
474 t2[p] = 0;
475 hdr->CHANNEL[CHAN].Off = flagTransform ? atof(t2) : 0.0;
476
477 hdr->CHANNEL[CHAN].PhysMax = hdr->CHANNEL[CHAN].DigMax * hdr->CHANNEL[CHAN].Cal + hdr->CHANNEL[CHAN].Off;
478 hdr->CHANNEL[CHAN].PhysMin = hdr->CHANNEL[CHAN].DigMin * hdr->CHANNEL[CHAN].Cal + hdr->CHANNEL[CHAN].Off;
479
480 // Kalibriert
481 t2 += p+1;
482 p = strcspn(t2,",");
483 t2[p] = 0;
484 // EinheitLang
485 t2 += p+1;
486 p = strcspn(t2,",");
487 t2[p] = 0;
488 //int len = min(atoi(t2),MAX_LENGTH_PHYSDIM);
489 // Einheit
490 t2 += p+1;
491 hdr->CHANNEL[CHAN].PhysDimCode = PhysDimCode(t2);
492
493 }
494 else if (!strncmp(t,"ND,1",4) && (level==4)) {
495 // Display properties
496 }
497 else if (!strncmp(t,"CN,1",4) && (level==4)) {
498 int p;
499 // Indexgruppe
500 p = strcspn(t2,",");
501 t2[p] = 0;
502 // 0
503 t2 += p+1;
504 p = strcspn(t2,",");
505 t2[p] = 0;
506 // IndexBit
507 t2 += p+1;
508 p = strcspn(t2,",");
509 t2[p] = 0;
510 // NameLang
511 t2 += p+1;
512 p = strcspn(t2,",");
513 t2[p] = 0;
514 int len = atoi(t2);
515 // Name
516 t2 += p+1;
517 len = min(len,MAX_LENGTH_LABEL);
518 strncpy(hdr->CHANNEL[CHAN].Label, t2, len);
519 hdr->CHANNEL[CHAN].Label[len] = 0;
520 // KommLang
521 t2 += len+1;
522 p = strcspn(t2,",");
523 t2[p] = 0;
524 // Kommentar
525 t2 += p+1;
526 p = strcspn(t2,",");
527
528 }
529
530 else if (!strncmp(t,"CS,1",4)) {
531 int p = strcspn(t2,",");
532 t2[p] = 0;
533 if (atol(t2)>1) {
534 biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "FAMOS: more than one CS section not supported");
535 }
536 hdr->HeadLen = pos-len+p;
537
538 if (VERBOSE_LEVEL>8)
539 fprintf(stdout,"FAMOS: CS section reached\n");
540
541
542 break;
543 }
544 pos += strcspn(Header1+pos,EOL);
545 pos += strspn(Header1+pos,EOL);
546 }
547 ifseek(hdr,hdr->HeadLen,SEEK_SET);
548 hdr->NRec = 1;
549
550 #ifdef __4HAERTEL__
551 if (flag_AbstandFile==1) {
552
553 size_t bpb = 0;
554 uint16_t k;
555 for (k=0; k<hdr->NS; k++) {
556 if (hdr->CHANNEL[k].SPR>=1)
557 hdr->SPR = lcm(hdr->SPR,hdr->CHANNEL[k].SPR);
558 hdr->CHANNEL[k].SPR = 1;
559 hdr->CHANNEL[k].bi = bpb;
560 bpb += GDFTYP_BITS[hdr->CHANNEL[k].GDFTYP]>>3;
561 }
562
563 hdr->NRec *= hdr->SPR;
564 hdr->SPR = 1;
565 hdr->AS.bpb = bpb;
566
567 // This part is necessary for broken files (if header information does not fit file size)
568 struct stat FileBuf;
569 stat(hdr->FileName,&FileBuf);
570 nrec_t tmp = (FileBuf.st_size - hdr->HeadLen)/bpb;
571 if (tmp < 0) hdr->NRec = -1;
572 else if (tmp < hdr->NRec) hdr->NRec = tmp;
573 }
574 #endif
575
576 };
577 // End of SOPEN_FAMOS_READ