]> git.ipfire.org Git - thirdparty/libsolv.git/commitdiff
Simplify RpmHead handling in repo_rpmdb
authorMichael Schroeder <mls@suse.de>
Wed, 11 Dec 2019 13:01:22 +0000 (14:01 +0100)
committerMichael Schroeder <mls@suse.de>
Wed, 11 Dec 2019 13:01:22 +0000 (14:01 +0100)
ext/repo_rpmdb.c
ext/repo_rpmdb_bdb.h
ext/repo_rpmdb_librpm.h

index 67aa97c15bcbe3106d4c50cfb5ad776ca4c52f51..ed9862815cc10c37a43b07bfa9ded98bd071cadc 100644 (file)
@@ -172,6 +172,15 @@ typedef struct rpmhead {
 } RpmHead;
 
 
+static inline void
+headinit(RpmHead *h, unsigned int cnt, unsigned int dcnt)
+{
+  h->cnt = (int)cnt;
+  h->dcnt = dcnt;
+  h->dp = h->data + 16 * cnt;
+  h->dp[dcnt] = 0;
+}
+
 static inline unsigned char *
 headfindtag(RpmHead *h, int tag)
 {
@@ -1240,7 +1249,7 @@ struct rpmdbstate {
   char *rootdir;
 
   RpmHead *rpmhead;    /* header storage space */
-  int rpmheadsize;
+  unsigned int rpmheadsize;
 };
 
 #endif
@@ -1248,49 +1257,55 @@ struct rpmdbstate {
 
 #ifndef ENABLE_RPMPKG_LIBRPM
 
-static int
-headfromfp(struct rpmdbstate *state, const char *name, FILE *fp, unsigned char *lead, unsigned int cnt, unsigned int dsize, unsigned int pad, Chksum *chk1, Chksum *chk2)
+static inline RpmHead *
+realloc_head(struct rpmdbstate *state, unsigned int len)
 {
-  RpmHead *rpmhead;
-  unsigned int len = 16 * cnt + dsize + pad;
-  if (len + 1 > state->rpmheadsize)
+  if (len > state->rpmheadsize)
     {
       state->rpmheadsize = len + 128;
       state->rpmhead = solv_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
     }
-  rpmhead = state->rpmhead;
+  return state->rpmhead;
+}
+
+static int
+headfromfp(struct rpmdbstate *state, const char *name, FILE *fp, unsigned char *lead, unsigned int cnt, unsigned int dsize, unsigned int pad, Chksum *chk1, Chksum *chk2)
+{
+  unsigned int len = 16 * cnt + dsize + pad;
+  RpmHead *rpmhead = realloc_head(state, len + 1);
   if (fread(rpmhead->data, len, 1, fp) != 1)
     return pool_error(state->pool, 0, "%s: unexpected EOF", name);
   if (chk1)
     solv_chksum_add(chk1, rpmhead->data, len);
   if (chk2)
     solv_chksum_add(chk2, rpmhead->data, len);
-  rpmhead->data[len] = 0;
-  rpmhead->cnt = cnt;
-  rpmhead->dcnt = dsize;
-  rpmhead->dp = rpmhead->data + cnt * 16;
+  headinit(rpmhead, cnt, dsize);
   return 1;
 }
 
-#if defined(ENABLE_RPMDB_BYRPMHEADER)
-static void
-headfromblob(struct rpmdbstate *state, const unsigned char *blob, unsigned int cnt, unsigned int dsize)
+# if defined(ENABLE_RPMDB) && (!defined(ENABLE_RPMDB_LIBRPM) || defined(HAVE_RPMDBNEXTITERATORHEADERBLOB))
+
+static int
+headfromhdrblob(struct rpmdbstate *state, const unsigned char *data, unsigned int size)
 {
+  unsigned int dsize, cnt, len;
   RpmHead *rpmhead;
-  unsigned int len = 16 * cnt + dsize;
-  if (len + 1 > state->rpmheadsize)
-    {
-      state->rpmheadsize = len + 128;
-      state->rpmhead = solv_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
-    }
-  rpmhead = state->rpmhead;
-  memcpy(rpmhead->data, blob, len);
-  rpmhead->data[len] = 0;
-  rpmhead->cnt = cnt;
-  rpmhead->dcnt = dsize;
-  rpmhead->dp = rpmhead->data + cnt * 16;
+  if (size < 8)
+    return pool_error(state->pool, 0, "corrupt rpm database (size)");
+  cnt = getu32(data);
+  dsize = getu32(data + 4);
+  if (cnt >= MAX_HDR_CNT || dsize >= MAX_HDR_DSIZE)
+    return pool_error(state->pool, 0, "corrupt rpm database (cnt/dcnt)");
+  if (8 + cnt * 16 + dsize > size)
+    return pool_error(state->pool, 0, "corrupt rpm database (data size)");
+  len = 16 * cnt + dsize;
+  rpmhead = realloc_head(state, len + 1);
+  memcpy(rpmhead->data, data + 8, len);
+  headinit(rpmhead, cnt, dsize);
+  return 1;
 }
-#endif
+
+# endif
 
 #else
 
@@ -2497,7 +2512,8 @@ rpm_byrpmh(void *rpmstate, Header h)
   struct rpmdbstate *state = rpmstate;
 #ifndef ENABLE_RPMPKG_LIBRPM
   const unsigned char *uh;
-  unsigned int dsize, cnt;
+  unsigned int dsize, cnt, len;
+  RpmHead *rpmhead;
 
   if (!h)
     return 0;
@@ -2515,7 +2531,10 @@ rpm_byrpmh(void *rpmstate, Header h)
       free((void *)uh);
       return 0;
     }
-  headfromblob(state, uh + 8, cnt, dsize);
+  len = 16 * cnt + dsize;
+  rpmhead = realloc_head(state, len + 1);;
+  memcpy(rpmhead->data, uh + 8, len);
+  headinit(rpmhead, cnt, dsize);
   free((void *)uh);
 #else
   if (!h)
index a8b8500873237715d740bb1344b1834b2bec7ec0..a7f5195190ec538cd37af4ad368e07f054993d97 100644 (file)
@@ -43,7 +43,7 @@ struct rpmdbstate {
   char *rootdir;
 
   RpmHead *rpmhead;    /* header storage space */
-  int rpmheadsize;
+  unsigned int rpmheadsize;
 
   int dbenvopened;     /* database environment opened */
   int pkgdbopened;     /* package database openend */
@@ -387,35 +387,7 @@ getinstalledrpmdbids(struct rpmdbstate *state, const char *index, const char *ma
   return entries;
 }
 
-/* common code, return dbid on success, -1 on error */
-static int
-getrpm_dbdata(struct rpmdbstate *state, DBT *dbdata, int dbid)
-{
-  unsigned int dsize, cnt, l;
-  RpmHead *rpmhead;
-
-  if (dbdata->size < 8)
-    return pool_error(state->pool, -1, "corrupt rpm database (size)");
-  cnt = getu32((const unsigned char *)dbdata->data);
-  dsize = getu32((const unsigned char *)dbdata->data + 4);
-  if (cnt >= MAX_HDR_CNT || dsize >= MAX_HDR_DSIZE)
-    return pool_error(state->pool, -1, "corrupt rpm database (cnt/dcnt)");
-  l = cnt * 16 + dsize;
-  if (8 + l > dbdata->size)
-    return pool_error(state->pool, -1, "corrupt rpm database (data size)");
-  if (l + 1 > state->rpmheadsize)
-    {
-      state->rpmheadsize = l + 128;
-      state->rpmhead = solv_realloc(state->rpmhead, sizeof(*rpmhead) + state->rpmheadsize);
-    }
-  rpmhead = state->rpmhead;
-  rpmhead->cnt = cnt;
-  rpmhead->dcnt = dsize;
-  memcpy(rpmhead->data, (unsigned char *)dbdata->data + 8, l);
-  rpmhead->data[l] = 0;
-  rpmhead->dp = rpmhead->data + cnt * 16;
-  return dbid;
-}
+static int headfromhdrblob(struct rpmdbstate *state, const unsigned char *data, unsigned int size);
 
 /* retrive header by rpmdbid, returns 0 if not found, -1 on error */
 static int
@@ -438,7 +410,9 @@ getrpm_dbid(struct rpmdbstate *state, Id dbid)
   dbdata.size = 0;
   if (state->db->get(state->db, NULL, &dbkey, &dbdata, 0))
     return 0;
-  return getrpm_dbdata(state, &dbdata, dbid);
+  if (!headfromhdrblob(state, (const unsigned char *)dbdata.data, (unsigned int)dbdata.size))
+    return -1;
+  return dbid;
 }
 
 static int
@@ -512,8 +486,11 @@ pkgdb_cursor_getrpm(struct rpmdbstate *state)
       if (dbkey.size != 4)
        return pool_error(state->pool, -1, "corrupt Packages database (key size)");
       dbid = db2rpmdbid(dbkey.data, state->byteswapped);
-      if (dbid)                /* ignore join key */
-        return getrpm_dbdata(state, &dbdata, dbid);
+      if (!dbid)
+       continue;       /* ignore join key */
+      if (!headfromhdrblob(state, (const unsigned char *)dbdata.data, (unsigned int)dbdata.size))
+       return -1;
+      return dbid;
     }
   return 0;    /* no more entries */
 }
@@ -534,4 +511,3 @@ hash_name_index(struct rpmdbstate *state, Chksum *chk)
   return 0;
 }
 
-
index b50a4b508adeef2db2e692bb836a10101f871a15..b424eddd71ca6aa808950bfadc89da555a5e931c 100644 (file)
@@ -20,7 +20,7 @@ struct rpmdbstate {
   char *rootdir;
 
   RpmHead *rpmhead;    /* header storage space */
-  int rpmheadsize;
+  unsigned int rpmheadsize;
 
   int dbenvopened;     /* database environment opened */
   int is_ostree;       /* read-only db that lives in /usr/share/rpm */
@@ -194,33 +194,7 @@ getinstalledrpmdbids(struct rpmdbstate *state, const char *index, const char *ma
 }
 
 #if defined(HAVE_RPMDBNEXTITERATORHEADERBLOB) && !defined(ENABLE_RPMPKG_LIBRPM)
-static int
-rpm_byrpmhdrblob(struct rpmdbstate *state, const unsigned char *data, unsigned int size)
-{
-  unsigned int dsize, cnt, len;
-  RpmHead *rpmhead;
-  if (size < 8)
-    return pool_error(state->pool, 0, "corrupt rpm database (size)");
-  cnt = getu32(data);
-  dsize = getu32(data + 4);
-  if (cnt >= MAX_HDR_CNT || dsize >= MAX_HDR_DSIZE)
-    return pool_error(state->pool, 0, "corrupt rpm database (cnt/dcnt)");
-  if (8 + cnt * 16 + dsize > size)
-    return pool_error(state->pool, 0, "corrupt rpm database (data size)");
-  len = 16 * cnt + dsize;
-  if (len + 1 > state->rpmheadsize)
-    {
-      state->rpmheadsize = len + 128;
-      state->rpmhead = solv_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
-    }
-  rpmhead = state->rpmhead;
-  memcpy(rpmhead->data, data + 8, len);
-  rpmhead->data[len] = 0;
-  rpmhead->cnt = cnt;
-  rpmhead->dcnt = dsize;
-  rpmhead->dp = rpmhead->data + cnt * 16;
-  return 1;
-}
+static int headfromhdrblob(struct rpmdbstate *state, const unsigned char *data, unsigned int size);
 #endif
 
 /* retrive header by rpmdbid, returns 0 if not found, -1 on error */
@@ -248,7 +222,7 @@ getrpm_dbid(struct rpmdbstate *state, Id rpmdbid)
       rpmdbFreeIterator(mi);
       return 0;
     }
-  if (!rpm_byrpmhdrblob(state, uh, uhlen))
+  if (!headfromhdrblob(state, uh, uhlen))
     {
       rpmdbFreeIterator(mi);
       return -1;
@@ -307,7 +281,7 @@ pkgdb_cursor_getrpm(struct rpmdbstate *state)
   while ((uh = rpmdbNextIteratorHeaderBlob(state->mi, &uhlen)) != 0)
     {
       Id dbid = rpmdbGetIteratorOffset(state->mi);
-      if (!rpm_byrpmhdrblob(state, uh, uhlen))
+      if (!headfromhdrblob(state, uh, uhlen))
        continue;
       return dbid;
     }