Skip to content

Commit

Permalink
Fix bit shift in adb_read_blk_header
Browse files Browse the repository at this point in the history
And also move it to the other file read functions.
  • Loading branch information
mlschroe committed Jan 16, 2025
1 parent fab399d commit faef2f4
Showing 1 changed file with 32 additions and 29 deletions.
61 changes: 32 additions & 29 deletions ext/repo_apkv3.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,40 +29,15 @@

#define ADB_MAX_SIZE 0x10000000


/* low level */

static inline unsigned int
adb_u32(const unsigned char *p)
{
return p[0] | p[1] << 8 | p[2] << 16 | p[3] << 24;
}

static int
adb_read_blk_header(FILE *fp, unsigned long long *sizep)
{
unsigned char buf[12];
unsigned int size;
unsigned long long lsize;
if (fread(buf, 4, 1, fp) != 1)
return -1;
size = buf[0] | buf[1] << 8 | buf[2] << 16 | (buf[3] & 0x3f) << 24;
if ((buf[3] & 0xc0) != 0xc0)
{
if (size < 4)
return -1;
*sizep = size - 4;
return (buf[3] & 0xc0) >> 3;
}
if (fread(buf, 12, 1, fp) != 1)
return -1;
lsize = adb_u32(buf + 4);
lsize |= (unsigned long long)adb_u32(buf + 8) << 32;
if (lsize < 16)
return -1;
*sizep = lsize - 16;
return size;
}

/* low level */

static const unsigned char *
adb_blob(const unsigned char *adb, size_t adblen, unsigned int v, size_t *bloblp)
{
Expand Down Expand Up @@ -317,6 +292,34 @@ add_add_idb_pkg(Pool *pool, Repo *repo, Repodata *data, const unsigned char *adb
return adb_add_pkg_info(pool, repo, data, adb, adblen, adb_idx(adb, v, cnt, 1), flags);
}

/* file reading */

static int
adb_read_blk_header(FILE *fp, unsigned long long *sizep)
{
unsigned char buf[12];
unsigned int size;
unsigned long long lsize;
if (fread(buf, 4, 1, fp) != 1)
return -1;
size = buf[0] | buf[1] << 8 | buf[2] << 16 | (buf[3] & 0x3f) << 24;
if ((buf[3] & 0xc0) != 0xc0)
{
if (size < 4)
return -1;
*sizep = size - 4;
return (buf[3] & 0xc0) >> 6;
}
if (fread(buf, 12, 1, fp) != 1)
return -1;
lsize = adb_u32(buf + 4);
lsize |= (unsigned long long)adb_u32(buf + 8) << 32;
if (lsize < 16)
return -1;
*sizep = lsize - 16;
return size;
}

static const unsigned char *
adb_read_adb_blk(Pool *pool, FILE *fp, const char *fn, size_t *adblenp)
{
Expand Down Expand Up @@ -391,7 +394,7 @@ apkv3_add_idx(Repo *repo, Repodata *data, FILE *fp, int flags)
unsigned int v, cnt, idx;
int idb = flags & APK_ADD_INSTALLED_DB ? 1 : 0;

if (fread(buf, 4, 1, fp) != 1 || memcmp(buf, (idb ? "idb" : "indx") , 4))
if (fread(buf, 4, 1, fp) != 1 || memcmp(buf, (idb ? "idb" : "indx") , 4) != 0)
{
pool_error(pool, -1, (idb ? "not an apkv3 installed database" : "not an apkv3 package index"));
return -1;
Expand Down

0 comments on commit faef2f4

Please sign in to comment.