-
Notifications
You must be signed in to change notification settings - Fork 166
/
common.cpp
2781 lines (2298 loc) · 51.7 KB
/
common.cpp
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
*
* 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
*
* In addition, as a special exception, the author gives permission to
* link the code of this program with the Half-Life Game Engine ("HL
* Engine") and Modified Game Libraries ("MODs") developed by Valve,
* L.L.C ("Valve"). You must obey the GNU General Public License in all
* respects for all of the code used other than the HL Engine and MODs
* from Valve. If you modify this file, you may extend this exception
* to your version of the file, but you are not obligated to do so. If
* you do not wish to do so, delete this exception statement from your
* version.
*
*/
#include "precompiled.h"
char serverinfo[MAX_INFO_STRING];
char gpszVersionString[32];
char gpszProductString[32];
char *Info_Serverinfo(void)
{
return serverinfo;
}
#ifndef COM_Functions_region
unsigned char COM_Nibble(char c)
{
if (c >= '0' && c <= '9')
{
return (unsigned char)(c - '0');
}
if (c >= 'A' && c <= 'F')
{
return (unsigned char)(c - 'A' + 0x0A);
}
if (c >= 'a' && c <= 'f')
{
return (unsigned char)(c - 'a' + 0x0A);
}
return '0';
}
void COM_HexConvert(const char *pszInput, int nInputLength, unsigned char *pOutput)
{
unsigned char *p;
int i;
const char *pIn;
p = pOutput;
for (i = 0; i < nInputLength - 1; i += 2)
{
pIn = &pszInput[i];
if (pIn[0] == 0 || pIn[1] == 0)
break;
*p = COM_Nibble(pIn[0]) << 4 | COM_Nibble(pIn[1]);
p++;
}
}
NOXREF char *COM_BinPrintf(unsigned char *buf, int nLen)
{
NOXREFCHECK;
static char szReturn[4096];
unsigned char c;
char szChunk[10];
int i;
Q_memset(szReturn, 0, sizeof(szReturn));
for (i = 0; i < nLen; i++)
{
c = (unsigned char)buf[i];
Q_snprintf(szChunk, sizeof(szChunk), "%02x", c);
Q_strncat(szReturn, szChunk, sizeof(szReturn) - Q_strlen(szReturn) - 1);
}
return szReturn;
}
void COM_ExplainDisconnection(qboolean bPrint, char *fmt, ...)
{
va_list argptr;
static char string[1024];
va_start(argptr, fmt);
Q_vsnprintf(string, sizeof(string), fmt, argptr);
va_end(argptr);
Q_strncpy(gszDisconnectReason, string, sizeof(gszDisconnectReason) - 1);
gszDisconnectReason[sizeof(gszDisconnectReason) - 1] = 0;
gfExtendedError = 1;
if (bPrint)
{
if (gszDisconnectReason[0] != '#')
Con_Printf("%s\n", gszDisconnectReason);
}
}
NOXREF void COM_ExtendedExplainDisconnection(qboolean bPrint, char *fmt, ...)
{
NOXREFCHECK;
va_list argptr;
static char string[1024];
va_start(argptr, fmt);
Q_vsnprintf(string, sizeof(string), fmt, argptr);
va_end(argptr);
Q_strncpy(gszExtendedDisconnectReason, string, sizeof(gszExtendedDisconnectReason) - 1);
gszExtendedDisconnectReason[sizeof(gszExtendedDisconnectReason) - 1] = 0;
if (bPrint)
{
if (gszExtendedDisconnectReason[0] != '#')
Con_Printf("%s\n", gszExtendedDisconnectReason);
}
}
#endif // COM_Functions_region
#ifndef Byte_Functions_region
/*
============================================================================
BYTE ORDER FUNCTIONS
============================================================================
*/
qboolean bigendien;
short (*BigShort)(short l);
short (*LittleShort)(short l);
int (*BigLong)(int l);
int (*LittleLong)(int l);
float (*BigFloat)(float l);
float (*LittleFloat)(float l);
int LongSwap(int l)
{
return bswap(l);
}
int LongNoSwap(int l)
{
return l;
}
short ShortSwap(short l)
{
return bswap(l);
}
short ShortNoSwap(short l)
{
return l;
}
float FloatSwap(float f)
{
/*union
{
float f;
byte b[4];
} dat1, dat2;
dat1.f = f;
dat2.b[0] = dat1.b[3];
dat2.b[1] = dat1.b[2];
dat2.b[2] = dat1.b[1];
dat2.b[3] = dat1.b[0];
return dat2.f;*/
//unsigned long u = bswap(*(unsigned long *)&f);
//return *(float *)&u;
return bswap(f);
}
float FloatNoSwap(float f)
{
return f;
}
#endif // Byte_Functions_region
#ifndef MSG_Functions_region
// MESSAGE IO FUNCTIONS
// Handles byte ordering and avoids alignment errors
int msg_badread;
int msg_readcount;
// Some bit tables...
const uint32 BITTABLE[] =
{
0x00000001, 0x00000002, 0x00000004, 0x00000008,
0x00000010, 0x00000020, 0x00000040, 0x00000080,
0x00000100, 0x00000200, 0x00000400, 0x00000800,
0x00001000, 0x00002000, 0x00004000, 0x00008000,
0x00010000, 0x00020000, 0x00040000, 0x00080000,
0x00100000, 0x00200000, 0x00400000, 0x00800000,
0x01000000, 0x02000000, 0x04000000, 0x08000000,
0x10000000, 0x20000000, 0x40000000, 0x80000000,
0x00000000,
};
const uint32 ROWBITTABLE[] =
{
0x00000000, 0x00000001, 0x00000003, 0x00000007,
0x0000000F, 0x0000001F, 0x0000003F, 0x0000007F,
0x000000FF, 0x000001FF, 0x000003FF, 0x000007FF,
0x00000FFF, 0x00001FFF, 0x00003FFF, 0x00007FFF,
0x0000FFFF, 0x0001FFFF, 0x0003FFFF, 0x0007FFFF,
0x000FFFFF, 0x001FFFFF, 0x003FFFFF, 0x007FFFFF,
0x00FFFFFF, 0x01FFFFFF, 0x03FFFFFF, 0x07FFFFFF,
0x0FFFFFFF, 0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF,
0xFFFFFFFF,
};
const uint32 INVBITTABLE[] =
{
0xFFFFFFFE, 0xFFFFFFFD, 0xFFFFFFFB, 0xFFFFFFF7,
0xFFFFFFEF, 0xFFFFFFDF, 0xFFFFFFBF, 0xFFFFFF7F,
0xFFFFFEFF, 0xFFFFFDFF, 0xFFFFFBFF, 0xFFFFF7FF,
0xFFFFEFFF, 0xFFFFDFFF, 0xFFFFBFFF, 0xFFFF7FFF,
0xFFFEFFFF, 0xFFFDFFFF, 0xFFFBFFFF, 0xFFF7FFFF,
0xFFEFFFFF, 0xFFDFFFFF, 0xFFBFFFFF, 0xFF7FFFFF,
0xFEFFFFFF, 0xFDFFFFFF, 0xFBFFFFFF, 0xF7FFFFFF,
0xEFFFFFFF, 0xDFFFFFFF, 0xBFFFFFFF, 0x7FFFFFFF,
0xFFFFFFFF,
};
void MSG_WriteChar(sizebuf_t *sb, int c)
{
unsigned char *buf = (unsigned char *)SZ_GetSpace(sb, 1);
*(char *)buf = (char)c;
}
void MSG_WriteByte(sizebuf_t *sb, int c)
{
unsigned char *buf = (unsigned char *)SZ_GetSpace(sb, 1);
*(byte *)buf = (byte)c;
}
void MSG_WriteShort(sizebuf_t *sb, int c)
{
unsigned char *buf = (unsigned char *)SZ_GetSpace(sb, 2);
*(int16 *)buf = (int16)c;
}
void MSG_WriteWord(sizebuf_t *sb, int c)
{
unsigned char *buf = (unsigned char *)SZ_GetSpace(sb, 2);
*(uint16 *)buf = (uint16)c;
}
void MSG_WriteLong(sizebuf_t *sb, int c)
{
unsigned char *buf = (unsigned char *)SZ_GetSpace(sb, 4);
*(uint32 *)buf = (uint32)c;
}
void MSG_WriteFloat(sizebuf_t *sb, float f)
{
int i = LittleLong(*(int *)&f);
SZ_Write(sb, &i, 4);
}
void MSG_WriteString(sizebuf_t *sb, const char *s)
{
if (s)
{
SZ_Write(sb, s, Q_strlen(s) + 1);
}
else
{
SZ_Write(sb, "", 1);
}
}
void MSG_WriteBuf(sizebuf_t *sb, int iSize, void *buf)
{
if (buf)
{
SZ_Write(sb, buf, iSize);
}
}
void MSG_WriteAngle(sizebuf_t *sb, float f)
{
MSG_WriteByte(sb, (int64)(fmod((double)f, 360.0) * 256.0 / 360.0) & 0xFF);
}
void MSG_WriteHiresAngle(sizebuf_t *sb, float f)
{
MSG_WriteShort(sb, (int64)(fmod((double)f, 360.0) * 65536.0 / 360.0) & 0xFFFF);
}
void MSG_WriteUsercmd(sizebuf_t *buf, usercmd_t *to, usercmd_t *from)
{
delta_t **ppdesc;
ppdesc = DELTA_LookupRegistration("usercmd_t");
MSG_StartBitWriting(buf);
DELTA_WriteDelta((byte *)from, (byte *)to, 1, *ppdesc, 0);
MSG_EndBitWriting(buf);
}
typedef struct bf_write_s
{
// For enhanced and safe bits writing functions
#if defined(REHLDS_FIXES)
#pragma pack(push, 1)
union {
uint64 u64;
uint32 u32[2];
uint8 u8[8];
} pendingData;
uint64 sse_highbits;
#pragma pack(pop)
int nCurOutputBit;
sizebuf_t *pbuf;
#else // defined(REHLDS_FIXES)
int nCurOutputBit;
unsigned char *pOutByte;
sizebuf_t *pbuf;
#endif // defined(REHLDS_FIXES)
} bf_write_t;
typedef struct bf_read_s
{
int nMsgReadCount; // was msg_readcount
sizebuf_t *pbuf;
int nBitFieldReadStartByte;
int nBytesRead;
int nCurInputBit;
unsigned char *pInByte;
} bf_read_t;
// Bit field reading/writing storage.
bf_read_t bfread;
ALIGN16 bf_write_t bfwrite;
void COM_BitOpsInit(void)
{
Q_memset(&bfwrite, 0, sizeof(bf_write_t));
Q_memset(&bfread, 0, sizeof(bf_read_t));
}
// Enhanced and safe bits writing functions
#if defined(REHLDS_FIXES)
void MSG_WBits_MaybeFlush() {
if (bfwrite.nCurOutputBit < 32)
return;
uint32* pDest = (uint32*)SZ_GetSpace(bfwrite.pbuf, 4);
if (!(bfwrite.pbuf->flags & SIZEBUF_OVERFLOWED))
*pDest = bfwrite.pendingData.u32[0];
bfwrite.pendingData.u32[0] = bfwrite.pendingData.u32[1];
bfwrite.pendingData.u32[1] = 0;
bfwrite.nCurOutputBit -= 32;
}
void MSG_WriteBits(uint32 data, int numbits)
{
uint32 maxval = _mm_cvtsi128_si32(_mm_slli_epi64(_mm_cvtsi32_si128(1), numbits)) - 1; //maxval = (1 << numbits) - 1
if (data > maxval)
data = maxval;
MSG_WBits_MaybeFlush();
__m128i pending = _mm_load_si128((__m128i*) &bfwrite.pendingData.u64);
__m128i mmdata = _mm_slli_epi64(_mm_cvtsi32_si128(data), bfwrite.nCurOutputBit); //mmdata = data << bfwrite.nCurOutputBit
pending = _mm_or_si128(pending, mmdata);
_mm_store_si128((__m128i*) &bfwrite.pendingData.u64, pending);
bfwrite.nCurOutputBit += numbits;
}
void MSG_WriteOneBit(int nValue) {
MSG_WriteBits(nValue, 1);
}
void MSG_StartBitWriting(sizebuf_t *buf)
{
bfwrite.nCurOutputBit = 0;
bfwrite.pbuf = buf;
bfwrite.pendingData.u64 = 0;
}
void MSG_EndBitWriting(sizebuf_t *buf)
{
int bytesNeed = bfwrite.nCurOutputBit / 8;
if ((bfwrite.nCurOutputBit % 8) || bytesNeed == 0) {
bytesNeed++;
}
uint8* pData = (uint8*)SZ_GetSpace(bfwrite.pbuf, bytesNeed);
if (!(bfwrite.pbuf->flags & SIZEBUF_OVERFLOWED)) {
for (int i = 0; i < bytesNeed; i++) {
pData[i] = bfwrite.pendingData.u8[i];
}
}
}
#else // defined(REHLDS_FIXES)
void MSG_WriteOneBit(int nValue)
{
if (bfwrite.nCurOutputBit >= 8)
{
SZ_GetSpace(bfwrite.pbuf, 1);
bfwrite.nCurOutputBit = 0;
++bfwrite.pOutByte;
}
if (!(bfwrite.pbuf->flags & SIZEBUF_OVERFLOWED))
{
if (nValue)
{
*bfwrite.pOutByte |= BITTABLE[bfwrite.nCurOutputBit];
}
else
{
*bfwrite.pOutByte &= INVBITTABLE[bfwrite.nCurOutputBit * 4];
}
bfwrite.nCurOutputBit++;
}
}
void MSG_StartBitWriting(sizebuf_t *buf)
{
bfwrite.nCurOutputBit = 0;
bfwrite.pbuf = buf;
bfwrite.pOutByte = &buf->data[buf->cursize];
}
void MSG_EndBitWriting(sizebuf_t *buf)
{
if (!(bfwrite.pbuf->flags & SIZEBUF_OVERFLOWED))
{
*bfwrite.pOutByte &= 255 >> (8 - bfwrite.nCurOutputBit);
SZ_GetSpace(bfwrite.pbuf, 1);
bfwrite.nCurOutputBit = 0;
bfwrite.pOutByte = 0;
bfwrite.pbuf = 0;
}
}
void MSG_WriteBits(uint32 data, int numbits)
{
if (numbits < 32)
{
if (data >= (uint32)(1 << numbits))
data = ROWBITTABLE[numbits];
}
int surplusBytes = 0;
if ((uint32)bfwrite.nCurOutputBit >= 8)
{
surplusBytes = 1;
bfwrite.nCurOutputBit = 0;
++bfwrite.pOutByte;
}
int bits = numbits + bfwrite.nCurOutputBit;
if (bits <= 32)
{
int bytesToWrite = bits >> 3;
int bitsLeft = bits & 7;
if (!bitsLeft)
--bytesToWrite;
SZ_GetSpace(bfwrite.pbuf, surplusBytes + bytesToWrite);
if (!(bfwrite.pbuf->flags & SIZEBUF_OVERFLOWED))
{
*(uint32 *)bfwrite.pOutByte = (data << bfwrite.nCurOutputBit) | *(uint32 *)bfwrite.pOutByte & ROWBITTABLE[bfwrite.nCurOutputBit];
bfwrite.nCurOutputBit = 8;
if (bitsLeft)
bfwrite.nCurOutputBit = bitsLeft;
bfwrite.pOutByte = &bfwrite.pOutByte[bytesToWrite];
}
}
else
{
SZ_GetSpace(bfwrite.pbuf, surplusBytes + 4);
if (!(bfwrite.pbuf->flags & SIZEBUF_OVERFLOWED))
{
*(uint32 *)bfwrite.pOutByte = (data << bfwrite.nCurOutputBit) | *(uint32 *)bfwrite.pOutByte & ROWBITTABLE[bfwrite.nCurOutputBit];
int leftBits = 32 - bfwrite.nCurOutputBit;
bfwrite.nCurOutputBit = bits & 7;
bfwrite.pOutByte += 4;
*(uint32 *)bfwrite.pOutByte = data >> leftBits;
}
}
}
#endif //defined(REHLDS_FIXES)
NOXREF qboolean MSG_IsBitWriting(void)
{
NOXREFCHECK;
return bfwrite.pbuf != 0;
}
void MSG_WriteSBits(int data, int numbits)
{
int idata = data;
if (numbits < 32)
{
int maxnum = (1 << (numbits - 1)) - 1;
if (data > maxnum || (maxnum = -maxnum, data < maxnum))
{
idata = maxnum;
}
}
int sigbits = idata < 0;
MSG_WriteOneBit(sigbits);
MSG_WriteBits(abs(idata), numbits - 1);
}
void MSG_WriteBitString(const char *p)
{
#ifdef REHLDS_FIXES
const uint8_t *pch = (uint8_t *)p;
#else // REHLDS_FIXES
char *pch = (char *)p;
#endif // REHLDS_FIXES
while (*pch)
{
MSG_WriteBits(*pch, 8);
++pch;
}
MSG_WriteBits(0, 8);
}
void MSG_WriteBitData(void *src, int length)
{
int i;
byte *p = (byte *)src;
for (i = 0; i < length; i++, p++)
{
MSG_WriteBits(*p, 8);
}
}
void MSG_WriteBitAngle(float fAngle, int numbits)
{
if (numbits >= 32)
{
Sys_Error("%s: Can't write bit angle with 32 bits precision\n", __func__);
}
uint32 shift = (1 << numbits);
uint32 mask = shift - 1;
int d = (int)(shift * fmod((double)fAngle, 360.0)) / 360;
d &= mask;
MSG_WriteBits(d, numbits);
}
float MSG_ReadBitAngle(int numbits)
{
return (float)(MSG_ReadBits(numbits) * (360.0 / (1 << numbits)));
}
int MSG_CurrentBit(void)
{
int nbits;
if (bfread.pbuf)
{
nbits = bfread.nCurInputBit + 8 * bfread.nBytesRead;
}
else
{
nbits = 8 * msg_readcount;
}
return nbits;
}
NOXREF qboolean MSG_IsBitReading(void)
{
NOXREFCHECK;
return bfread.pbuf != 0;
}
void MSG_StartBitReading(sizebuf_t *buf)
{
bfread.nCurInputBit = 0;
bfread.nBytesRead = 0;
bfread.nBitFieldReadStartByte = msg_readcount;
bfread.pbuf = buf;
bfread.pInByte = &buf->data[msg_readcount];
bfread.nMsgReadCount = msg_readcount + 1;
if (msg_readcount + 1 > buf->cursize)
{
msg_badread = 1;
}
}
void MSG_EndBitReading(sizebuf_t *buf)
{
if (bfread.nMsgReadCount > buf->cursize)
{
msg_badread = 1;
}
msg_readcount = bfread.nMsgReadCount;
bfread.nBitFieldReadStartByte = 0;
bfread.nCurInputBit = 0;
bfread.nBytesRead = 0;
bfread.pInByte = 0;
bfread.pbuf = 0;
}
int MSG_ReadOneBit(void)
{
int nValue;
if (msg_badread)
{
nValue = 1;
}
else
{
if (bfread.nCurInputBit >= 8)
{
++bfread.nMsgReadCount;
bfread.nCurInputBit = 0;
++bfread.nBytesRead;
++bfread.pInByte;
}
if (bfread.nMsgReadCount <= bfread.pbuf->cursize)
{
nValue = (*bfread.pInByte & BITTABLE[bfread.nCurInputBit]) != 0;
++bfread.nCurInputBit;
}
else
{
nValue = 1;
msg_badread = 1;
}
}
return nValue;
}
uint32 MSG_ReadBits(int numbits)
{
uint32 result;
#ifdef REHLDS_FIXES
if (numbits > 32) {
Sys_Error("%s: invalid numbits %d\n", __func__, numbits);
}
#endif // REHLDS_FIXES
if (msg_badread)
{
result = 1;
}
else
{
if (bfread.nCurInputBit >= 8)
{
++bfread.nMsgReadCount;
++bfread.nBytesRead;
++bfread.pInByte;
bfread.nCurInputBit = 0;
}
uint32 bits = (bfread.nCurInputBit + numbits) & 7;
if ((unsigned int)(bfread.nCurInputBit + numbits) <= 32)
{
result = (*(unsigned int *)bfread.pInByte >> bfread.nCurInputBit) & ROWBITTABLE[numbits];
uint32 bytes = (bfread.nCurInputBit + numbits) >> 3;
if (bits)
{
bfread.nCurInputBit = bits;
}
else
{
bfread.nCurInputBit = 8;
bytes--;
}
bfread.pInByte += bytes;
bfread.nMsgReadCount += bytes;
bfread.nBytesRead += bytes;
}
else
{
result = ((*(unsigned int *)(bfread.pInByte + 4) & ROWBITTABLE[bits]) << (32 - bfread.nCurInputBit)) | (*(unsigned int *)bfread.pInByte >> bfread.nCurInputBit);
bfread.nCurInputBit = bits;
bfread.pInByte += 4;
bfread.nMsgReadCount += 4;
bfread.nBytesRead += 4;
}
if (bfread.nMsgReadCount > bfread.pbuf->cursize)
{
result = 1;
msg_badread = 1;
}
}
return result;
}
NOXREF uint32 MSG_PeekBits(int numbits)
{
NOXREFCHECK;
bf_read_t savebf = bfread;
uint32 r = MSG_ReadBits(numbits);
bfread = savebf;
return r;
}
int MSG_ReadSBits(int numbits)
{
int nSignBit = MSG_ReadOneBit();
int result = MSG_ReadBits(numbits - 1);
if (nSignBit)
{
result = -result;
}
return result;
}
NOXREF char *MSG_ReadBitString(void)
{
NOXREFCHECK;
static char buf[8192];
char *p = &buf[0];
for (char c = MSG_ReadBits(8); c; c = MSG_ReadBits(8))
{
#ifdef REHLDS_FIXES
if (msg_badread) // Prevent infinite cycle if msg_badread
{
break;
}
#endif
*p++ = c;
}
*p = 0;
return buf;
}
int MSG_ReadBitData(void *dest, int length)
{
if (length > 0)
{
int i = length;
unsigned char * p = (unsigned char *)dest;
do
{
*p = (unsigned char)MSG_ReadBits(8);
p++;
--i;
}
while (i);
}
return length;
}
NOXREF float MSG_ReadBitCoord(void)
{
NOXREFCHECK;
float value = 0;
int intval = MSG_ReadOneBit();
int fractval = MSG_ReadOneBit();
if (intval || fractval)
{
int signbit = MSG_ReadOneBit();
if (intval)
{
intval = MSG_ReadBits(12);
}
if (fractval)
{
fractval = MSG_ReadBits(3);
}
value = (float)(fractval / 8.0 + intval);
if (signbit)
{
value = -value;
}
}
return value;
}
void MSG_WriteBitCoord(const float f)
{
int signbit = f <= -0.125;
int intval = abs((int32)f);
int fractval = abs((int32)f * 8) & 7;
MSG_WriteOneBit(intval);
MSG_WriteOneBit(fractval);
if (intval || fractval)
{
MSG_WriteOneBit(signbit);
if (intval)
MSG_WriteBits(intval, 12);
if (fractval)
MSG_WriteBits(fractval, 3);
}
}
NOXREF void MSG_ReadBitVec3Coord(vec3_t fa)
{
NOXREFCHECK;
int xflag = MSG_ReadOneBit();
int yflag = MSG_ReadOneBit();
int zflag = MSG_ReadOneBit();
if (xflag)
fa[0] = MSG_ReadBitCoord();
if (yflag)
fa[1] = MSG_ReadBitCoord();
if (zflag)
fa[2] = MSG_ReadBitCoord();
}
void MSG_WriteBitVec3Coord(const vec3_t fa)
{
bool xflag = fa[0] <= -0.125 || fa[0] >= 0.125;
bool yflag = fa[1] <= -0.125 || fa[1] >= 0.125;
bool zflag = fa[2] <= -0.125 || fa[2] >= 0.125;
MSG_WriteOneBit(xflag);
MSG_WriteOneBit(yflag);
MSG_WriteOneBit(zflag);
if (xflag)
MSG_WriteBitCoord(fa[0]);
if (yflag)
MSG_WriteBitCoord(fa[1]);
if (zflag)
MSG_WriteBitCoord(fa[2]);
}
NOXREF float MSG_ReadCoord(void)
{
NOXREFCHECK;
return (float)(MSG_ReadShort() * (1.0 / 8));
}
void MSG_WriteCoord(sizebuf_t *sb, const float f)
{
MSG_WriteShort(sb, (int)(f * 8.0));
}
NOXREF void MSG_ReadVec3Coord(sizebuf_t *sb, vec3_t fa)
{
NOXREFCHECK;
if (MSG_IsBitReading())
{
MSG_ReadBitVec3Coord(fa);
}
else
{
MSG_StartBitReading(sb);
MSG_ReadBitVec3Coord(fa);
MSG_EndBitReading(sb);
}
}
NOXREF void MSG_WriteVec3Coord(sizebuf_t *sb, const vec3_t fa)
{
NOXREFCHECK;
MSG_StartBitWriting(sb);
MSG_WriteBitVec3Coord(fa);
MSG_EndBitWriting(sb);
}
void MSG_BeginReading(void)
{
msg_readcount = 0;
msg_badread = 0;
}
int MSG_ReadChar(void)
{
int c;
if (msg_readcount < net_message.cursize)
{
c = net_message.data[msg_readcount];
msg_readcount++;
}
else
{
msg_badread = 1;
c = -1;
}
return c;
}
int MSG_ReadByte(void)
{
int c;
if (msg_readcount < net_message.cursize)
{
c = net_message.data[msg_readcount];
msg_readcount++;
}
else
{
msg_badread = 1;
c = -1;
}
return c;
}
int MSG_ReadShort(void)
{
int c;
if (msg_readcount + 2 <= net_message.cursize )
{
c = *(int16 *)&net_message.data[msg_readcount];