#!/usr/bin/perl # # Copyright (C) 2007 by Antonio "s4tan" Parata - s4tan@ictsc.it # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # use strict; use warnings; if ($#ARGV < 0) { usage(); } my $len = length($ARGV[0]); # lunghezza della stringa di ingresso my @chars = split '', $ARGV[0]; # array dei caratteri in ingresso my @output = (); my %encodeMap = &buildAsciiMap; # creo la mappa dei valori codati encodeString(1,'',@chars); # il carattere lo codifico encodeString(0,'',@chars); # il primo carattere non lo codifico for(my $i=0; $i<=$#output;$i++) { print $output[$i]."\n"; } ### Start routine # Effettua la codifica della parola sub encodeString { my ($mustEncode,$currentEncodedString,@input) = @_; if ($#input == 0) { # Condizione di terminazione # sono all'ultimo carattere, non devo piu' ricorrere :) if ($mustEncode) { push @output, $currentEncodedString.encode($input[0]); } else { push @output, $currentEncodedString.$input[0]; } } else { my $char = $input[0]; if ($mustEncode) { $char = encode($input[0]); } encodeString(1,$currentEncodedString.$char,@input[1..$#input]); encodeString(0,$currentEncodedString.$char,@input[1..$#input]); } } # Crea la mappa dei valori codificati della tabella ascii sub buildAsciiMap { my %map; for (my $i=0; $i<=255; $i++) { $map{chr($i)} = "%".sprintf "%x", $i; } return %map; } # Effettua la codifica del carattere sub encode { my ($char) = @_; return $encodeMap{$char}; } sub usage { print "\n usage: $0 \"word to encode\"\n\n"; exit 1; }