From 25b5e3f905fe7fbea40d4aae7bf07e0e6e8f6f8e Mon Sep 17 00:00:00 2001
From: Andrea Zoli <zoli@iasfbo.inaf.it>
Date: Mon, 21 Jul 2014 11:18:49 +0200
Subject: [PATCH] Add ByteStream functions getPaddedCopy() and
 getUnpaddedCopy().

---
 include/ByteStream.h | 14 ++++++++++++++
 src/ByteStream.cpp   | 36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 50 insertions(+)

diff --git a/include/ByteStream.h b/include/ByteStream.h
index 5d4b436..f8de93a 100644
--- a/include/ByteStream.h
+++ b/include/ByteStream.h
@@ -107,6 +107,20 @@ public:
     /// Gets the dimension of the stream
     dword size();
 
+	/// Create a copy of the stream with some additional padding. The stream is divided into chunks
+	/// with numberOfChunks = size() / chunkSize.
+	/// \param chunkSize the size of a chunk without padding.
+	/// \param padSize Size of the padding.
+	/// \return The padded copy of this byte stream.
+	ByteStreamPtr getPaddedCopy(dword chunkSize, dword padSize);
+
+	/// Create a copy of the stream removing some padding. The stream is divided into chunks
+	/// with numberOfChunks = size() / chunkSize.
+	/// \param chunkSize the size of a chunk with padding.
+	/// \param padSize Size of the padding.
+	/// \return The unpadded copy of this byte stream.
+	ByteStreamPtr getUnpaddedCopy(dword chunkSize, dword padSize);
+
     char* printStreamInHexadecimal();
 
     /// Get type of allocations.
diff --git a/src/ByteStream.cpp b/src/ByteStream.cpp
index 95ab024..717a9af 100644
--- a/src/ByteStream.cpp
+++ b/src/ByteStream.cpp
@@ -496,6 +496,42 @@ void PacketLib::ByteStream::deleteStreamMemory()
         delete[] stream;
 }
 
+ByteStreamPtr PacketLib::ByteStream::getPaddedCopy(dword chunkSize, dword padSize)
+{
+	if(byteInTheStream % chunkSize != 0)
+		throw new PacketException("getPadCopy() error. Chunk size must be a divisor of ByteStream::size().");
+
+	dword nChunks = byteInTheStream / chunkSize;
+	dword newChunkSize = chunkSize + padSize;
+
+	ByteStreamPtr sPtr = ByteStreamPtr(new ByteStream(nChunks*(newChunkSize), bigendian));
+	byte* raw = sPtr->getStream();
+
+	for(dword i=0; i<nChunks; i++)
+		memcpy(raw+i*newChunkSize, stream+i*chunkSize, chunkSize);
+
+	return sPtr;
+}
+
+ByteStreamPtr PacketLib::ByteStream::getUnpaddedCopy(dword chunkSize, dword padSize)
+{
+	if(byteInTheStream % chunkSize != 0)
+		throw new PacketException("getUnpadCopy() error. Chunk size must be a divisor of ByteStream::size().");
+	if(chunkSize - padSize <= 0)
+		throw new PacketException("getUnpadCopy() error. Chunk size - pad size gives a value <= 0.");
+
+	dword nChunks = byteInTheStream / chunkSize;
+	dword newChunkSize = chunkSize - padSize;
+
+	ByteStreamPtr sPtr = ByteStreamPtr(new ByteStream(nChunks*(newChunkSize), bigendian));
+	byte* raw = sPtr->getStream();
+
+	for(dword i=0; i<nChunks; i++)
+		memcpy(raw+i*newChunkSize, stream+i*chunkSize, newChunkSize);
+
+	return sPtr;
+}
+
 void PacketLib::ByteStream::swapWord() {
 	dword dim =  byteInTheStream;
 	for(dword i = 0; i< dim; i+=2)
-- 
GitLab