Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

C code translation to Perl

by bottch (Initiate)
on Jan 30, 2012 at 14:01 UTC ( #950776=perlquestion: print w/ replies, xml ) Need Help??
bottch has asked for the wisdom of the Perl Monks concerning the following question:

Hi guys. I need to translate some C code into Perl. I want to decrypt NS1crypted stuff on solaris. There is a tool written in C by the opensolaris project.

(http://stuff.iain.cx/2008/05/03/ns103eb2365be169abbe3a45088a10a/)

I need a Perlscript what does the same thing so i started to translate this code. But I kinda stuck in a function where I keep getting different return values. Here's the C code i want to translate:

http://src.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/lib/libsldap/common/ns_crypt.c

I stuck in the c_setup function.

Perlcode: http://pastebin.com/ucg6V0Ce

I guess it's some stuff with signed-overflow integers in the C-code but I don't really have an idea. I'm trying this for days now.. please help me. regards,bottch

Comment on C code translation to Perl
Re: C code translation to Perl
by jethro (Monsignor) on Jan 30, 2012 at 16:28 UTC

    If you can execute both c and perl code, why don't you just put print statements at similar positions in both programs (to print out variables naturally) and just watch where a difference turns up first with the same input values. Through bisecting you should be able to pinpoint the exact line where the problem occurs

Re: C code translation to Perl
by InfiniteSilence (Curate) on Jan 31, 2012 at 03:27 UTC

    I'm with jethro, if you feel the need to use Pastebin and what-not to get help you appear to be having problems isolating the place where your code is having trouble. In other words, you are more likely to get help if you are more specific with the description of your error.

    Before doing anything however, why can't the C code compile on Solaris (I don't have a Solaris VM or machine handy to answer the question for myself)? My thoughts are that if you could get it to compile then you could write an XS module from Perl to just use the functions you need. From a performance standpoint I cannot imagine how this would not be your best choice. Heck, even if the C code breaks it may be better to fix it and then follow the XS route rather than do this in Pure Perl.

    Celebrate Intellectual Diversity

      I printed every single variable already. First of all the crypt($pw,"Ho") statement returns different results. After I noticed that, I just set $pw to the value the c tool returns. Now $seed differs from the c code. It's wierd because the first 4-5 elements in the array are the same like in the c tool.

      Hexadecimal elements from @seed in Perl:
      seed: 2298 seed: effe9 seed: 329fb28 seed: f9f4815b seed: 7430a82151 seed: 277c8923528c seed: 11957511bac25e seed: 6a9a15bb7c3a5a5 seed: 53cb25459bf9ffe7 seed: 59b3b95dcd77f57d seed: a56ffe811a5b3cae seed: 4ffb834f11b60ab seed: 52dea09c13b9ef89

      Hexadecimal elements from seed in C:

      c: seed: 2298 c: seed: effe9 c: seed: 329fb28 c: seed: f9f4815b c: seed: 30a82151 c: seed: 8923528c c: seed: 11bac25e c: seed: b7c3a5a5 c: seed: 9bf9ffe7 c: seed: cd77f57d c: seed: 1a5b3cae c: seed: f11b60ab c: seed: 13b9ef89

      I have no clue why the values differs from the 5th element on. Thats why I'm asking here. Sorry for my incomprehensible question befor.

      regards bottch

        I have no clue why the values differs from the 5th element on.

        You are using a 64-bit perl, (or possibly a 32-bit Perl with 64-bit IVs enabled), which means that your attempts to reproduce the results from the C code which uses 32-bit integers isn't working.

        The type of bit-wise math in the C code relies heavily on the implicit mod 2**32 of intermediate results to work.

        For a way to 'fix' it, see Re: emulate 32-bit on perl 64.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        The start of some sanity?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://950776]
Approved by Ratazong
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (8)
As of 2014-12-21 03:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (102 votes), past polls