[635a28f] | 1 | /* |
---|
| 2 | * Copyright (c) 2016, Chris Johns <chrisj@rtems.org> |
---|
| 3 | * |
---|
| 4 | * Permission to use, copy, modify, and/or distribute this software for any |
---|
| 5 | * purpose with or without fee is hereby granted, provided that the above |
---|
| 6 | * copyright notice and this permission notice appear in all copies. |
---|
| 7 | * |
---|
| 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
---|
| 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
---|
| 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
---|
| 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
---|
| 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
---|
| 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
---|
| 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
---|
| 15 | */ |
---|
| 16 | /** |
---|
| 17 | * @file |
---|
| 18 | * |
---|
| 19 | * @ingroup rtems-ld |
---|
| 20 | * |
---|
| 21 | * @brief A buffer of data. |
---|
| 22 | * |
---|
| 23 | */ |
---|
| 24 | |
---|
| 25 | #if !defined (_RLD_BUFFER_H_) |
---|
| 26 | #define _RLD_BUFFER_H_ |
---|
| 27 | |
---|
| 28 | #include <string> |
---|
| 29 | |
---|
| 30 | #include <rld-files.h> |
---|
| 31 | |
---|
| 32 | namespace rld |
---|
| 33 | { |
---|
| 34 | namespace buffer |
---|
| 35 | { |
---|
| 36 | /** |
---|
| 37 | * A buffer to help manage formats. |
---|
| 38 | */ |
---|
| 39 | class buffer |
---|
| 40 | { |
---|
| 41 | public: |
---|
| 42 | /** |
---|
| 43 | * Create a buffer. |
---|
| 44 | */ |
---|
| 45 | buffer (const size_t size, bool le = true); |
---|
| 46 | |
---|
| 47 | /** |
---|
| 48 | * An empty buffer |
---|
| 49 | */ |
---|
| 50 | buffer (); |
---|
| 51 | |
---|
| 52 | /** |
---|
| 53 | * Copy a buffer. |
---|
| 54 | */ |
---|
| 55 | buffer (const buffer& orig); |
---|
| 56 | |
---|
| 57 | /* |
---|
| 58 | * Destory the buffer. |
---|
| 59 | */ |
---|
| 60 | ~buffer (); |
---|
| 61 | |
---|
| 62 | /** |
---|
| 63 | * Clear the buffer reseting the level to zero. |
---|
| 64 | */ |
---|
| 65 | void clear (); |
---|
| 66 | |
---|
| 67 | /** |
---|
| 68 | * Write the data to the buffer. |
---|
| 69 | * |
---|
| 70 | * @param data The data to write to the buffer. |
---|
| 71 | * @param length The amount of data in bytes to write. |
---|
| 72 | */ |
---|
| 73 | void write (const void* data_, const size_t length); |
---|
| 74 | |
---|
| 75 | /** |
---|
| 76 | * Read the data from the buffer. |
---|
| 77 | * |
---|
| 78 | * @param data Read from the buffer in the data. |
---|
| 79 | * @param length The amount of data in bytes to read. |
---|
| 80 | */ |
---|
| 81 | void read (void* data_, const size_t length); |
---|
| 82 | |
---|
| 83 | /** |
---|
| 84 | * Fill the data to the buffer. |
---|
| 85 | * |
---|
| 86 | * @param length The amount of data in bytes to fill with. |
---|
| 87 | * @param value The value to fill the buffer with. |
---|
| 88 | */ |
---|
| 89 | void fill (const size_t length, const uint8_t value = 0); |
---|
| 90 | |
---|
| 91 | /** |
---|
| 92 | * Set the write pointer in the buffer to the level provided filing with |
---|
| 93 | * the value also provided. |
---|
| 94 | * |
---|
| 95 | * @param out_ The new out pointer. |
---|
| 96 | * @param value The value to fill the buffer with. |
---|
| 97 | */ |
---|
| 98 | void set (const size_t out_, const uint8_t value = 0); |
---|
| 99 | |
---|
| 100 | /** |
---|
| 101 | * Skip the data in the buffer moving the read pointer. |
---|
| 102 | * |
---|
| 103 | * @param length The amount of data in bytes to skip. |
---|
| 104 | */ |
---|
| 105 | void skip (const size_t length); |
---|
| 106 | |
---|
| 107 | /** |
---|
| 108 | * Rewind the in pointer the buffer to the pointer. |
---|
| 109 | * |
---|
| 110 | * @param in_ The new in pointer. |
---|
| 111 | */ |
---|
| 112 | void rewind (const size_t in_); |
---|
| 113 | |
---|
| 114 | /* |
---|
| 115 | * The level in the buffer. |
---|
| 116 | */ |
---|
| 117 | size_t level () const; |
---|
| 118 | |
---|
| 119 | /* |
---|
| 120 | * Write the data buffered to the image. Clear the buffer after. |
---|
| 121 | */ |
---|
| 122 | void write (files::image& img); |
---|
| 123 | |
---|
| 124 | /* |
---|
| 125 | * Read the data from the image into the start of buffer. |
---|
| 126 | */ |
---|
| 127 | void read (files::image& img, size_t length = 0); |
---|
| 128 | |
---|
| 129 | /* |
---|
| 130 | * Dump. |
---|
| 131 | */ |
---|
| 132 | void dump (); |
---|
| 133 | |
---|
| 134 | private: |
---|
| 135 | uint8_t* data; //< The data held in the buffer. |
---|
| 136 | size_t size; //< The zie of the date the buffer hold. |
---|
| 137 | bool le; //< True is little endian else it is big. |
---|
| 138 | size_t in; //< The data in pointer, used when writing. |
---|
| 139 | size_t out; //< The data out ponter, used when reading. |
---|
| 140 | size_t level_; //< The level of data in the buffer. |
---|
| 141 | }; |
---|
| 142 | |
---|
| 143 | /** |
---|
| 144 | * Buffer template function for writing data to the buffer. |
---|
| 145 | */ |
---|
| 146 | template < typename T > |
---|
| 147 | void write (buffer& buf, const T value) |
---|
| 148 | { |
---|
| 149 | uint8_t bytes[sizeof (T)]; |
---|
| 150 | T v = value; |
---|
| 151 | int b = sizeof (T) - 1; |
---|
| 152 | while (b >= 0) |
---|
| 153 | { |
---|
| 154 | bytes[b--] = (uint8_t) v; |
---|
| 155 | v >>= 8; |
---|
| 156 | } |
---|
| 157 | buf.write (bytes, sizeof (T)); |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | /** |
---|
| 161 | * Buffer template function for reading data to the buffer. |
---|
| 162 | */ |
---|
| 163 | template < typename T > |
---|
| 164 | void read (buffer& buf, T& value) |
---|
| 165 | { |
---|
| 166 | uint8_t bytes[sizeof (T)]; |
---|
| 167 | int b = sizeof (T) - 1; |
---|
| 168 | buf.read (bytes, sizeof(T)); |
---|
| 169 | value = 0; |
---|
| 170 | while (b >= 0) |
---|
| 171 | { |
---|
| 172 | value <<= 8; |
---|
| 173 | value |= (T) bytes[b--]; |
---|
| 174 | } |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | /* |
---|
| 178 | * Insertion operators. |
---|
| 179 | */ |
---|
| 180 | buffer& operator<< (buffer& buf, const uint64_t value); |
---|
| 181 | buffer& operator<< (buffer& buf, const uint32_t value); |
---|
| 182 | buffer& operator<< (buffer& buf, const uint16_t value); |
---|
| 183 | buffer& operator<< (buffer& buf, const uint8_t value); |
---|
| 184 | buffer& operator<< (buffer& buf, const std::string& str); |
---|
| 185 | |
---|
| 186 | /* |
---|
| 187 | * Extraction operators. |
---|
| 188 | */ |
---|
| 189 | buffer& operator>> (buffer& buf, uint64_t& value); |
---|
| 190 | buffer& operator>> (buffer& buf, uint32_t& value); |
---|
| 191 | buffer& operator>> (buffer& buf, uint16_t& value); |
---|
| 192 | buffer& operator>> (buffer& buf, uint8_t& value); |
---|
| 193 | |
---|
| 194 | /* |
---|
| 195 | * Buffer fill manipulator. |
---|
| 196 | */ |
---|
| 197 | struct b_fill |
---|
| 198 | { |
---|
| 199 | const uint8_t value; |
---|
| 200 | const size_t amount; |
---|
| 201 | |
---|
| 202 | b_fill (const size_t amount, const uint8_t value) |
---|
| 203 | : value (value), |
---|
| 204 | amount (amount) { |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | friend buffer& operator<< (buffer& buf, const b_fill& bf) { |
---|
| 208 | buf.fill (bf.amount, bf.value); |
---|
| 209 | return buf; |
---|
| 210 | } |
---|
| 211 | }; |
---|
| 212 | |
---|
| 213 | b_fill fill (const size_t amount, const uint8_t value = 0); |
---|
| 214 | |
---|
| 215 | /* |
---|
| 216 | * Buffer set manipulator. |
---|
| 217 | */ |
---|
| 218 | struct b_set |
---|
| 219 | { |
---|
| 220 | const uint8_t value; |
---|
| 221 | const size_t level; |
---|
| 222 | |
---|
| 223 | b_set (const size_t level, const uint8_t value) |
---|
| 224 | : value (value), |
---|
| 225 | level (level) { |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | friend buffer& operator<< (buffer& buf, const b_set& bs) { |
---|
| 229 | buf.set (bs.level, bs.value); |
---|
| 230 | return buf; |
---|
| 231 | } |
---|
| 232 | }; |
---|
| 233 | |
---|
| 234 | b_set set (const size_t level, const uint8_t value = 0); |
---|
| 235 | |
---|
| 236 | /* |
---|
| 237 | * Buffer skip manipulator. |
---|
| 238 | */ |
---|
| 239 | struct b_skip |
---|
| 240 | { |
---|
| 241 | const size_t amount; |
---|
| 242 | |
---|
| 243 | b_skip (const size_t amount) |
---|
| 244 | : amount (amount) { |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | friend buffer& operator>> (buffer& buf, const b_skip& bs) { |
---|
| 248 | buf.skip (bs.amount); |
---|
| 249 | return buf; |
---|
| 250 | } |
---|
| 251 | }; |
---|
| 252 | |
---|
| 253 | b_skip skip (const size_t amount); |
---|
| 254 | } |
---|
| 255 | } |
---|
| 256 | |
---|
| 257 | #endif |
---|