~ubuntu-branches/ubuntu/saucy/kopete/saucy-proposed

« back to all changes in this revision

Viewing changes to protocols/oscar/liboscar/inputprotocolbase.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-06-21 02:22:39 UTC
  • Revision ID: package-import@ubuntu.com-20130621022239-63l3zc8p0nf26pt6
Tags: upstream-4.10.80
ImportĀ upstreamĀ versionĀ 4.10.80

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Kopete Groupwise Protocol
 
3
    inputprotocolbase.cpp - Ancestor of all protocols used for reading GroupWise input
 
4
 
 
5
    Copyright (c) 2004      SUSE Linux AG                http://www.suse.com
 
6
 
 
7
    Kopete (c) 2002-2004 by the Kopete developers <kopete-devel@kde.org>
 
8
 
 
9
    *************************************************************************
 
10
    *                                                                       *
 
11
    * This library is free software; you can redistribute it and/or         *
 
12
    * modify it under the terms of the GNU Lesser General Public            *
 
13
    * License as published by the Free Software Foundation; either          *
 
14
    * version 2 of the License, or (at your option) any later version.      *
 
15
    *                                                                       *
 
16
    *************************************************************************
 
17
*/
 
18
 
 
19
#include "inputprotocolbase.h"
 
20
#include <QByteArray>
 
21
#include <QDataStream>
 
22
 
 
23
InputProtocolBase::InputProtocolBase(QObject *parent)
 
24
 : QObject(parent)
 
25
{
 
26
        m_state = NeedMore;
 
27
        m_bytes = 0;
 
28
}
 
29
 
 
30
 
 
31
InputProtocolBase::~InputProtocolBase()
 
32
{
 
33
}
 
34
 
 
35
uint InputProtocolBase::state() const
 
36
{
 
37
        return m_state;
 
38
}
 
39
 
 
40
bool InputProtocolBase::readString( QString &message )
 
41
{
 
42
        uint len;
 
43
        QByteArray rawData;
 
44
        if ( !safeReadBytes( rawData, len ) )
 
45
                return false;
 
46
        message = QString::fromUtf8( rawData.data(), len - 1 );
 
47
        return true;
 
48
}
 
49
 
 
50
 
 
51
bool InputProtocolBase::okToProceed()
 
52
{
 
53
        if ( m_din )
 
54
        {
 
55
                if ( m_din->atEnd() )
 
56
                {
 
57
                        m_state = NeedMore;
 
58
                        qDebug( "InputProtocol::okToProceed() - Server message ended prematurely!" );
 
59
                }
 
60
                else
 
61
                        return true;
 
62
        }
 
63
        return false;
 
64
}
 
65
 
 
66
bool InputProtocolBase::safeReadBytes( QByteArray& data, uint & len )
 
67
{
 
68
        // read the length of the bytes
 
69
        quint32 val;
 
70
        if ( !okToProceed() )
 
71
                return false;
 
72
        *m_din >> val;
 
73
        m_bytes += sizeof( quint32 );
 
74
        if ( val > 1024 )
 
75
                return false;
 
76
        //qDebug( "EventProtocol::safeReadBytes() - expecting %i bytes", val );
 
77
        QByteArray temp;
 
78
        temp.reserve( val );
 
79
        if ( val != 0 )
 
80
        {
 
81
                if ( !okToProceed() )
 
82
                        return false;
 
83
                // if the server splits packets here we are in trouble,
 
84
                // as there is no way to see how much data was actually read
 
85
                m_din->readRawData( temp.data(), val );
 
86
                // the rest of the string will be filled with FF,
 
87
                // so look for that in the last position instead of \0
 
88
                // this caused a crash - guessing that temp.length() is set to the number of bytes actually read...
 
89
                // if ( (quint8)( * ( temp.data() + ( temp.length() - 1 ) ) ) == 0xFF )
 
90
                if ( (uint)temp.length() < ( val - 1 ) )
 
91
                {
 
92
                        qDebug( "InputProtocol::safeReadBytes() - string broke, giving up, only got: %i bytes out of %i",  temp.length(), val );
 
93
                        m_state = NeedMore;
 
94
                        return false;
 
95
                }
 
96
        }
 
97
        data = temp;
 
98
        len = val;
 
99
        m_bytes += val;
 
100
        return true;
 
101
}
 
102
 
 
103
#include "inputprotocolbase.moc"