http://www.perlmonks.org?node_id=11123177

Kroning has asked for the wisdom of the Perl Monks concerning the following question:

Hi!

I have some packet from app that looks like "ec bd ... 00 00 ff ff" which I am trying to decode.

From what I have found, it looks like "00 00 ff ff" is a Z_SYNC_FLUSH of deflate (zlib).

I tried to reproduce it on Compress::Raw::Zlib, Compress::Zlib and some others. But the ending pattern was "00 ff 00 ff". Am I missing something?

#!/usr/local/bin/perl use Compress::Zlib ; my $x = deflateInit() or die "Cannot create a deflation stream\n" ; ($output, $status) = $x->deflate('text') ; ($output, $status) = $x->flush(Z_SYNC_FLUSH) ; $output =~ s/(.)/sprintf '%04x', ord $1/seg; print "output=$output\n";
output=002a004900ad0028000100000000000000ff00ff00000000000000ff00ff

Replies are listed 'Best First'.
Re: Perl Zlib last block
by choroba (Archbishop) on Oct 26, 2020 at 11:39 UTC
    To get ffff, don't ask for 4 nibbles, but 2:
    $output =~ s/(.)/sprintf '%02x', ord $1/seg; # ^

    You can also use

    $output = unpack 'H*', $output;

    instead of the s/// with /e. It should be faster.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]