|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.IO.Pipes; |
| 5 | +using System.Linq; |
| 6 | +using System.Text; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using System.Windows; |
| 9 | +using System.Windows.Controls; |
| 10 | +using System.Windows.Data; |
| 11 | +using System.Windows.Documents; |
| 12 | +using System.Windows.Input; |
| 13 | +using System.Windows.Media; |
| 14 | +using System.Windows.Media.Imaging; |
| 15 | +using System.Windows.Shapes; |
| 16 | +using static QDTool.Utility; |
| 17 | + |
| 18 | +namespace QDTool |
| 19 | +{ |
| 20 | + /// <summary> |
| 21 | + /// Interaction logic for HexBrowser.xaml |
| 22 | + /// </summary> |
| 23 | + public partial class HexBrowser : Window |
| 24 | + { |
| 25 | + public HexBrowser() |
| 26 | + { |
| 27 | + InitializeComponent(); |
| 28 | + } |
| 29 | + |
| 30 | + public void AddTextToCanvas(string text) |
| 31 | + { |
| 32 | + var textBlock = new TextBlock |
| 33 | + { |
| 34 | + Text = text, |
| 35 | + Foreground = Brushes.Black, |
| 36 | + Background = Brushes.White |
| 37 | + // Další nastavení vzhledu, pokud je potřeba |
| 38 | + }; |
| 39 | + |
| 40 | + Canvas.SetLeft(textBlock, 50); // Nastavte X pozici |
| 41 | + Canvas.SetTop(textBlock, 50); // Nastavte Y pozici |
| 42 | + canvas.Children.Add(textBlock); |
| 43 | + } |
| 44 | + |
| 45 | + private readonly byte[] SharpASCII = { |
| 46 | + (byte)'_', (byte)' ', (byte)'e', (byte)' ', (byte)'~', (byte)' ', (byte)'t', (byte)'g', |
| 47 | + (byte)'h', (byte)' ', (byte)'b', (byte)'x', (byte)'d', (byte)'r', (byte)'p', (byte)'c', |
| 48 | + (byte)'q', (byte)'a', (byte)'z', (byte)'w', (byte)'s', (byte)'u', (byte)'i', (byte)' ', |
| 49 | + (byte)' ', (byte)'k', (byte)'f', (byte)'v', (byte)' ', (byte)' ', (byte)' ', (byte)'j', |
| 50 | + (byte)'n', (byte)' ', (byte)' ', (byte)'m', (byte)' ', (byte)' ', (byte)' ', (byte)'o', |
| 51 | + (byte)'l', (byte)' ', (byte)' ', (byte)' ', (byte)' ', (byte)'y', (byte)'{', (byte)' ', |
| 52 | + (byte)'|' }; |
| 53 | + |
| 54 | + private byte FromSHASCII(byte c) |
| 55 | + { |
| 56 | + if (c <= 0x5d) return c; |
| 57 | + if (c == 0x80) return (byte)'}'; |
| 58 | + if (c < 0x90 || c > 0xc0) return (byte)' '; // z neznámých znaků uděláme ' ' |
| 59 | + return SharpASCII[c - 0x90]; |
| 60 | + } |
| 61 | + |
| 62 | + private string ConvertToHexDump(byte[] data, int bytesPerLine = 16) |
| 63 | + { |
| 64 | + StringBuilder hexDump = new StringBuilder(); |
| 65 | + |
| 66 | + for (int i = 0; i < data.Length; i += bytesPerLine) |
| 67 | + { |
| 68 | + hexDump.AppendFormat("{0:X8}: ", i); // Hexadecimální adresa |
| 69 | + |
| 70 | + // Hexadecimální hodnoty bajtů |
| 71 | + for (int j = 0; j < bytesPerLine; j++) |
| 72 | + { |
| 73 | + if (i + j < data.Length) |
| 74 | + hexDump.AppendFormat("{0:X2} ", data[i + j]); |
| 75 | + else |
| 76 | + hexDump.Append(" "); // Pro poslední řádek s méně než 16 bajty |
| 77 | + if ((i + j) % 4 == 3) |
| 78 | + hexDump.Append(' '); |
| 79 | + } |
| 80 | + |
| 81 | + //hexDump.Append(" "); |
| 82 | + |
| 83 | + // ASCII reprezentace bajtů |
| 84 | + //int cnt = 0; |
| 85 | + for (int j = 0; j < bytesPerLine; j++) |
| 86 | + { |
| 87 | + if (i + j < data.Length) |
| 88 | + { |
| 89 | + hexDump.Append(char.IsControl((char)data[i + j]) ? '.' : (char)data[i + j]); |
| 90 | + //cnt++; |
| 91 | + } |
| 92 | + else |
| 93 | + hexDump.Append(' '); // Pro poslední řádek s méně než 16 bajty |
| 94 | + } |
| 95 | + //for (int j = 0; j < bytesPerLine - cnt; j++) |
| 96 | + //{ |
| 97 | + // hexDump.Append(' '); |
| 98 | + //} |
| 99 | + |
| 100 | + hexDump.Append(" "); |
| 101 | + |
| 102 | + // ASCII reprezentace bajtů |
| 103 | + for (int j = 0; j < bytesPerLine; j++) |
| 104 | + { |
| 105 | + if (i + j < data.Length) |
| 106 | + hexDump.Append(char.IsControl((char)data[i + j]) ? '.' : (char)FromSHASCII(data[i + j])); |
| 107 | + } |
| 108 | + |
| 109 | + hexDump.AppendLine(); |
| 110 | + } |
| 111 | + return hexDump.ToString(); |
| 112 | + } |
| 113 | + |
| 114 | + public void ShowHexDump((MZQFileHeader , MZQFileBody) MzfBlock) |
| 115 | + { |
| 116 | + MZQFileHeader header = MzfBlock.Item1; |
| 117 | + MZQFileBody body = MzfBlock.Item2; |
| 118 | + |
| 119 | + StringBuilder hexDump = new StringBuilder(); |
| 120 | + |
| 121 | + byte[] mzfHeaderData = new byte[128]; |
| 122 | + mzfHeaderData[0] = header.MzfFtype; |
| 123 | + Array.Copy(header.MzfFname, 0, mzfHeaderData, 1, header.MzfFname.Length); |
| 124 | + mzfHeaderData[17] = header.MzfFnameEnd; |
| 125 | + var mzfSizeBytes = BitConverter.GetBytes(header.MzfSize); |
| 126 | + Array.Copy(mzfSizeBytes, 0, mzfHeaderData, 18, mzfSizeBytes.Length); |
| 127 | + var mzfStartBytes = BitConverter.GetBytes(header.MzfStart); |
| 128 | + Array.Copy(mzfStartBytes, 0, mzfHeaderData, 20, mzfStartBytes.Length); |
| 129 | + var mzfExecBytes = BitConverter.GetBytes(header.MzfExec); |
| 130 | + Array.Copy(mzfExecBytes, 0, mzfHeaderData, 22, mzfExecBytes.Length); |
| 131 | + Array.Copy(header.MzfHeaderDescription, 0, mzfHeaderData, 24, 38+2+64); |
| 132 | + |
| 133 | + byte[] qdfHeaderData = new byte[70]; |
| 134 | + qdfHeaderData[0] = 0xA5; |
| 135 | + CRC_check(0xA5, true); |
| 136 | + qdfHeaderData[1] = header.MzfHeaderSign; |
| 137 | + CRC_check(header.MzfHeaderSign); |
| 138 | + var dataSizeBytes = BitConverter.GetBytes(header.DataSize); |
| 139 | + Array.Copy(dataSizeBytes, 0, qdfHeaderData, 2, dataSizeBytes.Length); |
| 140 | + CRC_check(dataSizeBytes, 0, dataSizeBytes.Length); |
| 141 | + qdfHeaderData[4] = header.MzfFtype; |
| 142 | + CRC_check(header.MzfFtype); |
| 143 | + Array.Copy(header.MzfFname, 0, qdfHeaderData, 5, header.MzfFname.Length); |
| 144 | + CRC_check(header.MzfFname, 0, header.MzfFname.Length); |
| 145 | + qdfHeaderData[21] = header.MzfFnameEnd; |
| 146 | + CRC_check(header.MzfFnameEnd); |
| 147 | + Array.Copy(header.Unused1, 0, qdfHeaderData, 22, header.Unused1.Length); |
| 148 | + CRC_check(header.Unused1, 0, header.Unused1.Length); |
| 149 | + Array.Copy(mzfSizeBytes, 0, qdfHeaderData, 24, mzfSizeBytes.Length); |
| 150 | + CRC_check(mzfSizeBytes, 0, mzfSizeBytes.Length); |
| 151 | + Array.Copy(mzfStartBytes, 0, qdfHeaderData, 26, mzfStartBytes.Length); |
| 152 | + CRC_check(mzfStartBytes, 0, mzfStartBytes.Length); |
| 153 | + Array.Copy(mzfExecBytes, 0, qdfHeaderData, 28, mzfExecBytes.Length); |
| 154 | + CRC_check(mzfExecBytes, 0, mzfExecBytes.Length); |
| 155 | + Array.Copy(header.MzfHeaderDescription, 0, qdfHeaderData, 30, 38); |
| 156 | + ushort crc = CRC_check(header.MzfHeaderDescription, 0, 38); |
| 157 | + qdfHeaderData[68] = ReverseBits((byte)(crc >> 8)); |
| 158 | + qdfHeaderData[69] = ReverseBits((byte)(crc & 0xFF)); |
| 159 | + |
| 160 | + hexDump.Append("ADDRESS MZF HEADER DATA ASCII SHASCII (EU)"); |
| 161 | + hexDump.AppendLine(); |
| 162 | + hexDump.Append(ConvertToHexDump(mzfHeaderData)); |
| 163 | + hexDump.AppendLine(); |
| 164 | + |
| 165 | + hexDump.Append("ADDRESS QDF HEADER DATA ASCII SHASCII (EU)"); |
| 166 | + hexDump.AppendLine(); |
| 167 | + hexDump.Append(ConvertToHexDump(qdfHeaderData)); |
| 168 | + hexDump.AppendLine(); |
| 169 | + |
| 170 | + hexDump.Append("ADDRESS FILE DATA ASCII SHASCII (EU)"); |
| 171 | + hexDump.AppendLine(); |
| 172 | + hexDump.Append(ConvertToHexDump(body.MzfBody)); |
| 173 | + |
| 174 | + TextBlock textBlock = new TextBlock |
| 175 | + { |
| 176 | + Text = hexDump.ToString(), |
| 177 | + FontFamily = new FontFamily("Consolas"), // Monospace font pro lepší čitelnost |
| 178 | + TextWrapping = TextWrapping.NoWrap, |
| 179 | + Margin = new Thickness(10) |
| 180 | + }; |
| 181 | + |
| 182 | + double letterWidth = 0; |
| 183 | + double lineHeight = 0; |
| 184 | + |
| 185 | + textBlock.Loaded += (sender, e) => |
| 186 | + { |
| 187 | + canvas.MinHeight = textBlock.ActualHeight; |
| 188 | + |
| 189 | + Typeface typeface = new Typeface(textBlock.FontFamily, textBlock.FontStyle, textBlock.FontWeight, textBlock.FontStretch); |
| 190 | + double fontSize = textBlock.FontSize; |
| 191 | + double pixelsPerDip = VisualTreeHelper.GetDpi(textBlock).PixelsPerDip; |
| 192 | + |
| 193 | + // Předpokládáme, že používáte monospace font |
| 194 | + string sampleText = "M"; // Máme zvolené "M", protože je to často jeden z nejširších znaků v monospace fontech |
| 195 | + |
| 196 | + FormattedText formattedText = new FormattedText( |
| 197 | + sampleText, |
| 198 | + System.Globalization.CultureInfo.CurrentCulture, |
| 199 | + FlowDirection.LeftToRight, |
| 200 | + typeface, |
| 201 | + fontSize, |
| 202 | + Brushes.Black, |
| 203 | + new NumberSubstitution(), |
| 204 | + TextFormattingMode.Display, |
| 205 | + pixelsPerDip); |
| 206 | + |
| 207 | + letterWidth = formattedText.Width; // WidthIncludingTrailingWhitespace; |
| 208 | + lineHeight = formattedText.Height; |
| 209 | + |
| 210 | + Rectangle rect1 = new Rectangle |
| 211 | + { |
| 212 | + Width = letterWidth * 11 + 1, |
| 213 | + Height = lineHeight, |
| 214 | + Stroke = Brushes.Red, |
| 215 | + StrokeThickness = 1, |
| 216 | + Fill = null |
| 217 | + }; |
| 218 | + |
| 219 | + Rectangle rect2 = new Rectangle |
| 220 | + { |
| 221 | + Width = letterWidth * 5 + 2, |
| 222 | + Height = lineHeight, |
| 223 | + Stroke = Brushes.Blue, |
| 224 | + StrokeThickness = 1, |
| 225 | + Fill = null |
| 226 | + }; |
| 227 | + |
| 228 | + var transform = textBlock.TransformToAncestor(canvas); |
| 229 | + var position = transform.Transform(new Point(0, 0)); |
| 230 | + |
| 231 | + Canvas.SetLeft(rect1, position.X + letterWidth * 10 - 7); |
| 232 | + Canvas.SetTop(rect1, position.Y + lineHeight * 11 + 1); |
| 233 | + |
| 234 | + Canvas.SetLeft(rect2, position.X + letterWidth * 22 - 4); |
| 235 | + Canvas.SetTop(rect2, position.Y + lineHeight * 15 + 1); |
| 236 | + |
| 237 | + canvas.Children.Add(rect1); |
| 238 | + canvas.Children.Add(rect2); |
| 239 | + |
| 240 | + }; |
| 241 | + |
| 242 | + canvas.Children.Clear(); |
| 243 | + canvas.Children.Add(textBlock); |
| 244 | + |
| 245 | + } |
| 246 | + |
| 247 | + } |
| 248 | +} |
0 commit comments