Amiga.org
Amiga computer related discussion => Amiga Software Issues and Discussion => Topic started by: Atheist on April 02, 2003, 11:07:15 AM
-
Found this source code example on the net...
/*
As an extreme example the following C code (mystery.c) is actually legal C code.
#include
main(t,_,a)
char *a;
{return!0main(-86, 0, a+1 )+a)):1,t<_?main(t+1, _, a ):3,main ( -94, -27+t, a
)&&t == 2 ?_<13 ?main ( 2, _+1, "%s %d %d\n" ):9:16:t<0?t<-72?main(_,
t,"@n'+,#'/*{}w+/w#cdnr/+,{}r/*de}+,/*{*+,/w{%+,/w#q#n+,/#{l,+,/n{n+\
,/+#n+,/#;#q#n+,/+k#;*+,/'r :'d*'3,}{w+K w'K:'+}e#';dq#'l q#'+d'K#!/\
+k#;q#'r}eKK#}w'r}eKK{nl]'/#;#q#n'){)#}w'){){nl]'/+#n';d}rw' i;# ){n\
l]!/n{n#'; r{#w'r nc{nl]'/#{l,+'K {rw' iK{;[{nl]'/w#q#\
n'wk nw' iwk{KK{nl]!/w{%'l##w#' i; :{nl]'/*{q#'ld;r'}{nlwb!/*de}'c \
;;{nl'-{}rw]'/+,}##'*}#nc,',#nw]'/+kd'+e}+;\
#'rdq#w! nr'/ ') }+}{rl#'{n' ')# }'+}##(!!/")
:t<-50?_==*a ?putchar(a[31]):main(-65,_,a+1):main((*a == '/')+t,_,a\
+1 ):0i@bK'(q)-[w]*%n+r3#l,{}:\nuwloca-O;m .vpbks,fxntdCeghiry"),a+1);}
*/
AmigaOne! Dumbfounded!!
-
Ahh yes. Reminds me of the obfuscated C competitions. I've seen some C sources like that that were shaped like a train or a whale, which is fun.
Nice example there; interesting use of recursion and the ? operator. I also didn't realise '_' was a legal variable name.
Of course, some languages like Brainf**k, Perl and my own "Bonk" (http://www.petergordon.org.uk) are pretty obfuscated as a matter of course :-D
-
On the first day of Christmas my true love gave to me
a partridge in a pear tree.
On the second day of Christmas my true love gave to me
two turtle dovesin a pear tree.
On the third day of Christmas my true love gave to me
three french hens, two turtle doves
On the fourth day of Christmas my true love gave to me
four calling birds, three french hens, two turtle doves
On the fifth day of Christmas my true love gave to me
five gold rings;ds, three french hens, two turtle doves
and a partridge in a pear tree.
On the sixth day of Christmas my true love gave to mees
and a partridge in a pear tree.
On the eighth day of Christmas my true love gave to me
eight maids a-milking, seven swans a-swimming,
six geese a-laying, five gold rings;
four calling birds, three french hens, two turtle doves
and a partridge in a pear tree.
On the ninth day of Christmas my true love gave to me
nine ladies dancing, eight maids a-milking, seven swans a-swimming,
six geese a-laying, five gold rings;
four calling birds, three french hens, two turtle doves
and a partridge in a pear tree.
On the tenth day of Christmas my true love gave to me
ten lords a-leaping,
and a partridge in a pear tree.
ten lords a-leaping,Christmas my true love gave to me
nine ladies dancing, eight maids a-milking, seven swans a-swimming,
six geese a-laying, five gold rings;
four calling birds, three french hens, two turtle doves
and a partridge in a pear tree.
eleven pipers piping, ten lords a-leaping,ove gave to me
nine ladies dancing, eight maids a-milking, seven swans a-swimming,
six geese a-laying, five gold rings;
four calling birds, three french hens, two turtle doves
and a partridge in a pear tree.
twelve drummers drumming, eleven pipers piping, ten lords a-leaping,
nine ladies dancing, eight maids a-milking, seven swans a-swimming,
six geese a-laying, five gold rings;
four calling birds, three french hens, two turtle doves
and a partridge in a pear tree.
-
Is that what it does? thats really cool :-D
Edit: Just compiled it, and yes it does. Thats very clever, i'm impressed!
-
That's cool.
Recursive call to main() :-)
I'd guess this source was generated by a program rather than created by hand, though I may be wrong :-D
-edit-
_ is a perfectly legal name. Not very useful, but legal :-D
-
It actually looks like this thing I saw once...
A program which takes a text file and generates an encryption of it that is expressed in program flow code, execution reconstitutes the text file.
But that was in Pascal :-) Maybe somebody ported it?
As an excersise, reformat the source and see if you can disentangle it :lol:
-
Hello Frodon or Tickly,
I was wondering, how many bytes is the compiled code?
AmigaOne! Dumbfounded + Amazed!
P.S. Found it here http://www.cs.cf.ac.uk/Dave/C/node4.html#SECTION00460000000000000000
-
Atheist wrote:
Hello Frodon or Tickly,
Gee, hi to you too :-D Something I said? (joking)
-
That's Cool,
I compiled it in gcc no probs, (well had to uncomment it 1st)
Now all I have to do is figure out how it does it.
Stay tuned folks I'm sure I'll figure it out by this time next year!! :-D
Cheers,
Crumpster
-
Strictly speaking it uses obsolete K&R function syntax in the declaration of main.
You should *cough* try
int main (int t, int _, char* c)
{
...
}
instead :-)
-
@ Athiest
Compiled size here (RedHat 7.3 / gcc 2.96) is 14540 bytes or 14ish Kb.
Cheers,
Crumpster
Changed the Kb size to 14 - cheers Karlos
-
You mean 14ish Kb?
-
Karlos wrote:
Atheist wrote:
Hello Frodon or Tickly,
Gee, hi to you too :-D Something I said? (joking)
I didn't say hi, because, you didn't compile it, and therefore, didn't have an answer to that question, speaking of which...
Hello Crumpster,
gcc, so that would be PPC and therefore RISC, so how many bytes was it, when you compiled it?
Thanks, I just HAD to know.
AmigaOne! C(++), ...phew!! Challenging!
-
Atheist wrote:
I didn't say hi, because, you didn't compile it, and therefore, didn't have an answer to that question, speaking of which...
Nope, but I will tonight :-D
BTW, doesn't have to be PPC just because he used gcc. There is gcc-68K / x86 / mips etc. etc. too :-)
It's a pretty funky code example, huh?
I still think this was generated by a machine - or a serious (and frighteningly clever) geek :-)
-
@ Karlos
yep 14 ish Kb sorry finger slipped on the decimal point
(I obviously can't count, did I cover it ok? :-P )
Thanks fo the correction.
@ Athiest
Not on an A1 yet I'm afraid, crappy works P2 233Mhz with redhat 7.3 on it.
Cheers
Crumpy
-
Compiled with GCC for the Win32 console the exe is 23,468 bytes.
-
Tickly Wrote Thus:
Compiled with GCC for the Win32 console the exe is 23,468 bytes.
Good old Windows Bloat!! :-D
-
Crumpster wrote:
Good old Windows Bloat!! :-D
Is it just me not understanding how these things work, or is that somwhat excessive? I wonder if it still relies on a DLL being present?
Also, as I wondered out loud in another thread, how would going to a 64 bit CPU instruction set affect the size?
AmigaOne! "What the DLL you talkin' 'bout, fool?", Mr. T?
-
That's very cool! :-o
-
Atheist wrote:
Also, as I wondered out loud in another thread, how would going to a 64 bit CPU instruction set affect the size?
AmigaOne! "What the DLL you talkin' 'bout, fool?", Mr. T?
Here (http://www.amiga.org/forums/showthread.php?t=1694)
-
4788 bytes on sparc if the -s switch is used :)
-
6216 bytes with SAS/C with default options on 68k AmigaOS :)
-
I get the same size with SAS/C, however if I size optimize it and turn off some other things I can get it down to 5k, but it then gurus 8000 0004 after running :( Oh well, 6k's still not bad ;)
-
3968 bytes on 68k linux. 3 cheers for 680x0 :).
3.875Kb sounds even better!
-rwxr-xr-x 1 dana dana 3968 Apr 3 04:44 a.out
-
Ok everbody,
How big is the pure object file generated on your systems?
The size of the stdio / startup stuff has a lot of impact on some systems.
To see the true code generation size, just look at the unlinked object file. Should be interesting :-)
-
Object file is 1128 bytes with SAS/C. :)
-
mine comes out to:
-rw-r--r-- 1 dana dana 2060 Apr 3 05:34 xmas.o
2060 bytes. I know little about gcc and any optimisations, but that's with 2.95 on debian woody on a 68040.
dana
-
Compiled under Watcom 11.0c as a Win32 character mode executable (speed rather than space opts)
02/04/2003 20:32 881 mystery.c
02/04/2003 20:36 31,232 mystery.exe
02/04/2003 20:36 113,664 mystery.ilk
02/04/2003 20:36 19 mystery.lk1
02/04/2003 20:36 22,729 mystery.map
02/04/2003 20:36 89 mystery.mk
02/04/2003 20:36 669 mystery.mk1
02/04/2003 20:36 1,496 mystery.obj
02/04/2003 20:36 16,599 mystery.sym
As you can see, the object file is only 1.4kB :-)
-edit-
Check out the winbloat! 30kb of sh*t linked just to make the exe :-D
-edit-
My guess is
1) the original text is compressed with something like LZ or dynamic huffman style compression, encoded into the big string in the middle
"@n'+,#'/*{}w+/w#cdnr/+,{}r/*de}+,/*{*+,/w{%+,/w#q#n+,/#{l,+,/n{n+\
,/+#n+,/#;#q#n+,/+k#;*+,/'r :'d*'3,}{w+K w'K:'+}e#';dq#'l q#'+d'K#!/\
+k#;q#'r}eKK#}w'r}eKK{nl]'/#;#q#n'){)#}w'){){nl]'/+#n';d}rw' i;# ){n\
l]!/n{n#'; r{#w'r nc{nl]'/#{l,+'K {rw' iK{;[{nl]'/w#q#\
n'wk nw' iwk{KK{nl]!/w{%'l##w#' i; :{nl]'/*{q#'ld;r'}{nlwb!/*de}'c \
;;{nl'-{}rw]'/+,}##'*}#nc,',#nw]'/+kd'+e}+;\
#'rdq#w! nr'/ ') }+}{rl#'{n' ')# }'+}##(!!/"
2) The binary lookup for the encoding is stored in the smaller string
"!ek;dc \
i@bK'(q)-[w]*%n+r3#l,{}:\nuwloca-O;m .vpbks,fxntdCeghiry"
3) main() is defined as a recursive extraction algorithm (similar to some huffman decoders) and character output.
4) This is an oversimplification since main() is called internally several times and tested conditionally with operator ? :...
Anybody else got any ideas?
This thing is cool :-)
-
danamania wrote:
mine comes out to:
-rw-r--r-- 1 dana dana 2060 Apr 3 05:34 xmas.o
2060 bytes. I know little about gcc and any optimisations, but that's with 2.95 on debian woody on a 68040.
dana
Given that youre exe was only a kilibyte larger, you sure do have a good startup/stdio lib :-D
Probably just a stub that relies on OS calls deeper in?
-
The size of the stdio / startup stuff has a lot of impact on some systems.
Yes indeed. So why not skip stdio & startup?
Here is what I did:
#include
#include
#include
#include
#define putchar(x) FPutC(Output(),x)
struct DosLibrary *DOSBase;
void entry(void)
{
struct ExecBase *SysBase = *(struct ExecBase **)4;
DOSBase = (struct DosLibrary *) OpenLibrary("dos.library", 37);
if (DOSBase)
{
main(1,NULL,NULL);
CloseLibrary((struct Library *) DOSBase);
}
}
//#include
.... rest of the original code...
Then compile with size optimization, link without startup code, and strip resulting exe:
68000, AmigaOS (SAS/C 6.58):
mystery 1048 ----rwed Today 23:32:37
PowerPC, MorphOS (gcc 2.95.3):
mystery 2200 ----rwed Today 23:32:42
Next step: Optimize the generated asm code. :-)
-
About obfuscated C, here's my .sig block I wrote some years ago:
--
l=2001;main(i){float o,O,_,I,D;for(;O=I=l/571.-1.75,l;)for(putchar(--l%80?
i:10),o=D=l%80*.05-2,i=31;_=O*O,O=2*o*O+I,o=o*o-_+D,o+_+_<4+D&i++<87;);puts
(" Harry 'Piru' Sintonen http://www.iki.fi/sintonen");}
..It's a twoliner if you get rid of the last info puts(). :-)
-
I used MS VC++ to compile the code.
The obj file length is 2015 bytes.
The first exe I VC generated was about 36K long. With size optimization, it was 32303 bytes. After some tinkering, I got it down to 28Kb.
Then, just to be cheeky, I ran it thru PECompact V1.32. That got it down to 13824 bytes. Yeah....I know that's cheating! :-D
-
Piru wrote:
68000, AmigaOS (SAS/C 6.58):
mystery 1048 ----rwed Today 23:32:37
PowerPC, MorphOS (gcc 2.95.3):
mystery 2200 ----rwed Today 23:32:42
Now, could one attribute the 1152 byte difference to RISC overhead (needing more instr, per CISC command)?
Piru wrote:
Next step: Optimize the generated asm code. :-)
Major BrainStrain could result in ones being carted away in a strait jacket, or perhaps, return sanity and clearity of the Universe for another? :-D
AmigaOne! Has this made a point?
-
Karlos wrote:
Check out the winbloat! 30kb of sh*t linked just to make the exe :-D
Hello Karlos,
Could you imagine 10 or 20 commands in the C: (I mean ms-dos/windos) directory, with that kind of non-sense? NOT!!!
AmigaOne! If only it were in assembler! :-D :-D :-D
-
Atheist wrote:
Karlos wrote:
Check out the winbloat! 30kb of sh*t linked just to make the exe :-D
Hello Karlos,
Could you imagine 10 or 20 commands in the C: (I mean ms-dos/windos) directory, with that kind of non-sense? NOT!!!
AmigaOne! If only it were in assembler! :-D :-D :-D
-edit-
You should see the sodding System32 folder!
-edit-
To be fair, the stuff in your C: directory is
1) usually written in asm (up to 3.1 anyhow)
2) Relies on AmigaOS dos.library for IO / startup
For example, the smallest hello world I got in
1) standard ANSI C : 20 kB (with stormC stdio)
2) ANSIC / dos.libnrary 500 odd bytes
3) asm / dos.library about 100 bytes
It's a real pity that exec.library RawDoFmt() doens't have proper C printf() templating - we could have a very compact stdio then.
But then again, it was written with BCPL in mind :-D
-
My guess is
1) the original text is compressed with something like LZ or dynamic huffman style compression, encoded into the big string in the middle
Hmm, if you zip up the output it compacts down to 296 bytes (WinZip, max compression), so maybe I'm onto something...;-)
-
Now, could one attribute the 1152 byte difference to RISC overhead (needing more instr, per CISC command)?
ELF is not as compact fileformat as amiga hunk format is. In fact you could sqeeze some extra bytes out of it by compacting the ELF header and stuff (I've seen it done on linux x86). The ELF fileformat size overhead only starts to play a role with extremely small executables.
As a rule of thumb the same PPC code is roughly 2x 68k code size. This varies and depends on the code itself, of course.
-
For example, the smallest hello world I got in
1) standard ANSI C : 20 kB (with stormC stdio)
2) ANSIC / dos.libnrary 500 odd bytes
3) asm / dos.library about 100 bytes
But then again, it was written with BCPL in mind
Heh, this reminds me of another project, the shortest possible hello world in assembler.
You asked for it, and here it is: the smallest possible Hello World program as amiga executable (feel free to prove me wrong, though):
Main:
bsr.b .main
dc.b 'Hello, World!',10
.main:
move.l $AC(a2),a4
move.l (sp)+,d1
moveq #14,d2
moveq #12,d0
jmp (a5)
That is:
- 14 bytes of code
- 14 bytes of data
Total 64 bytes as compiled to a executable. You can shorten the string to "Hello World\n", and thus have just 12 bytes of data, but the executable size remains as 64 due to padding.
And surprise surprise, the code (ab)uses some BCPL vector (globvec). :-)
-
An OS in assembly, well this ones X86 but.... MenuetOS.org its a GUI enhanced operating system, fits on a single floppy. ITs written entirely in ASM.. the floppy comes with Tasm I believe (or Fasm maybe?) an MP3 player, a cd audio player, a maze thing, and some other stuff. Its creepy what can be done with a bootable floppy. :idea: :shocked:
-
object file on sparc/sunos/gcc 1896 bytes
-
Piru wrote:
68000, AmigaOS (SAS/C 6.58):
mystery 1048 ----rwed Today
Anybody beaten this yet?
I might see what vbcc does for ppc it tonight...
-
Wow, I don't come here for one evening cos of the football and this thread doubles in size!!
Does anyone know what tricks to use on gcc to get the end file size down.
I'm a newbee when it comes to gcc, i can compile and change the output filename from a.out, and thats it!! :-?
Cheers,
Crumpy
-
Hi Crumpy,
Well, a quick summary. We went through the 'how big does the standard library bloat it out'
So we checked only the object size (not the exe). Unsurprisingly for my Win32 char mode exe attempt the stdio stuff was 20 times bigger than the actual code.
Different folks, different mileage...
Piru gave us an example of super minimalist startup code for AmigaOS and got it down to 1K :-)
I rambled about how I thought the thing worked...
You return and the thread is twice as long :-D
-
AARGH! By all gods and somewhat important deities! :-o :-o :-o
I was planning to learn C or C++, but this is too much!
/me runs back home to give a big hug to FreePascal :-D
-
whabang wrote:
AARGH! By all gods and somewhat important deities! :-o :-o :-o
I was planning to learn C or C++, but this is too much!
/me runs back home to give a big hug to FreePascal :-D
Fear not, for 'tis merely an exmple of confusication - with a capital C :-)
No real program would look like this. Unless part of the Win32 kernel :-D
-
/me sure hopes that you are right :nervous:
-
Have no fear matey,
C (and C++) is actually one of the most logically laid out languages there is. It's stuctured, terse and flexible. It has very few keywords, all the basic operators you could ever need and everything else is done using functions.
Before anybody starts - the above is true, once you get use to it :-)
I'm gonna be unemployed real soon so that long awaited tutorial site I was considering (see here) (http://www.amiga.org/forums/showthread.php?t=1377) may see the light of day :-D
-
@ Karlos
Cheers for the update, have read through the backlog now.
How come your losing your job? (I take it you work in Software Development??)
Cheers,
Crumpy
-
Incidentally, I would be interested in that tutorial site.
Can't remember if I piped up before.
I know a tad of C++ (a really ickle bit) so would welcome such a site.
-
Crumpster wrote:
How come your losing your job? (I take it you work in Software Development??)
Too much time on amiga.org :-D. Nah, sigh of the times :-(
-
Piru wrote:
68000, AmigaOS (SAS/C 6.58):
mystery 1048 ----rwed Today
Anybody beaten this yet?
These with my half-working 68k gcc 2.95.2 setup:
mystery-68000-gcc 968 ----rwed Today 16:41:16
mystery-68020-gcc 928 ----rwed Today 16:40:14
2 files - 1896 bytes used
...I wonder if I missed something with SAS/C though, it should gererate smaller code imo.
-
Wow, that's pretty titchy!
I guess the 020 code benefits from some addressing optimisations...
-
I wonder how big the .exe file would be on win32 vs. 68K vs. 68020 (or higher) vs. PPC if it was just one big
printf("On the first day of Christmas my true love gave to me/n
a partridge in a pear tree./n/n
On the second day of Christmas my true love gave to me/n
two turtle dovesin a pear tree./n/n
On the third day of Christmas my true love gave to me/n.......")
I think I got the printf(" ") part right.
I ask because, on win(lose)dohs, if you go to the trouble of writing IMPRESSIVE code, it still bites you in the end.
It is VERY IMPRESSIVE code to me, at any rate.
AmigaOne! Things are gonna get REAL interesting!!!
-
im not sure what that code was... but it reminds me of some code i saw in a magazine once that decoded some kind of DIVX or DVD encoding, that was said to be unbreakable!
ROFL! unbreakable they said...
HAHAH! completly secure they said...
-
Atheist wrote:
I ask because, on win(lose)dohs, if you go to the trouble of writing IMPRESSIVE code, it still bites you in the end.
One doesn't always get bitten.
For example, my portion of my senior project was a Win32 program to control WinAmp. The user interacts with WinAmp thru a touchscreen LCD connected to a serial port via an RF link. I found similar projects to control WinAmp thru a game port. The smallest of those I found had a 300KB executable file and all the really did was allow you to send the basic commands like play, stop and FF/RR. My program gives you control over those plus will send the name of the track playing, navigate playlists (with names) and had rudimentary support for manipulating WinAmp's equalizer.
Based on the size of the other programs I found, I thought mine would be in the neighborhood of 500KB. Imagine my and my partners surprize when the final executable was only 28KB.
-
iamaboringperson wrote:
im not sure what that code was... but it reminds me of some code i saw in a magazine once that decoded some kind of DIVX or DVD encoding, that was said to be unbreakable!
You're probably thinking about CSS and DeCSS. I find it amusing that you can buy a t-shirt with DeCSS in PERL silkscreened on it. I am a circumvention device....
-
All for a good cause, too - top of the Group !
anyway, we are here to talk about one of me other favourite topics - C.
I had a go tidying up (ie: reformatting in an attempt to make more readable) the 1st program.
I came across summat I hadnt seen before there, and in Piru's program too - the use of a Comma. what does this signify ?
In particular for Piru's program in the 2nd for loop:
[color=000000]putchar(--l%80?i:10)[/color][color=00ccoo],[/color]o=D=l%80*.05-2[color=00ccoo],[/color]i=31
I presume it means many statements per line or something... :-?
-
It's a sequencing operator - not many C programmers even realise this, using it only to seperate declarations of variables etc.
For example
x = (f(), g());
AFAIR, the value of x is whatever g() returns, but f() is always called before g().
This seems pointless when you could write
f();
x = g();
but it does have it's uses :-)
-
It's a sequencing operator - not many C programmers even realise this, using it only to seperate declarations of variables etc.
It's one of those things the longhaired geeks that sit in the basement all day coding wanted in C so they can write code no one else understands and feel very super-leet. :-)
-
Hey dude, I got my hair cut 2 years ago. My avatar image just old, that's all :-D
Actually, for C code, the comma operator is often useful in function-like macros that are supposed to 'return' a result and need to do several operations without ending the statement (ie with a semicolon).
A lot less useful in C++ where you just have an inline function.
-
Cheers Karlos, thats reet canny !
Casper, I am a Greater English Longhair meself.
now I know this I am well on the way to being leet !
(though super-leet is a long way off- I sit on the 2nd floor coding in PL/1 on our Mainframe - not as much fun as C !)
-
Reet canny :lol:
I'm homesick :cry:
-
Karlos wrote:
Hey dude, I got my hair cut 2 years ago. My avatar image just old, that's all :-D
I havent - shame on you !
Actually, for C code, the comma operator is often useful in function-like macros that are supposed to 'return' a result and need to do several operations without ending the statement (ie with a semicolon).
Thats roughly what I guessed at - how useful is that ?! :-D
I'll have to mess about with it on me Handspring
A lot less useful in C++ where you just have an inline function.
I never really got to grips with C++.
it seems to overcomplicate a language based on simplicity and straightforward-ness (?!),
but then maybe I need to actually attempt to get something done in C++ rather than trying to pick up concepts from a book ! (from Bernard Babani)
-
I had to cut me locks, man I wasn't getting hired!
Actually went to the other extreme for a bit ;-)
C++ is very cool and funky as long as yoi just don't dive in at the deep end with it - people look at the template driven STL stuff thats out there and go into cerebal meltdown.
Even without the OOP, there are many nice features you can use that aren't available in C (though the 1999 ISO standard does drag it up to date a bit).
Once you get into OOP though, you'll wonder how you did without it.
IMHO :-)
-
So am I, even though its only 60 miles east of here...
Jus' get y'sel doon tha pub an' swally doon some o'that broon ale man.
Ye'll be aal reet after that !
(yes ! I learned everything I never needed to know about Novocastrian speak in Viz ! :-o
and having the p1ss taken by mates who were real geordies... :-D )
-
:lol:
This guy here at work (Venkman) used to work in a music shop of some sort - this geordie came in asking for speakers.
Check his signature - you'll see ;-)
-
ooh. wasnt he in Ghostbusters ?
-
He sure was...
But we know him better as P. H. Pete :-)