From d55074ede49252a052bc40007ffdada6367058e0 Mon Sep 17 00:00:00 2001 From: Gina Peter Bnayard Date: Sat, 17 Aug 2024 00:24:53 +0200 Subject: [PATCH] ext/phar: Use zend_string instead of char* len pair --- ext/phar/phar_object.c | 211 ++++++++++++++++++++--------------------- 1 file changed, 105 insertions(+), 106 deletions(-) diff --git a/ext/phar/phar_object.c b/ext/phar/phar_object.c index 1196d2e419e..5232649d7fe 100644 --- a/ext/phar/phar_object.c +++ b/ext/phar/phar_object.c @@ -2600,12 +2600,11 @@ PHP_METHOD(Phar, isWritable) /* {{{ Deletes a named file within the archive. */ PHP_METHOD(Phar, delete) { - char *fname; - size_t fname_len; + zend_string *file_name; char *error; phar_entry_info *entry; - if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &fname, &fname_len) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "P", &file_name) == FAILURE) { RETURN_THROWS(); } @@ -2621,7 +2620,7 @@ PHP_METHOD(Phar, delete) zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar_obj->archive->fname); RETURN_THROWS(); } - if (NULL != (entry = zend_hash_str_find_ptr(&phar_obj->archive->manifest, fname, fname_len))) { + if (NULL != (entry = zend_hash_find_ptr(&phar_obj->archive->manifest, file_name))) { if (entry->is_deleted) { /* entry is deleted, but has not been flushed to disk yet */ RETURN_TRUE; @@ -2631,7 +2630,7 @@ PHP_METHOD(Phar, delete) phar_obj->archive->is_modified = 1; } } else { - zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Entry %s does not exist and cannot be deleted", fname); + zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Entry %s does not exist and cannot be deleted", ZSTR_VAL(file_name)); RETURN_THROWS(); } @@ -2679,12 +2678,13 @@ PHP_METHOD(Phar, getPath) */ PHP_METHOD(Phar, setAlias) { - char *alias, *error, *oldalias; + zend_string *new_alias = NULL; + char *error, *oldalias; phar_archive_data *fd_ptr; - size_t alias_len, oldalias_len; + size_t oldalias_len; int old_temp, readd = 0; - if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &alias, &alias_len) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &new_alias) == FAILURE) { RETURN_THROWS(); } @@ -2711,12 +2711,12 @@ PHP_METHOD(Phar, setAlias) RETURN_THROWS(); } - if (alias_len == phar_obj->archive->alias_len && memcmp(phar_obj->archive->alias, alias, alias_len) == 0) { + if (zend_string_equals_cstr(new_alias, phar_obj->archive->alias, phar_obj->archive->alias_len)) { RETURN_TRUE; } - if (alias_len && NULL != (fd_ptr = zend_hash_str_find_ptr(&(PHAR_G(phar_alias_map)), alias, alias_len))) { - spprintf(&error, 0, "alias \"%s\" is already used for archive \"%s\" and cannot be used for other archives", alias, fd_ptr->fname); - if (SUCCESS == phar_free_alias(fd_ptr, alias, alias_len)) { + if (NULL != (fd_ptr = zend_hash_find_ptr(&(PHAR_G(phar_alias_map)), new_alias))) { + spprintf(&error, 0, "alias \"%s\" is already used for archive \"%s\" and cannot be used for other archives", ZSTR_VAL(new_alias), fd_ptr->fname); + if (SUCCESS == phar_free_alias(fd_ptr, ZSTR_VAL(new_alias), ZSTR_LEN(new_alias))) { efree(error); goto valid_alias; } @@ -2724,9 +2724,9 @@ PHP_METHOD(Phar, setAlias) efree(error); RETURN_THROWS(); } - if (!phar_validate_alias(alias, alias_len)) { + if (!phar_validate_alias(ZSTR_VAL(new_alias), ZSTR_LEN(new_alias))) { zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, - "Invalid alias \"%s\" specified for phar \"%s\"", alias, phar_obj->archive->fname); + "Invalid alias \"%s\" specified for phar \"%s\"", ZSTR_VAL(new_alias), phar_obj->archive->fname); RETURN_THROWS(); } valid_alias: @@ -2743,13 +2743,8 @@ valid_alias: oldalias_len = phar_obj->archive->alias_len; old_temp = phar_obj->archive->is_temporary_alias; - if (alias_len) { - phar_obj->archive->alias = estrndup(alias, alias_len); - } else { - phar_obj->archive->alias = NULL; - } - - phar_obj->archive->alias_len = alias_len; + phar_obj->archive->alias = estrndup(ZSTR_VAL(new_alias), ZSTR_LEN(new_alias)); + phar_obj->archive->alias_len = ZSTR_LEN(new_alias); phar_obj->archive->is_temporary_alias = 0; phar_flush(phar_obj->archive, NULL, 0, 0, &error); @@ -2765,7 +2760,7 @@ valid_alias: RETURN_THROWS(); } - zend_hash_str_add_ptr(&(PHAR_G(phar_alias_map)), alias, alias_len, phar_obj->archive); + zend_hash_add_ptr(&(PHAR_G(phar_alias_map)), new_alias, phar_obj->archive); if (oldalias) { efree(oldalias); @@ -3408,13 +3403,13 @@ PHP_METHOD(Phar, decompressFiles) /* {{{ copy a file internal to the phar archive to another new file within the phar */ PHP_METHOD(Phar, copy) { - char *oldfile, *newfile, *error; + char *error; const char *pcr_error; - size_t oldfile_len, newfile_len; phar_entry_info *oldentry, newentry = {0}, *temp; - size_t tmp_len = 0; + zend_string *new_file = NULL; + zend_string *old_file = NULL; - if (zend_parse_parameters(ZEND_NUM_ARGS(), "pp", &oldfile, &oldfile_len, &newfile, &newfile_len) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "PP", &old_file, &new_file) == FAILURE) { RETURN_THROWS(); } @@ -3422,43 +3417,43 @@ PHP_METHOD(Phar, copy) if (PHAR_G(readonly) && !phar_obj->archive->is_data) { zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, - "Cannot copy \"%s\" to \"%s\", phar is read-only", oldfile, newfile); + "Cannot copy \"%s\" to \"%s\", phar is read-only", ZSTR_VAL(old_file), ZSTR_VAL(new_file)); RETURN_THROWS(); } - if (oldfile_len >= sizeof(".phar")-1 && !memcmp(oldfile, ".phar", sizeof(".phar")-1)) { + if (zend_string_starts_with_literal(old_file, ".phar")) { /* can't copy a meta file */ zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, - "file \"%s\" cannot be copied to file \"%s\", cannot copy Phar meta-file in %s", oldfile, newfile, phar_obj->archive->fname); + "file \"%s\" cannot be copied to file \"%s\", cannot copy Phar meta-file in %s", ZSTR_VAL(old_file), ZSTR_VAL(new_file), phar_obj->archive->fname); RETURN_THROWS(); } - if (newfile_len >= sizeof(".phar")-1 && !memcmp(newfile, ".phar", sizeof(".phar")-1)) { + if (zend_string_starts_with_literal(new_file, ".phar")) { /* can't copy a meta file */ zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, - "file \"%s\" cannot be copied to file \"%s\", cannot copy to Phar meta-file in %s", oldfile, newfile, phar_obj->archive->fname); + "file \"%s\" cannot be copied to file \"%s\", cannot copy to Phar meta-file in %s", ZSTR_VAL(old_file), ZSTR_VAL(new_file), phar_obj->archive->fname); RETURN_THROWS(); } - if (NULL == (oldentry = zend_hash_str_find_ptr(&phar_obj->archive->manifest, oldfile, oldfile_len)) || oldentry->is_deleted) { + if (NULL == (oldentry = zend_hash_find_ptr(&phar_obj->archive->manifest, old_file)) || oldentry->is_deleted) { zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, - "file \"%s\" cannot be copied to file \"%s\", file does not exist in %s", oldfile, newfile, phar_obj->archive->fname); + "file \"%s\" cannot be copied to file \"%s\", file does not exist in %s", ZSTR_VAL(old_file), ZSTR_VAL(new_file), phar_obj->archive->fname); RETURN_THROWS(); } - if (NULL != (temp = zend_hash_str_find_ptr(&phar_obj->archive->manifest, newfile, newfile_len)) && !temp->is_deleted) { + if (NULL != (temp = zend_hash_find_ptr(&phar_obj->archive->manifest, new_file)) && !temp->is_deleted) { zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, - "file \"%s\" cannot be copied to file \"%s\", file must not already exist in phar %s", oldfile, newfile, phar_obj->archive->fname); + "file \"%s\" cannot be copied to file \"%s\", file must not already exist in phar %s", ZSTR_VAL(old_file), ZSTR_VAL(new_file), phar_obj->archive->fname); RETURN_THROWS(); } - tmp_len = newfile_len; - if (phar_path_check(&newfile, &tmp_len, &pcr_error) > pcr_is_ok) { + size_t tmp_len = ZSTR_LEN(new_file); + char *tmp_new_file = ZSTR_VAL(new_file); + if (phar_path_check(&tmp_new_file, &tmp_len, &pcr_error) > pcr_is_ok) { zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, - "file \"%s\" contains invalid characters %s, cannot be copied from \"%s\" in phar %s", newfile, pcr_error, oldfile, phar_obj->archive->fname); + "file \"%s\" contains invalid characters %s, cannot be copied from \"%s\" in phar %s", ZSTR_VAL(new_file), pcr_error, ZSTR_VAL(old_file), phar_obj->archive->fname); RETURN_THROWS(); } - newfile_len = tmp_len; if (phar_obj->archive->is_persistent) { if (FAILURE == phar_copy_on_write(&(phar_obj->archive))) { @@ -3466,15 +3461,15 @@ PHP_METHOD(Phar, copy) RETURN_THROWS(); } /* re-populate with copied-on-write entry */ - oldentry = zend_hash_str_find_ptr(&phar_obj->archive->manifest, oldfile, oldfile_len); + oldentry = zend_hash_find_ptr(&phar_obj->archive->manifest, old_file); } memcpy((void *) &newentry, oldentry, sizeof(phar_entry_info)); phar_metadata_tracker_clone(&newentry.metadata_tracker); - newentry.filename = estrndup(newfile, newfile_len); - newentry.filename_len = newfile_len; + newentry.filename = estrndup(tmp_new_file, tmp_len); + newentry.filename_len = tmp_len; newentry.fp_refcount = 0; if (oldentry->fp_type != PHAR_FP) { @@ -3487,7 +3482,7 @@ PHP_METHOD(Phar, copy) } } - zend_hash_str_add_mem(&oldentry->phar->manifest, newfile, newfile_len, &newentry, sizeof(phar_entry_info)); + zend_hash_str_add_mem(&oldentry->phar->manifest, ZSTR_VAL(new_file), tmp_len, &newentry, sizeof(phar_entry_info)); phar_obj->archive->is_modified = 1; phar_flush(phar_obj->archive, 0, 0, 0, &error); @@ -3503,31 +3498,30 @@ PHP_METHOD(Phar, copy) /* {{{ determines whether a file exists in the phar */ PHP_METHOD(Phar, offsetExists) { - char *fname; - size_t fname_len; + zend_string *file_name; phar_entry_info *entry; - if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &fname, &fname_len) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "P", &file_name) == FAILURE) { RETURN_THROWS(); } PHAR_ARCHIVE_OBJECT(); - if (zend_hash_str_exists(&phar_obj->archive->manifest, fname, fname_len)) { - if (NULL != (entry = zend_hash_str_find_ptr(&phar_obj->archive->manifest, fname, fname_len))) { + if (zend_hash_exists(&phar_obj->archive->manifest, file_name)) { + if (NULL != (entry = zend_hash_find_ptr(&phar_obj->archive->manifest, file_name))) { if (entry->is_deleted) { /* entry is deleted, but has not been flushed to disk yet */ RETURN_FALSE; } } - if (fname_len >= sizeof(".phar")-1 && !memcmp(fname, ".phar", sizeof(".phar")-1)) { + if (zend_string_starts_with_literal(file_name, ".phar")) { /* none of these are real files, so they don't exist */ RETURN_FALSE; } RETURN_TRUE; } else { - RETURN_BOOL(zend_hash_str_exists(&phar_obj->archive->virtual_dirs, fname, fname_len)); + RETURN_BOOL(zend_hash_exists(&phar_obj->archive->virtual_dirs, file_name)); } } /* }}} */ @@ -3535,31 +3529,31 @@ PHP_METHOD(Phar, offsetExists) /* {{{ get a PharFileInfo object for a specific file */ PHP_METHOD(Phar, offsetGet) { - char *fname, *error; - size_t fname_len; + char *error; phar_entry_info *entry; + zend_string *file_name = NULL; - if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &fname, &fname_len) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "P", &file_name) == FAILURE) { RETURN_THROWS(); } PHAR_ARCHIVE_OBJECT(); /* security is 0 here so that we can get a better error message than "entry doesn't exist" */ - if (!(entry = phar_get_entry_info_dir(phar_obj->archive, fname, fname_len, 1, &error, 0))) { - zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Entry %s does not exist%s%s", fname, error?", ":"", error?error:""); + if (!(entry = phar_get_entry_info_dir(phar_obj->archive, ZSTR_VAL(file_name), ZSTR_LEN(file_name), 1, &error, 0))) { + zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Entry %s does not exist%s%s", ZSTR_VAL(file_name), error?", ":"", error?error:""); } else { - if (fname_len == sizeof(".phar/stub.php")-1 && !memcmp(fname, ".phar/stub.php", sizeof(".phar/stub.php")-1)) { + if (zend_string_equals_literal(file_name, ".phar/stub.php")) { zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot get stub \".phar/stub.php\" directly in phar \"%s\", use getStub", phar_obj->archive->fname); RETURN_THROWS(); } - if (fname_len == sizeof(".phar/alias.txt")-1 && !memcmp(fname, ".phar/alias.txt", sizeof(".phar/alias.txt")-1)) { + if (zend_string_equals_literal(file_name, ".phar/alias.txt")) { zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot get alias \".phar/alias.txt\" directly in phar \"%s\", use getAlias", phar_obj->archive->fname); RETURN_THROWS(); } - if (fname_len >= sizeof(".phar")-1 && !memcmp(fname, ".phar", sizeof(".phar")-1)) { + if (zend_string_starts_with_literal(file_name, ".phar")) { zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot directly get any files or directories in magic \".phar\" directory"); RETURN_THROWS(); } @@ -3569,7 +3563,7 @@ PHP_METHOD(Phar, offsetGet) efree(entry); } - zend_string *sfname = strpprintf(0, "phar://%s/%s", phar_obj->archive->fname, fname); + zend_string *sfname = strpprintf(0, "phar://%s/%s", phar_obj->archive->fname, ZSTR_VAL(file_name)); zval zfname; ZVAL_NEW_STR(&zfname, sfname); @@ -3584,11 +3578,9 @@ PHP_METHOD(Phar, offsetGet) /* }}} */ /* {{{ add a file within the phar archive from a string or resource */ -static void phar_add_file(phar_archive_data **pphar, char *filename, size_t filename_len, char *cont_str, size_t cont_len, zval *zresource) +static void phar_add_file(phar_archive_data **pphar, zend_string *file_name, const zend_string *content, zval *zresource) { - size_t start_pos=0; char *error; - size_t contents_len; phar_entry_data *data; php_stream *contents_file = NULL; php_stream_statbuf ssb; @@ -3597,14 +3589,21 @@ static void phar_add_file(phar_archive_data **pphar, char *filename, size_t file ALLOCA_FLAG(filename_use_heap) #endif - if (filename_len >= sizeof(".phar")-1) { - start_pos = '/' == filename[0]; /* account for any leading slash: multiple-leads handled elsewhere */ - if (!memcmp(&filename[start_pos], ".phar", sizeof(".phar")-1) && (filename[start_pos+5] == '/' || filename[start_pos+5] == '\\' || filename[start_pos+5] == '\0')) { + if ( + zend_string_starts_with_literal(file_name, ".phar") + || zend_string_starts_with_literal(file_name, "/.phar") + ) { + size_t prefix_len = (ZSTR_VAL(file_name)[0] == '/') + sizeof(".phar")-1; + char next_char = ZSTR_VAL(file_name)[prefix_len]; + if (next_char == '/' || next_char == '\\' || next_char == '\0') { zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot create any files in magic \".phar\" directory"); return; } } + /* TODO How to handle Windows path normalisation with zend_string ? */ + char *filename = ZSTR_VAL(file_name); + size_t filename_len = ZSTR_LEN(file_name); #ifdef PHP_WIN32 save_filename = filename; if (memchr(filename, '\\', filename_len)) { @@ -3628,9 +3627,11 @@ static void phar_add_file(phar_archive_data **pphar, char *filename, size_t file } if (!data->internal_file->is_dir) { - if (cont_str) { - contents_len = php_stream_write(data->fp, cont_str, cont_len); - if (contents_len != cont_len) { + size_t contents_len = 0; + if (content) { + contents_len = ZSTR_LEN(content); + size_t written_len = php_stream_write(data->fp, ZSTR_VAL(content), ZSTR_LEN(content)); + if (written_len != contents_len) { zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Entry %s could not be written to", filename); goto finish; } @@ -3679,17 +3680,17 @@ finish: ; /* }}} */ /* {{{ create a directory within the phar archive */ -static void phar_mkdir(phar_archive_data **pphar, char *dirname, size_t dirname_len) +static void phar_mkdir(phar_archive_data **pphar, zend_string *dir_name) { char *error; phar_entry_data *data; - if (!(data = phar_get_or_create_entry_data((*pphar)->fname, (*pphar)->fname_len, dirname, dirname_len, "w+b", 2, &error, 1))) { + if (!(data = phar_get_or_create_entry_data((*pphar)->fname, (*pphar)->fname_len, ZSTR_VAL(dir_name), ZSTR_LEN(dir_name), "w+b", 2, &error, 1))) { if (error) { - zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Directory %s does not exist and cannot be created: %s", dirname, error); + zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Directory %s does not exist and cannot be created: %s", ZSTR_VAL(dir_name), error); efree(error); } else { - zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Directory %s does not exist and cannot be created", dirname); + zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Directory %s does not exist and cannot be created", ZSTR_VAL(dir_name)); } return; @@ -3716,12 +3717,12 @@ static void phar_mkdir(phar_archive_data **pphar, char *dirname, size_t dirname_ /* {{{ set the contents of an internal file to those of an external file */ PHP_METHOD(Phar, offsetSet) { - char *fname, *cont_str = NULL; - size_t fname_len, cont_len; zval *zresource = NULL; + zend_string *file_name = NULL; + zend_string *file_content = NULL; - if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "pr", &fname, &fname_len, &zresource) == FAILURE - && zend_parse_parameters(ZEND_NUM_ARGS(), "ps", &fname, &fname_len, &cont_str, &cont_len) == FAILURE) { + if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "Pr", &file_name, &zresource) == FAILURE + && zend_parse_parameters(ZEND_NUM_ARGS(), "PS", &file_name, &file_content) == FAILURE) { RETURN_THROWS(); } @@ -3732,33 +3733,33 @@ PHP_METHOD(Phar, offsetSet) RETURN_THROWS(); } - if (fname_len == sizeof(".phar/stub.php")-1 && !memcmp(fname, ".phar/stub.php", sizeof(".phar/stub.php")-1)) { + if (zend_string_equals_literal(file_name, ".phar/stub.php")) { zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot set stub \".phar/stub.php\" directly in phar \"%s\", use setStub", phar_obj->archive->fname); RETURN_THROWS(); } - if (fname_len == sizeof(".phar/alias.txt")-1 && !memcmp(fname, ".phar/alias.txt", sizeof(".phar/alias.txt")-1)) { + if (zend_string_equals_literal(file_name, ".phar/alias.txt")) { zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot set alias \".phar/alias.txt\" directly in phar \"%s\", use setAlias", phar_obj->archive->fname); RETURN_THROWS(); } - if (fname_len >= sizeof(".phar")-1 && !memcmp(fname, ".phar", sizeof(".phar")-1)) { + if (zend_string_starts_with_literal(file_name, ".phar")) { zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot set any files or directories in magic \".phar\" directory"); RETURN_THROWS(); } - phar_add_file(&(phar_obj->archive), fname, fname_len, cont_str, cont_len, zresource); + phar_add_file(&(phar_obj->archive), file_name, file_content, zresource); } /* }}} */ /* {{{ remove a file from a phar */ PHP_METHOD(Phar, offsetUnset) { - char *fname, *error; - size_t fname_len; + char *error; + zend_string *file_name; phar_entry_info *entry; - if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &fname, &fname_len) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "P", &file_name) == FAILURE) { RETURN_THROWS(); } @@ -3769,8 +3770,8 @@ PHP_METHOD(Phar, offsetUnset) RETURN_THROWS(); } - if (zend_hash_str_exists(&phar_obj->archive->manifest, fname, fname_len)) { - if (NULL != (entry = zend_hash_str_find_ptr(&phar_obj->archive->manifest, fname, fname_len))) { + if (zend_hash_exists(&phar_obj->archive->manifest, file_name)) { + if (NULL != (entry = zend_hash_find_ptr(&phar_obj->archive->manifest, file_name))) { if (entry->is_deleted) { /* entry is deleted, but has not been flushed to disk yet */ return; @@ -3782,7 +3783,7 @@ PHP_METHOD(Phar, offsetUnset) RETURN_THROWS(); } /* re-populate entry after copy on write */ - entry = zend_hash_str_find_ptr(&phar_obj->archive->manifest, fname, fname_len); + entry = zend_hash_find_ptr(&phar_obj->archive->manifest, file_name); } entry->is_modified = 0; entry->is_deleted = 1; @@ -3801,55 +3802,53 @@ PHP_METHOD(Phar, offsetUnset) /* {{{ Adds an empty directory to the phar archive */ PHP_METHOD(Phar, addEmptyDir) { - char *dirname; - size_t dirname_len; + zend_string *dir_name; - if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &dirname, &dirname_len) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "P", &dir_name) == FAILURE) { RETURN_THROWS(); } PHAR_ARCHIVE_OBJECT(); - if (dirname_len >= sizeof(".phar")-1 && !memcmp(dirname, ".phar", sizeof(".phar")-1)) { + if (zend_string_starts_with_literal(dir_name, ".phar")) { zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot create a directory in magic \".phar\" directory"); RETURN_THROWS(); } - phar_mkdir(&phar_obj->archive, dirname, dirname_len); + phar_mkdir(&phar_obj->archive, dir_name); } /* }}} */ /* {{{ Adds a file to the archive using the filename, or the second parameter as the name within the archive */ PHP_METHOD(Phar, addFile) { - char *fname, *localname = NULL; - size_t fname_len, localname_len = 0; + zend_string *file_name; + zend_string *local_name = NULL; php_stream *resource; zval zresource; - if (zend_parse_parameters(ZEND_NUM_ARGS(), "p|s!", &fname, &fname_len, &localname, &localname_len) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "P|S!", &file_name, &local_name) == FAILURE) { RETURN_THROWS(); } PHAR_ARCHIVE_OBJECT(); - if (!strstr(fname, "://") && php_check_open_basedir(fname)) { - zend_throw_exception_ex(spl_ce_RuntimeException, 0, "phar error: unable to open file \"%s\" to add to phar archive, open_basedir restrictions prevent this", fname); + if (!strstr(ZSTR_VAL(file_name), "://") && php_check_open_basedir(ZSTR_VAL(file_name))) { + zend_throw_exception_ex(spl_ce_RuntimeException, 0, "phar error: unable to open file \"%s\" to add to phar archive, open_basedir restrictions prevent this", ZSTR_VAL(file_name)); RETURN_THROWS(); } - if (!(resource = php_stream_open_wrapper(fname, "rb", 0, NULL))) { - zend_throw_exception_ex(spl_ce_RuntimeException, 0, "phar error: unable to open file \"%s\" to add to phar archive", fname); + if (!(resource = php_stream_open_wrapper(ZSTR_VAL(file_name), "rb", 0, NULL))) { + zend_throw_exception_ex(spl_ce_RuntimeException, 0, "phar error: unable to open file \"%s\" to add to phar archive", ZSTR_VAL(file_name)); RETURN_THROWS(); } - if (localname) { - fname = localname; - fname_len = localname_len; + if (local_name) { + file_name = local_name; } php_stream_to_zval(resource, &zresource); - phar_add_file(&(phar_obj->archive), fname, fname_len, NULL, 0, &zresource); + phar_add_file(&(phar_obj->archive), file_name, NULL, &zresource); zval_ptr_dtor(&zresource); } /* }}} */ @@ -3857,16 +3856,16 @@ PHP_METHOD(Phar, addFile) /* {{{ Adds a file to the archive using its contents as a string */ PHP_METHOD(Phar, addFromString) { - char *localname, *cont_str; - size_t localname_len, cont_len; + zend_string *local_name = NULL; + zend_string *file_content = NULL; - if (zend_parse_parameters(ZEND_NUM_ARGS(), "ps", &localname, &localname_len, &cont_str, &cont_len) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "PS", &local_name, &file_content) == FAILURE) { RETURN_THROWS(); } PHAR_ARCHIVE_OBJECT(); - phar_add_file(&(phar_obj->archive), localname, localname_len, cont_str, cont_len, NULL); + phar_add_file(&(phar_obj->archive), local_name, file_content, NULL); } /* }}} */