Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
Don't ask to ask, just ask
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
package CGIpack; use strict; =head1 NAME CGIpack - Transforms parameters into a packed list of URL-compatible c +haracters and vice versa. =head1 SYNOPSIS use CGIpack; # Transform a list of numbers and back. #Results in: @data=(15,13,2**23,0,2**31); @bitsizes = (4,14,24,1,55 ); $encoded=encode({ bitsizes=>[@bitsizes], data=>[@data]}); $decoded=join ', ', decode( {str=>$encoded, bitsizes=>[@bitsizes]} ); ## only converted 32 bits instead of 55 #Data: 15, 13, 8388608, 0, 2147483648 #Encoded: V30000W000004000 #Decoded array: 15, 13, 8388608, 0, 2147483648 =head1 DESCRIPTION CGIpack is a module I wrote in response to a question. Question went +like: "I want my users to save a URL containing a CGI with parameters. To a +void problems with email-readers, the list should be as short as possible, +and to avoid users messing around with the values I would like to see them + encoded in a certain way." I came up with this solution. The encoder takes a list of data, toget +her with a list with the desired number of bits to be saved. The bits are + divided into chunks of 6 bits, that are encoded with the alphanumeric characters *and* % and - (64 characters in total). The resulting strin +g is short, contains as little bits as possible and/or desired by the us +er. =over 4 =item encode( $hashref ) Takes a hashref, which should contain a member 'data', referring to a +list, and a member 'bitsizes', referring to a list containing the number of desired bits for every value in the list. =item decode( $hashref ) Takes a hashref, which should contain a member 'str', containing a str +ing of characters produced by encode (preferably, you may wanna roll your +own ;-) and a member 'bitsizes', referring to a list containing the number + of desired bits for every value encoded in the string. =back =head1 CAVEAT If you want to encode characters, you will have to convert them manual +ly using unpack/pack 'c'. I just may add a wrapper for that in the futur +e, though. It does work on 32 bit integers, but not yet on 64 bit. I'm a little p +uzzled why not, it may have something to do with the fact I'm working on a 32 +-bit pentium right now. Will try at home with my PPC. =head1 AUTHOR Jeroen Elassaiss-Schaap =head1 LICENSE Perl/ artisitic license =head1 STATUS Alpha =cut use Exporter; use vars qw( @EXPORT @ISA @VERSION); @VERSION = 0.021; @ISA = qw( Exporter ); @EXPORT = qw( &encode &decode); sub encode{ my $hash = shift; my @data = @{$hash->{'data'}}; my @bitsizes = @{$hash->{'bitsizes'}}; my ($str, $bitstr); for my $bits (@bitsizes) { $bitstr .= unpack("b$bits", pack('VV', shift( @data ) )); } $bitstr .= '0' x ( length($bitstr) % 6 ); for my $item (0..( length($bitstr) / 6 - 1 )){ my $val=pack('b6', substr($bitstr, $item*6, 6) ); for ($val) { tr [\100\077\000-\010\011-\043\044-\075] [\045\055\060-\071\101-\132\141-\172]; $str.=$_; } } $str; } sub decode{ my $hash = shift; my $str = $hash->{'str'}; my @bitsizes= @{$hash->{'bitsizes'}}; my ($bitstr, $val); my @data; for (split //, $str){ tr [\045\055\060-\071\101-\132\141-\172] [\100\077\000-\010\011-\043\044-\075]; $bitstr .= unpack("b6",$_); } my $pointer = 0; for my $bits (@bitsizes) { my $val; for ($bits) { $val = unpack('c',pack("b$_",substr( $bitstr, $pointer, $_ + ))), last if $bits < 9; $val = unpack('v',pack("b$_",substr( $bitstr, $pointer, $_ + ))), last if $bits < 17; $val = unpack('V',pack("b32",substr( $bitstr, $pointer, $_ + ))), last if $bits < 33; if (! eval('{$val = unpack("Q",pack("b$_",substr( '. '$bitstr, $pointer, $_ )));1;}') ) { warn "only converted 32 bits instead of $_\n"; $val = unpack('V',pack("b$_", substr( $bitstr, $pointer, $_ ))); } } push( @data, $val); $pointer += $bits; } @data; } 1;

In reply to CGIpack by jeroenes

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (11)
    As of 2014-04-18 17:52 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (471 votes), past polls