[git commit master] archival/*: shrink by reusing sufficiently similar functions

Denys Vlasenko vda.linux at googlemail.com
Mon May 31 12:18:57 UTC 2010


commit: http://git.busybox.net/busybox/commit/?id=39a04f71ca8ccf81de2cdbd538df519cf34ef2e6
branch: http://git.busybox.net/busybox/commit/?id=refs/heads/master

function                                             old     new   delta
append_ext                                             -      16     +16
unxz_main                                             77      83      +6
unlzma_main                                           77      83      +6
uncompress_main                                       42      48      +6
gzip_main                                            184     190      +6
bzip2_main                                           114     120      +6
bunzip2_main                                          61      67      +6
bbunpack                                             469     475      +6
send_tree                                            355     360      +5
lzop_main                                             89      92      +3
gunzip_main                                           61      64      +3
make_new_name_lzop                                    84      82      -2
make_new_name_gunzip                                 114     112      -2
make_new_name_unxz                                    14       -     -14
make_new_name_unlzma                                  14       -     -14
make_new_name_uncompress                              14       -     -14
make_new_name_bunzip2                                 14       -     -14
make_new_name_gzip                                    17       -     -17
make_new_name_bzip2                                   17       -     -17
------------------------------------------------------------------------------
(add/remove: 1/6 grow/shrink: 10/2 up/down: 69/-94)           Total: -25 bytes

Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 archival/bbunzip.c  |   56 +++++++++++++++++++-------------------------------
 archival/bzip2.c    |   10 +-------
 archival/gzip.c     |   12 ++--------
 archival/lzop.c     |    6 ++--
 include/unarchive.h |    7 ++++-
 5 files changed, 34 insertions(+), 57 deletions(-)

diff --git a/archival/bbunzip.c b/archival/bbunzip.c
index 86adb6e..08db275 100644
--- a/archival/bbunzip.c
+++ b/archival/bbunzip.c
@@ -27,9 +27,15 @@ int open_to_or_warn(int to_fd, const char *filename, int flags, int mode)
 	return 0;
 }
 
+char* FAST_FUNC append_ext(char *filename, const char *expected_ext)
+{
+	return xasprintf("%s.%s", filename, expected_ext);
+}
+
 int FAST_FUNC bbunpack(char **argv,
-	char* (*make_new_name)(char *filename),
-	IF_DESKTOP(long long) int (*unpacker)(unpack_info_t *info)
+	IF_DESKTOP(long long) int FAST_FUNC (*unpacker)(unpack_info_t *info),
+	char* FAST_FUNC (*make_new_name)(char *filename, const char *expected_ext),
+	const char *expected_ext
 )
 {
 	struct stat stat_buf;
@@ -68,7 +74,7 @@ int FAST_FUNC bbunpack(char **argv,
 
 		/* Open dst if we are going to unpack to file */
 		if (filename) {
-			new_name = make_new_name(filename);
+			new_name = make_new_name(filename, expected_ext);
 			if (!new_name) {
 				bb_error_msg("%s: unknown suffix - ignored", filename);
 				goto err;
@@ -142,7 +148,7 @@ int FAST_FUNC bbunpack(char **argv,
 
 #if ENABLE_UNCOMPRESS || ENABLE_BUNZIP2 || ENABLE_UNLZMA || ENABLE_UNXZ
 static
-char* make_new_name_generic(char *filename, const char *expected_ext)
+char* FAST_FUNC make_new_name_generic(char *filename, const char *expected_ext)
 {
 	char *extension = strrchr(filename, '.');
 	if (!extension || strcmp(extension + 1, expected_ext) != 0) {
@@ -163,12 +169,7 @@ char* make_new_name_generic(char *filename, const char *expected_ext)
  */
 #if ENABLE_UNCOMPRESS
 static
-char* make_new_name_uncompress(char *filename)
-{
-	return make_new_name_generic(filename, "Z");
-}
-static
-IF_DESKTOP(long long) int unpack_uncompress(unpack_info_t *info UNUSED_PARAM)
+IF_DESKTOP(long long) int FAST_FUNC unpack_uncompress(unpack_info_t *info UNUSED_PARAM)
 {
 	IF_DESKTOP(long long) int status = -1;
 
@@ -185,7 +186,7 @@ int uncompress_main(int argc UNUSED_PARAM, char **argv)
 	getopt32(argv, "cf");
 	argv += optind;
 
-	return bbunpack(argv, make_new_name_uncompress, unpack_uncompress);
+	return bbunpack(argv, unpack_uncompress, make_new_name_generic, "Z");
 }
 #endif
 
@@ -219,7 +220,7 @@ int uncompress_main(int argc UNUSED_PARAM, char **argv)
  */
 #if ENABLE_GUNZIP
 static
-char* make_new_name_gunzip(char *filename)
+char* FAST_FUNC make_new_name_gunzip(char *filename, const char *expected_ext UNUSED_PARAM)
 {
 	char *extension = strrchr(filename, '.');
 
@@ -244,7 +245,7 @@ char* make_new_name_gunzip(char *filename)
 	return filename;
 }
 static
-IF_DESKTOP(long long) int unpack_gunzip(unpack_info_t *info)
+IF_DESKTOP(long long) int FAST_FUNC unpack_gunzip(unpack_info_t *info)
 {
 	IF_DESKTOP(long long) int status = -1;
 
@@ -292,7 +293,7 @@ int gunzip_main(int argc UNUSED_PARAM, char **argv)
 	if (applet_name[1] == 'c')
 		option_mask32 |= OPT_STDOUT;
 
-	return bbunpack(argv, make_new_name_gunzip, unpack_gunzip);
+	return bbunpack(argv, unpack_gunzip, make_new_name_gunzip, /*unused:*/ NULL);
 }
 #endif
 
@@ -305,12 +306,7 @@ int gunzip_main(int argc UNUSED_PARAM, char **argv)
  */
 #if ENABLE_BUNZIP2
 static
-char* make_new_name_bunzip2(char *filename)
-{
-	return make_new_name_generic(filename, "bz2");
-}
-static
-IF_DESKTOP(long long) int unpack_bunzip2(unpack_info_t *info UNUSED_PARAM)
+IF_DESKTOP(long long) int FAST_FUNC unpack_bunzip2(unpack_info_t *info UNUSED_PARAM)
 {
 	return unpack_bz2_stream_prime(STDIN_FILENO, STDOUT_FILENO);
 }
@@ -322,7 +318,7 @@ int bunzip2_main(int argc UNUSED_PARAM, char **argv)
 	if (applet_name[2] == 'c') /* bzcat */
 		option_mask32 |= OPT_STDOUT;
 
-	return bbunpack(argv, make_new_name_bunzip2, unpack_bunzip2);
+	return bbunpack(argv, unpack_bunzip2, make_new_name_generic, "bz2");
 }
 #endif
 
@@ -337,12 +333,7 @@ int bunzip2_main(int argc UNUSED_PARAM, char **argv)
  */
 #if ENABLE_UNLZMA
 static
-char* make_new_name_unlzma(char *filename)
-{
-	return make_new_name_generic(filename, "lzma");
-}
-static
-IF_DESKTOP(long long) int unpack_unlzma(unpack_info_t *info UNUSED_PARAM)
+IF_DESKTOP(long long) int FAST_FUNC unpack_unlzma(unpack_info_t *info UNUSED_PARAM)
 {
 	return unpack_lzma_stream(STDIN_FILENO, STDOUT_FILENO);
 }
@@ -360,19 +351,14 @@ int unlzma_main(int argc UNUSED_PARAM, char **argv)
 		option_mask32 |= OPT_STDOUT;
 
 	argv += optind;
-	return bbunpack(argv, make_new_name_unlzma, unpack_unlzma);
+	return bbunpack(argv, unpack_unlzma, make_new_name_generic, "lzma");
 }
 #endif
 
 
 #if ENABLE_UNXZ
 static
-char* make_new_name_unxz(char *filename)
-{
-	return make_new_name_generic(filename, "xz");
-}
-static
-IF_DESKTOP(long long) int unpack_unxz(unpack_info_t *info UNUSED_PARAM)
+IF_DESKTOP(long long) int FAST_FUNC unpack_unxz(unpack_info_t *info UNUSED_PARAM)
 {
 	return unpack_xz_stream(STDIN_FILENO, STDOUT_FILENO);
 }
@@ -390,6 +376,6 @@ int unxz_main(int argc UNUSED_PARAM, char **argv)
 		option_mask32 |= OPT_STDOUT;
 
 	argv += optind;
-	return bbunpack(argv, make_new_name_unxz, unpack_unxz);
+	return bbunpack(argv, unpack_unxz, make_new_name_generic, "xz");
 }
 #endif
diff --git a/archival/bzip2.c b/archival/bzip2.c
index bbaf566..f1c84d6 100644
--- a/archival/bzip2.c
+++ b/archival/bzip2.c
@@ -102,7 +102,7 @@ IF_DESKTOP(long long) int bz_write(bz_stream *strm, void* rbuf, ssize_t rlen, vo
 }
 
 static
-IF_DESKTOP(long long) int compressStream(unpack_info_t *info UNUSED_PARAM)
+IF_DESKTOP(long long) int FAST_FUNC compressStream(unpack_info_t *info UNUSED_PARAM)
 {
 	IF_DESKTOP(long long) int total;
 	ssize_t count;
@@ -135,12 +135,6 @@ IF_DESKTOP(long long) int compressStream(unpack_info_t *info UNUSED_PARAM)
 	return total;
 }
 
-static
-char* make_new_name_bzip2(char *filename)
-{
-	return xasprintf("%s.bz2", filename);
-}
-
 int bzip2_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int bzip2_main(int argc UNUSED_PARAM, char **argv)
 {
@@ -181,5 +175,5 @@ int bzip2_main(int argc UNUSED_PARAM, char **argv)
 
 	argv += optind;
 	option_mask32 &= 0x7; /* ignore all except -cfv */
-	return bbunpack(argv, make_new_name_bzip2, compressStream);
+	return bbunpack(argv, compressStream, append_ext, "bz2");
 }
diff --git a/archival/gzip.c b/archival/gzip.c
index a327d54..5cc553a 100644
--- a/archival/gzip.c
+++ b/archival/gzip.c
@@ -1998,13 +1998,7 @@ static void zip(ulg time_stamp)
 
 /* ======================================================================== */
 static
-char* make_new_name_gzip(char *filename)
-{
-	return xasprintf("%s.gz", filename);
-}
-
-static
-IF_DESKTOP(long long) int pack_gzip(unpack_info_t *info UNUSED_PARAM)
+IF_DESKTOP(long long) int FAST_FUNC pack_gzip(unpack_info_t *info UNUSED_PARAM)
 {
 	struct stat s;
 
@@ -2063,7 +2057,7 @@ static const char gzip_longopts[] ALIGN1 =
 #endif
 
 /*
- * Linux kernel build uses gzip -d -n. We accept and ignore it.
+ * Linux kernel build uses gzip -d -n. We accept and ignore -n.
  * Man page says:
  * -n --no-name
  * gzip: do not save the original file name and time stamp.
@@ -2113,5 +2107,5 @@ int gzip_main(int argc UNUSED_PARAM, char **argv)
 	/* Initialise the CRC32 table */
 	G1.crc_32_tab = crc32_filltable(NULL, 0);
 
-	return bbunpack(argv, make_new_name_gzip, pack_gzip);
+	return bbunpack(argv, pack_gzip, append_ext, "gz");
 }
diff --git a/archival/lzop.c b/archival/lzop.c
index ceace04..ab4d34c 100644
--- a/archival/lzop.c
+++ b/archival/lzop.c
@@ -1042,7 +1042,7 @@ static smallint do_lzo_decompress(void)
 	return lzo_decompress(&header);
 }
 
-static char* make_new_name_lzop(char *filename)
+static char* FAST_FUNC make_new_name_lzop(char *filename, const char *expected_ext UNUSED_PARAM)
 {
 	if (option_mask32 & OPT_DECOMPRESS) {
 		char *extension = strrchr(filename, '.');
@@ -1054,7 +1054,7 @@ static char* make_new_name_lzop(char *filename)
 	return xasprintf("%s.lzo", filename);
 }
 
-static IF_DESKTOP(long long) int pack_lzop(unpack_info_t *info UNUSED_PARAM)
+static IF_DESKTOP(long long) int FAST_FUNC pack_lzop(unpack_info_t *info UNUSED_PARAM)
 {
 	if (option_mask32 & OPT_DECOMPRESS)
 		return do_lzo_decompress();
@@ -1074,5 +1074,5 @@ int lzop_main(int argc UNUSED_PARAM, char **argv)
 		option_mask32 |= OPT_DECOMPRESS;
 
 	G.lzo_crc32_table = crc32_filltable(NULL, 0);
-	return bbunpack(argv, make_new_name_lzop, pack_lzop);
+	return bbunpack(argv, pack_lzop, make_new_name_lzop, /*unused:*/ NULL);
 }
diff --git a/include/unarchive.h b/include/unarchive.h
index 783a943..aa7ecec 100644
--- a/include/unarchive.h
+++ b/include/unarchive.h
@@ -155,9 +155,12 @@ IF_DESKTOP(long long) int unpack_Z_stream(int src_fd, int dst_fd) FAST_FUNC;
 /* wrapper which checks first two bytes to be "BZ" */
 IF_DESKTOP(long long) int unpack_bz2_stream_prime(int src_fd, int dst_fd) FAST_FUNC;
 
+char* append_ext(char *filename, const char *expected_ext) FAST_FUNC;
 int bbunpack(char **argv,
-	     char* (*make_new_name)(char *filename),
-	     IF_DESKTOP(long long) int (*unpacker)(unpack_info_t *info)) FAST_FUNC;
+	    IF_DESKTOP(long long) int FAST_FUNC (*unpacker)(unpack_info_t *info),
+	    char* FAST_FUNC (*make_new_name)(char *filename, const char *expected_ext),
+	    const char *expected_ext
+) FAST_FUNC;
 
 #if BB_MMU
 void open_transformer(int fd,
-- 
1.6.3.3



More information about the busybox-cvs mailing list