archive/iso9660: fix unaligned reads
Oh the horror! This plugin cannot possibly ever have worked. It was
broken from the start, when it was added in commit 37796699cf
nearly
twelve (!) years ago.
The plugin would always read at sector boundaries, so it could only
ever work at multiples of 2 kB.
This commit is contained in:
parent
1a5e0ef7c9
commit
17dd21ac7f
1
NEWS
1
NEWS
|
@ -5,6 +5,7 @@ ver 0.21.26 (not yet released)
|
||||||
* archive
|
* archive
|
||||||
- bzip2: fix crash on corrupt bzip2 file
|
- bzip2: fix crash on corrupt bzip2 file
|
||||||
- bzip2: flush output at end of input file
|
- bzip2: flush output at end of input file
|
||||||
|
- iso9660: fix unaligned reads
|
||||||
- zzip: fix crash on corrupt ZIP file
|
- zzip: fix crash on corrupt ZIP file
|
||||||
* decoder
|
* decoder
|
||||||
- sndfile: fix lost samples at end of file
|
- sndfile: fix lost samples at end of file
|
||||||
|
|
|
@ -29,9 +29,12 @@
|
||||||
#include "fs/Path.hxx"
|
#include "fs/Path.hxx"
|
||||||
#include "util/RuntimeError.hxx"
|
#include "util/RuntimeError.hxx"
|
||||||
#include "util/StringCompare.hxx"
|
#include "util/StringCompare.hxx"
|
||||||
|
#include "util/WritableBuffer.hxx"
|
||||||
|
|
||||||
#include <cdio/iso9660.h>
|
#include <cdio/iso9660.h>
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
@ -144,6 +147,47 @@ class Iso9660InputStream final : public InputStream {
|
||||||
|
|
||||||
const lsn_t lsn;
|
const lsn_t lsn;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* libiso9660 can only read whole sectors at a time, and this
|
||||||
|
* buffer is used to store one whole sector and allow Read()
|
||||||
|
* to handle partial sector reads.
|
||||||
|
*/
|
||||||
|
class BlockBuffer {
|
||||||
|
size_t position = 0, fill = 0;
|
||||||
|
|
||||||
|
std::array<uint8_t, ISO_BLOCKSIZE> data;
|
||||||
|
|
||||||
|
public:
|
||||||
|
ConstBuffer<uint8_t> Read() const noexcept {
|
||||||
|
assert(fill <= data.size());
|
||||||
|
assert(position <= fill);
|
||||||
|
|
||||||
|
return {&data[position], &data[fill]};
|
||||||
|
}
|
||||||
|
|
||||||
|
void Consume(size_t nbytes) noexcept {
|
||||||
|
assert(nbytes <= Read().size);
|
||||||
|
|
||||||
|
position += nbytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
WritableBuffer<uint8_t> Write() noexcept {
|
||||||
|
assert(Read().empty());
|
||||||
|
|
||||||
|
return {data.data(), data.size()};
|
||||||
|
}
|
||||||
|
|
||||||
|
void Append(size_t nbytes) noexcept {
|
||||||
|
assert(Read().empty());
|
||||||
|
assert(nbytes <= data.size());
|
||||||
|
|
||||||
|
fill = nbytes;
|
||||||
|
position = 0;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
BlockBuffer buffer;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Iso9660InputStream(const std::shared_ptr<Iso9660> &_iso,
|
Iso9660InputStream(const std::shared_ptr<Iso9660> &_iso,
|
||||||
const char *_uri,
|
const char *_uri,
|
||||||
|
@ -182,35 +226,56 @@ Iso9660ArchiveFile::OpenStream(const char *pathname,
|
||||||
size_t
|
size_t
|
||||||
Iso9660InputStream::Read(void *ptr, size_t read_size)
|
Iso9660InputStream::Read(void *ptr, size_t read_size)
|
||||||
{
|
{
|
||||||
const ScopeUnlock unlock(mutex);
|
const offset_type remaining = size - offset;
|
||||||
|
if (remaining == 0)
|
||||||
int readed = 0;
|
|
||||||
int no_blocks, cur_block;
|
|
||||||
size_t left_bytes = size - offset;
|
|
||||||
|
|
||||||
if (left_bytes < read_size) {
|
|
||||||
no_blocks = CEILING(left_bytes, ISO_BLOCKSIZE);
|
|
||||||
} else {
|
|
||||||
no_blocks = read_size / ISO_BLOCKSIZE;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (no_blocks == 0)
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
cur_block = offset / ISO_BLOCKSIZE;
|
if (offset_type(read_size) > remaining)
|
||||||
|
read_size = remaining;
|
||||||
|
|
||||||
readed = iso->SeekRead(ptr, lsn + cur_block, no_blocks);
|
auto r = buffer.Read();
|
||||||
|
|
||||||
if (readed != no_blocks * ISO_BLOCKSIZE)
|
if (r.empty()) {
|
||||||
throw FormatRuntimeError("error reading ISO file at lsn %lu",
|
/* the buffer is empty - read more data from the ISO file */
|
||||||
(unsigned long)cur_block);
|
|
||||||
|
|
||||||
if (left_bytes < read_size) {
|
assert(offset % ISO_BLOCKSIZE == 0);
|
||||||
readed = left_bytes;
|
|
||||||
|
const ScopeUnlock unlock(mutex);
|
||||||
|
|
||||||
|
const lsn_t read_lsn = lsn + offset / ISO_BLOCKSIZE;
|
||||||
|
|
||||||
|
if (read_size >= ISO_BLOCKSIZE) {
|
||||||
|
/* big read - read right into the caller's buffer */
|
||||||
|
|
||||||
|
auto nbytes = iso->SeekRead(ptr, read_lsn,
|
||||||
|
read_size / ISO_BLOCKSIZE);
|
||||||
|
if (nbytes <= 0)
|
||||||
|
throw std::runtime_error("Failed to read ISO9660 file");
|
||||||
|
|
||||||
|
offset += nbytes;
|
||||||
|
return nbytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* fill the buffer */
|
||||||
|
|
||||||
|
auto w = buffer.Write();
|
||||||
|
auto nbytes = iso->SeekRead(w.data, read_lsn,
|
||||||
|
w.size / ISO_BLOCKSIZE);
|
||||||
|
if (nbytes <= 0)
|
||||||
|
throw std::runtime_error("Failed to read ISO9660 file");
|
||||||
|
|
||||||
|
buffer.Append(nbytes);
|
||||||
|
|
||||||
|
r = buffer.Read();
|
||||||
}
|
}
|
||||||
|
|
||||||
offset += readed;
|
assert(!r.empty());
|
||||||
return readed;
|
|
||||||
|
size_t nbytes = std::min(read_size, r.size);
|
||||||
|
memcpy(ptr, r.data, nbytes);
|
||||||
|
buffer.Consume(nbytes);
|
||||||
|
offset += nbytes;
|
||||||
|
return nbytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
|
|
Loading…
Reference in New Issue