[PATCH] dhcpc: refactor xmalloc_optname_optval to shrink binary size

Martin Lewis martin.lewis.x84 at gmail.com
Tue Jun 9 21:59:54 UTC 2020


function                                             old     new   delta
len_of_option_as_string                               14      13      -1
dhcp_option_lengths                                   14      13      -1
udhcp_str2optset                                     717     715      -2
.rodata                                           159869  159867      -2
static.xmalloc_optname_optval                        959     828    -131
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/5 up/down: 0/-137)           Total: -137 bytes
   text	   data	    bss	    dec	    hex	filename
 993252	  16923	   1872	1012047	  f714f	busybox_old
 993117	  16923	   1872	1011912	  f70c8	busybox_unstripped

Signed-off-by: Martin Lewis <martin.lewis.x84 at gmail.com>
---
 networking/udhcp/common.h |  2 +-
 networking/udhcp/dhcpc.c  | 47 ++++++++++++++++-------------------------------
 2 files changed, 17 insertions(+), 32 deletions(-)

diff --git a/networking/udhcp/common.h b/networking/udhcp/common.h
index 60255eefa..0640c1241 100644
--- a/networking/udhcp/common.h
+++ b/networking/udhcp/common.h
@@ -78,7 +78,7 @@ struct BUG_bad_sizeof_struct_ip_udp_dhcp_packet {
 /*** Options ***/
 
 enum {
-	OPTION_IP = 1,
+	OPTION_IP = 0,
 	OPTION_IP_PAIR,
 	OPTION_STRING,
 	/* Opts of STRING_HOST type will be sanitized before they are passed
diff --git a/networking/udhcp/dhcpc.c b/networking/udhcp/dhcpc.c
index 6422181da..5cff53d60 100644
--- a/networking/udhcp/dhcpc.c
+++ b/networking/udhcp/dhcpc.c
@@ -208,9 +208,8 @@ static NOINLINE char *xmalloc_optname_optval(uint8_t *option, const struct dhcp_
 		case OPTION_IP:
 		case OPTION_IP_PAIR:
 			dest += sprint_nip(dest, "", option);
-			if (type == OPTION_IP)
-				break;
-			dest += sprint_nip(dest, "/", option + 4);
+			if (type == OPTION_IP_PAIR)
+				dest += sprint_nip(dest, "/", option + 4);
 			break;
 //		case OPTION_BOOLEAN:
 //			dest += sprintf(dest, *option ? "yes" : "no");
@@ -262,7 +261,6 @@ static NOINLINE char *xmalloc_optname_optval(uint8_t *option, const struct dhcp_
 
 			while (len >= 1 + 4) { /* mask + 0-byte ip + router */
 				uint32_t nip;
-				uint8_t *p;
 				unsigned mask;
 				int bytes;
 
@@ -272,12 +270,12 @@ static NOINLINE char *xmalloc_optname_optval(uint8_t *option, const struct dhcp_
 				len--;
 
 				nip = 0;
-				p = (void*) &nip;
 				bytes = (mask + 7) / 8; /* 0 -> 0, 1..8 -> 1, 9..16 -> 2 etc */
-				while (--bytes >= 0) {
-					*p++ = *option++;
-					len--;
-				}
+
+				memcpy(&nip, option, bytes);
+				len -= bytes;
+				option += bytes;
+
 				if (len < 4)
 					break;
 
@@ -326,17 +324,11 @@ static NOINLINE char *xmalloc_optname_optval(uint8_t *option, const struct dhcp_
 				/* 6rdPrefix */
 				dest += sprint_nip6(dest, /* "", */ option);
 				option += 16;
-				len -= 1 + 1 + 16 + 4;
-				/* "+ 4" above corresponds to the length of IPv4 addr
-				 * we consume in the loop below */
-				while (1) {
-					/* 6rdBRIPv4Address(es) */
-					dest += sprint_nip(dest, " ", option);
-					option += 4;
-					len -= 4; /* do we have yet another 4+ bytes? */
-					if (len < 0)
-						break; /* no */
-				}
+				len -= 1 + 1 + 16;
+				*dest++ = ' ';
+				type = OPTION_IP;
+				optlen = 4;
+				continue;
 			}
 
 			return ret;
@@ -364,17 +356,10 @@ static NOINLINE char *xmalloc_optname_optval(uint8_t *option, const struct dhcp_
 					free(ret);
 					return dest;
 				}
-			} else
-			if (option[-1] == 1) {
-				const char *pfx = "";
-				while (1) {
-					len -= 4;
-					if (len < 0)
-						break;
-					dest += sprint_nip(dest, pfx, option);
-					pfx = " ";
-					option += 4;
-				}
+			} else if (option[-1] == 1) {
+				type = OPTION_IP;
+				optlen = 4;
+				continue;
 			}
 			return ret;
 #endif
-- 
2.11.0



More information about the busybox mailing list