void Socket::IOControl(DWORD dwIoControlCode, const BufferPtr &inBuffer, const BufferPtr &outBuffer) { DWORD bytesReturned; if (0 != WSAIoctl(hSocket, dwIoControlCode, inBuffer->Pointer(), inBuffer->Size(), outBuffer->Pointer(), outBuffer->Size(), &bytesReturned, 0, 0)) { throw Win32Exception("WSAIoCtl"); } outBuffer->Resize(bytesReturned); }
uint64 File::ReadAt (const BufferPtr &buffer, uint64 position) const { if_debug (ValidateState()); #ifdef TC_TRACE_FILE_OPERATIONS TraceFileOperation (FileHandle, Path, false, buffer.Size(), position); #endif ssize_t bytesRead = pread (FileHandle, buffer, buffer.Size(), position); throw_sys_sub_if (bytesRead == -1, wstring (Path)); return bytesRead; }
void VolumeHeader::EncryptNew (const BufferPtr &newHeaderBuffer, const ConstBufferPtr &newSalt, const ConstBufferPtr &newHeaderKey, shared_ptr <Pkcs5Kdf> newPkcs5Kdf) { if (newHeaderBuffer.Size() != HeaderSize || newSalt.Size() != SaltSize) throw ParameterIncorrect (SRC_POS); shared_ptr <EncryptionMode> mode = EA->GetMode()->GetNew(); shared_ptr <EncryptionAlgorithm> ea = EA->GetNew(); if (typeid (*mode) == typeid (EncryptionModeXTS)) { mode->SetKey (newHeaderKey.GetRange (EA->GetKeySize(), EA->GetKeySize())); ea->SetKey (newHeaderKey.GetRange (0, ea->GetKeySize())); } else { mode->SetKey (newHeaderKey.GetRange (0, mode->GetKeySize())); ea->SetKey (newHeaderKey.GetRange (LegacyEncryptionModeKeyAreaSize, ea->GetKeySize())); } ea->SetMode (mode); newHeaderBuffer.CopyFrom (newSalt); BufferPtr headerData = newHeaderBuffer.GetRange (EncryptedHeaderDataOffset, EncryptedHeaderDataSize); Serialize (headerData); ea->Encrypt (headerData); if (newPkcs5Kdf) Pkcs5 = newPkcs5Kdf; }
void VolumeHeader::SerializeEntry (const T &entry, const BufferPtr &header, size_t &offset) const { offset += sizeof (T); if (offset > header.Size()) throw ParameterIncorrect (SRC_POS); *reinterpret_cast<T *> (header.Get() + offset - sizeof (T)) = Endian::Big (entry); }
void Serializer::Deserialize (const string &name, const BufferPtr &data) { ValidateName (name); uint64 size = Deserialize <uint64> (); if (data.Size() != size) throw ParameterIncorrect (SRC_POS); DataStream->ReadCompleteBuffer (data); }
void EncryptionTestDialog::SetTextCtrlData (wxTextCtrl *textCtrl, const BufferPtr &data) { wstring str; for (size_t i = 0; i < data.Size(); i++) { char strBuf[3]; sprintf (strBuf, "%02x", (int) data[i]); str += StringConverter::ToWide (strBuf); } textCtrl->SetValue (str); }
uint64 MemoryStream::Read (const BufferPtr &buffer) { if (Data.size() == 0) throw ParameterIncorrect (SRC_POS); ConstBufferPtr streamBuf (*this); size_t len = buffer.Size(); if (streamBuf.Size() - ReadPosition < len) len = streamBuf.Size() - ReadPosition; BufferPtr(buffer).CopyFrom (streamBuf.GetRange (ReadPosition, len)); ReadPosition += len; return len; }
void File::ReadCompleteBuffer (const BufferPtr &buffer) const { size_t dataLeft = buffer.Size(); size_t offset = 0; while (dataLeft > 0) { size_t dataRead = static_cast <size_t> (Read (buffer.GetRange (offset, dataLeft))); if (dataRead == 0) throw InsufficientData (SRC_POS); dataLeft -= dataRead; offset += dataRead; } }
void VolumeHeader::Serialize (const BufferPtr &header) const { if (header.Size() != EncryptedHeaderDataSize) throw ParameterIncorrect (SRC_POS); header.Zero(); header[0] = 'V'; header[1] = 'E'; header[2] = 'R'; header[3] = 'A'; size_t offset = 4; header.GetRange (DataAreaKeyOffset, DataAreaKey.Size()).CopyFrom (DataAreaKey); uint16 headerVersion = CurrentHeaderVersion; SerializeEntry (headerVersion, header, offset); SerializeEntry (RequiredMinProgramVersion, header, offset); SerializeEntry (Crc32::ProcessBuffer (header.GetRange (DataAreaKeyOffset, DataKeyAreaMaxSize)), header, offset); uint64 reserved64 = 0; SerializeEntry (reserved64, header, offset); SerializeEntry (reserved64, header, offset); SerializeEntry (HiddenVolumeDataSize, header, offset); SerializeEntry (VolumeDataSize, header, offset); SerializeEntry (EncryptedAreaStart, header, offset); SerializeEntry (EncryptedAreaLength, header, offset); SerializeEntry (Flags, header, offset); if (SectorSize < TC_MIN_VOLUME_SECTOR_SIZE || SectorSize > TC_MAX_VOLUME_SECTOR_SIZE || SectorSize % ENCRYPTION_DATA_UNIT_SIZE != 0) { throw ParameterIncorrect (SRC_POS); } SerializeEntry (SectorSize, header, offset); offset = TC_HEADER_OFFSET_HEADER_CRC - TC_HEADER_OFFSET_MAGIC; SerializeEntry (Crc32::ProcessBuffer (header.GetRange (0, TC_HEADER_OFFSET_HEADER_CRC - TC_HEADER_OFFSET_MAGIC)), header, offset); }
AsyncResultPtr Socket::BeginSend(const BufferPtr &buffer, AsyncCallback callback, const ObjectPtr &asyncState) { AsyncResultPtr asyncResult(this, buffer, asyncState, Nothing, callback); asyncResult->AddRef(); WSABUF wsabuf; wsabuf.buf = (char *)buffer->Pointer(); wsabuf.len = buffer->Size(); DWORD flags = 0; if (0 != WSASend(hSocket, &wsabuf, 1, 0, flags, asyncResult.operator ->(), 0) && WSAGetLastError() != WSA_IO_PENDING) { asyncResult->Release(); throw Win32Exception("WSASend"); } return asyncResult; }
void EncryptionAlgorithm::Encrypt (const BufferPtr &data) const { Encrypt (data, data.Size()); }
void Hash::ValidateDigestParameters (const BufferPtr &buffer) const { if (buffer.Size() != GetDigestSize ()) throw ParameterIncorrect (SRC_POS); }
void MemoryStream::ReadCompleteBuffer (const BufferPtr &buffer) { if (Read (buffer) != buffer.Size()) throw InsufficientData (SRC_POS); }