private void extractUpd(byte[] upd, out byte[] cf, out byte[] cg) { int CFlength = Convert.ToInt32(Oper.ByteArrayToString(Oper.returnportion(upd, 0xC, 4)), 16); int CGlength = Convert.ToInt32(Oper.ByteArrayToString(Oper.returnportion(upd, CFlength + 0xC, 4)), 16); cf = Oper.returnportion(upd, 0, CFlength); cg = Oper.returnportion(upd, CFlength, CGlength); cf = Nand.Nand.decrypt_CF(cf); cg = Nand.Nand.decrypt_CG(cg, cf); crc32 crc = new crc32(); int dash = Convert.ToInt32(Oper.ByteArrayToString(Oper.returnportion(cf, 0x2, 2)), 16); Console.WriteLine("cf_" + dash + ".bin," + crc.CRC(editbl(cf)).ToString("X8").ToLower()); crc = new crc32(); dash = Convert.ToInt32(Oper.ByteArrayToString(Oper.returnportion(cg, 0x2, 2)), 16); Console.WriteLine("cg_" + dash + ".bin," + crc.CRC(editbl(cg)).ToString("X8").ToLower()); }
private void fixup_header(ref byte[] header, byte[] pttable) { crc32 c = new crc32(); byte[] chk_gpt_pt = BitConverter.GetBytes(c.CRC(pttable)); header.Replace(chk_gpt_pt, 88, 4); header.Replace(Oper.StringToByteArray(DISK_GUID), 56, 16); for (int i = 16; i <= 19; i++) { header[i] = 0x00; } c = new crc32(); byte[] chk_header = BitConverter.GetBytes(c.CRC(Oper.returnportion(header, 0, 0x5C))); header.Replace(chk_header, 16, 4); }
public long getCRC(BinaryReader br, long cluster, long size) { long num; byte[] data = new byte[size]; int pos = 0; long num2 = size >> 12; long num3 = size - (num2 << 12); for (long i = cluster; i < (cluster + num2); i += 1L) { num = getOffset(i); br.BaseStream.Seek(num, SeekOrigin.Begin); Buffer.BlockCopy(br.ReadBytes(0x1000), 0, data, pos, 0x1000); pos += 0x1000; } num = getOffset(cluster + num2); br.BaseStream.Seek(num, SeekOrigin.Begin); Buffer.BlockCopy(br.ReadBytes((int)num3), 0, data, pos, (int)num3); crc32 cr = new crc32(); return(cr.CRC(data)); }
int send_data() { const int pr = 10; try { byte[] bdata = new byte[70]; byte[] ip = new byte[4]; int flag; int i; bdata[0] = Convert.ToByte('G'); //Идификатор транзакции bdata[1] = Convert.ToByte('B'); //Идификатор транзакции bdata[2] = Convert.ToByte(0x33); //Идификатор протокола bdata[3] = Convert.ToByte(0x33); //Идификатор протокола bdata[4] = Convert.ToByte(0); //Длина bdata[5] = Convert.ToByte(0x3D); //Длина bdata[6] = Convert.ToByte('A'); //Адрес bdata[7] = Convert.ToByte(0x10); //Функциональный код bdata[8] = Convert.ToByte(0); //Адрес первого регистра bdata[9] = Convert.ToByte(50); //Адрес первого регистра bdata[10] = Convert.ToByte(0); #region byte 1 - alarm bdata[1 + pr] = 0; if (GL.otnositelnoe_otobragenie == true) { bdata[1 + pr] = 1; } else { bdata[1 + pr] = 0; } if (GL.inversion_data == true) { bdata[1 + pr] += 0x02; } if (GL.inversion_rele == true) { bdata[1 + pr] += 0x04; } if (GL.avariya[0].avariya1_predupregdenie0 == 1) { bdata[1 + pr] += 0x10; } //avariya1_predupregdenie0 == 1, когда выбран тип "Авария" if (GL.avariya[1].avariya1_predupregdenie0 == 1) { bdata[1 + pr] += 0x20; } if (GL.avariya[2].avariya1_predupregdenie0 == 1) { bdata[1 + pr] += 0x40; } if (GL.avariya[3].avariya1_predupregdenie0 == 1) { bdata[1 + pr] += 0x80; } #endregion #region byte 2 - timeout_alarm UInt16 time = (UInt16)(GL.timeout_alarm); bdata[3 + pr] = Convert.ToByte(time); //time = Convert.ToUInt16(time >> 8); bdata[2 + pr] = 0; #endregion #region Ethernet setting // IP device flag = string_to_ip(text_ip_new.Text, ref ip, 1); if (flag != 0) { return(1); } for (i = 0; i < 4; i++) { bdata[i + 4 + pr] = ip[i]; } // maska flag = string_to_ip(text_maska_new.Text, ref ip, 2); if (flag != 0) { return(1); } for (i = 0; i < 4; i++) { bdata[i + 8 + pr] = ip[i]; } // IP1 //flag = string_to_ip(text_ip1.Text, ref ip, 0); //if (flag != 0) { return 1; } //for (i = 0; i < 4; i++) { bdata[i + 12 + pr] = ip[i]; } //int sp = Convert.ToInt32(text_sport_new.Text); int dp = Convert.ToInt32(text_dport_new.Text); //bdata[17 + pr] = (byte)(sp & 0x00FF); //bdata[16 + pr] = (byte)((sp >> 8) & 0x00FF); bdata[19 + pr] = (byte)(dp & 0x00FF); bdata[18 + pr] = (byte)((dp >> 8) & 0x00FF); #endregion #region smehenie int smechenie = 20; for (i = 0; i < 8; i++) { double dd = GL.data[i].smeshenie * 2.0; int idd = Convert.ToInt16(dd); bdata[smechenie + pr] = Convert.ToByte((idd >> 8) & 0x00FF); smechenie++; bdata[smechenie + pr] = Convert.ToByte(idd & 0x00FF); smechenie++; } #endregion #region display int ipr = GL.porog_max; bdata[37 + pr] = Convert.ToByte(ipr & 0x00FF); ipr = ipr >> 8; bdata[36 + pr] = Convert.ToByte(ipr & 0x00FF); ipr = GL.porog_min; bdata[39 + pr] = Convert.ToByte(ipr & 0x00FF); ipr = ipr >> 8; bdata[38 + pr] = Convert.ToByte(ipr & 0x00FF); #endregion #region alarm-porogi bdata[40 + pr] = (byte)(((GL.avariya[0].kolvo_avariynih_datchikov << 4) & 0xF0) | (GL.avariya[1].kolvo_avariynih_datchikov & 0x0F)); bdata[41 + pr] = (byte)(((GL.avariya[2].kolvo_avariynih_datchikov << 4) & 0xF0) | (GL.avariya[3].kolvo_avariynih_datchikov & 0x0F)); smechenie = 42; for (i = 0; i < 4; i++) { int idd = GL.avariya[i].porog_max; bdata[smechenie + pr] = Convert.ToByte((idd >> 8) & 0x00FF); smechenie++; bdata[smechenie + pr] = Convert.ToByte(idd & 0x00FF); smechenie++; idd = GL.avariya[i].porog_min; bdata[smechenie + pr] = Convert.ToByte((idd >> 8) & 0x00FF); smechenie++; bdata[smechenie + pr] = Convert.ToByte(idd & 0x00FF); smechenie++; } #endregion byte[] crc = new byte[58 + pr]; for (i = 0; i < 58 + pr; i++) { crc[i] = bdata[i]; } crc32 crc_f = new crc32(); GL.version_proshivki = crc_f.Crc16(crc, 58 + pr); ipr = GL.version_proshivki; bdata[59 + pr] = Convert.ToByte(ipr & 0x00FF); ipr = ipr >> 8; bdata[58 + pr] = Convert.ToByte(ipr & 0x00FF); if (TCP.tcp_start(GL.IP, GL.DPORT) < 0) { return(2); } TCP.tcp_send(bdata, 67, text_ip_old.Text, GL.DPORT); for (int ff = 0; ff < 5; ff++) { byte[] rez = TCP.recive();//text_ip_old.Text, 60, GL.DPORT if (rez == null) { continue; } if (rez[1] == Convert.ToByte('O') && rez[2] == Convert.ToByte('K') && rez[3] == Convert.ToByte('!')) { MessageBox.Show("Параметры успешно установлены!"); TCP.tcp_stop(); return(0); } } TCP.tcp_stop(); return(2); } catch (FormatException ee) { return(1); } catch { return(2); } }
public static DecodeStatus DecodeArticle( Article article) { System.IO.FileStream output = null; System.IO.StreamReader input = null; string line; string decoder = ""; string sdecoder = ""; string outputfile = ""; long outputsize = -1; bool crcfailed = false; crc32 crc = new crc32(); int decodedsegments = 0; foreach( Segment segment in article.Segments) { // Check if the file exists, if not, skip the segment if( !System.IO.File.Exists( System.IO.Path.GetFullPath(Global.m_CacheDirectory + segment.ArticleID))) continue; input = new System.IO.StreamReader( System.IO.Path.GetFullPath(Global.m_CacheDirectory + segment.ArticleID), System.Text.Encoding.GetEncoding("iso-8859-1")); // If uudecode is used, each file is automaticly a new segment if( decoder == "uudecode" || (decoder == "mime" && sdecoder == "base64")) decodedsegments ++; line = input.ReadLine(); while( line != null) { if( decoder == "mime") { if( line.StartsWith( "Content-Transfer-Encoding:")) sdecoder = line.Remove(0, 27); if( line.StartsWith("--=") || line.StartsWith("Content-Type:")) { decoder = ""; sdecoder = ""; outputfile = ""; output.Close(); output = null; } // Perhaps get filename out of this, but its also in the content type if( line.StartsWith( "Content-Disposition:")) line = ""; if( sdecoder == "base64") { if( line.Length % 4 > 0) line = ""; if( line != "") { byte[] buffer; buffer = Convert.FromBase64String( line); output.Write( buffer, 0, buffer.Length); } } } if( decoder == "uudecode") { if( line != "" && line != "end") { char[] buffer = line.ToCharArray(); if( uudecode_checkline( buffer)) { int p = 0; int n = 0; byte ch; n = uudecode_dec( buffer[p]); for( ++p; n > 0; p += 4, n -= 3) { if (n >= 3) { // Error ? if (!(uudecode_is_dec(buffer[p]) && uudecode_is_dec(buffer[p + 1]) && uudecode_is_dec(buffer[p + 2]) && uudecode_is_dec(buffer[p + 3]))) throw new Exception( "33"); ch = (byte)(uudecode_dec(buffer[p+0]) << 2 | uudecode_dec(buffer[p+1]) >> 4); output.WriteByte(ch); ch = (byte)(uudecode_dec(buffer[p+1]) << 4 | uudecode_dec(buffer[p+2]) >> 2); output.WriteByte(ch); ch = (byte)(uudecode_dec(buffer[p+2]) << 6 | uudecode_dec(buffer[p+3])); output.WriteByte(ch); } else { if (n >= 1) { if (!(uudecode_is_dec(buffer[p]) && uudecode_is_dec(buffer[p+1]))) throw new Exception( "34"); ch = (byte)(uudecode_dec(buffer[p+0]) << 2 | uudecode_dec(buffer[p+1]) >> 4); output.WriteByte(ch); } if (n >= 2) { if (!(uudecode_is_dec(buffer[p+1]) && uudecode_is_dec(buffer[p+2]))) throw new Exception( "35"); ch = (byte)(uudecode_dec(buffer[p+1]) << 4 | uudecode_dec(buffer[p+2]) >> 2); output.WriteByte(ch); } if (n >= 3) { if (!(uudecode_is_dec(buffer[p+2]) && uudecode_is_dec(buffer[p+3]))) throw new Exception( "36"); ch = (byte)(uudecode_dec(buffer[p+2]) << 6 | uudecode_dec(buffer[p+3])); output.WriteByte(ch); } } } } } if( line == "end") { decoder = ""; outputfile = ""; output.Close(); output = null; } } if( decoder == "yenc") { if( line.StartsWith( "=ypart ")) { // Part description string[] ypart = line.Split( " ".ToCharArray()); foreach( string s in ypart) { if( s.StartsWith( "begin")) { output.Seek( long.Parse( s.Remove(0, 6))-1, System.IO.SeekOrigin.Begin); } } } else { if( line.StartsWith( "=yend ")) { // End of the Yenc part, do CRC check decoder = ""; string[] yend = line.Split( " ".ToCharArray()); foreach( string s in yend) { if( s.StartsWith( "pcrc32")) { long opcrc = Convert.ToInt64(s.Remove(0, 7), 16); long cpcrc = crc.EndByteCRC(); if( opcrc != cpcrc) crcfailed = true; } } decodedsegments ++; if( outputsize == output.Length) outputsize = -1; if( outputsize == -1) { output.Close(); output = null; outputfile = ""; } } else { // Yenc Encoded part bool escape = false; foreach( char c in line.ToCharArray()) { if( c == '=' && !escape) { escape = true; } else { byte nc = (byte)c; if( escape) { nc = (byte)(nc-64); escape = false; } nc = (byte)(nc-42); output.WriteByte(nc); crc.AddByteCRC(nc); } } } } } if( decoder == "") { if( line.StartsWith( "=ybegin ")) { int c; decoder = "yenc"; crc.StartByteCRC(); //rebuild correctly missformed spaces in this line, but not the filename c = line.IndexOf( "name"); string ybegin = line.Substring(0, c-1); string name = line.Substring(c+5); line = ybegin.Replace(" ",""); //strip spaces ybegin = line.Replace("part="," part=");//add spaces where nedded line = ybegin.Replace("line="," line="); ybegin = line.Replace("size="," size="); line = ybegin + " name=" + name; // Check if its a valid ybegin line, as per 1.2 line, size and name have to be present if( line.IndexOf("line=") != -1 && line.IndexOf("size=") != -1 && line.IndexOf("name=") != -1) { int b, e; b = line.IndexOf( "size="); e = line.IndexOf( " ", b); outputsize = long.Parse( line.Substring(b+5, e-b-5)); b = line.IndexOf( "name="); if( outputfile != line.Substring(b+5)) { outputfile = line.Substring(b+5); if( article.Filename == "") article.Filename = outputfile; //why add second filename? //article.Filename = article.Filename + outputfile; string outputdir = GetDirectory(article); try { if( !System.IO.Directory.Exists( System.IO.Path.GetFullPath(outputdir))) System.IO.Directory.CreateDirectory( System.IO.Path.GetFullPath(outputdir)); } catch( Exception ex) { frmMain.LogWriteError( "Unable to create directory [" + outputdir + "]"); throw(ex); } if( output != null) { output.Close(); output = null; } string outputdrive = outputdir.Substring(0,2); long size = article.Size; ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid='" + outputdrive + "'"); try { if (size > Convert.ToInt64(disk[DiskProperties.FreeSpace.ToString()].ToString())) { frmMain.LogWriteError("Unable to create file : no space on drive " + outputdrive); input.Close(); input = null; return DecodeStatus.FailedNothingToDecode; } else { output = new System.IO.FileStream(System.IO.Path.GetFullPath(outputdir) + "\\" + outputfile, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write, System.IO.FileShare.None, 1024 * 1024); } } catch (Exception ex) { frmMain.LogWriteError("Unable to create file : " + ex.Message); input.Close(); input = null; return DecodeStatus.FailedNothingToDecode; } } } } if( line.StartsWith( "begin 644 ")) { decodedsegments ++; decoder = "uudecode"; outputfile = line.Remove(0, 10); if( article.Filename == "") article.Filename = outputfile; //why add second file name? //article.Filename = article.Filename + outputfile; string outputdir = GetDirectory(article); try { if( !System.IO.Directory.Exists( System.IO.Path.GetFullPath(outputdir))) System.IO.Directory.CreateDirectory( System.IO.Path.GetFullPath(outputdir)); } catch( Exception ex) { frmMain.LogWriteError( "Unable to create directory [" + outputdir + "]"); throw(ex); } if( output != null) { output.Close(); output = null; } string outputdrive = outputdir.Substring(0,2); long size = article.Size; ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid='" + outputdrive + "'"); try { if (size > Convert.ToInt64(disk[DiskProperties.FreeSpace.ToString()].ToString())) { frmMain.LogWriteError("Unable to create file : no space on drive " + outputdrive); input.Close(); input = null; return DecodeStatus.FailedNothingToDecode; } else { output = new System.IO.FileStream(System.IO.Path.GetFullPath(outputdir) + "\\" + outputfile, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write, System.IO.FileShare.None, 1024 * 1024); } } catch (Exception ex) { frmMain.LogWriteError("Unable to create file : " + ex.Message); input.Close(); input = null; return DecodeStatus.FailedNothingToDecode; } } if( line.StartsWith( "Content-Type: application/octet-stream;")) { decodedsegments ++; decoder = "mime"; sdecoder = ""; outputfile = line.Substring( line.IndexOf( "name=") + 5); if( outputfile[0] == '\"' && outputfile[outputfile.Length - 1] == '\"') outputfile = outputfile.Substring(1, outputfile.Length - 2); if( outputfile[0] == '\'' && outputfile[outputfile.Length - 1] == '\'') outputfile = outputfile.Substring(1, outputfile.Length - 2); if( article.Filename == "") article.Filename = outputfile; //why add second file name? //article.Filename = article.Filename + outputfile; string outputdir = GetDirectory(article); try { if( !System.IO.Directory.Exists( System.IO.Path.GetFullPath(outputdir))) System.IO.Directory.CreateDirectory( System.IO.Path.GetFullPath(outputdir)); } catch( Exception ex) { frmMain.LogWriteError( "Unable to create directory [" + outputdir + "]"); throw(ex); } if( output != null) { output.Close(); output = null; } string outputdrive = outputdir.Substring(0,2); long size = article.Size; ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid='" + outputdrive + "'"); try { if (size > Convert.ToInt64(disk[DiskProperties.FreeSpace.ToString()].ToString())) { frmMain.LogWriteError( "Unable to create file : no space on drive " + outputdrive); input.Close(); input = null; return DecodeStatus.FailedNothingToDecode; } else { output = new System.IO.FileStream( System.IO.Path.GetFullPath(outputdir) + "\\" + outputfile, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write, System.IO.FileShare.None, 1024*1024); } } catch( Exception ex ) { frmMain.LogWriteError( "Unable to create file : " + ex.Message); input.Close(); input = null; return DecodeStatus.FailedNothingToDecode; } } } line = input.ReadLine(); } input.Close(); input = null; try { if( output != null) output.Flush(); } catch( Exception ex) { frmMain.LogWriteError("pb during Flushing on disk"); throw(ex); } } if( output != null) { output.Close(); output = null; } // Changed the behaviour of deleting segments, delete all segments // unless nothing got decoded if( decodedsegments != 0) { // Pretty sure everything went ok, deleting partial files... foreach( Segment segment in article.Segments) /* if( System.IO.File.Exists( System.IO.Path.GetFullPath("Cache\\" + segment.ArticleID))) System.IO.File.Delete( System.IO.Path.GetFullPath("Cache\\" + segment.ArticleID)); */ if( System.IO.File.Exists(System.IO.Path.GetFullPath(Global.m_CacheDirectory + segment.ArticleID))) System.IO.File.Delete(System.IO.Path.GetFullPath(Global.m_CacheDirectory + segment.ArticleID)); } if( crcfailed) return DecodeStatus.FailedCRC; if( decodedsegments == 0) return DecodeStatus.FailedNothingToDecode; return DecodeStatus.Decoded; }
private void update_ini(string filename) { string[] lines = File.ReadAllLines(filename); string old_version = "", new_version = ""; string cf_checksum = "", cg_checksum = ""; foreach (string s in lines) { if (s.Contains("cf_")) { old_version = s.Substring(3, 5); break; } } foreach (PirsEntry a in p.getList()) { if (a.Filename == "xboxupd.bin") { byte[] upd = p.extractFile(a.Cluster, a.Size); int CFlength = Convert.ToInt32(Oper.ByteArrayToString(Oper.returnportion(upd, 0xC, 4)), 16); int CGlength = Convert.ToInt32(Oper.ByteArrayToString(Oper.returnportion(upd, CFlength + 0xC, 4)), 16); byte[] cf = Oper.returnportion(upd, 0, CFlength); byte[] cg = Oper.returnportion(upd, CFlength, CGlength); cf = Nand.Nand.decrypt_CF(cf); cg = Nand.Nand.decrypt_CG(cg, cf); crc32 crc = new crc32(); new_version = Convert.ToInt32(Oper.ByteArrayToString(Oper.returnportion(cf, 0x2, 2)), 16).ToString(); cf_checksum = crc.CRC(editbl(cf)).ToString("X8").ToLower(); crc = new crc32(); cg_checksum = crc.CRC(editbl(cg)).ToString("X8").ToLower(); break; } } bool test = false; Console.WriteLine(old_version); Console.WriteLine(new_version); Console.WriteLine(cf_checksum); Console.WriteLine(cg_checksum); for (int i = 0; i < lines.Length; i++) { if (String.IsNullOrWhiteSpace(lines[i])) { continue; } string line = lines[i]; if (line.Contains(old_version)) { lines[i] = line.Replace(old_version, new_version); line = lines[i]; } if (line.Contains("cf_")) { lines[i] = line.Substring(0, line.IndexOf(',')) + "," + cf_checksum; } if (line.Contains("cg_")) { lines[i] = line.Substring(0, line.IndexOf(',')) + "," + cg_checksum; } if (line.Contains("[flashfs]")) { test = true; continue; } if (test) { if (!line.Contains(",")) { continue; } string name = line.Substring(0, line.IndexOf(',')); foreach (PirsEntry a in p.getList()) { if ("$flash_" + name == a.Filename) { lines[i] = name + "," + a.CRC.ToString("X8").ToLower(); break; } } } } File.WriteAllLines(Path.Combine(Path.GetDirectoryName(filename), Path.GetFileNameWithoutExtension(filename) + "_new" + Path.GetExtension(filename)), lines); }