| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.IO; |
| | | 4 | | using System.Linq; |
| | | 5 | | using System.Text; |
| | | 6 | | using System.Threading.Tasks; |
| | | 7 | | |
| | | 8 | | namespace DeltaSharp; |
| | | 9 | | |
| | | 10 | | |
| | | 11 | | internal class TargetGenerator |
| | | 12 | | { |
| | | 13 | | public byte[] Source => Encoding.UTF8.GetBytes(string.Join("\n", SourceLines) + "\n"); |
| | | 14 | | |
| | | 15 | | public List<string> SourceLines { get; private set; } |
| | | 16 | | |
| | | 17 | | Random _random; |
| | | 18 | | |
| | | 19 | | public TargetGenerator(string? source = null, Random? random=null) |
| | | 20 | | { |
| | | 21 | | _random = random ?? new Random(); |
| | | 22 | | |
| | | 23 | | if (source is not null) |
| | | 24 | | { |
| | | 25 | | var lines = source.Split('\n').Select(l => l.TrimEnd('\r')).ToList(); |
| | | 26 | | SourceLines = lines; |
| | | 27 | | } |
| | | 28 | | else |
| | | 29 | | { |
| | | 30 | | SourceLines = new List<string>(); |
| | | 31 | | } |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | public TargetGenerator(IEnumerable<string> lines, Random? random = null) |
| | | 35 | | { |
| | | 36 | | _random = random ?? new Random(); |
| | | 37 | | |
| | | 38 | | SourceLines = lines.ToList(); |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | public void Execute(string input) |
| | | 42 | | { |
| | | 43 | | var lines = input.Split('\n').Select(l => l.TrimEnd('\r')).ToArray(); |
| | | 44 | | |
| | | 45 | | lines = lines.SelectMany(l => l.Split(';')).Select(l => l.Trim()).ToArray(); |
| | | 46 | | |
| | | 47 | | var target = new List<string>(SourceLines); |
| | | 48 | | |
| | | 49 | | var operations = TargetGeneratorOperation.Parse(lines); |
| | | 50 | | |
| | | 51 | | Execute(operations); |
| | | 52 | | |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | public void Execute(TargetGeneratorOperation[] operations) |
| | | 56 | | { |
| | | 57 | | var target = SourceLines; |
| | | 58 | | |
| | | 59 | | foreach (var op in operations) |
| | | 60 | | op.Apply(target,_random); |
| | | 61 | | |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | internal enum OperationEnum |
| | | 67 | | { |
| | | 68 | | Set, |
| | | 69 | | Add, |
| | | 70 | | Del, |
| | | 71 | | Swp |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | internal class TargetGeneratorOperation |
| | | 75 | | { |
| | 480 | 76 | | public OperationEnum Op { get; set; } |
| | | 77 | | |
| | 1850 | 78 | | public string[] Args { get; set; } = Array.Empty<string>(); |
| | | 79 | | |
| | | 80 | | |
| | 240 | 81 | | private TargetGeneratorOperation() |
| | | 82 | | { |
| | 240 | 83 | | } |
| | | 84 | | |
| | | 85 | | public void Apply(List<string> source,Random random) |
| | | 86 | | { |
| | 240 | 87 | | switch (Op) |
| | | 88 | | { |
| | 60 | 89 | | case OperationEnum.Add: ApplyAdd(source, random); break; |
| | 60 | 90 | | case OperationEnum.Del: ApplyDel(source); break; |
| | 300 | 91 | | case OperationEnum.Set: ApplySet(source, random); break; |
| | 30 | 92 | | case OperationEnum.Swp: ApplySwap(source); break; |
| | | 93 | | } |
| | 30 | 94 | | } |
| | | 95 | | |
| | | 96 | | private void ApplyAdd(List<string> source,Random random) |
| | | 97 | | { |
| | 30 | 98 | | if (Args.Length <1) |
| | 0 | 99 | | throw new InvalidDataException("[ApplyAdd] invalid argument count"); |
| | | 100 | | |
| | 30 | 101 | | if (!int.TryParse(Args[0],out var position)) |
| | 0 | 102 | | throw new InvalidDataException("[ApplyAdd] invalid position"); |
| | | 103 | | |
| | 30 | 104 | | if (position > source.Count - 1 || position == -1) |
| | 30 | 105 | | position = source.Count; |
| | | 106 | | |
| | 30 | 107 | | var buffer = new byte[16]; |
| | | 108 | | |
| | 30 | 109 | | var count = 1; |
| | 30 | 110 | | var data = string.Empty; |
| | 30 | 111 | | if (Args.Length > 1) |
| | | 112 | | { |
| | 30 | 113 | | data = Args[1]; |
| | | 114 | | |
| | 30 | 115 | | if (Args.Length > 2) |
| | | 116 | | { |
| | 20 | 117 | | if (!int.TryParse(Args[2], out count)) |
| | 0 | 118 | | throw new InvalidDataException("[ApplySet] invalid count"); |
| | | 119 | | } |
| | | 120 | | } |
| | | 121 | | else |
| | | 122 | | { |
| | 0 | 123 | | random.NextBytes(buffer); |
| | | 124 | | |
| | 0 | 125 | | data = Convert.ToBase64String(buffer, Base64FormattingOptions.None) |
| | 0 | 126 | | .Replace("==", "").Replace("=", ""); |
| | | 127 | | } |
| | | 128 | | |
| | | 129 | | |
| | 200 | 130 | | while (count > 0) |
| | | 131 | | { |
| | 170 | 132 | | var s = data; |
| | 170 | 133 | | if (data == "RND") |
| | | 134 | | { |
| | 0 | 135 | | random.NextBytes(buffer); |
| | | 136 | | |
| | 0 | 137 | | s = Convert.ToBase64String(buffer, Base64FormattingOptions.None) |
| | 0 | 138 | | .Replace("==", "").Replace("=", ""); |
| | | 139 | | |
| | | 140 | | } |
| | 170 | 141 | | else if (data.StartsWith("RND(") && data.EndsWith(")")) |
| | | 142 | | { |
| | 160 | 143 | | var numberString = data.Replace("RND(", "").Replace(")", ""); |
| | 160 | 144 | | var randCount = int.Parse(numberString); |
| | | 145 | | |
| | 160 | 146 | | var tmp = new byte[randCount]; |
| | 5120 | 147 | | for (var i = 0; i < tmp.Length; i++) |
| | 2400 | 148 | | tmp[i] = (byte)random.Next(32, 127); |
| | | 149 | | |
| | 160 | 150 | | s = Encoding.UTF8.GetString(tmp).Replace('\r', 'r').Replace('\n', 'n'); |
| | | 151 | | } |
| | | 152 | | |
| | 170 | 153 | | source.Insert(position, s); |
| | 170 | 154 | | count--; |
| | | 155 | | } |
| | | 156 | | |
| | 30 | 157 | | } |
| | | 158 | | |
| | | 159 | | private void ApplySet(List<string> source,Random random) |
| | | 160 | | { |
| | 150 | 161 | | if (Args.Length < 1) |
| | 0 | 162 | | throw new InvalidDataException("[ApplySet] invalid argument count"); |
| | | 163 | | |
| | 150 | 164 | | if (!int.TryParse(Args[0], out var position)) |
| | 0 | 165 | | throw new InvalidDataException("[ApplySet] invalid position"); |
| | | 166 | | |
| | 150 | 167 | | if(position>source.Count-1) |
| | 0 | 168 | | throw new InvalidDataException("[ApplySet] invalid position"); |
| | | 169 | | |
| | 150 | 170 | | var buffer = new byte[16]; |
| | | 171 | | |
| | 150 | 172 | | int count = 1; |
| | 150 | 173 | | int step = 1; |
| | 150 | 174 | | var data = string.Empty; |
| | | 175 | | |
| | 150 | 176 | | if (Args.Length > 1) |
| | | 177 | | { |
| | 150 | 178 | | data = Args[1]; |
| | | 179 | | |
| | 150 | 180 | | if (Args.Length > 2) |
| | | 181 | | { |
| | 20 | 182 | | if (!int.TryParse(Args[2], out count)) |
| | 0 | 183 | | throw new InvalidDataException("[ApplySet] invalid count"); |
| | | 184 | | } |
| | | 185 | | |
| | 150 | 186 | | if (Args.Length > 3) |
| | | 187 | | { |
| | 20 | 188 | | if (!int.TryParse(Args[3], out step)) |
| | 0 | 189 | | throw new InvalidDataException("[ApplySet] invalid step"); |
| | | 190 | | } |
| | | 191 | | } |
| | | 192 | | else |
| | | 193 | | { |
| | | 194 | | |
| | 0 | 195 | | random.NextBytes(buffer); |
| | | 196 | | |
| | 0 | 197 | | data = Convert.ToBase64String(buffer, Base64FormattingOptions.None) |
| | 0 | 198 | | .Replace("==", "").Replace("=", ""); |
| | | 199 | | } |
| | | 200 | | |
| | 340 | 201 | | while(count > 0) |
| | | 202 | | { |
| | 190 | 203 | | var i = position + (count - 1) * step; |
| | | 204 | | |
| | 190 | 205 | | var s = data; |
| | 190 | 206 | | if (data == "RND") |
| | | 207 | | { |
| | 60 | 208 | | random.NextBytes(buffer); |
| | 60 | 209 | | s = Convert.ToBase64String(buffer, Base64FormattingOptions.None) |
| | 60 | 210 | | .Replace("==", "").Replace("=", ""); |
| | | 211 | | } |
| | 130 | 212 | | else if (data.StartsWith("RND(") && data.EndsWith(")")) |
| | | 213 | | { |
| | 0 | 214 | | var numberString = data.Replace("RND(", "").Replace(")", ""); |
| | 0 | 215 | | var randCount = int.Parse(numberString); |
| | | 216 | | |
| | 0 | 217 | | var tmp = new byte[randCount]; |
| | 0 | 218 | | for (var z = 0; z < tmp.Length; z++) |
| | 0 | 219 | | tmp[z] = (byte)random.Next(32, 127); |
| | | 220 | | |
| | 0 | 221 | | s = Encoding.UTF8.GetString(tmp).Replace('\r', 'r').Replace('\n', 'n'); |
| | | 222 | | } |
| | 130 | 223 | | else if (data == "HASH") |
| | | 224 | | { |
| | 120 | 225 | | var current = source[i]; |
| | 120 | 226 | | s = GetHashed(current); |
| | | 227 | | } |
| | | 228 | | |
| | 190 | 229 | | source[i] = s; |
| | | 230 | | |
| | 190 | 231 | | count--; |
| | | 232 | | } |
| | 150 | 233 | | } |
| | | 234 | | |
| | | 235 | | private void ApplyDel(List<string> source) |
| | | 236 | | { |
| | 30 | 237 | | if (Args.Length < 1) |
| | 0 | 238 | | throw new InvalidDataException("[ApplyDel] invalid argument count"); |
| | | 239 | | |
| | 30 | 240 | | if (!int.TryParse(Args[0], out var position)) |
| | 0 | 241 | | throw new InvalidDataException("[ApplyDel] invalid position"); |
| | | 242 | | |
| | 30 | 243 | | var count = 1; |
| | 30 | 244 | | if (Args.Length > 1) |
| | | 245 | | { |
| | 30 | 246 | | if (!int.TryParse(Args[1], out count)) |
| | 0 | 247 | | throw new InvalidDataException("[ApplyDel] invalid count"); |
| | | 248 | | } |
| | | 249 | | |
| | 30 | 250 | | source.RemoveRange(position, count); |
| | 30 | 251 | | } |
| | | 252 | | |
| | | 253 | | private void ApplySwap(List<string> source) |
| | | 254 | | { |
| | 30 | 255 | | if (Args.Length < 2) |
| | 0 | 256 | | throw new InvalidDataException("[ApplySwap] invalid argument count"); |
| | | 257 | | |
| | 30 | 258 | | if (!int.TryParse(Args[0], out var n1)) |
| | 0 | 259 | | throw new InvalidDataException("[ApplySwap] invalid position 1"); |
| | | 260 | | |
| | 30 | 261 | | if (!int.TryParse(Args[1], out var n2)) |
| | 0 | 262 | | throw new InvalidDataException("[ApplySwap] invalid position 2"); |
| | | 263 | | |
| | 30 | 264 | | int n3 = 1; |
| | 30 | 265 | | if(Args.Length > 2) |
| | | 266 | | { |
| | | 267 | | //swap block |
| | | 268 | | |
| | 20 | 269 | | if (!int.TryParse(Args[2], out n3)) |
| | 0 | 270 | | throw new InvalidDataException("[ApplySwap] invalid position 2"); |
| | | 271 | | |
| | | 272 | | } |
| | 30 | 273 | | if (n2 + n3 > source.Count || n1 + n3 > source.Count) |
| | 0 | 274 | | throw new InvalidDataException("[ApplySwap] invalid range"); |
| | | 275 | | |
| | 240 | 276 | | foreach (var i in Enumerable.Range(0, n3)) |
| | | 277 | | { |
| | 90 | 278 | | var s = source[n1 + i]; |
| | 90 | 279 | | var s2 = source[n2 + i]; |
| | 90 | 280 | | source[n1 + i] = s2; |
| | 90 | 281 | | source[n2 + i] = s; |
| | | 282 | | } |
| | | 283 | | |
| | 30 | 284 | | } |
| | | 285 | | |
| | | 286 | | private string GetHashed(string source) |
| | | 287 | | { |
| | 120 | 288 | | var sb = new StringBuilder(); |
| | | 289 | | |
| | 120 | 290 | | using var md5 = System.Security.Cryptography.MD5.Create(); |
| | 120 | 291 | | var skip = 0; |
| | 300 | 292 | | while (sb.Length < source.Length) |
| | | 293 | | { |
| | 180 | 294 | | var s = source.Substring(skip, Math.Min(16, source.Length - skip)); |
| | 180 | 295 | | var hash = md5.ComputeHash(Encoding.UTF8.GetBytes(s)); |
| | 180 | 296 | | var hashString = Convert.ToBase64String(hash).Replace("==", "").Replace("=", ""); |
| | 180 | 297 | | sb.Append(hashString); |
| | | 298 | | |
| | 180 | 299 | | skip++; |
| | | 300 | | } |
| | | 301 | | |
| | 120 | 302 | | return sb.ToString().Substring(0, source.Length); |
| | 120 | 303 | | } |
| | | 304 | | |
| | | 305 | | public static TargetGeneratorOperation ParseOperation(string s) |
| | | 306 | | { |
| | 240 | 307 | | var opString = s.Substring(0, 3); |
| | 240 | 308 | | if (!Enum.TryParse<OperationEnum>(opString, ignoreCase: true, out var op)) |
| | 0 | 309 | | throw new InvalidDataException($"invalid operation {opString}"); |
| | | 310 | | |
| | 240 | 311 | | var args = s.Split(' ').Skip(1); |
| | | 312 | | |
| | 240 | 313 | | return new TargetGeneratorOperation() |
| | 240 | 314 | | { |
| | 240 | 315 | | Op = op, |
| | 240 | 316 | | Args = args.ToArray() |
| | 240 | 317 | | }; |
| | | 318 | | } |
| | | 319 | | |
| | | 320 | | public static TargetGeneratorOperation[] Parse(string[] lines) |
| | | 321 | | { |
| | | 322 | | |
| | 150 | 323 | | var operations = new List<TargetGeneratorOperation>(); |
| | | 324 | | |
| | 920 | 325 | | foreach(var line in lines) |
| | 310 | 326 | | if (!string.IsNullOrWhiteSpace(line)) |
| | 240 | 327 | | operations.Add(ParseOperation(line.Trim())); |
| | | 328 | | |
| | 150 | 329 | | return operations.ToArray(); |
| | | 330 | | } |
| | | 331 | | } |
| | | 332 | | |