Advertisement
Guest User

Untitled

a guest
Apr 28th, 2014
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.70 KB | None | 0 0
  1. #!/usr/bin/perl
  2. use feature "unicode_strings";
  3. use Encode qw/:all/;
  4. use Encode::Guess qw/utf8 iso-8859-1 cp1252/;
  5. use MIME::Base64;
  6.  
  7. my $ldif;
  8. $/ = ""; # switch to praragraph mode
  9.  
  10. while ($ldif = <STDIN>)
  11. {
  12.     $ldif =~ s/\n //g; # concatenate continued lines
  13.     $ldif =~ s/^(.+?):\s*?\n//mg; # ignore empty attributes
  14.     $ldif =~ s/^(.+?)::\s(.+?)$/&fix_charset($1, $2)/mge; # fix charset
  15. }
  16.  
  17. sub fix_charset
  18. {
  19.    my ($attribute, $value) = @_;
  20.    my $decoder = Encode::Guess->guess($value);
  21.    die $decoder unless ref($decoder);
  22.    my $string = $attribute.":: ".encode_base64(encode_utf8($decoder->decode(decode_base64($value))));
  23.    $string =~ s/\n//g; # whipe linefeeds
  24.    return $string;
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement