From 5ef8b53108f41220602febbd2e10f3b76ab32b82 Mon Sep 17 00:00:00 2001 From: Panu Matilainen Date: Mon, 7 Apr 2008 14:04:00 +0300 Subject: [PATCH] Eliminate several copy-paste hex converters, use pgpHexStr() instead --- lib/query.c | 15 +++------------ python/rpmfi-py.c | 21 +++++++-------------- rpmdb/header.c | 3 ++- rpmdb/header_internal.c | 18 ------------------ rpmdb/header_internal.h | 7 ------- rpmdb/rpmdb.c | 2 +- rpmio/digest.c | 11 +---------- 7 files changed, 14 insertions(+), 63 deletions(-) diff --git a/lib/query.c b/lib/query.c index 2ef0a3a..20068af 100644 --- a/lib/query.c +++ b/lib/query.c @@ -186,7 +186,7 @@ int showQueryPackage(QVA_t qva, rpmts ts, Header h) rpmfileState fstate; rpm_off_t fsize; const char * fn; - char fmd5[32+1]; + char *fmd5; const char * fuser; const char * fgroup; const char * flink; @@ -199,17 +199,7 @@ int showQueryPackage(QVA_t qva, rpmts ts, Header h) fstate = rpmfiFState(fi); fsize = rpmfiFSize(fi); fn = rpmfiFN(fi); - { static char const hex[] = "0123456789abcdef"; - unsigned const char * s = rpmfiMD5(fi); - char * p = fmd5; - int j; - for (j = 0; j < 16; j++) { - unsigned k = *s++; - *p++ = hex[ (k >> 4) & 0xf ]; - *p++ = hex[ (k ) & 0xf ]; - } - *p = '\0'; - } + fmd5 = pgpHexStr(rpmfiMD5(fi), rpmDigestLength(PGPHASHALGO_MD5)); fuser = rpmfiFUser(fi); fgroup = rpmfiFGroup(fi); flink = rpmfiFLink(fi); @@ -308,6 +298,7 @@ int showQueryPackage(QVA_t qva, rpmts ts, Header h) } } flushBuffer(&t, &te, 0); + free(fmd5); } rc = 0; diff --git a/python/rpmfi-py.c b/python/rpmfi-py.c index 15c6d30..8f870e4 100644 --- a/python/rpmfi-py.c +++ b/python/rpmfi-py.c @@ -5,6 +5,7 @@ #include "system.h" #include +#include #include "header-py.h" #include "rpmfi-py.h" @@ -194,19 +195,11 @@ rpmfi_iternext(rpmfiObject * s) int VFlags = rpmfiVFlags(s->fi); const char * FUser = rpmfiFUser(s->fi); const char * FGroup = rpmfiFGroup(s->fi); - const unsigned char * MD5 = rpmfiMD5(s->fi), *s = MD5; - char FMD5[2*16+1], *t = FMD5; - static const char hex[] = "0123456789abcdef"; - int gotMD5, i; - - gotMD5 = 0; - if (s) - for (i = 0; i < 16; i++) { - gotMD5 |= *s; - *t++ = hex[ (*s >> 4) & 0xf ]; - *t++ = hex[ (*s++ ) & 0xf ]; - } - *t = '\0'; + const unsigned char * MD5 = rpmfiMD5(s->fi); + char *FMD5 = NULL; + + if (MD5) + FMD5 = pgpHexStr(MD5, rpmDigestLength(PGPHASHALGO_MD5)); result = PyTuple_New(13); if (FN == NULL) { @@ -233,7 +226,7 @@ rpmfi_iternext(rpmfiObject * s) PyTuple_SET_ITEM(result, 11, Py_None); } else PyTuple_SET_ITEM(result, 11, Py_BuildValue("s", FGroup)); - if (!gotMD5) { + if (!FMD5) { Py_INCREF(Py_None); PyTuple_SET_ITEM(result, 12, Py_None); } else diff --git a/rpmdb/header.c b/rpmdb/header.c index 37f05fe..320a659 100644 --- a/rpmdb/header.c +++ b/rpmdb/header.c @@ -12,6 +12,7 @@ #include #include +#include #include "rpmdb/header_internal.h" #include "debug.h" @@ -2587,7 +2588,7 @@ static char * formatValue(headerSprintfArgs hsa, sprintfTag tag, int element) if (val) { need = strlen(val); } else { - val = bin2hex(data, count); + val = pgpHexStr(data, count); need = strlen(val) + tag->pad; } break; diff --git a/rpmdb/header_internal.c b/rpmdb/header_internal.c index b438bdd..1af4bc0 100644 --- a/rpmdb/header_internal.c +++ b/rpmdb/header_internal.c @@ -168,21 +168,3 @@ void headerDump(Header h, FILE *f, int flags, } } -char * bin2hex(const char *data, size_t size) -{ - static char const hex[] = "0123456789abcdef"; - const char * s = data; - char * t, * val; - val = t = xmalloc(size * 2 + 1); - while (size-- > 0) { - unsigned int i; - i = *s++; - *t++ = hex[ (i >> 4) & 0xf ]; - *t++ = hex[ (i ) & 0xf ]; - } - *t = '\0'; - - return val; -} - - diff --git a/rpmdb/header_internal.h b/rpmdb/header_internal.h index 4e8cf87..f789fbb 100644 --- a/rpmdb/header_internal.h +++ b/rpmdb/header_internal.h @@ -171,13 +171,6 @@ void headerDump(Header h, FILE *f, int flags, const struct headerTagTableEntry_s * tags); #define HEADER_DUMP_INLINE 1 -/** \ingroup header - * Convert binary blob to printable hex string - * @param[in] data binary data - * @param[in] count size of data in bytes - */ -char * bin2hex(const char *data, size_t count); - #ifdef __cplusplus } #endif diff --git a/rpmdb/rpmdb.c b/rpmdb/rpmdb.c index fcaf7f1..d00cdfa 100644 --- a/rpmdb/rpmdb.c +++ b/rpmdb/rpmdb.c @@ -1910,7 +1910,7 @@ static int mireSkip (const rpmdbMatchIterator mi) break; case RPM_BIN_TYPE: { - char * str = bin2hex((const char*) u.ptr, c); + char * str = pgpHexStr((const unsigned char*) u.ptr, c); rc = miregexec(mire, str); if ((!rc && !mire->notmatch) || (rc && mire->notmatch)) anymatch++; diff --git a/rpmio/digest.c b/rpmio/digest.c index 5becb6e..c360919 100644 --- a/rpmio/digest.c +++ b/rpmio/digest.c @@ -123,8 +123,6 @@ int rpmDigestFinal(DIGEST_CTX ctx, void ** datap, size_t *lenp, int asAscii) { unsigned char * digest; - char * t; - size_t i; size_t digestlen; if (ctx == NULL) @@ -147,14 +145,7 @@ DPRINTF((stderr, "*** Final(%p,%p,%p,%zd) hashctx %p digest %p\n", ctx, datap, l if (lenp) *lenp = (2*digestlen) + 1; if (datap) { const uint8_t * s = (const uint8_t *) digest; - static const char const hex[] = "0123456789abcdef"; - - *datap = t = xmalloc((2*digestlen) + 1); - for (i = 0 ; i < digestlen; i++) { - *t++ = hex[ (unsigned)((*s >> 4) & 0x0f) ]; - *t++ = hex[ (unsigned)((*s++ ) & 0x0f) ]; - } - *t = '\0'; + *datap = pgpHexStr(s, digestlen); } } if (digest) { -- 2.7.4