static inline void ttyrec_timestamp(struct ttyrec_header *th) { struct timeval t; gettimeofday(&t, 0); th->sec=to_little_endian(t.tv_sec); th->usec=to_little_endian(t.tv_usec); }
size_t rvidcc_write_serial(const void *ptr, size_t len) { size_t bytes; #ifndef RVIDCC_RAW unsigned long sentinel; #endif bytes = rvidcc_serial_can_write(); #ifndef RVIDCC_RAW if (bytes > sizeof(unsigned long)) { /* Size limit the amount of data to be written, allowing for sentinel */ if (len > (bytes - sizeof(unsigned long))) len = (bytes - sizeof(unsigned long)); sentinel = to_little_endian(UNCOMP_TTY_START | (unsigned short)len); rvidcc_write_lowlevel(&sentinel, sizeof(unsigned long)); rvidcc_write_lowlevel(ptr, len); } #else if (bytes > 0) { /* Size limit the amount of data to be written */ if (len > bytes) len = bytes; rvidcc_write_lowlevel(ptr, len); } #endif else len = 0; return len; }
// string 4 bytes length // return integer static int reply_length(lua_State *L) { const char * rawlen_str = luaL_checkstring(L, 1); int rawlen = 0; memcpy(&rawlen, rawlen_str, sizeof(int)); int length = to_little_endian(rawlen); lua_pushinteger(L, length - 4); return 1; }
hash_digest hash_transaction_impl(const transaction_type& tx, uint32_t* hash_type_code) { data_chunk serialized_tx(satoshi_raw_size(tx)); satoshi_save(tx, serialized_tx.begin()); if (hash_type_code != nullptr) extend_data(serialized_tx, to_little_endian(*hash_type_code)); return bitcoin_hash(serialized_tx); }
int rvidcc_transmit_ip_packet(void *packet, size_t length) { if (rvidcc_serial_can_write() >= (length + 4)) { unsigned long sentinel = to_little_endian(UNCOMP_ETH_START | (unsigned short)length); rvidcc_write_lowlevel(&sentinel, 4); rvidcc_write_lowlevel(packet, length); } else length = 0; return length; }
EMSCRIPTEN_KEEPALIVE int decode(Encoder* encoder, int length) { int encoded_frames = opus_decode( encoder->decoder, encoder->in_opus,//opus, length, encoder->in_decoded, MAX_FRAME_SIZE, 0 ); to_little_endian(encoder->in_decoded, MAX_OUTPUT_BYTES, encoder->out_little_endian); return encoded_frames; }
void outgoing::send(czmqpp::socket& socket) const { czmqpp::message message; // Optional, ROUTER sockets strip this. if (!destination_.empty()) message.append(destination_); message.append({ command_.begin(), command_.end() }); message.append(to_chunk(to_little_endian(id_))); message.append(data_); message.send(socket); }
std::string hd_public_key::encoded() const { auto prefix = mainnet_public_prefix; if (lineage_.testnet) prefix = testnet_public_prefix; auto data = build_data({ to_big_endian(prefix), to_byte(lineage_.depth), to_little_endian(lineage_.parent_fingerprint), to_big_endian(lineage_.child_number), c_, K_ }, checksum_size); append_checksum(data); return encode_base58(data); }
void FileBase::flush() { this->subflush(); if (m_dirty) { byte header[sizeof(m_flags)]; byte* ptr = header; for (auto&& f : m_flags) { to_little_endian(f, ptr); ptr += sizeof(f); } m_header->write_header(header, sizeof(header)); m_dirty = false; } m_header->flush_header(); m_stream->flush(); }
BCW_API std::string hd_public_key::serialize() const { data_chunk data; data.reserve(serialized_length); auto prefix = mainnet_public_prefix; if (lineage_.testnet) prefix = testnet_public_prefix; extend_data(data, to_big_endian(prefix)); data.push_back(lineage_.depth); extend_data(data, to_little_endian(lineage_.parent_fingerprint)); extend_data(data, to_big_endian(lineage_.child_number)); extend_data(data, c_); extend_data(data, K_); append_checksum(data); return encode_base58(data); }
void SimpleDirectory::subflush() { if (m_dirty) { m_stream->resize(0); char buffer[Directory::MAX_FILENAME_LENGTH + 1 + 32 + 4]; offset_type off = 0; for (auto&& pair : m_table) { memset(buffer, 0, sizeof(buffer)); if (pair.first.size() > MAX_FILENAME_LENGTH) continue; memcpy(buffer, pair.first.data(), pair.first.size()); memcpy(buffer + MAX_FILENAME_LENGTH + 1, pair.second.first.data(), ID_LENGTH); to_little_endian(static_cast<uint32_t>(pair.second.second), buffer + sizeof(buffer) - sizeof(uint32_t)); this->m_stream->write(buffer, off, sizeof(buffer)); off += sizeof(buffer); } m_dirty = false; } }
void outgoing_message::send(czmqpp::socket& socket) const { czmqpp::message message; // [ DESTINATION ] (optional - ROUTER sockets strip this) if (!dest_.empty()) message.append(dest_); // [ COMMAND ] append_str(message, command_); // [ ID ] auto raw_id = to_chunk(to_little_endian(id_)); BITCOIN_ASSERT(raw_id.size() == sizeof(id_)); message.append(raw_id); // [ DATA ] message.append(data_); // Send. message.send(socket); }
BC_API void append_checksum(data_chunk& data) { uint32_t checksum = bitcoin_checksum(data); extend_data(data, to_little_endian(checksum)); }
int rvidcc_init(void) { register unsigned int id; int err = 0; #ifndef RVIDCC_RAW unsigned int macRequest = to_little_endian(UNCOMP_ETH_START); #endif RVIDCC_DISABLE_INTERRUPTS; /* check core type, raising error if core not supported */ READ_CORE_ID(id); if ((id & 0xF00) == 0xA00) { arm_type = arm10; #ifndef RVIDCC_ARM10 err = RVIDCC_ERR_CORE_NOT_SUPPORTED; #endif } else if (id >= 0xb00) { arm_type = arm11_and_later; #ifndef RVIDCC_ARM11 err = RVIDCC_ERR_CORE_NOT_SUPPORTED; #endif } else { arm_type = arm9_and_earlier; #ifndef RVIDCC_ARM79 err = RVIDCC_ERR_CORE_NOT_SUPPORTED; #endif } if (!err) { dcc_inbuf.buf = dcc_inbuffer; dcc_inbuf.size = RVIDCC_BUFSIZE; dcc_inbuf.readPtr = dcc_inbuf.buf; dcc_inbuf.writePtr = dcc_inbuf.buf; dcc_outbuf.buf = dcc_outbuffer; dcc_outbuf.size = RVIDCC_BUFSIZE; dcc_outbuf.readPtr = dcc_outbuf.buf; dcc_outbuf.writePtr = dcc_outbuf.buf; #ifndef RVIDCC_RAW dcc_tty_inbuf.buf = dcc_tty_inbuffer; dcc_tty_inbuf.size = DCC_TTY_INBUF_SIZE; dcc_tty_inbuf.readPtr = dcc_tty_inbuf.buf; dcc_tty_inbuf.writePtr = dcc_tty_inbuf.buf; /* request a MAC address */ rvidcc_write_lowlevel(&macRequest, sizeof(unsigned int)); #endif /* Enable interrupts (write interrupt will be enabled when something is written) */ RVIDCC_ENABLE_READ_INTERRUPT; RVIDCC_ENABLE_INTERRUPTS; } return err; }
hash_digest transaction::hash(uint32_t hash_type_code) const { data_chunk serialized = to_data(); extend_data(serialized, to_little_endian(hash_type_code)); return bitcoin_hash(serialized); }
// 1 string data // 2 result document table // return boolean succ (false -> request id, error document) // number request_id // document first // string cursor_id // integer startfrom static int op_reply(lua_State *L) { size_t data_len = 0; const char * data = luaL_checklstring(L,1,&data_len); struct { // int32_t length; // total message size, including this int32_t request_id; // identifier for this message int32_t response_id; // requestID from the original request // (used in reponses from db) int32_t opcode; // request type int32_t flags; int32_t cursor_id[2]; int32_t starting; int32_t number; } const *reply = (const void *)data; if (data_len < sizeof(*reply)) { lua_pushboolean(L, 0); return 1; } int id = to_little_endian(reply->response_id); int flags = to_little_endian(reply->flags); if (flags & REPLY_QUERYFAILURE) { lua_pushboolean(L,0); lua_pushinteger(L, id); lua_pushlightuserdata(L, (void *)(reply+1)); return 3; } int starting_from = to_little_endian(reply->starting); int number = to_little_endian(reply->number); int sz = (int)data_len - sizeof(*reply); const uint8_t * doc = (const uint8_t *)(reply+1); if (lua_istable(L,2)) { int i = 1; while (sz > 4) { lua_pushlightuserdata(L, (void *)doc); lua_rawseti(L, 2, i); int32_t doc_len = get_length((const document)doc); doc += doc_len; sz -= doc_len; ++i; } if (i != number + 1) { lua_pushboolean(L,0); lua_pushinteger(L, id); return 2; } int c = lua_rawlen(L, 2); for (;i<=c;i++) { lua_pushnil(L); lua_rawseti(L, 2, i); } } lua_pushboolean(L,1); lua_pushinteger(L, id); if (number == 0) lua_pushnil(L); else lua_pushlightuserdata(L, (void *)(reply+1)); if (reply->cursor_id[0] == 0 && reply->cursor_id[1]==0) { // closed cursor lua_pushnil(L); } else { lua_pushlstring(L, (const char *)(reply->cursor_id), 8); } lua_pushinteger(L, starting_from); return 5; }
void append_checksum(data_chunk& data) { const auto checksum = bitcoin_checksum(data); extend_data(data, to_little_endian(checksum)); }
/* this is written out longhand because it may be called from an interrupt routine */ void rvidcc_read(void) { register unsigned int reg; int pollcount = -1; int some_transfer = 0; switch (arm_type) { #ifdef RVIDCC_ARM79 case arm9_and_earlier: { /* Try to read everything available */ while (ringbuf_available_space(&dcc_inbuf) >= sizeof(unsigned int)) { do { CAN_READ_ARM9_DCC(reg); } while (!reg && pollcount != -1 && --pollcount > 0); if (!reg) break; READ_ARM9_DCC(reg); *(unsigned int *)dcc_inbuf.writePtr = to_little_endian(reg); ringbuf_advance_write(&dcc_inbuf, sizeof(unsigned int)); pollcount = RVIDCC_MAX_INLINE_POLLS; some_transfer = 1; } break; } #endif #ifdef RVIDCC_ARM10 case arm10: { /* Try to read everything available */ while (ringbuf_available_space(&dcc_inbuf) >= sizeof(unsigned int)) { do { CAN_READ_ARM10_DCC(reg); } while (!reg && pollcount != -1 && --pollcount > 0); if (!reg) break; READ_ARM10_DCC(reg); *(unsigned int *)dcc_inbuf.writePtr = to_little_endian(reg); ringbuf_advance_write(&dcc_inbuf, sizeof(unsigned int)); pollcount = RVIDCC_MAX_INLINE_POLLS; some_transfer = 1; } break; } #endif #ifdef RVIDCC_ARM11 case arm11_and_later: { /* Try to read everything available */ while (ringbuf_available_space(&dcc_inbuf) >= sizeof(unsigned int)) { do { CAN_READ_ARM11_DCC(reg); } while (!reg && pollcount != -1 && --pollcount > 0); if (!reg) break; READ_ARM11_DCC(reg); *(unsigned int *)dcc_inbuf.writePtr = to_little_endian(reg); ringbuf_advance_write(&dcc_inbuf, sizeof(unsigned int)); pollcount = RVIDCC_MAX_INLINE_POLLS; some_transfer = 1; } break; } #endif default: break; } if (ringbuf_available_space(&dcc_inbuf) == 0) RVIDCC_DISABLE_READ_INTERRUPT; if (some_transfer) rvidcc_cb_notify(); }
void rvidcc_process(void) { unsigned int sentinel = 0; int compsize = 0; /* Process received data */ while (ringbuf_available_data(&dcc_inbuf) >= sizeof(unsigned int)) { int bytes; unsigned char *buf; /* Search for start sentinel */ sentinel = to_little_endian(*(unsigned int *)dcc_inbuf.readPtr); compsize = sentinel & 0xffff; /* Discard if not a start sentinel */ if ((sentinel & START_MASK) != START_SENTINEL || compsize > sizeof(dcc_temp_source) || compsize <= 0) { ringbuf_advance_read(&dcc_inbuf, sizeof(unsigned int)); continue; } /* Sentinel found, come back later if not enough data */ bytes = ringbuf_available_data(&dcc_inbuf); if (bytes < compsize + sizeof(unsigned int)) break; /* Copy to a contiguous buffer */ ringbuf_get(&dcc_inbuf, dcc_temp_source, compsize + sizeof(unsigned int)); sentinel &= 0xffff0000; buf = dcc_temp_source + sizeof(unsigned int); bytes = compsize; /* Round up to nearest whole word */ if (compsize & 0x3) compsize = (compsize + 4) & ~0x3; /* advance by packet size + sentinel */ ringbuf_advance_read(&dcc_inbuf, compsize + sizeof(unsigned int)); /* allow more data to be received */ RVIDCC_ENABLE_READ_INTERRUPT; /* Process TTY or IP packet accordingly */ if (sentinel == UNCOMP_TTY_START) { ringbuf_put(&dcc_tty_inbuf, buf, bytes); ringbuf_advance_write(&dcc_tty_inbuf, bytes); } else { if (bytes == 4) /* ARP 'is this IP address you?' */ { if (rvidcc_cb_has_addr(buf)) rvidcc_transmit_ip_packet(buf, 4); } else if (bytes == 6) /* set MAC address */ rvidcc_cb_set_mac_address(buf); else rvidcc_cb_ip_packet_received(buf, (size_t)bytes); } } }
void rvidcc_write(void) { register unsigned int reg; int pollcount = -1; int some_transfer = 0; switch (arm_type) { #ifdef RVIDCC_ARM79 case arm9_and_earlier: { /* Try to write everything available */ while (ringbuf_available_data(&dcc_outbuf) >= sizeof(unsigned int)) { /* On the first word, the write is aborted immediately if the DCC write * register is full. On subsequent words, the driver waits for a * bit (RVIDCC_MAX_INLINE_POLLS times) as the RVI will probably * read the DCC register soon because it knows data is being sent. */ do { CAN_WRITE_ARM9_DCC(reg); } while (!reg && pollcount != -1 && --pollcount > 0); if (!reg) break; reg = to_little_endian(*(unsigned int *)dcc_outbuf.readPtr); WRITE_ARM9_DCC(reg); ringbuf_advance_read(&dcc_outbuf, sizeof(unsigned int)); pollcount = RVIDCC_MAX_INLINE_POLLS; some_transfer = 1; } pollcount = -1; break; } #endif #ifdef RVIDCC_ARM10 case arm10: { /* Try to write everything available */ while (ringbuf_available_data(&dcc_outbuf) >= sizeof(unsigned int)) { do { CAN_WRITE_ARM10_DCC(reg); } while (!reg && pollcount != -1 && --pollcount > 0); if (!reg) break; reg = to_little_endian(*(unsigned int *)dcc_outbuf.readPtr); WRITE_ARM10_DCC(reg); ringbuf_advance_read(&dcc_outbuf, sizeof(unsigned int)); pollcount = RVIDCC_MAX_INLINE_POLLS; some_transfer = 1; } pollcount = -1; break; } #endif #ifdef RVIDCC_ARM11 case arm11_and_later: { /* Try to write everything available */ while (ringbuf_available_data(&dcc_outbuf) >= sizeof(unsigned int)) { do { CAN_WRITE_ARM11_DCC(reg); } while (!reg && pollcount != -1 && --pollcount > 0); if (!reg) break; reg = to_little_endian(*(unsigned int *)dcc_outbuf.readPtr); WRITE_ARM11_DCC(reg); ringbuf_advance_read(&dcc_outbuf, sizeof(unsigned int)); pollcount = RVIDCC_MAX_INLINE_POLLS; some_transfer = 1; } pollcount = -1; break; } #endif default: break; } if (some_transfer) rvidcc_cb_notify(); /* Disable interrupt if nothing left to write */ if (ringbuf_available_data(&dcc_outbuf) == 0) RVIDCC_DISABLE_WRITE_INTERRUPT; }
hash_digest transaction::hash(uint32_t sighash_type) const { auto serialized = to_data(); extend_data(serialized, to_little_endian(sighash_type)); return bitcoin_hash(serialized); }