1 | /* $Id: DisasmFormatBytes.cpp 99220 2023-03-30 12:40:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Disassembler - Helper for formatting the opcode bytes.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 |
|
---|
29 | /*********************************************************************************************************************************
|
---|
30 | * Header Files *
|
---|
31 | *********************************************************************************************************************************/
|
---|
32 | #include "DisasmInternal.h"
|
---|
33 | #include <iprt/string.h>
|
---|
34 | #include <iprt/assert.h>
|
---|
35 | #include <iprt/errcore.h>
|
---|
36 |
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Helper function for formatting the opcode bytes.
|
---|
40 | *
|
---|
41 | * @returns The number of output bytes.
|
---|
42 | *
|
---|
43 | * @param pDis Pointer to the disassembler state.
|
---|
44 | * @param pszDst The output buffer.
|
---|
45 | * @param cchDst The size of the output buffer.
|
---|
46 | * @param fFlags The flags passed to the formatter.
|
---|
47 | */
|
---|
48 | size_t disFormatBytes(PCDISSTATE pDis, char *pszDst, size_t cchDst, uint32_t fFlags)
|
---|
49 | {
|
---|
50 | size_t cchOutput = 0;
|
---|
51 | uint32_t cb = pDis->cbInstr;
|
---|
52 | AssertStmt(cb <= 16, cb = 16);
|
---|
53 |
|
---|
54 | #define PUT_C(ch) \
|
---|
55 | do { \
|
---|
56 | cchOutput++; \
|
---|
57 | if (cchDst > 1) \
|
---|
58 | { \
|
---|
59 | cchDst--; \
|
---|
60 | *pszDst++ = (ch); \
|
---|
61 | } \
|
---|
62 | } while (0)
|
---|
63 | #define PUT_NUM(cch, fmt, num) \
|
---|
64 | do { \
|
---|
65 | cchOutput += (cch); \
|
---|
66 | if (cchDst > 1) \
|
---|
67 | { \
|
---|
68 | const size_t cchTmp = RTStrPrintf(pszDst, cchDst, fmt, (num)); \
|
---|
69 | pszDst += cchTmp; \
|
---|
70 | cchDst -= cchTmp; \
|
---|
71 | } \
|
---|
72 | } while (0)
|
---|
73 |
|
---|
74 |
|
---|
75 | if (fFlags & DIS_FMT_FLAGS_BYTES_BRACKETS)
|
---|
76 | PUT_C('[');
|
---|
77 |
|
---|
78 | for (uint32_t i = 0; i < cb; i++)
|
---|
79 | {
|
---|
80 | if (i != 0 && (fFlags & DIS_FMT_FLAGS_BYTES_SPACED))
|
---|
81 | PUT_NUM(3, " %02x", pDis->u.abInstr[i]);
|
---|
82 | else
|
---|
83 | PUT_NUM(2, "%02x", pDis->u.abInstr[i]);
|
---|
84 | }
|
---|
85 |
|
---|
86 | if (fFlags & DIS_FMT_FLAGS_BYTES_BRACKETS)
|
---|
87 | PUT_C(']');
|
---|
88 |
|
---|
89 | /* Terminate it just in case. */
|
---|
90 | if (cchDst >= 1)
|
---|
91 | *pszDst = '\0';
|
---|
92 |
|
---|
93 | #undef PUT_C
|
---|
94 | #undef PUT_NUM
|
---|
95 | return cchOutput;
|
---|
96 | }
|
---|
97 |
|
---|