1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 #ifndef _XMLSEARCH_UTIL_DECOMPRESSOR_HXX_
24 #define _XMLSEARCH_UTIL_DECOMPRESSOR_HXX_
25 
26 #ifndef INCLUDED_STL_VECTOR
27 #include <vector>
28 #define INCLUDED_STL_VECTOR
29 #endif
30 #include <excep/XmlSearchExceptions.hxx>
31 #include <util/RandomAccessStream.hxx>
32 
33 
34 namespace xmlsearch {
35 
36 	namespace util {
37 
38 
39 		class CompressorIterator;
40 
41 
42 		class Decompressor
43 		{
44 		public:
45 
Decompressor()46 			Decompressor()
47 				: toRead_( 0 ),
48 				  path_( 0 )
49 			{
50 			}
51 
~Decompressor()52 			virtual ~Decompressor() { }
53 
54 			virtual sal_Int32 getNextByte() = 0;
55 
initReading()56 			virtual void initReading()
57 			{
58 				toRead_ = 0;
59 			}
60 
61 		private:
62 
63 			static const sal_Int32 BitsInByte;
64 			static const sal_Int32 NBits;
65 
66 			sal_Int32 readByte_, toRead_, path_;
67 		};
68 
69 
70 
71 
72 		class StreamDecompressor
73 			: public Decompressor
74 		{
75 		public:
76 
StreamDecompressor(RandomAccessStream * in)77 			StreamDecompressor( RandomAccessStream* in )
78 				: in_( in )
79 			{
80 			}
81 
~StreamDecompressor()82 			~StreamDecompressor() { }
83 
84 
85 			virtual sal_Int32 getNextByte();
86 
87 		private:
88 
89 			RandomAccessStream* in_;
90 
91 		};
92 
93 
94 
95 		class ByteArrayDecompressor
96 			: public Decompressor
97 		{
98 		public:
99 
ByteArrayDecompressor(sal_Int32 arrayL,sal_Int8 * array,sal_Int32 index)100 			ByteArrayDecompressor( sal_Int32 arrayL,sal_Int8* array,sal_Int32 index )
101 			{
102 				initReading(array,arrayL,index);
103 			}
104 
105 
~ByteArrayDecompressor()106 			~ByteArrayDecompressor() { }
107 
bytesRead()108 			sal_Int32 bytesRead()
109 			{
110 				return index_ - index0_;
111 			}
112 
113 
getNextByte()114 			sal_Int32 getNextByte() throw( xmlsearch::excep::XmlSearchException )
115 			{
116 				if( arrayL_ <= index_ )
117 					throw  xmlsearch::excep::XmlSearchException(
118 						rtl::OUString::createFromAscii( "ByteArrayDecompressor->getNextByte()" ) );
119 				return array_[index_++] & 0xFF;
120 			}
121 
122 
123 		private:
124 
125 			sal_Int32  arrayL_;
126 			sal_Int8   *array_;
127 
128 			sal_Int32  index_,index0_;
129 
130 			using xmlsearch::util::Decompressor::initReading;
initReading(sal_Int8 * array,sal_Int32 arrayL,sal_Int32 index)131 			void initReading( sal_Int8* array,sal_Int32 arrayL,sal_Int32 index )
132 			{
133 				arrayL_ = arrayL;
134 				array_ = array;
135 				index_ = index0_ = index;
136 				Decompressor::initReading();
137 			}
138 
139 		};
140 
141 
142 	}
143 
144 }
145 
146 
147 #endif
148