Ejemplo n.º 1
0
void ZlibTest::ZlibCompressFileContent(const char *inConverterName)
{
	bool isOk;
	std::string fileName = mPath + "utf8_demo.xml";

	UnicodeString uStr;
	isOk = File2UnicodeString(uStr, fileName.c_str());
	CPPUNIT_ASSERT(isOk);

	std::string str;
	isOk = ConvertUnicodeString2String(str, uStr, inConverterName);
	CPPUNIT_ASSERT(isOk);

	BinaryBuffer binaryBuffer;
	isOk = ZlibCompress(binaryBuffer, str.c_str(), str.length());
	CPPUNIT_ASSERT(isOk);

	unsigned int uncomprLen = (unsigned int)str.length();
	char *uncompr = NULL;
	isOk = ZlibDeflate(uncompr, uncomprLen, binaryBuffer.getBuffer(), binaryBuffer.getOccupancy());
	CPPUNIT_ASSERT(isOk);
	CPPUNIT_ASSERT_MESSAGE("Zlib: Bad uncompress", strncmp(uncompr, str.c_str(), uncomprLen) == 0);
	::free(uncompr);

	UnicodeString uStr2;
	isOk = ConvertString2UnicodeString(uStr2, str, inConverterName);
	CPPUNIT_ASSERT(isOk);

	CPPUNIT_ASSERT_MESSAGE("UnicodeString: Bad compression", uStr2 == uStr);
}
Ejemplo n.º 2
0
bool SerializeAttachment(Grid& grid, TAttachment& attachment,
						 typename geometry_traits<TElem>::iterator iterBegin,
						 typename geometry_traits<TElem>::iterator iterEnd,
						 BinaryBuffer& out)
{
	if(!grid.has_attachment<TElem>(attachment))
		return false;

//	copy data
	Grid::AttachmentAccessor<TElem, TAttachment> aa(grid, attachment);
	typedef typename TAttachment::ValueType ValueType;
	
//	write a magic number at the beginning and at the end.
	int magicNumber = 8304548;
	out.write((char*)&magicNumber, sizeof(int));

//TODO: remove the following test code.
//	test: write a number-value to check whether it is send correctly
/*
	number tNum = 1247.001234;
	out.write((char*)&tNum, sizeof(number));
*/	
	for(; iterBegin != iterEnd; ++iterBegin)
	{
		out.write((char*)&aa[*iterBegin], sizeof(ValueType));
	}
	out.write((char*)&magicNumber, sizeof(int));

	return true;
}
Ejemplo n.º 3
0
bool Request::respondRaw(const BinaryBuffer& buffer) {
  BinaryBuffer response;
  if (!channelReady_) {
    /* we must transmit our packet fingerprint so that the recipient can locate the start of our response */
    response = fingerprint_;
    channelReady_ = true;
  }
  response.append(buffer);
  cipher_.encrypt(response.begin, response.length);
  /* write the data on the wire */
  std::list<boost::asio::const_buffer> buffers;
  buffers.push_back(boost::asio::buffer(response.begin, response.length));
  libtorrent::error_code errorCode;
  socket_->write_some(buffers, errorCode);
  return !errorCode;
}
Ejemplo n.º 4
0
 void                  load(BinaryBuffer& bb, T* x, size_t n)
 {
   if (!detail::is_default< Serialization<T> >::value)
     for (size_t i = 0; i < n; ++i)
       diy::load(bb, x[i]);
   else      // if Serialization is not specialized for U, just load the binary data
     bb.load_binary((char*) &x[0], sizeof(T)*n);
 }
Ejemplo n.º 5
0
 void                  save(BinaryBuffer& bb, const T* x, size_t n)
 {
   if (!detail::is_default< Serialization<T> >::value)
     for (size_t i = 0; i < n; ++i)
       diy::save(bb, x[i]);
   else        // if Serialization is not specialized for U, just save the binary data
     bb.save_binary((const char*) &x[0], sizeof(T)*n);
 }
Ejemplo n.º 6
0
bool DeserializeAttachment(Grid& grid, TAttachment& attachment,
						 typename geometry_traits<TElem>::iterator iterBegin,
						 typename geometry_traits<TElem>::iterator iterEnd,
						 BinaryBuffer& in)
{
	if(!grid.has_attachment<TElem>(attachment))
		grid.attach_to<TElem>(attachment);

//	copy data
	Grid::AttachmentAccessor<TElem, TAttachment> aa(grid, attachment);
	typedef typename TAttachment::ValueType ValueType;
	
//	compare with the magic number

	int magicNumber = 8304548;
	int tInt;
	in.read((char*)&tInt, sizeof(int));

	if(tInt != magicNumber){
		UG_LOG("  WARNING: magic-number mismatch before read in DeserializeAttachment. Data-salad possible!\n");
		return false;
	}

//TODO: remove the following test code.
//	test: write a number-value to check whether it is send correctly
/*
	number tNum;
	in.read((char*)&tNum, sizeof(number));
	if(tNum != 1247.001234){
		UG_LOG("TEST-NUMBER TRANSMIT FAILED in DeserializeAttachment!\n");
		return false;
	}
*/
	for(; iterBegin != iterEnd; ++iterBegin)
	{
		in.read((char*)&aa[*iterBegin], sizeof(ValueType));
	}
	in.read((char*)&tInt, sizeof(int));

	if(tInt != magicNumber){
		UG_LOG("  WARNING: magic-number mismatch after read in DeserializeAttachment. Data-salad possible!\n");
		return false;
	}

	return true;
}
Ejemplo n.º 7
0
void ZlibTest::testZlibCompress()
{
	bool isOk;
	const char hello[] = "hello, hello!";
	unsigned int uncomprLen = (unsigned int)strlen(hello) + 1;

	BinaryBuffer binaryBuffer;
	isOk = ZlibCompress(binaryBuffer, hello, uncomprLen);
	CPPUNIT_ASSERT(isOk);

	char *uncompr = NULL;
	isOk = ZlibDeflate(uncompr, uncomprLen, binaryBuffer.getBuffer(), binaryBuffer.getOccupancy());
	CPPUNIT_ASSERT(isOk);

	CPPUNIT_ASSERT_MESSAGE("Zlib: Bad uncompress", strcmp(uncompr, hello) == 0);

	::free(uncompr);
}
Ejemplo n.º 8
0
void Request::handleRequest(boost::shared_ptr<libtorrent::socket_type>& socket, const libtorrent::rc4_handler& cipher) {
  if (!packet_ || packet_->expired()) {
    socket->close();
    onRequestComplete();
    return;
  }
  
  if (!!socket_) {
    /* we are currently handling exactly the same request for another peer */
    socket->close(); /* note that we are closing the second connection only */
    return;
  }

  socket_ = socket;
  cipher_ = cipher;
  channelReady_ = false;

  if (state_ == StateInitializing) {
    state_ = initializeState(socket);
  }

  if (state_ == StateArpUnresolved) {
    respond(BinaryBuffer());
    closeSocket();
    return;
  }

  if (state_ == StateArpResolved) {
    BinaryBuffer response = arpTable_.query(packet_->payload, externalEndpoint_);
    if (!response.empty()) {
      printf("Responding to an ARP request.\n");
      respond(response);
    }
    closeSocket();
    return;
  }

  if (state_ == StateComplete) {
    onRequestComplete();
    return;
  }
}
Ejemplo n.º 9
0
void EntryTest::testEntrySet(void)
{
	Entry e;
	EntrySet set1, set2;

	e.Set(1, 0, 0);
	set1.insert(e);
	e.Set(111, 222, 333);
	set1.insert(e);
	e.Set(DEF_IntMax, DEF_LongMax, DEF_LongMax);
	set1.insert(e);

	// Binary storage into flat buffer
	size_t dataSize;
	BinaryBuffer buffer;
	size_t nb1 = EntrySetToBuf(buffer, dataSize, set1);
	size_t nb2 = BufToEntrySet(set2, buffer.getBuffer(), buffer.getOccupancy(), NULL);
	size_t nb3 = GetBufEntrySetCount(buffer.getBuffer());

	// Check the size of the sets
  CPPUNIT_ASSERT(nb1 == nb2);
  CPPUNIT_ASSERT(set1.size() == nb1);
  CPPUNIT_ASSERT(set2.size() == nb2);
  CPPUNIT_ASSERT(nb2 == nb3);

	// Check the content of the sets
	Entry e1, e2;
	EntrySet::iterator it1, it2;	// Set iterator
	for (it1 = set1.begin(), it2 = set2.begin(); it1 != set1.end(); ++it1, ++it2) {
		e1 = (*it1);
		e2 = (*it2);
		CPPUNIT_ASSERT(e1 == e2);
	}

	// Check the binary storage of an empty set
	EntrySet set;
	EntrySetToBuf(buffer, dataSize, set);
	size_t nb = BufToEntrySet(set, buffer.getBuffer(), buffer.getOccupancy(), NULL);
  CPPUNIT_ASSERT(nb == 0);
}
Ejemplo n.º 10
0
Request::State Request::initializeState(boost::shared_ptr<libtorrent::socket_type>& socket) {
  if (packet_->hopCount) {
    if (!respond(BinaryBuffer())) {
      return StateComplete;
    }
    printf("Setting up a tunnel.\n");
    routePacket();
    return StateTunnelling;
  }

  if (packet_->command == ArpRequest) {
    printf("Got an ARP request.\n");
    BinaryBuffer response = arpTable_.query(packet_->payload, externalEndpoint_);
    if (!response.empty()) {
      return StateArpResolved;
    } else {
      broadcastArpRequest();
      return StateArpUnresolved;
    }
  }

  if (packet_->command == PingRequest) {
    printf("Got a ping request.\n");
    respond(BinaryBuffer());
    return StateComplete;
  }
  
  if (packet_->command == KeyRequest) {
    printf("Got a key request.\n");
    boost::shared_ptr<Persistence::State> state = Persistence::lock();
    BinaryBuffer ourDigest = state->rsaSysDigest;
    Persistence::unlock(false);
    respond(ourDigest);
    return StateComplete;
  }
  
  /* unknown type? just shutdown */
  return StateComplete;
}
Ejemplo n.º 11
0
 void                  load_back(BinaryBuffer& bb, T& x)           { bb.load_binary_back((char*) &x, sizeof(T)); }
Ejemplo n.º 12
0
 static void         load(BinaryBuffer& bb, T& x)                { bb.load_binary((char*)        &x, sizeof(T)); }
Ejemplo n.º 13
0
 static void         save(BinaryBuffer& bb, const T& x)          { bb.save_binary((const char*)  &x, sizeof(T)); }
Ejemplo n.º 14
0
void ZlibTest::testZlibCompressBinary()
{
	bool isOk;
	unsigned long length;
	unsigned long num, num1 = 0, num2 = 1640;
	std::string str, str1, str2, str3("hello");
	BinaryBuffer compressBuffer;

	unsigned long contentSize = 5 * DEF_SizeOfLong + str1.length() + str2.length() + str3.length();
	CPPUNIT_ASSERT_MESSAGE("Bad binary buffer size", contentSize == 25);

	{
		size_t lgSize = DEF_SizeOfLong + DEF_SizeOfLong;
		BinaryBuffer binaryBuffer;
		char *buf = (char *)::malloc(lgSize);
		char *pos = buf;

		// Store some numbers
		SetLgBuf(pos, num1);
		pos += DEF_SizeOfLong;
		SetLgBuf(pos, num2);
		pos += DEF_SizeOfLong;
		binaryBuffer.write(buf, lgSize);

		// Store a string
		length = (unsigned long)str1.length();
		SetLgBuf(buf, length);
		binaryBuffer.write(buf, DEF_SizeOfLong);
		binaryBuffer.write(str1.c_str(), length);

		// Store a string
		length = (unsigned long)str2.length();
		SetLgBuf(buf, length);
		binaryBuffer.write(buf, DEF_SizeOfLong);
		binaryBuffer.write(str2.c_str(), length);

		// Store a string
		length = (unsigned long)str3.length();
		SetLgBuf(buf, length);
		binaryBuffer.write(buf, DEF_SizeOfLong);
		binaryBuffer.write(str3.c_str(), length);

		// Zlib compression
		length = (unsigned long)binaryBuffer.getOccupancy();
		CPPUNIT_ASSERT_MESSAGE("Bad binary buffer occupancy", length == contentSize);
		SetLgBuf(buf, length);
		// Reset binary buffer
		compressBuffer.reset();
		compressBuffer.write(buf, DEF_SizeOfLong);
		isOk = ZlibCompress(compressBuffer, (const char *)binaryBuffer.getBuffer(), length);
		CPPUNIT_ASSERT_MESSAGE("Bad zlib binary compression", isOk);

		::free(buf);
	}

	{
		// Zlib deflate
		const char *pos = static_cast<const char*>(compressBuffer.getBuffer());
		size_t size = compressBuffer.getOccupancy();
		CPPUNIT_ASSERT_MESSAGE("Bad compress buffer occupancy", size == 26);
		length = GetLgBuf(pos);
		pos += DEF_SizeOfLong;
		CPPUNIT_ASSERT_MESSAGE("Bad binary buffer occupancy", length == contentSize);
		char *uncompr = NULL;
		isOk = ZlibDeflate(uncompr, length, (void *)pos, size - DEF_SizeOfLong);
		CPPUNIT_ASSERT_MESSAGE("Bad zlib binary delate", isOk);
		pos = uncompr;

		// Get the numbers
		num = GetLgBuf(pos);
		pos += DEF_SizeOfLong;
		CPPUNIT_ASSERT_MESSAGE("Bad number 1", num == num1);
		num = GetLgBuf(pos);
		pos += DEF_SizeOfLong;
		CPPUNIT_ASSERT_MESSAGE("Bad number 2", num == num2);

		// Get string
		length = GetLgBuf(pos);
		pos += DEF_SizeOfLong;
		str.assign(pos, length);
		pos += length;
		CPPUNIT_ASSERT_MESSAGE("Bad string 1", str == str1);

		// Get string
		length = GetLgBuf(pos);
		pos += DEF_SizeOfLong;
		str.assign(pos, length);
		pos += length;
		CPPUNIT_ASSERT_MESSAGE("Bad string 2", str == str2);

		// Get string
		length = GetLgBuf(pos);
		pos += DEF_SizeOfLong;
		str.assign(pos, length);
		pos += length;
		CPPUNIT_ASSERT_MESSAGE("Bad string 3", str == str3);

		::free(uncompr);
	}
}