repr 0.1
Reconstructable string representations and more
Loading...
Searching...
No Matches
undname.h
Go to the documentation of this file.
1#pragma once
2#include <array>
3#include <cstdlib>
4#include <climits>
5#include <memory>
9
10// Reference: https://github.com/nihilus/IDA_ClassInformer
11
12#ifndef REPR_DEMANGLE_MAX
13# define REPR_DEMANGLE_MAX 1024UL
14#endif
15
16namespace librepr::detail {
19
20namespace msvc {
21extern "C" {
22 using UndAlloc = void*(__cdecl*)(unsigned int);
23 using UndFree = void(__cdecl*)(void*);
24}
25
27 struct UndStrategy {
28 enum : unsigned int {
29 Complete = 0x00000, // Enable full undecoration
30 NoLeadingUnderscores = 0x00001, // Remove leading underscores from MS extended keywords
31 NoMsKeywords = 0x00002, // Disable expansion of MS extended keywords
32 NoFunctionReturns = 0x00004, // Disable expansion of return type for primary declaration
33 NoAllocationModel = 0x00008, // Disable expansion of the declaration model
34 NoAllocationLanguage = 0x00010, // Disable expansion of the declaration language specifier
35 NoMSThisType = 0x00020, // Disable expansion of MS keywords on the 'this' type for primary declaration
36 NoCVThisType = 0x00040, // Disable expansion of CV modifiers on the 'this' type for primary declaration
37 NoThisType = 0x00060, // Disable all modifiers on the 'this' type
38 NoAccessSpecifiers = 0x00080, // Disable expansion of access specifiers for members
39 NoThrowSignatures = 0x00100, // Disable expansion of 'throw-signatures' for functions and pointers to functions
40 NoMemberType = 0x00200, // Disable expansion of 'static' or 'virtual'ness of members
41 NoReturnUDTModel = 0x00400, // Disable expansion of MS model for UDT returns
42 Decode32Bit = 0x00800, // Undecorate 32-bit decorated names
43 NameOnly = 0x01000, // Crack only the name for primary declaration; return just [scope::]name. Does expand template params
44 TypeOnly = 0x02000, // Input is just a type encoding; compose an abstract declarator
45 HaveParameters = 0x04000, // The real templates parameters are available
46 NoECSU = 0x08000, // Suppress enum/class/struct/union
47 NoIdentCharCheck = 0x10000, // Suppress check for IsValidIdentChar
48 };
49
50 using Type = decltype(Complete);
51 using type = decltype(Complete);
52 };
53
55 void operator()(char* str) const
56 noexcept { std::free(str); }
57 };
58
60
61 inline void* __cdecl malloc_bridge(unsigned int size) {
62 return std::malloc(std::size_t(size));
63 }
64
65 extern "C" char* __cdecl __unDName( // NOLINT
66 char* buffer, const char* name, int buffer_size,
67 UndAlloc allocator, UndFree _free,
69 );
70
72 LIBREPR_FORCE_INLINE char* _unDName(const char* name,
73 char* buffer, std::size_t buffer_size, UndStrategy::Type flags) {
75 "The buffer_size {} is greater than INT_MAX ({}).", buffer_size, INT_MAX);
76 return __unDName(buffer, name, int(buffer_size), &malloc_bridge, &std::free, flags);
77 }
78
80 inline UndHandle unDName(const char* name, UndStrategy::Type flags) {
81 return UndHandle{ _unDName(name, nullptr, 0, flags) };
82 }
83
85 inline void unDName(const char* name, DemangleBuffer& buffer, UndStrategy::Type flags) {
86 [[maybe_unused]] char* ret = _unDName(name, buffer.data(), buffer.size(), flags);
87 LIBREPR_ASSERT((buffer.data() == ret), "__unDName output address did not match input buffer address.");
88 }
89} // namespace msvc
90
91using msvc::UndStrategy;
92using msvc::UndHandle;
93
94} // namespace librepr::detail
#define LIBREPR_ASSERT(cond,...)
Checked assertion, for constraint enforcement.
Definition assert.h:67
T free(T... args)
#define LIBREPR_FORCE_INLINE
Definition macro/util.h:52
T malloc(T... args)
UndHandle unDName(const char *name, UndStrategy::Type flags)
Returns a managed handle to the demangled buffer.
Definition undname.h:80
void *(__cdecl *)(unsigned int) UndAlloc
Definition undname.h:22
char *__cdecl __unDName(char *buffer, const char *name, int buffer_size, UndAlloc allocator, UndFree _free, UndStrategy::Type flags)
void *__cdecl malloc_bridge(unsigned int size)
Definition undname.h:61
std::unique_ptr< char[], UndFreeFunctor > UndHandle
Definition undname.h:59
void(__cdecl *)(void *) UndFree
Definition undname.h:23
LIBREPR_FORCE_INLINE char * _unDName(const char *name, char *buffer, std::size_t buffer_size, UndStrategy::Type flags)
Bridge for invoking __unDName.
Definition undname.h:72
Definition assert.h:89
std::string code_for()
Definition repr:39
void operator()(char *str) const noexcept
Definition undname.h:55
Flags for the method of undecoration.
Definition undname.h:27
decltype(Complete) type
Definition undname.h:51
@ TypeOnly
Definition undname.h:44
@ NoMemberType
Definition undname.h:40
@ HaveParameters
Definition undname.h:45
@ NoThrowSignatures
Definition undname.h:39
@ NoReturnUDTModel
Definition undname.h:41
@ Complete
Definition undname.h:29
@ NoMSThisType
Definition undname.h:35
@ NoECSU
Definition undname.h:46
@ NoAllocationLanguage
Definition undname.h:34
@ NoCVThisType
Definition undname.h:36
@ NoIdentCharCheck
Definition undname.h:47
@ NoAccessSpecifiers
Definition undname.h:38
@ NameOnly
Definition undname.h:43
@ NoMsKeywords
Definition undname.h:31
@ NoFunctionReturns
Definition undname.h:32
@ NoLeadingUnderscores
Definition undname.h:30
@ Decode32Bit
Definition undname.h:42
@ NoAllocationModel
Definition undname.h:33
@ NoThisType
Definition undname.h:37
decltype(Complete) Type
Definition undname.h:50