2021-08-19 12:55:25 -04:00
|
|
|
/*
|
|
|
|
otp/pdu.cpp
|
|
|
|
|
|
|
|
Copyright (c) 2021 Kevin Matz (kevin.matz@gmail.com)
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
SOFTWARE.
|
|
|
|
*/
|
|
|
|
#include "pdu.h"
|
|
|
|
|
|
|
|
namespace OTP::PDU {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief pdu::pdu
|
|
|
|
*/
|
|
|
|
pdu::pdu()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief pdu::~pdu
|
|
|
|
*/
|
|
|
|
pdu::~pdu()
|
|
|
|
{
|
|
|
|
if (data) delete data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-08-25 17:20:33 -04:00
|
|
|
/**
|
|
|
|
* @brief pdu::stream
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
Stream pdu::stream()
|
|
|
|
{
|
|
|
|
return stream_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-08-19 12:55:25 -04:00
|
|
|
/**
|
|
|
|
* @brief pdu::streamSize
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
size_t pdu::streamSize() const
|
|
|
|
{
|
|
|
|
size_t s = 4; // vector + length
|
|
|
|
|
|
|
|
if (data)
|
|
|
|
s += data->streamSize();
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief pdu::iStream
|
|
|
|
* @param stream
|
|
|
|
*/
|
|
|
|
void pdu::iStream(Stream stream)
|
|
|
|
{
|
|
|
|
uint16_t length;
|
|
|
|
|
|
|
|
*stream >> vector;
|
|
|
|
*stream >> length;
|
|
|
|
|
|
|
|
// length includes the vector and length.
|
|
|
|
// the remainder of the length is data
|
|
|
|
length -= 4;
|
|
|
|
|
|
|
|
// abort if the remaining PDU length isn't available
|
|
|
|
if (!stream->good() || stream->available() < length)
|
|
|
|
return stream->setstate(std::ios_base::failbit);
|
|
|
|
|
|
|
|
// create a stream buffer for the data
|
2023-04-02 13:24:55 -04:00
|
|
|
stream_ = Stream(new bufferstream(stream->data(), length));
|
2021-08-19 12:55:25 -04:00
|
|
|
if (stream_->available() != length)
|
|
|
|
return stream->setstate(std::ios_base::failbit);
|
|
|
|
|
|
|
|
// fast-forward the input stream
|
|
|
|
stream->ignore(length);
|
|
|
|
|
|
|
|
if (!stream->available())
|
|
|
|
return stream->setstate(std::ios_base::eofbit);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief pdu::oStream
|
|
|
|
* @param stream
|
|
|
|
*/
|
|
|
|
void pdu::oStream(Stream stream) const
|
|
|
|
{
|
|
|
|
uint16_t length = streamSize();
|
|
|
|
|
|
|
|
*stream << vector;
|
|
|
|
*stream << length;
|
|
|
|
|
|
|
|
data->oStream(stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace OTP::PDU
|