comparison HexFile.h @ 1:0b3630a29ad8

Initial version based on previous repository. Project was ported to QT6 and in now cmake based.
author Ideenmodellierer <tiefenrauscher@web.de>
date Thu, 27 Nov 2025 18:40:28 +0100
parents
children
comparison
equal deleted inserted replaced
0:76ccd6ce50c0 1:0b3630a29ad8
1 //////////////////////////////////////////////////////////////////////////////
2 /// \file HexFile.h
3 /// \brief Read .hex file in "Intel HEX" format.
4 /// \author JD Gascuel.
5 ///
6 /// \copyright (c) 2012-2016, JD Gascuel. All rights reserved.
7 /// $Id$
8 //////////////////////////////////////////////////////////////////////////////
9 //
10 // BSD 2-Clause License:
11 //
12 // Redistribution and use in source and binary forms, with or without
13 // modification, are permitted provided that the following conditions
14 // are met:
15 //
16 // 1. Redistributions of source code must retain the above copyright notice,
17 // this list of conditions and the following disclaimer.
18 //
19 // 2. Redistributions in binary form must reproduce the above copyright notice,
20 // this list of conditions and the following disclaimer in the documentation
21 // and/or other materials provided with the distribution.
22 //
23 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
33 // THE POSSIBILITY OF SUCH DAMAGE.
34 //
35 //////////////////////////////////////////////////////////////////////////////
36 // HISTORY
37 // 2012-02-12 : [jDG] Creation for Frog Companion firmware upload.
38 // 2012-09-22 : [jDG] Updated for OSTC Companion (OSTC3).
39 // 2014-07-07 : [jDG] Cleanups for Subsurface google-summer-of-code.
40 // 2014-07-25 : [jDG] BSD 2-clause license.
41
42 #include <QtGlobal>
43 #include <QFile>
44
45 class QProgressBar;
46
47 /////////////////////////////////////////////////////////////////////////////
48 /// \brief Read .hex file in "Intel HEX" format.
49 ///
50 /// \note that OSTC3 firmware is NOT distributed in Intel HEX format,
51 /// but is first flatened, then encrypted, then written back
52 /// in a similar looking format.
53 class HexFile
54 {
55 QFile _file;
56 size_t _memSize;
57 size_t _baseAddress; ///< Base for extended address modes.
58
59 unsigned char* _buffer;
60
61 /// \brief Read and interpret on line of the HEX file.
62 void readLine();
63
64 public:
65 /// \brief Create an empty object.
66 HexFile();
67 ~HexFile();
68
69 /// \brief Allocate initialized space.
70 bool allocate(size_t memSize, unsigned char fill = 0xFF);
71 bool sqwiz(size_t newMemSize);
72
73 /// \brief Load the .hex file into the internal buffer.
74 /// \return TRUE is ok, FAlSE if some error occurs.
75 void load(const QString& fileName);
76
77 /// \brief Return a pointer to the loaded data.
78 const unsigned char* data() const;
79
80 /// \brief Returns the 4 bytes checksum of the loaded data.
81 ///
82 /// This is a simple Adler32 checksum: The lower 16bits are the sum of
83 /// all the bytes (modulo 65536). And the second 16bits are the sum of
84 /// the running sum itself (also modulo 65536).
85 unsigned int checksum() const;
86
87 /// \brief Save to encrypted HEX file.
88 void saveEncrypted(const QString& fileName, unsigned char secretKey[16]);
89
90 /// \brief Reload encrypted HEX file.
91 void loadEncrypted(const QString& fileName, unsigned char secretKey[16]);
92 };