Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Exporting From C to PHP

Status
Not open for further replies.

PCHomepage

Programmer
Feb 24, 2009
609
US
I'm not sure where to post this. I've inherited a PHP application that was done in C (I have no idea what flavor) which, when compiled, creates a binary file containing the HTML and PHP. I'm not sure why it was done that way but, as I am totally unfamiliar with C, I would like to run a command that does everything like compiling it does except that it outputs to text rather than binary (the compiler is gcc in case it helps). Any ideas about how can that be done?
 
i doubt it is possible given the architecture you describe. is it intended to be a php extension perhaps? or are you saying both the programmatic code and php itself are compiled together (which sounds perhaps a little like hiphop save that in hiphop you write normal php and then run it through a special compiler, not gcc).

maybe if you post the c code (more the cpp than the header but both would be best) we can better assist. but best of all perhaps ask the creator why the application was architected into a single binary.
 
Yes, it appears to me that the .c file contains PHP and it definitely has HTML but it is clearly a binary file. I thought if I could extract it without it being binary (to see it the way the server and browser see it), then I could more easily convert it back to regular PHP.

I did speak to the creator who said he thought it would run faster and he knows C well but otherwise he was vague and doesn't know how to extract it.

Because the code belongs to my company, it's unfortunately not mine to post but perhaps when I'm in the office this morning I can post a sample.
 
Here is a brief snippet of the c code, which shows that it is just outputting to PHP and HTML:

Code:
sprintf(sqlstr,"SELECT qevalue FROM qe_nam_val_typ WHERE qename = '%s' and filename = '%s'",g_whattoplotony,fptrs[j]);
  mysql_query(g_conn,sqlstr);
  result = mysql_store_result(g_conn);
  num_fields = mysql_num_fields(result);

  while ((row = mysql_fetch_row(result))) {
      for(i = 0; i < num_fields; i++) {
         max_y = MAXOF(max_y,atoi(row[i]));
         min_y = MINOF(min_y,atoi(row[i]));
	 fprintf(fp, "%s,",row[i]);
      }
   }
  mysql_free_result(result);
}
  fprintf(fp,");\n");
 
The current gcc command to compile the code is:

Code:
gcc -lcgic -o filename.cgi filename.c `mysql_config --cflags --libs`; cp filename.cgi /usr/lib/cgi-bin/

I've tried to research it but I don't fully understand how gcc works so is there any switch, perhaps for debugging purposes, to output it to text?
 
I'm not sure that it helped much but I realized that I might be able to pipe the output from the CGI to a text file in Terminal by running the CGI itself:

Code:
./filename.cgi > filename.txt

This did indeed give me some output but it is no different than if I were to run it in the browser and view the source code. In order words, there is more to it than that (lots of database queries etc.) in PHP that are not being provided by this method. Any other thoughts or ideas?
 
compilers compile. there won't be any way to output to text in a meaningful manner.

i have no idea whatsoever what the purpose of this exercise might be.

the best solution, imo, is to contact the person who wrote the original and ask him or her what architectural requirements led to this solution and how best to undo it.

failing that the only solution that I can think of is a manual rewrite. this might be quite quick with some judicious use of pattern replace functions.

 
It might have been written as a cgi to hide the source code, If so you need to extract everything manually.

 
If it was just to occlude there are easier ways to achieve that goal.

Personally I question whether the code is php at all. It all looks like vanilla C to me. What makes you think that this is php interwoven with other stuff (nb mysql_* functions in php are very very similar to the function calls in the underlying C libraries as you'd expect)
 
I've come to the same conclusion that a manual converion is the only way. Most of it seems very straightforward and, you're right, it might not actually be PHP at all but most is very similar.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top