Stupid question

L

Leslie Kis-Adam

Hi everyone!
Can anyone explain me this program?
I'm getting really confused.
thx in advance.

Laszlo Kis-Adam
<[email protected]>

"


#include <stdio.h>
main(t,_,a)char *a;{return!0<t?t<3?main(-79,-13,a+main(-87,1-_,
main(-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;# ){nl]!/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(31[a]):main(-65,_,a+1):main((*a=='/')+t,_,a+1)
:0<t?main(2,2,"%s"):*a=='/'||main(0,main(-61,*a,
"!ek;dc i@bK'(q)-[w]*%n+r3#l,{}:\nuwloca-O;m .vpbks,fxntdCeghiry"),
a+1);}


"
 
R

Richard Heathfield

Leslie Kis-Adam said:
Hi everyone!
Can anyone explain me this program?
I'm getting really confused.

#include <stdio.h>
main(t,_,a)char *a;

It's actually very simple. Since main is required to take either no, or two,
parameters, this program (in which main takes /three/ parameters) doesn't
obey the rules of C, and so its behaviour is undefined. It might do
anything at all.
 
K

Keith Thompson

Richard Heathfield said:
Leslie Kis-Adam said:


It's actually very simple. Since main is required to take either no, or two,
parameters, this program (in which main takes /three/ parameters) doesn't
obey the rules of C, and so its behaviour is undefined. It might do
anything at all.

Assuming that the program is for a hosted implementation that doesn't
document this form of main() as an extension.
 
R

Robbie Hatley

Leslie-and-or-Lazlo Kis-Adam said:
Can anyone explain me this program?
(snip obfuscated source code)

Looks like an entry to the "Obfuscated C Contest". (Google that
if you don't know what it is.)
I'm getting really confused.

Obfuscation is the whole idea. You didn't think that C
programmers actually write code like that on the job, did you?
(Well, there's a certain ex-coworker of mine who used to write
code ALMOST that obfuscated... but he got fired for it, and
rightly so.)

If you really, really want a disection of this beast, I'm afraid
you'll have to pester someone else here into giving you one; I
don't torture my brain that way these days. I do structured
programming, not demolished programming.
thx in advance.

You're welcome, in retrospect.

Cheers,
Robbie Hatley
lone wolf intj at pac bell dot net
(Put "[usenet]" in subject to bypass spam filter.)
 
R

Richard Heathfield

Keith Thompson said:
Assuming that the program is for a hosted implementation that doesn't
document this form of main() as an extension.

Hosted? Granted. But I believe your other point is incorrect, since it would
apply only to a C99 program, which this clearly is not, as it relies on
implicit int.
 
J

james of tucson

Leslie said:
Hi everyone!
Can anyone explain me this program?
I'm getting really confused.
thx in advance.

It's making recursive calls to main(int, int, char *) (which already
takes us out of "Standard C conformance", which is slightly disappointing.)

There is a lot of conditional logic using the ternary operator, which,
if it were written in if-else syntax, might make the program a little
easier to follow. There are a couple of places that depend on operator
precedence as well, and stupid things like the fact that 31[a] is
equivalent to a[31]. Then there's the use of "_" as a scalar,
defaulting to "int", and the use of logical operators to delimit
statements (as opposed to using braces and semicolons.)

Getting past the mechanical level of obfuscation, which is really little
more than "ugly code", and which the author probably used some program
to make anyway, you will find that it's basically what you might call a
polymorphic function being called recursively, or maybe you would call
it a switch statement (but definitely being called recursively).

Then it does some kind of lookup-and-substitution, and all I can tell
you about that is it involves looking for the "/" character somehow.

If you really want to figure this program out, I would suggest
re-writing it, making use of whitespace and reasonable identifier names.
Instead of using the function "main" for the recursion, make another
function. Lose the ternary operators in favor of if-else. Where the
author used the side effects of expressions delimited by logical && and
||, use curly braces and semicolons. Then maybe you can step through it
in a debugger or something and figure out how the cipher actually works.
 
J

james of tucson

Richard said:
Keith Thompson said:


Hosted? Granted. But I believe your other point is incorrect, since it would
apply only to a C99 program, which this clearly is not, as it relies on
implicit int.

You guys are arguing about how it's not "Standard C" (which it's not),
but not making any indication that you can see how the cipher is
implemented. I can't. I can get *to* the cipher. I can understand how
it's blocked, more or less, but I could not tell you even in vague terms
how it works.

I know you guys make a hobby out of establishing the standard-C-ness of
the newsgroup, but come on! How does this program stop at 12 choruses?
How does it count your true love's gifts for each day? How does the
string decipher know how to pick each character from the ciphertext it
gets?


Here, let's make the program Standard-C conforming, no warnings with
$ cc -g -ansi --pedantic -o obfusc obfusc.c
And maybe put in a little whitespace (still horrible):


/* file: obfusc.c */
#include <stdio.h>
int gifts(int t, int _, char *a)
{
return !0 < t ? t < 3 ? gifts(-79, -13, a + gifts(-87, 1 - _,
gifts(-86, 0,
a + 1) +
a)) : 1,
t < _? gifts(t + 1, _, a) : 3, gifts(-94, -27 + t, a)
&& t == 2 ? _ < 13 ? gifts(2, _ + 1,
"%s %d %d\n") : 9 : 16 : t < 0 ? t < -72 ? gifts(_, 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;# ){nl]!/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(31[a]) : gifts(-65, _,
a + 1) : gifts((*a == '/') + t, _, a + 1)
: 0 < t ? gifts(2, 2, "%s") : *a == '/'
|| gifts(0, gifts(-61, *a,
"!ek;dc i@bK'(q)-[w]*%n+r3#l,{}:\nuwloca-O;m .vpbks,fxntdCeghiry"),
a + 1);
}

int main(int argc, char **argv)
{
return gifts(argc, argc, argv[0]);
}

/* end: obfusc.c */
 
K

Keith Thompson

Richard Heathfield said:
Keith Thompson said:

Hosted? Granted. But I believe your other point is incorrect, since
it would apply only to a C99 program, which this clearly is not, as
it relies on implicit int.

C99 says that an implementation may define other forms of main(), but
that permission is redundant given C99 4p6:

A conforming implementation may have extensions (including
additional library functions), provided they do not alter the
behavior of any strictly conforming program.

C90 has the same wording.

For that matter, a C99 implementation could provide implicit int as an
extension, though it would still be required to issue a diagnostic (in
conforming mode) for any use of it.
 
D

David T. Ashley

Leslie Kis-Adam said:
Hi everyone!
Can anyone explain me this program?
I'm getting really confused.
thx in advance.

Two things you should know:

#1: There is some sort of an obfuscated 'C' contest, see here:

http://www.ioccc.org/main.html

The winning entries from previous years are quite ... obfuse.

#2: Some companies make software that will transform 'C' code into code
that compiles equivalently for the purpose of protecting intellectual
property when source that compiles must be distributed. I've never used
such software, and I'm not sure that it could do such a downtown job as the
code you supplied.

Where did you get this code?

Dave.
 
D

dcorbit

This should be enough to make it obvious:

/* file:eek:bfusc.c */
static const char *string1 = "%s %d %d\n"; /* no printf(), so not a
format string -- that's a red herring. */
static const char *string2 =
"@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;# ){nl]!/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'
')# }'+}##(!!/";
static const char *string3 = "%s"; /* no printf(), so not a format
string */
static const char *string4 = "!ek;dc
i@bK'(q)-[w]*%n+r3#l,{}:\nuwloca-O;m .vpbks,fxntdCeghiry";
#include <stdio.h>
int gifts(int t, int u, char *a) {
return !0 < t ? t < 3 ? gifts(-79, -13, a + gifts(-87, 1 - u,
gifts(-86, 0, a + 1) + a))
: 1, t < u ? gifts(t + 1, u, a)
: 3, gifts(-94, -27 + t, a) && t == 2 ? u < 13 ? gifts(2, u +
1, string1)
: 9
: 16
: t < 0 ? t < -72 ? gifts(u, t, string2)
: t < -50 ? u == *a ? putchar(31[a]) /* identical to
putchar(a[31]) -- this is where stuff get spit out */
: gifts(-65, u, a + 1)
: gifts((*a == '/') + t, u, a + 1)
: 0 < t ? gifts(2, 2, string3)
: *a == '/' || gifts(0, gifts(-61, *a,
string4), a + 1);
}

int main(int argc, char **argv)
{
return gifts(argc, argc, argv[0]);
}

/*
Consider how recursion and a simple table might be helpful, given this
hint:
"On the <12> day of Christmas my true love gave to me
<12> drummers drumming, <11> pipers piping, <10> lords a-leaping,
<9> ladies dancing, <8> maids a-milking, <7> swans a-swimming,
<6> geese a-laying, <5> gold rings;
<4> calling birds, <3> french hens, <2> turtle doves
and a partridge in a pear tree."
*/
 
G

Guest

Keith said:
C99 says that an implementation may define other forms of main(), but
that permission is redundant given C99 4p6:

A conforming implementation may have extensions (including
additional library functions), provided they do not alter the
behavior of any strictly conforming program.

C90 has the same wording.

Using that wording, I don't believe there is a guarantee that execution
of the program starts with main(). It allows a declaration of int
main(int, int, char *) to really mean that execution starts at start().
(It would be a rather silly extension, of course.)
For that matter, a C99 implementation could provide implicit int as an
extension, though it would still be required to issue a diagnostic (in
conforming mode) for any use of it.

It could, but the behaviour would not be defined by the C standard. As
a result, it could provide the syntax of implicit int to mean implicit
void. And in that case, I'm not so sure it would be a silly extension.
 
R

Richard Heathfield

Keith Thompson said:

C99 says that an implementation may define other forms of main(), but
that permission is redundant given C99 4p6:

A conforming implementation may have extensions (including
additional library functions), provided they do not alter the
behavior of any strictly conforming program.

C90 has the same wording.

So you are claiming that the behaviour of the program is - what?
Well-defined? Clearly not, for various reasons (not least of which is that
it violates at least one "shall" that is not a constraint).
Implementation-defined? Clearly not, or my implementation would have to
document what it makes of int main(int, int, char *). Unspecified? Clearly
not, since this classification only applies to correct behaviour, and it is
obvious that any compiler is allowed to reject the program as incorrect.
Locale-specific? Clearly not, since - again - the implementation would have
to document it. Doesn't leave a lot of scope, does it? The only behaviour
classification I haven't mentioned is of course "undefined".
 
K

Keith Thompson

Richard Heathfield said:
Keith Thompson said:


So you are claiming that the behaviour of the program is - what?
Well-defined? Clearly not, for various reasons (not least of which is that
it violates at least one "shall" that is not a constraint).
Implementation-defined? Clearly not, or my implementation would have to
document what it makes of int main(int, int, char *). Unspecified? Clearly
not, since this classification only applies to correct behaviour, and it is
obvious that any compiler is allowed to reject the program as incorrect.
Locale-specific? Clearly not, since - again - the implementation would have
to document it. Doesn't leave a lot of scope, does it? The only behaviour
classification I haven't mentioned is of course "undefined".

It's undefined *unless* the implementation meets certain conditions.
 
R

Richard Heathfield

Keith Thompson said:
It's undefined *unless* the implementation meets certain conditions.

That's also true of fflush(stdin), j = ++i + i++, and so on and so forth.
Would you argue that those, too, are undefined unless the implementation
meets certain conditions?

So what do we mean by "the" implementation? The code was posted in
comp.lang.c, where it is reasonable to expect that quite a few people using
quite a few different compilers will have a go at compiling it. So there
isn't really any such thing as "the" implementation, although of course we
all use the phrase.
 
K

Keith Thompson

Richard Heathfield said:
Keith Thompson said:

That's also true of fflush(stdin), j = ++i + i++, and so on and so forth.
Would you argue that those, too, are undefined unless the implementation
meets certain conditions?

I think you're taking my pedantic quibble too seriously. :cool:}
 
L

Leslie Kis-Adam

This should be enough to make it obvious:

/* file:eek:bfusc.c */
static const char *string1 = "%s %d %d\n"; /* no printf(), so not a
format string -- that's a red herring. */
static const char *string2 =
"@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;# ){nl]!/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'
')# }'+}##(!!/";
static const char *string3 = "%s"; /* no printf(), so not a format
string */
static const char *string4 = "!ek;dc
i@bK'(q)-[w]*%n+r3#l,{}:\nuwloca-O;m .vpbks,fxntdCeghiry";
#include <stdio.h>
int gifts(int t, int u, char *a) {
return !0 < t ? t < 3 ? gifts(-79, -13, a + gifts(-87, 1 - u,
gifts(-86, 0, a + 1) + a))
: 1, t < u ? gifts(t + 1, u, a)
: 3, gifts(-94, -27 + t, a) && t == 2 ? u < 13 ? gifts(2, u +
1, string1)
: 9
: 16
: t < 0 ? t < -72 ? gifts(u, t, string2)
: t < -50 ? u == *a ? putchar(31[a]) /* identical to
putchar(a[31]) -- this is where stuff get spit out */
: gifts(-65, u, a + 1)
: gifts((*a == '/') + t, u, a + 1)
: 0 < t ? gifts(2, 2, string3)
: *a == '/' || gifts(0, gifts(-61, *a,
string4), a + 1);
}

int main(int argc, char **argv)
{
return gifts(argc, argc, argv[0]);
}

/*
Consider how recursion and a simple table might be helpful, given this
hint:
"On the <12> day of Christmas my true love gave to me
<12> drummers drumming, <11> pipers piping, <10> lords a-leaping,
<9> ladies dancing, <8> maids a-milking, <7> swans a-swimming,
<6> geese a-laying, <5> gold rings;
<4> calling birds, <3> french hens, <2> turtle doves
and a partridge in a pear tree."
*/

Tks very much!
Laszlo Kis-Adam
 
L

Leslie Kis-Adam

David said:
Two things you should know:

#1: There is some sort of an obfuscated 'C' contest, see here:

http://www.ioccc.org/main.html

The winning entries from previous years are quite ... obfuse.

#2: Some companies make software that will transform 'C' code into code
that compiles equivalently for the purpose of protecting intellectual
property when source that compiles must be distributed. I've never used
such software, and I'm not sure that it could do such a downtown job as the
code you supplied.

Where did you get this code?

Dave.

Tks for the hints. Actually I've got a hint from my lecturer Phd. Andras
Poppe, that there is a code like this, and one of my classmates shared
it on a forum.

Laszlo Kis-Adam
 
L

Leslie Kis-Adam

Robbie said:
Leslie-and-or-Lazlo Kis-Adam said:
Can anyone explain me this program?
(snip obfuscated source code)

Looks like an entry to the "Obfuscated C Contest". (Google that
if you don't know what it is.)
I'm getting really confused.

Obfuscation is the whole idea. You didn't think that C
programmers actually write code like that on the job, did you?
(Well, there's a certain ex-coworker of mine who used to write
code ALMOST that obfuscated... but he got fired for it, and
rightly so.)

If you really, really want a disection of this beast, I'm afraid
you'll have to pester someone else here into giving you one; I
don't torture my brain that way these days. I do structured
programming, not demolished programming.
thx in advance.

You're welcome, in retrospect.

Cheers,
Robbie Hatley
lone wolf intj at pac bell dot net
(Put "[usenet]" in subject to bypass spam filter.)

Well, thx for NOT torturing your brain! :D
Actually,
Leslie Kis-Adam
Laszlo Kis-Adam
László Kis-Ádám
Kis-Adam Laszlo
Kis-Ádám László
are the same.
László, or Laszlo means Leslie in english.
My real or original name is: "Kis-Ádám László" in this order.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,141
Latest member
BlissKeto
Top