コード例 #1
0
ファイル: packet.cpp プロジェクト: LuaxY/CawotteSrv
bool Packet::deserialize(ByteArray& buffer)
{
    _header = 0;
    _id = 0;
    _lengthType = 0;
    _length = 0;
    _data.clear();

    BinaryReader reader(buffer);
    uint countReadedBytes = 0;

    if (reader.bytesAvailable() < sizeof(_header))
    {
        return false;
    }

    _header = reader.readUShort();
    _id = getMessageId(_header);
    _lengthType = getMessageLengthType(_header);
    countReadedBytes += sizeof(_header);

    if (reader.bytesAvailable() < _lengthType)
    {
        return false;
    }

    _length = getMessageLength(_lengthType, reader);
    countReadedBytes += _lengthType;

    if (reader.bytesAvailable() < _length)
    {
        return false;
    }

    _data = reader.readBytes(_length);
    countReadedBytes += _length;

    buffer.erase(buffer.begin(), buffer.begin() + countReadedBytes);

    return true;
}
コード例 #2
0
shared_ptr<PfileHeader>PfileHeaderReader::ReadHeader(
  rmscrypto::api::SharedStream stream,
  uint32_t                     majorVersion,
  uint32_t                     minorVersion,
  const string               & cleartextRedirectionHeader)
{
  uint32_t headerSize;
  uint32_t extensionOffset = 0;
  uint32_t extensionLength = 0;
  uint32_t plOffset        = 0;
  uint32_t plLength        = 0;
  uint32_t contentOffset   = 0;

  stream->Read(reinterpret_cast<uint8_t *>(&headerSize),      sizeof(uint32_t));
  stream->Read(reinterpret_cast<uint8_t *>(&extensionOffset), sizeof(uint32_t));
  stream->Read(reinterpret_cast<uint8_t *>(&extensionLength), sizeof(uint32_t));
  stream->Read(reinterpret_cast<uint8_t *>(&plOffset),        sizeof(uint32_t));
  stream->Read(reinterpret_cast<uint8_t *>(&plLength),        sizeof(uint32_t));
  stream->Read(reinterpret_cast<uint8_t *>(&contentOffset),   sizeof(uint32_t));

  uint32_t endOfHeader      = plOffset + plLength;
  uint64_t originalFileSize = 0;
  uint32_t metadataOffset   = 0;
  uint32_t metadataLength   = 0;

  ByteArray metadata;
  ByteArray publishingLicense;

  if ((majorVersion >= 2) && (minorVersion >= 1))
  {
    stream->Read(reinterpret_cast<uint8_t *>(&originalFileSize),
                 sizeof(uint64_t));
    stream->Read(reinterpret_cast<uint8_t *>(&metadataOffset),
                 sizeof(uint32_t));
    stream->Read(reinterpret_cast<uint8_t *>(&metadataLength),
                 sizeof(uint32_t));
    endOfHeader = metadataOffset + metadataLength;
  }

  if (contentOffset < endOfHeader) {
    throw exceptions::RMSPFileException("Bad content offset",
                                        exceptions::RMSPFileException::BadArguments);
  }

  string extension = ReadExtension(stream, extensionOffset, extensionLength);

  ReadAtOffset(publishingLicense, stream, plOffset, plLength);

  // remove UTF-8 BOM
  if ((publishingLicense.size() > 3) &&
      (memcmp(publishingLicense.data(), "\xEF\xBB\xBF", 3) == 0)) {
    publishingLicense.erase(publishingLicense.begin(),
                            publishingLicense.begin() + 3);
  }

  if ((majorVersion == 2) && (minorVersion == 1))
  {
    ReadAtOffset(metadata, stream, metadataOffset, metadataLength);
  }
  return make_shared<PfileHeader>(move(publishingLicense), extension,
                                  contentOffset, originalFileSize,
                                  move(metadata), majorVersion, minorVersion,
                                  cleartextRedirectionHeader);
}