Код: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO;
namespace WindowsFormsApplication1 { public partial class Form1 : Form { long x = 0, y, btn2 = 0; byte[] buffer = new byte[1000000]; UInt32 fsize;
static FileStream fsr = new FileStream("D:\\project_name.bin", FileMode.Open, FileAccess.Read); static FileStream fsw = new FileStream("D:\\update.bin", FileMode.Create, FileAccess.Write); BinaryWriter bw = new BinaryWriter(fsw); BinaryReader br = new BinaryReader(fsr);
public Form1() { InitializeComponent(); }
private UInt16 crc16(byte[] data_p, UInt32 length) { byte i; UInt16 data_; UInt16 crc = 0xffff; UInt32 idx = 0;
if (length == 0) return ((UInt16)~crc);
do { for (i = 0, data_ = (UInt16)((UInt16)0xff & (byte)data_p[idx]); i < 8; i++, data_ >>= 1) { if (((crc & 0x0001) ^ (data_ & 0x0001)) != 0) crc = (UInt16)((crc >> 1) ^ 0x8408); else crc = (UInt16)(crc >> 1); } idx++; } while (--length != 0);
crc = (UInt16)~crc; data_ = crc; crc = (UInt16)((crc << 8) | (data_ >> 8 & 0xff));
return (crc); }
private void button1_Click(object sender, EventArgs e) { fsize = (UInt32)fsr.Length; UInt16 crc;
button1.Enabled = false; button1.Update();
buffer[0] = (byte)'S'; buffer[1] = (byte)'e'; buffer[2] = (byte)'C'; buffer[3] = (byte)'r'; buffer[4] = (byte)'E'; buffer[5] = (byte)'t'; br.Read(buffer, 6, (Int32)fsize); for (x = 6; x < fsize + 6; x++) { buffer[x] += (byte)(x-6); buffer[x] = (byte)~buffer[x]; buffer[x] = (byte)((byte)(buffer[x] << 4) | (byte)(buffer[x] >> 4)); buffer[x] += (byte)(x-6); if (x >= 6) bw.Write(buffer[x]); }
crc = crc16(buffer, fsize+6); buffer[fsize + 6] = (byte)(crc); bw.Write(buffer[fsize + 6]); buffer[fsize + 6 + 1] = (byte)(crc >> 8); bw.Write(buffer[fsize + 6 + 1]);
//MessageBox.Show("FILE WRITTEN", "[update.bin]"); br.Close(); bw.Close();
fsr.Close(); fsw.Close();
button1.Text = "FILE [update.bin] WRITTEN!"; button1.Update();
System.Threading.Thread.Sleep(1000);
Form1.ActiveForm.Close(); }
} } |