Skip to content

Commit faef2f4

Browse files
committed
Fix bit shift in adb_read_blk_header
And also move it to the other file read functions.
1 parent fab399d commit faef2f4

File tree

1 file changed

+32
-29
lines changed

1 file changed

+32
-29
lines changed

ext/repo_apkv3.c

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,40 +29,15 @@
2929

3030
#define ADB_MAX_SIZE 0x10000000
3131

32+
33+
/* low level */
34+
3235
static inline unsigned int
3336
adb_u32(const unsigned char *p)
3437
{
3538
return p[0] | p[1] << 8 | p[2] << 16 | p[3] << 24;
3639
}
3740

38-
static int
39-
adb_read_blk_header(FILE *fp, unsigned long long *sizep)
40-
{
41-
unsigned char buf[12];
42-
unsigned int size;
43-
unsigned long long lsize;
44-
if (fread(buf, 4, 1, fp) != 1)
45-
return -1;
46-
size = buf[0] | buf[1] << 8 | buf[2] << 16 | (buf[3] & 0x3f) << 24;
47-
if ((buf[3] & 0xc0) != 0xc0)
48-
{
49-
if (size < 4)
50-
return -1;
51-
*sizep = size - 4;
52-
return (buf[3] & 0xc0) >> 3;
53-
}
54-
if (fread(buf, 12, 1, fp) != 1)
55-
return -1;
56-
lsize = adb_u32(buf + 4);
57-
lsize |= (unsigned long long)adb_u32(buf + 8) << 32;
58-
if (lsize < 16)
59-
return -1;
60-
*sizep = lsize - 16;
61-
return size;
62-
}
63-
64-
/* low level */
65-
6641
static const unsigned char *
6742
adb_blob(const unsigned char *adb, size_t adblen, unsigned int v, size_t *bloblp)
6843
{
@@ -317,6 +292,34 @@ add_add_idb_pkg(Pool *pool, Repo *repo, Repodata *data, const unsigned char *adb
317292
return adb_add_pkg_info(pool, repo, data, adb, adblen, adb_idx(adb, v, cnt, 1), flags);
318293
}
319294

295+
/* file reading */
296+
297+
static int
298+
adb_read_blk_header(FILE *fp, unsigned long long *sizep)
299+
{
300+
unsigned char buf[12];
301+
unsigned int size;
302+
unsigned long long lsize;
303+
if (fread(buf, 4, 1, fp) != 1)
304+
return -1;
305+
size = buf[0] | buf[1] << 8 | buf[2] << 16 | (buf[3] & 0x3f) << 24;
306+
if ((buf[3] & 0xc0) != 0xc0)
307+
{
308+
if (size < 4)
309+
return -1;
310+
*sizep = size - 4;
311+
return (buf[3] & 0xc0) >> 6;
312+
}
313+
if (fread(buf, 12, 1, fp) != 1)
314+
return -1;
315+
lsize = adb_u32(buf + 4);
316+
lsize |= (unsigned long long)adb_u32(buf + 8) << 32;
317+
if (lsize < 16)
318+
return -1;
319+
*sizep = lsize - 16;
320+
return size;
321+
}
322+
320323
static const unsigned char *
321324
adb_read_adb_blk(Pool *pool, FILE *fp, const char *fn, size_t *adblenp)
322325
{
@@ -391,7 +394,7 @@ apkv3_add_idx(Repo *repo, Repodata *data, FILE *fp, int flags)
391394
unsigned int v, cnt, idx;
392395
int idb = flags & APK_ADD_INSTALLED_DB ? 1 : 0;
393396

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

0 commit comments

Comments
 (0)