aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/restricted/boost/libs/iostreams/src/bzip2.cpp
blob: af80cd2f94b7931635fc342d7442ee8f56e1bbcd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
// (C) Copyright 2003-2007 Jonathan Turkanis
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)

// See http://www.boost.org/libs/iostreams for documentation.

// To configure Boost to work with libbz2, see the 
// installation instructions here:
// http://boost.org/libs/iostreams/doc/index.html?path=7

// Define BOOST_IOSTREAMS_SOURCE so that <boost/iostreams/detail/config.hpp> 
// knows that we are building the library (possibly exporting code), rather 
// than using it (possibly importing code).
#define BOOST_IOSTREAMS_SOURCE 

#include <boost/throw_exception.hpp>
#include <boost/iostreams/detail/config/dyn_link.hpp>
#include <boost/iostreams/filter/bzip2.hpp> 
#include "bzlib.h"  // Julian Seward's "bzip.h" header.
                    // To configure Boost to work with libbz2, see the 
                    // installation instructions here:
                    // http://boost.org/libs/iostreams/doc/index.html?path=7
                    
namespace boost { namespace iostreams {

namespace bzip2 {

                    // Status codes

const int ok                   = BZ_OK;
const int run_ok               = BZ_RUN_OK;
const int flush_ok             = BZ_FLUSH_OK;
const int finish_ok            = BZ_FINISH_OK;
const int stream_end           = BZ_STREAM_END;
const int sequence_error       = BZ_SEQUENCE_ERROR;
const int param_error          = BZ_PARAM_ERROR;
const int mem_error            = BZ_MEM_ERROR;
const int data_error           = BZ_DATA_ERROR;
const int data_error_magic     = BZ_DATA_ERROR_MAGIC;
const int io_error             = BZ_IO_ERROR;
const int unexpected_eof       = BZ_UNEXPECTED_EOF;
const int outbuff_full         = BZ_OUTBUFF_FULL;
const int config_error         = BZ_CONFIG_ERROR;

                    // Action codes

const int finish               = BZ_FINISH;
const int run                  = BZ_RUN;

} // End namespace bzip2. 

//------------------Implementation of bzip2_error-----------------------------//
                    
bzip2_error::bzip2_error(int error)
    : BOOST_IOSTREAMS_FAILURE("bzip2 error"), error_(error) 
    { }

void bzip2_error::check BOOST_PREVENT_MACRO_SUBSTITUTION(int error)
{
    switch (error) {
    case BZ_OK: 
    case BZ_RUN_OK: 
    case BZ_FLUSH_OK:
    case BZ_FINISH_OK:
    case BZ_STREAM_END:
        return;
    case BZ_MEM_ERROR: 
        boost::throw_exception(std::bad_alloc());
    default:
        boost::throw_exception(bzip2_error(error));
    }
}

//------------------Implementation of bzip2_base------------------------------//

namespace detail {

bzip2_base::bzip2_base(const bzip2_params& params)
    : params_(params), stream_(new bz_stream), ready_(false) 
    { }

bzip2_base::~bzip2_base() { delete static_cast<bz_stream*>(stream_); }

void bzip2_base::before( const char*& src_begin, const char* src_end,
                         char*& dest_begin, char* dest_end )
{
    bz_stream* s = static_cast<bz_stream*>(stream_);
    s->next_in = const_cast<char*>(src_begin);
    s->avail_in = static_cast<unsigned>(src_end - src_begin);
    s->next_out = reinterpret_cast<char*>(dest_begin);
    s->avail_out= static_cast<unsigned>(dest_end - dest_begin);
}

void bzip2_base::after(const char*& src_begin, char*& dest_begin)
{
    bz_stream* s = static_cast<bz_stream*>(stream_);
    src_begin = const_cast<char*>(s->next_in);
    dest_begin = s->next_out;
}

int bzip2_base::check_end(const char* src_begin, const char* dest_begin)
{
    bz_stream* s = static_cast<bz_stream*>(stream_);
    if( src_begin == s->next_in &&
        s->avail_in == 0 &&
        dest_begin == s->next_out) {
        return bzip2::unexpected_eof;
    } else {
        return bzip2::ok;
    }
}

void bzip2_base::end(bool compress)
{
    if(!ready_) return;
    ready_ = false;
    bz_stream* s = static_cast<bz_stream*>(stream_);
    bzip2_error::check BOOST_PREVENT_MACRO_SUBSTITUTION(
        compress ?
            BZ2_bzCompressEnd(s) : 
            BZ2_bzDecompressEnd(s)
    ); 
}

int bzip2_base::compress(int action)
{
    return BZ2_bzCompress(static_cast<bz_stream*>(stream_), action);
}

int bzip2_base::decompress()
{
    return BZ2_bzDecompress(static_cast<bz_stream*>(stream_));
}

void bzip2_base::do_init
    ( bool compress, 
      bzip2::alloc_func /* alloc */, 
      bzip2::free_func /* free */, 
      void* derived )
{
    bz_stream* s = static_cast<bz_stream*>(stream_);

    // Current interface for customizing memory management 
    // is non-conforming and has been disabled:
    //    s->bzalloc = alloc;
    //    s->bzfree = free;
        s->bzalloc = 0;
        s->bzfree = 0;
    s->opaque = derived;
    bzip2_error::check BOOST_PREVENT_MACRO_SUBSTITUTION( 
        compress ?
            BZ2_bzCompressInit( s,
                                params_.block_size, 
                                0, 
                                params_.work_factor ) :
            BZ2_bzDecompressInit( s, 
                                  0, 
                                  params_.small )
    );
    ready_ = true;
}

} // End namespace detail.

//----------------------------------------------------------------------------//

} } // End namespaces iostreams, boost.