@@ -29,7 +29,7 @@ public static bool IsExtensionSupported(string ext)
29
29
/// allow the host software to detect and prevent attempts to download incompatible firmware.
30
30
/// </summary>
31
31
[ StructLayout ( LayoutKind . Sequential , Pack = 1 ) ]
32
- private struct Suffix
32
+ internal struct Suffix
33
33
{
34
34
/// <summary>
35
35
/// Total size of this structure in bytes.
@@ -195,52 +195,73 @@ public class FileContent
195
195
public Device . Identification DeviceInfo { get ; private set ; }
196
196
public Dictionary < byte , NamedMemory > ImagesByAltSetting { get ; private set ; }
197
197
198
- public FileContent ( ushort vendorId , ushort productId , ushort bcdProductVersion , ushort bcdDfuVersion )
198
+ public FileContent ( Device . Identification devInfo )
199
199
{
200
- DeviceInfo = new Device . Identification ( vendorId , productId , bcdProductVersion , bcdDfuVersion ) ;
200
+ DeviceInfo = devInfo ;
201
201
ImagesByAltSetting = new Dictionary < byte , NamedMemory > ( ) ;
202
202
}
203
203
}
204
204
205
+ private static Suffix ReadSuffix ( BinaryReader reader )
206
+ {
207
+ // this is the part of the file that the CRC is calculated on
208
+ byte [ ] content = new byte [ reader . BaseStream . Length - 4 ] ;
209
+ reader . BaseStream . Position = 0 ;
210
+ reader . Read ( content , 0 , content . Length ) ;
211
+
212
+ byte [ ] suffixdata = new byte [ Suffix . Size ] ;
213
+ reader . BaseStream . Position = reader . BaseStream . Length - Suffix . Size ;
214
+ reader . Read ( suffixdata , 0 , Suffix . Size ) ;
215
+ var suffix = suffixdata . ToStruct < Suffix > ( ) ;
216
+
217
+ // verify suffix
218
+ if ( suffix . dwCRC != Crc32 . Calculate ( content ) )
219
+ {
220
+ throw new ArgumentException ( "The selected dfu file has invalid CRC." ) ;
221
+ }
222
+ if ( suffix . bLength < Suffix . Size )
223
+ {
224
+ throw new ArgumentException ( "The selected dfu file has invalid suffix length." ) ;
225
+ }
226
+ if ( suffix . sDfuSignature != Suffix . Signature )
227
+ {
228
+ throw new ArgumentException ( "The selected dfu file has invalid suffix signature." ) ;
229
+ }
230
+
231
+ return suffix ;
232
+ }
233
+
234
+ /// <summary>
235
+ /// Extracts the device and firmware version information of a DFU file.
236
+ /// </summary>
237
+ /// <param name="filepath">Path to the DFU file</param>
238
+ /// <returns>The device and image version information</returns>
239
+ public static Device . Identification ParseFileInfo ( string filepath )
240
+ {
241
+ using ( BinaryReader reader = new BinaryReader ( File . Open ( filepath , FileMode . Open ) ) )
242
+ {
243
+ var suffix = ReadSuffix ( reader ) ;
244
+ return new Device . Identification ( suffix ) ;
245
+ }
246
+ }
247
+
205
248
/// <summary>
206
249
/// Extracts the contents of a DFU file.
207
250
/// </summary>
208
251
/// <param name="filepath">Path to the DFU file</param>
209
252
/// <returns>The device and memory image information</returns>
210
253
public static FileContent ParseFile ( string filepath )
211
254
{
212
- FileContent fc ;
213
-
214
255
using ( BinaryReader reader = new BinaryReader ( File . Open ( filepath , FileMode . Open ) ) )
215
256
{
216
- // this is the part of the file that the CRC is calculated on
217
- byte [ ] content = new byte [ reader . BaseStream . Length - 4 ] ;
218
- reader . Read ( content , 0 , content . Length ) ;
219
-
220
- // read the DFU file suffix first
221
- byte [ ] suffixdata = new byte [ Suffix . Size ] ;
222
- reader . BaseStream . Position = reader . BaseStream . Length - Suffix . Size ;
223
- reader . Read ( suffixdata , 0 , Suffix . Size ) ;
224
- var suffix = suffixdata . ToStruct < Suffix > ( ) ;
225
-
226
- // verify suffix
227
- if ( suffix . dwCRC != Crc32 . Calculate ( content ) )
228
- {
229
- throw new ArgumentException ( "The selected dfu file has invalid CRC." ) ;
230
- }
231
- if ( suffix . bLength < Suffix . Size )
232
- {
233
- throw new ArgumentException ( "The selected dfu file has invalid suffix length." ) ;
234
- }
235
- if ( suffix . sDfuSignature != Suffix . Signature )
236
- {
237
- throw new ArgumentException ( "The selected dfu file has invalid suffix signature." ) ;
238
- }
239
-
240
- fc = new FileContent ( suffix . idVendor , suffix . idProduct , suffix . bcdDevice , suffix . bcdDFU ) ;
257
+ var suffix = ReadSuffix ( reader ) ;
258
+ var devInfo = new Device . Identification ( suffix ) ;
259
+ var fc = new FileContent ( devInfo ) ;
241
260
242
261
// remove the suffix from the contents
243
- Array . Resize < byte > ( ref content , ( int ) reader . BaseStream . Length - suffix . bLength ) ;
262
+ byte [ ] content = new byte [ reader . BaseStream . Length - suffix . bLength ] ;
263
+ reader . BaseStream . Position = 0 ;
264
+ reader . Read ( content , 0 , content . Length ) ;
244
265
245
266
// if the protocol version is according to USB spec
246
267
if ( fc . DeviceInfo . DfuVersion <= Protocol . LatestVersion )
0 commit comments