F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
ValidateFileCommon.cpp
Go to the documentation of this file.
1 #include <Os/ValidateFile.hpp>
2 #include <Os/File.hpp>
3 #include <Utils/Hash/Hash.hpp>
4 #include <Os/FileSystem.hpp>
5 
6 namespace Os {
7 
8  File::Status computeHash(const char* fileName, Utils::HashBuffer &hashBuffer) {
9 
10  File::Status status;
11 
12  // Open file:
13  File file;
14  status = file.open(fileName, File::OPEN_READ);
15  if( File::OP_OK != status ) {
16  return status;
17  }
18 
19  // Get the file size:
20  FileSystem::Status fs_status;
21  FwSignedSizeType fileSize = 0;
22  fs_status = FileSystem::getFileSize(fileName, fileSize);
23  if( FileSystem::OP_OK != fs_status) {
24  return File::BAD_SIZE;
25  }
26  const FwSignedSizeType max_itr = (fileSize/VFILE_HASH_CHUNK_SIZE + 1);
27 
28  // Read all data from file and update hash:
29  Utils::Hash hash;
30  hash.init();
31  U8 buffer[VFILE_HASH_CHUNK_SIZE];
32  FwSignedSizeType size = 0;
33  FwSignedSizeType cnt = 0;
34  while( cnt <= max_itr ) {
35  // Read out chunk from file:
36  size = sizeof(buffer);
37  status = file.read(buffer, size, Os::File::WaitType::NO_WAIT);
38  if( File::OP_OK != status ) {
39  return status;
40  }
41  // If end of file, break:
42  if( size == 0 ) {
43  break;
44  }
45  // Add chunk to hash calculation:
46  hash.update(&buffer, static_cast<FwSizeType>(size));
47  cnt++;
48  }
49  file.close();
50 
51  // We should not have left the loop because of cnt > max_itr:
52  FW_ASSERT(size == 0);
53  FW_ASSERT(cnt <= max_itr);
54 
55  // Calculate hash:
56  Utils::HashBuffer computedHashBuffer;
57  hash.final(computedHashBuffer);
58  hashBuffer = computedHashBuffer;
59 
60  return status;
61  }
62 
63  File::Status readHash(const char* hashFileName, Utils::HashBuffer &hashBuffer) {
64 
65  File::Status status;
66 
67  // Open hash file:
68  File hashFile;
69  status = hashFile.open(hashFileName, File::OPEN_READ);
70  if( File::OP_OK != status ) {
71  return status;
72  }
73 
74  // Read hash from checksum file:
75  unsigned char savedHash[HASH_DIGEST_LENGTH];
76  FwSignedSizeType size = static_cast<FwSignedSizeType>(hashBuffer.getBuffCapacity());
77  status = hashFile.read(savedHash, size);
78  if( File::OP_OK != status ) {
79  return status;
80  }
81  if(static_cast<FwSizeType>(size) != hashBuffer.getBuffCapacity()) {
82  return File::BAD_SIZE;
83  }
84  hashFile.close();
85 
86  // Return the hash buffer:
87  Utils::HashBuffer savedHashBuffer(savedHash, static_cast<FwSizeType>(size));
88  hashBuffer = savedHashBuffer;
89 
90  return status;
91  }
92 
93  File::Status writeHash(const char* hashFileName, Utils::HashBuffer hashBuffer) {
94  // Open hash file:
95  File hashFile;
96  File::Status status;
97  status = hashFile.open(hashFileName, File::OPEN_WRITE);
98  if( File::OP_OK != status ) {
99  return status;
100  }
101 
102  // Write out the hash
103  FwSignedSizeType size = static_cast<FwSignedSizeType>(hashBuffer.getBuffLength());
104  status = hashFile.write(hashBuffer.getBuffAddr(), size, Os::File::WaitType::NO_WAIT);
105  if( File::OP_OK != status ) {
106  return status;
107  }
108  if(static_cast<FwSizeType>(size) != hashBuffer.getBuffLength()) {
109  return File::BAD_SIZE;
110  }
111  hashFile.close();
112 
113  return status;
114  }
115 
116  // Enum and function for translating from a status to a validation status:
117  typedef enum {
120  } StatusFileType;
121 
123 
124  switch (type) {
125  case FileType:
126  switch (status) {
127  case File::OP_OK:
129  case File::DOESNT_EXIST:
131  case File::NO_SPACE:
132  return ValidateFile::NO_SPACE;
133  case File::NO_PERMISSION:
135  case File::BAD_SIZE:
137  case File::NOT_OPENED:
139  case File::OTHER_ERROR:
141  default:
142  FW_ASSERT(0, status);
143  }
144  break;
145  case HashFileType:
146  switch (status) {
147  case File::OP_OK:
149  case File::DOESNT_EXIST:
151  case File::NO_SPACE:
152  return ValidateFile::NO_SPACE;
153  case File::NO_PERMISSION:
155  case File::BAD_SIZE:
157  case File::NOT_OPENED:
159  case File::OTHER_ERROR:
161  default:
162  FW_ASSERT(0, status);
163  }
164  break;
165  default:
166  FW_ASSERT(0, type);
167  }
168 
170  }
171 
172  ValidateFile::Status ValidateFile::validate(const char* fileName, const char* hashFileName) {
173  Utils::HashBuffer hashBuffer; // pass by reference - final value is unused
174  return validate(fileName, hashFileName, hashBuffer);
175  }
176 
177  ValidateFile::Status ValidateFile::validate(const char* fileName, const char* hashFileName, Utils::HashBuffer &hashBuffer) {
178 
179  File::Status status;
180 
181  // Read the hash file:
182  Utils::HashBuffer savedHash;
183  status = readHash(hashFileName, savedHash);
184  if( File::OP_OK != status ) {
185  return translateStatus(status, HashFileType);
186  }
187 
188  // Compute the file's hash:
189  Utils::HashBuffer computedHash;
190  status = computeHash(fileName, computedHash);
191  if( File::OP_OK != status ) {
192  return translateStatus(status, FileType);
193  }
194 
195  // Compare hashes and return:
196  if( savedHash != computedHash ){
198  }
199 
200  hashBuffer = savedHash;
201 
203  }
204 
205  ValidateFile::Status ValidateFile::createValidation(const char* fileName, const char* hashFileName, Utils::HashBuffer &hashBuffer) {
206 
207  File::Status status;
208 
209  // Compute the file's hash:
210  status = computeHash(fileName, hashBuffer);
211  if( File::OP_OK != status ) {
212  return translateStatus(status, FileType);
213  }
214 
215  status = writeHash(hashFileName, hashBuffer);
216  if( File::OP_OK != status ) {
217  return translateStatus(status, HashFileType);
218  }
219 
221  }
222 
223  ValidateFile::Status ValidateFile::createValidation(const char* fileName, const char* hashFileName) {
224  Utils::HashBuffer hashBuffer; // pass by reference - final value is unused
225  return createValidation(fileName, hashFileName, hashBuffer);
226  }
227 
228 }
void update(const void *const data, const FwSizeType len)
Definition: CRC32.cpp:56
File doesn&#39;t exist (for read)
#define HASH_DIGEST_LENGTH
Definition: CRC32.hpp:18
No permission to read/write file.
A catch-all for other errors. Have to look in implementation-specific code.
Invalid size parameter.
Status read(U8 *buffer, FwSignedSizeType &size)
read data from this file into supplied buffer bounded by size
Definition: File.cpp:143
No permission to read/write file.
Definition: File.hpp:33
Open file for writing.
Definition: File.hpp:23
Os::FileInterface::Status open(const char *path, Mode mode)
open file with supplied path and mode
Validation file doesn&#39;t exist (for read)
void init()
Definition: CRC32.cpp:50
void final(HashBuffer &buffer)
Definition: CRC32.cpp:67
The validation of the file passed.
Serializable::SizeType getBuffLength() const
returns current buffer size
No space left.
Definition: File.hpp:32
FwSizeType getBuffCapacity() const
File doesn&#39;t exist (for read)
Definition: File.hpp:31
Status validate(const char *fileName, const char *hashFileName, Utils::HashBuffer &hashBuffer)
Validate the contents of a file &#39;fileName&#39; against its hash.
File::Status computeHash(const char *fileName, Utils::HashBuffer &hashBuffer)
void close() override
close the file, if not opened then do nothing
Definition: File.cpp:70
A generic interface for creating and comparing hash values.
Definition: Hash.hpp:24
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:32
Status createValidation(const char *fileName, const char *hash, Utils::HashBuffer &hashBuffer)
No space left on the device for writing.
No permission to read/write file.
file hasn&#39;t been opened yet
Definition: File.hpp:35
Operation was successful.
Definition: File.hpp:30
A catch-all for other errors. Have to look in implementation-specific code.
Definition: File.hpp:40
Defines a file class to validate files or generate a file validator file.
Open file for reading.
Definition: File.hpp:21
A container class for holding a hash buffer.
Definition: HashBuffer.hpp:26
PlatformSignedSizeType FwSignedSizeType
Definition: FpConfig.h:30
static Status getFileSize(const char *path, FwSignedSizeType &size)
Get the size of the file (in bytes) at the specified path.
Definition: FileSystem.cpp:227
Operation was successful.
Definition: FileSystem.hpp:25
File::Status writeHash(const char *hashFileName, Utils::HashBuffer hashBuffer)
Invalid size parameter.
Definition: File.hpp:34
#define VFILE_HASH_CHUNK_SIZE
ValidateFile::Status translateStatus(File::Status status, StatusFileType type)
File::Status readHash(const char *hashFileName, Utils::HashBuffer &hashBuffer)
Status write(const U8 *buffer, FwSignedSizeType &size)
write data to this file from the supplied buffer bounded by size
Definition: File.cpp:163
#define FW_ASSERT(...)
Definition: Assert.hpp:14
The validation of the file did not pass.