public CrcCalculatorStream(Stream stream, int length) { this._length = 0; this._InnerStream = stream; this._Crc32 = new CRC32(); this._length = length; }
/// <summary> /// Calculates the CRC32 polynomial of an object. /// </summary> /// <param name="value">The object to check.</param> /// <returns>A checksum of <paramref name="value"/> as an integer.</returns> public static long CRC32(object value) { var raw = ToByteArray(value); var alg = new CRC32(); alg.ComputeHash(raw); return alg.Value; }
public static string EmailHash(string email) { email = email.ToLower().Trim(); byte[] rawBytes = System.Text.UTF8Encoding.UTF8.GetBytes(email); CRC32 crc = new CRC32(); UInt32 crcResult = crc.GetCrc32(new System.IO.MemoryStream(rawBytes)); MD5 md5 = new MD5CryptoServiceProvider(); byte[] md5Result = md5.ComputeHash(rawBytes); string md5Data = ToHexString(md5Result).ToLower(); return crcResult.ToString() + "_" + md5Data; }
public ZlibBaseStream(System.IO.Stream stream, ZlibStreamFlavor flavor, bool leaveOpen) : base() { this._flushMode = FlushType.None; //this._workingBuffer = new byte[WORKING_BUFFER_SIZE_DEFAULT]; this._stream = stream; this._leaveOpen = leaveOpen; this._flavor = flavor; // workitem 7159 if (flavor == ZlibStreamFlavor.GZIP) { crc = new CRC32(); } }
/// <summary> /// Метод возвращает строку, которая представляет хеш-сумму файла по указанному пути для алгоритма CRC-32. /// </summary> /// <returns></returns> public string GetCRCHash() { CRC32 crc32 = new CRC32(); String hash = String.Empty; try { using (FileStream fs = File.Open(filePath, FileMode.Open)) foreach (byte b in crc32.ComputeHash(fs)) hash += b.ToString("x2").ToLower(); } catch (Exception e) { System.Windows.MessageBox.Show(e.Message); } return hash; }
private void importPatchToolStripMenuItem_Click(object sender, EventArgs e) { openFileDialog.Title = "Please select a compressed patch file"; openFileDialog.CheckFileExists = false; openFileDialog.Filter = "Zip File (*.zip)|*.zip|Rar File (*.rar)|*.rar"; if( openFileDialog.ShowDialog(this) == DialogResult.OK ) { if( !File.Exists(openFileDialog.FileName) ) { MessageBox.Show(this, "That file does not exist!"); return; } CRC32 crc32 = new CRC32(); crc32.PercentCompleteChange += new ProgressChangeHandler(OnPercentChange); FileStream stream = File.OpenRead(openFileDialog.FileName); uint crc = crc32.GetCrc32(stream); stream.Close(); string location = string.Empty; PathInputForm form = new PathInputForm(); form.PatchLocation = "http://"; if( form.ShowDialog(this) == DialogResult.Cancel ) return; if (!form.PatchLocation.Contains(Path.GetFileName(openFileDialog.FileName))) { if (!form.PatchLocation.EndsWith("/")) form.PatchLocation += "/"; form.PatchLocation += Path.GetFileName(openFileDialog.FileName); } PatchlistFile file = new PatchlistFile(form.PatchLocation, crc, new FileInfo(openFileDialog.FileName).Length); if (!listBox.Items.Contains(file) && !DuplicateFileNames(file)) { _needsSaving = true; listBox.Items.Add(file); } } }
private static void convertDataTrunk(PngihdrTrunk ihdrTrunk, byte[] conversionBuffer, int nMaxInflateBuffer) { if (conversionBuffer == null) throw new ArgumentNullException("conversionBuffer"); inflate(out conversionBuffer); // Switch the color int nIndex = 0; for (int y = 0; y < ihdrTrunk.MnHeight; y++) { nIndex++; for (int x = 0; x < ihdrTrunk.MnWidth; x++) { byte nTemp = conversionBuffer[nIndex]; conversionBuffer[nIndex] = conversionBuffer[nIndex + 2]; conversionBuffer[nIndex + 2] = nTemp; nIndex += 4; } } ZlibCodec deflater = deflate(conversionBuffer, nMaxInflateBuffer); // Put the result in the first IDAT chunk (the only one to be written out) PngTrunk firstDataTrunk = getTrunk("IDAT"); var crc32 = new CRC32(); foreach (byte b in System.Text.Encoding.UTF8.GetBytes(firstDataTrunk.getName())) { crc32.UpdateCRC(b); } crc32.UpdateCRC((byte)deflater.NextOut,(int)deflater.TotalBytesOut); long lCrcValue = crc32.Crc32Result; firstDataTrunk.MnData = deflater.OutputBuffer; firstDataTrunk.MnCrc[0] = (byte) ((lCrcValue & 0xFF000000) >> 24); firstDataTrunk.MnCrc[1] = (byte) ((lCrcValue & 0xFF0000) >> 16); firstDataTrunk.MnCrc[2] = (byte) ((lCrcValue & 0xFF00) >> 8); firstDataTrunk.MnCrc[3] = (byte) (lCrcValue & 0xFF); firstDataTrunk.MnSize = (int) deflater.TotalBytesOut; }
public int GetBytes( byte[] source, int sourceIndex, int sourceCount, byte[] dest, int destIndex, bool flush ) { if (source == null || source == null) throw new ArgumentNullException(); int bytes = 0; int newDestIndex = destIndex; for(int i=sourceIndex; i<sourceIndex+sourceCount; i++) { bool escape = false; bool newline = false; bool abort = false; byte b; try { b = source[i]; if (!escapeNextByte) { switch (b) { case escapeByte: i++; escape = true; if (i<sourceIndex+sourceCount) { b = source[i]; lineBytes ++; } else { //what a pain, we cannot get the next character now, so //we set a flag to tell us to do it next time escapeNextByte = true; abort = true; } break; case 10: case 13: newline = true; break; } } } catch { throw new ArgumentOutOfRangeException(); } if ((!newline) && (!abort)) { b = DecodeByte(b, escape | escapeNextByte); escapeNextByte = false; try { dest[newDestIndex] = b; newDestIndex++; bytes++; } catch { throw new ArgumentException(); } } } if (flush) { crc32Hasher.TransformFinalBlock(dest, destIndex, bytes); storedHash = crc32Hasher.Hash; crc32Hasher = new CRC32(); } else crc32Hasher.TransformBlock(dest, destIndex, bytes, dest, destIndex); return bytes; }
public Level5_Resource(byte[] data) { data = Decompress.Level5Decom(data); using (DataReader r = new DataReader(new System.IO.MemoryStream(data))) { var magic = new string(r.ReadChars(6)); if (magic != "CHRC00" && magic != "CHRN01") { throw new FormatException("RES file is corrupt"); } // ----------------------- var unknown0 = r.ReadInt16(); var stringTableOffset = r.ReadInt16() << 2; var unknown1 = r.ReadInt16(); var materialTableOffset = r.ReadInt16() << 2; var materialTableSectionCount = r.ReadInt16(); var resourceNodeOffsets = r.ReadInt16() << 2; var resourceNodeCount = r.ReadInt16(); r.Seek((uint)stringTableOffset); while (r.Position < r.BaseStream.Length) { string mname = r.ReadString(); if (mname == "") { break; } if (!ResourceNames.ContainsKey(CRC32.Crc32C(mname))) { ResourceNames.Add(CRC32.Crc32C(mname), mname); } } r.Seek((uint)materialTableOffset); for (int i = 0; i < materialTableSectionCount; i++) { var offset = r.ReadInt16() << 2; var count = r.ReadInt16(); var unknown = r.ReadInt16(); var size = r.ReadInt16(); if (unknown == 0x270F) { continue; } var temp = r.Position; for (int j = 0; j < count; j++) { r.Position = (uint)(offset + j * size); var key = r.ReadUInt32(); string resourceName = (ResourceNames.ContainsKey(key) ? ResourceNames[key] : key.ToString("X")); //Console.WriteLine(resourceName + " " + unknown.ToString("X") + " " + size.ToString("X")); if (unknown == 0xF0) { TextureNames.Add(resourceName); } if (unknown == 0x122) { Level5_Material mat = new Level5_Material(); mat.Name = resourceName; r.Skip(12); key = r.ReadUInt32(); resourceName = (ResourceNames.ContainsKey(key) ? ResourceNames[key] : key.ToString("X")); mat.TexName = resourceName; Console.WriteLine(resourceName + " " + unknown.ToString("X") + " " + size.ToString("X")); Materials.Add(mat); } } r.Seek(temp); } r.Seek((uint)resourceNodeOffsets); for (int i = 0; i < resourceNodeCount; i++) { var offset = r.ReadInt16() << 2; var count = r.ReadInt16(); var unknown = r.ReadInt16(); var size = r.ReadInt16(); if (unknown == 0x270F) { continue; } var temp = r.Position; r.Seek((uint)offset); for (int j = 0; j < count; j++) { var key = r.ReadUInt32(); //Console.WriteLine((ResourceNames.ContainsKey(key) ? ResourceNames[key] : key.ToString("X")) + " " + unknown.ToString("X") + " " + size.ToString("X")); r.Position += (uint)(size - 4); } r.Seek(temp); } } }
int detectVersion(byte[] data, uint uncompressedSize, int crc, ushort compressionMethod) { if (compressionMethod != 0 && compressionMethod != 8) { throw new Exception("Unknown compression method " + compressionMethod + "!"); } // version 2 if (compressionMethod == 8) { try { var bytes = ZlibCodecDecompress(decryptBytesWithTable(data, 0x3ff, 1, table2)); CRC32 dataCheckSum = new CRC32(); int checkSum = dataCheckSum.GetCrc32(new MemoryStream(bytes)); if (bytes.Length == uncompressedSize && checkSum == crc) { return 2; } } catch { } } else { var bytes = decryptBytesWithTable(data, 0x3ff, 1, table2); CRC32 dataCheckSum = new CRC32(); int checkSum = dataCheckSum.GetCrc32(new MemoryStream(bytes)); if (bytes.Length == uncompressedSize && checkSum == crc) { return 2; } } //version 1 if (compressionMethod == 8) { try { var bytes = ZlibCodecDecompress(decryptBytesWithTable(data, 0x1f, 32, table1)); CRC32 dataCheckSum = new CRC32(); int checkSum = dataCheckSum.GetCrc32(new MemoryStream(bytes)); if (bytes.Length == uncompressedSize && checkSum == crc) { return 1; } } catch { } } else { var bytes = decryptBytesWithTable(data, 0x1f, 32, table1); CRC32 dataCheckSum = new CRC32(); int checkSum = dataCheckSum.GetCrc32(new MemoryStream(bytes)); if (bytes.Length == uncompressedSize && checkSum == crc) { return 2; } } return 0; }
private BaseCommand CreateCommand(List <byte> CommandBuffer) { byte[] cmdBytes = null; lock (CommandBuffer) { if (CommandBuffer.Count < 9) { Logger.Instance().Error("CreateCommand() Error! CommandBuffer.Count<10"); return(null); } cmdBytes = new byte[CommandBuffer.Count]; CommandBuffer.CopyTo(cmdBytes); } byte msgID = cmdBytes[1]; BaseCommand cmd = CommandFactory.CreateCommand(msgID); if (cmd == null) { string msg = string.Format("CreateCommand() Error! MessageID={0}", msgID); Logger.Instance().Error(msg); return(null); } cmd.Direction = cmdBytes[0]; cmd.Channel = cmdBytes[2]; //分析命令数据长度 cmd.PayloadLength = cmdBytes[3]; cmd.PayloadLengthReverse = cmdBytes[4]; //分离出最后4字节的Checksum cmd.Checksum = (uint)(cmdBytes[cmdBytes.Length - 1] << 24); cmd.Checksum += (uint)(cmdBytes[cmdBytes.Length - 2] << 16); cmd.Checksum += (uint)(cmdBytes[cmdBytes.Length - 3] << 8); cmd.Checksum += (uint)(cmdBytes[cmdBytes.Length - 4]); //重新计算泵端传入的数据checksum,如果对应不上,记录日志,由上层应用酌情处理,但不能作超时处理 uint checksum = CRC32.CalcCRC32Partial(cmdBytes, cmdBytes.Length - 4, CRC32.CRC32_SEED); checksum ^= CRC32.CRC32_SEED; //在DUBUG状态下就不要去检查checksum #if !DEBUG if ((cmd.Checksum ^ checksum) != 0) { cmd.ErrorMsg = "Checksum Error"; string buffer2String = BytesToString(cmdBytes, 0, cmdBytes.Length); Logger.Instance().ErrorFormat("ProtocolEngine::CreateCommand() Checksum error, buffer={0}", buffer2String); } #endif //将PayloadData提取出来,方便生成字段 byte[] arrFieldsBuf = new byte[cmd.PayloadLength]; int index = cmdBytes.Length - arrFieldsBuf.Length - 4; if (index <= 0) { string msg = string.Format("CreateCommand() Error! index<=0 cmdBytes.Length={0} arrFieldsBuf.Length={1}", cmdBytes.Length, arrFieldsBuf.Length); Logger.Instance().Error(msg); return(null); } Array.Copy(cmdBytes, index, arrFieldsBuf, 0, arrFieldsBuf.Length); //将字段填进命令 cmd.SetBytes(arrFieldsBuf); return(cmd); }
/// <summary> /// Core encoding algorithm. Encodes from the source bytes into the destination bytes. /// </summary> /// <param name="source"></param> /// <param name="sourceIndex">the offset in the source from where to start encoding</param> /// <param name="sourceCount">the number of bytes to encode</param> /// <param name="dest"></param> /// <param name="destIndex">the offset in the destination to start writing to</param> /// <param name="flush">set to true if this is the last block of data</param> /// <returns>the number of bytes output to the destination</returns> public int GetBytes( byte[] source, int sourceIndex, int sourceCount, byte[] dest, int destIndex, bool flush ) { if (source == null || dest == null) throw new ArgumentNullException(); int byteCount = 0; int lineBytes = this.lineBytes; for(int i=sourceIndex; i<sourceCount+sourceIndex; i++) { byte c; try { c = source[i]; } catch { throw new ArgumentOutOfRangeException(); } bool escape = false; byte b = EncodeByte(c, out escape); try { if (escape) { dest[destIndex] = escapeByte; destIndex ++; byteCount ++; lineBytes ++; } dest[destIndex] = b; destIndex ++; lineBytes ++; byteCount ++; if (lineBytes >= this.lineLength) { //line termination dest[destIndex] = 13; destIndex ++; byteCount ++; dest[destIndex] = 10; destIndex ++; byteCount ++; lineBytes = 0; } } catch { throw new ArgumentException(); } } if (flush) { if (doLineFeedAfterFlush) { dest[destIndex] = 13; destIndex ++; byteCount ++; dest[destIndex] = 10; destIndex ++; byteCount ++; } crc32Hasher.TransformFinalBlock(source, sourceIndex, sourceCount); storedHash = crc32Hasher.Hash; crc32Hasher = new CRC32(); this.lineBytes = 0; } else { crc32Hasher.TransformBlock(source, sourceIndex, sourceCount, source, sourceIndex); this.lineBytes = lineBytes; } return byteCount; }
private void _TakeAndCompress() { var rnd = new System.Random(); while (!_toCompress.IsCompleted) { WorkItem workitem = null; int ix = -1; try { ix = _toCompress.Take(); workitem = _pool[ix]; } catch (InvalidOperationException) { // The collection has been completed. // Some other thread has called CompleteAdding() // after this thread passed the // IsCompleted check. } if (workitem == null) { continue; } try { TraceOutput(TraceBits.Compress, "Compress lock wi({0}) ord({1})", workitem.index, workitem.ordinal); // compress one buffer Ionic.Zlib.CRC32 crc = new CRC32(); int ib = workitem.inputBytesAvailable; crc.SlurpBlock(workitem.buffer, 0, workitem.inputBytesAvailable); DeflateOneSegment(workitem); workitem.crc = crc.Crc32Result; TraceOutput(TraceBits.Compress, "Compress done wi({0}) ord({1}) ib-({2}) cba({3})", workitem.index, workitem.ordinal, ib, workitem.compressedBytesAvailable ); lock (_latestLock) { if (workitem.ordinal > _latestCompressed) { _latestCompressed = workitem.ordinal; } } _toWrite.Add(workitem.index); _newlyCompressedBlob.Set(); } catch (System.Exception exc1) { lock (_eLock) { // expose the exception to the main thread if (_pendingException != null) { _pendingException = exc1; } } } } }
public Source (String fileName) { buffer = FillBuffer (fileName); length = buffer.Length; MAX_LENGTH = Math.Max (length, MAX_LENGTH); CRC32 crc32 = new CRC32(); crc32.Update (buffer, length); crc = crc32.Value; }
private void _InitializePoolOfWorkItems() { _toWrite = new Queue<int>(); _toFill = new Queue<int>(); _pool = new System.Collections.Generic.List<WorkItem>(); int nTasks = BufferPairsPerCore * Environment.ProcessorCount; nTasks = Math.Min(nTasks, _maxBufferPairs); for(int i=0; i < nTasks; i++) { _pool.Add(new WorkItem(_bufferSize, _compressLevel, Strategy, i)); _toFill.Enqueue(i); } _newlyCompressedBlob = new AutoResetEvent(false); _runningCrc = new CRC32(); _currentlyFilling = -1; _lastFilled = -1; _lastWritten = -1; _latestCompressed = -1; }
public ZipReturn LocalFileHeaderReadQuick() { try { TrrntZip = true; BinaryReader br = new BinaryReader(_zipFs); _zipFs.Position = (long)RelativeOffsetOfLocalHeader; uint thisSignature = br.ReadUInt32(); if (thisSignature != LocalFileHeaderSignature) return ZipReturn.ZipLocalFileHeaderError; br.ReadUInt16(); // version needed to extract _generalPurposeBitFlag = br.ReadUInt16(); if ((_generalPurposeBitFlag & 8) == 8) return ZipReturn.ZipCannotFastOpen; _compressionMethod = br.ReadUInt16(); _lastModFileTime = br.ReadUInt16(); _lastModFileDate = br.ReadUInt16(); CRC = ReadCRC(br); _compressedSize = br.ReadUInt32(); UncompressedSize = br.ReadUInt32(); ushort fileNameLength = br.ReadUInt16(); ushort extraFieldLength = br.ReadUInt16(); byte[] bFileName = br.ReadBytes(fileNameLength); FileName = (_generalPurposeBitFlag & (1 << 11)) == 0 ? GetString(bFileName) : Encoding.UTF8.GetString(bFileName, 0, fileNameLength); byte[] extraField = br.ReadBytes(extraFieldLength); Zip64 = false; int pos = 0; while (extraFieldLength > pos) { ushort type = BitConverter.ToUInt16(extraField, pos); pos += 2; ushort blockLength = BitConverter.ToUInt16(extraField, pos); pos += 2; switch (type) { case 0x0001: Zip64 = true; if (UncompressedSize == 0xffffffff) { UncompressedSize = BitConverter.ToUInt64(extraField, pos); pos += 8; } if (_compressedSize == 0xffffffff) { _compressedSize = BitConverter.ToUInt64(extraField, pos); pos += 8; } break; case 0x7075: pos += 1; uint nameCRC32 = BitConverter.ToUInt32(extraField, pos); pos += 4; CRC32 crcTest = new CRC32(); crcTest.SlurpBlock(bFileName, 0, fileNameLength); uint fCRC = crcTest.Crc32ResultU; if (nameCRC32 != fCRC) return ZipReturn.ZipLocalFileHeaderError; int charLen = blockLength - 5; FileName = Encoding.UTF8.GetString(extraField, pos, charLen); pos += charLen; break; default: pos += blockLength; break; } } _dataLocation = (ulong)_zipFs.Position; return ZipReturn.ZipGood; } catch { return ZipReturn.ZipLocalFileHeaderError; } }
/// <summary> /// Uses polymorphic features of the IDataType interface to save the different data types differently /// Headers for all types of files match, save for the first 3 bytes, which use the file extension to /// save the proper string. /// </summary> /// <param name="pubVersion">Version of the pub file to save. For Ethan's client, items should be 1, otherwise, this should be 0 (for now)</param> /// <param name="error">ref parameter that provides the Exception.Message string on an error condition</param> /// <returns>True if successful, false on failure. Use the 'error' parameter to check error message</returns> public bool Save(int pubVersion, out string error) { try { using (FileStream sw = File.Create(FilePath)) //throw exceptions on error { if (FilePath.Length <= 4 || !FilePath.Contains('.')) throw new ArgumentException("The filename of the data file must have a 3 letter extension. Use EIF, ENF, ESF, or ECF."); //get the extension to write as the first 3 bytes byte[] extension = Encoding.ASCII.GetBytes(FilePath.ToUpper().Substring(FilePath.LastIndexOf('.') + 1)); if (extension.Length != 3) throw new ArgumentException("The filename of the data file must have a 3 letter extension. Use EIF, ENF, ESF, or ECF."); //allocate the data array for all the data to be saved byte[] allData; //write the file to memory first using (MemoryStream mem = new MemoryStream()) { mem.Write(extension, 0, 3); //E[I|N|S|C]F at beginning mem.Write(Packet.EncodeNumber(Rid, 4), 0, 4); //rid mem.Write(Packet.EncodeNumber(Data.Count, 2), 0, 2); //len Version = pubVersion; mem.WriteByte(Packet.EncodeNumber(Version, 1)[0]); //new version check for (int i = 1; i < Data.Count; ++i) { byte[] toWrite = Data[i].SerializeToByteArray(); mem.Write(toWrite, 0, toWrite.Length); } allData = mem.ToArray(); //get all data bytes } //write the data to the stream and overwrite whatever the rid is with the CRC CRC32 crc = new CRC32(); uint newRid = crc.Check(allData, 7, (uint)allData.Length - 7); Rid = (int)newRid; sw.Write(allData, 0, allData.Length); sw.Seek(3, SeekOrigin.Begin); //skip first 3 bytes sw.Write(Packet.EncodeNumber(Rid, 4), 0, 4); //overwrite the 4 RID (revision ID) bytes } } catch (Exception ex) { error = ex.Message; return false; } error = "none"; return true; }
//hash the TOC static void AddAsBytesTo(CRC32 crc32, int i) => crc32.Add(BitConverter.GetBytes(i));
public static string ComputeCRC32(byte[] buffer) { CRC32 crc32 = new CRC32(); string sHash = ""; byte[] bHash = crc32.ComputeHash(buffer); foreach (byte b in bHash) { sHash += b.ToString("x2").ToLower(); } return sHash; }
public void DebugOutput(string actionName) { Debug.LogError(CRC32.Compute(actionName) + "/*" + actionName + "*/"); }
/// <summary> /// 上传数据流 /// </summary> /// <param name="stream">(确定长度的)数据流</param> /// <param name="key">要保存的key</param> /// <param name="token">上传凭证</param> /// <param name="extra">上传可选设置</param> /// <returns>上传数据流后的返回结果</returns> public HttpResult UploadStream(Stream stream, string key, string token, PutExtra putExtra) { if (putExtra == null) { putExtra = new PutExtra(); } if (string.IsNullOrEmpty(putExtra.MimeType)) { putExtra.MimeType = "application/octet-stream"; } if (putExtra.ProgressHandler == null) { putExtra.ProgressHandler = DefaultUploadProgressHandler; } if (putExtra.UploadController == null) { putExtra.UploadController = DefaultUploadController; } string fname = key; if (string.IsNullOrEmpty(key)) { fname = "fname_temp"; } HttpResult result = new HttpResult(); using (stream) { try { string boundary = HttpManager.CreateFormDataBoundary(); StringBuilder bodyBuilder = new StringBuilder(); bodyBuilder.AppendLine("--" + boundary); if (key != null) { //write key when it is not null bodyBuilder.AppendLine("Content-Disposition: form-data; name=\"key\""); bodyBuilder.AppendLine(); bodyBuilder.AppendLine(key); bodyBuilder.AppendLine("--" + boundary); } //write token bodyBuilder.AppendLine("Content-Disposition: form-data; name=\"token\""); bodyBuilder.AppendLine(); bodyBuilder.AppendLine(token); bodyBuilder.AppendLine("--" + boundary); //write extra params if (putExtra.Params != null && putExtra.Params.Count > 0) { foreach (var p in putExtra.Params) { if (p.Key.StartsWith("x:")) { bodyBuilder.AppendFormat("Content-Disposition: form-data; name=\"{0}\"", p.Key); bodyBuilder.AppendLine(); bodyBuilder.AppendLine(); bodyBuilder.AppendLine(p.Value); bodyBuilder.AppendLine("--" + boundary); } } } //prepare data buffer int bufferSize = 1024 * 1024; byte[] buffer = new byte[bufferSize]; int bytesRead = 0; putExtra.ProgressHandler(0, stream.Length); MemoryStream dataMS = new MemoryStream(); while ((bytesRead = stream.Read(buffer, 0, bufferSize)) != 0) { dataMS.Write(buffer, 0, bytesRead); } //write crc32 uint crc32 = CRC32.CheckSumBytes(dataMS.ToArray()); //write key when it is not null bodyBuilder.AppendLine("Content-Disposition: form-data; name=\"crc32\""); bodyBuilder.AppendLine(); bodyBuilder.AppendLine(crc32.ToString()); bodyBuilder.AppendLine("--" + boundary); //write fname bodyBuilder.AppendFormat("Content-Disposition: form-data; name=\"file\"; filename=\"{0}\"", fname); bodyBuilder.AppendLine(); //write mime type bodyBuilder.AppendFormat("Content-Type: {0}", putExtra.MimeType); bodyBuilder.AppendLine(); bodyBuilder.AppendLine(); //write file data StringBuilder bodyEnd = new StringBuilder(); bodyEnd.AppendLine(); bodyEnd.AppendLine("--" + boundary + "--"); byte[] partData1 = Encoding.UTF8.GetBytes(bodyBuilder.ToString()); byte[] partData2 = dataMS.ToArray(); byte[] partData3 = Encoding.UTF8.GetBytes(bodyEnd.ToString()); MemoryStream ms = new MemoryStream(); ms.Write(partData1, 0, partData1.Length); ms.Write(partData2, 0, partData2.Length); ms.Write(partData3, 0, partData3.Length); //get upload host string ak = UpToken.GetAccessKeyFromUpToken(token); string bucket = UpToken.GetBucketFromUpToken(token); if (ak == null || bucket == null) { return(HttpResult.InvalidToken); } string uploadHost = this.config.UpHost(ak, bucket); putExtra.ProgressHandler(stream.Length / 5, stream.Length); result = httpManager.PostMultipart(uploadHost, ms.ToArray(), boundary, null); putExtra.ProgressHandler(stream.Length, stream.Length); if (result.Code == (int)HttpCode.OK) { result.RefText += string.Format("[{0}] [FormUpload] Uploaded: #STREAM# ==> \"{1}\"\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff"), key); } else { result.RefText += string.Format("[{0}] [FormUpload] Failed: code = {1}, text = {2}\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff"), result.Code, result.Text); } //close memory stream ms.Close(); dataMS.Close(); } catch (Exception ex) { StringBuilder sb = new StringBuilder(); sb.AppendFormat("[{0}] [FormUpload] Error: ", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff")); Exception e = ex; while (e != null) { sb.Append(e.Message + " "); e = e.InnerException; } sb.AppendLine(); if (ex is QiniuException) { QiniuException qex = (QiniuException)ex; result.Code = qex.HttpResult.Code; result.RefCode = qex.HttpResult.Code; result.Text = qex.HttpResult.Text; result.RefText += sb.ToString(); } else { result.RefCode = (int)HttpCode.USER_UNDEF; result.RefText += sb.ToString(); } } } return(result); }
/// <summary> /// PNGファイルを展開します。 /// </summary> /// <param name="path">展開するPNGファイルのパスを設定します。</param> /// <param name="imageList">展開した画像データを格納するコレクションを設定します。</param> /// <returns>画像のサイズとアニメーションの再生回数を返します。</returns> public (int width, int height, int times) Decode(string path, List <FrameModel> imageList) { var chunks = new List <string>(); var images = new List <byte[]>(); var frames = new List <fcTL>(); byte[] buffer; bool hasIHDR = false; bool hasPLTE = false; bool hasACTL = false; bool hasIEND = false; var ImageHeader = new IHDR(); var AnimationControl = new acTL(); bool isIdatFrame = false; int sequence = 0; bool isFCTL = false; imageList.Clear(); using (var stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read)) { // PNGヘッダをスキップ stream.Seek(Signature.Length, SeekOrigin.Begin); var offset = Signature.Length; while (!hasIEND) { // チャンクを読み取り var length = ReadUINT(stream); buffer = ReadBytes(stream, (int)(length + 4)); var type = Encoding.ASCII.GetString(buffer.Take(4).ToArray()); var crc = ReadUINT(stream); // CRCチェック var calcCRC = new CRC32(); calcCRC.SlurpBlock(buffer, 0, (int)(length + 4)); if (crc != (uint)calcCRC.Crc32Result) { throw CreateEx(type, offset, "CRCが一致していません"); } switch (type) { case "IHDR": // イメージヘッダ読み取り if (hasIHDR) { throw CreateEx(type, offset, "チャンクが複数存在しています"); } if (chunks.Count > 0) { throw CreateEx(type, offset, "チャンクの位置が不正です"); } ImageHeader.Width = ReadUINT(buffer, 4); ImageHeader.Height = ReadUINT(buffer, 8); ImageHeader.BitDepth = buffer[12]; ImageHeader.ColorType = (ColorType)buffer[13]; ImageHeader.CompressionMethod = buffer[14]; ImageHeader.FilterMethod = buffer[15]; ImageHeader.InterlaceMethod = (InterlaceMethod)buffer[16]; if (ImageHeader.Width == 0 || ImageHeader.Height == 0) { throw CreateEx(type, offset, "画像のサイズが不正です"); } if (!Enum.IsDefined(typeof(ColorType), ImageHeader.ColorType) || !AllowBitDepth.ContainsKey(ImageHeader.ColorType) || !AllowBitDepth[ImageHeader.ColorType].Contains(ImageHeader.BitDepth)) { throw CreateEx(type, offset, "カラーモードとビット深度の組み合わせが不正です"); } if (ImageHeader.CompressionMethod != 0) { throw CreateEx(type, offset, "圧縮手法の値が不正です"); } if (ImageHeader.FilterMethod != 0) { throw CreateEx(type, offset, "フィルター手法の値が不正です"); } if (!Enum.IsDefined(typeof(InterlaceMethod), ImageHeader.InterlaceMethod)) { throw CreateEx(type, offset, "インターレース手法の値が不正です"); } hasIHDR = true; break; case "PLTE": // パレット読み取り if (hasPLTE) { throw CreateEx(type, offset, "チャンクが複数存在しています"); } // // パレット読み込み処理 // hasPLTE = true; break; case "IDAT": // イメージデータ読み取り var idat = buffer.Skip(4).Take(buffer.Length - 4).ToArray(); if (chunks[chunks.Count - 1] != type) { if (images.Count != 0) { throw CreateEx(type, offset, "非連続なチャンクが存在しています"); } images.Add(idat); if (hasACTL && frames.Count == 1) { isIdatFrame = true; } } else { images[images.Count - 1] = images[images.Count - 1].Concat(idat).ToArray(); } if (hasACTL) { isFCTL = false; } break; case "acTL": // アニメーションコントロール読み取り if (hasACTL) { throw CreateEx(type, offset, "チャンクが複数存在しています"); } if (images.Count != 0) { throw CreateEx(type, offset, "チャンクの位置が不正です"); } AnimationControl.NumFrames = ReadUINT(buffer, 4); AnimationControl.NumPlays = ReadUINT(buffer, 8); if (AnimationControl.NumFrames == 0) { throw CreateEx(type, offset, "アニメーションフレーム数が0です"); } hasACTL = true; break; case "fcTL": // フレームコントロール読み取り if (isFCTL) { throw CreateEx(type, offset, "フレームコントロールが連続しています"); } var fctl = new fcTL() { SequenceNumber = ReadUINT(buffer, 4), Width = ReadUINT(buffer, 8), Height = ReadUINT(buffer, 12), XOffset = ReadUINT(buffer, 16), YOffset = ReadUINT(buffer, 20), DelayNum = ReadWORD(buffer, 24), DelayDen = ReadWORD(buffer, 26), DisposeOp = (DisposeOp)buffer[28], BlendOp = (BlendOp)buffer[29] }; if (frames.Count == 0 && sequence != 0) { throw CreateEx(type, offset, "最初のチャンクの位置が不正です"); } if (fctl.SequenceNumber != sequence) { throw CreateEx(type, offset, "シーケンス番号が不正です"); } if (frames.Count == 0 && (fctl.Width != ImageHeader.Width || fctl.Height != ImageHeader.Height || fctl.XOffset != 0 || fctl.YOffset != 0)) { throw CreateEx(type, offset, "1枚目のアニメーションフレームのサイズが不正です"); } if (fctl.Width == 0 || fctl.Height == 0 || fctl.XOffset + fctl.Width > ImageHeader.Width || fctl.YOffset + fctl.Height > ImageHeader.Height) { throw CreateEx(type, offset, "アニメーションフレームのサイズが不正です"); } if (!Enum.IsDefined(typeof(DisposeOp), fctl.DisposeOp)) { throw CreateEx(type, offset, "フレーム描画後の処理方法の値が不正です"); } if (!Enum.IsDefined(typeof(BlendOp), fctl.BlendOp)) { throw CreateEx(type, offset, "フレーム描画方法の値が不正です"); } if (fctl.DelayDen == 0) { fctl.DelayDen = 100; } frames.Add(fctl); sequence++; isFCTL = true; break; case "fdAT": // フレームデータ読み取り if (!isFCTL) { throw CreateEx(type, offset, "対応するフレームコントロールが存在しません"); } var seq = ReadUINT(buffer, 4); if (seq != sequence) { throw CreateEx(type, offset, "シーケンス番号が不正です"); } var fdat = buffer.Skip(8).Take(buffer.Length - 8).ToArray(); if (chunks[chunks.Count - 1] != type) { images.Add(fdat); } else { images[images.Count - 1] = images[images.Count - 1].Concat(fdat).ToArray(); } sequence++; isFCTL = false; break; case "IEND": hasIEND = true; break; } chunks.Add(type); offset += buffer.Length + 8; } } // 必須チャンクチェック if (!hasIHDR || !hasIEND || (!hasPLTE && ImageHeader.ColorType == ColorType.Palette)) { throw new InvalidOperationException("必須チャンクが存在しません"); } // 不要チャンクチェック if (hasPLTE && (ImageHeader.ColorType == ColorType.Grayscale || ImageHeader.ColorType == ColorType.GrayscaleAlpha)) { throw new InvalidOperationException("不要なチャンクが存在しています"); } // フレーム数チェック if (hasACTL && (AnimationControl.NumFrames != frames.Count || AnimationControl.NumFrames != images.Count - (isIdatFrame ? 0 : 1))) { throw new InvalidOperationException("アニメーションフレーム数が不正です"); } // イメージデータチェック if (images.Count == 0) { throw new InvalidOperationException("イメージデータが存在しません"); } // イメージデータ展開 if (hasACTL) { // APNG for (var i = 0; i < frames.Count; i++) { imageList.Add(new FrameModel() { XOffset = (int)frames[i].XOffset, YOffset = (int)frames[i].YOffset, Width = (int)frames[i].Width, Height = (int)frames[i].Height, Delay = frames[i].DelayNum * 1000 / frames[i].DelayDen, Dispose = frames[i].DisposeOp == DisposeOp.APNG_DISPOSE_OP_NONE ? FrameModel.DisposeMode.None : frames[i].DisposeOp == DisposeOp.APNG_DISPOSE_OP_BACKGROUND ? FrameModel.DisposeMode.Background : FrameModel.DisposeMode.Previous, Blend = frames[i].BlendOp == BlendOp.APNG_BLEND_OP_SOURCE ? FrameModel.BlendMode.Normal : FrameModel.BlendMode.AlphaBlending, Data = Unfilter(ZlibStream.UncompressBuffer(images[i + (isIdatFrame ? 0 : 1)]), (int)frames[i].Width, (int)frames[i].Height, ImageHeader.ColorType) }); } } else { // PNG imageList.Add(new FrameModel() { Width = (int)ImageHeader.Width, Height = (int)ImageHeader.Height, Data = Unfilter(ZlibStream.UncompressBuffer(images[0]), (int)ImageHeader.Width, (int)ImageHeader.Height, ImageHeader.ColorType) }); } return(width : (int)ImageHeader.Width, height : (int)ImageHeader.Height, times : (int)(hasACTL ? AnimationControl.NumPlays : 0));
// This ctor is private - no validation is done here. This is to allow the use // of a (specific) negative value for the _lengthLimit, to indicate that there // is no length set. So we validate the length limit in those ctors that use an // explicit param, otherwise we don't validate, because it could be our special // value. private CrcCalculatorStream (bool leaveOpen, Int64 length, System.IO.Stream stream, CRC32 crc32) : base() { _innerStream = stream; _Crc32 = crc32 ?? new CRC32(); _lengthLimit = length; _leaveOpen = leaveOpen; }
/// <summary> /// Add 1 byte to the buffer. /// </summary> /// <param name="data"></param> public void AddData(byte data) { if (data == HEADER && IsHeaderReceived == false) { /* * If the package header is received. package is null * this is the header identifier rather than the payload. */ bwr.Write(data); IsHeaderReceived = true; } else if (IsHeaderReceived) { if (memStream.Length > 0) { /* * This is the payload, put it into the byte array. */ bwr.Write(data); // The length of the package has received. if (memStream.Length == 3) { var tmp = memStream.ToArray(); LengthExpected = tmp[1] + tmp[2] * 256; } else if (LengthExpected > 0) { // We received a complete package if (memStream.Length == LengthExpected + SIZE_OVERALL_COST) { //TODO too verbose without pointer? buffer = memStream.ToArray(); var crc_expected = new CRC32().Calculate(buffer, LengthExpected + SIZE_OVERALL_COST - SIZE_CRC); var crc_bytes = new byte[4]; Buffer.BlockCopy(buffer, LengthExpected + SIZE_OVERALL_COST - SIZE_CRC, crc_bytes, 0, 4); var crc_origin = BitConverter.ToUInt32(crc_bytes, 0); if (crc_expected == crc_origin) { memStream.Seek(0, SeekOrigin.Begin); using (BinaryReader reader = new BinaryReader(memStream)) { // drop Header reader.ReadByte(); // drop length reader.ReadUInt16(); // API Identifier API_Identifier = reader.ReadByte(); // Frame ID FrameID = reader.ReadUInt16(); // Command Command = (Commands.CommandDef)reader.ReadByte(); // payload Payload = reader.ReadBytes(LengthExpected - SIZE_API_ID - SIZE_FRAME_ID - SIZE_COMMAND); } IsPassCRC = true; CRC = crc_expected; OnPackageReceived?.Invoke(this, Payload); } else { IsPassCRC = false; } IsPackageFound = true; } } } } }
// static constructor static CRC32() { _defaultCRC = new CRC32(); }
internal void Crc32(CRC32 @out, long pos, int cnt) { @out.Update(array, (int)(pos - start), cnt); }
private void CRC32_Click(object sender, EventArgs e) { CRC32 crc32 = new CRC32(); byte[] szInput = Encoding.ASCII.GetBytes(txtInput.Text); crc32.Crc32(); uint szOutput = crc32.ComputeChecksum(szInput); txtOutput.Text = String.Format("0x{0:X}", szOutput.ToString("X")); }
/// <summary> /// The public constructor for the DM637x device AIS generator. /// The constructor is where the device differentiation is defined. /// </summary> public AISGen_OMAP_L138() { // Define the device name - used for default file names devNameShort = "OMAP-L138"; devNameLong = "OMAPL138"; // Define the device caches (they are considered internal memories since // bootrom turns off caching) - two identical sets since the memory map // has the caches at two locations Cache = new CacheInfo[6]; Cache[0].level = CacheLevel.L2; Cache[0].type = CacheType.Program | CacheType.Data; Cache[0].startAddr = 0x00800000; Cache[0].size = 0x40000; Cache[1].level = CacheLevel.L1; Cache[1].type = CacheType.Program; Cache[1].startAddr = 0x00E08000; Cache[1].size = 0x8000; Cache[2].level = CacheLevel.L1; Cache[2].type = CacheType.Data; Cache[2].startAddr = 0x00F10000; Cache[2].size = 0x8000; Cache[3].level = CacheLevel.L2; Cache[3].type = CacheType.Program | CacheType.Data; Cache[3].startAddr = 0x10800000; Cache[3].size = 0x40000; Cache[4].level = CacheLevel.L1; Cache[4].type = CacheType.Program; Cache[4].startAddr = 0x10E08000; Cache[4].size = 0x8000; Cache[5].level = CacheLevel.L1; Cache[5].type = CacheType.Data; Cache[5].startAddr = 0x10F10000; Cache[5].size = 0x8000; // Define the IDMA channels for internal memory transfers IDMA = new IDMARegisters[2]; IDMA[0] = new IDMARegisters(0, 0x01820000); IDMA[1] = new IDMARegisters(1, 0x01820100); // Define OMAP-L138 ROM boot loader functions ROMFunc = new ROMFunction[13]; ROMFunc[0].funcName = ROMFunctionNames.PLL0Config; ROMFunc[0].iniSectionName = "PLL0CONFIG"; ROMFunc[0].numParams = 2; ROMFunc[0].paramNames = new String[2] { "PLL0CFG0", "PLL0CFG1" }; ROMFunc[1].funcName = ROMFunctionNames.PLL1Config; ROMFunc[1].iniSectionName = "PLL1CONFIG"; ROMFunc[1].numParams = 2; ROMFunc[1].paramNames = new String[2] { "PLL1CFG0", "PLL1CFG1" }; ROMFunc[2].funcName = ROMFunctionNames.PeriphClockConfig; ROMFunc[2].iniSectionName = "PERIPHCLKCFG"; ROMFunc[2].numParams = 1; ROMFunc[2].paramNames = new String[1] { "PERIPHCLKCFG" }; ROMFunc[3].funcName = ROMFunctionNames.EMIF3AConfigDDR; ROMFunc[3].iniSectionName = "EMIF3DDR"; ROMFunc[3].numParams = 8; ROMFunc[3].paramNames = new String[8] { "PLL1CFG0", "PLL1CFG1", "DDRPHYC1R", "SDCR", "SDTIMR", "SDTIMR2", "SDRCR", "CLK2XSRC" }; ROMFunc[4].funcName = ROMFunctionNames.EMIF25ConfigSDRAM; ROMFunc[4].iniSectionName = "EMIF25SDRAM"; ROMFunc[4].numParams = 5; ROMFunc[4].paramNames = new String[5] { "SDBCR", "SDTIMR", "SDRSRPDEXIT", "SDRCR", "DIV4p5_CLK_ENABLE" }; ROMFunc[5].funcName = ROMFunctionNames.EMIF25ConfigAsync; ROMFunc[5].iniSectionName = "EMIF25ASYNC"; ROMFunc[5].numParams = 5; ROMFunc[5].paramNames = new String[5] { "A1CR", "A2CR", "A3CR", "A4CR", "NANDFCR" }; ROMFunc[6].funcName = ROMFunctionNames.PLLandClockConfig; ROMFunc[6].iniSectionName = "PLLANDCLOCKCONFIG"; ROMFunc[6].numParams = 3; ROMFunc[6].paramNames = new String[3] { "PLL0CFG0", "PLL0CFG1", "PERIPHCLKCFG" }; ROMFunc[7].funcName = ROMFunctionNames.PSCConfig; ROMFunc[7].iniSectionName = "PSCCONFIG"; ROMFunc[7].numParams = 1; ROMFunc[7].paramNames = new String[1] { "LPSCCFG" }; ROMFunc[8].funcName = ROMFunctionNames.PINMUXConfig; ROMFunc[8].iniSectionName = "PINMUX"; ROMFunc[8].numParams = 3; ROMFunc[8].paramNames = new String[3] { "REGNUM", "MASK", "VALUE" }; ROMFunc[9].funcName = ROMFunctionNames.FastBoot; ROMFunc[9].iniSectionName = "FASTBOOT"; ROMFunc[9].numParams = 0; ROMFunc[9].paramNames = null; ROMFunc[10].funcName = ROMFunctionNames.IOPUConfig; ROMFunc[10].iniSectionName = "IOPUCONFIG"; ROMFunc[10].numParams = 2; ROMFunc[10].paramNames = new String[2] { "IOPUSELECT", "MPPAVALUE" }; ROMFunc[11].funcName = ROMFunctionNames.MPUConfig; ROMFunc[11].iniSectionName = "MPUCONFIG"; ROMFunc[11].numParams = 4; ROMFunc[11].paramNames = new String[4] { "MPUSELECT", "STARTADDR", "ENDADDR", "MPPAVALUE" }; ROMFunc[12].funcName = ROMFunctionNames.TAPSConfig; ROMFunc[12].iniSectionName = "TAPSCONFIG"; ROMFunc[12].numParams = 1; ROMFunc[12].paramNames = new String[1] { "TAPSCFG" }; // Configuration info for the AISExtras functions (provided in AISExtraFileName COFF file) /* AISExtraFileName = null; * * AISExtraFunc = null;*/ AISExtraFileName = "AISExtra_" + devNameShort + ".out"; AISExtraFunc = new AISExtraFunction[1]; AISExtraFunc[0].funcName = "MakeROMEmulatableAndWait"; AISExtraFunc[0].iniSectionName = "OPENROMFORDEBUG"; AISExtraFunc[0].numParams = 0; AISExtraFunc[0].paramNames = null; AISExtraFunc[0].isInitFunc = true; // OMAP-L138 is little endian devEndian = Endian.LittleEndian; // OMAP-L138 AIS data is little endian; devAISEndian = Endian.LittleEndian; // Default settings for UARTSendDONE function UARTSendDONEAddr = 0x0; SendUARTSendDONE = false; // Default boot mode (can be changed from INI file) for this device bootMode = BootModes.NONE; // Create default CRC object for this device devCRC = new CRC32(0x04C11DB7, 0xFFFFFFFF, 0xFFFFFFFF, true, 1, UtilLib.CRC.CRCType.INCREMENTAL, UtilLib.CRC.CRCCalcMethod.LUT); crcType = CRCCheckType.NO_CRC; }
// static constructor static CRC32() { _crc32TablesCache = Hashtable.Synchronized(new Hashtable()); _defaultCRC = new CRC32(); }
/// <summary> /// The sender then sends a ZFILE header with ZMODEM Conversion, Management, and Transport options[3] /// followed by a ZCRCW data subpacket containing the file name, file length, /// modification date, and other information identical to that used by YMODEM Batch. /// </summary> /// <param name="fileInfo"></param> /// <param name="crcCalculator"></param> private bool SendZFILEHeaderCommand(string filename, int length, DateTimeOffset lastWriteTimeUtc, CRC32 crcCalculator) { var result = default(bool); var isExtended = true; var zFileHeader = Utils.Build32BitBinHeader( HeaderType.ZFILE, ZFILEConversionOption.ZCBIN, ZFILEManagementOption.ZMNEWL, ZFILETransportOption.None, ZFILEExtendedOptions.None, crcCalculator ); var zFileHeaderQueue = new Queue <byte>(); foreach (var c in zFileHeader) { zFileHeaderQueue.Enqueue((byte)c); } // Send ZFILE header first - No response SendCommand(zFileHeaderQueue.ToArray()); var dataQueue = new Queue <byte>(); foreach (char c in filename) { dataQueue.Enqueue((byte)c); } if (isExtended) { dataQueue.Enqueue(0); // File length as decimal string var fileLength = length.ToString(); foreach (var c in fileLength) { dataQueue.Enqueue((byte)c); } // Space dataQueue.Enqueue(0x20); var utcTime = lastWriteTimeUtc.ToUnixTimeSeconds(); var octalString = Convert.ToString(utcTime, 8); // Modification date foreach (var c in octalString) { dataQueue.Enqueue((byte)c); } } else { // The file information is terminated by a null. // If only the pathname is sent, the pathname is terminated with two nulls. dataQueue.Enqueue(0); dataQueue.Enqueue(0); } byte[] data = dataQueue.Concat(new byte[] { (byte)ZDLESequence.ZCRCW }).ToArray(); dataQueue.Enqueue((byte)ControlBytes.ZDLE); dataQueue.Enqueue((byte)ZDLESequence.ZCRCW); var crc = crcCalculator.ComputeHash(data); var encodedCRC = dataQueue .Concat(ZDLEEncoder.EscapeControlCharacters(crc)) .ToArray(); var response = SendCommand(encodedCRC, true, HeaderType.ZRPOS); // We expect ZRPOS if (response?.ZHeader == HeaderType.ZRPOS) { result = true; } /* * The receiver may respond with a ZSKIP header, which makes the sender * proceed to the next file (if any) in the batch. * * A ZRPOS header from the receiver initiates transmission of the file * data starting at the offset in the file specified in the ZRPOS header. * Normally the receiver specifies the data transfer to begin begin at * offset 0 in the file. */ if (response?.ZHeader == HeaderType.ZSKIP || response?.ZHeader == HeaderType.ZRPOS || response?.ZHeader == HeaderType.ZCRC) { /* * The receiver has a file with the same name and length, may * respond with a ZCRC header with a byte count, which * requires the sender to perform a 32 bit CRC on the * specified number of bytes in the file and transmit the * complement of the CRC in an answering ZCRC header.the crc is * initialised to 0xfffffff; a byte count of 0 implies the entire * file the receiver uses this information to determine whether to * accept the file or skip it. This sequence may be triggered * by the ZMCRC Management Option. */ } return(result); }
private void upload(HttpFormFile fFile, string key, string token, UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler) { if (uploadOptions == null) { uploadOptions = UploadOptions.defaultOptions(); } Dictionary <string, string> vPostParams = new Dictionary <string, string>(); //设置key if (!string.IsNullOrEmpty(key)) { vPostParams.Add("key", key); } //设置token vPostParams.Add("token", token); //设置crc32校验 if (uploadOptions.CheckCrc32) { switch (fFile.BodyType) { case HttpFileType.DATA_SLICE: vPostParams.Add("crc32", string.Format("{0}", CRC32.CheckSumSlice(fFile.BodyBytes, fFile.Offset, fFile.Count))); break; case HttpFileType.DATA_BYTES: vPostParams.Add("crc32", string.Format("{0}", CRC32.CheckSumBytes(fFile.BodyBytes))); break; case HttpFileType.FILE_STREAM: long streamLength = fFile.BodyStream.Length; byte[] buffer = new byte[streamLength]; int cnt = fFile.BodyStream.Read(buffer, 0, (int)streamLength); vPostParams.Add("crc32", string.Format("{0}", CRC32.CheckSumSlice(buffer, 0, cnt))); fFile.BodyStream.Seek(0, SeekOrigin.Begin); break; case HttpFileType.FILE_PATH: vPostParams.Add("crc32", string.Format("{0}", CRC32.CheckSumFile(fFile.BodyFile))); break; } } //设置MimeType // FIX: (添加了下一行代码) // 修正上传文件MIME总为octect-stream(原因:未初始化FormFile.ContentType)的问题 // @fengyh 2016-08-17 14:50 fFile.ContentType = uploadOptions.MimeType; //设置扩展参数 foreach (KeyValuePair <string, string> kvp in uploadOptions.ExtraParams) { vPostParams.Add(kvp.Key, kvp.Value); } //设置进度处理和取消信号 ProgressHandler fUpProgressHandler = new ProgressHandler(delegate(long bytesWritten, long totalBytes) { double percent = (double)bytesWritten / totalBytes; //这样做是为了等待回复 if (percent > 0.95) { percent = 0.95; } uploadOptions.ProgressHandler(key, percent); }); CancellationSignal fCancelSignal = new CancellationSignal(delegate() { return(uploadOptions.CancellationSignal()); }); //第一次失败后使用备用域名重试一次 CompletionHandler fUpCompletionHandler = new CompletionHandler(delegate(ResponseInfo respInfo, string response) { Console.WriteLine("form upload result, {0}", respInfo.StatusCode); if (respInfo.needRetry()) { if (fFile.BodyStream != null) { fFile.BodyStream.Seek(0, SeekOrigin.Begin); } CompletionHandler retried = new CompletionHandler(delegate(ResponseInfo retryRespInfo, string retryResponse) { Console.WriteLine("form upload retry result, {0}", retryRespInfo.StatusCode); if (respInfo.isOk()) { uploadOptions.ProgressHandler(key, 1.0); } if (fFile.BodyStream != null) { fFile.BodyStream.Close(); } if (upCompletionHandler != null) { try { upCompletionHandler(key, retryRespInfo, retryResponse); } catch (Exception ex) { Console.WriteLine("form upload retry completion error, {0}", ex.Message); } } }); this.mHttpManager.postMultipartDataForm(Config.ZONE.UploadHost, null, vPostParams, fFile, fUpProgressHandler, retried); } else { if (respInfo.isOk()) { uploadOptions.ProgressHandler(key, 1.0); } if (fFile.BodyStream != null) { fFile.BodyStream.Close(); } if (upCompletionHandler != null) { try { upCompletionHandler(key, respInfo, response); } catch (Exception ex) { Console.WriteLine("form upload completion error, {0}", ex.Message); } } } }); this.mHttpManager.postMultipartDataForm(Config.ZONE.UploadHost, null, vPostParams, fFile, fUpProgressHandler, fUpCompletionHandler); }
public static uint Quick(byte[] buffer) { return(~CRC32.BufferHash(CRC32.Default.Table, -1, buffer, 0, (int)buffer.Length)); }
static void WriteData(Dictionary <string, string> resPathDic) { AssetBundleConfig config = new AssetBundleConfig(); config.ABList = new List <ABBase>(); foreach (string path in resPathDic.Keys) { if (!ValidPath(path)) { continue; } ABBase abBase = new ABBase(); abBase.Path = path; abBase.Crc = CRC32.GetCrc32(path); abBase.ABName = resPathDic[path]; abBase.AssetName = path.Remove(0, path.LastIndexOf("/") + 1); abBase.ABDependce = new List <string>(); string[] resDependce = AssetDatabase.GetDependencies(path); for (int i = 0; i < resDependce.Length; i++) { string tempPath = resDependce[i]; if (tempPath == path || path.EndsWith(".cs")) { continue; } string abName = ""; if (resPathDic.TryGetValue(tempPath, out abName)) { if (abName == resPathDic[path]) { continue; } if (!abBase.ABDependce.Contains(abName)) { abBase.ABDependce.Add(abName); } } } config.ABList.Add(abBase); } //写入xml string xmlPath = Application.dataPath + "/AssetBundleConfig.xml"; if (File.Exists(xmlPath)) { File.Delete(xmlPath); } FileStream fileStream = new FileStream(xmlPath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); StreamWriter sw = new StreamWriter(fileStream, System.Text.Encoding.UTF8); XmlSerializer xs = new XmlSerializer(config.GetType()); xs.Serialize(sw, config); sw.Close(); fileStream.Close(); //写入二进制 foreach (ABBase abBase in config.ABList) { abBase.Path = ""; } FileStream fs = new FileStream(ABBYTEPATH, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); fs.Seek(0, SeekOrigin.Begin); fs.SetLength(0); BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(fs, config); fs.Close(); AssetDatabase.Refresh(); SetABName("AssetBundleConfig", ABBYTEPATH); }
public static uint Quick(uint polynomial, uint seed, byte[] buffer) { return(~CRC32.BufferHash(CRC32.Default.Table, seed, buffer, 0, (int)buffer.Length)); }
private FileHash CalculateFileHash(string rootFolder, string path, Dictionary <uint, FileHash> checkSums) { CRC32 crc32 = new CRC32(); // check if this is a folder if (Directory.Exists(path)) { FileHash folder = new FileHash(); folder.IsFolder = true; folder.Name = Path.GetFileName(path); folder.FullName = path.Substring(rootFolder.Length - 1); // process child folders and files List <string> childFiles = new List <string>(); childFiles.AddRange(Directory.GetDirectories(path)); childFiles.AddRange(Directory.GetFiles(path)); foreach (string childFile in childFiles) { FileHash childHash = CalculateFileHash(rootFolder, childFile, checkSums); folder.Files.Add(childHash); // check sum folder.CheckSum += childHash.CheckSum; folder.CheckSum += ConvertCheckSumToInt(crc32.ComputeHash(Encoding.UTF8.GetBytes(childHash.Name))); //Debug.WriteLine(folder.CheckSum + " : " + folder.FullName); } // move list to array folder.FilesArray = folder.Files.ToArray(); if (!checkSums.ContainsKey(folder.CheckSum)) { checkSums.Add(folder.CheckSum, folder); } return(folder); } FileHash file = new FileHash(); file.Name = Path.GetFileName(path); file.FullName = path.Substring(rootFolder.Length - 1); // calculate CRC32 using (FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read)) { file.CheckSum = ConvertCheckSumToInt( crc32.ComputeHash(fs)); } if (!checkSums.ContainsKey(file.CheckSum)) { checkSums.Add(file.CheckSum, file); } //Debug.WriteLine(file.CheckSum + " : " + file.FullName); return(file); }
public static uint String(string str) { return(CRC32.Quick(Encoding.ASCII.GetBytes(str))); }
public static void SerializeDirToPak(string dir, string pakfile) { string[] allFiles = recursiveGetFiles(dir, ""); List<Dir> dirEntryList = new List<Dir>(); using (FormattedWriter fw = new FormattedWriter(pakfile)) { // Write files foreach (var file in allFiles) { fw.Write(new Signature() { signature = stringFileHeader2 }); File f = new File(); byte[] bytes = System.IO.File.ReadAllBytes(Path.Combine(dir, file)); byte[] compressed = decryptBytesWithTable(ZlibCodecCompress(bytes), 0x3ff, 1, table2); f.compressedSize = (uint)compressed.Length; f.compressionMethod = 8; CRC32 dataCheckSum = new CRC32(); f.crc = dataCheckSum.GetCrc32(new MemoryStream(bytes)); f.data = compressed; f.extractSystem = 0; f.extractVersion = 20; f.extraField = new byte[0]; f.extraFieldLength = 0; f.filename = Encoding.UTF8.GetBytes(file); f.filenameLength = (ushort)f.filename.Length; f.generalPurposeFlagBits = 0; f.lastModDate = 0; f.lastModTime = 0; f.uncompressedSize = (uint)bytes.Length; fw.Write(f); Dir d = new Dir(); d.comment = new byte[0]; d.commentLength = 0; d.compressedSize = f.compressedSize; d.compressType = f.compressionMethod; d.crc = f.crc; d.createSystem = f.extractSystem; d.createVersion = f.extractVersion; d.date = f.lastModDate; d.diskNumberStart = 0; d.externalFileAttributes = 33; d.extractSystem = f.extractSystem; d.extractVersion = f.extractVersion; d.extraField = new byte[0]; d.extraFieldLength = 0; d.filename = f.filename; d.filenameLength = f.filenameLength; d.flagBits = f.generalPurposeFlagBits; d.internalFileAttributes = 0; d.localHeaderOffset = 0; d.time = f.lastModTime; d.uncompressedSize = f.uncompressedSize; dirEntryList.Add(d); } var dirEntryListStartPos = fw.BaseStream.BaseStream.Position; foreach (var d in dirEntryList) { fw.Write(new Signature() { signature = stringCentralDir2 }); fw.Write(d); } var dirEntryListEndPos = fw.BaseStream.BaseStream.Position; End e = new End(); e.byteSizeOfCentralDirectory = (uint)(dirEntryListEndPos - dirEntryListStartPos); e.centralDirRecordsOnDisk = (short)dirEntryList.Count; e.centralDirStartDisk = 0; e.comment = new byte[0]; e.commentLength = 0; e.diskNumber = 0; e.offsetOfStartOfCentralDirectory = (uint)dirEntryListStartPos; e.totalNumOfCentralDirRecords = (short)dirEntryList.Count; fw.Write(new Signature() { signature = stringEndArchive2 }); fw.Write(e); } }
public CRC32(uint polynomial, uint seed) { this.table = (polynomial != -306674912 ? CRC32.ProcessHashTable(polynomial) : CRC32.Default.Table); this.seed = seed; this.Initialize(); }
//写依赖包 private static void WriteData(Dictionary <string, string> assetPathDic) { AssetBundleLoadProfile assetBundleLoadProfile = new AssetBundleLoadProfile(); assetBundleLoadProfile.ABList = new List <ABBase>(); foreach (var item in assetPathDic) { ABBase aBBase = new ABBase { Path = item.Key, Crc = CRC32.GetCRC32(item.Key), AssetBundleName = item.Value, AssetName = item.Key.Remove(0, item.Key.LastIndexOf('/') + 1), DependentBundles = new List <string>() }; string[] dependencies = AssetDatabase.GetDependencies(aBBase.Path); for (int i = 0; i < dependencies.Length; i++) { string path = dependencies[i]; if (path == aBBase.Path || path.EndsWith(".cs")) { continue; } string assetBundleName; if (assetPathDic.TryGetValue(path, out assetBundleName)) { if (assetBundleName.Equals(item.Value)) { continue; } if (!aBBase.DependentBundles.Contains(assetBundleName)) { aBBase.DependentBundles.Add(assetBundleName); } } } assetBundleLoadProfile.ABList.Add(aBBase); } if (File.Exists(GetFrameProfile().ABLoadXmlPath)) { File.Delete(GetFrameProfile().ABLoadXmlPath); } FileStream fs = new FileStream(GetFrameProfile().ABLoadXmlPath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); XmlSerializer xmlSerializer = new XmlSerializer(typeof(AssetBundleLoadProfile)); xmlSerializer.Serialize(fs, assetBundleLoadProfile); fs.Close(); assetBundleLoadProfile.ABList.ForEach(aBBase => { aBBase.Path = ""; }); FileStream fs2 = new FileStream(GetFrameProfile().ABLoadBytesPath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite ); BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(fs2, assetBundleLoadProfile); fs2.Close(); // AssetDatabase.Refresh(); }
/// <summary> /// Writes a file to the cache and updates the <seealso cref="ReferenceTable" /> that /// it is associated with. /// </summary> /// <param name="type"> The type of file. </param> /// <param name="file"> The file id. </param> /// <param name="container"> The <seealso cref="Container" /> to write. </param> /// <exception cref="IOException"> if an I/O error occurs. </exception> public void Write(int type, int file, Container container) { /* we don't want people reading/manipulating these manually */ if (type == 255) { throw new IOException("Reference tables can only be modified with the low level FileStore API!"); } /* increment the container's version */ container.SetVersion(container.GetVersion() + 1); /* decode the reference table for this index */ var tableContainer = Container.Decode(store.Read(255, type)); var table = ReferenceTable.Decode(tableContainer.GetData()); /* grab the bytes we need for the checksum */ var buffer = container.Encode(); var bytes = new byte[buffer.limit() - 2]; // last two bytes are the version and shouldn't be included buffer.mark(); try { buffer.position(0); buffer.get(bytes, 0, bytes.Length); } finally { buffer.reset(); } /* calculate the new CRC checksum */ var crc = new CRC32(); crc.update(bytes, 0, bytes.Length); /* update the version and checksum for this file */ var entry = table.GetEntry(file); if (entry == null) { /* create a new entry for the file */ entry = new ReferenceTable.Entry(); table.PutEntry(file, entry); } entry.SetVersion(container.GetVersion()); entry.SetCrc((int)crc.getValue()); /* calculate and update the whirlpool digest if we need to */ if ((table.GetFlags() & ReferenceTable.FLAG_WHIRLPOOL) != 0) { var whirlpool = Whirlpool.Crypt(bytes, 0, bytes.Length); entry.SetWhirlpool(whirlpool); } /* update the reference table version */ table.SetVersion(table.GetVersion() + 1); /* save the reference table */ tableContainer = new Container(tableContainer.GetType(), table.Encode()); store.Write(255, type, tableContainer.Encode()); /* save the file itself */ store.Write(type, file, buffer); }
private byte[] getCrc32(byte[] data, long dataLength) { CRC32 crc32 = new CRC32(); crc32.Initialize(); byte[] hash = crc32.ComputeHash(data, 0, (int)dataLength); Array.Reverse(hash); return hash; }
/// <summary> /// Computes a standard CRC-32 (polynomial 0xedb88320) on a buffer of data. /// </summary> /// <param name="data">Buffer to process.</param> /// <returns>CRC value.</returns> public static uint ComputeBufferCRC(byte[] data) { return(CRC32.OnBuffer(0, data, 0, data.Length)); }
private void WriteChunk( string type, byte[] data, int offset, int length ) { // Write out the length to the PNG. WriteInteger( _stream, length ); // Write the chunck type out to the PNG. byte[] typeArray = new byte[4]; typeArray[0] = (byte) type[0]; typeArray[1] = (byte) type[1]; typeArray[2] = (byte) type[2]; typeArray[3] = (byte) type[3]; _stream.Write( typeArray, 0, 4 ); // If we have some data to write out (some chunk types don't), the do so. if ( data != null ) { _stream.Write( data, offset, length ); } // All chunk types need to have a CRC32 value at their end to make sure they haven't been currupted. CRC32 crcCode = new CRC32(); crcCode.Add( typeArray, 4 ); if ( data != null ) { crcCode.Add( data, length, (uint) offset ); } WriteInteger( _stream, crcCode.CurrentValue ); }
private void SendData() { _logger.Log("Sending data...", LoggingLevel.Info); if (_receivedSectorCount == 1) { _logger.Log("Reading sector " + _receivedSectorIndex, LoggingLevel.Debug); } else { _logger.Log("Reading sectors " + _receivedSectorIndex + " - " + (_receivedSectorIndex + _receivedSectorCount - 1), LoggingLevel.Debug); } byte[] sendDataBuffer = _localDisk.ReadSectors(Convert.ToInt32(_receivedSectorIndex), Convert.ToInt32(_receivedSectorCount)); UInt32 crc32Checksum = CRC32.CalculateCRC32(sendDataBuffer); SerialFlags serialFlags = SerialFlags.None; if (_compressionIsEnabled) { serialFlags |= SerialFlags.Compression; } _logger.Log($"Sending serial flags: {serialFlags}...", LoggingLevel.Debug); _serialPort.BaseStream.WriteByte(Convert.ToByte(serialFlags)); var numUncompressedBytes = sendDataBuffer.Length; string sendingMessage = $"Sending {numUncompressedBytes} bytes"; if (serialFlags.HasFlag(SerialFlags.Compression)) { sendDataBuffer = Utilities.LZ4.CompressAsStandardLZ4Block(sendDataBuffer); sendingMessage = $"Sending {sendDataBuffer.Length} bytes"; _transferStartDateTime = DateTime.Now; byte[] dataLenBuffer = new byte[4]; dataLenBuffer[0] = (byte)((sendDataBuffer.Length >> 24) & 0xff); dataLenBuffer[1] = (byte)((sendDataBuffer.Length >> 16) & 0xff); dataLenBuffer[2] = (byte)((sendDataBuffer.Length >> 8) & 0xff); dataLenBuffer[3] = (byte)(sendDataBuffer.Length & 0xff); float percentageOfOriginalSize = (100 / (float)numUncompressedBytes) * sendDataBuffer.Length; _logger.Log($"Compression: { percentageOfOriginalSize.ToString("00.00")}% of { numUncompressedBytes} bytes", LoggingLevel.Debug); _serialPort.BaseStream.Write(dataLenBuffer, 0, dataLenBuffer.Length); } _logger.Log(sendingMessage, LoggingLevel.Info); for (int i = 0; i < sendDataBuffer.Length; i++) { _serialPort.BaseStream.WriteByte(sendDataBuffer[i]); string percentSent = ((Convert.ToDecimal(i + 1) / sendDataBuffer.Length) * 100).ToString("00.00"); Console.Write($"\rSent [{(i + 1).ToString("D" + sendDataBuffer.Length.ToString().Length)} / {sendDataBuffer.Length} Bytes] {percentSent}% "); } Console.WriteLine(); byte[] crc32Buffer = new byte[4]; crc32Buffer[0] = (byte)((crc32Checksum >> 24) & 0xff); crc32Buffer[1] = (byte)((crc32Checksum >> 16) & 0xff); crc32Buffer[2] = (byte)((crc32Checksum >> 8) & 0xff); crc32Buffer[3] = (byte)(crc32Checksum & 0xff); _logger.Log("Sending CRC32...", LoggingLevel.Debug); _serialPort.BaseStream.Write(crc32Buffer, 0, crc32Buffer.Length); _state = ReceiverState.ReceiveStartMagic; _logger.Log($"Receiver state: {_state}", LoggingLevel.Debug); }
/// <summary> /// A constructor allowing the specification of the length of the stream /// to read, as well as whether to keep the underlying stream open upon /// Close(), and the CRC32 instance to use. /// </summary> /// <remarks> /// <para> /// The stream uses the specified CRC32 instance, which allows the /// application to specify how the CRC gets calculated. /// </para> /// </remarks> /// <param name="stream">The underlying stream</param> /// <param name="length">The length of the stream to slurp</param> /// <param name="leaveOpen">true to leave the underlying stream /// open upon close of the <c>CrcCalculatorStream</c>; false otherwise.</param> /// <param name="crc32">the CRC32 instance to use to calculate the CRC32</param> public CrcCalculatorStream(System.IO.Stream stream, Int64 length, bool leaveOpen, CRC32 crc32) : this(leaveOpen, length, stream, crc32) { if (length < 0) throw new ArgumentException("length"); }
private void NetworkLoop() { while (!Cancellator.IsCancellationRequested) { try { if (!Socket.Poll(100000, SelectMode.SelectRead)) { continue; } } catch (SocketException) { break; } byte[] Data; try { byte[] Buffer = new byte[8]; Stream.Read(Buffer, 0, 8); int PacketLength = ToInt32(Buffer, 0); if (ToUInt32(Buffer, 4) != 0x31305456U) { break; } Data = new byte[PacketLength]; int Offset = 0; do { int BytesRead = Stream.Read(Data, Offset, PacketLength); if (BytesRead == 0) { break; } Offset += BytesRead; PacketLength -= BytesRead; } while (PacketLength > 0); if (PacketLength != 0) { break; } } catch (IOException) { break; } try { if (State == EncryptionState.Encrypted) { MessageReceived(Encryptor.Decrypt(Data)); } else { MessageType Type = MessageType.Invalid; if (Data.Length > 3) { Type = (MessageType)(ToUInt32(Data, 0) & ~0x80000000U); } if (State == EncryptionState.Connected && Type == MessageType.ChannelEncrypt) { RawMessage <ChannelEncrypt> Message = new RawMessage <ChannelEncrypt>(Data); if (Message.Payload.Length >= 16) { RawMessage <ChannelEncrypt> Response = new RawMessage <ChannelEncrypt>(MessageType.ChannelEncryptResponse); byte[] Challenge = Message.Payload, EncryptedBlob, SessionKey = new byte[32]; using (RandomNumberGenerator RNG = RandomNumberGenerator.Create()) RNG.GetBytes(SessionKey); using (RSA RSA = RSA.Create()) { RSA.ImportParameters(Parameters); byte[] BlobToEncrypt = new byte[32 + Challenge.Length]; Copy(SessionKey, BlobToEncrypt, 32); Copy(Challenge, 0, BlobToEncrypt, 32, Challenge.Length); EncryptedBlob = RSA.Encrypt(BlobToEncrypt, RSAEncryptionPadding.OaepSHA1); } byte[] CRCHash; using (CRC32 CRC = new CRC32()) CRCHash = CRC.ComputeHash(EncryptedBlob); int Length = EncryptedBlob.Length; Response.Payload = new byte[Length + 8]; Copy(EncryptedBlob, Response.Payload, Length); Copy(CRCHash, 0, Response.Payload, Length, 4); Encryptor = new EncryptionFilter(SessionKey); State = EncryptionState.Challenged; Send(Response.Serialize()); } else { Disconnect(false); } } else if (State == EncryptionState.Challenged && Type == MessageType.ChannelEncryptResult) { if (new RawMessage <ChannelEncryptResult>(Data).Body.Success) { State = EncryptionState.Encrypted; Log($"Established encrypted TCP connection to {EndpointAddress}"); Connected?.Invoke(); } else { Disconnect(false); } } else { Disconnect(false); } } } catch { } } bool UserInitiated = Cancellator.IsCancellationRequested; if (UserInitiated) { Shutdown(); } Release(UserInitiated); }
private EReaderProcessor(Pdb pdb, string name, string ccNumber) { if (!(pdb.Filetype == "PNRd" && pdb.Creator == "PPrs")) throw new FormatException("Invalid eReader file."); pdbReader = pdb; var eReaderPdb = new EReaderPdb(pdbReader); if (!eReaderPdb.HaveDrm) throw new InvalidOperationException("File doesn't have DRM or have unknown DRM version: {0}!"); if (!(eReaderPdb.CompressionMethod == EReaderCompression.Drm1 || eReaderPdb.CompressionMethod == EReaderCompression.Drm2)) throw new InvalidOperationException(string.Format("Unsupported compression method or DRM version: {0}!", eReaderPdb.CompressionMethod)); data = pdbReader.GetSection(1); ICryptoTransform desEngine = GetDesEngine(data.Copy(0, 8)); byte[] decryptedData = desEngine.TransformFinalBlock(data.Copy(-8), 0, 8); int cookieShuf = decryptedData[0] << 24 | decryptedData[1] << 16 | decryptedData[2] << 8 | decryptedData[3]; int cookieSize = decryptedData[4] << 24 | decryptedData[5] << 16 | decryptedData[6] << 8 | decryptedData[7]; if (cookieShuf < 0x03 || cookieShuf > 0x14 || cookieSize < 0xf0 || cookieSize > 0x200) throw new InvalidOperationException("Unsupportd eReader format"); byte[] input = desEngine.TransformFinalBlock(data.Copy(-cookieSize), 0, cookieSize); byte[] r = UnshuffData(input.SubRange(0, -8), cookieShuf); //using (var stream = new FileStream(pdb.Filename+".eReaderSection1Dump", FileMode.Create, FileAccess.ReadWrite, FileShare.Read)) stream.Write(r, 0, r.Length); byte[] userKeyPart1 = Encoding.ASCII.GetBytes(FixUserName(name)); byte[] userKeyPart2 = Encoding.ASCII.GetBytes(ccNumber.ToCharArray().Copy(-8)); long userKey; using (var stream1 = new MemoryStream(userKeyPart1)) using (var stream2 = new MemoryStream(userKeyPart2)) { userKey = new CRC32().GetCrc32(stream1) & 0xffffffff; userKey = userKey << 32 | new CRC32().GetCrc32(stream2) & 0xffffffff; } var drmSubVersion = (ushort)(r[0] << 8 | r[1]); numTextPages = (ushort)(r[2] << 8 | r[3]) - 1; flags = r[4] << 24 | r[5] << 16 | r[6] << 8 | r[7]; firstImagePage = (ushort)(r[24] << 8 | r[25]); numImagePages = (ushort)(r[26] << 8 | r[27]); if ((flags & ReqdFlags) != ReqdFlags) throw new InvalidOperationException(string.Format("Unsupported flags combination: {0:x8}", flags)); byte[] userKeyArray = BitConverter.GetBytes(userKey); if (BitConverter.IsLittleEndian) userKeyArray = userKeyArray.Reverse(); desEngine = GetDesEngine(userKeyArray); byte[] encryptedKey = new byte[0], encryptedKeySha = new byte[0]; if (eReaderPdb.CompressionMethod == EReaderCompression.Drm1) { if (drmSubVersion != 13) throw new InvalidOperationException(string.Format("Unknown eReader DRM subversion ID: {0}", drmSubVersion)); encryptedKey = r.Copy(44, 8); encryptedKeySha = r.Copy(52, 20); } else if (eReaderPdb.CompressionMethod == EReaderCompression.Drm2) { encryptedKey = r.Copy(172, 8); encryptedKeySha = r.Copy(56, 20); } contentKey = desEngine.TransformFinalBlock(encryptedKey, 0, encryptedKey.Length); byte[] checkHash = SHA1.Create().ComputeHash(contentKey); if (!encryptedKeySha.IsEqualTo(checkHash)) { var s = new StringBuilder(); for (var x = 0; x < (r.Length - 8); x += 2) { for (var y = 0; y < (x - 20); y += 2) if (TestKeyDecryption(desEngine, r, x, y)) s.AppendFormat("keyOffset={0}, hashOffset={1}\n", x, y); for (var y = x + 8; y < (r.Length - 20); y += 2) if (TestKeyDecryption(desEngine, r, x, y)) s.AppendFormat("keyOffset={0}, hashOffset={1}\n", x, y); } if (s.Length > 0) throw new InvalidDataException("Key and/or KeyHash offset mismatch. Possible values:\n\n" + s); throw new ArgumentException("Incorrect Name of Credit Card number."); } contentDecryptor = GetDesEngine(contentKey); }
public static byte[] ComputeCrc32(byte[] buffer, int start, int len) { byte[] result = new CRC32().ComputeHash(buffer, start, len); return(new byte[] { result[3], result[2], result[1], result[0] }); }
/// <summary> /// [Deprecated] Do Not Use! ( CheckSum calculated in Background Worker Object in fileSplitter Form ) /// </summary> public void CalculateMD5ofFile() { CRC32 crchash = new CRC32(); FileStream fOpen; byte[] buffer = new byte[8192]; HashAlgorithm crc32 = (HashAlgorithm)(crchash); long bytesHashed=0; //Start Hashing crc32.Initialize(); using (fOpen = File.Open(filePath, FileMode.Open, FileAccess.ReadWrite)) { while (bytesHashed != fOpen.Length) { bytesHashed += fOpen.Read(buffer, 0, buffer.Length); ((CRC32)crc32).CalcHashBuffer(buffer, 0, buffer.Length); currentHashProg = ((float)bytesHashed / (float)fOpen.Length) * 100; } } this.MD5Sum=((CRC32)crc32).CalcHashFinal(); }
public ZipReturn LocalFileHeaderRead() { try { TrrntZip = true; BinaryReader br = new BinaryReader(_zipFs); _zipFs.Position = (long)RelativeOffsetOfLocalHeader; uint thisSignature = br.ReadUInt32(); if (thisSignature != LocalFileHeaderSignature) return ZipReturn.ZipLocalFileHeaderError; br.ReadUInt16(); // version needed to extract ushort generalPurposeBitFlagLocal = br.ReadUInt16(); if (generalPurposeBitFlagLocal != _generalPurposeBitFlag) TrrntZip = false; ushort tshort = br.ReadUInt16(); if (tshort != _compressionMethod) return ZipReturn.ZipLocalFileHeaderError; tshort = br.ReadUInt16(); if (tshort != _lastModFileTime) return ZipReturn.ZipLocalFileHeaderError; tshort = br.ReadUInt16(); if (tshort != _lastModFileDate) return ZipReturn.ZipLocalFileHeaderError; byte[] tCRC = ReadCRC(br); if (((_generalPurposeBitFlag & 8) == 0) && !ByteArrCompare(tCRC, CRC)) return ZipReturn.ZipLocalFileHeaderError; uint tCompressedSize = br.ReadUInt32(); if (Zip64 && tCompressedSize != 0xffffffff && tCompressedSize != _compressedSize) // if Zip64 File then the compressedSize should be 0xffffffff return ZipReturn.ZipLocalFileHeaderError; if ((_generalPurposeBitFlag & 8) == 8 && tCompressedSize != 0) // if bit 4 set then no compressedSize is set yet return ZipReturn.ZipLocalFileHeaderError; if (!Zip64 && (_generalPurposeBitFlag & 8) != 8 && tCompressedSize != _compressedSize) // check the compressedSize return ZipReturn.ZipLocalFileHeaderError; uint tUnCompressedSize = br.ReadUInt32(); if (Zip64 && tUnCompressedSize != 0xffffffff && tUnCompressedSize != UncompressedSize) // if Zip64 File then the unCompressedSize should be 0xffffffff return ZipReturn.ZipLocalFileHeaderError; if ((_generalPurposeBitFlag & 8) == 8 && tUnCompressedSize != 0) // if bit 4 set then no unCompressedSize is set yet return ZipReturn.ZipLocalFileHeaderError; if (!Zip64 && (_generalPurposeBitFlag & 8) != 8 && tUnCompressedSize != UncompressedSize) // check the unCompressedSize return ZipReturn.ZipLocalFileHeaderError; ushort fileNameLength = br.ReadUInt16(); ushort extraFieldLength = br.ReadUInt16(); byte[] bFileName = br.ReadBytes(fileNameLength); string tFileName = (generalPurposeBitFlagLocal & (1 << 11)) == 0 ? GetString(bFileName) : Encoding.UTF8.GetString(bFileName, 0, fileNameLength); byte[] extraField = br.ReadBytes(extraFieldLength); Zip64 = false; int pos = 0; while (extraFieldLength > pos) { ushort type = BitConverter.ToUInt16(extraField, pos); pos += 2; ushort blockLength = BitConverter.ToUInt16(extraField, pos); pos += 2; switch (type) { case 0x0001: Zip64 = true; if (tUnCompressedSize == 0xffffffff) { ulong tLong = BitConverter.ToUInt64(extraField, pos); if (tLong != UncompressedSize) return ZipReturn.ZipLocalFileHeaderError; pos += 8; } if (tCompressedSize == 0xffffffff) { ulong tLong = BitConverter.ToUInt64(extraField, pos); if (tLong != _compressedSize) return ZipReturn.ZipLocalFileHeaderError; pos += 8; } break; case 0x7075: //byte version = extraField[pos]; pos += 1; uint nameCRC32 = BitConverter.ToUInt32(extraField, pos); pos += 4; CRC32 crcTest = new CRC32(); crcTest.SlurpBlock(bFileName, 0, fileNameLength); uint fCRC = crcTest.Crc32ResultU; if (nameCRC32 != fCRC) return ZipReturn.ZipLocalFileHeaderError; int charLen = blockLength - 5; tFileName = Encoding.UTF8.GetString(extraField, pos, charLen); pos += charLen; break; default: pos += blockLength; break; } } if (!CompareString(FileName, tFileName)) return ZipReturn.ZipLocalFileHeaderError; _dataLocation = (ulong)_zipFs.Position; if ((_generalPurposeBitFlag & 8) == 0) return ZipReturn.ZipGood; _zipFs.Position += (long)_compressedSize; tCRC = ReadCRC(br); if (!ByteArrCompare(tCRC, new byte[] { 0x50, 0x4b, 0x07, 0x08 })) tCRC = ReadCRC(br); if (!ByteArrCompare(tCRC, CRC)) return ZipReturn.ZipLocalFileHeaderError; uint tint = br.ReadUInt32(); if (tint != _compressedSize) return ZipReturn.ZipLocalFileHeaderError; tint = br.ReadUInt32(); if (tint != UncompressedSize) return ZipReturn.ZipLocalFileHeaderError; return ZipReturn.ZipGood; } catch { return ZipReturn.ZipLocalFileHeaderError; } }
/// <summary> /// 写入数据(xml文件 二进制文件) /// </summary> static void writeData(Dictionary <string, string> resPathDict) { AssetBundleConfig config = new AssetBundleConfig(); config.ABBaseList = new List <ABBase>(); foreach (string path in resPathDict.Keys) { ABBase abBase = new ABBase(); abBase.Path = path; abBase.Crc = CRC32.GetCRC32(path); abBase.ABName = resPathDict[path]; abBase.AssetName = path.Remove(0, path.LastIndexOf("/") + 1); abBase.ABDependceList = new List <string>(); string[] resDependce = AssetDatabase.GetDependencies(path); for (int i = 0; i < resDependce.Length; i++) { string tempPath = resDependce[i]; if (tempPath == path || path.EndsWith(".cs")) { continue; } string abName; if (resPathDict.TryGetValue(tempPath, out abName)) { if (abName == resPathDict[path]) { continue; } if (!abBase.ABDependceList.Contains(abName)) { abBase.ABDependceList.Add(abName); } } } config.ABBaseList.Add(abBase); } //写入xml string xmlPath = Application.dataPath + "/AssetBundleConfig.xml"; if (File.Exists(xmlPath)) { File.Delete(xmlPath); } FileStream fs = new FileStream(xmlPath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8); XmlSerializer xmlSerializer = new XmlSerializer(config.GetType()); xmlSerializer.Serialize(sw, config); sw.Close(); fs.Close(); //写入二进制 foreach (ABBase abBase in config.ABBaseList) { //xml中的Path,只是为了方便查看路径.实际的加载是通过CRC进行.在二进制文件中清空path,可以减少二进制文件的大小 abBase.Path = string.Empty; } string bytePath = mAssetBundleBytesPath; FileStream fileStream = new FileStream(bytePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(fileStream, config); fileStream.Close(); }
public ZipReturn CenteralDirectoryRead() { try { BinaryReader br = new BinaryReader(_zipFs); uint thisSignature = br.ReadUInt32(); if (thisSignature != CentralDirectoryHeaderSigniature) return ZipReturn.ZipCenteralDirError; br.ReadUInt16(); // Version Made By br.ReadUInt16(); // Version Needed To Extract _generalPurposeBitFlag = br.ReadUInt16(); _compressionMethod = br.ReadUInt16(); if (_compressionMethod != 8 && _compressionMethod != 0) return ZipReturn.ZipUnsupportedCompression; _lastModFileTime = br.ReadUInt16(); _lastModFileDate = br.ReadUInt16(); CRC = ReadCRC(br); _compressedSize = br.ReadUInt32(); UncompressedSize = br.ReadUInt32(); ushort fileNameLength = br.ReadUInt16(); ushort extraFieldLength = br.ReadUInt16(); ushort fileCommentLength = br.ReadUInt16(); br.ReadUInt16(); // diskNumberStart br.ReadUInt16(); // internalFileAttributes br.ReadUInt32(); // externalFileAttributes RelativeOffsetOfLocalHeader = br.ReadUInt32(); byte[] bFileName = br.ReadBytes(fileNameLength); FileName = (_generalPurposeBitFlag & (1 << 11)) == 0 ? GetString(bFileName) : Encoding.UTF8.GetString(bFileName, 0, fileNameLength); Byte[] extraField = br.ReadBytes(extraFieldLength); br.ReadBytes(fileCommentLength); // File Comments int pos = 0; while (extraFieldLength > pos) { ushort type = BitConverter.ToUInt16(extraField, pos); pos += 2; ushort blockLength = BitConverter.ToUInt16(extraField, pos); pos += 2; switch (type) { case 0x0001: Zip64 = true; if (UncompressedSize == 0xffffffff) { UncompressedSize = BitConverter.ToUInt64(extraField, pos); pos += 8; } if (_compressedSize == 0xffffffff) { _compressedSize = BitConverter.ToUInt64(extraField, pos); pos += 8; } if (RelativeOffsetOfLocalHeader == 0xffffffff) { RelativeOffsetOfLocalHeader = BitConverter.ToUInt64(extraField, pos); pos += 8; } break; case 0x7075: //byte version = extraField[pos]; pos += 1; uint nameCRC32 = BitConverter.ToUInt32(extraField, pos); pos += 4; CRC32 crcTest = new CRC32(); crcTest.SlurpBlock(bFileName, 0, fileNameLength); uint fCRC = crcTest.Crc32ResultU; if (nameCRC32 != fCRC) return ZipReturn.ZipCenteralDirError; int charLen = blockLength - 5; FileName = Encoding.UTF8.GetString(extraField, pos, charLen); pos += charLen; break; default: pos += blockLength; break; } } return ZipReturn.ZipGood; } catch { return ZipReturn.ZipCenteralDirError; } }
public static EventId CreateEventId(string fullTypeName, [CallerMemberName] string memberName = "", [CallerLineNumber] int sourceLineNumber = 0) { return(new EventId((int)CRC32.Compute(string.Concat(fullTypeName, memberName)) + sourceLineNumber, memberName)); }
private void _DeflateOne(Object wi) { // compress one buffer WorkItem workitem = (WorkItem) wi; try { CRC32 crc = new CRC32(); // calc CRC on the buffer crc.SlurpBlock(workitem.buffer, 0, workitem.inputBytesAvailable); // deflate it DeflateOneSegment(workitem); // update status workitem.crc = crc.Crc32Result; TraceOutput(TraceBits.Compress, "Compress wi({0}) ord({1}) len({2})", workitem.index, workitem.ordinal, workitem.compressedBytesAvailable ); lock(_latestLock) { if (workitem.ordinal > _latestCompressed) _latestCompressed = workitem.ordinal; } lock (_toWrite) { _toWrite.Enqueue(workitem.index); } _newlyCompressedBlob.Set(); } catch (System.Exception exc1) { lock(_eLock) { // expose the exception to the main thread if (_pendingException!=null) _pendingException = exc1; } } }
public static uint HashCRC32(this string input) { return(CRC32.Hash(input)); }
/// <summary> /// Resets the stream for use with another stream. /// </summary> /// <remarks> /// Because the ParallelDeflateOutputStream is expensive to create, it /// has been designed so that it can be recycled and re-used. You have /// to call Close() on the stream first, then you can call Reset() on /// it, to use it again on another stream. /// </remarks> /// /// <param name="stream"> /// The new output stream for this era. /// </param> /// /// <example> /// <code> /// ParallelDeflateOutputStream deflater = null; /// foreach (var inputFile in listOfFiles) /// { /// string outputFile = inputFile + ".compressed"; /// using (System.IO.Stream input = System.IO.File.OpenRead(inputFile)) /// { /// using (var outStream = System.IO.File.Create(outputFile)) /// { /// if (deflater == null) /// deflater = new ParallelDeflateOutputStream(outStream, /// CompressionLevel.Best, /// CompressionStrategy.Default, /// true); /// deflater.Reset(outStream); /// /// while ((n= input.Read(buffer, 0, buffer.Length)) != 0) /// { /// deflater.Write(buffer, 0, n); /// } /// } /// } /// } /// </code> /// </example> public void Reset(Stream stream) { TraceOutput(TraceBits.Session, "-------------------------------------------------------"); TraceOutput(TraceBits.Session, "Reset {0:X8} firstDone({1})", this.GetHashCode(), _firstWriteDone); if (!_firstWriteDone) return; // reset all status _toWrite.Clear(); _toFill.Clear(); foreach (var workitem in _pool) { _toFill.Enqueue(workitem.index); workitem.ordinal = -1; } _firstWriteDone = false; _totalBytesProcessed = 0L; _runningCrc = new CRC32(); _isClosed= false; _currentlyFilling = -1; _lastFilled = -1; _lastWritten = -1; _latestCompressed = -1; _outStream = stream; }
static void VerifyArgs(string[] args) { string currentArg; int argcounter = 1; bool fullVerify = true; while ((currentArg = args[argcounter++]).StartsWith("-")) { currentArg = currentArg.ToLower(); if (currentArg == "-fullverify") { fullVerify = args[argcounter++].ToLower() == "on"; } else { HaltAndCatchFire($"Unknown command: \"{currentArg}\"", 1); return; } } string filename = args[--argcounter]; if (!File.Exists(filename)) { HaltAndCatchFire($"Input file does not exist: {filename}", 1); } ExtendedArchive arc; try { arc = new ExtendedArchive(filename); } catch (PpexException ex) { if (ex.ErrorCode == PpexException.PpexErrorCode.FileNotPPXArchive || ex.ErrorCode == PpexException.PpexErrorCode.IncorrectVersionNumber) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(ex.Message); Console.ResetColor(); return; } else { throw; } } Console.CursorVisible = false; Action <int, string> UpdateProgress = (i, x) => { Console.SetCursorPosition(0, Console.CursorTop); string message = $"Verification {i}% complete [{x}]"; message = message.PadRight(Console.BufferWidth - 1); Console.Write(message); }; var orderedChunks = arc.Chunks.OrderBy(x => x.Offset).ToArray(); Console.WriteLine("Archive name: " + arc.Title); Console.WriteLine("File count: " + arc.Files.Count); long currentOffset = 0; long totalOffset = orderedChunks.Sum(x => (long)x.CompressedLength); HashSet <(Md5Hash, ulong, ulong)> verifiedHashes = new HashSet <(Md5Hash, ulong, ulong)>(); for (int i = 0; i < orderedChunks.Length; i++) { ExtendedArchiveChunk chunk = orderedChunks[i]; using var compressedBuffer = MemoryPool <byte> .Shared.Rent((int) chunk.CompressedLength); var compressedMemory = compressedBuffer.Memory.Slice(0, (int)chunk.CompressedLength); using (var rawStream = chunk.GetRawStream()) { rawStream.Read(compressedMemory.Span); } if (chunk.CRC32 != CRC32.Compute(compressedMemory.Span)) { HaltAndCatchFire($"Chunk hash mismatch; id: {chunk.ID}, offset: {chunk.Offset}", 8); } if (fullVerify) { using var uncompressedBuffer = MemoryPool <byte> .Shared.Rent((int) chunk.UncompressedLength); var uncompressedMemory = uncompressedBuffer.Memory.Slice(0, (int)chunk.UncompressedLength); chunk.CopyToMemory(uncompressedMemory); foreach (var file in chunk.Files) { var expectedHash = (file.RawSource.Md5, file.RawSource.Offset, file.RawSource.Size); if (verifiedHashes.Contains(expectedHash)) { continue; } var actualMd5 = Utility.GetMd5(uncompressedMemory.Span.Slice((int)file.RawSource.Offset, (int)file.RawSource.Size)); if ((Md5Hash)file.RawSource.Md5 != (Md5Hash)actualMd5) { HaltAndCatchFire($"File md5 mismatch; chunk id: {chunk.ID}, archive: {file.ArchiveName}, file: {file.Name}", 9); } verifiedHashes.Add((actualMd5, file.RawSource.Offset, file.RawSource.Size)); } verifiedHashes.Clear(); } currentOffset += (long)chunk.CompressedLength; //int percentage = (int)((float)(100 * i) / (float)orderedChunks.Length); int percentage = (int)Math.Round((float)(100 * currentOffset) / (float)totalOffset); UpdateProgress(percentage, $"{i + 1}/{orderedChunks.Length} : {ConvertToReadable(currentOffset)}/{ConvertToReadable(totalOffset)}"); } Console.WriteLine(); Console.WriteLine($"\"{arc.Title}\" successfully verified."); Console.CursorVisible = true; }
private bool _ExtractTo( int index, Stream output, int outBufRemain ) { XUnzipFileInfo fileInfo = _GetFileInfo ( index ); BinaryReader br = new BinaryReader ( inputStream ); if ( fileInfo.offsetData == -1 ) { if ( inputStream.Seek ( fileInfo.offsetLocalHeader, SeekOrigin.Begin ) != fileInfo.offsetLocalHeader ) throw new Exception ( XUNZIP_ERR.CANT_READ_FILE.ToString () ); if ( !ReadLocalHeader ( ref fileInfo, br ) ) return false; } inputStream.Seek ( fileInfo.offsetData, SeekOrigin.Begin ); byte [] bufIn; int inputRemain; int outputRemain; int toRead; uint crc32 = 0; bufIn = new byte [ 32768 ]; inputRemain = fileInfo.compressedSize; outputRemain = fileInfo.uncompressedSize; if ( fileInfo.method == XUNZIP_COMPRESSION_METHOD.METHOD_STORE ) { CRC32 crcGen = new CRC32 (); while ( inputRemain > 0 ) { toRead = System.Math.Min ( System.Math.Min ( 32768, inputRemain ), outputRemain ); int temp = br.Read ( bufIn, 0, toRead ); output.Write ( bufIn, 0, toRead ); crcGen.Update ( bufIn, 0, toRead ); crc32 = crcGen.Result; inputRemain -= toRead; outputRemain -= temp; } } else { DeflateStream stream = new DeflateStream ( inputStream, CompressionMode.Decompress ); CRC32 crcGen = new CRC32 (); while ( inputRemain > 0 ) { toRead = System.Math.Min ( System.Math.Min ( 32768, inputRemain ), outputRemain ); int temp = stream.Read ( bufIn, 0, outputRemain ); output.Write ( bufIn, 0, temp ); crcGen.Update ( bufIn, 0, toRead ); crc32 = crcGen.Result; inputRemain -= toRead; outputRemain -= temp; } } //if ( crc32 != fileInfo.crc32 ) //return false; if ( outputRemain > 0 ) return false; output.Position = 0; return true; }
private void formVerify_Shown(object sender, EventArgs e) { prgChunkProgress.Maximum = arc.Chunks.Count; var files = arc.Files; /*arc.Chunks.Where(x => x.Type == ChunkType.Generic) * .SelectMany(x => x.Files) * .Where(x => x.Type == ArchiveFileType.Raw);*/ prgFileProgress.Maximum = files.Count(); Task.Factory.StartNew(() => { int fails = 0; foreach (var chunk in arc.Chunks) { ListViewItem item = null; lsvChunkHash.Invoke(new MethodInvoker(() => { item = lsvChunkHash.Items.Add(chunk.ID.ToString()); item.SubItems.Add(chunk.CRC32.ToString("X8")); })); uint crc; using (MemoryStream mem = new MemoryStream()) using (Stream stream = chunk.GetRawStream()) { stream.CopyTo(mem); mem.Position = 0; crc = CRC32.Compute(mem); } lsvChunkHash.Invoke(new MethodInvoker(() => { item.SubItems.Add(crc.ToString("X8")); if (crc != chunk.CRC32) { item.BackColor = Color.Red; fails++; } prgChunkProgress.Value++; })); } foreach (var file in files) { ListViewItem item = null; lsvFileHash.Invoke(new MethodInvoker(() => { item = lsvFileHash.Items.Add(file.ArchiveName); item.SubItems.Add(file.Name); item.SubItems.Add(((Md5Hash)file.Source.Md5).ToString()); })); Md5Hash hash; using (Stream stream = file.GetRawStream()) { hash = Utility.GetMd5(stream); } lsvFileHash.Invoke(new MethodInvoker(() => { item.SubItems.Add(hash.ToString()); if (!Utility.CompareBytes(file.Source.Md5, hash)) { item.BackColor = Color.Red; fails++; } prgFileProgress.Value++; })); } btnOk.Invoke(new MethodInvoker(() => { MessageBox.Show("Operation completed with " + fails + " hash fails."); btnOk.Enabled = true; })); }); }