void handle_recv_header( gce::errcode_t const& ec, size_t bytes_transferred, gce::message& m, gce::adaptor a ) { if (ec) { a(ec, bytes_transferred); } else { hdr_ = msg_header(); boost::amsg::zero_copy_buffer zbuf(buf_, MaxMsgSize); boost::amsg::read(zbuf, hdr_); if (zbuf.bad()) { gce::errcode_t ec = boost::asio::error::make_error_code( boost::asio::error::invalid_argument ); a(ec, bytes_transferred); return; } std::size_t size = hdr_.size_; std::size_t max_size = MaxMsgSize; if (size > max_size) { gce::errcode_t ec = boost::asio::error::make_error_code( boost::asio::error::invalid_argument ); a(ec, bytes_transferred); return; } if (size > 0) { boost::asio::async_read( sock_, boost::asio::buffer(buf_, size), boost::bind( &self_type::handle_recv_body, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred, boost::ref(m), a ) ); } else { m = gce::message(hdr_.type_, buf_, hdr_.size_, hdr_.tag_offset_); a(ec, bytes_transferred); } } }
void pre_send(gce::message const& msg, boost::array<boost::asio::const_buffer, 2>& bufs) { msg_header hdr; hdr.size_ = msg.size(); hdr.type_ = msg.get_type(); hdr.tag_offset_ = msg.get_tag_offset(); gce::byte_t buf[sizeof(msg_header)]; boost::amsg::zero_copy_buffer zbuf(buf, sizeof(msg_header)); boost::amsg::write(zbuf, hdr); bufs[0] = boost::asio::buffer(buf, zbuf.write_length()); bufs[1] = boost::asio::buffer(msg.data(), msg.size()); }
// Given an absolute path, get the parent directory of the path. inline int get_parent_dir(char* sFilePath, char* sParentDir, size_t szParentDir) { char* sTemp; size_t szTemp = (strlen(sFilePath) + 1) * sizeof(char); if(bad_stralloc(sTemp, szTemp)) return 0; zbuf(sTemp, szTemp); strcpy(sTemp, sFilePath); *(strrchr(sTemp, '\\')) = '\0'; szTemp = strlen(sTemp); if(!szTemp || szParentDir < szTemp) { free(sTemp); return 0; } strcpy(sParentDir, sTemp); return 1; }
gce::message recv(gce::yield_t yield) { BOOST_STATIC_ASSERT((MaxMsgSize > MSG_HEADER_SIZE)); gce::byte_t buf[MaxMsgSize]; std::size_t header_size = MSG_HEADER_SIZE; boost::asio::async_read( sock_, boost::asio::buffer(buf, header_size), yield ); if (yield.ec_ && *yield.ec_) { return gce::message(); } msg_header hdr; boost::amsg::zero_copy_buffer zbuf(buf, MaxMsgSize); boost::amsg::read(zbuf, hdr); if (zbuf.bad()) { throw std::runtime_error("message header parse error"); } std::size_t size = hdr.size_; std::size_t max_size = MaxMsgSize; if (size > max_size) { std::runtime_error("message out of length"); } if (size > 0) { boost::asio::async_read( sock_, boost::asio::buffer(buf, size), yield ); } return gce::message(hdr.type_, buf, hdr.size_, hdr.tag_offset_); }
void MOUL::NetMsgStream::write(DS::Stream* stream) const { stream->write<uint32_t>(m_stream.size()); stream->write<uint8_t>(m_compression); if (m_compression == e_CompressZlib) { if (m_stream.size() < 2) throw DS::MalformedData(); uLongf zlength = compressBound(m_stream.size() - 2); std::unique_ptr<uint8_t[]> zbuf(new uint8_t[zlength + 2]); memcpy(zbuf.get(), m_stream.buffer(), 2); int result = compress(zbuf.get() + 2, &zlength, m_stream.buffer() + 2, m_stream.size() - 2); if (result != Z_OK) throw DS::MalformedData(); stream->write<uint32_t>(zlength + 2); stream->writeBytes(zbuf.get(), zlength + 2); } else { stream->write<uint32_t>(m_stream.size()); stream->writeBytes(m_stream.buffer(), m_stream.size()); } }
void MOUL::NetMsgStream::read(DS::Stream* stream) { uint32_t uncompressedSize = stream->read<uint32_t>(); m_compression = static_cast<Compression>(stream->read<uint8_t>()); uint32_t size = stream->read<uint32_t>(); std::unique_ptr<uint8_t[]> buffer(new uint8_t[size]); stream->readBytes(buffer.get(), size); if (m_compression == e_CompressZlib) { if (size < 2) throw DS::MalformedData(); std::unique_ptr<uint8_t[]> zbuf(new uint8_t[uncompressedSize]); uLongf zlength = uncompressedSize - 2; memcpy(zbuf.get(), buffer.get(), 2); int result = uncompress(zbuf.get() + 2, &zlength, buffer.get() + 2, size - 2); if (result != Z_OK) throw DS::MalformedData(); m_stream.steal(zbuf.release(), uncompressedSize); } else { m_stream.steal(buffer.release(), size); } }
/* Execute our inlined script to collect some info on our targeted python installation. */ static int get_python_info(char* python) { FILE *pPipe; int retcode = 0; size_t szExecBuf, szExecLen; char psBuffer[OUTPUT_BUF_LEN] = "", *cmdExec, *psBuffPart; // Check that python string isn't null. if(is_null(python)) { debug("Python string is null."); return 0; } // Calculate our cmd buffer length. if(getinfo_script_length == 0) getinfo_script_length = strlen(getinfo_script); szExecLen = PYTHON_EXEC_LEN + getinfo_script_length + strlen(python); szExecBuf = sizeof(char) * szExecLen; // Attempt to allocate our cmd buffer. if(bad_stralloc(cmdExec, szExecBuf)) { debug("Allocation failed."); return 0; } // Zero out our cmd buffer, then assign the cmd to execute. zbuf(cmdExec, szExecBuf); sprintf(cmdExec, python_exec, python, getinfo_script); // Attempt to open our process. if(is_null(pPipe = _popen(cmdExec, "rt"))) { free(cmdExec); debug("Could not open pipe."); return 0; } // Read process pipe until end of file, or an error occurs. while(fgets(psBuffer, OUTPUT_BUF_LEN, pPipe)); // Free our cmd buffer and make sure our process didn't error out. free(cmdExec); if (!feof( pPipe)) { debug("Pipe was not at EOF."); return 0; } // Close pipe & check the return value of our process. retcode = _pclose(pPipe); if(retcode != 0 || !strlen(psBuffer)) { debug("Python returned a non-zero return code."); return 0; } // Python major & minor version. if(is_null(psBuffPart = var_split(psBuffer)) || (strlen(psBuffPart) != 2)) { debug("Failed to parse python version."); return 0; } strcpy(python_version, (const char*)psBuffPart); // Python API version if(is_null(next_var(psBuffPart)) || (strlen(psBuffPart) != 4)) { debug("Failed to parse python API version."); return 0; } strcpy(python_api_string, (const char*)psBuffPart); if(!(python_api_version = atoi(python_api_string))) return 0; // Python prefix if(is_null(next_var(psBuffPart)) || (strlen(psBuffPart) == 0)) { debug("Failed to parse python prefix."); return 0; } strcpy(python_prefix, (const char*)psBuffPart); // Python dll if(is_null(next_var(psBuffPart)) || (strlen(psBuffPart) == 0)) { debug("Failed to parse python DLL."); return 0; } strcpy(python_library, (const char*)psBuffPart); return 1; }