Reversing Circle

One of the CISS classes I had the oportunity to participate in was Software Reverse Engineering. The final reversing project had us taking a look at a math utility that displayed information about points on a particular circle.

The bulk of the code was open-source software from FSU's John Burkardt - see his page here to download the C++ source for circle_arc_grid.

For my part I was not given the original source - but rather an executable binary for Windows. The task was to reverse engineer a couple missing functions (circle_arc_grid() and timestamp()) from a provided peice of C++ code. The timestamp function is pretty easy, and simply involves calling a couple windows APIs (noted in the assembly) - so I'll skip providing either the finished code or reversing logic here.

The circle_arc_grid function is more complex - with the majority being shown in the screenshot below of my disassembly logic (assembly on the left - my comments on the right).

The final code looks like:

double * circle_arc_grid(double radius, double centers[2], double angles[2], int n) {   
// Declare new constant variables
    double pi = 3.141592653589793;
    double radian = 180;

    // Create the results array, can't use VLAs so instantiate a pointer first
    // Size is number of points times two (like the 2-column layout or sin/cos)
    double *xy;
    xy = new double[2*n];

    // Reverse counter counts down
    int j = n;

    // Normal counter controls loop and counts up
    for (int i = 0; i < n; i++) {
        // This is a temporary value stored in ebp-14
        double temp = ((((j - 1) * angles[0] + i * angles[1]) / (n - 1)) * pi) / radian;

        // i is an index into the results array xy
        // Multiply by two since each store event stores the cos then the sin
        xy[2 * (i)] = (cos(temp) * radius) + centers[0];
        // Store into the "second slot"
        xy[2 * (i) + 1] = (sin(temp) * radius) + centers[1];

        j--;
    }

    return xy;
}