66#include "libc/string.h"
77
88#define PACKED __attribute__((packed))
9- #define __GET_UNALIGNED_T (type , ptr ) \
10- ({ \
11- const struct { \
12- type x; \
13- } PACKED *__pptr = (typeof(__pptr)) (ptr); \
14- __pptr->x; \
15- })
16-
17- #define __PUT_UNALIGNED_T (type , val , ptr ) \
18- do { \
19- struct { \
20- type x; \
21- } PACKED *__pptr = (typeof(__pptr)) (ptr); \
22- __pptr->x = (val); \
23- } while (0)
9+ #define __GET_UNALIGNED_T (type , ptr ) \
10+ ({ \
11+ const struct \
12+ { \
13+ type x; \
14+ } PACKED *__pptr = (typeof(__pptr))(ptr); \
15+ __pptr->x; \
16+ })
17+
18+ #define __PUT_UNALIGNED_T (type , val , ptr ) \
19+ do \
20+ { \
21+ struct \
22+ { \
23+ type x; \
24+ } PACKED *__pptr = (typeof(__pptr))(ptr); \
25+ __pptr->x = (val); \
26+ } while (0)
2427
2528#define GET_UNALIGNED4 (ptr ) __GET_UNALIGNED_T(uint32_t, (ptr))
2629#define PUT_UNALIGNED4 (val , ptr ) __PUT_UNALIGNED_T(uint32_t, (val), (ptr))
@@ -33,15 +36,15 @@ typedef struct
3336 uint32_t match4 ;
3437} MinMatch ;
3538
36- static MinMatch readMinMatch (const uint8_t * buf )
39+ static MinMatch readMinMatch (const uint8_t * buf )
3740{
3841 const uint32_t mask = 0xffffff00 ;
3942 const uint32_t match4 = GET_UNALIGNED4 (buf );
4043 const uint32_t match3 = match4 & mask ;
41- return (MinMatch ) { match3 , match4 };
44+ return (MinMatch ){ match3 , match4 };
4245}
4346
44- static uint32_t readMinMatch3 (const uint8_t * buf )
47+ static uint32_t readMinMatch3 (const uint8_t * buf )
4548{
4649 const uint32_t mask = 0xffffff00 ;
4750 const uint32_t match4 = GET_UNALIGNED4 (buf );
@@ -59,22 +62,28 @@ typedef struct
5962
6063static MinMatcHash hashMinMatch (MinMatch match )
6164{
62- return (MinMatcHash ) { ((match .match3 * 506832829U ) << 8 ) >> (32 - HASH_CHAINS_BITS ), (match .match4 * 2654435761U ) >> (32 - HASH_CHAINS_BITS )};
65+ return (MinMatcHash ){ ((match .match3 * 506832829U ) << 8 ) >> (32 - HASH_CHAINS_BITS ), (match .match4 * 2654435761U ) >> (32 - HASH_CHAINS_BITS )};
6366}
6467
65- static void wildCopy4 (uint8_t * dst , const uint8_t * src , const uint8_t * dstEnd )
68+ static void wildCopy4 (uint8_t * dst , const uint8_t * src , const uint8_t * dstEnd )
6669{
6770 do
6871 {
69- PUT_UNALIGNED4 (GET_UNALIGNED4 (src ), dst );
72+ PUT_UNALIGNED4 (GET_UNALIGNED4 (src ), dst );
7073 src += 4 ;
7174 dst += 4 ;
7275 } while (dst < dstEnd );
7376}
7477
75- #define WILD_COPY_MOVE (dst , src , amount ) do{ uint8_t* start = dst; dst += (amount); wildCopy4(start, src, dst); }while(0)
78+ #define WILD_COPY_MOVE (dst , src , amount ) \
79+ do \
80+ { \
81+ uint8_t *start = dst; \
82+ dst += (amount); \
83+ wildCopy4(start, src, dst); \
84+ } while (0)
7685
77- static void encodeLargeInt (uint8_t * * _out , int leftToEncode )
86+ static void encodeLargeInt (uint8_t * * _out , int leftToEncode )
7887{
7988#define out (*_out)
8089 bool keepEncoding = true;
@@ -90,12 +99,12 @@ static void encodeLargeInt(uint8_t** _out, int leftToEncode)
9099typedef struct
91100{
92101 int matchLen ;
93- const uint8_t * inMatch ;
102+ const uint8_t * inMatch ;
94103} MinMatchPtr ;
95104
96- static MinMatchPtr tryMinMatch (uint32_t potentialMinMatchOffset , MinMatch minMatch , const uint8_t * inCursor , const uint8_t * inLimit , int inCursorOffset )
105+ static MinMatchPtr tryMinMatch (uint32_t potentialMinMatchOffset , MinMatch minMatch , const uint8_t * inCursor , const uint8_t * inLimit , int inCursorOffset )
97106{
98- const uint8_t * potentialMinMatch = inCursor - inCursorOffset + potentialMinMatchOffset ;
107+ const uint8_t * potentialMinMatch = inCursor - inCursorOffset + potentialMinMatchOffset ;
99108
100109 // need to do some fun wraparound stuff
101110 if (potentialMinMatch > inCursor - 4 )
@@ -115,30 +124,30 @@ static MinMatchPtr tryMinMatch(uint32_t potentialMinMatchOffset, MinMatch minMat
115124 matchLen ++ ;
116125 }
117126
118- return (MinMatchPtr ) { matchLen , potentialMinMatch };
127+ return (MinMatchPtr ){ matchLen , potentialMinMatch };
119128 }
120129 else
121130 {
122- return (MinMatchPtr ) { 0 , NULL };
131+ return (MinMatchPtr ){ 0 , NULL };
123132 }
124133}
125134
126- void mlz4_compress (const uint8_t * in , const uint32_t inSize , uint8_t * out , uint32_t * outSize )
135+ void mlz4_compress (const uint8_t * in , const uint32_t inSize , uint8_t * out , uint32_t * outSize )
127136{
128- uint8_t * compressedData = out ;
137+ uint8_t * compressedData = out ;
129138
130- uint8_t * outCursor = compressedData ;
139+ uint8_t * outCursor = compressedData ;
131140#ifndef TARGET_N64
132141 uint8_t hashChains3 [HASH_CHAINS_SIZE ] = {};
133142 uint8_t hashChains4 [HASH_CHAINS_SIZE ] = {};
134143#else
135- // placed in gDecompressionHeap
136- uint8_t * hashChains3 = (uint8_t * ) 0x801c1000 ;
137- uint8_t * hashChains4 = (uint8_t * ) 0x801c1000 + HASH_CHAINS_SIZE ;
144+ // placed in gDecompressionHeap
145+ uint8_t * hashChains3 = (uint8_t * ) 0x801c1000 ;
146+ uint8_t * hashChains4 = (uint8_t * ) 0x801c1000 + HASH_CHAINS_SIZE ;
138147#endif
139148
140- // should be safe to do
141- * (uint32_t * )outCursor = inSize ;
149+ // should be safe to do
150+ * (uint32_t * )outCursor = inSize ;
142151 outCursor += 4 ;
143152
144153 // uncompressible data, just write it out as literals
@@ -147,14 +156,14 @@ void mlz4_compress(const uint8_t* in, const uint32_t inSize, uint8_t* out, uint3
147156 outCursor [0 ] = ((uint8_t )inSize ) << 4 ;
148157 outCursor ++ ;
149158 WILD_COPY_MOVE (outCursor , in , inSize );
150- * outSize = (size_t ) (outCursor - compressedData );
151- return ;
159+ * outSize = (size_t )(outCursor - compressedData );
160+ return ;
152161 }
153162
154- const uint8_t * inEnd = in + inSize ;
155- const uint8_t * inCursor = in + 4 ;
156- const uint8_t * inLimit = inEnd - 4 ;
157- const uint8_t * inLiteralStart = in ;
163+ const uint8_t * inEnd = in + inSize ;
164+ const uint8_t * inCursor = in + 4 ;
165+ const uint8_t * inLimit = inEnd - 4 ;
166+ const uint8_t * inLiteralStart = in ;
158167
159168 // start the loop
160169 while (1 )
@@ -179,17 +188,17 @@ void mlz4_compress(const uint8_t* in, const uint32_t inSize, uint8_t* out, uint3
179188 }
180189 else
181190 {
182- * (outCursor ++ ) = ((uint8_t ) literalsCount ) << 4 ;
191+ * (outCursor ++ ) = ((uint8_t )literalsCount ) << 4 ;
183192 }
184193
185194 WILD_COPY_MOVE (outCursor , inLiteralStart , literalsCount );
186- * outSize = (uint32_t )(outCursor - compressedData );
187- return ;
195+ * outSize = (uint32_t )(outCursor - compressedData );
196+ return ;
188197 }
189198
190199 MinMatch minMatch = readMinMatch (inCursor );
191200 MinMatcHash hashValue = hashMinMatch (minMatch );
192- uint8_t inCursorOffset = (uint8_t ) (inCursor - in );
201+ uint8_t inCursorOffset = (uint8_t )(inCursor - in );
193202
194203 uint8_t potentialMinMatchOffset3 = hashChains3 [hashValue .hash3 ];
195204 uint8_t potentialMinMatchOffset4 = hashChains4 [hashValue .hash4 ];
@@ -201,8 +210,8 @@ void mlz4_compress(const uint8_t* in, const uint32_t inSize, uint8_t* out, uint3
201210 if (match .matchLen )
202211 {
203212 // write the match...
204- uint8_t * pnibble = outCursor ++ ;
205- const int literalsCount = (int ) (inCursor - inLiteralStart );
213+ uint8_t * pnibble = outCursor ++ ;
214+ const int literalsCount = (int )(inCursor - inLiteralStart );
206215 if (literalsCount >= 15 )
207216 {
208217 * pnibble = 0xf0 ;
@@ -238,15 +247,15 @@ void mlz4_compress(const uint8_t* in, const uint32_t inSize, uint8_t* out, uint3
238247 }
239248}
240249
241- void mlz4_decompress (const uint8_t * in , uint8_t * out )
250+ void mlz4_decompress (const uint8_t * in , uint8_t * out )
242251{
243- const uint8_t * inCursor = in ;
244- uint32_t originalSize = * (uint32_t * )inCursor ;
252+ const uint8_t * inCursor = in ;
253+ uint32_t originalSize = * (uint32_t * )inCursor ;
245254 inCursor += 4 ;
246255
247- uint8_t * outCursor = out ;
248- uint8_t * outEnd = out + originalSize ;
249- uint8_t * outSafetyMargin = outEnd - 4 ;
256+ uint8_t * outCursor = out ;
257+ uint8_t * outEnd = out + originalSize ;
258+ uint8_t * outSafetyMargin = outEnd - 4 ;
250259 while (1 )
251260 {
252261 uint8_t nibble = * (inCursor ++ );
@@ -264,7 +273,7 @@ void mlz4_decompress(const uint8_t* in, uint8_t* out)
264273
265274 if (literalsCount )
266275 {
267- uint8_t * literalsEnd = outCursor + literalsCount ;
276+ uint8_t * literalsEnd = outCursor + literalsCount ;
268277 if (literalsEnd >= outSafetyMargin )
269278 {
270279 // last 4 literals must be in safety margin
@@ -294,7 +303,7 @@ void mlz4_decompress(const uint8_t* in, uint8_t* out)
294303 }
295304 matchesCount += MINMATCH ;
296305
297- const uint8_t * matchStart = outCursor - offset ;
306+ const uint8_t * matchStart = outCursor - offset ;
298307 WILD_COPY_MOVE (outCursor , matchStart , matchesCount );
299308 }
300309}
0 commit comments