Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 3 | #include <endian.h> |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 4 | #include <string.h> |
| 5 | |
Zane Shelley | ca9f625 | 2019-10-25 21:17:30 -0500 | [diff] [blame] | 6 | #include <hei_includes.hpp> |
| 7 | |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 8 | namespace libhei |
| 9 | { |
| 10 | |
Zane Shelley | b8b3cdf | 2020-05-04 13:28:04 -0500 | [diff] [blame] | 11 | /** @brief A streaming utility to read a Chip Data File buffer. */ |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 12 | class ChipDataStream |
| 13 | { |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 14 | public: |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 15 | /** |
Zane Shelley | b8b3cdf | 2020-05-04 13:28:04 -0500 | [diff] [blame] | 16 | * @brief Constructor. |
| 17 | * @param i_buffer A pointer to the buffer. |
| 18 | * @param i_bufferSize The buffer size. |
| 19 | */ |
Zane Shelley | fe27b65 | 2019-10-28 11:33:07 -0500 | [diff] [blame] | 20 | ChipDataStream(void* i_buffer, size_t i_bufferSize) : |
Zane Shelley | b8b3cdf | 2020-05-04 13:28:04 -0500 | [diff] [blame] | 21 | iv_buffer(i_buffer), iv_bufferSize(i_bufferSize) |
| 22 | { |
| 23 | HEI_ASSERT(nullptr != i_buffer); |
| 24 | HEI_ASSERT(0 < i_bufferSize); |
| 25 | } |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 26 | |
Zane Shelley | b8b3cdf | 2020-05-04 13:28:04 -0500 | [diff] [blame] | 27 | /** @brief Destructor. */ |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 28 | ~ChipDataStream() = default; |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 29 | |
Zane Shelley | b8b3cdf | 2020-05-04 13:28:04 -0500 | [diff] [blame] | 30 | /** @brief Copy constructor. */ |
| 31 | ChipDataStream(const ChipDataStream&) = delete; |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 32 | |
Zane Shelley | b8b3cdf | 2020-05-04 13:28:04 -0500 | [diff] [blame] | 33 | /** @brief Assignment operator. */ |
| 34 | ChipDataStream& operator=(const ChipDataStream&) = delete; |
| 35 | |
| 36 | private: |
| 37 | /** Pointer to the first address of the Chip Data File buffer. */ |
| 38 | const void* const iv_buffer; |
| 39 | |
| 40 | /** The Chip Data File buffer size. */ |
| 41 | const size_t iv_bufferSize; |
| 42 | |
| 43 | /** Current byte index within the buffer. */ |
| 44 | size_t iv_currentIndex = 0; |
| 45 | |
| 46 | public: |
| 47 | /** @brief Output stream operator. */ |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 48 | template <class D> |
Zane Shelley | fe27b65 | 2019-10-28 11:33:07 -0500 | [diff] [blame] | 49 | ChipDataStream& operator>>(D& o_right) |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 50 | { |
| 51 | read(&o_right, sizeof(D)); |
| 52 | return *this; |
| 53 | } |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 54 | |
| 55 | private: |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 56 | /** |
Zane Shelley | b8b3cdf | 2020-05-04 13:28:04 -0500 | [diff] [blame] | 57 | * @brief Copies the given number of data bytes into the buffer from the |
| 58 | * current index and then increments the index. |
| 59 | * @param o_buf The output buffer. |
| 60 | * @param i_size The number of bytes to copy. |
| 61 | */ |
Zane Shelley | fe27b65 | 2019-10-28 11:33:07 -0500 | [diff] [blame] | 62 | void read(void* o_buf, size_t i_size) |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 63 | { |
Zane Shelley | b8b3cdf | 2020-05-04 13:28:04 -0500 | [diff] [blame] | 64 | // Check for buffer overflow. |
| 65 | HEI_ASSERT((iv_currentIndex + i_size) <= iv_bufferSize); |
| 66 | |
| 67 | // Copy the buffer. |
| 68 | memcpy(o_buf, (char*)iv_buffer + iv_currentIndex, i_size); |
| 69 | |
| 70 | // Increment the curent index for the next piece of data. |
| 71 | iv_currentIndex += i_size; |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 72 | } |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 73 | }; |
| 74 | |
Zane Shelley | b8b3cdf | 2020-05-04 13:28:04 -0500 | [diff] [blame] | 75 | /** @brief Template specialization for uint16_t. */ |
Zane Shelley | 7f7a42d | 2019-10-28 13:28:31 -0500 | [diff] [blame] | 76 | template <> |
| 77 | inline ChipDataStream& ChipDataStream::operator>>(uint16_t& o_right) |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 78 | { |
| 79 | read(&o_right, sizeof(o_right)); |
Zane Shelley | b8b3cdf | 2020-05-04 13:28:04 -0500 | [diff] [blame] | 80 | o_right = be16toh(o_right); // Chip Data is big-endian |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 81 | return *this; |
| 82 | } |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 83 | |
Zane Shelley | b8b3cdf | 2020-05-04 13:28:04 -0500 | [diff] [blame] | 84 | /** @brief Template specialization for int16_t. */ |
Zane Shelley | 7f7a42d | 2019-10-28 13:28:31 -0500 | [diff] [blame] | 85 | template <> |
| 86 | inline ChipDataStream& ChipDataStream::operator>>(int16_t& o_right) |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 87 | { |
| 88 | read(&o_right, sizeof(o_right)); |
Zane Shelley | b8b3cdf | 2020-05-04 13:28:04 -0500 | [diff] [blame] | 89 | o_right = be16toh(o_right); // Chip Data is big-endian |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 90 | return *this; |
| 91 | } |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 92 | |
Zane Shelley | b8b3cdf | 2020-05-04 13:28:04 -0500 | [diff] [blame] | 93 | /** @brief Template specialization for uint32_t. */ |
Zane Shelley | 7f7a42d | 2019-10-28 13:28:31 -0500 | [diff] [blame] | 94 | template <> |
| 95 | inline ChipDataStream& ChipDataStream::operator>>(uint32_t& o_right) |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 96 | { |
| 97 | read(&o_right, sizeof(o_right)); |
Zane Shelley | b8b3cdf | 2020-05-04 13:28:04 -0500 | [diff] [blame] | 98 | o_right = be32toh(o_right); // Chip Data is big-endian |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 99 | return *this; |
| 100 | } |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 101 | |
Zane Shelley | b8b3cdf | 2020-05-04 13:28:04 -0500 | [diff] [blame] | 102 | /** @brief Template specialization for int32_t. */ |
Zane Shelley | 7f7a42d | 2019-10-28 13:28:31 -0500 | [diff] [blame] | 103 | template <> |
| 104 | inline ChipDataStream& ChipDataStream::operator>>(int32_t& o_right) |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 105 | { |
| 106 | read(&o_right, sizeof(o_right)); |
Zane Shelley | b8b3cdf | 2020-05-04 13:28:04 -0500 | [diff] [blame] | 107 | o_right = be32toh(o_right); // Chip Data is big-endian |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 108 | return *this; |
| 109 | } |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 110 | |
Zane Shelley | b8b3cdf | 2020-05-04 13:28:04 -0500 | [diff] [blame] | 111 | /** @brief Template specialization for uint64_t. */ |
Zane Shelley | 7f7a42d | 2019-10-28 13:28:31 -0500 | [diff] [blame] | 112 | template <> |
| 113 | inline ChipDataStream& ChipDataStream::operator>>(uint64_t& o_right) |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 114 | { |
| 115 | read(&o_right, sizeof(o_right)); |
Zane Shelley | b8b3cdf | 2020-05-04 13:28:04 -0500 | [diff] [blame] | 116 | o_right = be64toh(o_right); // Chip Data is big-endian |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 117 | return *this; |
| 118 | } |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 119 | |
Zane Shelley | b8b3cdf | 2020-05-04 13:28:04 -0500 | [diff] [blame] | 120 | /** @brief Template specialization for int64_t. */ |
Zane Shelley | 7f7a42d | 2019-10-28 13:28:31 -0500 | [diff] [blame] | 121 | template <> |
| 122 | inline ChipDataStream& ChipDataStream::operator>>(int64_t& o_right) |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 123 | { |
| 124 | read(&o_right, sizeof(o_right)); |
Zane Shelley | b8b3cdf | 2020-05-04 13:28:04 -0500 | [diff] [blame] | 125 | o_right = be64toh(o_right); // Chip Data is big-endian |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 126 | return *this; |
| 127 | } |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 128 | |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 129 | } // namespace libhei |