/// <summary>
        /// Disassembles the specified hex code <paramref name="hex"/> and returns the result.
        /// </summary>
        /// <returns>The resulting assembly code from the disassembled hex code.</returns>
        /// <example>
        /// <code>
        /// string assembly = Disassembler.Disassemble ("00008052");
        /// Console.WriteLine (assembly);
        /// </code>
        /// </example>
        /// <exception cref="System.Net.WebException">
        /// Thrown when the hex code is invalid.
        /// </exception>
        /// See <see cref="Disassembler.MultiDisassemble(string[], ArchSelection, int?)"/> to disassemble an array of elements of hex code.
        /// <param name="hex">The hex code string 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 string Disassemble(string hex, ArchSelection archSelection = ArchSelection.AArch64, int?offset = null)
        {
            var webClient = new WebClient();

            webClient.Headers[HttpRequestHeader.ContentType] = "application/json";

            string json   = $"{{\"hex\":\"{hex}\",\"offset\":\"0x{offset ?? 0}\",\"arch\":\"{ArchToString(archSelection)}\"}}";
            string result = webClient.UploadString("https://armconverter.com/api/convert", json);

            switch (archSelection)
            {
            case ArchSelection.AArch64:
                string aArch64Assembly = AArch64Json.FromJson(result).Asm.AArch64[1].ToString();
                return(aArch64Assembly);

            case ArchSelection.AArch32:
                string aArch32Assembly = AArch32Json.FromJson(result).Asm.AArch32[1].ToString();
                return(aArch32Assembly);

            case ArchSelection.AArch32BigEndian:
                string aArch32BigEndianAssembly = AArch32BigEndianJson.FromJson(result).Asm.AArch32BigEndian[1].ToString();
                return(aArch32BigEndianAssembly);

            case ArchSelection.Thumb:
                string thumbAssembly = ThumbJson.FromJson(result).Asm.Thumb[1].ToString();
                return(thumbAssembly);

            case ArchSelection.ThumbBigEndian:
                string thumbBigEndianAssembly = ThumbBigEndianJson.FromJson(result).Asm.ThumbBigEndian[1].ToString();
                return(thumbBigEndianAssembly);

            default:
                return(null);
            }
        }
Пример #2
0
        /// <summary>
        /// Assembles the specified assembly code <paramref name="assembly"/> and returns the result.
        /// </summary>
        /// <returns>The resulting hex code from the assembled assembly code.</returns>
        /// <example>
        /// <code>
        /// string hex = Assembler.Assemble ("mov w0, #0");
        /// Console.WriteLine (hex);
        /// </code>
        /// </example>
        /// <exception cref="System.FormatException">
        /// Thrown when an error occurs after attempting to assemble the assembly code.
        /// </exception>
        /// See <see cref="Assembler.MultiAssemble(string[], ArchSelection, int?)"/> to assemble an array of lines of assemble code.
        /// <param name="assembly">The assembly code string 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 string Assemble(string assembly, ArchSelection archSelection = ArchSelection.AArch64, int?offset = null)
        {
            var webClient = new WebClient();

            webClient.Headers[HttpRequestHeader.ContentType] = "application/json";

            string json   = $"{{\"asm\":\"{assembly}\",\"offset\":\"0x{offset ?? 0}\",\"arch\":\"{ArchToString(archSelection)}\"}}";
            string result = webClient.UploadString("https://armconverter.com/api/convert", json);

            switch (archSelection)
            {
            case ArchSelection.AArch64:
                string aArch64Hex = AArch64Json.FromJson(result).Hex.AArch64[1].ToString().Replace("### ", string.Empty);

                if (ExceptionMessageList.Contains(aArch64Hex))
                {
                    throw new FormatException(aArch64Hex);
                }

                return(aArch64Hex);

            case ArchSelection.AArch32:
                string aArch32Hex = AArch32Json.FromJson(result).Hex.AArch32[1].ToString().Replace("### ", string.Empty);

                if (ExceptionMessageList.Contains(aArch32Hex))
                {
                    throw new FormatException(aArch32Hex);
                }

                return(aArch32Hex);

            case ArchSelection.AArch32BigEndian:
                string aArch32BigEndianHex = AArch32BigEndianJson.FromJson(result).Hex.AArch32BigEndian[1].ToString().Replace("### ", string.Empty);

                if (ExceptionMessageList.Contains(aArch32BigEndianHex))
                {
                    throw new FormatException(aArch32BigEndianHex);
                }

                return(aArch32BigEndianHex);

            case ArchSelection.Thumb:
                string thumbHex = ThumbJson.FromJson(result).Hex.Thumb[1].ToString().Replace("### ", string.Empty);

                if (ExceptionMessageList.Contains(thumbHex))
                {
                    throw new FormatException(thumbHex);
                }

                return(thumbHex);

            case ArchSelection.ThumbBigEndian:
                string thumbBigEndianHex = ThumbBigEndianJson.FromJson(result).Hex.ThumbBigEndian[1].ToString().Replace("### ", string.Empty);

                if (ExceptionMessageList.Contains(thumbBigEndianHex))
                {
                    throw new FormatException(thumbBigEndianHex);
                }

                return(thumbBigEndianHex);

            default:
                return(null);
            }
        }