Search code examples
phpcpolyglot

Polyglot for C and PHP


I was thinking of a compiler that spits out C/PHP polyglots, but not sure if possible. Main issue is the PHP opening/closing brackets, <?php and ?>. I wanted to make them as macros that expand to nothing, but macros can't have such identifiers. Any way around this?

#define <?php
#define ?>
#define $

The polyglot does not need to be readable, semi-readable is good enough. Thankful for any feedback!


Solution

  • One small example:

    //<?php echo "\x08\x08"; ob_start(); ?>
    #include <stdio.h>
    #define function 
    
    int
    // <?php
    function main(int $x)
    {
        printf("Hello %d\n", $x);
    }
    
    // ?>
    // <?php ob_end_clean(); main(1);
    

    And with C syntax highlight (but same piece of code):

    //<?php echo "\x08\x08"; ob_start(); ?>
    #include <stdio.h>
    #define function 
    
    int
    // <?php
    function main(int $x)
    {
        printf("Hello %d\n", $x);
    }
    
    // ?>
    // <?php ob_end_clean(); main(1);
    

    Edit: Well the int function type won't work. Right now, I'm using #__C__ prefix on such lines, and then sed to remove it before compiling. So not 100% polyglot, sadly. ^^