This code is on the wiki page
%decompress LZSS bytes knowing resulting bytes should be length N
function dbytes = decompressLZSS(bytes, N)
%flip the bit order within the compressed section
bytes = [bytes(1:3); bin2dec(fliplr(dec2bin(bytes(4:end))))];
%save bits to temp file
ftemp = fopen('temp.lzs', 'w+');
fwrite(ftemp, bytes, 'ubit8');
frewind(ftemp)
fout = fopen('out.lzs', 'w+');
% bits = fread(ftemp, inf, 'ubit1');
% fprintf('%d', (bits(1:end).'))
% fprintf('\n')
% frewind(ftemp);
%read compression header bytes
width1 = fread(ftemp, 1, 'uint8');
width2 = fread(ftemp, 1, 'uint8');
width3 = fread(ftemp, 1, 'uint8');
%step through compressed bits and decompress
%stop once N bits have been written to output
while ftell(fout) < N
b = fread(ftemp, 1, 'ubit1');
if ~b
%read width2 bits, add 2 -> dist
dist = uintbe(fread(ftemp, width2, 'ubit1'))+2;
%read width1 bits, add 2 -> count
count = uintbe(fread(ftemp, width1, 'ubit1'))+2;
%rewind dist bytes in output
fseek(fout, -dist, 'eof');
%copy count bytes from output to end
copy = fread(fout, count, 'ubit8');
fseek(fout, 0, 'eof');
fwrite(fout, copy, 'ubit8');
% fprintf('Copy from small -%d, %d bytes\n', dist, count);
else
b = fread(ftemp, 1, 'ubit1');
if ~b
%read width3 bits, add 2+1<<width2 -> dist
dist = uintbe(fread(ftemp, width3, 'ubit1'))+bitshift(1, width2)+2;
%read width1 bits, add 2 -> count
count = uintbe(fread(ftemp, width1, 'ubit1'))+2;
%rewind dist bytes in output
fseek(fout, -dist, 'eof');
%copy count bytes from output to end
copy = fread(fout, count, 'ubit8');
fseek(fout, 0, 'eof');
fwrite(fout, copy, 'ubit8');
% fprintf('Copy from large -%d, %d bytes\n', dist, count);
else
%read a byte and copy to end of output
copy = uintbe(fread(ftemp, 8, 'ubit1'));
fseek(fout, 0, 'eof');
fwrite(fout, copy, 'ubit8');
% fprintf('Read byte %d: %d\n', ftell(fout)-1, copy);
end
end
end
%rewind output and read it to get all bytes
frewind(fout);
dbytes = fread(fout, inf, 'uint8');
%close files and delete temps
fclose(ftemp);
fclose(fout);
delete('temp.lzs');
delete('out.lzs');
end
%convert BE bits to unsigned int
function i = uintbe(bits)
i = (2.^(length(bits)-1:-1:0))*bits(:);
end
It appears to flip the bits prior to decompression using this code
%flip the bit order within the compressed section
bytes = [bytes(1:3); bin2dec(fliplr(dec2bin(bytes(4:end))))];
Now if someone understands this can you tell me if it is reversing the bytes within the block
or is it reversing the bits within each byte
or is it totally flipping the bits withing the block
or is it not touching the first three bytes and flipping the rest using one of the above methods
thanks in advance for any help