patch 9.0.0795: readblob() always reads the whole file

Problem:    readblob() always reads the whole file.
Solution:   Add arguments to read part of the file. (Ken Takata,
            closes #11402)
diff --git a/src/blob.c b/src/blob.c
index a4e9981..92efb67 100644
--- a/src/blob.c
+++ b/src/blob.c
@@ -182,22 +182,52 @@
 }
 
 /*
- * Read "blob" from file "fd".
+ * Read blob from file "fd".
+ * Caller has allocated a blob in "rettv".
  * Return OK or FAIL.
  */
     int
-read_blob(FILE *fd, blob_T *blob)
+read_blob(FILE *fd, typval_T *rettv, off_T offset, off_T size_arg)
 {
+    blob_T	*blob = rettv->vval.v_blob;
     struct stat	st;
+    int		whence;
+    off_T	size = size_arg;
 
     if (fstat(fileno(fd), &st) < 0)
+	return FAIL;  // can't read the file, error
+
+    if (offset >= 0)
+    {
+	if (size == -1)
+	    // size may become negative, checked below
+	    size = st.st_size - offset;
+	whence = SEEK_SET;
+    }
+    else
+    {
+	if (size == -1)
+	    size = -offset;
+	whence = SEEK_END;
+    }
+    // Trying to read bytes that aren't there results in an empty blob, not an
+    // error.
+    if (size < 0 || size > st.st_size)
+	return OK;
+    if (vim_fseek(fd, offset, whence) != 0)
+	return OK;
+
+    if (ga_grow(&blob->bv_ga, (int)size) == FAIL)
 	return FAIL;
-    if (ga_grow(&blob->bv_ga, st.st_size) == FAIL)
-	return FAIL;
-    blob->bv_ga.ga_len = st.st_size;
+    blob->bv_ga.ga_len = (int)size;
     if (fread(blob->bv_ga.ga_data, 1, blob->bv_ga.ga_len, fd)
 						  < (size_t)blob->bv_ga.ga_len)
+    {
+	// An empty blob is returned on error.
+	blob_free(rettv->vval.v_blob);
+	rettv->vval.v_blob = NULL;
 	return FAIL;
+    }
     return OK;
 }
 
diff --git a/src/evalfunc.c b/src/evalfunc.c
index b769c08..8943a30 100644
--- a/src/evalfunc.c
+++ b/src/evalfunc.c
@@ -1078,6 +1078,7 @@
 static argcheck_T arg3_string_any_string[] = {arg_string, NULL, arg_string};
 static argcheck_T arg3_string_bool_bool[] = {arg_string, arg_bool, arg_bool};
 static argcheck_T arg3_string_number_bool[] = {arg_string, arg_number, arg_bool};
+static argcheck_T arg3_string_number_number[] = {arg_string, arg_number, arg_number};
 static argcheck_T arg3_string_or_dict_bool_dict[] = {arg_string_or_dict_any, arg_bool, arg_dict_any};
 static argcheck_T arg3_string_string_bool[] = {arg_string, arg_string, arg_bool};
 static argcheck_T arg3_string_string_dict[] = {arg_string, arg_string, arg_dict_any};
@@ -2339,7 +2340,7 @@
 			ret_number,	    f_rand},
     {"range",		1, 3, FEARG_1,	    arg3_number,
 			ret_list_number,    f_range},
-    {"readblob",	1, 1, FEARG_1,	    arg1_string,
+    {"readblob",	1, 3, FEARG_1,	    arg3_string_number_number,
 			ret_blob,	    f_readblob},
     {"readdir",		1, 3, FEARG_1,	    arg3_string_any_dict,
 			ret_list_string,    f_readdir},
diff --git a/src/filepath.c b/src/filepath.c
index d9fe83a..023c732 100644
--- a/src/filepath.c
+++ b/src/filepath.c
@@ -1792,16 +1792,27 @@
     long	cnt	 = 0;
     char_u	*p;			// position in buf
     char_u	*start;			// start of current line
+    off_T	offset = 0;
+    off_T	size = -1;
 
     if (argvars[1].v_type != VAR_UNKNOWN)
     {
-	if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
-	    binary = TRUE;
-	if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
-	    blob = TRUE;
+	if (always_blob)
+	{
+	    offset = (off_T)tv_get_number(&argvars[1]);
+	    if (argvars[2].v_type != VAR_UNKNOWN)
+		size = (off_T)tv_get_number(&argvars[2]);
+	}
+	else
+	{
+	    if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
+		binary = TRUE;
+	    if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
+		blob = TRUE;
 
-	if (argvars[2].v_type != VAR_UNKNOWN)
-	    maxline = (long)tv_get_number(&argvars[2]);
+	    if (argvars[2].v_type != VAR_UNKNOWN)
+		maxline = (long)tv_get_number(&argvars[2]);
+	}
     }
 
     if ((blob ? rettv_blob_alloc(rettv) : rettv_list_alloc(rettv)) == FAIL)
@@ -1818,19 +1829,15 @@
     }
     if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
     {
-	semsg(_(e_cant_open_file_str), *fname == NUL ? (char_u *)_("<empty>") : fname);
+	semsg(_(e_cant_open_file_str),
+			       *fname == NUL ? (char_u *)_("<empty>") : fname);
 	return;
     }
 
     if (blob)
     {
-	if (read_blob(fd, rettv->vval.v_blob) == FAIL)
-	{
+	if (read_blob(fd, rettv, offset, size) == FAIL)
 	    semsg(_(e_cant_read_file_str), fname);
-	    // An empty blob is returned on error.
-	    blob_free(rettv->vval.v_blob);
-	    rettv->vval.v_blob = NULL;
-	}
 	fclose(fd);
 	return;
     }
@@ -2007,7 +2014,11 @@
     void
 f_readblob(typval_T *argvars, typval_T *rettv)
 {
-    if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
+    if (in_vim9script()
+	    && (check_for_string_arg(argvars, 0) == FAIL
+		|| check_for_opt_number_arg(argvars, 1) == FAIL
+		|| (argvars[1].v_type != VAR_UNKNOWN
+		    && check_for_opt_number_arg(argvars, 2) == FAIL)))
 	return;
 
     read_file_or_blob(argvars, rettv, TRUE);
diff --git a/src/proto/blob.pro b/src/proto/blob.pro
index 54e4a37..176f43e 100644
--- a/src/proto/blob.pro
+++ b/src/proto/blob.pro
@@ -10,7 +10,7 @@
 void blob_set(blob_T *blob, int idx, int byte);
 void blob_set_append(blob_T *blob, int idx, int byte);
 int blob_equal(blob_T *b1, blob_T *b2);
-int read_blob(FILE *fd, blob_T *blob);
+int read_blob(FILE *fd, typval_T *rettv, off_T offset, off_T size);
 int write_blob(FILE *fd, blob_T *blob);
 char_u *blob2string(blob_T *blob, char_u **tofree, char_u *numbuf);
 blob_T *string2blob(char_u *str);
diff --git a/src/testdir/test_blob.vim b/src/testdir/test_blob.vim
index ce8c3d0..a54cede 100644
--- a/src/testdir/test_blob.vim
+++ b/src/testdir/test_blob.vim
@@ -488,10 +488,29 @@
       call writefile(b, 'Xblob')
       VAR br = readfile('Xblob', 'B')
       call assert_equal(b, br)
+      VAR br2 = readblob('Xblob')
+      call assert_equal(b, br2)
+      VAR br3 = readblob('Xblob', 1)
+      call assert_equal(b[1 :], br3)
+      VAR br4 = readblob('Xblob', 1, 2)
+      call assert_equal(b[1 : 2], br4)
+      VAR br5 = readblob('Xblob', -3)
+      call assert_equal(b[-3 :], br5)
+      VAR br6 = readblob('Xblob', -3, 2)
+      call assert_equal(b[-3 : -2], br6)
+      
+      VAR br1e = readblob('Xblob', 10000)
+      call assert_equal(0z, br1e)
+      VAR br2e = readblob('Xblob', -10000)
+      call assert_equal(0z, br2e)
+
       call delete('Xblob')
   END
   call v9.CheckLegacyAndVim9Success(lines)
 
+  call assert_fails("call readblob('notexist')", 'E484:')
+  " TODO: How do we test for the E485 error?
+
   " This was crashing when calling readfile() with a directory.
   call assert_fails("call readfile('.', 'B')", 'E17: "." is a directory')
 endfunc
diff --git a/src/version.c b/src/version.c
index a143f0d..a5855b8 100644
--- a/src/version.c
+++ b/src/version.c
@@ -696,6 +696,8 @@
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    795,
+/**/
     794,
 /**/
     793,