aboutsummaryrefslogtreecommitdiff
path: root/libbb/appletlib.c
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2018-11-02 14:14:31 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2018-11-02 14:14:51 +0100
commitc339c7f7b393fbcd51b0f96df837baa1edad7fd8 (patch)
tree3fa49732a3426e0666ba732da7e4182db7dc66ca /libbb/appletlib.c
parent0df289f427da6279e3ca198d14e90015c079af44 (diff)
downloadbusybox-w32-c339c7f7b393fbcd51b0f96df837baa1edad7fd8.tar.gz
busybox-w32-c339c7f7b393fbcd51b0f96df837baa1edad7fd8.tar.bz2
busybox-w32-c339c7f7b393fbcd51b0f96df837baa1edad7fd8.zip
libarchive: add a function to unpack embedded data
Similar code to unpack embedded data is used to decompress usage messages, embedded scripts and the config file (in the non-default bbconfig applet). Moving this code to a common function reduces the size of the default build and hides more of the internals of libarchive. function old new delta unpack_bz2_data - 135 +135 bb_show_usage 137 157 +20 get_script_content 32 47 +15 unpack_scripts 119 - -119 unpack_usage_messages 124 - -124 ------------------------------------------------------------------------------ (add/remove: 1/2 grow/shrink: 2/0 up/down: 170/-243) Total: -73 bytes Signed-off-by: Ron Yorston <rmy@pobox.com> Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'libbb/appletlib.c')
-rw-r--r--libbb/appletlib.c81
1 files changed, 15 insertions, 66 deletions
diff --git a/libbb/appletlib.c b/libbb/appletlib.c
index d48b2ea60..6dfaf1f41 100644
--- a/libbb/appletlib.c
+++ b/libbb/appletlib.c
@@ -107,34 +107,8 @@ static const char usage_messages[] ALIGN1 = UNPACKED_USAGE;
107 107
108static const char packed_usage[] ALIGN1 = { PACKED_USAGE }; 108static const char packed_usage[] ALIGN1 = { PACKED_USAGE };
109# include "bb_archive.h" 109# include "bb_archive.h"
110static const char *unpack_usage_messages(void) 110# define unpack_usage_messages() \
111{ 111 unpack_bz2_data(packed_usage, sizeof(packed_usage), sizeof(UNPACKED_USAGE))
112 char *outbuf = NULL;
113 bunzip_data *bd;
114 int i;
115 jmp_buf jmpbuf;
116
117 /* Setup for I/O error handling via longjmp */
118 i = setjmp(jmpbuf);
119 if (i == 0) {
120 i = start_bunzip(&jmpbuf,
121 &bd,
122 /* src_fd: */ -1,
123 /* inbuf: */ packed_usage,
124 /* len: */ sizeof(packed_usage)
125 );
126 }
127 /* read_bunzip can longjmp and end up here with i != 0
128 * on read data errors! Not trivial */
129 if (i == 0) {
130 /* Cannot use xmalloc: will leak bd in NOFORK case! */
131 outbuf = malloc_or_warn(sizeof(UNPACKED_USAGE));
132 if (outbuf)
133 read_bunzip(bd, outbuf, sizeof(UNPACKED_USAGE));
134 }
135 dealloc_bunzip(bd);
136 return outbuf;
137}
138# define dealloc_usage_messages(s) free(s) 112# define dealloc_usage_messages(s) free(s)
139 113
140#else 114#else
@@ -152,21 +126,23 @@ void FAST_FUNC bb_show_usage(void)
152 /* Imagine that this applet is "true". Dont suck in printf! */ 126 /* Imagine that this applet is "true". Dont suck in printf! */
153 const char *usage_string = unpack_usage_messages(); 127 const char *usage_string = unpack_usage_messages();
154 128
155 if (*usage_string == '\b') { 129 if (usage_string) {
156 full_write2_str("No help available.\n\n"); 130 if (*usage_string == '\b') {
157 } else { 131 full_write2_str("No help available.\n\n");
158 full_write2_str("Usage: "SINGLE_APPLET_STR" "); 132 } else {
159 full_write2_str(usage_string); 133 full_write2_str("Usage: "SINGLE_APPLET_STR" ");
160 full_write2_str("\n\n"); 134 full_write2_str(usage_string);
135 full_write2_str("\n\n");
136 }
137 if (ENABLE_FEATURE_CLEAN_UP)
138 dealloc_usage_messages((char*)usage_string);
161 } 139 }
162 if (ENABLE_FEATURE_CLEAN_UP)
163 dealloc_usage_messages((char*)usage_string);
164#else 140#else
165 const char *p; 141 const char *p;
166 const char *usage_string = p = unpack_usage_messages(); 142 const char *usage_string = p = unpack_usage_messages();
167 int ap = find_applet_by_name(applet_name); 143 int ap = find_applet_by_name(applet_name);
168 144
169 if (ap < 0) /* never happens, paranoia */ 145 if (ap < 0 || usage_string == NULL)
170 xfunc_die(); 146 xfunc_die();
171 while (ap) { 147 while (ap) {
172 while (*p++) continue; 148 while (*p++) continue;
@@ -986,38 +962,11 @@ find_script_by_name(const char *name)
986 return -0x10000; /* make it so that NUM_APPLETS + <error> is still < 0 */ 962 return -0x10000; /* make it so that NUM_APPLETS + <error> is still < 0 */
987} 963}
988 964
989static char *
990unpack_scripts(void)
991{
992 char *outbuf = NULL;
993 bunzip_data *bd;
994 int i;
995 jmp_buf jmpbuf;
996
997 /* Setup for I/O error handling via longjmp */
998 i = setjmp(jmpbuf);
999 if (i == 0) {
1000 i = start_bunzip(&jmpbuf,
1001 &bd,
1002 /* src_fd: */ -1,
1003 /* inbuf: */ packed_scripts,
1004 /* len: */ sizeof(packed_scripts)
1005 );
1006 }
1007 /* read_bunzip can longjmp and end up here with i != 0
1008 * on read data errors! Not trivial */
1009 if (i == 0) {
1010 outbuf = xmalloc(UNPACKED_SCRIPTS_LENGTH);
1011 read_bunzip(bd, outbuf, UNPACKED_SCRIPTS_LENGTH);
1012 }
1013 dealloc_bunzip(bd);
1014 return outbuf;
1015}
1016
1017char* FAST_FUNC 965char* FAST_FUNC
1018get_script_content(unsigned n) 966get_script_content(unsigned n)
1019{ 967{
1020 char *t = unpack_scripts(); 968 char *t = unpack_bz2_data(packed_scripts, sizeof(packed_scripts),
969 UNPACKED_SCRIPTS_LENGTH);
1021 if (t) { 970 if (t) {
1022 while (n != 0) { 971 while (n != 0) {
1023 while (*t++ != '\0') 972 while (*t++ != '\0')