fileformat.c 5.21 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
#include <stdlib.h>
#include <boolean.h>
#include <string.h>
#include "file/file_path.h"
#include "zlib.h"

#include "fileformat.h"
#include "unzip.h"
#include "log.h"

11
static const char *gme_allowed_exts[] = {
12
13
14
15
16
17
18
19
20
21
22
    "ay","AY",
    "gbs","GBS",
    "gym","GYM",
    "hes","HES",
    "kss","KSS",
    "nsf","NSF",
    "nsfe","NSFE",
    "sap","SAP",
    "spc","SPC",
    "vgm","VGM",
    "vgz","VGZ"
23
24
25
26
};

static bool is_gme_allowed_ext(char *ext)
{
Libretro-Admin's avatar
Libretro-Admin committed
27
28
29
30
31
32
33
34
35
   int i;
   int arr_length;
   arr_length = sizeof(gme_allowed_exts) / sizeof(char*);
   for(i=0;i<arr_length;i++)
   {
      if(strcmp(ext,gme_allowed_exts[i])==0)
         return true;
   }
   return false;
36
37
38
39
}

static bool uncompress_file_data(file_data** fd)
{
Libretro-Admin's avatar
Libretro-Admin committed
40
41
42
43
44
   int srcLen,dstLen;
   int err            = -1;
   z_stream strm      = {0};
   file_data* src_fd  = *fd;
   file_data* dest_fd = NULL;
Libretro-Admin's avatar
Libretro-Admin committed
45

Libretro-Admin's avatar
Libretro-Admin committed
46
47
48
49
50
51
52
53
54
55
56
   srcLen = src_fd->length;
   memcpy(&dstLen,&(src_fd->data[src_fd->length-4]),4);
   dest_fd = malloc(sizeof(file_data));
   dest_fd->length = dstLen;
   dest_fd->name = calloc(strlen(src_fd->name)+1,sizeof(char));
   strcpy(dest_fd->name,src_fd->name);
   dest_fd->data = malloc(dstLen * sizeof(char));
   strm.total_in  = strm.avail_in  = srcLen;
   strm.total_out = strm.avail_out = dstLen;
   strm.next_in   = (Bytef *) src_fd->data;
   strm.next_out  = (Bytef *) dest_fd->data;
57

Libretro-Admin's avatar
Libretro-Admin committed
58
59
60
   strm.zalloc = Z_NULL;
   strm.zfree  = Z_NULL;
   strm.opaque = Z_NULL;
61
62


Libretro-Admin's avatar
Libretro-Admin committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
   err = inflateInit2(&strm, (15 + 32)); //15 window bits, and the +32 tells zlib to to detect if using gzip or zlib
   if (err == Z_OK)
   {
      err = inflate(&strm, Z_FINISH);
      if (err != Z_STREAM_END)
      {
         inflateEnd(&strm);
         return false;
      }
   }
   else
   {
      inflateEnd(&strm);
      return false;
   }
   inflateEnd(&strm);
   free(src_fd->data);
   free(src_fd->name);
   free(src_fd);
   *fd = dest_fd;
   return true;
84
85
}

86
static bool get_files_from_zip(const char *path, file_data ***dest_files, int *dest_numfiles)
87
{
Libretro-Admin's avatar
Libretro-Admin committed
88
89
90
91
92
93
94
95
   unzFile uf = NULL;
   unz_global_info64 gi;
   unz_file_info64 file_info;
   int i;
   char filename_inzip[256];
   char *ext;
   file_data **files;
   int numfiles,position;
Libretro-Admin's avatar
Libretro-Admin committed
96

Libretro-Admin's avatar
Libretro-Admin committed
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
   //load zip content
   uf = unzOpen64(path);
   unzGetGlobalInfo64(uf,&gi);
   numfiles = (int)gi.number_entry;
   files = malloc(sizeof(file_data*) * numfiles);
   position = 0;
   for(i=0;i<gi.number_entry;i++)
   {
      void* buf;
      int err;
      int bytes_read;
      uInt size_buf = 8192;
      //read compressed file info
      err = unzGetCurrentFileInfo64(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0);
      if(err!=UNZ_OK)
      {
         return false;
      }
      if(filename_inzip[file_info.size_filename -1]=='/')
         ext = strrchr(filename_inzip,'/');
      else
         ext = strrchr(filename_inzip,'.') + 1;
      if(is_gme_allowed_ext(ext))
      {
         //get file name in zip
         files[position] = malloc(sizeof(file_data));
         files[position]->name = calloc(strlen(filename_inzip)+1,sizeof(char));
         strcpy(files[position]->name,filename_inzip);
         //allocate uncompressed data buffer
         files[position]->length= sizeof(char) * file_info.uncompressed_size;
         files[position]->data = (char*)malloc(files[position]->length);
         //setup buffer
         bytes_read = 0;
         buf = (void*)malloc(size_buf);
         if (buf==NULL)
132
            return false;
Libretro-Admin's avatar
Libretro-Admin committed
133
134
135
136
137
138
139
140
141
142
143
         //read file from zip
         err = unzOpenCurrentFilePassword(uf,NULL);
         if (err!=UNZ_OK)
            return false;
         //get data from zip
         do 
         {
            err = unzReadCurrentFile(uf,buf,size_buf);
            if(err<0)
               return false;
            if(err>0)
144
            {
Libretro-Admin's avatar
Libretro-Admin committed
145
146
147
148
149
150
               memcpy(files[position]->data + bytes_read,buf,err * sizeof(char));
               bytes_read += err;
            }
         } while (err>0);
         if(buf!=NULL)
            free(buf);
151

Libretro-Admin's avatar
Libretro-Admin committed
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
         if(strcmp(ext,"vgz")==0)
            if(!uncompress_file_data(&(files[position])))
               return false;
         position++;
      }
      else
      {
         numfiles--;
      }
      if ((i+1)<gi.number_entry)
         unzGoToNextFile(uf);
   }
   files = realloc(files,sizeof(file_data*) * numfiles);
   *dest_files = files;
   *dest_numfiles = numfiles;
   return true;
168
169
}

170
bool get_file_data(const char *path,file_data ***dest_files, int *dest_numfiles)
171
{
Libretro-Admin's avatar
Libretro-Admin committed
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
   //local variables
   FILE *fp;
   file_data *fd;
   file_data **files;
   //get file name and extension
   const char *bname = path_basename(path);
   char *ext         = strrchr(path,'.') +1;
   //get file data
   if(strcmp(ext,"zip")==0)
      return get_files_from_zip(path,dest_files,dest_numfiles);
   if (!(fp = fopen(path,"rb")))
      return false;
   files = malloc(sizeof(file_data*));
   fd    = malloc(sizeof(file_data));
   //get file length
   fseek (fp,0,SEEK_END);
   fd->length = ftell(fp);
   rewind(fp);
   //get file data
   fd->data = malloc(sizeof(char)*fd->length);
   fread(fd->data,1,fd->length,fp);
   fclose(fp);
   fd->name = calloc(strlen(bname)+1,sizeof(char));
   strcpy(fd->name,bname);
   if(strcmp(ext,"vgz")==0)
   {
      if(!uncompress_file_data(&fd))
         return false;
   }
   files[0]       = fd;
   *dest_files    = files;
   *dest_numfiles = 1;
   return true;
205
}