libzipper 1.0.1
|
00001 // Copyright (C) 2011 Michael McMaster <michael@codesrc.com> 00002 // 00003 // This file is part of libzipper. 00004 // 00005 // libzipper is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // libzipper is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with libzipper. If not, see <http://www.gnu.org/licenses/>. 00017 00018 #ifndef zipper_hh 00019 #define zipper_hh 00020 00021 #include <stdexcept> 00022 #include <memory> 00023 #include <string> 00024 #include <vector> 00025 00026 #include <cstdint> 00027 00028 #include <sys/stat.h> // For mode_t 00029 #include <sys/time.h> // For timeval 00030 00142 00143 00144 namespace zipper 00145 { 00152 typedef uint64_t zsize_t; 00153 00166 enum ContainerFormat 00167 { 00169 Container_begin = 0, 00170 00172 Container_none = 0, 00173 00175 Container_zip, 00176 00178 Container_gzip, 00179 00181 Container_end 00182 }; 00183 00188 struct Container 00189 { 00192 enum CapabilityBits 00193 { 00195 Compression = 1, 00196 00199 Decompression = 2, 00200 00203 EmbeddedFilenames = 4, 00204 00207 Archive = 8, 00208 00211 FileSize = 16 00212 }; 00213 00215 ContainerFormat format; 00216 00219 std::string mediaType; 00220 00222 uint32_t capabilities; 00223 }; 00224 00227 extern const timeval s_now; 00228 00230 const Container& getContainer(ContainerFormat format); 00231 00233 class Exception : public std::runtime_error 00234 { 00235 public: 00238 Exception(const std::string& what); 00239 }; 00240 00243 class FormatException : public Exception 00244 { 00245 public: 00248 FormatException(const std::string& what); 00249 }; 00250 00253 class IOException : public Exception 00254 { 00255 public: 00258 IOException(const std::string& what); 00259 }; 00260 00268 class UnsupportedException : public Exception 00269 { 00270 public: 00273 UnsupportedException(const std::string& what); 00274 }; 00275 00286 class Reader 00287 { 00288 public: 00290 virtual ~Reader(); 00291 00296 virtual const std::string& getSourceName() const = 0; 00297 00301 virtual const timeval& getModTime() const = 0; 00302 00307 virtual zsize_t getSize() const = 0; 00308 00321 virtual void readData( 00322 zsize_t offset, zsize_t bytes, uint8_t* dest 00323 ) const = 0; 00324 00325 }; 00326 00329 class FileReader : public Reader 00330 { 00331 public: 00333 FileReader(const std::string& filename); 00334 00345 FileReader(const std::string& filename, int fd, bool closeFd); 00346 00348 virtual ~FileReader(); 00349 00351 virtual const std::string& getSourceName() const; 00352 00354 virtual const timeval& getModTime() const; 00355 00357 virtual zsize_t getSize() const; 00358 00360 virtual void readData( 00361 zsize_t offset, zsize_t bytes, uint8_t* dest 00362 ) const; 00363 private: 00364 FileReader(const FileReader&); 00365 FileReader& operator=(const FileReader&); 00366 00367 class FileReaderImpl; 00368 FileReaderImpl* m_impl; 00369 }; 00370 00373 typedef std::shared_ptr<Reader> ReaderPtr; 00374 00383 class Writer 00384 { 00385 public: 00387 virtual ~Writer(); 00388 00390 virtual zsize_t getSize() const = 0; 00391 00403 virtual void writeData( 00404 zsize_t offset, zsize_t bytes, const uint8_t* data 00405 ) = 0; 00406 }; 00407 00410 typedef std::shared_ptr<Writer> WriterPtr; 00411 00414 class FileWriter : public Writer 00415 { 00416 public: 00431 FileWriter( 00432 const std::string& filename, 00433 mode_t createPermissions = 0664, 00434 const timeval& modTime = s_now); 00435 00447 FileWriter(const std::string& filename, int fd, bool closeFd); 00448 00450 virtual ~FileWriter(); 00451 00453 virtual zsize_t getSize() const; 00454 00456 virtual void writeData( 00457 zsize_t offset, zsize_t bytes, const uint8_t* data 00458 ); 00459 private: 00460 FileWriter(const FileWriter&); 00461 FileWriter& operator=(const FileWriter&); 00462 00463 class FileWriterImpl; 00464 FileWriterImpl* m_impl; 00465 }; 00466 00471 class CompressedFile 00472 { 00473 public: 00475 virtual ~CompressedFile(); 00476 00482 virtual bool isDecompressSupported() const = 0; 00483 00486 virtual void decompress(Writer& writer) = 0; 00487 00492 virtual const std::string& getPath() const = 0; 00493 00498 virtual zsize_t getCompressedSize() const = 0; 00499 00507 virtual zsize_t getUncompressedSize() const = 0; 00508 00510 virtual const timeval& getModificationTime() const = 0; 00511 }; 00514 typedef std::shared_ptr<CompressedFile> CompressedFilePtr; 00515 00519 class Decompressor 00520 { 00521 public: 00523 Decompressor(const ReaderPtr& reader); 00524 00530 Decompressor(Reader& reader); 00531 00533 ~Decompressor(); 00534 00536 ContainerFormat getContainerFormat() const; 00537 00540 std::vector<CompressedFilePtr> getEntries() const; 00541 private: 00542 Decompressor(const Decompressor&); 00543 Decompressor& operator=(const Decompressor&); 00544 00545 class DecompressorImpl; 00546 DecompressorImpl* m_decompressor; 00547 }; 00548 00552 class Compressor 00553 { 00554 public: 00560 Compressor(ContainerFormat format, const WriterPtr& writer); 00561 00569 Compressor(ContainerFormat format, Writer& writer); 00570 00575 ~Compressor(); 00576 00579 void addFile(const Reader& reader); 00580 00581 class CompressorImpl; 00582 private: 00583 Compressor(const Compressor&); 00584 Compressor& operator=(const Compressor&); 00585 00586 CompressorImpl* m_compressor; 00587 }; 00588 } 00589 00590 #endif 00591