LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-19-2012, 07:03 PM   #1
Blackened Justice
Member
 
Registered: Jun 2011
Posts: 82

Rep: Reputation: Disabled
Problems with strcpy/memcpy


Hey everyone,

I've been reading up a bit on obfuscation techniques, including the following article: http://www.brandonparker.net/code_obf.php

In helloworld-4.c, he beforehand uses a small C program to get the pair of doubles whose values correspond to the string "Hello World". To do that, he uses strcpy to copy into the double array the string "Hello World", and then prints out the values of the array members. Well, I can't seem to get this to work. The first float prints out the right value (219144411970...), but the second just prints out 0.000000... What's happening here? I have checked, and sizeof double == 8 in my machine, so I should be getting the same outcome.

Cheers
 
Old 05-20-2012, 12:38 AM   #2
lej
LQ Newbie
 
Registered: Aug 2011
Location: UK
Distribution: Slackware
Posts: 29

Rep: Reputation: Disabled
As far as I can see, he is not properly initializing str[1], i.e. he is writing 'Hello Wo' into str[0] (no problem), but only 'rld\0' into str[1], leaving the upper 4 bytes uninitialized, so the second number he got was essentially random!
 
Old 05-20-2012, 08:22 AM   #3
Blackened Justice
Member
 
Registered: Jun 2011
Posts: 82

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by lej View Post
As far as I can see, he is not properly initializing str[1], i.e. he is writing 'Hello Wo' into str[0] (no problem), but only 'rld\0' into str[1], leaving the upper 4 bytes uninitialized, so the second number he got was essentially random!
But even if I place the array outside main (globals are initialized to zeros) or if I memset it to zero, it still prints out 0.000000 as the second number. I don't expect it to print out the same number that he got, he probably did have garbage remaining, I just want to have a non-zero value It probably has to do with the internal representation of a double. If I print it out using %g, I get 3.25162e-317, so maybe its filling up the fraction bits but not the exponent ones.
 
Old 05-20-2012, 08:54 AM   #4
lej
LQ Newbie
 
Registered: Aug 2011
Location: UK
Distribution: Slackware
Posts: 29

Rep: Reputation: Disabled
Blackened Justice, I tested the code and got the same numbers as you. Yeah, it seems to be precision related, %f only prints to so many decimal places, while %g uses significant digits. The exponent and sign are the high 12 bits, which will be in the highest bytes on x86, so as the code doesn't write to those bytes, they were presumably zeroed to start with, resulting in a tiny number that %f just truncates to zero.

Last edited by lej; 05-20-2012 at 08:56 AM.
 
Old 05-20-2012, 09:32 AM   #5
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
A double is 8 bytes long and the most significant bytes are last. Not every pattern of 8 bytes is a double that can be uniquely translated for printing. Doubles that end with zeroes are especially tricky.

"Hello World" has 11 non zero bytes. So your second double has three non zero bytes followed by five zero bytes.

I suggest initializing the second double to some reasonable value, then do the strcpy, which will overwrite the four least significant bytes of the second double. Then you still have a value you can print and maybe a value that can be reconstructed from what you print.

I tried this on my system. I put both translations in one program for ease of testing:

Code:
#include <stdio.h>

int main(){
  double str[2] = {0.0, 1e16 };
  strcpy((char*)str,"Hello World");
  printf("%.17g\n%.0f\n",str[0],str[1]);

  double x[] = { 2.1914441197069634e228, 9999998138243300 };
  printf("%s\n", x);
}
That works, at least on my system. I tested a few times to discover how many significant digits were needed. I expected it to take 16, but the first number seems to need 18 (one before the dot and 17 after).

Last edited by johnsfine; 05-20-2012 at 01:27 PM.
 
Old 05-20-2012, 01:03 PM   #6
Blackened Justice
Member
 
Registered: Jun 2011
Posts: 82

Original Poster
Rep: Reputation: Disabled
Thank you all for your help, I ended up learning a lot from this whole thing
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
memcpy or mmap or other problems snowing Programming 3 12-26-2005 11:49 PM
memcpy or mmap or other problems snowing Programming 0 12-26-2005 09:56 PM
memcpy problems alaios Programming 4 09-17-2005 07:26 AM
strcpy problem rajatgarg Programming 5 11-20-2003 12:46 AM
question with strcpy Jo_Nak Programming 1 07-02-2003 04:23 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:26 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration