|
1 | 1 | using System; |
| 2 | +using System.Collections; |
2 | 3 | using System.Collections.Generic; |
3 | 4 | using System.Text.RegularExpressions; |
4 | 5 |
|
| 6 | +public class ByteReaderData { |
| 7 | + public List<CodeLabel> labels; |
| 8 | + public byte[] block; |
| 9 | + public bool completed = false; |
| 10 | +} |
| 11 | + |
5 | 12 | public class ByteReader { |
6 | 13 | public enum ReadMode { Dec, Hex, Bin }; |
7 | 14 |
|
8 | 15 | readonly static Regex rgComments = new Regex("/\\*(?>(?:(?>[^*]+)|\\*(?!/))*)\\*/", RegexOptions.IgnoreCase, TimeSpan.FromSeconds(1)); |
9 | 16 | readonly static Regex rgComment = new Regex("//(.*?)\r?\n", RegexOptions.IgnoreCase, TimeSpan.FromSeconds(1)); |
10 | 17 |
|
| 18 | + internal static IEnumerator ReadBlock(string data, int pbar, ByteReaderData res) { |
| 19 | + ReadMode mode = ReadMode.Dec; |
| 20 | + data = rgComments.Replace(data, ""); |
| 21 | + data = rgComment.Replace(data, ""); |
| 22 | + data = data.Trim().Replace('\r', ' ').Replace('\n', ' '); |
| 23 | + while (data.IndexOf(" ") != -1) data = data.Replace(" ", " "); |
| 24 | + |
| 25 | + res.labels = new List<CodeLabel>(); |
| 26 | + Consolidator consolidator = new Consolidator(); |
| 27 | + int tot = data.Length; |
| 28 | + while (data.Length > 0) { |
| 29 | + yield return PBar.Progress(pbar + 100 * (tot - data.Length) / tot); |
| 30 | + // Get the part, it is whatever we got until a whitespace |
| 31 | + string part = ""; |
| 32 | + foreach (char c in data) { |
| 33 | + if (c == ' ') break; |
| 34 | + part += c; |
| 35 | + } |
| 36 | + part = part.ToLowerInvariant(); |
| 37 | + // What we have? |
| 38 | + if (part[part.Length - 1] == ':') { // Label |
| 39 | + res.labels.Add(new CodeLabel { name = part.Substring(0, part.Length - 1).Trim(), start = consolidator.GetPos() }); |
| 40 | + data = data.Substring(part.Length).Trim(); |
| 41 | + continue; |
| 42 | + } |
| 43 | + else if (part.Equals("}")) { |
| 44 | + // Consolidate all parts we found |
| 45 | + res.block = consolidator.Consolidate(); |
| 46 | + yield return PBar.Progress(pbar + 100); |
| 47 | + res.completed = true; |
| 48 | + yield break; |
| 49 | + } |
| 50 | + else if (part.Equals("usehex")) { |
| 51 | + mode = ReadMode.Hex; |
| 52 | + data = data.Substring(part.Length).Trim(); |
| 53 | + continue; |
| 54 | + } |
| 55 | + else if (part.Equals("usebin")) { |
| 56 | + mode = ReadMode.Bin; |
| 57 | + data = data.Substring(part.Length).Trim(); |
| 58 | + continue; |
| 59 | + } |
| 60 | + else if (part.Equals("usedec")) { |
| 61 | + mode = ReadMode.Dec; |
| 62 | + data = data.Substring(part.Length).Trim(); |
| 63 | + continue; |
| 64 | + } |
| 65 | + // Data |
| 66 | + |
| 67 | + data = data.Substring(part.Length).Trim(); |
| 68 | + if ((part.Length > 2 && part[0] == '0' && part[1] == 'x') || (part.Length > 1 && part[0] == 'x')) { // Do we start with 0x? |
| 69 | + if (part[0] == 'x') |
| 70 | + part = part.Substring(1); |
| 71 | + else |
| 72 | + part = part.Substring(2); |
| 73 | + // Get 2 chars as a byte in hex, and continue till the end of the string |
| 74 | + while (part.Length > 0) { |
| 75 | + string val = part[0] + (part.Length > 1 ? part[1].ToString() : " "); |
| 76 | + try { |
| 77 | + consolidator.AddByte((byte)Convert.ToInt32(val, 16)); |
| 78 | + } catch(Exception) { |
| 79 | + throw new Exception("Cannot parse \"" + val + "\" as Hex"); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + else if (part.Length > 2 && part[0] == '0' && part[1] == 'b') { // Do we start with 0b? |
| 84 | + part = part.Substring(2); |
| 85 | + // Parse all value, then split in bytes |
| 86 | + int b; |
| 87 | + try { |
| 88 | + b = Convert.ToInt32(part, 2); |
| 89 | + } catch (Exception) { |
| 90 | + throw new Exception("Cannot parse \"" + part + "\" as Binary"); |
| 91 | + } |
| 92 | + if (b < 256) consolidator.AddByte((byte)b); |
| 93 | + else if (b < 65536) { |
| 94 | + byte l = (byte)(b & 0xff); |
| 95 | + byte h = (byte)((b & 0xff00) >> 8); |
| 96 | + consolidator.AddByte(h); |
| 97 | + consolidator.AddByte(l); |
| 98 | + } |
| 99 | + else { |
| 100 | + byte b0 = (byte)(b & 0xff); |
| 101 | + byte b1 = (byte)((b & 0xff00) >> 8); |
| 102 | + byte b2 = (byte)((b & 0xff0000) >> 16); |
| 103 | + byte b3 = (byte)((b & 0xff000000) >> 24); |
| 104 | + consolidator.AddByte(b3); |
| 105 | + consolidator.AddByte(b2); |
| 106 | + consolidator.AddByte(b1); |
| 107 | + consolidator.AddByte(b0); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + if (mode == ReadMode.Dec) { // Parse it as dec, split in bytes |
| 112 | + int b; |
| 113 | + try { |
| 114 | + b = Convert.ToInt32(part, 10); |
| 115 | + } catch (Exception) { |
| 116 | + PBar.Hide(); |
| 117 | + throw new Exception("Cannot parse \"" + part + "\" as Decimal"); |
| 118 | + } |
| 119 | + if (b < 256) consolidator.AddByte((byte)b); |
| 120 | + else if (b < 65536) { |
| 121 | + byte l = (byte)(b & 0xff); |
| 122 | + byte h = (byte)((b & 0xff00) >> 8); |
| 123 | + consolidator.AddByte(h); |
| 124 | + consolidator.AddByte(l); |
| 125 | + } |
| 126 | + else { |
| 127 | + byte b0 = (byte)(b & 0xff); |
| 128 | + byte b1 = (byte)((b & 0xff00) >> 8); |
| 129 | + byte b2 = (byte)((b & 0xff0000) >> 16); |
| 130 | + byte b3 = (byte)((b & 0xff000000) >> 24); |
| 131 | + consolidator.AddByte(b3); |
| 132 | + consolidator.AddByte(b2); |
| 133 | + consolidator.AddByte(b1); |
| 134 | + consolidator.AddByte(b0); |
| 135 | + } |
| 136 | + } |
| 137 | + else if (mode == ReadMode.Hex) { // Parse it as hex |
| 138 | + while (part.Length > 0) { |
| 139 | + string val = part[0] + (part.Length > 1 ? part[1].ToString() : " "); |
| 140 | + try { |
| 141 | + consolidator.AddByte((byte)Convert.ToInt32(val, 16)); |
| 142 | + } catch (Exception) { |
| 143 | + PBar.Hide(); |
| 144 | + throw new Exception("Cannot parse \"" + val + "\" as Hex"); |
| 145 | + } |
| 146 | + if (part.Length < 3) break; |
| 147 | + part = part.Substring(2); |
| 148 | + } |
| 149 | + } |
| 150 | + else if (mode == ReadMode.Bin) { // Parse it as bin, split in bytes |
| 151 | + int b; |
| 152 | + try { |
| 153 | + b = Convert.ToInt32(part, 2); |
| 154 | + } catch (Exception) { |
| 155 | + PBar.Hide(); |
| 156 | + throw new Exception("Cannot parse \"" + part + "\" as Binary"); |
| 157 | + } |
| 158 | + if (b < 256) consolidator.AddByte((byte)b); |
| 159 | + else if (b < 65536) { |
| 160 | + byte l = (byte)(b & 0xff); |
| 161 | + byte h = (byte)((b & 0xff00) >> 8); |
| 162 | + consolidator.AddByte(h); |
| 163 | + consolidator.AddByte(l); |
| 164 | + } |
| 165 | + else { |
| 166 | + byte b0 = (byte)(b & 0xff); |
| 167 | + byte b1 = (byte)((b & 0xff00) >> 8); |
| 168 | + byte b2 = (byte)((b & 0xff0000) >> 16); |
| 169 | + byte b3 = (byte)((b & 0xff000000) >> 24); |
| 170 | + consolidator.AddByte(b3); |
| 171 | + consolidator.AddByte(b2); |
| 172 | + consolidator.AddByte(b1); |
| 173 | + consolidator.AddByte(b0); |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + yield return PBar.Progress(pbar + 100); |
| 178 | + // Consolidate all parts we found |
| 179 | + res.block = consolidator.Consolidate(); |
| 180 | + res.completed = true; |
| 181 | + } |
| 182 | + |
| 183 | + |
| 184 | + |
11 | 185 | internal static void ReadBlock(string data, out List<CodeLabel> labels, out byte[] block) { |
12 | 186 | ReadMode mode = ReadMode.Dec; |
13 | 187 | data = rgComments.Replace(data, ""); |
@@ -65,7 +239,7 @@ internal static void ReadBlock(string data, out List<CodeLabel> labels, out byte |
65 | 239 | string val = part[0] + (part.Length > 1 ? part[1].ToString() : " "); |
66 | 240 | try { |
67 | 241 | consolidator.AddByte((byte)Convert.ToInt32(val, 16)); |
68 | | - } catch(Exception) { |
| 242 | + } catch (Exception) { |
69 | 243 | throw new Exception("Cannot parse \"" + val + "\" as Hex"); |
70 | 244 | } |
71 | 245 | } |
|
0 commit comments