unknown
  +   lY>eJ}@?q1h_I|+ ?     /*
   +----------------------------------------------------------------------+
   | PHP Version 5                                                        |
   +----------------------------------------------------------------------+
   | Copyright (c) 1997-2016 The PHP Group                                |
   +----------------------------------------------------------------------+
   | This source file is subject to version 3.01 of the PHP license,      |
   | that is bundled with this package in the file LICENSE, and is        |
   | available through the world-wide-web at the following url:           |
   | http://www.php.net/license/3_01.txt                                  |
   | If you did not receive a copy of the PHP license and are unable to   |
   | obtain it through the world-wide-web, please send a note to          |
   | license@php.net so we can mail you a copy immediately.               |
   +----------------------------------------------------------------------+
   | Authors: Rasmus Lerdorf <rasmus@php.net>                             |
   |          Jim Winstead <jimw@php.net>                                 |
   |          Jaakko Hyvtti <jaakko@hyvatti.iki.fi>                      |
   +----------------------------------------------------------------------+
 */
/* $Id$ */

#include <stdio.h>
#include <ctype.h>
#include "php.h"
#include "ext/standard/php_string.h"
#include "php_ereg.h"
#include "ext/standard/info.h"

/* {{{ arginfo */
ZEND_BEGIN_ARG_INFO_EX(arginfo_ereg, 0, 0, 2)
	ZEND_ARG_INFO(0, pattern)
	ZEND_ARG_INFO(0, string)
	ZEND_ARG_INFO(1, registers) /* ARRAY_INFO(1, registers, 1) */
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO(arginfo_ereg_replace, 0)
	ZEND_ARG_INFO(0, pattern)
	ZEND_ARG_INFO(0, replacement)
	ZEND_ARG_INFO(0, string)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_split, 0, 0, 2)
	ZEND_ARG_INFO(0, pattern)
	ZEND_ARG_INFO(0, string)
	ZEND_ARG_INFO(0, limit)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO(arginfo_sql_regcase, 0)
	ZEND_ARG_INFO(0, string)
ZEND_END_ARG_INFO()
/* }}} */

/* {{{ Function table */
const zend_function_entry ereg_functions[] = {
	PHP_DEP_FE(ereg,			arginfo_ereg)
	PHP_DEP_FE(ereg_replace,	arginfo_ereg_replace)
	PHP_DEP_FE(eregi,			arginfo_ereg)
	PHP_DEP_FE(eregi_replace,	arginfo_ereg_replace)
	PHP_DEP_FE(split,			arginfo_split)
	PHP_DEP_FE(spliti,			arginfo_split)
	PHP_DEP_FE(sql_regcase,		arginfo_sql_regcase)
	PHP_FE_END
};
/* }}} */

/* {{{ reg_cache */
typedef struct {
	regex_t preg;
	int cflags;
	unsigned long lastuse;
} reg_cache;
static int reg_magic = 0;
#define EREG_CACHE_SIZE 4096
/* }}} */

ZEND_DECLARE_MODULE_GLOBALS(ereg)
static PHP_GINIT_FUNCTION(ereg);
static PHP_GSHUTDOWN_FUNCTION(ereg);

/* {{{ Module entry */
zend_module_entry ereg_module_entry = {
	STANDARD_MODULE_HEADER,
	"ereg",
	ereg_functions,
	NULL,
	NULL,
	NULL,
	NULL,
	PHP_MINFO(ereg),
	NO_VERSION_YET,
	PHP_MODULE_GLOBALS(ereg),
	PHP_GINIT(ereg),
	PHP_GSHUTDOWN(ereg),
	NULL,
	STANDARD_MODULE_PROPERTIES_EX
};
/* }}} */

/* {{{ COMPILE_DL_EREG */
#ifdef COMPILE_DL_EREG
ZEND_GET_MODULE(ereg)
#endif
/* }}} */

/* {{{ ereg_lru_cmp */
static int ereg_lru_cmp(const void *a, const void *b TSRMLS_DC)
{
	Bucket *f = *((Bucket **) a);
	Bucket *s = *((Bucket **) b);

	if (((reg_cache *)f->pData)->lastuse <
				((reg_cache *)s->pData)->lastuse) {
		return -1;
	} else if (((reg_cache *)f->pData)->lastuse ==
				((reg_cache *)s->pData)->lastuse) {
		return 0;
	} else {
		return 1;
	}
}
/* }}} */

/* {{{ static ereg_clean_cache */
static int ereg_clean_cache(void *data, void *arg TSRMLS_DC)
{
	int *num_clean = (int *)arg;

	if (*num_clean > 0) {
		(*num_clean)--;
		return ZEND_HASH_APPLY_REMOVE;
	} else {
		return ZEND_HASH_APPLY_STOP;
	}
}
/* }}} */

/* {{{ _php_regcomp
 */
static int _php_regcomp(regex_t *preg, const char *pattern, int cflags TSRMLS_DC)
{
	int r = 0;
	int patlen = strlen(pattern);
	reg_cache *rc = NULL;

	if (zend_hash_num_elements(&EREG(ht_rc)) >= EREG_CACHE_SIZE) {
		/* easier than dealing with overflow as it happ