static int lua_ZipExtract(lua_State *L) { int argc = lua_gettop(L); if(argc != 2 && argc != 3) return luaL_error(L, "Argument error: ZIP.extract(zip, dirto, [password]) takes two or three arguments."); const char *password; const char *filetoextract = luaL_checkstring(L, 1); mkdir("ms0:/tempLPP", 0777); CopiaFile(filetoextract,"ms0:/tempLPP/temp.zip"); Zip *handle = ZipOpen("ms0:/tempLPP/temp.zip"); if(argc == 3) password = luaL_checkstring(L, 3); else password = NULL; int result = ZipExtract(handle, password); ZipClose(handle); const char *dir = luaL_checkstring(L, 2); sceIoMvdir("ms0:/tempLPP",dir); char ziptemp[256]; sprintf(ziptemp,"%s/temp.zip",dir); remove(ziptemp); lua_pushboolean(L, result); return 1; }
int main(int argc, char **argv) { ZipT *zip; int i; if (argc < 2) Usage(argv[0]); if ((zip = ZipOpen(argv[1]))) { if (argc == 2) { ZipList(zip); } else { for (i = 2; i < argc; i++) { RwOpsT *file; if ((file = ZipRead(zip, argv[i]))) { int size = IoSize(file); uint8_t *data = MemNew(size); IoRead(file, data, size); IoClose(file); fwrite(data, size, 1, stdout); MemUnref(data); } else { printf("%s: no such file!", argv[i]); } } } MemUnref(zip); } return 0; }
// ZIP Funcions static int lua_ZipOpen(lua_State *L) { int argc = lua_gettop(L); if(argc != 1) return luaL_error(L, "Argument error: ZIP.open(filename) takes one argument."); const char *filepath = luaL_checkstring(L, 1); if(!filepath) return luaL_error(L, "Argument error: ZIP.open(filename), argument must be a file path."); lua_gc(L, LUA_GCCOLLECT, 0); Zip *newZip = ZipOpen(filepath); if(!newZip) { lua_pushnil(L); return 1; } Zip **luaNewZip = pushZip(L); *luaNewZip = newZip; return 1; }
int BzipOpen(bool bootApp) { int nMemLen; // Zip name number nZipsFound = 0; // Haven't found zips yet nTotalSize = 0; if (szBzipName == NULL) { return 1; } BzipClose(); // Make sure nothing is open if(!bootApp) { // reset information strings BzipText.Reset(); BzipDetail.Reset(); } // Count the number of roms needed for (nRomCount = 0; ; nRomCount++) { if (BurnDrvGetRomInfo(NULL, nRomCount)) { break; } } if (nRomCount <= 0) { return 1; } // Create an array for holding lookups for each rom -> zip entries nMemLen = nRomCount * sizeof(struct RomFind); RomFind = (struct RomFind*)malloc(nMemLen); if (RomFind == NULL) { return 1; } memset(RomFind, 0, nMemLen); for (int z = 0; z < BZIP_MAX; z++) { char* szName = NULL; if (BurnDrvGetZipName(&szName, z)) { break; } for (int d = 0; d < DIRS_MAX; d++) { free(szBzipName[z]); szBzipName[z] = (TCHAR*)malloc(MAX_PATH * sizeof(TCHAR)); _stprintf(szBzipName[z], _T("%s%hs"), szAppRomPaths[d], szName); if (ZipOpen(TCHARToANSI(szBzipName[z], NULL, 0)) == 0) { // Open the rom zip file nZipsFound++; nCurrentZip = z; break; } } if (nCurrentZip >= 0) { if (!bootApp) { BzipText.Add(_T("Found %s;\n"), szBzipName[z]); } ZipGetList(&List, &nListCount); // Get the list of entries for (int i = 0; i < nRomCount; i++) { struct BurnRomInfo ri; int nFind; if (RomFind[i].nState == 1) { // Already found this and it's okay continue; } memset(&ri, 0, sizeof(ri)); nFind = FindRom(i); if (nFind < 0) { // Couldn't find this rom at all continue; } RomFind[i].nZip = z; // Remember which zip file it is in RomFind[i].nPos = nFind; RomFind[i].nState = 1; // Set to found okay BurnDrvGetRomInfo(&ri, i); // Get info about the rom if ((ri.nType & BRF_OPT) == 0 && (ri.nType & BRF_NODUMP) == 0) { nTotalSize += ri.nLen; } if (List[nFind].nLen == ri.nLen) { if (ri.nCrc) { // If we know the CRC if (List[nFind].nCrc != ri.nCrc) { // Length okay, but CRC wrong RomFind[i].nState = 2; } } } else { if (List[nFind].nLen < ri.nLen) { RomFind[i].nState = 3; // Too small } else { RomFind[i].nState = 4; // Too big } } if (!bootApp) { if (RomFind[i].nState != 1) { RomDescribe(&BzipDetail, &ri); if (RomFind[i].nState == 2) { BzipDetail.Add(_T("%hs has a CRC of %.8X. (It should be %.8X.)\n"), GetFilenameA(List[nFind].szName), List[nFind].nCrc, ri.nCrc); } if (RomFind[i].nState == 3) { BzipDetail.Add(_T("%hs is %dk which is incomplete. (It should be %dkB.)\n"), GetFilenameA(List[nFind].szName), List[nFind].nLen >> 10, ri.nLen >> 10); } if (RomFind[i].nState == 4) { BzipDetail.Add(_T("%hs is %dk which is too big. (It should be %dkB.)\n"), GetFilenameA(List[nFind].szName), List[nFind].nLen >> 10, ri.nLen >> 10); } } } }
static int __cdecl BzipBurnLoadRom(unsigned char* Dest, int* pnWrote, int i) { #if defined (BUILD_WIN32) MSG Msg; #endif struct BurnRomInfo ri; int nWantZip = 0; TCHAR szText[128]; char* pszRomName = NULL; int nRet = 0; if (i < 0 || i >= nRomCount) { return 1; } ri.nLen = 0; BurnDrvGetRomInfo(&ri, i); // Get info // show what we're doing BurnDrvGetRomName(&pszRomName, i, 0); if (pszRomName == NULL) { pszRomName = "unknown"; } _stprintf(szText, _T("Loading")); if (ri.nType & (BRF_PRG | BRF_GRA | BRF_SND | BRF_BIOS)) { if (ri.nType & BRF_BIOS) { _stprintf (szText + _tcslen(szText), _T(" %s"), _T("BIOS ")); } if (ri.nType & BRF_PRG) { _stprintf (szText + _tcslen(szText), _T(" %s"), _T("program ")); } if (ri.nType & BRF_GRA) { _stprintf (szText + _tcslen(szText), _T(" %s"), _T("graphics ")); } if (ri.nType & BRF_SND) { _stprintf (szText + _tcslen(szText), _T(" %s"), _T("sound ")); } _stprintf(szText + _tcslen(szText), _T("(%hs)..."), pszRomName); } else { _stprintf(szText + _tcslen(szText), _T(" %hs..."), pszRomName); } ProgressUpdateBurner(ri.nLen ? 1.0 / ((double)nTotalSize / ri.nLen) : 0, szText, 0); #if defined (BUILD_WIN32) // Check for messages: while (PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE)) { DispatchMessage(&Msg); } #endif if (RomFind[i].nState == 0) { // Rom not found in zip at all TCHAR szTemp[128] = _T(""); _stprintf(szTemp, "%s (not found)\n",szText); fprintf(stderr, szTemp); AppError(szTemp, 1); return 1; } nWantZip = RomFind[i].nZip; // Which zip file it is in if (nCurrentZip != nWantZip) { // If we haven't got the right zip file currently open ZipClose(); nCurrentZip = -1; if (ZipOpen(TCHARToANSI(szBzipName[nWantZip], NULL, 0))) { return 1; } nCurrentZip = nWantZip; } // Read in file and return how many bytes we read if (ZipLoadFile(Dest, ri.nLen, pnWrote, RomFind[i].nPos)) { // Error loading from the zip file TCHAR szTemp[128] = _T(""); _stprintf(szTemp, _T("%s reading %.30hs from %.30s"), nRet == 2 ? _T("CRC error") : _T("Error"), pszRomName, GetFilenameW(szBzipName[nCurrentZip])); fprintf(stderr, szTemp); AppError(szTemp, 1); return 1; } fprintf(stderr, "%s (OK)\n", szText); return 0; }
int BzipOpen(bool bootApp) { int nMemLen; // Zip name number nTotalSize = 0; nBzipError = 0; if (szBzipName == NULL) return 1; BzipClose(); // Make sure nothing is open // Count the number of roms needed for (nRomCount = 0; ; nRomCount++) { if (BurnDrvGetRomInfo(NULL, nRomCount)) { break; } } if (nRomCount <= 0) { return 1; } // Create an array for holding lookups for each rom -> zip entries nMemLen = nRomCount * sizeof(struct RomFind); RomFind = (struct RomFind*)malloc(nMemLen); if (RomFind == NULL) { return 1; } memset(RomFind, 0, nMemLen); for (int y = 0; y < BZIP_MAX; y++) { free(szBzipName[y]); szBzipName[y] = NULL; } // Locate each zip file for (int y = 0, z = 0; y < BZIP_MAX && z < BZIP_MAX; y++) { char* szName = NULL; bool bFound = false; if (BurnDrvGetZipName(&szName, y)) { break; } for (int d = 0; d < DIRS_MAX; d++) { char szFullName[MAX_PATH]; sprintf(szFullName, "%s%s", szAppRomPaths[d], szName); if (ZipOpen(szFullName) == 0) { // Open the rom zip file ZipClose(); bFound = true; szBzipName[z] = (TCHAR*)malloc(MAX_PATH * sizeof(TCHAR)); strcpy(szBzipName[z], szFullName); z++; } } } // Locate the ROM data in the zip files for (int z = 0; z < BZIP_MAX; z++) { if (szBzipName[z] == NULL) { continue; } if (ZipOpen(szBzipName[z]) == 0) { // Open the rom zip file nCurrentZip = z; ZipGetList(&List, &nListCount); // Get the list of entries for (int i = 0; i < nRomCount; i++) { struct BurnRomInfo ri; int nFind; if (RomFind[i].nState == 1) { // Already found this and it's okay continue; } memset(&ri, 0, sizeof(ri)); nFind = FindRom(i); if (nFind < 0) { // Couldn't find this rom at all continue; } RomFind[i].nZip = z; // Remember which zip file it is in RomFind[i].nPos = nFind; RomFind[i].nState = 1; // Set to found okay BurnDrvGetRomInfo(&ri, i); // Get info about the rom if ((ri.nType & BRF_OPT) == 0) { nTotalSize += ri.nLen; } if (List[nFind].nLen == ri.nLen) { if (ri.nCrc) { // If we know the CRC if (List[nFind].nCrc != ri.nCrc) { // Length okay, but CRC wrong //if (ndisablecrc == 1) { RomFind[i].nState = 2; //} else { // RomFind[i].nState = 1; //} } } } else { //if (nLoadMenuShowX & IDC_CHECKCRC) { // ri.nLen = List[nFind].nLen; // RomFind[i].nState = 1; //} else { if (List[nFind].nLen < ri.nLen) { RomFind[i].nState = 3; // Too small } else { RomFind[i].nState = 4; // Too big } } } } BzipListFree(); } ZipClose(); // Close the last zip file if open nCurrentZip = -1; } if (!bootApp) { // Check the roms to see if the code, graphics etc are complete CheckRoms(); BurnExtLoadRom = BzipBurnLoadRom; // Okay to call our function to load each rom } else { return CheckRomsBoot(); } return 0; }
static int __cdecl BzipBurnLoadRom(unsigned char* Dest, int* pnWrote, int i) { struct BurnRomInfo ri; int nWantZip = 0; TCHAR szText[128]; char* pszRomName = NULL; //int nRet = 0; if (i < 0 || i >= nRomCount) { return 1; } ri.nLen = 0; BurnDrvGetRomInfo(&ri, i); // Get info // show what we're doing BurnDrvGetRomName(&pszRomName, i, 0); if (pszRomName == NULL) { pszRomName = "unknown"; } sprintf(szText, "%-12s ... %4dKb", pszRomName, ri.nLen/1024 ); if (ri.nType & (BRF_PRG | BRF_GRA | BRF_SND | BRF_BIOS)) { if (ri.nType & BRF_BIOS) { sprintf (szText + strlen(szText), " %s", "BIOS "); } if (ri.nType & BRF_PRG) { sprintf (szText + strlen(szText), " %s", "program "); } if (ri.nType & BRF_GRA) { sprintf (szText + strlen(szText), " %s", "graphics "); } if (ri.nType & BRF_SND) { sprintf (szText + strlen(szText), " %s", "sound "); } } ProgressUpdateBurner(ri.nLen ? 1.0 / ((double)nTotalSize / ri.nLen) : 0, szText, 0); // FIXME: eliminate later in favor of ProgressUpdateBurner show_rom_loading_text(szText, ri.nLen, nTotalSize); if (RomFind[i].nState == 0) { // Rom not found in zip at all TCHAR szTemp[128] = _T(""); _stprintf(szTemp, "%s (not found)\n",szText); fprintf(stderr,szTemp); ProgressError(szTemp, 1); return 1; } nWantZip = RomFind[i].nZip; // Which zip file it is in if (nCurrentZip != nWantZip) { // If we haven't got the right zip file currently open ZipClose(); nCurrentZip = -1; if (ZipOpen(szBzipName[nWantZip])) { printf("%s (open zip err: %s)\n", szText, szBzipName[nWantZip]); return 1; } nCurrentZip = nWantZip; } // Read in file and return how many bytes we read if (ZipLoadFile(Dest, ri.nLen, pnWrote, RomFind[i].nPos)) { printf("%s (ERR)\n", szText); return 1; } printf("%s (OK)\n", szText); return 0; }
int BzipOpen(bool bootApp) { int nMemLen; // Zip name number nTotalSize = 0; nBzipError = 0; if (szBzipName == NULL) { return 1; } BzipClose(); // Make sure nothing is open // Count the number of roms needed for (nRomCount = 0; ; nRomCount++) { if (BurnDrvGetRomInfo(NULL, nRomCount)) { break; } } if (nRomCount <= 0) { return 2; } // Create an array for holding lookups for each rom -> zip entries nMemLen = nRomCount * sizeof(struct RomFind); RomFind = (struct RomFind*)malloc(nMemLen); if (RomFind == NULL) { return 3; } memset(RomFind, 0, nMemLen); for (int y = 0; y < BZIP_MAX; y++) { free(szBzipName[y]); szBzipName[y] = NULL; } // Locate each zip file for (int y = 0, z = 0; y < BZIP_MAX && z < BZIP_MAX; y++) { char* szName = NULL; bool bFound = false; if (BurnDrvGetZipName(&szName, y)) { break; } for (int d = 0; d < DIRS_MAX; d++) { if ( strlen(szAppRomPaths[d]) > 0 ){ TCHAR szFullName[MAX_PATH]; _stprintf(szFullName, _T("%s%hs"), szAppRomPaths[d], szName); if (ZipOpen(szFullName) == 0) { // Open the rom zip file ZipClose(); bFound = true; szBzipName[z] = (TCHAR*)malloc(MAX_PATH * sizeof(TCHAR)); _tcscpy(szBzipName[z], szFullName); if (!bootApp) { //FBAPopupAddText(PUF_TEXT_DEFAULT, MAKEINTRESOURCE(IDS_ERR_LOAD_FOUND), szName, szBzipName[z]); } z++; // Look further in the last path specified, so you can put files with ROMs // used only by FB Alpha there without causing problems with dat files if (d < DIRS_MAX - 2) { d = DIRS_MAX - 2; } else { if (d >= DIRS_MAX - 1) { break; } } } } } if (!bootApp && !bFound) { //FBAPopupAddText(PUF_TEXT_DEFAULT, MAKEINTRESOURCE(IDS_ERR_LOAD_NOTFOUND), szName); } } if (!bootApp) { //FBAPopupAddText(PUF_TEXT_DEFAULT, _T("\n")); } // Locate the ROM data in the zip files for (int z = 0; z < BZIP_MAX; z++) { if (szBzipName[z] == NULL) { continue; } if (ZipOpen(szBzipName[z]) == 0) { // Open the rom zip file nCurrentZip = z; ZipGetList(&List, &nListCount); // Get the list of entries for (int i = 0; i < nRomCount; i++) { struct BurnRomInfo ri; int nFind; if (RomFind[i].nState == 1) { // Already found this and it's okay continue; } memset(&ri, 0, sizeof(ri)); nFind = FindRom(i); if (nFind < 0) { // Couldn't find this rom at all continue; } RomFind[i].nZip = z; // Remember which zip file it is in RomFind[i].nPos = nFind; RomFind[i].nState = 1; // Set to found okay BurnDrvGetRomInfo(&ri, i); // Get info about the rom if ((ri.nType & BRF_OPT) == 0 && (ri.nType & BRF_NODUMP) == 0) { nTotalSize += ri.nLen; } if (List[nFind].nLen == ri.nLen) { if (ri.nCrc) { // If we know the CRC if (List[nFind].nCrc != ri.nCrc) { // Length okay, but CRC wrong RomFind[i].nState = 2; } } } else { if (List[nFind].nLen < ri.nLen) { RomFind[i].nState = 3; // Too small } else { RomFind[i].nState = 4; // Too big } } if (!bootApp) { if (RomFind[i].nState != 1) { RomDescribe(&ri); if (RomFind[i].nState == 2) { // FBAPopupAddText(PUF_TEXT_DEFAULT, MAKEINTRESOURCE(IDS_ERR_LOAD_CRC), GetFilenameA(List[nFind].szName), List[nFind].nCrc, ri.nCrc); } if (RomFind[i].nState == 3) { // FBAPopupAddText(PUF_TEXT_DEFAULT, MAKEINTRESOURCE(IDS_ERR_LOAD_SMALL), GetFilenameA(List[nFind].szName), List[nFind].nLen >> 10, ri.nLen >> 10); } if (RomFind[i].nState == 4) { // FBAPopupAddText(PUF_TEXT_DEFAULT, MAKEINTRESOURCE(IDS_ERR_LOAD_LARGE), GetFilenameA(List[nFind].szName), List[nFind].nLen >> 10, ri.nLen >> 10); } } } } BzipListFree(); } ZipClose(); // Close the last zip file if open nCurrentZip = -1; } if (!bootApp) { // Check the roms to see if the code, graphics etc are complete CheckRoms(); if (nBzipError & 0x2000) { if (!(nBzipError & 0x0F0F)) { // FBAPopupAddText(PUF_TEXT_DEFAULT, MAKEINTRESOURCE(IDS_ERR_LOAD_OK)); } else { // FBAPopupAddText(PUF_TEXT_DEFAULT, _T("\n")); // FBAPopupAddText(PUF_TEXT_DEFAULT, MAKEINTRESOURCE(IDS_ERR_LOAD_PROBLEM)); } if (nBzipError & 0x0101) { // FBAPopupAddText(PUF_TEXT_DEFAULT, _T("\n ") _T(SEPERATOR_1)); if (nBzipError & 0x0001) { // FBAPopupAddText(PUF_TEXT_DEFAULT, MAKEINTRESOURCE(IDS_ERR_LOAD_ESS_MISS)); } else { // FBAPopupAddText(PUF_TEXT_DEFAULT, MAKEINTRESOURCE(IDS_ERR_LOAD_ESS_BAD)); } } if (nBzipError & 0x0202) { // FBAPopupAddText(PUF_TEXT_DEFAULT, _T("\n ") _T(SEPERATOR_1)); // FBAPopupAddText(PUF_TEXT_DEFAULT, MAKEINTRESOURCE(IDS_ERR_LOAD_DET_PRG)); if (nBzipError & 0x0002) { // FBAPopupAddText(PUF_TEXT_DEFAULT, MAKEINTRESOURCE(IDS_ERR_LOAD_DATA_MISS)); } else { // FBAPopupAddText(PUF_TEXT_DEFAULT, MAKEINTRESOURCE(IDS_ERR_LOAD_DATA_BAD)); } } if (nBzipError & 0x0404) { // FBAPopupAddText(PUF_TEXT_DEFAULT, _T("\n ") _T(SEPERATOR_1)); // FBAPopupAddText(PUF_TEXT_DEFAULT, MAKEINTRESOURCE(IDS_ERR_LOAD_DET_GRA)); if (nBzipError & 0x0004) { // FBAPopupAddText(PUF_TEXT_DEFAULT, MAKEINTRESOURCE(IDS_ERR_LOAD_DATA_MISS)); } else { // FBAPopupAddText(PUF_TEXT_DEFAULT, MAKEINTRESOURCE(IDS_ERR_LOAD_DATA_BAD)); } } if (nBzipError & 0x0808) { // FBAPopupAddText(PUF_TEXT_DEFAULT, _T("\n ") _T(SEPERATOR_1)); // FBAPopupAddText(PUF_TEXT_DEFAULT, MAKEINTRESOURCE(IDS_ERR_LOAD_DET_SND)); if (nBzipError & 0x0008) { // FBAPopupAddText(PUF_TEXT_DEFAULT, MAKEINTRESOURCE(IDS_ERR_LOAD_DATA_MISS)); } else { // FBAPopupAddText(PUF_TEXT_DEFAULT, MAKEINTRESOURCE(IDS_ERR_LOAD_DATA_BAD)); } } // Catch non-categorised ROMs if ((nBzipError & 0x0F0F) == 0) { if (nBzipError & 0x0010) { // FBAPopupAddText(PUF_TEXT_DEFAULT, _T("\n ") _T(SEPERATOR_1)); if (nBzipError & 0x1000) { // FBAPopupAddText(PUF_TEXT_DEFAULT, MAKEINTRESOURCE(IDS_ERR_LOAD_DATA_MISS)); } else { // FBAPopupAddText(PUF_TEXT_DEFAULT, MAKEINTRESOURCE(IDS_ERR_LOAD_DATA_BAD)); } } } } else { // FBAPopupAddText(PUF_TEXT_DEFAULT, _T("\n")); // FBAPopupAddText(PUF_TEXT_DEFAULT, MAKEINTRESOURCE(IDS_ERR_LOAD_NODATA)); } BurnExtLoadRom = BzipBurnLoadRom; // Okay to call our function to load each rom } else { return CheckRomsBoot(); } return 0; }
static int BzipBurnLoadRom(unsigned char* Dest, int* pnWrote, int i) { struct BurnRomInfo ri; int nWantZip = 0; TCHAR szText[128]; char* pszRomName = NULL; int nRet = 0; if (i < 0 || i >= nRomCount) { return 1; } ri.nLen = 0; BurnDrvGetRomInfo(&ri, i); // Get info // show what we're doing BurnDrvGetRomName(&pszRomName, i, 0); if (pszRomName == NULL) { pszRomName = "unknown"; } _stprintf(szText, _T("Loading")); if (ri.nType & (BRF_PRG | BRF_GRA | BRF_SND | BRF_BIOS)) { if (ri.nType & BRF_BIOS) { _stprintf (szText + _tcslen(szText), _T(" %s"), _T("BIOS ")); } if (ri.nType & BRF_PRG) { _stprintf (szText + _tcslen(szText), _T(" %s"), _T("program ")); } if (ri.nType & BRF_GRA) { _stprintf (szText + _tcslen(szText), _T(" %s"), _T("graphics ")); } if (ri.nType & BRF_SND) { _stprintf (szText + _tcslen(szText), _T(" %s"), _T("sound ")); } _stprintf(szText + _tcslen(szText), _T("(%hs)..."), pszRomName); } else { _stprintf(szText + _tcslen(szText), _T(" %hs..."), pszRomName); } //ProgressUpdateBurner(ri.nLen ? 1.0 / ((double)nTotalSize / ri.nLen) : 0, szText, 0); if (RomFind[i].nState == 0) { // Rom not found in zip at all return 1; } nWantZip = RomFind[i].nZip; // Which zip file it is in if (nCurrentZip != nWantZip) { // If we haven't got the right zip file currently open ZipClose(); nCurrentZip = -1; if (ZipOpen(szBzipName[nWantZip])) { return 1; } nCurrentZip = nWantZip; } // Read in file and return how many bytes we read if (ZipLoadFile(Dest, ri.nLen, pnWrote, RomFind[i].nPos)) { // Error loading from the zip file // FBAPopupAddText(PUF_TEXT_DEFAULT, MAKEINTRESOURCE(nRet == 2 ? IDS_ERR_LOAD_DISK_CRC : IDS_ERR_LOAD_DISK), pszRomName, GetFilenameW(szBzipName[nCurrentZip])); // FBAPopupDisplay(PUF_TYPE_WARNING); return 1; } return 0; }
// Load one file directly, added by regret INT32 __cdecl ZipLoadOneFile(const char* arcName, const char* fileName, void** Dest, INT32* pnWrote) { if (ZipOpen(arcName)) { return 1; } unz_global_info ZipGlobalInfo; memset(&ZipGlobalInfo, 0, sizeof(ZipGlobalInfo)); unzGetGlobalInfo(Zip, &ZipGlobalInfo); INT32 nListLen = ZipGlobalInfo.number_entry; if (nListLen <= 0) { ZipClose(); return 1; } INT32 nRet = unzGoToFirstFile(Zip); if (nRet != UNZ_OK) { unzClose(Zip); return 1; } unz_file_info FileInfo; memset(&FileInfo, 0, sizeof(FileInfo)); if (fileName != NULL) { // Step through all of the files, until we get to the end INT32 nNextRet = 0; char szName[MAX_PATH] = ""; for (nCurrFile = 0, nNextRet = UNZ_OK; nCurrFile < nListLen && nNextRet == UNZ_OK; nCurrFile++, nNextRet = unzGoToNextFile(Zip)) { nRet = unzGetCurrentFileInfo(Zip, &FileInfo, szName, MAX_PATH, NULL, 0, NULL, 0); if (nRet != UNZ_OK) continue; if (!strcmp(szName, fileName)) { break; } } if (nCurrFile == nListLen) { ZipClose(); return 1; // didn't find } } else { nRet = unzGetCurrentFileInfo(Zip, &FileInfo, NULL, 0, NULL, 0, NULL, 0); if (nRet != UNZ_OK) { ZipClose(); return 1; } } // Extract file nRet = unzOpenCurrentFile(Zip); if (nRet != UNZ_OK) { unzCloseCurrentFile(Zip); ZipClose(); return 1; } if (*Dest == NULL) { *Dest = (UINT8*)malloc(FileInfo.uncompressed_size); if (!*Dest) { unzCloseCurrentFile(Zip); ZipClose(); return 1; } } nRet = unzReadCurrentFile(Zip, *Dest, FileInfo.uncompressed_size); // Return how many bytes were copied if (nRet >= 0 && pnWrote != NULL) *pnWrote = nRet; nRet = unzCloseCurrentFile(Zip); ZipClose(); if (nRet == UNZ_CRCERROR) { if (*Dest) { free(*Dest); } return 2; } if (nRet != UNZ_OK) { if (*Dest) { free(*Dest); } return 1; } return 0; }
bool installTTP(char* path, u8 mediatype) { // Install a TTP file. (needs libzip and installCIA) Result res; FS_Archive archive = {ARCHIVE_SDMC, {PATH_EMPTY, 0, 0}}; FSUSER_OpenArchive(&archive); FSUSER_DeleteDirectoryRecursively(archive, fsMakePath(PATH_ASCII, "/tmp/cias")); FSUSER_CreateDirectory(archive, fsMakePath(PATH_ASCII, "/tmp/cias"), 0); FILE* ttp = fopen(path, "rb"); FILE* tmp = fopen("/tmp/cias/ttp.tmp", "wb"); u32 titlesAmount; AM_GetTitleCount(mediatype, &titlesAmount); u64* titleIDs = malloc(sizeof(u64) * titlesAmount); AM_GetTitleIdList(mediatype, titlesAmount, titleIDs); u32 size; fseek(ttp, 0x19, SEEK_SET); fread(&size, 0x4, 1, ttp); fseek(ttp, 0x1D, SEEK_SET); u32 blockAmount = size / 0x160000; // Finds how many blocks of 4MB you have in the file u32 i; char* block = malloc(0x160000); for (i = 0; i < blockAmount; i++) { fread(block, 1, 0x160000, ttp); fwrite(block, 1, 0x160000, tmp); } if (size % 0x160000 != 0) { fread(block, 1, size-0x160000*blockAmount, ttp); fwrite(block, 1, size-0x160000*blockAmount, tmp); } free(block); fclose(ttp); fclose(tmp); FSUSER_DeleteDirectoryRecursively(archive, fsMakePath(PATH_ASCII, "/tmp/cias")); FSUSER_CreateDirectory(archive, fsMakePath(PATH_ASCII, "/tmp/cias"), 0); Zip *zipHandle = ZipOpen("/tmp/cias/ttp.tmp"); ZipExtract(zipHandle, NULL); ZipClose(zipHandle); Handle ciaDir; FS_Archive fsarchive; u32 actualAmount; FS_DirectoryEntry* entries; FSUSER_DeleteFile(archive, fsMakePath(PATH_ASCII, "/tmp/cias/ttp.tmp")); res = FSUSER_OpenDirectory(&ciaDir, fsarchive, fsMakePath(PATH_ASCII, "/tmp/cias")); if (res != 0) { free(titleIDs); return false; } entries = malloc(256 * sizeof(FS_DirectoryEntry)); res = FSDIR_Read(ciaDir, &actualAmount, 256, entries); if (res != 0) { free(titleIDs); return false; } char* ciaPath; Handle ciaFileHandle; AM_TitleEntry ciaInfo; for (i = 0; i < actualAmount; i++) { ciaPath = malloc(14 + strlen(entries[i].shortName)); strcpy(ciaPath, "/tmp/cias/"); strcat(ciaPath, entries[i].shortName); strcat(ciaPath, ".cia"); FSUSER_OpenFile(&ciaFileHandle, archive, fsMakePath(PATH_ASCII, ciaPath), FS_OPEN_READ, 0); AM_GetCiaFileInfo(mediatype, &ciaInfo, ciaFileHandle); FSFILE_Close(ciaFileHandle); if (ciaInfo.titleID == 0x0004013800000002LL || ciaInfo.titleID == 0x0004013820000002LL) { if (!installCIA(ciaPath, mediatype, titleIDs, entries[i].shortName)) if (!installCIA(ciaPath, mediatype, titleIDs, entries[i].shortName)) // Tries to install the CIA 3 times then give up. If it has to give up, that probably means brick. if (!installCIA(ciaPath, mediatype, titleIDs, entries[i].shortName)) return false; break; } free(ciaPath); } for (i = 0; i < actualAmount; i++) { ciaPath = malloc(14 + strlen(entries[i].shortName)); strcpy(ciaPath, "/tmp/cias/"); strcat(ciaPath, entries[i].shortName); strcat(ciaPath, ".cia"); if (!installCIA(ciaPath, mediatype, titleIDs, entries[i].shortName)) if (!installCIA(ciaPath, mediatype, titleIDs, entries[i].shortName)) // Tries to install the CIA 3 times then give up. If it has to give up, that probably means brick. installCIA(ciaPath, mediatype, titleIDs, entries[i].shortName); free(ciaPath); } FSUSER_DeleteDirectoryRecursively(archive, fsMakePath(PATH_ASCII, "/tmp/cias")); FSUSER_CloseArchive(&archive); free(titleIDs); return true; }