/* val= 3066839698 val= 0 val= 3799812176 t=14446 */ public static void testCRC32() { CRC32 crc1 = new CRC32(); crc1.Update(new byte[] { 1, 2 }); if (crc1.GetValue() != 3066839698) throw new Exception("Bad CRC32!"); Console.WriteLine("Testing CRC32"); Console.WriteLine("val= " + crc1.GetValue()); crc1.Reset(); Console.WriteLine("val= " + crc1.GetValue()); if (crc1.GetValue() != 0) throw new Exception("Bad CRC32!!"); Random r = new Random(); byte[] all = new byte[2000 * 4]; long t0 = Environment.TickCount; for (int n = 0; n < 2000; n++) { byte[] b = createBytes7(n < 50 ? n : n * n - 7); CRC32 crc = new CRC32(); int offset = 0; while (offset < b.Length) { int len = r.Next(b.Length - offset) + 1; crc.Update(b, offset, len); offset += len; } long x = crc.GetValue(); all[n * 4] = (byte)((x >> 24) & 0xff); all[n * 4 + 1] = (byte)((x >> 16) & 0xff); all[n * 4 + 2] = (byte)((x >> 8) & 0xff); all[n * 4 + 3] = (byte)((x) & 0xff); } long t1 = Environment.TickCount; Adler32 a = new Adler32(); a.Update(all); long v = a.GetValue(); Console.WriteLine("val= " + v + " t=" + (t1 - t0)); if (v != 3799812176) throw new Exception("Bad cRC32");// tested with Java CRC32 }
private void EndChunkGoForNext() { // Called after readging the last byte of chunk // Checks CRC, and read ID from next CHUNK // Those values are left in idLastChunk / lenLastChunk // Skips empty IDATS do { int crc = Hjg.Pngcs.PngHelperInternal.ReadInt4(inputStream); // offset += 4; if (checkCrc) { int crccalc = (int)crcEngine.GetValue(); if (lenLastChunk > 0 && crc != crccalc) { throw new PngjBadCrcException("error reading idat; offset: " + offset); } crcEngine.Reset(); } lenLastChunk = Hjg.Pngcs.PngHelperInternal.ReadInt4(inputStream); if (lenLastChunk < 0) { throw new PngjInputException("invalid len for chunk: " + lenLastChunk); } toReadThisChunk = lenLastChunk; Hjg.Pngcs.PngHelperInternal.ReadBytes(inputStream, idLastChunk, 0, 4); offset += 8; ended = !PngCsUtils.arraysEqual4(idLastChunk, Hjg.Pngcs.Chunks.ChunkHelper.b_IDAT); if (!ended) { foundChunksInfo.Add(new PngIDatChunkInputStream.IdatChunkInfo(lenLastChunk, (offset - 8))); if (checkCrc) { crcEngine.Update(idLastChunk, 0, 4); } } // PngHelper.logdebug("IDAT ended. next len= " + lenLastChunk + " idat?" + // (!ended)); } while (lenLastChunk == 0 && !ended); // rarely condition is true (empty IDAT ??) }