diff src/fileio.c @ 2267:c08f91142c41 vim73

Crypt the swapfile.
author Bram Moolenaar <bram@vim.org>
date Mon, 21 Jun 2010 06:15:46 +0200
parents ae2e615a7320
children 941ff1cd317a
line wrap: on
line diff
--- a/src/fileio.c
+++ b/src/fileio.c
@@ -64,7 +64,7 @@ static void check_marks_read __ARGS((voi
 #endif
 #ifdef FEAT_CRYPT
 static int get_crypt_method __ARGS((char *ptr, int len));
-static char_u *check_for_cryptkey __ARGS((char_u *cryptkey, char_u *ptr, long *sizep, off_t *filesizep, int newfile, int *did_ask));
+static char_u *check_for_cryptkey __ARGS((char_u *cryptkey, char_u *ptr, long *sizep, off_t *filesizep, int newfile, char_u *fname, int *did_ask));
 #endif
 #ifdef UNIX
 static void set_file_time __ARGS((char_u *fname, time_t atime, time_t mtime));
@@ -995,6 +995,13 @@ retry:
 #endif
     }
 
+#ifdef FEAT_CRYPT
+    if (cryptkey != NULL)
+	/* Need to reset the state, but keep the key, don't want to ask for it
+	 * again. */
+	crypt_pop_state();
+#endif
+
     /*
      * When retrying with another "fenc" and the first time "fileformat"
      * will be reset.
@@ -1426,7 +1433,8 @@ retry:
 		 */
 		if (filesize == 0)
 		    cryptkey = check_for_cryptkey(cryptkey, ptr, &size,
-					&filesize, newfile, &did_ask_for_key);
+					&filesize, newfile, sfname,
+					&did_ask_for_key);
 		/*
 		 * Decrypt the read bytes.
 		 */
@@ -2277,8 +2285,14 @@ failed:
 	save_file_ff(curbuf);		/* remember the current file format */
 
 #ifdef FEAT_CRYPT
-    if (cryptkey != curbuf->b_p_key)
-	free_crypt_key(cryptkey);
+    if (cryptkey != NULL)
+    {
+	crypt_pop_state();
+	if (cryptkey != curbuf->b_p_key)
+	    free_crypt_key(cryptkey);
+	/* don't set cryptkey to NULL, it's used below as a flag that
+	 * encryption was used */
+    }
 #endif
 
 #ifdef FEAT_MBYTE
@@ -2869,12 +2883,13 @@ get_crypt_method(ptr, len)
  * Return the (new) encryption key, NULL for no encryption.
  */
     static char_u *
-check_for_cryptkey(cryptkey, ptr, sizep, filesizep, newfile, did_ask)
+check_for_cryptkey(cryptkey, ptr, sizep, filesizep, newfile, fname, did_ask)
     char_u	*cryptkey;	/* previous encryption key or NULL */
     char_u	*ptr;		/* pointer to read bytes */
     long	*sizep;		/* length of read bytes */
     off_t	*filesizep;	/* nr of bytes used from file */
     int		newfile;	/* editing a new buffer */
+    char_u	*fname;		/* file name to display */
     int		*did_ask;	/* flag: whether already asked for key */
 {
     int method = get_crypt_method((char *)ptr, *sizep);
@@ -2882,7 +2897,6 @@ check_for_cryptkey(cryptkey, ptr, sizep,
     if (method >= 0)
     {
 	curbuf->b_p_cm = method;
-	use_crypt_method = method;
 	if (method > 0)
 	    (void)blowfish_self_test();
 	if (cryptkey == NULL && !*did_ask)
@@ -2895,6 +2909,8 @@ check_for_cryptkey(cryptkey, ptr, sizep,
 		 * option and don't free it.  bf needs hash of the key saved.
 		 * Don't ask for the key again when first time Enter was hit.
 		 * Happens when retrying to detect encoding. */
+		smsg((char_u *)_(need_key_msg), fname);
+		msg_scroll = TRUE;
 		cryptkey = get_crypt_key(newfile, FALSE);
 		*did_ask = TRUE;
 
@@ -2913,6 +2929,8 @@ check_for_cryptkey(cryptkey, ptr, sizep,
 	    int seed_len = crypt_seed_len[method];
 	    int salt_len = crypt_salt_len[method];
 
+	    crypt_push_state();
+	    use_crypt_method = method;
 	    if (method == 0)
 		crypt_init_keys(cryptkey);
 	    else
@@ -2924,7 +2942,8 @@ check_for_cryptkey(cryptkey, ptr, sizep,
 	    /* Remove magic number from the text */
 	    *filesizep += CRYPT_MAGIC_LEN + salt_len + seed_len;
 	    *sizep -= CRYPT_MAGIC_LEN + salt_len + seed_len;
-	    mch_memmove(ptr, ptr + CRYPT_MAGIC_LEN + salt_len + seed_len, (size_t)*sizep);
+	    mch_memmove(ptr, ptr + CRYPT_MAGIC_LEN + salt_len + seed_len,
+							      (size_t)*sizep);
 	}
     }
     /* When starting to edit a new file which does not have encryption, clear
@@ -2956,6 +2975,7 @@ prepare_crypt_read(fp)
     if (method < 0 || method != curbuf->b_p_cm)
 	return FAIL;
 
+    crypt_push_state();
     if (method == 0)
 	crypt_init_keys(curbuf->b_p_key);
     else
@@ -2974,6 +2994,8 @@ prepare_crypt_read(fp)
 /*
  * Prepare for writing encrypted bytes for buffer "buf".
  * Returns a pointer to an allocated header of length "*lenp".
+ * When out of memory returns NULL.
+ * Otherwise calls crypt_push_state(), call crypt_pop_state() later.
  */
     char_u *
 prepare_crypt_write(buf, lenp)
@@ -2990,6 +3012,7 @@ prepare_crypt_write(buf, lenp)
 						    + CRYPT_SEED_LEN_MAX + 2);
     if (header != NULL)
     {
+	crypt_push_state();
 	use_crypt_method = buf->b_p_cm;  /* select pkzip or blowfish */
 	vim_strncpy(header, (char_u *)crypt_magic[use_crypt_method],
 							     CRYPT_MAGIC_LEN);
@@ -4404,7 +4427,7 @@ restore_backup:
     write_info.bw_fd = fd;
 
 #ifdef FEAT_CRYPT
-    if (*buf->b_p_key && !filtering)
+    if (*buf->b_p_key != NUL && !filtering)
     {
 	char_u *header;
 	int    header_len;
@@ -4674,6 +4697,10 @@ restore_backup:
     if (!backup_copy)
 	mch_set_acl(wfname, acl);
 #endif
+#ifdef FEAT_CRYPT
+    if (wb_flags & FIO_ENCRYPTED)
+	crypt_pop_state();
+#endif
 
 
 #if defined(FEAT_MBYTE) && defined(FEAT_EVAL)