<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package Archive::Cpio::OldBinary;

use Archive::Cpio::Common;

my $TRAILER    = 'TRAILER!!!';
my $BLOCK_SIZE = 512;

my @HEADER = qw(
  magic
  dev
  ino
  mode
  uid
  gid
  nlink
  rdev
  mtime_high
  mtime_low
  namesize
  datasize_high
  datasize_low
);

sub new {
    my ($class, $magic) = @_;
    bless { magic =&gt; unpack('v', $magic) }, $class;
}

sub read_one {
    my ($o, $FHwp) = @_;
    my $entry = read_one_header($o, $FHwp);

    $entry-&gt;{name} = $FHwp-&gt;read($entry-&gt;{namesize});
    $entry-&gt;{name} =~ s/\0$//;

    $entry-&gt;{name} ne $TRAILER or return;
    $FHwp-&gt;read(padding(2, $entry-&gt;{namesize}));

    $entry-&gt;{data} = $FHwp-&gt;read($entry-&gt;{datasize});
    $FHwp-&gt;read(padding(2, $entry-&gt;{datasize}));

    cleanup_entry($entry);

    $entry;
}

sub read_one_header {
    my ($o, $FHwp) = @_;

    my %h;
    my @vals = unpack('v*', $FHwp-&gt;read(2 * @HEADER));
    foreach my $field (@HEADER) {
        $h{$field} = shift @vals;
    }
    foreach ('mtime', 'datasize') {
        $h{$_} = $h{$_ . '_high'} * 0x10000 + $h{$_ . '_low'};
    }

    $h{magic} == $o-&gt;{magic} or die "bad magic ($h{magic} vs $o-&gt;{MAGIC})\n";

    \%h;
}

sub write_one {
    my ($o, $F, $entry) = @_;

    $entry-&gt;{magic} = $o-&gt;{magic};
    $entry-&gt;{namesize} = length($entry-&gt;{name}) + 1;
    $entry-&gt;{datasize} = length($entry-&gt;{data});

    foreach ('mtime', 'datasize') {
        $entry-&gt;{$_ . '_high'} = int($entry-&gt;{$_} / 0x10000);
        $entry-&gt;{$_ . '_low'} = $entry-&gt;{$_} % 0x10000;
    }

    write_or_die($F, pack_header($entry) .
                     $entry-&gt;{name} . "\0" .
                     "\0" x padding(2, $entry-&gt;{namesize}));
    write_or_die($F, $entry-&gt;{data});
    write_or_die($F, "\0" x padding(2, $entry-&gt;{datasize}));

    cleanup_entry($entry);
}

sub write_trailer {
    my ($o, $F) = @_;

    write_one($o, $F, { name =&gt; $TRAILER, data =&gt; '', nlink =&gt; 1 });
    write_or_die($F, "\0" x padding($BLOCK_SIZE, tell($F)));
}

sub cleanup_entry {
    my ($entry) = @_;

    foreach ('datasize', 'namesize', 'magic') {
        delete $entry-&gt;{$_};
    }
    foreach (keys %$entry) {
        /_low$|_high$/ and delete $entry-&gt;{$_};
    }
}

sub pack_header {
    my ($h) = @_;
    pack('v*', map { $h-&gt;{$_} || 0 } @HEADER);
}

1;
</pre></body></html>