summaryrefslogtreecommitdiff
path: root/rtemstoolkit/rld-buffer.h
blob: 08df4baa5ce3fb7886d977c9d92ba67e1d06c07e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
 * Copyright (c) 2016, Chris Johns <chrisj@rtems.org>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */
/**
 * @file
 *
 * @ingroup rtems-ld
 *
 * @brief A buffer of data.
 *
 */

#if !defined (_RLD_BUFFER_H_)
#define _RLD_BUFFER_H_

#include <string>

#include <rld-files.h>

namespace rld
{
  namespace buffer
  {
    /**
     * A buffer to help manage formats.
     */
    class buffer
    {
    public:
      /**
       * Create a buffer.
       */
      buffer (const size_t size, bool le = true);

      /**
       * An empty buffer
       */
      buffer ();

      /**
       * Copy a buffer.
       */
      buffer (const buffer& orig);

      /*
       * Destory the buffer.
       */
      ~buffer ();

      /*
       * Return true if the byte order is little-endian.
       */
      bool little_endian () const {
        return le;
      }

      /**
       * Clear the buffer reseting the level to zero.
       */
      void clear ();

      /**
       * Write the data to the buffer.
       *
       * @param data The data to write to the buffer.
       * @param length The amount of data in bytes to write.
       */
      void write (const void* data_, const size_t length);

      /**
       * Read the data from the buffer.
       *
       * @param data Read from the buffer in the data.
       * @param length The amount of data in bytes to read.
       */
      void read (void* data_, const size_t length);

      /**
       * Fill the data to the buffer.
       *
       * @param length The amount of data in bytes to fill with.
       * @param value The value to fill the buffer with.
       */
      void fill (const size_t length, const uint8_t value = 0);

      /**
       * Set the write pointer in the buffer to the level provided filing with
       * the value also provided.
       *
       * @param out_ The new out pointer.
       * @param value The value to fill the buffer with.
       */
      void set (const size_t out_, const uint8_t value = 0);

      /**
       * Skip the data in the buffer moving the read pointer.
       *
       * @param length The amount of data in bytes to skip.
       */
      void skip (const size_t length);

      /**
       * Rewind the in pointer the buffer to the pointer.
       *
       * @param in_ The new in pointer.
       */
      void rewind (const size_t in_);

      /*
       * The level in the buffer.
       */
      size_t level () const;

      /*
       * Write the data buffered to the image. Clear the buffer after.
       */
      void write (files::image& img);

      /*
       * Read the data from the image into the start of buffer.
       */
      void read (files::image& img, size_t length = 0);

      /*
       * Dump.
       */
      void dump ();

    private:
      uint8_t* data;    //< The data held in the buffer.
      size_t   size;    //< The zie of the date the buffer hold.
      bool     le;      //< True is little endian else it is big.
      size_t   in;      //< The data in pointer, used when writing.
      size_t   out;     //< The data out ponter, used when reading.
      size_t   level_;  //< The level of data in the buffer.
    };

    /**
     * Buffer template function for writing data to the buffer.
     */
    template < typename T >
    void write (buffer& buf, const T value)
    {
      uint8_t bytes[sizeof (T)];
      T       v = value;
      if (buf.little_endian ())
      {
        size_t b = sizeof (T);
        while (b != 0)
        {
          bytes[--b] = (uint8_t) v;
          v >>= 8;
        }
      }
      else
      {
        size_t b = 0;
        while (b < sizeof (T))
        {
          bytes[b++] = (uint8_t) v;
          v >>= 8;
        }
      }
      buf.write (bytes, sizeof (T));
    }

    /**
     * Buffer template function for reading data to the buffer.
     */
    template < typename T >
    void read (buffer& buf, T& value)
    {
      uint8_t bytes[sizeof (T)];
      buf.read (bytes, sizeof (T));
      value = 0;
      if (buf.little_endian ())
      {
        size_t b = sizeof (T);
        while (b != 0)
        {
          value <<= 8;
          value |= (T) bytes[--b];
        }
      }
      else
      {
        size_t b = 0;
        while (b < sizeof (T))
        {
          value <<= 8;
          value |= (T) bytes[b++];
        }
      }
    }

    /*
     * Insertion operators.
     */
    buffer& operator<< (buffer& buf, const uint64_t value);
    buffer& operator<< (buffer& buf, const uint32_t value);
    buffer& operator<< (buffer& buf, const uint16_t value);
    buffer& operator<< (buffer& buf, const uint8_t value);
    buffer& operator<< (buffer& buf, const std::string& str);

    /*
     * Extraction operators.
     */
    buffer& operator>> (buffer& buf, uint64_t& value);
    buffer& operator>> (buffer& buf, uint32_t& value);
    buffer& operator>> (buffer& buf, uint16_t& value);
    buffer& operator>> (buffer& buf, uint8_t& value);

    /*
     * Buffer fill manipulator.
     */
    struct b_fill
    {
      const uint8_t value;
      const size_t  amount;

      b_fill (const size_t amount, const uint8_t value)
        : value (value),
          amount (amount) {
      }

      friend buffer& operator<< (buffer& buf, const b_fill& bf) {
        buf.fill (bf.amount, bf.value);
        return buf;
      }
    };

    b_fill fill (const size_t amount, const uint8_t value = 0);

    /*
     * Buffer set manipulator.
     */
    struct b_set
    {
      const uint8_t value;
      const size_t  level;

      b_set (const size_t level, const uint8_t value)
        : value (value),
          level (level) {
      }

      friend buffer& operator<< (buffer& buf, const b_set& bs) {
        buf.set (bs.level, bs.value);
        return buf;
      }
    };

    b_set set (const size_t level, const uint8_t value = 0);

    /*
     * Buffer skip manipulator.
     */
    struct b_skip
    {
      const size_t amount;

      b_skip (const size_t amount)
        : amount (amount) {
      }

      friend buffer& operator>> (buffer& buf, const b_skip& bs) {
        buf.skip (bs.amount);
        return buf;
      }
    };

    b_skip skip (const size_t amount);
  }
}

#endif