diff --git a/lib/kabufuda/BlockAllocationTable.cpp b/lib/kabufuda/BlockAllocationTable.cpp index ae700fb..d6b5b67 100644 --- a/lib/kabufuda/BlockAllocationTable.cpp +++ b/lib/kabufuda/BlockAllocationTable.cpp @@ -94,17 +94,19 @@ uint16_t BlockAllocationTable::allocateBlocks(uint16_t count, uint16_t maxBlocks if (freeBlock != 0xFFFF) { uint16_t tmpCount = count; - while ((count--) > 0) + while ((count--) > 0 && freeBlock != 0xFFFF) { m_map[(freeBlock - FSTBlocks)] = 0xFFFF; if (count != 0) { - m_map[(freeBlock - FSTBlocks)] = nextFreeBlock(maxBlocks - FSTBlocks, m_lastAllocated + 1); + m_map[(freeBlock - FSTBlocks)] = nextFreeBlock(maxBlocks - FSTBlocks, freeBlock + 1); freeBlock = m_map[(freeBlock - FSTBlocks)]; } - m_lastAllocated = freeBlock; } + if (freeBlock == 0xFFFF) + return 0xFFFF; + m_lastAllocated = freeBlock; m_freeBlocks -= tmpCount; } return firstBlock; diff --git a/lib/kabufuda/Card.cpp b/lib/kabufuda/Card.cpp index b57f305..dd3371b 100644 --- a/lib/kabufuda/Card.cpp +++ b/lib/kabufuda/Card.cpp @@ -186,6 +186,8 @@ ECardResult Card::createFile(const char* filename, size_t size, { handleOut.reset(); + if (size <= 0) + return ECardResult::FATAL_ERROR; if (strlen(filename) > 32) return ECardResult::NAMETOOLONG; if (m_currentDir->getFile(m_game, m_maker, filename)) @@ -198,7 +200,7 @@ ECardResult Card::createFile(const char* filename, size_t size, _updateDirAndBat(); File* f = m_currentDir->getFirstFreeFile(m_game, m_maker, filename); - uint16_t block = m_currentBat->allocateBlocks(neededBlocks, m_maxBlock); + uint16_t block = m_currentBat->allocateBlocks(neededBlocks, m_maxBlock - FSTBlocks); if (f && block != 0xFFFF) { f->m_modifiedTime = uint32_t(getGCTime()); diff --git a/test/main.cpp b/test/main.cpp index 5a046c6..d5af4aa 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -4,54 +4,27 @@ int main() { kabufuda::Card mc{_S("test.USA.raw"), "GM8E", "01"}; - if (!mc) - mc.format(kabufuda::ECardSlot::SlotA, kabufuda::ECardSize::Card2043Mb); + mc.format(kabufuda::ECardSlot::SlotA, kabufuda::ECardSize::Card2043Mb); uint64_t a = 0; mc.getSerial(a); - kabufuda::Card mc2{_S("test2.USA.raw"), "GM8E", "01"}; - if (!mc2) - mc2.format(kabufuda::ECardSlot::SlotA, kabufuda::ECardSize::Card2043Mb); - std::unique_ptr f; mc.openFile("MetroidPrime A", f); - if (!f) + for (uint32_t i = 0; i < 127; i++) { - mc.createFile("MetroidPrime A", kabufuda::BlockSize, f); + char name[32] = {'\0'}; + sprintf(name, "Metroid Prime %i", i); + kabufuda::ECardResult res = mc.createFile(name, kabufuda::BlockSize, f); + if (res == kabufuda::ECardResult::INSSPACE || res == kabufuda::ECardResult::NOFILE) + break; + mc.setPublic(f, true); mc.setCanCopy(f, true); mc.setCanMove(f, true); - } - - - if (f) - { - mc.setBannerFormat(f, kabufuda::EImageFormat::C8); - mc.setIconFormat(f, 0, kabufuda::EImageFormat::C8); - mc.setIconSpeed(f, 0, kabufuda::EAnimationSpeed::Middle); - - mc.seek(f, 4, kabufuda::SeekOrigin::Begin); - mc.setCommentAddress(f, 4); - - std::string comment("Metroid Prime PC Edition"); - mc.write(f, comment.c_str(), comment.length()); - mc.seek(f, 32 - comment.length(), kabufuda::SeekOrigin::Current); - comment = "Metroid Prime PC Is Cool"; - mc.write(f, comment.c_str(), comment.length()); - mc.seek(f, 32 - comment.length(), kabufuda::SeekOrigin::Current); - mc.setImageAddress(f, mc.tell(f)); - - if (mc.copyFileTo(f, mc2)) - printf("Copy succeeded!\n"); - else - printf("Copy failed...\n"); - - std::unique_ptr it = mc.firstFile(); - while (it) - { - printf("%.4s%.2s-%s\n", mc.gameId(it), mc.maker(it), mc.getFilename(it)); - it = mc.nextFile(it); - } + mc.setCommentAddress(f, 0); + mc.write(f, "Test\0", strlen("Test") + 1); + mc.seek(f, 32, kabufuda::SeekOrigin::Begin); + mc.write(f, "Test\0", strlen("Test") + 1); } return 0; }