[9266] | 1 | /* $Id: DisasmFormatBytes.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
| 3 | * VBox Disassembler - Helper for formatting the opcode bytes.
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[76553] | 7 | * Copyright (C) 2008-2019 Oracle Corporation
|
---|
[9266] | 8 | *
|
---|
| 9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
| 10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
| 11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
| 12 | * General Public License (GPL) as published by the Free Software
|
---|
| 13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
| 14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
| 15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
| 16 | */
|
---|
| 17 |
|
---|
| 18 |
|
---|
[57358] | 19 | /*********************************************************************************************************************************
|
---|
| 20 | * Header Files *
|
---|
| 21 | *********************************************************************************************************************************/
|
---|
[9266] | 22 | #include "DisasmInternal.h"
|
---|
| 23 | #include <iprt/string.h>
|
---|
| 24 | #include <iprt/assert.h>
|
---|
[76455] | 25 | #include <iprt/errcore.h>
|
---|
[9266] | 26 |
|
---|
| 27 |
|
---|
| 28 | /**
|
---|
| 29 | * Helper function for formatting the opcode bytes.
|
---|
| 30 | *
|
---|
| 31 | * @returns The number of output bytes.
|
---|
| 32 | *
|
---|
[41789] | 33 | * @param pDis Pointer to the disassembler state.
|
---|
[9266] | 34 | * @param pszDst The output buffer.
|
---|
| 35 | * @param cchDst The size of the output buffer.
|
---|
| 36 | * @param fFlags The flags passed to the formatter.
|
---|
| 37 | */
|
---|
[41790] | 38 | size_t disFormatBytes(PCDISSTATE pDis, char *pszDst, size_t cchDst, uint32_t fFlags)
|
---|
[9266] | 39 | {
|
---|
[41658] | 40 | size_t cchOutput = 0;
|
---|
[41789] | 41 | uint32_t cb = pDis->cbInstr;
|
---|
[41658] | 42 | AssertStmt(cb <= 16, cb = 16);
|
---|
[9266] | 43 |
|
---|
| 44 | #define PUT_C(ch) \
|
---|
| 45 | do { \
|
---|
| 46 | cchOutput++; \
|
---|
| 47 | if (cchDst > 1) \
|
---|
| 48 | { \
|
---|
| 49 | cchDst--; \
|
---|
| 50 | *pszDst++ = (ch); \
|
---|
| 51 | } \
|
---|
| 52 | } while (0)
|
---|
| 53 | #define PUT_NUM(cch, fmt, num) \
|
---|
| 54 | do { \
|
---|
| 55 | cchOutput += (cch); \
|
---|
| 56 | if (cchDst > 1) \
|
---|
| 57 | { \
|
---|
| 58 | const size_t cchTmp = RTStrPrintf(pszDst, cchDst, fmt, (num)); \
|
---|
| 59 | pszDst += cchTmp; \
|
---|
| 60 | cchDst -= cchTmp; \
|
---|
| 61 | } \
|
---|
| 62 | } while (0)
|
---|
| 63 |
|
---|
| 64 |
|
---|
| 65 | if (fFlags & DIS_FMT_FLAGS_BYTES_BRACKETS)
|
---|
| 66 | PUT_C('[');
|
---|
| 67 |
|
---|
[18449] | 68 | for (uint32_t i = 0; i < cb; i++)
|
---|
[9266] | 69 | {
|
---|
| 70 | if (i != 0 && (fFlags & DIS_FMT_FLAGS_BYTES_SPACED))
|
---|
[41789] | 71 | PUT_NUM(3, " %02x", pDis->abInstr[i]);
|
---|
[9266] | 72 | else
|
---|
[41789] | 73 | PUT_NUM(2, "%02x", pDis->abInstr[i]);
|
---|
[9266] | 74 | }
|
---|
| 75 |
|
---|
| 76 | if (fFlags & DIS_FMT_FLAGS_BYTES_BRACKETS)
|
---|
| 77 | PUT_C(']');
|
---|
| 78 |
|
---|
| 79 | /* Terminate it just in case. */
|
---|
| 80 | if (cchDst >= 1)
|
---|
| 81 | *pszDst = '\0';
|
---|
| 82 |
|
---|
| 83 | #undef PUT_C
|
---|
| 84 | #undef PUT_NUM
|
---|
| 85 | return cchOutput;
|
---|
| 86 | }
|
---|
| 87 |
|
---|