root/OpenSceneGraph/trunk/src/osgPlugins/zip/unzip.h
@
13041
| Revision 13041, 10.8 kB (checked in by robert, 14 months ago) | |
|---|---|
|
|
| Line | |
|---|---|
| 1 | |
| 2 | // UNZIPPING functions -- for unzipping. |
| 3 | // This file is a repackaged form of extracts from the zlib code available |
| 4 | // at www.gzip.org/zlib, by Jean-Loup Gailly and Mark Adler. The original |
| 5 | // copyright notice may be found in unzip.cpp. The repackaging was done |
| 6 | // by Lucian Wischik to simplify and extend its use in Windows/C++. Also |
| 7 | // encryption and unicode filenames have been added. |
| 8 | |
| 9 | |
| 10 | #ifndef _unzip_H |
| 11 | #define _unzip_H |
| 12 | // |
| 13 | |
| 14 | #ifdef ZIP_STD |
| 15 | #include <stdio.h> |
| 16 | #include <string.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <time.h> |
| 19 | #ifdef _MSC_VER |
| 20 | #include <sys/utime.h> // microsoft puts it here |
| 21 | #else |
| 22 | #include <utime.h> |
| 23 | #endif |
| 24 | #if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__MINGW32__) |
| 25 | #include <direct.h> |
| 26 | #define lumkdir(t) (_mkdir(t)) |
| 27 | #else |
| 28 | #include <unistd.h> |
| 29 | #define lumkdir(t) (mkdir(t,0755)) |
| 30 | #endif |
| 31 | #include <sys/types.h> |
| 32 | #include <sys/stat.h> |
| 33 | // |
| 34 | typedef unsigned short WORD; |
| 35 | #define _tcslen strlen |
| 36 | #define _tcsicmp stricmp |
| 37 | #define _tcsncpy strncpy |
| 38 | #define _tcsstr strstr |
| 39 | #define INVALID_HANDLE_VALUE 0 |
| 40 | #ifndef _T |
| 41 | #define _T(s) s |
| 42 | #endif |
| 43 | #ifndef S_IWUSR |
| 44 | #define S_IWUSR 0000200 |
| 45 | #define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR) |
| 46 | #define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG) |
| 47 | #endif |
| 48 | // |
| 49 | #else |
| 50 | #include <windows.h> |
| 51 | #include <stdio.h> |
| 52 | #include <stdlib.h> |
| 53 | #include <string.h> |
| 54 | #include <tchar.h> |
| 55 | #endif |
| 56 | |
| 57 | #ifdef ZIP_STD |
| 58 | #include <time.h> |
| 59 | #define DECLARE_HANDLE(name) struct name##__ { int unused; }; typedef struct name##__ *name |
| 60 | #ifndef MAX_PATH |
| 61 | #define MAX_PATH 1024 |
| 62 | #endif |
| 63 | typedef unsigned long DWORD; |
| 64 | typedef char TCHAR; |
| 65 | typedef FILE* HANDLE; |
| 66 | typedef time_t FILETIME; |
| 67 | #endif |
| 68 | |
| 69 | |
| 70 | #ifndef _zip_H |
| 71 | DECLARE_HANDLE(HZIP); |
| 72 | #endif |
| 73 | // An HZIP identifies a zip file that has been opened |
| 74 | |
| 75 | typedef DWORD ZRESULT; |
| 76 | // return codes from any of the zip functions. Listed later. |
| 77 | |
| 78 | typedef struct |
| 79 | { int index; // index of this file within the zip |
| 80 | TCHAR name[MAX_PATH]; // filename within the zip |
| 81 | DWORD attr; // attributes, as in GetFileAttributes. |
| 82 | FILETIME atime,ctime,mtime;// access, create, modify filetimes |
| 83 | long comp_size; // sizes of item, compressed and uncompressed. These |
| 84 | long unc_size; // may be -1 if not yet known (e.g. being streamed in) |
| 85 | } ZIPENTRY; |
| 86 | |
| 87 | HZIP OpenZip(const TCHAR *fn, const char *password); |
| 88 | HZIP OpenZip(void *z,unsigned int len, const char *password); |
| 89 | HZIP OpenZipHandle(HANDLE h, const char *password); |
| 90 | // OpenZip - opens a zip file and returns a handle with which you can |
| 91 | // subsequently examine its contents. You can open a zip file from: |
| 92 | // from a pipe: OpenZipHandle(hpipe_read,0); |
| 93 | // from a file (by handle): OpenZipHandle(hfile,0); |
| 94 | // from a file (by name): OpenZip("c:\\test.zip","password"); |
| 95 | // from a memory block: OpenZip(bufstart, buflen,0); |
| 96 | // If the file is opened through a pipe, then items may only be |
| 97 | // accessed in increasing order, and an item may only be unzipped once, |
| 98 | // although GetZipItem can be called immediately before and after unzipping |
| 99 | // it. If it's opened in any other way, then full random access is possible. |
| 100 | // Note: pipe input is not yet implemented. |
| 101 | // Note: zip passwords are ascii, not unicode. |
| 102 | // Note: for windows-ce, you cannot close the handle until after CloseZip. |
| 103 | // but for real windows, the zip makes its own copy of your handle, so you |
| 104 | // can close yours anytime. |
| 105 | |
| 106 | ZRESULT GetZipItem(HZIP hz, int index, ZIPENTRY *ze); |
| 107 | // GetZipItem - call this to get information about an item in the zip. |
| 108 | // If index is -1 and the file wasn't opened through a pipe, |
| 109 | // then it returns information about the whole zipfile |
| 110 | // (and in particular ze.index returns the number of index items). |
| 111 | // Note: the item might be a directory (ze.attr & FILE_ATTRIBUTE_DIRECTORY) |
| 112 | // See below for notes on what happens when you unzip such an item. |
| 113 | // Note: if you are opening the zip through a pipe, then random access |
| 114 | // is not possible and GetZipItem(-1) fails and you can't discover the number |
| 115 | // of items except by calling GetZipItem on each one of them in turn, |
| 116 | // starting at 0, until eventually the call fails. Also, in the event that |
| 117 | // you are opening through a pipe and the zip was itself created into a pipe, |
| 118 | // then then comp_size and sometimes unc_size as well may not be known until |
| 119 | // after the item has been unzipped. |
| 120 | |
| 121 | ZRESULT FindZipItem(HZIP hz, const TCHAR *name, bool ic, int *index, ZIPENTRY *ze); |
| 122 | // FindZipItem - finds an item by name. ic means 'insensitive to case'. |
| 123 | // It returns the index of the item, and returns information about it. |
| 124 | // If nothing was found, then index is set to -1 and the function returns |
| 125 | // an error code. |
| 126 | |
| 127 | ZRESULT UnzipItem(HZIP hz, int index, const TCHAR *fn); |
| 128 | ZRESULT UnzipItem(HZIP hz, int index, void *z,unsigned int len); |
| 129 | ZRESULT UnzipItemHandle(HZIP hz, int index, HANDLE h); |
| 130 | // UnzipItem - given an index to an item, unzips it. You can unzip to: |
| 131 | // to a pipe: UnzipItemHandle(hz,i, hpipe_write); |
| 132 | // to a file (by handle): UnzipItemHandle(hz,i, hfile); |
| 133 | // to a file (by name): UnzipItem(hz,i, ze.name); |
| 134 | // to a memory block: UnzipItem(hz,i, buf,buflen); |
| 135 | // In the final case, if the buffer isn't large enough to hold it all, |
| 136 | // then the return code indicates that more is yet to come. If it was |
| 137 | // large enough, and you want to know precisely how big, GetZipItem. |
| 138 | // Note: zip files are normally stored with relative pathnames. If you |
| 139 | // unzip with ZIP_FILENAME a relative pathname then the item gets created |
| 140 | // relative to the current directory - it first ensures that all necessary |
| 141 | // subdirectories have been created. Also, the item may itself be a directory. |
| 142 | // If you unzip a directory with ZIP_FILENAME, then the directory gets created. |
| 143 | // If you unzip it to a handle or a memory block, then nothing gets created |
| 144 | // and it emits 0 bytes. |
| 145 | ZRESULT SetUnzipBaseDir(HZIP hz, const TCHAR *dir); |
| 146 | // if unzipping to a filename, and it's a relative filename, then it will be relative to here. |
| 147 | // (defaults to current-directory). |
| 148 | |
| 149 | |
| 150 | ZRESULT CloseZip(HZIP hz); |
| 151 | // CloseZip - the zip handle must be closed with this function. |
| 152 | |
| 153 | unsigned int FormatZipMessage(ZRESULT code, TCHAR *buf,unsigned int len); |
| 154 | // FormatZipMessage - given an error code, formats it as a string. |
| 155 | // It returns the length of the error message. If buf/len points |
| 156 | // to a real buffer, then it also writes as much as possible into there. |
| 157 | |
| 158 | |
| 159 | // These are the result codes: |
| 160 | #define ZR_OK 0x00000000 // nb. the pseudo-code zr-recent is never returned, |
| 161 | #define ZR_RECENT 0x00000001 // but can be passed to FormatZipMessage. |
| 162 | // The following come from general system stuff (e.g. files not openable) |
| 163 | #define ZR_GENMASK 0x0000FF00 |
| 164 | #define ZR_NODUPH 0x00000100 // couldn't duplicate the handle |
| 165 | #define ZR_NOFILE 0x00000200 // couldn't create/open the file |
| 166 | #define ZR_NOALLOC 0x00000300 // failed to allocate some resource |
| 167 | #define ZR_WRITE 0x00000400 // a general error writing to the file |
| 168 | #define ZR_NOTFOUND 0x00000500 // couldn't find that file in the zip |
| 169 | #define ZR_MORE 0x00000600 // there's still more data to be unzipped |
| 170 | #define ZR_CORRUPT 0x00000700 // the zipfile is corrupt or not a zipfile |
| 171 | #define ZR_READ 0x00000800 // a general error reading the file |
| 172 | #define ZR_PASSWORD 0x00001000 // we didn't get the right password to unzip the file |
| 173 | // The following come from mistakes on the part of the caller |
| 174 | #define ZR_CALLERMASK 0x00FF0000 |
| 175 | #define ZR_ARGS 0x00010000 // general mistake with the arguments |
| 176 | #define ZR_NOTMMAP 0x00020000 // tried to ZipGetMemory, but that only works on mmap zipfiles, which yours wasn't |
| 177 | #define ZR_MEMSIZE 0x00030000 // the memory size is too small |
| 178 | #define ZR_FAILED 0x00040000 // the thing was already failed when you called this function |
| 179 | #define ZR_ENDED 0x00050000 // the zip creation has already been closed |
| 180 | #define ZR_MISSIZE 0x00060000 // the indicated input file size turned out mistaken |
| 181 | #define ZR_PARTIALUNZ 0x00070000 // the file had already been partially unzipped |
| 182 | #define ZR_ZMODE 0x00080000 // tried to mix creating/opening a zip |
| 183 | // The following come from bugs within the zip library itself |
| 184 | #define ZR_BUGMASK 0xFF000000 |
| 185 | #define ZR_NOTINITED 0x01000000 // initialisation didn't work |
| 186 | #define ZR_SEEK 0x02000000 // trying to seek in an unseekable file |
| 187 | #define ZR_NOCHANGE 0x04000000 // changed its mind on storage, but not allowed |
| 188 | #define ZR_FLATE 0x05000000 // an internal error in the de/inflation code |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | // e.g. |
| 195 | // |
| 196 | // SetCurrentDirectory("c:\\docs\\stuff"); |
| 197 | // HZIP hz = OpenZip("c:\\stuff.zip",0); |
| 198 | // ZIPENTRY ze; GetZipItem(hz,-1,&ze); int numitems=ze.index; |
| 199 | // for (int i=0; i<numitems; i++) |
| 200 | // { GetZipItem(hz,i,&ze); |
| 201 | // UnzipItem(hz,i,ze.name); |
| 202 | // } |
| 203 | // CloseZip(hz); |
| 204 | // |
| 205 | // |
| 206 | // HRSRC hrsrc = FindResource(hInstance,MAKEINTRESOURCE(1),RT_RCDATA); |
| 207 | // HANDLE hglob = LoadResource(hInstance,hrsrc); |
| 208 | // void *zipbuf=LockResource(hglob); |
| 209 | // unsigned int ziplen=SizeofResource(hInstance,hrsrc); |
| 210 | // HZIP hz = OpenZip(zipbuf, ziplen, 0); |
| 211 | // - unzip to a membuffer - |
| 212 | // ZIPENTRY ze; int i; FindZipItem(hz,"file.dat",true,&i,&ze); |
| 213 | // char *ibuf = new char[ze.unc_size]; |
| 214 | // UnzipItem(hz,i, ibuf, ze.unc_size); |
| 215 | // delete[] ibuf; |
| 216 | // - unzip to a fixed membuff - |
| 217 | // ZIPENTRY ze; int i; FindZipItem(hz,"file.dat",true,&i,&ze); |
| 218 | // char ibuf[1024]; ZRESULT zr=ZR_MORE; unsigned long totsize=0; |
| 219 | // while (zr==ZR_MORE) |
| 220 | // { zr = UnzipItem(hz,i, ibuf,1024); |
| 221 | // unsigned long bufsize=1024; if (zr==ZR_OK) bufsize=ze.unc_size-totsize; |
| 222 | // totsize+=bufsize; |
| 223 | // } |
| 224 | // - unzip to a pipe - |
| 225 | // HANDLE hwrite; HANDLE hthread=CreateWavReaderThread(&hwrite); |
| 226 | // int i; ZIPENTRY ze; FindZipItem(hz,"sound.wav",true,&i,&ze); |
| 227 | // UnzipItemHandle(hz,i, hwrite); |
| 228 | // CloseHandle(hwrite); |
| 229 | // WaitForSingleObject(hthread,INFINITE); |
| 230 | // CloseHandle(hwrite); CloseHandle(hthread); |
| 231 | // - finished - |
| 232 | // CloseZip(hz); |
| 233 | // // note: no need to free resources obtained through Find/Load/LockResource |
| 234 | // |
| 235 | // |
| 236 | // SetCurrentDirectory("c:\\docs\\pipedzipstuff"); |
| 237 | // HANDLE hread,hwrite; CreatePipe(&hread,&hwrite,0,0); |
| 238 | // CreateZipWriterThread(hwrite); |
| 239 | // HZIP hz = OpenZipHandle(hread,0); |
| 240 | // for (int i=0; ; i++) |
| 241 | // { ZIPENTRY ze; |
| 242 | // ZRESULT zr=GetZipItem(hz,i,&ze); if (zr!=ZR_OK) break; // no more |
| 243 | // UnzipItem(hz,i, ze.name); |
| 244 | // } |
| 245 | // CloseZip(hz); |
| 246 | // |
| 247 | // |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | // Now we indulge in a little skullduggery so that the code works whether |
| 253 | // the user has included just zip or both zip and unzip. |
| 254 | // Idea: if header files for both zip and unzip are present, then presumably |
| 255 | // the cpp files for zip and unzip are both present, so we will call |
| 256 | // one or the other of them based on a dynamic choice. If the header file |
| 257 | // for only one is present, then we will bind to that particular one. |
| 258 | ZRESULT CloseZipU(HZIP hz); |
| 259 | unsigned int FormatZipMessageU(ZRESULT code, TCHAR *buf,unsigned int len); |
| 260 | bool IsZipHandleU(HZIP hz); |
| 261 | #ifdef _zip_H |
| 262 | #undef CloseZip |
| 263 | #define CloseZip(hz) (IsZipHandleU(hz)?CloseZipU(hz):CloseZipZ(hz)) |
| 264 | #else |
| 265 | #define CloseZip CloseZipU |
| 266 | #define FormatZipMessage FormatZipMessageU |
| 267 | #endif |
| 268 | |
| 269 | |
| 270 | |
| 271 | #endif // _unzip_H |
Note: See TracBrowser
for help on using the browser.
