| 195 | // NOTE: the data in 'result' may be modified even in the case of a failed read. |
| 196 | template<typename ReadableFileType> |
| 197 | Status ValidateAndReadData(ReadableFileType* reader, uint64_t file_size, |
| 198 | uint64_t* offset, uint64_t length, |
| 199 | faststring* result) { |
| 200 | // Validate the read length using the file size. |
| 201 | if (*offset + length > file_size) { |
| 202 | return Status::Incomplete("File size not large enough to be valid", |
| 203 | Substitute("Proto container file $0: " |
| 204 | "Tried to read $1 bytes at offset " |
| 205 | "$2 but file size is only $3 bytes", |
| 206 | reader->filename(), length, |
| 207 | *offset, file_size)); |
| 208 | } |
| 209 | |
| 210 | // Perform the read. |
| 211 | result->resize(length); |
| 212 | RETURN_NOT_OK(reader->Read(*offset, Slice(*result))); |
| 213 | *offset += length; |
| 214 | return Status::OK(); |
| 215 | } |
| 216 | |
| 217 | // Helper macro for use with ParseAndCompareChecksum(). Example usage: |
| 218 | // RETURN_NOT_OK_PREPEND(ParseAndCompareChecksum(checksum.data(), { data }), |
no test coverage detected