/// <summary> /// Asynchronously disassembles the specified hex code array <paramref name="hex"/> and returns the result, this method does not block the calling thread. /// </summary> /// <returns>The resulting assembly code array from the disassembled hex code.</returns> /// <example> /// <code> /// string[] hex = { "00008052", "C0035FD6", "230040B9", "631C0012", "7F840171", "E1000054", "21010010", "FF8301D1", "FD2FFF17" }; /// string[] assembly = await Disassembler.MultiDisassembleAsync (hex); /// Console.WriteLine (string.Join ('\n', assembly)); /// </code> /// </example> /// <exception cref ="System.InvalidOperationException"> /// Thrown when trying to convert one of the ARM64 big-endian hex code elements to assembly code. /// </exception> /// <exception cref="System.Net.WebException"> /// Thrown when one of the elements of the hex code is invalid. /// </exception> /// See <see cref="Disassembler.DisassembleAsync(string, ArchSelection, int?)"/> to disassemble a single line of hex code asynchronously. /// <param name="hex">The hex code string array to disassemble.</param> /// <param name="archSelection">The architecture that the hex code <paramref name="hex"/> corresponds to.</param> /// <param name="offset">The offset integer that the hex code <paramref name="hex"/> is shifted by.</param> public static async Task <string[]> MultiDisassembleAsync(string[] hex, ArchSelection archSelection = ArchSelection.AArch64, int?offset = null) { var assembly = new List <string> (); foreach (string element in hex) { var webClient = new WebClient(); webClient.Headers[HttpRequestHeader.ContentType] = "application/json"; string json = $"{{\"hex\":\"{element}\",\"offset\":\"0x{offset ?? 0}\",\"arch\":\"{ArchToString(archSelection)}\"}}"; string result = await webClient.UploadStringTaskAsync("https://armconverter.com/api/convert", json); switch (archSelection) { case ArchSelection.AArch64: var aArch64Json = await AArch64Json.FromJsonAsync(result); string aArch64Assembly = aArch64Json.Asm.AArch64[1].ToString(); assembly.Add(aArch64Assembly); break; case ArchSelection.AArch32: var aArch32Json = await AArch32Json.FromJsonAsync(result); string aArch32Assembly = aArch32Json.Asm.AArch32[1].ToString(); assembly.Add(aArch32Assembly); break; case ArchSelection.AArch32BigEndian: var aArch32BigEndianJson = await AArch32BigEndianJson.FromJsonAsync(result); string aArch32BigEndianAssembly = aArch32BigEndianJson.Asm.AArch32BigEndian[1].ToString(); assembly.Add(aArch32BigEndianAssembly); break; case ArchSelection.Thumb: var thumbJson = await ThumbJson.FromJsonAsync(result); string thumbAssembly = thumbJson.Asm.Thumb[1].ToString(); assembly.Add(thumbAssembly); break; case ArchSelection.ThumbBigEndian: var thumbBigEndianJson = await ThumbBigEndianJson.FromJsonAsync(result); string thumbBigEndianAssembly = thumbBigEndianJson.Asm.ThumbBigEndian[1].ToString(); assembly.Add(thumbBigEndianAssembly); break; default: return(null); } } return(assembly.ToArray()); }
/// <summary> /// Asynchronously assembles the specified assembly code array <paramref name="assembly"/> and returns the result, this method does not block the calling thread. /// </summary> /// <returns>The resulting hex code array from the assembled assembly code.</returns> /// <example> /// <code> /// string[] assembly = { "mov w0, #0", "ret", "ldr w3, [x1]", "and w3, w3, #0xff", "cmp w3, #0x61", "b.ne #0x1c", "adr x1, #0x24", "sub sp, sp, #0x60", "b #0xfffffffffffcbff4" }; /// string[] hex = await Assembler.MultiAssembleAsync (assembly); /// Console.WriteLine (string.Join ('\n', hex)); /// </code> /// </example> /// <exception cref="System.FormatException"> /// Thrown when an error occurs after attempting to assemble one of the lines of the assembly code. /// </exception> /// See <see cref="Assembler.AssembleAsync(string, ArchSelection, int?)"/> to assemble a single line of assemble code asynchronously. /// <param name="assembly">The assembly code string array to assemble.</param> /// <param name="archSelection">The architecture that the assembly code <paramref name="assembly"/> corresponds to.</param> /// <param name="offset">The offset integer that the resulting hex code should be shifted by.</param> public static async Task <string[]> MultiAssembleAsync(string[] assembly, ArchSelection archSelection = ArchSelection.AArch64, int?offset = null) { var hex = new List <string> (); foreach (string line in assembly) { var webClient = new WebClient(); webClient.Headers[HttpRequestHeader.ContentType] = "application/json"; string json = $"{{\"asm\":\"{line}\",\"offset\":\"0x{offset ?? 0}\",\"arch\":\"{ArchToString(archSelection)}\"}}"; string result = await webClient.UploadStringTaskAsync("https://armconverter.com/api/convert", json); switch (archSelection) { case ArchSelection.AArch64: var aArch64Json = await AArch64Json.FromJsonAsync(result); string aArch64Hex = aArch64Json.Hex.AArch64[1].ToString().Replace("### ", string.Empty); if (ExceptionMessageList.Contains(aArch64Hex)) { throw new FormatException(aArch64Hex); } hex.Add(aArch64Hex); break; case ArchSelection.AArch32: var aArch32Json = await AArch32Json.FromJsonAsync(result); string aArch32Hex = aArch32Json.Hex.AArch32[1].ToString().Replace("### ", string.Empty); if (ExceptionMessageList.Contains(aArch32Hex)) { throw new FormatException(aArch32Hex); } hex.Add(aArch32Hex); break; case ArchSelection.AArch32BigEndian: var aArch32BigEndianJson = await AArch32BigEndianJson.FromJsonAsync(result); string aArch32BigEndianHex = aArch32BigEndianJson.Hex.AArch32BigEndian[1].ToString().Replace("### ", string.Empty); if (ExceptionMessageList.Contains(aArch32BigEndianHex)) { throw new FormatException(aArch32BigEndianHex); } hex.Add(aArch32BigEndianHex); break; case ArchSelection.Thumb: var thumbJson = await ThumbJson.FromJsonAsync(result); string thumbHex = thumbJson.Hex.Thumb[1].ToString().Replace("### ", string.Empty); if (ExceptionMessageList.Contains(thumbHex)) { throw new FormatException(thumbHex); } hex.Add(thumbHex); break; case ArchSelection.ThumbBigEndian: var thumbBigEndianJson = await ThumbBigEndianJson.FromJsonAsync(result); string thumbBigEndianHex = thumbBigEndianJson.Hex.ThumbBigEndian[1].ToString().Replace("### ", string.Empty); if (ExceptionMessageList.Contains(thumbBigEndianHex)) { throw new FormatException(thumbBigEndianHex); } hex.Add(thumbBigEndianHex); break; default: return(null); } } return(hex.ToArray()); }