Line data Source code
1 : // Snap Websites Server -- manage sessions for users, forms, etc.
2 : // Copyright (c) 2013-2019 Made to Order Software Corp. All Rights Reserved
3 : //
4 : // This program is free software; you can redistribute it and/or modify
5 : // it under the terms of the GNU General Public License as published by
6 : // the Free Software Foundation; either version 2 of the License, or
7 : // (at your option) any later version.
8 : //
9 : // This program is distributed in the hope that it will be useful,
10 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : // GNU General Public License for more details.
13 : //
14 : // You should have received a copy of the GNU General Public License
15 : // along with this program; if not, write to the Free Software
16 : // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 : #pragma once
18 :
19 : #include "snapwebsites/snap_exception.h"
20 :
21 : #include "snapwebsites/snap_string_list.h"
22 :
23 : #include <unistd.h>
24 :
25 :
26 : namespace snap
27 : {
28 : namespace compression
29 : {
30 :
31 0 : class compression_exception : public snap_exception
32 : {
33 : public:
34 0 : compression_exception(char const * whatmsg) : snap_exception("compression", whatmsg) {}
35 : compression_exception(std::string const & whatmsg) : snap_exception("compression", whatmsg) {}
36 0 : compression_exception(QString const & whatmsg) : snap_exception("compression", whatmsg) {}
37 : };
38 :
39 0 : class compression_exception_not_implemented : public compression_exception
40 : {
41 : public:
42 0 : compression_exception_not_implemented(char const * whatmsg) : compression_exception(whatmsg) {}
43 : compression_exception_not_implemented(std::string const & whatmsg) : compression_exception(whatmsg) {}
44 : compression_exception_not_implemented(QString const & whatmsg) : compression_exception(whatmsg) {}
45 : };
46 :
47 0 : class compression_exception_not_supported : public compression_exception
48 : {
49 : public:
50 0 : compression_exception_not_supported(char const * whatmsg) : compression_exception(whatmsg) {}
51 : compression_exception_not_supported(std::string const & whatmsg) : compression_exception(whatmsg) {}
52 : compression_exception_not_supported(QString const & whatmsg) : compression_exception(whatmsg) {}
53 : };
54 :
55 0 : class compression_exception_not_compatible : public compression_exception
56 : {
57 : public:
58 0 : compression_exception_not_compatible(char const * whatmsg) : compression_exception(whatmsg) {}
59 : compression_exception_not_compatible(std::string const & whatmsg) : compression_exception(whatmsg) {}
60 0 : compression_exception_not_compatible(QString const & whatmsg) : compression_exception(whatmsg) {}
61 : };
62 :
63 :
64 :
65 :
66 : // compression level is a percent (a number from 0 to 100)
67 : typedef int level_t;
68 :
69 : // all compressors derive from this class
70 : class compressor_t
71 : {
72 : public:
73 : static char const * BEST_COMPRESSION;
74 : static char const * NO_COMPRESSION;
75 :
76 : compressor_t(char const * name);
77 : virtual ~compressor_t();
78 : virtual char const *get_name() const = 0;
79 : virtual QByteArray compress(QByteArray const & input, level_t level, bool text) = 0;
80 : virtual bool compatible(QByteArray const & input) const = 0;
81 : virtual QByteArray decompress(QByteArray const & input) = 0;
82 : virtual QByteArray decompress(QByteArray const & input, size_t uncompressed_size) = 0;
83 : };
84 :
85 : class archiver_t
86 : {
87 : public:
88 : class file_t
89 : {
90 : public:
91 : enum class type_t
92 : {
93 : FILE_TYPE_REGULAR,
94 : FILE_TYPE_DIRECTORY
95 : };
96 :
97 : void set_type(type_t type);
98 : void set_data(QByteArray const & data);
99 : void set_filename(QString const & filename);
100 : void set_user(QString const & user, uid_t uid);
101 : void set_group(QString const & group, gid_t gid);
102 : void set_mode(mode_t mode);
103 : void set_mtime(time_t mtime);
104 :
105 : type_t get_type() const;
106 : QByteArray const & get_data() const;
107 : QString const & get_filename() const;
108 : QString const & get_user() const;
109 : QString const & get_group() const;
110 : uid_t get_uid() const;
111 : gid_t get_gid() const;
112 : mode_t get_mode() const;
113 : time_t get_mtime() const;
114 :
115 : private:
116 : type_t f_type = type_t::FILE_TYPE_REGULAR;
117 : QByteArray f_data = QByteArray();
118 : QString f_filename = QString();
119 : QString f_user = QString();
120 : QString f_group = QString();
121 : uid_t f_uid = 0;
122 : gid_t f_gid = 0;
123 : mode_t f_mode = 0;
124 : time_t f_mtime = 0;
125 : };
126 :
127 : archiver_t(char const * name);
128 : virtual ~archiver_t();
129 : void set_archive(QByteArray const & input);
130 : QByteArray get_archive() const;
131 : virtual char const * get_name() const = 0;
132 : virtual void append_file(file_t const & file) = 0;
133 : virtual bool next_file(file_t & file) const = 0;
134 : virtual void rewind_file() = 0;
135 :
136 : protected:
137 : QByteArray f_archive = QByteArray();
138 : };
139 :
140 : //void register_compressor(compressor_t * compressor_name); -- automatic at this point
141 : snap_string_list compressor_list();
142 : compressor_t * get_compressor(QString const & compressor_name);
143 : QByteArray compress(QString & compressor_name, QByteArray const & input, level_t level, bool text);
144 : QByteArray decompress(QString & compressor_name, QByteArray const & input);
145 :
146 : snap_string_list archiver_list();
147 : archiver_t * get_archiver(QString const & archiver_name);
148 :
149 : } // namespace snap
150 : } // namespace compression
151 : // vim: ts=4 sw=4 et
|