uint32_t FileRead::calcChecksum(uint32_t start) { uint32_t oldpos = tell(); seek(start); // buffered reading char buffer[128]; size_t len = length(); boost::crc_32_type crc; while(true) { int maxread = std::min(sizeof(buffer), len - tell()); readRawBytes( buffer, maxread ); // read into buffer for(int i = 0; i < maxread; ++i) { crc.process_bytes(buffer, maxread); } if(maxread < 32) break; } // return read pointer back to old position seek(oldpos); return crc(); }
/*---------------------------------------------------------------------------*/ int loopbackTest(int serialPort) { const uint8_t testLength = 32; dataBuffer[0] = testLength; /* msg len */ dataBuffer[1] = 2; /* command */ for (int i = 0; i < testLength; ++i) { dataBuffer[i+2] = i; } write(serialPort,dataBuffer,testLength+2); readRawBytes(serialPort,dataBuffer,testLength,20000); for (int i = 1; i < testLength; ++i) { if(dataBuffer[i] != i-1) { printf("Data mismatch: got: %d expected: %d\r\n",dataBuffer[i],i-1); return -1; } } return 0; }
std::string FileRead::readString() { char buffer[32]; // thats our read buffer std::string read = ""; // thats what we read so far size_t len = length(); while(true) // check that we can read as much as want { int maxread = std::min(sizeof(buffer), len - tell()); readRawBytes( buffer, maxread ); // read into buffer for(int i = 0; i < maxread; ++i) { if(buffer[i] == 0) { seek( tell() - maxread + i + 1); return read; } else { read += buffer[i]; // this might not be the most efficient way... } } // when we reached the end of file if(maxread < 32) break; } BOOST_THROW_EXCEPTION(EOFException(mFileName)); }
boost::shared_array<char> FileRead::readRawBytes( std::size_t num_of_bytes ) { // creates the buffer boost::shared_array<char> buffer ( new char[num_of_bytes] ); readRawBytes( buffer.get(), num_of_bytes ); return buffer; }
char FileRead::readByte() { check_file_open(); char ret; readRawBytes(reinterpret_cast<char*>(&ret), sizeof(ret)); return ret; }
float FileRead::readFloat() { check_file_open(); float ret; readRawBytes(reinterpret_cast<char*>(&ret), sizeof(ret)); return ret; }
/*---------------------------------------------------------------------------*/ int pullDataFromFifo(int serialPort,int len, uint8_t* rxBuffer) { dataBuffer[0] = 2; /* msg len */ dataBuffer[1] = 10; /* command */ dataBuffer[2] = len; /* fifo read length */ write(serialPort,dataBuffer,3); return readRawBytes(serialPort,rxBuffer,len,20000); }
/*---------------------------------------------------------------------------*/ int getVersion(int serialPort) { dataBuffer[0] = 1; /* msg len */ dataBuffer[1] = 1; /* command */ write(serialPort,dataBuffer,2); readRawBytes(serialPort,dataBuffer,1,20000); return (int8_t)dataBuffer[0]; }
/*---------------------------------------------------------------------------*/ int getRxFifoCount(int serialPort) { dataBuffer[0] = 1; /* msg len */ dataBuffer[1] = 9; /* command */ write(serialPort,dataBuffer,2); readRawBytes(serialPort,dataBuffer,2,20000); return (uint16_t)(dataBuffer[0]+(dataBuffer[1]<<8)); }
/*---------------------------------------------------------------------------*/ int isSending(int serialPort) { dataBuffer[0] = 1; /* msg len */ dataBuffer[1] = 6; /* command */ write(serialPort,dataBuffer,2); int rc = readRawBytes(serialPort,dataBuffer,1,20000); return (int8_t)dataBuffer[0]; }
/*---------------------------------------------------------------------------*/ int sendRadioMessage(int serialPort, uint8_t* buf, int len) { int rc; int timeoutCounter; uint8_t rxBuff[32]; write(serialPort,buf,len); readRawBytes(serialPort,dataBuffer,1,20000); if(dataBuffer[0] != 0xAA) { printf("[sendRadioMessage]: ACK error!\n"); return -1; } timeoutCounter = 0; while(isSending(serialPort) && (timeoutCounter < TIMEOUT)) { timeoutCounter++; } if(!(timeoutCounter < TIMEOUT)) { // printf("[sendRadioMessage]: Timeout err!\n"); return -1; } if(getTransmissionResult(serialPort) == 0) { timeoutCounter = 0; while((getRxFifoCount(serialPort) == 0) && (timeoutCounter < TIMEOUT)) { timeoutCounter++; } if(!(timeoutCounter < TIMEOUT)) { // printf("[sendRadioMessage]: Timeout err!\n"); return -1; } pullDataFromFifo(serialPort,32,rxBuff); return 0; } else { // printf("[sendRadioMessage]: Missing message?\n"); return -1; } }
/*---------------------------------------------------------------------------*/ int setRXAddress(int serialPort, uint8_t* buf) { dataBuffer[0] = 6; /* msg len */ dataBuffer[1] = 3; /* uC command */ dataBuffer[2] = buf[0]; dataBuffer[3] = buf[1]; dataBuffer[4] = buf[2]; dataBuffer[5] = buf[3]; dataBuffer[6] = buf[4]; write(serialPort,dataBuffer,7); readRawBytes(serialPort,dataBuffer,1,20000); if(dataBuffer[0] != 0xAA) { printf("[sendRadioMessage]: ACK error!\n"); return -1; } return 0; }
CryptoKey* V8ScriptValueDeserializerForModules::readCryptoKey() { // Read params. uint8_t rawKeyType; if (!readOneByte(&rawKeyType)) return nullptr; WebCryptoKeyAlgorithm algorithm; WebCryptoKeyType keyType = WebCryptoKeyTypeSecret; switch (rawKeyType) { case AesKeyTag: { uint32_t rawId; WebCryptoAlgorithmId id; uint32_t lengthBytes; if (!readUint32(&rawId) || !algorithmIdFromWireFormat(rawId, &id) || !readUint32(&lengthBytes) || lengthBytes > std::numeric_limits<unsigned short>::max() / 8u) return nullptr; algorithm = WebCryptoKeyAlgorithm::createAes(id, lengthBytes * 8); keyType = WebCryptoKeyTypeSecret; break; } case HmacKeyTag: { uint32_t lengthBytes; uint32_t rawHash; WebCryptoAlgorithmId hash; if (!readUint32(&lengthBytes) || lengthBytes > std::numeric_limits<unsigned>::max() / 8 || !readUint32(&rawHash) || !algorithmIdFromWireFormat(rawHash, &hash)) return nullptr; algorithm = WebCryptoKeyAlgorithm::createHmac(hash, lengthBytes * 8); keyType = WebCryptoKeyTypeSecret; break; } case RsaHashedKeyTag: { uint32_t rawId; WebCryptoAlgorithmId id; uint32_t rawKeyType; uint32_t modulusLengthBits; uint32_t publicExponentSize; const void* publicExponentBytes; uint32_t rawHash; WebCryptoAlgorithmId hash; if (!readUint32(&rawId) || !algorithmIdFromWireFormat(rawId, &id) || !readUint32(&rawKeyType) || !asymmetricKeyTypeFromWireFormat(rawKeyType, &keyType) || !readUint32(&modulusLengthBits) || !readUint32(&publicExponentSize) || !readRawBytes(publicExponentSize, &publicExponentBytes) || !readUint32(&rawHash) || !algorithmIdFromWireFormat(rawHash, &hash)) return nullptr; algorithm = WebCryptoKeyAlgorithm::createRsaHashed( id, modulusLengthBits, reinterpret_cast<const unsigned char*>(publicExponentBytes), publicExponentSize, hash); break; } case EcKeyTag: { uint32_t rawId; WebCryptoAlgorithmId id; uint32_t rawKeyType; uint32_t rawNamedCurve; WebCryptoNamedCurve namedCurve; if (!readUint32(&rawId) || !algorithmIdFromWireFormat(rawId, &id) || !readUint32(&rawKeyType) || !asymmetricKeyTypeFromWireFormat(rawKeyType, &keyType) || !readUint32(&rawNamedCurve) || !namedCurveFromWireFormat(rawNamedCurve, &namedCurve)) return nullptr; algorithm = WebCryptoKeyAlgorithm::createEc(id, namedCurve); break; } case NoParamsKeyTag: { uint32_t rawId; WebCryptoAlgorithmId id; if (!readUint32(&rawId) || !algorithmIdFromWireFormat(rawId, &id)) return nullptr; algorithm = WebCryptoKeyAlgorithm::createWithoutParams(id); break; } } if (algorithm.isNull()) return nullptr; // Read key usages. uint32_t rawUsages; WebCryptoKeyUsageMask usages; bool extractable; if (!readUint32(&rawUsages) || !keyUsagesFromWireFormat(rawUsages, &usages, &extractable)) return nullptr; // Read key data. uint32_t keyDataLength; const void* keyData; if (!readUint32(&keyDataLength) || !readRawBytes(keyDataLength, &keyData)) return nullptr; WebCryptoKey key = WebCryptoKey::createNull(); if (!Platform::current()->crypto()->deserializeKeyForClone( algorithm, keyType, extractable, usages, reinterpret_cast<const unsigned char*>(keyData), keyDataLength, key)) return nullptr; return CryptoKey::create(key); }