Newer
Older
s4 / mpsplit.pl
@HIROSE Yuuji HIROSE Yuuji on 6 Jun 2020 947 bytes Filename regularization
#!/usr/bin/env perl
$sep = "--" . $ARGV[0];
$dir = ($ARGV[1] || "tmp");
if ($dir =~ /^([^<>\;\&]*)$/) {
  $dir = $1;
}

#print "sep=".$sep, "dir=$dir\n";
#binmode STDIN;
$/ = undef;
$stream = (<STDIN>);
$n=0;
@slices = split($sep, $stream);
shift(@slices);
pop(@slices);
foreach $item (@slices) {
  $item =~ s/^\n//;
  ($header = $item) =~ s/\r\n\r\n.*//s;
  ($body = $item) =~ s/.*?\r\n\r\n//s;
  $body =~ s/\r\n$//;
  #print "h=[$header]\n";
  #print "b=[$body]\n";
  unless ($header =~ /\bname=([\"']?)(.*?)\1/) {
    next;
  }
  $name = $2;
  #print "name=$name\n";
  if ($header =~ /filename=(['\"]?)(.*?)\1/ && $2 gt "") {
    $fn=$2;
    if ($fn =~ /^([^\/]*)$/) {
      $fn = $1;
    }
    $fn =~ s/[ 	<>&\#'\"\&|*?]/_/g;
    $fn =~ s,.*[/\\],,;
    open(OUT, ">$dir/$fn");
      print OUT $body;
    close(OUT);
    printf("%s:filename=%s\n", $name, unpack("H*", $fn));
  } else {
    printf("%s=%s\n", $name, unpack("H*", $body));
  }
}