Closed Thread Icon

Preserved Topic: Obfuscated C in one function Pages that link to <a href="https://ozoneasylum.com/backlink?for=16826" title="Pages that link to Preserved Topic: Obfuscated C in one function" rel="nofollow" >Preserved Topic: Obfuscated C in one function\

 
Author Thread
Petskull
Maniac (V) Mad Scientist

From: 127 Halcyon Road, Marenia, Atlantis
Insane since: Aug 2000

posted posted 07-24-2002 20:49

what does this do?

line and model are char strings ('line' should be about 200 long while 'model' should be like 7)

strncmp(line, model, strlen(model)

I tried to find out on my own and even managed to come across the "standard" definition of the function. Check this out:

int strncmp(const char *s1, const char *s2, size_t n);

"The strncmp() function compares not more than n bytes (bytes that follow a null byte are not compared) from the array pointed to by s1 to the array pointed to by s2.

The sign of a non-zero return value is determined by the sign of the difference between the values of the first pair of bytes (both interpreted as type unsigned char) that differ in the strings being compared."


um... obviously...duh...



Code - CGI - links - DHTML - Javascript - Perl - programming - Magic - http://www.twistedport.com
ICQ: 67751342

[This message has been edited by Petskull (edited 07-24-2002).]

Wangenstein
Maniac (V) Inmate

From: The year 1881
Insane since: Mar 2001

posted posted 07-24-2002 21:01

I'm going to guess that it looks to see if 'line' and 'model' are identical for the length of 'model', which suggests that it's looking to make sure that 'line' starts with 'model'. Of course, I'm not a programmer (which I may or may not have just clearly illustrated).



Odd behavior; even temperament...

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 07-24-2002 21:08

I think it basically tells you if the two strings are the same and if not how many characters are different.

It will test the two up to the length of the 3rd argument.

so if you have
strncmp(line, model, strlen(model))

it should compare whatever "line" is to "model" up to the length of the string "model". I think it returns a value depending on where in the string the 2 characters don't match.

so if
line = "abcdefghijk"
and model = "abcIefg"

it will test the characters one by one and tell you which ones don't match. In the example above I think it will return -3 but I'm not sure. also in the example above I assume since you have the strlen(model) it will only test up until the 6th character, if they match up until then it will return 0 even though there is more on the end. Sorry I'm not a C coder but that's my best guess.

I think the confusing part is the reference to array's. I'm assuming that C like some other hi-level languages refrences character strings as arrays'



.:[ Never resist a perfect moment ]:.

Petskull
Maniac (V) Mad Scientist

From: 127 Halcyon Road, Marenia, Atlantis
Insane since: Aug 2000

posted posted 07-24-2002 22:34

see, I'm good with the first paragraph (and yes, in C multi character strings are arrays), it's the second paragraph that wipes me out.

I think your explanation did the trick, bit. Three things, though. What does it return if two or more characters are off? What if I want the part that matches to be somewhere in the middle? And won't the end of line character ('\0', not the newline character '\n') always guarantee a mis-match? Oh, how badly I yearn for some of Perl's wonderful regexps right about now...

The thing I'm having trouble is the searching for and extracting of very specific information from configuration and data files. This function shows promise but others have before- only to turn into dead ends. I fear I may have to resort to reading and comparing one character at a time...


Code - CGI - links - DHTML - Javascript - Perl - programming - Magic - http://www.twistedport.com
ICQ: 67751342

Wangenstein
Maniac (V) Inmate

From: The year 1881
Insane since: Mar 2001

posted posted 07-24-2002 23:00

Here's a little explanation I found: "Compares the first num characters of string1 to the first num characters of string2. The comparison is performed character by character. If a character that is not equal in both strings is found the function ends and returns a value that determines which of them was greater."


"/* strncmp example */
#include <stdio.h>
#include <string.h>

int main ()
{
char str[][5] = { "R2D2" , "C3PO" , "V4DR" , "C3A4" , "LUKE" };
int n;
printf ("Looking for human relations robots...\n");
for (n=0 ; n&t;5 ; n++)
if (strncmp (str[n],"C3**",2) == 0)
{
printf ("found %s\n",str[n]);
}
return 0;
}

Output:
Looking for human relations robots...
found C3PO
found C3A4"

hyperbole
Paranoid (IV) Inmate

From: Madison, Indiana, USA
Insane since: Aug 2000

posted posted 07-27-2002 06:00
quote:
The thing I'm having trouble is the searching for and extracting of very specific information from configuration and data files.



From this description, it seems to me the funtion you are looking for is 'strstr(s1, s2)'. This function returns a pointer the position in 's1' where 's2' starts. The function returns NULL* if 's2' is not found in 's1'.

The function 'strncmp' compares two strings. The last argument, 'n', limits the length of the search, in case one or both strings doesn't have a NULL terminater. Based on what you wrote above, I don't think it is the function you want.

I hope that helps. If you need to know more, feel free to ask again.




-- not necessarily stoned... just beautiful.

Petskull
Maniac (V) Mad Scientist

From: 127 Halcyon Road, Marenia, Atlantis
Insane since: Aug 2000

posted posted 07-27-2002 18:42

thanks... I looked up some info on that function and I'm not sure I see how it can help me...

perhaps you have deeper examples of how this works?


Code - CGI - links - DHTML - Javascript - Perl - programming - Magic - http://www.twistedport.com
ICQ: 67751342

« BackwardsOnwards »

Show Forum Drop Down Menu