public void UpdateByteTest() { Adler32 checksum = new Adler32(); foreach (byte d in LicenseData) checksum.Update(d); Assert.AreEqual(LicenseChecksum, checksum.Value); }
public void MixedUpdateTest() { Adler32 checksum = new Adler32(); int i = 0; checksum.Reset(); while (i < LicenseData.Length / 4) checksum.Update(LicenseData[i++]); for (int j = 0; j < 2; j++) { checksum.Update(LicenseData, i, LicenseData.Length / 4); i += LicenseData.Length / 4; } while (i < LicenseData.Length) checksum.Update(LicenseData[i++]); Assert.AreEqual(LicenseChecksum, checksum.Value); }
/// <summary>Calculates the Adler-32 checksum on specified portion of a buffer.</summary> /// <param name="data">Data buffer to perform checksum on.</param> /// <param name="startIndex">Starts index in data buffer to begin checksum.</param> /// <param name="length">Total number of bytes from <paramref name="startIndex">startIndex</paramref> to /// perform checksum over.</param> /// <returns>Computed Adler-32 checksum over the specified portion of the buffer.</returns> public static uint Adler32Checksum(this byte[] data, int startIndex, int length) { Adler32 checksum = new Adler32(); checksum.Update(data, startIndex, length); return checksum.Value; }
// Reads the body of a record from the PQDIF file. private RecordBody ReadRecordBody(int byteSize) { byte[] bytes; Adler32 checksum; if (byteSize == 0) return null; bytes = m_fileReader.ReadBytes(byteSize); checksum = new Adler32(); checksum.Update(bytes); if (m_compressionAlgorithm == CompressionAlgorithm.Zlib && m_compressionStyle != CompressionStyle.None) bytes = ZlibStream.UncompressBuffer(bytes); using (MemoryStream stream = new MemoryStream(bytes)) using (BinaryReader reader = new BinaryReader(stream)) { return new RecordBody() { Collection = ReadCollection(reader), Checksum = checksum.Value }; } }
/// <summary> /// Writes the given record to the PQDIF file. /// </summary> /// <param name="record">The record to be written to the file.</param> /// <param name="lastRecord">Indicates whether this record is the last record in the file.</param> public void WriteRecord(Record record, bool lastRecord = false) { byte[] bodyImage; Adler32 checksum; if (m_disposed) throw new ObjectDisposedException(GetType().Name); using (BlockAllocatedMemoryStream bodyStream = new BlockAllocatedMemoryStream()) using (BinaryWriter bodyWriter = new BinaryWriter(bodyStream)) { // Write the record body to the memory stream if ((object)record.Body != null) WriteCollection(bodyWriter, record.Body.Collection); // Read and compress the body to a byte array bodyImage = bodyStream.ToArray(); if (m_compressionAlgorithm == CompressionAlgorithm.Zlib && m_compressionStyle == CompressionStyle.RecordLevel) bodyImage = ZlibStream.CompressBuffer(bodyImage); // Create the checksum after compression checksum = new Adler32(); checksum.Update(bodyImage); // Write the record body to the memory stream if ((object)record.Body != null) record.Body.Checksum = checksum.Value; } // Fix the pointer to the next // record before writing this record if (m_stream.CanSeek && m_stream.Length > 0) { m_writer.Write((int)m_stream.Length); m_stream.Seek(0L, SeekOrigin.End); } // Make sure the header points to the correct location based on the size of the body record.Header.HeaderSize = 64; record.Header.BodySize = bodyImage.Length; record.Header.NextRecordPosition = (int)m_stream.Length + record.Header.HeaderSize + record.Header.BodySize; record.Header.Checksum = checksum.Value; // Write up to the next record position m_writer.Write(record.Header.RecordSignature.ToByteArray()); m_writer.Write(record.Header.RecordTypeTag.ToByteArray()); m_writer.Write(record.Header.HeaderSize); m_writer.Write(record.Header.BodySize); // The PQDIF standard defines the NextRecordPosition to be 0 for the last record in the file // We treat seekable streams differently because we can go back and fix the pointers later if (m_stream.CanSeek || lastRecord) m_writer.Write(0); else m_writer.Write(record.Header.NextRecordPosition); // Write the rest of the header as well as the body m_writer.Write(record.Header.Checksum); m_writer.Write(record.Header.Reserved); m_writer.Write(bodyImage); // If the stream is seekable, seek to the next record // position so we can fix the pointer if we end up // writing another record to the file if (m_stream.CanSeek) m_stream.Seek(-(24 + record.Header.BodySize), SeekOrigin.Current); // Dispose of the writer if this is the last record if (!m_stream.CanSeek && lastRecord) Dispose(); }