-
Notifications
You must be signed in to change notification settings - Fork 503
/
device_ddf_bundle.cpp
206 lines (169 loc) · 4.67 KB
/
device_ddf_bundle.cpp
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include "deconz/u_assert.h"
#include "device_ddf_bundle.h"
int DDFB_FindChunk(U_BStream *bs, const char *tag, unsigned *size)
{
char fourcc[4];
unsigned sz;
unsigned long origPos = bs->pos;
for (;bs->pos < bs->size && bs->status == U_BSTREAM_OK;)
{
fourcc[0] = U_bstream_get_u8(bs);
fourcc[1] = U_bstream_get_u8(bs);
fourcc[2] = U_bstream_get_u8(bs);
fourcc[3] = U_bstream_get_u8(bs);
sz = U_bstream_get_u32_le(bs);
if (bs->pos + sz > bs->size)
{
break; // invalid size
}
if (fourcc[0] == tag[0] && fourcc[1] == tag[1] && fourcc[2] == tag[2] && fourcc[3] == tag[3])
{
*size = sz;
return 1;
}
bs->pos += sz;
}
bs->pos = origPos;
*size = 0;
return 0;
}
int DDFB_IsChunk(U_BStream *bs, const char *tag)
{
unsigned char *fourcc;
if (bs->pos + 4 < bs->size)
{
fourcc = &bs->data[bs->pos];
return fourcc[0] == tag[0] && fourcc[1] == tag[1] && fourcc[2] == tag[2] && fourcc[3] == tag[3];
}
return 0;
}
int DDFB_SkipChunk(U_BStream *bs)
{
char fourcc[4];
unsigned sz;
fourcc[0] = U_bstream_get_u8(bs);
fourcc[1] = U_bstream_get_u8(bs);
fourcc[2] = U_bstream_get_u8(bs);
fourcc[3] = U_bstream_get_u8(bs);
sz = U_bstream_get_u32_le(bs);
if (bs->status == U_BSTREAM_OK)
{
if (bs->pos + sz <= bs->size)
{
bs->pos += sz;
return 1;
}
}
return 0;
}
/*
https://github.com/deconz-community/ddf-tools/blob/main/packages/bundler/README.md
*/
int IsValidDDFBundle(U_BStream *bs, unsigned char sha256[U_SHA256_HASH_SIZE])
{
unsigned chunkSize;
if (DDFB_FindChunk(bs, "RIFF", &chunkSize) == 0)
{
return 0;
}
if (DDFB_FindChunk(bs, "DDFB", &chunkSize) == 0)
{
return 0;
}
{
// Bundle hash over DDFB chunk (header + data)
if (U_Sha256(&bs->data[bs->pos - 8], chunkSize + 8, sha256) == 0)
{
return 0; // this
}
}
U_BStream bsDDFB;
U_bstream_init(&bsDDFB, &bs->data[bs->pos], chunkSize);
// check DESC JSON has required fields
if (DDFB_FindChunk(&bsDDFB, "DESC", &chunkSize) == 0)
{
return 0;
}
// DBG_Printf(DBG_INFO, "DESC: %.*s\n", chunkSize, &bsDDFB.data[bsDDFB.pos]);
return 1;
}
bool DDFB_SanitizeBundleHashString(char *str, unsigned len)
{
if (len != 64)
return false;
for (unsigned i = 0; i < len; i++)
{
char ch = str[i];
if (ch >= '0' && ch <= '9') { } // ok
else if (ch >= 'a' && ch <= 'f') { } // ok
else if (ch >= 'A' && ch <= 'F') { str[i] = ch + ('a' - 'A'); } // convert to lower case
else
{
return false; // invalid hex char
}
}
return true;
}
int DDFB_ReadExtfChunk(U_BStream *bs, DDFB_ExtfChunk *extf)
{
U_BStream bs1;
char fourcc[4];
unsigned chunkSize;
unsigned long origPos = bs->pos;
extf->emptyString = '\0';
fourcc[0] = U_bstream_get_u8(bs);
fourcc[1] = U_bstream_get_u8(bs);
fourcc[2] = U_bstream_get_u8(bs);
fourcc[3] = U_bstream_get_u8(bs);
chunkSize = U_bstream_get_u32_le(bs);
if (bs->status != U_BSTREAM_OK)
{
return 0;
}
if (bs->size < bs->pos + chunkSize)
{
return 0;
}
U_ASSERT(fourcc[0] == 'E');
U_ASSERT(fourcc[1] == 'X');
U_ASSERT(fourcc[2] == 'T');
U_ASSERT(fourcc[3] == 'F');
U_bstream_init(&bs1, &bs->data[bs->pos], chunkSize);
bs->pos += chunkSize; // move bs behind chunk
extf->fileType[0] = (char)U_bstream_get_u8(&bs1);
extf->fileType[1] = (char)U_bstream_get_u8(&bs1);
extf->fileType[2] = (char)U_bstream_get_u8(&bs1);
extf->fileType[3] = (char)U_bstream_get_u8(&bs1);
extf->fileType[4] = '\0';
// file path string
extf->pathLength = U_bstream_get_u16_le(&bs1);
if (bs1.size < bs1.pos + extf->pathLength)
{
return 0;
}
extf->path = (const char*)&bs1.data[bs1.pos];
bs1.pos += extf->pathLength;
// modification time string
extf->modificationTimeLength = U_bstream_get_u16_le(&bs1);
if (bs1.size < bs1.pos + extf->modificationTimeLength)
{
return 0;
}
if (extf->modificationTimeLength == 0) // optional
{
extf->modificationTime = &extf->emptyString;
}
else
{
extf->modificationTime = (const char*)&bs1.data[bs1.pos];
}
bs1.pos += extf->modificationTimeLength;
// file content
extf->fileSize = U_bstream_get_u32_le(&bs1);
if (bs1.size < bs1.pos + extf->fileSize)
{
return 0;
}
extf->fileData = &bs1.data[bs1.pos];
return 1;
}