Skip to content

Commit fb4b13e

Browse files
authored
Add files via upload
1 parent 34469ec commit fb4b13e

File tree

1 file changed

+123
-9
lines changed

1 file changed

+123
-9
lines changed

Compression/lzx_source/Lz_List.cpp

Lines changed: 123 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,19 @@
2525
#define TYPZX7 4 // Original ZX7 compression - 1 bit for each unpacked byte and 1 bit + elias-gama length for sekvence
2626
#define TYPBLK 5 // Bitstream BLK first simple - elias-gama length for both sekvence and unpacked block + 1 bit for sekvence / unpacked
2727
#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
2829

2930
// Systems for coding offsets:
3031

3132
#define POSLZM 1 // Standart for LZM compression - one byte (8 bit) offset
3233
#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
3435
#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)
3536
#define POSOF1 5 // One fixed offset used for all sekvences
3637
#define POSOF2 6 // Two fixed offsets (similar than ZX7)
3738
#define POSOFD 7 // Simple elias-gama coding offset
39+
#define POSZX0 8 // Elias(MSB(offset)+1) LSB(offset) Elias(length-1)
40+
3841

3942
///////////////////////////////////////////////////////////////////////////////
4043

@@ -48,7 +51,10 @@
4851

4952
#define MAXLEN 80000
5053

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;
5258
int data_len;
5359
int over_len;
5460
int overhead;
@@ -81,10 +87,11 @@ int GetBit(void)
8187
over_len++;
8288
}
8389
overhead++;
84-
if (DBG > 1) printf(" %c ", (bits >> 8) & 0x01 | '0');
90+
if (DBG > 1) printf(" %c ", ((bits >> 8) & 0x01) + '0');
8591
return (bits >> 8) & 0x01;
8692
}
8793

94+
8895
int GetByte(void)
8996
{
9097
if (DBG > 1) printf(" %02X ", packed_data[packed_index]);
@@ -100,6 +107,7 @@ int StandaloneFirstByte()
100107
case TYPLZE:
101108
case TYPLZ_:
102109
case TYPBLK: return 0;
110+
case TYPZX0:
103111
case TYPZX7:
104112
case TYPBS1: return 1;
105113
default: return -1;
@@ -129,6 +137,7 @@ int GetValue(int bitwide)
129137
return value;
130138
}
131139

140+
// bitwide je inicializovana hodnota s pocatecnim poctem nulovych bitu
132141
int GetEliasGama(int bitwide)
133142
{
134143
if (DBG > 1) printf(" (EG:%u) ", bitwide);
@@ -141,6 +150,31 @@ int GetEliasGama(int bitwide)
141150
return value;
142151
}
143152

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+
144178
int GetBlockParams(int *resoff, int *reslen)
145179
{
146180
int packed = 0;
@@ -174,6 +208,36 @@ int GetBlockParams(int *resoff, int *reslen)
174208
if (DBG > 1) printf(" - ");
175209
packed = !GetBit(); if (packed) length++;
176210
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("\nnew literal: %i\n",last_zx0_literal);
240+
break;
177241
case TYPZX7:
178242
length = 1;
179243
packed = !GetBit();
@@ -268,6 +332,43 @@ int GetBlockParams(int *resoff, int *reslen)
268332
offset = GetValue(offset1bits * (width + 1));
269333
for (int adds = width; adds >= 0; adds--) offset += 1 << (adds * offset1bits);
270334
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;
271372
default:
272373
printf("\nError in %s: Unknown offset coding %u\n", packed_name, compress_posi);
273374
return 1;
@@ -286,6 +387,7 @@ void DisplayCompression()
286387
case TYPLZM: printf("LZM"); break;
287388
case TYPLZE: printf("LZE"); break;
288389
case TYPLZ_: printf("LZ_"); break;
390+
case TYPZX0: printf("ZX0"); break;
289391
case TYPZX7: printf("ZX7"); break;
290392
case TYPBLK: printf("BLK"); break;
291393
case TYPBS1: printf("BS1"); break;
@@ -303,9 +405,10 @@ void DisplayCompression()
303405
case POSOF1: z = printf("fixed offset %2u", offset1bits); break;
304406
case POSOF2: z = printf("two offsets %2u %u", offset1bits, offset2bits); break;
305407
case POSOFD: z = printf("elias-gama offset"); break;
408+
case POSZX0: z = printf("elias(hi(offset)+1) lo(offset)"); break;
306409
default: z = printf("(unknown)");
307410
}
308-
printf(" %s ", "...................." + z);
411+
printf(" %s ", "..............................." + z);
309412
}
310413

311414
int main(int number_of_params, char **parameters)
@@ -411,6 +514,12 @@ int main(int number_of_params, char **parameters)
411514
compress_posi = POSLZ_;
412515
if (DBG) puts("Compress type: LZ_");
413516
}
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+
}
414523
else
415524
{
416525
printf("Cannot determine compress type for file %s\n", packed_name); continue;
@@ -494,16 +603,21 @@ int main(int number_of_params, char **parameters)
494603
}
495604

496605
bits = 0x80;
606+
first_zx0_literal=1;
607+
last_zx0_offset=1;
497608
overhead = 0;
498609
packed_index = 0;
499610
unpack_index = prolog_size;
500611

501612
int packed_count = 0;
502613
int unpack_count = 0;
503614

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+
507621
if (stand1stbyte)
508622
{
509623
if (!make_depack)
@@ -603,13 +717,13 @@ int main(int number_of_params, char **parameters)
603717
if (err) continue;
604718
if (DBG || !make_depack) putchar('\n');
605719

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",
607721
packed_name, packed_index, unpack_index, numsek, packed_count, unpack_count, (overhead + 7) >> 3, overhead);
608722

609723
if (make_depack)
610724
{
611725
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);
613727
fclose(unpack_file);
614728
}
615729

0 commit comments

Comments
 (0)