iso9660: remove per-reader zero comparison buffer

Check reserved fields by comparing adjacent bytes after confirming the
first byte is zero. This preserves the memcmp fast path and removes
2 KiB from each ISO9660 reader state.
This commit is contained in:
datauwu committed 2026-07-21 17:07:05 +08:00
1 parent 42b2444920
commit b15a0a0bb5
1 file changed
+34 -42
@@ -379,8 +379,6 @@ struct iso9660 {
size_t utf16be_path_len;
unsigned char *utf16be_previous_path;
size_t utf16be_previous_path_len;
/* Null buffer used in bidder to improve its performance. */
unsigned char null[2048];
};
static int archive_read_format_iso9660_bid(struct archive_read *, int);
@@ -403,12 +401,12 @@ static time_t isodate17(const unsigned char *);
static int isodate17_valid(const unsigned char *);
static time_t isodate7(const unsigned char *);
static int isodate7_valid(const unsigned char *);
static int isBootRecord(struct iso9660 *, const unsigned char *);
static int isBootRecord(const unsigned char *);
static int isVolumePartition(struct iso9660 *, const unsigned char *);
static int isVDSetTerminator(struct iso9660 *, const unsigned char *);
static int isVDSetTerminator(const unsigned char *);
static int isJolietSVD(struct iso9660 *, const unsigned char *);
static int isSVD(struct iso9660 *, const unsigned char *);
static int isEVD(struct iso9660 *, const unsigned char *);
static int isSVD(const unsigned char *);
static int isEVD(const unsigned char *);
static int isPVD(struct iso9660 *, const unsigned char *);
static int isRootDirectoryRecord(const unsigned char *);
static int isValid723Integer(const unsigned char *);
@@ -547,15 +545,15 @@ archive_read_format_iso9660_bid(struct archive_read *a, int best_bid)
if (isJolietSVD(iso9660, p))
continue;
}
if (isBootRecord(iso9660, p))
if (isBootRecord(p))
continue;
if (isEVD(iso9660, p))
if (isEVD(p))
continue;
if (isSVD(iso9660, p))
if (isSVD(p))
continue;
if (isVolumePartition(iso9660, p))
continue;
if (isVDSetTerminator(iso9660, p)) {
if (isVDSetTerminator(p)) {
seenTerminator = 1;
break;
}
@@ -603,26 +601,23 @@ archive_read_format_iso9660_options(struct archive_read *a,
}
static int
isNull(struct iso9660 *iso9660, const unsigned char *h, unsigned offset,
unsigned bytes)
isNull(const unsigned char *h, unsigned offset, unsigned bytes)
{
while (bytes >= sizeof(iso9660->null)) {
if (!memcmp(iso9660->null, h + offset, sizeof(iso9660->null)))
return (0);
offset += sizeof(iso9660->null);
bytes -= sizeof(iso9660->null);
}
if (bytes)
return memcmp(iso9660->null, h + offset, bytes) == 0;
else
if (bytes == 0)
return (1);
/*
* If the first byte is zero and every byte equals the following
* byte, the entire range is zero.
*/
return (h[offset] == 0 &&
(bytes == 1 ||
memcmp(h + offset, h + offset + 1, bytes - 1) == 0));
}
static int
isBootRecord(struct iso9660 *iso9660, const unsigned char *h)
isBootRecord(const unsigned char *h)
{
(void)iso9660; /* UNUSED */
/* Type of the Volume Descriptor Boot Record must be 0. */
if (h[0] != 0)
@@ -662,9 +657,8 @@ isVolumePartition(struct iso9660 *iso9660, const unsigned char *h)
}
static int
isVDSetTerminator(struct iso9660 *iso9660, const unsigned char *h)
isVDSetTerminator(const unsigned char *h)
{
(void)iso9660; /* UNUSED */
/* Type of the Volume Descriptor Set Terminator must be 255. */
if (h[0] != 255)
@@ -675,7 +669,7 @@ isVDSetTerminator(struct iso9660 *iso9660, const unsigned char *h)
return (0);
/* Reserved field must be 0. */
if (!isNull(iso9660, h, 7, 2048-7))
if (!isNull(h, 7, 2048-7))
return (0);
return (1);
@@ -690,7 +684,7 @@ isJolietSVD(struct iso9660 *iso9660, const unsigned char *h)
/* Check if current sector is a kind of Supplementary Volume
* Descriptor. */
if (!isSVD(iso9660, h))
if (!isSVD(h))
return (0);
/* FIXME: do more validations according to joliet spec. */
@@ -731,25 +725,24 @@ isJolietSVD(struct iso9660 *iso9660, const unsigned char *h)
}
static int
isSVD(struct iso9660 *iso9660, const unsigned char *h)
isSVD(const unsigned char *h)
{
const unsigned char *p;
ssize_t logical_block_size;
int32_t volume_block;
int32_t location;
(void)iso9660; /* UNUSED */
/* Type 2 means it's a SVD. */
if (h[SVD_type_offset] != 2)
return (0);
/* Reserved field must be 0. */
if (!isNull(iso9660, h, SVD_reserved1_offset, SVD_reserved1_size))
if (!isNull(h, SVD_reserved1_offset, SVD_reserved1_size))
return (0);
if (!isNull(iso9660, h, SVD_reserved2_offset, SVD_reserved2_size))
if (!isNull(h, SVD_reserved2_offset, SVD_reserved2_size))
return (0);
if (!isNull(iso9660, h, SVD_reserved3_offset, SVD_reserved3_size))
if (!isNull(h, SVD_reserved3_offset, SVD_reserved3_size))
return (0);
/* File structure version must be 1 for ISO9660/ECMA119. */
@@ -791,14 +784,13 @@ isSVD(struct iso9660 *iso9660, const unsigned char *h)
}
static int
isEVD(struct iso9660 *iso9660, const unsigned char *h)
isEVD(const unsigned char *h)
{
const unsigned char *p;
ssize_t logical_block_size;
int32_t volume_block;
int32_t location;
(void)iso9660; /* UNUSED */
/* Type of the Enhanced Volume Descriptor must be 2. */
if (h[PVD_type_offset] != 2)
@@ -813,11 +805,11 @@ isEVD(struct iso9660 *iso9660, const unsigned char *h)
return (0);
/* Reserved field must be 0. */
if (!isNull(iso9660, h, PVD_reserved2_offset, PVD_reserved2_size))
if (!isNull(h, PVD_reserved2_offset, PVD_reserved2_size))
return (0);
/* Reserved field must be 0. */
if (!isNull(iso9660, h, PVD_reserved3_offset, PVD_reserved3_size))
if (!isNull(h, PVD_reserved3_offset, PVD_reserved3_size))
return (0);
/* Logical block size must be > 0. */
@@ -853,11 +845,11 @@ isEVD(struct iso9660 *iso9660, const unsigned char *h)
return (0);
/* Reserved field must be 0. */
if (!isNull(iso9660, h, PVD_reserved4_offset, PVD_reserved4_size))
if (!isNull(h, PVD_reserved4_offset, PVD_reserved4_size))
return (0);
/* Reserved field must be 0. */
if (!isNull(iso9660, h, PVD_reserved5_offset, PVD_reserved5_size))
if (!isNull(h, PVD_reserved5_offset, PVD_reserved5_size))
return (0);
/* Read Root Directory Record in Volume Descriptor. */
@@ -891,7 +883,7 @@ isPVD(struct iso9660 *iso9660, const unsigned char *h)
return (0);
/* Reserved field must be 0. */
if (!isNull(iso9660, h, PVD_reserved2_offset, PVD_reserved2_size))
if (!isNull(h, PVD_reserved2_offset, PVD_reserved2_size))
return (0);
/* Volume space size must be encoded according to 7.3.3 */
@@ -903,7 +895,7 @@ isPVD(struct iso9660 *iso9660, const unsigned char *h)
return (0);
/* Reserved field must be 0. */
if (!isNull(iso9660, h, PVD_reserved3_offset, PVD_reserved3_size))
if (!isNull(h, PVD_reserved3_offset, PVD_reserved3_size))
return (0);
/* Volume set size must be encoded according to 7.2.3 */
@@ -961,7 +953,7 @@ isPVD(struct iso9660 *iso9660, const unsigned char *h)
return (0);
/* Reserved field must be 0. */
if (!isNull(iso9660, h, PVD_reserved5_offset, PVD_reserved5_size))
if (!isNull(h, PVD_reserved5_offset, PVD_reserved5_size))
return (0);
/* XXX TODO: Check other values for sanity; reject more