1 // SPDX-License-Identifier: GPL-2.0
8 #include "demangle-java.h"
10 #include <linux/ctype.h>
11 #include <linux/kernel.h>
18 MODE_CTYPE
= 4, /* class arg */
21 #define BASE_ENT(c, n) [c - 'A']=n
22 static const char *base_types
['Z' - 'A' + 1] = {
23 BASE_ENT('B', "byte" ),
24 BASE_ENT('C', "char" ),
25 BASE_ENT('D', "double" ),
26 BASE_ENT('F', "float" ),
27 BASE_ENT('I', "int" ),
28 BASE_ENT('J', "long" ),
29 BASE_ENT('S', "short" ),
30 BASE_ENT('Z', "boolean" ),
34 * demangle Java symbol between str and end positions and stores
35 * up to maxlen characters into buf. The parser starts in mode.
37 * Use MODE_PREFIX to process entire prototype till end position
38 * Use MODE_TYPE to process return type if str starts on return type char
45 __demangle_java_sym(const char *str
, const char *end
, char *buf
, int maxlen
, int mode
)
53 end
= str
+ strlen(str
);
55 for (q
= str
; q
!= end
; q
++) {
57 if (rlen
== (maxlen
- 1))
62 if (mode
== MODE_PREFIX
|| mode
== MODE_TYPE
) {
63 if (mode
== MODE_TYPE
) {
65 rlen
+= scnprintf(buf
+ rlen
, maxlen
- rlen
, ", ");
68 if (mode
== MODE_PREFIX
)
83 if (mode
== MODE_TYPE
) {
85 rlen
+= scnprintf(buf
+ rlen
, maxlen
- rlen
, ", ");
86 rlen
+= scnprintf(buf
+ rlen
, maxlen
- rlen
, "%s", base_types
[*q
- 'A']);
88 rlen
+= scnprintf(buf
+ rlen
, maxlen
- rlen
, "[]");
95 if (mode
== MODE_TYPE
) {
96 rlen
+= scnprintf(buf
+ rlen
, maxlen
- rlen
, "void");
98 rlen
+= scnprintf(buf
+ rlen
, maxlen
- rlen
, "[]");
104 if (mode
!= MODE_TYPE
)
109 if (mode
!= MODE_FUNC
)
115 if (mode
!= MODE_TYPE
)
121 if (mode
!= MODE_CLASS
&& mode
!= MODE_CTYPE
)
123 /* safe because at least one other char to process */
124 if (isalpha(*(q
+ 1)) && mode
== MODE_CLASS
)
125 rlen
+= scnprintf(buf
+ rlen
, maxlen
- rlen
, ".");
126 if (mode
== MODE_CLASS
)
128 else if (mode
== MODE_CTYPE
)
132 if (mode
!= MODE_CLASS
&& mode
!= MODE_CTYPE
)
134 rlen
+= scnprintf(buf
+ rlen
, maxlen
- rlen
, ".");
147 * Demangle Java function signature (openJDK, not GCJ)
149 * str: string to parse. String is not modified
150 * flags: comobination of JAVA_DEMANGLE_* flags to modify demangling
152 * if input can be demangled, then a newly allocated string is returned.
153 * if input cannot be demangled, then NULL is returned
155 * Note: caller is responsible for freeing demangled string
158 java_demangle_sym(const char *str
, int flags
)
167 /* find start of retunr type */
168 p
= strrchr(str
, ')');
173 * expansion factor estimated to 3x
175 len
= strlen(str
) * 3 + 1;
181 if (!(flags
& JAVA_DEMANGLE_NORET
)) {
183 * get return type first
185 ptr
= __demangle_java_sym(p
+ 1, NULL
, buf
, len
, MODE_TYPE
);
189 /* add space between return type and function prototype */
194 /* process function up to return type */
195 ptr
= __demangle_java_sym(str
, p
+ 1, buf
+ l1
, len
- l1
, MODE_PREFIX
);