Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Perl: Системное программирование > C -> Perl


Автор: Gh0sT 15.6.2006, 02:41
Всем доброго времени суток!

Цитата

How to calculate the key from the lock:
We begin with creating a buffer exactly as big as the lock-string. Now for each character with index i in the lock-string (except from the first one where i = 0) we'll calculate the key at that position from the character i and i-1. Then we calculate the first key character from the first lock character and the two last. When this is done, we nibble-swap the entire key. Now we have a key that is almost ready to be sent to the server. We just have to replace a few characters with a meta-character.
An example in c++ (we have a lock lock and we send using send()):
Код

// get size from lock-string
int len = strlen(lock);
// create buffer for key
char *key = new char[len];
// create the key-data
for (i = 1; i < len; i++)
   key[i] = lock[i] ^ lock[i-1];
key[0] = lock[0] ^ lock[len-1] ^ lock[len-2] ^ 5;
// nibble-swap the key
for (i = 0; i < len; i++)
   key[i] = ((key[i]<<4) & 240) | ((key[i]>>4) & 15);
escapeDCN(key);
string s = "$Key ";
s += key;
send(s);
delete[] key;

The escapeDCN is a function that escapes the characters 0, 5, 36, 96, 124, and 126 to /%DCN000%/, /%DCN005%/, /%DCN036%/, /%DCN096%/, /%DCN124%/, and /%DCN126%/ respectively.


Это цитата из доки по опсианию дц протокола.
Есть проблема: Нужно код на C перевести в рабочий код на Perl'e, но С я вообще не знаю smile
Помогите, пожалуйста!

Всем зараннее спасибо!

Gh0sT 

Автор: Ivan Kolesnikov 15.6.2006, 08:30
Привет, я тоже с C не очень знаком, но разобраться было не сложно, единственное что не знаю, это что выполняет функция send.
Если я правильно понял описание:
Код

use strict;

my $lock = "test string";
my @arr_lock = map {ord} split //, $lock;
my @arr_key;
for (my $i = 1; $i <= $#arr_lock; $i++) {
  $arr_key[$i] = $arr_lock[$i] ^ $arr_lock[$i-1];
}
$arr_key[0] = $arr_lock[0] ^ $arr_lock[-1] ^ $arr_lock[-2] ^ 5;

$_ = (($_ << 4) & 240) | (($_ >> 4) & 15) foreach @arr_key;

escapeDCN(@arr_key);

my $s = '$Key ' . join '', @arr_key;

print $s;

#send($s)

sub escapeDCN {
  $_ = {0 => 1, 5 => 1, 36 => 1, 96 => 1, 124 => 1, 126 => 1}->{$_} ? sprintf "/%%DCN%03d/", $_ : chr $_ foreach @_;
}

 

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)