Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
102446e
Seven (7/347) Tests Pass GNU v2 Rewrite
brightprogrammer Jan 3, 2025
4ff587b
(31/349) Function Param Type Demangling
brightprogrammer Jan 3, 2025
078a388
Update CI Without `with_gpl` Meson Option
brightprogrammer Jan 3, 2025
6847f23
(74/348) Demangling of Refs & Ptrs In Function Params
brightprogrammer Jan 4, 2025
ea348f0
Remove `r-value` Reference Demangle Check
brightprogrammer Jan 4, 2025
d2c70c7
Update Reuse Config & Remove GPL License File
brightprogrammer Jan 4, 2025
30b2d0f
Added Operators, Now Total 159 Tests Pass
brightprogrammer Jan 7, 2025
a338379
Refactor Function Params To A Vector
brightprogrammer Jan 9, 2025
9529a03
(242) Template Parameters Added
brightprogrammer Jan 14, 2025
b762255
(252) Template Parameters : Some Working, Some Need Fixing
brightprogrammer Jan 14, 2025
b3bc0e2
(269) Template Classes Working
brightprogrammer Jan 16, 2025
364b4e9
(323) Function Pointers & Virtual Tables Remain
brightprogrammer Jan 16, 2025
0f70055
(343) Just Two Test Cases Remain To Pass
brightprogrammer Jan 17, 2025
8e22ad4
(344) Complete GNU v2
brightprogrammer Jan 17, 2025
ac42199
First Run Itanium ABI (GPL v3)
brightprogrammer Feb 1, 2025
41269bb
Fix Recursion Problems
brightprogrammer Feb 2, 2025
150707b
V3 Progress & Some Tests Fix
brightprogrammer Feb 10, 2025
2e9e1ca
Improve Substitution
brightprogrammer Feb 11, 2025
5e12019
Remove Match & Do & Improve Optional Matches
brightprogrammer Feb 11, 2025
6905fe8
Many Tests Passing, Some Tests Just Need Readjustment To Pass, Templa…
brightprogrammer Feb 11, 2025
793d3d9
Test File Split. Lots Of Tests Passing. Templates, Ctors & Dtors Remain
brightprogrammer Feb 12, 2025
72de14d
Better Substitutable Type Detection
brightprogrammer Feb 12, 2025
b3db303
Templates Working, Some Substitution Detection Bug Exist
brightprogrammer Feb 13, 2025
a63a044
Branch From Actual Grammar & Many Many Tests Pass
brightprogrammer Feb 14, 2025
fcc7d24
Better Substitutions
brightprogrammer Feb 14, 2025
11d5a47
Std Namespace Use Correctly
brightprogrammer Feb 14, 2025
6175e0b
Detecting Functors & Better Type Detection & Substitution
brightprogrammer Feb 15, 2025
9132efa
Template Ctor & Dtor
brightprogrammer Feb 16, 2025
bcc72eb
Template Params (`T[N]_`)
brightprogrammer Feb 16, 2025
94a0de8
Substitution Bugs Exist. Pointer To Member Type Demangling Works.
brightprogrammer Mar 20, 2025
597e260
Constructors With Template Arguments
brightprogrammer Mar 21, 2025
861dc9d
Inline Rewrite Of Boolean Values
brightprogrammer Mar 21, 2025
d270403
Better Const Propagation
brightprogrammer Mar 21, 2025
e477ba3
Test Fix
brightprogrammer Apr 18, 2025
e44a39e
Substution Improvements & Test Fixes
brightprogrammer Apr 19, 2025
7c182cd
Template Param Detection Bug
brightprogrammer Apr 19, 2025
c966d9c
Fix Detected Types In `RULE(prefix)`
brightprogrammer Apr 19, 2025
8b9b656
Expr Primary Fix
brightprogrammer Apr 19, 2025
9a60257
Test Fixes & Prefix Bug Fix
brightprogrammer Apr 19, 2025
baaa269
Append Type Only When `const` is Added In `RULE(name)`
brightprogrammer Apr 19, 2025
bacc6ea
Force Append Type On Template Substitution
brightprogrammer Apr 19, 2025
3350f93
More Green, More Tests Pass
brightprogrammer Apr 19, 2025
d0cfdff
Detect Names Before Templates Are Parsed As Well
brightprogrammer Apr 20, 2025
b64090c
Better Template Detection
brightprogrammer Apr 24, 2025
bee9a05
More Tests Pass Less Failures Remain
brightprogrammer Apr 27, 2025
2949402
Test Fixes
brightprogrammer Apr 27, 2025
aa10fa1
Template Param Substitutions Must Be Forcefully Appended To Detected …
brightprogrammer Apr 27, 2025
b3dd702
Toplevel Template Parameters Reset
brightprogrammer Apr 27, 2025
1b1cfd5
Apply @wargio's suggestions
brightprogrammer Apr 27, 2025
65b71c2
Template Params Are Always Added, Without Maintaining Uniqueness
brightprogrammer Apr 27, 2025
076970e
Array Type Parsing
brightprogrammer Apr 27, 2025
4c2258f
Correct Rule Use
brightprogrammer Apr 28, 2025
d2914ad
save_work
brightprogrammer May 15, 2025
21bdf09
To GNF
brightprogrammer May 15, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ libdemangle_c_args = []
libdemangle_src = [
'src' / 'borland.c',
'src' / 'cxx.c',

'src' / 'cp' / 'demangle.c',
'src' / 'cp' / 'common.c',
'src' / 'cp' / 'param.c',
'src' / 'cp' / 'v2.c',
'src' / 'cp' / 'v3.c',

'src' / 'demangler.c',
'src' / 'demangler_util.c',
'src' / 'java.c',
Expand Down
38 changes: 38 additions & 0 deletions src/cp/common.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "cp/common.h"

CpDem* cpdem_init (CpDem* dem, const char* mangled, CpDemOptions opts) {
if (!dem || !mangled) {
return NULL;
}

memset (dem, 0, sizeof (CpDem));
dem->original =
((StrIter) {.beg = mangled, .end = mangled + strlen (mangled) + 1, .cur = mangled});
dem->opts = opts;
dem_string_init (&dem->base_name);
dem_string_init (&dem->suffix);
dem_string_init (&dem->prefix);
param_vec_init (&dem->func_params);
return dem;
}

CpDem* cpdem_deinit (CpDem* dem) {
if (!dem) {
return NULL;
}

/* free all demstring and deinit qualifiers vector */
vec_foreach_ptr (&dem->qualifiers, q, { dem_string_deinit (q); });
vec_deinit (&dem->qualifiers);

// deinit all func params first
param_vec_deinit (&dem->func_params);

dem_string_deinit (&dem->base_name);
dem_string_deinit (&dem->prefix);
dem_string_deinit (&dem->suffix);
dem_string_deinit (&dem->custom_operator);

memset (dem, 0, sizeof (CpDem));
return dem;
}
156 changes: 156 additions & 0 deletions src/cp/common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
// SPDX-FileCopyrightText: 2025 RizinOrg <info@rizin.re>
// SPDX-FileCopyrightText: 2025 Siddharth Mishra <admin@brightprogrammer.in>
// SPDX-License-Identifier: LGPL-3.0-only

#ifndef CPDEM_COMMON_H
#define CPDEM_COMMON_H

#include "cp/demangle.h"
#include "cp/param.h"
#include "cp/vec.h"
#include "demangler_util.h"

/**
* \b String iterator
**/
typedef struct StrIter {
const char* beg; /**< \b Beginning position of string. */
const char* end; /**< \b Ending of string (usually points to the null-terminator char). */
const char* cur; /**< \b Current read position. */
} StrIter;

typedef Vec (DemString) ClassNameVec;

typedef struct {
StrIter original;
CpDemOptions opts; // TODO: use options to simplify output while demangling

ClassNameVec qualifiers;
DemString base_name; // Used to identify base type in list of function params
ParamVec func_params; // Names of all function params
bool has_params; // There are cases where control never reaches `cpdem_func_params`

DemString suffix; // anything that is to be put at the very end of demangled output
DemString prefix; // a return type, or another keyword to be put before name
DemString custom_operator; // A special case of operator "__op<L><TYPE>"

bool has_global_name; /* __vt or _GLOBAL$I/D$, etc... */
bool is_ctor;
bool is_dtor;
ut8 operator_type; // 0 if not an operator, otherwise a positive value
} CpDem;

CpDem* cpdem_init (CpDem* dem, const char* mangled, CpDemOptions opts);
CpDem* cpdem_deinit (CpDem* dem);

/**
* \b Give current read position.
*
* \return const char pointer to current read position.
*/
#define CUR() (dem->original.cur)

/**
* \b Give position where string begins.
*
* \return const char pointer to beginning of mangled string.
*/
#define BEG() (dem->original.beg)

/**
* \b Give position of NULL terminator.
*
* \return const char pointer to end of mangled string.
*/
#define END() (dem->original.end)

/**
* \b Check whether the provided position is in range of readable address.
*
* \p read_pos : char pointer to check for range.
*
* \return 1 if in range.
* \return 0 otherwise.
*/
#define IN_RANGE(read_pos) ((read_pos) >= BEG() ? ((read_pos) < END() ? 1 : 0) : 0)

/**
* \b Seek to given read position if it's in range. This will change the current
* read position to given target_read_pos.
*
* \p target_read_pos : char pointer specifying the target read position to seek to.
*
* \return target_read_pos on success.
* \return CUR() otherwise.
*/
#define SEEK_TO(target_read_pos) \
(dem->original.cur = IN_RANGE (target_read_pos) ? (target_read_pos) : CUR())

/**
* Peek one character from current read position in demangling context.
* This will NOT advance, unlike READ().
*
* \return char on success.
* \return 0 if no more characters left
*/
#define PEEK() (IN_RANGE (CUR()) ? *dem->original.cur : 0)

/**
* \b Read one character from current read position in demangling context
* and then advance by one position.
*
* \return char on success.
* \return 0 if no more characters left
*/
#define READ(ch) (IN_RANGE (CUR()) ? ((*dem->original.cur == ch) ? (ADV(), 1) : 0) : 0)

/**
* \b Advance current read position by one character, if this next
* position is in range, otherwise stay at current read position.
*
* \return updated read position on success.
* \return NULL otherwise.
*/
#define ADV() (IN_RANGE (CUR() + 1) ? dem->original.cur++ : NULL)

/**
* \b Advance current read position by "n" characters, if this next
* position is in range, otherwise stay at current read position.
*
* \return updated read position on success.
* \return NULL otherwise.
*/
#define ADV_BY(n) (IN_RANGE (CUR() + n) ? (dem->original.cur = dem->original.cur + (n)) : NULL)

/**
* \b Save current read position in demangling context to restore it later.
* This is used when we know that while matching a rule we might fail, and we'll
* need to backtrack. For this we must remember the initial trial start pos.
*/
#define SAVE_POS() const char* _____trial_start_pos = CUR();

/**
* \b Restore saved position
*/
#define RESTORE_POS() \
do { \
SEEK_TO (_____trial_start_pos); \
} while (0)

/**
* Reads a number from current demangling position to provided "var" variable.
* Automatically will adjust next read position if numbe read is successful, otherwise, will
* set var to -1
*/
#define READ_NUMBER(var) \
do { \
char* end = NULL; \
(var) = strtoll (CUR(), &end, 10); \
if (!end) { \
(var) = -1; \
break; \
} \
SEEK_TO (end); \
} while (0)

#endif // CPDEM_COMMON_H
Loading