1 /*
2  * steghide 0.5.1 - a steganography program
3  * Copyright (C) 1999-2003 Stefan Hetzl <shetzl@chello.at>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
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.
14  *
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  *
19  */
20 
21 #include "AUtils.h"
22 #include "BitString.h"
23 #include "common.h"
24 
25 #ifdef USE_ZLIB
26 namespace zlib {
27 #include <zlib.h>
28 }
29 #endif // def USE_ZLIB
30 
31 #define BITPOS(n) (n % 8)
32 #define BYTEPOS(n) (n / 8)
33 
BitString(EmbValue arity)34 BitString::BitString (EmbValue arity)
35 	: Length(0)
36 {
37 	setArity(arity) ;
38 }
39 
BitString(const BitString & bs)40 BitString::BitString (const BitString& bs)
41 {
42 	Length = bs.Length ;
43 	Arity = bs.Arity ;
44 	ArityNBits = bs.ArityNBits ;
45 	Data.resize(bs.Data.size()) ;
46 	for (unsigned long i = 0 ; i < bs.Data.size() ; i++) {
47 		Data[i] = bs.Data[i] ;
48 	}
49 }
50 
BitString(unsigned long l)51 BitString::BitString (unsigned long l)
52 {
53 	unsigned long nbytes = 0 ;
54 	if (l % 8 == 0) {
55 		nbytes = l / 8 ;
56 	}
57 	else {
58 		nbytes = (l / 8) + 1 ;
59 	}
60 
61 	Data = std::vector<BYTE> (nbytes, 0) ; // is initialized to zeros in std::vector's constructor
62 	Length = l ;
63 	setArity(2) ;
64 }
65 
BitString(const std::vector<BYTE> & d)66 BitString::BitString (const std::vector<BYTE> &d)
67 	: Length(0)
68 {
69 	setArity(2) ;
70 	append (d) ;
71 }
72 
BitString(const std::string & d)73 BitString::BitString (const std::string &d)
74 	: Length(0)
75 {
76 	setArity(2) ;
77 	append (d) ;
78 }
79 
setArity(EmbValue arity)80 void BitString::setArity (EmbValue arity)
81 {
82 	BYTE tmp = Arity = arity ;
83 	ArityNBits = 0 ;
84 	while (tmp > 1) {
85 		myassert (tmp % 2 == 0) ; // only implemented for arity = 2^i
86 		tmp /= 2 ;
87 		ArityNBits++ ;
88 	}
89 }
90 
clear()91 BitString& BitString::clear()
92 {
93 	Data.clear() ;
94 	Length = 0 ;
95 	return *this ;
96 }
97 
_append(BIT v)98 void BitString::_append (BIT v)
99 {
100 	if (Length % 8 == 0) {
101 		Data.push_back (0) ;
102 	}
103 	Data[BYTEPOS(Length)] |= (v << BITPOS(Length)) ;
104 	Length++ ;
105 }
106 
append(BIT v)107 BitString& BitString::append (BIT v)
108 {
109 	_append (v) ;
110 	return *this ;
111 }
112 
append(const BYTE v,const unsigned short n)113 BitString& BitString::append (const BYTE v, const unsigned short n)
114 {
115 	for (unsigned short i = 0 ; i < n ; i++) {
116 		_append ((BIT) ((v & (1 << i)) >> i)) ;
117 	}
118 	return *this ;
119 }
120 
append(const UWORD16 v,const unsigned short n)121 BitString& BitString::append (const UWORD16 v, const unsigned short n)
122 {
123 	for (unsigned short i = 0 ; i < n ; i++) {
124 		_append ((BIT) ((v & (1 << i)) >> i)) ;
125 	}
126 	return *this ;
127 }
128 
append(const UWORD32 v,const unsigned short n)129 BitString& BitString::append (const UWORD32 v, const unsigned short n)
130 {
131 	for (unsigned short i = 0 ; i < n ; i++) {
132 		_append ((BIT) ((v & (1 << i)) >> i)) ;
133 	}
134 	return *this ;
135 }
136 
append(const BitString & v)137 BitString& BitString::append (const BitString& v)
138 {
139 	for (unsigned long i = 0 ; i < v.getLength() ; i++) {
140 		_append (v[i]) ;
141 	}
142 	return *this ;
143 }
144 
append(const std::vector<BYTE> & v)145 BitString& BitString::append (const std::vector<BYTE>& v)
146 {
147 	for (std::vector<BYTE>::const_iterator i = v.begin() ; i != v.end() ; i++) {
148 		append (*i) ;
149 	}
150 	return *this ;
151 }
152 
append(const std::string & v)153 BitString& BitString::append (const std::string& v)
154 {
155 	for (std::string::const_iterator i = v.begin() ; i != v.end() ; i++) {
156 		append ((BYTE) *i) ;
157 	}
158 	return *this ;
159 }
160 
operator [](unsigned long i) const161 BIT BitString::operator[] (unsigned long i) const
162 {
163 	myassert (i < Length) ;
164 	return ((Data[BYTEPOS(i)] >> BITPOS(i)) & 1) ;
165 }
166 
setBit(unsigned long i,BIT v)167 BitString& BitString::setBit (unsigned long i, BIT v)
168 {
169 	myassert (i < Length) ;
170 	BYTE mask = ~(1 << BITPOS(i)) ;
171 	Data[BYTEPOS(i)] &= mask ;
172 	Data[BYTEPOS(i)] |= (v << BITPOS(i)) ;
173 	return (*this) ;
174 }
175 
getBits(unsigned long s,unsigned long l) const176 BitString BitString::getBits (unsigned long s, unsigned long l) const
177 {
178 	BitString retval ;
179 	for (unsigned long i = s ; i < s + l ; i++) {
180 		retval.append ((*this)[i]) ;
181 	}
182 	return retval ;
183 }
184 
cutBits(unsigned long s,unsigned long l)185 BitString BitString::cutBits (unsigned long s, unsigned long l)
186 {
187 	myassert (s + l <= Length) ;
188 	BitString retval ;
189 	for (unsigned long i = s, j = s + l ; i < s + l || j < Length ; i++, j++) {
190 		if (i < s + l) {
191 			retval.append ((*this)[i]) ;
192 		}
193 		if (j < Length) {
194 			setBit(i, (*this)[j]) ;
195 		}
196 	}
197 	Length -= l ;
198 	Data.resize (AUtils::div_roundup<UWORD32> (Length, 8)) ;
199 	clearUnused() ;
200 
201 	return retval ;
202 }
203 
getValue(unsigned long s,unsigned short l) const204 UWORD32 BitString::getValue (unsigned long s, unsigned short l) const
205 {
206 	myassert (l <= 32) ;
207 	UWORD32 retval = 0 ;
208 	for (unsigned short i = 0 ; i < l ; i++) {
209 		retval |= (*this)[s + i] << i ;
210 	}
211 	return retval ;
212 }
213 
getBytes() const214 const std::vector<BYTE>& BitString::getBytes() const
215 {
216 	myassert (Length % 8 == 0) ;
217 	return Data ;
218 }
219 
truncate(const unsigned long s,const unsigned long e)220 BitString& BitString::truncate (const unsigned long s, const unsigned long e)
221 {
222 	unsigned long newsize_i = e - s ;
223 	unsigned long newsize_y = 0 ;
224 	if (newsize_i % 8 == 0) {
225 		newsize_y = newsize_i / 8 ;
226 	}
227 	else {
228 		newsize_y = newsize_i / 8 + 1 ;
229 	}
230 
231 	for (unsigned long i = 0 ; i < newsize_i ; i++) {
232 		// replace i-th bit with (s+i)-th bit
233 		BIT bit = (*this)[s + i] ;
234 		Data[BYTEPOS(i)] &= (BYTE) ~(1 << BITPOS(i)) ; // delete i-th bit
235 		Data[BYTEPOS(i)] |= (BYTE) (bit << BITPOS(i)) ;
236 	}
237 	Length = newsize_i ;
238 
239 	clearUnused() ;
240 
241 	return *this ;
242 }
243 
pad(unsigned long mult,BIT v)244 BitString& BitString::pad (unsigned long mult, BIT v)
245 {
246 	while (Length % mult != 0) {
247 		_append (v) ;
248 	}
249 	return *this ;
250 }
251 
padRandom(unsigned long mult)252 BitString& BitString::padRandom (unsigned long mult)
253 {
254 	while (Length % mult != 0) {
255 		append (RndSrc.getBool()) ;
256 	}
257 	return *this ;
258 }
259 
getNAry(unsigned long p) const260 BYTE BitString::getNAry (unsigned long p) const
261 {
262 	unsigned long pbinary = p * ArityNBits ;
263 	BYTE retval = 0 ;
264 	for (unsigned short i = 0 ; (i < ArityNBits) && (pbinary + i < Length /* Length not a multiple of ArityNBits */) ; i++) {
265 		retval |= (*this)[pbinary + i] << i ;
266 	}
267 	return retval ;
268 }
269 
appendNAry(BYTE v)270 void BitString::appendNAry (BYTE v)
271 {
272 	for (unsigned short i = 0 ; i < ArityNBits ; i++) {
273 		_append ((v & (1 << i)) >> i) ;
274 	}
275 }
276 
277 #ifdef USE_ZLIB
compress(int level)278 BitString& BitString::compress (int level)
279 {
280 	myassert (level >= 1 && level <= 9) ;
281 	pad (8, 0) ;
282 
283 	// prepare source buffer
284 	zlib::uLong srclen = Data.size() ;
285 	zlib::Byte *srcbuf = new zlib::Byte[srclen] ;
286 	for (unsigned long i = 0 ; i < srclen ; i++) {
287 		srcbuf[i] = Data[i] ;
288 	}
289 
290 	// prepare dest buffer (see zlib manual for this magic length)
291 	zlib::uLong destlen = ((zlib::uLong) ((Data.size() + 12) * 1.001)) + 1 ;
292 	zlib::Byte* destbuf = new zlib::Byte[destlen] ;
293 
294 	// do compression
295 	int ret = zlib::compress2 (destbuf, &destlen, (const zlib::Bytef*) srcbuf, srclen, level) ;
296 	if (ret != Z_OK) {
297 		switch (ret) {
298 			case Z_MEM_ERROR:
299 			throw SteghideError (_("could not allocate memory.")) ;
300 			break ;
301 
302 			default:
303 			throw SteghideError (_("error %d while calling zlib's compress2."), ret) ;
304 			break ;
305 		}
306 	}
307 
308 	// write compressed data into Data
309 	Data.resize (destlen) ;
310 	for (unsigned long i = 0 ; i < destlen ; i++) {
311 		Data[i] = destbuf[i] ;
312 	}
313 	Length = destlen * 8 ;
314 
315 	delete[] srcbuf ;
316 	delete[] destbuf ;
317 	return *this ;
318 }
319 
uncompress(unsigned long idestlen)320 BitString& BitString::uncompress (unsigned long idestlen)
321 {
322 	myassert (Length % 8 == 0) ;
323 
324 	// prepare source buffer
325 	zlib::uLong srclen = Length / 8 ;
326 	zlib::Byte* srcbuf = new zlib::Byte[srclen] ;
327 	for (unsigned long i = 0 ; i < srclen ; i++) {
328 		srcbuf[i] = Data[i] ;
329 	}
330 
331 	// prepare destination buffer
332 	zlib::uLong ydestlen = 0 ;
333 	if (idestlen % 8 == 0) {
334 		ydestlen = idestlen / 8 ;
335 	}
336 	else {
337 		ydestlen = (idestlen / 8) + 1 ;
338 	}
339 	zlib::Byte* destbuf = new zlib::Byte[ydestlen] ;
340 
341 	// do uncompression
342 	zlib::uLong ydestlen_before = ydestlen ;
343 	int ret = zlib::uncompress (destbuf, &ydestlen, (const zlib::Bytef*) srcbuf, srclen) ;
344 	if (ret != Z_OK) {
345 		switch (ret) {
346 			case Z_MEM_ERROR:
347 			throw SteghideError (_("could not allocate memory.")) ;
348 			break ;
349 
350 			case Z_DATA_ERROR:
351 			throw SteghideError (_("can not uncompress data. compressed data is corrupted.")) ;
352 			break ;
353 
354 			default:
355 			throw SteghideError (_("error %d while calling zlib's uncompress."), ret) ;
356 			break ;
357 		}
358 	}
359 	myassert (ydestlen_before == ydestlen) ; // because idestlen must be _exactly_ size in bits
360 
361 	// write uncompressed data into Data
362 	Data.resize (ydestlen) ;
363 	for (unsigned long i = 0 ; i < ydestlen ; i++) {
364 		Data[i] = destbuf[i] ;
365 	}
366 	Length = idestlen ;
367 
368 	clearUnused() ;
369 
370 	delete[] srcbuf ;
371 	delete[] destbuf ;
372 	return *this ;
373 }
374 #endif // def USE_ZLIB
375 
operator ==(const BitString & v) const376 bool BitString::operator== (const BitString &v) const
377 {
378 	bool retval = true ;
379 
380 	if (v.getLength() == getLength()) {
381 		unsigned int n = getLength() ;
382 		for (unsigned int i = 0 ; i < n ; i++) {
383 			if (v[i] != (*this)[i]) {
384 				retval = false ;
385 			}
386 		}
387 	}
388 	else {
389 		retval = false ;
390 	}
391 
392 	return retval ;
393 }
394 
operator !=(const BitString & v) const395 bool BitString::operator!= (const BitString &v) const
396 {
397 	bool retval = false ;
398 
399 	if (v.getLength() != getLength()) {
400 		retval = true ;
401 	}
402 	else {
403 		for (unsigned int i = 0 ; i < getLength() ; i++) {
404 			if (v[i] != (*this)[i]) {
405 				retval = true ;
406 				break ;
407 			}
408 		}
409 	}
410 
411 	return retval ;
412 }
413 
operator ^=(const BitString & v)414 BitString& BitString::operator^= (const BitString &v)
415 {
416 	for (unsigned long i = 0 ; i < Length ; i++) {
417 		unsigned long bytepos = BYTEPOS (i) ;
418 		unsigned int bitpos = BITPOS (i) ;
419 		BIT bit = (Data[bytepos] & (1 << bitpos)) >> bitpos ;
420 		bit ^= v[i] ;
421 		Data[bytepos] = (Data[bytepos] & ~(1 << bitpos)) | (bit << bitpos) ;
422 	}
423 	return *this ;
424 }
425 
clearUnused()426 void BitString::clearUnused ()
427 {
428 	if (Length % 8 != 0) {
429 		// clear unused part of last byte (_append depends on this)
430 		unsigned short nbitsfilled = Length % 8 ;
431 		BYTE mask = 0x00 ;
432 		for (unsigned short i = 0 ; i < nbitsfilled ; i++) {
433 			mask <<= 1 ;
434 			mask |= 1 ;
435 		}
436 		Data[Data.size() - 1] &= mask ;
437 	}
438 }
439 
print(unsigned short spc) const440 void BitString::print (unsigned short spc) const
441 {
442 	char* space = new char[spc + 1] ;
443 	for (unsigned short i = 0 ; i < spc ; i++) {
444 		space[i] = ' ' ;
445 	}
446 	space[spc] = '\0' ;
447 	std::cerr << space << "Length: " << getLength() << std::endl ;
448 	std::cerr << space << "Data.size(): " << Data.size() << std::endl ;
449 	std::cerr << space << "Data:" << std::endl ;
450 
451 	std::cerr << space ;
452 	for (unsigned long i = 0 ; i < getLength() ; i++) {
453 		if ((i % 64 == 0) && (i > 0)) { // new line
454 			std::cerr << std::endl << space ;
455 		}
456 		if (i % 8 == 0) { // new byte
457 			std::cerr << " " ;
458 		}
459 		std::cerr << ((*this)[i]) ;
460 	}
461 	std::cerr << std::endl ;
462 }
463 
464 #ifdef DEBUG
printDebug(unsigned short level,unsigned short spc) const465 void BitString::printDebug (unsigned short level, unsigned short spc) const
466 {
467 	if (RUNDEBUGLEVEL(level)) {
468 		print (spc) ;
469 	}
470 }
471 #endif
472