Tsukada
ttsuk****@users*****
2002年 6月 3日 (月) 01:30:38 JST
ttsukada 02/06/03 01:30:38 Modified: ext/mbstring buffer.c mbfunction.c mbstring.c mbstring.h php_mb_buf.h php_mb_str.h string.c Log: mbIterator, mbBuffer object Revision Changes Path 1.6 +51 -19 php4/ext/mbstring/buffer.c Index: buffer.c =================================================================== RCS file: /cvsroot/php-i18n/php4/ext/mbstring/buffer.c,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- buffer.c 26 May 2002 06:05:52 -0000 1.5 +++ buffer.c 2 Jun 2002 16:30:37 -0000 1.6 @@ -24,7 +24,7 @@ PHP_MB_BUF_TSRMLS_SET(buffer); buffer->value = NULL; buffer->length = 0; - buffer->size = 0; + buffer->capacity = 0; buffer->realloc = PHP_MB_BUF_ALLOC_SIZE; buffer->refcount = 1; if (persist) { @@ -92,9 +92,41 @@ } PHPAPI int +_php_mb_buf_allocate(php_mb_buf *buffer, size_t size TSRMLS_DC) +{ + int *tmp; + + if (size < 0) { + PHP_MB_ERRSET(PHP_MB_ERR_ILLEGAL_ARGUMENT); + return FAILURE; + } + if (buffer->capacity >= size) { + return SUCCESS; + } + if (buffer->memory_type_value == PHP_MB_MEMORY_TYPE_CONST) { + PHP_MB_ERRSET(PHP_MB_ERR_BUFFER_OVER_FLOW); + return FAILURE; + } + + tmp = NULL; + if (buffer->memory_type_value == PHP_MB_MEMORY_TYPE_SALLOC) { + tmp = realloc(buffer->value, size*sizeof(int)); + } else if (buffer->memory_type_value == PHP_MB_MEMORY_TYPE_DALLOC) { + tmp = erealloc(buffer->value, size*sizeof(int)); + } + if (tmp == NULL) { + PHP_MB_ERRSET(PHP_MB_ERR_NO_MEMORY); + return FAILURE; + } + buffer->capacity = size; + buffer->value = tmp; + return SUCCESS; +} + +PHPAPI int _php_mb_buf_append_char(php_mb_buf *buffer, int c TSRMLS_DC) { - if (buffer->length >= buffer->size) { + if (buffer->length >= buffer->capacity) { /* reallocate buffer */ size_t newsize; int *tmp; @@ -103,7 +135,7 @@ PHP_MB_ERRSET(PHP_MB_ERR_BUFFER_OVER_FLOW); return FAILURE; } - newsize = buffer->size + buffer->realloc; + newsize = buffer->capacity + buffer->realloc; tmp = NULL; if (buffer->memory_type_value == PHP_MB_MEMORY_TYPE_SALLOC) { tmp = realloc(buffer->value, newsize*sizeof(int)); @@ -114,7 +146,7 @@ PHP_MB_ERRSET(PHP_MB_ERR_NO_MEMORY); return FAILURE; } - buffer->size = newsize; + buffer->capacity = newsize; buffer->value = tmp; } @@ -137,7 +169,7 @@ return FAILURE; } - if ((dst->length + length) >= dst->size) { + if ((dst->length + length) >= dst->capacity) { /* reallocate buffer */ if (dst->memory_type_value == PHP_MB_MEMORY_TYPE_CONST) { PHP_MB_ERRSET(PHP_MB_ERR_BUFFER_OVER_FLOW); @@ -154,7 +186,7 @@ PHP_MB_ERRSET(PHP_MB_ERR_NO_MEMORY); return FAILURE; } - dst->size = n; + dst->capacity = n; dst->value = w; } @@ -194,7 +226,7 @@ return FAILURE; } - if ((dst->length + src->length) >= dst->size) { + if ((dst->length + src->length) >= dst->capacity) { /* reallocate buffer */ if (dst->memory_type_value == PHP_MB_MEMORY_TYPE_CONST) { PHP_MB_ERRSET(PHP_MB_ERR_BUFFER_OVER_FLOW); @@ -211,7 +243,7 @@ PHP_MB_ERRSET(PHP_MB_ERR_NO_MEMORY); return FAILURE; } - dst->size = len; + dst->capacity = len; dst->value = p; } @@ -296,7 +328,7 @@ } len = end - start; - if ((dst->length - len + src->length) >= dst->size) { + if ((dst->length - len + src->length) >= dst->capacity) { /* reallocate buffer */ if (dst->memory_type_value == PHP_MB_MEMORY_TYPE_CONST) { PHP_MB_ERRSET(PHP_MB_ERR_BUFFER_OVER_FLOW); @@ -313,7 +345,7 @@ PHP_MB_ERRSET(PHP_MB_ERR_NO_MEMORY); return FAILURE; } - dst->size = len; + dst->capacity = len; dst->value = p; } @@ -407,15 +439,15 @@ PHPAPI int _php_mb_buf_nullpad(php_mb_buf *buffer TSRMLS_DC) { - if (buffer->length >= buffer->size) { + if (buffer->length >= buffer->capacity) { /* reallocate buffer */ size_t newsize; int *tmp; if (buffer->memory_type_value == PHP_MB_MEMORY_TYPE_CONST) { PHP_MB_ERRSET(PHP_MB_ERR_BUFFER_OVER_FLOW); - if (buffer->value != NULL && buffer->size > 0) { - buffer->value[buffer->size - 1] = 0; + if (buffer->value != NULL && buffer->capacity > 0) { + buffer->value[buffer->capacity - 1] = 0; } return FAILURE; } @@ -428,12 +460,12 @@ } if (tmp == NULL) { PHP_MB_ERRSET(PHP_MB_ERR_NO_MEMORY); - if (buffer->value != NULL && buffer->size > 0) { - buffer->value[buffer->size - 1] = 0; + if (buffer->value != NULL && buffer->capacity > 0) { + buffer->value[buffer->capacity - 1] = 0; } return FAILURE; } - buffer->size = newsize; + buffer->capacity = newsize; buffer->value = tmp; } @@ -447,7 +479,7 @@ { php_mb_buf *buffer = (php_mb_buf *)stream_resource; - if (buffer->length >= buffer->size) { + if (buffer->length >= buffer->capacity) { /* reallocate buffer */ size_t newsize; int *tmp; @@ -457,7 +489,7 @@ PHP_MB_ERRSET(PHP_MB_ERR_BUFFER_OVER_FLOW); return -1; } - newsize = buffer->size + buffer->realloc; + newsize = buffer->capacity + buffer->realloc; tmp = NULL; if (buffer->memory_type_value == PHP_MB_MEMORY_TYPE_SALLOC) { tmp = realloc(buffer->value, newsize*sizeof(int)); @@ -468,7 +500,7 @@ PHP_MB_ERRSET(PHP_MB_ERR_NO_MEMORY); return -1; } - buffer->size = newsize; + buffer->capacity = newsize; buffer->value = tmp; } 1.9 +4 -16 php4/ext/mbstring/mbfunction.c Index: mbfunction.c =================================================================== RCS file: /cvsroot/php-i18n/php4/ext/mbstring/mbfunction.c,v retrieving revision 1.8 retrieving revision 1.9 diff -u -r1.8 -r1.9 --- mbfunction.c 30 May 2002 17:50:32 -0000 1.8 +++ mbfunction.c 2 Jun 2002 16:30:37 -0000 1.9 @@ -2102,16 +2102,10 @@ { char *result; + result = NULL; mime_header_encoder_flush(encoder TSRMLS_CC); php_mb_str_nullpad(encoder->result); - result = encoder->result->value; - if (result != NULL && return_len != NULL) { - *return_len = encoder->result->length; - } - encoder->result->value = NULL; - encoder->result->length = 0; - encoder->result->size = 0; - + php_mb_str_extract(encoder->result, &result, return_len); return result; } @@ -2439,16 +2433,10 @@ { char *result; + result = NULL; mime_header_decoder_flush(decoder TSRMLS_CC); php_mb_str_nullpad(decoder->result); - result = decoder->result->value; - if (result != NULL && return_len != NULL) { - *return_len = decoder->result->length; - } - decoder->result->value = NULL; - decoder->result->length = 0; - decoder->result->size = 0; - + php_mb_str_extract(decoder->result, &result, return_len); return result; } 1.9 +2082 -0 php4/ext/mbstring/mbstring.c Index: mbstring.c =================================================================== RCS file: /cvsroot/php-i18n/php4/ext/mbstring/mbstring.c,v retrieving revision 1.8 retrieving revision 1.9 diff -u -r1.8 -r1.9 --- mbstring.c 30 May 2002 19:00:46 -0000 1.8 +++ mbstring.c 2 Jun 2002 16:30:37 -0000 1.9 @@ -198,9 +198,94 @@ PHP_FALIAS(mbereg_search_getpos, mb_ereg_search_getpos, NULL) PHP_FALIAS(mbereg_search_setpos, mb_ereg_search_setpos, NULL) #endif + + PHP_FE(mb_iter_create, NULL) + PHP_FE(mb_iter_free, NULL) + PHP_FE(mb_iter_set_index, NULL) + PHP_FE(mb_iter_get_index, NULL) + PHP_FE(mb_iter_current_byte, NULL) + PHP_FE(mb_iter_next_byte, NULL) + PHP_FE(mb_iter_begin_index, NULL) + PHP_FE(mb_iter_end_index, NULL) + PHP_FE(mb_iter_first, NULL) + PHP_FE(mb_iter_current, NULL) + PHP_FE(mb_iter_next, NULL) + + PHP_FE(mb_buf_create, NULL) + PHP_FE(mb_buf_free, NULL) + PHP_FE(mb_buf_capacity, NULL) + PHP_FE(mb_buf_ensure_capacity, NULL) + PHP_FE(mb_buf_to_string, NULL) + PHP_FE(mb_buf_encode, NULL) + PHP_FE(mb_buf_equals, NULL) + PHP_FE(mb_buf_append, first_arg_force_ref) + PHP_FE(mb_buf_delete, first_arg_force_ref) + PHP_FE(mb_buf_insert, first_arg_force_ref) + PHP_FE(mb_buf_get_at, first_arg_force_ref) + PHP_FE(mb_buf_set_at, first_arg_force_ref) + PHP_FE(mb_buf_replace, first_arg_force_ref) + PHP_FE(mb_buf_reverse, first_arg_force_ref) + PHP_FE(mb_buf_substr, NULL) + PHP_FE(mb_buf_trim, NULL) + PHP_FE(mb_buf_pick, NULL) + PHP_FE(mb_buf_remove, NULL) + PHP_FE(mb_buf_edit, NULL) + PHP_FE(mb_buf_iterate, NULL) + PHP_FE(mb_buf_check_include, NULL) + PHP_FE(mb_buf_check_exclude, NULL) + PHP_FE(mb_buf_length, NULL) + PHP_FE(mb_buf_index_of, NULL) + PHP_FE(mb_buf_last_index_of, NULL) + { NULL, NULL, NULL } +}; + +static function_entry php_mbiterator_class_functions[] = { + PHP_FALIAS(mbiterator, mb_iter_create, NULL) + PHP_FALIAS(free, mb_iter_free, NULL) + PHP_FALIAS(set_index, mb_iter_set_index, NULL) + PHP_FALIAS(get_index, mb_iter_get_index, NULL) + PHP_FALIAS(current_byte, mb_iter_current_byte, NULL) + PHP_FALIAS(next_byte, mb_iter_next_byte, NULL) + PHP_FALIAS(begin_index, mb_iter_begin_index, NULL) + PHP_FALIAS(end_index, mb_iter_end_index, NULL) + PHP_FALIAS(first, mb_iter_first, NULL) + PHP_FALIAS(current, mb_iter_current, NULL) + PHP_FALIAS(next, mb_iter_next, NULL) { NULL, NULL, NULL } }; +static function_entry php_mb_buf_class_functions[] = { + PHP_FALIAS(mbbuffer, mb_buf_create, NULL) + PHP_FALIAS(free, mb_buf_free, NULL) + PHP_FALIAS(capacity, mb_buf_capacity, NULL) + PHP_FALIAS(ensure_capacity, mb_buf_ensure_capacity, NULL) + PHP_FALIAS(__sleep, mb_buf_sleep, NULL) + PHP_FALIAS(__wakeup, mb_buf_wakeup, NULL) + PHP_FALIAS(to_string, mb_buf_to_string, NULL) + PHP_FALIAS(encode, mb_buf_encode, NULL) + PHP_FALIAS(equals, mb_buf_equals, NULL) + PHP_FALIAS(append, mb_buf_append, NULL) + PHP_FALIAS(delete, mb_buf_delete, NULL) + PHP_FALIAS(insert, mb_buf_insert, NULL) + PHP_FALIAS(get_at, mb_buf_get_at, NULL) + PHP_FALIAS(set_at, mb_buf_set_at, NULL) + PHP_FALIAS(replace, mb_buf_replace, NULL) + PHP_FALIAS(reverse, mb_buf_reverse, NULL) + PHP_FALIAS(substr, mb_buf_substr, NULL) + PHP_FALIAS(trim, mb_buf_trim, NULL) + PHP_FALIAS(pick, mb_buf_pick, NULL) + PHP_FALIAS(remove, mb_buf_remove, NULL) + PHP_FALIAS(edit, mb_buf_edit, NULL) + PHP_FALIAS(iterate, mb_buf_iterate, NULL) + PHP_FALIAS(check_include, mb_buf_check_include, NULL) + PHP_FALIAS(check_exclude, mb_buf_check_exclude, NULL) + PHP_FALIAS(length, mb_buf_length, NULL) + PHP_FALIAS(index_of, mb_buf_index_of, NULL) + PHP_FALIAS(last_index_of, mb_buf_last_index_of, NULL) + { NULL, NULL, NULL } +}; + + zend_module_entry mbstring_module_entry = { STANDARD_MODULE_HEADER, "mbstring", @@ -230,8 +315,29 @@ #endif +/* resource lists */ +static int le_string, le_buffer, le_filter; + /* mbstring builtin objects class entry */ zend_class_entry *mbstdresource_class_entry; +zend_class_entry *mbiterator_class_entry; +zend_class_entry *mbbuffer_class_entry; + +/* resource destructor */ +static void _free_mbbuffer(zend_rsrc_list_entry *rsrc TSRMLS_DC) +{ + php_mb_buf_free((php_mb_buf *)rsrc->ptr); +} + +static void _free_mbstring(zend_rsrc_list_entry *rsrc TSRMLS_DC) +{ + php_mb_str_free((php_mb_str *)rsrc->ptr); +} + +static void _free_conv_filter(zend_rsrc_list_entry *rsrc TSRMLS_DC) +{ + php_mb_conv_filt_free((mbfl_convert_filter *)rsrc->ptr); +} /* set property handler for read only member */ static int @@ -595,8 +701,16 @@ REGISTER_LONG_CONSTANT("MB_OVERLOAD_STRING", MB_OVERLOAD_STRING, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("MB_OVERLOAD_REGEX", MB_OVERLOAD_REGEX, CONST_CS | CONST_PERSISTENT); + le_string = zend_register_list_destructors_ex(_free_mbstring, NULL, "mbstring", module_number); + le_filter = zend_register_list_destructors_ex(_free_conv_filter, NULL, "mbfilter", module_number); + le_buffer = zend_register_list_destructors_ex(_free_mbbuffer, NULL, "mbbuffer", module_number); + INIT_OVERLOADED_CLASS_ENTRY(ce, "mbStdResource", NULL, NULL, NULL, set_property_handler_ro); mbstdresource_class_entry = zend_register_internal_class(&ce TSRMLS_CC); + INIT_OVERLOADED_CLASS_ENTRY(ce, "mbiterator", php_mbiterator_class_functions, NULL, NULL, NULL); + mbiterator_class_entry = zend_register_internal_class(&ce TSRMLS_CC); + INIT_OVERLOADED_CLASS_ENTRY(ce, "mbbuffer", php_mb_buf_class_functions, NULL, NULL, NULL); + mbbuffer_class_entry = zend_register_internal_class(&ce TSRMLS_CC); return SUCCESS; } @@ -3494,6 +3608,1974 @@ RETVAL_LONG(MBSTRG(func_overload)); } else { RETURN_FALSE; + } +} +/* }}} */ + + +#define PHP_MB_CHECK_OBJECT(obj, arg) \ +{ \ + obj = getThis(); \ + if (obj == NULL && arg != NULL) { \ + obj = *arg; \ + } \ + if (obj == NULL || Z_TYPE_P(obj) != IS_OBJECT) { \ + PHP_MB_ERRSET(PHP_MB_ERR_ILLEGAL_ARGUMENT); \ + php_mb_error(); \ + RETURN_FALSE; \ + } \ +} + + +/* + * mbIterator Object + */ + +#define MBITER_PROP_FILTER 0 +#define MBITER_PROP_TEXT 1 +#define MBITER_PROP_CURIND 2 +#define MBITER_PROP_CURBYTE 3 +#define MBITER_PROP_NEXTBYTE 4 +#define MBITER_PROP_BEGIND 5 +#define MBITER_PROP_ENDIND 6 +#define MBITER_PROP_CURCHAR 7 +#define MBITER_PROP_COUNT 8 + +struct iter_collector_data { + int index; + int byte; + int curbyte; + int nextbyte; + int curchar; +}; + +static int +iter_collector(int c, void *stream_resource) +{ + struct iter_collector_data *pc = (struct iter_collector_data *)stream_resource; + + pc->index++; + pc->curbyte = pc->nextbyte; + pc->nextbyte = pc->byte; + pc->curchar = c; + return c; +} + +static int +iter_fetch_propertys(zval *object, zval **props, mbfl_convert_filter **filter, struct iter_collector_data *pc) +{ + zval **prop; + int n, type; + mbfl_convert_filter *f; + + prop = NULL; + n = 0; + type = 0; + if (zend_hash_index_find(Z_OBJPROP_P(object), MBITER_PROP_FILTER, (void **) &prop) == SUCCESS) { + f = zend_list_find(Z_RESVAL_PP(prop), &type); + } + if (type != le_filter) { + PHP_MB_ERRSET(PHP_MB_ERR_ILLEGAL_ARGUMENT); + php_mb_error(); + return FAILURE; + } + props[n++] = *prop; + while (n < MBITER_PROP_COUNT) { + if (zend_hash_index_find(Z_OBJPROP_P(object), n, (void **) &prop) == FAILURE) { + PHP_MB_ERRSET(PHP_MB_ERR_ILLEGAL_ARGUMENT); + php_mb_error(); + return FAILURE; + } + props[n++] = *prop; + } + if (filter != NULL) { + *filter = f; + } + if (pc != NULL) { + pc->index = Z_LVAL_P(props[MBITER_PROP_CURIND]); + pc->byte = Z_LVAL_P(props[MBITER_PROP_NEXTBYTE]); + pc->curbyte = Z_LVAL_P(props[MBITER_PROP_CURBYTE]); + pc->nextbyte = Z_LVAL_P(props[MBITER_PROP_NEXTBYTE]); + pc->curchar = Z_LVAL_P(props[MBITER_PROP_CURCHAR]); + } + + return SUCCESS; +} + +static int +iter_save_propertys(zval **props, struct iter_collector_data *pc) +{ + Z_LVAL_P(props[MBITER_PROP_CURIND]) = pc->index; + Z_LVAL_P(props[MBITER_PROP_CURBYTE]) = pc->curbyte; + Z_LVAL_P(props[MBITER_PROP_NEXTBYTE]) = pc->nextbyte; + Z_LVAL_P(props[MBITER_PROP_CURCHAR]) = pc->curchar; + + return SUCCESS; +} + + +/* {{{ proto object mb_iter_create(string text) + proto object mb_iter_create(string text, int pos) + proto object mb_iter_create(string text, int begin, int end, int pos) + */ +PHP_FUNCTION(mb_iter_create) +{ + zval **arg_text, **arg_pos, **arg_beg, **arg_end, *result, *prop; + int n, pos, beg, end; + unsigned char *p; + mbfl_convert_filter *filter; + struct iter_collector_data cd; + + PHP_MB_ERRRST(); + if ((ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg_text) == FAILURE) && + (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &arg_text, &arg_pos) == FAILURE) && + (ZEND_NUM_ARGS() != 4 || zend_get_parameters_ex(4, &arg_text, &arg_beg, &arg_end, &arg_pos) == FAILURE)) { + WRONG_PARAM_COUNT; + } + + filter = php_mb_conv_filt_create(MBSTRG(internal_encoding_r), MBSTRG(wchar_encoding), iter_collector, NULL, NULL); + if (filter == NULL) { + php_mb_error(); + RETURN_FALSE; + } + + convert_to_string_ex(arg_text); + pos = 0; + cd.index = 0; + cd.byte = 0; + cd.curbyte = 0; + cd.nextbyte = 0; + cd.curchar = 0; + beg = 0; + end = -1; + if (ZEND_NUM_ARGS() >= 2) { + convert_to_long_ex(arg_pos); + pos = Z_LVAL_PP(arg_pos); + if (pos < 0) { + pos = 0; + } + } + if (ZEND_NUM_ARGS() == 4) { + convert_to_long_ex(arg_beg); + convert_to_long_ex(arg_end); + beg = Z_LVAL_PP(arg_beg); + end = Z_LVAL_PP(arg_end); + } + if (beg < 0) { + beg = 0; + } + if (pos < beg) { + pos = beg; + } + if (end >= 0 && pos > end) { + pos = end; + } + + filter->stream_resource = &cd; + p = Z_STRVAL_PP(arg_text); + n = Z_STRLEN_PP(arg_text); + while (n > 0 && cd.index <= pos) { + cd.byte++; + PHP_MB_CONV_FILT_PUTC(*p, filter); + p++; + n--; + } + + result = getThis(); + if (result == NULL) { + result = return_value; + object_init_ex(result, mbiterator_class_entry); + } + MAKE_STD_ZVAL(prop); + ZEND_REGISTER_RESOURCE(prop, filter, le_filter); + zend_hash_index_update(Z_OBJPROP_P(result), MBITER_PROP_FILTER, &prop, sizeof(zval *), NULL); /* filter */ + zval_add_ref(arg_text); + zend_hash_index_update(Z_OBJPROP_P(result), MBITER_PROP_TEXT, arg_text, sizeof(zval *), NULL); /* text */ + MAKE_STD_ZVAL(prop); + ZVAL_LONG(prop, cd.index); + zend_hash_index_update(Z_OBJPROP_P(result), MBITER_PROP_CURIND, &prop, sizeof(zval *), NULL); /* current index */ + MAKE_STD_ZVAL(prop); + ZVAL_LONG(prop, cd.curbyte); + zend_hash_index_update(Z_OBJPROP_P(result), MBITER_PROP_CURBYTE, &prop, sizeof(zval *), NULL); /* current byte index */ + MAKE_STD_ZVAL(prop); + ZVAL_LONG(prop, cd.nextbyte); + zend_hash_index_update(Z_OBJPROP_P(result), MBITER_PROP_NEXTBYTE, &prop, sizeof(zval *), NULL); /* next byte index */ + MAKE_STD_ZVAL(prop); + ZVAL_LONG(prop, beg); + zend_hash_index_update(Z_OBJPROP_P(result), MBITER_PROP_BEGIND, &prop, sizeof(zval *), NULL); /* begin index */ + MAKE_STD_ZVAL(prop); + ZVAL_LONG(prop, end); + zend_hash_index_update(Z_OBJPROP_P(result), MBITER_PROP_ENDIND, &prop, sizeof(zval *), NULL); /* end index */ + MAKE_STD_ZVAL(prop); + ZVAL_LONG(prop, cd.curchar); + zend_hash_index_update(Z_OBJPROP_P(result), MBITER_PROP_CURCHAR, &prop, sizeof(zval *), NULL); /* current character */ +} +/* }}} */ + +/* {{{ proto object mb_iter_free(object iter) + */ +PHP_FUNCTION(mb_iter_free) +{ + zval **arg_obj, *object, **objid; + int type; + mbfl_convert_filter *filter; + + PHP_MB_ERRRST(); + arg_obj = NULL; + if (ZEND_NUM_ARGS() != 0 && + (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg_obj) == FAILURE)) { + WRONG_PARAM_COUNT; + } + + PHP_MB_CHECK_OBJECT(object, arg_obj); + objid = NULL; + type = 0; + filter = NULL; + if (zend_hash_index_find(Z_OBJPROP_P(object), 0, (void **) &objid) == SUCCESS) { + filter = zend_list_find(Z_RESVAL_PP(objid), &type); + } + if (type != le_filter) { + PHP_MB_ERRSET(PHP_MB_ERR_ILLEGAL_ARGUMENT); + php_mb_error(); + RETURN_FALSE; + } + zend_hash_clean(Z_OBJPROP_P(object)); + RETVAL_TRUE; +} +/* }}} */ + +/* {{{ proto int mb_iter_set_index(object iter, int index) + */ +PHP_FUNCTION(mb_iter_set_index) +{ + zval **arg_obj, **arg_index, *object, *props[MBITER_PROP_COUNT]; + int n, pos; + unsigned char *p; + mbfl_convert_filter *filter; + struct iter_collector_data cd; + + PHP_MB_ERRRST(); + arg_obj = NULL; + if ((ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg_index) == FAILURE) && + (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &arg_obj, &arg_index) == FAILURE)) { + WRONG_PARAM_COUNT; + } + + PHP_MB_CHECK_OBJECT(object, arg_obj); + filter = NULL; + if (iter_fetch_propertys(object, props, &filter, &cd) == FAILURE) { + RETURN_FALSE; + } + convert_to_long_ex(arg_index); + pos = Z_LVAL_PP(arg_index); + if (cd.index == pos) { + RETURN_TRUE; + } else if (cd.index > pos) { + cd.index = 0; + cd.byte = 0; + cd.curbyte = 0; + cd.nextbyte = 0; + } + filter->stream_resource = &cd; + p = Z_STRVAL_P(props[MBITER_PROP_TEXT]); + p = &p[cd.nextbyte]; + n = Z_STRLEN_P(props[MBITER_PROP_TEXT]) - cd.nextbyte; + while (n > 0 && cd.index <= pos) { + cd.byte++; + PHP_MB_CONV_FILT_PUTC(*p, filter); + p++; + n--; + } + cd.nextbyte = Z_STRLEN_P(props[MBITER_PROP_TEXT]) - n; + iter_save_propertys(props, &cd); + RETVAL_LONG(cd.curchar); +} +/* }}} */ + +static void +php_mb_iter_get_property(INTERNAL_FUNCTION_PARAMETERS, int id) +{ + zval **arg_obj, *object, *props[MBITER_PROP_COUNT]; + int n; + + arg_obj = NULL; + PHP_MB_ERRRST(); + if (ZEND_NUM_ARGS() != 0 && + (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg_obj) == FAILURE)) { + WRONG_PARAM_COUNT; + } + + PHP_MB_CHECK_OBJECT(object, arg_obj); + if (iter_fetch_propertys(object, props, NULL, NULL) == FAILURE) { + RETURN_FALSE; + } + n = Z_LVAL_P(props[id]); + if (id == MBITER_PROP_CURIND) { + n--; + } + RETVAL_LONG(n); +} + +/* {{{ proto int mb_iter_get_index(object iter) + */ +PHP_FUNCTION(mb_iter_get_index) +{ + php_mb_iter_get_property(INTERNAL_FUNCTION_PARAM_PASSTHRU, MBITER_PROP_CURIND); +} +/* }}} */ + +/* {{{ proto int mb_iter_current_byte(object iter) + */ +PHP_FUNCTION(mb_iter_current_byte) +{ + php_mb_iter_get_property(INTERNAL_FUNCTION_PARAM_PASSTHRU, MBITER_PROP_CURBYTE); +} +/* }}} */ + +/* {{{ proto int mb_iter_next_byte(object iter) + */ +PHP_FUNCTION(mb_iter_next_byte) +{ + php_mb_iter_get_property(INTERNAL_FUNCTION_PARAM_PASSTHRU, MBITER_PROP_NEXTBYTE); +} +/* }}} */ + +/* {{{ proto int mb_iter_begin_index(object iter) + */ +PHP_FUNCTION(mb_iter_begin_index) +{ + php_mb_iter_get_property(INTERNAL_FUNCTION_PARAM_PASSTHRU, MBITER_PROP_BEGIND); +} +/* }}} */ + +/* {{{ proto object mb_iter_end_index(object iter) + */ +PHP_FUNCTION(mb_iter_end_index) +{ + php_mb_iter_get_property(INTERNAL_FUNCTION_PARAM_PASSTHRU, MBITER_PROP_ENDIND); +} +/* }}} */ + +/* {{{ proto object mb_iter_current(object iter) + */ +PHP_FUNCTION(mb_iter_current) +{ + php_mb_iter_get_property(INTERNAL_FUNCTION_PARAM_PASSTHRU, MBITER_PROP_CURCHAR); +} +/* }}} */ + +/* {{{ proto int mb_iter_first(object iter) + */ +PHP_FUNCTION(mb_iter_first) +{ + zval **arg_obj, *object, *props[MBITER_PROP_COUNT]; + int n, pos; + unsigned char *p; + mbfl_convert_filter *filter; + struct iter_collector_data cd; + + PHP_MB_ERRRST(); + arg_obj = NULL; + if (ZEND_NUM_ARGS() != 0 && + (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(2, &arg_obj) == FAILURE)) { + WRONG_PARAM_COUNT; + } + + PHP_MB_CHECK_OBJECT(object, arg_obj); + filter = NULL; + if (iter_fetch_propertys(object, props, &filter, &cd) == FAILURE) { + RETURN_FALSE; + } + cd.index = 0; + cd.byte = 0; + cd.curbyte = 0; + cd.nextbyte = 0; + php_mb_conv_filt_clear(filter); + filter->stream_resource = &cd; + pos = Z_LVAL_P(props[MBITER_PROP_BEGIND]); + p = Z_STRVAL_P(props[MBITER_PROP_TEXT]); + n = Z_STRLEN_P(props[MBITER_PROP_TEXT]); + while (n > 0 && cd.index <= pos) { + cd.byte++; + PHP_MB_CONV_FILT_PUTC(*p, filter); + p++; + n--; + } + cd.nextbyte = Z_STRLEN_P(props[MBITER_PROP_TEXT]) - n; + iter_save_propertys(props, &cd); + RETVAL_LONG(cd.curchar); +} +/* }}} */ + +/* {{{ proto object mb_iter_next(object iter) + */ +PHP_FUNCTION(mb_iter_next) +{ + zval **arg_obj, *object, *props[MBITER_PROP_COUNT]; + int n, pos; + unsigned char *p; + mbfl_convert_filter *filter; + struct iter_collector_data cd; + + PHP_MB_ERRRST(); + arg_obj = NULL; + if (ZEND_NUM_ARGS() != 0 && + (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(2, &arg_obj) == FAILURE)) { + WRONG_PARAM_COUNT; + } + + PHP_MB_CHECK_OBJECT(object, arg_obj); + filter = NULL; + if (iter_fetch_propertys(object, props, &filter, &cd) == FAILURE) { + RETURN_FALSE; + } + pos = cd.index; + n = Z_LVAL_P(props[MBITER_PROP_ENDIND]); + if (n >= 0 && pos >= n) { + RETURN_FALSE; + } + filter->stream_resource = &cd; + p = Z_STRVAL_P(props[MBITER_PROP_TEXT]); + p = &p[cd.nextbyte]; + n = Z_STRLEN_P(props[MBITER_PROP_TEXT]) - cd.nextbyte; + while (n > 0 && cd.index <= pos) { + cd.byte++; + PHP_MB_CONV_FILT_PUTC(*p, filter); + p++; + n--; + } + cd.nextbyte = Z_STRLEN_P(props[MBITER_PROP_TEXT]) - n; + iter_save_propertys(props, &cd); + if (cd.index > pos) { + RETVAL_LONG(cd.curchar); + } else { + RETVAL_FALSE; + } +} +/* }}} */ + + + +/* + * mbBuffer Object + */ + +#define PHP_MB_FETCH_BUFFER(obj, id, t, buf) \ +{ \ + if (zend_hash_index_find(Z_OBJPROP_P(obj), 0, (void **) &id) == SUCCESS) { \ + buf = zend_list_find(Z_RESVAL_PP(id), &t); \ + } \ + if (t != le_buffer) { \ + PHP_MB_ERRSET(PHP_MB_ERR_ILLEGAL_ARGUMENT); \ + php_mb_error(); \ + RETURN_FALSE; \ + } \ +} + +/* {{{ proto object mb_buf_create([mixed object]) + Create mbBuffer object */ +PHP_FUNCTION(mb_buf_create) +{ + zval **arg1, **objid, *result, *prop; + int type; + php_mb_buf *buffer, *src; + + PHP_MB_ERRRST(); + if (ZEND_NUM_ARGS() != 0 && + (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg1) == FAILURE)) { + WRONG_PARAM_COUNT; + } + + objid = NULL; + type = 0; + src = NULL; + buffer = NULL; + if (ZEND_NUM_ARGS() == 1) { + switch (Z_TYPE_PP(arg1)) { + case IS_OBJECT: + if (zend_hash_index_find(Z_OBJPROP_PP(arg1), 0, (void **) &objid) == SUCCESS) { + src = zend_list_find(Z_RESVAL_PP(objid), &type); + } + if (type == le_buffer) { + buffer= php_mb_buf_create(); + if (buffer != NULL) { + php_mb_buf_concat(buffer, src); + } + break; + } + /* fall through */ + default: + convert_to_string_ex(arg1); + buffer = php_mb_convert_internal_to_wchar(Z_STRVAL_PP(arg1), Z_STRLEN_PP(arg1)); + break; + } + } else { + buffer= php_mb_buf_create(); + } + if (buffer == NULL) { + php_mb_error(); + RETURN_FALSE; + } + php_mb_buf_nullpad(buffer); + + result = getThis(); + if (result == NULL) { + result = return_value; + object_init_ex(result, mbbuffer_class_entry); + } + MAKE_STD_ZVAL(prop); + ZEND_REGISTER_RESOURCE(prop, buffer, le_buffer); + zend_hash_index_update(Z_OBJPROP_P(result), 0, &prop, sizeof(zval *), NULL); +} +/* }}} */ + +/* {{{ proto void mb_buf_free(object buffer) + */ +PHP_FUNCTION(mb_buf_free) +{ + zval **arg_obj, *object, **objid; + int type; + php_mb_buf *buffer; + + PHP_MB_ERRRST(); + arg_obj = NULL; + if (ZEND_NUM_ARGS() != 0 && + (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg_obj) == FAILURE)) { + WRONG_PARAM_COUNT; + } + + objid = NULL; + type = 0; + buffer = NULL; + PHP_MB_CHECK_OBJECT(object, arg_obj); + PHP_MB_FETCH_BUFFER(object, objid, type, buffer); + zend_hash_clean(Z_OBJPROP_P(object)); + RETVAL_TRUE; +} +/* }}} */ + +/* {{{ proto int mb_buf_capacity(object buffer) + */ +PHP_FUNCTION(mb_buf_capacity) +{ + zval **arg_obj, *object, **objid; + int type; + php_mb_buf *buffer; + + PHP_MB_ERRRST(); + arg_obj = NULL; + if (ZEND_NUM_ARGS() != 0 && + (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg_obj) == FAILURE)) { + WRONG_PARAM_COUNT; + } + + objid = NULL; + type = 0; + buffer = NULL; + PHP_MB_CHECK_OBJECT(object, arg_obj); + PHP_MB_FETCH_BUFFER(object, objid, type, buffer); + RETVAL_LONG(buffer->capacity); +} +/* }}} */ + +/* {{{ proto int mb_buf_ensure_capacity(object buffer, int minsize) + */ +PHP_FUNCTION(mb_buf_ensure_capacity) +{ + zval **arg_obj, **arg_min, *object, **objid; + int type; + php_mb_buf *buffer; + + PHP_MB_ERRRST(); + arg_obj = NULL; + if ((ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg_obj) == FAILURE) && + (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &arg_obj, &arg_min) == FAILURE)) { + WRONG_PARAM_COUNT; + } + + objid = NULL; + type = 0; + buffer = NULL; + PHP_MB_CHECK_OBJECT(object, arg_obj); + PHP_MB_FETCH_BUFFER(object, objid, type, buffer); + convert_to_long_ex(arg_min); + if (php_mb_buf_allocate(buffer, Z_LVAL_PP(arg_min)) == SUCCESS) { + RETVAL_TRUE; + } else { + RETVAL_FALSE; + } +} +/* }}} */ + +/* {{{ proto array mb_buf_sleep() + */ +PHP_FUNCTION(mb_buf_sleep) +{ + zval *object, **objid, *prop; + int type, *p, n; + php_mb_buf *buffer; + php_mb_str *tmpstr; + mbfl_convert_filter *filt_utf8, *filt_b64; + + PHP_MB_ERRRST(); + if (ZEND_NUM_ARGS() != 0) { + WRONG_PARAM_COUNT; + } + + objid = NULL; + type = 0; + buffer = NULL; + PHP_MB_CHECK_OBJECT(object, objid); + PHP_MB_FETCH_BUFFER(object, objid, type, buffer); + tmpstr = php_mb_str_create(); + if (tmpstr == NULL) { + php_mb_error(); + RETURN_FALSE; + } + filt_b64 = php_mb_conv_filt_create(MBSTRG(wchar_encoding), php_mb_enc_get(php_mb_encid_base64), php_mb_str_output, NULL, tmpstr); + if (filt_b64 == NULL) { + php_mb_str_free(tmpstr); + php_mb_error(); + RETURN_FALSE; + } + filt_utf8 = php_mb_conv_filt_create(MBSTRG(wchar_encoding), php_mb_enc_get(php_mb_encid_utf8inner), (int (*)(int, void*))filt_b64->filter_function, NULL, filt_b64); + if (filt_utf8 == NULL) { + php_mb_conv_filt_free(filt_b64); + php_mb_str_free(tmpstr); + php_mb_error(); + RETURN_FALSE; + } + filt_b64->mode |= MBFL_CONVFILTER_MIME_MODE_NO_CRLE; + PHP_MB_STR_TSRMLS_SET(tmpstr); + PHP_MB_FILT_TSRMLS_SET(filt_b64); + PHP_MB_FILT_TSRMLS_SET(filt_utf8); + p = buffer->value; + n = buffer->length; + while (n > 0) { + PHP_MB_CONV_FILT_PUTC(*p, filt_utf8); + p++; + n--; + } + php_mb_conv_filt_flush(filt_utf8); + php_mb_conv_filt_flush(filt_b64); + php_mb_conv_filt_free(filt_utf8); + php_mb_conv_filt_free(filt_b64); + php_mb_str_nullpad(tmpstr); + MAKE_STD_ZVAL(prop); + ZVAL_STRINGL(prop, tmpstr->value, tmpstr->length, 0); + zend_hash_update(Z_OBJPROP_P(object), "s", sizeof("s"), &prop, sizeof(zval *), NULL); + tmpstr->value = NULL; + php_mb_str_free(tmpstr); + array_init(return_value); + add_next_index_string(return_value, "s", 1); +} +/* }}} */ + +/* {{{ proto array mb_buf_wakeup() + */ +PHP_FUNCTION(mb_buf_wakeup) +{ + zval *object, **data, *prop; + char *p; + int n; + php_mb_buf *buffer; + mbfl_convert_filter *filt_utf8, *filt_b64; + + PHP_MB_ERRRST(); + if (ZEND_NUM_ARGS() != 0) { + WRONG_PARAM_COUNT; + } + + data = NULL; + PHP_MB_CHECK_OBJECT(object, data); + if (zend_hash_find(Z_OBJPROP_P(object), "s", sizeof("s"), (void **) &data) == FAILURE) { + RETURN_FALSE; + } + buffer = php_mb_buf_create(); + if (buffer == NULL) { + php_mb_error(); + RETURN_FALSE; + } + filt_utf8 = php_mb_conv_filt_create(php_mb_enc_get(php_mb_encid_utf8inner), MBSTRG(wchar_encoding), php_mb_buf_output, NULL, buffer); + if (filt_utf8 == NULL) { + php_mb_buf_free(buffer); + php_mb_error(); + RETURN_FALSE; + } + filt_b64 = php_mb_conv_filt_create(php_mb_enc_get(php_mb_encid_base64), MBSTRG(wchar_encoding), (int (*)(int, void*))filt_utf8->filter_function, NULL, filt_utf8); + if (filt_b64 == NULL) { + php_mb_conv_filt_free(filt_utf8); + php_mb_buf_free(buffer); + php_mb_error(); + RETURN_FALSE; + } + PHP_MB_STR_TSRMLS_SET(buffer); + PHP_MB_FILT_TSRMLS_SET(filt_b64); + PHP_MB_FILT_TSRMLS_SET(filt_utf8); + p = Z_STRVAL_PP(data); + n = Z_STRLEN_PP(data); + while (n > 0) { + PHP_MB_CONV_FILT_PUTC(*p, filt_b64); + p++; + n--; + } + php_mb_conv_filt_flush(filt_b64); + php_mb_conv_filt_flush(filt_utf8); + php_mb_conv_filt_free(filt_b64); + php_mb_conv_filt_free(filt_utf8); + php_mb_buf_nullpad(buffer); + MAKE_STD_ZVAL(prop); + ZEND_REGISTER_RESOURCE(prop, buffer, le_buffer); + zend_hash_index_update(Z_OBJPROP_P(object), 0, &prop, sizeof(zval *), NULL); + zend_hash_del(Z_OBJPROP_P(object), "s", sizeof("s")); + RETVAL_TRUE; +} +/* }}} */ + +/* {{{ proto string mb_buf_to_string(object buffer) + */ +PHP_FUNCTION(mb_buf_to_string) +{ + zval **arg_obj, *object, **objid; + int type, result_len; + char *result_val; + php_mb_buf *buffer; + + PHP_MB_ERRRST(); + arg_obj = NULL; + if (ZEND_NUM_ARGS() != 0 && + (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg_obj) == FAILURE)) { + WRONG_PARAM_COUNT; + } + + objid = NULL; + type = 0; + buffer = NULL; + PHP_MB_CHECK_OBJECT(object, arg_obj); + PHP_MB_FETCH_BUFFER(object, objid, type, buffer); + result_val = php_mb_convert_wchar_to_internal(buffer, &result_len); + if (result_val != NULL) { + RETVAL_STRINGL(result_val, result_len, 0); + } else { + php_mb_error(); + RETVAL_FALSE; + } +} +/* }}} */ + +/* {{{ proto string mb_buf_encode(object buffer, string encoding) + */ +PHP_FUNCTION(mb_buf_encode) +{ + zval **arg_obj, **arg_enc, *object, **objid; + int type, result_len; + char *result_val; + php_mb_enc *encoding; + php_mb_buf *buffer; + + PHP_MB_ERRRST(); + arg_obj = NULL; + if ((ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg_enc) == FAILURE) && + (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &arg_obj, &arg_enc) == FAILURE)) { + WRONG_PARAM_COUNT; + } + + objid = NULL; + type = 0; + buffer = NULL; + PHP_MB_CHECK_OBJECT(object, arg_obj); + PHP_MB_FETCH_BUFFER(object, objid, type, buffer); + convert_to_string_ex(arg_enc); + encoding = php_mb_enc_resolve(Z_STRVAL_PP(arg_enc)); + if (encoding == NULL) { + php_mb_error(); + RETURN_FALSE; + } + result_val = php_mb_convert_wchar_to_encoding(buffer, encoding, &result_len); + if (result_val != NULL) { + RETVAL_STRINGL(result_val, result_len, 0); + } else { + php_mb_error(); + RETVAL_FALSE; + } +} +/* }}} */ + +/* {{{ proto bool mb_buf_equals(object buffer, mixed text) + */ +PHP_FUNCTION(mb_buf_equals) +{ + zval **arg_obj, **arg_text, *object, **objid; + int type, result, *p1, *p2, n; + php_mb_buf *buffer1, *buffer2, *tmpbuf; + + PHP_MB_ERRRST(); + arg_obj = NULL; + if ((ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg_text) == FAILURE) && + (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &arg_obj, &arg_text) == FAILURE)) { + WRONG_PARAM_COUNT; + } + + objid = NULL; + type = 0; + buffer1 = NULL; + buffer2 = NULL; + tmpbuf = NULL; + PHP_MB_CHECK_OBJECT(object, arg_obj); + PHP_MB_FETCH_BUFFER(object, objid, type, buffer1); + switch (Z_TYPE_PP(arg_text)) { + case IS_OBJECT: + objid = NULL; + type = 0; + if (zend_hash_index_find(Z_OBJPROP_PP(arg_text), 0, (void **) &objid) == SUCCESS) { + buffer2 = zend_list_find(Z_RESVAL_PP(objid), &type); + } + if (type == le_buffer) { + break; + } + /* fall through */ + default: + convert_to_string_ex(arg_text); + tmpbuf = php_mb_convert_internal_to_wchar(Z_STRVAL_PP(arg_text), Z_STRLEN_PP(arg_text)); + if (tmpbuf == NULL) { + php_mb_error(); + RETURN_FALSE; + } + buffer2 = tmpbuf; + break; + } + + result = 0; + if (buffer1->length == buffer2->length) { + result = 1; + p1 = buffer1->value; + p2 = buffer2->value; + n = buffer1->length; + while (n-- > 0) { + if (*p1 != *p2) { + result = 0; + break; + } + } + } + + if (result == 1) { + RETVAL_TRUE; + } else { + RETVAL_FALSE; + } + if (tmpbuf != NULL) { + php_mb_buf_free(tmpbuf); + } +} +/* }}} */ + +/* {{{ proto string mb_buf_get_at(object buffer, int index) + */ +PHP_FUNCTION(mb_buf_get_at) +{ + zval **arg_obj, **arg_index, *object, **objid; + int type, result_len; + char *result_val; + php_mb_buf *buffer, *tmpbuf; + + PHP_MB_ERRRST(); + arg_obj = NULL; + if ((ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg_index) == FAILURE) && + (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &arg_obj, &arg_index) == FAILURE)) { + WRONG_PARAM_COUNT; + } + + objid = NULL; + type = 0; + buffer = NULL; + PHP_MB_CHECK_OBJECT(object, arg_obj); + PHP_MB_FETCH_BUFFER(object, objid, type, buffer); + convert_to_long_ex(arg_index); + if (Z_LVAL_PP(arg_index) < 0 || Z_LVAL_PP(arg_index) >= buffer->length) { + PHP_MB_ERRSET(PHP_MB_ERR_INDEX_OUT_OF_BOUNDS); + php_mb_error(); + RETURN_FALSE; + } + + tmpbuf = php_mb_buf_create(); + if (tmpbuf == NULL) { + php_mb_error(); + RETURN_FALSE; + } + php_mb_buf_append_char(tmpbuf, buffer->value[Z_LVAL_PP(arg_index)]); + + result_val = php_mb_convert_wchar_to_encoding(tmpbuf, MBSTRG(internal_encoding_r), &result_len); + if (result_val != NULL) { + RETVAL_STRINGL(result_val, result_len, 0); + } else { + php_mb_error(); + RETVAL_FALSE; + } + php_mb_buf_free(tmpbuf); +} +/* }}} */ + +/* {{{ proto bool mb_buf_set_at(object mbbuffer, int index, mixed char) + */ +PHP_FUNCTION(mb_buf_set_at) +{ + zval **arg_obj, **arg_index, **arg_char, *object, **objid; + int type; + php_mb_buf *buffer; + + PHP_MB_ERRRST(); + arg_obj = NULL; + if ((ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &arg_index, &arg_char) == FAILURE) && + (ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &arg_obj, &arg_index, &arg_char) == FAILURE)) { + WRONG_PARAM_COUNT; + } + + objid = NULL; + type = 0; + buffer = NULL; + PHP_MB_CHECK_OBJECT(object, arg_obj); + PHP_MB_FETCH_BUFFER(object, objid, type, buffer); + convert_to_long_ex(arg_index); + if (Z_LVAL_PP(arg_index) < 0 || Z_LVAL_PP(arg_index) >= buffer->length || buffer->value == NULL) { + PHP_MB_ERRSET(PHP_MB_ERR_INDEX_OUT_OF_BOUNDS); + php_mb_error(); + RETURN_FALSE; + } + convert_to_long_ex(arg_char); + buffer->value[Z_LVAL_PP(arg_index)] = Z_LVAL_PP(arg_char); + RETVAL_TRUE; +} +/* }}} */ + +/* {{{ proto object mb_buf_append(object buffer, mixed src) + */ +PHP_FUNCTION(mb_buf_append) +{ + zval **arg_obj, **arg_src, *object, **objid; + int type; + php_mb_buf *buffer1, *buffer2, *tmpbuf; + + PHP_MB_ERRRST(); + arg_obj = NULL; + if ((ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg_src) == FAILURE) && + (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &arg_obj, &arg_src) == FAILURE)) { + WRONG_PARAM_COUNT; + } + + objid = NULL; + type = 0; + buffer1 = NULL; + buffer2 = NULL; + tmpbuf = NULL; + PHP_MB_CHECK_OBJECT(object, arg_obj); + PHP_MB_FETCH_BUFFER(object, objid, type, buffer1); + switch (Z_TYPE_PP(arg_src)) { + case IS_OBJECT: + objid = NULL; + type = 0; + if (zend_hash_index_find(Z_OBJPROP_PP(arg_src), 0, (void **) &objid) == SUCCESS) { + buffer2 = zend_list_find(Z_RESVAL_PP(objid), &type); + } + if (type == le_buffer) { + break; + } + /* fall through */ + default: + convert_to_string_ex(arg_src); + tmpbuf = php_mb_convert_internal_to_wchar(Z_STRVAL_PP(arg_src), Z_STRLEN_PP(arg_src)); + buffer2 = tmpbuf; + break; + } + if (buffer2 != NULL) { + php_mb_buf_concat(buffer1, buffer2); + } + if (tmpbuf != NULL) { + php_mb_buf_free(tmpbuf); + } + if (PHP_MB_IS_ERROR()) { + php_mb_error(); + } + if (return_value_used) { + *return_value = *object; + zval_copy_ctor(return_value); + } +} +/* }}} */ + +/* {{{ proto object mb_buf_delete(object buffer, int start, int end) + */ +PHP_FUNCTION(mb_buf_delete) +{ + zval **arg_obj, **arg_start, **arg_end, *object, **objid; + int type; + php_mb_buf *buffer; + + PHP_MB_ERRRST(); + arg_obj = NULL; + if ((ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &arg_start, &arg_end) == FAILURE) && + (ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &arg_obj, &arg_start, &arg_end) == FAILURE)) { + WRONG_PARAM_COUNT; + } + + objid = NULL; + type = 0; + buffer = NULL; + PHP_MB_CHECK_OBJECT(object, arg_obj); + PHP_MB_FETCH_BUFFER(object, objid, type, buffer); + convert_to_long_ex(arg_start); + convert_to_long_ex(arg_end); + php_mb_buf_delete(buffer, Z_LVAL_PP(arg_start), Z_LVAL_PP(arg_end)); + if (PHP_MB_IS_ERROR()) { + php_mb_error(); + } + if (return_value_used) { + *return_value = *object; + zval_copy_ctor(return_value); + } +} +/* }}} */ + +/* {{{ proto object mb_buf_insert(object buffer, int offset, mixed src) + */ +PHP_FUNCTION(mb_buf_insert) +{ + zval **arg_obj, **arg_offset, **arg_src, *object, **objid; + int type; + php_mb_buf *buffer1, *buffer2, *tmpbuf; + + PHP_MB_ERRRST(); + arg_obj = NULL; + if ((ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &arg_offset, &arg_src) == FAILURE) && + (ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &arg_obj, &arg_offset, &arg_src) == FAILURE)) { + WRONG_PARAM_COUNT; + } + + objid = NULL; + type = 0; + buffer1 = NULL; + buffer2 = NULL; + tmpbuf = NULL; + PHP_MB_CHECK_OBJECT(object, arg_obj); + PHP_MB_FETCH_BUFFER(object, objid, type, buffer1); + switch (Z_TYPE_PP(arg_src)) { + case IS_OBJECT: + objid = NULL; + type = 0; + if (zend_hash_index_find(Z_OBJPROP_PP(arg_src), 0, (void **) &objid) == SUCCESS) { + buffer2 = zend_list_find(Z_RESVAL_PP(objid), &type); + } + if (type == le_buffer) { + break; + } + /* fall through */ + default: + convert_to_string_ex(arg_src); + tmpbuf = php_mb_convert_internal_to_wchar(Z_STRVAL_PP(arg_src), Z_STRLEN_PP(arg_src)); + buffer2 = tmpbuf; + break; + } + convert_to_long_ex(arg_offset); + if (buffer2 != NULL) { + php_mb_buf_insert(buffer1, Z_LVAL_PP(arg_offset), buffer2); + } + if (tmpbuf != NULL) { + php_mb_buf_free(tmpbuf); + } + if (PHP_MB_IS_ERROR()) { + php_mb_error(); + } + if (return_value_used) { + *return_value = *object; + zval_copy_ctor(return_value); + } +} +/* }}} */ + +/* {{{ proto object mb_buf_replace(object buffer, int start, int end, mixed src) + */ +PHP_FUNCTION(mb_buf_replace) +{ + zval **arg_obj, **arg_start, **arg_end, **arg_src, *object, **objid; + int type; + php_mb_buf *buffer1, *buffer2, *tmpbuf; + + PHP_MB_ERRRST(); + arg_obj = NULL; + if ((ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &arg_start, &arg_end, &arg_src) == FAILURE) && + (ZEND_NUM_ARGS() != 4 || zend_get_parameters_ex(4, &arg_obj, &arg_start, &arg_end, &arg_src) == FAILURE)) { + WRONG_PARAM_COUNT; + } + + objid = NULL; + type = 0; + buffer1 = NULL; + buffer2 = NULL; + tmpbuf = NULL; + PHP_MB_CHECK_OBJECT(object, arg_obj); + PHP_MB_FETCH_BUFFER(object, objid, type, buffer1); + switch (Z_TYPE_PP(arg_src)) { + case IS_OBJECT: + objid = NULL; + type = 0; + if (zend_hash_index_find(Z_OBJPROP_PP(arg_src), 0, (void **) &objid) == SUCCESS) { + buffer2 = zend_list_find(Z_RESVAL_PP(objid), &type); + } + if (type == le_buffer) { + break; + } + /* fall through */ + default: + convert_to_string_ex(arg_src); + tmpbuf = php_mb_convert_internal_to_wchar(Z_STRVAL_PP(arg_src), Z_STRLEN_PP(arg_src)); + buffer2 = tmpbuf; + break; + } + convert_to_long_ex(arg_start); + convert_to_long_ex(arg_end); + if (buffer2 != NULL) { + php_mb_buf_replace(buffer1, Z_LVAL_PP(arg_start), Z_LVAL_PP(arg_end), buffer2); + } + if (tmpbuf != NULL) { + php_mb_buf_free(tmpbuf); + } + if (PHP_MB_IS_ERROR()) { + php_mb_error(); + } + if (return_value_used) { + *return_value = *object; + zval_copy_ctor(return_value); + } +} +/* }}} */ + +/* {{{ proto object mb_buf_reverse(object buffer, int start, int end) + */ +PHP_FUNCTION(mb_buf_reverse) +{ + zval **arg_obj, **arg_start, **arg_end, *object, **objid; + int type; + php_mb_buf *buffer; + + PHP_MB_ERRRST(); + arg_obj = NULL; + if ((ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &arg_start, &arg_end) == FAILURE) && + (ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &arg_obj, &arg_start, &arg_end) == FAILURE)) { + WRONG_PARAM_COUNT; + } + + objid = NULL; + type = 0; + buffer = NULL; + PHP_MB_CHECK_OBJECT(object, arg_obj); + PHP_MB_FETCH_BUFFER(object, objid, type, buffer); + convert_to_long_ex(arg_start); + convert_to_long_ex(arg_end); + php_mb_buf_reverse(buffer, Z_LVAL_PP(arg_start), Z_LVAL_PP(arg_end)); + if (PHP_MB_IS_ERROR()) { + php_mb_error(); + } + if (return_value_used) { + *return_value = *object; + zval_copy_ctor(return_value); + } +} +/* }}} */ + +/* {{{ proto object mb_buf_substr(object buffer, int start [, int end]) + */ +PHP_FUNCTION(mb_buf_substr) +{ + zval **arg_obj, **arg_start, **arg_end, *object, **objid; + int type, start, end; + php_mb_buf *buffer, *result; + + PHP_MB_ERRRST(); + arg_obj = NULL; + arg_end = NULL; + object = getThis(); + if (object != NULL) { + if ((ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg_start) == FAILURE) && + (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &arg_start, &arg_end) == FAILURE)) { + WRONG_PARAM_COUNT; + } + } else { + if ((ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &arg_obj, &arg_start) == FAILURE) && + (ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &arg_obj, &arg_start, &arg_end) == FAILURE)) { + WRONG_PARAM_COUNT; + } + } + + objid = NULL; + type = 0; + buffer = NULL; + PHP_MB_CHECK_OBJECT(object, arg_obj); + PHP_MB_FETCH_BUFFER(object, objid, type, buffer); + convert_to_long_ex(arg_start); + start = Z_LVAL_PP(arg_start); + end = buffer->length; + if (arg_end != NULL) { + convert_to_long_ex(arg_end); + end = Z_LVAL_PP(arg_end); + } + if (start < 0 || start > buffer->length || start > end || buffer->value == NULL) { + PHP_MB_ERRSET(PHP_MB_ERR_INDEX_OUT_OF_BOUNDS); + php_mb_error(); + RETURN_FALSE; + } + if (end > buffer->length) { + end = buffer->length; + } + result = php_mb_buf_create(); + if (result == NULL) { + php_mb_error(); + RETURN_FALSE; + } + php_mb_buf_append_wstr(result, &buffer->value[start], end - start); + php_mb_buf_nullpad(result); + if (PHP_MB_IS_ERROR()) { + php_mb_error(); + } + object_init_ex(return_value, Z_OBJCE_P(object)); + MAKE_STD_ZVAL(object); + ZEND_REGISTER_RESOURCE(object, result, le_buffer); + zend_hash_index_update(Z_OBJPROP_P(return_value), 0, &object, sizeof(zval *), NULL); +} +/* }}} */ + +/* {{{ proto object mb_buf_trim(object buffer) + */ +PHP_FUNCTION(mb_buf_trim) +{ + zval **arg_obj, *object, **objid; + int type, length, *p, *start; + php_mb_buf *buffer, *result; + + PHP_MB_ERRRST(); + arg_obj = NULL; + if (ZEND_NUM_ARGS() != 0 && + (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg_obj) == FAILURE)) { + WRONG_PARAM_COUNT; + } + + objid = NULL; + type = 0; + buffer = NULL; + PHP_MB_CHECK_OBJECT(object, arg_obj); + PHP_MB_FETCH_BUFFER(object, objid, type, buffer); + result = php_mb_buf_create(); + if (result == NULL) { + php_mb_error(); + RETURN_FALSE; + } + length = buffer->length; + p = buffer->value; + if (p != NULL) { + while (*p <= 0x20) { + length--; + p++; + } + start = p; + p = &buffer->value[buffer->length - 1]; + while (*p <= 0x20) { + length--; + p--; + } + php_mb_buf_append_wstr(result, start, length); + } + php_mb_buf_nullpad(result); + if (PHP_MB_IS_ERROR()) { + php_mb_error(); + } + + object_init_ex(return_value, Z_OBJCE_P(object)); + MAKE_STD_ZVAL(object); + ZEND_REGISTER_RESOURCE(object, result, le_buffer); + zend_hash_index_update(Z_OBJPROP_P(return_value), 0, &object, sizeof(zval *), NULL); +} +/* }}} */ + +/* {{{ proto bool mb_buf_check_include(object buffer, array codemap) + */ +PHP_FUNCTION(mb_buf_check_include) +{ + zval **arg_obj, **arg_map, *object, **objid; + int type, i, j, c, result, mapsize, *map, *mapelm, *p; + php_mb_buf *buffer; + + PHP_MB_ERRRST(); + arg_obj = NULL; + if ((ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg_map) == FAILURE) && + (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &arg_obj, &arg_map) == FAILURE)) { + WRONG_PARAM_COUNT; + } + + objid = NULL; + type = 0; + buffer = NULL; + PHP_MB_CHECK_OBJECT(object, arg_obj); + PHP_MB_FETCH_BUFFER(object, objid, type, buffer); + + /* map */ + mapsize = 0; + map = php_mb_make_map(arg_map, &mapsize); + mapsize /= 2; + if (map == NULL || mapsize <= 0) { + if (map != NULL) { + efree(map); + } + PHP_MB_ERRSET(PHP_MB_ERR_ILLEGAL_ARGUMENT); + php_mb_error(); + RETURN_FALSE; + } + + result = 0; + p = buffer->value; + i = buffer->length; + if (p != NULL) { + while (i-- > 0) { + result = 0; + c = *p++; + j = 0; + while (j < mapsize) { + mapelm = &map[j*2]; + if (c >= mapelm[0] && c <= mapelm[1]) { + result = 1; + break; + } + j++; + } + if (result == 0) { + break; + } + } + } + efree(map); + + if (result == 0) { + RETVAL_FALSE; + } else { + RETVAL_TRUE; + } +} +/* }}} */ + +/* {{{ proto bool mb_buf_check_exclude(object buffer, array codemap) + */ +PHP_FUNCTION(mb_buf_check_exclude) +{ + zval **arg_obj, **arg_map, *object, **objid; + int type, i, j, c, result, mapsize, *map, *mapelm, *p; + php_mb_buf *buffer; + + PHP_MB_ERRRST(); + arg_obj = NULL; + if ((ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg_map) == FAILURE) && + (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &arg_obj, &arg_map) == FAILURE)) { + WRONG_PARAM_COUNT; + } + + objid = NULL; + type = 0; + buffer = NULL; + PHP_MB_CHECK_OBJECT(object, arg_obj); + PHP_MB_FETCH_BUFFER(object, objid, type, buffer); + + /* map */ + mapsize = 0; + map = php_mb_make_map(arg_map, &mapsize); + mapsize /= 2; + if (map == NULL || mapsize <= 0) { + if (map != NULL) { + efree(map); + } + PHP_MB_ERRSET(PHP_MB_ERR_ILLEGAL_ARGUMENT); + php_mb_error(); + RETURN_FALSE; + } + + result = 1; + p = buffer->value; + i = buffer->length; + if (p != NULL) { + while (i-- > 0) { + result = 0; + c = *p++; + j = 0; + while (j < mapsize) { + mapelm = &map[j*2]; + if (c >= mapelm[0] && c <= mapelm[1]) { + result = 1; + break; + } + j++; + } + if (result == 1) { + break; + } + } + } + efree(map); + + if (result == 0) { + RETVAL_TRUE; + } else { + RETVAL_FALSE; + } +} +/* }}} */ + +/* {{{ proto object mb_buf_pick(object buffer, array codemap) + */ +PHP_FUNCTION(mb_buf_pick) +{ + zval **arg_obj, **arg_map, *object, **objid; + int type, i, j, c, mapsize, *map, *mapelm, *p; + php_mb_buf *buffer, *result; + + PHP_MB_ERRRST(); + arg_obj = NULL; + if ((ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg_map) == FAILURE) && + (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &arg_obj, &arg_map) == FAILURE)) { + WRONG_PARAM_COUNT; + } + + objid = NULL; + type = 0; + buffer = NULL; + PHP_MB_CHECK_OBJECT(object, arg_obj); + PHP_MB_FETCH_BUFFER(object, objid, type, buffer); + + /* map */ + mapsize = 0; + map = php_mb_make_map(arg_map, &mapsize); + mapsize /= 2; + if (map == NULL || mapsize <= 0) { + if (map != NULL) { + efree(map); + } + PHP_MB_ERRSET(PHP_MB_ERR_ILLEGAL_ARGUMENT); + php_mb_error(); + RETURN_FALSE; + } + + result = php_mb_buf_create(); + if (result == NULL) { + php_mb_error(); + efree(map); + RETURN_FALSE; + } + php_mb_buf_adjust_realloc(result, buffer->length + 1); + p = buffer->value; + i = buffer->length; + if (p != NULL) { + while (i-- > 0) { + c = *p++; + j = 0; + while (j < mapsize) { + mapelm = &map[j*2]; + if (c >= mapelm[0] && c <= mapelm[1]) { + php_mb_buf_append_char(result, c); + break; + } + j++; + } + } + } + php_mb_buf_adjust_realloc(result, 0); + php_mb_buf_nullpad(result); + if (PHP_MB_IS_ERROR()) { + php_mb_error(); + } + efree(map); + + object_init_ex(return_value, Z_OBJCE_P(object)); + MAKE_STD_ZVAL(object); + ZEND_REGISTER_RESOURCE(object, result, le_buffer); + zend_hash_index_update(Z_OBJPROP_P(return_value), 0, &object, sizeof(zval *), NULL); +} +/* }}} */ + +/* {{{ proto object mb_buf_remove(object buffer, array codemap) + */ +PHP_FUNCTION(mb_buf_remove) +{ + zval **arg_obj, **arg_map, *object, **objid; + int type, i, j, f, c, mapsize, *map, *mapelm, *p; + php_mb_buf *buffer, *result; + + PHP_MB_ERRRST(); + arg_obj = NULL; + if ((ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg_map) == FAILURE) && + (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &arg_obj, &arg_map) == FAILURE)) { + WRONG_PARAM_COUNT; + } + + objid = NULL; + type = 0; + buffer = NULL; + PHP_MB_CHECK_OBJECT(object, arg_obj); + PHP_MB_FETCH_BUFFER(object, objid, type, buffer); + + /* map */ + mapsize = 0; + map = php_mb_make_map(arg_map, &mapsize); + mapsize /= 2; + if (map == NULL || mapsize <= 0) { + if (map != NULL) { + efree(map); + } + PHP_MB_ERRSET(PHP_MB_ERR_ILLEGAL_ARGUMENT); + php_mb_error(); + RETURN_FALSE; + } + + result = php_mb_buf_create(); + if (result == NULL) { + php_mb_error(); + efree(map); + RETURN_FALSE; + } + php_mb_buf_adjust_realloc(result, buffer->length + 1); + p = buffer->value; + i = buffer->length; + if (p != NULL) { + while (i-- > 0) { + c = *p++; + f = 0; + j = 0; + while (j < mapsize) { + mapelm = &map[j*2]; + if (c >= mapelm[0] && c <= mapelm[1]) { + f = 1; + break; + } + j++; + } + if (f == 0) { + php_mb_buf_append_char(result, c); + } + } + } + php_mb_buf_adjust_realloc(result, 0); + php_mb_buf_nullpad(result); + if (PHP_MB_IS_ERROR()) { + php_mb_error(); + } + efree(map); + + object_init_ex(return_value, Z_OBJCE_P(object)); + MAKE_STD_ZVAL(object); + ZEND_REGISTER_RESOURCE(object, result, le_buffer); + zend_hash_index_update(Z_OBJPROP_P(return_value), 0, &object, sizeof(zval *), NULL); +} +/* }}} */ + +/* {{{ proto object mb_buf_edit(object buffer, array editmap) + */ +PHP_FUNCTION(mb_buf_edit) +{ + zval **arg_obj, **arg_map, *object, **objid; + int type, i, j, f, c, mapsize, *map, *mapelm, *p; + php_mb_buf *buffer, *result; + + PHP_MB_ERRRST(); + arg_obj = NULL; + if ((ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg_map) == FAILURE) && + (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &arg_obj, &arg_map) == FAILURE)) { + WRONG_PARAM_COUNT; + } + + objid = NULL; + type = 0; + buffer = NULL; + PHP_MB_CHECK_OBJECT(object, arg_obj); + PHP_MB_FETCH_BUFFER(object, objid, type, buffer); + + /* map */ + mapsize = 0; + map = php_mb_make_map(arg_map, &mapsize); + mapsize /= 5; + if (map == NULL || mapsize <= 0) { + if (map != NULL) { + efree(map); + } + PHP_MB_ERRSET(PHP_MB_ERR_ILLEGAL_ARGUMENT); + php_mb_error(); + RETURN_FALSE; + } + + result = php_mb_buf_create(); + if (result == NULL) { + php_mb_error(); + efree(map); + RETURN_FALSE; + } + php_mb_buf_adjust_realloc(result, buffer->length + 1); + p = buffer->value; + i = buffer->length; + if (p != NULL) { + while (i-- > 0) { + c = *p++; + f = 0; + j = 0; + while (j < mapsize) { + mapelm = &map[j*5]; + if (c >= mapelm[0] && c <= mapelm[1]) { + c &= mapelm[2]; + c |= mapelm[3]; + c += mapelm[4]; + f = 1; + break; + } + j++; + } + if (f == 1) { + if (c >= 0) { + php_mb_buf_append_char(result, c); + } + } else { + php_mb_buf_append_char(result, c); + } + } + } + php_mb_buf_adjust_realloc(result, 0); + php_mb_buf_nullpad(result); + if (PHP_MB_IS_ERROR()) { + php_mb_error(); + } + efree(map); + + object_init_ex(return_value, Z_OBJCE_P(object)); + MAKE_STD_ZVAL(object); + ZEND_REGISTER_RESOURCE(object, result, le_buffer); + zend_hash_index_update(Z_OBJPROP_P(return_value), 0, &object, sizeof(zval *), NULL); +} +/* }}} */ + +/* {{{ proto object mb_buf_iterate(object buffer, string function) + */ +PHP_FUNCTION(mb_buf_iterate) +{ + zval **arg_obj, **arg_fn, *object, **objid, **object_pp, **args[1], *arg1, *retval; + void *ptr; + int type, i, *p; + php_mb_buf *buffer, *result, *tmpbuf; + + PHP_MB_ERRRST(); + arg_obj = NULL; + if ((ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg_fn) == FAILURE) && + (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &arg_obj, &arg_fn) == FAILURE)) { + WRONG_PARAM_COUNT; + } + + objid = NULL; + type = 0; + buffer = NULL; + PHP_MB_CHECK_OBJECT(object, arg_obj); + PHP_MB_FETCH_BUFFER(object, objid, type, buffer); + + convert_to_string_ex(arg_fn); + object_pp = NULL; + if (zend_hash_find(&Z_OBJCE_P(object)->function_table, Z_STRVAL_PP(arg_fn), Z_STRLEN_PP(arg_fn) + 1, &ptr) == SUCCESS) { + object_pp = &object; + } else if (zend_hash_find(EG(function_table), Z_STRVAL_PP(arg_fn), Z_STRLEN_PP(arg_fn) + 1, &ptr) == SUCCESS) { + ; + } else { + PHP_MB_ERRSET(PHP_MB_ERR_ILLEGAL_ARGUMENT); + php_mb_error(); + RETURN_FALSE; + } + + result = php_mb_buf_create(); + if (result == NULL) { + php_mb_error(); + RETURN_FALSE; + } + php_mb_buf_adjust_realloc(result, buffer->length + 1); + MAKE_STD_ZVAL(arg1); + Z_TYPE_P(arg1) = IS_LONG; + Z_LVAL_P(arg1) = 0; + args[0] = &arg1; + p = buffer->value; + i = buffer->length; + if (p != NULL) { + while (i-- > 0) { + Z_LVAL_P(arg1) = *p++; + if (call_user_function_ex(EG(function_table), object_pp, *arg_fn, + &retval, 1, args, 0, NULL TSRMLS_CC) == SUCCESS) { + switch (Z_TYPE_P(retval)) { + case IS_LONG: + php_mb_buf_append_char(result, Z_LVAL_P(retval)); + break; + case IS_OBJECT: + objid = NULL; + type = 0; + tmpbuf = NULL; + if (zend_hash_index_find(Z_OBJPROP_P(retval), 0, (void **) &objid) == SUCCESS) { + tmpbuf = zend_list_find(Z_RESVAL_PP(objid), &type); + } + if (type == le_buffer) { + php_mb_buf_concat(result, tmpbuf); + } + break; + case IS_NULL: + break; + case IS_BOOL: + if (Z_BVAL_P(retval)) { + php_mb_buf_append_wstr(result, p, i); + } + zval_ptr_dtor(&retval); + goto end; + break; + default: + convert_to_string_ex(&retval); + tmpbuf = php_mb_convert_internal_to_wchar(Z_STRVAL_P(retval), Z_STRLEN_P(retval)); + if (tmpbuf == NULL) { + php_mb_error(); + RETURN_FALSE; + } + php_mb_buf_concat(result, tmpbuf); + php_mb_buf_free(tmpbuf); + break; + } + zval_ptr_dtor(&retval); + } + } + } +end: + php_mb_buf_adjust_realloc(result, 0); + php_mb_buf_nullpad(result); + if (PHP_MB_IS_ERROR()) { + php_mb_error(); + } + efree(arg1); + + object_init_ex(return_value, Z_OBJCE_P(object)); + MAKE_STD_ZVAL(object); + ZEND_REGISTER_RESOURCE(object, result, le_buffer); + zend_hash_index_update(Z_OBJPROP_P(return_value), 0, &object, sizeof(zval *), NULL); +} +/* }}} */ + +/* {{{ proto int mb_buf_length(object buffer) + */ +PHP_FUNCTION(mb_buf_length) +{ + zval **arg_obj, *object, **objid; + int type; + php_mb_buf *buffer; + + PHP_MB_ERRRST(); + arg_obj = NULL; + if (ZEND_NUM_ARGS() != 0 && + (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg_obj) == FAILURE)) { + WRONG_PARAM_COUNT; + } + + objid = NULL; + type = 0; + buffer = NULL; + PHP_MB_CHECK_OBJECT(object, arg_obj); + PHP_MB_FETCH_BUFFER(object, objid, type, buffer); + RETVAL_LONG(buffer->length); +} +/* }}} */ + +/* {{{ proto int mb_buf_index_of(object buffer, mixed search [, int fromindex]) + */ +PHP_FUNCTION(mb_buf_index_of) +{ + zval **arg_obj, **arg_search, **arg_from, *object, **objid; + int type, n, i, len, *p, *p1, *p2, *end, *search; + php_mb_buf *buffer1, *buffer2, *tmpbuf; + + PHP_MB_ERRRST(); + arg_obj = NULL; + arg_from = NULL; + object = getThis(); + if (object != NULL) { + if ((ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg_search) == FAILURE) && + (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &arg_search, &arg_from) == FAILURE)) { + WRONG_PARAM_COUNT; + } + } else { + if ((ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &arg_obj, &arg_search) == FAILURE) && + (ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &arg_obj, &arg_search, &arg_from) == FAILURE)) { + WRONG_PARAM_COUNT; + } + } + + objid = NULL; + type = 0; + buffer1 = NULL; + buffer2 = NULL; + tmpbuf = NULL; + PHP_MB_CHECK_OBJECT(object, arg_obj); + PHP_MB_FETCH_BUFFER(object, objid, type, buffer1); + switch (Z_TYPE_PP(arg_search)) { + case IS_OBJECT: + objid = NULL; + type = 0; + if (zend_hash_index_find(Z_OBJPROP_PP(arg_search), 0, (void **) &objid) == SUCCESS) { + buffer2 = zend_list_find(Z_RESVAL_PP(objid), &type); + } + if (type == le_buffer) { + break; + } + /* fall through */ + default: + convert_to_string_ex(arg_search); + tmpbuf = php_mb_convert_internal_to_wchar(Z_STRVAL_PP(arg_search), Z_STRLEN_PP(arg_search)); + buffer2 = tmpbuf; + break; + } + if (buffer2 == NULL) { + php_mb_error(); + RETURN_FALSE; + } + n = 0; + if (arg_from != NULL) { + convert_to_long_ex(arg_from); + n = Z_LVAL_PP(arg_from); + if (n >= buffer1->length) { + PHP_MB_ERRSET(PHP_MB_ERR_INDEX_OUT_OF_BOUNDS); + php_mb_error(); + RETURN_FALSE; + } + } + if (buffer1->value == NULL || buffer1->length == 0 || + buffer2->value == NULL || buffer2->length == 0 || + buffer1->length < buffer2->length) { + RETVAL_FALSE; + } else { + p = &buffer1->value[n]; + end = &p[buffer1->length - buffer2->length]; + search = buffer2->value; + len = buffer2->length; + n = 0; + while (p <= end) { + if (*p == *search) { + p1 = p; + p2 = search; + i = 0; + while (*p1 == *p2 && i < len) { + i++; + if (i == len) { + RETVAL_LONG(n); + goto end; + } + p1++; + p2++; + } + } + p++; + n++; + } + RETVAL_FALSE; + } +end: + if (tmpbuf != NULL) { + php_mb_buf_free(tmpbuf); + } +} +/* }}} */ + +/* {{{ proto int mb_buf_last_index_of(object buffer, mixed search [, int fromindex]) + */ +PHP_FUNCTION(mb_buf_last_index_of) +{ + zval **arg_obj, **arg_search, **arg_from, *object, **objid; + int type, n, i, len, *p, *p1, *p2, *end, *search; + php_mb_buf *buffer1, *buffer2, *tmpbuf; + + PHP_MB_ERRRST(); + arg_obj = NULL; + arg_from = NULL; + object = getThis(); + if (object != NULL) { + if ((ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg_search) == FAILURE) && + (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &arg_search, &arg_from) == FAILURE)) { + WRONG_PARAM_COUNT; + } + } else { + if ((ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &arg_obj, &arg_search) == FAILURE) && + (ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &arg_obj, &arg_search, &arg_from) == FAILURE)) { + WRONG_PARAM_COUNT; + } + } + + objid = NULL; + type = 0; + buffer1 = NULL; + buffer2 = NULL; + tmpbuf = NULL; + PHP_MB_CHECK_OBJECT(object, arg_obj); + PHP_MB_FETCH_BUFFER(object, objid, type, buffer1); + switch (Z_TYPE_PP(arg_search)) { + case IS_OBJECT: + objid = NULL; + type = 0; + if (zend_hash_index_find(Z_OBJPROP_PP(arg_search), 0, (void **) &objid) == SUCCESS) { + buffer2 = zend_list_find(Z_RESVAL_PP(objid), &type); + } + if (type == le_buffer) { + break; + } + /* fall through */ + default: + convert_to_string_ex(arg_search); + tmpbuf = php_mb_convert_internal_to_wchar(Z_STRVAL_PP(arg_search), Z_STRLEN_PP(arg_search)); + buffer2 = tmpbuf; + break; + } + if (buffer2 == NULL) { + php_mb_error(); + RETURN_FALSE; + } + n = 0; + if (arg_from != NULL) { + convert_to_long_ex(arg_from); + n = Z_LVAL_PP(arg_from); + if (n >= buffer1->length) { + PHP_MB_ERRSET(PHP_MB_ERR_INDEX_OUT_OF_BOUNDS); + php_mb_error(); + RETURN_FALSE; + } + } + if (buffer1->value == NULL || buffer1->length == 0 || + buffer2->value == NULL || buffer2->length == 0 || + buffer1->length < buffer2->length) { + RETVAL_FALSE; + } else { + p = &p[buffer1->length - buffer2->length]; + end = &buffer1->value[n]; + search = buffer2->value; + len = buffer2->length; + n = buffer1->length - buffer2->length; + while (p >= end) { + if (*p == *search) { + p1 = p; + p2 = search; + i = 0; + while (*p1 == *p2 && i < len) { + i++; + if (i == len) { + RETVAL_LONG(n); + goto end; + } + p1++; + p2++; + } + } + p--; + n--; + } + RETVAL_FALSE; + } +end: + if (tmpbuf != NULL) { + php_mb_buf_free(tmpbuf); } } /* }}} */ 1.6 +6 -6 php4/ext/mbstring/mbstring.h Index: mbstring.h =================================================================== RCS file: /cvsroot/php-i18n/php4/ext/mbstring/mbstring.h,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- mbstring.h 30 May 2002 18:49:53 -0000 1.5 +++ mbstring.h 2 Jun 2002 16:30:37 -0000 1.6 @@ -106,15 +106,18 @@ PHP_FUNCTION(mb_iter_free); PHP_FUNCTION(mb_iter_set_index); PHP_FUNCTION(mb_iter_get_index); -PHP_FUNCTION(mb_iter_get_byte_index); -PHP_FUNCTION(mb_iter_get_begin_index); -PHP_FUNCTION(mb_iter_get_end_index); +PHP_FUNCTION(mb_iter_current_byte); +PHP_FUNCTION(mb_iter_next_byte); +PHP_FUNCTION(mb_iter_begin_index); +PHP_FUNCTION(mb_iter_end_index); PHP_FUNCTION(mb_iter_first); PHP_FUNCTION(mb_iter_current); PHP_FUNCTION(mb_iter_next); PHP_FUNCTION(mb_buf_create); PHP_FUNCTION(mb_buf_free); +PHP_FUNCTION(mb_buf_capacity); +PHP_FUNCTION(mb_buf_ensure_capacity); PHP_FUNCTION(mb_buf_sleep); PHP_FUNCTION(mb_buf_wakeup); PHP_FUNCTION(mb_buf_to_string); @@ -138,9 +141,6 @@ PHP_FUNCTION(mb_buf_length); PHP_FUNCTION(mb_buf_index_of); PHP_FUNCTION(mb_buf_last_index_of); -PHP_FUNCTION(mb_buf_reg_search); -PHP_FUNCTION(mb_buf_reg_replace_first); -PHP_FUNCTION(mb_buf_reg_replace_all); PHP_FUNCTION(mb_filter_create); PHP_FUNCTION(mb_filter_free); 1.4 +3 -1 php4/ext/mbstring/php_mb_buf.h Index: php_mb_buf.h =================================================================== RCS file: /cvsroot/php-i18n/php4/ext/mbstring/php_mb_buf.h,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- php_mb_buf.h 26 May 2002 06:12:27 -0000 1.3 +++ php_mb_buf.h 2 Jun 2002 16:30:37 -0000 1.4 @@ -15,7 +15,7 @@ void *global_resource; int *value; size_t length; - size_t size; + size_t capacity; size_t realloc; int refcount; char memory_type_value; @@ -28,6 +28,7 @@ PHPAPI void php_mb_buf_reset(php_mb_buf *buffer); PHPAPI void php_mb_buf_unput(php_mb_buf *buffer); PHPAPI void php_mb_buf_adjust_realloc(php_mb_buf *buffer, size_t length); +PHPAPI int _php_mb_buf_allocate(php_mb_buf *buffer, size_t size TSRMLS_DC); PHPAPI int _php_mb_buf_append_char(php_mb_buf *buffer, int c TSRMLS_DC); PHPAPI int _php_mb_buf_append_wstr(php_mb_buf *dst, const int *src, size_t length TSRMLS_DC); PHPAPI int _php_mb_buf_concat(php_mb_buf *dst, php_mb_buf *src TSRMLS_DC); @@ -42,6 +43,7 @@ #define php_mb_buf_create() _php_mb_buf_create(0 TSRMLS_CC) #define php_mb_buf_create_persist() _php_mb_buf_create(1 TSRMLS_CC) #define php_mb_buf_free(buffer) _php_mb_buf_free(buffer TSRMLS_CC) +#define php_mb_buf_allocate(buffer, size) _php_mb_buf_allocate(buffer, size TSRMLS_CC) #define php_mb_buf_append_char(buffer, c) _php_mb_buf_append_char(buffer, c TSRMLS_CC) #define php_mb_buf_append_wstr(dst, src, length) _php_mb_buf_append_wstr(dst, src, length TSRMLS_CC) #define php_mb_buf_concat(dst, src) _php_mb_buf_concat(dst, src TSRMLS_CC) 1.4 +1 -1 php4/ext/mbstring/php_mb_str.h Index: php_mb_str.h =================================================================== RCS file: /cvsroot/php-i18n/php4/ext/mbstring/php_mb_str.h,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- php_mb_str.h 26 May 2002 06:15:07 -0000 1.3 +++ php_mb_str.h 2 Jun 2002 16:30:37 -0000 1.4 @@ -16,7 +16,7 @@ php_mb_enc *encoding; unsigned char *value; size_t length; - size_t size; + size_t capacity; size_t realloc; int refcount; char memory_type_value; 1.6 +17 -17 php4/ext/mbstring/string.c Index: string.c =================================================================== RCS file: /cvsroot/php-i18n/php4/ext/mbstring/string.c,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- string.c 26 May 2002 06:15:25 -0000 1.5 +++ string.c 2 Jun 2002 16:30:37 -0000 1.6 @@ -24,7 +24,7 @@ string->encoding = MBSTRG(internal_encoding_r); string->value = NULL; string->length = 0; - string->size = 0; + string->capacity = 0; string->realloc = PHP_MB_STR_ALLOC_SIZE; string->refcount = 1; if (persist) { @@ -99,7 +99,7 @@ PHP_MB_ERRSET(PHP_MB_ERR_ILLEGAL_ARGUMENT); return FAILURE; } - if (string->size >= size) { + if (string->capacity >= size) { return SUCCESS; } if (string->memory_type_value == PHP_MB_MEMORY_TYPE_CONST) { @@ -117,7 +117,7 @@ PHP_MB_ERRSET(PHP_MB_ERR_NO_MEMORY); return FAILURE; } - string->size = size; + string->capacity = size; string->value = tmp; return SUCCESS; } @@ -138,14 +138,14 @@ len = 1; } - if ((string->length + len) > string->size) { + if ((string->length + len) > string->capacity) { size_t newsize; /* reallocate buffer */ if (string->memory_type_value == PHP_MB_MEMORY_TYPE_CONST) { PHP_MB_ERRSET(PHP_MB_ERR_BUFFER_OVER_FLOW); - if (string->value != NULL && string->size > 0) { - string->value[string->size - 1] = '\0'; + if (string->value != NULL && string->capacity > 0) { + string->value[string->capacity - 1] = '\0'; } return FAILURE; } @@ -158,12 +158,12 @@ } if (p == NULL) { PHP_MB_ERRSET(PHP_MB_ERR_NO_MEMORY); - if (string->value != NULL && string->size > 0) { - string->value[string->size - 1] = '\0'; + if (string->value != NULL && string->capacity > 0) { + string->value[string->capacity - 1] = '\0'; } return FAILURE; } - string->size = newsize; + string->capacity = newsize; string->value = p; } @@ -184,7 +184,7 @@ return FAILURE; } - if ((string->length + length) >= string->size) { + if ((string->length + length) >= string->capacity) { /* reallocate buffer */ size_t newsize; unsigned char *tmp; @@ -204,7 +204,7 @@ PHP_MB_ERRSET(PHP_MB_ERR_NO_MEMORY); return FAILURE; } - string->size = newsize; + string->capacity = newsize; string->value = tmp; } @@ -217,7 +217,7 @@ PHPAPI int _php_mb_str_concat(php_mb_str *dst, php_mb_str *src TSRMLS_DC) { - if ((dst->length + src->length) >= dst->size) { + if ((dst->length + src->length) >= dst->capacity) { /* reallocate buffer */ size_t newsize; unsigned char *tmp; @@ -237,7 +237,7 @@ PHP_MB_ERRSET(PHP_MB_ERR_NO_MEMORY); return FAILURE; } - dst->size = newsize; + dst->capacity = newsize; dst->value = tmp; } @@ -264,7 +264,7 @@ } string->value = NULL; string->length = 0; - string->size = 0; + string->capacity = 0; } PHPAPI int @@ -287,7 +287,7 @@ { php_mb_str *string = (php_mb_str *)stream_resource; - if (string->length >= string->size) { + if (string->length >= string->capacity) { /* reallocate buffer */ size_t newsize; unsigned char *tmp; @@ -297,7 +297,7 @@ PHP_MB_ERRSET(PHP_MB_ERR_BUFFER_OVER_FLOW); return -1; } - newsize = string->size + string->realloc; + newsize = string->capacity + string->realloc; tmp = NULL; if (string->memory_type_value == PHP_MB_MEMORY_TYPE_SALLOC) { tmp = realloc(string->value, newsize*sizeof(unsigned char)); @@ -308,7 +308,7 @@ PHP_MB_ERRSET(PHP_MB_ERR_NO_MEMORY); return -1; } - string->size = newsize; + string->capacity = newsize; string->value = tmp; }