]>
git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/backends/bind/huffman.cc
2 PowerDNS Versatile Database Driven Nameserver
3 Copyright (C) 2002 PowerDNS.COM BV
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License version 2
7 as published by the Free Software Foundation
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 void HuffmanCodec::set(char c
,const string
&code
)
35 HuffmanCodec::HuffmanCodec()
51 set('9',"1000111100");
52 set('*',"100011110100");
53 set('q',"100011110101");
54 set('7',"10001111011");
82 for(map
<char,string
>::const_iterator i
=d_dict
.begin();i
!=d_dict
.end();++i
) {
83 d_min
=min(d_min
,i
->second
.length());
84 d_max
=max(d_max
,i
->second
.length());
86 (d_rdict
[i
->second
.length()])[i
->second
]=i
->first
;
88 d_last_compressed
=d_last_out
="";
92 void HuffmanCodec::passthrough(bool shoulddo
)
94 d_passthrough
=shoulddo
;
98 // Bitify input: 1001101110101001000101
99 //Decode got offered: '1001101110101001'
102 void HuffmanCodec::decode(const string
&compressed
, string
&out
)
108 if(compressed
==d_last_compressed
) {
115 unbitify(compressed
, full
);
116 // cout<<"Decode got offered: '"<<full<<"'"<<endl;
119 size_t cleft
=full
.length();
121 out
.reserve(full
.length()/5);
123 map
<string
,char>::const_iterator i
;
125 for(mlen
=d_min
;mlen
<=cleft
&& mlen
<=d_max
;++mlen
) {
126 if(d_rdict
[mlen
].empty())
129 i
=d_rdict
[mlen
].find(full
.substr(pos
,mlen
));
131 if(i
!=d_rdict
[mlen
].end()) { // match
133 d_last_compressed
=compressed
;
138 out
.append(1,i
->second
);
147 throw AhuException("Unable to parse huffman symbol "+full
.substr(pos
));
148 d_last_compressed
=compressed
;
152 void HuffmanCodec::encode(const string
&in
, string
&out
)
159 for(string::const_iterator i
=in
.begin();i
!=in
.end();++i
) {
160 map
<char,string
>::const_iterator j
=d_dict
.find(tolower(*i
));
161 if(j
==d_dict
.end()) {
165 throw AhuException("Trying to huffman encode an unknown symbol '"+c
+"'");
167 full
.append(j
->second
);
169 full
.append(d_dict
[0]);
171 // cout<<"full: "<<full<<endl;
174 void HuffmanCodec::bitify(const string
&full
, string
&out
)
176 unsigned char bitpos
=0;
177 unsigned char curbyte
=0;
178 // cout<<"Bitify input: "<<full<<endl;
179 for(string::const_iterator i
=full
.begin();i
!=full
.end();++i
) {
180 curbyte
|= (*i
=='1')<<(7-bitpos
);
182 out
.append(1,curbyte
);
187 out
.append(1,curbyte
);
190 void HuffmanCodec::unbitify(const string
&in
, string
&full
)
194 full
.reserve(in
.length()*8);
195 for(string::const_iterator i
=in
.begin();i
!=in
.end();++i
) {
199 full
.append(os
.str());
203 int main(int argc
, char **argv
)
210 // hc.initDictionary(dict);
211 // cout<<"in: "<<in.length()<<endl;
212 hc
.encode(in
,compressed
);
213 // cout<<"compressed: "<<compressed.length()<<endl;
214 // cout<<"Compressed: '"<<compressed<<"'"<<endl;
217 hc
.decode(compressed
,out
);
219 cout
<<"'"<<out
<<"'"<<endl
;
221 catch(AhuException
&ae
) {
222 cerr
<<"Fatal error: "<<ae
.reason
<<endl
;