forked from xonotic/darkplaces
-
Notifications
You must be signed in to change notification settings - Fork 0
/
com_msg.c
413 lines (346 loc) · 9.82 KB
/
com_msg.c
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
Copyright (C) 1996-1997 Id Software, Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
// com_msg.c
#include "quakedef.h"
/*
============================================================================
BYTE ORDER FUNCTIONS
============================================================================
*/
float BuffBigFloat (const unsigned char *buffer)
{
union
{
float f;
unsigned int i;
}
u;
u.i = (buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | buffer[3];
return u.f;
}
int BuffBigLong (const unsigned char *buffer)
{
return (buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | buffer[3];
}
short BuffBigShort (const unsigned char *buffer)
{
return (buffer[0] << 8) | buffer[1];
}
float BuffLittleFloat (const unsigned char *buffer)
{
union
{
float f;
unsigned int i;
}
u;
u.i = (buffer[3] << 24) | (buffer[2] << 16) | (buffer[1] << 8) | buffer[0];
return u.f;
}
int BuffLittleLong (const unsigned char *buffer)
{
return (buffer[3] << 24) | (buffer[2] << 16) | (buffer[1] << 8) | buffer[0];
}
short BuffLittleShort (const unsigned char *buffer)
{
return (buffer[1] << 8) | buffer[0];
}
void StoreBigLong (unsigned char *buffer, unsigned int i)
{
buffer[0] = (i >> 24) & 0xFF;
buffer[1] = (i >> 16) & 0xFF;
buffer[2] = (i >> 8) & 0xFF;
buffer[3] = i & 0xFF;
}
void StoreBigShort (unsigned char *buffer, unsigned short i)
{
buffer[0] = (i >> 8) & 0xFF;
buffer[1] = i & 0xFF;
}
void StoreLittleLong (unsigned char *buffer, unsigned int i)
{
buffer[0] = i & 0xFF;
buffer[1] = (i >> 8) & 0xFF;
buffer[2] = (i >> 16) & 0xFF;
buffer[3] = (i >> 24) & 0xFF;
}
void StoreLittleShort (unsigned char *buffer, unsigned short i)
{
buffer[0] = i & 0xFF;
buffer[1] = (i >> 8) & 0xFF;
}
/*
==============================================================================
MESSAGE IO FUNCTIONS
Handles byte ordering and avoids alignment errors
==============================================================================
*/
//
// writing functions
//
void MSG_WriteChar (sizebuf_t *sb, int c)
{
unsigned char *buf;
buf = SZ_GetSpace (sb, 1);
buf[0] = c;
}
void MSG_WriteByte (sizebuf_t *sb, int c)
{
unsigned char *buf;
buf = SZ_GetSpace (sb, 1);
buf[0] = c;
}
void MSG_WriteShort (sizebuf_t *sb, int c)
{
unsigned char *buf;
buf = SZ_GetSpace (sb, 2);
buf[0] = c&0xff;
buf[1] = c>>8;
}
void MSG_WriteLong (sizebuf_t *sb, int c)
{
unsigned char *buf;
buf = SZ_GetSpace (sb, 4);
buf[0] = c&0xff;
buf[1] = (c>>8)&0xff;
buf[2] = (c>>16)&0xff;
buf[3] = c>>24;
}
void MSG_WriteFloat (sizebuf_t *sb, float f)
{
union
{
float f;
int l;
} dat;
dat.f = f;
dat.l = LittleLong (dat.l);
SZ_Write (sb, (unsigned char *)&dat.l, 4);
}
void MSG_WriteString (sizebuf_t *sb, const char *s)
{
if (!s || !*s)
MSG_WriteChar (sb, 0);
else
SZ_Write (sb, (unsigned char *)s, (int)strlen(s)+1);
}
void MSG_WriteUnterminatedString (sizebuf_t *sb, const char *s)
{
if (s && *s)
SZ_Write (sb, (unsigned char *)s, (int)strlen(s));
}
void MSG_WriteCoord13i (sizebuf_t *sb, float f)
{
MSG_WriteShort (sb, Q_rint(f*8));
}
void MSG_WriteCoord16i (sizebuf_t *sb, float f)
{
MSG_WriteShort (sb, Q_rint(f));
}
void MSG_WriteCoord32f (sizebuf_t *sb, float f)
{
MSG_WriteFloat (sb, f);
}
void MSG_WriteCoord (sizebuf_t *sb, float f, protocolversion_t protocol)
{
if (protocol == PROTOCOL_QUAKE || protocol == PROTOCOL_QUAKEDP || protocol == PROTOCOL_NEHAHRAMOVIE || protocol == PROTOCOL_NEHAHRABJP || protocol == PROTOCOL_NEHAHRABJP2 || protocol == PROTOCOL_NEHAHRABJP3 || protocol == PROTOCOL_QUAKEWORLD)
MSG_WriteCoord13i (sb, f);
else if (protocol == PROTOCOL_DARKPLACES1)
MSG_WriteCoord32f (sb, f);
else if (protocol == PROTOCOL_DARKPLACES2 || protocol == PROTOCOL_DARKPLACES3 || protocol == PROTOCOL_DARKPLACES4)
MSG_WriteCoord16i (sb, f);
else
MSG_WriteCoord32f (sb, f);
}
void MSG_WriteVector (sizebuf_t *sb, const vec3_t v, protocolversion_t protocol)
{
MSG_WriteCoord (sb, v[0], protocol);
MSG_WriteCoord (sb, v[1], protocol);
MSG_WriteCoord (sb, v[2], protocol);
}
// LadyHavoc: round to nearest value, rather than rounding toward zero, fixes crosshair problem
void MSG_WriteAngle8i (sizebuf_t *sb, float f)
{
MSG_WriteByte (sb, (int)Q_rint(f*(256.0/360.0)) & 255);
}
void MSG_WriteAngle16i (sizebuf_t *sb, float f)
{
MSG_WriteShort (sb, (int)Q_rint(f*(65536.0/360.0)) & 65535);
}
void MSG_WriteAngle32f (sizebuf_t *sb, float f)
{
MSG_WriteFloat (sb, f);
}
void MSG_WriteAngle (sizebuf_t *sb, float f, protocolversion_t protocol)
{
if (protocol == PROTOCOL_QUAKE || protocol == PROTOCOL_QUAKEDP || protocol == PROTOCOL_NEHAHRAMOVIE || protocol == PROTOCOL_NEHAHRABJP || protocol == PROTOCOL_NEHAHRABJP2 || protocol == PROTOCOL_NEHAHRABJP3 || protocol == PROTOCOL_DARKPLACES1 || protocol == PROTOCOL_DARKPLACES2 || protocol == PROTOCOL_DARKPLACES3 || protocol == PROTOCOL_DARKPLACES4 || protocol == PROTOCOL_QUAKEWORLD)
MSG_WriteAngle8i (sb, f);
else
MSG_WriteAngle16i (sb, f);
}
//
// reading functions
//
void MSG_InitReadBuffer (sizebuf_t *buf, unsigned char *data, int size)
{
memset(buf, 0, sizeof(*buf));
buf->data = data;
buf->maxsize = buf->cursize = size;
MSG_BeginReading(buf);
}
void MSG_BeginReading(sizebuf_t *sb)
{
sb->readcount = 0;
sb->badread = false;
}
int MSG_ReadLittleShort(sizebuf_t *sb)
{
if (sb->readcount+2 > sb->cursize)
{
sb->badread = true;
return -1;
}
sb->readcount += 2;
return (short)(sb->data[sb->readcount-2] | (sb->data[sb->readcount-1]<<8));
}
int MSG_ReadBigShort (sizebuf_t *sb)
{
if (sb->readcount+2 > sb->cursize)
{
sb->badread = true;
return -1;
}
sb->readcount += 2;
return (short)((sb->data[sb->readcount-2]<<8) + sb->data[sb->readcount-1]);
}
int MSG_ReadLittleLong (sizebuf_t *sb)
{
if (sb->readcount+4 > sb->cursize)
{
sb->badread = true;
return -1;
}
sb->readcount += 4;
return sb->data[sb->readcount-4] | (sb->data[sb->readcount-3]<<8) | (sb->data[sb->readcount-2]<<16) | (sb->data[sb->readcount-1]<<24);
}
int MSG_ReadBigLong (sizebuf_t *sb)
{
if (sb->readcount+4 > sb->cursize)
{
sb->badread = true;
return -1;
}
sb->readcount += 4;
return (sb->data[sb->readcount-4]<<24) + (sb->data[sb->readcount-3]<<16) + (sb->data[sb->readcount-2]<<8) + sb->data[sb->readcount-1];
}
float MSG_ReadLittleFloat (sizebuf_t *sb)
{
union
{
float f;
int l;
} dat;
if (sb->readcount+4 > sb->cursize)
{
sb->badread = true;
return -1;
}
sb->readcount += 4;
dat.l = sb->data[sb->readcount-4] | (sb->data[sb->readcount-3]<<8) | (sb->data[sb->readcount-2]<<16) | (sb->data[sb->readcount-1]<<24);
return dat.f;
}
float MSG_ReadBigFloat (sizebuf_t *sb)
{
union
{
float f;
int l;
} dat;
if (sb->readcount+4 > sb->cursize)
{
sb->badread = true;
return -1;
}
sb->readcount += 4;
dat.l = (sb->data[sb->readcount-4]<<24) | (sb->data[sb->readcount-3]<<16) | (sb->data[sb->readcount-2]<<8) | sb->data[sb->readcount-1];
return dat.f;
}
char *MSG_ReadString (sizebuf_t *sb, char *string, size_t maxstring)
{
int c;
size_t l = 0;
// read string into sbfer, but only store as many characters as will fit
while ((c = MSG_ReadByte(sb)) > 0)
if (l < maxstring - 1)
string[l++] = c;
string[l] = 0;
return string;
}
int MSG_ReadBytes (sizebuf_t *sb, int numbytes, unsigned char *out)
{
int l, c;
for (l = 0;l < numbytes && (c = MSG_ReadByte(sb)) != -1;l++)
out[l] = c;
return l;
}
float MSG_ReadCoord13i (sizebuf_t *sb)
{
return MSG_ReadLittleShort(sb) * (1.0/8.0);
}
float MSG_ReadCoord16i (sizebuf_t *sb)
{
return (signed short) MSG_ReadLittleShort(sb);
}
float MSG_ReadCoord32f (sizebuf_t *sb)
{
return MSG_ReadLittleFloat(sb);
}
float MSG_ReadCoord (sizebuf_t *sb, protocolversion_t protocol)
{
if (protocol == PROTOCOL_QUAKE || protocol == PROTOCOL_QUAKEDP || protocol == PROTOCOL_NEHAHRAMOVIE || protocol == PROTOCOL_NEHAHRABJP || protocol == PROTOCOL_NEHAHRABJP2 || protocol == PROTOCOL_NEHAHRABJP3 || protocol == PROTOCOL_QUAKEWORLD)
return MSG_ReadCoord13i(sb);
else if (protocol == PROTOCOL_DARKPLACES1)
return MSG_ReadCoord32f(sb);
else if (protocol == PROTOCOL_DARKPLACES2 || protocol == PROTOCOL_DARKPLACES3 || protocol == PROTOCOL_DARKPLACES4)
return MSG_ReadCoord16i(sb);
else
return MSG_ReadCoord32f(sb);
}
void MSG_ReadVector (sizebuf_t *sb, vec3_t v, protocolversion_t protocol)
{
v[0] = MSG_ReadCoord(sb, protocol);
v[1] = MSG_ReadCoord(sb, protocol);
v[2] = MSG_ReadCoord(sb, protocol);
}
// LadyHavoc: round to nearest value, rather than rounding toward zero, fixes crosshair problem
float MSG_ReadAngle8i (sizebuf_t *sb)
{
return (signed char) MSG_ReadByte (sb) * (360.0/256.0);
}
float MSG_ReadAngle16i (sizebuf_t *sb)
{
return (signed short)MSG_ReadShort (sb) * (360.0/65536.0);
}
float MSG_ReadAngle32f (sizebuf_t *sb)
{
return MSG_ReadFloat (sb);
}
float MSG_ReadAngle (sizebuf_t *sb, protocolversion_t protocol)
{
if (protocol == PROTOCOL_QUAKE || protocol == PROTOCOL_QUAKEDP || protocol == PROTOCOL_NEHAHRAMOVIE || protocol == PROTOCOL_NEHAHRABJP || protocol == PROTOCOL_NEHAHRABJP2 || protocol == PROTOCOL_NEHAHRABJP3 || protocol == PROTOCOL_DARKPLACES1 || protocol == PROTOCOL_DARKPLACES2 || protocol == PROTOCOL_DARKPLACES3 || protocol == PROTOCOL_DARKPLACES4 || protocol == PROTOCOL_QUAKEWORLD)
return MSG_ReadAngle8i (sb);
else
return MSG_ReadAngle16i (sb);
}