This repository has been archived by the owner on Jun 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pack.pl
49 lines (44 loc) · 1.46 KB
/
pack.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/perl
use warnings;
use strict;
use Digest::MD5 qw(md5 md5_hex md5_base64);
use MIME::Base64;
use File::Basename;
use FindBin;
use lib $FindBin::Bin;
use transport-layer-tcp-protocol;
my $BLOCK_SIZE = 512;
my $filename = shift @ARGV;
my($encoded_filename, $directories, $suffix) = fileparse($filename);
$encoded_filename = $encoded_filename.$suffix;
print STDERR "Encoding $encoded_filename...\n";
open my $input_handler,"$filename" or die $!;
my $base64_line;
{
$/ = undef;
$base64_line = encode_base64(<$input_handler>);
}
$base64_line =~ s#\n##g;
my $blocks = [];
@{$blocks} = unpack("(A$BLOCK_SIZE)*", $base64_line);
my $block_counter;
init_block_counter(\$block_counter);
my $block_count = scalar @{$blocks};
my $headers = [];
push @{$headers}, gen_header($block_counter);
push @{$headers}, gen_header($block_count);
push @{$headers}, gen_header($encoded_filename);
my $header_count = scalar @{$headers};
print add_overhead();
for my $payload (@{$blocks}) {
my $block_line = sprintf("%010d", $header_count + 1).gen_header($block_counter);
for (my $header_number = 0; $header_number < $header_count; $header_number++) {
$block_line = $block_line.$headers->[$header_number];
}
$block_line = $block_line.$payload;
my $signed_block_line = md5_base64($block_line)."$block_line";
print "$signed_block_line\n";
block_counter_increment(\$block_counter);
}
print add_overhead();
print STDERR "File successfully encoded\nTotal blocks:\t\t".($block_counter)."\n";