-
Notifications
You must be signed in to change notification settings - Fork 0
/
contains
executable file
·74 lines (62 loc) · 2.55 KB
/
contains
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env perl
use strict;
use warnings;
use File::Basename;
# Check if we have the correct number of arguments
if (@ARGV != 2) {
die "Usage: $0 <file1> <file2>\n";
}
my ($file1, $file2) = @ARGV;
# Determine which file is larger and which is smaller
my $file1_size = -s $file1;
my $file2_size = -s $file2;
my ($larger_file, $smaller_file);
if ($file1_size > $file2_size) {
$larger_file = $file1;
$smaller_file = $file2;
} elsif ($file2_size > $file1_size) {
$larger_file = $file2;
$smaller_file = $file1;
} else {
die "Error: Both files are the same size. One file must be smaller than the other.\n";
}
sub read_file {
my ($filename) = @_;
open my $fh, '<:raw', $filename or die "Could not open $filename: $!";
local $/;
my $content = <$fh>;
close $fh;
return $content;
}
# Read the content of both files
my $larger_content = read_file($larger_file);
my $smaller_content = read_file($smaller_file);
# Get the lengths of the contents
my $larger_length = length($larger_content);
my $smaller_length = length($smaller_content);
# Search for the smaller content within the larger content
my $offset = index($larger_content, $smaller_content);
if ($offset != -1) {
print "Validation successful!\n";
print "The content of '$smaller_file' (size: $smaller_length bytes) was found at offset $offset of '$larger_file' (size: $larger_length bytes).\n";
# Extract filename components for verification
my ($smaller_basename, $dir, $ext) = fileparse($smaller_file, qr/\.[^.]*$/);
my ($num_bytes, $actual_length, $reported_offset) = $smaller_basename =~ m!\.(\d+)\.(\d+)\.(\d+)$!;
if ($reported_offset) {
if ($num_bytes == $smaller_length && $actual_length == $smaller_length && $reported_offset == $offset) {
print "The filename components match the actual values:\n";
print "Number of bytes: $num_bytes\n";
print "Actual length: $actual_length\n";
print "Offset: $offset\n";
} else {
print "Warning: Some filename components do not match the actual values:\n";
print "Number of bytes (filename vs actual): $num_bytes vs $smaller_length\n";
print "Length (filename vs actual): $actual_length vs $smaller_length\n";
print "Offset (filename vs actual): $reported_offset vs $offset\n";
}
} else {
print "Warning: Couldn't parse the smaller filename for additional validation.\n";
}
} else {
print "Validation failed: The content of the smaller file was not found in the larger file.\n";
}