Solved

obfuscating array declaration ?!?

Posted on 2003-10-28
10
235 Views
Last Modified: 2010-04-15
Dear experts,

Shortly i have seen some C-Code where a array was declared like:

char str="ABC"[4];

Is someone kindly to explain me whats going on and how i could access this array-type !

P.S.: I alway thought a anonymous array should be e.g.  char *ptr=(char[4]){"ABC"};

thanks in advance,

Marc
0
Comment
Question by:marcpres
  • 4
  • 4
  • 2
10 Comments
 
LVL 8

Expert Comment

by:akshayxx
Comment Utility
>>char str="ABC"[4];
i think u missed something there... can u post more of the code, because i dont think it can be so obfuscated to break some basic rules.

show the snippet of the original code, and how s/he has used the variable,  also what compiler have u tested it with, that didnt work with my gcc on cygwin.. it compiled but the intentional string constant didnt come to effect.

0
 

Author Comment

by:marcpres
Comment Utility
hi,

the problem is i haven´t this code anymore, but i remember that the identifier was never used in a context,
could it be, that is some sort of padding bytes, which is wanted to not access it ?

Btw. I use gcc/linux and it isn´t complaining while compiling, but i have not a glue also, how to use this identifier actually i compile if with -Wall -pedantic without any problems.
0
 
LVL 8

Expert Comment

by:akshayxx
Comment Utility
see as for the variable type is concerned, i am 100% sure about the type of the 'str' variable here. which is just a 'char'.
and in no ways it is that 'str' holds the address of the string constant "ABC";.. so if u do prinf "%s" on str, it will either crash of give junk( u got to be lucky for second case).
so the 'trick' lies on the right hand side of the assignment operator... which will probably map to some integer value, which when typecasted to 'char' will give some special character.
it would have been helpful to have the original code, to say something more to-the-point, becuase usually the parts of obfuscated codes  are so tightly bound with each other, that any one part of it, if used separately, will make little sense.
still lets try to figure out what it could have been used for here.
akshay
0
 
LVL 8

Expert Comment

by:akshayxx
Comment Utility
>>, it will either crash of give junk( u got to be lucky for second case).
read as
, it will either crash OR  give junk( u got to be lucky for second case).

0
 
LVL 5

Expert Comment

by:snehanshu
Comment Utility
marcpres,
  str is not an array.
  A simple explanation is that str is assigned the 5th element of the constant array "ABC".
  The actual code would have been something like
char str="ABCDEFG"[4];
   Here it would mean str is initialized to be 'E'.
Here's a test for this, try this out:

 char str = "ABCDEF"[4];
 std::cout << str<< std::endl;
 for(int i=0;i<4;i++)
 {
         str = "ABCDEF"[i];
         cout << str;
 }

Cheers,
...Shu
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 5

Accepted Solution

by:
snehanshu earned 500 total points
Comment Utility
I had to use std::cout because of my visual studio.
read the test code as:
 char str = "ABCDEF"[4];
 cout << str<< endl;
 for(int i=0;i<4;i++)
 {
         str = "ABCDEF"[i];
         cout << str;
 }

:)
...Shu
0
 
LVL 5

Expert Comment

by:snehanshu
Comment Utility
OK, cout is for c++.
Use printf instead!

:)++
...S
0
 

Author Comment

by:marcpres
Comment Utility
thx snehanshu,

your explanation was very comprehensive !


0
 
LVL 5

Expert Comment

by:snehanshu
Comment Utility
Yes, I guess the 3 posts for one simple thing made it VERY COMPREHENSIVE ;)

Sometimes it helps to be a non-standard language programmer: I had to do this in Delphi quite often!

Cheers!
...Shu

Akshay, so we meet again :)
0
 
LVL 8

Expert Comment

by:akshayxx
Comment Utility
>>Akshay, so we meet again :)
Oh Hi, how r u doing, nice catch there, it took me to go out for dinner to get the same idea.. just that i didnt have any net connection there..
and i was disappointed to see that you already got it solved.. just-kidding..
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now