Message*
    Packet::deserialize(std::istream& ifs)
    {
      std::vector<char> data;

      // Get the message header.
      data.resize(DUNE_IMC_CONST_HEADER_SIZE);
      ifs.read(&data[0], DUNE_IMC_CONST_HEADER_SIZE);

      // If we're at the EOF there's nothing more to do.
      if (ifs.eof())
        return 0;

      if (ifs.gcount() < DUNE_IMC_CONST_HEADER_SIZE)
        throw BufferTooShort();

      Header hdr;
      deserializeHeader(hdr, (uint8_t*)&data[0], DUNE_IMC_CONST_HEADER_SIZE);

      // Get remaining data.
      uint16_t remaining = hdr.size + DUNE_IMC_CONST_FOOTER_SIZE;
      data.resize(DUNE_IMC_CONST_HEADER_SIZE + remaining);
      ifs.read(&data[DUNE_IMC_CONST_HEADER_SIZE], remaining);

      if (ifs.gcount() < remaining)
        throw BufferTooShort();

      return deserializePayload(hdr, (uint8_t*)&data[0], DUNE_IMC_CONST_HEADER_SIZE + remaining, 0);
    }
    Message*
    Packet::deserialize(std::istream& ifs, Utils::ByteBuffer& bfr)
    {
      // Get the message header.
      bfr.setSize(DUNE_IMC_CONST_HEADER_SIZE);
      ifs.read(bfr.getBufferSigned(), DUNE_IMC_CONST_HEADER_SIZE);

      // If we're at the EOF there's nothing more to do.
      if (ifs.eof())
        return 0;

      if (ifs.gcount() < DUNE_IMC_CONST_HEADER_SIZE)
        throw BufferTooShort();

      Header hdr;
      deserializeHeader(hdr, bfr.getBuffer(), DUNE_IMC_CONST_HEADER_SIZE);

      // Get remaining data.
      uint16_t remaining = hdr.size + DUNE_IMC_CONST_FOOTER_SIZE;
      bfr.setSize(DUNE_IMC_CONST_HEADER_SIZE + remaining);
      ifs.read(bfr.getBufferSigned() + DUNE_IMC_CONST_HEADER_SIZE, remaining);

      if (ifs.gcount() < remaining)
        throw BufferTooShort();

      return deserializePayload(hdr, bfr.getBuffer(), DUNE_IMC_CONST_HEADER_SIZE + remaining, 0);
    }
Exemple #3
0
uint16_t
deserialize(std::vector<char>& t, const uint8_t* bfr, uint16_t& bfr_len)
{
    if (bfr_len < 2)
        throw BufferTooShort();

    uint16_t s = 0;
    std::memcpy(&s, bfr, 2);

    if (bfr_len < s + 2)
        throw BufferTooShort();

    t.assign((const char*)(bfr + 2), (const char*)(bfr + 2 + s));
    bfr_len -= s + 2;

    return s + 2;
}
Exemple #4
0
uint16_t
reverseDeserialize(std::string& t, const uint8_t* bfr, uint16_t& bfr_len)
{
    if (bfr_len < 2)
        throw BufferTooShort();

    uint16_t s = 0;
    Utils::reverseCopy(s, (char*)bfr);

    if (bfr_len < s + 2)
        throw BufferTooShort();

    t.assign((const char*)(bfr + 2), s);
    bfr_len -= s + 2;

    return s + 2;
}
Exemple #5
0
    inline uint16_t
    reverseDeserialize(Type& t, const uint8_t* bfr, uint16_t& length)
    {
      uint16_t size = sizeof(Type);

      if (length < size)
        throw BufferTooShort();

      Utils::reverseCopy(t, (char*)bfr);
      length -= size;

      return size;
    }
Exemple #6
0
    inline uint16_t
    deserialize(Type& t, const uint8_t* bfr, uint16_t& length)
    {
      uint16_t size = sizeof(Type);

      if (length < size)
        throw BufferTooShort();

      std::memcpy(&t, bfr, size);
      length -= size;

      return size;
    }
    Message*
    Packet::deserialize(const uint8_t* bfr, uint16_t bfr_len, Message* msg)
    {
      Header hdr;

      // Get the message header.
      deserializeHeader(hdr, bfr, bfr_len);

      // Check if we can unpack the message.
      if (hdr.size > bfr_len - (DUNE_IMC_CONST_HEADER_SIZE + DUNE_IMC_CONST_FOOTER_SIZE))
        throw BufferTooShort();

      return deserializePayload(hdr, bfr, bfr_len, msg);
    }
    uint16_t
    Packet::serialize(const Message* msg, uint8_t* bfr, uint16_t size)
    {
      unsigned total = msg->getSerializationSize();
      if (total > DUNE_IMC_CONST_MAX_SIZE)
        throw InvalidMessageSize(total);

      uint16_t n = total;
      uint8_t* ptr = bfr;

      if (size < n)
        throw BufferTooShort();

      ptr += serializeHeader(msg, bfr, size);
      msg->serializeFields(ptr);

      uint16_t crc = Algorithms::CRC16::compute(bfr, n - DUNE_IMC_CONST_FOOTER_SIZE);
      IMC::serialize(crc, (bfr + (n - DUNE_IMC_CONST_FOOTER_SIZE)));

      return n;
    }
Exemple #9
0
    unsigned long
    Bzip2Compressor::compressBlock(char* dst, unsigned long dst_len, char* src, unsigned long src_len)
    {
      // Get proper compression level.
      int plevel = level();
      if (plevel < 1)
        plevel = 1;

      unsigned compressed_length = dst_len;
      int rv = BZ2_bzBuffToBuffCompress(dst, &compressed_length, src, src_len, plevel, 0, 0);

      if (rv == BZ_OK)
        return compressed_length;

      if (rv == BZ_MEM_ERROR)
        throw OutOfMemory();

      if (rv == BZ_OUTBUFF_FULL)
        throw BufferTooShort(dst_len);

      return 0;
    }
    void
    Packet::deserializeHeader(Header& hdr, const uint8_t* bfr, uint16_t bfr_len)
    {
      // Check if we can at least parse the header.
      if (bfr_len < DUNE_IMC_CONST_HEADER_SIZE)
        throw BufferTooShort();

      // Parse synchronize number and get byte order.
      Utils::ByteCopy::copy(hdr.sync, bfr);

      // Read header.
      if (hdr.sync == DUNE_IMC_CONST_SYNC)
      {
        Utils::ByteCopy::copy(hdr.mgid, bfr + 2);
        Utils::ByteCopy::copy(hdr.size, bfr + 4);
        Utils::ByteCopy::copy(hdr.timestamp, bfr + 6);
        Utils::ByteCopy::copy(hdr.src, bfr + 14);
        Utils::ByteCopy::copy(hdr.src_ent, bfr + 16);
        Utils::ByteCopy::copy(hdr.dst, bfr + 17);
        Utils::ByteCopy::copy(hdr.dst_ent, bfr + 19);
      }
      else if (hdr.sync == DUNE_IMC_CONST_SYNC_REV)
      {
        Utils::ByteCopy::rcopy(hdr.mgid, bfr + 2);
        Utils::ByteCopy::rcopy(hdr.size, bfr + 4);
        Utils::ByteCopy::rcopy(hdr.timestamp, bfr + 6);
        Utils::ByteCopy::rcopy(hdr.src, bfr + 14);
        Utils::ByteCopy::rcopy(hdr.src_ent, bfr + 16);
        Utils::ByteCopy::rcopy(hdr.dst, bfr + 17);
        Utils::ByteCopy::rcopy(hdr.dst_ent, bfr + 19);
      }
      else
      {
        throw InvalidSync(hdr.sync);
      }
    }