/* This file is part of libWiiSharp * Copyright (C) 2009 Leathl * Copyright (C) 2020 Github Contributors * * libWiiSharp is free software: you can redistribute it and/or * modify it under the terms of the GNU General Public License as published * by the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * libWiiSharp is distributed in the hope that it will be * useful, but WITHOUT ANY WARRANTY; without even the implied warranty * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ using System; using System.ComponentModel; using System.IO; using System.Net.Sockets; using System.Runtime.InteropServices; namespace libWiiSharp { public enum Protocol { /// /// Will preconfigure all settings for HBC to 1.0.5 (HAXX). /// HAXX = 0, /// /// Will preconfigure all settings for HBC from 1.0.5 (JODI). /// JODI = 1, /// /// Remember to define your custom settings. /// Custom = 2, } /// /// The HbcTransmitter can easily transmit files to the Homebrew Channel. /// In order to use compression, you need zlib1.dll in the application directory. /// public class HbcTransmitter : IDisposable { private int blocksize = 4096; private int wiiloadMayor; private int wiiloadMinor = 5; private bool compress; private string ipAddress; private int port = 4299; private string lastErrorMessage = string.Empty; private Protocol protocol; private TcpClient tcpClient; private NetworkStream nwStream; private string lastError = string.Empty; private int transmittedLength; private int compressionRatio; private bool isDisposed; /// /// The size of the buffer that is used to transmit the data. /// Default is 4 * 1024. If you're facing problems (freezes while transmitting), try a higher size. /// public int Blocksize { get => this.blocksize; set => this.blocksize = value; } /// /// The mayor version of wiiload. You might need to change it for upcoming releases of the HBC. /// public int WiiloadVersionMayor { get => this.wiiloadMayor; set => this.wiiloadMayor = value; } /// /// The minor version of wiiload. You might need to change it for upcoming releases of the HBC. /// public int WiiloadVersionMinor { get => this.wiiloadMinor; set => this.wiiloadMinor = value; } /// /// If true, the data will be compressed before being transmitted. NOT available for Protocol.HAXX! /// Also, compression will only work if zlib1.dll is in the application folder. /// public bool Compress { get => this.compress; set { if (this.protocol == Protocol.HAXX) return; this.compress = value; } } /// /// The IP address of the Wii. /// public string IpAddress { get => this.ipAddress; set => this.ipAddress = value; } /// The port used for the transmission. /// You don't need to touch this unless the port changes in future releases of the HBC. /// public int Port { get => this.port; set => this.port = value; } /// /// After a successfully completed transmission, this value holds the number of transmitted bytes. /// public int TransmittedLength => this.transmittedLength; /// /// After a successfully completed transmission, this value holds the compression ratio. /// Will be 0 if the data wasn't compressed. /// public int CompressionRatio => this.compressionRatio; /// /// Holds the last occured error message. /// public string LastError => this.lastError; public HbcTransmitter(Protocol protocol, string ipAddress) { this.protocol = protocol; this.ipAddress = ipAddress; this.wiiloadMinor = protocol == Protocol.HAXX ? 4 : 5; this.compress = protocol == Protocol.JODI; } #region IDisposable Members ~HbcTransmitter() => this.Dispose(false); public void Dispose() { this.Dispose(true); GC.SuppressFinalize((object) this); } protected virtual void Dispose(bool disposing) { if (disposing && !this.isDisposed) { this.ipAddress = (string) null; this.lastErrorMessage = (string) null; this.lastError = (string) null; if (this.nwStream != null) { this.nwStream.Close(); this.nwStream = (NetworkStream) null; } if (this.tcpClient != null) { this.tcpClient.Close(); this.tcpClient = (TcpClient) null; } } this.isDisposed = true; } #endregion #region Public Functions public bool TransmitFile(string pathToFile) => this.transmit(Path.GetFileName(pathToFile), File.ReadAllBytes(pathToFile)); public bool TransmitFile(string fileName, byte[] fileData) => this.transmit(fileName, fileData); #endregion #region Private Functions private bool transmit(string fileName, byte[] fileData) { this.fireDebug("Transmitting {0} to {1}:{2}...", (object) fileName, (object) this.ipAddress, (object) this.port); if (!Environment.OSVersion.ToString().ToLower().Contains("windows")) this.compress = false; if (fileName.ToLower().EndsWith(".zip")) this.compress = false; this.tcpClient = new TcpClient(); byte[] buffer1 = new byte[4]; this.fireDebug(" Connecting..."); try { this.tcpClient.Connect(this.ipAddress, 4299); } catch (Exception ex) { this.fireDebug(" -> Connection Failed:\n" + ex.Message); this.lastError = "Connection Failed:\n" + ex.Message; this.tcpClient.Close(); return false; } this.nwStream = this.tcpClient.GetStream(); this.fireDebug(" Sending Magic..."); buffer1[0] = (byte) 72; buffer1[1] = (byte) 65; buffer1[2] = (byte) 88; buffer1[3] = (byte) 88; try { this.nwStream.Write(buffer1, 0, 4); } catch (Exception ex) { this.fireDebug(" -> Error sending Magic:\n" + ex.Message); this.lastError = "Error sending Magic:\n" + ex.Message; this.nwStream.Close(); this.tcpClient.Close(); return false; } this.fireDebug(" Sending Version Info..."); buffer1[0] = (byte) this.wiiloadMayor; buffer1[1] = (byte) this.wiiloadMinor; buffer1[2] = (byte) (fileName.Length + 2 >> 8 & (int) byte.MaxValue); buffer1[3] = (byte) (fileName.Length + 2 & (int) byte.MaxValue); try { this.nwStream.Write(buffer1, 0, 4); } catch (Exception ex) { this.fireDebug(" -> Error sending Version Info:\n" + ex.Message); this.lastError = "Error sending Version Info:\n" + ex.Message; this.nwStream.Close(); this.tcpClient.Close(); return false; } byte[] buffer2; if (this.compress) { this.fireDebug(" Compressing File..."); try { buffer2 = zlibWrapper.Compress(fileData); } catch { this.fireDebug(" -> Compression failed, continuing without compression..."); this.compress = false; buffer2 = fileData; fileData = new byte[0]; } } else { buffer2 = fileData; fileData = new byte[0]; } this.fireDebug(" Sending Filesize..."); buffer1[0] = (byte) (buffer2.Length >> 24 & (int) byte.MaxValue); buffer1[1] = (byte) (buffer2.Length >> 16 & (int) byte.MaxValue); buffer1[2] = (byte) (buffer2.Length >> 8 & (int) byte.MaxValue); buffer1[3] = (byte) (buffer2.Length & (int) byte.MaxValue); try { this.nwStream.Write(buffer1, 0, 4); } catch (Exception ex) { this.fireDebug(" -> Error sending Filesize:\n" + ex.Message); this.lastError = "Error sending Filesize:\n" + ex.Message; this.nwStream.Close(); this.tcpClient.Close(); return false; } if (this.protocol != Protocol.HAXX) { buffer1[0] = (byte) (fileData.Length >> 24 & (int) byte.MaxValue); buffer1[1] = (byte) (fileData.Length >> 16 & (int) byte.MaxValue); buffer1[2] = (byte) (fileData.Length >> 8 & (int) byte.MaxValue); buffer1[3] = (byte) (fileData.Length & (int) byte.MaxValue); try { this.nwStream.Write(buffer1, 0, 4); } catch (Exception ex) { this.fireDebug(" -> Error sending Filesize:\n" + ex.Message); this.lastError = "Error sending Filesize:\n" + ex.Message; this.nwStream.Close(); this.tcpClient.Close(); return false; } } this.fireDebug(" Sending File..."); int offset = 0; int num1 = 0; int num2 = buffer2.Length / this.Blocksize; int num3 = buffer2.Length % this.Blocksize; try { do { this.fireProgress(++num1 * 100 / num2); this.nwStream.Write(buffer2, offset, this.Blocksize); offset += this.Blocksize; } while (num1 < num2); if (num3 > 0) this.nwStream.Write(buffer2, offset, buffer2.Length - offset); } catch (Exception ex) { this.fireDebug(" -> Error sending File:\n" + ex.Message); this.lastError = "Error sending File:\n" + ex.Message; this.nwStream.Close(); this.tcpClient.Close(); return false; } this.fireDebug(" Sending Arguments..."); byte[] buffer3 = new byte[fileName.Length + 2]; for (int index = 0; index < fileName.Length; ++index) buffer3[index] = (byte) fileName.ToCharArray()[index]; try { this.nwStream.Write(buffer3, 0, buffer3.Length); } catch (Exception ex) { this.fireDebug(" -> Error sending Arguments:\n" + ex.Message); this.lastError = "Error sending Arguments:\n" + ex.Message; this.nwStream.Close(); this.tcpClient.Close(); return false; } this.nwStream.Close(); this.tcpClient.Close(); this.transmittedLength = buffer2.Length; this.compressionRatio = !this.compress || fileData.Length == 0 ? 0 : buffer2.Length * 100 / fileData.Length; this.fireDebug("Transmitting {0} to {1}:{2} Finished...", (object) fileName, (object) this.ipAddress, (object) this.port); return true; } #endregion #region Events /// /// Fires the Progress of various operations /// public event EventHandler Progress; /// /// Fires debugging messages. You may write them into a log file or log textbox. /// public event EventHandler Debug; private void fireDebug(string debugMessage, params object[] args) { EventHandler debug = this.Debug; if (debug == null) return; debug(new object(), new MessageEventArgs(string.Format(debugMessage, args))); } private void fireProgress(int progressPercentage) { EventHandler progress = this.Progress; if (progress == null) return; progress(new object(), new ProgressChangedEventArgs(progressPercentage, (object) string.Empty)); } #endregion } internal class zlibWrapper { [DllImport("zlib1.dll")] private static extern zlibWrapper.ZLibError compress2( byte[] dest, ref int destLength, byte[] source, int sourceLength, int level); public static byte[] Compress(byte[] inFile) { byte[] array = new byte[inFile.Length + 64]; int destLength = -1; zlibWrapper.ZLibError zlibError = zlibWrapper.compress2(array, ref destLength, inFile, inFile.Length, 6); if (zlibError != zlibWrapper.ZLibError.Z_OK || destLength <= -1 || destLength >= inFile.Length) throw new Exception("An error occured while compressing! Code: " + zlibError.ToString()); Array.Resize(ref array, destLength); return array; } public enum ZLibError { Z_VERSION_ERROR = -6, // 0xFFFFFFFA Z_BUF_ERROR = -5, // 0xFFFFFFFB Z_MEM_ERROR = -4, // 0xFFFFFFFC Z_DATA_ERROR = -3, // 0xFFFFFFFD Z_STREAM_ERROR = -2, // 0xFFFFFFFE Z_ERRNO = -1, // 0xFFFFFFFF Z_OK = 0, Z_STREAM_END = 1, Z_NEED_DICT = 2, } } }