:..net

UNIX C

Home Page
Domains
Software
Perl Hacker
UNIX C
Matthew B-M
Links

As well as Perl, I often use C. I have lots of code fragments around, but they are merely ideas, often.

I have co-written some obfuscated code in C. It's not IOCCC worthy, but it's vaguely interesting in itself.

#define _l typedef
#define _I putchar(
#define _(__) (ll[ll[l1+(__)]-'!'])
_l char _1;__(ll,l1) _1 *ll,l1;{(ll
[1+l1]=='H')?_I _(2)-_(3)+'!'):(_(1
)=_(2)-_(3)+'!'); return((_(2)==_(3
))?ll[l1]-'!':l1+4);}main(){_1 ll=0
;_1 l1[]="Matthew Byng-Maddick & A"
"ndrew Collier say FISH!_WOOF GOTH"
"Y_AMY!.AMY? WHO THE **** IS AMY?";
while((ll=__(l1,ll))!=-1);_I'\n');}

We wrote the program as a sort of challenge - you have to work out what it will do without compiling and running it. And then work out what the point of the algorithm we were using was.

The point is that it is a single instruction virtual processor, using an ascii text string as its memory.

The macro _ does the pointer dereference of a given instruction. The function __ takes a pointer to the memory base and its current program counter, and implements the single instruction, returning the new program counter, or -1 to halt.

 

For a previous employer, I also wrote the following code (which I'd hoped to put on a company t-shirt):

                            A(C ,N)                                     {;;
                            for (;N                                     --;
  )/*A       BC*/       printf(     "%"   "c"     ,C) ;1;}      char    *S=
"\\CACeC   @\\CACeC   @BDGDGGEC CCE  CAD FDD      F@!HCHCIAC  BCACFJBH  BF@!C"
"CC   ACD CAC    DCA CCE    GDC CAD   BDAC@       !CGC   DCA CDCA  CDCH CDCAJ"
"AC       @!C    CCA CDC    ACD CAC    CEG        CDC    ACH C@!HCHCIAC BC"
"AC   BCA CDC    BHB F@B    DGD GGA   CACCC       ACA    CDC DDE        E@"
,D=1,C;;   main(){;   for(;C=*S ;1,  S++ )C^  040 &&(    64^  C?C^33?A  (32+(D
  =!D)       *10,       C-64):( D=! D):   A(9 +1, 1))    ;/*    CODI     X.*/}

which will print out:

                            *** ***                                     ***
                            *** ***                                     ***
  ****       ****       *******     ***   ***     *** ****      ****    ******
********   ********   ********* ***  *** ***      **********  ********  ******
***   *** ***    *** ***    *** ***   *****       ****   *** ****  **** ***
***       ***    *** ***    *** ***    ***        ***    *** ********** ***
***   *** ***    *** ***    *** ***   *****       ***    *** ***        ***
********   ********   ********* ***  *** ***  *** ***    ***  ********  ******
  ****       ****       ******* *** ***   *** *** ***    ***    ****     *****

when run.

All in all, it's a fairly simple program, the string is just runlength encoded, and N x C characters are printed out by the A function. It relies on the fact that 32 and 42 are " " and "*" respectively. An '@' character signifies a newline (A(9 +1, 1)).

There is a version of it with syntax highlighting if you're having problems following it.

 

I also wrote the following quine, for no particularly good reason:

char *rot13(char *s, char *f) {
  char *u=f;
  while(*s)
    *(f++)=(*s>='a'&&*s<='m')?(*(s++)+13):((*s>='n'&&*s<='z')?(*(s++)-13):*s++);
  *f=0;
  return u;
}

int main(int c, char **v) {
  char s[]="pune *ebg13(pune *f, pune *s) {
  pune *h=s;
  juvyr(*f)
    *(s++)=(*f>='n'&&*f<='z')?(*(f++)+13):((*f>='a'&&*f<='m')?(*(f++)-13):*f++);
  *s=0;
  erghea h;
}

vag znva(vag p, pune **i) {
  pune f[]=%p%f%p,s[1024];
  cevags(ebg13(f,s),34,f,34,10);
}%p",f[1024];
  printf(rot13(s,f),34,s,34,10);
}

It's fairly standard, other than the use of the rot13 in the middle, and as with the codix program, there is a syntax highlit version if you are having trouble seeing why it works.

Valid XHTML 1.0!

This page last modified on Monday, 05-Feb-2018 19:43:34 UTC
Contact <webmaster@colondot.net> for more information about this site, or <plunder@colondot.net> if you want not to be able to send any more mail to this machine.