static void append(std::vector<buffer_t>& buffers, buffer_t const& arg) { append_size(buffers, '$', arg.size()); if (arg.size() > RESP_LARGE_BUFFER_SIZE) { buffers.push_back(arg); buffers.push_back("\r\n"); } else { buffer_t& buffer = buffers.back(); buffer.append(arg); buffer.append("\r\n"); } }
void i2c::write( const buffer_t& data ) { setAddress(); if( ::write(mDevice, data.data(), data.size()) < 0 ) { ostringstream os; os << "unable to write: " << strerror( errno ); throw runtime_error{ move( os.str() ) }; } }
bool CLabelManagerLanguageMonitor::ReadStatus(buffer_t& Status) { time_t t = time(NULL); fprintf(stderr, "DEBUG: CLabelManagerLanguageMonitor::ReadStatus() %s\n", ctime(&t)); bool Result = false; Status.clear(); buffer_t RequestStatusCommand = CLabelManagerDriver::GetRequestStatusCommand(); Environment_.WriteData(RequestStatusCommand); Environment_.ReadData(Status); if (Status.size() > 0) { Result = true; } fprintf(stderr, "DEBUG: ReadStatus() returned %i %i\n", (int)Status.size(), (int)Result); return Result; }
buffer_t hmac::make( hash_t type, const buffer_t& key, const unsigned char* src, size_t length) { auto digest = digest_pair(type); mbedcrypto_c_call( mbedtls_md_hmac, std::get<0>(digest), to_const_ptr(key), key.size(), src, length, to_ptr(std::get<1>(digest))); return std::get<1>(digest); }
/* Add a request to the back of the queue. If the queue was empty/finished, point current to the new request. */ void push_back(buffer_t wr) { debug2("<WriteQueue> Inserted WR: size=%u, current=%u, size=%u\n", (uint32_t) wr->size(), current_, (uint32_t) size()); q.push_back(std::move(wr)); }
void Client_connection::recv_response(buffer_t buf) { if (buf->empty()) { end_response({Error::NO_REPLY}); return; } const std::string data{(char*) buf->data(), buf->size()}; // restart timer since we got data if(timer_.is_running()) timer_.restart(timeout_dur_); // create response if not exist if(res_ == nullptr) { try { res_ = make_response(data); // this also parses } catch(...) { end_response({Error::INVALID}); return; } } // if there already is a response else { // this is the case when Status line is received, but not yet headers. if(not res_->headers_complete() && req_->method() != HEAD) { *res_ << data; res_->parse(); } // here we assume all headers has already been received (could not be true?) else { // add chunks of body data res_->add_chunk(data); } } const auto& header = res_->header(); // TODO: Temporary, not good enough // if(res_->is_complete()) // Assume we want some headers if(!header.is_empty()) { if(header.has_field(header::Content_Length)) { try { const unsigned conlen = std::stoul(std::string(header.value(header::Content_Length))); const unsigned body_size = res_->body().size(); //printf("<http::Connection> [%s] Data: %u ConLen: %u Body:%u\n", // req_->uri().to_string().to_string().c_str(), data.size(), conlen, body_size); // risk buffering forever if no timeout if(body_size == conlen) { end_response(); } else if(body_size > conlen) { end_response({Error::INVALID}); } } catch(...) { end_response({Error::INVALID}); } } else end_response(); } else if(req_->method() == HEAD) { end_response(); } }
void hmac::start(const buffer_t& key) { mbedcrypto_c_call( mbedtls_md_hmac_starts, &pimpl->ctx_, to_const_ptr(key), key.size()); }
void SidTuneBase::acceptSidTune(const char* dataFileName, const char* infoFileName, buffer_t& buf, bool isSlashedFileName) { // Make a copy of the data file name and path, if available. if (dataFileName != nullptr) { const size_t fileNamePos = isSlashedFileName ? SidTuneTools::slashedFileNameWithoutPath(dataFileName) : SidTuneTools::fileNameWithoutPath(dataFileName); info->m_path = std::string(dataFileName, fileNamePos); info->m_dataFileName = std::string(dataFileName + fileNamePos); } // Make a copy of the info file name, if available. if (infoFileName != nullptr) { const size_t fileNamePos = isSlashedFileName ? SidTuneTools::slashedFileNameWithoutPath(infoFileName) : SidTuneTools::fileNameWithoutPath(infoFileName); info->m_infoFileName = std::string(infoFileName + fileNamePos); } // Fix bad sidtune set up. if (info->m_songs > MAX_SONGS) { info->m_songs = MAX_SONGS; } else if (info->m_songs == 0) { info->m_songs = 1; } if (info->m_startSong == 0 || info->m_startSong > info->m_songs) { info->m_startSong = 1; } info->m_dataFileLen = buf.size(); info->m_c64dataLen = buf.size() - fileOffset; // Calculate any remaining addresses and then // confirm all the file details are correct resolveAddrs(&buf[fileOffset]); if (checkRelocInfo() == false) { throw loadError(ERR_BAD_RELOC); } if (checkCompatibility() == false) { throw loadError(ERR_BAD_ADDR); } if (info->m_dataFileLen >= 2) { // We only detect an offset of two. Some position independent // sidtunes contain a load address of 0xE000, but are loaded // to 0x0FFE and call player at 0x1000. info->m_fixLoad = (endian_little16(&buf[fileOffset])==(info->m_loadAddr+2)); } // Check the size of the data. if (info->m_c64dataLen > MAX_MEMORY) { throw loadError(ERR_DATA_TOO_LONG); } else if (info->m_c64dataLen == 0) { throw loadError(ERR_EMPTY); } cache.swap(buf); }
ostream(buffer_t &buf) : buf(buf), offset(buf.size()) {}