void EventHeader::fromStream( std::istream &stream ) { clear( ); uint8 mode; stream.read( reinterpret_cast<char*>(&mode), sizeof(mode) ); setMode( mode ); uint8 tmp[3]; stream.read( reinterpret_cast<char*>(tmp), 3 ); uint32 nev = tmp[0] + tmp[1]*256 + tmp[2]*65536; float32 efs; readLittleEndian( stream, efs ); setSamplingRate( efs ); std::vector<uint32> positions; std::vector<uint16> types; positions.resize( nev ); types.resize( nev ); for( size_t i=0; i<nev; i++ ) readLittleEndian( stream, positions[i] ); for( size_t i=0; i<nev; i++ ) readLittleEndian( stream, types[i] ); if( getMode() == 1 ) { for( size_t i=0; i<nev; i++ ) { Mode1Event e; e.position = positions[i]; e.type = types[i]; addEvent( e ); } } else if( getMode() == 3 ) { std::vector<uint16> channels; std::vector<uint32> durations; channels.resize( nev ); durations.resize( nev ); for( size_t i=0; i<nev; i++ ) readLittleEndian( stream, channels[i] ); for( size_t i=0; i<nev; i++ ) readLittleEndian( stream, durations[i] ); for( size_t i=0; i<nev; i++ ) { Mode3Event e; e.position = positions[i]; e.type = types[i]; e.channel = channels[i]; e.duration = durations[i]; addEvent( e ); } } else throw exception::invalid_eventmode( boost::lexical_cast<std::string>( getMode() ) ); }
static void processCommand(const uint8_t *command) { switch (command[0] & 0x3f) { case CMD_GO_IDLE: // If a virtual block device wasn't specified, don't initialize if (gBlockDevData) { gIsReady = true; gCurrentState = STATE_SEND_RESULT; gCommandResult = 1; } break; case CMD_SEND_OP_COND: if (gResetDelay) { gCommandResult = 1; gResetDelay--; } else gCommandResult = 0; gCurrentState = STATE_SEND_RESULT; break; case CMD_SET_BLOCKLEN: if (!gIsReady) { printf("CMD_SET_BLOCKLEN: card not ready\n"); exit(1); } gBlockLength = readLittleEndian(command + 1); gCurrentState = STATE_SEND_RESULT; gCommandResult = 0; break; case CMD_READ_SINGLE_BLOCK: if (!gIsReady) { printf("CMD_READ_SINGLE_BLOCK: card not ready\n"); exit(1); } gReadOffset = readLittleEndian(command + 1) * gBlockLength; gCurrentState = STATE_WAIT_READ_RESPONSE; gStateDelay = rand() & 0xf; // Wait a random amount of time gResponseValue = 0; break; } }
static bool decodeKey(const uint8_t*& data, const uint8_t* end, IDBKeyData& result) { if (!data || data >= end) return false; SIDBKeyType type = static_cast<SIDBKeyType>(data++[0]); switch (type) { case SIDBKeyType::Min: result = IDBKeyData::minimum(); return true; case SIDBKeyType::Max: result = IDBKeyData::maximum(); return true; case SIDBKeyType::Number: { double d; if (!readDouble(data, end, d)) return false; result.setNumberValue(d); return true; } case SIDBKeyType::Date: { double d; if (!readDouble(data, end, d)) return false; result.setDateValue(d); return true; } case SIDBKeyType::String: { uint32_t length; if (!readLittleEndian(data, end, length)) return false; if (static_cast<uint64_t>(end - data) < length * 2) return false; Vector<UChar> buffer; buffer.reserveInitialCapacity(length); for (size_t i = 0; i < length; i++) { uint16_t ch; if (!readLittleEndian(data, end, ch)) return false; buffer.uncheckedAppend(ch); } result.setStringValue(String::adopt(WTFMove(buffer))); return true; } case SIDBKeyType::Binary: { uint64_t size64; if (!readLittleEndian(data, end, size64)) return false; if (static_cast<uint64_t>(end - data) < size64) return false; if (size64 > std::numeric_limits<size_t>::max()) return false; size_t size = static_cast<size_t>(size64); Vector<uint8_t> dataVector; dataVector.append(data, size); data += size; result.setBinaryValue(ThreadSafeDataBuffer::adoptVector(dataVector)); return true; } case SIDBKeyType::Array: { uint64_t size64; if (!readLittleEndian(data, end, size64)) return false; if (size64 > std::numeric_limits<size_t>::max()) return false; size_t size = static_cast<size_t>(size64); Vector<IDBKeyData> array; array.reserveInitialCapacity(size); for (size_t i = 0; i < size; ++i) { IDBKeyData keyData; if (!decodeKey(data, end, keyData)) return false; ASSERT(keyData.isValid()); array.uncheckedAppend(WTFMove(keyData)); } result.setArrayValue(array); return true; } default: LOG_ERROR("decodeKey encountered unexpected type: %i", (int)type); return false; } }
static bool readDouble(const uint8_t*& data, const uint8_t* end, double& d) { return readLittleEndian(data, end, *reinterpret_cast<uint64_t*>(&d)); }