/* sava_data bin (C->S) */ EdpPacket* PacketSavedataBin(const char* dst_devid, cJSON* desc_obj, uint8* bin_data, uint32 bin_len) { EdpPacket* pkg; uint32 remainlen; /* check arguments */ char* desc_out = cJSON_Print(desc_obj); uint32 desc_len = mystrlen(desc_out); if (desc_len > (0x01 << 16) || bin_len > (3 * (0x01 << 20)) /* desc < 2^16 && bin_len < 3M*/ || cJSON_GetObjectItem(desc_obj, "ds_id") == 0) /* desc_obj MUST has ds_id */ { hx_free(desc_out); return 0; } pkg = NewBuffer(); /* msg type */ WriteByte(pkg, SAVEDATA); if (dst_devid) { /* remain len */ remainlen = 1+(2+mystrlen(dst_devid))+1+(2+desc_len)+(4+bin_len); WriteRemainlen(pkg, remainlen); /* translate address flag */ WriteByte(pkg, 0x80); /* dst devid */ WriteStr(pkg, dst_devid); } else { /* remain len */ remainlen = 1+1+(2+desc_len)+(4+bin_len); WriteRemainlen(pkg, remainlen); /* translate address flag */ WriteByte(pkg, 0x00); } /* bin flag */ WriteByte(pkg, 0x02); /* desc */ WriteStr(pkg, desc_out); hx_free(desc_out); /* bin data */ WriteUint32(pkg, bin_len); WriteBytes(pkg, bin_data, bin_len); return pkg; }
SocketWriter& WriteString(const char* value) { uint32_t valuelen = strlen(value); WriteUint32(valuelen); if (valuelen == 0) { return *this; } CHECK(iov_index_ < kIovSize); iov_[iov_index_].iov_base = const_cast<char*>(value); iov_[iov_index_].iov_len = valuelen; ++iov_index_; return *this; }
PRStatus nsSOCKSSocketInfo::WriteV4ConnectRequest() { PRNetAddr *addr = &mDestinationAddr; int32_t proxy_resolve; NS_ABORT_IF_FALSE(mState == SOCKS_CONNECTING_TO_PROXY, "Invalid state!"); proxy_resolve = mFlags & nsISocketProvider::PROXY_RESOLVES_HOST; mDataLength = 0; mState = SOCKS4_WRITE_CONNECT_REQUEST; LOGDEBUG(("socks4: sending connection request (socks4a resolve? %s)", proxy_resolve? "yes" : "no")); // Send a SOCKS 4 connect request. WriteUint8(0x04); // version -- 4 WriteUint8(0x01); // command -- connect WriteNetPort(addr); if (proxy_resolve) { // Add the full name, null-terminated, to the request // according to SOCKS 4a. A fake IP address, with the first // four bytes set to 0 and the last byte set to something other // than 0, is used to notify the proxy that this is a SOCKS 4a // request. This request type works for Tor and perhaps others. WriteUint32(PR_htonl(0x00000001)); // Fake IP WriteUint8(0x00); // Send an emtpy username if (mDestinationHost.Length() > MAX_HOSTNAME_LEN) { LOGERROR(("socks4: destination host name is too long!")); HandshakeFinished(PR_BAD_ADDRESS_ERROR); return PR_FAILURE; } WriteString(mDestinationHost); // Hostname WriteUint8(0x00); } else if (PR_NetAddrFamily(addr) == PR_AF_INET) { WriteNetAddr(addr); // Add the IPv4 address WriteUint8(0x00); // Send an emtpy username } else if (PR_NetAddrFamily(addr) == PR_AF_INET6) { LOGERROR(("socks: SOCKS 4 can't handle IPv6 addresses!")); HandshakeFinished(PR_BAD_ADDRESS_ERROR); return PR_FAILURE; } return PR_SUCCESS; }
EdpPacket* PacketSavedataBinStr(const int8_t* dst_devid, const int8_t* desc_str, const uint8_t* bin_data, uint32_t bin_len) { EdpPacket* pkg = NULL; uint32_t remainlen = 0; uint32_t desc_len = 0; /* check arguments */ desc_len = strlen((const char *)desc_str); if (desc_len > (0x01 << 16) || bin_len > (3 * (0x01 << 20))) { /* desc < 2^16 && bin_len < 3M*/ return 0; } pkg = NewBuffer(); /* msg type */ WriteByte(pkg, SAVEDATA); if (dst_devid) { /* remain len */ remainlen = 1 + (2 + strlen((const char *)dst_devid)) + 1 + (2 + desc_len) + (4 + bin_len); WriteRemainlen(pkg, remainlen); /* translate address flag */ WriteByte(pkg, 0x80); /* dst devid */ WriteStr(pkg, dst_devid); } else { /* remain len */ remainlen = 1 + 1 + (2 + desc_len) + (4 + bin_len); WriteRemainlen(pkg, remainlen); /* translate address flag */ WriteByte(pkg, 0x00); } /* bin flag */ WriteByte(pkg, 0x02); /* desc */ WriteStr(pkg, desc_str); /* bin data */ WriteUint32(pkg, bin_len); WriteBytes(pkg, bin_data, bin_len); return pkg; }
EdpPacket* PacketCmdResp(const int8_t* cmdid, uint16_t cmdid_len, const int8_t* resp, uint32_t resp_len) { EdpPacket* send_pkg = NULL; unsigned remainlen = 0; send_pkg = NewBuffer(); /* 6 = 2 + 4 = len(cmdid_len) + len(resp_len) */ remainlen = cmdid_len + resp_len + (resp_len ? 6 : 2); WriteByte(send_pkg, CMDRESP); WriteRemainlen(send_pkg, remainlen); WriteUint16(send_pkg, cmdid_len); WriteBytes(send_pkg, cmdid, cmdid_len); if (resp_len) { WriteUint32(send_pkg, resp_len); WriteBytes(send_pkg, resp, resp_len); } return send_pkg; }
uint8 API_onRcvDataPassTrough( ApiMessageFrame * p_pMsgFrame ) { uint8 * pRcvData = (uint8 *)(p_pMsgFrame + 1); // skip the header if( p_pMsgFrame->MessageHeader.m_bIsRsp ) { if (p_pMsgFrame->MessageType == READ_DATA_RESP ) { while(p_pMsgFrame->MessageSize >= 5 ) { #if (BOARD_TYPE_DEVELOPMENT == BOARD_TYPE) if(pRcvData[0] >= DIGITAL_DATA_ATTR_ID_OFFSET) { UAPDATA_SetDigitalVal( UAP_DATA_DIGITAL1 + pRcvData[0] - DIGITAL_DATA_ATTR_ID_OFFSET, pRcvData[4] ); } else #endif { float fTmp; WriteUint32( &fTmp, pRcvData + 1, 4 ); #if (BOARD_TYPE_DEVELOPMENT == BOARD_TYPE) UAPDATA_SetAnalogVal(UAP_DATA_TEMP, UAP_DATA_TEMP + pRcvData[0] - ANALOG_DATA_ATTR_ID_OFFSET, fTmp ); #elif (BOARD_TYPE_HART_DEV_KIT == BOARD_TYPE) UAPDATA_SetAnalogVal(UAP_DATA_LOCAL_ANALOG_INPUT, UAP_DATA_LOCAL_ANALOG_INPUT + pRcvData[0] - ANALOG_DATA_ATTR_ID_OFFSET - LOCAL_ANALOG_CH_NO, fTmp ); #endif } p_pMsgFrame->MessageSize -= 5; pRcvData += 5; } return 1; } } return 0; }
void UTPex::encode(BEncoder & enc,const std::map<Uint32,net::Address> & ps) { if (ps.size() == 0) { enc.write(""); return; } Uint8* buf = new Uint8[ps.size() * 6]; Uint32 size = 0; std::map<Uint32,net::Address>::const_iterator i = ps.begin(); while (i != ps.end()) { const net::Address & addr = i->second; WriteUint32(buf,size,addr.ip()); WriteUint16(buf,size + 4,addr.port()); size += 6; i++; } enc.write(buf,size); delete [] buf; }
/** * This method encodes and writes an `int32_t` value to current input frame. * * Before using this method `BeginFrame()` must be called to start and prepare a new input frame. Otherwise, this * method does nothing and returns error status `OT_ERROR_INVALID_STATE`. * * If no buffer space is available, this method will discard and clear the current input frame and return the * error status `OT_ERROR_NO_BUFS`. * * @param[in] aInt32 The value to add to input frame. * * @retval OT_ERROR_NONE Successfully added given value to the frame. * @retval OT_ERROR_NO_BUFS Insufficient buffer space available to add the value. * @retval OT_ERROR_INVALID_STATE `BeginFrame()` has not been called earlier to start the frame. * */ otError WriteInt32(int32_t aInt32) { return WriteUint32(static_cast<uint32_t>(aInt32)); }
void cBinaryFile::WriteString(const std::string &var) { if (!m_WriteMode) return; WriteUint32(var.size()); m_File.write(var.c_str(), var.size()); m_CurLoc += var.size(); }