static LPBYTE LoadFileToMemory(TCascStorage * hs, LPBYTE pbEncodingKey, DWORD * pcbFileData) { QUERY_KEY EncodingKey; LPBYTE pbFileData = NULL; HANDLE hFile; DWORD cbBytesRead = 0; DWORD cbFileData = 0; // Open the file by encoding key EncodingKey.pbData = pbEncodingKey; EncodingKey.cbData = MD5_HASH_SIZE; if(CascOpenFileByEncodingKey((HANDLE)hs, &EncodingKey, 0, &hFile)) { // Retrieve the file size cbFileData = CascGetFileSize(hFile, NULL); if(cbFileData > 0) { pbFileData = CASC_ALLOC(BYTE, cbFileData); if(pbFileData != NULL) { CascReadFile(hFile, pbFileData, cbFileData, &cbBytesRead); } } // Close the file CascCloseFile(hFile); } // Give the file to the caller if(pcbFileData != NULL) pcbFileData[0] = cbBytesRead; return pbFileData; }
bool ChunkedFile::loadFile(HANDLE mpq, std::string const& fileName, bool log) { free(); HANDLE file; if (!CascOpenFile(mpq, fileName.c_str(), CASC_LOCALE_ALL, 0, &file)) { if (log) printf("No such file %s\n", fileName.c_str()); return false; } data_size = CascGetFileSize(file, nullptr); data = new uint8[data_size]; CascReadFile(file, data, data_size, nullptr/*bytesRead*/); parseChunks(); if (prepareLoadedData()) { CascCloseFile(file); return true; } printf("Error loading %s\n", fileName.c_str()); CascCloseFile(file); free(); return false; }
static int TestOpenStorage_OpenFile(const TCHAR * szStorage, const char * szFileName) { HANDLE hStorage; HANDLE hFile; DWORD dwBytesRead; BYTE Buffer[0x1000]; int nError = ERROR_SUCCESS; // Open the storage directory if(!CascOpenStorage(szStorage, 0, &hStorage)) { assert(GetLastError() != ERROR_SUCCESS); nError = GetLastError(); } DWORD dwFileCount = 0; if(CascGetStorageInfo(hStorage, CascStorageFileCount, &dwFileCount, sizeof(DWORD), NULL)) { printf("file count: %d\n", dwFileCount); } DWORD dwFeatures = 0; if(CascGetStorageInfo(hStorage, CascStorageFeatures, &dwFeatures, sizeof(DWORD), NULL)) { printf("support listfile? %s\n", (dwFeatures & CASC_FEATURE_LISTFILE) ? "YES" : "NO"); } if(nError == ERROR_SUCCESS) { // Open a file if(!CascOpenFile(hStorage, szFileName, CASC_LOCALE_ZHCN, 0, &hFile)) { assert(GetLastError() != ERROR_SUCCESS); nError = GetLastError(); } } // Read some data from the file if(nError == ERROR_SUCCESS) { DWORD dwFileSize, dwFileSizeHigh; dwFileSize = CascGetFileSize(hFile, &dwFileSizeHigh); printf("file name : %s, file size: %d\n", szFileName, dwFileSize); // Read data from the file CascReadFile(hFile, Buffer, sizeof(Buffer), &dwBytesRead); CascCloseFile(hFile); } // Close storage and return if(hStorage != NULL) CascCloseStorage(hStorage); return nError; }
qint64 CascFile::readData(char *data, qint64 maxlen){ QMutexLocker lock(&d->Storage->Lock); quint32 readLen = maxlen & 0xFFFFFFFF; quint32 read = 0; if(!CascReadFile(d->FileHandle, data, readLen, &read)){ this->setErrorString(d->Storage->translateError(GetLastError())); return -1; } return read; }
uint32 ReadBuild(int locale) { // include build info file also std::string filename = std::string("component.wow-") + localeNames[locale] + ".txt"; //printf("Read %s file... ", filename.c_str()); HANDLE dbcFile; if (!CascOpenFile(CascStorage, filename.c_str(), CASC_LOCALE_ALL, 0, &dbcFile)) { printf("Locale %s not installed.\n", localeNames[locale]); return 0; } char buff[512]; DWORD readBytes = 0; CascReadFile(dbcFile, buff, 512, &readBytes); if (!readBytes) { printf("Fatal error: Not found %s file!\n", filename.c_str()); exit(1); } std::string text = std::string(buff, readBytes); CascCloseFile(dbcFile); size_t pos = text.find("version=\""); size_t pos1 = pos + strlen("version=\""); size_t pos2 = text.find("\"", pos1); if (pos == text.npos || pos2 == text.npos || pos1 >= pos2) { printf("Fatal error: Invalid %s file format!\n", filename.c_str()); exit(1); } std::string build_str = text.substr(pos1, pos2 - pos1); int build = atoi(build_str.c_str()); if (build <= 0) { printf("Fatal error: Invalid %s file format!\n", filename.c_str()); exit(1); } return build; }
static int TestOpenStorage_OpenFile(const TCHAR * szStorage, const char * szFileName) { HANDLE hStorage; HANDLE hFile; DWORD dwBytesRead; BYTE Buffer[0x1000]; int nError = ERROR_SUCCESS; // Open the storage directory if(!CascOpenStorage(szStorage, 0, &hStorage)) { assert(GetLastError() != ERROR_SUCCESS); nError = GetLastError(); } if(nError == ERROR_SUCCESS) { // Open a file if(!CascOpenFile(hStorage, szFileName, 0, 0, &hFile)) { assert(GetLastError() != ERROR_SUCCESS); nError = GetLastError(); } } // Read some data from the file if(nError == ERROR_SUCCESS) { // Read data from the file CascReadFile(hFile, Buffer, sizeof(Buffer), &dwBytesRead); CascCloseFile(hFile); } // Close storage and return if(hStorage != NULL) CascCloseStorage(hStorage); return nError; }
static int ExtractFile(HANDLE hStorage, const char * szFileName, const TCHAR * szLocalPath, DWORD dwLocaleFlags) { TFileStream * pStream = NULL; HANDLE hFile = NULL; TCHAR szLocalFileName[MAX_PATH]; TCHAR * szNamePtr = szLocalFileName; BYTE Buffer[0x1000]; DWORD dwBytesRead; int nError = ERROR_SUCCESS; // Create the file path _tcscpy(szNamePtr, szLocalPath); szNamePtr += _tcslen(szLocalPath); *szNamePtr++ = _T('\\'); // Copy the plain file name CopyString(szNamePtr, szFileName, strlen(szFileName)); // Open the CASC file if(nError == ERROR_SUCCESS) { // Open a file if(!CascOpenFile(hStorage, szFileName, dwLocaleFlags, 0, &hFile)) { assert(GetLastError() != ERROR_SUCCESS); nError = GetLastError(); } } // Create the local file if(nError == ERROR_SUCCESS) { pStream = FileStream_CreateFile(szLocalFileName, 0); if(pStream == NULL) { // Try to create all directories and retry ForceCreatePath(szLocalFileName); pStream = FileStream_CreateFile(szLocalFileName, 0); if(pStream == NULL) nError = GetLastError(); } } // Read some data from the file if(nError == ERROR_SUCCESS) { for(;;) { // Read data from the file CascReadFile(hFile, Buffer, sizeof(Buffer), &dwBytesRead); if(dwBytesRead == 0) break; // Write the local file FileStream_Write(pStream, NULL, Buffer, dwBytesRead); } } // Close handles if(pStream != NULL) FileStream_Close(pStream); if(hFile != NULL) CascCloseFile(hFile); return nError; }