public static int Decompress(byte[] buffer, int length, ref byte[] newbuf, int newbufsize) { var newlen = 0; var flag_offset = 0; newbuf[0] = buffer[0]; if (buffer[0] == 0x00) { flag_offset = 2; newbuf[1] = buffer[1]; } else { flag_offset = 1; } if (length > 2 && buffer[flag_offset] == 0x5a) { // TODO: Decompress is broken I suspect. // This packet is compressed, so decompress (Inflate) it (and add 2 at the end of this long f*****g method call) newlen = SharpZip.InflatePacket(buffer, flag_offset + 1, length - (flag_offset + 1) - 2, newbuf, flag_offset) + 2; newbuf[newlen++] = buffer[length - 2]; newbuf[newlen++] = buffer[length - 1]; // TODO: F****d up for now until I get this under test and refactor. // Take our 2048 byte array and make it fit the data. We're not doing any pointer shit here. var tmpBuff = new byte[newlen]; Buffer.BlockCopy(newbuf, 0, tmpBuff, 0, newlen); // Now overwrite. newbuf = tmpBuff; } else if (length > 2 && buffer[flag_offset] == 0xa5) { // This packet is not compressed. Just remove the compression indicator. Buffer.BlockCopy(buffer, flag_offset + 1, newbuf, flag_offset, length - (flag_offset + 1)); newlen = length - 1; var tmpBuff = new byte[newlen]; Buffer.BlockCopy(newbuf, 0, tmpBuff, 0, newlen); newbuf = tmpBuff; } else { // Nothing to do. // TODO: Copy it? Could just reassign it probably? newbuf = buffer; newlen = length; } return(newlen); }
public static int Compress(byte[] buffer, int length, ref byte[] newbuf, int newbufsize) { int flag_offset = 1; int newlength; newbuf[0] = buffer[0]; if (buffer[0] == 0) { flag_offset = 2; newbuf[1] = buffer[1]; } if (length > 30) { newlength = SharpZip.DeflatePacket(buffer, flag_offset, length - flag_offset, newbuf, flag_offset + 1); newbuf[flag_offset] = 0x5a; newlength += flag_offset + 1; var tmpbuf = new byte[newlength]; Buffer.BlockCopy(newbuf, 0, tmpbuf, 0, newlength); newbuf = tmpbuf; } else { // Not large enough to bother compressing. Inject the Not Compessed indicator into the array. newlength = length + 1; var tmpbuf = new byte[newlength]; // Copy the data before the point where we will insert the indicator. Buffer.BlockCopy(newbuf, 0, tmpbuf, 0, flag_offset); // Add the indicator. tmpbuf[flag_offset] = 0xa5; // Now copy the rest. Buffer.BlockCopy(buffer, flag_offset, tmpbuf, flag_offset + 1, length - flag_offset); newbuf = tmpbuf; } return(newlength); }