25
25
#define TYPZX7 4 // Original ZX7 compression - 1 bit for each unpacked byte and 1 bit + elias-gama length for sekvence
26
26
#define TYPBLK 5 // Bitstream BLK first simple - elias-gama length for both sekvence and unpacked block + 1 bit for sekvence / unpacked
27
27
#define TYPBS1 6 // Bitstream LZX standart 1 - 0=unpack byte, 10=2 byte sek, 110=3 byte sek, 1110 + EG length = sekvence, 1111 + EG length = unpacked block
28
+ #define TYPZX0 7 // Original ZX0 compression - 1 bit for each unpacked byte and 1 bit + elias-gama length for sekvence
28
29
29
30
// Systems for coding offsets:
30
31
31
32
#define POSLZM 1 // Standart for LZM compression - one byte (8 bit) offset
32
33
#define POSLZE 2 // Standart for LZE compression - one or two byte (7 or 15 bit) offset
33
- #define POSLZ_ 3 // Standart for LZE compression - one or two byte (7 or 15 bit) negative offset
34
+ #define POSLZ_ 3 // Standart for LZ_ compression - like LZE with binary incompatibility for flags and negative offset
34
35
#define POSOF4 4 // Four step offsets (1,2,3,4 or 2,4,6,8 or 3,6,9,12 or 4,8,12,16 bits)
35
36
#define POSOF1 5 // One fixed offset used for all sekvences
36
37
#define POSOF2 6 // Two fixed offsets (similar than ZX7)
37
38
#define POSOFD 7 // Simple elias-gama coding offset
39
+ #define POSZX0 8 // Elias(MSB(offset)+1) LSB(offset) Elias(length-1)
40
+
38
41
39
42
// /////////////////////////////////////////////////////////////////////////////
40
43
48
51
49
52
#define MAXLEN 80000
50
53
51
- int bits;
54
+ int bits; // every file start with 0x80;
55
+ int first_zx0_literal; // 0 = false
56
+ int last_zx0_literal; // 0 = repeat offset, 1 = new offset, 2 = unpack
57
+ int last_zx0_offset;
52
58
int data_len;
53
59
int over_len;
54
60
int overhead;
@@ -81,10 +87,11 @@ int GetBit(void)
81
87
over_len++;
82
88
}
83
89
overhead++;
84
- if (DBG > 1 ) printf (" %c " , (bits >> 8 ) & 0x01 | ' 0' );
90
+ if (DBG > 1 ) printf (" %c " , (( bits >> 8 ) & 0x01 ) + ' 0' );
85
91
return (bits >> 8 ) & 0x01 ;
86
92
}
87
93
94
+
88
95
int GetByte (void )
89
96
{
90
97
if (DBG > 1 ) printf (" %02X " , packed_data[packed_index]);
@@ -100,6 +107,7 @@ int StandaloneFirstByte()
100
107
case TYPLZE:
101
108
case TYPLZ_:
102
109
case TYPBLK: return 0 ;
110
+ case TYPZX0:
103
111
case TYPZX7:
104
112
case TYPBS1: return 1 ;
105
113
default : return -1 ;
@@ -129,6 +137,7 @@ int GetValue(int bitwide)
129
137
return value;
130
138
}
131
139
140
+ // bitwide je inicializovana hodnota s pocatecnim poctem nulovych bitu
132
141
int GetEliasGama (int bitwide)
133
142
{
134
143
if (DBG > 1 ) printf (" (EG:%u) " , bitwide);
@@ -141,6 +150,31 @@ int GetEliasGama(int bitwide)
141
150
return value;
142
151
}
143
152
153
+
154
+ // origin je pocatecni nastaveni hodnoty
155
+ // 0x0001 pro delku
156
+ // 0x??FE pro ofset
157
+ int GetInterlacedEliasGama (int origin)
158
+ {
159
+ if (DBG > 1 ) printf (" (IEG:%u) " , origin);
160
+ while (!GetBit ())
161
+ {
162
+ origin <<= 1 ;
163
+ origin += GetBit ();
164
+ }
165
+ return origin;
166
+ }
167
+
168
+
169
+ // origin je pocatecni nastaveni hodnoty
170
+ // 0x0001 pro delku
171
+ // 0x??FE pro ofset
172
+ int GetInterlacedEliasGama_no_first_check (int origin)
173
+ {
174
+ return GetInterlacedEliasGama (2 *origin + GetBit ());
175
+ }
176
+
177
+
144
178
int GetBlockParams (int *resoff, int *reslen)
145
179
{
146
180
int packed = 0 ;
@@ -174,6 +208,36 @@ int GetBlockParams(int *resoff, int *reslen)
174
208
if (DBG > 1 ) printf (" - " );
175
209
packed = !GetBit (); if (packed) length++;
176
210
break ;
211
+ case TYPZX0:
212
+ if (first_zx0_literal)
213
+ {
214
+ first_zx0_literal = 0 ;
215
+ last_zx0_literal = 2 ; // unpacked
216
+ }
217
+ else
218
+ {
219
+ if (last_zx0_literal < 2 )
220
+ {
221
+ last_zx0_literal = GetBit ();
222
+ if (!last_zx0_literal)
223
+ last_zx0_literal = 2 ;
224
+ }
225
+ else
226
+ {
227
+ last_zx0_literal = GetBit ();
228
+ }
229
+ }
230
+ if ( last_zx0_literal == 2 )
231
+ length = GetInterlacedEliasGama (1 );
232
+ else
233
+ {
234
+ length = 0 ;
235
+ packed = 1 ;
236
+ }
237
+
238
+ if (DBG>2 )
239
+ printf (" \n new literal: %i\n " ,last_zx0_literal);
240
+ break ;
177
241
case TYPZX7:
178
242
length = 1 ;
179
243
packed = !GetBit ();
@@ -268,6 +332,43 @@ int GetBlockParams(int *resoff, int *reslen)
268
332
offset = GetValue (offset1bits * (width + 1 ));
269
333
for (int adds = width; adds >= 0 ; adds--) offset += 1 << (adds * offset1bits);
270
334
break ;
335
+ case POSZX0:
336
+ // packed
337
+ if ( last_zx0_literal == 0 ) // same offset
338
+ {
339
+ offset = last_zx0_offset;
340
+ *reslen = GetInterlacedEliasGama (1 );
341
+ offset = last_zx0_offset;
342
+ }
343
+ else
344
+ {
345
+ offset = GetInterlacedEliasGama (0x00FE );
346
+
347
+ offset++;
348
+ offset &= 0xFF ;
349
+
350
+ if (!offset) // End mark //
351
+ {
352
+ *reslen = 0 ;
353
+ *resoff = 0 ;
354
+ break ;
355
+ }
356
+
357
+ offset <<= 8 ;
358
+ offset += GetByte ();
359
+
360
+ if (offset & 1 )
361
+ *reslen = 2 ;
362
+ else
363
+ *reslen = GetInterlacedEliasGama_no_first_check (1 ) + 1 ;
364
+
365
+ offset >>= 1 ;
366
+ offset ^= 0x7FFF ;
367
+ offset++;
368
+
369
+ last_zx0_offset = offset;
370
+ }
371
+ break ;
271
372
default :
272
373
printf (" \n Error in %s: Unknown offset coding %u\n " , packed_name, compress_posi);
273
374
return 1 ;
@@ -286,6 +387,7 @@ void DisplayCompression()
286
387
case TYPLZM: printf (" LZM" ); break ;
287
388
case TYPLZE: printf (" LZE" ); break ;
288
389
case TYPLZ_: printf (" LZ_" ); break ;
390
+ case TYPZX0: printf (" ZX0" ); break ;
289
391
case TYPZX7: printf (" ZX7" ); break ;
290
392
case TYPBLK: printf (" BLK" ); break ;
291
393
case TYPBS1: printf (" BS1" ); break ;
@@ -303,9 +405,10 @@ void DisplayCompression()
303
405
case POSOF1: z = printf (" fixed offset %2u" , offset1bits); break ;
304
406
case POSOF2: z = printf (" two offsets %2u %u" , offset1bits, offset2bits); break ;
305
407
case POSOFD: z = printf (" elias-gama offset" ); break ;
408
+ case POSZX0: z = printf (" elias(hi(offset)+1) lo(offset)" ); break ;
306
409
default : z = printf (" (unknown)" );
307
410
}
308
- printf (" %s " , " ...................." + z);
411
+ printf (" %s " , " ........... ...................." + z);
309
412
}
310
413
311
414
int main (int number_of_params, char **parameters)
@@ -411,6 +514,12 @@ int main(int number_of_params, char **parameters)
411
514
compress_posi = POSLZ_;
412
515
if (DBG) puts (" Compress type: LZ_" );
413
516
}
517
+ else if (name_length > 4 && !stricmp (packed_name + name_length - 4 , " .zx0" ))
518
+ {
519
+ compress_type = TYPZX0;
520
+ compress_posi = POSZX0;
521
+ if (DBG) puts (" Compress type: ZX0" );
522
+ }
414
523
else
415
524
{
416
525
printf (" Cannot determine compress type for file %s\n " , packed_name); continue ;
@@ -494,16 +603,21 @@ int main(int number_of_params, char **parameters)
494
603
}
495
604
496
605
bits = 0x80 ;
606
+ first_zx0_literal=1 ;
607
+ last_zx0_offset=1 ;
497
608
overhead = 0 ;
498
609
packed_index = 0 ;
499
610
unpack_index = prolog_size;
500
611
501
612
int packed_count = 0 ;
502
613
int unpack_count = 0 ;
503
614
504
- printf (" packed_index:%+6d\n " ,packed_index);
505
- printf (" unpack_index:%+6d\n " ,unpack_index);
506
-
615
+ if (DBG>1 )
616
+ {
617
+ printf (" packed_index:%+6d\n " ,packed_index);
618
+ printf (" unpack_index:%+6d\n " ,unpack_index);
619
+ }
620
+
507
621
if (stand1stbyte)
508
622
{
509
623
if (!make_depack)
@@ -603,13 +717,13 @@ int main(int number_of_params, char **parameters)
603
717
if (err) continue ;
604
718
if (DBG || !make_depack) putchar (' \n ' );
605
719
606
- printf (" %-32s %5u => %-5u NumSek: %-4u Packed: %-5u NoPack: %-5u Overhead: %-5u (%u bits)\n " ,
720
+ printf (" %-32s \n\t %5u => %-5u NumSek: %-4u Packed: %-5u NoPack: %-5u Overhead: %-5u (%u bits)\n " ,
607
721
packed_name, packed_index, unpack_index, numsek, packed_count, unpack_count, (overhead + 7 ) >> 3 , overhead);
608
722
609
723
if (make_depack)
610
724
{
611
725
FILE *unpack_file = fopen (unpack_name, " wb" ); if (!unpack_file) { perror (unpack_name); continue ; }
612
- if (unpack_index != fwrite (unpack_data+prolog_size, 1 , unpack_index-prolog_size, unpack_file)) printf (" Can't write %s\n " , unpack_name);
726
+ if (( unsigned int ) unpack_index-prolog_size != fwrite (unpack_data+prolog_size, 1 , unpack_index-prolog_size, unpack_file)) printf (" Can't write %s\n " , unpack_name);
613
727
fclose (unpack_file);
614
728
}
615
729
0 commit comments