__int64 CSimpleFileStream::GetSize() { __int64 result; GetFileSize64(m_fileHandle, &result); return result; }
fs_entry* fs_GetEntry(fs_DIR *dp) { // Directory structs struct fs_dirent *tmp_entry; fs_DIR *tmp_dptr; u32 namlen = 0; //printf("get api dir entry from dir ptr\n"); tmp_entry = fs_readdir(dp); //printf("if null, return\n"); if(!tmp_entry) return NULL; #ifdef _WIN32 namlen = tmp_entry->d_namlen; #else namlen = strlen(tmp_entry->d_name); #endif //printf("allocate memory for entry\n"); fs_entry *entry = malloc(sizeof(fs_entry)); memset(entry,0,sizeof(fs_entry)); //Copy FS compatible Entry name entry->fs_name = malloc(sizeof(fs_char)*(namlen+1)); memset(entry->fs_name,0,sizeof(fs_char)*(namlen+1)); memcpy(entry->fs_name,tmp_entry->d_name,sizeof(fs_char)*namlen); // Convert Entry name into RomFS u16 char (windows wchar_t, thanks Nintendo) #if _WIN32 str_u16_to_u16(&entry->name,&entry->name_len,tmp_entry->d_name,namlen); #else str_utf8_to_u16(&entry->name,&entry->name_len,(u8*)tmp_entry->d_name,namlen); #endif //printf("get dir entry from dir ptr to check if dir\n"); tmp_dptr = fs_opendir(entry->fs_name); if(tmp_dptr) { //printf("is dir\n"); fs_closedir(tmp_dptr); entry->IsDir = true; entry->size = 0; entry->fp = NULL; } else // Open file if it is a file { entry->IsDir = false; #ifdef _WIN32 entry->size = wGetFileSize64(entry->fs_name); entry->fp = _wfopen(entry->fs_name,L"rb"); #else entry->size = GetFileSize64(entry->fs_name); entry->fp = fopen(entry->fs_name,"rb"); #endif } //printf("fs_GetEntry() return\n"); return entry; }
bool CSimpleFileStream::IsEndOfStream() { __int64 currentPos; GetFilePointer64(m_fileHandle, ¤tPos); __int64 size; GetFileSize64(m_fileHandle, &size); return (currentPos == size); }
//IO Misc u8* ImportFile(char *file, u64 size) { u64 fsize = GetFileSize64(file); if(size > 0 && size != fsize){ fprintf(stderr,"[!] %s has an invalid size (0x%"PRIx64")\n",file, fsize); return NULL; } u8 *data = (u8*)calloc(1,fsize); if(!data){ fprintf(stderr,"[!] Not enough memory\n"); return NULL; } FILE *fp = fopen(file,"rb"); fread(data,fsize,1,fp); fclose(fp); return data; }
int ProcessCverDataForCci(cci_settings *set) { u64 tmdSize,tmdOffset; u64 dataSize = GetFileSize64(set->options.cverDataPath); FILE *data = fopen(set->options.cverDataPath,"rb"); if(set->options.cverDataType == CVER_DTYPE_CIA){ cia_hdr *ciaHdr = calloc(1,sizeof(cia_hdr)); ReadFile64(ciaHdr,sizeof(cia_hdr),0,data); tmdSize = GetCiaTmdSize(ciaHdr); tmdOffset = GetCiaTmdOffset(ciaHdr); free(ciaHdr); } else{ tmdSize = dataSize; tmdOffset = 0; } u8 *tmd = calloc(1,tmdSize); ReadFile64(tmd,tmdSize,tmdOffset,data); fclose(data); tmd_hdr *tmdHdr = GetTmdHdr(tmd); if(!tmdHdr){ fprintf(stderr,"[CCI ERROR] Corrupt cver TMD\n"); free(tmd); return FAILED_TO_IMPORT_FILE; } set->options.tmdHdr = calloc(1,sizeof(tmd_hdr)); memcpy(set->options.tmdHdr,tmdHdr,sizeof(tmd_hdr)); free(tmd); return 0; }
// Attempt to mmap a file for read-only access. void MmapFileMap(MemoryMappedFile* self, const char *fn) { TimingScope timing_scope(&g_Stats.m_MmapCalls, &g_Stats.m_MmapTimeCycles); const DWORD desired_access = GENERIC_READ; const DWORD share_mode = FILE_SHARE_READ; const DWORD creation_disposition = OPEN_EXISTING; const DWORD flags = FILE_ATTRIBUTE_NORMAL; HANDLE file = CreateFileA(fn, desired_access, share_mode, NULL, creation_disposition, flags, NULL); if (INVALID_HANDLE_VALUE == file) { return; } const uint64_t file_size = GetFileSize64(file); HANDLE mapping = CreateFileMapping(file, NULL, PAGE_READONLY, DWORD(file_size >> 32), DWORD(file_size), NULL); if (nullptr == mapping) { Log(kError, "CreateFileMapping() failed: %u", GetLastError()); CloseHandle(file); return; } void* address = MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, DWORD(file_size)); if (nullptr == address) { Log(kError, "MapViewOfFile() failed: %u", GetLastError()); CloseHandle(mapping); CloseHandle(file); return; } self->m_Address = address; self->m_Size = (size_t) file_size; self->m_SysData[0] = (uintptr_t) file; self->m_SysData[1] = (uintptr_t) mapping; }
int64 CSrFile::GetFileSize64 (void) { int64 Result; GetFileSize64(Result); return (Result); }
BOOL MakeSelfExtractingPackage (HWND hwndDlg, char *szDestDir) { int i, x; unsigned char inputFile [TC_MAX_PATH]; unsigned char outputFile [TC_MAX_PATH]; unsigned char szTmpFilePath [TC_MAX_PATH]; unsigned char szTmp32bit [4] = {0}; unsigned char *szTmp32bitPtr = szTmp32bit; unsigned char *buffer = NULL, *compressedBuffer = NULL; unsigned char *bufIndex = NULL; char tmpStr [2048]; int bufLen = 0, compressedDataLen = 0, uncompressedDataLen = 0; x = strlen (szDestDir); if (x < 2) return FALSE; if (szDestDir[x - 1] != '\\') strcat (szDestDir, "\\"); GetModuleFileName (NULL, inputFile, sizeof (inputFile)); strcpy (outputFile, szDestDir); strncat (outputFile, OutputPackageFile, sizeof (outputFile) - strlen (outputFile) - 1); // Clone 'TrueCrypt Setup.exe' to create the base of the new self-extracting archive if (!TCCopyFile (inputFile, outputFile)) { handleWin32Error (hwndDlg); PkgError ("Cannot copy 'TrueCrypt Setup.exe' to the package"); return FALSE; } // Determine the buffer size needed for all the files and meta data and check if all required files exist bufLen = 0; for (i = 0; i < sizeof (szCompressedFiles) / sizeof (szCompressedFiles[0]); i++) { _snprintf (szTmpFilePath, sizeof(szTmpFilePath), "%s%s", szDestDir, szCompressedFiles[i]); if (!FileExists (szTmpFilePath)) { char tmpstr [1000]; _snprintf (tmpstr, sizeof(tmpstr), "File not found:\n\n'%s'", szTmpFilePath); remove (outputFile); PkgError (tmpstr); return FALSE; } bufLen += (int) GetFileSize64 (szTmpFilePath); bufLen += 2; // 16-bit filename length bufLen += strlen(szCompressedFiles[i]); // Filename bufLen += 4; // CRC-32 bufLen += 4; // 32-bit file length } buffer = malloc (bufLen + 524288); // + 512K reserve if (buffer == NULL) { PkgError ("Cannot allocate memory for uncompressed data"); remove (outputFile); return FALSE; } // Write the start marker if (!SaveBufferToFile (MAG_START_MARKER, outputFile, strlen (MAG_START_MARKER), TRUE)) { PkgError ("Cannot write the start marker"); remove (outputFile); return FALSE; } bufIndex = buffer; // Copy all required files and their meta data to the buffer for (i = 0; i < sizeof (szCompressedFiles) / sizeof (szCompressedFiles[0]); i++) { DWORD tmpFileSize; unsigned char *tmpBuffer; _snprintf (szTmpFilePath, sizeof(szTmpFilePath), "%s%s", szDestDir, szCompressedFiles[i]); tmpBuffer = LoadFile (szTmpFilePath, &tmpFileSize); if (tmpBuffer == NULL) { char tmpstr [1000]; free (tmpBuffer); _snprintf (tmpstr, sizeof(tmpstr), "Cannot load file \n'%s'", szTmpFilePath); remove (outputFile); PkgError (tmpstr); goto msep_err; } // Copy the filename length to the main buffer mputWord (bufIndex, (WORD) strlen(szCompressedFiles[i])); // Copy the filename to the main buffer memcpy (bufIndex, szCompressedFiles[i], strlen(szCompressedFiles[i])); bufIndex += strlen(szCompressedFiles[i]); // Compute CRC-32 hash of the uncompressed file and copy it to the main buffer mputLong (bufIndex, GetCrc32 (tmpBuffer, tmpFileSize)); // Copy the file length to the main buffer mputLong (bufIndex, (unsigned __int32) tmpFileSize); // Copy the file contents to the main buffer memcpy (bufIndex, tmpBuffer, tmpFileSize); bufIndex += tmpFileSize; free (tmpBuffer); } // Calculate the total size of the uncompressed data uncompressedDataLen = (int) (bufIndex - buffer); // Write total size of the uncompressed data szTmp32bitPtr = szTmp32bit; mputLong (szTmp32bitPtr, (unsigned __int32) uncompressedDataLen); if (!SaveBufferToFile (szTmp32bit, outputFile, sizeof (szTmp32bit), TRUE)) { remove (outputFile); PkgError ("Cannot write the total size of the uncompressed data"); return FALSE; } // Compress all the files and meta data in the buffer to create a solid archive compressedBuffer = malloc (uncompressedDataLen + 524288); // + 512K reserve if (compressedBuffer == NULL) { remove (outputFile); PkgError ("Cannot allocate memory for compressed data"); return FALSE; } compressedDataLen = CompressBuffer (compressedBuffer, buffer, uncompressedDataLen); if (compressedDataLen <= 0) { remove (outputFile); PkgError ("Failed to compress the data"); return FALSE; } free (buffer); // Write the total size of the compressed data szTmp32bitPtr = szTmp32bit; mputLong (szTmp32bitPtr, (unsigned __int32) compressedDataLen); if (!SaveBufferToFile (szTmp32bit, outputFile, sizeof (szTmp32bit), TRUE)) { remove (outputFile); PkgError ("Cannot write the total size of the compressed data"); return FALSE; } // Write the compressed data if (!SaveBufferToFile (compressedBuffer, outputFile, compressedDataLen, TRUE)) { remove (outputFile); PkgError ("Cannot write compressed data to the package"); return FALSE; } // Write the end marker if (!SaveBufferToFile (MagEndMarker, outputFile, strlen (MagEndMarker), TRUE)) { remove (outputFile); PkgError ("Cannot write the end marker"); return FALSE; } free (compressedBuffer); // Compute and write CRC-32 hash of the entire package { DWORD tmpFileSize; char *tmpBuffer; tmpBuffer = LoadFile (outputFile, &tmpFileSize); if (tmpBuffer == NULL) { handleWin32Error (hwndDlg); remove (outputFile); PkgError ("Cannot load the package to compute CRC"); return FALSE; } // Zero all bytes that change when the exe is digitally signed (except appended blocks). WipeSignatureAreas (tmpBuffer); szTmp32bitPtr = szTmp32bit; mputLong (szTmp32bitPtr, GetCrc32 (tmpBuffer, tmpFileSize)); if (!SaveBufferToFile (szTmp32bit, outputFile, sizeof (szTmp32bit), TRUE)) { remove (outputFile); PkgError ("Cannot write the total size of the compressed data"); return FALSE; } free (tmpBuffer); } sprintf (tmpStr, "Self-extracting package successfully created (%s)", outputFile); PkgInfo (tmpStr); return TRUE; msep_err: free (buffer); free (compressedBuffer); return FALSE; }
/* ret: -1 = 更新失敗 -2 = Avi2のため更新できず(avi2でriff-avixの書き換えは未実装なため) */ DWORD CTag_OpenDML::Save(LPCTSTR szFileName) { DWORD dwWin32errorCode = ERROR_SUCCESS; HANDLE hFile; DWORD dwRet; // LONG lAddressHeight; DWORD dwRiffAviSize; __int64 llFileSize; __int64 llLastJUNKPtr; DWORD dwLastJUNKSize; __int64 llLargeJUNKPtr; DWORD dwLargeJunkSize; DWORD dwInfoSize; FOURCC id,type; BOOL bFindJUNK; DWORD dwSize; __int64 llOffset; FieldMap::iterator p; //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //ファイルを開く hFile = CreateFile(szFileName, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ,NULL, OPEN_EXISTING, //ファイルを開きます。指定したファイルが存在していない場合、この関数は失敗します。 FILE_ATTRIBUTE_NORMAL, NULL); if(hFile == INVALID_HANDLE_VALUE) { dwWin32errorCode = GetLastError(); goto exit; } llFileSize = GetFileSize64(hFile); //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //RIFF-AVI を確認 if(!ReadFile(hFile,&id,sizeof(id),&dwRet,NULL)) { dwWin32errorCode = GetLastError(); goto exit; } // 総サイズ if(!ReadFile(hFile,&dwRiffAviSize,sizeof(dwRiffAviSize),&dwRet,NULL)) { dwWin32errorCode = GetLastError(); goto exit; } // (念のため) if((dwRiffAviSize+8) > llFileSize) { dwRiffAviSize = (llFileSize & 0x000000007fffffff) - 8; if(dwRiffAviSize <= 0) { goto exit; } } // Riff type if(!ReadFile(hFile,&type,sizeof(type),&dwRet,NULL)) { dwWin32errorCode = GetLastError(); goto exit; } if((FOURCC_RIFF != id) && (m_type != type)) { dwWin32errorCode = GetLastError(); goto exit; } //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> // LIST-INFOを押し込めるスペースを計算 dwLargeJunkSize = FindJUNK_LISTINFO(hFile,(__int64 )dwRiffAviSize); TRACE(_T("FindJUNK_LISTINFO=%d\n"),dwLargeJunkSize); // 付加するLIST-INFOのサイズを計算 dwInfoSize = GetInfoChunkSize(); dwInfoSize += 12; if((dwLargeJunkSize < (dwInfoSize+8/*JUNK____*/)) && !(dwLargeJunkSize == dwInfoSize) ) { // LIST-INFOを終端に追加可能か? //先頭チャンクに戻る SetFilePointer64(hFile,0,FILE_BEGIN); if(FindChunk(hFile,llFileSize,MMIO_FINDRIFF,mmioFOURCC('A','V','I','X'),&dwSize)) { // AVIXが後に続く場合はファイルの作り直しが必要 return Save_1(hFile); } } //先頭チャンクに戻る(すべてのLIST-INFOを処理) SetFilePointer64(hFile,12,FILE_BEGIN); while(1) { //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //LIST-INFOを検索(見つからないときはRiff論理終端に移動) if(FindChunk(hFile,dwRiffAviSize + 8,MMIO_FINDLIST,mmioFOURCC('I','N','F','O'),&dwSize)) { // (チャンクの先頭+8にいる) llOffset = SetFilePointer64(hFile,0,FILE_CURRENT); if((llOffset + dwSize) < llFileSize) { //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //LIST-INFOが最後尾に付いていないときは、現存LIST-INFOをJUNKに置換する SetFilePointer64(hFile,-12,FILE_CURRENT); id = mmioFOURCC('J','U','N','K'); WriteFile(hFile,&id,sizeof(id),&dwRet,NULL); //先頭チャンクに戻る(すべてのLIST-INFOを処理) SetFilePointer64(hFile,12,FILE_BEGIN); continue; } else { //LIST-INFOの先頭に移動 SetFilePointer64(hFile,-12,FILE_CURRENT); //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //LIST-INFOを削除 SetEndOfFile(hFile); dwRiffAviSize = (DWORD)(llOffset - 12 - 8); //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //RIFF-AVI チャンクサイズを修正 SetFilePointer(hFile,4,NULL,FILE_BEGIN); WriteFile(hFile,&dwRiffAviSize,sizeof(dwRiffAviSize),&dwRet,NULL); SetFilePointer64(hFile,dwRiffAviSize+8,FILE_BEGIN); } } break; } if(dwInfoSize == 12) { // 保存するデータはなし goto exit; } //先頭チャンクに戻る(すべてのLIST-INFOを処理) SetFilePointer64(hFile,12,FILE_BEGIN); bFindJUNK = FALSE; dwLastJUNKSize = 0; dwLargeJunkSize = 0; while(1) { //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> // JUNKを統合 if(FindChunk(hFile,llFileSize,MMIO_FINDCHUNK,mmioFOURCC('J','U','N','K'),&dwSize)) { // (チャンクの先頭+8にいる) if(bFindJUNK) { __int64 llNowPtr = SetFilePointer64(hFile,0,FILE_CURRENT) - 8; // 直前のJUNKと統合可能か? if(llNowPtr == (llLastJUNKPtr + dwLastJUNKSize)) { // 統合 SetFilePointer64(hFile,llLastJUNKPtr+4,FILE_BEGIN); dwLastJUNKSize += dwSize + 8; dwSize = dwLastJUNKSize - 8; WriteFile(hFile,&dwSize,sizeof(dwSize),&dwRet,NULL); for(DWORD i=0; i<dwSize; i++) { if((DWORD)m_strJunkHeader.GetLength() > i) { char c = (char)m_strJunkHeader[i]; // QQQ ANSI文字のみ対応 WriteFile(hFile,&c,1,&dwRet,NULL); } else { WriteFile(hFile,"",1,&dwRet,NULL); } } } else { SetFilePointer64(hFile,dwSize,FILE_CURRENT); bFindJUNK = FALSE; } } else { bFindJUNK = TRUE; llLastJUNKPtr = SetFilePointer64(hFile,0,FILE_CURRENT) - 8; dwLastJUNKSize = dwSize + 8; SetFilePointer64(hFile,dwSize,FILE_CURRENT); } if(dwLargeJunkSize < dwLastJUNKSize) { llLargeJUNKPtr = llLastJUNKPtr; dwLargeJunkSize = dwLastJUNKSize; } continue; } break; } // LIST-INFOがJUNKの中に収まるか計算 if((dwLargeJunkSize < (dwInfoSize+8/*JUNK____*/)) && !(dwLargeJunkSize == dwInfoSize) ) { // (収まらない) // LIST-INFOを終端に追加 llLargeJUNKPtr = dwRiffAviSize+8; //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //RIFF-AVI チャンクサイズを修正 dwRiffAviSize += dwInfoSize; SetFilePointer(hFile,4,NULL,FILE_BEGIN); WriteFile(hFile,&dwRiffAviSize,sizeof(dwRiffAviSize),&dwRet,NULL); } //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //LIST-INFOを書き込む SetFilePointer64(hFile,llLargeJUNKPtr,FILE_BEGIN); dwSize = dwInfoSize - 8; id = FOURCC_LIST; type = mmioFOURCC('I','N','F','O'); WriteFile(hFile,&id,sizeof(id),&dwRet,NULL); WriteFile(hFile,&dwSize,sizeof(dwSize),&dwRet,NULL); WriteFile(hFile,&type,sizeof(type),&dwRet,NULL); //全てのフィールド情報を保存 p = m_fields.begin(); while(p != m_fields.end()) { FOURCC id = p->first; CString *pStr = &p->second; DWORD len; char *str = TstrToBytesAlloc(*pStr, -1, (int*)&len, BTC_CODE_ANSI); if(str) { if(len > 1) { WriteFile(hFile,&id,sizeof(id),&dwRet,NULL); WriteFile(hFile,&len,sizeof(len),&dwRet,NULL); WriteFile(hFile,str,len,&dwRet,NULL); if(len%2) { WriteFile(hFile,"\x00",1,&dwRet,NULL); } } free(str); } p++; } ////////////////////////////////////// // 残りスペースをJUNKで埋める TRACE(_T("dwLargeJunkSize=%d\n"),dwLargeJunkSize); TRACE(_T("dwInfoSize=%d\n"),dwInfoSize); if(dwLargeJunkSize >= (dwInfoSize+8/*JUNK____*/)) { dwSize = dwLargeJunkSize-dwInfoSize-8; id = mmioFOURCC('J','U','N','K'); WriteFile(hFile,&id,sizeof(id),&dwRet,NULL); WriteFile(hFile,&dwSize,sizeof(dwSize),&dwRet,NULL); TRACE(_T("Start dwSize=%d\n"),dwSize); for(DWORD i=0; i<dwSize; i++) { if((DWORD)m_strJunkHeader.GetLength() > i) { char c = (char)m_strJunkHeader[i]; // QQQ ANSI文字のみ対応 WriteFile(hFile,&c,1,&dwRet,NULL); } else { WriteFile(hFile,"",1,&dwRet,NULL); } } TRACE(_T("End dwSize=%d\n"),dwSize); } exit: if(hFile != INVALID_HANDLE_VALUE) { CloseHandle(hFile); } return dwWin32errorCode; }
/* ret: -1 = ロード失敗 */ DWORD CTag_OpenDML::Load(LPCTSTR szFileName,char id1,char id2,char id3,char id4) { DWORD dwWin32errorCode = ERROR_SUCCESS; HANDLE hFile; DWORD dwRet; DWORD dwRiffAviSize; __int64 llFileSize; // __int64 llLastRiffPtr; FOURCC id,type; DWORD dwSize; Release(); FieldMap::iterator p; //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //ファイルを開く hFile = CreateFile(szFileName, GENERIC_READ, FILE_SHARE_READ,NULL, OPEN_EXISTING, //ファイルを開きます。指定したファイルが存在していない場合、この関数は失敗します。 FILE_ATTRIBUTE_NORMAL, NULL); if(hFile == INVALID_HANDLE_VALUE) { dwWin32errorCode = GetLastError(); goto exit; } llFileSize = GetFileSize64(hFile); //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //RIFF-AVI を確認 if(!ReadFile(hFile,&id,sizeof(id),&dwRet,NULL)) { dwWin32errorCode = GetLastError(); goto exit; } // 総サイズ if(!ReadFile(hFile,&dwRiffAviSize,sizeof(dwRiffAviSize),&dwRet,NULL)) { dwWin32errorCode = GetLastError(); goto exit; } // (念のため) if((dwRiffAviSize+8) > llFileSize) { dwRiffAviSize = (llFileSize & 0x000000007fffffff) - 8; if(dwRiffAviSize <= 0) { goto exit; } } // Riff type if(!ReadFile(hFile,&type,sizeof(type),&dwRet,NULL)) { dwWin32errorCode = GetLastError(); goto exit; } m_type = mmioFOURCC(id1,id2,id3,id4); if((FOURCC_RIFF != id) || (m_type != type)) { dwWin32errorCode = GetLastError(); goto exit; } m_bEnable = TRUE; OutputDebugString(_T("CTag_OpenDML::Load m_bEnable = TRUE\n")); TRACE(_T("%s Len=%I64d(%08I64x) RiffLen=%lu(%08x)\n"),szFileName,llFileSize,llFileSize,dwRiffAviSize,dwRiffAviSize); //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //LIST-INFOを検索(見つからないときはRiff論理終端に移動) if(FindChunk(hFile,dwRiffAviSize + 8,MMIO_FINDLIST,mmioFOURCC('I','N','F','O'),&dwSize) && (dwSize > 8)) { // (チャンクの先頭+8にいる) //全ての情報を取り込む char *buf = (char *)malloc(dwSize); if(buf == NULL) { dwWin32errorCode = -1; goto exit; } if(!ReadFile(hFile,buf,dwSize,&dwRet,NULL)) { dwWin32errorCode = GetLastError(); free(buf); goto exit; } DWORD dwRemainSize = dwSize; while(dwRemainSize > 0) { if(dwRemainSize < 8) { break; //用心 } FOURCC id = mmioFOURCC( buf[dwSize-dwRemainSize], buf[dwSize-dwRemainSize+1], buf[dwSize-dwRemainSize+2], buf[dwSize-dwRemainSize+3] ); DWORD size = *(DWORD *)&buf[dwSize-dwRemainSize+4]; char *data = (char *)&buf[dwSize-dwRemainSize+8]; if(dwRemainSize < (8+size)) { break; //用心 } if(size>0) { //mapに追加 if(data[size-1] == '\0') { m_fields.insert(std::pair<FOURCC,CString>(id,data)); } else { m_fields.insert(std::pair<FOURCC,CString>(id,CString(data,size))); } } if(dwRemainSize <= (size+8)) { break; } dwRemainSize -= size + 8; if(dwRemainSize & 0x01) { dwRemainSize--; //ワード境界合わせ } } free(buf); } exit: if(hFile != INVALID_HANDLE_VALUE) { CloseHandle(hFile); } return dwWin32errorCode; }
int CreateInputFilePtrs(ncch_settings *ncchset, user_settings *usrset) { if(usrset->ncch.romfsPath){ ncchset->componentFilePtrs.romfsSize = GetFileSize64(usrset->ncch.romfsPath); ncchset->componentFilePtrs.romfs = fopen(usrset->ncch.romfsPath,"rb"); if(!ncchset->componentFilePtrs.romfs){ fprintf(stderr,"[NCCH ERROR] Failed to open RomFs file '%s'\n",usrset->ncch.romfsPath); return FAILED_TO_IMPORT_FILE; } } if(usrset->ncch.elfPath){ ncchset->componentFilePtrs.elfSize = GetFileSize64(usrset->ncch.elfPath); ncchset->componentFilePtrs.elf = fopen(usrset->ncch.elfPath,"rb"); if(!ncchset->componentFilePtrs.elf){ fprintf(stderr,"[NCCH ERROR] Failed to open elf file '%s'\n",usrset->ncch.elfPath); return FAILED_TO_IMPORT_FILE; } } if(usrset->ncch.bannerPath){ ncchset->componentFilePtrs.bannerSize = GetFileSize64(usrset->ncch.bannerPath); ncchset->componentFilePtrs.banner = fopen(usrset->ncch.bannerPath,"rb"); if(!ncchset->componentFilePtrs.banner){ fprintf(stderr,"[NCCH ERROR] Failed to open banner file '%s'\n",usrset->ncch.bannerPath); return FAILED_TO_IMPORT_FILE; } } if(usrset->ncch.iconPath){ ncchset->componentFilePtrs.iconSize = GetFileSize64(usrset->ncch.iconPath); ncchset->componentFilePtrs.icon = fopen(usrset->ncch.iconPath,"rb"); if(!ncchset->componentFilePtrs.icon){ fprintf(stderr,"[NCCH ERROR] Failed to open icon file '%s'\n",usrset->ncch.iconPath); return FAILED_TO_IMPORT_FILE; } } if(usrset->ncch.logoPath){ ncchset->componentFilePtrs.logoSize = GetFileSize64(usrset->ncch.logoPath); ncchset->componentFilePtrs.logo = fopen(usrset->ncch.logoPath,"rb"); if(!ncchset->componentFilePtrs.logo){ fprintf(stderr,"[NCCH ERROR] Failed to open logo file '%s'\n",usrset->ncch.logoPath); return FAILED_TO_IMPORT_FILE; } } if(usrset->ncch.codePath){ ncchset->componentFilePtrs.codeSize = GetFileSize64(usrset->ncch.codePath); ncchset->componentFilePtrs.code = fopen(usrset->ncch.codePath,"rb"); if(!ncchset->componentFilePtrs.code){ fprintf(stderr,"[NCCH ERROR] Failed to open ExeFs Code file '%s'\n",usrset->ncch.codePath); return FAILED_TO_IMPORT_FILE; } } if(usrset->ncch.exheaderPath){ ncchset->componentFilePtrs.exhdrSize = GetFileSize64(usrset->ncch.exheaderPath); ncchset->componentFilePtrs.exhdr = fopen(usrset->ncch.exheaderPath,"rb"); if(!ncchset->componentFilePtrs.exhdr){ fprintf(stderr,"[NCCH ERROR] Failed to open ExHeader file '%s'\n",usrset->ncch.exheaderPath); return FAILED_TO_IMPORT_FILE; } } if(usrset->ncch.plainRegionPath){ ncchset->componentFilePtrs.plainregionSize = GetFileSize64(usrset->ncch.plainRegionPath); ncchset->componentFilePtrs.plainregion = fopen(usrset->ncch.plainRegionPath,"rb"); if(!ncchset->componentFilePtrs.plainregion){ fprintf(stderr,"[NCCH ERROR] Failed to open PlainRegion file '%s'\n",usrset->ncch.plainRegionPath); return FAILED_TO_IMPORT_FILE; } } return 0; }