Back to index

php5  5.3.10
recode.c
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | PHP Version 5                                                        |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 1997-2012 The PHP Group                                |
00006    +----------------------------------------------------------------------+
00007    | This source file is subject to version 3.01 of the PHP license,      |
00008    | that is bundled with this package in the file LICENSE, and is        |
00009    | available through the world-wide-web at the following url:           |
00010    | http://www.php.net/license/3_01.txt                                  |
00011    | If you did not receive a copy of the PHP license and are unable to   |
00012    | obtain it through the world-wide-web, please send a note to          |
00013    | license@php.net so we can mail you a copy immediately.               |
00014    +----------------------------------------------------------------------+
00015    | Author: Kristian Koehntopp <kris@koehntopp.de>                                       |
00016    +----------------------------------------------------------------------+
00017  */
00018  
00019 /* $Id: recode.c 321634 2012-01-01 13:15:04Z felipe $ */
00020 
00021 /* {{{ includes & prototypes */
00022 
00023 #ifdef HAVE_CONFIG_H
00024 #include "config.h"
00025 #endif
00026 
00027 #include "php.h"
00028 #include "php_streams.h"
00029 
00030 #if HAVE_LIBRECODE
00031 
00032 /* For recode 3.5 */
00033 #if HAVE_BROKEN_RECODE
00034 extern char *program_name;
00035 char *program_name = "php";
00036 #endif
00037 
00038 #ifdef HAVE_STDBOOL_H
00039 # include <stdbool.h>
00040 #else
00041   typedef enum {false = 0, true = 1} bool;
00042 #endif
00043 
00044 #include <stdio.h>
00045 #include <sys/types.h>
00046 #include <unistd.h>
00047 #include <recode.h>
00048 
00049 #include "php_recode.h"
00050 #include "ext/standard/info.h"
00051 #include "ext/standard/file.h"
00052 #include "ext/standard/php_string.h"
00053 
00054 /* }}} */
00055 
00056 ZEND_BEGIN_MODULE_GLOBALS(recode)
00057     RECODE_OUTER  outer;
00058 ZEND_END_MODULE_GLOBALS(recode)
00059 
00060 #ifdef ZTS
00061 # define ReSG(v) TSRMG(recode_globals_id, zend_recode_globals *, v)
00062 #else
00063 # define ReSG(v) (recode_globals.v)
00064 #endif
00065     
00066 ZEND_DECLARE_MODULE_GLOBALS(recode);
00067 static PHP_GINIT_FUNCTION(recode);
00068 
00069 /* {{{ arginfo */
00070 ZEND_BEGIN_ARG_INFO_EX(arginfo_recode_string, 0, 0, 2)
00071        ZEND_ARG_INFO(0, request)
00072        ZEND_ARG_INFO(0, str)
00073 ZEND_END_ARG_INFO()
00074 
00075 ZEND_BEGIN_ARG_INFO_EX(arginfo_recode_file, 0, 0, 3)
00076        ZEND_ARG_INFO(0, request)
00077        ZEND_ARG_INFO(0, input)
00078        ZEND_ARG_INFO(0, output)
00079 ZEND_END_ARG_INFO()
00080 /* }}} */
00081 
00082 /* {{{ module stuff */
00083 static const zend_function_entry php_recode_functions[] = {
00084        PHP_FE(recode_string,       arginfo_recode_string)
00085        PHP_FE(recode_file,  arginfo_recode_file)
00086        PHP_FALIAS(recode, recode_string, arginfo_recode_string)
00087        PHP_FE_END
00088 };
00089 
00090 zend_module_entry recode_module_entry = {
00091        STANDARD_MODULE_HEADER,
00092        "recode", 
00093        php_recode_functions, 
00094        PHP_MINIT(recode), 
00095        PHP_MSHUTDOWN(recode), 
00096        NULL,
00097        NULL, 
00098        PHP_MINFO(recode), 
00099        NO_VERSION_YET,
00100        PHP_MODULE_GLOBALS(recode),
00101        PHP_GINIT(recode),
00102        NULL,
00103        NULL,
00104        STANDARD_MODULE_PROPERTIES_EX
00105 };
00106 
00107 #ifdef COMPILE_DL_RECODE
00108 ZEND_GET_MODULE(recode)
00109 #endif
00110 
00111 static PHP_GINIT_FUNCTION(recode)
00112 {
00113        recode_globals->outer = NULL;
00114 }
00115 
00116 PHP_MINIT_FUNCTION(recode)
00117 {
00118        ReSG(outer) = recode_new_outer(false);
00119        if (ReSG(outer) == NULL) {
00120               return FAILURE;
00121        }
00122 
00123        return SUCCESS;
00124 }
00125 
00126 PHP_MSHUTDOWN_FUNCTION(recode)
00127 {
00128        if (ReSG(outer)) {
00129               recode_delete_outer(ReSG(outer));
00130        }
00131        return SUCCESS;
00132 }
00133 
00134 PHP_MINFO_FUNCTION(recode)
00135 {
00136        php_info_print_table_start();
00137        php_info_print_table_row(2, "Recode Support", "enabled");
00138        php_info_print_table_row(2, "Revision", "$Revision: 321634 $");
00139        php_info_print_table_end();
00140 }
00141 
00142 /* {{{ proto string recode_string(string request, string str)
00143    Recode string str according to request string */
00144 PHP_FUNCTION(recode_string)
00145 {
00146        RECODE_REQUEST request = NULL;
00147        char *r = NULL;
00148        size_t r_len = 0, r_alen = 0;
00149        int req_len, str_len;
00150        char *req, *str;
00151 
00152        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &req, &req_len, &str, &str_len) == FAILURE) {
00153               return;
00154        }
00155 
00156        request = recode_new_request(ReSG(outer));
00157 
00158        if (request == NULL) {
00159               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot allocate request structure");
00160               RETURN_FALSE;
00161        }
00162 
00163        if (!recode_scan_request(request, req)) {
00164               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Illegal recode request '%s'", req);
00165               goto error_exit;
00166        }
00167        
00168        recode_buffer_to_buffer(request, str, str_len, &r, &r_len, &r_alen);
00169        if (!r) {
00170               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Recoding failed.");
00171 error_exit:
00172               RETVAL_FALSE;
00173        } else {
00174               RETVAL_STRINGL(r, r_len, 1);
00175               free(r);
00176        }
00177 
00178        recode_delete_request(request);
00179 
00180        return;
00181 }
00182 /* }}} */
00183 
00184 /* {{{ proto bool recode_file(string request, resource input, resource output)
00185    Recode file input into file output according to request */
00186 PHP_FUNCTION(recode_file)
00187 {
00188        RECODE_REQUEST request = NULL;
00189        char *req;
00190        int req_len;
00191        zval *input, *output;
00192        php_stream *instream, *outstream;
00193        FILE  *in_fp,  *out_fp;
00194 
00195        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "srr", &req, &req_len, &input, &output) == FAILURE) {
00196               return;
00197        }
00198 
00199        php_stream_from_zval(instream, &input);
00200        php_stream_from_zval(outstream, &output);
00201 
00202        if (FAILURE == php_stream_cast(instream, PHP_STREAM_AS_STDIO, (void**)&in_fp, REPORT_ERRORS))     {
00203               RETURN_FALSE;
00204        }
00205        
00206        if (FAILURE == php_stream_cast(outstream, PHP_STREAM_AS_STDIO, (void**)&out_fp, REPORT_ERRORS))   {
00207               RETURN_FALSE;
00208        }
00209 
00210        request = recode_new_request(ReSG(outer));
00211        if (request == NULL) {
00212               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot allocate request structure");
00213               RETURN_FALSE;
00214        }
00215 
00216        if (!recode_scan_request(request, req)) {
00217               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Illegal recode request '%s'", req);
00218               goto error_exit;
00219        }
00220        
00221        if (!recode_file_to_file(request, in_fp, out_fp)) {
00222               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Recoding failed.");
00223               goto error_exit;
00224        }
00225 
00226        recode_delete_request(request);
00227        RETURN_TRUE;
00228 
00229 error_exit:
00230        recode_delete_request(request);
00231        RETURN_FALSE;
00232 }
00233 /* }}} */
00234 
00235 #endif
00236 
00237 /*
00238  * Local variables:
00239  * tab-width: 4
00240  * c-basic-offset: 4
00241  * End:
00242  */