static int ZIP_isArchive(const char *filename, int forWriting) { PHYSFS_uint32 sig; int retval = 0; void *in; in = __PHYSFS_platformOpenRead(filename); BAIL_IF_MACRO(in == NULL, NULL, 0); /* * The first thing in a zip file might be the signature of the * first local file record, so it makes for a quick determination. */ if (readui32(in, &sig)) { retval = (sig == ZIP_LOCAL_FILE_SIG); if (!retval) { /* * No sig...might be a ZIP with data at the start * (a self-extracting executable, etc), so we'll have to do * it the hard way... */ retval = (zip_find_end_of_central_dir(in, NULL) != -1); } /* if */ } /* if */ __PHYSFS_platformClose(in); return(retval); } /* ZIP_isArchive */
PHYSFS_sint64 __PHYSFS_platformGetLastModTime(const char *fname) { PHYSFS_sint64 retval = -1; WIN32_FILE_ATTRIBUTE_DATA attr; int rc = 0; memset(&attr, '\0', sizeof(attr)); /* GetFileAttributesEx didn't show up until Win98 and NT4. */ if ((pGetFileAttributesExW != NULL) || (pGetFileAttributesExA != NULL)) { WCHAR *wstr; UTF8_TO_UNICODE_STACK_MACRO(wstr, fname); if (wstr != NULL) /* if NULL, maybe the fallback will work. */ { if (pGetFileAttributesExW != NULL) /* NT/XP/Vista/etc system. */ rc = pGetFileAttributesExW(wstr, GetFileExInfoStandard, &attr); else /* Win98/ME system */ { const int len = (int)(wStrLen(wstr) + 1); char *cp = (char *)__PHYSFS_smallAlloc(len); if (cp != NULL) { WideCharToMultiByte(CP_ACP, 0, wstr, len, cp, len, 0, 0); rc = pGetFileAttributesExA(cp, GetFileExInfoStandard, &attr); __PHYSFS_smallFree(cp); } /* if */ } /* else */ __PHYSFS_smallFree(wstr); } /* if */ } /* if */ if (rc) /* had API entry point and it worked. */ { /* 0 return value indicates an error or not supported */ if ((attr.ftLastWriteTime.dwHighDateTime != 0) || (attr.ftLastWriteTime.dwLowDateTime != 0)) { retval = FileTimeToPhysfsTime(&attr.ftLastWriteTime); } /* if */ } /* if */ /* GetFileTime() has been in the Win32 API since the start. */ if (retval == -1) /* try a fallback... */ { FILETIME ft; BOOL rc; const char *err; WinApiFile *f = (WinApiFile *)__PHYSFS_platformOpenRead(fname); BAIL_IF_MACRO(f == NULL, NULL, -1) rc = GetFileTime(f->handle, NULL, NULL, &ft); err = winApiStrError(); CloseHandle(f->handle); allocator.Free(f); BAIL_IF_MACRO(!rc, err, -1); retval = FileTimeToPhysfsTime(&ft); } /* if */ return(retval); } /* __PHYSFS_platformGetLastModTime */
static fvoid *WAD_openRead(dvoid *opaque, const char *fnm, int *fileExists) { WADinfo *info = ((WADinfo *) opaque); WADfileinfo *finfo; WADentry *entry; entry = wad_find_entry(info, fnm); *fileExists = (entry != NULL); BAIL_IF_MACRO(entry == NULL, NULL, NULL); finfo = (WADfileinfo *) allocator.Malloc(sizeof (WADfileinfo)); BAIL_IF_MACRO(finfo == NULL, ERR_OUT_OF_MEMORY, NULL); finfo->handle = __PHYSFS_platformOpenRead(info->filename); if ( (finfo->handle == NULL) || (!__PHYSFS_platformSeek(finfo->handle, entry->startPos)) ) { allocator.Free(finfo); return NULL; } /* if */ finfo->curPos = 0; finfo->entry = entry; return finfo; } /* WAD_openRead */
static int mvl_open(const char *filename, int forWriting, void **fh, PHYSFS_uint32 *count) { PHYSFS_uint8 buf[4]; *fh = NULL; BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0); *fh = __PHYSFS_platformOpenRead(filename); BAIL_IF_MACRO(*fh == NULL, NULL, 0); if (__PHYSFS_platformRead(*fh, buf, 4, 1) != 1) goto openMvl_failed; if (memcmp(buf, "DMVL", 4) != 0) { __PHYSFS_setError(ERR_UNSUPPORTED_ARCHIVE); goto openMvl_failed; } /* if */ if (__PHYSFS_platformRead(*fh, count, sizeof (PHYSFS_uint32), 1) != 1) goto openMvl_failed; *count = PHYSFS_swapULE32(*count); return 1; openMvl_failed: if (*fh != NULL) __PHYSFS_platformClose(*fh); *count = -1; *fh = NULL; return 0; } /* mvl_open */
static int hog_open(const char *filename, int forWriting, void **fh, PHYSFS_uint32 *count) { PHYSFS_uint8 buf[13]; PHYSFS_uint32 size; PHYSFS_sint64 pos; *count = 0; *fh = NULL; BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0); *fh = __PHYSFS_platformOpenRead(filename); BAIL_IF_MACRO(*fh == NULL, NULL, 0); if (__PHYSFS_platformRead(*fh, buf, 3, 1) != 1) goto openHog_failed; if (memcmp(buf, "DHF", 3) != 0) { __PHYSFS_setError(ERR_UNSUPPORTED_ARCHIVE); goto openHog_failed; } /* if */ while( 1 ) { if (__PHYSFS_platformRead(*fh, buf, 13, 1) != 1) break; //eof here is ok if (__PHYSFS_platformRead(*fh, &size, 4, 1) != 1) goto openHog_failed; size = PHYSFS_swapULE32(size); (*count)++; // Skip over entry pos = __PHYSFS_platformTell(*fh); if (pos == -1) goto openHog_failed; if (!__PHYSFS_platformSeek(*fh, pos + size)) goto openHog_failed; } // Rewind to start of entries if (!__PHYSFS_platformSeek(*fh, 3)) goto openHog_failed; return(1); openHog_failed: if (*fh != NULL) __PHYSFS_platformClose(*fh); *count = -1; *fh = NULL; return(0); } /* hog_open */
static void *LZMA_openArchive(const char *name, int forWriting) { PHYSFS_uint64 len; LZMAarchive *archive = NULL; ISzAlloc allocImp; ISzAlloc allocTempImp; BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, NULL); BAIL_IF_MACRO(!LZMA_isArchive(name,forWriting), ERR_UNSUPPORTED_ARCHIVE, 0); archive = (LZMAarchive *) allocator.Malloc(sizeof (LZMAarchive)); BAIL_IF_MACRO(archive == NULL, ERR_OUT_OF_MEMORY, NULL); archive->firstEntry = NULL; archive->lastEntry = NULL; if ((archive->stream.File = __PHYSFS_platformOpenRead(name)) == NULL) { allocator.Free(archive); return NULL; } /* if */ /* Prepare structs for 7z */ archive->stream.InStream.Read = SzFileReadImp; archive->stream.InStream.Seek = SzFileSeekImp; allocImp.Alloc = SzAllocPhysicsFS; allocImp.Free = SzFreePhysicsFS; allocTempImp.Alloc = SzAllocPhysicsFS; allocTempImp.Free = SzFreePhysicsFS; InitCrcTable(); SzArDbExInit(&archive->db); if (lzma_err(SzArchiveOpen(&archive->stream.InStream, &archive->db, &allocImp, &allocTempImp)) != SZ_OK) { __PHYSFS_platformClose(archive->stream.File); allocator.Free(archive); return NULL; } /* if */ len = archive->db.Database.NumFolders * sizeof (LZMAfolder); archive->folder = (LZMAfolder *) allocator.Malloc(len); BAIL_IF_MACRO(archive->folder == NULL, ERR_OUT_OF_MEMORY, NULL); /* * Init with 0 so we know when a folder is already cached * Values will be set by LZMA_read() */ memset(archive->folder, 0, (size_t) len); return(archive); } /* LZMA_openArchive */
static fvoid *HHA_openRead(dvoid *opaque, const char *fnm, int *fileExists) { HHAinfo *info = (HHAinfo *) opaque; HHAfileinfo *finfo; HHAentry *entry; entry = HHA_find_entry(info, fnm); *fileExists = (entry != NULL); BAIL_IF_MACRO(entry == NULL, NULL, NULL); finfo = (HHAfileinfo *) allocator.Malloc(sizeof (HHAfileinfo)); BAIL_IF_MACRO(finfo == NULL, ERR_OUT_OF_MEMORY, NULL); memset(finfo, '\0', sizeof (HHAfileinfo)); finfo->handle = __PHYSFS_platformOpenRead(info->filename); if ( (finfo->handle == NULL) || (!__PHYSFS_platformSeek(finfo->handle, entry->offset)) ) { allocator.Free(finfo); return(NULL); } /* if */ finfo->entry = entry; if (finfo->entry->compress == HHA_COMPRESS_ZLIB) { initializeZStream(&finfo->zlib_stream); if (zlib_err(inflateInit2(&finfo->zlib_stream, -MAX_WBITS)) != Z_OK) { HHA_fileClose(finfo); return(NULL); } /* if */ finfo->buffer = (PHYSFS_uint8 *) allocator.Malloc(ZIP_READBUFSIZE); if (finfo->buffer == NULL) { HHA_fileClose(finfo); BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL); } /* if */ } /* if */ return(finfo); } /* HHA_openRead */
static int hha_open(const char *filename, int forWriting, void **fh, PHYSFS_uint32 *filenameSize, PHYSFS_uint32 *count) { PHYSFS_uint32 magic[2]; *fh = NULL; BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0); *fh = __PHYSFS_platformOpenRead(filename); BAIL_IF_MACRO(*fh == NULL, NULL, 0); if (__PHYSFS_platformRead(*fh, magic, sizeof(PHYSFS_uint32), 2) != 2) goto openHHA_failed; magic[0] = PHYSFS_swapULE32(magic[0]); magic[1] = PHYSFS_swapULE32(magic[1]); if (!(magic[0] == HHA_FILE_MAGIC && magic[1] == HHA_FILE_VERSION)) { __PHYSFS_setError(ERR_UNSUPPORTED_ARCHIVE); goto openHHA_failed; } /* if */ if (__PHYSFS_platformRead(*fh, filenameSize, sizeof (PHYSFS_uint32), 1) != 1) goto openHHA_failed; *filenameSize = PHYSFS_swapULE32(*filenameSize); if (__PHYSFS_platformRead(*fh, count, sizeof (PHYSFS_uint32), 1) != 1) goto openHHA_failed; *count = PHYSFS_swapULE32(*count); return(1); openHHA_failed: if (*fh != NULL) __PHYSFS_platformClose(*fh); *filenameSize = -1; *count = -1; *fh = NULL; return(0); } /* HHA_open */
static int LZMA_isArchive(const char *filename, int forWriting) { PHYSFS_uint8 sig[k7zSignatureSize]; PHYSFS_uint8 res; void *in; BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0); in = __PHYSFS_platformOpenRead(filename); BAIL_IF_MACRO(in == NULL, NULL, 0); if (__PHYSFS_platformRead(in, sig, k7zSignatureSize, 1) != 1) BAIL_MACRO(NULL, 0); /* Test whether sig is the 7z signature */ res = TestSignatureCandidate(sig); __PHYSFS_platformClose(in); return res; } /* LZMA_isArchive */
PHYSFS_sint64 __PHYSFS_platformGetLastModTime(const char *fname) { PHYSFS_sint64 retval = -1; WIN32_FILE_ATTRIBUTE_DATA attrData; memset(&attrData, '\0', sizeof (attrData)); /* GetFileAttributesEx didn't show up until Win98 and NT4. */ if (pGetFileAttributesEx != NULL) { if (pGetFileAttributesEx(fname, GetFileExInfoStandard, &attrData)) { /* 0 return value indicates an error or not supported */ if ( (attrData.ftLastWriteTime.dwHighDateTime != 0) || (attrData.ftLastWriteTime.dwLowDateTime != 0) ) { retval = FileTimeToPhysfsTime(&attrData.ftLastWriteTime); } /* if */ } /* if */ } /* if */ /* GetFileTime() has been in the Win32 API since the start. */ if (retval == -1) /* try a fallback... */ { FILETIME ft; BOOL rc; const char *err; win32file *f = (win32file *) __PHYSFS_platformOpenRead(fname); BAIL_IF_MACRO(f == NULL, NULL, -1) rc = GetFileTime(f->handle, NULL, NULL, &ft); err = win32strerror(); CloseHandle(f->handle); free(f); BAIL_IF_MACRO(!rc, err, -1); retval = FileTimeToPhysfsTime(&ft); } /* if */ return(retval); } /* __PHYSFS_platformGetLastModTime */
static FileHandle *HOG_openRead(DirHandle *h, const char *fnm, int *fileExists) { HOGinfo *info = ((HOGinfo *) h->opaque); FileHandle *retval; HOGfileinfo *finfo; HOGentry *entry; entry = hog_find_entry(info, fnm); *fileExists = (entry != NULL); BAIL_IF_MACRO(entry == NULL, NULL, NULL); retval = (FileHandle *) malloc(sizeof (FileHandle)); BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL); finfo = (HOGfileinfo *) malloc(sizeof (HOGfileinfo)); if (finfo == NULL) { free(retval); BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL); } /* if */ finfo->handle = __PHYSFS_platformOpenRead(info->filename); if ( (finfo->handle == NULL) || (!__PHYSFS_platformSeek(finfo->handle, entry->startPos)) ) { free(finfo); free(retval); return(NULL); } /* if */ finfo->curPos = 0; finfo->entry = entry; retval->opaque = (void *) finfo; retval->funcs = &__PHYSFS_FileFunctions_HOG; retval->dirHandle = h; return(retval); } /* HOG_openRead */