summaryrefslogtreecommitdiffstats
path: root/include/ipmid/message/unpack.hpp
blob: d96928fb561b658b8256cffe977f5759cdfa33b7 (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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/**
 * Copyright © 2018 Intel Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#pragma once

#include <array>
#include <ipmid/message/types.hpp>
#include <optional>
#include <string>
#include <tuple>
#include <vector>

namespace ipmi
{

namespace message
{

namespace details
{

/**************************************
 * ipmi return type helpers
 **************************************/

template <typename NumericType, size_t byteIndex = 0>
void UnpackBytes(uint8_t* pointer, NumericType& i)
{
    if constexpr (byteIndex < sizeof(NumericType))
    {
        i |= static_cast<NumericType>(*pointer) << (CHAR_BIT * byteIndex);
        UnpackBytes<NumericType, byteIndex + 1>(pointer + 1, i);
    }
}

template <typename NumericType, size_t byteIndex = 0>
void UnpackBytesUnaligned(Payload& p, NumericType& i)
{
    if constexpr (byteIndex < sizeof(NumericType))
    {
        i |= static_cast<NumericType>(p.popBits(CHAR_BIT))
             << (CHAR_BIT * byteIndex);
        UnpackBytesUnaligned<NumericType, byteIndex + 1>(p, i);
    }
}

/** @struct UnpackSingle
 *  @brief Utility to unpack a single C++ element from a Payload
 *
 *  User-defined types are expected to specialize this template in order to
 *  get their functionality.
 *
 *  @tparam T - Type of element to unpack.
 */
template <typename T>
struct UnpackSingle
{
    /** @brief Do the operation to unpack element.
     *
     *  @param[in] p - Payload to unpack from.
     *  @param[out] t - The reference to unpack item into.
     */
    static int op(Payload& p, T& t)
    {
        if constexpr (std::is_fundamental<T>::value)
        {
            t = 0;
            if (p.bitCount)
            {
                if (p.fillBits(CHAR_BIT * sizeof(t)))
                {
                    return 1;
                }
                UnpackBytesUnaligned<T>(p, t);
            }
            else
            {
                // copy out bits from vector....
                if (p.raw.size() < (p.rawIndex + sizeof(t)))
                {
                    return 1;
                }
                auto iter = p.raw.data() + p.rawIndex;
                t = 0;
                UnpackBytes<T>(iter, t);
                p.rawIndex += sizeof(t);
            }
            return 0;
        }
        else
        {
            if constexpr (utility::is_tuple<T>::value)
            {
                bool priorError = p.unpackError;
                size_t priorIndex = p.rawIndex;
                // more stuff to unroll if partial bytes are out
                size_t priorBitCount = p.bitCount;
                fixed_uint_t<details::bitStreamSize> priorBits = p.bitStream;
                int ret = p.unpack(t);
                if (ret != 0)
                {
                    t = T();
                    p.rawIndex = priorIndex;
                    p.bitStream = priorBits;
                    p.bitCount = priorBitCount;
                    p.unpackError = priorError;
                }
                return 0;
            }
        }
    }
};

/** @struct UnpackSingle
 *  @brief Utility to unpack a single C++ element from a Payload
 *
 *  Specialization to unpack std::string represented as a
 *  UCSD-Pascal style string
 */
template <>
struct UnpackSingle<std::string>
{
    static int op(Payload& p, std::string& t)
    {
        // pop len first
        if (p.rawIndex > (p.raw.size() - sizeof(uint8_t)))
        {
            return 1;
        }
        uint8_t len = p.raw[p.rawIndex++];
        // check to see that there are n bytes left
        auto [first, last] = p.pop<char>(len);
        if (first == last)
        {
            return 1;
        }
        t.reserve(last - first);
        t.insert(0, first, (last - first));
        return 0;
    }
};

/** @brief Specialization of UnpackSingle for fixed_uint_t types
 */
template <unsigned N>
struct UnpackSingle<fixed_uint_t<N>>
{
    static int op(Payload& p, fixed_uint_t<N>& t)
    {
        static_assert(N <= (details::bitStreamSize - CHAR_BIT));
        constexpr size_t count = N;
        // acquire enough bits in the stream to fulfill the Payload
        if (p.fillBits(count))
        {
            return -1;
        }
        fixed_uint_t<details::bitStreamSize> bitmask = ((1 << count) - 1);
        t = (p.bitStream & bitmask).convert_to<fixed_uint_t<N>>();
        p.bitStream >>= count;
        p.bitCount -= count;
        return 0;
    }
};

/** @brief Specialization of UnpackSingle for bool. */
template <>
struct UnpackSingle<bool>
{
    static int op(Payload& p, bool& b)
    {
        // acquire enough bits in the stream to fulfill the Payload
        if (p.fillBits(1))
        {
            return -1;
        }
        b = static_cast<bool>(p.bitStream & 0x01);
        // clear bits from stream
        p.bitStream >>= 1;
        p.bitCount -= 1;
        return 0;
    }
};

/** @brief Specialization of UnpackSingle for std::bitset<N>
 */
template <size_t N>
struct UnpackSingle<std::bitset<N>>
{
    static int op(Payload& p, std::bitset<N>& t)
    {
        static_assert(N <= (details::bitStreamSize - CHAR_BIT));
        size_t count = N;
        // acquire enough bits in the stream to fulfill the Payload
        if (p.fillBits(count))
        {
            return -1;
        }
        fixed_uint_t<details::bitStreamSize> bitmask = ((1 << count) - 1);
        t |= (p.bitStream & bitmask).convert_to<unsigned long long>();
        p.bitStream >>= count;
        p.bitCount -= count;
        return 0;
    }
};

/** @brief Specialization of UnpackSingle for std::optional<T> */
template <typename T>
struct UnpackSingle<std::optional<T>>
{
    static int op(Payload& p, std::optional<T>& t)
    {
        bool priorError = p.unpackError;
        size_t priorIndex = p.rawIndex;
        // more stuff to unroll if partial bytes are out
        size_t priorBitCount = p.bitCount;
        fixed_uint_t<details::bitStreamSize> priorBits = p.bitStream;
        t.emplace();
        int ret = UnpackSingle<T>::op(p, *t);
        if (ret != 0)
        {
            t.reset();
            p.rawIndex = priorIndex;
            p.bitStream = priorBits;
            p.bitCount = priorBitCount;
            p.unpackError = priorError;
        }
        return 0;
    }
};

/** @brief Specialization of UnpackSingle for std::array<T, N> */
template <typename T, size_t N>
struct UnpackSingle<std::array<T, N>>
{
    static int op(Payload& p, std::array<T, N>& t)
    {
        int ret = 0;
        size_t priorIndex = p.rawIndex;
        for (auto& v : t)
        {
            ret = UnpackSingle<T>::op(p, v);
            if (ret)
            {
                p.rawIndex = priorIndex;
                t = std::array<T, N>();
                break;
            }
        }
        return ret;
    }
};

/** @brief Specialization of UnpackSingle for std::array<uint8_t> */
template <size_t N>
struct UnpackSingle<std::array<uint8_t, N>>
{
    static int op(Payload& p, std::array<uint8_t, N>& t)
    {
        if (p.raw.size() - p.rawIndex < N)
        {
            t.fill(0);
            return -1;
        }
        // copy out the bytes
        std::copy(p.raw.begin() + p.rawIndex, p.raw.begin() + p.rawIndex + N,
                  t.begin());
        p.rawIndex += N;
        return 0;
    }
};

/** @brief Specialization of UnpackSingle for std::vector<T> */
template <typename T>
struct UnpackSingle<std::vector<T>>
{
    static int op(Payload& p, std::vector<T>& t)
    {
        int ret = 0;
        while (p.rawIndex < p.raw.size())
        {
            t.emplace_back();
            ret = UnpackSingle<T>::op(p, t.back());
            if (ret)
            {
                t.pop_back();
                break;
            }
        }
        return ret;
    }
};

/** @brief Specialization of UnpackSingle for std::vector<uint8_t> */
template <>
struct UnpackSingle<std::vector<uint8_t>>
{
    static int op(Payload& p, std::vector<uint8_t>& t)
    {
        // copy out the remainder of the message
        t.reserve(p.raw.size() - p.rawIndex);
        t.insert(t.begin(), p.raw.begin() + p.rawIndex, p.raw.end());
        p.rawIndex = p.raw.size();
        return 0;
    }
};

/** @brief Specialization of UnpackSingle for Payload */
template <>
struct UnpackSingle<Payload>
{
    static int op(Payload& p, Payload& t)
    {
        // mark that this payload is being included in the args
        p.trailingOk = true;
        t = p;
        // reset the unpacking flags so it can be properly checked
        t.trailingOk = false;
        t.unpackCheck = true;
        t.unpackError = false;
        return 0;
    }
};

} // namespace details

} // namespace message

} // namespace ipmi
OpenPOWER on IntegriCloud