Beispiel #1
0
	Entity::Entity(const Sprite &sprite, Stream &sr) :EntityWorldProxy(sr), m_sprite(sprite) {
		resetAnimState();

		u8 flags;
		sr.unpack(flags, m_pos, m_dir_angle);

		m_is_seq_finished = flags & flag_is_finished;
		m_is_seq_looped = flags & flag_is_looped;

		if(flags & flag_compressed) {
			u8 frame_idx, seq_idx, dir_idx;
			sr.unpack(seq_idx, frame_idx, dir_idx);
			m_frame_idx = frame_idx;
			m_seq_idx = seq_idx;
			m_dir_idx = dir_idx;
		}
		else
			sr.unpack(m_seq_idx, m_frame_idx, m_dir_idx);

		if(flags & flag_has_overlay) {
			m_oseq_idx = decodeInt(sr);
			m_oframe_idx = decodeInt(sr);
		}
		else {
			m_oseq_idx = -1;
			m_oframe_idx = -1;
		}
	}
Beispiel #2
0
	const int3 decodeInt3(Stream &sr) {
		int3 out;
		out.x = decodeInt(sr);
		out.y = decodeInt(sr);
		out.z = decodeInt(sr);
		return out;
	}
Beispiel #3
0
bool LogEncoderDecoder::decodeFileInvalidationEntry(char *buf, int16_t size,
                                                    int64_t &timestamp,
                                                    int64_t &seqId) {
  folly::ByteRange br((uint8_t *)buf, size);
  try {
    timestamp = decodeInt(br);
    seqId = decodeInt(br);
  } catch (const std::exception &ex) {
    LOG(ERROR) << "got exception " << folly::exceptionStr(ex);
    return false;
  }
  return !checkForOverflow(br.start() - (uint8_t *)buf, size);
}
Beispiel #4
0
	void Inventory::load(Stream &sr) {
		int count = decodeInt(sr);
		ASSERT(count >= 0 && count <= max_entries);

		m_entries.clear();
		for(int n = 0; n < count; n++) {
			int icount = decodeInt(sr);
			ASSERT(icount > 0);

			Item item(sr);
			m_entries.emplace_back(Entry{item, icount});
		}
	}
Beispiel #5
0
Actor::Actor(Stream &sr)
    :EntityImpl(sr), m_actor(*m_proto.actor) {
    m_inventory.setDummyWeapon(Weapon(*m_actor.punch_weapon));

    u8 flags;
    sr.unpack(flags, m_stance, m_action);
    m_hit_points = decodeInt(sr);
    m_sound_variation = decodeInt(sr);
    m_client_id = decodeInt(sr);
    m_faction_id = decodeInt(sr);

    if(flags & 1)
        sr >> m_target_angle;
    else
Beispiel #6
0
void LZ78D::readToken() {
	int uncompressedSeqSize = 0;
	while(!input.getNextBit()) 
		++uncompressedSeqSize;
	
	if(uncompressedSeqSize != 0) {
		for(int i = 0; i < uncompressedSeqSize; ++i)
			buffer.push_back(input.getBunchOfBits(8));
		dictionary.push_back(make_pair(nextAvailableBytePos, buffer.size()-1));	
	} else {
		int entry = decodeInt(), mismatchingByte = input.getBunchOfBits(8); 

		for(int i = dictionary[entry].first; i <= dictionary[entry].second; ++i) 
			buffer.push_back(buffer[i]);
		buffer.push_back(mismatchingByte);	

		dictionary.push_back(make_pair(nextAvailableBytePos, buffer.size() - 1));
	}
	
	if(dictionary.size() >= dictionaryMaxSize) {
		dictionary.resize(1);
		for(int i = 0; i + nextAvailableBytePos < buffer.size(); ++i) {
			buffer[i] = buffer[nextAvailableBytePos+i];
		}
		buffer.resize(buffer.size()-nextAvailableBytePos);
		nextAvailableBytePos = 0;
	}
}
Beispiel #7
0
/**
 * Decode the simple type "charsType"
 *
 * @param out         The out parameter, returning a string contaning the decoded value
 * @param bufferPos   The buffer containing the BIXIE encoded data
 *
 * @return            Returns and integer error flag if an error occurred
 */
int decodeCharsType(char** out, byte** bufferPos){
  int nOfVarintBytes = 1, i, nOfCharTypes = 0;
  char* intStr;
  char* tempStr;
  char CharType[5];
  decodeInt(&intStr, bufferPos);
  nOfCharTypes = atoi(intStr);
  free(intStr);
  while(((*bufferPos)[nOfVarintBytes-1] & 0x80) == 0x80) {
	  nOfVarintBytes++;
  }
  if (nOfCharTypes == 0) {
    asprintf(out, "");
  }
  tempStr = (char*) malloc ((nOfCharTypes*4)+1);
  tempStr[0] = '\0';
  for(i = 0; i < nOfCharTypes; i++) {
    sprintf(CharType, "\\x%.2x", (byte) ((*bufferPos)[i]));
    strcat(tempStr, CharType);
  }
  *out = (char*) malloc ((strlen(tempStr)+1)*sizeof(char)); //Remembering +1 for the null terminator
  strcpy(*out, tempStr);
  free(tempStr);
  *bufferPos += nOfCharTypes;
  
  return NORMAL_RUN;
}
Beispiel #8
0
    static uint32_t readString(const std::vector<uint8_t>& buffer, uint32_t offset, std::string& result)
    {
        uint32_t originalOffset = offset;

        uint16_t length;

        uint32_t ret = decodeInt(buffer, offset, 2, length);

        if (ret == 0)
        {
            return 0;
        }

        offset += ret;

        if (buffer.size() - offset < length)
        {
            return 0;
        }

        result.assign(reinterpret_cast<const char*>(buffer.data() + offset), length);
        offset += length;

        return offset - originalOffset;
    }
Beispiel #9
0
    static uint32_t readStrictArray(const std::vector<uint8_t>& buffer, uint32_t offset, std::vector<Node>& result)
    {
        uint32_t originalOffset = offset;

        uint32_t count;

        uint32_t ret = decodeInt(buffer, offset, 4, count);

        if (ret == 0)
        {
            return 0;
        }

        offset += ret;

        for (uint32_t i = 0; i < count; ++i)
        {
            Node node;

            if (!node.decode(buffer, offset))
            {
                return 0;
            }

            result.push_back(node);
        }

        return offset - originalOffset;
    }
Beispiel #10
0
    void decodeByteBuffer(
        RCF::ByteBuffer & value, 
        const RCF::ByteBuffer & byteBuffer, 
        std::size_t & pos)
    {
        int len_ = 0;
        decodeInt(len_, byteBuffer, pos);
        std::size_t len = static_cast<unsigned int>(len_);

        RCF_VERIFY(
            pos+len <= byteBuffer.getLength(),
            RCF::Exception(RCF::_RcfError_Decoding()));

        if (len == 0)
        {
            value = RCF::ByteBuffer(value, 0, 0);
        }
        else
        {
            if (value.getLength() < len)
            {
                value = RCF::ByteBuffer(len);
            }
            value = RCF::ByteBuffer(value, 0, len);
            memcpy(value.getPtr(), byteBuffer.getPtr()+pos, len);
        }
        
        pos += len;
    }
Beispiel #11
0
static err_t testStruct_decode(buf_t *in, void *out) {
  struct testStruct *x = (struct testStruct *)out;

  vomControl ctl = controlNone;
  uint64_t index;

  while (true) {
    err_t err = decodeVar128(in, &index, &ctl); ck();
    if (ctl == controlEnd) {
      return ERR_OK;
    }
    if (ctl != controlNone) {
      // unexpected control message here
      err = ERR_DECVOM; ck();
    }

    switch (index) {
    case 0:
      err = decodeInt(in, &x->A); ck();
      break; 
    case 1:
      err = decodeDouble(in, &x->B); ck();
      break;
    case 2:
      err = inner_decode(in, &x->Inner); ck();
      break;
    default:
      // unexpected index number.
      err = ERR_DECVOM; ck();
    }
  }

  return ERR_OK;
};
Beispiel #12
0
static SECStatus
readItem(PRFileDesc *fd, SECItem *item)
{
    unsigned char buf[4];
    int bytesRead;


    bytesRead = PR_Read(fd, buf, 4);
    if (bytesRead != 4) {
	return SECFailure;
    }
    item->len = decodeInt(buf);

    item->data = PORT_Alloc(item->len);
    if (item->data == NULL) {
	item->len = 0;
	return SECFailure;
    }
    bytesRead = PR_Read(fd, item->data, item->len);
    if (bytesRead != item->len) {
	PORT_Free(item->data);
	item->data = NULL;
	item->len = 0;
	return SECFailure;
    }
    return SECSuccess;
}
Beispiel #13
0
 void decodeInt(
     boost::uint32_t &           value, 
     const RCF::ByteBuffer &     byteBuffer, 
     std::size_t &               pos)
 {
     int nValue = 0;
     decodeInt(nValue, byteBuffer, pos);
     value = static_cast<boost::uint32_t>(nValue);
 }
Beispiel #14
0
bool LogEncoderDecoder::decodeFileCreationEntry(char *buf, int16_t size,
                                                int64_t &timestamp,
                                                std::string &fileName,
                                                int64_t &seqId,
                                                int64_t &fileSize) {
  folly::ByteRange br((uint8_t *)buf, size);
  try {
    timestamp = decodeInt(br);
    if (!decodeString(br, buf, size, fileName)) {
      return false;
    }
    seqId = decodeInt(br);
    fileSize = decodeInt(br);
  } catch (const std::exception &ex) {
    LOG(ERROR) << "got exception " << folly::exceptionStr(ex);
    return false;
  }
  return !checkForOverflow(br.start() - (uint8_t *)buf, size);
}
Beispiel #15
0
	void ActorInventory::load(Stream &sr) {
		Inventory::load(sr);
		u8 flags;
		sr >> flags;

		m_weapon = flags & 1? Weapon(Item(sr)) : m_dummy_weapon;
		m_armour = flags & 2? Armour(Item(sr)) : Item::dummyArmour();
		m_ammo.item = flags & 4? Item(sr) : Item::dummyAmmo();
		m_ammo.count = flags & 4? decodeInt(sr) : 0;
	}
  uint32_t TSaslTransport::readLength() {
    uint8_t lenBuf[PAYLOAD_LENGTH_BYTES];

    transport_->readAll(lenBuf, PAYLOAD_LENGTH_BYTES);
    int32_t len = decodeInt(lenBuf, 0);
    if (len < 0) {
      throw TTransportException("Frame size has negative value");
    }
    return static_cast<uint32_t>(len);
  }
Beispiel #17
0
/* Zipfile seeking functions */
static int
seek_to_central_directory(FILE *f)
{
    long minstart, size, i;
    int state;
    char *buf;
    minstart = 65557;
    fseek(f, 0, SEEK_END);
    size = ftell(f);
    if (minstart > size) {
        minstart = size;
    }
    fseek(f, -minstart, SEEK_END);
    buf = (char *)malloc(minstart);
    if (!buf) {
        /* Disastrous memory exhaustion failure. Fragmentation? */
        return 0;
    }
    if ((long)fread(&buf[0], 1, minstart, f) != minstart) {
        /* Couldn't read the file suffix. Probably a premature EOF. */
        /* This really shouldn't happen */
        free(buf);
        return 0;
    }
    state = 0;
    for (i = minstart - 19; i >= 0; --i) {
        switch(buf[i]) {
        case 0x06:
            state = 1;
            break;
        case 0x05:
            state = (state == 1) ? 2 : 0;
            break;
        case 0x4b:
            state = (state == 2) ? 3 : 0;
            break;
        case 0x50:
            if (state == 3) {
                fseek(f, decodeInt((unsigned char *)buf+i+16), SEEK_SET);
                free(buf);
                /* Found it! */
                return 1;
            }
            state = 0;
            break;
        default:
            state = 0;
            break;
        }
    }
    free(buf);
    /* Not a ZIP archive */
    return 0;
}
Beispiel #18
0
bool LogEncoderDecoder::decodeLogHeader(char *buf, int16_t size,
                                        int64_t &timestamp, int &version,
                                        std::string &recoveryId,
                                        std::string &senderIp,
                                        int64_t &config) {
  folly::ByteRange br((uint8_t *)buf, size);
  try {
    timestamp = decodeInt(br);
    version = decodeInt(br);
    if (!decodeString(br, buf, size, recoveryId)) {
      return false;
    }
    if (!decodeString(br, buf, size, senderIp)) {
      return false;
    }
    config = decodeInt(br);
  } catch (const std::exception &ex) {
    LOG(ERROR) << "got exception " << folly::exceptionStr(ex);
    return false;
  }
  return !checkForOverflow(br.start() - (uint8_t *)buf, size);
}
Beispiel #19
0
const char* KeyPrefix::decode(const char* start, const char* limit, KeyPrefix* result)
{
    if (start == limit)
        return 0;

    unsigned char firstByte = *start++;

    int databaseIdBytes = ((firstByte >> 5) & 0x7) + 1;
    int objectStoreIdBytes = ((firstByte >> 2) & 0x7) + 1;
    int indexIdBytes = (firstByte & 0x3) + 1;

    if (start + databaseIdBytes + objectStoreIdBytes + indexIdBytes > limit)
        return 0;

    result->m_databaseId = decodeInt(start, start + databaseIdBytes);
    start += databaseIdBytes;
    result->m_objectStoreId = decodeInt(start, start + objectStoreIdBytes);
    start += objectStoreIdBytes;
    result->m_indexId = decodeInt(start, start + indexIdBytes);
    start += indexIdBytes;

    return start;
}
Beispiel #20
0
    void decodeString(
        std::string &value,
        const RCF::ByteBuffer &byteBuffer,
        std::size_t &pos)
    {
        int len_ = 0;
        decodeInt(len_, byteBuffer, pos);
        std::size_t len = static_cast<unsigned int>(len_);

        RCF_VERIFY(
            pos+len <= byteBuffer.getLength(),
            RCF::Exception(RCF::_RcfError_Decoding()));

        value.assign(byteBuffer.getPtr()+pos, len);
        pos += len;
    }
Beispiel #21
0
    static uint32_t readECMAArray(const std::vector<uint8_t>& buffer, uint32_t offset, std::map<std::string, Node>& result)
    {
        uint32_t originalOffset = offset;

        uint32_t count;

        uint32_t ret = decodeInt(buffer, offset, 4, count);

        if (ret == 0)
        {
            return 0;
        }

        offset += ret;

        for (uint32_t i = 0; i < count; ++i)
        {
            std::string key;

            ret = readString(buffer, offset, key);

            if (ret == 0)
            {
                return 0;
            }

            offset += ret;

            Node node;
            ret = node.decode(buffer, offset);

            if (ret == 0)
            {
                return 0;
            }

            offset += ret;

            result[key] = node;
        }

        return offset - originalOffset;
    }
Beispiel #22
0
/**
 * Decode the simple type "String"
 *
 * @param out         The out parameter, returning a string contaning the decoded value
 * @param bufferPos   The buffer containing the BIXIE encoded data
 *
 * @return            Returns and integer error flag if an error occurred
 */
int decodeString(char** out, byte** bufferPos) {
	int sizeOfLeadingVarint = 1, inputSize;
	char* inputSizeString;
	char tempStr[4096];
	
	while(((*bufferPos)[sizeOfLeadingVarint-1] & 0x80) == 0x80) {
		sizeOfLeadingVarint++;
	}
	decodeInt(&inputSizeString, bufferPos);
	inputSize = atoi(inputSizeString);
  free(inputSizeString);
	int outBytes = smaz_decompress(*bufferPos, inputSize, tempStr, sizeof(tempStr));
	*out = (char*) malloc(outBytes *sizeof(char) + 1);
  memcpy(*out, tempStr, outBytes);
  (*out)[outBytes] = '\0';
	*bufferPos += inputSize;
	
	return NORMAL_RUN;
}
Beispiel #23
0
    static uint32_t readDate(const std::vector<uint8_t>& buffer, uint32_t offset, Date& result)
    {
        uint32_t originalOffset = offset;

        uint32_t ret = decodeDouble(buffer, offset, result.ms);

        if (ret == 0) // date in milliseconds from 01/01/1970
        {
            return 0;
        }

        offset += ret;

        ret = decodeInt(buffer, offset, 4, result.timezone);

        if (ret == 0) // unsupported timezone
        {
            return 0;
        }

        offset += ret;

        return offset - originalOffset;
    }
  /**
   * Read a complete Thrift SASL message.
   *
   * @return The SASL status and payload from this message.
   * @throws TTransportException
   *           Thrown if there is a failure reading from the underlying
   *           transport, or if a status code of BAD or ERROR is encountered.
   */
  uint8_t* TSaslTransport::receiveSaslMessage(NegotiationStatus* status,
                                              uint32_t* length) {
    uint8_t messageHeader[HEADER_LENGTH];

    // read header
    transport_->readAll(messageHeader, HEADER_LENGTH);

    // get payload status
    *status = (NegotiationStatus)messageHeader[0];
    if ((*status < TSASL_START) || (*status > TSASL_COMPLETE)) {
      throw TTransportException("invalid sasl status");
    } else if (*status == TSASL_BAD || *status == TSASL_ERROR) {
        throw TTransportException("sasl Peer indicated failure: ");
    }

    // get the length
    *length = decodeInt(messageHeader, STATUS_BYTES);

    // get payload
    protoBuf_.reset(new uint8_t[*length]);
    transport_->readAll(protoBuf_.get(), *length);

    return protoBuf_.get();
  }
Beispiel #25
0
    bool IStream::begin(Node &node)
    {
        while (true)
        {
            Byte8 byte = 0;
            read_byte(byte);

            switch (byte)
            {
            case Blank: 
                {
                    Byte8 count = 0;
                    read_byte(count);
                    std::vector<Byte8> buffer(count);
                    UInt32 bytesRead = read( &(buffer[0]), count);
                    if (bytesRead != static_cast<UInt32>(count))
                    {
                        RCF::Exception e(RCF::_SfError_DataFormat());
                        RCF_THROW(e)(bytesRead)(count);
                    }
                    continue;
                }

            case BeginArchiveMetadata:
                {
                    int runtimeVersion = 0;
                    int archiveVersion = 0;
                    bool pointerTrackingEnabled = false;
                    bool * pPointerTrackingEnabled = NULL;

                    const size_t BufferLen = 11;
                    char buffer[BufferLen] = {0};
                    RCF::ByteBuffer byteBuffer( &buffer[0], BufferLen);
                    std::size_t pos0 = static_cast<std::size_t>(mpIs->tellg());

#ifdef _MSC_VER
#pragma warning( push )
#pragma warning( disable : 4996 )  // warning C4996: 'std::basic_istream<_Elem,_Traits>::readsome': Function call with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked Iterators'
#endif
                    std::size_t bytesRead = static_cast<std::size_t>(mpIs->readsome(buffer, BufferLen));

#ifdef _MSC_VER
#pragma warning( pop )
#endif
                    byteBuffer = RCF::ByteBuffer(byteBuffer, 0, bytesRead);
                    std::size_t pos1 = 0;
                    decodeInt(runtimeVersion, byteBuffer, pos1);
                    decodeInt(archiveVersion, byteBuffer, pos1);

                    if (runtimeVersion >= 10)
                    {
                        decodeBool(pointerTrackingEnabled, byteBuffer, pos1);
                        pPointerTrackingEnabled = &pointerTrackingEnabled;
                    }

                    mpIs->seekg(
                        static_cast<std::istream::off_type>(pos0 + pos1), 
                        std::ios_base::beg);

                    if (!mIgnoreVersionStamp)
                    {
                        if (runtimeVersion)
                        {
                            mRuntimeVersion = runtimeVersion;
                        }
                        if (archiveVersion)
                        {
                            mArchiveVersion = archiveVersion;
                        }
                    }

                    if (pPointerTrackingEnabled && !*pPointerTrackingEnabled)
                    {
                        getTrackingContext().setEnabled(false);
                    }

                    continue;
                }

            case Begin:
                {
                    read_byte( byte );
                    Byte8 attrSpec = byte;

                    // id
                    if (attrSpec & 1)
                    {
                        read_int(node.id);
                    }

                    // ref
                    attrSpec = attrSpec >> 1;
                    if (attrSpec & 1)
                    {
                        node.ref = 1;
                    }

                    // type
                    attrSpec = attrSpec >> 1;
                    if (attrSpec & 1)
                    {
                        UInt32 length = 0;
                        read_int(length);
                        node.type.allocate(length);
                        read(node.type.get(), length );
                    }

                    // label
                    attrSpec = attrSpec >> 1;
                    if (attrSpec & 1)
                    {
                        UInt32 length = 0;
                        read_int(length);
                        node.label.allocate(length);
                        read(node.label.get(), length);
                    }

                    return true;
                }

            default:
                {
                    RCF::Exception e(RCF::_SfError_DataFormat());
                    RCF_THROW(e)(byte);
                }
            }
        }

    }
Beispiel #26
0
int32_t BamBam_BamSingleAlignment_GetTLen(BamBam_BamSingleAlignment const * data)
{
	return decodeInt(data->data + 28, 4);
}
Beispiel #27
0
	void ServerStatusChunk::load(Stream &sr) {
		sr.unpack(address.ip, address.port, game_mode, is_passworded);
		sr >> server_name >> map_name;
		num_players = decodeInt(sr);
		max_players = decodeInt(sr);
	}
Beispiel #28
0
    bool Sender::handlePacket(const rtmp::Packet& packet)
    {
        switch (packet.messageType)
        {
            case rtmp::MessageType::SET_CHUNK_SIZE:
            {
                uint32_t offset = 0;

                uint32_t ret = decodeInt(packet.data, offset, 4, inChunkSize);

                if (ret == 0)
                {
                    return false;
                }

#ifdef DEBUG
                std::cout << "Chunk size: " << inChunkSize << std::endl;
#endif
                sendSetChunkSize();

                break;
            }

            case rtmp::MessageType::PING:
            {
                uint32_t offset = 0;

                uint16_t pingType;
                uint32_t ret = decodeInt(packet.data, offset, 2, pingType);

                if (ret == 0)
                {
                    return false;
                }

                offset += ret;

                uint32_t param;
                ret = decodeInt(packet.data, offset, 4, param);

                if (ret == 0)
                {
                    return false;
                }

                offset += ret;

#ifdef DEBUG
                std::cout << "Ping type: " << pingType << ", param: " << param << std::endl;
#endif
                break;
            }

            case rtmp::MessageType::SERVER_BANDWIDTH:
            {
                uint32_t offset = 0;

                uint32_t bandwidth;
                uint32_t ret = decodeInt(packet.data, offset, 4, bandwidth);

                if (ret == 0)
                {
                    return false;
                }

                offset += ret;

#ifdef DEBUG
                std::cout << "Server bandwidth: " << bandwidth << std::endl;
#endif

                break;
            }

            case rtmp::MessageType::CLIENT_BANDWIDTH:
            {
                uint32_t offset = 0;

                uint32_t bandwidth;
                uint32_t ret = decodeInt(packet.data, offset, 4, bandwidth);

                if (ret == 0)
                {
                    return false;
                }

                offset += ret;

                uint8_t type;
                ret = decodeInt(packet.data, offset, 1, type);

                if (ret == 0)
                {
                    return false;
                }

                offset += ret;

#ifdef DEBUG
                std::cout << "Client bandwidth: " << bandwidth << ", type: " << static_cast<uint32_t>(type) << std::endl;
#endif
                
                break;
            }

            case rtmp::MessageType::NOTIFY:
            {
                break;
            }

            case rtmp::MessageType::AUDIO_PACKET:
            {
                // ignore this, sender should not receive audio data
                break;
            }

            case rtmp::MessageType::VIDEO_PACKET:
            {
                // ignore this, sender should not receive video data
                break;
            }

            case rtmp::MessageType::INVOKE:
            {
                uint32_t offset = 0;

                amf0::Node command;

                uint32_t ret = command.decode(packet.data, offset);

                if (ret == 0)
                {
                    return false;
                }

                offset += ret;

#ifdef DEBUG
                std::cout << "INVOKE command: ";
                command.dump();
#endif

                amf0::Node transactionId;

                ret = transactionId.decode(packet.data, offset);

                if (ret == 0)
                {
                    return false;
                }

                offset += ret;

#ifdef DEBUG
                std::cout << "Transaction ID: ";
                transactionId.dump();
#endif

                amf0::Node argument1;

                if ((ret = argument1.decode(packet.data, offset)) > 0)
                {
                    offset += ret;

#ifdef DEBUG
                    std::cout << "Argument 1: ";
                    argument1.dump();
#endif
                }

                amf0::Node argument2;

                if ((ret = argument2.decode(packet.data, offset)) > 0)
                {
                    offset += ret;

#ifdef DEBUG
                    std::cout << "Argument 2: ";
                    argument2.dump();
#endif
                }

                if (command.asString() == "onBWDone")
                {
                    sendCheckBW();
                }
                else if (command.asString() == "onFCPublish")
                {
                }
                else if (command.asString() == "_error")
                {
                    auto i = invokes.find(static_cast<uint32_t>(transactionId.asDouble()));

                    if (i != invokes.end())
                    {
                        std::cerr << i->second << " error" << std::endl;

                        invokes.erase(i);
                    }
                }
                else if (command.asString() == "_result")
                {
                    auto i = invokes.find(static_cast<uint32_t>(transactionId.asDouble()));

                    if (i != invokes.end())
                    {
                        std::cerr << i->second << " result" << std::endl;
                        
                        if (i->second == "connect")
                        {
                            connected = true;
                            if (!streamName.empty())
                            {
                                sendReleaseStream();
                                sendFCPublish();
                                sendCreateStream();
                            }
                        }
                        else if (i->second == "releaseStream")
                        {
                        }
                        else if (i->second == "createStream")
                        {
                            streamId = static_cast<uint32_t>(argument2.asDouble());
                            sendPublish();

                            streaming = true;
                        }

                        invokes.erase(i);
                    }
                }
                break;
            }
                
            default:
            {
                std::cerr << "Unhandled message" << std::endl;
                break;
            }
        }

        return true;
    }
Beispiel #29
0
int AbstractNumberModel<T>::decompressNumber(BinaryCode& code, unsigned int& num) const{
    int ret = decodeInt(code, num);
    if(ret < 0)
        return ret;
    return 0;
}
Beispiel #30
0
static PRBool
blapi_SHVerifyFile(const char *shName, PRBool self)
{
    char *checkName = NULL;
    PRFileDesc *checkFD = NULL;
    PRFileDesc *shFD = NULL;
    void  *hashcx = NULL;
    const SECHashObject *hashObj = NULL;
    SECItem signature = { 0, NULL, 0 };
    SECItem hash;
    int bytesRead, offset;
    SECStatus rv;
    DSAPublicKey key;
    int count;
#ifdef FREEBL_USE_PRELINK
    int pid = 0;
#endif

    PRBool result = PR_FALSE; /* if anything goes wrong,
			       * the signature does not verify */
    unsigned char buf[4096];
    unsigned char hashBuf[HASH_LENGTH_MAX];

    PORT_Memset(&key,0,sizeof(key));
    hash.data = hashBuf;
    hash.len = sizeof(hashBuf);

    /* If our integrity check was never ran or failed, fail any other 
     * integrity checks to prevent any token going into FIPS mode. */
    if (!self && (BL_FIPSEntryOK(PR_FALSE) != SECSuccess)) {
	return PR_FALSE;
    }

    if (!shName) {
	goto loser;
    }

    /* figure out the name of our check file */
    checkName = mkCheckFileName(shName);
    if (!checkName) {
	goto loser;
    }

    /* open the check File */
    checkFD = PR_Open(checkName, PR_RDONLY, 0);
    if (checkFD == NULL) {
#ifdef DEBUG_SHVERIFY
        fprintf(stderr, "Failed to open the check file %s: (%d, %d)\n",
                checkName, (int)PR_GetError(), (int)PR_GetOSError());
#endif /* DEBUG_SHVERIFY */
	goto loser;
    }

    /* read and Verify the headerthe header */
    bytesRead = PR_Read(checkFD, buf, 12);
    if (bytesRead != 12) {
	goto loser;
    }
    if ((buf[0] != NSS_SIGN_CHK_MAGIC1) || (buf[1] != NSS_SIGN_CHK_MAGIC2)) {
	goto loser;
    }
    if ((buf[2] != NSS_SIGN_CHK_MAJOR_VERSION) || 
				(buf[3] < NSS_SIGN_CHK_MINOR_VERSION)) {
	goto loser;
    }
#ifdef notdef
    if (decodeInt(&buf[8]) != CKK_DSA) {
	goto loser;
    }
#endif

    /* seek past any future header extensions */
    offset = decodeInt(&buf[4]);
    if (PR_Seek(checkFD, offset, PR_SEEK_SET) < 0) {
	goto loser;
    }

    /* read the key */
    rv = readItem(checkFD,&key.params.prime);
    if (rv != SECSuccess) {
	goto loser;
    }
    rv = readItem(checkFD,&key.params.subPrime);
    if (rv != SECSuccess) {
	goto loser;
    }
    rv = readItem(checkFD,&key.params.base);
    if (rv != SECSuccess) {
	goto loser;
    }
    rv = readItem(checkFD,&key.publicValue);
    if (rv != SECSuccess) {
	goto loser;
    }
    /* read the siganture */
    rv = readItem(checkFD,&signature);
    if (rv != SECSuccess) {
	goto loser;
    }

    /* done with the check file */
    PR_Close(checkFD);
    checkFD = NULL;

    hashObj = HASH_GetRawHashObject(PQG_GetHashType(&key.params));
    if (hashObj == NULL) {
	goto loser;
    }

    /* open our library file */
#ifdef FREEBL_USE_PRELINK
    shFD = bl_OpenUnPrelink(shName,&pid);
#else
    shFD = PR_Open(shName, PR_RDONLY, 0);
#endif
    if (shFD == NULL) {
#ifdef DEBUG_SHVERIFY
        fprintf(stderr, "Failed to open the library file %s: (%d, %d)\n",
                shName, (int)PR_GetError(), (int)PR_GetOSError());
#endif /* DEBUG_SHVERIFY */
	goto loser;
    }

    /* hash our library file with SHA1 */
    hashcx = hashObj->create();
    if (hashcx == NULL) {
	goto loser;
    }
    hashObj->begin(hashcx);

    count = 0;
    while ((bytesRead = PR_Read(shFD, buf, sizeof(buf))) > 0) {
	hashObj->update(hashcx, buf, bytesRead);
	count += bytesRead;
    }
#ifdef FREEBL_USE_PRELINK
    bl_CloseUnPrelink(shFD, pid);
#else
    PR_Close(shFD);
#endif
    shFD = NULL;

    hashObj->end(hashcx, hash.data, &hash.len, hash.len);


    /* verify the hash against the check file */
    if (DSA_VerifyDigest(&key, &signature, &hash) == SECSuccess) {
	result = PR_TRUE;
    }
#ifdef DEBUG_SHVERIFY
  {
        int i,j;
        fprintf(stderr,"File %s: %d bytes\n",shName, count);
        fprintf(stderr,"  hash: %d bytes\n", hash.len);
#define STEP 10
        for (i=0; i < hash.len; i += STEP) {
           fprintf(stderr,"   ");
           for (j=0; j < STEP && (i+j) < hash.len; j++) {
                fprintf(stderr," %02x", hash.data[i+j]);
           }
           fprintf(stderr,"\n");
        }
        fprintf(stderr,"  signature: %d bytes\n", signature.len);
        for (i=0; i < signature.len; i += STEP) {
           fprintf(stderr,"   ");
           for (j=0; j < STEP && (i+j) < signature.len; j++) {
                fprintf(stderr," %02x", signature.data[i+j]);
           }
           fprintf(stderr,"\n");
        }
	fprintf(stderr,"Verified : %s\n",result?"TRUE": "FALSE");
    }
#endif /* DEBUG_SHVERIFY */


loser:
    if (checkName != NULL) {
	PORT_Free(checkName);
    }
    if (checkFD != NULL) {
	PR_Close(checkFD);
    }
    if (shFD != NULL) {
	PR_Close(shFD);
    }
    if (hashcx != NULL) {
	if (hashObj) {
	    hashObj->destroy(hashcx,PR_TRUE);
	}
    }
    if (signature.data != NULL) {
	PORT_Free(signature.data);
    }
    if (key.params.prime.data != NULL) {
	PORT_Free(key.params.prime.data);
    }
    if (key.params.subPrime.data != NULL) {
	PORT_Free(key.params.subPrime.data);
    }
    if (key.params.base.data != NULL) {
	PORT_Free(key.params.base.data);
    }
    if (key.publicValue.data != NULL) {
	PORT_Free(key.publicValue.data);
    }

    return result;
}