Ejemplo n.º 1
0
HRESULT Read(SOCKET socket, MemoryBuffer& buffer)
{
	const size_t position = buffer.GetPosition();
	const size_t size = buffer.GetSize();

	int length = static_cast<int>(size - position);

	char* data = reinterpret_cast<char*>(buffer.GetData()) + position;

	const int status = recv(socket, data, length, 0);

	if (status <= 0)
	{
		const DWORD code = WSAGetLastError();
		const HRESULT result = (WSAEWOULDBLOCK == code) ? S_FALSE : E_FAIL;
		return result;
	}
	length -= status;
	buffer.SetPosition(position + status);

	if (length > 0)
	{
		return S_FALSE;
	}
	return S_OK;
}
Ejemplo n.º 2
0
	void TestAutoUTFOutputStream(UTFType type, bool putBOM, const char *expectedFilename) {
		// Test FileWriteStream
		{
			char filename[L_tmpnam];
			TempFilename(filename);

			FILE *fp = fopen(filename, "wb");
			char buffer[16];
			FileWriteStream os(fp, buffer, sizeof(buffer));
			AutoUTFOutputStream<unsigned, FileWriteStream> eos(os, type, putBOM);
			StringStream s(json_);
			while (s.Peek() != '\0') {
				bool success = Transcoder<UTF8<>, AutoUTF<unsigned> >::Transcode(s, eos);
				EXPECT_TRUE(success);
			}
			eos.Flush();
			fclose(fp);
			EXPECT_TRUE(CompareFile(filename, expectedFilename));
			remove(filename);
		}

		// Test MemoryBuffer
		{
			MemoryBuffer mb;
			AutoUTFOutputStream<unsigned, MemoryBuffer> eos(mb, type, putBOM);
			StringStream s(json_);
			while (s.Peek() != '\0') {
				bool success = Transcoder<UTF8<>, AutoUTF<unsigned> >::Transcode(s, eos);
				EXPECT_TRUE(success);
			}
			eos.Flush();
			EXPECT_TRUE(CompareBufferFile(mb.GetBuffer(), mb.GetSize(), expectedFilename));
		}
	}
Ejemplo n.º 3
0
HRESULT CLrpStClientImpl::Invoke(MemoryBuffer& buffer)
{
	uint32 size = static_cast<uint32>(buffer.GetSize());
	if (size < sizeof(uint32))
	{
		throw runtime_error("Input buffer is too small");
	}
	buffer.SetPosition(0);
	WriteInt32(size - 4, buffer);

	buffer.SetPosition(12);

	uint16 componentId = ReadUInt16(buffer);
	uint16 methodId = ReadUInt16(buffer);
	Translate(componentId, methodId);

	buffer.SetPosition(12);
	WriteUInt16(componentId, buffer);
	WriteUInt16(methodId, buffer);

	CTimeout timeout(m_operationTimeoutInMs);

	if (!SendEx(m_socket, timeout, buffer.GetData(), buffer.GetSize()))
	{
		closesocket(m_socket);
		m_socket = INVALID_SOCKET;
		throw runtime_error("Couldn't send data");
	}
	if (!ReceiveEx(m_socket, timeout, buffer))
	{
		closesocket(m_socket);
		m_socket = INVALID_SOCKET;
		throw runtime_error("Couldn't receive data");
	}
	buffer.SetPosition(12);
	const HRESULT result = ReadInt32(buffer);
	return result;
}
Ejemplo n.º 4
0
HRESULT Write(SOCKET socket, MemoryBuffer& buffer)
{
	const size_t position = buffer.GetPosition();
	const size_t size = buffer.GetSize();

	int length = static_cast<int>(size - position);

	const char* data = reinterpret_cast<const char*>(buffer.GetData()) + position;

	const int status = send(socket, data, length, 0);

	if (status < 0)
	{
		return E_FAIL;
	}
	length -= status;
	buffer.SetPosition(position + status);

	if (length > 0)
	{
		return S_FALSE;
	}
	return S_OK;
}
Ejemplo n.º 5
0
TEST(Writer, Transcode)
{ const char json[] = "{\"hello\":\"world\",\"t\":true,\"f\":false,\"n\":null,\"i\":123,\"pi\":3.1416,\"a\":[1,2,3],\"dollar\":\"\x24\",\"cents\":\"\xC2\xA2\",\"euro\":\"\xE2\x82\xAC\",\"gclef\":\"\xF0\x9D\x84\x9E\"}";

  // UTF8 -> UTF16 -> UTF8
  TestTranscode<UTF8<> >(json);

  // UTF8 -> ASCII -> UTF8
  TestTranscode<ASCII<> >(json);

  // UTF8 -> UTF16 -> UTF8
  TestTranscode<UTF16<> >(json);

  // UTF8 -> UTF32 -> UTF8
  TestTranscode<UTF32<> >(json);

  // UTF8 -> AutoUTF -> UTF8
  UTFType types[] = { kUTF8, kUTF16LE , kUTF16BE, kUTF32LE , kUTF32BE };
  for (size_t i = 0; i < 5; i++)
  { StringStream s(json);
    MemoryBuffer buffer;
    AutoUTFOutputStream<unsigned, MemoryBuffer> os(buffer, types[i], true);
    Writer<AutoUTFOutputStream<unsigned, MemoryBuffer>, UTF8<>, AutoUTF<unsigned> > writer(os);
    Reader reader;
    reader.Parse(s, writer);

    StringBuffer buffer2;
    Writer<StringBuffer> writer2(buffer2);
    GenericReader<AutoUTF<unsigned>, UTF8<> > reader2;
    MemoryStream s2(buffer.GetBuffer(), buffer.GetSize());
    AutoUTFInputStream<unsigned, MemoryStream> is(s2);
    reader2.Parse(is, writer2);

    EXPECT_STREQ(json, buffer2.GetString());
  }

}
bool StandardEncryption::EncryptBuffer (MemoryBuffer *memSource, PCHAR szPassword, bool bEncrypt)
{
	HCRYPTPROV hCryptProv; 
	HCRYPTHASH hHash; 
	HCRYPTKEY hKey;
	DWORD dwBufferlen;
	DWORD dwBufsize;
	MemoryBuffer memOutput;

	// Get the handle to the default provider. 
	if(CryptAcquireContext(&hCryptProv, NULL, NULL, PROVIDER , 0)) {
	   //printf("A cryptographic provider has been acquired. \n");
	} else {
	   MyHandleError("Error during CryptAcquireContext!"); 
	   return false;
	}

	if(!szPassword ) { 
		return false;
	} else { 
		// Create a hash object. 
		if(CryptCreateHash(hCryptProv, CALG_MD5, 0, 0, &hHash)) {
			//printf("A hash object has been created. \n");
		} else { 
			 MyHandleError("Error during CryptCreateHash!");
			 return false;
		}

		//-------------------------------------------------------------------
		// Hash the password.
		if(CryptHashData(hHash, (BYTE *)szPassword, strlen(szPassword), 0)) {
			//printf("The password has been added to the hash. \n");
		} else {
			MyHandleError("Error during CryptHashData."); 
			return false;
		}

		//-------------------------------------------------------------------
		// Derive a session key from the hash object. 
		if(CryptDeriveKey(hCryptProv, m_Currentalg, hHash, m_dwKeylength, &hKey)) {
			//printf("An encryption key is derived from the password hash. \n"); 
		} else {
			MyHandleError("Error during CryptDeriveKey!");
			return false;
		}
		//-------------------------------------------------------------------
		// Destroy hash object. 
		if(hHash) {
			if(!(CryptDestroyHash(hHash))) {
			   MyHandleError("Error during CryptDestroyHash"); 
			   return false;
			}
			hHash = 0;
		}

		// Encrypt / Decrypt data.
		if (bEncrypt == true) {
			// First get the size of the buffer needed.
			dwBufferlen = memSource->GetSize ();
			dwBufsize = memSource->GetSize ();

			CryptEncrypt (hKey, 0, TRUE, 0, NULL, &dwBufferlen, dwBufsize);

			if (dwBufferlen > 0) {
				dwBufsize = dwBufferlen;
				memOutput.SetSize (dwBufferlen);
				memOutput.Write (memSource->GetBuffer (), 0, memSource->GetSize ());

				if (!CryptEncrypt (hKey, 0, FALSE, 0, (BYTE *) memOutput.GetBuffer (), &dwBufferlen, dwBufsize)) {
					MyHandleError ("Error during Encrypt.");
					return false;
				} else {
					memSource->Clear ();
					memSource->SetSize (memOutput.GetSize ());
					memSource->Write (memOutput.GetBuffer (), 0, memOutput.GetSize ());
					memOutput.Clear ();
				}
			} else {
				OutputText ("Unable to obtain encrypted buffer size.");
				return false;
			}		
		} else {
			dwBufferlen = memSource->GetSize ();

			memOutput.SetSize (dwBufferlen);
			memOutput.Write (memSource->GetBuffer (), 0, memSource->GetSize ());

			if (!CryptDecrypt (hKey, 0, FALSE, 0, (BYTE *) memOutput.GetBuffer (), &dwBufferlen)) {
				MyHandleError ("Error during Decrypt.");
				return false;
			} else {
				memSource->Clear ();
				memSource->SetSize (dwBufferlen);
				memSource->Write (memOutput.GetBuffer (), 0, dwBufferlen);
				memOutput.Clear ();
			}
		}

		//-------------------------------------------------------------------
		// Destroy the session key. 
		if(hKey)
		{
			if(!(CryptDestroyKey(hKey))) {
				MyHandleError("Error during CryptDestroyKey");
				return false;
			}
				
		}

		//-------------------------------------------------------------------
		// Release the provider handle. 

		if(hCryptProv)
		{
			if(!(CryptReleaseContext(hCryptProv, 0))) {
				MyHandleError("Error during CryptReleaseContext");
				return false;
			}
				
		}
			return true;
		}
}