/** * 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 #include #include #include #include #include namespace ipmi { namespace message { namespace details { /************************************** * ipmi return type helpers **************************************/ template void UnpackBytes(uint8_t* pointer, NumericType& i) { if constexpr (byteIndex < sizeof(NumericType)) { i |= static_cast(*pointer) << (CHAR_BIT * byteIndex); UnpackBytes(pointer + 1, i); } } template void UnpackBytesUnaligned(Payload& p, NumericType& i) { if constexpr (byteIndex < sizeof(NumericType)) { i |= static_cast(p.popBits(CHAR_BIT)) << (CHAR_BIT * byteIndex); UnpackBytesUnaligned(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 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::value) { t = 0; if (p.bitCount) { if (p.fillBits(CHAR_BIT * sizeof(t))) { return 1; } UnpackBytesUnaligned(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(iter, t); p.rawIndex += sizeof(t); } return 0; } else if constexpr (utility::is_tuple::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 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 ret; } else { static_assert( utility::dependent_false::value, "Attempt to unpack a type that has no IPMI unpack operation"); } } }; /** @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 { 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(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 struct UnpackSingle> { static int op(Payload& p, fixed_uint_t& 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 bitmask = ((1 << count) - 1); t = (p.bitStream & bitmask).convert_to>(); p.bitStream >>= count; p.bitCount -= count; return 0; } }; /** @brief Specialization of UnpackSingle for bool. */ template <> struct UnpackSingle { 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(p.bitStream & 0x01); // clear bits from stream p.bitStream >>= 1; p.bitCount -= 1; return 0; } }; /** @brief Specialization of UnpackSingle for std::bitset */ template struct UnpackSingle> { static int op(Payload& p, std::bitset& 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 bitmask = ~fixed_uint_t(0) >> (details::bitStreamSize - count); t |= (p.bitStream & bitmask).convert_to(); p.bitStream >>= count; p.bitCount -= count; return 0; } }; /** @brief Specialization of UnpackSingle for std::optional */ template struct UnpackSingle> { static int op(Payload& p, std::optional& 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 priorBits = p.bitStream; t.emplace(); int ret = UnpackSingle::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 */ template struct UnpackSingle> { static int op(Payload& p, std::array& t) { int ret = 0; size_t priorIndex = p.rawIndex; for (auto& v : t) { ret = UnpackSingle::op(p, v); if (ret) { p.rawIndex = priorIndex; t = std::array(); break; } } return ret; } }; /** @brief Specialization of UnpackSingle for std::array */ template struct UnpackSingle> { static int op(Payload& p, std::array& 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 */ template struct UnpackSingle> { static int op(Payload& p, std::vector& t) { while (p.rawIndex < p.raw.size()) { t.emplace_back(); if (UnpackSingle::op(p, t.back())) { t.pop_back(); break; } } // unpacking a vector is always successful: // either stuff was unpacked successfully (return 0) // or stuff was not unpacked, but should still return // success because an empty vector or a not-fully-unpacked // payload is not a failure. return 0; } }; /** @brief Specialization of UnpackSingle for std::vector */ template <> struct UnpackSingle> { static int op(Payload& p, std::vector& 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 { static int op(Payload& p, Payload& t) { t = p; // mark that this payload is being included in the args p.trailingOk = true; return 0; } }; } // namespace details } // namespace message } // namespace ipmi