archivers/mix.c
changeset 650 298b8bb26775
parent 648 5c993684b8f2
child 657 dad3b5c307a9
equal deleted inserted replaced
649:85aeb90378bc 650:298b8bb26775
    78     MIXentry *entry; /* pointer to the MIX entry */
    78     MIXentry *entry; /* pointer to the MIX entry */
    79     MIXinfo *info; /* pointer to our MIXinfo */
    79     MIXinfo *info; /* pointer to our MIXinfo */
    80     void *handle; /* filehandle */
    80     void *handle; /* filehandle */
    81 } MIXfileinfo;
    81 } MIXfileinfo;
    82 
    82 
    83 static void MIX_dirClose(void *opaque);
    83 static PHYSFS_sint64 MIX_read(fvoid *opaque, void *buffer,
    84 static PHYSFS_sint64 MIX_read(FileHandle *handle, void *buffer,
       
    85                               PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
    84                               PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
    86 static PHYSFS_sint64 MIX_write(FileHandle *handle, const void *buffer,
    85 static PHYSFS_sint64 MIX_write(fvoid *opaque, const void *buffer,
    87                                PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
    86                                PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
    88 static int MIX_eof(FileHandle *handle);
    87 static int MIX_eof(fvoid *opaque);
    89 static PHYSFS_sint64 MIX_tell(FileHandle *handle);
    88 static PHYSFS_sint64 MIX_tell(fvoid *opaque);
    90 static int MIX_seek(FileHandle *handle, PHYSFS_uint64 offset);
    89 static int MIX_seek(fvoid *opaque, PHYSFS_uint64 offset);
    91 static PHYSFS_sint64 MIX_fileLength(FileHandle *handle);
    90 static PHYSFS_sint64 MIX_fileLength(fvoid *opaque);
    92 static int MIX_fileClose(FileHandle *handle);
    91 static int MIX_fileClose(fvoid *opaque);
    93 static int MIX_isArchive(const char *filename, int forWriting);
    92 static int MIX_isArchive(const char *filename, int forWriting);
    94 static void *MIX_openArchive(const char *name, int forWriting);
    93 static void *MIX_openArchive(const char *name, int forWriting);
    95 static LinkedStringList *MIX_enumerateFiles(void *opaque,
    94 static LinkedStringList *MIX_enumerateFiles(dvoid *opaque,
    96                                             const char *dirname,
    95                                             const char *dirname,
    97                                             int omitSymLinks);
    96                                             int omitSymLinks);
    98 static int MIX_exists(void *opaque, const char *name);
    97 static int MIX_exists(dvoid *opaque, const char *name);
    99 static int MIX_isDirectory(void *opaque, const char *name, int *fileExists);
    98 static int MIX_isDirectory(dvoid *opaque, const char *name, int *fileExists);
   100 static int MIX_isSymLink(void *opaque, const char *name, int *fileExists);
    99 static int MIX_isSymLink(dvoid *opaque, const char *name, int *fileExists);
   101 static PHYSFS_sint64 MIX_getLastModTime(void *opaque, const char *n, int *e);
   100 static PHYSFS_sint64 MIX_getLastModTime(dvoid *opaque, const char *n, int *e);
   102 static FileHandle *MIX_openRead(void *opaque, const char *name, int *exist);
   101 static fvoid *MIX_openRead(dvoid *opaque, const char *name, int *exist);
   103 static FileHandle *MIX_openWrite(void *opaque, const char *name);
   102 static fvoid *MIX_openWrite(dvoid *opaque, const char *name);
   104 static FileHandle *MIX_openAppend(void *opaque, const char *name);
   103 static fvoid *MIX_openAppend(dvoid *opaque, const char *name);
   105 static int MIX_remove(void *opaque, const char *name);
   104 static int MIX_remove(dvoid *opaque, const char *name);
   106 static int MIX_mkdir(void *opaque, const char *name);
   105 static int MIX_mkdir(dvoid *opaque, const char *name);
       
   106 static void MIX_dirClose(dvoid *opaque);
   107 
   107 
   108 const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_MIX =
   108 const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_MIX =
   109 {
   109 {
   110     "MIX",
   110     "MIX",
   111     "Westwood archive (Tiberian Dawn / Red Alert)",
   111     "Westwood archive (Tiberian Dawn / Red Alert)",
   112     "Sebastian Steinhauer <steini@steini-welt.de>",
   112     "Sebastian Steinhauer <steini@steini-welt.de>",
   113     "http://icculus.org/physfs/",
   113     "http://icculus.org/physfs/",
   114 };
   114 };
   115 
   115 
   116 
   116 
   117 static const FileFunctions __PHYSFS_FileFunctions_MIX =
   117 const PHYSFS_Archiver __PHYSFS_Archiver_MIX =
   118 {
       
   119     MIX_read,       /* read() method       */
       
   120     MIX_write,      /* write() method      */
       
   121     MIX_eof,        /* eof() method        */
       
   122     MIX_tell,       /* tell() method       */
       
   123     MIX_seek,       /* seek() method       */
       
   124     MIX_fileLength, /* fileLength() method */
       
   125     MIX_fileClose   /* fileClose() method  */
       
   126 };
       
   127 
       
   128 
       
   129 const DirFunctions __PHYSFS_DirFunctions_MIX =
       
   130 {
   118 {
   131     &__PHYSFS_ArchiveInfo_MIX,
   119     &__PHYSFS_ArchiveInfo_MIX,
   132     MIX_isArchive,          /* isArchive() method      */
   120     MIX_isArchive,          /* isArchive() method      */
   133     MIX_openArchive,        /* openArchive() method    */
   121     MIX_openArchive,        /* openArchive() method    */
   134     MIX_enumerateFiles,     /* enumerateFiles() method */
   122     MIX_enumerateFiles,     /* enumerateFiles() method */
   139     MIX_openRead,           /* openRead() method       */
   127     MIX_openRead,           /* openRead() method       */
   140     MIX_openWrite,          /* openWrite() method      */
   128     MIX_openWrite,          /* openWrite() method      */
   141     MIX_openAppend,         /* openAppend() method     */
   129     MIX_openAppend,         /* openAppend() method     */
   142     MIX_remove,             /* remove() method         */
   130     MIX_remove,             /* remove() method         */
   143     MIX_mkdir,              /* mkdir() method          */
   131     MIX_mkdir,              /* mkdir() method          */
   144     MIX_dirClose            /* dirClose() method       */
   132     MIX_dirClose,           /* dirClose() method       */
       
   133     MIX_read,               /* read() method           */
       
   134     MIX_write,              /* write() method          */
       
   135     MIX_eof,                /* eof() method            */
       
   136     MIX_tell,               /* tell() method           */
       
   137     MIX_seek,               /* seek() method           */
       
   138     MIX_fileLength,         /* fileLength() method     */
       
   139     MIX_fileClose           /* fileClose() method      */
   145 };
   140 };
       
   141 
   146 
   142 
   147 static PHYSFS_uint32 MIX_hash(const char *name)
   143 static PHYSFS_uint32 MIX_hash(const char *name)
   148 {
   144 {
   149     PHYSFS_uint32 id = 0;
   145     PHYSFS_uint32 id = 0;
   150     PHYSFS_uint32 a = 0;
   146     PHYSFS_uint32 a = 0;
   174 
   170 
   175     return(id);
   171     return(id);
   176 } /* MIX_hash */
   172 } /* MIX_hash */
   177 
   173 
   178 
   174 
   179 static void MIX_dirClose(void *opaque)
   175 static void MIX_dirClose(dvoid *opaque)
   180 {
   176 {
   181     MIXinfo *info = ((MIXinfo *) opaque);
   177     MIXinfo *info = ((MIXinfo *) opaque);
   182     free(info->entry);
   178     free(info->entry);
   183     free(info->filename);
   179     free(info->filename);
   184 } /* MIX_dirClose */
   180 } /* MIX_dirClose */
   185 
   181 
   186 
   182 
   187 static PHYSFS_sint64 MIX_read(FileHandle *handle, void *buffer,
   183 static PHYSFS_sint64 MIX_read(fvoid *opaque, void *buffer,
   188                               PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
   184                               PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
   189 {
   185 {
   190     MIXfileinfo *finfo = (MIXfileinfo*)handle->opaque;
   186     MIXfileinfo *finfo = (MIXfileinfo *) opaque;
   191     MIXentry *entry = finfo->entry;
   187     MIXentry *entry = finfo->entry;
   192     PHYSFS_uint32 read;
   188     PHYSFS_uint32 read;
   193     
   189     
   194     /* set position in the archive */
   190     /* set position in the archive */
   195     __PHYSFS_platformSeek(finfo->handle,
   191     __PHYSFS_platformSeek(finfo->handle,
   206     
   202     
   207     return(read);
   203     return(read);
   208 } /* MIX_read */
   204 } /* MIX_read */
   209 
   205 
   210 
   206 
   211 static PHYSFS_sint64 MIX_write(FileHandle *handle, const void *buffer,
   207 static PHYSFS_sint64 MIX_write(fvoid *opaque, const void *buffer,
   212                                PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
   208                                PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
   213 {
   209 {
   214     BAIL_MACRO(ERR_NOT_SUPPORTED, -1);
   210     BAIL_MACRO(ERR_NOT_SUPPORTED, -1);
   215 } /* MIX_write */
   211 } /* MIX_write */
   216 
   212 
   217 
   213 
   218 static int MIX_eof(FileHandle *handle)
   214 static int MIX_eof(fvoid *opaque)
   219 {
   215 {
   220     MIXfileinfo *fifo = (MIXfileinfo *) handle->opaque;
   216     MIXfileinfo *fifo = (MIXfileinfo *) opaque;
   221     return(fifo->cur_pos >= fifo->size);
   217     return(fifo->cur_pos >= fifo->size);
   222 } /* MIX_eof */
   218 } /* MIX_eof */
   223 
   219 
   224 
   220 
   225 static PHYSFS_sint64 MIX_tell(FileHandle *handle)
   221 static PHYSFS_sint64 MIX_tell(fvoid *opaque)
   226 {
   222 {
   227     return(((MIXfileinfo *) (handle->opaque))->cur_pos);
   223     return(((MIXfileinfo *) opaque)->cur_pos);
   228 } /* MIX_tell */
   224 } /* MIX_tell */
   229 
   225 
   230 
   226 
   231 static int MIX_seek(FileHandle *handle, PHYSFS_uint64 offset)
   227 static int MIX_seek(fvoid *opaque, PHYSFS_uint64 offset)
   232 {
   228 {
   233     MIXfileinfo *h = (MIXfileinfo *) (handle->opaque);
   229     MIXfileinfo *h = (MIXfileinfo *) opaque;
   234     
   230     
   235     BAIL_IF_MACRO(offset < 0, ERR_INVALID_ARGUMENT, 0);
   231     BAIL_IF_MACRO(offset < 0, ERR_INVALID_ARGUMENT, 0);
   236     BAIL_IF_MACRO(offset >= h->size, ERR_PAST_EOF, 0);
   232     BAIL_IF_MACRO(offset >= h->size, ERR_PAST_EOF, 0);
   237     h->cur_pos = offset;
   233     h->cur_pos = offset;
   238     return(1);
   234     return(1);
   239 } /* MIX_seek */
   235 } /* MIX_seek */
   240 
   236 
   241 
   237 
   242 static PHYSFS_sint64 MIX_fileLength(FileHandle *handle)
   238 static PHYSFS_sint64 MIX_fileLength(fvoid *opaque)
   243 {
   239 {
   244     return (((MIXfileinfo *) (handle->opaque))->size);
   240     return (((MIXfileinfo *) opaque)->size);
   245 } /* MIX_fileLength */
   241 } /* MIX_fileLength */
   246 
   242 
   247 
   243 
   248 static int MIX_fileClose(FileHandle *handle)
   244 static int MIX_fileClose(fvoid *opaque)
   249 {
   245 {
   250     MIXfileinfo *finfo = (MIXfileinfo *) handle->opaque;
   246     MIXfileinfo *finfo = (MIXfileinfo *) opaque;
   251     __PHYSFS_platformClose(finfo->handle);
   247     __PHYSFS_platformClose(finfo->handle);
   252     free(finfo);
   248     free(finfo);
   253     free(handle);
       
   254     return(1);
   249     return(1);
   255 } /* MIX_fileClose */
   250 } /* MIX_fileClose */
   256 
   251 
   257 
   252 
   258 static int MIX_isArchive(const char *filename, int forWriting)
   253 static int MIX_isArchive(const char *filename, int forWriting)
   259 {
   254 {
   260     /* TODO:
   255     /* !!! FIXME:
   261             write a simple detection routine for MIX files.
   256             write a simple detection routine for MIX files.
   262             Unfortunaly MIX files have no ID in the header.
   257             Unfortunaly MIX files have no ID in the header.
   263     */
   258     */
   264     return(1);
   259     return(1);
   265 } /* MIX_isArchive */
   260 } /* MIX_isArchive */
   357 
   352 
   358     return(NULL);
   353     return(NULL);
   359 } /* MIX_openArchive */
   354 } /* MIX_openArchive */
   360 
   355 
   361 
   356 
   362 static LinkedStringList *MIX_enumerateFiles(void *opaque,
   357 static LinkedStringList *MIX_enumerateFiles(dvoid *opaque,
   363                                             const char *dirname,
   358                                             const char *dirname,
   364                                             int omitSymLinks)
   359                                             int omitSymLinks)
   365 {
   360 {
   366     LinkedStringList *retval = NULL, *p = NULL;
   361     LinkedStringList *retval = NULL, *p = NULL;
   367     MIXinfo *info = (MIXinfo*) opaque;
   362     MIXinfo *info = (MIXinfo*) opaque;
   397     /* nothing found... :( */
   392     /* nothing found... :( */
   398     return(NULL);
   393     return(NULL);
   399 } /* MIX_find_entry */
   394 } /* MIX_find_entry */
   400 
   395 
   401 
   396 
   402 static int MIX_exists(void *opaque, const char *name)
   397 static int MIX_exists(dvoid *opaque, const char *name)
   403 {
   398 {
   404     return(MIX_find_entry(((MIXinfo *) opaque), name) != NULL);
   399     return(MIX_find_entry(((MIXinfo *) opaque), name) != NULL);
   405 } /* MIX_exists */
   400 } /* MIX_exists */
   406 
   401 
   407 
   402 
   408 static int MIX_isDirectory(void *opaque, const char *name, int *fileExists)
   403 static int MIX_isDirectory(dvoid *opaque, const char *name, int *fileExists)
   409 {
   404 {
   410     *fileExists = MIX_exists(opaque, name);
   405     *fileExists = MIX_exists(opaque, name);
   411     return(0);  /* never directories in a MIX */
   406     return(0);  /* never directories in a MIX */
   412 } /* MIX_isDirectory */
   407 } /* MIX_isDirectory */
   413 
   408 
   414 
   409 
   415 static int MIX_isSymLink(void *opaque, const char *name, int *fileExists)
   410 static int MIX_isSymLink(dvoid *opaque, const char *name, int *fileExists)
   416 {
   411 {
   417     *fileExists = MIX_exists(opaque, name);
   412     *fileExists = MIX_exists(opaque, name);
   418     return(0);  /* never symlinks in a MIX. */
   413     return(0);  /* never symlinks in a MIX. */
   419 } /* MIX_isSymLink */
   414 } /* MIX_isSymLink */
   420 
   415 
   421 
   416 
   422 static PHYSFS_sint64 MIX_getLastModTime(void *opaque,
   417 static PHYSFS_sint64 MIX_getLastModTime(dvoid *opaque,
   423                                         const char *name,
   418                                         const char *name,
   424                                         int *fileExists)
   419                                         int *fileExists)
   425 {
   420 {
   426     BAIL_MACRO(ERR_NOT_SUPPORTED, 0);  /* !!! FIXME: return .MIX's modtime. */
   421     BAIL_MACRO(ERR_NOT_SUPPORTED, 0);  /* !!! FIXME: return .MIX's modtime. */
   427 } /* MIX_getLastModTime */
   422 } /* MIX_getLastModTime */
   428 
   423 
   429 
   424 
   430 static FileHandle *MIX_openRead(void *opaque, const char *fnm, int *fileExists)
   425 static fvoid *MIX_openRead(dvoid *opaque, const char *fnm, int *fileExists)
   431 {
   426 {
   432     FileHandle *retval;
       
   433     MIXinfo *info = ((MIXinfo*) opaque);
   427     MIXinfo *info = ((MIXinfo*) opaque);
   434     MIXfileinfo *finfo;
   428     MIXfileinfo *finfo;
   435     MIXentry *entry;
   429     MIXentry *entry;
   436     
   430     
   437     /* try to find this file */
   431     /* try to find this file */
   439     BAIL_IF_MACRO(entry == NULL, ERR_NO_SUCH_FILE, NULL);
   433     BAIL_IF_MACRO(entry == NULL, ERR_NO_SUCH_FILE, NULL);
   440     
   434     
   441     /* allocate a MIX handle */
   435     /* allocate a MIX handle */
   442     finfo = (MIXfileinfo *) malloc(sizeof (MIXfileinfo));
   436     finfo = (MIXfileinfo *) malloc(sizeof (MIXfileinfo));
   443     BAIL_IF_MACRO(finfo == NULL, ERR_OUT_OF_MEMORY, NULL);
   437     BAIL_IF_MACRO(finfo == NULL, ERR_OUT_OF_MEMORY, NULL);
   444     
       
   445     /* allocate a filehandle */
       
   446     retval = (FileHandle*) malloc(sizeof (FileHandle));
       
   447     if (!retval)
       
   448     {
       
   449         free(finfo);
       
   450         BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
       
   451     } /* if */
       
   452 
   438 
   453     /* open the archive */
   439     /* open the archive */
   454     finfo->handle = __PHYSFS_platformOpenRead(info->filename);
   440     finfo->handle = __PHYSFS_platformOpenRead(info->filename);
   455     if(!finfo->handle)
   441     if(!finfo->handle)
   456     {
   442     {
   457         free(finfo);
   443         free(finfo);
   458         free(retval);
       
   459         return(NULL);
   444         return(NULL);
   460     };
   445     } /* if */
   461     
   446     
   462     /* setup structures */
   447     /* setup structures */
   463     finfo->cur_pos = 0;
   448     finfo->cur_pos = 0;
   464     finfo->info = info;
   449     finfo->info = info;
   465     finfo->entry = entry;
   450     finfo->entry = entry;
   466     finfo->size = entry->end_offset - entry->start_offset;
   451     finfo->size = entry->end_offset - entry->start_offset;
   467     
   452 
   468     retval->opaque = (void *) finfo;
   453     return(finfo);
   469     retval->funcs = &__PHYSFS_FileFunctions_MIX;
       
   470 
       
   471     return(retval);
       
   472 } /* MIX_openRead */
   454 } /* MIX_openRead */
   473 
   455 
   474 
   456 
   475 static FileHandle *MIX_openWrite(void *opaque, const char *name)
   457 static fvoid *MIX_openWrite(dvoid *opaque, const char *name)
   476 {
   458 {
   477     BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
   459     BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
   478 } /* MIX_openWrite */
   460 } /* MIX_openWrite */
   479 
   461 
   480 
   462 
   481 static FileHandle *MIX_openAppend(void *opaque, const char *name)
   463 static fvoid *MIX_openAppend(dvoid *opaque, const char *name)
   482 {
   464 {
   483     BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
   465     BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
   484 } /* MIX_openAppend */
   466 } /* MIX_openAppend */
   485 
   467 
   486 
   468 
   487 static int MIX_remove(void *opaque, const char *name)
   469 static int MIX_remove(dvoid *opaque, const char *name)
   488 {
   470 {
   489     BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
   471     BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
   490 } /* MIX_remove */
   472 } /* MIX_remove */
   491 
   473 
   492 
   474 
   493 static int MIX_mkdir(void *opaque, const char *name)
   475 static int MIX_mkdir(dvoid *opaque, const char *name)
   494 {
   476 {
   495     BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
   477     BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
   496 } /* MIX_mkdir */
   478 } /* MIX_mkdir */
   497 
   479 
   498 #endif  /* defined PHYSFS_SUPPORTS_MIX */
   480 #endif  /* defined PHYSFS_SUPPORTS_MIX */