I am translating c code into perl to make it more portable, but I have run into a problems I do not not know how to solve. The software receives a string from the server and must respond by returning a string generated from the original (lock and key). The translation involves several bitwise xors and nibble swaps.
Let me quote the c code written by Eric Proveteau because he is better at explaining this.
the key is quite easily :) computed from the lock key[x]= ns(lock[x]^lock[x-1]) ns is a nibble swap (switch the upper 4 bits with the lower 4 bits) exception: ey[0] is a bit different let's name A and B the 2 last bytes of the lock key[0]= ns(lock[0]^A^B^0x05) ; 0x05 is a kind of magic nibble
Here is the code I have written attempting to follow the specification: (@lock_arr is the string to change, each character is at its own index)
$key_arr[0] = pack 'h2', unpack 'H2', ( $lock_arr[0] ^ $lock_arr[@lock_arr - 1] ^ $lock_arr[@lock_arr - 2] ); for (my $i=1; $i<@lock_arr; $i++) { $key_arr[$i] = pack 'h2', unpack 'H2', ($lock_arr[$i] ^ $lock_arr[$i-1]); }
As far as I know, I have two problems here.
1. I haven't been able to figure out the syntax to add 0x05 into the first operation.
2. I have no way to show this works without asking the server.
Are there better ways to do this and is there a way to display the contents of @key_arr in a way that would make sense?

In reply to Low level operations by narse

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.