Results 1 to 9 of 9

Thread: xor encode buffer in c

  1. #1

    xor encode buffer in c

    First of all i know this is not the forum to ask c questions.
    So if you know better places where i can ask this question do let me know.

    The idea is that i get an pointer to data what i want to xor encode with an password.
    I propably should cast the pointer or something like that to get things to work.
    But for the moment i am out of ideas.

    building:

    Code:
    void XorEncode(void *password, void *data) 
    { 
       int dlength = length(data); 
       int ipl = length(password); 
       int ip = 0; 
       int i =0; 
       for &#40;i=0; i < dlength; i++&#41; &#123; 
                  if &#40;ip = ipl&#41; ip = 0; 
                data&#91;i&#93; = data&#91;i&#93; ^ password&#91;ip&#93; ;  //this is line 29 
                ip +=1; 
       &#125; 
    
    &#125;

    gives the following error:

    Code:
    Borland C++ 5.5.1 for Win32 Copyright &#40;c&#41; 1993, 2000 Borland 
    crypt.c&#58; 
    Error E2453 crypt.c 29&#58; Size of the type 'void' is unknown or zero in function X 
    orEncode 
    Error E2109 crypt.c 29&#58; Not an allowed type in function XorEncode 
    Error E2453 crypt.c 29&#58; Size of the type 'void' is unknown or zero in function X 
    orEncode 
    Error E2109 crypt.c 29&#58; Not an allowed type in function XorEncode 
    Error E2453 crypt.c 29&#58; Size of the type 'void' is unknown or zero in function X 
    orEncode 
    Error E2109 crypt.c 29&#58; Not an allowed type in function XorEncode 
    *** 6 errors in Compile *** 
    
    ** error 1 ** deleting crypt.obj


    Thanks for your answers in advance.
    http://3das.noeska.com - create adventure games without programming

  2. #2

    xor encode buffer in c

    So if you know better places where i can ask this question do let me know.
    www.gamedev.net is a good place.

    Binary operators are allowed only for integer data you can cast this void pointer to char or unsigned char (byte). If you are encoding text data just change parameter types from void* to char*.

    P.S. I think it should be if(ip == ipl) not if(ip = ipl) :?:

  3. #3

    Re: xor encode buffer in c

    What are the length() functions? Pass the data size in, and use a NUL-terminated string for the password.

    Code:
    void XorEncode&#40;const char *password, void *data, int size&#41; 
    &#123; 
       // Typecast data to a char * so the compiler knows what size it is
       char *d = &#40;char *&#41;data;
       // Start at the first character of the password
       const char *p = password;
       // Loop once for every byte in data
       while &#40;size--&#41;
       &#123;
          // If we reach the NUL terminator of the string, start at the beginning
          if &#40;!*p&#41;
            p = password;
          // Xor the byte with the password character, then go to the
          // next byte in data and the next character in the password
          *d++ ^= *p++;
       &#125; 
    &#125;

  4. #4
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    Re: xor encode buffer in c

    Quote Originally Posted by noeska
    First of all i know this is not the forum to ask c questions.
    I forgive you... --but it's gonna cost you 2 Pascal topics! :twisted:

    Jason McMillen
    Pascal Game Development
    Co-Founder





  5. #5

    xor encode buffer in c

    thanks for your answers this is how the function looks now:

    Code:
    void XorEncode&#40;char *password, int ipl, char *data, int dlength&#41; 
    &#123;
    	//char newdata&#91;&#93; = new char&#91;dlength&#93;;
    	//int dlength = length&#40;data&#41;;
    	//int ipl = length&#40;password&#41;;
    	int ip = 0;
    	int i =0;
    	for &#40;i=0; i < dlength; i++&#41; &#123;
    				data&#91;i&#93; = data&#91;i&#93; ^ password&#91;ip&#93; ;
    				ip++;
    				if &#40;ip == ipl&#41; ip = 0;
    	&#125;
    
    &#125;
    i was able to get the lengths of the 'pointer' before calling my own function. And yes length does not seem to be a valid c thing although the compiler did not complain about it but the linker did.

    so i consider this solved.
    http://3das.noeska.com - create adventure games without programming

  6. #6

    xor encode buffer in c

    That is one of the disadvantages of C: the compiler is not able to complain if procedures or variables do not exist. For small programs it is no problem, for begin problem you discover the problem only in the end.

  7. #7

    xor encode buffer in c

    The C compiler will give an error if the function or variable is not declared before use, but if the function/variable is declared but not defined (implementation), it will not find this error until the linker stage.

    The only difference in Pascal is that you cannot declare a function/variable without having a definition (implementation) in the same unit.

  8. #8

    xor encode buffer in c

    No, it eats it without any complaints:
    Code:
    daniel@laptop&#58;~> cat bla.c
    int main&#40;&#41; &#123;
      ninja&#40;&#41;;
    &#125;;
    daniel@laptop&#58;~> gcc -c bla.c
    daniel@laptop&#58;~>
    Afaik you can enable warnings, but that's about it.

  9. #9

    xor encode buffer in c

    Well, I'll be damned.

    But that is only true for C compilers. C++ compilers do indeed give an error about the missing function declaration. Using arm-linux-gcc (it's the only gcc I have handy) and the Visual C++ 2005 command line compiler.

    Code:
    D&#58;\Temp>type test.c
    int main&#40;int argc, char *argv&#91;&#93;&#41;
    &#123;
      ninja&#40;&#41;;
    &#125;
    
    D&#58;\Temp>arm-linux-gcc -c test.c
    
    D&#58;\Temp>cl /c test.c
    Microsoft &#40;R&#41; 32-bit C/C++ Optimizing Compiler Version 14.00.50727.42 for 80x86
    Copyright &#40;C&#41; Microsoft Corporation.  All rights reserved.
    
    test.c
    
    D&#58;\Temp>ren test.c test.cpp
    
    D&#58;\Temp>arm-linux-gcc -c test.cpp
    test.cpp&#58; In function 'int main&#40;int, char**&#41;'&#58;
    test.cpp&#58;3&#58; error&#58; 'ninja' was not declared in this scope
    
    D&#58;\Temp>cl /c test.cpp
    Microsoft &#40;R&#41; 32-bit C/C++ Optimizing Compiler Version 14.00.50727.42 for 80x86
    Copyright &#40;C&#41; Microsoft Corporation.  All rights reserved.
    
    test.cpp
    test.cpp&#40;3&#41; &#58; error C3861&#58; 'ninja'&#58; identifier not found
    
    D&#58;\Temp>

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •