-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbitfile.c
87 lines (69 loc) · 2.18 KB
/
bitfile.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "bitfile.h"
int BitFile_Read(FILE* hFile, struct BitFile_Sections** pSections) {
uchar cSectionID = 0;
uchar* pSectionData;
uint32_t iSectionSize;
uchar dFileHeader[] = { 0x00, 0x09, 0x0f, 0xf0, 0x0f, 0xf0, 0x0f, 0xf0, 0x0f, 0xf0, 0x00, 0x00, 0x01 };
uchar dData[MAX(4, sizeof(dFileHeader))];
struct BitFile_Sections* this = calloc(1, sizeof(struct BitFile_Sections));
if(!this) return -1;
if(fread(dData, 1, sizeof(dFileHeader), hFile) != sizeof(dFileHeader)) goto error_nofree;
if(memcmp(dData, dFileHeader, sizeof(dFileHeader)) != 0) goto error_nofree;
for(;;) {
if(fread(&cSectionID, 1, 1, hFile) != 1) {
if(feof(hFile)) break;
goto error_nofree;
}
uchar iLengthBytes = cSectionID == 'e' ? 4 : 2;
uchar dData[4];
if(fread(dData, 1, iLengthBytes, hFile) != iLengthBytes) goto error_nofree;
if(iLengthBytes == 4) iSectionSize = dData[0] << 24 | dData[1] << 16 | dData[2] << 8 | dData[3];
else iSectionSize = dData[0] << 8 | dData[1];
pSectionData = malloc(iSectionSize + 1);
if(!pSectionData) goto error_nofree;
if(fread(pSectionData, 1, iSectionSize, hFile) != iSectionSize) goto error;
pSectionData[iSectionSize] = '\0';
switch(cSectionID) {
case 'a':
if(this->sDescription) goto error;
this->sDescription = (char*)pSectionData;
break;
case 'b':
if(this->sPart) goto error;
this->sPart = (char*)pSectionData;
break;
case 'c':
if(this->sTimestampDate) goto error;
this->sTimestampDate = (char*)pSectionData;
break;
case 'd':
if(this->sTimestampTime) goto error;
this->sTimestampTime = (char*)pSectionData;
break;
case 'e':
if(this->dBitStream) goto error;
this->dBitStream = pSectionData;
this->iBitStreamLength = iSectionSize;
break;
default:
fprintf(stderr, "WARNING: Unsupported bit file section: 0x%02X\n", cSectionID);
free(pSectionData);
break;
}
}
*pSections = this;
return 0;
error:
free(pSectionData);
error_nofree:
BitFile_Free(this);
return -1;
}
void BitFile_Free(struct BitFile_Sections* this) {
free(this->sDescription);
free(this->sPart);
free(this->sTimestampDate);
free(this->sTimestampTime);
free(this->dBitStream);
free(this);
}