A.I.M.S algorithms


lz.h
Go to the documentation of this file.
1 /* This software and supporting documentation are distributed by
2  * Institut Federatif de Recherche 49
3  * CEA/NeuroSpin, Batiment 145,
4  * 91191 Gif-sur-Yvette cedex
5  * France
6  *
7  * This software is governed by the CeCILL-B license under
8  * French law and abiding by the rules of distribution of free software.
9  * You can use, modify and/or redistribute the software under the
10  * terms of the CeCILL-B license as circulated by CEA, CNRS
11  * and INRIA at the following URL "http://www.cecill.info".
12  *
13  * As a counterpart to the access to the source code and rights to copy,
14  * modify and redistribute granted by the license, users are provided only
15  * with a limited warranty and the software's author, the holder of the
16  * economic rights, and the successive licensors have only limited
17  * liability.
18  *
19  * In this respect, the user's attention is drawn to the risks associated
20  * with loading, using, modifying and/or developing or reproducing the
21  * software by the user in light of its specific status of free software,
22  * that may mean that it is complicated to manipulate, and that also
23  * therefore means that it is reserved for developers and experienced
24  * professionals having in-depth computer knowledge. Users are therefore
25  * encouraged to load and test the software's suitability as regards their
26  * requirements in conditions enabling the security of their systems and/or
27  * data to be ensured and, more generally, to use and operate it in the
28  * same conditions as regards security.
29  *
30  * The fact that you are presently reading this means that you have had
31  * knowledge of the CeCILL-B license and that you accept its terms.
32  */
33 
34 
35 #ifndef AIMS_COMPRESSION_LZ_H
36 #define AIMS_COMPRESSION_LZ_H
37 
39 #include <aims/compression/lzhl.h>
41 #include <string.h>
42 
43 
44 #define LZBUFMASK ( (LZBUFSIZE) - 1 )
45 
46 
47 //
48 // class LZBuffer
49 //
51 {
52  protected:
53  inline LZBuffer();
54  inline ~LZBuffer();
55 
56  static int _wrap( size_t pos );
57  static int _distance( int diff );
58 
59  void _toBuf( byte );
60  void _toBuf( const byte*, size_t sz );
61  void _bufCpy( byte* dst, int pos, size_t sz );
62  int _nMatch( int pos, const byte* p, int nLimit );
63 
65  size_t _bufPos;
66 };
67 
68 
69 //
70 // class LZHLCompressor
71 //
73 {
74  public:
76  ~LZHLCompressor();
77 
78  static size_t calcMaxBuf( size_t rawSz )
79  { return LZHLEncoder::calcMaxBuf( rawSz ); }
80 
81  size_t compress( byte* dst, const byte* src, size_t sz );
82 
83  private:
84  LZHLEncoderStat _stat;
85  ushort* _table;
86 
87  void _wrapTable();
88  uint _updateTable( uint hash, const byte* src, uint pos, int len );
89 };
90 
91 
92 //
93 // class LZHLDecompressor
94 //
96  : private LZBuffer, private LZHLDecoderStat
97 {
98  public:
100  ~LZHLDecompressor();
101 
102  bool decompress( byte* dst, size_t* dstSz, const byte* src,
103  size_t* srcSz );
104 
105  private:
106  size_t _bits;
107  int _nBits;
108 
109  int _get( const byte*& src, const byte* srcEnd, int n );
110 };
111 
112 
113 
114 //----------------------------------------------------------------------------
115 
116 inline
118 {
119  _buf= new byte[ LZBUFSIZE ];
120  _bufPos = 0;
121 }
122 
123 
124 inline
126 {
127  delete [] _buf;
128 }
129 
130 
131 inline
132 int LZBuffer::_wrap( size_t pos )
133 {
134  return pos & LZBUFMASK;
135 }
136 
137 
138 inline
139 int LZBuffer::_distance( int diff )
140 {
141  return diff & LZBUFMASK;
142 }
143 
144 
145 inline
147 {
148  _buf[ _wrap( _bufPos++ ) ] = c;
149 }
150 
151 
152 inline
153 void LZBuffer::_toBuf( const byte* src, size_t sz )
154 {
155  ASSERT( sz < LZBUFSIZE );
156  int begin = _wrap( _bufPos );
157  int end = begin + sz;
158  if ( end > LZBUFSIZE )
159  {
160  size_t left = LZBUFSIZE - begin;
161  memcpy( _buf + begin, src, left );
162  memcpy( _buf, src + left, sz - left );
163  }
164  else
165  memcpy( _buf + begin, src, sz );
166  _bufPos += sz;
167 }
168 
169 
170 inline
171 void LZBuffer::_bufCpy( byte* dst, int pos, size_t sz )
172 {
173  ASSERT( sz < LZBUFSIZE );
174  int begin = _wrap( pos );
175  int end = begin + sz;
176  if ( end > LZBUFSIZE )
177  {
178  size_t left = LZBUFSIZE - begin;
179  memcpy( dst, _buf + begin, left );
180  memcpy( dst + left, _buf, sz - left );
181  }
182  else
183  memcpy( dst, _buf + begin, sz );
184 }
185 
186 
187 #endif
byte * _buf
Definition: lz.h:64
Definition: lz.h:50
#define LZBUFSIZE
Definition: lzhl.h:66
static size_t calcMaxBuf(size_t rawSz)
static size_t calcMaxBuf(size_t rawSz)
Definition: lz.h:78
static int _wrap(size_t pos)
Definition: lz.h:132
void _toBuf(byte)
Definition: lz.h:146
~LZBuffer()
Definition: lz.h:125
void _bufCpy(byte *dst, int pos, size_t sz)
Definition: lz.h:171
LZBuffer()
Definition: lz.h:117
#define LZBUFMASK
Definition: lz.h:44
unsigned int uint
uint8_t byte
size_t _bufPos
Definition: lz.h:65
#define AIMSALGO_API
#define ASSERT(EX)
static int _distance(int diff)
Definition: lz.h:139
unsigned short ushort