From 8d01524d480dccb7324bf525a3110840166dd010 Mon Sep 17 00:00:00 2001 From: Michael Schroeder Date: Wed, 2 Mar 2011 12:39:15 +0100 Subject: [PATCH] - add sat_chksum_type2str() and sat_chksum_str2type() and use them --- ext/repo_content.c | 39 +++++++++++++++++++-------------------- ext/repo_deb.c | 13 ++++++------- ext/repo_repomdxml.c | 22 ++++++++-------------- ext/repo_rpmmd.c | 16 +++++----------- ext/repo_susetags.c | 13 ++++--------- src/chksum.c | 28 ++++++++++++++++++++++++++++ src/chksum.h | 2 ++ tools/repomdxml2solv.c | 6 +++--- 8 files changed, 75 insertions(+), 64 deletions(-) diff --git a/ext/repo_content.c b/ext/repo_content.c index e35bf6b..db35176 100644 --- a/ext/repo_content.c +++ b/ext/repo_content.c @@ -2,6 +2,8 @@ * repo_content.c * * Parses 'content' file into .solv + * A 'content' file is the repomd.xml of the susetags format + * * See http://en.opensuse.org/Standards/YaST2_Repository_Metadata/content for a description * of the syntax * @@ -23,6 +25,7 @@ #include "pool.h" #include "repo.h" #include "util.h" +#include "chksum.h" #include "repo_content.h" #define DISABLE_SPLIT #include "tools_util.h" @@ -139,7 +142,7 @@ adddep(Pool *pool, struct parsedata *pd, unsigned int olddeps, char *line, Id ma if (!rel || !evr) { pool_debug(pool, SAT_FATAL, "repo_content: bad relation '%s %s'\n", name, rel); - exit(1); + continue; } for (flags = 0; flags < 6; flags++) if (!strcmp(rel, flagtab[flags])) @@ -147,7 +150,7 @@ adddep(Pool *pool, struct parsedata *pd, unsigned int olddeps, char *line, Id ma if (flags == 6) { pool_debug(pool, SAT_FATAL, "repo_content: unknown relation '%s'\n", rel); - exit(1); + continue; } id = rel2id(pool, id, str2id(pool, evr, 1), flags + 1, 1); } @@ -332,21 +335,17 @@ repo_add_content(Repo *repo, FILE *fp, int flags) continue; if (!*value) continue; - if (!strcasecmp(checksumtype, "sha") || !strcasecmp(checksumtype, "sha1")) - l = SIZEOF_SHA1 * 2, type = REPOKEY_TYPE_SHA1; - else if (!strcasecmp(checksumtype, "sha256")) - l = SIZEOF_SHA256 * 2, type = REPOKEY_TYPE_SHA256; - else if (!strcasecmp(checksumtype, "md5")) - l = SIZEOF_MD5 * 2, type = REPOKEY_TYPE_MD5; - else - { + type = sat_chksum_str2type(checksumtype); + if (!type) + { fprintf(stderr, "Unknown checksum type: %s: %s\n", value, checksumtype); - exit(1); - } - if (strlen(checksum) != l) + continue; + } + l = sat_chksum_len(type); + if (strlen(checksum) != 2 * l) { fprintf(stderr, "Invalid checksum length: %s: for %s\n", value, checksum); - exit(1); + continue; } fh = repodata_new_handle(data); repodata_set_poolstr(data, fh, SUSETAGS_FILE_TYPE, key); @@ -496,14 +495,14 @@ repo_add_content(Repo *repo, FILE *fp, int flags) if (defvendor) free(defvendor); + if (s && !s->name) + { + pool_debug(pool, SAT_FATAL, "repo_content: 'content' incomplete, no product solvable created!\n"); + repo_free_solvable_block(repo, s - pool->solvables, 1, 1); + s = 0; + } if (s) { - if (!s->name) - { - pool_debug(pool, SAT_FATAL, "repo_content: 'content' incomplete, no product solvable created!\n"); - exit(1); - } - if (pd.tmprel) s->evr = makeevr(pool, join(&pd, pd.tmpvers, "-", pd.tmprel)); else diff --git a/ext/repo_deb.c b/ext/repo_deb.c index 841b580..2c0574b 100644 --- a/ext/repo_deb.c +++ b/ext/repo_deb.c @@ -169,6 +169,7 @@ control2solvable(Solvable *s, Repodata *data, char *control) int havesource = 0; char checksum[32 * 2 + 1]; Id checksumtype = 0; + Id newtype; p = control; while (*p) @@ -277,15 +278,13 @@ control2solvable(Solvable *s, Repodata *data, char *control) s->recommends = makedeps(repo, q, s->recommends, 0); break; case 'S' << 8 | 'H': - if (!strcasecmp(tag, "sha1") && checksumtype != REPOKEY_TYPE_SHA256 && strlen(q) == 20 * 2) - { - strcpy(checksum, q); - checksumtype = REPOKEY_TYPE_SHA1; - } - else if (!strcasecmp(tag, "sha256") && strlen(q) == 32 * 2) + newtype = sat_chksum_str2type(tag); + if (!newtype || sat_chksum_len(newtype) * 2 != strlen(q)) + break; + if (!checksumtype || (newtype == REPOKEY_TYPE_SHA1 && checksumtype != REPOKEY_TYPE_SHA256) || newtype == REPOKEY_TYPE_SHA256) { strcpy(checksum, q); - checksumtype = REPOKEY_TYPE_SHA256; + checksumtype = newtype; } break; case 'S' << 8 | 'O': diff --git a/ext/repo_repomdxml.c b/ext/repo_repomdxml.c index 193953d..ef702a6 100644 --- a/ext/repo_repomdxml.c +++ b/ext/repo_repomdxml.c @@ -18,6 +18,7 @@ #include "pool.h" #include "repo.h" +#include "chksum.h" #include "repo_updateinfoxml.h" //#define DUMPOUT 0 @@ -347,24 +348,17 @@ endElement(void *userData, const char *name) case STATE_CHECKSUM: case STATE_OPENCHECKSUM: { - int l; - Id type; - if (!strcasecmp(pd->tmpattr, "sha") || !strcasecmp(pd->tmpattr, "sha1")) - l = SIZEOF_SHA1 * 2, type = REPOKEY_TYPE_SHA1; - else if (!strcasecmp(pd->tmpattr, "sha256")) - l = SIZEOF_SHA256 * 2, type = REPOKEY_TYPE_SHA256; - else if (!strcasecmp(pd->tmpattr, "md5")) - l = SIZEOF_MD5 * 2, type = REPOKEY_TYPE_MD5; - else - { + Id type = sat_chksum_str2type(pd->tmpattr); + if (!type) + { fprintf(stderr, "Unknown checksum type: %d: %s\n", (unsigned int)XML_GetCurrentLineNumber(*pd->parser), pd->tmpattr); exit(1); - } - if (strlen(pd->content) != l) - { + } + if (strlen(pd->content) != 2 * sat_chksum_len(type)) + { fprintf(stderr, "Invalid checksum length: %d: for %s\n", (unsigned int)XML_GetCurrentLineNumber(*pd->parser), pd->tmpattr); exit(1); - } + } repodata_set_checksum(pd->data, pd->rdhandle, pd->state == STATE_CHECKSUM ? REPOSITORY_REPOMD_CHECKSUM : REPOSITORY_REPOMD_OPENCHECKSUM, type, pd->content); break; } diff --git a/ext/repo_rpmmd.c b/ext/repo_rpmmd.c index cd71fad..d4a202e 100644 --- a/ext/repo_rpmmd.c +++ b/ext/repo_rpmmd.c @@ -964,20 +964,14 @@ endElement(void *userData, const char *name) break; case STATE_CHECKSUM: { - int l; Id type, index; - if (!strcasecmp(pd->tmpattr, "sha") || !strcasecmp(pd->tmpattr, "sha1")) - l = SIZEOF_SHA1 * 2, type = REPOKEY_TYPE_SHA1; - else if (!strcasecmp(pd->tmpattr, "sha256")) - l = SIZEOF_SHA256 * 2, type = REPOKEY_TYPE_SHA256; - else if (!strcasecmp(pd->tmpattr, "md5")) - l = SIZEOF_MD5 * 2, type = REPOKEY_TYPE_MD5; - else - { + type = sat_chksum_str2type(pd->tmpattr); + if (!type) + { fprintf(stderr, "Unknown checksum type: %d: %s\n", (unsigned int)XML_GetCurrentLineNumber(*pd->parser), pd->tmpattr); exit(1); - } - if (strlen(pd->content) != l) + } + if (strlen(pd->content) != 2 * sat_chksum_len(type)) { fprintf(stderr, "Invalid checksum length: %d: for %s\n", (unsigned int)XML_GetCurrentLineNumber(*pd->parser), pd->tmpattr); exit(1); diff --git a/ext/repo_susetags.c b/ext/repo_susetags.c index 1e2885c..68ab764 100644 --- a/ext/repo_susetags.c +++ b/ext/repo_susetags.c @@ -15,6 +15,7 @@ #include "pool.h" #include "repo.h" #include "hash.h" +#include "chksum.h" #include "tools_util.h" #include "repo_susetags.h" @@ -175,25 +176,19 @@ static void set_checksum(struct parsedata *pd, Repodata *data, Id handle, Id keyname, char *line) { char *sp[3]; - int l; Id type; if (split(line, sp, 3) != 2) { pool_debug(pd->repo->pool, SAT_FATAL, "susetags: bad checksum line: %d: %s\n", pd->lineno, line); exit(1); } - if (!strcasecmp(sp[0], "sha1")) - l = SIZEOF_SHA1 * 2, type = REPOKEY_TYPE_SHA1; - else if (!strcasecmp(sp[0], "sha256")) - l = SIZEOF_SHA256 * 2, type = REPOKEY_TYPE_SHA256; - else if (!strcasecmp(sp[0], "md5")) - l = SIZEOF_MD5 * 2, type = REPOKEY_TYPE_MD5; - else + type = sat_chksum_str2type(sp[0]); + if (!type) { pool_debug(pd->repo->pool, SAT_FATAL, "susetags: unknown checksum type: %d: %s\n", pd->lineno, sp[0]); exit(1); } - if (strlen(sp[1]) != l) + if (strlen(sp[1]) != 2 * sat_chksum_len(type)) { pool_debug(pd->repo->pool, SAT_FATAL, "susetags: bad checksum length: %d: for %s: %s\n", pd->lineno, sp[0], sp[1]); exit(1); diff --git a/src/chksum.c b/src/chksum.c index 179ce83..7f28d16 100644 --- a/src/chksum.c +++ b/src/chksum.c @@ -127,6 +127,34 @@ sat_chksum_get_type(void *handle) return h->type; } +const char * +sat_chksum_type2str(Id type) +{ + switch(type) + { + case REPOKEY_TYPE_MD5: + return "md5"; + case REPOKEY_TYPE_SHA1: + return "sha1"; + case REPOKEY_TYPE_SHA256: + return "sha256"; + default: + return 0; + } +} + +Id +sat_chksum_str2type(const char *str) +{ + if (!strcasecmp(str, "md5")) + return REPOKEY_TYPE_MD5; + if (!strcasecmp(str, "sha") || !strcasecmp(str, "sha1")) + return REPOKEY_TYPE_SHA1; + if (!strcasecmp(str, "sha256")) + return REPOKEY_TYPE_SHA256; + return 0; +} + void * sat_chksum_free(void *handle, unsigned char *cp) { diff --git a/src/chksum.h b/src/chksum.h index bcdbc4f..06fa64c 100644 --- a/src/chksum.h +++ b/src/chksum.h @@ -6,6 +6,8 @@ void sat_chksum_add(void *handle, const void *data, int len); Id sat_chksum_get_type(void *handle); const unsigned char *sat_chksum_get(void *handle, int *lenp); void *sat_chksum_free(void *handle, unsigned char *cp); +const char *sat_chksum_type2str(Id type); +Id sat_chksum_str2type(const char *str); static inline int sat_chksum_len(Id type) { diff --git a/tools/repomdxml2solv.c b/tools/repomdxml2solv.c index 2dcdb7e..854d790 100644 --- a/tools/repomdxml2solv.c +++ b/tools/repomdxml2solv.c @@ -15,6 +15,7 @@ #include "pool.h" #include "repo.h" +#include "chksum.h" #include "repo_repomdxml.h" #include "common_write.h" @@ -74,11 +75,10 @@ doquery(Pool *pool, Repo *repo, const char *query) case REPOKEY_TYPE_NUM: printf("%d\n", di.kv.num); break; + case REPOKEY_TYPE_MD5: case REPOKEY_TYPE_SHA1: - printf("sha1:%s\n", repodata_chk2str(di.data, di.key->type, (unsigned char *)di.kv.str)); - break; case REPOKEY_TYPE_SHA256: - printf("sha256:%s\n", repodata_chk2str(di.data, di.key->type, (unsigned char *)di.kv.str)); + printf("%s:%s\n", sat_chksum_type2str(di.key->type), repodata_chk2str(di.data, di.key->type, (unsigned char *)di.kv.str)); break; default: break; -- 2.7.4