Question

What would be the best and simplest way to implement some operation work on byte array when you need to work segment by segment from that array. For example, segment size is 4096 bytes and you need to perform any operation but using segments of 4096 bytes. Return value should again be byte array, and I am looking on some way to avoid alot of copying into new arrays if possible. Also, it must work on .net 3.0

Was it helpful?

Solution

Maybe something along these lines? Your question is pretty generic...

const int SEGMENT_SIZE = 4096;
int segment, i;

f(byte[] data, int offset, int length)
{
  byte[] output = new byte[length];

  // TODO: implement

  return output;
}

segment = 0;
while(segment * SEGMENT_SIZE < buffer.Length)
{
    output = f(buffer, segment * SEGMENT_SIZE, SEGMENT_SIZE);
    segment++;
}

Output without segments:

g(byte[] src, byte[] dst, int srcOffset, int dstOffset, int length)
{
  // TODO: implement process from src[srcOffset + i] to dst[dstOffset + i]

  // no return, has side-effect on dst
}


outputBuffer = new byte[inputBuffer.Length];
segment = 0;
while(segment * SEGMENT_SIZE < inputBuffer.Length)
{
    g(inputBuffer, outputBuffer, segment * SEGMENT_SIZE, segment * SEGMENT_SIZE, SEGMENT_SIZE);
    segment++;
}

AES encryption maintains the size of the blocks, so data will be aligned block-by-block. Pay attention to padding, last segment might be partial. Use the right padding, I've had a hard time to get it working because of a bad padding. Also make sure you can have 4096 byte blocks... IIRC you should use 2 blocks of 256 bytes each. Either way, you'll have to fiddle with values quite a lot! Good luck :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top