diff --git a/src/activities/reader/EpubReaderActivity.cpp b/src/activities/reader/EpubReaderActivity.cpp index fff77935..1cbd84c3 100644 --- a/src/activities/reader/EpubReaderActivity.cpp +++ b/src/activities/reader/EpubReaderActivity.cpp @@ -846,8 +846,8 @@ bool EpubReaderActivity::drawCurrentPageToBuffer(const std::string& filePath, Gf if (Storage.openFileForRead("SLP", epub->getCachePath() + "/progress.bin", f)) { uint8_t data[6]; if (f.read(data, 6) == 6) { - spineIndex = data[0] | (data[1] << 8); - pageNumber = data[2] | (data[3] << 8); + spineIndex = (int)((uint32_t)data[0] | ((uint32_t)data[1] << 8)); + pageNumber = (int)((uint32_t)data[2] | ((uint32_t)data[3] << 8)); } f.close(); } diff --git a/src/activities/reader/TxtReaderActivity.cpp b/src/activities/reader/TxtReaderActivity.cpp index 157d3cbe..60ad66c1 100644 --- a/src/activities/reader/TxtReaderActivity.cpp +++ b/src/activities/reader/TxtReaderActivity.cpp @@ -20,6 +20,7 @@ constexpr size_t CHUNK_SIZE = 8 * 1024; // 8KB chunk for reading // Cache file magic and version constexpr uint32_t CACHE_MAGIC = 0x54585449; // "TXTI" constexpr uint8_t CACHE_VERSION = 2; // Increment when cache format changes +constexpr uint32_t MAX_CACHE_PAGES = 65535; // Sanity cap to prevent unbounded reserve() } // namespace void TxtReaderActivity::onEnter() { @@ -608,6 +609,24 @@ bool TxtReaderActivity::drawCurrentPageToBuffer(const std::string& filePath, Gfx return false; } + // Apply the reader orientation so margins match what the reader would produce + switch (SETTINGS.orientation) { + case CrossPointSettings::ORIENTATION::PORTRAIT: + renderer.setOrientation(GfxRenderer::Orientation::Portrait); + break; + case CrossPointSettings::ORIENTATION::LANDSCAPE_CW: + renderer.setOrientation(GfxRenderer::Orientation::LandscapeClockwise); + break; + case CrossPointSettings::ORIENTATION::INVERTED: + renderer.setOrientation(GfxRenderer::Orientation::PortraitInverted); + break; + case CrossPointSettings::ORIENTATION::LANDSCAPE_CCW: + renderer.setOrientation(GfxRenderer::Orientation::LandscapeCounterClockwise); + break; + default: + break; + } + // Compute layout values that match what initializeReader() produces const int fontId = SETTINGS.getReaderFontId(); const uint8_t screenMargin = SETTINGS.screenMargin; @@ -674,7 +693,7 @@ bool TxtReaderActivity::drawCurrentPageToBuffer(const std::string& filePath, Gfx uint32_t numPages; serialization::readPod(cacheFile, numPages); - if (numPages == 0) { + if (numPages == 0 || numPages > MAX_CACHE_PAGES) { cacheFile.close(); return false; } @@ -694,7 +713,7 @@ bool TxtReaderActivity::drawCurrentPageToBuffer(const std::string& filePath, Gfx if (Storage.openFileForRead("SLP", txt.getCachePath() + "/progress.bin", progFile)) { uint8_t data[4]; if (progFile.read(data, 4) == 4) { - savedPage = data[0] + (data[1] << 8); + savedPage = (int)((uint32_t)data[0] | ((uint32_t)data[1] << 8)); } progFile.close(); } diff --git a/src/activities/reader/XtcReaderActivity.cpp b/src/activities/reader/XtcReaderActivity.cpp index 052e2f8f..9522c3af 100644 --- a/src/activities/reader/XtcReaderActivity.cpp +++ b/src/activities/reader/XtcReaderActivity.cpp @@ -365,7 +365,7 @@ bool XtcReaderActivity::drawCurrentPageToBuffer(const std::string& filePath, Gfx if (Storage.openFileForRead("SLP", xtc.getCachePath() + "/progress.bin", f)) { uint8_t data[4]; if (f.read(data, 4) == 4) { - savedPage = data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24); + savedPage = (uint32_t)data[0] | ((uint32_t)data[1] << 8) | ((uint32_t)data[2] << 16) | ((uint32_t)data[3] << 24); } f.close(); }