coding problem

This is a discussion on coding problem within the C Programming forums, part of the General Programming Boards category; Hey I got this code and I just wanted to know what this code do and where the error is ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    10

    coding problem

    Hey I got this code and I just wanted to know what this code do and where the error is located:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #include <sys/time.h>
    
    typedef int int32;
    typedef char int8;
    
    void xorStringRounds(int8  *output, int8  *pbDataOne, const int8  *pbDataTwo, int32 length)
    {
    	int32 i = 0;
    
    	for(i=0; i<length; i++)
    	{
    		output[i] = (int8)(pbDataOne[i] ^ pbDataTwo[i]);
    	}
    
    	return;
    }
    
    void itoa( int32 num, int8  *alpha, int32 radix )
    {
    	if( radix == 10 )
    	{
    		sprintf(alpha, "%i", num);
    	}
    	else if( radix == 16 )
    	{
    		sprintf(alpha, "%X", num);
    	}
    }
    
    int8 *oldModString(int32 modifier, const int8  *pbDataOne, int32 length)
    {
    	int8  leading[3];
    	int32 ileading;
    	int8 * tempStr = NULL;
    	int8 * ret;
    	int32 i = 0;
    
    	itoa(modifier/2, leading, 10);
    	ileading = atoi(leading);
    	tempStr = (int8 *) malloc(8);
    	ret = (int8 *) malloc(length);
    	memset(tempStr, 0, 8);
    	tempStr[0] = 0;
    
    	if( (modifier+1)%2 == 0 ) {
    		tempStr[0] = (int8)((ileading<<4) + 8);
    	}
    	else {
    		tempStr[0] = (int8)(ileading<<4);
    	}
    
    	for(i=0; i<(length>>3); i++)
    	{
    		xorStringRounds(ret+i*8, tempStr, pbDataOne+i*8, 8);
    	}
    	free(tempStr);
    	return ret;
    }
    
    int main(int argc, char **argv) {
    	int8 data[32];
    
    	memset(data, 0x0A, sizeof(data));
    	int8 *resp = oldModString(0xFF, data, sizeof(data));
    
    	free(resp);
    
    	return 0;
    }
    And I kind of don't want to download the linux mostly because I am afraid to
    If you could help me that would be great and thanks

  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Why don't you just read through the code and figure out what it does yourself? And how do you know that there is an error?

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    itoa(modifier/2, leading, 10);
    ileading = atoi(leading);
    why not just

    ileading = modifier/2;
    at least youwill not have memory overrun in this case
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    I agree with quantumpete. The key point is to break down this complicated issue you are dealing with into smaller pieces. Then concentrate on the issue you want to solve. Much better this way. Also if you are afraid of installing Linux, then what have you used to compile this, better yet what compiler are you using, have you even tried to compile this?

    When you post such open ended questions like this, you bound to get a bunch more thrown at you.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    But why would you need Linux in the first place?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    Quote Originally Posted by Elysia View Post
    But why would you need Linux in the first place?
    Lol, you don't really. I was just commenting against their statement.

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    4
    So Learner87 did you get the exact errors and its issues in the code. If yes, please post it so that it could help others too...

    korn

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    4
    Quote Originally Posted by vart View Post
    why not just

    ileading = modifier/2;
    at least youwill not have memory overrun in this case
    Hello,

    After changing the name of itoa function to my_itoa, the code runs fine. Its because of the itoa() also defined in stdlib.h
    However I have a question here,
    What exactly does oldModString() do? Is it just masking the values?

    --
    korn

  9. #9
    Registered User
    Join Date
    Jul 2009
    Posts
    1

    This was a test

    The code learner87 attached was a test for a local encryption programmer position.

    I guess learner87 was not so learned87 after all.

  10. #10
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305

    @ jesseutsa
    The code learner87 attached was a test for a local encryption programmer position.

    I guess learner87 was not so learned87 after all.
    Thats really funny. I mean posting interview questions so that you can send the answer back and claim it as your own.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  3. Replies: 16
    Last Post: 01-26-2006, 01:38 PM
  4. Replies: 5
    Last Post: 11-07-2005, 10:34 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21