public void DeserializeObj(System.Runtime.Serialization.IFormatter formatter, System.IO.Stream ins) { List <ProgState> prgl = (List <ProgState>)formatter.Deserialize(ins); ArrayList <ProgState> arl = new ArrayList <ProgState>(); for (int i = 0; i < prgl.Count; i++) { arl.Add(prgl.ElementAt(i)); } SetPrgList(arl); }
public IEnumerable <string> ListContent <T>(string path, bool recursive = false)//rather slow(needs to load part of files) { var tp = GetReaderByOutput(typeof(T).FullName); foreach (var file in ListContent(path, false)) { using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read)) { ContentFile res = formatter.Deserialize(fs) as ContentFile; if (res == null) { continue; } Console.WriteLine(res.FileType); if (res.FileType == tp.GetType().FullName)//TODO inheritance { yield return(file); } } } yield break; }
object ICloudFileDataTransfer.Deserialize(System.Runtime.Serialization.IFormatter dataFormatter) { using (var cache = new MemoryStream()) { // get the data _fsEntry.GetDataTransferAccessor().Transfer(cache, nTransferDirection.nDownload); // go to the start cache.Position = 0; // go ahead return(dataFormatter.Deserialize(cache)); } }
public static object Deserialize(String xml) { try { byte[] bytes = System.Convert.FromBase64String(xml); MemoryStream ms = new MemoryStream(bytes); object obj = formatter.Deserialize(ms); ms.Close(); return(obj); } catch { } return(null); }
/// <summary> /// Loads keybindings from file if file exist, else loads default keybindsings. /// </summary> public void LoadKeybinds() { if (File.Exists(Application.persistentDataPath + "\\Keybindings.txt")) { //Debug.Log("Loading keybinds from location: \"" + Application.persistentDataPath + "\\Keybindings.txt\""); Stream stream = new FileStream(Application.persistentDataPath + "\\Keybindings.txt", FileMode.Open, FileAccess.Read); keybindings = (Keybindings)formatter.Deserialize(stream); temp_keybindings = keybindings.Copy(); } else { //Debug.Log("Loading default keybinds"); keybindings = new Keybindings(); temp_keybindings = keybindings.Copy(); } }
public T Deserializer <T>() { int count = GetInt(); if (count <= 0 || (_position + count) >= _data.Length) { return(default(T)); } if (_BinaryFormatter == null) { _BinaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); } T ReturnValue; using (var _MemoryStream = new System.IO.MemoryStream(_data, _position, count)) { ReturnValue = (T)_BinaryFormatter.Deserialize(_MemoryStream); } return(ReturnValue); }
/// <summary> /// Called when load data. /// </summary> /// <param name="Data">The data.</param> /// <returns>Returns true if succeeded.</returns> protected override bool OnLoad(ref Stream DataStream, ref System.Runtime.Serialization.IFormatter DataFormater) { DataSerializableRef = (DataSerializable)DataFormater.Deserialize(DataStream); return(true); }
private static object ManageOldVersion(string Version, string FileWhereRead, string Password) { try { switch (Version) { case "v0.0": if (!System.IO.File.Exists(FileWhereRead)) { return(null); } SerializationMode mode = ModeFromFname(FileWhereRead); System.Runtime.Serialization.IFormatter FR = default(System.Runtime.Serialization.IFormatter); if (mode == SerializationMode.Xml) { FR = CreateFormatterForMode(SerializationMode.Xml); } else if (mode == SerializationMode.Binary) { FR = CreateFormatterForMode(SerializationMode.Binary); } else { throw new Exception("Unknown DeSerialization Mode"); } System.IO.FileStream FS = default(System.IO.FileStream); //File Stream System.Security.Cryptography.SymmetricAlgorithm DE = default(System.Security.Cryptography.SymmetricAlgorithm); //Decryption Engine System.Security.Cryptography.CryptoStream DS = null; //Decrypted Stream FS = new System.IO.FileStream(FileWhereRead, FileMode.Open, FileAccess.Read, FileShare.Read); if ((Password != null)) { DE = System.Security.Cryptography.SymmetricAlgorithm.Create(); DS = new System.Security.Cryptography.CryptoStream(FS, DE.CreateDecryptor(GenerateKey(Password, Convert.ToInt32(DE.KeySize / 8)), GenerateKey("Vettore di inizializzazione", 16)), System.Security.Cryptography.CryptoStreamMode.Read); } object Result = null; if ((DS != null)) { Result = FR.Deserialize(DS); FS.Close(); } else { Result = FR.Deserialize(FS); FS.Close(); } //Save in newer version ObjToFile(Result, FileWhereRead, Password); return(Result); } } catch (Exception ex) { ManageReadError(FileWhereRead, ex); } return(null); }
public static object ObjFromFile(string filename, string Password, bool AskForMissingPassword) { object rv = null; lock (ThreadLock) { Exception err = null; filename = System.IO.Path.Combine(LaserGRBL.GrblCore.DataPath, filename); if ((File.Exists(filename + ".bak") & !File.Exists(filename))) { ManageOrphanTmp(filename); } if (File.Exists(filename)) { System.Security.Cryptography.SymmetricAlgorithm EE = null; Stream FinalStream = null; System.Runtime.Serialization.IFormatter SR = default(System.Runtime.Serialization.IFormatter); bool REncrypted = false; bool RCompressed = false; SerializationMode Rmode = default(SerializationMode); string RVersion = null; byte[] Rhash = null; byte[] IV = null; byte[] CypherKey = null; try { //Open a stream on the file for reading (overwrite if exist) and lock the file FinalStream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read); GetSerializerTag(FinalStream, ref RVersion, ref Rmode, ref REncrypted, ref IV, ref Rhash, ref RCompressed); //Get serializer tag end move stream position //GESTISCI LA VERSIONE CORRENTE if (RVersion == SerializerVersion) { SR = CreateFormatterForMode(Rmode); if (RCompressed) { FinalStream = new System.IO.Compression.DeflateStream(FinalStream, System.IO.Compression.CompressionMode.Decompress); } if (REncrypted && Password == null) { if (AskForMissingPassword) { string NewKey = InputBox.Show(null, "Insert password:"******"Protected file", "", null).Text; if ((NewKey != null)) { FinalStream.Close(); return(ObjFromFile(filename, NewKey, AskForMissingPassword)); } else { throw new MissingPasswordException(filename); } } else { throw new MissingPasswordException(filename); } } //GENERATE KEY AND CRYPTO SERVICE if (REncrypted) { EE = System.Security.Cryptography.SymmetricAlgorithm.Create(); EE.IV = IV; CypherKey = GenerateKey(Password, Convert.ToInt32(EE.KeySize / 8)); } //TEST KEY VALIDITY WITH HASH COMPARE if (REncrypted) { byte[] CurHash = GenerateHash(CypherKey); if (Rhash == null || CurHash == null) { throw new WrongPasswordException(filename); } if (!(Rhash.Length == CurHash.Length)) { throw new WrongPasswordException(filename); } for (int I = 0; I <= Rhash.Length - 1; I++) { if (!(Rhash[I] == CurHash[I])) { throw new WrongPasswordException(filename); } } } if (REncrypted) { FinalStream = new System.Security.Cryptography.CryptoStream(FinalStream, EE.CreateDecryptor(CypherKey, EE.IV), System.Security.Cryptography.CryptoStreamMode.Read); } rv = SR.Deserialize(FinalStream); //READ DATA FinalStream.Close(); } else { FinalStream?.Close(); rv = ManageOldVersion(RVersion, filename, Password); } } catch (Exception ex) { err = ex; System.Diagnostics.Debug.WriteLine(string.Format("Serialization exception in {0} Position {1}", filename, FinalStream.Position)); try { FinalStream?.Close(); } catch { } try { ManageReadError(filename, ex); } catch { } } } else { ; } } return(rv); }
public static Object Deserialize(BinaryReader br) { return(formatter.Deserialize(br.BaseStream)); }