From 30f1d8464c84a6f100ac5d63336e97cd705457fa Mon Sep 17 00:00:00 2001 From: M4x1m3 Date: Sat, 27 Feb 2021 23:42:06 +0100 Subject: [PATCH] [external] Fixed #492 --- apps/external/archive.cpp | 46 +++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/apps/external/archive.cpp b/apps/external/archive.cpp index 5636f0ef6..8f3d000bf 100644 --- a/apps/external/archive.cpp +++ b/apps/external/archive.cpp @@ -55,29 +55,43 @@ bool fileAtIndex(size_t index, File &entry) { * TAR files are comprised of a set of records aligned to 512 bytes boundary * followed by data. */ - while (index-- > 0) { + + for(;;) { + // Calculate the size size = 0; for (int i = 0; i < 11; i++) size = size * 8 + (tar->size[i] - '0'); + + // Check if we found our file. + if (index == 0) { + // If yes, check for sanity and for exam mode stuff + if (!isSane(tar) || isExamModeAndFileNotExecutable(tar)) { + return false; + } - // Move to the next TAR header. - unsigned stride = (sizeof(TarHeader) + size + 511); - stride = (stride >> 9) << 9; - tar = reinterpret_cast(reinterpret_cast(tar) + stride); + // File entry found, copy data out. + entry.name = tar->name; + entry.data = reinterpret_cast(tar) + sizeof(TarHeader); + entry.dataLength = size; + entry.isExecutable = (tar->mode[4] & 0x01) == 1; - // Sanity check. - if (!isSane(tar) || isExamModeAndFileNotExecutable(tar)) { - return false; + return true; + } else { + // move to the next TAR header. + unsigned stride = (sizeof(TarHeader) + size + 511); + stride = (stride >> 9) << 9; + tar = reinterpret_cast(reinterpret_cast(tar) + stride); + + // Sanity check. + if (!isSane(tar)) { + return false; + } } + index--; } - - // File entry found, copy data out. - entry.name = tar->name; - entry.data = reinterpret_cast(tar) + sizeof(TarHeader); - entry.dataLength = size; - entry.isExecutable = (tar->mode[4] & 0x01) == 1; - - return true; + + // Achievement unlock: How did we get there ? + return false; } extern "C" void (* const apiPointers[])(void);