AlcapDAQ  1
CFASegment.c
Go to the documentation of this file.
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 
5 #include "CFASegment.h"
6 
7 
8 CFASegmentPtr newSegment(FILE* fin, int* last) {
9  static const int stubSize = sizeof(CFASegmentStub);
10  CFASegmentPtr segm;
11  CFASegmentStub* stub = malloc(stubSize);
12  fread(stub, 1, sizeof(CFASegmentStub), fin);
13 
14  if (stub->shebang != CFA_SHEBANG) {
15  free(stub);
16  return NULL;
17  }
18 
19  stub = realloc(stub, stub->headerSizeBytes + stub->fwSizeBytes);
20  fread((char*)stub + stubSize, 1, stub->headerSizeBytes + stub->fwSizeBytes - stubSize, fin);
21  if (getSegmentType(stub) == CFA_SEGMENT_TYPE_1) {
22  int i, jump = 0;
23  CFASegmentType1* t1s = malloc(sizeof(CFASegmentType1));
24  memcpy(t1s, stub, sizeof(CFASegmentType1));
25 
26  t1s->models = calloc(t1s->numModels, sizeof(CFAModelType1*));
27  for (i=0; i < t1s->numModels; i++) {
28  CFAModelType1* first = (CFAModelType1*)((char*)stub+stubSize+CFA_SEGMENT_T1_FAMILY_FIELDS_SIZE);
29  int size = ((CFAModelType1*)((char*)first + jump))->numChecks * 8 + 8;
30  t1s->models[i] = malloc(size);
31  memcpy(t1s->models[i], (char*)first + jump, size);
32  jump += size;
33  }
34  t1s->fwData = malloc(t1s->fwSizeBytes);
35  memcpy(t1s->fwData, (char*)stub+stub->headerSizeBytes, t1s->fwSizeBytes);
36  segm = t1s;
37  } else {
38  int i, jump = 0;
39  CFASegmentType2* t2s = malloc(sizeof(CFASegmentType2));
40  memcpy(t2s, stub, sizeof(CFASegmentType2));
41 
42  t2s->models = calloc(t2s->numModels, sizeof(CFAModelType2*));
43  for (i=0; i < t2s->numModels; i++) {
44  CFAModelType2* first = (CFAModelType2*)((char*)stub+stubSize);
45  int size = 4;
46  t2s->models[i] = malloc(size);
47  memcpy(t2s->models[i], (char*)first + jump, size);
48  jump += size;
49  }
50  t2s->fwData = malloc(t2s->fwSizeBytes);
51  memcpy(t2s->fwData, (char*)stub+stub->headerSizeBytes, t2s->fwSizeBytes);
52  segm = t2s;
53  }
54 
55  if (last != NULL) *last = stub->lastHeader;
56 
57  free(stub);
58 
59  return segm;
60 }
61 
62 
64  int i;
65  if (getSegmentType(segm) == CFA_SEGMENT_TYPE_1) {
66  CFASegmentType1* t1s = toType1(segm);
67  for (i=0; i<t1s->numModels; i++)
68  free(t1s->models[i]);
69  free(t1s->models);
70  } else {
71  CFASegmentType2* t2s = toType2(segm);
72  for (i=0; i<t2s->numModels; i++)
73  free(t2s->models[i]);
74  free(t2s->models);
75  }
76  free(segm);
77 }
78 
80  return toStub(segm)->segmentTypeId > TYPE_2_HEADER_VERSIONS ? 1 : 2;
81 }