DirectoryEntry Next() override { assert(m_find != INVALID_HANDLE_VALUE); DirectoryEntry entry; bool invalidEntry = true; while(invalidEntry && !m_lastError) { //due to the way the FindFirstFile api works, //the first entry will already be loaded by the time we get here. entry = ParseFileInfo(m_ffd, true); Aws::String fileName = Aws::Utils::StringUtils::FromWString(m_ffd.cFileName); if (fileName != ".." && fileName != ".") { AWS_LOGSTREAM_TRACE(FILE_SYSTEM_UTILS_LOG_TAG, "Found entry " << entry.path); invalidEntry = false; } else { entry.fileType = FileType::None; AWS_LOGSTREAM_TRACE(FILE_SYSTEM_UTILS_LOG_TAG, "Skipping . or .. entries."); } if(!FindNextFileW(m_find, &m_ffd)) { m_lastError = GetLastError(); AWS_LOGSTREAM_ERROR(FILE_SYSTEM_UTILS_LOG_TAG, "Could not fetch next entry from " << m_directoryEntry.path << " with error code " << m_lastError); break; } } return entry; }
bool RemoveDirectoryIfExists(const char* path) { AWS_LOGSTREAM_INFO(FILE_SYSTEM_UTILS_LOG_TAG, "Removing directory at " << path); if(RemoveDirectoryW(ToLongPath(Aws::Utils::StringUtils::ToWString(path)).c_str())) { AWS_LOGSTREAM_DEBUG(FILE_SYSTEM_UTILS_LOG_TAG, "The remove operation of file at " << path << " Succeeded."); return true; } else { int errorCode = GetLastError(); if (errorCode == ERROR_DIR_NOT_EMPTY) { AWS_LOGSTREAM_ERROR(FILE_SYSTEM_UTILS_LOG_TAG, "The remove operation of file at " << path << " failed. with error code because it was not empty."); } else if(errorCode == ERROR_DIRECTORY) { AWS_LOGSTREAM_DEBUG(FILE_SYSTEM_UTILS_LOG_TAG, "The deletion of directory at " << path << " failed because it doesn't exist."); return true; } AWS_LOGSTREAM_DEBUG(FILE_SYSTEM_UTILS_LOG_TAG, "The remove operation of file at " << path << " failed. with error code " << errorCode); return false; } }
CryptoBuffer CommonCryptoCipher::DecryptBuffer(const CryptoBuffer& encryptedData) { if (m_failure) { AWS_LOGSTREAM_FATAL(CC_LOG_TAG, "Cipher not properly initialized for decryption. Aborting"); return CryptoBuffer(); } CheckInitDecryptor(); size_t lengthWritten = encryptedData.GetLength() + (GetBlockSizeBytes() - 1); CryptoBuffer decryptedText(static_cast<size_t>(lengthWritten)); CCStatus status = CCCryptorUpdate(m_cryptoHandle, encryptedData.GetUnderlyingData(), encryptedData.GetLength(), decryptedText.GetUnderlyingData(), decryptedText.GetLength(), &lengthWritten); if (status != kCCSuccess) { m_failure = true; AWS_LOGSTREAM_ERROR(CC_LOG_TAG, "Decryption of buffer failed with status code: " << status); return CryptoBuffer(); } if (lengthWritten < decryptedText.GetLength()) { return CryptoBuffer(decryptedText.GetUnderlyingData(), static_cast<size_t>(lengthWritten)); } return decryptedText; }
/** * Generate random number per 4 bytes and use each byte for the byte in the iv */ CryptoBuffer SymmetricCipher::GenerateIV(size_t ivLengthBytes, bool ctrMode) { CryptoBuffer iv(GenerateXRandomBytes(ivLengthBytes, ctrMode)); if(iv.GetLength() == 0) { AWS_LOGSTREAM_ERROR(LOG_TAG, "Unable to generate iv of length " << ivLengthBytes); return iv; } if(ctrMode) { //init the counter size_t length = iv.GetLength(); //[ nonce 1/4] [ iv 1/2 ] [ ctr 1/4 ] size_t ctrStart = (length / 2) + (length / 4); for(; ctrStart < iv.GetLength() - 1; ++ ctrStart) { iv[ctrStart] = 0; } iv[length - 1] = 1; } return iv; }
CryptoBuffer SymmetricCipher::GenerateKey(size_t keyLengthBytes) { CryptoBuffer&& key = GenerateXRandomBytes(keyLengthBytes, false); if(key.GetLength() == 0) { AWS_LOGSTREAM_ERROR(LOG_TAG, "Unable to generate key of length " << keyLengthBytes); } return key; }
void AES_CTR_Cipher_CommonCrypto::InitDecryptor_Internal() { CCCryptorStatus status = CCCryptorCreateWithMode(kCCDecrypt, kCCModeCTR, kCCAlgorithmAES, ccNoPadding, m_initializationVector.GetUnderlyingData(), m_key.GetUnderlyingData(), m_key.GetLength(), nullptr, 0, 0, kCCModeOptionCTR_BE, &m_cryptoHandle); if (status != kCCSuccess) { m_failure = true; AWS_LOGSTREAM_ERROR(CTR_CC_LOG_TAG, "Error while initializing AES 256 CTR in Encryption mode. Status code: " << status); } }
void PulseAudioPCMOutputDriver::InitDevice() { if (!m_driver) { int errorCode = -1; m_driver = pa_simple_new(nullptr, APP_NAME, PA_STREAM_PLAYBACK, NULL, "playback", &m_selectedCaps, nullptr, nullptr, &errorCode); if(!m_driver) { AWS_LOGSTREAM_ERROR(CLASS_NAME, " error initializing device " << pa_strerror(errorCode)); } } }
bool PulseAudioPCMOutputDriver::WriteBufferToDevice(const unsigned char* buffer, size_t size) { InitDevice(); if(m_driver) { int error(-1); if(pa_simple_write(m_driver, buffer, size, &error) < 0) { AWS_LOGSTREAM_ERROR(CLASS_NAME, " error writing buffer to output device " << pa_strerror(error)); return false; } return true; } return false; }
User32Directory(const Aws::String& path, const Aws::String& relativePath) : Directory(path, relativePath), m_find(INVALID_HANDLE_VALUE), m_lastError(0) { WIN32_FIND_DATAW ffd; AWS_LOGSTREAM_TRACE(FILE_SYSTEM_UTILS_LOG_TAG, "Entering directory " << m_directoryEntry.path); m_find = FindFirstFileW(ToLongPath(Aws::Utils::StringUtils::ToWString(m_directoryEntry.path.c_str())).c_str(), &ffd); if (m_find != INVALID_HANDLE_VALUE) { m_directoryEntry = ParseFileInfo(ffd, false); FindClose(m_find); auto seachPath = Join(m_directoryEntry.path, "*"); m_find = FindFirstFileW(ToLongPath(Aws::Utils::StringUtils::ToWString(seachPath.c_str())).c_str(), &m_ffd); } else { AWS_LOGSTREAM_ERROR(FILE_SYSTEM_UTILS_LOG_TAG, "Could not load directory " << m_directoryEntry.path << " with error code " << GetLastError()); } }
CryptoBuffer AES_KeyWrap_Cipher_CommonCrypto::FinalizeDecryption() { CheckInitDecryptor(); size_t outputBufferLength = m_workingKeyBuffer.GetLength() - GetBlockSizeBytes(); CryptoBuffer outputBuffer(outputBufferLength); CCCryptorStatus status = CCSymmetricKeyUnwrap(kCCWRAPAES, CCrfc3394_iv, CCrfc3394_ivLen, m_key.GetUnderlyingData(), m_key.GetLength(), m_workingKeyBuffer.GetUnderlyingData(), m_workingKeyBuffer.GetLength(), outputBuffer.GetUnderlyingData(), &outputBufferLength); if(status != kCCSuccess) { m_failure = true; AWS_LOGSTREAM_ERROR(AES_KEY_WRAP_LOG_TAG, "Key unwrap failed with status code " << status); return CryptoBuffer(); } return outputBuffer; }
bool DropPlayerDataTable(const std::unique_ptr<DynamoDBClient>& client) { AWS_LOGSTREAM_INFO(SchemaTag, "Dropping old table: " << ExampleTableName); DeleteTableRequest deleteTableRequest; deleteTableRequest.SetTableName(ExampleTableName); DeleteTableOutcome deleteTableOutcome = client->DeleteTable(deleteTableRequest); if (!deleteTableOutcome.IsSuccess()) { AWSError<DynamoDBErrors> error = deleteTableOutcome.GetError(); AWS_LOGSTREAM_ERROR(SchemaTag, "Failed to drop old table: " << ExampleTableName << ": " << error.GetMessage() << "(" << error.GetExceptionName() << ")"); return false; } WaitOnTableStatus(client, ExampleTableName, TableStatusWaitUntil::GONE); return true; }
CryptoBuffer CommonCryptoCipher::FinalizeDecryption() { if (m_failure) { AWS_LOGSTREAM_FATAL(CC_LOG_TAG, "Cipher not properly initialized for decryption finalization. Aborting"); return CryptoBuffer(); } CryptoBuffer finalBlock(GetBlockSizeBytes()); size_t writtenSize = static_cast<int>(finalBlock.GetLength()); CCStatus status = CCCryptorFinal(m_cryptoHandle, finalBlock.GetUnderlyingData(), finalBlock.GetLength(), &writtenSize); if (status != kCCSuccess) { m_failure = true; AWS_LOGSTREAM_ERROR(CC_LOG_TAG, "Decryption of buffer failed with status code: " << status); return CryptoBuffer(); } return CryptoBuffer(finalBlock.GetUnderlyingData(), writtenSize); }
bool CreatePlayerDataTable(const std::unique_ptr<DynamoDBClient>& client) { AWS_LOGSTREAM_INFO(SchemaTag, "Creating " << ExampleTableName << " table"); // Build the create request CreateTableRequest createTableRequest; AttributeDefinition identityAttribute; identityAttribute.SetAttributeName(IdentityColumnName); identityAttribute.SetAttributeType(ScalarAttributeType::S); KeySchemaElement identityKeySchemaElement; identityKeySchemaElement.WithAttributeName(IdentityColumnName).WithKeyType(KeyType::HASH); ProvisionedThroughput provisionedThroughput; provisionedThroughput.SetReadCapacityUnits(5); provisionedThroughput.SetWriteCapacityUnits(1); createTableRequest.AddAttributeDefinitions(identityAttribute); createTableRequest.AddKeySchema(identityKeySchemaElement); createTableRequest.WithProvisionedThroughput(provisionedThroughput); createTableRequest.WithTableName(ExampleTableName); CreateTableOutcome createTableOutcome = client->CreateTable(createTableRequest); assert(createTableOutcome.IsSuccess() || createTableOutcome.GetError().GetErrorType() == DynamoDBErrors::RESOURCE_IN_USE); if(!createTableOutcome.IsSuccess()) { AWSError<DynamoDBErrors> error = createTableOutcome.GetError(); AWS_LOGSTREAM_ERROR(SchemaTag, "Failed to create table: " << ExampleTableName << ": " << error.GetMessage().c_str() << "(" << error.GetExceptionName() << ")"); return false; } // Wait for table to become active WaitOnTableStatus(client, ExampleTableName, TableStatusWaitUntil::ACTIVE); return true; }
bool CreateDirectoryIfNotExists(const char* path, bool createParentDirs) { Aws::String directoryName = path; AWS_LOGSTREAM_INFO(FILE_SYSTEM_UTILS_LOG_TAG, "Creating directory " << directoryName); // Create intermediate directories or create the target directory once. for (size_t i = createParentDirs ? 0 : directoryName.size() - 1; i < directoryName.size(); i++) { // Create the intermediate directory if we find a delimiter and the delimiter is not the first char, or if this is the target directory. if (i != 0 && (directoryName[i] == FileSystem::PATH_DELIM || i == directoryName.size() - 1)) { // the last delimeter can be removed safely. if (directoryName[i] == FileSystem::PATH_DELIM) { directoryName[i] = '\0'; } if (CreateDirectoryW(ToLongPath(StringUtils::ToWString(directoryName.c_str())).c_str(), nullptr)) { AWS_LOGSTREAM_DEBUG(FILE_SYSTEM_UTILS_LOG_TAG, "Creation of directory " << directoryName.c_str() << " succeeded."); } else { DWORD errorCode = GetLastError(); if (errorCode != ERROR_ALREADY_EXISTS && errorCode != NO_ERROR) // in vs2013 the errorCode is NO_ERROR { AWS_LOGSTREAM_ERROR(FILE_SYSTEM_UTILS_LOG_TAG, " Creation of directory " << directoryName.c_str() << " returned code: " << errorCode); return false; } AWS_LOGSTREAM_DEBUG(FILE_SYSTEM_UTILS_LOG_TAG, " Creation of directory " << directoryName.c_str() << " returned code: " << errorCode); } // Restore the path. We are good even if we didn't change that char to '\0', because we are ready to return. directoryName[i] = FileSystem::PATH_DELIM; } } return true; }