~ubuntu-branches/ubuntu/raring/clucene-core/raring-proposed

« back to all changes in this revision

Viewing changes to src/contribs-lib/CLucene/util/gzipinputstream.cpp

  • Committer: Package Import Robot
  • Author(s): Fathi Boudra
  • Date: 2012-08-11 09:33:38 UTC
  • mfrom: (1.1.5)
  • Revision ID: package-import@ubuntu.com-20120811093338-fgrx41ftqew3qt6a
Tags: 2.3.3.4-1
* New upstream release (Closes: #661703).
* Convert package to multiarch.
* Drop obsolete patches:
  - 01_add_missing_include_bug505667.diff
  - 02_posixness_fix_bug530308.diff
* Add patches:
  - Fixing_ZLIB_configuration_in_shared_CMakeLists.patch
  - Fix-pkgconfig-file-by-adding-clucene-shared-library.patch
  - Install-contribs-lib.patch
  - multiarch.patch
* Update debian/compat: bump to 8.
* Update debian/control:
  - update build dependencies (add cmake, libboost-dev and libz-dev).
  - bump Standards-Version to 3.9.3.
  - rename packages due to ABI bump: libclucene0ldbl -> libclucene-core1.
  - add libclucene-contribs1 package.
* Update debian/rules:
  - rewrite to use CMake.
  - add multiarch support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*------------------------------------------------------------------------------
 
2
* Copyright (C) 2003-2006 Jos van den Oever
 
3
 
4
* Distributable under the terms of either the Apache License (Version 2.0) or 
 
5
* the GNU Lesser General Public License, as specified in the COPYING file.
 
6
------------------------------------------------------------------------------*/
 
7
#include "CLucene/_ApiHeader.h"
 
8
#include "gzipinputstream.h"
 
9
#include "CLucene/util/_bufferedstream.h"
 
10
#include <zlib.h>
 
11
 
 
12
CL_NS_DEF(util)
 
13
 
 
14
 
 
15
class GZipInputStream::Internal{
 
16
public:
 
17
        class JStreamsBuffer: public BufferedInputStreamImpl{
 
18
                z_stream_s* zstream;
 
19
                BufferedInputStream* input;
 
20
        protected:
 
21
 
 
22
                int32_t fillBuffer(signed char* start, int32_t space) {
 
23
                         if (zstream == 0) return -1;
 
24
                         // make sure there is data to decompress
 
25
                         if (zstream->avail_out) {
 
26
                                  // read data from the input stream
 
27
                                  const signed char* inStart;
 
28
                                  int32_t nread = input->read(inStart, 1, 0);
 
29
                                  if (nread < 1) {
 
30
                                                _CLTHROWA(CL_ERR_IO, "unexpected end of stream");
 
31
                                  } else {
 
32
                                                zstream->next_in = (Bytef*)inStart;
 
33
                                                zstream->avail_in = nread;
 
34
                                  }
 
35
                         }
 
36
                         // make sure we can write into the buffer
 
37
                         zstream->avail_out = space;
 
38
                         zstream->next_out = (Bytef*)start;
 
39
                         // decompress
 
40
                         int r = inflate(zstream, Z_SYNC_FLUSH);
 
41
                         // inform the buffer of the number of bytes that was read
 
42
                         int32_t nwritten = space - zstream->avail_out;
 
43
                         switch (r) {
 
44
                         case Z_NEED_DICT:
 
45
                                  _CLTHROWA(CL_ERR_IO, "Z_NEED_DICT while inflating stream.");
 
46
                                  break;
 
47
                         case Z_DATA_ERROR:
 
48
                                  _CLTHROWA(CL_ERR_IO, "Z_DATA_ERROR while inflating stream.");
 
49
                                  break;
 
50
                         case Z_MEM_ERROR:
 
51
                                  _CLTHROWA(CL_ERR_IO, "Z_MEM_ERROR while inflating stream.");
 
52
                                  break;
 
53
                         case Z_STREAM_END:
 
54
                                  if (zstream->avail_in) {
 
55
                                                input->reset(input->position()-zstream->avail_in);
 
56
                                  }
 
57
                                  // we are finished decompressing,
 
58
                                  // (but this stream is not yet finished)
 
59
                                  dealloc();
 
60
                         }
 
61
                         return nwritten;
 
62
                }
 
63
                void dealloc() {
 
64
                         if (zstream) {
 
65
                                  inflateEnd(zstream);
 
66
                                  free(zstream);
 
67
                                  zstream = 0;
 
68
                         }
 
69
                }
 
70
                bool checkMagic() {
 
71
                         const unsigned char* buf;
 
72
                         const signed char* begin;
 
73
                         int32_t nread;
 
74
 
 
75
                         int64_t pos = input->position();
 
76
                         nread = input->read(begin, 2, 2);
 
77
                         input->reset(pos);
 
78
                         if (nread != 2) {
 
79
                                  return false;
 
80
                         }
 
81
 
 
82
                         buf = (const unsigned char*)begin;
 
83
                         return buf[0] == 0x1f && buf[1] == 0x8b;
 
84
                }
 
85
 
 
86
        public:
 
87
                int encoding;
 
88
 
 
89
                JStreamsBuffer(BufferedInputStream* input, GZipInputStream::ZipFormat format){
 
90
                  this->input = input;
 
91
 
 
92
                  // check first bytes of stream before allocating buffer
 
93
                  if (format == GZipInputStream::GZIPFORMAT && !checkMagic()) {
 
94
                                _CLTHROWA(CL_ERR_IO, "Magic bytes are wrong.");
 
95
                  }
 
96
 
 
97
                  // initialize the z_stream
 
98
                  zstream = (z_stream_s*)malloc(sizeof(z_stream_s));
 
99
                  zstream->zalloc = Z_NULL;
 
100
                  zstream->zfree = Z_NULL;
 
101
                  zstream->opaque = Z_NULL;
 
102
                  zstream->avail_in = 0;
 
103
                  zstream->next_in = Z_NULL;
 
104
                  // initialize for reading gzip streams
 
105
                  // for reading libz streams, you need inflateInit(zstream)
 
106
                  int r;
 
107
                  switch(format) {
 
108
                  case GZipInputStream::ZLIBFORMAT:
 
109
                                r = inflateInit(zstream);
 
110
                                break;
 
111
                  case GZipInputStream::GZIPFORMAT:
 
112
                                r = inflateInit2(zstream, 15+16);
 
113
                                break;
 
114
                  case GZipInputStream::ZIPFORMAT:
 
115
                  default:
 
116
                                r = inflateInit2(zstream, -MAX_WBITS);
 
117
                                break;
 
118
                  }
 
119
                  if (r != Z_OK) {
 
120
                                dealloc();
 
121
                                _CLTHROWA(CL_ERR_IO, "Error initializing GZipInputStream.");
 
122
                  }
 
123
 
 
124
                  // signal that we need to read into the buffer
 
125
                  zstream->avail_out = 1;
 
126
                }
 
127
                void _setMinBufSize(int32_t bufsize){
 
128
                        this->setMinBufSize(bufsize);
 
129
                }
 
130
 
 
131
                ~JStreamsBuffer(){
 
132
                  dealloc();
 
133
                }
 
134
        };
 
135
 
 
136
        JStreamsBuffer* jsbuffer;
 
137
 
 
138
        Internal(BufferedInputStream* input, GZipInputStream::ZipFormat format){
 
139
                jsbuffer = new JStreamsBuffer(input, format);
 
140
        }
 
141
        ~Internal(){
 
142
                delete jsbuffer;
 
143
        }
 
144
};
 
145
 
 
146
GZipInputStream::GZipInputStream(InputStream* input, ZipFormat format) {
 
147
         internal = new Internal(_CLNEW FilteredBufferedInputStream(input, false), format);
 
148
}
 
149
GZipInputStream::GZipInputStream(BufferedInputStream* input, ZipFormat format) {
 
150
         internal = new Internal(input, format);
 
151
}
 
152
 
 
153
GZipInputStream::~GZipInputStream() {
 
154
    delete internal;
 
155
}
 
156
 
 
157
 
 
158
int32_t GZipInputStream::read(const signed char*& start, int32_t min, int32_t max){
 
159
        return internal->jsbuffer->read(start,min,max);
 
160
}
 
161
int32_t GZipInputStream::read(const unsigned char*& _start, int32_t min, int32_t max){
 
162
   const signed char* start = 0;
 
163
        int32_t ret = internal->jsbuffer->read(start,min,max);
 
164
   _start = (const unsigned char*)start;
 
165
   return ret;
 
166
}
 
167
int64_t GZipInputStream::position(){
 
168
        return internal->jsbuffer->position();
 
169
}
 
170
int64_t GZipInputStream::reset(int64_t to){
 
171
        return internal->jsbuffer->reset(to);
 
172
}
 
173
int64_t GZipInputStream::skip(int64_t ntoskip){
 
174
        return internal->jsbuffer->skip(ntoskip);
 
175
}
 
176
void GZipInputStream::setMinBufSize(int32_t minbufsize){
 
177
        internal->jsbuffer->_setMinBufSize(minbufsize);
 
178
}
 
179
size_t GZipInputStream::size(){
 
180
        return internal->jsbuffer->size();
 
181
}
 
182
 
 
183
 
 
184
CL_NS_END