Perl - 使用指针和索引进行二进制解包

时间:2012-11-18 04:43:06

标签: perl binary block

我有一个二进制文件,包含3个文件,一个PNG,一个PHP和一个TGA文件。

这里的文件可以提供您的想法:container.bin

文件以这种方式构建: 前6个字节是指向索引的指针,在本例中为211794 然后你将所有3个文件一个接一个地堆叠起来 在第211794页,你有索引,告诉你文件的开始和结束位置

在这个例子中你有:

[offset start] [offset end] [random data] [offset start] [name]

6 15149 asdf 6 Capture.PNG
15149 15168 4584 15149 index.php
15168 211794 12 15168 untilted.tga

表示capture.png从偏移量6开始,在偏移量15149处结束,然后asdf是随机数据,并且再次重复起始偏移量。

现在我要做的是在这个二进制文件上分离文件的perl。 perl需要检查文件的第一个6偏移量(标题),然后跳转到索引位置,并使用列表提取文件​​。

2 个答案:

答案 0 :(得分:2)

seekread的混合可用于完成任务:

#!/usr/bin/env perl

use strict;
use warnings;

use Fcntl 'SEEK_SET';

sub get_files_info {
    my ( $fh, $offset ) = @_;
    my %file;

    while (<$fh>) {
        chomp;
        my $split_count = my ( $offset_start, $offset_end, $random_data, $offset_start_copy,
            $file_name ) = split /\s/;
        next if $split_count != 5;

        if ( $offset_start != $offset_start_copy ) {
            warn "Start of offset mismatch: $file_name\n";
            next;
        }

        $file{$file_name} = {
            'offset_start' => $offset_start,
            'offset_end'   => $offset_end,
            'random_data'  => $random_data,
        };
    }

    return %file;
}

sub write_file {
    my ( $fh, $file_name, $file_info ) = @_;

    seek $fh, $file_info->{'offset_start'}, SEEK_SET;
    read $fh, my $contents,
      $file_info->{'offset_end'} - $file_info->{'offset_start'};

    open my $fh_out, '>', $file_name or die 'Error opening file: $!';
    binmode $fh_out;
    print $fh_out $contents;

    print "Wrote file: $file_name\n";
}

open my $fh, '<', 'container.bin' or die "Error opening file: $!";
binmode $fh;
read $fh, my $offset, 6;

seek $fh, $offset, SEEK_SET;

my %file = get_files_info $fh, $offset;

for my $file_name ( keys %file ) {
    write_file $fh, $file_name, $file{$file_name};
}

答案 1 :(得分:2)

这里唯一真正的困难是确保以二进制模式读取输入和输出文件。这可以通过在打开文件时使用:raw PerlIO层来实现。

这个程序似乎做你想要的。它首先将索引块定位并读入字符串,然后打开该字符串以进行输入,并读取每个组成文件的开始和结束位置以及名称。此后处理每个文件很简单。

请注意,除非索引块的格式比您说的更严格,否则您只能依赖每行上的第一个,第二个和最后一个以空格分隔的字段,因为随机文本可以包含空格。也无法指定包含空格的文件名。

使用Data::Dump的输出用于演示正确的功能,对于程序的运行不是必需的。

use v5.10;
use warnings;

use Fcntl ':seek';

use autodie qw/ open read seek close /;

open my $fh, '<:raw', 'container.bin';

read $fh, my $index_loc, 6;

seek $fh, $index_loc, SEEK_SET;
read $fh, my ($index), 1024;

my %contents;
open my $idx, '<', \$index;
while (<$idx>) {
  my @fields = split;
  next unless @fields;
  $contents{$fields[-1]} = [ $fields[0], $fields[1] ];
}

use Data::Dump;
dd \%contents;

for my $file (keys %contents) {

  my ($start, $end) = @{ $contents{$file} };
  my $size = $end - $start;
  seek $fh, $start, SEEK_SET;

  my $nbytes = read $fh, my ($data), $size;
  die "Premature EOF" unless $nbytes == $size;

  open my $out, '>:raw', $file;
  print { $out } $data;
  close $out;
}

<强>输出

{
  "Capture.PNG"  => [6, 15149],
  "index.php"    => [15149, 15168],
  "untilted.tga" => [15168, 211794],
}
相关问题