Generic C / Python Polyglot

A polyglot — coming from the Greek words πολύς (many) and γλώττα (tongue) — is a piece of source code which can run in multiple languages, often performing language-dependent tasks.
Even though such a source code’s feature may not seem particularily useful or possible with certain language combinations, trying to bend not one but multiple language’s syntactic rules and overall behavior is still interesting.

An example of a rather simple polyglot would be a Python 2 / Python 3 polyglot — if one counts those as two separate languages. Because of their enormous similarities, one can pick out differences and use those to determine which language runs the source code.

if 1/2>0:print("Python 3")
else:print("Python 2")

Utilizing Python’s version difference regarding integer division and real division is one of the most common ways to tell them apart, as it can also be used to only control a specific part of a program instead of having to write two nearly identical programs (increases the program’s style and cuts on bytes — an important consideration if one golfs a polyglot).

However, polyglots combining languages that are not as similar as Python 2 and Python 3 require more effort. The following is a general Python 2 / C polyglot, meaning that nearly all C and Python 2 programs can be mixed using this template (there are a few rules both languages need to abide which will come apparent later).

#define _\
"""
main(){printf("C");}
#define _"""
#define/*
print"Python 2"
#*/_f

In the above code, main(){printf("C");} can be nearly any C code and print"Python 2" can be nearly any Python 2 code.
Language determination is exclusively done via syntax. A C compiler sees a #define statement and a line continuation \, another two #define statements with a block comment in between and actual compilable C source code (view first emphasis).
Python, on the other hand, treats all octothorps, #, as comments, ignoring the line continuation, and triple-quoted strings, """...""", as strings rather than statements and thus only sees the Python code (view second emphasis).

My first ever polyglot used this exact syntactical language differentiation and solved a task titled Life and Death of Trees (link to my answer).

Advertisements

BMP implementation in C

C is one cool and important language. CPython and Unix are based on it, the Mars Curiosity rover is run by it and even the GCC C compiler itself is written in C. However, as C is some years old by now, it lacks a lot of higher-level features most modern languages possess, being more down to the silicon, as the cool kids say. Concepts like pointer manipulation, bit fiddling and its string implementation — just to name a few — are at times cumbersome, insecure and error-prone; nevertheless is there a certain appeal to writing in C.

Being only one abstraction level away from Assembly — which itself is only one abstraction level above raw byte code — and having access to file manipulation down to the individual bit, I set out to write a Microsoft Bitmap (.bmp) implementation in pure C. As Microsoft’s standard for this image file format is quite feature-rich, I decided to focus on the bar minimum — a bitmap with 24-bit color depth (three colors, one byte per), one color plane, no compression, no palette and 300 DPI.
My Bitmap implementation supports both reading and writing .bmp files, as well as generating some test images — including a Mandelbrot Set fractal renderer, of course. Implementation source code can be downloaded (bmp.c) or seen below.

Mandelbrot Set fractal
A Mandelbrot Set fractal rendering.

Implementing a file format requires knowing its specification. Although it is not the best article I have ever seen, this Wikipedia article gave me some insights. The missing pieces were reverse engineered using Adobe Photoshop CC and the HxD hex editor.
The following is a snippet of the implementation’s savebmp function (full source code listed below). It illustrates the Bitmap file’s byte layout only showing the file header, omitting a lengthy data part concatenated to the header. S, K, W, H and B are all byte arrays of length four (little-endian format) which contain the file’s total size, the bitmap data offset (which is constant, since the header is always exactly 54 bytes large), the image’s dimensions (horizontal and vertical) and the bitmap data’s section’s size, respectively.

/*  bitmap file header  */
0x42, 0x4D,             // BM
S[0], S[1], S[2], S[3], // file size
0x00, 0x00, 0x00, 0x00, // unused
K[0], K[1], K[2], K[3], // bitmap data offset
/*      DIB header      */
0x28, 0x00, 0x00, 0x00, // DIB size
W[0], W[1], W[2], W[3], // pixel width
H[0], H[1], H[2], H[3], // pixel height
0x01, 0x00,             // one color plane
0x18, 0x00,             // 24 bit color depth
0x00, 0x00, 0x00, 0x00, // no compression
B[0], B[1], B[2], B[3], // bitmap data size
0x23, 0x2E, 0x00, 0x00, // 300 DPI (horizontal)
0x23, 0x2E, 0x00, 0x00, // 300 DPI (vertical)
0x00, 0x00, 0x00, 0x00, // no palette
0x00, 0x00, 0x00, 0x00  // color importance
/*  data bytes follow   */

Key bytes to note are the first two identifying the file type (the ASCII-encoded letters BM) and the DPI bytes, 0x23, 0x2E, which indicate 0x00002E23 = 11811 pixels per meter in both the horizontal and vertical direction. Converting from pixels per meter to dots per inch results in 11811 / (1 meter / 1 inch) = 11811 * 127 / 5000 = 300 DPI (roughly).
Most values are represented using four bytes in little-endian format. Translating an 32-bit integer into four little-endian formatted bytes can be achieved as follows.

/* unsigned 32-bit integer */
unsigned int n = 0b10100100010000100000100000010000;
/*                 < m sig><sm sig><sl sig>< l sig> */

/* byte (unsigned char) array of size four */
unsigned char N[4] = {          
	(n & 0xff000000) >>  0, // most significant byte
	(n & 0x00ff0000) >>  8, // second most significant byte
	(n & 0x0000ff00) >> 16, // second least significant byte
	(n & 0x000000ff) >> 24  // least significant byte
};

Other than rendering a fractal, I also implemented three nested loops which output an image containing every possible color exactly once ((2**8)**3 = 16777216 pixels in total).

All sixteen million colors
All sixteen million colors in one image.

An image’s data type is implemented as a struct image which contains three variables — width and height, two integers specifying the image’s dimensions, and *px, a pointer to an one-dimensional integer array of size width*height which holds the entire image data.
Defined functions are listed ahead.

  • image * readbmp(char []);
    • Reads an image specified by a file name. If reading fails, a NULL pointer is returned.
  • void savebmp(image *, char []);
    • Saves given image to a file with specified name.
  • image * newimage (int, int);
    • Returns a pointer to an image struct with specified dimensions (image will be filled with nothing but black pixels).
  • void freeimage (image *);
    • Frees an image struct’s memory.
  • int getpx (image *, int, int);
    • Returns the pixel color at specified coordinates.
  • void setpx (image *, int, int, int);
    • Sets the pixel color at specified coordinates.
  • void fill (image *, int);
    • Fills a given image with a given color (all pixels are set to specified color).
  • int color(byte, byte, byte);
    • Returns a 32-bit integer representing a color specified by three bytes (byte is defined through typedef unsigned char byte;).
  • int hsl (double, double, double);
    • Returns a 32-bit integer representing a color specified by three doubles in the HSL color format.

Images shown in this post were converted to .png files as WordPress does not allow .bmp file uploads; the raw pixel data should, however, be identical.


/* ================================================== *
 *                GENERAL INFORMATION                 *
 * ================================================== *
 * Bitmap file format implementation in C.            *
 * Supported functionality: 24-bit color depth image  *
 *  struct, reading and writing .bmp files and simple *
 *  image manipulation.                               *
 * Supported color formats: RGB and HSL.              *
 * Additional functionality: Mandelbrot Set fractal   *
 *  rendering.                                        *
 *                                                    *
 * Author: Jonathan Frech                             *
 *                                                    *
 * Edit history: 23rd, 24th, 27th, 28th, 29th, 30th   *
 *  of June, 1st, 2nd, 3rd, 10th, 11th, 13th, 14th,   *
 *  15th, 16th, 17th, 18th, 19th, 20th, 25th, 26th,   *
 *  27th, 29th of July, 11th, 16th of August, 17th,   *
 *  18th, 19th of October 2017                        */
 
/* ================================================== *
 *                    COMPILATION                     *
 * ================================================== *
 * $ rm bmp; gcc bmp.c -lm -o bmp; ./bmp              */

Continue reading

Tau Day MMXVII

Today it is June the 28th which means that it is \tau day!
The irrational and transcendental constant \tau is what defines \pi = \frac{\tau}{2}, which obviously makes it an important constant. To celebrate this day, I created a C program which calculates \tau by randomly creating 9-dimensional points inside the 9-dimensional hypercube and testing if they are inside the 9-dimensional hypersphere with its center located at (0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5).

Today’s \tau time is 3:18:53 as \tau = 6.2831853 \dots. As one does not know if the time is specified as ante or post meridiem, there are actually two perfectly acceptable \tau times.

            ;b$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$b
        h$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ 
     .$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$+ 
    $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$  
  ,$$$$$$$$$kn}~I"`($$$$$$$x````````````````   
 )$$$m             $$$$$$$$                    
+$$c              q$$$$$$$>                    
$$                $$$$$$$$                     
                 $$$$$$$$.                     
                '$$$$$$$$                      
                $$$$$$$$                       
               ?$$$$$$$a                       
               $$$$$$$$                        
              Q$$$$$$$f                        
              $$$$$$$$                         
             $$$$$$$$i                         
             $$$$$$$$                          
            $$$$$$$$;                          
            $$$$$$$$                           
           J$$$$$$$w                           
           $$$$$$$$;             $$~           
           $$$$$$$$             $$$            
          '$$$$$$$$C          "$$$             
           $$$$$$$$$w       '$$$$              
           $$$$$$$$$$$$$$$$$$$$B               
            $$$$$$$$$$$$$$$$$$.                
             $$$$$$$$$$$$$$$'                  
                8$$$$$$$q                      

The formula used for calculating \tau is derived from a 9-dimensional hypersphere’s hypervolume formula V = \frac{32 \cdot \pi^4}{945} \cdot R^9 (see this Wikipedia article).
V = \frac{2^5 \cdot \tau^4 \cdot R^9}{945 \cdot 2^4} = \frac{2 \cdot R^9}{945} \cdot \tau^4
\tau^4 = \frac{V \cdot 945}{2 \cdot R^9}; R = 0.5
\tau^4 = \frac{V \cdot 945 \cdot 2^9}{2}
\tau = \sqrt[4]{V \cdot 241920}

The constant gets calculated to \tau^* = 6.293700. The real value is approximately \tau = 6.283185 \dots, which makes the percent error \left|\frac{\tau^*-\tau}{\tau}\right| = \left|\frac{6.293700-6.283185}{6.283185}\right| = 0.001673514 = 0.167\%.
Thereby this C program’s approximation is not too far off. The source code is listed below and can also be downloaded here. Instructions on how to compile it using GCC can be seen below or in the source code.

gcc tau.c -o tau -lm; ./tau
tau = 6.293700

Resources worth checking out regarding \tau are The Tau Manifesto and 2 Pi or Not 2 Pi?. I wish everybody a happy \tau day.


// C Code
// Jonathan Frech, 27th of June 2017

Continue reading