public void Adler_Data() { AdlerChecksum adler = new AdlerChecksum(1); byte[] data = { 1, 2, 3, 4, 5, 6, 7 }; adler.Update(data); Assert.AreEqual(0x5b001d, adler.Value); adler = new AdlerChecksum(); adler.Update("penguin"); Assert.AreEqual(0x0bcf02f6, adler.Value); adler = new AdlerChecksum(1); adler.Update("penguin"); Assert.AreEqual(0x0bd602f7, adler.Value); }
public void Adler_as_shortener() { var ac = new AdlerChecksum(); var url = "http://www.google.com"; var shortchecksum = ac.AdlerString(url); Assert.IsNotNull(shortchecksum, "AdlerString should not be null"); }
public void Repeated_Adler_is_identical() { var ac = new AdlerChecksum(); var url = "http://www.google.com"; var chk1 = ac.AdlerString(url); var chk2 = ac.AdlerString(url); Assert.AreEqual(chk1, chk2, "Multiple checksums are identical for same input string"); }
public void Adler_Instantiation_no_exception() { try { var ac = new AdlerChecksum(); } catch (Exception ex) { Assert.Fail($"Expected no exception but got: {ex.ToString()}"); } }
public void Adler_Null() { AdlerChecksum adler = new AdlerChecksum(); Assert.AreEqual(0, adler.Value); adler = new AdlerChecksum(1); Assert.AreEqual(1, adler.Value); adler = new AdlerChecksum(556); Assert.AreEqual(556, adler.Value); }
/// <summary> /// Compress bytes using zlib compression /// </summary> /// <param name="decompressedBytes"></param> /// <param name="compressedBytes"></param> public static byte[] CompressZLib(byte[] decompressedBytes, out int compressedBytes) { // Calculate the adler32 checksum of the uncompressed data, that will go at the end of the compressed data, per the zlib spec. var adler32 = AdlerChecksum.MakeForBuff(decompressedBytes) ?? 0; // Get the bytes of the adler32, making sure they are network order/big-endian var adler32Bytes = BitConverter.GetBytes(adler32); if (BitConverter.IsLittleEndian) { Array.Reverse(adler32Bytes); } using (var msCompressed = new MemoryStream()) { // We must skip the first two bytes // See http://george.chiramattel.com/blog/2007/09/deflatestream-block-length-does-not-match.html // EAT the zlib headers, the rest is a normal 'deflated stream // https://stackoverflow.com/questions/9050260/what-does-a-zlib-header-look-like // 78 01 - No Compression / low // 78 9C - Default Compression // 78 DA - Best Compression var zlibCompressedBytes = new List <byte> { // Add the zlib header bytes to the data 0x78, 0x9C }; // Compress the data using the deflate algorithm using (var deflater = new DeflateStream(msCompressed, CompressionMode.Compress, true)) { deflater.Write(decompressedBytes, 0, decompressedBytes.Length); } msCompressed.Seek(0, SeekOrigin.Begin); zlibCompressedBytes.AddRange(msCompressed.ToArray()); // add the Adler-32 checksum bytes zlibCompressedBytes.AddRange(adler32Bytes); compressedBytes = zlibCompressedBytes.Count; return(zlibCompressedBytes.ToArray()); } }
internal uint find_CRC(R_MythicPackageData data) { AdlerChecksum crc = new AdlerChecksum(); byte[] tocheck = new byte[BitConverter.GetBytes(data.Flag).Length + BitConverter.GetBytes(data.DataOffset).Length + data.Data.Length]; BitConverter.GetBytes(data.Flag).CopyTo(tocheck, 0); //tocheck position in array BitConverter.GetBytes(data.DataOffset).CopyTo(tocheck, BitConverter.GetBytes(data.Flag).Length); //tocheck position in array data.Data.CopyTo(tocheck, BitConverter.GetBytes(data.Flag).Length + BitConverter.GetBytes(data.DataOffset).Length); if (crc.MakeForBuff(tocheck)) { return(crc.ChecksumValue); } else { return(0); //this will be Error } }
public void Extract() { bool doExtract = false; if (File.Exists("TibiaAPI_Inject.dll")) { byte[] embeddedBytes = Drakenbot.Properties.Resources.Inject; byte[] existingBytes = File.ReadAllBytes("Inject.dll"); if (embeddedBytes.Length == existingBytes.Length) { uint embeddedChecksum = AdlerChecksum.Generate( ref embeddedBytes, 0, embeddedBytes.Length); uint existingChecksum = AdlerChecksum.Generate( ref existingBytes, 0, existingBytes.Length); if (embeddedChecksum != existingChecksum) { doExtract = true; } } else { doExtract = true; } } else { doExtract = true; } if (doExtract) { FileStream fileStream = new FileStream("TibiaAPI_Inject.dll", FileMode.Create); fileStream.Write(Drakenbot.Properties.Resources.Inject, 0, (int)Drakenbot.Properties.Resources.Inject.Length); fileStream.Close(); } }
/// <summary> /// Inserts the Adler-32 checksum. /// </summary> public void InsertAdler32Checksum() { Array.Copy(BitConverter.GetBytes(AdlerChecksum.Calculate(ref _buffer, 6, _length)), 0, _buffer, 2, 4); }
/// <summary> /// Determines whether [has valid Adler-32 checksum]. /// </summary> /// <returns> /// <c>true</c> if [has valid Adler-32 checksum]; otherwise, <c>false</c>. /// </returns> public bool HasValidAdler32Checksum() { return(AdlerChecksum.Calculate(ref _buffer, 6, Length) == ReadAdler32Checksum()); }