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