| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // ====================================================================== | ||
| 2 | // \title CRCChecker.cpp | ||
| 3 | // \author ortega | ||
| 4 | // \brief cpp file for a crc32 checker | ||
| 5 | // | ||
| 6 | // \copyright | ||
| 7 | // Copyright 2009-2020, by the California Institute of Technology. | ||
| 8 | // ALL RIGHTS RESERVED. United States Government Sponsorship | ||
| 9 | // acknowledged. | ||
| 10 | // ====================================================================== | ||
| 11 | |||
| 12 | #include <Fw/FPrimeBasicTypes.hpp> | ||
| 13 | #include <Fw/Types/Assert.hpp> | ||
| 14 | #include <Fw/Types/FileNameString.hpp> | ||
| 15 | #include <Os/File.hpp> | ||
| 16 | #include <Os/FileSystem.hpp> | ||
| 17 | #include <Utils/CRCChecker.hpp> | ||
| 18 | #include <Utils/Hash/Hash.hpp> | ||
| 19 | |||
| 20 | namespace Utils { | ||
| 21 | 34 | crc_stat_t create_checksum_file(const char* const fname) { | |
| 22 | 34 | FW_ASSERT(fname != nullptr); | |
| 23 | |||
| 24 | FwSizeType i; | ||
| 25 | FwSizeType blocks; | ||
| 26 | FwSizeType remaining_bytes; | ||
| 27 | 34 | FwSizeType filesize; | |
| 28 |
1/1✓ Branch 2 taken 34 times.
|
34 | Os::File f; |
| 29 | Os::FileSystem::Status fs_stat; | ||
| 30 | Os::File::Status stat; | ||
| 31 |
1/1✓ Branch 2 taken 34 times.
|
34 | Utils::Hash hash; |
| 32 | 34 | U32 checksum; | |
| 33 | 34 | FwSizeType bytes_to_read; | |
| 34 | 34 | FwSizeType bytes_to_write; | |
| 35 |
1/1✓ Branch 2 taken 34 times.
|
34 | Fw::FileNameString hashFilename; |
| 36 | 34 | U8 block_data[CRC_FILE_READ_BLOCK]; | |
| 37 | |||
| 38 |
1/1✓ Branch 1 taken 34 times.
|
34 | fs_stat = Os::FileSystem::getFileSize(fname, filesize); |
| 39 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 33 times.
|
34 | if (fs_stat != Os::FileSystem::OP_OK) { |
| 40 | 1 | return FAILED_FILE_SIZE; | |
| 41 | } | ||
| 42 | |||
| 43 | // Open file | ||
| 44 |
1/1✓ Branch 2 taken 33 times.
|
33 | stat = f.open(fname, Os::File::OPEN_READ); |
| 45 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
|
33 | if (stat != Os::File::OP_OK) { |
| 46 | ✗ | return FAILED_FILE_OPEN; | |
| 47 | } | ||
| 48 | |||
| 49 | // Read file | ||
| 50 | 33 | bytes_to_read = CRC_FILE_READ_BLOCK; | |
| 51 | 33 | blocks = filesize / CRC_FILE_READ_BLOCK; | |
| 52 |
2/2✓ Branch 0 taken 39 times.
✓ Branch 1 taken 33 times.
|
72 | for (i = 0; i < blocks; i++) { |
| 53 |
1/1✓ Branch 2 taken 39 times.
|
39 | stat = f.read(block_data, bytes_to_read); |
| 54 |
2/4✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 39 times.
|
39 | if (stat != Os::File::OP_OK || bytes_to_read != CRC_FILE_READ_BLOCK) { |
| 55 | ✗ | f.close(); | |
| 56 | ✗ | return FAILED_FILE_READ; | |
| 57 | } | ||
| 58 | |||
| 59 |
1/1✓ Branch 1 taken 39 times.
|
39 | hash.update(block_data, bytes_to_read); |
| 60 | } | ||
| 61 | |||
| 62 | 33 | remaining_bytes = filesize % CRC_FILE_READ_BLOCK; | |
| 63 | 33 | bytes_to_read = remaining_bytes; | |
| 64 |
2/2✓ Branch 0 taken 31 times.
✓ Branch 1 taken 2 times.
|
33 | if (remaining_bytes > 0) { |
| 65 |
1/1✓ Branch 2 taken 31 times.
|
31 | stat = f.read(block_data, bytes_to_read); |
| 66 |
2/4✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 31 times.
|
31 | if (stat != Os::File::OP_OK || bytes_to_read != remaining_bytes) { |
| 67 | ✗ | f.close(); | |
| 68 | ✗ | return FAILED_FILE_READ; | |
| 69 | } | ||
| 70 | |||
| 71 |
1/1✓ Branch 1 taken 31 times.
|
31 | hash.update(block_data, remaining_bytes); |
| 72 | } | ||
| 73 | |||
| 74 | // close file | ||
| 75 |
1/1✓ Branch 2 taken 33 times.
|
33 | f.close(); |
| 76 | |||
| 77 | // generate checksum | ||
| 78 |
1/1✓ Branch 1 taken 33 times.
|
33 | hash.finalize(checksum); |
| 79 | |||
| 80 | // open checksum file | ||
| 81 |
1/1✓ Branch 2 taken 33 times.
|
33 | Fw::FormatStatus formatStatus = hashFilename.format("%s%s", fname, HASH_EXTENSION_STRING); |
| 82 | 33 | FW_ASSERT(formatStatus == Fw::FormatStatus::SUCCESS); | |
| 83 | |||
| 84 |
1/1✓ Branch 4 taken 33 times.
|
33 | stat = f.open(hashFilename.toChar(), Os::File::OPEN_WRITE); |
| 85 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
|
33 | if (stat != Os::File::OP_OK) { |
| 86 | ✗ | return FAILED_FILE_CRC_OPEN; | |
| 87 | } | ||
| 88 | |||
| 89 | // Write checksum file | ||
| 90 | 33 | bytes_to_write = sizeof(checksum); | |
| 91 |
1/1✓ Branch 2 taken 33 times.
|
33 | stat = f.write(reinterpret_cast<U8*>(&checksum), bytes_to_write); |
| 92 |
2/4✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 33 times.
|
33 | if (stat != Os::File::OP_OK || sizeof(checksum) != bytes_to_write) { |
| 93 | ✗ | f.close(); | |
| 94 | ✗ | return FAILED_FILE_CRC_WRITE; | |
| 95 | } | ||
| 96 | |||
| 97 | // close checksum file | ||
| 98 |
1/1✓ Branch 2 taken 33 times.
|
33 | f.close(); |
| 99 | |||
| 100 | 33 | return PASSED_FILE_CRC_WRITE; | |
| 101 | 34 | } | |
| 102 | |||
| 103 | 62 | crc_stat_t read_crc32_from_file(const char* const fname, U32& checksum_from_file) { | |
| 104 |
1/1✓ Branch 2 taken 62 times.
|
62 | Os::File f; |
| 105 | Os::File::Status stat; | ||
| 106 |
1/1✓ Branch 2 taken 62 times.
|
62 | Fw::FileNameString hashFilename; |
| 107 | 62 | FW_ASSERT(fname != nullptr); | |
| 108 | // open checksum file | ||
| 109 |
1/1✓ Branch 2 taken 62 times.
|
62 | Fw::FormatStatus formatStatus = hashFilename.format("%s%s", fname, HASH_EXTENSION_STRING); |
| 110 | 62 | FW_ASSERT(formatStatus == Fw::FormatStatus::SUCCESS); | |
| 111 | |||
| 112 |
1/1✓ Branch 4 taken 62 times.
|
62 | stat = f.open(hashFilename.toChar(), Os::File::OPEN_READ); |
| 113 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 60 times.
|
62 | if (stat != Os::File::OP_OK) { |
| 114 | 2 | return FAILED_FILE_CRC_OPEN; | |
| 115 | } | ||
| 116 | |||
| 117 | // Read checksum file | ||
| 118 | 60 | FwSizeType checksum_from_file_size = static_cast<FwSizeType>(sizeof(checksum_from_file)); | |
| 119 |
1/1✓ Branch 2 taken 60 times.
|
60 | stat = f.read(reinterpret_cast<U8*>(&checksum_from_file), checksum_from_file_size); |
| 120 |
3/4✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 58 times.
|
60 | if (stat != Os::File::OP_OK || checksum_from_file_size != sizeof(checksum_from_file)) { |
| 121 |
1/1✓ Branch 2 taken 2 times.
|
2 | f.close(); |
| 122 | 2 | return FAILED_FILE_CRC_READ; | |
| 123 | } | ||
| 124 | |||
| 125 | // close checksum file | ||
| 126 |
1/1✓ Branch 2 taken 58 times.
|
58 | f.close(); |
| 127 | 58 | return PASSED_FILE_CRC_CHECK; | |
| 128 | 62 | } | |
| 129 | |||
| 130 | 60 | crc_stat_t verify_checksum(const char* const fname, U32& expected, U32& actual) { | |
| 131 | 60 | FW_ASSERT(fname != nullptr); | |
| 132 | |||
| 133 | FwSizeType i; | ||
| 134 | FwSizeType blocks; | ||
| 135 | FwSizeType remaining_bytes; | ||
| 136 | 60 | FwSizeType filesize; | |
| 137 |
1/1✓ Branch 2 taken 60 times.
|
60 | Os::File f; |
| 138 | Os::FileSystem::Status fs_stat; | ||
| 139 | Os::File::Status stat; | ||
| 140 |
1/1✓ Branch 2 taken 60 times.
|
60 | Utils::Hash hash; |
| 141 | 60 | U32 checksum; | |
| 142 | 60 | U32 checksum_from_file; | |
| 143 | 60 | FwSizeType bytes_to_read; | |
| 144 | 60 | U8 block_data[CRC_FILE_READ_BLOCK]; | |
| 145 | |||
| 146 |
1/1✓ Branch 1 taken 60 times.
|
60 | fs_stat = Os::FileSystem::getFileSize(fname, filesize); |
| 147 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 59 times.
|
60 | if (fs_stat != Os::FileSystem::OP_OK) { |
| 148 | 1 | return FAILED_FILE_SIZE; | |
| 149 | } | ||
| 150 | |||
| 151 | // Open file | ||
| 152 |
1/1✓ Branch 2 taken 59 times.
|
59 | stat = f.open(fname, Os::File::OPEN_READ); |
| 153 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 59 times.
|
59 | if (stat != Os::File::OP_OK) { |
| 154 | ✗ | return FAILED_FILE_OPEN; | |
| 155 | } | ||
| 156 | |||
| 157 | // Read file | ||
| 158 | 59 | bytes_to_read = CRC_FILE_READ_BLOCK; | |
| 159 | 59 | blocks = filesize / CRC_FILE_READ_BLOCK; | |
| 160 |
2/2✓ Branch 0 taken 73 times.
✓ Branch 1 taken 59 times.
|
132 | for (i = 0; i < blocks; i++) { |
| 161 |
1/1✓ Branch 2 taken 73 times.
|
73 | stat = f.read(block_data, bytes_to_read); |
| 162 |
2/4✓ Branch 0 taken 73 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 73 times.
|
73 | if (stat != Os::File::OP_OK || bytes_to_read != CRC_FILE_READ_BLOCK) { |
| 163 | ✗ | f.close(); | |
| 164 | ✗ | return FAILED_FILE_READ; | |
| 165 | } | ||
| 166 | |||
| 167 |
1/1✓ Branch 1 taken 73 times.
|
73 | hash.update(block_data, static_cast<FwSizeType>(bytes_to_read)); |
| 168 | } | ||
| 169 | |||
| 170 | 59 | remaining_bytes = filesize % CRC_FILE_READ_BLOCK; | |
| 171 | 59 | bytes_to_read = remaining_bytes; | |
| 172 |
2/2✓ Branch 0 taken 57 times.
✓ Branch 1 taken 2 times.
|
59 | if (remaining_bytes > 0) { |
| 173 |
1/1✓ Branch 2 taken 57 times.
|
57 | stat = f.read(block_data, bytes_to_read); |
| 174 |
2/4✓ Branch 0 taken 57 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 57 times.
|
57 | if (stat != Os::File::OP_OK || bytes_to_read != remaining_bytes) { |
| 175 | ✗ | f.close(); | |
| 176 | ✗ | return FAILED_FILE_READ; | |
| 177 | } | ||
| 178 | |||
| 179 |
1/1✓ Branch 1 taken 57 times.
|
57 | hash.update(block_data, remaining_bytes); |
| 180 | } | ||
| 181 | |||
| 182 | // close file | ||
| 183 |
1/1✓ Branch 2 taken 59 times.
|
59 | f.close(); |
| 184 | // generate checksum | ||
| 185 |
1/1✓ Branch 1 taken 59 times.
|
59 | hash.finalize(checksum); |
| 186 | |||
| 187 |
1/1✓ Branch 1 taken 59 times.
|
59 | crc_stat_t crcstat = read_crc32_from_file(fname, checksum_from_file); |
| 188 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 57 times.
|
59 | if (crcstat != PASSED_FILE_CRC_CHECK) { |
| 189 | 2 | return crcstat; | |
| 190 | } | ||
| 191 | |||
| 192 | // compare checksums | ||
| 193 |
2/2✓ Branch 0 taken 26 times.
✓ Branch 1 taken 31 times.
|
57 | if (checksum != checksum_from_file) { |
| 194 | 26 | expected = checksum_from_file; | |
| 195 | 26 | actual = checksum; | |
| 196 | 26 | return FAILED_FILE_CRC_CHECK; | |
| 197 | } | ||
| 198 | |||
| 199 | 31 | expected = checksum_from_file; | |
| 200 | 31 | actual = checksum; | |
| 201 | 31 | return PASSED_FILE_CRC_CHECK; | |
| 202 | 60 | } | |
| 203 | |||
| 204 | } // namespace Utils | ||
| 205 |