Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : PHP bytecode Compiler

PHP bytecode Compiler

Introduction

Warning:

This extension is EXPERIMENTAL. The behaviour of this extension -- including the names of its functions and anything else documented about this extension -- may change without notice in a future release of PHP. Use this extension at your own risk.

Bcompiler was written for several reasons:

To encode entire script in a proprietary PHP application
To encode some classes and/or functions in a proprietary PHP application
To enable the production of php-gtk applications that could be used on client desktops, without the need for a php.exe.
To do the feasibility study for a PHP to C converter

The first of these goals is achieved using the bcompiler_write_header(), bcompiler_write_file() and bcompiler_write_footer() functions. The bytecode files can be written as either uncompressed or plain. To use the generated bytecode, you can simply include it with include or require statements.

The second of these goals is achieved using the bcompiler_write_header(), bcompiler_write_class(), bcompiler_write_footer(), bcompiler_read(), and bcompiler_load() functions. The bytecode files can be written as either uncompressed or plain. The bcompiler_load() reads a bzip compressed bytecode file, which tends to be 1/3 of the size of the original file.

To create EXE type files, bcompiler has to be used with a modified sapi file or a version of PHP which has been compiled as a shared library. In this scenario, bcompiler reads the compressed bytecode from the end of the exe file.

bcompiler can improve performance by about 30% when used with uncompressed bytecodes only. But keep in mind that uncompressed bytecode can be up to 5 times larger than the original source code. Using bytecode compression can save your space, but decompression requires much more time than parsing a source. bcompiler also does not do any bytecode optimization, this could be added in the future...

In terms of code protection, it is safe to say that it would be impossible to recreate the exact source code that it was built from, and without the accompanying source code comments. It would effectively be useless to use the bcompiler bytecodes to recreate and modify a class. However it is possible to retrieve data from a bcompiled bytecode file - so don't put your private passwords or anything in it.

Installation

short installation note:

  • You need at least PHP 4.3.0 for the compression to work
  • To install on PHP 4.3.0 and later at the Unix command prompt type pear install bcompiler
  • To install on Windows, until the binary package distribution mechanism is finished please search the archives of the pear-general mailing list for pre-built packages. (or send an email to it if you could not find a reference)
  • To install on older versions you need to make some slight changes to the build.
  • untar the bcompiler.tgz archive into php4/ext.(Get it directly from PECL » http://pecl.php.net/get/bcompiler)
  • If the new directory is now called something like bcompiler-0.x, then you should rename it to bcompiler (except you only want to build it as self-contained php-module).
  • If you are using versions before PHP 4.3.0, the you will need to copy the Makefile.in.old to Makefile.in and config.m4.old to config.m4.
  • run phpize in ext/bcompiler
  • run ./buildconf in php4
  • run configure with --enable-bcompiler (and your other options)
  • make; make install
  • that's it.

Contact Information

If you have comments, bugfixes, enhancements or want to help developing this beast, you can drop me a mail at » alan_k@php.net. Any help is very welcome.

Table of Contents

bcompiler_load_exe — Reads and creates classes from a bcompiler exe file
bcompiler_load — Reads and creates classes from a bz compressed file
bcompiler_parse_class — Reads the bytecodes of a class and calls back to a user function
bcompiler_read — Reads and creates classes from a filehandle
bcompiler_write_class — Writes an defined class as bytecodes
bcompiler_write_constant — Writes a defined constant as bytecodes
bcompiler_write_exe_footer — Writes the start pos, and sig to the end of a exe type file
bcompiler_write_file — Writes a php source file as bytecodes
bcompiler_write_footer — Writes the single character \x00 to indicate End of compiled data
bcompiler_write_function — Writes an defined function as bytecodes
bcompiler_write_functions_from_file — Writes all functions defined in a file as bytecodes
bcompiler_write_header — Writes the bcompiler header
bcompiler_write_included_filename — Writes an included file as bytecodes

Code Examples / Notes » ref.bcompiler

sinured

Regarding alan at akbkhome dot com's note:
Not only __FILE__ will be handled differently; same for errors thrown by PHP:
If the file was compiled from a another file (bcompiler_write_file()), then PHP will blame the original filename - looks very strange when you put a file which was compiled under Windows on an UNIX system.
If the file was compiled by bcompiler_write_[class|constant|function](), error messages will contain the filename of the file which has called these functions.


arplynn

PHP seems to choke on trying to compile PHP5 classes with the private, protected or public modifiers. Additionally, when including files with properly compiled classes on PHP5, it works fine but when php tries to exit it throws a segmentation fault.
It's just not a great idea to use bcompiler for anything other than functions on PHP5, at least for now.


alan

Note:
the special constant __FILE__ cannot be handled correctly by bcompiler (it will always contain the name of the original file that was compiled). There is no known way around this, as it is converted to a string by the Zend engine.


cdsheen+bencoder

Here's a script to act as a front-end for bcompiler, which may be very similar to the eA ones:
http://bbs.giga.net.tw/bencoder.php
BENCODER v1.1 - Encode your PHP script using bcompiler
Usage: bencoder [-f] -o FILE    file1.php
      bencoder [-f] -o OUTDIR  file1.php file2.php ...
      bencoder [-f] -o OUTDIR  -a SRCDIR  [-s SUFFIX] [-c] [-r]
 -f        : force overwriting even if the target exists
 -o FILE   : the file name to write the encoded script
             (default to '-encoded.php' suffix)
 -o OUTDIR : the directory to write all encoded files
 -a SRCDIR : encode all files in this source directory
 -s SUFFIX : encode the files with the SUFFIX extension only (default: php)
 -c        : copy files those shouldn't be encoded (no by default)


roberto

hello guys, since bencoder don't work for __FILE__ constants i have a bcompiler script working, anyone that want it can send me an email
the source is 100% compatible with original file, the only problem is php5 new function methods that
<?
if (!function_exists('function_name')){
function function_name()
}
?>
don't work but it's a php5 new method for understand bytecode


tda

Creating a function after checking if a function exists using function_exists will result in a "Zero Sized Reply" error.
this does NOT work any longer:
if (!function_exists("file_get_contents")) {
       function file_get_contents($filename, $use_include_path = 0)
       { ...define your own function here... }
}
Apache returns: "Zero Sized Reply" after compilation.


albert kok

A few notes to succesfully configure PHP5 to load bcompiler.
In case of using Debian/Ubuntu Linux: When installing from the PECL (PEAR) package, make sure you have the php5-dev library installed in your system (apt-get install php5-dev).
Newer versions of Ubuntu may error out while installing bcompiler thru PECL, asking you to reinstall bzip2. This will not fix the installation. Instead, install the libbz2-dev library (apt-get install libbz2-dev) and try installing bcompiler from PECL again.
After installing bcompiler from PECL, don't forget to make sure you load the bcompiler.so library in your PHP configuration for the new bcompiler functions to become available. This is done by adding the following line to the bottom of your php.ini file(s):
extension=bcompiler.so
If you were to use the bcompiler functions thru your webserver, you need to restart it to reload the php configuration.
Regards,
Albert Kok


shannah

>the special constant __FILE__ cannot be handled correctly by bcompiler
> (it will always contain the name of the original file that was compiled).
I would think that this is the preferred behavior anyways since the compilation should be independent from the source code.  I would want to be able to compile any part of my source code without having to worry if any constants will change as a result of it.  Hence it is better for __FILE__ to contain the name of the original file that was compiled.


Change Language


Follow Navioo On Twitter
.NET Functions
Apache-specific Functions
Alternative PHP Cache
Advanced PHP debugger
Array Functions
Aspell functions [deprecated]
BBCode Functions
BCMath Arbitrary Precision Mathematics Functions
PHP bytecode Compiler
Bzip2 Compression Functions
Calendar Functions
CCVS API Functions [deprecated]
Class/Object Functions
Classkit Functions
ClibPDF Functions [deprecated]
COM and .Net (Windows)
Crack Functions
Character Type Functions
CURL
Cybercash Payment Functions
Credit Mutuel CyberMUT functions
Cyrus IMAP administration Functions
Date and Time Functions
DB++ Functions
Database (dbm-style) Abstraction Layer Functions
dBase Functions
DBM Functions [deprecated]
dbx Functions
Direct IO Functions
Directory Functions
DOM Functions
DOM XML Functions
enchant Functions
Error Handling and Logging Functions
Exif Functions
Expect Functions
File Alteration Monitor Functions
Forms Data Format Functions
Fileinfo Functions
filePro Functions
Filesystem Functions
Filter Functions
Firebird/InterBase Functions
Firebird/Interbase Functions (PDO_FIREBIRD)
FriBiDi Functions
FrontBase Functions
FTP Functions
Function Handling Functions
GeoIP Functions
Gettext Functions
GMP Functions
gnupg Functions
Net_Gopher
Haru PDF Functions
hash Functions
HTTP
Hyperwave Functions
Hyperwave API Functions
i18n Functions
IBM Functions (PDO_IBM)
IBM DB2
iconv Functions
ID3 Functions
IIS Administration Functions
Image Functions
Imagick Image Library
IMAP
Informix Functions
Informix Functions (PDO_INFORMIX)
Ingres II Functions
IRC Gateway Functions
PHP / Java Integration
JSON Functions
KADM5
LDAP Functions
libxml Functions
Lotus Notes Functions
LZF Functions
Mail Functions
Mailparse Functions
Mathematical Functions
MaxDB PHP Extension
MCAL Functions
Mcrypt Encryption Functions
MCVE (Monetra) Payment Functions
Memcache Functions
Mhash Functions
Mimetype Functions
Ming functions for Flash
Miscellaneous Functions
mnoGoSearch Functions
Microsoft SQL Server Functions
Microsoft SQL Server and Sybase Functions (PDO_DBLIB)
Mohawk Software Session Handler Functions
mSQL Functions
Multibyte String Functions
muscat Functions
MySQL Functions
MySQL Functions (PDO_MYSQL)
MySQL Improved Extension
Ncurses Terminal Screen Control Functions
Network Functions
Newt Functions
NSAPI-specific Functions
Object Aggregation/Composition Functions
Object property and method call overloading
Oracle Functions
ODBC Functions (Unified)
ODBC and DB2 Functions (PDO_ODBC)
oggvorbis
OpenAL Audio Bindings
OpenSSL Functions
Oracle Functions [deprecated]
Oracle Functions (PDO_OCI)
Output Control Functions
Ovrimos SQL Functions
Paradox File Access
Parsekit Functions
Process Control Functions
Regular Expression Functions (Perl-Compatible)
PDF Functions
PDO Functions
Phar archive stream and classes
PHP Options&Information
POSIX Functions
Regular Expression Functions (POSIX Extended)
PostgreSQL Functions
PostgreSQL Functions (PDO_PGSQL)
Printer Functions
Program Execution Functions
PostScript document creation
Pspell Functions
qtdom Functions
Radius
Rar Functions
GNU Readline
GNU Recode Functions
RPM Header Reading Functions
runkit Functions
SAM - Simple Asynchronous Messaging
Satellite CORBA client extension [deprecated]
SCA Functions
SDO Functions
SDO XML Data Access Service Functions
SDO Relational Data Access Service Functions
Semaphore
SESAM Database Functions
PostgreSQL Session Save Handler
Session Handling Functions
Shared Memory Functions
SimpleXML functions
SNMP Functions
SOAP Functions
Socket Functions
Standard PHP Library (SPL) Functions
SQLite Functions
SQLite Functions (PDO_SQLITE)
Secure Shell2 Functions
Statistics Functions
Stream Functions
String Functions
Subversion Functions
Shockwave Flash Functions
Swish Functions
Sybase Functions
TCP Wrappers Functions
Tidy Functions
Tokenizer Functions
Unicode Functions
URL Functions
Variable Handling Functions
Verisign Payflow Pro Functions
vpopmail Functions
W32api Functions
WDDX Functions
win32ps Functions
win32service Functions
xattr Functions
xdiff Functions
XML Parser Functions
XML-RPC Functions
XMLReader functions
XMLWriter Functions
XSL functions
XSLT Functions
YAZ Functions
YP/NIS Functions
Zip File Functions
Zlib Compression Functions
eXTReMe Tracker