Traducir codigo PERL a FWH/xHarbour

Traducir codigo PERL a FWH/xHarbour

Postby JmGarcia » Mon Aug 15, 2011 10:17 am

Desearía que me ayudarais a traducir estos códigos fuente de lenguaje PERL a FWH/xHarbour.

El primero es este y las llamadas que se le hacen desde el código fuente PERL son del tipo my $rx_clr = rx_decode($rx_msg, $key1, $key2); siendo key1 y key2 los bytes de un CRC16 (eso creo).
Code: Select all  Expand view
sub rx_decode
{
    my $rx_msg     = shift;
    my $key1    = shift;
    my $key2    = shift;

    die "Undefined Message Bytes"     unless defined $rx_msg;

    # Descramble payload bytes.  This is perfomed in a similar way to the
    # encoder by calling the CRC routine initially with magic values
    # then doing some bit shifts and logic.
 
    my $enc1 = 0;            # Defaults to 0

    my $rx_clr;

    # Take each message byte in turn and descramble:
    foreach my $byte (split //,$rx_msg)
    {
        $byte = ord $byte;

        # Starting with the magic init values, use the returned
        # CRC16 as the init values for the next round.

        $key1 = crc16($key1, 0x1021, 0xFF);
        $key2 = crc16($key2, 0x8005, 0xFF);

        # Now some bit shifting weirdness:
        my $enc2 = ($enc1 >> 3) | ($enc1 << 5);

        # And some XORing for good measure:
        my $enc3 = ($byte ^ $enc2) & 0xFF;
        my $enc4 = ($enc3 ^ $key1) & 0xFF;
        my $enc5 = ($enc4 ^ $key2) & 0xFF;
        $enc1    = ($enc2 ^ $enc5) & 0xFF;
        $rx_clr .= chr $enc5;
    }

    return $rx_clr;
}


Dentro de este código hay una llamada a la función crc16 que es esta:
Code: Select all  Expand view
sub crc16
{
    my $init = shift;
    my $poly = shift;
    my $val  = shift;

    die "Undefined CRC value"
        unless defined $val && defined $poly && defined $init;

    my $var;
    for (my $i=0; $i<8; $i++)
    {
        $var = $val << 8;
        $var ^= $init;
        $init += $init;
        $val <<= 1;

        if ($var & 0x8000)
        {
            $init ^= $poly;
        }
    }

    return $init & 0xFFFF;
}


Muchas gracias por vuestra ayuda.
Mi abuelo decía: Los aviones vuelan porque Dios quiere, y los helicópteros ni Dios sabe porque vuelan.
FWH 16.02, xHarbour 1.2.3, Harbour 3.2.0, WorkShop 4.5, AJ Make 0.30, Borlan BCC 7.00, VisualStudio 2013
User avatar
JmGarcia
 
Posts: 654
Joined: Mon May 29, 2006 3:14 pm
Location: Madrid - ESPAÑA

Return to FiveWin para Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 21 guests