f4b8e3444581545fc6dbc288ea051fa2ebca808a
[framework/web/wrt-commons.git] / modules / core / include / dpl / zip_input.h
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /*
17  * @file        zip_input.h
18  * @author      Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
19  * @version     1.0
20  * @brief       This file is the header file of zip input
21  */
22 #ifndef DPL_ZIP_INPUT_H
23 #define DPL_ZIP_INPUT_H
24
25 #include <dpl/exception.h>
26 #include <dpl/noncopyable.h>
27 #include <dpl/abstract_input.h>
28 #include <utility>
29 #include <vector>
30 #include <string>
31
32 namespace DPL
33 {
34 class ZipInput
35     : private Noncopyable
36 {
37 public:
38     class Exception
39     {
40     public:
41         DECLARE_EXCEPTION_TYPE(DPL::Exception, Base)
42         DECLARE_EXCEPTION_TYPE(Base, OpenFailed)
43         DECLARE_EXCEPTION_TYPE(Base, ReadGlobalInfoFailed)
44         DECLARE_EXCEPTION_TYPE(Base, ReadGlobalCommentFailed)
45         DECLARE_EXCEPTION_TYPE(Base, SeekFileFailed)
46         DECLARE_EXCEPTION_TYPE(Base, FileInfoFailed)
47         DECLARE_EXCEPTION_TYPE(Base, OpenFileFailed)
48         DECLARE_EXCEPTION_TYPE(Base, ReadFileFailed)
49     };
50
51     typedef std::pair<size_t, size_t> FileHandle;
52
53     struct FileDateTime
54     {
55         unsigned int second; //< seconds after the minute - [0,59]
56         unsigned int minute; //< minutes after the hour - [0,59]
57         unsigned int hour;   //< hours since midnight - [0,23]
58         unsigned int day;    //< day of the month - [1,31]
59         unsigned int month;  //< months since January - [0,11]
60         unsigned int year;   //< years - [1980..2044]
61
62         FileDateTime()
63             :   second(0),
64                 minute(0),
65                 hour(0),
66                 day(0),
67                 month(0),
68                 year(0)
69         {
70         }
71
72         FileDateTime(unsigned int secondArg,
73                      unsigned int minuteArg,
74                      unsigned int hourArg,
75                      unsigned int dayArg,
76                      unsigned int monthArg,
77                      unsigned int yearArg)
78             :   second(secondArg),
79                 minute(minuteArg),
80                 hour(hourArg),
81                 day(dayArg),
82                 month(monthArg),
83                 year(yearArg)
84         {
85         }
86     };
87
88     struct FileInfo
89     {
90         // File handle
91         FileHandle handle;
92
93         // File name and comment
94         std::string name;
95         std::string comment;
96
97         // File information
98         unsigned long version;                //< version made by
99         unsigned long versionNeeded;          //< version needed to extract
100         unsigned long flag;                   //< general purpose bit flag
101         unsigned long compressionMethod;      //< compression method
102         unsigned long dosDate;                //< last mod file date in Dos fmt
103         unsigned long crc;                    //< crc-32
104         off64_t       compressedSize;         //< compressed size
105         off64_t       uncompressedSize;       //< uncompressed size
106         unsigned long diskNumberStart;        //< disk number start
107         unsigned long internalFileAttributes; //< internal file attributes
108         unsigned long externalFileAttributes; //< external file attributes
109
110         FileDateTime dateTime;
111
112         FileInfo()
113             : handle(),
114               name(),
115               comment(),
116               version(0),
117               versionNeeded(0),
118               flag(0),
119               compressionMethod(0),
120               dosDate(0),
121               crc(0),
122               compressedSize(0),
123               uncompressedSize(0),
124               diskNumberStart(0),
125               internalFileAttributes(0),
126               externalFileAttributes(0),
127               dateTime()
128         {
129         }
130
131         FileInfo(const FileHandle &handleArg,
132                  const std::string &nameArg,
133                  const std::string &commentArg,
134                  unsigned long versionArg,
135                  unsigned long versionNeededArg,
136                  unsigned long flagArg,
137                  unsigned long compressionMethodArg,
138                  unsigned long dosDateArg,
139                  unsigned long crcArg,
140                  const off64_t &compressedSizeArg,
141                  const off64_t &uncompressedSizeArg,
142                  unsigned long diskNumberStartArg,
143                  unsigned long internalFileAttributesArg,
144                  unsigned long externalFileAttributesArg,
145                  const FileDateTime &dateTimeArg)
146             : handle(handleArg),
147               name(nameArg),
148               comment(commentArg),
149               version(versionArg),
150               versionNeeded(versionNeededArg),
151               flag(flagArg),
152               compressionMethod(compressionMethodArg),
153               dosDate(dosDateArg),
154               crc(crcArg),
155               compressedSize(compressedSizeArg),
156               uncompressedSize(uncompressedSizeArg),
157               diskNumberStart(diskNumberStartArg),
158               internalFileAttributes(internalFileAttributesArg),
159               externalFileAttributes(externalFileAttributesArg),
160               dateTime(dateTimeArg)
161         {
162         }
163     };
164
165     class File
166         : public DPL::AbstractInput
167     {
168     private:
169         void *m_file;
170
171         friend class ZipInput;
172         File(class Device *device, FileHandle handle);
173
174     public:
175         ~File();
176
177         virtual DPL::BinaryQueueAutoPtr Read(size_t size);
178     };
179
180 private:
181     class Device *m_device;
182     void *m_masterFile;
183
184     size_t m_numberOfFiles;
185     size_t m_globalCommentSize;
186     std::string m_globalComment;
187
188     // At least cache handles
189     typedef std::vector<FileInfo> FileInfoList;
190     FileInfoList m_fileInfos;
191
192     void ReadGlobalInfo(void *masterFile);
193     void ReadGlobalComment(void *masterFile);
194     void ReadInfos(void *masterFile);
195
196 public:
197     typedef FileInfoList::const_iterator const_iterator;
198     typedef FileInfoList::const_reverse_iterator const_reverse_iterator;
199     typedef FileInfoList::size_type size_type;
200
201 public:
202     /**
203      * Open zip file from file
204      */
205     explicit ZipInput(const std::string &fileName);
206
207     /**
208      * Destructor
209      */
210     ~ZipInput();
211
212     // Iterators
213     const_iterator begin() const;
214     const_iterator end() const;
215
216     const_reverse_iterator rbegin() const;
217     const_reverse_iterator rend() const;
218
219     // Size, empty
220     size_type size() const;
221     bool empty() const;
222
223     /**
224      * Open a binary file for given file handle
225      *
226      * @return file object
227      * @param[in] handle Zip file handle to open
228      * @exception std::bad_alloc Cannot allocate memory to hold additional data
229      * @exception SteamOpenFailed Cannot find file with given handle
230      * @see BinaryQueue::BufferDeleterFree
231      */
232     File *OpenFile(FileHandle handle);
233
234     /**
235      * Open a binary file for given file name
236      *
237      * @return file object
238      * @param[in] fileName Zip file name to open
239      * @exception std::bad_alloc Cannot allocate memory to hold additional data
240      * @exception SteamOpenFailed Cannot find file with given handle
241      * @see BinaryQueue::BufferDeleterFree
242      */
243     File *OpenFile(const std::string &fileName);
244
245     /**
246      * Get archive global comment
247      *
248      * @return Global archive comment
249      */
250     const std::string &GetGlobalComment() const;
251 };
252 } // namespace DPL
253
254 #endif // DPL_ZIP_INPUT_H