diff options
author | Bram Moolenaar <Bram@vim.org> | 2010-06-21 06:15:46 +0200 |
---|---|---|
committer | Bram Moolenaar <Bram@vim.org> | 2010-06-21 06:15:46 +0200 |
commit | a8ffcbbf5d6070380e41b3d0841c3944396a27c0 (patch) | |
tree | b8608922fc9956be229912f1323b0d0a94de526c /src/fileio.c | |
parent | 191e0a2bc7cb4787e19aa1f8c6958b47e05d7882 (diff) | |
download | vim-git-a8ffcbbf5d6070380e41b3d0841c3944396a27c0.tar.gz |
Crypt the swapfile.
Diffstat (limited to 'src/fileio.c')
-rw-r--r-- | src/fileio.c | 43 |
1 files changed, 35 insertions, 8 deletions
diff --git a/src/fileio.c b/src/fileio.c index aad76170e..b09946167 100644 --- a/src/fileio.c +++ b/src/fileio.c @@ -64,7 +64,7 @@ static void check_marks_read __ARGS((void)); #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, filesizep, newfile, did_ask) 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, filesizep, newfile, did_ask) * 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, filesizep, newfile, did_ask) 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, filesizep, newfile, did_ask) /* 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) |