#!/usr/bin/perl -w # ##### # Provides fuctions for bencoding/decoding ### # Decodes a bencoded string to a hash/array reference sub bDecode { my $dictref = $_[0]; my $retref = undef; if (substr(${$dictref},0,1) eq "d") { # print Dumper($dictref); $retref = {}; ${$dictref} = substr(${$dictref},1); PROCDICTHASH: while (substr(${$dictref},0,1) ne 'e') { my ($key, $value); ${$dictref} =~ /^(-?[\d]+)/; $key = substr(${$dictref}, length($1)+1, $1); ${$dictref} = substr(${$dictref}, length($1)+$1+1); if (${$dictref} =~ /^[ldi\d]/) { $value = bDecode(\${$dictref}); if (!defined($value)) { return undef; } } else { return undef; } $retref->{$key} = $value; } ${$dictref} = substr(${$dictref},1); } elsif (substr(${$dictref},0,1) eq "l") { # print Dumper($dictref); $retref = []; ${$dictref} = substr(${$dictref},1); PROCDICTARR: while (substr(${$dictref},0,1) ne 'e') { if (${$dictref} =~ /^[ldi\d]/) { my $value = bDecode(\${$dictref}); if (!defined($value)) { return undef; } push (@$retref, $value); } else { return undef; } } ${$dictref} = substr(${$dictref},1); } elsif (${$dictref} =~ /^i(-?[\d]+)e/) { ${$dictref} = substr(${$dictref}, length($1)+2); return $1; } elsif (${$dictref} =~ /^([\d]+):/) { $retref = substr(${$dictref}, length($1)+1, $1); ${$dictref} = substr(${$dictref}, length($1)+$1+1); return $retref; } else { return undef; } return $retref; } #Encodes a hash/array ref to a bencoded string sub bEncode { my $dictref = $_[0]; my $retval = ''; if (ref($dictref) eq 'HASH') { $retval = 'd'; for my $key (sort keys %$dictref) { $retval .= length($key).':'.$key; $retval .= bEncode($dictref->{$key}); } $retval .= 'e'; } elsif (ref($dictref) eq 'ARRAY') { $retval = 'l'; for (my $i = 0; $i <= $#{$dictref}; $i++) { $retval .= bEncode($dictref->[$i]); } $retval .= 'e'; } elsif ($dictref =~ /^-?[\d]+$/) { $retval = 'i'.$dictref.'e'; } else { $retval = length($dictref).':'.$dictref; } return $retval; };

In reply to serialise/unserialising data structures by monoxide

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.