/// <summary> /// Create the stream object /// </summary> /// <param name="asm">The assembly to read or write</param> public ApplicationStream(Assembly asm) { this.assembly = asm; this.tmpContentFile = Path.GetTempFileName(); new FileInfo(tmpContentFile).DeleteOnExit(); this.tmpAssemblyFile = Path.GetTempFileName(); new FileInfo(tmpAssemblyFile).DeleteOnExit(); File.Copy(assembly.Location, tmpAssemblyFile, true); //Out file (Assembly) using (FileStream tmpOut = new FileStream(tmpAssemblyFile, FileMode.Open)) { //In file (Temporary) using (FileStream tmpIn = new FileStream(tmpContentFile, FileMode.Create)) { int read = 0; uint counter = 0; //Search pasttern while (tmpOut.Length > tmpOut.Position) { //Read byte read = tmpOut.ReadByte(); //Byte is the signature byte (dependence of counter state) if (read == signature[counter]) { counter++; //Add counter (goon in signature byte array) if (counter >= signature.Length) //Is counter full (end of signature byte array) { //Copy to tmp stream byte[] buffer = tmpOut.ReadToEnd(); tmpIn.Write(buffer, 0, buffer.Length); //Clear data from exe (cut it) long length = buffer.Length + signature.Length; tmpOut.SetLength(tmpOut.Length - length); //Break while break; } } else //Byte is not the signature byte counter = 0; //Reset counter (search signature from start) } tmpIn.Close(); } tmpOut.Close(); } //Set stream to temporary file this.stream = new FileStream(tmpContentFile, FileMode.Open); }
/// <summary> /// An event handler called when setting the key for a PlanetLab slice. /// </summary> /// <param name="sender">The sender object.</param> /// <param name="e">The event arguments.</param> private void OnSetKey(object sender, EventArgs e) { // Open the dialog. if (this.openFileDialog.ShowDialog(this) == DialogResult.OK) { try { // Open the file. using (FileStream fileStream = new FileStream(this.openFileDialog.FileName, FileMode.Open)) { // Set the key data. this.config.Key = fileStream.ReadToEnd(); } } catch (Exception exception) { // Show an error dialog if an exception is thrown. MessageBox.Show("Could not open the RSA key file. {0}".FormatWith(exception.Message), "Cannot Open File", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
/// <summary> /// An event handler called when loading the data from a file. /// </summary> /// <param name="sender">The sender object.</param> /// <param name="e">The event arguments.</param> private void OnLoadKey(object sender, EventArgs e) { // Set the dialog filer. this.openFileDialog.Filter = "All files (*.*)|*.*"; // Open the dialog. if (this.openFileDialog.ShowDialog(this) == DialogResult.OK) { try { // Open the file. using (FileStream fileStream = new FileStream(this.openFileDialog.FileName, FileMode.Open)) { // Get the key data. this.sshKey = fileStream.ReadToEnd(); // Set the key data as a string to the text box. this.textBoxKey.Text = this.sshKey != null ? Encoding.UTF8.GetString(this.sshKey).Replace("\n", Environment.NewLine) : string.Empty; } } catch (Exception exception) { // Show an error dialog if an exception is thrown. MessageBox.Show("Could not open the RSA key file. {0}".FormatWith(exception.Message), "Cannot Open File", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }