Link to home
Start Free TrialLog in
Avatar of learning_t0_pr0gram
learning_t0_pr0gram

asked on

Can someone convert this to Visual Basic for me?

Can somebody convert this to Visual Basic 6.0?

http://aluigi.altervista.org/papers/ventrilo_algo.h

I tried to do it myself but there are minor things that I don't know how to do in C++ that I can't figure out anywhere.  It doesn't look like it would be very hard.

Thanks in advance.
Avatar of mish33
mish33
Flag of United States of America image

link is broken
Avatar of learning_t0_pr0gram
learning_t0_pr0gram

ASKER

It works fine for me?  Here's the coding though.  I just thought it would be easier to read with a direct link.

#include <string.h>



typedef struct {
    unsigned char   key[256];
    unsigned int    pos;
    unsigned int    size;
} ventrilo_key_ctx;



int ventrilo_read_keys(ventrilo_key_ctx *client, ventrilo_key_ctx *server, unsigned char *data, int size) {
    ventrilo_key_ctx    *tmp;
    unsigned char       *p,
                        *del = NULL;

    for(p = data; size && *p; size--, p++) {
        if(del) continue;

        if(*p == ',') {         // old versions
            del = p;
        } else if(*p == '|') {  // version 2.3
            tmp    = server;    // exchange
            server = client;
            client = tmp;
            del = p;
        }
    }

    if(!del) return(-1);

    client->size = p - (del + 1);
    server->size = del - data;

    if((client->size > 256) || (server->size > 256)) {
         return(-1);
    }

    client->pos  = 0;
    memcpy(client->key, del + 1, client->size);
    server->pos  = 0;
    memcpy(server->key, data,    server->size);

    return(0);
}



void ventrilo_first_dec(unsigned char *data, int size) {
    const static unsigned char  first[] = "\xAA\x55\x22\xCC\x69\x7C\x38\x91\x88\xF5\xE1";
    int     i;

    for(i = 0; i < size; i++) {
        *data -= first[i % 11] + (i % 27);
        data++;
    }
}



void ventrilo_first_enc(unsigned char *data, int size) {
    const static unsigned char  first[] = "\xAA\x55\x22\xCC\x69\x7C\x38\x91\x88\xF5\xE1";
    int     i;

    for(i = 0; i < size; i++) {
        *data += first[i % 11] + (i % 27);
        data++;
    }
}



void ventrilo_dec(ventrilo_key_ctx *ctx, unsigned char *data, int size) {
    int     i;

    for(i = 0; i < size; i++) {
        *data -= ctx->key[ctx->pos] + (i % 45);
        data++;
        ctx->pos++;
        if(ctx->pos == ctx->size) ctx->pos = 0;
    }    
}



void ventrilo_enc(ventrilo_key_ctx *ctx, unsigned char *data, int size) {
    int     i;

    for(i = 0; i < size; i++) {
        *data += ctx->key[ctx->pos] + (i % 45);
        data++;
        ctx->pos++;
        if(ctx->pos == ctx->size) ctx->pos = 0;
    }
}
>> I tried to do it myself but there are minor things that I don't know how to do in C++
What are those things?
Umm... well I only know the basics of c++, but I kind of understand everything in there.  What I don't get are things like:

ventrilo_key_ctx *ctx, unsigned char *data

What does the * do to those variables?  What's the difference if you take it off?
--------------
first[i % 11] + (i % 27);

What does the % do?
--------------
for(p = data; size && *p; size--, p++)

size && *p, what does the && do in a position like that?  I know what && means in an if statement, but what about in the for statement like that? Also, the * there confuses me too, but if that's just like the variable declaration then...

That's about all I don't understand, by the looks of it.
I should also add that I don't get what some of the data types do compared to visual basic.  For instance, take that first function, ventrilo_read_keys.  del = p;  What is del storing?  A character?  Then why is it doing this further down: client->size = p - (del + 1);  If it's a character?  This type of stuff confuses me.  I just thought that someone who knew both VB and C++ could convert it in no time.
>> ventrilo_key_ctx *ctx, unsigned char *data
This is a pointer type.

>> What does the % do?
% is a mod operator. Similar to mathematical mod for integer division.
Ex:  5/2 = 2, 5%2 = 1
ASKER CERTIFIED SOLUTION
Avatar of rajeev_devin
rajeev_devin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> ventrilo_key_ctx *ctx, unsigned char *data
As I said before pointer variables can pointer to nowhere. In that case your program crashes if you use that variable.

Ex:
int* p; // p is a pointer variable
*p = 10; // p is pointing to nothing. PROGRAM CRASH.
So if *p is pointing to variable x, if x changes, p changes also?  Is that what the pointer variable is?  Does this mean that the for loop:

for(p = data; size && *p; size--, p++) {

will keep on looping until size OR data (p is pointing to data, right?) is empty?  Since size moves down once every iteration, it would loop a max amount of 'size'.  That's what I make of it.

But what happens when you point a pointer to a pointer?  It's pointing del at p, both are pointers.  Does this mean that when data changes, p changes, and then del changes?  If so why not just point del to data?

Am I right when I say the % returns the remainder?  6 % 2 would return 0?  If so then I understand that.

I'm still a little confused about how the different variable times are working.  I don't have much experience in c++, so please forgive me if i'm being stupid here.

In the for loop in that first function, when it does 'p = data', is it posible for an 'unsigned char' to hold more than one character?  Because I am pretty sure you're supposed to pass more than one character into it, otherwise I don't see the point of the for loop.  Plus p++ is in the third parameter of the for loop.  If p is a pointer, when you raise it by one, does it raise the variable it's pointing to?

I understand everything I asked in my previous posts now except the data part which I just asked again.  I think i'm most confused on that for loop because I don't get what is going on.  If you understand what is going on, if you explained it I would probably understand everything I asked so far.  It looks like everything else I could convert to VB easily.
>> keep on looping until size OR data (p is pointing to data, right?) is empty?
You are right
>> But what happens when you point a pointer to a pointer?
Let me modify the language little bit.
"del is pointing to the value where p is pointing"
The thing that you are talking about is something different.

Ex:
int *p;
int **del; // See the difference in syntax
int x = 10;
p = &x;
del = &p; // Now del is pointing to a pointer.
>> Does this mean that when data changes, p changes, and then del changes?  If so why not just point del to data?
Yes, the data will change, but not the complete data.

Exmaple:
char p[] = "ABCDE";
char* ptr = p;
ptr = ptr + 2; // Now ptr is pointing to 'C'.
*p = 'Z'; // Will only change that value. String will  be "ABZDE".
>> Am I right when I say the % returns the remainder?  6 % 2 would return 0?  If so then I understand that.
Yes you are right.
>> unsigned char *data;
When you are passing this that means you are passing a char string.
In C or C++ string comprises of n number of charaters terminated by NULL (which is 0).

Example:
unsigned char *data = "ABCDE",
data[0] = 'A';
data[1] = 'B'
data[2] = 'C'
data[3] = 'D'
data[4] = 'E'
data[5] = '\0' // Or 0, Or NULL, all are same.

length of string = 5, Last NULL will not be counted.

So, now using pointer you can iterate all the charaters in the string.

unsigned char *ptr = data;
unsigned char c; // It is a single charater
for (; *ptr ;) // *ptr will be 0 when it encounterd the terminating charater, as I said it is NULL (which is 0)
{
   c = *ptr;
}

Now don't get confused will NULL and 0. As I said NULL, 0, and '\0' are same.
Ok, I get everything you said, but i'm still confused about a bit more.  I converted the first function 100% and it works (I think).  But I moved onto the next and i'm a little confused about it:

void ventrilo_first_dec(unsigned char *data, int size) {
    const static unsigned char  first[] = "\xAA\x55\x22\xCC\x69\x7C\x38\x91\x88\xF5\xE1";
    int     i;

    for(i = 0; i < size; i++) {
        *data -= first[i % 11] + (i % 27);
        data++;
    }
}

I don't get what is happening.  I wrote up a little test to see what was going on so I could convert it, but all that seemed to happen was it removed the first character off of data each iteration.  Here's the test script I made

    int i;
    const static unsigned char  first[] = "\xAA\x55\x22\xCC\x69\x7C\x38\x91\x88\xF5\xE1";
    unsigned char *data = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ";

    for(i = 0; i < 52; i++) {
        cout << "BEFORE: " << data << endl;
        *data -= first[i % 11] + (i % 27);
        data++;
        cout << "AFTER: " << data << endl;
    }

It only removes the first character off of data each time and I was wondering what the point of it was.  Or if my test script is somehow wrong.  I knew I would run into these problems while converting which is why I was hoping for someone else to do it.  You need to understand every bit that is happening in order to convert it, and each time I get confused I can't go on.  But if you explain each problem I have I will eventually get it done.
Little bit of modification it required in this case

int i;
const static unsigned char  first[] = "\xAA\x55\x22\xCC\x69\x7C\x38\x91\x88\xF5\xE1";
char data[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ";
char *pData = data;

cout << "BEFORE: " << data << endl;
for(i = 0; i < strlen(data); i++) {
      *data -= first[i % 11] + (i % 27);
      pData++;
}
cout << "AFTER: " << data << endl;
All that does is add 1 character to the end of data.  The other one did the same thing except it was removing it right after.  So could you look at that ventrilo_first_dec and tell me exactly what you think it's supposed to do?
I meant to the beginning of data, not the end.
I made this function little bit simpler for you

void ventrilo_first_dec(unsigned char data[], int size) {
    const static unsigned char  first[] = "\xAA\x55\x22\xCC\x69\x7C\x38\x91\x88\xF5\xE1";
    int     i;
    for(i = 0; i < size; i++) {
        data[i] -= first[i % 11] + (i % 27);
    }
}
And that does the same thing as the previous one?  And is it just subtracting ASCII values?  It looks to me like it's shifting decimal places DOWN in the amount of (first[i % 11] + (i % 27)).  If this does the same thing, I think I can do it now.  I am just wondering why this new one you wrote returns different characters than the one in my 2nd comment.
All the best