Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 10:11:29

0001 /* This Source Code Form is subject to the terms of the Mozilla Public
0002  * License, v. 2.0. If a copy of the MPL was not distributed with this
0003  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
0004  *
0005  * This file is a part of bxzstr (https://github.com/tmaklin/bxzstr)
0006  * Written by Tommi Mäklin (tommi@maklin.fi) */
0007 
0008 #ifndef BXZSTR_COMPRESSION_TYPES_HPP
0009 #define BXZSTR_COMPRESSION_TYPES_HPP
0010 
0011 #include <exception>
0012 
0013 #include "stream_wrapper.hpp"
0014 #include "bz_stream_wrapper.hpp"
0015 #include "lzma_stream_wrapper.hpp"
0016 #include "z_stream_wrapper.hpp"
0017 #include "zstd_stream_wrapper.hpp"
0018 
0019 namespace bxz {
0020     enum Compression { z, bz2, lzma, zstd, plaintext };
0021 inline Compression detect_type(const char* in_buff_start,const  char* in_buff_end) {
0022     const unsigned char b0 = *reinterpret_cast<const  unsigned char * >(in_buff_start);
0023     const unsigned char b1 = *reinterpret_cast<const  unsigned char * >(in_buff_start + 1);
0024     bool gzip_header = (b0 == 0x1F && b1 == 0x8B);
0025     bool zlib_header = (b0 == 0x78 && (b1 == 0x01 || b1 == 0x9C || b1 == 0xDA));
0026     if (in_buff_start + 1 <= in_buff_end && (gzip_header || zlib_header)) return z;
0027     const unsigned char b2 = *reinterpret_cast<const  unsigned char * >(in_buff_start + 2);
0028     bool bz2_header = (b0 == 0x42 && b1 == 0x5a && b2 == 0x68);
0029     if (in_buff_start + 2 <= in_buff_end && bz2_header) return bz2;
0030     const unsigned char b3 = *reinterpret_cast<const  unsigned char * >(in_buff_start + 3);
0031     const unsigned char b4 = *reinterpret_cast<const  unsigned char * >(in_buff_start + 4);
0032     const unsigned char b5 = *reinterpret_cast<const  unsigned char * >(in_buff_start + 5);
0033     bool lzma_header = (b0 == 0xFD && b1 == 0x37 && b2 == 0x7A
0034             && b3 == 0x58 && b4 == 0x5A && b5 == 0x00);
0035     if (in_buff_start + 5 <= in_buff_end && lzma_header) return lzma;
0036     bool zstd_header = (b0 == 0x28 && b1 == 0xB5 && b2 == 0x2F && b3 == 0xFD);
0037     if (in_buff_start + 3 <= in_buff_end && zstd_header) return zstd;
0038     return plaintext;
0039 }
0040 
0041 #if defined(BXZSTR_LZMA_STREAM_WRAPPER_HPP) || defined(BXZSTR_BZ_STREAM_WRAPPER_HPP) || defined(BXZSTR_Z_STREAM_WRAPPER_HPP) || defined(BXZSTR_ZSTD_STREAM_WRAPPER_HPP)
0042 inline void init_stream(const Compression &type, const bool is_input, const int level,
0043             std::unique_ptr<detail::stream_wrapper> *strm_p) {
0044 #else
0045 inline void init_stream(const Compression &type, const bool, const int,
0046             std::unique_ptr<detail::stream_wrapper> *) {
0047 #endif
0048     switch (type) {
0049 #ifdef BXZSTR_LZMA_STREAM_WRAPPER_HPP
0050         case lzma : strm_p->reset(new detail::lzma_stream_wrapper(is_input, level));
0051     break;
0052 #endif
0053 #ifdef BXZSTR_BZ_STREAM_WRAPPER_HPP
0054         case bz2 : strm_p->reset(new detail::bz_stream_wrapper(is_input, level));
0055     break;
0056 #endif
0057 #ifdef BXZSTR_Z_STREAM_WRAPPER_HPP
0058         case z : strm_p->reset(new detail::z_stream_wrapper(is_input, level));
0059     break;
0060 #endif
0061 #ifdef BXZSTR_ZSTD_STREAM_WRAPPER_HPP
0062         case zstd : strm_p->reset(new detail::zstd_stream_wrapper(is_input, level));
0063     break;
0064 #endif
0065     default : throw std::runtime_error("Unrecognized compression type.");
0066     }
0067 }
0068 inline void init_stream(const Compression &type, const bool is_input,
0069             std::unique_ptr<detail::stream_wrapper> *strm_p) {
0070     init_stream(type, is_input, 6, strm_p);
0071 }
0072 
0073 inline int bxz_run(const Compression &type) {
0074     switch(type){
0075 #ifdef BXZSTR_LZMA_STREAM_WRAPPER_HPP
0076     case lzma: return 0;
0077     break; // LZMA_RUN
0078 #endif
0079 #ifdef BXZSTR_BZ_STREAM_WRAPPER_HPP
0080         case bz2: return 0;
0081     break; // BZ_RUN
0082 #endif
0083 #ifdef BXZSTR_Z_STREAM_WRAPPER_HPP
0084         case z: return 0;
0085     break;// Z_NO_FLUSH
0086 #endif
0087 #ifdef BXZSTR_ZSTD_STREAM_WRAPPER_HPP
0088         case zstd: return 0;
0089     break;// ZSTD_NO_FLUSH
0090 #endif
0091     default: throw std::runtime_error("Unrecognized compression type.");
0092     }
0093 }
0094 inline int bxz_finish(const Compression &type) {
0095     switch(type){
0096 #ifdef BXZSTR_LZMA_STREAM_WRAPPER_HPP
0097     case lzma: return 3;
0098     break; // LZMA_FINISH
0099 #endif
0100 #ifdef BXZSTR_BZ_STREAM_WRAPPER_HPP
0101         case bz2: return 2;
0102     break; // BZ_FINISH
0103 #endif
0104 #ifdef BXZSTR_Z_STREAM_WRAPPER_HPP
0105         case z: return 4;
0106     break; // Z_FINISH
0107 #endif
0108 #ifdef BXZSTR_ZSTD_STREAM_WRAPPER_HPP
0109         case zstd: return 1;
0110     break; // endStream == true
0111 #endif
0112     default: throw std::runtime_error("Unrecognized compression type.");
0113     }
0114 }
0115 }
0116 
0117 #endif