Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old June 24th, 2008, 01:44 PM
Newbie
 
Join Date: Jun 2008
Posts: 1
Default Convert Perl Code in PHP

Hi Every one,

I have a perl script which uses the following code to decode the username saved in a cookie. I want to parse that cookie in php for adding some more features in the application. The source application in perl but I don't know any thing about perl. Here is the code ...

Expand|Select|Wrap|Line Numbers
  1. @table = (('A' .. 'Z'), ('a' .. 'z'), ('0' .. '9'), '+', '/', '|');
  2. for ($_ = 0; $_ <= $#table; $_++) {
  3. $decode_table[ord($table[$_])] = $_;
  4. }
  5. local $_ = $_[0];
  6. s/./unpack('B6', chr($decode_table[ord($&)] << 2))/eg;
  7. pack('B' . (int(length($_) / 8) * 8), $_);
  8.  
Can any one help me to convert this code in PHP?

Thanks in advance
Reply
  #2  
Old June 30th, 2008, 12:54 PM
numberwhun's Avatar
Forum Leader
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,156
Default

Sure, if this were the PHP forum . Unfortunately, it is not and I know that I do not know any PHP. What I can do though, is move this to the PHP forum where hopefully they can help you.

Please know that you might have to specify what your requirements are for the script to do and also write code (these are learning forums).

On the other hand, if you are looking for someone to write this for you, then you can certainly post this to the Jobs forum and offer to pay someone. The choice is up to you.

Regards,

Jeff
Reply
  #3  
Old July 2nd, 2008, 02:19 AM
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Age: 24
Posts: 5,310
Default

Heya, aliusman.

I'm no Perl guru, but I dabble from time to time in the dark arts. Let's see if we can run through this and generate a suitable translation.

And if I happen to get anything wrong, perhaps numberwhun would be wiling to step in and assist... *AHEM*.

Anyway.

Expand|Select|Wrap|Line Numbers
  1. @table = (('A' .. 'Z'), ('a' .. 'z'), ('0' .. '9'), '+', '/', '|');
This line creates a big 'ol table with all the letters and numbers, and a few symbols to boot.

Expand|Select|Wrap|Line Numbers
  1. for ($_ = 0; $_ <= $#table; $_++) {
  2. $decode_table[ord($table[$_])] = $_;
  3. }
Now we're looping through the table array that we created and generating a new hash called #decode_table (in Perl, hashes start with the '#' character, but you reference individual elements in the hash using '$'). The keys of this table are the ordinal values of @table and the values start with 0 and increase by 1 each time.

Expand|Select|Wrap|Line Numbers
  1. local $_ = $_[0];
  2.  
This creates a local variable $_ and sets it equal to the first index in the special $_ variable (rough equivalent in PHP is probably $_GET or $_COOKIE).

Expand|Select|Wrap|Line Numbers
  1. s/./unpack('B6', chr($decode_table[ord($&)] << 2))/eg;
  2. pack('B' . (int(length($_) / 8) * 8), $_);
Got no idea here. Perhaps numberwhun could be of some assistance as to the purpose of these lines. *AHEM*
Reply
  #4  
Old July 2nd, 2008, 11:25 AM
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Age: 18
Posts: 2,857
Default



Sounds like you need some cough medicine, Josh.
Reply
  #5  
Old July 3rd, 2008, 03:04 AM
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Age: 24
Posts: 5,310
Default

Ooh, cherry-flavored.

Alrightey. After further consultation with our Perl forum leader, we (well, he) determined that the code you provided is not adequate to determine what's going on.

He checked it out, and here are the errors it generates:

Expand|Select|Wrap|Line Numbers
  1. Useless use of pack in void context at C:/coding/Eclipse_Workspace/Perl_Testing_Area/test4.pl line 15.
  2. Use of uninitialized value $_ in substitution (s///) at C:/coding/Eclipse_Workspace/Perl_Testing_Area/test4.pl line 13.
  3. Use of uninitialized value $_ in length at C:/coding/Eclipse_Workspace/Perl_Testing_Area/test4.pl line 15.
  4. Use of uninitialized value $_ in pack at C:/coding/Eclipse_Workspace/Perl_Testing_Area/test4.pl line 15.
If you can, please post the rest of the code. If that's all you have, we'll need some more info on where the code came from.
Reply
  #6  
Old July 3rd, 2008, 07:59 AM
Administrator
 
Join Date: Sep 2006
Posts: 11,312
Default

Quote:
Originally Posted by pbmods
...
Expand|Select|Wrap|Line Numbers
  1. s/./unpack('B6', chr($decode_table[ord($&)] << 2))/eg;
  2. pack('B' . (int(length($_) / 8) * 8), $_);
Got no idea here. Perhaps numberwhun could be of some assistance as to the purpose of these lines. *AHEM*
That's probably intended to be the encoding/decoding part.
The pack converts a string into some machine level representation and the unpack does the opposite. The OP might just want to explain what their algorithm is for the decode and then we could suggest the php equivalence of that.
Reply
  #7  
Old July 4th, 2008, 03:19 AM
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Age: 24
Posts: 5,310
Default

Unfortunately, PHP's pack() function takes a slightly different syntax than Perl's (http://php.net/pack), so I'm afraid that this is beyond my ken.

I will go ahead and move this thread back to the Perl forum so that the OP might be able to get a better idea of what's going on. Once we have that, then we can tackle how to translate it.
Reply
  #8  
Old July 4th, 2008, 09:05 PM
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Posts: 3,662
Default

it would be nice to see what the value of $_ is from this line of the code:

local $_ = $_[0];

without knowing what the input is trying to figure out the correct output is going to be useless. But I have a feeling this thread has been abandoned anyway.
Reply
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles