mirror of
https://github.com/vim/vim.git
synced 2025-10-24 08:54:47 -04:00
patch 9.1.1774: cannot calculate sha256 of a Blob
Problem: cannot calculate sha256() of a Blob
Solution: Change sha256() to accept a Blob or String argument
(thinca).
closes: #18336
Signed-off-by: thinca <thinca@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
This commit is contained in:
committed by
Christian Brabandt
parent
e8b0e926d0
commit
4150283b83
@@ -1257,6 +1257,7 @@ static argcheck_T arg1_string[] = {arg_string};
|
||||
static argcheck_T arg1_string_or_list_any[] = {arg_string_or_list_any};
|
||||
static argcheck_T arg1_string_or_list_string[] = {arg_string_or_list_string};
|
||||
static argcheck_T arg1_string_or_nr[] = {arg_string_or_nr};
|
||||
static argcheck_T arg1_string_or_blob[] = {arg_string_or_blob};
|
||||
static argcheck_T arg2_any_buffer[] = {arg_any, arg_buffer};
|
||||
static argcheck_T arg2_buffer_any[] = {arg_buffer, arg_any};
|
||||
static argcheck_T arg2_buffer_bool[] = {arg_buffer, arg_bool};
|
||||
@@ -2842,7 +2843,7 @@ static const funcentry_T global_functions[] =
|
||||
ret_number_bool, f_settagstack},
|
||||
{"setwinvar", 3, 3, FEARG_3, arg3_number_string_any,
|
||||
ret_void, f_setwinvar},
|
||||
{"sha256", 1, 1, FEARG_1, arg1_string,
|
||||
{"sha256", 1, 1, FEARG_1, arg1_string_or_blob,
|
||||
ret_string,
|
||||
#ifdef FEAT_CRYPT
|
||||
f_sha256
|
||||
@@ -11742,20 +11743,36 @@ f_settagstack(typval_T *argvars, typval_T *rettv)
|
||||
|
||||
#ifdef FEAT_CRYPT
|
||||
/*
|
||||
* "sha256({string})" function
|
||||
* "sha256({expr})" function
|
||||
*/
|
||||
static void
|
||||
f_sha256(typval_T *argvars, typval_T *rettv)
|
||||
{
|
||||
char_u *p;
|
||||
int len;
|
||||
|
||||
if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
|
||||
if (in_vim9script() && check_for_string_or_blob_arg(argvars, 0) == FAIL)
|
||||
return;
|
||||
|
||||
p = tv_get_string(&argvars[0]);
|
||||
rettv->vval.v_string = vim_strsave(
|
||||
sha256_bytes(p, (int)STRLEN(p), NULL, 0));
|
||||
rettv->v_type = VAR_STRING;
|
||||
rettv->vval.v_string = NULL;
|
||||
|
||||
if (argvars[0].v_type == VAR_BLOB)
|
||||
{
|
||||
blob_T *blob = argvars[0].vval.v_blob;
|
||||
if (blob != NULL)
|
||||
{
|
||||
p = (char_u *)blob->bv_ga.ga_data;
|
||||
len = blob->bv_ga.ga_len;
|
||||
rettv->vval.v_string = vim_strsave(sha256_bytes(p, len, NULL, 0));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
p = tv_get_string(&argvars[0]);
|
||||
rettv->vval.v_string = vim_strsave(
|
||||
sha256_bytes(p, (int)STRLEN(p), NULL, 0));
|
||||
}
|
||||
}
|
||||
#endif // FEAT_CRYPT
|
||||
|
||||
|
||||
@@ -18,6 +18,16 @@ function Test_sha256()
|
||||
|
||||
" test for contains non-ascii char:
|
||||
call assert_equal('5f78c33274e43fa9de5659265c1d917e25c03722dcb0b8d27db8d5feaa813953', sha256("\xde\xad\xbe\xef"))
|
||||
|
||||
" test for blob:
|
||||
" empty blob
|
||||
call assert_equal('e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', sha256(0z))
|
||||
" blob with single byte
|
||||
call assert_equal('ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb', sha256(0z61))
|
||||
" blob with "abc"
|
||||
call assert_equal('ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad', sha256(0z616263))
|
||||
" blob with non-ascii bytes
|
||||
call assert_equal('5f78c33274e43fa9de5659265c1d917e25c03722dcb0b8d27db8d5feaa813953', sha256(0zdeadbeef))
|
||||
endfunction
|
||||
|
||||
" vim: shiftwidth=2 sts=2 expandtab
|
||||
|
||||
@@ -4162,10 +4162,14 @@ def Test_setwinvar()
|
||||
enddef
|
||||
|
||||
def Test_sha256()
|
||||
v9.CheckSourceDefAndScriptFailure(['sha256(100)'], ['E1013: Argument 1: type mismatch, expected string but got number', 'E1174: String required for argument 1'])
|
||||
v9.CheckSourceDefAndScriptFailure(['sha256(0zABCD)'], ['E1013: Argument 1: type mismatch, expected string but got blob', 'E1174: String required for argument 1'])
|
||||
v9.CheckSourceDefAndScriptFailure(['sha256(100)'], ['E1013: Argument 1: type mismatch, expected string but got number', 'E1221: String or Blob required for argument 1'])
|
||||
assert_equal('ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad', sha256('abc'))
|
||||
assert_equal('e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', sha256(''))
|
||||
|
||||
assert_equal('ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad', sha256(0z616263))
|
||||
assert_equal('e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', sha256(0z))
|
||||
assert_equal('ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb', sha256(0z61))
|
||||
assert_equal('5f78c33274e43fa9de5659265c1d917e25c03722dcb0b8d27db8d5feaa813953', sha256(0zdeadbeef))
|
||||
enddef
|
||||
|
||||
def Test_shiftwidth()
|
||||
|
||||
@@ -724,6 +724,8 @@ static char *(features[]) =
|
||||
|
||||
static int included_patches[] =
|
||||
{ /* Add new patch number below this line */
|
||||
/**/
|
||||
1774,
|
||||
/**/
|
||||
1773,
|
||||
/**/
|
||||
|
||||
Reference in New Issue
Block a user