GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/tmwa/messagein.cpp Lines: 0 47 0.0 %
Date: 2017-11-29 Branches: 0 52 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/tmwa/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 TmwAthena
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
{
53
    // Read the message ID
54
    mId = readId();
55
    IGNOREDEBUGLOG;
56
    DEBUGLOG2("Receive packet", 0, "MessageIn");
57
    readInt16(str);
58
}
59
60
uint16_t MessageIn::readId() const
61
{
62
    int16_t value = -1;
63
    if (mPos + 2 <= mLength)
64
    {
65
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
66
        int16_t swap;
67
        memcpy(&swap, mData + CAST_SIZE(mPos), sizeof(int16_t));
68
        value = SDL_Swap16(swap);
69
#else  // SDL_BYTEORDER == SDL_BIG_ENDIAN
70
71
        memcpy(&value, mData + CAST_SIZE(mPos), sizeof(int16_t));
72
#endif  // SDL_BYTEORDER == SDL_BIG_ENDIAN
73
    }
74
    return value;
75
}
76
77
int16_t MessageIn::readInt16(const char *const str)
78
{
79
    int16_t value = -1;
80
    if (mPos + 2 <= mLength)
81
    {
82
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
83
        int16_t swap;
84
        memcpy(&swap, mData + CAST_SIZE(mPos), sizeof(int16_t));
85
        value = SDL_Swap16(swap);
86
#else  // SDL_BYTEORDER == SDL_BIG_ENDIAN
87
88
        memcpy(&value, mData + CAST_SIZE(mPos), sizeof(int16_t));
89
#endif  // SDL_BYTEORDER == SDL_BIG_ENDIAN
90
    }
91
    DEBUGLOG2("readInt16:  " + toStringPrint(CAST_U32(
92
        CAST_U16(value))),
93
        mPos, str);
94
    mPos += 2;
95
    PacketCounters::incInBytes(2);
96
    return value;
97
}
98
99
uint16_t MessageIn::readUInt16(const char *const str)
100
{
101
    uint16_t value = 0xffU;
102
    if (mPos + 2 <= mLength)
103
    {
104
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
105
        uint16_t swap;
106
        memcpy(&swap, mData + CAST_SIZE(mPos), sizeof(uint16_t));
107
        value = SDL_Swap16(swap);
108
#else  // SDL_BYTEORDER == SDL_BIG_ENDIAN
109
110
        memcpy(&value, mData + CAST_SIZE(mPos), sizeof(uint16_t));
111
#endif  // SDL_BYTEORDER == SDL_BIG_ENDIAN
112
    }
113
    DEBUGLOG2("readUInt16:  " + toStringPrint(CAST_U32(
114
        CAST_U16(value))),
115
        mPos, str);
116
    mPos += 2;
117
    PacketCounters::incInBytes(2);
118
    return value;
119
}
120
121
int32_t MessageIn::readInt32(const char *const str)
122
{
123
    int32_t value = -1;
124
    if (mPos + 4 <= mLength)
125
    {
126
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
127
        int32_t swap;
128
        memcpy(&swap, mData + CAST_SIZE(mPos), sizeof(int32_t));
129
        value = SDL_Swap32(swap);
130
#else  // SDL_BYTEORDER == SDL_BIG_ENDIAN
131
132
        memcpy(&value, mData + CAST_SIZE(mPos), sizeof(int32_t));
133
#endif  // SDL_BYTEORDER == SDL_BIG_ENDIAN
134
    }
135
    DEBUGLOG2("readInt32:  " + toStringPrint(CAST_U32(value)),
136
        mPos, str);
137
    mPos += 4;
138
    PacketCounters::incInBytes(4);
139
    return value;
140
}
141
142
BeingId MessageIn::readBeingId(const char *const str)
143
{
144
    return fromInt(readInt32(str), BeingId);
145
}
146
147
int64_t MessageIn::readInt64(const char *const str)
148
{
149
    int64_t value = -1;
150
    if (mPos + 8 <= mLength)
151
    {
152
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
153
        int64_t swap;
154
        memcpy(&swap, mData + CAST_SIZE(mPos), sizeof(int64_t));
155
        value = SDL_Swap64(swap);
156
#else  // SDL_BYTEORDER == SDL_BIG_ENDIAN
157
158
        memcpy(&value, mData + CAST_SIZE(mPos), sizeof(int64_t));
159
#endif  // SDL_BYTEORDER == SDL_BIG_ENDIAN
160
    }
161
    DEBUGLOG2("readInt64:  " + toStringPrint(CAST_U32(value)),
162
        mPos, str);
163
    mPos += 8;
164
    PacketCounters::incInBytes(8);
165
    return value;
166
}
167
168
}  // namespace TmwAthena