Results 1 to 35 of 35

Thread: Self Reproducing Code

  1. #1

    Thread Starter
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849

    Self Reproducing Code

    Does anyone here have any VB sample of such code?

    C Code given below
    Code:
    main(){char q=34,n=10,*a="main(){char q=34,n=10,*a=%c%s%c;printf(a,q,a,q,n);}%c";printf(a,q,a,q,n);}

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109
    That doesn't look like it does much. You declare a few variables, one of which is a long string variable, then print it out. Big deal. What does it do that is surprising?

  3. #3

    Thread Starter
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    Compile it and run. The Output is the exact, complete source. The code re-produces itself.

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109
    Yeah, but it is a trivial example. That could be easily done in VB, all you have to do is take your entire source code, copy it, enclose the copy in quotes, and assign it to a variable. You would lose some of the formatting, but if you re-install the formatting during printing, you'd have it all.

  5. #5

    Thread Starter
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    Could you, if you do not mind, please whip up something for me? Please.

    Remember, each and every compilation of the output must produce the original source code as the output.

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109
    Ok now, THAT would be a a neat trick. The code you posted only prints it's own source for that particular program. I see only one way to do that in VB, and that would be to write something that opened and printed it's own source files. Without doing that, which is no more difficult than printing any other file, I don't know a way to do it.

    It would be possible to so something like the code you posted where for any particular code, it would print itself, but for each change to the code, you'd also have to be sure to change the part that does the printing, an annoying and tedious process.

  7. #7
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    I'd like to see that in JavaScript, and the entire thing enclosed in eval()


  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109
    What does that do?

  9. #9
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Eval() evaluates whatever's in the bracket. I'm thinking that could set up a recursive, infinite loop, causing the universe to collapse into itself.

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109
    Actually, it would just run twice.

  11. #11
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Don't ruin my fantasy.

  12. #12

    Thread Starter
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    JavaScript

    Code:
    <html>
    <head>
    <script language="JavaScript">
    a=new Array();a[0]='a=new Array();'; a[1]='['; a[2]=']'; a[3]='\''; a[4]='\\'; a
    [5]='='; a[6]='a'; a[7]=';'; a[8]=''; a[9]='for(i=0;i<10;i++)document.writeln((i
    ==0?a[0]:a[8])+a[6]+a[1]+i+a[2]+a[5]+a[3]+((i==3||i==4)?a[4]:a[8])+a[i]+a[3]+a[7
    ]+(i==9?a[9]:a[8]))';for(i=0;i<10;i++)document.writeln((i==0?a[0]:a[8])+a[6]+a[1
    ]+i+a[2]+a[5]+a[3]+((i==3||i==4)?a[4]:a[8])+a[i]+a[3]+a[7]+(i==9?a[9]:a[8]))
    </head>
    </html>

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  13. #13

    Thread Starter
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    Almost every language is capable of it. I have samples from others, created my own, in almost all programming language, including BASIC! But I'm comletely unable to do it in VB. Here is a BASIC Sample. I don't have a compiler on hand. Run it and see it if it can be translated into VB.
    Code:
    a$="?chr$(97)chr$(36)chr$(61)chr$(34)a$ chr$(34)chr$(58)a$":?chr$(97)
    chr$(36)chr$(61)chr$(34)a$ chr$(34)chr$(58)a$
    and another one
    Code:
    10 READ A$:PRINT 10 A$:PRINT 20 "DATA" A$
    20 DATA READ A$:PRINT 10 A$:PRINT 20 "DATA" A$

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  14. #14
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109
    But all you are saying is something like this:

    VB Code:
    1. public sub Main()
    2.  Dim i as integer
    3.  
    4.  'Do something to print this
    5.  Print "public sub Main()
    6.  Dim i as integer
    7.  
    8.  'Do something to print this
    9.  Print "
    10.  
    11. end sub"
    12.  
    13. end sub

  15. #15
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109
    Ok, there would have to be a little formatting, but that print comand is bogus anyways. Still, assuming you could write a print function that would adequately format the code, this would work....but only for this specific example. I know of no way to write a program such that it always prints it's own source code, regardless of innocent changes made to the source code (by innocent, I mean any change that wouldn't otherwise cripple the program). The examples you post don't have that ability, either.

  16. #16

    Thread Starter
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    Yes, only for a "specific" example, as you put. That is what I am after. You cannot, IMHO, make a generic one.

    The whole point is to prove that Code-is-Data-is-Code for any arbitary Programming Language.

    Actually, my point is, it's just fun.

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  17. #17
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109
    So, did I come close enough?

  18. #18
    Member
    Join Date
    Feb 2004
    Posts
    46
    Hmm, that's a neat trick. Kind of pointless if you ask me, but neat anyways
    writing software in C++ is like writing software in VB, except that it's slower and harder and more frustrating and more finicky and doesn't give you nice convenient shortcuts on a silver platter. And it's better. *puts on heat-reflecting armor*

  19. #19
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    Originally posted by Shaggy Hiker
    So, did I come close enough?
    shaggy, why not try making a full code that can print itself?

    you never tried it, yet you keep saying it is easy as writing a print statement.
    Remember, if someone's post was not helpful, you can always rate their post negatively .

  20. #20
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    I think it's impossible in VB. The languages that can uses pointers (The C code references back to itself), or tricks in the language:

    Code:
    <script language="Javascript">
    function a() {
        document.write(a, "a()");
    }
    a()
    </script>
    Last edited by BuggyProgrammer; Apr 14th, 2004 at 08:56 PM.
    Remember, if someone's post was not helpful, you can always rate their post negatively .

  21. #21

    Thread Starter
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    Have a Unix box? Do you have an account on a Unix box? The "login" program contains a "trojan" by no less a person than Ken Thompson, the co-creator of Unix and the neatest quine (self-reproducing code) ever to have concieved. 20 years ago, this happened.

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  22. #22
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109
    Ok, I'll change my example:

    VB Code:
    1. Public Sub Main()
    2.  dim i as integer
    3.  dim str as string
    4.  
    5.  str="Public Sub Main()
    6.  dim i as integer
    7.  dim str as string
    8.  
    9.  str=
    10.  
    11.  printF str
    12.  
    13. End Sub"
    14.  
    15.  
    16.  printF str
    17.  
    18. End Sub

    Now, that doesn't quite work as it stands, but it can be made to work based on the implementation of printF (which is defined in the standard C library, but that's unimportant, since it isn't in VB, so I can write my own).

    If you look at the original example, a* is just a string variable. However, the implementation of printF in C will decode the string to be containing the information needed. We can certainly implement the same thing in VB where you pass in a string to a function which parses the string, and if it finds X, replaces X with the argument passed in.

    In other words, KayJay's original post is only recursive because of the character of printf, but printf doesn't do magic, it just parses it's variables. That can be replicated in VB.

  23. #23
    Fanatic Member
    Join Date
    Feb 2003
    Location
    C:\Windows\Microsoft.NET\Framework
    Posts
    574
    Hey dude!

    The only, only way to do it is to print out the source code file. It is also one of the begining examples in K&R right in the first chapter if you recall?

  24. #24
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    I did it!!

    It's messy but it works... could be shorter
    VB Code:
    1. ...snip...

    EDIT: Probably better to use Debug.Print to be able to copy and paste the results:
    VB Code:
    1. ..snip...
    Last edited by BuggyProgrammer; Apr 16th, 2004 at 05:42 PM.
    Remember, if someone's post was not helpful, you can always rate their post negatively .

  25. #25
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    Sorry about screwing up the width of the page

    I got it down to 406 characters (minus ten if you replace debug.print with Msgbox, minus 30 if you replace the function name PrintF with a single character, like 'a'):

    VB Code:
    1. snippppppppppp

    EDIT: shorter (~280 chars), and i submitted it here: http://www.planetsourcecode.com/vb/s...53203&lngWId=1
    VB Code:
    1. Sub Main(): Debug.Print a("Sub Main(): Debug.Print a(%): End Sub|Function a(t): z = Chr(34): a = Replace(Replace(t, Chr(124), vbCrLf), Chr(37), z & t & z): End Function"): End Sub
    2. Function a(t): z = Chr(34): a = Replace(Replace(t, Chr(124), vbCrLf), Chr(37), z & t & z): End Function


    also includes a neater version
    VB Code:
    1. Sub Main()
    2.     Debug.Print a("Sub Main()|Debug.Print a(%)|End Sub|Function a(t)|z = Chr(34)|a = Replace(Replace(t, Chr(124), vbCrLf), Chr(37), z & t & z)|End Function")
    3. End Sub
    4.  
    5.  
    6. Function a(t)
    7.     z = Chr(34)
    8.     a = Replace(Replace(t, Chr(124), vbCrLf), Chr(37), z & t & z)
    9. End Function
    Last edited by BuggyProgrammer; Apr 16th, 2004 at 09:14 PM.
    Remember, if someone's post was not helpful, you can always rate their post negatively .

  26. #26

    Thread Starter
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    Lovely! Good work, Buggy.

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  27. #27
    Addicted Member danielkw's Avatar
    Join Date
    Mar 2000
    Location
    Sweden
    Posts
    134
    whats the point with printing your own source code if it's hardcoded? thats no fun at all. I mean take a look at this example:
    VB Code:
    1. Public Sub Main
    2.     Print "Public Sub Main" & vbcrlf & _
    3.             "End Sub"
    4. End Sub

    this is wrong! if you compare the "source code" being printed to the complete source code you will notice that the "Print..." line is excluded. If you include this line in the "source code" print you will soon notice that it will become an endless line since you can't include everything without printing everything (which then will become everything * 2). Maybe I'm no good at explaining this but any decent person will get the idea.
    The only real way to print your source code is to make the program open its exe-file and translate the binary into real source code.
    Everyone knows my profile

  28. #28
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    Originally posted by danielkw
    whats the point with printing your own source code if it's hardcoded? thats no fun at all. I mean take a look at this example:
    VB Code:
    1. Public Sub Main
    2.     Print "Public Sub Main" & vbcrlf & _
    3.             "End Sub"
    4. End Sub

    this is wrong! if you compare the "source code" being printed to the complete source code you will notice that the "Print..." line is excluded. If you include this line in the "source code" print you will soon notice that it will become an endless line since you can't include everything without printing everything (which then will become everything * 2). Maybe I'm no good at explaining this but any decent person will get the idea.
    The only real way to print your source code is to make the program open its exe-file and translate the binary into real source code.
    Uhh... did you look at my example? Just run it, then copy and paste the code from the immediate window and paste it back into the code editor. It would run exactly the same (and should look the same).

    Oh, and if you don't know how to run my code, just follow these steps:

    1. New project
    2. Delete the form (right-click in the project explorer and click delete)
    3. Add a Module (.bas)
    4. Paste the code in
    5. Go to Project->Project1 Properties
    6. Startup object set the Sub Main()
    7. Close. Turn on the immediate window (Ctrl-G or View->immediate window)
    8. Run the code

    Now you would see that the output is the same as the source code used to print its own code . You can copy and paste the output back, replacing everything in the code and it will still work.
    Last edited by BuggyProgrammer; Apr 17th, 2004 at 11:59 AM.
    Remember, if someone's post was not helpful, you can always rate their post negatively .

  29. #29
    Hyperactive Member DovyWeiss's Avatar
    Join Date
    Feb 2002
    Location
    Miami Beach, FL
    Posts
    366
    To all readers:

    First of all, BuggyProgrammer's code was excellent! But I have things to say...

    I would like to say that the first example in C is perhaps not a self-replicating program. It relies on the printf() function in C (which indeed makes the example possible), which is included in stdio.h if I am not mistaken. In order for this program to be called a self-replicating program, it would also have to output the printf() function to the console, which it does not.

    If such logic were sound, that we only need to recreate the main function but not anything that it relies upon, then making such a program would be extremely easy in VB:

    VB Code:
    1. 'Main module:
    2. Sub Main()
    3.   Print GetMyCode()
    4. End Sub
    5.  
    6. 'In another module, a library, which will
    7. 'not be reproduced, just like the printf():
    8. Function GetMyCode()
    9.   GetMyCode = "Sub Main()" & vbCrLf & "  Print GetMyCode()" & vbCrLf & "End Sub"
    10. End Function

    As a matter of fact, you could skip the function and make it a Const! And the program would reproduce its Main() function, but NOT ANYTHING ELSE.

    If so, then BuggyProgrammer's code is indeed better than the original C++'s code, because it produces the "a" function as well! (Well, in truth, it relies on the Replace() function as well as the Chr() function, and even the & operator, but the former could be reproduced quite easily {it wasn't even in VB5} and that other two are pretty low level, and if we didn't let that, then we'd have to write the whole thing in binary format (or Assembly, I'll be nice).

    When I read part of this thread I wrote almost exactly BuggyProgrammer's code. It was an obvious (and yet non-obvious) concept, to hardcode the code but have it once formatted, and then once without formatting, in the original hardcoded form. However Buggy, like me and probably others, realized that if you want to Print the second function, you would have to write it differently so it wouldn't parse itself, but only the original code. Buggy solved it using Chr() characters in the function as opposed to the characters themselves. I thought of this too, but wondered if there were more ways to solve it. I realized that it could be solved also by using a ReplaceOnce function that I made (like Replace, but only replaces the first instance in the string). I then further created an example using another function I created that injects a string into itself at position n, (and doubles up any quotes it finds, so VB can handle it) which would also work.

    This is a nice way for programmers to exercise their skills, but quite pointless...

    I wonder if any VBS malware programs (I'm not saying the v-word) use this technique to copy their own code into another VBS files. It would work, but it would be hard to write, because you have to duplicate the entire function in the string. (It would also be useless as you can probably access VBS code while Windows runs it just as any other program.) DON'T TRY IT.

    On that note, I would like to post what I think is the shortest possible example of such a program, and does not rely on ANY other functions.
    Great, no? Whatever output you get will be EXACTLY the source code. This example may not work in VB which requires a Sub Main(), but it would work in VBScript, right?.

  30. #30
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    Well if you had a form, then it could be empty .

    Although technically it didn't output anything.
    Remember, if someone's post was not helpful, you can always rate their post negatively .

  31. #31
    Addicted Member danielkw's Avatar
    Join Date
    Mar 2000
    Location
    Sweden
    Posts
    134
    Originally posted by DovyWeiss
    you forgot to post your code...
    Everyone knows my profile

  32. #32
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    Originally posted by danielkw
    you forgot to post your code...
    That's the trick. The source code is = "". But then it's not really print it.....
    Remember, if someone's post was not helpful, you can always rate their post negatively .

  33. #33
    Addicted Member danielkw's Avatar
    Join Date
    Mar 2000
    Location
    Sweden
    Posts
    134
    aha! I C funny
    Everyone knows my profile

  34. #34
    Hyperactive Member DovyWeiss's Avatar
    Join Date
    Feb 2002
    Location
    Miami Beach, FL
    Posts
    366
    Okay, I'm sorry, I thought that everybody would understand that I meant an empty program's output will always equal its source code...

    The code was purposely left blank. I should have been a little more obvious. Sorry.

  35. #35
    Addicted Member danielkw's Avatar
    Join Date
    Mar 2000
    Location
    Sweden
    Posts
    134
    no one can help that I'm stupid...
    Everyone knows my profile

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width