misc/libphysfs/archiver_lzma.c
changeset 12218 bb5522e88ab2
parent 8524 a65e9bcf0a03
equal deleted inserted replaced
12217:ea891871f481 12218:bb5522e88ab2
   122  * WARNING: If the ISzInStream in 'object' is not contained in a valid FileInputStream this _will_ break horribly!
   122  * WARNING: If the ISzInStream in 'object' is not contained in a valid FileInputStream this _will_ break horribly!
   123  */
   123  */
   124 SZ_RESULT SzFileReadImp(void *object, void *buffer, size_t size,
   124 SZ_RESULT SzFileReadImp(void *object, void *buffer, size_t size,
   125                         size_t *processedSize)
   125                         size_t *processedSize)
   126 {
   126 {
   127     FileInputStream *s = (FileInputStream *)((unsigned long)object - offsetof(FileInputStream, inStream)); /* HACK! */
   127     FileInputStream *s = (FileInputStream *)((size_t)object - offsetof(FileInputStream, inStream)); /* HACK! */
   128     const size_t processedSizeLoc = s->io->read(s->io, buffer, size);
   128     const size_t processedSizeLoc = s->io->read(s->io, buffer, size);
   129     if (processedSize != NULL)
   129     if (processedSize != NULL)
   130         *processedSize = processedSizeLoc;
   130         *processedSize = processedSizeLoc;
   131     return SZ_OK;
   131     return SZ_OK;
   132 } /* SzFileReadImp */
   132 } /* SzFileReadImp */
   137  * Seek implementation, to be passed to 7z
   137  * Seek implementation, to be passed to 7z
   138  * WARNING: If the ISzInStream in 'object' is not contained in a valid FileInputStream this _will_ break horribly!
   138  * WARNING: If the ISzInStream in 'object' is not contained in a valid FileInputStream this _will_ break horribly!
   139  */
   139  */
   140 SZ_RESULT SzFileSeekImp(void *object, CFileSize pos)
   140 SZ_RESULT SzFileSeekImp(void *object, CFileSize pos)
   141 {
   141 {
   142     FileInputStream *s = (FileInputStream *)((unsigned long)object - offsetof(FileInputStream, inStream)); /* HACK! */
   142     FileInputStream *s = (FileInputStream *)((size_t)object - offsetof(FileInputStream, inStream)); /* HACK! */
   143     if (s->io->seek(s->io, (PHYSFS_uint64) pos))
   143     if (s->io->seek(s->io, (PHYSFS_uint64) pos))
   144         return SZ_OK;
   144         return SZ_OK;
   145     return SZE_FAIL;
   145     return SZE_FAIL;
   146 } /* SzFileSeekImp */
   146 } /* SzFileSeekImp */
   147 
   147 
   203  */
   203  */
   204 static LZMAfile * lzma_find_file(const LZMAarchive *archive, const char *name)
   204 static LZMAfile * lzma_find_file(const LZMAarchive *archive, const char *name)
   205 {
   205 {
   206     LZMAfile *file = bsearch(name, archive->files, archive->db.Database.NumFiles, sizeof(*archive->files), lzma_file_cmp_stdlib); /* FIXME: Should become __PHYSFS_search!!! */
   206     LZMAfile *file = bsearch(name, archive->files, archive->db.Database.NumFiles, sizeof(*archive->files), lzma_file_cmp_stdlib); /* FIXME: Should become __PHYSFS_search!!! */
   207 
   207 
   208     BAIL_IF_MACRO(file == NULL, PHYSFS_ERR_NO_SUCH_PATH, NULL);
   208     BAIL_IF_MACRO(file == NULL, PHYSFS_ERR_NOT_FOUND, NULL);
   209 
   209 
   210     return file;
   210     return file;
   211 } /* lzma_find_file */
   211 } /* lzma_find_file */
   212 
   212 
   213 
   213 
   289     switch (rc)
   289     switch (rc)
   290     {
   290     {
   291         case SZ_OK: /* Same as LZMA_RESULT_OK */
   291         case SZ_OK: /* Same as LZMA_RESULT_OK */
   292             break;
   292             break;
   293         case SZE_DATA_ERROR: /* Same as LZMA_RESULT_DATA_ERROR */
   293         case SZE_DATA_ERROR: /* Same as LZMA_RESULT_DATA_ERROR */
   294             __PHYSFS_setError(PHYSFS_ERR_CORRUPT); /*!!!FIXME: was "PHYSFS_ERR_DATA_ERROR" */
   294             PHYSFS_setErrorCode(PHYSFS_ERR_CORRUPT); /*!!!FIXME: was "PHYSFS_ERR_DATA_ERROR" */
   295             break;
   295             break;
   296         case SZE_OUTOFMEMORY:
   296         case SZE_OUTOFMEMORY:
   297             __PHYSFS_setError(PHYSFS_ERR_OUT_OF_MEMORY);
   297             PHYSFS_setErrorCode(PHYSFS_ERR_OUT_OF_MEMORY);
   298             break;
   298             break;
   299         case SZE_CRC_ERROR:
   299         case SZE_CRC_ERROR:
   300             __PHYSFS_setError(PHYSFS_ERR_CORRUPT);
   300             PHYSFS_setErrorCode(PHYSFS_ERR_CORRUPT);
   301             break;
   301             break;
   302         case SZE_NOTIMPL:
   302         case SZE_NOTIMPL:
   303             __PHYSFS_setError(PHYSFS_ERR_UNSUPPORTED);
   303             PHYSFS_setErrorCode(PHYSFS_ERR_UNSUPPORTED);
   304             break;
   304             break;
   305         case SZE_FAIL:
   305         case SZE_FAIL:
   306             __PHYSFS_setError(PHYSFS_ERR_OTHER_ERROR);  /* !!! FIXME: right? */
   306             PHYSFS_setErrorCode(PHYSFS_ERR_OTHER_ERROR);  /* !!! FIXME: right? */
   307             break;
   307             break;
   308         case SZE_ARCHIVE_ERROR:
   308         case SZE_ARCHIVE_ERROR:
   309             __PHYSFS_setError(PHYSFS_ERR_CORRUPT);  /* !!! FIXME: right? */
   309             PHYSFS_setErrorCode(PHYSFS_ERR_CORRUPT);  /* !!! FIXME: right? */
   310             break;
   310             break;
   311         default:
   311         default:
   312             __PHYSFS_setError(PHYSFS_ERR_OTHER_ERROR);
   312             PHYSFS_setErrorCode(PHYSFS_ERR_OTHER_ERROR);
   313     } /* switch */
   313     } /* switch */
   314 
   314 
   315     return rc;
   315     return rc;
   316 } /* lzma_err */
   316 } /* lzma_err */
   317 
   317 
   529     cb(callbackdata, odir, newstr);
   529     cb(callbackdata, odir, newstr);
   530     __PHYSFS_smallFree(newstr);
   530     __PHYSFS_smallFree(newstr);
   531 } /* doEnumCallback */
   531 } /* doEnumCallback */
   532 
   532 
   533 
   533 
   534 static void LZMA_enumerateFiles(PHYSFS_Dir *opaque, const char *dname,
   534 static void LZMA_enumerateFiles(void *opaque, const char *dname,
   535                                 int omitSymLinks, PHYSFS_EnumFilesCallback cb,
   535                                 PHYSFS_EnumFilesCallback cb,
   536                                 const char *origdir, void *callbackdata)
   536                                 const char *origdir, void *callbackdata)
   537 {
   537 {
   538     size_t dlen = strlen(dname),
   538     size_t dlen = strlen(dname),
   539            dlen_inc = dlen + ((dlen > 0) ? 1 : 0);
   539            dlen_inc = dlen + ((dlen > 0) ? 1 : 0);
   540     LZMAarchive *archive = (LZMAarchive *) opaque;
   540     LZMAarchive *archive = (LZMAarchive *) opaque;
   549         else
   549         else
   550         {
   550         {
   551             file = archive->files;
   551             file = archive->files;
   552         }
   552         }
   553 
   553 
   554     BAIL_IF_MACRO(file == NULL, PHYSFS_ERR_NO_SUCH_PATH, );
   554     BAIL_IF_MACRO(file == NULL, PHYSFS_ERR_NOT_FOUND, );
   555 
   555 
   556     while (file < lastFile)
   556     while (file < lastFile)
   557     {
   557     {
   558         const char * fname = file->item->Name;
   558         const char * fname = file->item->Name;
   559         const char * dirNameEnd = fname + dlen_inc;
   559         const char * dirNameEnd = fname + dlen_inc;
   573         file++;
   573         file++;
   574     }
   574     }
   575 } /* LZMA_enumerateFiles */
   575 } /* LZMA_enumerateFiles */
   576 
   576 
   577 
   577 
   578 static PHYSFS_Io *LZMA_openRead(PHYSFS_Dir *opaque, const char *name,
   578 static PHYSFS_Io *LZMA_openRead(void *opaque, const char *name)
   579                                 int *fileExists)
       
   580 {
   579 {
   581     LZMAarchive *archive = (LZMAarchive *) opaque;
   580     LZMAarchive *archive = (LZMAarchive *) opaque;
   582     LZMAfile *file = lzma_find_file(archive, name);
   581     LZMAfile *file = lzma_find_file(archive, name);
   583     PHYSFS_Io *io = NULL;
   582     PHYSFS_Io *io = NULL;
   584 
   583 
   585     *fileExists = (file != NULL);
   584     BAIL_IF_MACRO(file == NULL, PHYSFS_ERR_NOT_FOUND, NULL);
   586     BAIL_IF_MACRO(file == NULL, PHYSFS_ERR_NO_SUCH_PATH, NULL);
       
   587     BAIL_IF_MACRO(file->folder == NULL, PHYSFS_ERR_NOT_A_FILE, NULL);
   585     BAIL_IF_MACRO(file->folder == NULL, PHYSFS_ERR_NOT_A_FILE, NULL);
   588 
   586 
   589     file->position = 0;
   587     file->position = 0;
   590     file->folder->references++; /* Increase refcount for automatic cleanup... */
   588     file->folder->references++; /* Increase refcount for automatic cleanup... */
   591 
   589 
   596 
   594 
   597     return io;
   595     return io;
   598 } /* LZMA_openRead */
   596 } /* LZMA_openRead */
   599 
   597 
   600 
   598 
   601 static PHYSFS_Io *LZMA_openWrite(PHYSFS_Dir *opaque, const char *filename)
   599 static PHYSFS_Io *LZMA_openWrite(void *opaque, const char *filename)
   602 {
   600 {
   603     BAIL_MACRO(PHYSFS_ERR_READ_ONLY, NULL);
   601     BAIL_MACRO(PHYSFS_ERR_READ_ONLY, NULL);
   604 } /* LZMA_openWrite */
   602 } /* LZMA_openWrite */
   605 
   603 
   606 
   604 
   607 static PHYSFS_Io *LZMA_openAppend(PHYSFS_Dir *opaque, const char *filename)
   605 static PHYSFS_Io *LZMA_openAppend(void *opaque, const char *filename)
   608 {
   606 {
   609     BAIL_MACRO(PHYSFS_ERR_READ_ONLY, NULL);
   607     BAIL_MACRO(PHYSFS_ERR_READ_ONLY, NULL);
   610 } /* LZMA_openAppend */
   608 } /* LZMA_openAppend */
   611 
   609 
   612 
   610 
   613 static void LZMA_closeArchive(PHYSFS_Dir *opaque)
   611 static void LZMA_closeArchive(void *opaque)
   614 {
   612 {
   615     LZMAarchive *archive = (LZMAarchive *) opaque;
   613     LZMAarchive *archive = (LZMAarchive *) opaque;
   616 
   614 
   617 #if 0  /* !!! FIXME: you shouldn't have to do this. */
   615 #if 0  /* !!! FIXME: you shouldn't have to do this. */
   618     PHYSFS_uint32 fileIndex = 0, numFiles = archive->db.Database.NumFiles;
   616     PHYSFS_uint32 fileIndex = 0, numFiles = archive->db.Database.NumFiles;
   626     archive->stream.io->destroy(archive->stream.io);
   624     archive->stream.io->destroy(archive->stream.io);
   627     lzma_archive_exit(archive);
   625     lzma_archive_exit(archive);
   628 } /* LZMA_closeArchive */
   626 } /* LZMA_closeArchive */
   629 
   627 
   630 
   628 
   631 static int LZMA_remove(PHYSFS_Dir *opaque, const char *name)
   629 static int LZMA_remove(void *opaque, const char *name)
   632 {
   630 {
   633     BAIL_MACRO(PHYSFS_ERR_READ_ONLY, 0);
   631     BAIL_MACRO(PHYSFS_ERR_READ_ONLY, 0);
   634 } /* LZMA_remove */
   632 } /* LZMA_remove */
   635 
   633 
   636 
   634 
   637 static int LZMA_mkdir(PHYSFS_Dir *opaque, const char *name)
   635 static int LZMA_mkdir(void *opaque, const char *name)
   638 {
   636 {
   639     BAIL_MACRO(PHYSFS_ERR_READ_ONLY, 0);
   637     BAIL_MACRO(PHYSFS_ERR_READ_ONLY, 0);
   640 } /* LZMA_mkdir */
   638 } /* LZMA_mkdir */
   641 
   639 
   642 static int LZMA_stat(PHYSFS_Dir *opaque, const char *filename,
   640 static int LZMA_stat(void *opaque, const char *filename, PHYSFS_Stat *stat)
   643                      int *exists, PHYSFS_Stat *stat)
       
   644 {
   641 {
   645     const LZMAarchive *archive = (const LZMAarchive *) opaque;
   642     const LZMAarchive *archive = (const LZMAarchive *) opaque;
   646     const LZMAfile *file = lzma_find_file(archive, filename);
   643     const LZMAfile *file = lzma_find_file(archive, filename);
   647 
   644 
   648     *exists = (file != 0);
       
   649     if (!file)
   645     if (!file)
   650         return 0;
   646         return 0;
   651 
   647 
   652     if(file->item->IsDirectory)
   648     if(file->item->IsDirectory)
   653     {
   649     {
   676 } /* LZMA_stat */
   672 } /* LZMA_stat */
   677 
   673 
   678 
   674 
   679 const PHYSFS_Archiver __PHYSFS_Archiver_LZMA =
   675 const PHYSFS_Archiver __PHYSFS_Archiver_LZMA =
   680 {
   676 {
       
   677     CURRENT_PHYSFS_ARCHIVER_API_VERSION,
   681     {
   678     {
   682         "7Z",
   679         "7Z",
   683         "LZMA (7zip) format",
   680         "LZMA (7zip) format",
   684         "Dennis Schridde <devurandom@gmx.net>",
   681         "Dennis Schridde <devurandom@gmx.net>",
   685         "http://icculus.org/physfs/",
   682         "https://icculus.org/physfs/",
       
   683         0,  /* supportsSymlinks */
   686     },
   684     },
   687     LZMA_openArchive,        /* openArchive() method    */
   685     LZMA_openArchive,
   688     LZMA_enumerateFiles,     /* enumerateFiles() method */
   686     LZMA_enumerateFiles,
   689     LZMA_openRead,           /* openRead() method       */
   687     LZMA_openRead,
   690     LZMA_openWrite,          /* openWrite() method      */
   688     LZMA_openWrite,
   691     LZMA_openAppend,         /* openAppend() method     */
   689     LZMA_openAppend,
   692     LZMA_remove,             /* remove() method         */
   690     LZMA_remove,
   693     LZMA_mkdir,              /* mkdir() method          */
   691     LZMA_mkdir,
   694     LZMA_closeArchive,       /* closeArchive() method   */
   692     LZMA_stat,
   695     LZMA_stat                /* stat() method           */
   693     LZMA_closeArchive
   696 };
   694 };
   697 
   695 
   698 #endif  /* defined PHYSFS_SUPPORTS_7Z */
   696 #endif  /* defined PHYSFS_SUPPORTS_7Z */
   699 
   697 
   700 /* end of lzma.c ... */
   698 /* end of archiver_lzma.c ... */
   701 
   699