Skip to content

Commit a94b1e6

Browse files
committed
Simplify RpmHead handling in repo_rpmdb
1 parent 3d16610 commit a94b1e6

3 files changed

Lines changed: 62 additions & 93 deletions

File tree

ext/repo_rpmdb.c

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,15 @@ typedef struct rpmhead {
172172
} RpmHead;
173173

174174

175+
static inline void
176+
headinit(RpmHead *h, unsigned int cnt, unsigned int dcnt)
177+
{
178+
h->cnt = (int)cnt;
179+
h->dcnt = dcnt;
180+
h->dp = h->data + 16 * cnt;
181+
h->dp[dcnt] = 0;
182+
}
183+
175184
static inline unsigned char *
176185
headfindtag(RpmHead *h, int tag)
177186
{
@@ -1240,57 +1249,63 @@ struct rpmdbstate {
12401249
char *rootdir;
12411250

12421251
RpmHead *rpmhead; /* header storage space */
1243-
int rpmheadsize;
1252+
unsigned int rpmheadsize;
12441253
};
12451254

12461255
#endif
12471256

12481257

12491258
#ifndef ENABLE_RPMPKG_LIBRPM
12501259

1251-
static int
1252-
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)
1260+
static inline RpmHead *
1261+
realloc_head(struct rpmdbstate *state, unsigned int len)
12531262
{
1254-
RpmHead *rpmhead;
1255-
unsigned int len = 16 * cnt + dsize + pad;
1256-
if (len + 1 > state->rpmheadsize)
1263+
if (len > state->rpmheadsize)
12571264
{
12581265
state->rpmheadsize = len + 128;
12591266
state->rpmhead = solv_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
12601267
}
1261-
rpmhead = state->rpmhead;
1268+
return state->rpmhead;
1269+
}
1270+
1271+
static int
1272+
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)
1273+
{
1274+
unsigned int len = 16 * cnt + dsize + pad;
1275+
RpmHead *rpmhead = realloc_head(state, len + 1);
12621276
if (fread(rpmhead->data, len, 1, fp) != 1)
12631277
return pool_error(state->pool, 0, "%s: unexpected EOF", name);
12641278
if (chk1)
12651279
solv_chksum_add(chk1, rpmhead->data, len);
12661280
if (chk2)
12671281
solv_chksum_add(chk2, rpmhead->data, len);
1268-
rpmhead->data[len] = 0;
1269-
rpmhead->cnt = cnt;
1270-
rpmhead->dcnt = dsize;
1271-
rpmhead->dp = rpmhead->data + cnt * 16;
1282+
headinit(rpmhead, cnt, dsize);
12721283
return 1;
12731284
}
12741285

1275-
#if defined(ENABLE_RPMDB_BYRPMHEADER)
1276-
static void
1277-
headfromblob(struct rpmdbstate *state, const unsigned char *blob, unsigned int cnt, unsigned int dsize)
1286+
# if defined(ENABLE_RPMDB) && (!defined(ENABLE_RPMDB_LIBRPM) || defined(HAVE_RPMDBNEXTITERATORHEADERBLOB))
1287+
1288+
static int
1289+
headfromhdrblob(struct rpmdbstate *state, const unsigned char *data, unsigned int size)
12781290
{
1291+
unsigned int dsize, cnt, len;
12791292
RpmHead *rpmhead;
1280-
unsigned int len = 16 * cnt + dsize;
1281-
if (len + 1 > state->rpmheadsize)
1282-
{
1283-
state->rpmheadsize = len + 128;
1284-
state->rpmhead = solv_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
1285-
}
1286-
rpmhead = state->rpmhead;
1287-
memcpy(rpmhead->data, blob, len);
1288-
rpmhead->data[len] = 0;
1289-
rpmhead->cnt = cnt;
1290-
rpmhead->dcnt = dsize;
1291-
rpmhead->dp = rpmhead->data + cnt * 16;
1293+
if (size < 8)
1294+
return pool_error(state->pool, 0, "corrupt rpm database (size)");
1295+
cnt = getu32(data);
1296+
dsize = getu32(data + 4);
1297+
if (cnt >= MAX_HDR_CNT || dsize >= MAX_HDR_DSIZE)
1298+
return pool_error(state->pool, 0, "corrupt rpm database (cnt/dcnt)");
1299+
if (8 + cnt * 16 + dsize > size)
1300+
return pool_error(state->pool, 0, "corrupt rpm database (data size)");
1301+
len = 16 * cnt + dsize;
1302+
rpmhead = realloc_head(state, len + 1);
1303+
memcpy(rpmhead->data, data + 8, len);
1304+
headinit(rpmhead, cnt, dsize);
1305+
return 1;
12921306
}
1293-
#endif
1307+
1308+
# endif
12941309

12951310
#else
12961311

@@ -2497,7 +2512,8 @@ rpm_byrpmh(void *rpmstate, Header h)
24972512
struct rpmdbstate *state = rpmstate;
24982513
#ifndef ENABLE_RPMPKG_LIBRPM
24992514
const unsigned char *uh;
2500-
unsigned int dsize, cnt;
2515+
unsigned int dsize, cnt, len;
2516+
RpmHead *rpmhead;
25012517

25022518
if (!h)
25032519
return 0;
@@ -2515,7 +2531,10 @@ rpm_byrpmh(void *rpmstate, Header h)
25152531
free((void *)uh);
25162532
return 0;
25172533
}
2518-
headfromblob(state, uh + 8, cnt, dsize);
2534+
len = 16 * cnt + dsize;
2535+
rpmhead = realloc_head(state, len + 1);;
2536+
memcpy(rpmhead->data, uh + 8, len);
2537+
headinit(rpmhead, cnt, dsize);
25192538
free((void *)uh);
25202539
#else
25212540
if (!h)

ext/repo_rpmdb_bdb.h

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ struct rpmdbstate {
4343
char *rootdir;
4444

4545
RpmHead *rpmhead; /* header storage space */
46-
int rpmheadsize;
46+
unsigned int rpmheadsize;
4747

4848
int dbenvopened; /* database environment opened */
4949
int pkgdbopened; /* package database openend */
@@ -387,35 +387,7 @@ getinstalledrpmdbids(struct rpmdbstate *state, const char *index, const char *ma
387387
return entries;
388388
}
389389

390-
/* common code, return dbid on success, -1 on error */
391-
static int
392-
getrpm_dbdata(struct rpmdbstate *state, DBT *dbdata, int dbid)
393-
{
394-
unsigned int dsize, cnt, l;
395-
RpmHead *rpmhead;
396-
397-
if (dbdata->size < 8)
398-
return pool_error(state->pool, -1, "corrupt rpm database (size)");
399-
cnt = getu32((const unsigned char *)dbdata->data);
400-
dsize = getu32((const unsigned char *)dbdata->data + 4);
401-
if (cnt >= MAX_HDR_CNT || dsize >= MAX_HDR_DSIZE)
402-
return pool_error(state->pool, -1, "corrupt rpm database (cnt/dcnt)");
403-
l = cnt * 16 + dsize;
404-
if (8 + l > dbdata->size)
405-
return pool_error(state->pool, -1, "corrupt rpm database (data size)");
406-
if (l + 1 > state->rpmheadsize)
407-
{
408-
state->rpmheadsize = l + 128;
409-
state->rpmhead = solv_realloc(state->rpmhead, sizeof(*rpmhead) + state->rpmheadsize);
410-
}
411-
rpmhead = state->rpmhead;
412-
rpmhead->cnt = cnt;
413-
rpmhead->dcnt = dsize;
414-
memcpy(rpmhead->data, (unsigned char *)dbdata->data + 8, l);
415-
rpmhead->data[l] = 0;
416-
rpmhead->dp = rpmhead->data + cnt * 16;
417-
return dbid;
418-
}
390+
static int headfromhdrblob(struct rpmdbstate *state, const unsigned char *data, unsigned int size);
419391

420392
/* retrive header by rpmdbid, returns 0 if not found, -1 on error */
421393
static int
@@ -438,7 +410,9 @@ getrpm_dbid(struct rpmdbstate *state, Id dbid)
438410
dbdata.size = 0;
439411
if (state->db->get(state->db, NULL, &dbkey, &dbdata, 0))
440412
return 0;
441-
return getrpm_dbdata(state, &dbdata, dbid);
413+
if (!headfromhdrblob(state, (const unsigned char *)dbdata.data, (unsigned int)dbdata.size))
414+
return -1;
415+
return dbid;
442416
}
443417

444418
static int
@@ -512,8 +486,11 @@ pkgdb_cursor_getrpm(struct rpmdbstate *state)
512486
if (dbkey.size != 4)
513487
return pool_error(state->pool, -1, "corrupt Packages database (key size)");
514488
dbid = db2rpmdbid(dbkey.data, state->byteswapped);
515-
if (dbid) /* ignore join key */
516-
return getrpm_dbdata(state, &dbdata, dbid);
489+
if (!dbid)
490+
continue; /* ignore join key */
491+
if (!headfromhdrblob(state, (const unsigned char *)dbdata.data, (unsigned int)dbdata.size))
492+
return -1;
493+
return dbid;
517494
}
518495
return 0; /* no more entries */
519496
}
@@ -534,4 +511,3 @@ hash_name_index(struct rpmdbstate *state, Chksum *chk)
534511
return 0;
535512
}
536513

537-

ext/repo_rpmdb_librpm.h

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ struct rpmdbstate {
2020
char *rootdir;
2121

2222
RpmHead *rpmhead; /* header storage space */
23-
int rpmheadsize;
23+
unsigned int rpmheadsize;
2424

2525
int dbenvopened; /* database environment opened */
2626
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
194194
}
195195

196196
#if defined(HAVE_RPMDBNEXTITERATORHEADERBLOB) && !defined(ENABLE_RPMPKG_LIBRPM)
197-
static int
198-
rpm_byrpmhdrblob(struct rpmdbstate *state, const unsigned char *data, unsigned int size)
199-
{
200-
unsigned int dsize, cnt, len;
201-
RpmHead *rpmhead;
202-
if (size < 8)
203-
return pool_error(state->pool, 0, "corrupt rpm database (size)");
204-
cnt = getu32(data);
205-
dsize = getu32(data + 4);
206-
if (cnt >= MAX_HDR_CNT || dsize >= MAX_HDR_DSIZE)
207-
return pool_error(state->pool, 0, "corrupt rpm database (cnt/dcnt)");
208-
if (8 + cnt * 16 + dsize > size)
209-
return pool_error(state->pool, 0, "corrupt rpm database (data size)");
210-
len = 16 * cnt + dsize;
211-
if (len + 1 > state->rpmheadsize)
212-
{
213-
state->rpmheadsize = len + 128;
214-
state->rpmhead = solv_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
215-
}
216-
rpmhead = state->rpmhead;
217-
memcpy(rpmhead->data, data + 8, len);
218-
rpmhead->data[len] = 0;
219-
rpmhead->cnt = cnt;
220-
rpmhead->dcnt = dsize;
221-
rpmhead->dp = rpmhead->data + cnt * 16;
222-
return 1;
223-
}
197+
static int headfromhdrblob(struct rpmdbstate *state, const unsigned char *data, unsigned int size);
224198
#endif
225199

226200
/* retrive header by rpmdbid, returns 0 if not found, -1 on error */
@@ -248,7 +222,7 @@ getrpm_dbid(struct rpmdbstate *state, Id rpmdbid)
248222
rpmdbFreeIterator(mi);
249223
return 0;
250224
}
251-
if (!rpm_byrpmhdrblob(state, uh, uhlen))
225+
if (!headfromhdrblob(state, uh, uhlen))
252226
{
253227
rpmdbFreeIterator(mi);
254228
return -1;
@@ -307,7 +281,7 @@ pkgdb_cursor_getrpm(struct rpmdbstate *state)
307281
while ((uh = rpmdbNextIteratorHeaderBlob(state->mi, &uhlen)) != 0)
308282
{
309283
Id dbid = rpmdbGetIteratorOffset(state->mi);
310-
if (!rpm_byrpmhdrblob(state, uh, uhlen))
284+
if (!headfromhdrblob(state, uh, uhlen))
311285
continue;
312286
return dbid;
313287
}

0 commit comments

Comments
 (0)