Skip to content

Commit f37a4da

Browse files
committed
- fix PVSDecode undefined behaviour on MSVC and visibility issue on GCC, apply unsigned data types
1 parent cda23a5 commit f37a4da

3 files changed

Lines changed: 19 additions & 19 deletions

File tree

src_rebuild/Game/C/draw.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,6 @@ int current_pvs_cell;
371371
void SetupDrawMapPSX(void)
372372
{
373373
int cell_x, cell_z;
374-
int theta;
375374
int pvs_cell;
376375

377376
if (setupYet != 0)

src_rebuild/Game/C/map.c

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ int pvsSize[4] = { 0, 0, 0, 0 };
486486
unsigned char *PVSEncodeTable = NULL;
487487

488488
// [D] [T]
489-
void PVSDecode(char *output, char *celldata, ushort sz, int havanaCorruptCellBodge)
489+
void PVSDecode(char *output, u_char *celldata, ushort sz, int havanaCorruptCellBodge)
490490
{
491491
u_char* nybblearray;
492492
int pixelIndex;
@@ -503,7 +503,7 @@ void PVSDecode(char *output, char *celldata, ushort sz, int havanaCorruptCellBod
503503

504504
// decode byte-swapped array
505505
for (i = 0; i < sz; i++)
506-
((ushort*)nybblearray)[i] = M_SHRT_2((u_char)celldata[i], (u_char)celldata[i] >> 4) & 0xf0f;
506+
((ushort*)nybblearray)[i] = M_SHRT_2(celldata[i], celldata[i] >> 4) & 0xf0f;
507507

508508
pixelIndex = 0;
509509
symIndex = 0;
@@ -512,15 +512,15 @@ void PVSDecode(char *output, char *celldata, ushort sz, int havanaCorruptCellBod
512512
// decompress image
513513
while (i < sz * 2)
514514
{
515-
int ni;
516-
int sym;
515+
u_int ni;
516+
u_int sym;
517517

518518
ni = nybblearray[i++];
519519
if (ni < 12)
520520
{
521521
symIndex = ni * 2;
522522
spod:
523-
sym = M_SHRT_2((u_char)PVSEncodeTable[symIndex], (u_char)PVSEncodeTable[symIndex + 1]);
523+
sym = M_SHRT_2(PVSEncodeTable[symIndex], PVSEncodeTable[symIndex + 1]);
524524
}
525525
else
526526
{
@@ -534,7 +534,8 @@ void PVSDecode(char *output, char *celldata, ushort sz, int havanaCorruptCellBod
534534
goto spod;
535535
}
536536

537-
sym = ((sym & 3) * 16 + nybblearray[i++]) * 16 + nybblearray[i++ + 1];
537+
sym = ((sym & 3) * 16 + nybblearray[i]) * 16 + nybblearray[i + 1];
538+
i += 2;
538539
}
539540

540541
pixelIndex += (sym >> 1);
@@ -549,22 +550,22 @@ void PVSDecode(char *output, char *celldata, ushort sz, int havanaCorruptCellBod
549550
decodebuf[pvs_square_sq-1] ^= 1;
550551

551552
size = pvs_square - 2;
552-
op = (decodebuf - 1) + (size * pvs_square + pvs_square);
553+
op = (decodebuf - 1) + (size+1) * pvs_square;
553554
for (i = size; i >= 0; --i)
554555
{
555556
for (j = pvs_square; j > 0; --j, --op)
556557
{
557-
*op = *op ^ op[pvs_square];
558+
op[0] ^= op[pvs_square];
558559
}
559560
}
560561

561562
size = pvs_square - 1;
562-
op = (decodebuf - 2) + (size * pvs_square + pvs_square);
563+
op = (decodebuf - 2) + (size+1) * pvs_square;
563564
for (i = size; i >= 0; --i, --op)
564565
{
565566
for (j = pvs_square - 2; j >= 0; --j, --op)
566567
{
567-
*op = *op ^ op[1];
568+
*op ^= op[1];
568569
}
569570
}
570571

@@ -595,8 +596,8 @@ void GetPVSRegionCell2(int source_region, int region, int cell, char *output)
595596
{
596597
int k;
597598
u_int havanaCorruptCellBodge;
598-
char *tbp;
599-
char *bp;
599+
u_char *tbp;
600+
u_char *bp;
600601
ushort length;
601602

602603
#ifndef PSX
@@ -612,17 +613,17 @@ void GetPVSRegionCell2(int source_region, int region, int cell, char *output)
612613

613614
if (regions_unpacked[source_region] == region && loading_region[source_region] == -1)
614615
{
615-
bp = PVS_Buffers[source_region];
616+
bp = (u_char*)PVS_Buffers[source_region];
616617
PVSEncodeTable = (u_char *)(bp + 0x802);
617618
tbp = bp + cell * 2;
618619

619-
length = M_SHRT_2((u_char)tbp[2], (u_char)tbp[3]) - M_SHRT_2((u_char)tbp[0], (u_char)tbp[1]) & 0xffff;
620+
length = M_SHRT_2(tbp[2], tbp[3]) - M_SHRT_2(tbp[0], tbp[1]);
620621

621622
if (length != 0)
622623
{
623624
havanaCorruptCellBodge = (regions_unpacked[source_region] == 158 && cell == 168);
624625

625-
PVSDecode(output, bp + M_SHRT_2((u_char)tbp[0], (u_char)tbp[1]), length, havanaCorruptCellBodge);
626+
PVSDecode(output, bp + M_SHRT_2(tbp[0], tbp[1]), length, havanaCorruptCellBodge);
626627
}
627628
else
628629
{

src_rebuild/Game/reversing.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
//----------------------------------------
66

77
// makes short out of two bytes
8-
#define M_SHRT_2(a,b) (((a) << 8) | (b))
8+
#define M_SHRT_2(a,b) (((u_short)(a) << 8) | (u_short)(b))
99

1010
// makes int out of two shorts
11-
#define M_INT_2(a,b) (((a) << 16) | (b))
11+
#define M_INT_2(a,b) (((u_int)(a) << 16) | (u_int)(b))
1212

1313
// makes int out of four bytes
14-
#define M_INT_4(a,b,c,d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d) )
14+
#define M_INT_4(a,b,c,d) (((u_int)(a) << 24) | ((u_int)(b) << 16) | ((u_int)(c) << 8) | (u_int)(d) )
1515
#define M_INT_4R(a,b,c,d) M_INT_4(d, c, b, a)
1616

1717
#define M_BIT(x) (1 << (x))

0 commit comments

Comments
 (0)