<commit_before>/*
    Kopete Oscar Protocol
    snacprotocol.cpp - reads the protocol used by Oscar for signalling stuff

    Copyright (c) 2004 <NAME> <<EMAIL>>
    Based on code copyright (c) 2004 SUSE Linux AG <http://www.suse.com>

    Kopete (c) 2002-2004 by the Kopete developers <<EMAIL>>

    *************************************************************************
    *                                                                       *
    * 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 "snacprotocol.h"

#include <qdatastream.h>
#include <qobject.h>
#include <kdebug.h>
#include <stdlib.h>
#include "transfer.h"


using namespace Oscar;

SnacProtocol::SnacProtocol(QObject *parent)
 : InputProtocolBase(parent)
{
}

SnacProtocol::~SnacProtocol()
{
}

Transfer* SnacProtocol::parse( const QByteArray & packet, uint& bytes )
{
	BYTE b;
	WORD w;
	DWORD dw;

	FLAP f;
	SNAC s;
	SnacTransfer *st;
	QDataStream* m_din = new QDataStream( const_cast<QByteArray*>( &packet ), QIODevice::ReadOnly );

	//flap parsing
	*m_din >> b; //this should be the start byte
	//kDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "start byte is " << b << endl;
	*m_din >> b;
	f.channel = b;
	*m_din >> w;
	f.sequence = w;
	*m_din >> w;
	f.length = w;

	if ( ( f.length + 6 ) > packet.size() )
	{
		kDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "Packet not big enough to parse!" << endl;
		kDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "packet size is " << packet.size()
			<< " we need " << f.length + 6 << endl;
		return 0;
	}
	//snac parsing
	*m_din >> w;
	s.family = w;
	*m_din >> w;
	s.subtype = w;
	*m_din >> w;
	s.flags = w;
	*m_din >> dw;
	s.id = dw;

	kDebug(OSCAR_RAW_DEBUG) << k_funcinfo  << "family: " << s.family
			<< " subtype: " << s.subtype << " flags: " << s.flags
			<< " id: " << s.id << endl;

	//use pointer arithmatic to skip the flap and snac headers
	//so we don't have to do double parsing in the tasks
	char* charPacket = const_cast<char*>( packet.data() );
	char* snacData;
	int snacOffset = 10; //default
	if ( s.flags >= 0x8000  ) //skip the next 8 bytes, we don't care about the snac version ATM
	{
		//kDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "skipping snac version" << endl;
		snacOffset = 18;
		snacData = charPacket + 24;
	}
	else
	{
		snacOffset = 10;
		snacData = charPacket + 16;
	}

	Buffer *snacBuffer = new Buffer( snacData, f.length - snacOffset  );
	st = new SnacTransfer( f, s, snacBuffer );
	bytes = f.length + 6;
	delete m_din;
	m_din = 0;
	return st;
}


#include "snacprotocol.moc"<commit_msg>fix memory leak (CID 2124)<commit_after>/*
    Kopete Oscar Protocol
    snacprotocol.cpp - reads the protocol used by Oscar for signalling stuff

    Copyright (c) 2004 <NAME> <<EMAIL>>
    Based on code copyright (c) 2004 SUSE Linux AG <http://www.suse.com>

    Kopete (c) 2002-2004 by the Kopete developers <<EMAIL>>

    *************************************************************************
    *                                                                       *
    * 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 "snacprotocol.h"

#include <qdatastream.h>
#include <qobject.h>
#include <kdebug.h>
#include <stdlib.h>
#include "transfer.h"


using namespace Oscar;

SnacProtocol::SnacProtocol(QObject *parent)
 : InputProtocolBase(parent)
{
}

SnacProtocol::~SnacProtocol()
{
}

Transfer* SnacProtocol::parse( const QByteArray & packet, uint& bytes )
{
	BYTE b;
	WORD w;
	DWORD dw;

	FLAP f;
	SNAC s;
	SnacTransfer *st;
	QDataStream din( const_cast<QByteArray*>( &packet ), QIODevice::ReadOnly );

	//flap parsing
	din >> b; //this should be the start byte
	//kDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "start byte is " << b << endl;
	din >> b;
	f.channel = b;
	din >> w;
	f.sequence = w;
	din >> w;
	f.length = w;

	if ( ( f.length + 6 ) > packet.size() )
	{
		kDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "Packet not big enough to parse!" << endl;
		kDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "packet size is " << packet.size()
			<< " we need " << f.length + 6 << endl;
		return 0;
	}
	//snac parsing
	din >> w;
	s.family = w;
	din >> w;
	s.subtype = w;
	din >> w;
	s.flags = w;
	din >> dw;
	s.id = dw;

	kDebug(OSCAR_RAW_DEBUG) << k_funcinfo  << "family: " << s.family
			<< " subtype: " << s.subtype << " flags: " << s.flags
			<< " id: " << s.id << endl;

	//use pointer arithmatic to skip the flap and snac headers
	//so we don't have to do double parsing in the tasks
	char* charPacket = const_cast<char*>( packet.data() );
	char* snacData;
	int snacOffset = 10; //default
	if ( s.flags >= 0x8000  ) //skip the next 8 bytes, we don't care about the snac version ATM
	{
		//kDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "skipping snac version" << endl;
		snacOffset = 18;
		snacData = charPacket + 24;
	}
	else
	{
		snacOffset = 10;
		snacData = charPacket + 16;
	}

	Buffer *snacBuffer = new Buffer( snacData, f.length - snacOffset  );
	st = new SnacTransfer( f, s, snacBuffer );
	bytes = f.length + 6;
	return st;
}


#include "snacprotocol.moc"
{'fraction_non_alphanumeric': 0.17544160521351398, 'fraction_numerical': 0.013548276453438519, 'mean_word_length': 2.7408595253367545, 'pattern_counts': {'":': 0, '<': 131, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2481373', 'n_tokens_mistral': 2125, 'n_tokens_neox': 1930, 'n_words': 589}