Merge pull request #4857 from t-nelson/Gotham_13.2_backports
[vuplus_xbmc] / xbmc / filesystem / ZipManager.h
1 #ifndef ZIP_MANAGER_H_
2 #define ZIP_MANAGER_H_
3
4 /*
5  *      Copyright (C) 2005-2013 Team XBMC
6  *      http://xbmc.org
7  *
8  *  This Program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2, or (at your option)
11  *  any later version.
12  *
13  *  This Program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with XBMC; see the file COPYING.  If not, see
20  *  <http://www.gnu.org/licenses/>.
21  *
22  */
23
24 // See http://www.pkware.com/documents/casestudies/APPNOTE.TXT
25 #define ZIP_LOCAL_HEADER 0x04034b50
26 #define ZIP_CENTRAL_HEADER 0x02014b50
27 #define ZIP_END_CENTRAL_HEADER 0x06054b50
28 #define LHDR_SIZE 30
29 #define CHDR_SIZE 46
30 #define ECDREC_SIZE 22
31
32 #include  "utils/StdString.h"
33
34 #include <memory.h>
35 #include <vector>
36 #include <map>
37
38 struct SZipEntry {
39   unsigned int header;
40   unsigned short version;
41   unsigned short flags;
42   unsigned short method;
43   unsigned short mod_time;
44   unsigned short mod_date;
45   unsigned int crc32;
46   unsigned int csize; // compressed size
47   unsigned int usize; // uncompressed size
48   unsigned short flength; // filename length
49   unsigned short elength; // extra field length (local file header)
50   unsigned short eclength; // extra field length (central file header)
51   unsigned short clength; // file comment length (central file header)
52   unsigned int lhdrOffset; // Relative offset of local header
53   int64_t offset;         // offset in file to compressed data
54   char name[255];
55
56   SZipEntry()
57   {
58     header = 0;
59     version = 0;
60     flags = 0;
61     method = 0;
62     mod_time = 0;
63     mod_date = 0;
64     crc32 = 0;
65     csize = 0;
66     usize = 0;
67     flength = 0;
68     elength = 0;
69     eclength = 0;
70     clength = 0;
71     lhdrOffset = 0;
72     offset = 0;
73     name[0] = '\0';
74   }
75
76   SZipEntry(const SZipEntry& SNewItem)
77   {
78     memcpy(&header,&SNewItem.header,sizeof(unsigned int));
79     memcpy(&version,&SNewItem.version,sizeof(unsigned short));
80     memcpy(&flags,&SNewItem.flags,sizeof(unsigned short));
81     memcpy(&method,&SNewItem.method,sizeof(unsigned short));
82     memcpy(&mod_time,&SNewItem.mod_time,sizeof(unsigned short));
83     memcpy(&mod_date,&SNewItem.mod_date,sizeof(unsigned short));
84     memcpy(&crc32,&SNewItem.crc32,sizeof(unsigned int));
85     memcpy(&csize,&SNewItem.csize,sizeof(unsigned int));
86     memcpy(&usize,&SNewItem.usize,sizeof(unsigned int));
87     memcpy(&flength,&SNewItem.flength,sizeof(unsigned short));
88     memcpy(&elength,&SNewItem.elength,sizeof(unsigned short));
89     memcpy(&eclength,&SNewItem.eclength,sizeof(unsigned short));
90     memcpy(&clength,&SNewItem.clength,sizeof(unsigned short));
91     memcpy(&lhdrOffset,&SNewItem.lhdrOffset,sizeof(unsigned int));
92     memcpy(&offset,&SNewItem.offset,sizeof(int64_t));
93     memcpy(name,SNewItem.name,255*sizeof(char));
94   }
95 };
96
97 class CZipManager
98 {
99 public:
100   CZipManager();
101   ~CZipManager();
102
103   bool GetZipList(const CStdString& strPath, std::vector<SZipEntry>& items);
104   bool GetZipEntry(const CStdString& strPath, SZipEntry& item);
105   bool ExtractArchive(const CStdString& strArchive, const CStdString& strPath);
106   void CleanUp(const CStdString& strArchive, const CStdString& strPath); // deletes extracted archive. use with care!
107   void release(const CStdString& strPath); // release resources used by list zip
108   static void readHeader(const char* buffer, SZipEntry& info);
109   static void readCHeader(const char* buffer, SZipEntry& info);
110 private:
111   std::map<CStdString,std::vector<SZipEntry> > mZipMap;
112   std::map<CStdString,int64_t> mZipDate;
113 };
114
115 extern CZipManager g_ZipManager;
116
117 #endif