|
| 1 | +// This plugin renders 64-bit binary integer decimal floating point constants directly in the |
| 2 | +// decompilation. See the sample binary at `examples/bid64_constant/sample_binary` for an |
| 3 | +// example of a binary that uses this unusual format. |
| 4 | + |
| 5 | +#define _CRT_SECURE_NO_WARNINGS |
| 6 | +#include <cinttypes> |
| 7 | +#include <cstdio> |
| 8 | +#include <cstring> |
| 9 | +#include <map> |
| 10 | +#include <functional> |
| 11 | +#include <vector> |
| 12 | +#include "binaryninjaapi.h" |
| 13 | + |
| 14 | +using namespace BinaryNinja; |
| 15 | +using namespace std; |
| 16 | + |
| 17 | + |
| 18 | +static string Bid64ToString(bool sign, uint64_t magnitude, int exponent) |
| 19 | +{ |
| 20 | + if (magnitude == 0) |
| 21 | + exponent = 0; |
| 22 | + |
| 23 | + string digits = to_string(magnitude); |
| 24 | + int intPartDigits = digits.length() + exponent; |
| 25 | + |
| 26 | + string fracDigits; |
| 27 | + if (intPartDigits < 0) |
| 28 | + fracDigits = digits; |
| 29 | + else if (intPartDigits <= digits.length()) |
| 30 | + fracDigits = digits.substr(intPartDigits); |
| 31 | + |
| 32 | + int trailingZeros = 0; |
| 33 | + for (size_t i = 0; i < fracDigits.length(); i++) |
| 34 | + { |
| 35 | + if (fracDigits[(fracDigits.length() - 1) - i] != '0') |
| 36 | + break; |
| 37 | + trailingZeros++; |
| 38 | + } |
| 39 | + |
| 40 | + int nonzeroFracDigits = fracDigits.length() - trailingZeros; |
| 41 | + fracDigits = fracDigits.substr(0, nonzeroFracDigits); |
| 42 | + |
| 43 | + string result; |
| 44 | + if (intPartDigits > 0) |
| 45 | + { |
| 46 | + for (size_t i = 0; i < intPartDigits; i++) |
| 47 | + { |
| 48 | + if (i >= digits.length()) |
| 49 | + result += "0"; |
| 50 | + else |
| 51 | + result += string(1, digits[i]); |
| 52 | + } |
| 53 | + } |
| 54 | + else |
| 55 | + { |
| 56 | + result = "0"; |
| 57 | + } |
| 58 | + |
| 59 | + if (intPartDigits < 0 && fracDigits.length() > 0) |
| 60 | + { |
| 61 | + result += "."; |
| 62 | + for (size_t i = 0; i < -intPartDigits; i++) |
| 63 | + result += "0"; |
| 64 | + result += fracDigits; |
| 65 | + } |
| 66 | + else if (fracDigits.length() > 0) |
| 67 | + { |
| 68 | + result += "."; |
| 69 | + result += fracDigits; |
| 70 | + } |
| 71 | + |
| 72 | + return result; |
| 73 | +} |
| 74 | + |
| 75 | + |
| 76 | +class Bid64ConstantRenderer : public ConstantRenderer |
| 77 | +{ |
| 78 | +public: |
| 79 | + Bid64ConstantRenderer() : ConstantRenderer("bid64_constant") |
| 80 | + { |
| 81 | + } |
| 82 | + |
| 83 | + bool RenderConstant(const HighLevelILInstruction&, Type* type, int64_t val, HighLevelILTokenEmitter& tokens, |
| 84 | + DisassemblySettings* settings, BNOperatorPrecedence) override |
| 85 | + { |
| 86 | + // Typedef name doesn't survive propagation, but check for 8 byte integers with the |
| 87 | + // alternate name "long long unsigned int", which is what BID_UINT64 is a typedef for. |
| 88 | + if (!type || type->GetClass() != IntegerTypeClass) |
| 89 | + return false; |
| 90 | + if (type->GetWidth() != 8) |
| 91 | + return false; |
| 92 | + if (type->GetAlternateName() != "long long unsigned int") |
| 93 | + return false; |
| 94 | + |
| 95 | + // Get sign bit and raw exponent |
| 96 | + bool sign = (val & (1LL << 63)) != 0; |
| 97 | + int rawExponent = (int)((val >> 53) & 0x3ff); |
| 98 | + if (rawExponent >= 0x300) |
| 99 | + { |
| 100 | + // Don't try and render NaN or infinity |
| 101 | + return false; |
| 102 | + } |
| 103 | + |
| 104 | + // Get magnitude and actual exponent |
| 105 | + constexpr uint64_t BIAS = 398; |
| 106 | + int exponent = rawExponent - BIAS; |
| 107 | + uint64_t magnitude = val & ((1LL << 53) - 1); |
| 108 | + |
| 109 | + tokens.Append(FloatingPointToken, Bid64ToString(sign, magnitude, exponent) + "_bid"); |
| 110 | + return true; |
| 111 | + } |
| 112 | +}; |
| 113 | + |
| 114 | + |
| 115 | +extern "C" |
| 116 | +{ |
| 117 | + BN_DECLARE_CORE_ABI_VERSION |
| 118 | + |
| 119 | + BINARYNINJAPLUGIN void CorePluginDependencies() |
| 120 | + { |
| 121 | + } |
| 122 | + |
| 123 | + BINARYNINJAPLUGIN bool CorePluginInit() |
| 124 | + { |
| 125 | + ConstantRenderer::Register(new Bid64ConstantRenderer()); |
| 126 | + return true; |
| 127 | + } |
| 128 | +} |
0 commit comments