Skip to content

Commit

Permalink
Correct sector count for > 2k block sizes.
Browse files Browse the repository at this point in the history
In case the iso size is not aligned to the block size.
  • Loading branch information
unknownbrackets committed Feb 3, 2015
1 parent 87e45b0 commit a882556
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/compress.h
Expand Up @@ -7,7 +7,7 @@

namespace maxcso {

static const char *VERSION = "1.4.3";
static const char *VERSION = "1.4.4";

struct Task;

Expand Down
6 changes: 5 additions & 1 deletion src/input.cpp
Expand Up @@ -79,7 +79,7 @@ void Input::DetectFormat() {
++csoBlockShift_;
}

const uint32_t sectors = static_cast<uint32_t>(size_ >> csoBlockShift_);
const uint32_t sectors = static_cast<uint32_t>(SizeAligned() >> csoBlockShift_);
csoIndex_ = new uint32_t[sectors + 1];
const unsigned int bytes = (sectors + 1) * sizeof(uint32_t);
const uv_buf_t buf = uv_buf_init(reinterpret_cast<char *>(csoIndex_), bytes);
Expand Down Expand Up @@ -383,4 +383,8 @@ bool Input::DecompressSectorLZ4(uint8_t *dst, const uint8_t *src, int dstSize, s
return true;
}

inline int64_t Input::SizeAligned() {
return size_ + csoBlockSize_ - 1;
}

};
1 change: 1 addition & 0 deletions src/input.h
Expand Up @@ -24,6 +24,7 @@ class Input {
void SetupCache(uint32_t minSize);
void ReadSector();
void EnqueueDecompressSector(uint8_t *src, uint32_t len, uint32_t offset, bool isLZ4);
inline int64_t SizeAligned();

enum FileType {
UNKNOWN,
Expand Down
12 changes: 8 additions & 4 deletions src/output.cpp
Expand Up @@ -44,7 +44,7 @@ void Output::SetFile(uv_file file, int64_t srcSize, uint32_t blockSize, CSOForma
++blockShift_;
}

const uint32_t sectors = static_cast<uint32_t>(srcSize >> blockShift_);
const uint32_t sectors = static_cast<uint32_t>((srcSize + blockSize_ - 1) >> blockShift_);
// Start after the header and index, which we'll fill in later.
index_ = new uint32_t[sectors + 1];
// Start after the end of the index data and header.
Expand Down Expand Up @@ -120,7 +120,7 @@ void Output::Enqueue(int64_t pos, uint8_t *buffer) {
if (blockSize_ != SECTOR_SIZE && pos + SECTOR_SIZE >= srcSize_) {
// Our src may not be aligned to the blockSize_, so this sector might never wake up.
// So let's send in some padding if needed.
const int64_t paddedSize = (srcSize_ + blockSize_ - 1) & ~static_cast<int64_t>(blockSize_ - 1);
const int64_t paddedSize = SrcSizeAligned() & ~static_cast<int64_t>(blockSize_ - 1);
for (int64_t padPos = srcSize_; padPos < paddedSize; padPos += SECTOR_SIZE) {
// Sector takes ownership, so we need a new one each time.
uint8_t *padBuffer = pool.Alloc();
Expand Down Expand Up @@ -220,7 +220,7 @@ void Output::HandleReadySector(Sector *sector) {
// If we're working on the last sectors, then the index is ready to write.
if (nextPos >= srcSize_) {
// Update the final index entry.
const int32_t s = static_cast<int32_t>(srcSize_ >> blockShift_);
const int32_t s = static_cast<int32_t>(SrcSizeAligned() >> blockShift_);
index_[s] = static_cast<int32_t>(dstPos >> indexShift_);

state_ |= STATE_INDEX_READY;
Expand Down Expand Up @@ -304,7 +304,7 @@ void Output::Flush() {
header->unused[0] = 0;
header->unused[1] = 0;

const uint32_t sectors = static_cast<uint32_t>(srcSize_ >> blockShift_);
const uint32_t sectors = static_cast<uint32_t>(SrcSizeAligned() >> blockShift_);

uv_buf_t bufs[2];
bufs[0] = uv_buf_init(reinterpret_cast<char *>(header), sizeof(CSOHeader));
Expand All @@ -328,4 +328,8 @@ void Output::CheckFinish() {
}
}

inline int64_t Output::SrcSizeAligned() {
return srcSize_ + blockSize_ - 1;
}

};
1 change: 1 addition & 0 deletions src/output.h
Expand Up @@ -31,6 +31,7 @@ class Output {
int32_t Align(int64_t &pos);
void HandleReadySector(Sector *sector);
bool ShouldCompress(int64_t pos, uint8_t *buffer);
inline int64_t SrcSizeAligned();

enum State {
STATE_INIT = 0x00,
Expand Down

0 comments on commit a882556

Please sign in to comment.