GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/eathena/messagein.cpp Lines: 0 51 0.0 %
Date: 2017-11-29 Branches: 0 62 0.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2004-2009  The Mana World Development Team
4
 *  Copyright (C) 2009-2010  The Mana Developers
5
 *  Copyright (C) 2011-2017  The ManaPlus Developers
6
 *
7
 *  This file is part of The ManaPlus Client.
8
 *
9
 *  This program is free software; you can redistribute it and/or modify
10
 *  it under the terms of the GNU General Public License as published by
11
 *  the Free Software Foundation; either version 2 of the License, or
12
 *  any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
#include "net/eathena/messagein.h"
24
25
#include "logger.h"
26
27
#include "net/net.h"
28
#include "net/packetcounters.h"
29
30
#include "utils/cast.h"
31
#include "utils/stringutils.h"
32
33
PRAGMA48(GCC diagnostic push)
34
PRAGMA48(GCC diagnostic ignored "-Wshadow")
35
#ifndef SDL_BIG_ENDIAN
36
#include <SDL_endian.h>
37
#endif  // SDL_BYTEORDER
38
PRAGMA48(GCC diagnostic pop)
39
40
#include "debug.h"
41
42
namespace EAthena
43
{
44
45
MessageIn::MessageIn(const char *const data,
46
                     const unsigned int length) :
47
    Net::MessageIn(data, length)
48
{
49
}
50
51
void MessageIn::postInit(const char *const str,
52
                         const unsigned int version)
53
{
54
    // Read the message ID
55
    mId = readId();
56
    mVersion = version;
57
    IGNOREDEBUGLOG;
58
    DEBUGLOG2("Receive packet", 0, "MessageIn");
59
#ifdef ENABLEDEBUGLOG
60
    if (mVersion > 0)
61
    {
62
        const std::string verStr = toString(mVersion);
63
        DEBUGLOG2("Version", 0, verStr.c_str());
64
    }
65
#endif  // ENABLEDEBUGLOG
66
67
    readInt16(str);
68
}
69
70
uint16_t MessageIn::readId() const
71
{
72
    int16_t value = -1;
73
    if (mPos + 2 <= mLength)
74
    {
75
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
76
        int16_t swap;
77
        memcpy(&swap, mData + CAST_SIZE(mPos), sizeof(int16_t));
78
        value = SDL_Swap16(swap);
79
#else  // SDL_BYTEORDER == SDL_BIG_ENDIAN
80
81
        memcpy(&value, mData + CAST_SIZE(mPos), sizeof(int16_t));
82
#endif  // SDL_BYTEORDER == SDL_BIG_ENDIAN
83
    }
84
    return value;
85
}
86
87
int16_t MessageIn::readInt16(const char *const str)
88
{
89
    int16_t value = -1;
90
    if (mPos + 2 <= mLength)
91
    {
92
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
93
        int16_t swap;
94
        memcpy(&swap, mData + CAST_SIZE(mPos), sizeof(int16_t));
95
        value = SDL_Swap16(swap);
96
#else  // SDL_BYTEORDER == SDL_BIG_ENDIAN
97
98
        memcpy(&value, mData + CAST_SIZE(mPos), sizeof(int16_t));
99
#endif  // SDL_BYTEORDER == SDL_BIG_ENDIAN
100
    }
101
    DEBUGLOG2("readInt16:  " + toStringPrint(CAST_U32(
102
        CAST_U16(value))),
103
        mPos, str);
104
    mPos += 2;
105
    PacketCounters::incInBytes(2);
106
    return value;
107
}
108
109
uint16_t MessageIn::readUInt16(const char *const str)
110
{
111
    uint16_t value = 0xffU;
112
    if (mPos + 2 <= mLength)
113
    {
114
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
115
        uint16_t swap;
116
        memcpy(&swap, mData + CAST_SIZE(mPos), sizeof(uint16_t));
117
        value = SDL_Swap16(swap);
118
#else  // SDL_BYTEORDER == SDL_BIG_ENDIAN
119
120
        memcpy(&value, mData + CAST_SIZE(mPos), sizeof(uint16_t));
121
#endif  // SDL_BYTEORDER == SDL_BIG_ENDIAN
122
    }
123
    DEBUGLOG2("readUInt16:  " + toStringPrint(CAST_U32(
124
        CAST_U16(value))),
125
        mPos, str);
126
    mPos += 2;
127
    PacketCounters::incInBytes(2);
128
    return value;
129
}
130
131
int32_t MessageIn::readInt32(const char *const str)
132
{
133
    int32_t value = -1;
134
    if (mPos + 4 <= mLength)
135
    {
136
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
137
        int32_t swap;
138
        memcpy(&swap, mData + CAST_SIZE(mPos), sizeof(int32_t));
139
        value = SDL_Swap32(swap);
140
#else  // SDL_BYTEORDER == SDL_BIG_ENDIAN
141
142
        memcpy(&value, mData + CAST_SIZE(mPos), sizeof(int32_t));
143
#endif  // SDL_BYTEORDER == SDL_BIG_ENDIAN
144
    }
145
    DEBUGLOG2("readInt32:  " + toStringPrint(CAST_U32(value)),
146
        mPos, str);
147
    mPos += 4;
148
    PacketCounters::incInBytes(4);
149
    return value;
150
}
151
152
BeingId MessageIn::readBeingId(const char *const str)
153
{
154
    return fromInt(readInt32(str), BeingId);
155
}
156
157
int64_t MessageIn::readInt64(const char *const str)
158
{
159
    int64_t value = -1;
160
    if (mPos + 8 <= mLength)
161
    {
162
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
163
        int64_t swap;
164
        memcpy(&swap, mData + CAST_SIZE(mPos), sizeof(int64_t));
165
        value = SDL_Swap64(swap);
166
#else  // SDL_BYTEORDER == SDL_BIG_ENDIAN
167
168
        memcpy(&value, mData + CAST_SIZE(mPos), sizeof(int64_t));
169
#endif  // SDL_BYTEORDER == SDL_BIG_ENDIAN
170
    }
171
    DEBUGLOG2("readInt64:  " + toStringPrint(CAST_U32(value)),
172
        mPos, str);
173
    mPos += 8;
174
    PacketCounters::incInBytes(8);
175
    return value;
176
}
177
178
}  // namespace EAthena