void NetworkChangeNotification::deserialize(const Bytes& bytes, const Bytes::size_type length) { Bytes::size_type idx{0}; if (bytes.empty() || bytes.size() < length || get_type() != static_cast<Notification::Type>(bytes[idx++])) { throw std::runtime_error("Notification deserialization failed."); } check_idx(idx, length); idx += decode_int<decltype(m_iface_index)>(m_iface_index, bytes.data() + idx, length - idx); check_idx(idx, length); m_change_type = static_cast<net::NetworkChangeNotifier::ChangeType>(bytes[idx++]); }
Bytes hex_decode(const String& string) { Bytes ret; ret.reserve(string.length()/2); for (auto i: range(0, (string.length()/2)*2, 2)) { if (!isHex(string[i]) || !isHex(string[i+1])) break; ret.push_back((fromHex(string[i]) << 4) | fromHex(string[i+1])); } return ret; }
static Try<Nothing> increasePageCache(const vector<string>& tokens) { const Bytes UNIT = Megabytes(1); if (tokens.size() < 2) { return Error("Expect at least one argument"); } Try<Bytes> size = Bytes::parse(tokens[1]); if (size.isError()) { return Error("The first argument '" + tokens[1] + "' is not a byte size"); } // TODO(chzhcn): Currently, we assume the current working directory // is a temporary directory and will be cleaned up when the test // finishes. Since the child process will inherit the current // working directory from the parent process, that means the test // that uses this helper probably needs to inherit from // TemporaryDirectoryTest. Consider relaxing this constraint. Try<string> path = os::mktemp(path::join(os::getcwd(), "XXXXXX")); if (path.isError()) { return Error("Failed to create a temporary file: " + path.error()); } Try<int> fd = os::open(path.get(), O_WRONLY); if (fd.isError()) { return Error("Failed to open file: " + fd.error()); } // NOTE: We are doing round-down here to calculate the number of // writes to do. for (uint64_t i = 0; i < size.get().bytes() / UNIT.bytes(); i++) { // Write UNIT size to disk at a time. The content isn't important. Try<Nothing> write = os::write(fd.get(), string(UNIT.bytes(), 'a')); if (write.isError()) { os::close(fd.get()); return Error("Failed to write file: " + write.error()); } // Use fsync to make sure data is written to disk. if (fsync(fd.get()) == -1) { // Save the error message because os::close below might // overwrite the errno. const string message = os::strerror(errno); os::close(fd.get()); return Error("Failed to fsync: " + message); } } os::close(fd.get()); return Nothing(); }
bool yarp::os::impl::LocalCarrier::checkHeader(const Bytes& header) { if (header.length()==8) { std::string target = getSpecifierName(); for (int i=0; i<8; i++) { if (!(target[i]==header.get()[i])) { return false; } } return true; } return false; }
bool yarp::os::impl::HttpCarrier::checkHeader(const Bytes& header, const char *prefix) { if (header.length()==8) { String target = prefix; for (unsigned int i=0; i<target.length(); i++) { if (!(target[i]==header.get()[i])) { return false; } } return true; } return false; }
virtual bool checkHeader(const Bytes& header) { if (header.length()!=8) { return false; } const char *target = "HUMANITY"; for (int i=0; i<8; i++) { if (header.get()[i] != target[i]) { return false; } } return true; }
bool ares::from_bytes(Date& date, Bytes const& bytes) { if (bytes.size() < 7) return false; date.set(unpack_int16(bytes.begin()), // year bytes.begin()[2], // mon bytes.begin()[3], // day bytes.begin()[4], // hour bytes.begin()[5], // min bytes.begin()[6]); // sec return true; }
void ExtAudioFile::convert(Bytes& data, ALenum& format, ALsizei& frequency) { OSStatus err; // Read in the original file format. AudioStreamBasicDescription in_format; UInt32 in_format_size = sizeof(AudioStreamBasicDescription); err = ExtAudioFileGetProperty(_id, kExtAudioFileProperty_FileDataFormat, &in_format_size, &in_format); check_os_err(err, "ExtAudioFileGetProperty"); frequency = in_format.mSampleRate; if (in_format.mChannelsPerFrame == 1) { format = AL_FORMAT_MONO16; } else if (in_format.mChannelsPerFrame == 2) { format = AL_FORMAT_STEREO16; } else { throw Exception("audio file has more than two channels"); } // Convert to 16-bit native-endian linear PCM. Preserve the frequency and channel count // of the original format. AudioStreamBasicDescription out_format = in_format; out_format.mFormatID = kAudioFormatLinearPCM; out_format.mBytesPerPacket = 2 * out_format.mChannelsPerFrame; out_format.mFramesPerPacket = 1; out_format.mBytesPerFrame = 2 * out_format.mChannelsPerFrame; out_format.mBitsPerChannel = 16; out_format.mFormatFlags = kAudioFormatFlagsNativeEndian | kAudioFormatFlagIsPacked | kAudioFormatFlagIsSignedInteger; err = ExtAudioFileSetProperty(_id, kExtAudioFileProperty_ClientDataFormat, sizeof(AudioStreamBasicDescription), &out_format); check_os_err(err, "ExtAudioFileSetProperty"); // Get the number of frames. SInt64 frame_count; UInt32 frame_count_size = sizeof(int64_t); err = ExtAudioFileGetProperty(_id, kExtAudioFileProperty_FileLengthFrames, &frame_count_size, &frame_count); check_os_err(err, "ExtAudioFileGetProperty"); // Read the converted frames into memory. UInt32 frame_count_32 = frame_count; data.resize(frame_count * out_format.mBytesPerFrame); AudioBufferList data_buffer; data_buffer.mNumberBuffers = 1; data_buffer.mBuffers[0].mDataByteSize = data.size(); data_buffer.mBuffers[0].mNumberChannels = out_format.mChannelsPerFrame; data_buffer.mBuffers[0].mData = data.data(); err = ExtAudioFileRead(_id, &frame_count_32, &data_buffer); check_os_err(err, "ExtAudioFileRead"); }
Bytes Http::fetch( const size_t length, const shared_ptr< Response >& response ) { if ( response == nullptr ) { throw invalid_argument( String::empty ); } auto request = response->m_pimpl->m_request; if ( request == nullptr or request->m_pimpl->m_buffer == nullptr or request->m_pimpl->m_socket == nullptr ) { throw invalid_argument( String::empty ); } Bytes data = { }; if ( length > request->m_pimpl->m_buffer->size( ) ) { error_code error; const size_t size = length - request->m_pimpl->m_buffer->size( ); request->m_pimpl->m_socket->read( request->m_pimpl->m_buffer, size, error ); if ( error and error not_eq asio::error::eof ) { throw runtime_error( String::format( "Socket receive failed: '%s'", error.message( ).data( ) ) ); } const auto data_ptr = asio::buffer_cast< const Byte* >( request->m_pimpl->m_buffer->data( ) ); data = Bytes( data_ptr, data_ptr + length ); request->m_pimpl->m_buffer->consume( length ); } else { const auto data_ptr = asio::buffer_cast< const Byte* >( request->m_pimpl->m_buffer->data( ) ); data = Bytes( data_ptr, data_ptr + length ); request->m_pimpl->m_buffer->consume( length ); } auto& body = response->m_pimpl->m_body; if ( body.empty( ) ) { body = data; } else { body.insert( body.end( ), data.begin( ), data.end( ) ); } return data; }
Bytes buildBaseReadEepromResponseV1(uint16 valueRead) { ChecksumBuilder checksum; checksum.append_uint16(valueRead); //build success response Bytes bytes; bytes.push_back(0x72); bytes.push_back(Utils::msb(valueRead)); bytes.push_back(Utils::lsb(valueRead)); return bytes; }
TEST_F(RequestWriteTest, WriteString) { String test_string = "foo"; WriteString(test_string, *stream); Bytes bytes = ReadEverything(); ASSERT_TRUE(bytes); ASSERT_EQ(2 + 3, bytes->size()); ASSERT_EQ(0, bytes->at(0)); ASSERT_EQ(3, bytes->at(1)); ASSERT_EQ('f', bytes->at(2)); ASSERT_EQ('o', bytes->at(3)); ASSERT_EQ('o', bytes->at(4)); }
static void get_hnames(Iterator *it, std::vector<std::string> *list){ while(it->next()){ Bytes ks = it->key(); if(ks.data()[0] != DataType::HSIZE){ break; } std::string n; if(decode_hsize_key(ks, &n) == -1){ continue; } list->push_back(n); } }
static bool AstDecodeName(AstDecodeContext& c, AstName* name) { Bytes bytes; if (!c.d.readBytes(&bytes)) return false; size_t length = bytes.length(); char16_t *buffer = static_cast<char16_t *>(c.lifo.alloc(length * sizeof(char16_t))); for (size_t i = 0; i < length; i++) buffer[i] = bytes[i]; *name = AstName(buffer, length); return true; }
value lime_bytes_from_data_pointer (double data, int length) { intptr_t ptr = (intptr_t)data; Bytes bytes = Bytes (length); if (ptr) { memcpy (bytes.Data (), (const void*)ptr, length); } return bytes.Value (); }
Handle<Value> CreateExternalArrayBuffer(Local<Value> ba) { Handle<Value> buffer; Bytes *bytes = BYTES_FROM_BIN(ba->ToObject()); ASSERT_PIN(bytes, "Cannot create an external buffer from an invalid Bytes object"); bytes->setResizable(false); buffer = CreateExternalArrayBuffer(bytes->getLength(), bytes->getBytes()); buffer->ToObject()->SetHiddenValue(String::New(kRefByteArrayPropName), ba); buffer->ToObject()->Set(String::New("externalBuffer"), True(), ReadOnly); return buffer; }
ssize_t MpiP2PStream::read(const Bytes& b) { if (readAvail == 0) { // get new data reset(); int size; int available = 0; int tag = 0; int rank = comm->rank(); MPI_Status status; while (true) { if (terminate) return -1; // Check for a message MPI_Iprobe(!rank, tag, comm->comm, &available, &status); if (available) break; // Prevent the busy polling which hurts // performance in the oversubscription scenario Time::yield(); } MPI_Get_count(&status, MPI_BYTE, &size); if (size == (int)b.length()) { // size of received data matches expected data // do not use buffer, but write directly MPI_Recv(b.get(), size, MPI_BYTE, !rank, tag, comm->comm, &status); return size; } else { // allocate new buffer readBuffer = new char[size]; MPI_Recv(readBuffer, size, MPI_BYTE, !rank, tag, comm->comm, &status); //printf("got new msg of size %d\n", size); readAvail = size; readAt = 0; } } if (readAvail>0) { // copy data from buffer to destination object int take = readAvail; if (take>(int)b.length()) { take = (int)b.length(); } memcpy(b.get(),readBuffer+readAt,take); //printf("read %d of %d \n", take, readAvail); readAt += take; readAvail -= take; return take; } return 0; }
// returns the number of newly added items static int hset_one(SSDBImpl *ssdb, const Bytes &name, const Bytes &key, const Bytes &val, char log_type){ if(name.empty() || key.empty()){ log_error("empty name or key!"); return -1; } if(name.size() > SSDB_KEY_LEN_MAX ){ log_error("name too long! %s", hexmem(name.data(), name.size()).c_str()); return -1; } if(key.size() > SSDB_KEY_LEN_MAX){ log_error("key too long! %s", hexmem(key.data(), key.size()).c_str()); return -1; } int ret = 0; std::string dbval; if(ssdb->hget(name, key, &dbval) == 0){ // not found std::string hkey = encode_hash_key(name, key); ssdb->binlogs->Put(hkey, slice(val)); ssdb->binlogs->add_log(log_type, BinlogCommand::HSET, hkey); ret = 1; }else{ if(dbval != val){ std::string hkey = encode_hash_key(name, key); ssdb->binlogs->Put(hkey, slice(val)); ssdb->binlogs->add_log(log_type, BinlogCommand::HSET, hkey); } ret = 0; } return ret; }
Value read(const Bytes &vec, const String &key) { auto ff = detectDataFormat(vec); switch (ff) { case DataFormat::Cbor: return readCbor(vec); break; case DataFormat::Json: return readJson(CharReaderBase((char *)vec.data(), vec.size())); break; default: break; } return Value(); }
void GarbledCct::gen_init(const vector<Bytes> &ot_keys, const Bytes &gen_inp_mask, const Bytes &seed) { m_ot_keys = &ot_keys; m_gen_inp_mask = gen_inp_mask; m_prng.srand(seed); // R is a random k-bit string whose 0-th bit has to be 1 static Bytes tmp; tmp = m_prng.rand(Env::k()); tmp.set_ith_bit(0, 1); tmp.resize(16, 0); m_R = _mm_loadu_si128(reinterpret_cast<const __m128i*>(&tmp[0])); m_gate_ix = 0; m_gen_inp_ix = 0; m_evl_inp_ix = 0; m_gen_out_ix = 0; m_evl_out_ix = 0; m_o_bufr.clear(); if (m_w == 0) { m_w = new __m128i[Env::circuit().m_cnt]; } tmp.assign(16, 0); for (size_t ix = 0; ix < Env::k(); ix++) tmp.set_ith_bit(ix, 1); m_clear_mask = _mm_loadu_si128(reinterpret_cast<__m128i*>(&tmp[0])); m_m.resize(Env::circuit().gen_inp_cnt()*2); m_M.resize(Env::circuit().gen_inp_cnt()*2); Z m0, m1; // init group elements associated with the generator's input bits for (size_t ix = 0; ix < Env::circuit().gen_inp_cnt(); ix++) { m0.random(m_prng); m1.random(m_prng); m_m[2*ix+0] = m0; m_m[2*ix+1] = m1; m_M[2*ix+0] = Env::clawfree().F(0, m0); m_M[2*ix+1] = Env::clawfree().F(1, m1); } }
ssize_t MpiBcastStream::read(const Bytes& b) { if (terminate) { return -1; } if (readAvail == 0) { // get new data reset(); int size; #ifdef MPI_DEBUG printf("[MpiBcastStream @ %s] Trying to read\n", name.c_str()); #endif MPI_Bcast(&size, 1, MPI_INT, 0,comm->comm); #ifdef MPI_DEBUG printf("[MpiBcastStream @ %s] got size %d\n", name.c_str(), size); #endif if (size < 0) { execCmd(size); return 0; } if ((size_t)size == b.length()) { // size of received data matches expected data // do not use buffer, but write directly MPI_Bcast(b.get(), size, MPI_BYTE, 0, comm->comm); return size; } else { // allocate new buffer readBuffer = new char[size]; MPI_Bcast(readBuffer, size, MPI_BYTE, 0, comm->comm); //printf("got new msg of size %d\n", size); readAvail = size; readAt = 0; } } if (readAvail>0) { // copy data from buffer to destination object int take = readAvail; if (take>(int)b.length()) { take = b.length(); } memcpy(b.get(),readBuffer+readAt,take); //printf("read %d of %d \n", take, readAvail); readAt += take; readAvail -= take; return take; } return 0; }
bool decompress(const Bytes& cimg, FlexImage& img) { bool debug = false; if (!active) { init(); active = true; } cinfo.client_data = &error_buffer; cinfo.err = jpeg_std_error(&jerr.pub); jerr.pub.error_exit = net_error_exit; if (setjmp(jerr.setjmp_buffer)) { jpeg_finish_decompress(&cinfo); return false; } jpeg_net_src(&cinfo,(char*)cimg.get(),cimg.length()); jpeg_save_markers(&cinfo, JPEG_COM, 0xFFFF); jpeg_read_header(&cinfo, TRUE); jpeg_calc_output_dimensions(&cinfo); if(cinfo.jpeg_color_space == JCS_GRAYSCALE) { img.setPixelCode(VOCAB_PIXEL_MONO); } else { img.setPixelCode(VOCAB_PIXEL_RGB); } if (debug) printf("Got image %dx%d\n", cinfo.output_width, cinfo.output_height); img.resize(cinfo.output_width,cinfo.output_height); jpeg_start_decompress(&cinfo); //int row_stride = cinfo.output_width * cinfo.output_components; int at = 0; while (cinfo.output_scanline < cinfo.output_height) { JSAMPLE *lines[1]; lines[0] = (JSAMPLE*)(img.getPixelAddress(0,at)); jpeg_read_scanlines(&cinfo, lines, 1); at++; } if(readEnvelopeCallback && cinfo.marker_list && cinfo.marker_list->data_length > 0) { Bytes envelope(reinterpret_cast<char*>(cinfo.marker_list->data), cinfo.marker_list->data_length); readEnvelopeCallback(readEnvelopeCallbackData, envelope); } if (debug) printf("Read image!\n"); jpeg_finish_decompress(&cinfo); return true; }
value lime_lzma_decompress (value buffer) { #ifdef LIME_LZMA Bytes data; data.Set (buffer); Bytes result; LZMA::Decompress (&data, &result); return result.Value (); #else return alloc_null (); #endif }
WirelessPacket buildWriteEepromResponse(int nodeAddress) { Bytes payload; payload.push_back(0x00); payload.push_back(0x04); //build the correct packet response first WirelessPacket packet; packet.deliveryStopFlags(DeliveryStopFlags::fromByte(0x00)); packet.type(static_cast<WirelessPacket::PacketType>(0x00)); packet.nodeAddress(nodeAddress); packet.payload(payload); return packet; }
value lime_deflate_compress (value buffer) { #ifdef LIME_ZLIB Bytes data; data.Set (buffer); Bytes result; Zlib::Compress (DEFLATE, &data, &result); return result.Value (); #else return alloc_null(); #endif }
value lime_gzip_decompress (value buffer) { #ifdef LIME_ZLIB Bytes data; data.Set (buffer); Bytes result; Zlib::Decompress (GZIP, &data, &result); return result.Value (); #else return alloc_null (); #endif }
value lime_bytes_from_data_pointer (value data, value length) { int size = val_int (length); intptr_t ptr = (intptr_t)val_float (data); Bytes bytes = Bytes (size); if (ptr) { memcpy (bytes.Data (), (const void*)ptr, size); } return bytes.Value (); }
value lime_text_layout_position (value textHandle, value fontHandle, value size, value textString, value data) { #if defined(LIME_FREETYPE) && defined(LIME_HARFBUZZ) TextLayout *text = (TextLayout*)(intptr_t)val_float (textHandle); Font *font = (Font*)(intptr_t)val_float (fontHandle); Bytes bytes = Bytes (data); text->Position (font, val_int (size), val_string (textString), &bytes); return bytes.Value (); #endif return alloc_null (); }
int BackendSync::Client::copy(){ if(this->iter == NULL){ log_debug("new iterator, last_key: '%s'", hexmem(last_key.data(), last_key.size()).c_str()); this->iter = backend->ssdb->iterator(this->last_key, "", -1); } for(int i=0; i<1000; i++){ if(!iter->next()){ log_info("fd: %d, copy end", link->fd()); this->status = Client::SYNC; delete this->iter; this->iter = NULL; Binlog log(this->last_seq, BinlogType::COPY, BinlogCommand::END, ""); log_trace("fd: %d, %s", link->fd(), log.dumps().c_str()); link->send(log.repr(), "copy_end"); break; }else{ Bytes key = iter->key(); Bytes val = iter->val(); this->last_key = key.String(); if(key.size() == 0){ continue; } char cmd = 0; char data_type = key.data()[0]; if(data_type == DataType::KV){ cmd = BinlogCommand::KSET; }else if(data_type == DataType::HASH){ cmd = BinlogCommand::HSET; }else if(data_type == DataType::ZSET){ cmd = BinlogCommand::ZSET; }else{ continue; } Binlog log(this->last_seq, BinlogType::COPY, cmd, key.Slice()); log_trace("fd: %d, %s", link->fd(), log.dumps().c_str()); link->send(log.repr(), val); //if(link->output->size() > 1024 * 1024){ break; //} } } return 1; }
void AutoCalResult_shmLink::parse(const Bytes& autoCalInfo) { if(autoCalInfo.size() < 19) { assert(false); return; } typedef WirelessTypes WT; DataBuffer data(autoCalInfo); //Ch1 error flag and offset m_errorFlagCh1 = static_cast<WT::AutoCalErrorFlag>(data.read_uint8()); m_offsetCh1 = data.read_float(); //Ch2 error flag and offset m_errorFlagCh2 = static_cast<WT::AutoCalErrorFlag>(data.read_uint8()); m_offsetCh2 = data.read_float(); //Ch3 error flag and offset m_errorFlagCh3 = static_cast<WT::AutoCalErrorFlag>(data.read_uint8()); m_offsetCh3 = data.read_float(); //temperature at time of cal m_temperature = data.read_float(); }
// This function should be async-signal-safe but it isn't: at least // posix_memalign, mlock, memset and perror are not safe. int consumeMemory(const Bytes& _size, const Duration& duration, int pipes[2]) { // In child process ::close(pipes[1]); int buf; // Wait until the parent signals us to continue. while (::read(pipes[0], &buf, sizeof(buf)) == -1 && errno == EINTR); ::close(pipes[0]); size_t size = static_cast<size_t>(_size.bytes()); void* buffer = NULL; if (posix_memalign(&buffer, getpagesize(), size) != 0) { perror("Failed to allocate page-aligned memory, posix_memalign"); abort(); } // We use mlock and memset here to make sure that the memory // actually gets paged in and thus accounted for. if (mlock(buffer, size) != 0) { perror("Failed to lock memory, mlock"); abort(); } if (memset(buffer, 1, size) != buffer) { perror("Failed to fill memory, memset"); abort(); } os::sleep(duration); return 0; }