summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/oscar/liboscar/transfer.cpp
blob: 9af57b30d83e1d72e35a49ad0875f4056f6bb2de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
    transfer.cpp - Kopete Groupwise Protocol

    Copyright (c) 2004 Matt Rogers <[email protected]>
    
    Based on code copyright (c) 2004 SUSE Linux AG <http://www.suse.com>

    Kopete (c) 2002-2004 by the Kopete developers <[email protected]>

    *************************************************************************
    *                                                                       *
    * This library is free software; you can redistribute it and/or         *
    * modify it under the terms of the GNU Lesser General Public            *
    * License as published by the Free Software Foundation; either          *
    * version 2 of the License, or (at your option) any later version.      *
    *                                                                       *
    *************************************************************************
*/

#include "transfer.h"
#include <ctype.h>
#include <tqdeepcopy.h>
#include <kdebug.h>

Transfer::Transfer()
{
	m_isBufferValid = false;
}

Transfer::Transfer( Buffer* buf )
{
	m_buffer = buf;
	m_isBufferValid = true;
}

Transfer::TransferType Transfer::type() const
{
	return Transfer::RawTransfer;
}

TQByteArray Transfer::toWire()
{
	m_wireFormat.duplicate( m_buffer->buffer(), m_buffer->length() );
	TQByteArray wire = TQDeepCopy<TQByteArray>( m_wireFormat );
	return wire;
}

Transfer::~Transfer()
{
	delete m_buffer;
	m_buffer = 0;
}

void Transfer::setBuffer( Buffer* buffer )
{
	m_buffer = buffer;
}

Buffer* Transfer::buffer()
{
	return m_buffer;
}

const Buffer* Transfer::buffer() const
{
	return m_buffer;
}

bool Transfer::dataValid() const
{
	return m_isBufferValid;
}

TQString Transfer::toString() const
{
	// line format:
	//00 03 00 0b 00 00 90 b8 f5 9f 09 31 31 33 37 38   |;tJ�..........|

	int i = 0;
	TQString output = "\n";
	TQString hex, ascii;

	TQByteArray::ConstIterator it;
	TQByteArray::ConstIterator end = m_wireFormat.end();
	for ( it = m_wireFormat.begin(); it != end; ++it )
	{
		i++;

		unsigned char c = static_cast<unsigned char>(*it);

		if(c < 0x10)
			hex.append("0");
		hex.append(TQString("%1 ").arg(c, 0, 16));

		ascii.append(isprint(c) ? c : '.');

		if (i == 16)
		{
			output += hex + "   |" + ascii + "|\n";
			i=0;
			hex=TQString();
			ascii=TQString();
		}
	}

	if(!hex.isEmpty())
		output += hex.leftJustify(48, ' ') + "   |" + ascii.leftJustify(16, ' ') + '|';
	output.append('\n');

	return output;
}

void Transfer::populateWireBuffer( int offset, const TQByteArray& buffer )
{
	int j;
	for ( uint i = 0; i < buffer.size(); ++i )
	{
		j = i + offset;
		m_wireFormat[j] = buffer[i];
	}
}
	

FlapTransfer::FlapTransfer()
	: Transfer()
{
	m_isFlapValid = false;
}

FlapTransfer::FlapTransfer( struct FLAP f, Buffer* buffer )
	: Transfer( buffer )
{
	m_flapChannel = f.channel;
	m_flapSequence = f.sequence;
	m_flapLength = f.length;

	if ( m_flapChannel == 0 || m_flapLength < 6 )
		m_isFlapValid = false;
	else
		m_isFlapValid = true;

}

FlapTransfer::FlapTransfer( Buffer* buffer, BYTE chan, WORD seq, WORD len )
	: Transfer( buffer )
{
	m_flapChannel = chan;
	m_flapSequence = seq;
	m_flapLength = len;
	
	if ( m_flapChannel == 0 || m_flapLength < 6 )
		m_isFlapValid = false;
	else
		m_isFlapValid = true;
}

FlapTransfer::~FlapTransfer()
{

}

TQByteArray FlapTransfer::toWire()
{
	//kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "Buffer length is " << m_buffer.length() << endl;
	//kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "Buffer is " << m_buffer.toString() << endl;
	
	m_wireFormat.truncate( 0 );
	TQByteArray useBuf;
	useBuf.duplicate( m_buffer->buffer(), m_buffer->length() );
	m_flapLength = useBuf.size();
	m_wireFormat.resize( 6 + m_flapLength );
	m_wireFormat[0] = 0x2A;
	m_wireFormat[1] = m_flapChannel;
	m_wireFormat[2] = (m_flapSequence & 0xFF00) >> 8;
	m_wireFormat[3] = (m_flapSequence & 0x00FF);
	m_wireFormat[4] = (m_flapLength & 0xFF00) >> 8;
	m_wireFormat[5] = (m_flapLength & 0x00FF);
	
	//deepcopy the high-level buffer to the wire format buffer
	populateWireBuffer( 6, useBuf );
	TQByteArray wire = TQDeepCopy<TQByteArray>( m_wireFormat );
	return wire;
}

void FlapTransfer::setFlapChannel( BYTE channel )
{
	if ( channel != 0 )
	{
		m_flapChannel = channel;
		m_isFlapValid = true;
	}
}


BYTE FlapTransfer::flapChannel() const
{
	return m_flapChannel;
}


void FlapTransfer::setFlapSequence( WORD seq )
{
	m_flapSequence = seq;
}


WORD FlapTransfer::flapSequence() const
{
	return m_flapSequence;
}

void FlapTransfer::setFlapLength( WORD len )
{
	m_flapLength = len;
}

WORD FlapTransfer::flapLength() const
{
	return m_flapLength;
}

bool FlapTransfer::flapValid() const
{
	return m_isFlapValid;
}

Transfer::TransferType FlapTransfer::type() const
{
	return Transfer::FlapTransfer;
}



SnacTransfer::SnacTransfer()
	: FlapTransfer()
{
	m_isSnacValid = false;
}


SnacTransfer::SnacTransfer( Buffer* buffer, BYTE chan, WORD seq, WORD len, WORD service, 
				 WORD subtype, WORD flags, DWORD reqId )
	: FlapTransfer( buffer, chan, seq, len )
{
	m_snacService = service;
	m_snacSubtype = subtype;
	m_snacFlags = flags;
	m_snacReqId = reqId;
	
	if ( m_snacService == 0 || m_snacSubtype == 0 )
		m_isSnacValid = false;
	else
		m_isSnacValid = true;
	
}

SnacTransfer::SnacTransfer( struct FLAP f, struct SNAC s, Buffer* buffer )
	: FlapTransfer( f, buffer )
{
	m_snacService = s.family;
	m_snacSubtype = s.subtype;
	m_snacFlags = s.flags;
	m_snacReqId = s.id;
	
	if ( m_snacService == 0 || m_snacSubtype == 0 )
		m_isSnacValid = false;
	else
		m_isSnacValid = true;
}

SnacTransfer::~SnacTransfer()
{

}

TQByteArray SnacTransfer::toWire()
{
	
	m_wireFormat.truncate( 0 );	
	TQByteArray useBuf;
	useBuf.duplicate( m_buffer->buffer(), m_buffer->length() );
	setFlapLength( useBuf.size() + 10 );
	m_wireFormat.resize( 16 + useBuf.size() );
	
	//Transfer the flap - 6 bytes
	m_wireFormat[0] = 0x2A;
	m_wireFormat[1] = flapChannel();
	m_wireFormat[2] = (flapSequence() & 0xFF00) >> 8;
	m_wireFormat[3] = (flapSequence() & 0x00FF);
	m_wireFormat[4] = (flapLength() & 0xFF00) >> 8;
	m_wireFormat[5] = (flapLength() & 0x00FF);
	
	//Transfer the Snac - 10 bytes
	m_wireFormat[6] = (m_snacService & 0xFF00) >> 8;
	m_wireFormat[7] = (m_snacService & 0x00FF);
	m_wireFormat[8] = (m_snacSubtype & 0xFF00) >> 8;
	m_wireFormat[9] = (m_snacSubtype & 0x00FF);
	m_wireFormat[10] = (m_snacFlags & 0xFF00) >> 8;
	m_wireFormat[11] = (m_snacFlags & 0x00FF);
	m_wireFormat[12] = (m_snacReqId & 0xFF000000) >> 24;
	m_wireFormat[13] = (m_snacReqId & 0x00FF0000) >> 16;
	m_wireFormat[14] = (m_snacReqId & 0x0000FF00) >> 8;
	m_wireFormat[15] = (m_snacReqId & 0x000000FF);
	
	//deepcopy the high-level buffer to the wire format buffer
	populateWireBuffer( 16, useBuf );
	TQByteArray wire = TQDeepCopy<TQByteArray>( m_wireFormat );
	return wire;
}

Transfer::TransferType SnacTransfer::type() const
{
	return Transfer::SnacTransfer;
}

bool SnacTransfer::snacValid() const
{
	return m_isSnacValid;
}

void SnacTransfer::setSnacService( WORD service )
{
	m_snacService = service;
}

WORD SnacTransfer::snacService() const
{
	return m_snacService;
}

void SnacTransfer::setSnacSubtype( WORD subtype )
{
	m_snacSubtype = subtype;
}

WORD SnacTransfer::snacSubtype() const
{
	return m_snacSubtype;
}

void SnacTransfer::setSnacFlags( WORD flags )
{
	m_snacFlags = flags;
}

WORD SnacTransfer::snacFlags() const
{
	return m_snacFlags;
}

void SnacTransfer::setSnacRequest( DWORD id )
{
	m_snacReqId = id;
}

DWORD SnacTransfer::snacRequest() const
{
	return m_snacReqId;
}

SNAC SnacTransfer::snac() const
{
	SNAC s = { m_snacService, m_snacSubtype, m_snacFlags, m_snacReqId };
	return s;
}