Skip to content

Commit 82eb655

Browse files
committed
Fixes for r2-indent
* Supports r2pm's uncrustify without having to r2pm -r * Add r2-indent -a to run indent-whitelist.sh * Add --fix option in whitelist script
1 parent d49fbcb commit 82eb655

File tree

4 files changed

+103
-69
lines changed

4 files changed

+103
-69
lines changed

doc/uncrustify.cfg

+8-7
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ indent_with_tabs = 1
3535
indent_cmt_with_tabs = false
3636
indent_align_string = false
3737
indent_xml_string = 0
38+
indent_ternary_operator = 2
3839
indent_brace = 0
3940
indent_braces = false
4041
indent_braces_no_func = false
@@ -246,13 +247,13 @@ sp_before_oc_block_caret = ignore
246247
sp_after_oc_block_caret = ignore
247248
sp_after_oc_msg_receiver = ignore
248249
sp_after_oc_property = ignore
249-
sp_cond_colon = ignore
250-
sp_cond_colon_before = ignore
251-
sp_cond_colon_after = ignore
252-
sp_cond_question = ignore
253-
sp_cond_question_before = ignore
254-
sp_cond_question_after = ignore
255-
sp_cond_ternary_short = ignore
250+
sp_cond_colon = remove
251+
sp_cond_colon_before = remove
252+
sp_cond_colon_after = force
253+
sp_cond_question = force
254+
sp_cond_question_before = remove
255+
sp_cond_question_after = force
256+
sp_cond_ternary_short = remove
256257
sp_case_label = remove
257258
sp_range = ignore
258259
sp_after_for_colon = force

libr/util/mem.c

+62-51
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ R_API int r_mem_cmp_mask(const ut8 *dest, const ut8 *orig, const ut8 *mask, int
4747
return ret;
4848
}
4949
for (i = 0; i < len; i++) {
50-
mdest[i] = dest[i]&mask[i];
51-
morig[i] = orig[i]&mask[i];
50+
mdest[i] = dest[i] & mask[i];
51+
morig[i] = orig[i] & mask[i];
5252
}
5353
ret = memcmp (mdest, morig, len);
5454
free (mdest);
@@ -58,7 +58,7 @@ R_API int r_mem_cmp_mask(const ut8 *dest, const ut8 *orig, const ut8 *mask, int
5858

5959
R_API void r_mem_copybits(ut8 *dst, const ut8 *src, int bits) {
6060
ut8 srcmask, dstmask;
61-
int bytes = (int)(bits / 8);
61+
int bytes = (int) (bits / 8);
6262
bits = bits % 8;
6363
memcpy (dst, src, bytes);
6464
if (bits) {
@@ -81,40 +81,42 @@ R_API void r_mem_copybits_delta(ut8 *dst, int doff, const ut8 *src, int soff, in
8181
int nbits = bits;
8282
#if 0
8383
int dofb, sofb;
84-
int bdoff = (doff/8);
85-
int bsoff = (soff/8);
84+
int bdoff = (doff / 8);
85+
int bsoff = (soff / 8);
8686
int nbits = 0;
8787
ut8 mask;
88-
int sdelta = soff-doff;
88+
int sdelta = soff - doff;
8989
/* apply delta offsets */
90-
src = src+bsoff;
91-
dst = dst+bdoff;
92-
dofb=doff%8;
93-
sofb=soff%8;
94-
if (sofb||dofb) {
90+
src = src + bsoff;
91+
dst = dst + bdoff;
92+
dofb = doff % 8;
93+
sofb = soff % 8;
94+
if (sofb || dofb) {
9595
// TODO : this algorithm is not implemented
96-
int mask = (1<<sofb);
97-
int nmask = 0xff^mask;
98-
int s = src[0]<<sofb;
99-
int d = dst[0]<<dofb;
100-
if (soff == doff && bits==1) {
101-
mask = 0xff^(1<<dofb);
102-
dst[0] = ((src[0]&mask) | (dst[0]&mask));
103-
} else printf("TODO: Oops. not supported method of bitcopy\n");
96+
int mask = (1 << sofb);
97+
int nmask = 0xff ^ mask;
98+
int s = src[0] << sofb;
99+
int d = dst[0] << dofb;
100+
if (soff == doff && bits == 1) {
101+
mask = 0xff ^ (1 << dofb);
102+
dst[0] = ((src[0] & mask) | (dst[0] & mask));
103+
} else {
104+
printf ("TODO: Oops. not supported method of bitcopy\n");
105+
}
104106
/*
105-
1) shift algin src i dst
106-
2) copy (8-dofb) bits from dst to src
107-
3) dst[0] = dst[0]&^(0x1<<nbits) | (src&(1<<nbits))
108-
*/
107+
1) shift algin src i dst
108+
2) copy (8-dofb) bits from dst to src
109+
3) dst[0] = dst[0]&^(0x1<<nbits) | (src&(1<<nbits))
110+
*/
109111
src++;
110112
dst++;
111113
}
112114
/*
113-
doff v
114-
dst |__________|___________|
115-
soff v
116-
src |__________|_________|
117-
*/
115+
doff v
116+
dst |__________|___________|
117+
soff v
118+
src |__________|_________|
119+
*/
118120
#endif
119121
r_mem_copybits (dst, src, nbits);
120122
}
@@ -135,17 +137,17 @@ R_API ut64 r_mem_get_num(const ut8 *b, int size) {
135137
}
136138

137139
// TODO: SEE: R_API ut64 r_reg_get_value(RReg *reg, RRegItem *item) { .. dupped code?
138-
R_API int r_mem_set_num (ut8 *dest, int dest_size, ut64 num) {
140+
R_API int r_mem_set_num(ut8 *dest, int dest_size, ut64 num) {
139141
// LITTLE ENDIAN is the default for streams
140142
switch (dest_size) {
141143
case 1:
142-
r_write_le8 (dest, (ut8)(num & UT8_MAX));
144+
r_write_le8 (dest, (ut8) (num & UT8_MAX));
143145
break;
144146
case 2:
145-
r_write_le16 (dest, (ut16)(num & UT16_MAX));
147+
r_write_le16 (dest, (ut16) (num & UT16_MAX));
146148
break;
147149
case 4:
148-
r_write_le32 (dest, (ut32)(num & UT32_MAX));
150+
r_write_le32 (dest, (ut32) (num & UT32_MAX));
149151
break;
150152
case 8:
151153
r_write_le64 (dest, num);
@@ -159,7 +161,7 @@ R_API int r_mem_set_num (ut8 *dest, int dest_size, ut64 num) {
159161
// The default endian is LE for streams.
160162
// This function either swaps or copies len bytes depending on bool big_endian
161163
// TODO: Remove completely
162-
R_API void r_mem_swaporcopy(ut8* dest, const ut8* src, int len, bool big_endian) {
164+
R_API void r_mem_swaporcopy(ut8 *dest, const ut8 *src, int len, bool big_endian) {
163165
if (big_endian) {
164166
r_mem_swapendian (dest, src, len);
165167
} else {
@@ -211,15 +213,17 @@ R_API void r_mem_swapendian(ut8 *dest, const ut8 *orig, int size) {
211213
}
212214
}
213215

214-
//R_DOC r_mem_mem: Finds the needle of nlen size into the haystack of hlen size
215-
//R_UNIT printf("%s\n", r_mem_mem("food is pure lame", 20, "is", 2));
216+
// R_DOC r_mem_mem: Finds the needle of nlen size into the haystack of hlen size
217+
// R_UNIT printf("%s\n", r_mem_mem("food is pure lame", 20, "is", 2));
216218
R_API const ut8 *r_mem_mem(const ut8 *haystack, int hlen, const ut8 *needle, int nlen) {
217-
int i, until = hlen-nlen+1;
218-
if (hlen<1 || nlen<1)
219+
int i, until = hlen - nlen + 1;
220+
if (hlen < 1 || nlen < 1) {
219221
return NULL;
220-
for (i=0; i<until; i++) {
221-
if (!memcmp (haystack+i, needle, nlen))
222-
return haystack+i;
222+
}
223+
for (i = 0; i < until; i++) {
224+
if (!memcmp (haystack + i, needle, nlen)) {
225+
return haystack + i;
226+
}
223227
}
224228
return NULL;
225229
}
@@ -237,8 +241,8 @@ R_API const ut8 *r_mem_mem_aligned(const ut8 *haystack, int hlen, const ut8 *nee
237241
until -= (until % align);
238242
}
239243
for (i = 0; i < until; i += align) {
240-
if (!memcmp (haystack+i, needle, nlen)) {
241-
return haystack+i;
244+
if (!memcmp (haystack + i, needle, nlen)) {
245+
return haystack + i;
242246
}
243247
}
244248
return NULL;
@@ -247,17 +251,24 @@ R_API const ut8 *r_mem_mem_aligned(const ut8 *haystack, int hlen, const ut8 *nee
247251
R_API int r_mem_protect(void *ptr, int size, const char *prot) {
248252
#if __UNIX__
249253
int p = 0;
250-
if (strchr (prot, 'x')) p |= PROT_EXEC;
251-
if (strchr (prot, 'r')) p |= PROT_READ;
252-
if (strchr (prot, 'w')) p |= PROT_WRITE;
253-
if (mprotect (ptr, size, p)==-1)
254+
if (strchr (prot, 'x')) {
255+
p |= PROT_EXEC;
256+
}
257+
if (strchr (prot, 'r')) {
258+
p |= PROT_READ;
259+
}
260+
if (strchr (prot, 'w')) {
261+
p |= PROT_WRITE;
262+
}
263+
if (mprotect (ptr, size, p) == -1) {
254264
return false;
265+
}
255266
#elif __WINDOWS__ || __CYGWIN__
256267
int r, w, x;
257268
DWORD p = PAGE_NOACCESS;
258269
r = strchr (prot, 'r')? 1: 0;
259270
w = strchr (prot, 'w')? 1: 0;
260-
x = strchr (prot, 'x')? 1: 0;;
271+
x = strchr (prot, 'x')? 1: 0;
261272
if (w && x) {
262273
return false;
263274
}
@@ -272,12 +283,12 @@ R_API int r_mem_protect(void *ptr, int size, const char *prot) {
272283
return false;
273284
}
274285
#else
275-
#warning Unknown platform
286+
#warning Unknown platform
276287
#endif
277288
return true;
278289
}
279290

280-
R_API void *r_mem_dup (void *s, int l) {
291+
R_API void *r_mem_dup(void *s, int l) {
281292
void *d = malloc (l);
282293
if (!d) {
283294
return NULL;
@@ -296,9 +307,9 @@ R_API void r_mem_reverse(ut8 *b, int l) {
296307
}
297308
}
298309

299-
R_API bool r_mem_is_printable (const ut8 *a, int la) {
310+
R_API bool r_mem_is_printable(const ut8 *a, int la) {
300311
int i;
301-
for (i = 0; i< la; i++) {
312+
for (i = 0; i < la; i++) {
302313
if (a[i] != '\n' && a[i] != '\t' && !IS_PRINTABLE (a[i])) {
303314
return false;
304315
}

sys/indent-whitelist.sh

+20-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/bin/sh
22
FILES="
3+
libr/util/mem.c
34
libr/util/base64.c
45
libr/util/name.c
56
libr/util/idpool.c
@@ -65,17 +66,27 @@ binr/rabin2/rabin2.c
6566
binr/rasm2/rasm2.c
6667
binr/rax2/rax2.c
6768
"
69+
70+
chk() {
71+
if [ -z "$2" ]; then
72+
return 0
73+
fi
74+
echo "$1" | grep -q "$2"
75+
}
76+
6877
case "$1" in
69-
"help"|-h)
70-
echo "Usage. sys/indent-whitelist.sh [commit] [apply]"
71-
;;
72-
"commit")
73-
sys/indent.sh -i ${FILES}
74-
git commit sys/indent* ${FILES}
78+
help|-h)
79+
echo "Usage. sys/indent-whitelist.sh [--fix] [regex]"
7580
;;
76-
"apply")
77-
sys/indent.sh -i ${FILES}
81+
--fix)
82+
for f in $FILES ; do
83+
chk $f $2 || continue
84+
r2pm -r sys/indent.sh -i $f
85+
done
7886
;;
7987
*)
80-
sys/indent.sh -u ${FILES}
88+
for f in $FILES ; do
89+
chk $f $1 || continue
90+
r2pm -r sys/indent.sh -u $f
91+
done
8192
esac

sys/indent.sh

+13-2
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,33 @@
88

99
IFILE="$1"
1010
P=`readlink $0`
11+
[ -z "$P" ] && P="$0"
1112
cd `dirname $P`/..
1213

1314
if [ -z "${IFILE}" ]; then
1415
echo "Usage: r2-indent [-i|-u] [file] [...]"
16+
echo " -a indent all whitelisted files"
1517
echo " -i indent in place (modify file)"
1618
echo " -u unified diff of the file"
1719
exit 1
1820
fi
1921

2022
CWD="$PWD"
2123
INPLACE=0
24+
ALLWHITE=0
2225
UNIFIED=0
2326
ROOTDIR=/
2427

2528
UNCRUST=1
2629

30+
if [ "${IFILE}" = "-a" ]; then
31+
shift
32+
ALLWHITE=1
33+
IFILE="$1"
34+
$CWD/sys/indent-whitelist.sh $@
35+
exit 0
36+
fi
37+
2738
if [ "${IFILE}" = "-i" ]; then
2839
shift
2940
INPLACE=1
@@ -42,7 +53,7 @@ fi
4253

4354
if [ "${UNCRUST}" = 1 ]; then
4455
# yell, rather than overwrite an innocent file
45-
if ! type uncrustify >/dev/null; then
56+
if ! r2pm -r type uncrustify >/dev/null; then
4657
echo "This script requires uncrustify to function. Check r2pm -i uncrustify"
4758
exit 1
4859
fi
@@ -64,7 +75,7 @@ indentFile() {
6475
if [ "${UNCRUST}" = 1 ]; then
6576
cp -f doc/clang-format ${CWD}/.clang-format
6677
cd "$CWD"
67-
uncrustify -c ${CWD}/doc/uncrustify.cfg -f "${IFILE}" > .tmp-format
78+
r2pm -r uncrustify -c ${CWD}/doc/uncrustify.cfg -f "${IFILE}" > .tmp-format || exit 1
6879
else
6980
cp -f doc/clang-format ${CWD}/.clang-format
7081
cd "$CWD"

0 commit comments

Comments
 (0)