From 7d652fa42b598c65e7486f72514f3ae2c8401ac3 Mon Sep 17 00:00:00 2001 From: "Markus F.X.J. Oberhumer" Date: Wed, 20 Dec 2000 19:54:30 +0000 Subject: [PATCH] Increased precision of get_ratio() and swapped parameters. committer: mfx 977342070 +0000 --- src/ui.cpp | 28 ++++++++++++++-------------- src/util.cpp | 26 ++++++++++++-------------- src/util.h | 3 +-- 3 files changed, 27 insertions(+), 30 deletions(-) diff --git a/src/ui.cpp b/src/ui.cpp index 8e2d9541..0bf8be4c 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -117,20 +117,15 @@ static const char *mkline(unsigned long fu_len, unsigned long fc_len, char r[7+1]; const char *f; -#if 0 - unsigned ratio = get_ratio(fc_len,fu_len); - upx_snprintf(r,sizeof(r)," %1d.%03d", ratio / 10000, (ratio % 10000) / 10); -#else - unsigned ratio = get_ratio(fc_len,fu_len, 1000); - upx_snprintf(r,sizeof(r),"%3d.%02d%%", ratio / 1000, (ratio % 1000) / 10); - UNUSED(u_len); UNUSED(c_len); -#endif + unsigned ratio = get_ratio(fu_len, fc_len); + upx_snprintf(r,sizeof(r),"%3d.%02d%%", ratio / 10000, (ratio % 10000) / 100); if (decompress) f = "%10ld <-%10ld %7s %13s %s"; else f = "%10ld ->%10ld %7s %13s %s"; upx_snprintf(buf,sizeof(buf),f, fu_len, fc_len, r, center_string(format_name,13), filename); + UNUSED(u_len); UNUSED(c_len); return buf; } @@ -351,7 +346,7 @@ void __UPX_ENTRY UiPacker::callback(upx_uint isize, upx_uint osize, int state, v void UiPacker::doCallback(unsigned isize, unsigned osize) { - static const char spinner[] = "|\\-/"; + static const char spinner[] = "|/-\\"; if (s->pass < 0) // no callback wanted return; @@ -373,8 +368,13 @@ void UiPacker::doCallback(unsigned isize, unsigned osize) // compute progress position int pos = -1; - if (isize > 0) - pos = get_ratio(isize,s->u_len) * s->bar_len / 10000; + if (isize >= s->u_len) + pos = s->bar_len; + else if (isize > 0) + { + pos = get_ratio(s->u_len, isize) * s->bar_len / 1000000; + assert(pos >= 0); assert(pos <= s->bar_len); + } if (pos < s->pos) return; if (pos < 0 && pos == s->pos) @@ -397,12 +397,12 @@ void UiPacker::doCallback(unsigned isize, unsigned osize) *m++ = ']'; // compute current compression ratio - unsigned ratio = 100*100; + unsigned ratio = 1000000; if (osize > 0) - ratio = get_ratio(osize,isize); + ratio = get_ratio(isize, osize); sprintf(m," %3d.%1d%% %c ", - ratio / 100, (ratio % 100) / 10, + ratio / 10000, (ratio % 10000) / 1000, spinner[s->counter]); assert((int)strlen(s->msg_buf) < 1 + 80); diff --git a/src/util.cpp b/src/util.cpp index bbf877dd..e1d355cb 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -443,23 +443,21 @@ bool isafile(int fd) /************************************************************************* -// +// return compression ratio, where 100% == 1000*1000 == 1e6 **************************************************************************/ -unsigned get_ratio (unsigned long packedsize, unsigned long size, - unsigned long scale) +unsigned get_ratio (unsigned u_len, unsigned c_len) { - unsigned long n1, n2; - - n1 = 100 * scale; n2 = 1; - if (size <= 0) - return (unsigned) n1; - while (n1 > 1 && packedsize > (ULONG_MAX / n1)) - { - n1 /= 10; - n2 *= 10; - } - return (unsigned) ((packedsize * n1) / (size / n2)) + 5; +#if defined(__GNUC__) + const unsigned long long n = 1000000; +#elif defined(_MSC_VER) + const unsigned __int64 n = 1000000; +#else +#error +#endif + if (u_len <= 0) + return (unsigned) n; + return (unsigned) ((c_len * n) / u_len) + 5; } diff --git a/src/util.h b/src/util.h index ee214f06..836ca224 100644 --- a/src/util.h +++ b/src/util.h @@ -48,8 +48,7 @@ bool maketempname(char *ofilename, const char *ifilename, bool makebakname(char *ofilename, const char *ifilename, bool force=true); bool isafile(int fd); -unsigned get_ratio(unsigned long packedsize, unsigned long size, - unsigned long scale=100); +unsigned get_ratio(unsigned u_len, unsigned c_len); char *center_string(const char *name, size_t s);