11/* radare - LGPL - Copyright 2012-2024 - pancake */
22
3- #include <r_lib.h>
4- #include <r_flag.h>
5- #include <r_anal.h>
63#include <r_asm.h>
74
8- static int replace (int argc , const char * argv [], char * newstr ) {
5+ static char * replace (int argc , const char * argv []) {
96 int i , j , k ;
107 struct {
118 const char * op ;
@@ -183,32 +180,32 @@ static int replace(int argc, const char *argv[], char *newstr) {
183180 { "mul-double" , "1 = 2 * 3" },
184181 { "move-wide" , "1 = 2" },
185182 { "move-wide/16" , "1 = 2" },
183+ { "return-void" , "return" },
186184 { "return-wide" , "return (wide) 1" },
187185 { "return-object" , "return (object) 1" },
188186 // { "sget", "1 = 2[3]" },
189187 { NULL }
190188 };
191189
190+ RStrBuf * sb = r_strbuf_new ("" );
192191 for (i = 0 ; ops [i ].op ; i ++ ) {
193192 if (!strcmp (ops [i ].op , argv [0 ])) {
194- if (newstr ) {
195- for (j = k = 0 ; ops [i ].str [j ] != '\0' ; j ++ , k ++ ) {
196- if (ops [i ].str [j ] >= '1' && ops [i ].str [j ] <= '9' ) {
197- const char * w = argv [ops [i ].str [j ] - '0' ];
198- if (w ) {
199- strcpy (newstr + k , w );
200- k += strlen (w ) - 1 ;
201- }
202- } else {
203- newstr [k ] = ops [i ].str [j ];
193+ for (j = k = 0 ; ops [i ].str [j ] != '\0' ; j ++ , k ++ ) {
194+ if (ops [i ].str [j ] >= '1' && ops [i ].str [j ] <= '9' ) {
195+ const char * w = argv [ops [i ].str [j ] - '0' ];
196+ if (w ) {
197+ r_strbuf_append (sb , w );
204198 }
199+ } else {
200+ char ch = ops [i ].str [j ];
201+ r_strbuf_append_n (sb , & ch , 1 );
205202 }
206- newstr [k ] = '\0' ;
207203 }
208- return true ;
204+ return r_strbuf_drain ( sb ) ;
209205 }
210206 }
211-
207+ r_strbuf_free (sb );
208+ #if 0
212209 /* TODO: this is slow */
213210 if (newstr ) {
214211 newstr [0 ] = '\0' ;
@@ -217,8 +214,8 @@ static int replace(int argc, const char *argv[], char *newstr) {
217214 strcat (newstr , (i == 0 || i == argc - 1 )?" " :", " );
218215 }
219216 }
220-
221- return false ;
217+ #endif
218+ return NULL ;
222219}
223220
224221static char * patch (RAsmPluginSession * aps , RAnalOp * aop , const char * op ) {
@@ -248,7 +245,7 @@ static char *patch(RAsmPluginSession *aps, RAnalOp *aop, const char *op) {
248245 } \
249246 } while (0)
250247
251- static bool parse (RAsmPluginSession * aps , const char * data , char * str ) {
248+ static char * parse (RAsmPluginSession * aps , const char * data ) {
252249 int i , len = strlen (data );
253250 char * buf , * ptr , * optr , * ptr2 ;
254251 char w0 [64 ];
@@ -261,8 +258,7 @@ static bool parse(RAsmPluginSession *aps, const char *data, char *str) {
261258 || !strcmp (data , "???" )
262259 || !strcmp (data , "nop" )
263260 || !strcmp (data , "DEPRECATED" )) {
264- str [0 ] = 0 ;
265- return true;
261+ return strdup ("" );
266262 }
267263
268264 // malloc can be slow here :?
@@ -273,21 +269,20 @@ static bool parse(RAsmPluginSession *aps, const char *data, char *str) {
273269
274270 r_str_trim (buf );
275271
272+ char * str = NULL ;
276273 if (* buf ) {
277- w0 [0 ]= '\0' ;
278- w1 [0 ]= '\0' ;
279- w2 [0 ]= '\0' ;
280- w3 [0 ]= '\0' ;
281- w4 [0 ]= '\0' ;
274+ w0 [0 ] = '\0' ;
275+ w1 [0 ] = '\0' ;
276+ w2 [0 ] = '\0' ;
277+ w3 [0 ] = '\0' ;
278+ w4 [0 ] = '\0' ;
282279 ptr = strchr (buf , ' ' );
283280 if (!ptr ) {
284281 ptr = strchr (buf , '\t' );
285282 }
286283 if (ptr ) {
287284 * ptr = '\0' ;
288- for (ptr ++ ; * ptr == ' ' ; ptr ++ ) {
289- ;
290- }
285+ ptr = (char * )r_str_trim_head_ro (ptr + 1 );
291286 strncpy (w0 , buf , sizeof (w0 ) - 1 );
292287 w0 [sizeof (w0 )- 1 ] = '\0' ;
293288 strncpy (w1 , ptr , sizeof (w1 ) - 1 );
@@ -343,8 +338,8 @@ static bool parse(RAsmPluginSession *aps, const char *data, char *str) {
343338 nw ++ ;
344339 }
345340 }
346- replace (nw , wa , str );
347- {
341+ str = replace (nw , wa );
342+ if ( str ) {
348343 char * p = strdup (str );
349344 p = r_str_replace (p , "+ -" , "- " , 0 );
350345#if EXPERIMENTAL_ZERO
@@ -362,13 +357,12 @@ static bool parse(RAsmPluginSession *aps, const char *data, char *str) {
362357 REPLACE ("%s = %s >>" , "%s >>=" );
363358 REPLACE ("%s = %s <<" , "%s <<=" );
364359 }
365- strcpy (str , p );
366- free (p );
360+ str = p ;
367361 }
368362 }
369363 }
370364 free (buf );
371- return true ;
365+ return str ;
372366}
373367
374368RAsmPlugin r_asm_plugin_dalvik = {
0 commit comments