1 /***************************************************************************
2 buffer.cpp - description
6 Copyright (c) 2002 by Tom Linsky <twl6@po.cwru.edu>
7 Copyright (c) 2004,2005 by Matt Rogers <mattr@kde.org>
8 Kopete (c) 2002-2005 by the Kopete developers <kopete-devel@kde.org>
10 *************************************************************************
12 * This program is free software; you can redistribute it and/or modify *
13 * it under the terms of the GNU General Public License as published by *
14 * the Free Software Foundation; either version 2 of the License, or *
15 * (at your option) any later version. *
17 *************************************************************************
24 #include <kapplication.h>
32 Buffer::Buffer( const Buffer
& other
)
34 mBuffer
= other
.mBuffer
;
35 mReadPos
= other
.mReadPos
;
38 Buffer::Buffer(const char *b
, int len
)
40 mBuffer
= QByteArray::fromRawData( b
, len
);
44 Buffer::Buffer( const QByteArray
& data
)
56 int Buffer::addByte(const BYTE b
)
59 mBuffer
[mBuffer
.size()-1] = b
;
61 return mBuffer
.size();
64 int Buffer::addLEByte(const BYTE b
)
67 mBuffer
[mBuffer
.size()-1] = ((b
) & 0xff);
69 return mBuffer
.size();
73 int Buffer::addWord(const WORD w
)
76 mBuffer
[mBuffer
.size()-2] = ((w
& 0xff00) >> 8);
77 mBuffer
[mBuffer
.size()-1] = (w
& 0x00ff);
79 return mBuffer
.size();
82 int Buffer::addLEWord(const WORD w
)
85 mBuffer
[mBuffer
.size()-2] = (unsigned char) ((w
>> 0) & 0xff);
86 mBuffer
[mBuffer
.size()-1] = (unsigned char) ((w
>> 8) & 0xff);
88 return mBuffer
.size();
92 int Buffer::addDWord(const DWORD dw
)
95 mBuffer
[mBuffer
.size()-4] = (dw
& 0xff000000) >> 24;
96 mBuffer
[mBuffer
.size()-3] = (dw
& 0x00ff0000) >> 16;
97 mBuffer
[mBuffer
.size()-2] = (dw
& 0x0000ff00) >> 8;
98 mBuffer
[mBuffer
.size()-1] = (dw
& 0x000000ff);
100 return mBuffer
.size();
103 int Buffer::addLEDWord(const DWORD dw
)
106 mBuffer
[mBuffer
.size()-4] = (unsigned char) ((dw
>> 0) & 0xff);
107 mBuffer
[mBuffer
.size()-3] = (unsigned char) ((dw
>> 8) & 0xff);
108 mBuffer
[mBuffer
.size()-2] = (unsigned char) ((dw
>> 16) & 0xff);
109 mBuffer
[mBuffer
.size()-1] = (unsigned char) ((dw
>> 24) & 0xff);
111 return mBuffer
.size();
114 int Buffer::addString( const QByteArray
& s
)
117 return mBuffer
.size();
120 int Buffer::addString(QByteArray s
, DWORD len
)
123 return addString( s
);
126 int Buffer::addString( const char* s
, DWORD len
)
128 QByteArray
qba( s
, len
);
129 return addString( qba
);
132 int Buffer::addString(const unsigned char* s
, DWORD len
)
134 QByteArray
qba( (const char*) s
, len
);
135 return addString( qba
);
138 int Buffer::addLEString(const char *s
, const DWORD len
)
140 unsigned int pos
= mBuffer
.size();
142 //concatenate the new string onto the buffer
143 for(unsigned int i
=0; i
<len
; i
++)
145 mBuffer
[pos
+i
]=((s
[i
]) & 0xff);
147 return mBuffer
.size();
153 mBuffer
.truncate( 0 );
157 int Buffer::addTLV( const TLV
& t
)
159 return addTLV( t
.type
, t
.data
);
162 int Buffer::addTLV( WORD type
, const QByteArray
& data
)
165 addWord( data
.length() );
166 return addString( data
);
169 int Buffer::addLETLV( WORD type
, const QByteArray
& data
)
172 addLEWord( data
.length() );
173 return addString( data
);
176 BYTE
Buffer::getByte()
180 if(mReadPos
< mBuffer
.size())
182 thebyte
= mBuffer
[mReadPos
];
186 kDebug(14150) << "Buffer::getByte(): mBuffer empty" << endl
;
191 void Buffer::skipBytes( int bytesToSkip
)
193 if (mReadPos
< mBuffer
.size())
194 mReadPos
+= bytesToSkip
;
197 BYTE
Buffer::getLEByte()
203 WORD
Buffer::getWord()
205 WORD theword
, theword2
, retword
;
207 theword2
= getByte();
208 retword
= (theword
<< 8) | theword2
;
212 WORD
Buffer::getLEWord()
214 WORD theword1
, theword2
, retword
;
215 theword1
= getLEByte();
216 theword2
= getLEByte();
217 retword
= (theword2
<< 8) | theword1
;
221 DWORD
Buffer::getDWord()
227 retdword
= (word1
<< 16) | word2
;
231 DWORD
Buffer::getLEDWord()
233 DWORD word1
, word2
, retdword
;
236 retdword
= (word2
<< 16) | word1
;
240 void Buffer::setBuf(char *b
, const WORD len
)
242 mBuffer
= QByteArray::fromRawData(b
, len
);
246 QByteArray
Buffer::getBlock(WORD len
)
251 for ( int i
= 0; i
< len
; i
++ )
259 QByteArray
Buffer::getBBlock(WORD len
)
261 QByteArray data
= QByteArray::fromRawData( mBuffer
.data() + mReadPos
, len
);
267 WORD
*Buffer::getWordBlock(WORD len
)
269 kDebug(14150) << k_funcinfo
<< "of length " << len
<< endl
;
270 WORD
*ch
=new WORD
[len
+1];
271 for (unsigned int i
=0; i
<len
; i
++)
280 QByteArray
Buffer::getLEBlock(WORD len
)
283 for (unsigned int i
=0;i
<len
;i
++)
289 int Buffer::addTLV16(const WORD type
, const WORD data
)
292 addWord(0x0002); //2 bytes long
293 return addWord(data
);
296 int Buffer::addLETLV16(const WORD type
, const WORD data
)
299 addLEWord(0x0002); //2 bytes long
300 return addLEWord(data
);
303 int Buffer::addTLV8(const WORD type
, const BYTE data
)
306 addWord(0x0001); //1 byte long
307 return addByte(data
);
310 int Buffer::addLETLV8(const WORD type
, const BYTE data
)
313 addLEWord(0x0001); //1 byte long
314 return addLEByte(data
);
320 if(bytesAvailable() >= 4)
323 t
.length
= getWord();
325 t
.data
= getBlock( t
.length
);
327 kDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "Invalid TLV in buffer" << endl;*/
330 //kDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "TLV data is " << t.data << endl;
334 QList
<TLV
> Buffer::getTLVList()
338 while (mReadPos
< mBuffer
.size())
345 kDebug(14150) << k_funcinfo
<< "Invalid TLV found" << endl
;
349 //kDebug(14150) << k_funcinfo << "got TLV(" << t.type << ")" << endl;
356 int Buffer::addChatTLV(const WORD type
, const WORD exchange
,
357 const QString
&roomname
, const WORD instance
)
360 addWord(0x0005 + roomname
.length());
362 addByte(roomname
.length());
363 addString(roomname
.toLatin1(), roomname
.length()); // TODO: check encoding
365 return addWord(instance
);
368 void Buffer::expandBuffer(unsigned int inc
)
370 mBuffer
.resize(mBuffer
.size()+inc
);
373 QByteArray
Buffer::getLNTS()
375 WORD len
= getWord() - 1;
376 QByteArray
qcs( getBlock(len
) );
381 QByteArray
Buffer::getLELNTS()
383 WORD len
= getLEWord() - 1;
384 QByteArray
qcs( getBlock(len
) );
389 int Buffer::addLNTS(const char *s
)
391 unsigned int len
= strlen(s
);
396 int ret
= addByte(0x00);
400 int Buffer::addLELNTS(const char *s
)
402 unsigned int len
= strlen(s
);
403 int ret
= addLEWord(len
+1);
405 ret
= addLEString(s
, len
);
410 int Buffer::addBSTR(const char * s
)
412 unsigned int len
= strlen(s
);
413 int ret
= addWord(len
);
415 ret
= addString(s
, len
);
419 QByteArray
Buffer::getBSTR()
421 WORD len
= getWord();
422 QByteArray
qba( getBlock(len
) );
426 int Buffer::addBUIN(const char * s
)
428 unsigned int len
= strlen(s
);
429 int ret
= addByte(len
);
430 ret
= addString(s
, len
);
434 QByteArray
Buffer::getBUIN()
436 BYTE len
= getByte();
437 QByteArray
qba( getBlock(len
) );
441 QByteArray
Buffer::buffer() const
446 int Buffer::length() const
448 return mBuffer
.size();
451 int Buffer::bytesAvailable() const
453 return (mBuffer
.size() - mReadPos
);
458 QString
Buffer::toString() const
461 //00 03 00 0b 00 00 90 b8 f5 9f 09 31 31 33 37 38 |;tJ�..........|
464 QString output
= "\n";
467 QByteArray::ConstIterator it
;
468 for ( it
= mBuffer
.begin(); it
!= mBuffer
.end(); ++it
)
472 unsigned char c
= static_cast<unsigned char>(*it
);
476 hex
.append(QString("%1 ").arg(c
, 0, 16));
478 ascii
.append(isprint(c
) ? c
: '.');
482 output
+= hex
+ " |" + ascii
+ "|\n";
490 output
+= hex
.leftJustified(48, ' ') + " |" + ascii
.leftJustified(16, ' ') + '|';
496 QString
Buffer::peekBSTR()
498 int lastPos
= mReadPos
;
499 QByteArray qba
= getBSTR();
501 return QString( qba
);
503 QString
Buffer::peekBUIN()
505 int lastPos
= mReadPos
;
506 QByteArray qba
= getBUIN();
508 return QString( qba
);
512 int Buffer::addGuid( const Guid
& g
)
515 return addString( g
.data() );
516 return mBuffer
.size();
520 Guid
Buffer::getGuid()
522 return Guid(getBBlock(16)); //block or bblock?
526 Buffer::operator QByteArray() const
530 //kate: tab-width 4; indent-mode csands;