private static void scanFile(TextDecoder decoder, int stringLength, string outFile, Encoding encoding) { int filePosition = 0; try { using (StreamWriter writer = new StreamWriter(outFile, false, encoding)) { foreach (var textString in decoder.GetDecodedStrings()) { if (textString.TextString.Count >= stringLength) { writer.WriteLine("Position: " + filePosition.ToString("X")); writer.WriteLine(string.Concat(textString.TextString)); writer.WriteLine(); } filePosition += textString.BytesRead + 1; } } } catch (Exception ex) { Console.WriteLine(ex.Message); return; } }
private TextDecoder initDecoder() { TextDecoder decoder = new TextDecoder(); decoder.OpenTable("test.tbl"); return(decoder); }
public void Initialize() { _fileDataSource = TestHelpers.CreateFileDataSource <ParserContext10>("test-file-data.10.csv", false); _textDecoder = new TextDecoder { Pattern = @"*.", FailValidationResult = ValidationResultType.Critical }; _fileProcessorDefinition = new FileProcessorDefinition10 { HeaderRowProcessorDefinition = new RowProcessorDefinition { FieldProcessorDefinitions = new FieldProcessorDefinition[] { }, }, DataRowProcessorDefinition = new RowProcessorDefinition { FieldProcessorDefinitions = new FieldProcessorDefinition[] { new FieldProcessorDefinition { Decoder = _textDecoder, FieldName = "FieldA", Description = "Field A" }, new FieldProcessorDefinition { Decoder = _textDecoder, FieldName = "FieldB", Description = "Field B" }, new FieldProcessorDefinition { Decoder = _textDecoder, FieldName = "FieldC", Description = "Field C" } } }, TrailerRowProcessorDefinition = new RowProcessorDefinition { FieldProcessorDefinitions = new FieldProcessorDefinition[] { } } }; }
public static void TestTextEncoder() { var values = new object[] { 2, '\n', TextEncodingEscaper.DefaultSeparator, long.MaxValue, int.MinValue, double.MaxValue / 2, -double.Epsilon, DateTime.Now, "\ne#$", "\t" }; foreach (var value in values) { foreach (var type in new[] { IoType.Key, IoType.Value }) { var stream = new StreamContext(); var encoder = new TextEncoder(stream.writer); var decoder = new TextDecoder(stream.reader); Action f = () => encoder.WriteChar(TextEncodingEscaper.DefaultSeparator, IoType.Raw); TestEncoderHelper(value, encoder, decoder, stream, type, f); } } }
public void RunUninitialized() { TextDecoder decoder = new TextDecoder(); string temp = string.Empty; int result = decoder.DecodeString(ref temp, string.Empty); Assert.Equal(0, result); }
public void InvalidStringLengthOffsetThrowsException() { TextDecoder decoder = initDecoder(); Assert.Throws <ArgumentOutOfRangeException>(() => decoder.StringLength = 0); decoder.StringLength = 5; Assert.Throws <ArgumentOutOfRangeException>(() => decoder.StringOffset = 4); }
public void MultiLineDecode() { TextDecoder decoder = initDecoder(); decoder.SetHexBlock(new byte[] { 0, 3, 1, 3, 2 }); var decodedStrings = decoder.GetDecodedStrings("0"); Assert.Equal(3, decodedStrings.Count()); }
public void NormalFixedLengthDecode() { TextDecoder decoder = initDecoder(); decoder.SetHexBlock(new byte[] { 3, 2, 1, 0 }); List <String> decodeResult = decoder.GetDecodedFixedLengthStrings().ToList(); Assert.Equal(1, decodeResult.First().Length); Assert.Equal(4, decodeResult.Count()); }
private string extractMessageBitsFromPixel(int x, int y, string binaryMessage) { var pixelColor = this.GetPixelColor(x, y); if (!isHeaderPixel(x, y)) { binaryMessage += TextDecoder.ExtractMessageBits(pixelColor, this.headerPixels.BitsPerColorChannel); } return(binaryMessage); }
public void NormalDecode(byte[] encodedString, string endString, int readBytes, string expectedString, bool stopOnUndefinedChar) { TextDecoder decoder = initDecoder(); decoder.StopOnUndefinedCharacter = stopOnUndefinedChar; decoder.SetHexBlock(encodedString); string decodedString = string.Empty; int parsedCharacters = decoder.DecodeString(ref decodedString, endString); Assert.Equal(readBytes, parsedCharacters); Assert.Equal(expectedString, decodedString); }
public void FixedLengthDecodeOffsetAndPartialCheck() { TextDecoder decoder = initDecoder(); decoder.StringLength = 2; decoder.StringOffset = 3; decoder.SetHexBlock(new byte[] { 1, 2, 0xff, 0 }); string results = decoder.DecodeFixedLengthString(); Assert.Equal("st", results); results = decoder.DecodeFixedLengthString(); Assert.Equal("e", results); }
public void Decode_Given_an_invalid_text_Value_should_be_null() { var field = new Field { Raw = "ABC" }; target = new TextDecoder { Pattern = "(AAA|BBB)", FailValidationResult = ValidationResultType.Warning }; target.Decode(field); Assert.IsNull(field.Value); }
public void Decode_Given_an_invalid_text_ValidationResult_should_be_set_with_the_value_assigned_to_the_decoder(ValidationResultType failValidationResult) { var field = new Field { Raw = "ABC" }; target = new TextDecoder { Pattern = "(AAA|BBB)", FailValidationResult = failValidationResult }; target.Decode(field); Assert.AreEqual(failValidationResult, field.ValidationResult); }
public void Decode_Given_a_valid_text_Value_should_be_set_with_the_text() { var field = new Field { Raw = "BBB" }; target = new TextDecoder { Pattern = "(AAA|BBB)", FailValidationResult = ValidationResultType.Warning }; target.Decode(field); Assert.AreEqual("BBB", field.Value); }
public void Decode_Given_a_valid_text_ValidationResult_should_be_valid() { var field = new Field { Raw = "AAA" }; target = new TextDecoder { Pattern = "(AAA|BBB)", FailValidationResult = ValidationResultType.Warning }; target.Decode(field); Assert.AreEqual(ValidationResultType.Valid, field.ValidationResult); }
private void AddMarkers() { var paramList = TextDecoder.CreateCustomMarkerParamsList(markerData.text); foreach (var sth in paramList) { var options = new MarkerOptions() .Position(sth.getLatLng()) .Title(sth.getTitle()) .Icon(NewCustomDescriptor()) .Snippet(sth.getSnippet()); var marker = map.AddMarker(options); } }
public static IEnumerable <DecodedString> GetDecodedStrings(this TextDecoder decoder) { List <string> decodedString = new List <string>(); int consumedBytes = 0; do { decodedString.Clear(); consumedBytes = decoder.DecodeChars(decodedString, string.Empty); if (consumedBytes >= 0) { yield return(new DecodedString(decodedString, consumedBytes)); } } while (consumedBytes >= 0 && !decoder.BlockEmpty); }
private static void HttpGetCameraDevice(HttpServerResponse resp, HttpServerRequest req) { networkLed.High(); try { string Xml; byte[] Data; int c; using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Camera.UPnP.CameraDevice.xml")) { c = (int)stream.Length; Data = new byte[c]; stream.Position = 0; stream.Read(Data, 0, c); Xml = TextDecoder.DecodeString(Data, System.Text.Encoding.UTF8); } string HostName = System.Net.Dns.GetHostName(); System.Net.IPHostEntry HostEntry = System.Net.Dns.GetHostEntry(HostName); foreach (System.Net.IPAddress Address in HostEntry.AddressList) { if (Address.AddressFamily == req.ClientEndPoint.AddressFamily) { Xml = Xml.Replace("{IP}", Address.ToString()); break; } } Xml = Xml.Replace("{PORT}", upnpServer.Port.ToString()); Xml = Xml.Replace("{UDN}", defaultSettings.UDN); resp.ContentType = "text/xml"; resp.Encoding = System.Text.Encoding.UTF8; resp.ReturnCode = HttpStatusCode.Successful_OK; resp.Write(Xml); } finally { networkLed.Low(); } }
/// <summary> /// Decodes the text message from the bitmap. /// Precondition: none /// Post-condition: none /// </summary> /// <returns>The embedded text message from the color channel bytes.</returns> public string DecodeTextMessage() { var binaryMessage = string.Empty; for (var y = 0; y < this.Height; y++) { for (var x = 0; x < this.Width; x++) { if (TextDecoder.EndsWithStopIndicator(binaryMessage)) { break; } binaryMessage = this.extractMessageBitsFromPixel(x, y, binaryMessage); } } return(this.headerPixels.HasEncryption ? TextCipher.DecryptText(binaryMessage.ConvertBinaryToString()) : TextDecoder.RemoveDecodeIndicator(binaryMessage.ConvertBinaryToString())); }
public void Decode_Given_that_property_pattern_is_not_set_Should_throw_an_exception() { var field = new Field { Raw = "abc" }; target = new TextDecoder { FailValidationResult = ValidationResultType.Warning }; try { target.Decode(field); } catch (InvalidOperationException ex) { Assert.AreEqual("Property Pattern cannot be empty or null", ex.Message); return; } Assert.Fail("An exception was not thrown"); }
static void Main(string[] args) { string romName = string.Empty; string tableName = string.Empty; int validStringLength = 5; string encodingName = "utf-8"; string outputName = "output.txt"; bool showHelp = false; Encoding encoding; var options = new OptionSet() { {"l|length=", "the minimum length for a valid string. Default is 5", l => validStringLength = Convert.ToInt32(l) }, {"e|encoding=", "the encoding of the table file (e.g. shift-jis). Default is utf-8", e => encodingName = e }, {"o|output=", "the name of the file to write the text to. Default is output.txt", o => outputName = o}, {"h|help", "show this message", h => showHelp = h != null} }; List<string> unparsedOptions; try { unparsedOptions = options.Parse(args); } catch (OptionException ex) { Console.Write("Text Scanner"); Console.WriteLine(ex.Message); Console.WriteLine("Try 'TextScanner --help' for more information"); return; } if (showHelp) { showUsage(options); return; } if (unparsedOptions.Count < 2) { showUsage(options); return; } if (!checkFile(unparsedOptions[0], "rom")) { return; } romName = unparsedOptions[0]; if (!checkFile(unparsedOptions[1], "table")) { return; } tableName = unparsedOptions[1]; if (validStringLength <= 0) { Console.WriteLine("Error: Invalid string length"); return; } try { encoding = Encoding.GetEncoding(encodingName); } catch (Exception ex) { Console.WriteLine(ex.Message); return; } var decoder = new TextDecoder(); decoder.OpenTable(tableName, encodingName); decoder.SetHexBlock(File.ReadAllBytes(romName)); decoder.StopOnUndefinedCharacter = true; scanFile(decoder, validStringLength, outputName, encoding); }
static void Main(string[] args) { string romName = string.Empty; string tableName = string.Empty; int validStringLength = 5; string encodingName = "utf-8"; string outputName = "output.txt"; bool showHelp = false; Encoding encoding; var options = new OptionSet() { { "l|length=", "the minimum length for a valid string. Default is 5", l => validStringLength = Convert.ToInt32(l) }, { "e|encoding=", "the encoding of the table file (e.g. shift-jis). Default is utf-8", e => encodingName = e }, { "o|output=", "the name of the file to write the text to. Default is output.txt", o => outputName = o }, { "h|help", "show this message", h => showHelp = h != null } }; List <string> unparsedOptions; try { unparsedOptions = options.Parse(args); } catch (OptionException ex) { Console.Write("Text Scanner"); Console.WriteLine(ex.Message); Console.WriteLine("Try 'TextScanner --help' for more information"); return; } if (showHelp) { showUsage(options); return; } if (unparsedOptions.Count < 2) { showUsage(options); return; } if (!checkFile(unparsedOptions[0], "rom")) { return; } romName = unparsedOptions[0]; if (!checkFile(unparsedOptions[1], "table")) { return; } tableName = unparsedOptions[1]; if (validStringLength <= 0) { Console.WriteLine("Error: Invalid string length"); return; } try { encoding = Encoding.GetEncoding(encodingName); } catch (Exception ex) { Console.WriteLine(ex.Message); return; } var decoder = new TextDecoder(); decoder.OpenTable(tableName, encodingName); decoder.SetHexBlock(File.ReadAllBytes(romName)); decoder.StopOnUndefinedCharacter = true; scanFile(decoder, validStringLength, outputName, encoding); }
public static TextDecoder get() { if (obj == null) obj = new TextDecoder(); return obj; }
void DumpRoomObjectImages(Room room, ObjectData obj, Gdi gdi) { var text = new ScummText(obj.Name); var sb = new StringBuilder(); sb.Append("Object #" + obj.Number).Append(" "); var decoder = new TextDecoder(sb); text.Decode(decoder); sb.AppendFormat(" size: {0}x{1}", obj.Width, obj.Height); Console.WriteLine(sb); var j = 0; foreach (var img in obj.Images) { // try // { var screen = new VirtScreen(0, obj.Width, obj.Height, PixelFormat.Indexed8, 2); if (img.IsBomp) { var bdd = new BompDrawData(); bdd.Src = img.Data; bdd.Dst = new PixelNavigator(screen.Surfaces[0]); bdd.X = 0; bdd.Y = 0; bdd.Width = obj.Width; bdd.Height = obj.Height; bdd.ScaleX = 255; bdd.ScaleY = 255; bdd.DrawBomp(); } else { gdi.DrawBitmap(img, screen, new Point(0, 0), obj.Width, obj.Height & 0xFFF8, 0, obj.Width / 8, room.Header.Width, DrawBitmaps.None, true); } using (var bmp = ToBitmap(room, screen)) { bmp.Save("obj_" + obj.Number + "_" + (++j) + ".png"); } // } // catch (Exception e) // { // Console.ForegroundColor = ConsoleColor.Red; // Console.WriteLine(e); // Console.ResetColor(); // Console.ReadLine(); // } } }
public static int Main(string[] args) { bool showHelp = false; bool dumpAllObjectImages = false; bool dumpAllRoomImages = false; string input = null; var scripts = new List<int>(); var scriptObjects = new List<int>(); var scriptRooms = new List<int>(); var rooms = new List<int>(); var objects = new List<int>(); Initialize(); var options = new OptionSet() { { "f=", "The input file", v => input = v }, { "s|script=", "the global script number to dump", (int s) => scripts.Add(s) }, { "so|script_object=", "the object number whose script has to be dumped", (int s) => scriptObjects.Add(s) }, { "sr|script_room=", "the room number whose script has to be dumped", (int s) => scriptRooms.Add(s) }, { "r|room=", "the room image to dump", (int r) => rooms.Add(r) }, { "ra", "dump all object images",ra => dumpAllRoomImages = ra != null }, { "o|object=", "the object images to dump",(int o) => objects.Add(o) }, { "oa", "dump all object images",oa => dumpAllObjectImages = oa != null }, { "h|help", "show this message and exit", v => showHelp = v != null } }; try { options.Parse(args); } catch (OptionException) { System.Console.WriteLine("Try `nsdump --help' for more information."); return 1; } if (input == null || showHelp) { Usage(options); return 0; } var resStream = typeof(GameManager).Assembly.GetManifestResourceStream(typeof(GameManager), "Nscumm.xml"); var gm = GameManager.Create(resStream); var game = gm.GetInfo(input); if (game == null) { System.Console.Error.WriteLine("This game is not supported, sorry please contact me if you want to support this game."); return 1; } var index = ResourceManager.Load(game); var scriptDumper = new ScriptDumper(game); var dumper = new ConsoleDumper(); // dump scripts foreach (var script in scripts) { var scr = index.GetScript(script); dumper.WriteLine("script " + script); scriptDumper.DumpScript(scr, dumper); } // dump room scripts if (scriptRooms.Count > 0) { var roomScripts = index.Rooms.Where(r => scriptRooms.Contains(r.Number)).ToList(); foreach (var room in roomScripts) { dumper.WriteLine("Room {0}", room.Number); if (room.EntryScript.Data.Length > 0) { dumper.WriteLine("Entry"); scriptDumper.DumpScript(room.EntryScript.Data, dumper); dumper.WriteLine(); } if (room.ExitScript.Data.Length > 0) { dumper.WriteLine("Exit"); scriptDumper.DumpScript(room.ExitScript.Data, dumper); dumper.WriteLine(); } for (int i = 0; i < room.LocalScripts.Length; i++) { var ls = room.LocalScripts[i]; if (ls != null) { dumper.WriteLine("LocalScript {0}", i); scriptDumper.DumpScript(ls.Data, dumper); } } for (int i = 0; i < room.Objects.Count; i++) { var obj = room.Objects[i]; if (obj != null && obj.Script.Data.Length > 0) { var sb = new StringBuilder(); var decoder = new TextDecoder(sb); var text = new ScummText(obj.Name); text.Decode(decoder); dumper.WriteLine("obj {0} {1}", obj.Number, sb); var tmp = obj.Script.Offset; var offsets = new long[] { 0, obj.Script.Data.Length }.Concat(obj.ScriptOffsets.Select(off => off.Value - tmp)).OrderBy(o => o).Distinct().ToList(); var scr = new List<Tuple<long, byte[]>>(); for (int j = 0; j < offsets.Count - 1; j++) { var len = offsets[j + 1] - offsets[j]; var d = new byte[len]; Array.Copy(obj.Script.Data, offsets[j], d, 0, len); scr.Add(Tuple.Create(offsets[j], d)); } foreach (var s in scr) { var keys = obj.ScriptOffsets.Where(o => o.Value - tmp == s.Item1).Select(o => o.Key).ToList(); foreach (var key in keys) { dumper.WriteLine("{0}", (VerbsV0)key); } scriptDumper.DumpScript(s.Item2, dumper); } dumper.WriteLine(); } } } } // dump object scripts if (scriptObjects.Count > 0) { var objs = index.Rooms.SelectMany(r => r.Objects).Where(o => scriptObjects.Contains(o.Number)).ToList(); foreach (var obj in objs) { dumper.WriteLine("obj {0} {1} {{", obj.Number, System.Text.Encoding.UTF8.GetString(obj.Name)); dumper.WriteLine("Script offset: {0}", obj.Script.Offset); foreach (var off in obj.ScriptOffsets) { dumper.WriteLine("idx #{0}: {1}", off.Key, off.Value - obj.Script.Offset); } dumper.WriteLine("script"); scriptDumper.DumpScript(obj.Script.Data, dumper); } } // dump rooms if (dumpAllRoomImages || rooms.Count > 0) { var imgDumper = new ImageDumper(game); imgDumper.DumpRoomImages(index, dumpAllRoomImages ? null : rooms); } // dump objects if (dumpAllObjectImages || objects.Count > 0) { var imgDumper = new ImageDumper(game); imgDumper.DumpObjectImages(index, dumpAllObjectImages ? null : objects); } return 0; }
public void Initialize() { _fileDataSource = TestHelpers.CreateFileDataSource <ParserContext20>("test-file-data-trailer.20.csv", false); _textDecoder = new TextDecoder { Pattern = @"*.", FailValidationResult = ValidationResultType.Critical }; _dataType1 = new DataRowProcessorDefinition { DataTypeFieldIndex = 0, RowProcessorDefinition = new RowProcessorDefinition { FieldProcessorDefinitions = new FieldProcessorDefinition[] { new FieldProcessorDefinition { Decoder = _textDecoder, FieldName = "DataType", Description = "DT1 Field A" }, new FieldProcessorDefinition { Decoder = _textDecoder, FieldName = "key-field", Description = "DT1 Field B" }, new FieldProcessorDefinition { Decoder = _textDecoder, FieldName = "DT1-Field-c", Description = "DT1 Field C" } } } }; _dataType2 = new DataRowProcessorDefinition { DataTypeFieldIndex = 0, RowProcessorDefinition = new RowProcessorDefinition { FieldProcessorDefinitions = new FieldProcessorDefinition[] { new FieldProcessorDefinition { Decoder = _textDecoder, FieldName = "DataType", Description = "DT2 Field A" }, new FieldProcessorDefinition { Decoder = _textDecoder, FieldName = "key-field", Description = "DT2 Field B" }, new FieldProcessorDefinition { Decoder = _textDecoder, FieldName = "DT2-Field-c", Description = "DT2 Field C" }, new FieldProcessorDefinition { Decoder = _textDecoder, FieldName = "DT2-Field-d", Description = "DT2 Field D" } } } }; _trailer = new RowProcessorDefinition { FieldProcessorDefinitions = new FieldProcessorDefinition[] { new FieldProcessorDefinition { Decoder = _textDecoder, FieldName = "Field-TA" }, new FieldProcessorDefinition { Decoder = _textDecoder, FieldName = "Field-TB" } } }; _fileProcessorDefinition = new FileProcessorDefinition20 { DataTypeField = "FieldA", KeyField = "key-field", HeaderRowProcessorDefinition = new RowProcessorDefinition { FieldProcessorDefinitions = new FieldProcessorDefinition[] { }, }, TrailerRowProcessorDefinition = _trailer, DataRowProcessorDefinitions = new Dictionary <string, DataRowProcessorDefinition> { { "dt1", _dataType1 }, { "dt2", _dataType2 } } }; }