public static ConfigNode ToConfigNode(this HarddiskFile file, string nodeName) { var node = new ConfigNode(nodeName); node.AddValue(FilenameValueString, file.Name); FileContent content = file.ReadAll(); if (content.Category == FileCategory.KSM) { node.AddValue("line", PersistenceUtilities.EncodeBase64(content.Bytes)); } else { if (SafeHouse.Config.UseCompressedPersistence) { node.AddValue("line", EncodeBase64(content.String)); } else { node.AddValue("line", PersistenceUtilities.EncodeLine(content.String)); } } return(node); }
public static FileContent ToHarddiskFile(this ConfigNode configNode, Harddisk harddisk, HarddiskDirectory directory) { try { string content = null; if (configNode.TryGetValue("ascii", ref content)) // ASCII files just get decoded from the ConfigNode safe representation { return(new FileContent(PersistenceUtilities.DecodeLine(content))); } if (configNode.TryGetValue("binary", ref content)) // binary files get decoded from Base64 and gzip { return(new FileContent(PersistenceUtilities.DecodeBase64ToBinary(content))); } if (configNode.TryGetValue("line", ref content)) // fall back to legacy logic { return(Decode(content)); } } catch (Exception ex) { SafeHouse.Logger.LogError(string.Format("Exception caught loading file information: {0}\n\nStack Trace:\n{1}", ex.Message, ex.StackTrace)); } SafeHouse.Logger.LogError(string.Format("Error loading file information from ConfigNode at path {0} on hard disk {1}", directory.Path, harddisk.Name)); return(new FileContent("")); // if there was an error, just return a blank file. }
private FileSystemInfo Search(VolumePath volumePath, bool ksmDefault) { var path = GetArchivePath(volumePath); if (Directory.Exists(path)) { return(new DirectoryInfo(path)); } if (File.Exists(path)) { return(new FileInfo(path)); } var kerboscriptFile = new FileInfo(PersistenceUtilities.CookedFilename(path, KERBOSCRIPT_EXTENSION, true)); var kosMlFile = new FileInfo(PersistenceUtilities.CookedFilename(path, KOS_MACHINELANGUAGE_EXTENSION, true)); if (kerboscriptFile.Exists && kosMlFile.Exists) { return(ksmDefault ? kosMlFile : kerboscriptFile); } if (kerboscriptFile.Exists) { return(kerboscriptFile); } if (kosMlFile.Exists) { return(kosMlFile); } return(null); }
private void HandleValue(ConfigNode configNode, string key, object value) { if (value is Dump) { configNode.AddNode(key, ToConfigNode(key, value as Dump)); } else if (value is List <object> ) { configNode.AddNode(key, ToConfigNode(key, value as List <object>)); } else if (value is int) { configNode.AddNode(key, ScalarToConfigNode(key, ScalarKey, Convert.ToString(value))); } else if (value is double) { configNode.AddNode(key, ScalarToConfigNode(key, ScalarKey, ((double)value).ToString("R"))); } else if (value is bool) { configNode.AddNode(key, ScalarToConfigNode(key, BooleanKey, Convert.ToString(value))); } else { configNode.AddValue(key, PersistenceUtilities.EncodeLine(value.ToString())); } }
public IPersistenceStorageOperationContext <TStrictedEntity> GetStricted <TStrictedEntity>() where TStrictedEntity : class, IPersistenceEntity { var key = PersistenceUtilities.GetReferenceKeyTypeDescriptor(entityType: typeof(TStrictedEntity).Arg(nameof(TStrictedEntity))); return((IPersistenceStorageOperationContext <TStrictedEntity>)P_GetStrictedCtx(key: key)); }
private static FileContent Decode(string input) { try { return(new FileContent(PersistenceUtilities.DecodeBase64ToBinary(input))); } catch // if there is an exception of any kind decoding, fall back to standard decoding { string decodedString = PersistenceUtilities.DecodeLine(input); return(new FileContent(decodedString)); } }
public override void Execute(SharedObjects shared) { string fileName = shared.Cpu.PopValue(true).ToString(); // If no filename extension, then give it one: fileName = PersistenceUtilities.CookedFilename(fileName, Volume.KERBOSCRIPT_EXTENSION); if (shared.VolumeMgr != null) { Volume vol = shared.VolumeMgr.CurrentVolume; shared.Window.OpenPopupEditor(vol, fileName); } }
private static FileContent Decode(string input) { try { return(new FileContent(PersistenceUtilities.DecodeBase64ToBinary(input))); } catch (FormatException) { // standard encoding string decodedString = PersistenceUtilities.DecodeLine(input); return(new FileContent(decodedString)); } }
public static byte[] ConvertFromWindowsNewlines(byte[] bytes) { FileCategory category = PersistenceUtilities.IdentifyCategory(bytes); if (!PersistenceUtilities.IsBinary(category)) { string asString = FileContent.DecodeString(bytes); // Only evil windows gets evil windows line breaks, and only if this is some sort of ASCII: asString = asString.Replace("\r\n", "\n"); return(FileContent.EncodeString(asString)); } return(bytes); }
public void OnLoad(ConfigNode node) { try { var scriptBuilder = new StringBuilder(); foreach (ConfigNode contextNode in node.GetNodes("context")) { foreach (ConfigNode varNode in contextNode.GetNodes("variables")) { foreach (ConfigNode.Value value in varNode.values) { string varValue = PersistenceUtilities.DecodeLine(value.value); scriptBuilder.AppendLine(string.Format("set {0} to {1}.", value.name, varValue)); } } } if (shared.ScriptHandler == null || scriptBuilder.Length <= 0) { return; } var programBuilder = new ProgramBuilder(); // TODO: figure out how to store the filename and reload it for arg 1 below: // (Possibly all of OnLoad needs work because it never seemed to bring // back the context fully right anyway, which is why this hasn't been // addressed yet). try { SafeHouse.Logger.Log("Parsing Context:\n\n" + scriptBuilder); programBuilder.AddRange(shared.ScriptHandler.Compile("reloaded file", 1, scriptBuilder.ToString())); List <Opcode> program = programBuilder.BuildProgram(); RunProgram(program, true); } catch (NullReferenceException ex) { SafeHouse.Logger.Log("program builder failed on load. " + ex.Message); } } catch (Exception e) { if (shared.Logger != null) { shared.Logger.Log(e); } } }
public Dump FromConfigNode(ConfigNode configNode) { Dump result = new Dump(); foreach (ConfigNode.Value val in configNode.values) { result[val.name] = PersistenceUtilities.DecodeLine(val.value); } foreach (ConfigNode subNode in configNode.GetNodes()) { result[subNode.name] = ObjectFromConfigNode(subNode); } return(result); }
public void OnSave(ConfigNode node) { try { var contextNode = new ConfigNode("context"); // Save variables if (variables.Count > 0) { var varNode = new ConfigNode("variables"); foreach (var kvp in variables) { if (!(kvp.Value is BoundVariable) && (kvp.Value.Name.IndexOfAny(new[] { '*', '-' }) == -1)) // variables that have this characters are internal and shouldn't be persisted { if (kvp.Value.Value.GetType().ToString() == "System.String") // if the variable is a string, enclose the value in "" { varNode.AddValue(kvp.Key.TrimStart('$'), (char)34 + PersistenceUtilities.EncodeLine(kvp.Value.Value.ToString()) + (char)34); } else { varNode.AddValue(kvp.Key.TrimStart('$'), PersistenceUtilities.EncodeLine(kvp.Value.Value.ToString())); } } } contextNode.AddNode(varNode); } node.AddNode(contextNode); } catch (Exception e) { if (shared.Logger != null) { shared.Logger.Log(e); } } }
public static ConfigNode ToConfigNode(this ProgramFile programFile, string nodeName) { var node = new ConfigNode(nodeName); node.AddValue(FILENAME_VALUE_STRING, programFile.Filename); if (programFile.Category == FileCategory.KSM) { node.AddValue("line", EncodeBase64(programFile.BinaryContent)); } else { if (Config.Instance.UseCompressedPersistence) { node.AddValue("line", EncodeBase64(programFile.StringContent)); } else { node.AddValue("line", PersistenceUtilities.EncodeLine(programFile.StringContent)); } } return(node); }
private static void Decode(ProgramFile programFile, string input) { try { string decodedString; try { // base64 encoding // Fix for issue #429. See comment up in EncodeBase64() method above for an explanation: string massagedInput = input.Replace(',', '/'); byte[] decodedBuffer = DecodeBase64ToBinary(massagedInput); FileCategory whatKind = PersistenceUtilities.IdentifyCategory(decodedBuffer); if (whatKind == FileCategory.ASCII || whatKind == FileCategory.KERBOSCRIPT) { decodedString = Encoding.ASCII.GetString(decodedBuffer); programFile.StringContent = decodedString; } else { programFile.BinaryContent = decodedBuffer; } } catch (FormatException) { // standard encoding decodedString = PersistenceUtilities.DecodeLine(input); programFile.StringContent = decodedString; } } catch (Exception e) { SafeHouse.Logger.Log(string.Format("Exception decoding: {0} | {1}", e, e.Message)); } }
private List <object> ListFromConfigNode(ConfigNode configNode) { List <object> result = new List <object>(); bool hasMoreValues = true; for (int i = 0; hasMoreValues; i++) { if (configNode.HasValue(i.ToString())) { result.Add(PersistenceUtilities.DecodeLine(configNode.GetValue(i.ToString()))); } else if (configNode.HasNode(i.ToString())) { result.Add(ObjectFromConfigNode(configNode.GetNode(i.ToString()))); } else { hasMoreValues = false; } } return(result); }
public override void Execute(SharedObjects shared) { bool defaultOutput = false; bool justCompiling = false; // is this load() happening to compile, or to run? string fileNameOut = null; object topStack = PopValueAssert(shared, true); // null if there's no output file (output file means compile, not run). if (topStack != null) { justCompiling = true; string outputArg = topStack.ToString(); if (outputArg.Equals("-default-compile-out-")) { defaultOutput = true; } else { fileNameOut = PersistenceUtilities.CookedFilename(outputArg, Volume.KOS_MACHINELANGUAGE_EXTENSION); } } string fileName = null; topStack = PopValueAssert(shared, true); if (topStack != null) { fileName = topStack.ToString(); } AssertArgBottomAndConsume(shared); if (fileName == null) { throw new KOSFileException("No filename to load was given."); } VolumeFile file = shared.VolumeMgr.CurrentVolume.Open(fileName, !justCompiling); // if running, look for KSM first. If compiling look for KS first. if (file == null) { throw new KOSFileException(string.Format("Can't find file '{0}'.", fileName)); } fileName = file.Name; // just in case GetByName picked an extension that changed it. FileContent fileContent = file.ReadAll(); // filename is now guaranteed to have an extension. To make default output name, replace the extension with KSM: if (defaultOutput) { fileNameOut = fileName.Substring(0, fileName.LastIndexOf('.')) + "." + Volume.KOS_MACHINELANGUAGE_EXTENSION; } if (fileNameOut != null && fileName == fileNameOut) { throw new KOSFileException("Input and output filenames must differ."); } if (shared.VolumeMgr == null) { return; } if (shared.VolumeMgr.CurrentVolume == null) { throw new KOSFileException("Volume not found"); } if (shared.ScriptHandler != null) { shared.Cpu.StartCompileStopwatch(); var options = new CompilerOptions { LoadProgramsInSameAddressSpace = true, FuncManager = shared.FunctionManager }; string filePath = shared.VolumeMgr.GetVolumeRawIdentifier(shared.VolumeMgr.CurrentVolume) + "/" + fileName; // add this program to the address space of the parent program, // or to a file to save: if (justCompiling) { List <CodePart> compileParts = shared.ScriptHandler.Compile(filePath, 1, fileContent.String, string.Empty, options); VolumeFile volumeFile = shared.VolumeMgr.CurrentVolume.Save(fileNameOut, new FileContent(compileParts)); if (volumeFile == null) { throw new KOSFileException("Can't save compiled file: not enough space or access forbidden"); } } else { var programContext = ((CPU)shared.Cpu).SwitchToProgramContext(); List <CodePart> parts; if (fileContent.Category == FileCategory.KSM) { string prefix = programContext.Program.Count.ToString(); parts = fileContent.AsParts(filePath, prefix); } else { parts = shared.ScriptHandler.Compile(filePath, 1, fileContent.String, "program", options); } int programAddress = programContext.AddObjectParts(parts); // push the entry point address of the new program onto the stack shared.Cpu.PushStack(programAddress); } shared.Cpu.StopCompileStopwatch(); } }
private static string EncodeBase64(string input) { return(PersistenceUtilities.EncodeBase64(Encoding.ASCII.GetBytes(input))); }